| 1 | /*- |
| 2 | * Copyright (c) 1988 The Regents of the University of California. |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * %sccs.include.redist.c% |
| 6 | */ |
| 7 | |
| 8 | #ifndef lint |
| 9 | char copyright[] = |
| 10 | "@(#) Copyright (c) 1988 The Regents of the University of California.\n\ |
| 11 | All rights reserved.\n"; |
| 12 | #endif /* not lint */ |
| 13 | |
| 14 | #ifndef lint |
| 15 | static char sccsid[] = "@(#)chpass.c 5.16 (Berkeley) %G%"; |
| 16 | #endif /* not lint */ |
| 17 | |
| 18 | #include <sys/param.h> |
| 19 | #include <sys/stat.h> |
| 20 | #include <sys/signal.h> |
| 21 | #include <sys/time.h> |
| 22 | #include <sys/resource.h> |
| 23 | #include <fcntl.h> |
| 24 | #include <pwd.h> |
| 25 | #include <errno.h> |
| 26 | #include <stdio.h> |
| 27 | #include <ctype.h> |
| 28 | #include <string.h> |
| 29 | #include "chpass.h" |
| 30 | #include "pathnames.h" |
| 31 | |
| 32 | char *progname = "chpass"; |
| 33 | char *tempname; |
| 34 | uid_t uid; |
| 35 | |
| 36 | main(argc, argv) |
| 37 | int argc; |
| 38 | char **argv; |
| 39 | { |
| 40 | extern int optind; |
| 41 | extern char *optarg; |
| 42 | register enum { NEWSH, LOADENTRY, EDITENTRY } op; |
| 43 | register struct passwd *pw; |
| 44 | struct passwd lpw; |
| 45 | int ch, pfd, tfd; |
| 46 | char *arg; |
| 47 | |
| 48 | op = EDITENTRY; |
| 49 | while ((ch = getopt(argc, argv, "a:s:")) != EOF) |
| 50 | switch(ch) { |
| 51 | case 'a': |
| 52 | op = LOADENTRY; |
| 53 | arg = optarg; |
| 54 | break; |
| 55 | case 's': |
| 56 | op = NEWSH; |
| 57 | arg = optarg; |
| 58 | break; |
| 59 | case '?': |
| 60 | default: |
| 61 | usage(); |
| 62 | } |
| 63 | argc -= optind; |
| 64 | argv += optind; |
| 65 | |
| 66 | uid = getuid(); |
| 67 | |
| 68 | if (op == EDITENTRY || op == NEWSH) |
| 69 | switch(argc) { |
| 70 | case 0: |
| 71 | if (!(pw = getpwuid(uid))) { |
| 72 | (void)fprintf(stderr, |
| 73 | "chpass: unknown user: uid %u\n", uid); |
| 74 | exit(1); |
| 75 | } |
| 76 | break; |
| 77 | case 1: |
| 78 | if (!(pw = getpwnam(*argv))) { |
| 79 | (void)fprintf(stderr, |
| 80 | "chpass: unknown user %s.\n", *argv); |
| 81 | exit(1); |
| 82 | } |
| 83 | if (uid && uid != pw->pw_uid) |
| 84 | baduser(); |
| 85 | break; |
| 86 | default: |
| 87 | usage(); |
| 88 | } |
| 89 | |
| 90 | if (op == NEWSH) { |
| 91 | /* protect p_shell -- it thinks NULL is /bin/sh */ |
| 92 | if (!arg[0]) |
| 93 | usage(); |
| 94 | if (p_shell(arg, pw, (ENTRY *)NULL)) |
| 95 | pw_error((char *)NULL, 0, 1); |
| 96 | } |
| 97 | |
| 98 | if (op == LOADENTRY) { |
| 99 | if (uid) |
| 100 | baduser(); |
| 101 | pw = &lpw; |
| 102 | if (!pw_scan(arg, pw)) |
| 103 | exit(1); |
| 104 | } |
| 105 | |
| 106 | /* |
| 107 | * The file descriptor usage is a little tricky through here. |
| 108 | * 1: We start off with two fd's, one for the master password |
| 109 | * file, and one for the temporary file. |
| 110 | * 2: Get an fp for the temporary file, copy the info to be |
| 111 | * edited into it, and close the fp (closing the underlying |
| 112 | * fd). |
| 113 | * 3: The user edits the temporary file some number of times. |
| 114 | * 4: Get an fp for the temporary file, and verify the contents. |
| 115 | * We can't use the fp from step 2, because the user's editor |
| 116 | * may have created a new instance of the file. Close the |
| 117 | * fp when done. |
| 118 | * 5: Get an fp for the temporary file, truncating it as we do |
| 119 | * so. Get an fp for the master password file. Copy the |
| 120 | * master password file into the temporary file, replacing the |
| 121 | * user record with a new one. Close the temporary file fp |
| 122 | * when done -- can't close the password fp, or we'd lose the |
| 123 | * lock. |
| 124 | * 6: Call pw_mkdb() and exit. The exit closes the master password |
| 125 | * fd from step 1, and the master password fp from step 5. |
| 126 | */ |
| 127 | pw_init(); |
| 128 | pfd = pw_lock(); |
| 129 | tfd = pw_tmp(); |
| 130 | |
| 131 | if (op == EDITENTRY) |
| 132 | edit(tfd, pw); |
| 133 | |
| 134 | pw_copy(pfd, pw); |
| 135 | |
| 136 | if (!pw_mkdb()) |
| 137 | pw_error((char *)NULL, 0, 1); |
| 138 | exit(0); |
| 139 | } |
| 140 | |
| 141 | baduser() |
| 142 | { |
| 143 | (void)fprintf(stderr, "chpass: %s\n", strerror(EACCES)); |
| 144 | exit(1); |
| 145 | } |
| 146 | |
| 147 | usage() |
| 148 | { |
| 149 | (void)fprintf(stderr, "usage: chpass [-a list] [-s shell] [user]\n"); |
| 150 | exit(1); |
| 151 | } |