PASS8 affects only input, not output
[unix-history] / usr / src / usr.bin / passwd / passwd.c
CommitLineData
1f978f4c
KM
1/*
2 * Copyright (c) 1983 Regents of the University of California.
3 * All rights reserved. The Berkeley software License Agreement
4 * specifies the terms and conditions for redistribution.
5 */
6
7#ifndef lint
8char copyright[] =
9"@(#) Copyright (c) 1983 Regents of the University of California.\n\
10 All rights reserved.\n";
11#endif not lint
12
252e456d 13#ifndef lint
6afaba66 14static char sccsid[] = "@(#)passwd.c 4.22 (Berkeley) %G%";
1f978f4c 15#endif not lint
252e456d
SL
16
17/*
844df0cd
RC
18 * Modify a field in the password file (either
19 * password, login shell, or gecos field).
9a59f452
SL
20 * This program should be suid with an owner
21 * with write permission on /etc/passwd.
252e456d 22 */
6afaba66 23#include <sys/types.h>
252e456d 24#include <sys/file.h>
844df0cd
RC
25#include <sys/time.h>
26#include <sys/resource.h>
252e456d
SL
27
28#include <stdio.h>
29#include <signal.h>
30#include <pwd.h>
9ae20471 31#include <ndbm.h>
252e456d 32#include <errno.h>
844df0cd
RC
33#include <strings.h>
34#include <ctype.h>
252e456d 35
6afaba66
KM
36/*
37 * This should be the first thing returned from a getloginshells()
38 * but too many programs know that it is /bin/sh.
39 */
40#define DEFSHELL "/bin/sh"
41
8d3b95af 42char temp[] = "/etc/ptmp";
252e456d 43char passwd[] = "/etc/passwd";
252e456d
SL
44char *getpass();
45char *getlogin();
844df0cd
RC
46char *getfingerinfo();
47char *getloginshell();
48char *getnewpasswd();
bd94915c 49char *malloc();
6afaba66 50char *getusershell();
252e456d
SL
51extern int errno;
52
53main(argc, argv)
54 char *argv[];
55{
844df0cd 56 struct passwd *pwd;
a1b0286d 57 char *cp, *uname, *progname;
bd94915c 58 int fd, u, dochfn, dochsh, err;
252e456d 59 FILE *tf;
9ae20471 60 DBM *dp;
252e456d 61
98abc60f 62 if ((progname = rindex(argv[0], '/')) == NULL)
a1b0286d
RC
63 progname = argv[0];
64 else
65 progname++;
844df0cd
RC
66 dochfn = 0, dochsh = 0;
67 argc--, argv++;
68 while (argc > 0 && argv[0][0] == '-') {
69 for (cp = &argv[0][1]; *cp; cp++) switch (*cp) {
70
71 case 'f':
72 if (dochsh)
73 goto bad;
74 dochfn = 1;
75 break;
76
77 case 's':
78 if (dochfn) {
79 bad:
80 fprintf(stderr,
81 "passwd: Only one of -f and -s allowed.\n");
82 exit(1);
83 }
84 dochsh = 1;
85 break;
86
87 default:
88 fprintf(stderr, "passwd: -%c: unknown option.\n", *cp);
89 exit(1);
90 }
91 argc--, argv++;
92 }
a1b0286d
RC
93 if (!dochfn && !dochsh) {
94 if (strcmp(progname, "chfn") == 0)
95 dochfn = 1;
96 else if (strcmp(progname, "chsh") == 0)
97 dochsh = 1;
98 }
844df0cd 99 if (argc < 1) {
252e456d 100 if ((uname = getlogin()) == NULL) {
a1b0286d 101 fprintf(stderr, "Usage: %s [-f] [-s] [user]\n", progname);
252e456d
SL
102 exit(1);
103 }
844df0cd
RC
104 printf("Changing %s for %s.\n",
105 dochfn ? "finger information" :
106 dochsh ? "login shell" : "password",
107 uname);
252e456d 108 } else
98abc60f 109 uname = *argv++;
8d3b95af 110 pwd = getpwnam(uname);
05e78391 111 if (pwd == NULL) {
844df0cd 112 fprintf(stderr, "passwd: %s: unknown user.\n", uname);
05e78391
JB
113 exit(1);
114 }
252e456d 115 u = getuid();
05e78391 116 if (u != 0 && u != pwd->pw_uid) {
252e456d
SL
117 printf("Permission denied.\n");
118 exit(1);
119 }
844df0cd 120 if (dochfn)
bd94915c 121 cp = getfingerinfo(pwd);
844df0cd 122 else if (dochsh)
98abc60f 123 cp = getloginshell(pwd, u, *argv);
844df0cd
RC
124 else
125 cp = getnewpasswd(pwd, u);
bd94915c
KM
126 (void) signal(SIGHUP, SIG_IGN);
127 (void) signal(SIGINT, SIG_IGN);
128 (void) signal(SIGQUIT, SIG_IGN);
129 (void) signal(SIGTSTP, SIG_IGN);
643bb829 130 (void) umask(0);
9a59f452 131 fd = open(temp, O_WRONLY|O_CREAT|O_EXCL, 0644);
252e456d 132 if (fd < 0) {
ba9597f7
RE
133 err = errno;
134
252e456d 135 fprintf(stderr, "passwd: ");
ba9597f7 136 if (err == EEXIST)
252e456d 137 fprintf(stderr, "password file busy - try again.\n");
ba9597f7
RE
138 else {
139 errno = err;
252e456d 140 perror(temp);
ba9597f7 141 }
252e456d
SL
142 exit(1);
143 }
252e456d
SL
144 if ((tf = fdopen(fd, "w")) == NULL) {
145 fprintf(stderr, "passwd: fdopen failed?\n");
146 exit(1);
147 }
844df0cd 148 if ((dp = dbm_open(passwd, O_RDWR, 0644)) == NULL) {
ba9597f7 149 err = errno;
844df0cd 150 fprintf(stderr, "Warning: dbm_open failed: ");
ba9597f7 151 errno = err;
9ae20471 152 perror(passwd);
844df0cd 153 } else if (flock(dp->dbm_dirf, LOCK_EX) < 0) {
9ae20471 154 perror("Warning: lock failed");
844df0cd 155 dbm_close(dp);
9ae20471
RC
156 dp = NULL;
157 }
844df0cd
RC
158 unlimit(RLIMIT_CPU);
159 unlimit(RLIMIT_FSIZE);
252e456d
SL
160 /*
161 * Copy passwd to temp, replacing matching lines
162 * with new password.
163 */
164 while ((pwd = getpwent()) != NULL) {
8d3b95af 165 if (strcmp(pwd->pw_name, uname) == 0) {
252e456d
SL
166 if (u && u != pwd->pw_uid) {
167 fprintf(stderr, "passwd: permission denied.\n");
8d3b95af 168 goto out;
252e456d 169 }
844df0cd
RC
170 if (dochfn)
171 pwd->pw_gecos = cp;
172 else if (dochsh)
173 pwd->pw_shell = cp;
174 else
175 pwd->pw_passwd = cp;
176 if (pwd->pw_gecos[0] == '*') /* ??? */
252e456d 177 pwd->pw_gecos++;
9ae20471 178 replace(dp, pwd);
252e456d
SL
179 }
180 fprintf(tf,"%s:%s:%d:%d:%s:%s:%s\n",
181 pwd->pw_name,
182 pwd->pw_passwd,
183 pwd->pw_uid,
184 pwd->pw_gid,
185 pwd->pw_gecos,
186 pwd->pw_dir,
187 pwd->pw_shell);
188 }
189 endpwent();
844df0cd
RC
190 if (dp != NULL && dbm_error(dp))
191 fprintf(stderr, "Warning: dbm_store failed\n");
bd94915c 192 (void) fflush(tf);
183d82a9
RE
193 if (ferror(tf)) {
194 fprintf(stderr, "Warning: %s write error, %s not updated\n",
195 temp, passwd);
196 goto out;
197 }
198 (void) fclose(tf);
eb635a62
KM
199 if (dp != NULL)
200 dbm_close(dp);
9ae20471 201 if (rename(temp, passwd) < 0) {
183d82a9 202 perror("passwd: rename");
9ae20471 203 out:
bd94915c 204 (void) unlink(temp);
9ae20471
RC
205 exit(1);
206 }
207 exit(0);
8d3b95af
RC
208}
209
844df0cd
RC
210unlimit(lim)
211{
212 struct rlimit rlim;
213
214 rlim.rlim_cur = rlim.rlim_max = RLIM_INFINITY;
215 (void) setrlimit(lim, &rlim);
216}
217
9ae20471
RC
218/*
219 * Replace the password entry in the dbm data base with pwd.
220 */
221replace(dp, pwd)
222 DBM *dp;
223 struct passwd *pwd;
8d3b95af 224{
9ae20471
RC
225 datum key, content;
226 register char *cp, *tp;
227 char buf[BUFSIZ];
8d3b95af 228
9ae20471
RC
229 if (dp == NULL)
230 return;
231
232 cp = buf;
233#define COMPACT(e) tp = pwd->pw_/**/e; while (*cp++ = *tp++);
234 COMPACT(name);
235 COMPACT(passwd);
3c92851f
RC
236 bcopy((char *)&pwd->pw_uid, cp, sizeof (int));
237 cp += sizeof (int);
238 bcopy((char *)&pwd->pw_gid, cp, sizeof (int));
239 cp += sizeof (int);
240 bcopy((char *)&pwd->pw_quota, cp, sizeof (int));
241 cp += sizeof (int);
9ae20471
RC
242 COMPACT(comment);
243 COMPACT(gecos);
244 COMPACT(dir);
245 COMPACT(shell);
246 content.dptr = buf;
247 content.dsize = cp - buf;
248 key.dptr = pwd->pw_name;
249 key.dsize = strlen(pwd->pw_name);
844df0cd 250 dbm_store(dp, key, content, DBM_REPLACE);
9ae20471
RC
251 key.dptr = (char *)&pwd->pw_uid;
252 key.dsize = sizeof (int);
844df0cd
RC
253 dbm_store(dp, key, content, DBM_REPLACE);
254}
255
256char *
257getnewpasswd(pwd, u)
258 register struct passwd *pwd;
259 int u;
260{
261 char saltc[2];
bd94915c 262 long salt;
844df0cd
RC
263 int i, insist = 0, ok, flags;
264 int c, pwlen;
265 static char pwbuf[10];
bd94915c 266 long time();
844df0cd
RC
267 char *crypt(), *pw, *p;
268
269 if (pwd->pw_passwd[0] && u != 0) {
bd94915c 270 (void) strcpy(pwbuf, getpass("Old password:"));
844df0cd
RC
271 pw = crypt(pwbuf, pwd->pw_passwd);
272 if (strcmp(pw, pwd->pw_passwd) != 0) {
273 printf("Sorry.\n");
274 exit(1);
275 }
276 }
277tryagain:
bd94915c 278 (void) strcpy(pwbuf, getpass("New password:"));
844df0cd
RC
279 pwlen = strlen(pwbuf);
280 if (pwlen == 0) {
281 printf("Password unchanged.\n");
282 exit(1);
283 }
284 /*
285 * Insure password is of reasonable length and
286 * composition. If we really wanted to make things
287 * sticky, we could check the dictionary for common
288 * words, but then things would really be slow.
289 */
290 ok = 0;
291 flags = 0;
292 p = pwbuf;
293 while (c = *p++) {
294 if (c >= 'a' && c <= 'z')
295 flags |= 2;
296 else if (c >= 'A' && c <= 'Z')
297 flags |= 4;
298 else if (c >= '0' && c <= '9')
299 flags |= 1;
300 else
301 flags |= 8;
302 }
303 if (flags >= 7 && pwlen >= 4)
304 ok = 1;
305 if ((flags == 2 || flags == 4) && pwlen >= 6)
306 ok = 1;
307 if ((flags == 3 || flags == 5 || flags == 6) && pwlen >= 5)
308 ok = 1;
309 if (!ok && insist < 2) {
310 printf("Please use %s.\n", flags == 1 ?
311 "at least one non-numeric character" :
312 "a longer password");
313 insist++;
314 goto tryagain;
315 }
316 if (strcmp(pwbuf, getpass("Retype new password:")) != 0) {
317 printf("Mismatch - password unchanged.\n");
318 exit(1);
319 }
bd94915c 320 (void) time(&salt);
844df0cd
RC
321 salt = 9 * getpid();
322 saltc[0] = salt & 077;
323 saltc[1] = (salt>>6) & 077;
324 for (i = 0; i < 2; i++) {
325 c = saltc[i] + '.';
326 if (c > '9')
327 c += 7;
328 if (c > 'Z')
329 c += 6;
330 saltc[i] = c;
331 }
332 return (crypt(pwbuf, saltc));
333}
334
844df0cd 335char *
98abc60f 336getloginshell(pwd, u, arg)
844df0cd
RC
337 struct passwd *pwd;
338 int u;
98abc60f 339 char *arg;
844df0cd 340{
6afaba66
KM
341 static char newshell[BUFSIZ];
342 char *cp, *valid, *getusershell();
844df0cd
RC
343
344 if (pwd->pw_shell == 0 || *pwd->pw_shell == '\0')
345 pwd->pw_shell = DEFSHELL;
98abc60f 346 if (arg != 0) {
bd94915c 347 (void) strncpy(newshell, arg, sizeof newshell - 1);
98abc60f
EW
348 newshell[sizeof newshell - 1] = 0;
349 } else {
350 printf("Old shell: %s\nNew shell: ", pwd->pw_shell);
bd94915c 351 (void)fgets(newshell, sizeof (newshell) - 1, stdin);
98abc60f
EW
352 cp = index(newshell, '\n');
353 if (cp)
354 *cp = '\0';
355 }
844df0cd
RC
356 if (newshell[0] == '\0' || strcmp(newshell, pwd->pw_shell) == 0) {
357 printf("Login shell unchanged.\n");
358 exit(1);
359 }
360 /*
361 * Allow user to give shell name w/o preceding pathname.
362 */
6afaba66
KM
363 if (u == 0) {
364 valid = newshell;
365 } else {
366 for (valid = getusershell(); valid; valid = getusershell()) {
367 if (newshell[0] == '/') {
368 cp = valid;
369 } else {
370 cp = rindex(valid, '/');
371 if (cp == 0)
372 cp = valid;
373 else
374 cp++;
375 }
376 if (strcmp(newshell, cp) == 0)
844df0cd
RC
377 break;
378 }
844df0cd 379 }
6afaba66
KM
380 if (valid == 0) {
381 printf("%s is unacceptable as a new shell.\n",
382 newshell);
844df0cd
RC
383 exit(1);
384 }
6afaba66
KM
385 if (access(valid, X_OK) < 0) {
386 printf("%s is unavailable.\n", valid);
387 exit(1);
bd94915c 388 }
6afaba66
KM
389 if (strcmp(valid, DEFSHELL) == 0)
390 valid[0] = '\0';
391 return (valid);
bd94915c 392}
844df0cd
RC
393
394struct default_values {
395 char *name;
396 char *office_num;
397 char *office_phone;
398 char *home_phone;
399};
400
401/*
402 * Get name, room number, school phone, and home phone.
403 */
404char *
bd94915c 405getfingerinfo(pwd)
844df0cd 406 struct passwd *pwd;
844df0cd
RC
407{
408 char in_str[BUFSIZ];
409 struct default_values *defaults, *get_defaults();
410 static char answer[4*BUFSIZ];
411
412 answer[0] = '\0';
413 defaults = get_defaults(pwd->pw_gecos);
414 printf("Default values are printed inside of of '[]'.\n");
415 printf("To accept the default, type <return>.\n");
416 printf("To have a blank entry, type the word 'none'.\n");
417 /*
418 * Get name.
419 */
420 do {
421 printf("\nName [%s]: ", defaults->name);
422 (void) fgets(in_str, BUFSIZ, stdin);
423 if (special_case(in_str, defaults->name))
424 break;
425 } while (illegal_input(in_str));
426 (void) strcpy(answer, in_str);
427 /*
428 * Get room number.
429 */
430 do {
431 printf("Room number (Exs: 597E or 197C) [%s]: ",
432 defaults->office_num);
433 (void) fgets(in_str, BUFSIZ, stdin);
434 if (special_case(in_str, defaults->office_num))
435 break;
436 } while (illegal_input(in_str) || illegal_building(in_str));
437 (void) strcat(strcat(answer, ","), in_str);
438 /*
439 * Get office phone number.
008e3485 440 * Remove hyphens.
844df0cd
RC
441 */
442 do {
008e3485 443 printf("Office Phone (Ex: 6426000) [%s]: ",
844df0cd
RC
444 defaults->office_phone);
445 (void) fgets(in_str, BUFSIZ, stdin);
446 if (special_case(in_str, defaults->office_phone))
447 break;
448 remove_hyphens(in_str);
008e3485 449 } while (illegal_input(in_str) || not_all_digits(in_str));
844df0cd
RC
450 (void) strcat(strcat(answer, ","), in_str);
451 /*
452 * Get home phone number.
453 * Remove hyphens if present.
454 */
455 do {
456 printf("Home Phone (Ex: 9875432) [%s]: ", defaults->home_phone);
457 (void) fgets(in_str, BUFSIZ, stdin);
458 if (special_case(in_str, defaults->home_phone))
459 break;
460 remove_hyphens(in_str);
461 } while (illegal_input(in_str) || not_all_digits(in_str));
462 (void) strcat(strcat(answer, ","), in_str);
463 if (strcmp(answer, pwd->pw_gecos) == 0) {
464 printf("Finger information unchanged.\n");
465 exit(1);
466 }
467 return (answer);
468}
469
470/*
471 * Prints an error message if a ':' or a newline is found in the string.
472 * A message is also printed if the input string is too long.
473 * The password file uses :'s as seperators, and are not allowed in the "gcos"
474 * field. Newlines serve as delimiters between users in the password file,
475 * and so, those too, are checked for. (I don't think that it is possible to
476 * type them in, but better safe than sorry)
477 *
478 * Returns '1' if a colon or newline is found or the input line is too long.
479 */
480illegal_input(input_str)
481 char *input_str;
482{
483 char *ptr;
484 int error_flag = 0;
485 int length = strlen(input_str);
486
487 if (index(input_str, ':')) {
488 printf("':' is not allowed.\n");
489 error_flag = 1;
490 }
491 if (input_str[length-1] != '\n') {
492 /* the newline and the '\0' eat up two characters */
493 printf("Maximum number of characters allowed is %d\n",
494 BUFSIZ-2);
495 /* flush the rest of the input line */
496 while (getchar() != '\n')
497 /* void */;
498 error_flag = 1;
499 }
500 /*
501 * Delete newline by shortening string by 1.
502 */
503 input_str[length-1] = '\0';
504 /*
505 * Don't allow control characters, etc in input string.
506 */
507 for (ptr=input_str; *ptr != '\0'; ptr++) {
508 if ((int) *ptr < 040) {
509 printf("Control characters are not allowed.\n");
510 error_flag = 1;
511 break;
512 }
513 }
514 return (error_flag);
515}
516
517/*
518 * Removes '-'s from the input string.
519 */
520remove_hyphens(str)
521 char *str;
522{
523 char *hyphen;
524
525 while ((hyphen = index(str, '-')) != NULL)
526 (void) strcpy(hyphen, hyphen+1);
527}
528
529/*
530 * Checks to see if 'str' contains only digits (0-9). If not, then
531 * an error message is printed and '1' is returned.
532 */
533not_all_digits(str)
534 char *str;
535{
536 char *ptr;
537
538 for (ptr = str; *ptr != '\0'; ++ptr)
539 if (!isdigit(*ptr)) {
540 printf("Phone numbers can only contain digits.\n");
541 return (1);
542 }
543 return (0);
544}
545
844df0cd 546/*
5700acfe 547 * Deal with Berkeley buildings. Abbreviating Cory to C and Evans to E.
844df0cd 548 * Correction changes "str".
844df0cd
RC
549 *
550 * Returns 1 if incorrect room format.
551 *
552 * Note: this function assumes that the newline has been removed from str.
553 */
554illegal_building(str)
5700acfe 555 register char *str;
844df0cd
RC
556{
557 int length = strlen(str);
5700acfe 558 register char *ptr;
844df0cd
RC
559
560 /*
5700acfe
EW
561 * If the string is [Ee]vans or [Cc]ory or ends in
562 * [ \t0-9][Ee]vans or [ \t0-9M][Cc]ory, then contract the name
563 * into 'E' or 'C', as the case may be, and delete leading blanks.
844df0cd 564 */
5700acfe
EW
565 if (length >= 5 && strcmp(ptr = str + length - 4, "vans") == 0 &&
566 (*--ptr == 'e' || *ptr == 'E') &&
567 (--ptr < str || isspace(*ptr) || isdigit(*ptr))) {
568 for (; ptr > str && isspace(*ptr); ptr--)
569 ;
570 ptr++;
571 *ptr++ = 'E';
572 *ptr = '\0';
573 } else
574 if (length >= 4 && strcmp(ptr = str + length - 3, "ory") == 0 &&
575 (*--ptr == 'c' || *ptr == 'C') &&
576 (--ptr < str || *ptr == 'M' || isspace(*ptr) || isdigit(*ptr))) {
577 for (; ptr > str && isspace(*ptr); ptr--)
578 ;
579 ptr++;
580 *ptr++ = 'C';
581 *ptr = '\0';
844df0cd
RC
582 }
583 return (0);
584}
585
586/*
587 * get_defaults picks apart "str" and returns a structure points.
588 * "str" contains up to 4 fields separated by commas.
589 * Any field that is missing is set to blank.
590 */
591struct default_values *
592get_defaults(str)
593 char *str;
594{
595 struct default_values *answer;
844df0cd
RC
596
597 answer = (struct default_values *)
598 malloc((unsigned)sizeof(struct default_values));
599 if (answer == (struct default_values *) NULL) {
600 fprintf(stderr,
601 "\nUnable to allocate storage in get_defaults!\n");
602 exit(1);
603 }
604 /*
605 * Values if no corresponding string in "str".
606 */
607 answer->name = str;
608 answer->office_num = "";
609 answer->office_phone = "";
610 answer->home_phone = "";
611 str = index(answer->name, ',');
612 if (str == 0)
613 return (answer);
614 *str = '\0';
615 answer->office_num = str + 1;
616 str = index(answer->office_num, ',');
617 if (str == 0)
618 return (answer);
619 *str = '\0';
620 answer->office_phone = str + 1;
621 str = index(answer->office_phone, ',');
622 if (str == 0)
623 return (answer);
624 *str = '\0';
625 answer->home_phone = str + 1;
626 return (answer);
627}
628
629/*
630 * special_case returns true when either the default is accepted
631 * (str = '\n'), or when 'none' is typed. 'none' is accepted in
632 * either upper or lower case (or any combination). 'str' is modified
633 * in these two cases.
634 */
635special_case(str,default_str)
636 char *str, *default_str;
637{
638 static char word[] = "none\n";
639 char *ptr, *wordptr;
640
641 /*
642 * If the default is accepted, then change the old string do the
643 * default string.
644 */
645 if (*str == '\n') {
646 (void) strcpy(str, default_str);
647 return (1);
648 }
649 /*
650 * Check to see if str is 'none'. (It is questionable if case
651 * insensitivity is worth the hair).
652 */
653 wordptr = word-1;
654 for (ptr = str; *ptr != '\0'; ++ptr) {
655 ++wordptr;
656 if (*wordptr == '\0') /* then words are different sizes */
657 return (0);
658 if (*ptr == *wordptr)
659 continue;
660 if (isupper(*ptr) && (tolower(*ptr) == *wordptr))
661 continue;
662 /*
663 * At this point we have a mismatch, so we return
664 */
665 return (0);
666 }
667 /*
668 * Make sure that words are the same length.
669 */
670 if (*(wordptr+1) != '\0')
671 return (0);
672 /*
673 * Change 'str' to be the null string
674 */
675 *str = '\0';
676 return (1);
252e456d 677}