Skip to content

Don't overwrite shell when updating user entry #889

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

Merged
merged 2 commits into from
Apr 25, 2025
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
8 changes: 8 additions & 0 deletions internal/users/db/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,13 @@ func TestUpdateUserEntry(t *testing.T) {
Dir: "/new/home/user1",
Shell: "/bin/bash",
},
"user1-new-shell": {
Name: "user1",
UID: 1111,
Gecos: "User1 gecos\nOn multiple lines",
Dir: "/new/home/user1",
Shell: "/new/shell",
},
"user1-without-gecos": {
Name: "user1",
UID: 1111,
Expand Down Expand Up @@ -185,6 +192,7 @@ func TestUpdateUserEntry(t *testing.T) {
// User and Group updates
"Update_user_by_changing_attributes": {userCase: "user1-new-attributes", dbFile: "one_user_and_group"},
"Update_user_does_not_change_homedir_if_it_exists": {userCase: "user1-new-homedir", dbFile: "one_user_and_group"},
"Update_user_does_not_change_shell_if_it_exists": {userCase: "user1-new-shell", dbFile: "one_user_and_group"},
"Update_user_by_removing_optional_gecos_field_if_not_set": {userCase: "user1-without-gecos", dbFile: "one_user_and_group"},

// Group updates
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ users:
gid: 11111
gecos: New user1 gecos
dir: /home/user1
shell: /bin/dash
shell: /bin/bash
groups:
- name: group1
gid: 11111
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
users:
- name: user1
uid: 1111
gid: 11111
gecos: |-
User1 gecos
On multiple lines
dir: /home/user1
shell: /bin/bash
groups:
- name: group1
gid: 11111
ugid: "12345678"
users_to_groups:
- uid: 1111
gid: 11111
9 changes: 8 additions & 1 deletion internal/users/db/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ func (m *Manager) UpdateUserEntry(user UserRow, authdGroups []GroupRow, localGro

// handleUserUpdate updates the user record in the database.
func handleUserUpdate(db queryable, u UserRow) error {
log.Debugf(context.Background(), "Updating entry of user %q (UID: %d)", u.Name, u.UID)

existingUser, err := userByID(db, u.UID)
if err != nil && !errors.Is(err, NoDataFoundError{}) {
return err
Expand All @@ -64,7 +66,12 @@ func handleUserUpdate(db queryable, u UserRow) error {
u.Dir = existingUser.Dir
}

log.Debugf(context.Background(), "Updating entry of user %q (UID: %d)", u.Name, u.UID)
// Ensure that we use the same shell as the one we have in the database.
if existingUser.Shell != "" && existingUser.Shell != u.Shell {
log.Debugf(context.TODO(), "Not updating shell to %q because it's already set to %q", u.Shell, existingUser.Shell)
u.Shell = existingUser.Shell
}

return insertOrUpdateUserByID(db, u)
}

Expand Down
Loading