Commit | Line | Data |
---|---|---|
59f1a2c1 | 1 | /*- |
8b35ab41 KB |
2 | * Copyright (c) 1988 The Regents of the University of California. |
3 | * All rights reserved. | |
4 | * | |
59f1a2c1 | 5 | * %sccs.include.redist.c% |
8b35ab41 KB |
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 | |
bc3d7b01 | 15 | static char sccsid[] = "@(#)chpass.c 5.16 (Berkeley) %G%"; |
8b35ab41 KB |
16 | #endif /* not lint */ |
17 | ||
18 | #include <sys/param.h> | |
8b35ab41 KB |
19 | #include <sys/stat.h> |
20 | #include <sys/signal.h> | |
21 | #include <sys/time.h> | |
22 | #include <sys/resource.h> | |
bc3d7b01 | 23 | #include <fcntl.h> |
b7eb8299 | 24 | #include <pwd.h> |
8b35ab41 KB |
25 | #include <errno.h> |
26 | #include <stdio.h> | |
27 | #include <ctype.h> | |
59f1a2c1 | 28 | #include <string.h> |
435e8dff KB |
29 | #include "chpass.h" |
30 | #include "pathnames.h" | |
8b35ab41 | 31 | |
bc3d7b01 KB |
32 | char *progname = "chpass"; |
33 | char *tempname; | |
b7eb8299 | 34 | uid_t uid; |
8b35ab41 KB |
35 | |
36 | main(argc, argv) | |
37 | int argc; | |
38 | char **argv; | |
39 | { | |
bc3d7b01 | 40 | extern int optind; |
b7eb8299 | 41 | extern char *optarg; |
bc3d7b01 KB |
42 | register enum { NEWSH, LOADENTRY, EDITENTRY } op; |
43 | register struct passwd *pw; | |
44 | struct passwd lpw; | |
45 | int ch, pfd, tfd; | |
46 | char *arg; | |
8b35ab41 | 47 | |
bc3d7b01 | 48 | op = EDITENTRY; |
34dda72c | 49 | while ((ch = getopt(argc, argv, "a:s:")) != EOF) |
b7eb8299 KB |
50 | switch(ch) { |
51 | case 'a': | |
bc3d7b01 KB |
52 | op = LOADENTRY; |
53 | arg = optarg; | |
b7eb8299 | 54 | break; |
34dda72c | 55 | case 's': |
bc3d7b01 KB |
56 | op = NEWSH; |
57 | arg = optarg; | |
34dda72c | 58 | break; |
b7eb8299 KB |
59 | case '?': |
60 | default: | |
61 | usage(); | |
8b35ab41 | 62 | } |
b7eb8299 KB |
63 | argc -= optind; |
64 | argv += optind; | |
65 | ||
bc3d7b01 KB |
66 | uid = getuid(); |
67 | ||
68 | if (op == EDITENTRY || op == NEWSH) | |
b7eb8299 KB |
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 | } | |
34dda72c KB |
83 | if (uid && uid != pw->pw_uid) |
84 | baduser(); | |
b7eb8299 KB |
85 | break; |
86 | default: | |
87 | usage(); | |
8b35ab41 | 88 | } |
8b35ab41 | 89 | |
bc3d7b01 KB |
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); | |
34dda72c | 96 | } |
8b35ab41 | 97 | |
bc3d7b01 KB |
98 | if (op == LOADENTRY) { |
99 | if (uid) | |
100 | baduser(); | |
101 | pw = &lpw; | |
102 | if (!pw_scan(arg, pw)) | |
8b35ab41 | 103 | exit(1); |
8b35ab41 | 104 | } |
7df40547 | 105 | |
b7eb8299 | 106 | /* |
bc3d7b01 KB |
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. | |
b7eb8299 | 126 | */ |
bc3d7b01 KB |
127 | pw_init(); |
128 | pfd = pw_lock(); | |
129 | tfd = pw_tmp(); | |
8b35ab41 | 130 | |
bc3d7b01 KB |
131 | if (op == EDITENTRY) |
132 | edit(tfd, pw); | |
8b35ab41 | 133 | |
bc3d7b01 | 134 | pw_copy(pfd, pw); |
b7eb8299 | 135 | |
bc3d7b01 KB |
136 | if (!pw_mkdb()) |
137 | pw_error((char *)NULL, 0, 1); | |
138 | exit(0); | |
7df40547 KB |
139 | } |
140 | ||
34dda72c KB |
141 | baduser() |
142 | { | |
143 | (void)fprintf(stderr, "chpass: %s\n", strerror(EACCES)); | |
144 | exit(1); | |
145 | } | |
146 | ||
b7eb8299 KB |
147 | usage() |
148 | { | |
34dda72c | 149 | (void)fprintf(stderr, "usage: chpass [-a list] [-s shell] [user]\n"); |
b7eb8299 KB |
150 | exit(1); |
151 | } |