|
| 1 | +//go:build !windows |
| 2 | + |
| 3 | +package usergroup |
| 4 | + |
| 5 | +import ( |
| 6 | + "bytes" |
| 7 | + "fmt" |
| 8 | + "io" |
| 9 | + "os/exec" |
| 10 | + "strconv" |
| 11 | + "syscall" |
| 12 | + |
| 13 | + "github.com/docker/docker/pkg/idtools" |
| 14 | + "github.com/moby/sys/user" |
| 15 | +) |
| 16 | + |
| 17 | +// LookupUser uses traditional local system files lookup (from libcontainer/user) on a username, |
| 18 | +// followed by a call to `getent` for supporting host configured non-files passwd and group dbs |
| 19 | +func LookupUser(name string) (user.User, error) { |
| 20 | + // first try a local system files lookup using existing capabilities |
| 21 | + usr, err := user.LookupUser(name) |
| 22 | + if err == nil { |
| 23 | + return usr, nil |
| 24 | + } |
| 25 | + // local files lookup failed; attempt to call `getent` to query configured passwd dbs |
| 26 | + usr, err = getentUser(name) |
| 27 | + if err != nil { |
| 28 | + return user.User{}, err |
| 29 | + } |
| 30 | + return usr, nil |
| 31 | +} |
| 32 | + |
| 33 | +// LookupUID uses traditional local system files lookup (from libcontainer/user) on a uid, |
| 34 | +// followed by a call to `getent` for supporting host configured non-files passwd and group dbs |
| 35 | +func LookupUID(uid int) (user.User, error) { |
| 36 | + // first try a local system files lookup using existing capabilities |
| 37 | + usr, err := user.LookupUid(uid) |
| 38 | + if err == nil { |
| 39 | + return usr, nil |
| 40 | + } |
| 41 | + // local files lookup failed; attempt to call `getent` to query configured passwd dbs |
| 42 | + return getentUser(strconv.Itoa(uid)) |
| 43 | +} |
| 44 | + |
| 45 | +func getentUser(name string) (user.User, error) { |
| 46 | + reader, err := callGetent("passwd", name) |
| 47 | + if err != nil { |
| 48 | + return user.User{}, err |
| 49 | + } |
| 50 | + users, err := user.ParsePasswd(reader) |
| 51 | + if err != nil { |
| 52 | + return user.User{}, err |
| 53 | + } |
| 54 | + if len(users) == 0 { |
| 55 | + return user.User{}, fmt.Errorf("getent failed to find passwd entry for %q", name) |
| 56 | + } |
| 57 | + return users[0], nil |
| 58 | +} |
| 59 | + |
| 60 | +// LookupGroup uses traditional local system files lookup (from libcontainer/user) on a group name, |
| 61 | +// followed by a call to `getent` for supporting host configured non-files passwd and group dbs |
| 62 | +func LookupGroup(name string) (user.Group, error) { |
| 63 | + // first try a local system files lookup using existing capabilities |
| 64 | + group, err := user.LookupGroup(name) |
| 65 | + if err == nil { |
| 66 | + return group, nil |
| 67 | + } |
| 68 | + // local files lookup failed; attempt to call `getent` to query configured group dbs |
| 69 | + return getentGroup(name) |
| 70 | +} |
| 71 | + |
| 72 | +// LookupGID uses traditional local system files lookup (from libcontainer/user) on a group ID, |
| 73 | +// followed by a call to `getent` for supporting host configured non-files passwd and group dbs |
| 74 | +func LookupGID(gid int) (user.Group, error) { |
| 75 | + // first try a local system files lookup using existing capabilities |
| 76 | + group, err := user.LookupGid(gid) |
| 77 | + if err == nil { |
| 78 | + return group, nil |
| 79 | + } |
| 80 | + // local files lookup failed; attempt to call `getent` to query configured group dbs |
| 81 | + return getentGroup(strconv.Itoa(gid)) |
| 82 | +} |
| 83 | + |
| 84 | +func getentGroup(name string) (user.Group, error) { |
| 85 | + reader, err := callGetent("group", name) |
| 86 | + if err != nil { |
| 87 | + return user.Group{}, err |
| 88 | + } |
| 89 | + groups, err := user.ParseGroup(reader) |
| 90 | + if err != nil { |
| 91 | + return user.Group{}, err |
| 92 | + } |
| 93 | + if len(groups) == 0 { |
| 94 | + return user.Group{}, fmt.Errorf("getent failed to find groups entry for %q", name) |
| 95 | + } |
| 96 | + return groups[0], nil |
| 97 | +} |
| 98 | + |
| 99 | +func callGetent(database, key string) (io.Reader, error) { |
| 100 | + getentCmd, err := resolveBinary("getent") |
| 101 | + // if no `getent` command within the execution environment, can't do anything else |
| 102 | + if err != nil { |
| 103 | + return nil, fmt.Errorf("unable to find getent command: %w", err) |
| 104 | + } |
| 105 | + command := exec.Command(getentCmd, database, key) |
| 106 | + // we run getent within container filesystem, but without /dev so /dev/null is not available for exec to mock stdin |
| 107 | + command.Stdin = io.NopCloser(bytes.NewReader(nil)) |
| 108 | + out, err := command.CombinedOutput() |
| 109 | + if err != nil { |
| 110 | + exitCode, errC := getExitCode(err) |
| 111 | + if errC != nil { |
| 112 | + return nil, err |
| 113 | + } |
| 114 | + switch exitCode { |
| 115 | + case 1: |
| 116 | + return nil, fmt.Errorf("getent reported invalid parameters/database unknown") |
| 117 | + case 2: |
| 118 | + return nil, fmt.Errorf("getent unable to find entry %q in %s database", key, database) |
| 119 | + case 3: |
| 120 | + return nil, fmt.Errorf("getent database doesn't support enumeration") |
| 121 | + default: |
| 122 | + return nil, err |
| 123 | + } |
| 124 | + } |
| 125 | + return bytes.NewReader(out), nil |
| 126 | +} |
| 127 | + |
| 128 | +// getExitCode returns the ExitStatus of the specified error if its type is |
| 129 | +// exec.ExitError, returns 0 and an error otherwise. |
| 130 | +func getExitCode(err error) (int, error) { |
| 131 | + exitCode := 0 |
| 132 | + if exiterr, ok := err.(*exec.ExitError); ok { |
| 133 | + if procExit, ok := exiterr.Sys().(syscall.WaitStatus); ok { |
| 134 | + return procExit.ExitStatus(), nil |
| 135 | + } |
| 136 | + } |
| 137 | + return exitCode, fmt.Errorf("failed to get exit code") |
| 138 | +} |
| 139 | + |
| 140 | +// LoadIdentityMapping takes a requested username and |
| 141 | +// using the data from /etc/sub{uid,gid} ranges, creates the |
| 142 | +// proper uid and gid remapping ranges for that user/group pair |
| 143 | +func LoadIdentityMapping(name string) (idtools.IdentityMapping, error) { |
| 144 | + usr, err := LookupUser(name) |
| 145 | + if err != nil { |
| 146 | + return idtools.IdentityMapping{}, fmt.Errorf("could not get user for username %s: %v", name, err) |
| 147 | + } |
| 148 | + |
| 149 | + subuidRanges, err := lookupSubRangesFile("/etc/subuid", usr) |
| 150 | + if err != nil { |
| 151 | + return idtools.IdentityMapping{}, err |
| 152 | + } |
| 153 | + subgidRanges, err := lookupSubRangesFile("/etc/subgid", usr) |
| 154 | + if err != nil { |
| 155 | + return idtools.IdentityMapping{}, err |
| 156 | + } |
| 157 | + |
| 158 | + return idtools.IdentityMapping{ |
| 159 | + UIDMaps: subuidRanges, |
| 160 | + GIDMaps: subgidRanges, |
| 161 | + }, nil |
| 162 | +} |
| 163 | + |
| 164 | +func lookupSubRangesFile(path string, usr user.User) ([]idtools.IDMap, error) { |
| 165 | + uidstr := strconv.Itoa(usr.Uid) |
| 166 | + rangeList, err := user.ParseSubIDFileFilter(path, func(sid user.SubID) bool { |
| 167 | + return sid.Name == usr.Name || sid.Name == uidstr |
| 168 | + }) |
| 169 | + if err != nil { |
| 170 | + return nil, err |
| 171 | + } |
| 172 | + if len(rangeList) == 0 { |
| 173 | + return nil, fmt.Errorf("no subuid ranges found for user %q", usr.Name) |
| 174 | + } |
| 175 | + |
| 176 | + idMap := []idtools.IDMap{} |
| 177 | + |
| 178 | + containerID := 0 |
| 179 | + for _, idrange := range rangeList { |
| 180 | + idMap = append(idMap, idtools.IDMap{ |
| 181 | + ContainerID: containerID, |
| 182 | + HostID: int(idrange.SubID), |
| 183 | + Size: int(idrange.Count), |
| 184 | + }) |
| 185 | + containerID = containerID + int(idrange.Count) |
| 186 | + } |
| 187 | + return idMap, nil |
| 188 | +} |
0 commit comments