*: rename ATTRIBUTE_XXX to just XXX.
[platform/upstream/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 tarball for details.
7  */
8
9 #include "libbb.h"
10
11 #if ENABLE_GETOPT_LONG
12 static const char chpasswd_longopts[] ALIGN1 =
13         "encrypted\0" No_argument "e"
14         "md5\0"       No_argument "m"
15         ;
16 #endif
17
18 #define OPT_ENC         1
19 #define OPT_MD5         2
20
21 int chpasswd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
22 int chpasswd_main(int argc UNUSED_PARAM, char **argv)
23 {
24         char *name, *pass;
25         char salt[sizeof("$N$XXXXXXXX")];
26         int opt, rc;
27         int rnd = rnd; /* we *want* it to be non-initialized! */
28
29         if (getuid())
30                 bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
31
32         opt_complementary = "m--e:e--m";
33         USE_GETOPT_LONG(applet_long_options = chpasswd_longopts;)
34         opt = getopt32(argv, "em");
35
36         while ((name = xmalloc_fgetline(stdin)) != NULL) {
37                 pass = strchr(name, ':');
38                 if (!pass)
39                         bb_error_msg_and_die("missing new password");
40                 *pass++ = '\0';
41
42                 xuname2uid(name); /* dies if there is no such user */
43
44                 if (!(opt & OPT_ENC)) {
45                         rnd = crypt_make_salt(salt, 1, rnd);
46                         if (opt & OPT_MD5) {
47                                 strcpy(salt, "$1$");
48                                 rnd = crypt_make_salt(salt + 3, 4, rnd);
49                         }
50                         pass = pw_encrypt(pass, salt, 0);
51                 }
52
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 */
58 #endif
59                         rc = update_passwd(bb_path_passwd_file, name, pass);
60                 /* LOGMODE_BOTH logs to syslog also */
61                 logmode = LOGMODE_BOTH;
62                 if (rc < 0)
63                         bb_error_msg_and_die("an error occurred updating password for %s", name);
64                 if (rc)
65                         bb_info_msg("Password for '%s' changed", name);
66                 logmode = LOGMODE_STDIO;
67                 free(name);
68                 free(pass);
69         }
70
71         return 0;
72 }