-
Notifications
You must be signed in to change notification settings - Fork 19
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
Try harder to get existing user ID #180
Changes from all commits
c93cbd1
7821e8e
c765897
39b577e
3ce03f4
a4f1794
31daf3d
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 |
---|---|---|
|
@@ -6,6 +6,7 @@ import ( | |
osuser "os/user" | ||
"sort" | ||
"strconv" | ||
"syscall" | ||
|
||
"github.com/mauromorales/xpasswd/pkg/users" | ||
"github.com/pkg/errors" | ||
|
@@ -101,41 +102,52 @@ func createUser(fs vfs.FS, u schema.User, console Console) error { | |
} | ||
} | ||
|
||
if u.Homedir == "" { | ||
u.Homedir = fmt.Sprintf("%s/%s", usrDefaults["HOME"], u.Name) | ||
} | ||
|
||
uid := -1 | ||
|
||
// If UID is specified just put it there. No matter whats in the system or the collisions. Good luck. | ||
if u.UID != "" { | ||
// User defined-uid | ||
uid, err = strconv.Atoi(u.UID) | ||
if err != nil { | ||
return errors.Wrap(err, "invalid uid defined") | ||
} | ||
} else { | ||
// Try to get the existing UID in the system | ||
list := users.NewUserList() | ||
list.SetPath(etcpasswd) | ||
list.Load() | ||
|
||
user := list.Get(u.Name) | ||
|
||
if user != nil { | ||
uid, err = user.UID() | ||
if err != nil { | ||
return errors.Wrap(err, "could not get user id") | ||
} | ||
} else { | ||
// 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") | ||
// Try to see if the user was created previously with a given UID by checking for an existing home dir | ||
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. won't this be problematic since you can define a user like:
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. everything is problematic in here, thats what happens when you give users too much choice. They dont know whats better for them. We are just trying harder to get it if its there, unfortunately we cannot cover all angles. What happens if during one boot the user has the default dir and the next one he modified to now set the homedir specifically in the config? All their files are gone? On a different dir? What if he also changes the uid? Now they are gone and also he cant access them anymore? Too many different paths and scenarios due to the broad config spectrum of the user. |
||
userDir, err := os.Stat(u.Homedir) | ||
if err == nil { | ||
if stat, ok := userDir.Sys().(*syscall.Stat_t); ok { | ||
uid = int(stat.Uid) | ||
} | ||
} else { | ||
// Now generate one if we havent been able to pick the existing one | ||
// 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) | ||
} | ||
|
||
if u.Shell == "" { | ||
u.Shell = usrDefaults["SHELL"] | ||
} | ||
|
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.
🤣