-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Various fixes to allow uid to be in a correct range #159
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -69,30 +69,15 @@ func createUser(fs vfs.FS, u schema.User, console Console) error { | |
} | ||
|
||
primaryGroup := u.Name | ||
gid := 1000 | ||
|
||
gid := -1 // -1 instructs entities to find the next free id and assign it | ||
if u.PrimaryGroup != "" { | ||
gr, err := osuser.LookupGroup(u.PrimaryGroup) | ||
if err != nil { | ||
return errors.Wrap(err, "could not resolve primary group of user") | ||
} | ||
gid, _ = strconv.Atoi(gr.Gid) | ||
primaryGroup = u.PrimaryGroup | ||
} else { | ||
// Create a new group after the user name | ||
all, _ := entities.ParseGroup(etcgroup) | ||
if len(all) != 0 { | ||
usedGids := []int{} | ||
for _, entry := range all { | ||
usedGids = append(usedGids, *entry.Gid) | ||
} | ||
sort.Ints(usedGids) | ||
if len(usedGids) == 0 { | ||
return errors.New("no new guid found") | ||
} | ||
gid = usedGids[len(usedGids)-1] | ||
gid++ | ||
} | ||
} | ||
|
||
updateGroup := entities.Group{ | ||
|
@@ -101,9 +86,22 @@ func createUser(fs vfs.FS, u schema.User, console Console) error { | |
Gid: &gid, | ||
Users: u.Name, | ||
} | ||
updateGroup.Apply(etcgroup, false) | ||
err = updateGroup.Apply(etcgroup, false) | ||
if err != nil { | ||
return errors.Wrap(err, "creating the user's group") | ||
} | ||
|
||
uid := 1000 | ||
// reload the group to get the generated GID | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This wouldn't be needed if the ParseGroup had a pointer receiver. I don't want to change that here though to avoid breaking other things. We can consider changing it in the future to avoid this second parsing of groups. |
||
groups, _ := entities.ParseGroup(etcgroup) | ||
for name, group := range groups { | ||
if name == updateGroup.Name { | ||
updateGroup = group | ||
gid = *group.Gid | ||
break | ||
} | ||
} | ||
|
||
uid := -1 | ||
if u.UID != "" { | ||
// User defined-uid | ||
uid, err = strconv.Atoi(u.UID) | ||
|
@@ -123,9 +121,16 @@ func createUser(fs vfs.FS, u schema.User, console Console) error { | |
return errors.Wrap(err, "could not get user id") | ||
} | ||
} else { | ||
uid = list.GenerateUID() | ||
// https://systemd.io/UIDS-GIDS/#special-distribution-uid-ranges | ||
uid, err = list.GenerateUIDInRange(entities.HumanIDMin, entities.HumanIDMax) | ||
if err != nil { | ||
return errors.Wrap(err, "no available uid") | ||
} | ||
} | ||
} | ||
if uid == -1 { | ||
return errors.New("could not set uid for user") | ||
} | ||
|
||
if u.Homedir == "" { | ||
u.Homedir = fmt.Sprintf("%s/%s", usrDefaults["HOME"], u.Name) | ||
|
@@ -162,7 +167,7 @@ func createUser(fs vfs.FS, u schema.User, console Console) error { | |
os.Chown(homedir, uid, gid) | ||
} | ||
|
||
groups, _ := entities.ParseGroup(etcgroup) | ||
groups, _ = entities.ParseGroup(etcgroup) | ||
for name, group := range groups { | ||
for _, w := range u.Groups { | ||
if w == name { | ||
|
@@ -205,11 +210,11 @@ func User(l logger.Interface, s schema.Stage, fs vfs.FS, console Console) error | |
for _, k := range users { | ||
r := s.Users[k] | ||
r.Name = k | ||
if !s.Users[k].Exists() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice catch |
||
if !r.Exists() { | ||
if err := createUser(fs, r, console); err != nil { | ||
errs = multierror.Append(errs, err) | ||
} | ||
} else if s.Users[k].PasswordHash != "" { | ||
} else if r.PasswordHash != "" { | ||
if err := setUserPass(fs, r.Name, r.PasswordHash); err != nil { | ||
return err | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not need anymore. That's what the
updateGroup.Apply
will do if the gid is-1
.