Denis Vlasenko | 1ec5eae | 2007-07-20 21:29:49 +0000 | [diff] [blame] | 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 tarball for details. |
| 7 | */ |
| 8 | |
| 9 | #include "libbb.h" |
| 10 | |
| 11 | #if ENABLE_GETOPT_LONG |
| 12 | #include <getopt.h> |
| 13 | |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 14 | static const char chpasswd_longopts[] ALIGN1 = |
Denis Vlasenko | bdc88fd | 2007-07-23 17:14:14 +0000 | [diff] [blame] | 15 | "encrypted\0" No_argument "e" |
| 16 | "md5\0" No_argument "m" |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 17 | ; |
Denis Vlasenko | 1ec5eae | 2007-07-20 21:29:49 +0000 | [diff] [blame] | 18 | #endif |
| 19 | |
| 20 | #define OPT_ENC 1 |
| 21 | #define OPT_MD5 2 |
| 22 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 23 | int chpasswd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame^] | 24 | int chpasswd_main(int argc ATTRIBUTE_UNUSED, char **argv) |
Denis Vlasenko | 1ec5eae | 2007-07-20 21:29:49 +0000 | [diff] [blame] | 25 | { |
| 26 | char *name, *pass; |
| 27 | char salt[sizeof("$N$XXXXXXXX")]; |
Denis Vlasenko | e8feca0 | 2007-07-21 13:54:26 +0000 | [diff] [blame] | 28 | int opt, rc; |
Denis Vlasenko | 1ec5eae | 2007-07-20 21:29:49 +0000 | [diff] [blame] | 29 | int rnd = rnd; /* we *want* it to be non-initialized! */ |
Denis Vlasenko | 1ec5eae | 2007-07-20 21:29:49 +0000 | [diff] [blame] | 30 | |
Denis Vlasenko | 557fb71 | 2007-07-21 13:25:28 +0000 | [diff] [blame] | 31 | if (getuid()) |
Denis Vlasenko | 1ec5eae | 2007-07-20 21:29:49 +0000 | [diff] [blame] | 32 | bb_error_msg_and_die(bb_msg_perm_denied_are_you_root); |
| 33 | |
Denis Vlasenko | 6a2f7f4 | 2007-08-16 10:35:17 +0000 | [diff] [blame] | 34 | opt_complementary = "m--e:e--m"; |
Denis Vlasenko | 64a8a3b | 2007-07-23 18:49:16 +0000 | [diff] [blame] | 35 | USE_GETOPT_LONG(applet_long_options = chpasswd_longopts;) |
Denis Vlasenko | fe7cd64 | 2007-08-18 15:32:12 +0000 | [diff] [blame] | 36 | opt = getopt32(argv, "em"); |
Denis Vlasenko | 1ec5eae | 2007-07-20 21:29:49 +0000 | [diff] [blame] | 37 | |
| 38 | while ((name = xmalloc_getline(stdin)) != NULL) { |
| 39 | pass = strchr(name, ':'); |
| 40 | if (!pass) |
| 41 | bb_error_msg_and_die("missing new password"); |
| 42 | *pass++ = '\0'; |
| 43 | |
Denis Vlasenko | 557fb71 | 2007-07-21 13:25:28 +0000 | [diff] [blame] | 44 | xuname2uid(name); /* dies if there is no such user */ |
Denis Vlasenko | 1ec5eae | 2007-07-20 21:29:49 +0000 | [diff] [blame] | 45 | |
| 46 | if (!(opt & OPT_ENC)) { |
| 47 | rnd = crypt_make_salt(salt, 1, rnd); |
| 48 | if (opt & OPT_MD5) { |
| 49 | strcpy(salt, "$1$"); |
| 50 | rnd = crypt_make_salt(salt + 3, 4, rnd); |
| 51 | } |
| 52 | pass = pw_encrypt(pass, salt); |
| 53 | } |
| 54 | |
Denis Vlasenko | e8feca0 | 2007-07-21 13:54:26 +0000 | [diff] [blame] | 55 | /* This is rather complex: if user is not found in /etc/shadow, |
| 56 | * we try to find & change his passwd in /etc/passwd */ |
| 57 | #if ENABLE_FEATURE_SHADOWPASSWDS |
Denis Vlasenko | 845db2a | 2007-07-21 22:29:26 +0000 | [diff] [blame] | 58 | rc = update_passwd(bb_path_shadow_file, name, pass); |
Denis Vlasenko | e8feca0 | 2007-07-21 13:54:26 +0000 | [diff] [blame] | 59 | if (rc == 0) /* no lines updated, no errors detected */ |
| 60 | #endif |
| 61 | rc = update_passwd(bb_path_passwd_file, name, pass); |
| 62 | /* LOGMODE_BOTH logs to syslog also */ |
Denis Vlasenko | 1ec5eae | 2007-07-20 21:29:49 +0000 | [diff] [blame] | 63 | logmode = LOGMODE_BOTH; |
Denis Vlasenko | e8feca0 | 2007-07-21 13:54:26 +0000 | [diff] [blame] | 64 | if (rc < 0) |
Denis Vlasenko | 557fb71 | 2007-07-21 13:25:28 +0000 | [diff] [blame] | 65 | bb_error_msg_and_die("an error occurred updating password for %s", name); |
Denis Vlasenko | e8feca0 | 2007-07-21 13:54:26 +0000 | [diff] [blame] | 66 | if (rc) |
Denis Vlasenko | 1ec5eae | 2007-07-20 21:29:49 +0000 | [diff] [blame] | 67 | bb_info_msg("Password for '%s' changed", name); |
Denis Vlasenko | 1ec5eae | 2007-07-20 21:29:49 +0000 | [diff] [blame] | 68 | logmode = LOGMODE_STDIO; |
| 69 | free(name); |
| 70 | } |
| 71 | |
| 72 | return 0; |
| 73 | } |