Skip to content
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

Merged
merged 7 commits into from
Oct 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 22 additions & 10 deletions pkg/plugins/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
osuser "os/user"
"sort"
"strconv"
"syscall"

"github.com/mauromorales/xpasswd/pkg/users"
"github.com/pkg/errors"
Expand Down Expand Up @@ -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.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤣

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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

won't this be problematic since you can define a user like:

stages:
   default:
     - name: "Setup users"
       users: 
          bastion: 
            passwd: "strongpassword"
            homedir: "/home/foo

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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"]
}
Expand Down
Loading