Convert setuid/setgid users to xsetuid/xsetgid.
[platform/upstream/busybox.git] / loginutils / passwd.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
4  */
5
6 #include <fcntl.h>
7 #include <stdio.h>
8 #include <string.h>
9 #include <signal.h>
10 #include <sys/stat.h>
11 #include <sys/types.h>
12 #include <unistd.h>
13 #include <utime.h>
14 #include <syslog.h>
15 #include <time.h>
16 #include <sys/resource.h>
17 #include <errno.h>
18
19 #include "busybox.h"
20
21 static char crypt_passwd[128];
22
23 static int create_backup(const char *backup, FILE * fp);
24 static int new_password(const struct passwd *pw, int amroot, int algo);
25 static void set_filesize_limit(int blocks);
26
27
28 static int get_algo(char *a)
29 {
30         int x = 1;                                      /* standard: MD5 */
31
32         if (strcasecmp(a, "des") == 0)
33                 x = 0;
34         return x;
35 }
36
37
38 static int update_passwd(const struct passwd *pw, const char *crypt_pw)
39 {
40         char filename[1024];
41         char buf[1025];
42         char buffer[80];
43         char username[32];
44         char *pw_rest;
45         int mask;
46         int continued;
47         FILE *fp;
48         FILE *out_fp;
49         struct stat sb;
50         struct flock lock;
51
52 #if ENABLE_FEATURE_SHADOWPASSWDS
53         if (access(bb_path_shadow_file, F_OK) == 0) {
54                 snprintf(filename, sizeof filename, "%s", bb_path_shadow_file);
55         } else
56 #endif
57         {
58                 snprintf(filename, sizeof filename, "%s", bb_path_passwd_file);
59         }
60
61         if (((fp = fopen(filename, "r+")) == 0) || (fstat(fileno(fp), &sb))) {
62                 /* return 0; */
63                 return 1;
64         }
65
66         /* Lock the password file before updating */
67         lock.l_type = F_WRLCK;
68         lock.l_whence = SEEK_SET;
69         lock.l_start = 0;
70         lock.l_len = 0;
71         if (fcntl(fileno(fp), F_SETLK, &lock) < 0) {
72                 fprintf(stderr, "%s: %s\n", filename, strerror(errno));
73                 return 1;
74         }
75         lock.l_type = F_UNLCK;
76
77         snprintf(buf, sizeof buf, "%s-", filename);
78         if (create_backup(buf, fp)) {
79                 fcntl(fileno(fp), F_SETLK, &lock);
80                 fclose(fp);
81                 return 1;
82         }
83         snprintf(buf, sizeof buf, "%s+", filename);
84         mask = umask(0777);
85         out_fp = fopen(buf, "w");
86         umask(mask);
87         if ((!out_fp) || (fchmod(fileno(out_fp), sb.st_mode & 0777))
88                 || (fchown(fileno(out_fp), sb.st_uid, sb.st_gid))) {
89                 fcntl(fileno(fp), F_SETLK, &lock);
90                 fclose(fp);
91                 fclose(out_fp);
92                 return 1;
93         }
94
95         continued = 0;
96         snprintf(username, sizeof username, "%s:", pw->pw_name);
97         rewind(fp);
98         while (!feof(fp)) {
99                 fgets(buffer, sizeof buffer, fp);
100                 if (!continued) { /* Check to see if we're updating this line.  */
101                         if (strncmp(username, buffer, strlen(username)) == 0) {
102                                 /* we have a match. */
103                                 pw_rest = strchr(buffer, ':');
104                                 *pw_rest++ = '\0';
105                                 pw_rest = strchr(pw_rest, ':');
106                                 fprintf(out_fp, "%s:%s%s", buffer, crypt_pw, pw_rest);
107                         } else {
108                                 fputs(buffer, out_fp);
109                         }
110                 } else {
111                         fputs(buffer, out_fp);
112                 }
113                 if (buffer[strlen(buffer) - 1] == '\n') {
114                         continued = 0;
115                 } else {
116                         continued = 1;
117                 }
118                 memset(buffer, 0, sizeof buffer);
119         }
120
121         if (fflush(out_fp) || fsync(fileno(out_fp)) || fclose(out_fp)) {
122                 unlink(buf);
123                 fcntl(fileno(fp), F_SETLK, &lock);
124                 fclose(fp);
125                 return 1;
126         }
127         if (rename(buf, filename) < 0) {
128                 fcntl(fileno(fp), F_SETLK, &lock);
129                 fclose(fp);
130                 return 1;
131         } else {
132                 fcntl(fileno(fp), F_SETLK, &lock);
133                 fclose(fp);
134                 return 0;
135         }
136 }
137
138
139 int passwd_main(int argc, char **argv)
140 {
141         int amroot;
142         char *cp;
143         char *np;
144         char *name;
145         char *myname;
146         int flag;
147         int algo = 1;                           /* -a - password algorithm */
148         int lflg = 0;                           /* -l - lock account */
149         int uflg = 0;                           /* -u - unlock account */
150         int dflg = 0;                           /* -d - delete password */
151         const struct passwd *pw;
152
153         amroot = (getuid() == 0);
154         openlog("passwd", LOG_PID | LOG_CONS | LOG_NOWAIT, LOG_AUTH);
155         while ((flag = getopt(argc, argv, "a:dlu")) != EOF) {
156                 switch (flag) {
157                 case 'a':
158                         algo = get_algo(optarg);
159                         break;
160                 case 'd':
161                         dflg++;
162                         break;
163                 case 'l':
164                         lflg++;
165                         break;
166                 case 'u':
167                         uflg++;
168                         break;
169                 default:
170                         bb_show_usage();
171                 }
172         }
173         myname = (char *) bb_xstrdup(bb_getpwuid(NULL, getuid(), -1));
174         /* exits on error */
175         if (optind < argc) {
176                 name = argv[optind];
177         } else {
178                 name = myname;
179         }
180         if ((lflg || uflg || dflg) && (optind >= argc || !amroot)) {
181                 bb_show_usage();
182         }
183         pw = getpwnam(name);
184         if (!pw) {
185                 bb_error_msg_and_die("Unknown user %s\n", name);
186         }
187         if (!amroot && pw->pw_uid != getuid()) {
188                 syslog(LOG_WARNING, "can't change pwd for `%s'", name);
189                 bb_error_msg_and_die("Permission denied.\n");
190         }
191         if (ENABLE_FEATURE_SHADOWPASSWDS) {
192                 struct spwd *sp = getspnam(name);
193                 if (!sp) bb_error_msg_and_die("Unknown user %s", name);
194                 cp = sp->sp_pwdp;
195         } else cp = pw->pw_passwd;
196
197         np = name;
198         safe_strncpy(crypt_passwd, cp, sizeof(crypt_passwd));
199         if (!(dflg || lflg || uflg)) {
200                 if (!amroot) {
201                         if (cp[0] == '!') {
202                                 syslog(LOG_WARNING, "password locked for `%s'", np);
203                                 bb_error_msg_and_die( "The password for `%s' cannot be changed.\n", np);
204                         }
205                 }
206                 printf("Changing password for %s\n", name);
207                 if (new_password(pw, amroot, algo)) {
208                         bb_error_msg_and_die( "The password for %s is unchanged.\n", name);
209                 }
210         } else if (lflg) {
211                 if (crypt_passwd[0] != '!') {
212                         memmove(&crypt_passwd[1], crypt_passwd,
213                                         sizeof crypt_passwd - 1);
214                         crypt_passwd[sizeof crypt_passwd - 1] = '\0';
215                         crypt_passwd[0] = '!';
216                 }
217         } else if (uflg) {
218                 if (crypt_passwd[0] == '!') {
219                         memmove(crypt_passwd, &crypt_passwd[1],
220                                         sizeof crypt_passwd - 1);
221                 }
222         } else if (dflg) {
223                 crypt_passwd[0] = '\0';
224         }
225         set_filesize_limit(30000);
226         signal(SIGHUP, SIG_IGN);
227         signal(SIGINT, SIG_IGN);
228         signal(SIGQUIT, SIG_IGN);
229         umask(077);
230         xsetuid(0);
231         if (!update_passwd(pw, crypt_passwd)) {
232                 syslog(LOG_INFO, "password for `%s' changed by user `%s'", name,
233                            myname);
234                 printf("Password changed.\n");
235         } else {
236                 syslog(LOG_WARNING, "an error occurred updating the password file");
237                 bb_error_msg_and_die("An error occurred updating the password file.\n");
238         }
239         if (ENABLE_FEATURE_CLEAN_UP) free(myname);
240         return (0);
241 }
242
243
244
245 static int create_backup(const char *backup, FILE * fp)
246 {
247         struct stat sb;
248         struct utimbuf ub;
249         FILE *bkfp;
250         int c, mask;
251
252         if (fstat(fileno(fp), &sb))
253                 /* return -1; */
254                 return 1;
255
256         mask = umask(077);
257         bkfp = fopen(backup, "w");
258         umask(mask);
259         if (!bkfp)
260                 /* return -1; */
261                 return 1;
262
263         /* TODO: faster copy, not one-char-at-a-time.  --marekm */
264         rewind(fp);
265         while ((c = getc(fp)) != EOF) {
266                 if (putc(c, bkfp) == EOF)
267                         break;
268         }
269         if (c != EOF || fflush(bkfp)) {
270                 fclose(bkfp);
271                 /* return -1; */
272                 return 1;
273         }
274         if (fclose(bkfp))
275                 /* return -1; */
276                 return 1;
277
278         ub.actime = sb.st_atime;
279         ub.modtime = sb.st_mtime;
280         utime(backup, &ub);
281         return 0;
282 }
283
284 static int i64c(int i)
285 {
286         if (i <= 0)
287                 return ('.');
288         if (i == 1)
289                 return ('/');
290         if (i >= 2 && i < 12)
291                 return ('0' - 2 + i);
292         if (i >= 12 && i < 38)
293                 return ('A' - 12 + i);
294         if (i >= 38 && i < 63)
295                 return ('a' - 38 + i);
296         return ('z');
297 }
298
299 static char *crypt_make_salt(void)
300 {
301         time_t now;
302         static unsigned long x;
303         static char result[3];
304
305         time(&now);
306         x += now + getpid() + clock();
307         result[0] = i64c(((x >> 18) ^ (x >> 6)) & 077);
308         result[1] = i64c(((x >> 12) ^ x) & 077);
309         result[2] = '\0';
310         return result;
311 }
312
313
314 static int new_password(const struct passwd *pw, int amroot, int algo)
315 {
316         char *clear;
317         char *cipher;
318         char *cp;
319         char salt[12]; /* "$N$XXXXXXXX" or "XX" */
320         char orig[200];
321         char pass[200];
322
323         if (!amroot && crypt_passwd[0]) {
324                 if (!(clear = bb_askpass(0, "Old password:"))) {
325                         /* return -1; */
326                         return 1;
327                 }
328                 cipher = pw_encrypt(clear, crypt_passwd);
329                 if (strcmp(cipher, crypt_passwd) != 0) {
330                         syslog(LOG_WARNING, "incorrect password for `%s'",
331                                    pw->pw_name);
332                         bb_do_delay(FAIL_DELAY);
333                         fprintf(stderr, "Incorrect password.\n");
334                         /* return -1; */
335                         return 1;
336                 }
337                 safe_strncpy(orig, clear, sizeof(orig));
338                 memset(clear, 0, strlen(clear));
339                 memset(cipher, 0, strlen(cipher));
340         } else {
341                 orig[0] = '\0';
342         }
343         if (! (cp=bb_askpass(0, "Enter the new password (minimum of 5, maximum of 8 characters)\n"
344                                           "Please use a combination of upper and lower case letters and numbers.\n"
345                                           "Enter new password: ")))
346         {
347                 memset(orig, 0, sizeof orig);
348                 /* return -1; */
349                 return 1;
350         }
351         safe_strncpy(pass, cp, sizeof(pass));
352         memset(cp, 0, strlen(cp));
353         /* if (!obscure(orig, pass, pw)) { */
354         if (obscure(orig, pass, pw)) {
355                 if (amroot) {
356                         printf("\nWarning: weak password (continuing).\n");
357                 } else {
358                         /* return -1; */
359                         return 1;
360                 }
361         }
362         if (!(cp = bb_askpass(0, "Re-enter new password: "))) {
363                 memset(orig, 0, sizeof orig);
364                 /* return -1; */
365                 return 1;
366         }
367         if (strcmp(cp, pass)) {
368                 fprintf(stderr, "Passwords do not match.\n");
369                 /* return -1; */
370                 return 1;
371         }
372         memset(cp, 0, strlen(cp));
373         memset(orig, 0, sizeof(orig));
374         memset(salt, 0, sizeof(salt));
375
376         if (algo == 1) {
377                 strcpy(salt, "$1$");
378                 strcat(salt, crypt_make_salt());
379                 strcat(salt, crypt_make_salt());
380                 strcat(salt, crypt_make_salt());
381         }
382
383         strcat(salt, crypt_make_salt());
384         cp = pw_encrypt(pass, salt);
385
386         memset(pass, 0, sizeof pass);
387         safe_strncpy(crypt_passwd, cp, sizeof(crypt_passwd));
388         return 0;
389 }
390
391 static void set_filesize_limit(int blocks)
392 {
393         struct rlimit rlimit_fsize;
394
395         rlimit_fsize.rlim_cur = rlimit_fsize.rlim_max = 512L * blocks;
396         setrlimit(RLIMIT_FSIZE, &rlimit_fsize);
397 }