loginutils/*: convert to new-style "one file" applets
[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 //config:config CHPASSWD
9 //config:       bool "chpasswd"
10 //config:       default y
11 //config:       help
12 //config:         Reads a file of user name and password pairs from standard input
13 //config:         and uses this information to update a group of existing users.
14 //config:
15 //config:config FEATURE_DEFAULT_PASSWD_ALGO
16 //config:       string "Default password encryption method (passwd -a, cryptpw -m parameter)"
17 //config:       default "des"
18 //config:       depends on PASSWD || CRYPTPW
19 //config:       help
20 //config:         Possible choices are "d[es]", "m[d5]", "s[ha256]" or "sha512".
21
22 //applet:IF_CHPASSWD(APPLET(chpasswd, BB_DIR_USR_SBIN, BB_SUID_DROP))
23
24 //kbuild:lib-$(CONFIG_CHPASSWD) += chpasswd.o
25
26 //usage:#define chpasswd_trivial_usage
27 //usage:        IF_LONG_OPTS("[--md5|--encrypted]") IF_NOT_LONG_OPTS("[-m|-e]")
28 //usage:#define chpasswd_full_usage "\n\n"
29 //usage:       "Read user:password from stdin and update /etc/passwd\n"
30 //usage:        IF_LONG_OPTS(
31 //usage:     "\n        -e,--encrypted  Supplied passwords are in encrypted form"
32 //usage:     "\n        -m,--md5        Use MD5 encryption instead of DES"
33 //usage:        )
34 //usage:        IF_NOT_LONG_OPTS(
35 //usage:     "\n        -e      Supplied passwords are in encrypted form"
36 //usage:     "\n        -m      Use MD5 encryption instead of DES"
37 //usage:        )
38
39 //TODO: implement -c ALGO
40
41 #include "libbb.h"
42
43 #if ENABLE_LONG_OPTS
44 static const char chpasswd_longopts[] ALIGN1 =
45         "encrypted\0" No_argument "e"
46         "md5\0"       No_argument "m"
47         ;
48 #endif
49
50 #define OPT_ENC  1
51 #define OPT_MD5  2
52
53 int chpasswd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
54 int chpasswd_main(int argc UNUSED_PARAM, char **argv)
55 {
56         char *name;
57         int opt;
58
59         if (getuid() != 0)
60                 bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
61
62         opt_complementary = "m--e:e--m";
63         IF_LONG_OPTS(applet_long_options = chpasswd_longopts;)
64         opt = getopt32(argv, "em");
65
66         while ((name = xmalloc_fgetline(stdin)) != NULL) {
67                 char *free_me;
68                 char *pass;
69                 int rc;
70
71                 pass = strchr(name, ':');
72                 if (!pass)
73                         bb_error_msg_and_die("missing new password");
74                 *pass++ = '\0';
75
76                 xuname2uid(name); /* dies if there is no such user */
77
78                 free_me = NULL;
79                 if (!(opt & OPT_ENC)) {
80                         char salt[sizeof("$N$XXXXXXXX")];
81
82                         crypt_make_salt(salt, 1);
83                         if (opt & OPT_MD5) {
84                                 salt[0] = '$';
85                                 salt[1] = '1';
86                                 salt[2] = '$';
87                                 crypt_make_salt(salt + 3, 4);
88                         }
89                         free_me = pass = pw_encrypt(pass, salt, 0);
90                 }
91
92                 /* This is rather complex: if user is not found in /etc/shadow,
93                  * we try to find & change his passwd in /etc/passwd */
94 #if ENABLE_FEATURE_SHADOWPASSWDS
95                 rc = update_passwd(bb_path_shadow_file, name, pass, NULL);
96                 if (rc > 0) /* password in /etc/shadow was updated */
97                         pass = (char*)"x";
98                 if (rc >= 0)
99                         /* 0 = /etc/shadow missing (not an error), >0 = passwd changed in /etc/shadow */
100 #endif
101                         rc = update_passwd(bb_path_passwd_file, name, pass, NULL);
102                 /* LOGMODE_BOTH logs to syslog also */
103                 logmode = LOGMODE_BOTH;
104                 if (rc < 0)
105                         bb_error_msg_and_die("an error occurred updating password for %s", name);
106                 if (rc)
107                         bb_info_msg("Password for '%s' changed", name);
108                 logmode = LOGMODE_STDIO;
109                 free(name);
110                 free(free_me);
111         }
112         return EXIT_SUCCESS;
113 }