@@ -165,12 +165,13 @@ func parseGroupFile(r io.Reader, filter func(*Group) bool) ([]*Group, error) {
165165 return out , nil
166166}
167167
168- // Given a string like "user", "1000", "user:group", "1000:1000", returns the uid, gid, and list of supplementary group IDs, if possible .
169- func GetUserGroupSupplementary (userSpec string , defaultUid int , defaultGid int ) (int , int , []int , error ) {
168+ // Given a string like "user", "1000", "user:group", "1000:1000", returns the uid, gid, list of supplementary group IDs, and home directory, if available and/or applicable .
169+ func GetUserGroupSupplementaryHome (userSpec string , defaultUid , defaultGid int , defaultHome string ) (int , int , []int , string , error ) {
170170 var (
171171 uid = defaultUid
172172 gid = defaultGid
173173 suppGids = []int {}
174+ home = defaultHome
174175
175176 userArg , groupArg string
176177 )
@@ -188,23 +189,24 @@ func GetUserGroupSupplementary(userSpec string, defaultUid int, defaultGid int)
188189 if userArg == "" {
189190 userArg = strconv .Itoa (uid )
190191 }
191- return 0 , 0 , nil , fmt .Errorf ("Unable to find user %v: %v" , userArg , err )
192+ return 0 , 0 , nil , "" , fmt .Errorf ("Unable to find user %v: %v" , userArg , err )
192193 }
193194
194195 haveUser := users != nil && len (users ) > 0
195196 if haveUser {
196197 // if we found any user entries that matched our filter, let's take the first one as "correct"
197198 uid = users [0 ].Uid
198199 gid = users [0 ].Gid
200+ home = users [0 ].Home
199201 } else if userArg != "" {
200202 // we asked for a user but didn't find them... let's check to see if we wanted a numeric user
201203 uid , err = strconv .Atoi (userArg )
202204 if err != nil {
203205 // not numeric - we have to bail
204- return 0 , 0 , nil , fmt .Errorf ("Unable to find user %v" , userArg )
206+ return 0 , 0 , nil , "" , fmt .Errorf ("Unable to find user %v" , userArg )
205207 }
206208 if uid < minId || uid > maxId {
207- return 0 , 0 , nil , ErrRange
209+ return 0 , 0 , nil , "" , ErrRange
208210 }
209211
210212 // if userArg couldn't be found in /etc/passwd but is numeric, just roll with it - this is legit
@@ -223,7 +225,7 @@ func GetUserGroupSupplementary(userSpec string, defaultUid int, defaultGid int)
223225 return false
224226 })
225227 if err != nil && ! os .IsNotExist (err ) {
226- return 0 , 0 , nil , fmt .Errorf ("Unable to find groups for user %v: %v" , users [0 ].Name , err )
228+ return 0 , 0 , nil , "" , fmt .Errorf ("Unable to find groups for user %v: %v" , users [0 ].Name , err )
227229 }
228230
229231 haveGroup := groups != nil && len (groups ) > 0
@@ -236,10 +238,10 @@ func GetUserGroupSupplementary(userSpec string, defaultUid int, defaultGid int)
236238 gid , err = strconv .Atoi (groupArg )
237239 if err != nil {
238240 // not numeric - we have to bail
239- return 0 , 0 , nil , fmt .Errorf ("Unable to find group %v" , groupArg )
241+ return 0 , 0 , nil , "" , fmt .Errorf ("Unable to find group %v" , groupArg )
240242 }
241243 if gid < minId || gid > maxId {
242- return 0 , 0 , nil , ErrRange
244+ return 0 , 0 , nil , "" , ErrRange
243245 }
244246
245247 // if groupArg couldn't be found in /etc/group but is numeric, just roll with it - this is legit
@@ -252,5 +254,5 @@ func GetUserGroupSupplementary(userSpec string, defaultUid int, defaultGid int)
252254 }
253255 }
254256
255- return uid , gid , suppGids , nil
257+ return uid , gid , suppGids , home , nil
256258}
0 commit comments