|
| 1 | +//go:buid linux |
| 2 | + |
| 3 | +package fixer |
| 4 | + |
| 5 | +import ( |
| 6 | + "errors" |
| 7 | + "fmt" |
| 8 | + "io/fs" |
| 9 | + "os/user" |
| 10 | + "path/filepath" |
| 11 | + "strconv" |
| 12 | + "strings" |
| 13 | + |
| 14 | + "github.com/buildkite/elastic-ci-stack-for-aws/v6/internal/fixperms/fdfs" |
| 15 | +) |
| 16 | + |
| 17 | +// Main contains the higher-level operations of the permissions fixer. |
| 18 | +func Main(argv []string, baseDir, uname string) (string, int) { |
| 19 | + if len(argv) != 4 { |
| 20 | + return exitf(1, "Usage: %s AGENT_DIR ORG_DIR PIPELINE_DIR", argv[0]) |
| 21 | + } |
| 22 | + for _, seg := range argv[1:] { |
| 23 | + if seg != filepath.Clean(seg) { |
| 24 | + return exitf(2, "Invalid argument %q", seg) |
| 25 | + } |
| 26 | + if seg == "." || seg == ".." || strings.ContainsRune(seg, '/') { |
| 27 | + return exitf(2, "Invalid argument %q", seg) |
| 28 | + } |
| 29 | + } |
| 30 | + subpath := filepath.Join(argv[1:]...) |
| 31 | + |
| 32 | + // Get a file descriptor for the base builds directory. |
| 33 | + bd, err := fdfs.DirFS(baseDir) |
| 34 | + if err != nil { |
| 35 | + if errors.Is(err, fs.ErrNotExist) { |
| 36 | + return exit0() |
| 37 | + } |
| 38 | + return exitf(3, "Couldn't open %s: %v", baseDir, err) |
| 39 | + } |
| 40 | + defer bd.Close() |
| 41 | + |
| 42 | + // Get a file descriptor for the agentdir/orgdir/pipelinedir within the |
| 43 | + // builds directory. |
| 44 | + // openat2(2) flags ensures this is within the builds directory, and does |
| 45 | + // not involve a symlink. |
| 46 | + pd, err := bd.Sub(subpath) |
| 47 | + if err != nil { |
| 48 | + if errors.Is(err, fs.ErrNotExist) { |
| 49 | + return exit0() |
| 50 | + } |
| 51 | + return exitf(3, "Couldn't open %s: %v", subpath, err) |
| 52 | + } |
| 53 | + defer pd.Close() |
| 54 | + |
| 55 | + // Get the uid and gid of buildkite-agent |
| 56 | + agentUser, err := user.Lookup(uname) |
| 57 | + if err != nil { |
| 58 | + return exitf(4, "Couldn't look up buildkite-agent user: %v", err) |
| 59 | + } |
| 60 | + uid, err := strconv.Atoi(agentUser.Uid) |
| 61 | + if err != nil { |
| 62 | + return exitf(4, "buildkite-agent uid %q not an integer: %v", agentUser.Uid, err) |
| 63 | + } |
| 64 | + gid, err := strconv.Atoi(agentUser.Gid) |
| 65 | + if err != nil { |
| 66 | + return exitf(4, "buildkite-agent gid %q not an integer: %v", agentUser.Gid, err) |
| 67 | + } |
| 68 | + |
| 69 | + // fs.WalkDir to find everything within the directory. |
| 70 | + // fchownat(2) to change the owner of the item. |
| 71 | + // We allow symlinks here, but operate on the symlinks themselves. |
| 72 | + if err := fs.WalkDir(pd, ".", func(path string, d fs.DirEntry, err error) error { |
| 73 | + return pd.Lchown(path, uid, gid) |
| 74 | + }); err != nil { |
| 75 | + return exitf(5, "Couldn't recursively chown %s: %v", subpath, err) |
| 76 | + } |
| 77 | + |
| 78 | + return exit0() |
| 79 | +} |
| 80 | + |
| 81 | +func exit0() (string, int) { return "", 0 } |
| 82 | + |
| 83 | +func exitf(code int, f string, v ...any) (string, int) { |
| 84 | + return fmt.Sprintf(f, v...), code |
| 85 | +} |
0 commit comments