brctl: fix build failure by s/strtotimeval/bb_strtotimeval/ (android has strtotimeval)
[oweals/busybox.git] / loginutils / passwd.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Licensed under GPLv2 or later, see file LICENSE in this source tree.
4  */
5
6 //usage:#define passwd_trivial_usage
7 //usage:       "[OPTIONS] [USER]"
8 //usage:#define passwd_full_usage "\n\n"
9 //usage:       "Change USER's password (default: current user)"
10 //usage:     "\n"
11 //usage:     "\n        -a ALG  Encryption method"
12 //usage:     "\n        -d      Set password to ''"
13 //usage:     "\n        -l      Lock (disable) account"
14 //usage:     "\n        -u      Unlock (enable) account"
15
16 #include "libbb.h"
17 #include <syslog.h>
18
19 static void nuke_str(char *str)
20 {
21         if (str) memset(str, 0, strlen(str));
22 }
23
24 static char* new_password(const struct passwd *pw, uid_t myuid, const char *algo)
25 {
26         char salt[MAX_PW_SALT_LEN];
27         char *orig = (char*)"";
28         char *newp = NULL;
29         char *cp = NULL;
30         char *ret = NULL; /* failure so far */
31
32         if (myuid != 0 && pw->pw_passwd[0]) {
33                 char *encrypted;
34
35                 orig = bb_ask_stdin("Old password: "); /* returns ptr to static */
36                 if (!orig)
37                         goto err_ret;
38                 encrypted = pw_encrypt(orig, pw->pw_passwd, 1); /* returns malloced str */
39                 if (strcmp(encrypted, pw->pw_passwd) != 0) {
40                         syslog(LOG_WARNING, "incorrect password for %s", pw->pw_name);
41                         bb_do_delay(LOGIN_FAIL_DELAY);
42                         puts("Incorrect password");
43                         goto err_ret;
44                 }
45                 if (ENABLE_FEATURE_CLEAN_UP)
46                         free(encrypted);
47         }
48         orig = xstrdup(orig); /* or else bb_ask_stdin() will destroy it */
49         newp = bb_ask_stdin("New password: "); /* returns ptr to static */
50         if (!newp)
51                 goto err_ret;
52         newp = xstrdup(newp); /* we are going to bb_ask_stdin() again, so save it */
53         if (ENABLE_FEATURE_PASSWD_WEAK_CHECK
54          && obscure(orig, newp, pw)
55          && myuid != 0
56         ) {
57                 goto err_ret; /* non-root is not allowed to have weak passwd */
58         }
59
60         cp = bb_ask_stdin("Retype password: ");
61         if (!cp)
62                 goto err_ret;
63         if (strcmp(cp, newp) != 0) {
64                 puts("Passwords don't match");
65                 goto err_ret;
66         }
67
68         crypt_make_pw_salt(salt, algo);
69
70         /* pw_encrypt returns malloced str */
71         ret = pw_encrypt(newp, salt, 1);
72         /* whee, success! */
73
74  err_ret:
75         nuke_str(orig);
76         if (ENABLE_FEATURE_CLEAN_UP) free(orig);
77
78         nuke_str(newp);
79         if (ENABLE_FEATURE_CLEAN_UP) free(newp);
80
81         nuke_str(cp);
82         return ret;
83 }
84
85 int passwd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
86 int passwd_main(int argc UNUSED_PARAM, char **argv)
87 {
88         enum {
89                 OPT_algo   = (1 << 0), /* -a - password algorithm */
90                 OPT_lock   = (1 << 1), /* -l - lock account */
91                 OPT_unlock = (1 << 2), /* -u - unlock account */
92                 OPT_delete = (1 << 3), /* -d - delete password */
93                 OPT_lud    = OPT_lock | OPT_unlock | OPT_delete,
94         };
95         unsigned opt;
96         int rc;
97         const char *opt_a = CONFIG_FEATURE_DEFAULT_PASSWD_ALGO;
98         const char *filename;
99         char *myname;
100         char *name;
101         char *newp;
102         struct passwd *pw;
103         uid_t myuid;
104         struct rlimit rlimit_fsize;
105         char c;
106 #if ENABLE_FEATURE_SHADOWPASSWDS
107         /* Using _r function to avoid pulling in static buffers */
108         struct spwd spw;
109         char buffer[256];
110 #endif
111
112         logmode = LOGMODE_BOTH;
113         openlog(applet_name, 0, LOG_AUTH);
114         opt = getopt32(argv, "a:lud", &opt_a);
115         //argc -= optind;
116         argv += optind;
117
118         myuid = getuid();
119         /* -l, -u, -d require root priv and username argument */
120         if ((opt & OPT_lud) && (myuid != 0 || !argv[0]))
121                 bb_show_usage();
122
123         /* Will complain and die if username not found */
124         myname = xstrdup(xuid2uname(myuid));
125         name = argv[0] ? argv[0] : myname;
126
127         pw = xgetpwnam(name);
128         if (myuid != 0 && pw->pw_uid != myuid) {
129                 /* LOGMODE_BOTH */
130                 bb_error_msg_and_die("%s can't change password for %s", myname, name);
131         }
132
133 #if ENABLE_FEATURE_SHADOWPASSWDS
134         {
135                 /* getspnam_r may return 0 yet set result to NULL.
136                  * At least glibc 2.4 does this. Be extra paranoid here. */
137                 struct spwd *result = NULL;
138                 errno = 0;
139                 if (getspnam_r(pw->pw_name, &spw, buffer, sizeof(buffer), &result) != 0
140                  || !result /* no error, but no record found either */
141                  || strcmp(result->sp_namp, pw->pw_name) != 0 /* paranoia */
142                 ) {
143                         if (errno != ENOENT) {
144                                 /* LOGMODE_BOTH */
145                                 bb_perror_msg("no record of %s in %s, using %s",
146                                         name, bb_path_shadow_file,
147                                         bb_path_passwd_file);
148                         }
149                         /* else: /etc/shadow does not exist,
150                          * apparently we are on a shadow-less system,
151                          * no surprise there */
152                 } else {
153                         pw->pw_passwd = result->sp_pwdp;
154                 }
155         }
156 #endif
157
158         /* Decide what the new password will be */
159         newp = NULL;
160         c = pw->pw_passwd[0] - '!';
161         if (!(opt & OPT_lud)) {
162                 if (myuid != 0 && !c) { /* passwd starts with '!' */
163                         /* LOGMODE_BOTH */
164                         bb_error_msg_and_die("can't change "
165                                         "locked password for %s", name);
166                 }
167                 printf("Changing password for %s\n", name);
168                 newp = new_password(pw, myuid, opt_a);
169                 if (!newp) {
170                         logmode = LOGMODE_STDIO;
171                         bb_error_msg_and_die("password for %s is unchanged", name);
172                 }
173         } else if (opt & OPT_lock) {
174                 if (!c)
175                         goto skip; /* passwd starts with '!' */
176                 newp = xasprintf("!%s", pw->pw_passwd);
177         } else if (opt & OPT_unlock) {
178                 if (c)
179                         goto skip; /* not '!' */
180                 /* pw->pw_passwd points to static storage,
181                  * strdup'ing to avoid nasty surprizes */
182                 newp = xstrdup(&pw->pw_passwd[1]);
183         } else if (opt & OPT_delete) {
184                 newp = (char*)"";
185         }
186
187         rlimit_fsize.rlim_cur = rlimit_fsize.rlim_max = 512L * 30000;
188         setrlimit(RLIMIT_FSIZE, &rlimit_fsize);
189         bb_signals(0
190                 + (1 << SIGHUP)
191                 + (1 << SIGINT)
192                 + (1 << SIGQUIT)
193                 , SIG_IGN);
194         umask(077);
195         xsetuid(0);
196
197 #if ENABLE_FEATURE_SHADOWPASSWDS
198         filename = bb_path_shadow_file;
199         rc = update_passwd(bb_path_shadow_file, name, newp, NULL);
200         if (rc > 0)
201                 /* password in /etc/shadow was updated */
202                 newp = (char*) "x";
203         if (rc >= 0)
204                 /* 0 = /etc/shadow missing (not an error), >0 = passwd changed in /etc/shadow */
205 #endif
206         {
207                 filename = bb_path_passwd_file;
208                 rc = update_passwd(bb_path_passwd_file, name, newp, NULL);
209         }
210         /* LOGMODE_BOTH */
211         if (rc < 0)
212                 bb_error_msg_and_die("can't update password file %s", filename);
213         bb_info_msg("Password for %s changed by %s", name, myname);
214
215         /*if (ENABLE_FEATURE_CLEAN_UP) free(newp); - can't, it may be non-malloced */
216  skip:
217         if (!newp) {
218                 bb_error_msg_and_die("password for %s is already %slocked",
219                         name, (opt & OPT_unlock) ? "un" : "");
220         }
221
222         if (ENABLE_FEATURE_CLEAN_UP)
223                 free(myname);
224         return 0;
225 }