This commit is contained in:
90
pkg/usermanager/linux/linux.go
Normal file
90
pkg/usermanager/linux/linux.go
Normal file
@ -0,0 +1,90 @@
|
||||
package linux
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
passGen "github.com/sethvargo/go-password/password"
|
||||
)
|
||||
|
||||
// LinuxUserManager implements UserManager for Linux systems.
|
||||
type LinuxUserManager struct{}
|
||||
|
||||
func (l LinuxUserManager) NewLinuxManager() *LinuxUserManager {
|
||||
return &LinuxUserManager{}
|
||||
}
|
||||
|
||||
// AddUser adds a new user to the system.
|
||||
func (l LinuxUserManager) AddUser(username, homeDir, shell string, isSystem bool, groups, args []string) (string, []string) {
|
||||
baseArgs := []string{}
|
||||
|
||||
if isSystem {
|
||||
baseArgs = append(baseArgs, "--system")
|
||||
}
|
||||
|
||||
if homeDir != "" {
|
||||
baseArgs = append(baseArgs, "--home", homeDir)
|
||||
}
|
||||
|
||||
if shell != "" {
|
||||
baseArgs = append(baseArgs, "--shell", shell)
|
||||
}
|
||||
|
||||
if len(groups) > 0 {
|
||||
baseArgs = append(baseArgs, "--groups", strings.Join(groups, ","))
|
||||
}
|
||||
|
||||
if len(args) > 0 {
|
||||
baseArgs = append(baseArgs, args...)
|
||||
}
|
||||
|
||||
args = append(baseArgs, username)
|
||||
|
||||
cmd := "useradd"
|
||||
return cmd, args
|
||||
}
|
||||
|
||||
func (l LinuxUserManager) ModifyPassword(username, password string) (string, *strings.Reader, string) {
|
||||
cmd := "chpasswd"
|
||||
if password == "" {
|
||||
password = passGen.MustGenerate(20, 5, 5, false, false)
|
||||
}
|
||||
stdin := strings.NewReader(fmt.Sprintf("%s:%s", username, password))
|
||||
return cmd, stdin, password
|
||||
}
|
||||
|
||||
// RemoveUser removes an existing user from the system.
|
||||
func (l LinuxUserManager) RemoveUser(username string) (string, []string) {
|
||||
cmd := "userdel"
|
||||
|
||||
return cmd, []string{username}
|
||||
}
|
||||
|
||||
// ModifyUser modifies an existing user's details.
|
||||
func (l LinuxUserManager) ModifyUser(username, homeDir, shell string, groups []string) (string, []string) {
|
||||
args := []string{}
|
||||
|
||||
if homeDir != "" {
|
||||
args = append(args, "--home", homeDir)
|
||||
}
|
||||
|
||||
if shell != "" {
|
||||
args = append(args, "--shell", shell)
|
||||
}
|
||||
|
||||
if len(groups) > 0 {
|
||||
args = append(args, "--groups", strings.Join(groups, ","))
|
||||
}
|
||||
|
||||
args = append(args, username)
|
||||
|
||||
cmd := "usermod"
|
||||
|
||||
return cmd, args
|
||||
}
|
||||
|
||||
// UserExists checks if a user exists on the system.
|
||||
func (l LinuxUserManager) UserExists(username string) (string, []string) {
|
||||
cmd := "id"
|
||||
return cmd, []string{username}
|
||||
}
|
35
pkg/usermanager/userman.go
Normal file
35
pkg/usermanager/userman.go
Normal file
@ -0,0 +1,35 @@
|
||||
package usermanager
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"git.andrewnw.xyz/CyberShell/backy/pkg/usermanager/linux"
|
||||
)
|
||||
|
||||
// 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)
|
||||
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
|
||||
// Should return a password as the last argument
|
||||
// TODO: refactor when adding more systems instead of Linux
|
||||
ModifyPassword(username, password string) (string, *strings.Reader, string)
|
||||
UserExists(username string) (string, []string)
|
||||
}
|
||||
|
||||
func NewUserManager(system string) (UserManager, error) {
|
||||
var manager UserManager
|
||||
|
||||
switch system {
|
||||
case "linux", "Linux":
|
||||
manager = linux.LinuxUserManager{}
|
||||
default:
|
||||
return nil, fmt.Errorf("usermanger system %s is not recognized", system)
|
||||
}
|
||||
|
||||
return manager, nil
|
||||
|
||||
}
|
Reference in New Issue
Block a user