6c4296faa0ff916e48bbe59a9b1adf1f7f6a6ec8
[oweals/busybox.git] / loginutils / chpasswd.c
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 //usage:#define chpasswd_trivial_usage
11 //usage:        IF_LONG_OPTS("[--md5|--encrypted]") IF_NOT_LONG_OPTS("[-m|-e]")
12 //usage:#define chpasswd_full_usage "\n\n"
13 //usage:       "Read user:password from stdin and update /etc/passwd\n"
14 //usage:     "\nOptions:"
15 //usage:        IF_LONG_OPTS(
16 //usage:     "\n        -e,--encrypted  Supplied passwords are in encrypted form"
17 //usage:     "\n        -m,--md5        Use MD5 encryption instead of DES"
18 //usage:        )
19 //usage:        IF_NOT_LONG_OPTS(
20 //usage:     "\n        -e      Supplied passwords are in encrypted form"
21 //usage:     "\n        -m      Use MD5 encryption instead of DES"
22 //usage:        )
23
24 #if ENABLE_LONG_OPTS
25 static const char chpasswd_longopts[] ALIGN1 =
26         "encrypted\0" No_argument "e"
27         "md5\0"       No_argument "m"
28         ;
29 #endif
30
31 #define OPT_ENC  1
32 #define OPT_MD5  2
33
34 int chpasswd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
35 int chpasswd_main(int argc UNUSED_PARAM, char **argv)
36 {
37         char *name, *pass;
38         char salt[sizeof("$N$XXXXXXXX")];
39         int opt, rc;
40         int rnd = rnd; /* we *want* it to be non-initialized! */
41
42         if (getuid())
43                 bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
44
45         opt_complementary = "m--e:e--m";
46         IF_LONG_OPTS(applet_long_options = chpasswd_longopts;)
47         opt = getopt32(argv, "em");
48
49         while ((name = xmalloc_fgetline(stdin)) != NULL) {
50                 pass = strchr(name, ':');
51                 if (!pass)
52                         bb_error_msg_and_die("missing new password");
53                 *pass++ = '\0';
54
55                 xuname2uid(name); /* dies if there is no such user */
56
57                 if (!(opt & OPT_ENC)) {
58                         rnd = crypt_make_salt(salt, 1, rnd);
59                         if (opt & OPT_MD5) {
60                                 strcpy(salt, "$1$");
61                                 rnd = crypt_make_salt(salt + 3, 4, rnd);
62                         }
63                         pass = pw_encrypt(pass, salt, 0);
64                 }
65
66                 /* This is rather complex: if user is not found in /etc/shadow,
67                  * we try to find & change his passwd in /etc/passwd */
68 #if ENABLE_FEATURE_SHADOWPASSWDS
69                 rc = update_passwd(bb_path_shadow_file, name, pass, NULL);
70                 if (rc > 0) /* password in /etc/shadow was updated */
71                         pass = (char*)"x";
72                 if (rc >= 0)
73                         /* 0 = /etc/shadow missing (not an error), >0 = passwd changed in /etc/shadow */
74 #endif
75                         rc = update_passwd(bb_path_passwd_file, name, pass, NULL);
76                 /* LOGMODE_BOTH logs to syslog also */
77                 logmode = LOGMODE_BOTH;
78                 if (rc < 0)
79                         bb_error_msg_and_die("an error occurred updating password for %s", name);
80                 if (rc)
81                         bb_info_msg("Password for '%s' changed", name);
82                 logmode = LOGMODE_STDIO;
83                 free(name);
84                 if (!(opt & OPT_ENC))
85                         free(pass);
86         }
87         return EXIT_SUCCESS;
88 }