From 2c57c399bf0d2f06dc8a8fed244ec80667a671f1 Mon Sep 17 00:00:00 2001 From: Tomas Mraz Date: Thu, 21 Dec 2017 09:12:58 +0100 Subject: [PATCH] Make userdel to work with -R. The userdel checks for users with getpwnam() which might not work properly in chroot. Check for the user's presence in local files only. --- src/userdel.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/userdel.c b/src/userdel.c index 9092b5c1..84486049 100644 --- a/src/userdel.c +++ b/src/userdel.c @@ -96,6 +96,7 @@ static char *user_home; static bool fflg = false; static bool rflg = false; static bool Zflg = false; +static bool Rflg = false; static bool is_shadow_pwd; @@ -1029,6 +1030,7 @@ int main (int argc, char **argv) rflg = true; break; case 'R': /* no-op, handled in process_root_flag () */ + Rflg = true; break; #ifdef WITH_SELINUX case 'Z': @@ -1103,9 +1105,12 @@ int main (int argc, char **argv) */ user_name = argv[argc - 1]; { - struct passwd *pwd; - pwd = getpwnam (user_name); /* local, no need for xgetpwnam */ + const struct passwd *pwd; + + pw_open(O_RDONLY); + pwd = pw_locate (user_name); /* we care only about local users */ if (NULL == pwd) { + pw_close(); fprintf (stderr, _("%s: user '%s' does not exist\n"), Prog, user_name); #ifdef WITH_AUDIT @@ -1119,6 +1124,7 @@ int main (int argc, char **argv) user_id = pwd->pw_uid; user_gid = pwd->pw_gid; user_home = xstrdup (pwd->pw_dir); + pw_close(); } #ifdef WITH_TCB if (shadowtcb_set_user (user_name) == SHADOWTCB_FAILURE) { @@ -1150,7 +1156,7 @@ int main (int argc, char **argv) * Note: This is a best effort basis. The user may log in between, * a cron job may be started on her behalf, etc. */ - if (user_busy (user_name, user_id) != 0) { + if (!Rflg && user_busy (user_name, user_id) != 0) { if (!fflg) { #ifdef WITH_AUDIT audit_logger (AUDIT_DEL_USER, Prog,