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
14 static const struct option chpasswd_opts[] = {
15 { "encrypted", no_argument, NULL, 'e' },
16 { "md5", no_argument, NULL, 'm' },
24 int chpasswd_main(int argc, char **argv);
25 int chpasswd_main(int argc, char **argv)
28 char salt[sizeof("$N$XXXXXXXX")];
30 int rnd = rnd; /* we *want* it to be non-initialized! */
33 bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
35 opt_complementary = "m--e:e--m";
36 USE_GETOPT_LONG(applet_long_options = chpasswd_opts;)
37 opt = getopt32(argc, argv, "em");
39 while ((name = xmalloc_getline(stdin)) != NULL) {
40 pass = strchr(name, ':');
42 bb_error_msg_and_die("missing new password");
45 xuname2uid(name); /* dies if there is no such user */
47 if (!(opt & OPT_ENC)) {
48 rnd = crypt_make_salt(salt, 1, rnd);
51 rnd = crypt_make_salt(salt + 3, 4, rnd);
53 pass = pw_encrypt(pass, salt);
56 /* This is rather complex: if user is not found in /etc/shadow,
57 * we try to find & change his passwd in /etc/passwd */
58 #if ENABLE_FEATURE_SHADOWPASSWDS
59 rc = update_passwd(bb_path_shadow_file, name, pass);
60 if (rc == 0) /* no lines updated, no errors detected */
62 rc = update_passwd(bb_path_passwd_file, name, pass);
63 /* LOGMODE_BOTH logs to syslog also */
64 logmode = LOGMODE_BOTH;
66 bb_error_msg_and_die("an error occurred updating password for %s", name);
68 bb_info_msg("Password for '%s' changed", name);
69 logmode = LOGMODE_STDIO;