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