[3320] | 1 | /* vi: set sw=4 ts=4: */
|
---|
| 2 | /*
|
---|
| 3 | * chpasswd.c
|
---|
| 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 source tree.
|
---|
| 7 | */
|
---|
| 8 | #include "libbb.h"
|
---|
| 9 |
|
---|
| 10 | #if ENABLE_LONG_OPTS
|
---|
| 11 | static const char chpasswd_longopts[] ALIGN1 =
|
---|
| 12 | "encrypted\0" No_argument "e"
|
---|
| 13 | "md5\0" No_argument "m"
|
---|
| 14 | ;
|
---|
| 15 | #endif
|
---|
| 16 |
|
---|
| 17 | #define OPT_ENC 1
|
---|
| 18 | #define OPT_MD5 2
|
---|
| 19 |
|
---|
| 20 | int chpasswd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
---|
| 21 | int chpasswd_main(int argc UNUSED_PARAM, char **argv)
|
---|
| 22 | {
|
---|
| 23 | char *name, *pass;
|
---|
| 24 | char salt[sizeof("$N$XXXXXXXX")];
|
---|
| 25 | int opt, rc;
|
---|
| 26 | int rnd = rnd; /* we *want* it to be non-initialized! */
|
---|
| 27 |
|
---|
| 28 | if (getuid())
|
---|
| 29 | bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
|
---|
| 30 |
|
---|
| 31 | opt_complementary = "m--e:e--m";
|
---|
| 32 | IF_LONG_OPTS(applet_long_options = chpasswd_longopts;)
|
---|
| 33 | opt = getopt32(argv, "em");
|
---|
| 34 |
|
---|
| 35 | while ((name = xmalloc_fgetline(stdin)) != NULL) {
|
---|
| 36 | pass = strchr(name, ':');
|
---|
| 37 | if (!pass)
|
---|
| 38 | bb_error_msg_and_die("missing new password");
|
---|
| 39 | *pass++ = '\0';
|
---|
| 40 |
|
---|
| 41 | xuname2uid(name); /* dies if there is no such user */
|
---|
| 42 |
|
---|
| 43 | if (!(opt & OPT_ENC)) {
|
---|
| 44 | rnd = crypt_make_salt(salt, 1, rnd);
|
---|
| 45 | if (opt & OPT_MD5) {
|
---|
| 46 | strcpy(salt, "$1$");
|
---|
| 47 | rnd = crypt_make_salt(salt + 3, 4, rnd);
|
---|
| 48 | }
|
---|
| 49 | pass = pw_encrypt(pass, salt, 0);
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | /* This is rather complex: if user is not found in /etc/shadow,
|
---|
| 53 | * we try to find & change his passwd in /etc/passwd */
|
---|
| 54 | #if ENABLE_FEATURE_SHADOWPASSWDS
|
---|
| 55 | rc = update_passwd(bb_path_shadow_file, name, pass, NULL);
|
---|
| 56 | if (rc == 0) /* no lines updated, no errors detected */
|
---|
| 57 | #endif
|
---|
| 58 | rc = update_passwd(bb_path_passwd_file, name, pass, NULL);
|
---|
| 59 | /* LOGMODE_BOTH logs to syslog also */
|
---|
| 60 | logmode = LOGMODE_BOTH;
|
---|
| 61 | if (rc < 0)
|
---|
| 62 | bb_error_msg_and_die("an error occurred updating password for %s", name);
|
---|
| 63 | if (rc)
|
---|
| 64 | bb_info_msg("Password for '%s' changed", name);
|
---|
| 65 | logmode = LOGMODE_STDIO;
|
---|
| 66 | free(name);
|
---|
| 67 | if (!(opt & OPT_ENC))
|
---|
| 68 | free(pass);
|
---|
| 69 | }
|
---|
| 70 | return EXIT_SUCCESS;
|
---|
| 71 | }
|
---|