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

adds uid and gid as values to adduser #87

Merged
merged 1 commit into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions RECIPE.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,8 @@ Create a new user.
- *Fullname* (`string`): The full name (display name) of the new user.
- *Groups* (`[string]`): A list of groups the new user belongs to (the new user is automatically part of its own group).
- *Password* (optional `string`): The password for the user. If not provided, password login will be disabled.
- *UID* (optional `int`): The UID for the user. Will be determined automatically if not provided.
- *GID* (optional `int`): The GID for the user. Will be determined automatically if not provided.

### timezone

Expand Down
23 changes: 16 additions & 7 deletions core/recipe.go
Original file line number Diff line number Diff line change
Expand Up @@ -696,7 +696,9 @@ func runPostInstallOperation(chroot bool, operation string, args []interface{})
* - *Username* (`string`): The username of the new user.
* - *Fullname* (`string`): The full name (display name) of the new user.
* - *Groups* (`[string]`): A list of groups the new user belongs to (the new user is automatically part of its own group).
* - *Password* (optional `string`): The password for the user. If not provided, password login will be disabled.
* - *Password* (optional `string`): The password for the user. If not provided or empty, password login will be disabled.
* - *UID* (optional `int`): The UID for the user. Will be determined automatically if not provided.
* - *GID* (optional `int`): The GID for the user. Will be determined automatically if not provided.
*/
case "adduser":
username := args[0].(string)
Expand All @@ -706,13 +708,20 @@ func runPostInstallOperation(chroot bool, operation string, args []interface{})
groupStr := group.(string)
groups = append(groups, groupStr)
}
var err error
if len(args) == 4 {
password := args[3].(string)
err = system.AddUser(targetRoot, username, fullname, groups, password)
} else {
err = system.AddUser(targetRoot, username, fullname, groups)
password := ""
if len(args) >= 4 {
password = args[3].(string)
}
uid := -1
if len(args) >= 5 {
uid = int(args[4].(float64))
}
gid := -1
if len(args) >= 6 {
gid = int(args[5].(float64))
}

err := system.AddUser(targetRoot, username, fullname, groups, password, uid, gid)
if err != nil {
return operationError(operation, err)
}
Expand Down
26 changes: 19 additions & 7 deletions core/system/post_install.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,37 @@ func SetTimezone(targetRoot, tz string) error {
return nil
}

func AddUser(targetRoot, username, fullname string, groups []string, password ...string) error {
adduserCmd := "useradd --shell /bin/bash %s && usermod -c \"%s\" %s"
// AddUser creates a new user and adds it to the groups provided
//
// If password is left empty, password login will be disabled.
// If uid and/or gid are -1, they will be ignored.
func AddUser(targetRoot, username, fullname string, groups []string, password string, uid, gid int) error {
adduserCmd := "useradd --shell /bin/bash %s %s && usermod -c \"%s\" %s"

extraArgs := ""
if uid != -1 {
extraArgs = " --uid " + fmt.Sprint(uid)
}
if gid != -1 {
extraArgs = " --gid " + fmt.Sprint(gid)
}

var err error
if targetRoot != "" {
err = util.RunInChroot(targetRoot, fmt.Sprintf(adduserCmd, username, fullname, username))
err = util.RunInChroot(targetRoot, fmt.Sprintf(adduserCmd, extraArgs, username, fullname, username))
} else {
err = util.RunCommand(fmt.Sprintf(adduserCmd, username, fullname, username))
err = util.RunCommand(fmt.Sprintf(adduserCmd, extraArgs, username, fullname, username))
}
if err != nil {
return fmt.Errorf("failed to create user: %s", err)
}

if len(password) == 1 {
if password != "" {
passwdCmd := "echo \"%s:%s\" | chpasswd"
if targetRoot != "" {
err = util.RunInChroot(targetRoot, fmt.Sprintf(passwdCmd, username, password[0]))
err = util.RunInChroot(targetRoot, fmt.Sprintf(passwdCmd, username, password))
} else {
err = util.RunCommand(fmt.Sprintf(passwdCmd, username, password[0]))
err = util.RunCommand(fmt.Sprintf(passwdCmd, username, password))
}
if err != nil {
return fmt.Errorf("failed to set password: %s", err)
Expand Down
2 changes: 1 addition & 1 deletion utils/sample_recipe.json
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@
{
"chroot": true,
"operation": "adduser",
"params": ["vanilla", "vanilla", ["sudo", "lpadmin"], "vanilla"]
"params": ["vanilla", "vanilla", ["sudo", "lpadmin"], "vanilla", 1200]
},
{
"chroot": true,
Expand Down