From 99c622b69f0252004241c35a4ef674d273aac2ae Mon Sep 17 00:00:00 2001 From: Andrew Woodlee Date: Tue, 11 Mar 2025 15:30:07 -0500 Subject: [PATCH] UserCommands: add field CreateUserHome --- pkg/backy/backy.go | 5 +---- pkg/backy/types.go | 2 ++ pkg/backy/utils.go | 1 + pkg/usermanager/linux/linux.go | 7 ++++++- pkg/usermanager/userman.go | 2 +- 5 files changed, 11 insertions(+), 6 deletions(-) diff --git a/pkg/backy/backy.go b/pkg/backy/backy.go index 958bc61..1b3de00 100644 --- a/pkg/backy/backy.go +++ b/pkg/backy/backy.go @@ -223,11 +223,8 @@ func (command *Command) RunCmd(cmdCtxLogger zerolog.Logger, opts *ConfigOpts) ([ if err != nil { return collectOutput(&cmdOutBuf, command.Name, cmdCtxLogger, command.OutputToLog), fmt.Errorf("error creating file %s/authorized_keys: %v", userSshDir, err) } + } - } - if err != nil { - return collectOutput(&cmdOutBuf, command.Name, cmdCtxLogger, command.OutputToLog), fmt.Errorf("error opening file %s/authorized_keys: %v", userSshDir, err) - } f, err = os.OpenFile(fmt.Sprintf("%s/authorized_keys", userSshDir), 0700, os.ModeAppend) if err != nil { return collectOutput(&cmdOutBuf, command.Name, cmdCtxLogger, command.OutputToLog), fmt.Errorf("error opening file %s/authorized_keys: %v", userSshDir, err) diff --git a/pkg/backy/types.go b/pkg/backy/types.go index dca6db6..2c32d06 100644 --- a/pkg/backy/types.go +++ b/pkg/backy/types.go @@ -115,6 +115,8 @@ type ( UserShell string `yaml:"userShell,omitempty"` + UserCreateHome bool `yaml:"userCreateHome,omitempty"` + SystemUser bool `yaml:"systemUser,omitempty"` UserPassword string `yaml:"userPassword,omitempty"` diff --git a/pkg/backy/utils.go b/pkg/backy/utils.go index 07c1732..975d4f0 100644 --- a/pkg/backy/utils.go +++ b/pkg/backy/utils.go @@ -294,6 +294,7 @@ func getCommandTypeAndSetCommandInfo(command *Command) *Command { command.UserHome, command.UserShell, command.SystemUser, + command.UserCreateHome, command.UserGroups, command.Args) case "modify": diff --git a/pkg/usermanager/linux/linux.go b/pkg/usermanager/linux/linux.go index d0826ac..ff3bee3 100644 --- a/pkg/usermanager/linux/linux.go +++ b/pkg/usermanager/linux/linux.go @@ -15,7 +15,7 @@ func (l LinuxUserManager) NewLinuxManager() *LinuxUserManager { } // AddUser adds a new user to the system. -func (l LinuxUserManager) AddUser(username, homeDir, shell string, isSystem bool, groups, args []string) (string, []string) { +func (l LinuxUserManager) AddUser(username, homeDir, shell string, createHome, isSystem bool, groups, args []string) (string, []string) { baseArgs := []string{} if isSystem { @@ -38,6 +38,11 @@ func (l LinuxUserManager) AddUser(username, homeDir, shell string, isSystem bool baseArgs = append(baseArgs, args...) } + if createHome { + baseArgs = append(baseArgs, "-m") + + } + args = append(baseArgs, username) cmd := "useradd" diff --git a/pkg/usermanager/userman.go b/pkg/usermanager/userman.go index d0f6a4d..b3e95e2 100644 --- a/pkg/usermanager/userman.go +++ b/pkg/usermanager/userman.go @@ -10,7 +10,7 @@ import ( // UserManager defines the interface for user management operations. // All functions but one return a string for the command and any args. type UserManager interface { - AddUser(username, homeDir, shell string, isSystem bool, groups, args []string) (string, []string) + AddUser(username, homeDir, shell string, createHome, isSystem bool, groups, args []string) (string, []string) RemoveUser(username string) (string, []string) ModifyUser(username, homeDir, shell string, groups []string) (string, []string) // Modify password uses chpasswd for Linux systems to build the command to change the password