1 /* vi: set sw=4 ts=4: */
5 * Written for SLIND (from passwd.c) by Alexander Shishkin <virtuoso@slind.org>
6 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
11 #if ENABLE_GETOPT_LONG
12 static const char chpasswd_longopts[] ALIGN1 =
13 "encrypted\0" No_argument "e"
14 "md5\0" No_argument "m"
21 int chpasswd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
22 int chpasswd_main(int argc UNUSED_PARAM, char **argv)
25 char salt[sizeof("$N$XXXXXXXX")];
27 int rnd = rnd; /* we *want* it to be non-initialized! */
30 bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
32 opt_complementary = "m--e:e--m";
33 USE_GETOPT_LONG(applet_long_options = chpasswd_longopts;)
34 opt = getopt32(argv, "em");
36 while ((name = xmalloc_fgetline(stdin)) != NULL) {
37 pass = strchr(name, ':');
39 bb_error_msg_and_die("missing new password");
42 xuname2uid(name); /* dies if there is no such user */
44 if (!(opt & OPT_ENC)) {
45 rnd = crypt_make_salt(salt, 1, rnd);
48 rnd = crypt_make_salt(salt + 3, 4, rnd);
50 pass = pw_encrypt(pass, salt, 0);
53 /* This is rather complex: if user is not found in /etc/shadow,
54 * we try to find & change his passwd in /etc/passwd */
55 #if ENABLE_FEATURE_SHADOWPASSWDS
56 rc = update_passwd(bb_path_shadow_file, name, pass);
57 if (rc == 0) /* no lines updated, no errors detected */
59 rc = update_passwd(bb_path_passwd_file, name, pass);
60 /* LOGMODE_BOTH logs to syslog also */
61 logmode = LOGMODE_BOTH;
63 bb_error_msg_and_die("an error occurred updating password for %s", name);
65 bb_info_msg("Password for '%s' changed", name);
66 logmode = LOGMODE_STDIO;