From 557fb713e0f943ac9b87c9f3804ba24e73d55bb0 Mon Sep 17 00:00:00 2001 From: Denis Vlasenko Date: Sat, 21 Jul 2007 13:25:28 +0000 Subject: chpasswd: fixes and code shrink update_passwd 732 734 +2 chpasswd_main 318 292 -26 ------------------------------------------------------------------------------ (add/remove: 0/0 grow/shrink: 1/1 up/down: 2/-26) Total: -24 bytes text data bss dec hex filename 781564 1168 11900 794632 c2008 busybox_old 781548 1168 11900 794616 c1ff8 busybox_unstripped --- loginutils/chpasswd.c | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-) (limited to 'loginutils/chpasswd.c') diff --git a/loginutils/chpasswd.c b/loginutils/chpasswd.c index 124fc86e2..d5de424f0 100644 --- a/loginutils/chpasswd.c +++ b/loginutils/chpasswd.c @@ -26,19 +26,13 @@ int chpasswd_main(int argc, char **argv) { char *name, *pass; char salt[sizeof("$N$XXXXXXXX")]; - int opt, rc; + int opt; int rnd = rnd; /* we *want* it to be non-initialized! */ - const char *pwfile = bb_path_passwd_file; - if (getuid() != 0) + if (getuid()) bb_error_msg_and_die(bb_msg_perm_denied_are_you_root); -#if ENABLE_FEATURE_SHADOWPASSWDS - if (access(bb_path_shadow_file, F_OK) == 0) - pwfile = bb_path_shadow_file; -#endif - - opt_complementary = "m--e"; + opt_complementary = "?m--e:e--m"; USE_GETOPT_LONG(applet_long_options = chpasswd_opts;) opt = getopt32(argc, argv, "em"); @@ -48,8 +42,7 @@ int chpasswd_main(int argc, char **argv) bb_error_msg_and_die("missing new password"); *pass++ = '\0'; - //if (!getpwnam(name)) - // bb_error_msg_and_die("unknown user %s", name); + xuname2uid(name); /* dies if there is no such user */ if (!(opt & OPT_ENC)) { rnd = crypt_make_salt(salt, 1, rnd); @@ -60,15 +53,17 @@ int chpasswd_main(int argc, char **argv) pass = pw_encrypt(pass, salt); } - rc = update_passwd(pwfile, name, pass); /* LOGMODE_BOTH logs to syslog */ logmode = LOGMODE_BOTH; - if (rc < 0) - bb_error_msg_and_die("an error occurred updating %s", pwfile); - if (rc > 0) + + if ((ENABLE_FEATURE_SHADOWPASSWDS + && !update_passwd(bb_path_shadow_file, name, pass)) + || !update_passwd(bb_path_passwd_file, name, pass) + ) { + bb_error_msg_and_die("an error occurred updating password for %s", name); + } else { bb_info_msg("Password for '%s' changed", name); - else - bb_info_msg("User '%s' not found", name); + } logmode = LOGMODE_STDIO; free(name); } -- cgit v1.2.3