@@ -349,51 +349,59 @@ func GetExecUser(userSpec string, defaults *ExecUser, passwd, group io.Reader) (
349
349
return user , nil
350
350
}
351
351
352
- // GetAdditionalGroupsPath is a wrapper for GetAdditionalGroups. It reads data from the
353
- // given file path and uses that data as the arguments to GetAdditionalGroups.
352
+ // GetAdditionalGroupsPath looks up a list of groups by name or group id
353
+ // against the group file. If a group name cannot be found, an error will be
354
+ // returned. If a group id cannot be found, it will be returned as-is.
354
355
func GetAdditionalGroupsPath (additionalGroups []string , groupPath string ) ([]int , error ) {
355
- var groupIds []int
356
-
357
- for _ , ag := range additionalGroups {
358
- groupReader , err := os .Open (groupPath )
359
- if err != nil {
360
- return nil , fmt .Errorf ("Failed to open group file: %v" , err )
361
- }
362
- defer groupReader .Close ()
363
-
364
- groupId , err := GetAdditionalGroup (ag , groupReader )
365
- if err != nil {
366
- return nil , err
367
- }
368
- groupIds = append (groupIds , groupId )
356
+ groupReader , err := os .Open (groupPath )
357
+ if err != nil {
358
+ return nil , fmt .Errorf ("Failed to open group file: %v" , err )
369
359
}
360
+ defer groupReader .Close ()
370
361
371
- return groupIds , nil
372
- }
373
-
374
- // GetAdditionalGroup looks up the specified group in the passed groupReader.
375
- func GetAdditionalGroup (additionalGroup string , groupReader io.Reader ) (int , error ) {
376
362
groups , err := ParseGroupFilter (groupReader , func (g Group ) bool {
377
- return g .Name == additionalGroup || strconv .Itoa (g .Gid ) == additionalGroup
363
+ for _ , ag := range additionalGroups {
364
+ if g .Name == ag || strconv .Itoa (g .Gid ) == ag {
365
+ return true
366
+ }
367
+ }
368
+ return false
378
369
})
379
370
if err != nil {
380
- return - 1 , fmt .Errorf ("Unable to find additional groups %v: %v" , additionalGroup , err )
371
+ return nil , fmt .Errorf ("Unable to find additional groups %v: %v" , additionalGroups , err )
381
372
}
382
- if groups != nil && len (groups ) > 0 {
383
- // if we found any group entries that matched our filter, let's take the first one as "correct"
384
- return groups [0 ].Gid , nil
385
- } else {
386
- // we asked for a group but didn't find id... let's check to see if we wanted a numeric group
387
- addGroup , err := strconv .Atoi (additionalGroup )
388
- if err != nil {
389
- // not numeric - we have to bail
390
- return - 1 , fmt .Errorf ("Unable to find group %v" , additionalGroup )
391
- }
392
373
393
- // Ensure gid is inside gid range.
394
- if addGroup < minId || addGroup > maxId {
395
- return - 1 , ErrRange
374
+ gidMap := make (map [int ]struct {})
375
+ for _ , ag := range additionalGroups {
376
+ var found bool
377
+ for _ , g := range groups {
378
+ // if we found a matched group either by name or gid, take the
379
+ // first matched as correct
380
+ if g .Name == ag || strconv .Itoa (g .Gid ) == ag {
381
+ if _ , ok := gidMap [g .Gid ]; ! ok {
382
+ gidMap [g .Gid ] = struct {}{}
383
+ found = true
384
+ break
385
+ }
386
+ }
387
+ }
388
+ // we asked for a group but didn't find it. let's check to see
389
+ // if we wanted a numeric group
390
+ if ! found {
391
+ gid , err := strconv .Atoi (ag )
392
+ if err != nil {
393
+ return nil , fmt .Errorf ("Unable to find group %s" , ag )
394
+ }
395
+ // Ensure gid is inside gid range.
396
+ if gid < minId || gid > maxId {
397
+ return nil , ErrRange
398
+ }
399
+ gidMap [gid ] = struct {}{}
396
400
}
397
- return addGroup , nil
398
401
}
402
+ gids := []int {}
403
+ for gid := range gidMap {
404
+ gids = append (gids , gid )
405
+ }
406
+ return gids , nil
399
407
}
0 commit comments