Skip to content

Commit

Permalink
core: Add support for removing users from the system
Browse files Browse the repository at this point in the history
Functionality primarily added to remove the accidentally added
sambashares user as part of a typical samba update.
  • Loading branch information
joebonrichie committed May 9, 2023
1 parent 50657ce commit 0a16b8f
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
17 changes: 16 additions & 1 deletion core/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func (c *Context) CreateGroup(name string, id string) error {
return nil
}

// DeleteGroup deletes a preexisting group with the given name and ID
// DeleteGroup deletes a preexisting group with the given name
func (c *Context) DeleteGroup(name string) error {
cmd := exec.Command("groupdel", name)
if err := cmd.Run(); err != nil {
Expand All @@ -154,6 +154,21 @@ func (c *Context) DeleteGroup(name string) error {
return nil
}

// DeleteUser deletes a preexisting user with the given user
func (c *Context) DeleteUser(user *User) (ran bool, err error) {
cmd := exec.Command("userdel", "-r", "-f", user.Name)
ran = true
if err := cmd.Run(); err == nil {
// remove user from c.users
for i, v := range c.users {
if v.Name == user.Name {
c.users = append(c.users[0:i], c.users[i+1:]...)
}
}
}
return
}

// UpdateGroupID finds a group with the given name and changes its ID to the given ID
func (c *Context) UpdateGroupID(name string, id string) error {
cmd := exec.Command("groupmod", "-g", id, name)
Expand Down
33 changes: 32 additions & 1 deletion core/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type Migration struct {
UpdateGroup []*UpdateGroup `toml:"group-update"`
RemoveUsers []*RemoveUsers `toml:"users-remove"`
RemoveGroup []*RemoveGroup `toml:"group-delete"`
DeleteUsers []*DeleteUsers `toml:"users-delete"`
}

// UpdateUsers is a type of modification that adds a group to a specific set of users
Expand All @@ -59,6 +60,11 @@ type RemoveGroup struct {
GroupName string `toml:"name"`
}

// DeleteUser is a type of modification that attempts to delete a preexisting user from the system
type DeleteUsers struct {
UserName string `toml:"name"`
}

// LoadMigrations finds migration files in SysDir and UsrDir and attempts to load them
func LoadMigrations() []Migration {
var allMigrations []Migration
Expand Down Expand Up @@ -139,6 +145,9 @@ func (m *Migration) Run(context *Context) {
for _, task := range m.RemoveGroup {
m.removeGroup(context, task)
}
for _, task := range m.DeleteUsers {
m.deleteUsers(context, task)
}
}

func (m *Migration) updateUsers(context *Context, task *UpdateUsers) {
Expand Down Expand Up @@ -225,8 +234,30 @@ func (m *Migration) removeGroup(context *Context, task *RemoveGroup) {
} else {
waterlog.Debugf("\tSuccessfully removed group %s from the system\n", task.GroupName)
}
// Group doesn't exist
} else if byName == nil {
waterlog.Debugf("\tGroup %s doesn't exist, skipping\n", task.GroupName)
}
}

func (m *Migration) deleteUsers(context *Context, task *DeleteUsers) {

var byName *User

for _, user := range context.users {
if user.Name == task.UserName {
user := user
byName = &user
if ran, err := context.DeleteUser(&user); err != nil {
waterlog.Warnf("\tFailed to delete user %s from system due to error: %s\n", task.UserName, err)
} else if ran {
waterlog.Debugf("\tSuccessfully deleted user %s from the system\n", task.UserName)
} else {
waterlog.Warnf("\tUnknown error occured when deleting user %s from system, err %s\n", task.UserName, err)
}
}
}

if byName == nil {
waterlog.Debugf("\tUser %s doesn't exist on the system, skipping\n", task.UserName)
}
}

0 comments on commit 0a16b8f

Please sign in to comment.