]>
Commit | Line | Data |
---|---|---|
6dbe3af9 KZ |
1 | /* |
2 | * Copyright (c) 1987 Regents of the University of California. | |
3 | * All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms, with or without | |
6 | * modification, are permitted provided that the following conditions | |
7 | * are met: | |
8 | * 1. Redistributions of source code must retain the above copyright | |
9 | * notice, this list of conditions and the following disclaimer. | |
10 | * 2. Redistributions in binary form must reproduce the above copyright | |
11 | * notice, this list of conditions and the following disclaimer in the | |
12 | * documentation and/or other materials provided with the distribution. | |
13 | * 3. All advertising materials mentioning features or use of this software | |
14 | * must display the following acknowledgement: | |
15 | * This product includes software developed by the University of | |
16 | * California, Berkeley and its contributors. | |
17 | * 4. Neither the name of the University nor the names of its contributors | |
18 | * may be used to endorse or promote products derived from this software | |
19 | * without specific prior written permission. | |
20 | * | |
21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
31 | * SUCH DAMAGE. | |
726f69e2 KZ |
32 | * |
33 | * Updated Thu Oct 12 09:56:55 1995 by faith@cs.unc.edu with security | |
34 | * patches from Zefram <A.Main@dcs.warwick.ac.uk> | |
35 | * | |
fd6b7a7f KZ |
36 | * Updated Thu Nov 9 21:58:53 1995 by Martin Schulze |
37 | * <joey@finlandia.infodrom.north.de>. Support for vigr. | |
38 | * | |
39 | * Martin Schulze's patches adapted to Util-Linux by Nicolai Langfeldt. | |
6dbe3af9 KZ |
40 | */ |
41 | ||
fd6b7a7f KZ |
42 | static char version_string[] = "vipw 1.4"; |
43 | ||
6dbe3af9 KZ |
44 | #include <sys/types.h> |
45 | #include <sys/stat.h> | |
46 | #include <pwd.h> | |
47 | #include <stdio.h> | |
48 | #include <stdlib.h> | |
49 | #include <string.h> | |
50 | #include <sys/param.h> | |
51 | #include <sys/time.h> | |
52 | #include <sys/wait.h> | |
53 | #include <sys/resource.h> | |
54 | #include <sys/file.h> | |
55 | #include <signal.h> | |
56 | #include <fcntl.h> | |
57 | #include <errno.h> | |
58 | #include <paths.h> | |
59 | #include <unistd.h> | |
60 | ||
fd6b7a7f | 61 | #include "setpwnam.h" |
6dbe3af9 | 62 | |
fd6b7a7f | 63 | #define FILENAMELEN 67 |
6dbe3af9 | 64 | |
fd6b7a7f KZ |
65 | char *progname; |
66 | enum { VIPW, VIGR }; | |
67 | int program; | |
68 | char orig_file[FILENAMELEN]; /* original file /etc/passwd or /etc/group */ | |
69 | char tmp_file[FILENAMELEN]; /* tmp file */ | |
70 | char tmptmp_file[FILENAMELEN]; /* very tmp file */ | |
6dbe3af9 | 71 | |
fd6b7a7f | 72 | void pw_error __P((char *, int, int)); |
6dbe3af9 | 73 | |
fd6b7a7f | 74 | void |
6dbe3af9 KZ |
75 | copyfile(from, to) |
76 | register int from, to; | |
77 | { | |
78 | register int nr, nw, off; | |
79 | char buf[8*1024]; | |
80 | ||
81 | while ((nr = read(from, buf, sizeof(buf))) > 0) | |
82 | for (off = 0; off < nr; nr -= nw, off += nw) | |
83 | if ((nw = write(to, buf + off, nr)) < 0) | |
fd6b7a7f KZ |
84 | pw_error(tmp_file, 1, 1); |
85 | ||
6dbe3af9 | 86 | if (nr < 0) |
fd6b7a7f | 87 | pw_error(orig_file, 1, 1); |
6dbe3af9 KZ |
88 | } |
89 | ||
90 | ||
91 | void | |
92 | pw_init() | |
93 | { | |
94 | struct rlimit rlim; | |
95 | ||
96 | /* Unlimited resource limits. */ | |
97 | rlim.rlim_cur = rlim.rlim_max = RLIM_INFINITY; | |
98 | (void)setrlimit(RLIMIT_CPU, &rlim); | |
99 | (void)setrlimit(RLIMIT_FSIZE, &rlim); | |
100 | (void)setrlimit(RLIMIT_STACK, &rlim); | |
101 | (void)setrlimit(RLIMIT_DATA, &rlim); | |
102 | (void)setrlimit(RLIMIT_RSS, &rlim); | |
103 | ||
104 | /* Don't drop core (not really necessary, but GP's). */ | |
105 | rlim.rlim_cur = rlim.rlim_max = 0; | |
106 | (void)setrlimit(RLIMIT_CORE, &rlim); | |
107 | ||
108 | /* Turn off signals. */ | |
109 | (void)signal(SIGALRM, SIG_IGN); | |
110 | (void)signal(SIGHUP, SIG_IGN); | |
111 | (void)signal(SIGINT, SIG_IGN); | |
112 | (void)signal(SIGPIPE, SIG_IGN); | |
113 | (void)signal(SIGQUIT, SIG_IGN); | |
114 | (void)signal(SIGTERM, SIG_IGN); | |
115 | (void)signal(SIGTSTP, SIG_IGN); | |
116 | (void)signal(SIGTTOU, SIG_IGN); | |
117 | ||
118 | /* Create with exact permissions. */ | |
119 | (void)umask(0); | |
120 | } | |
121 | ||
122 | int | |
123 | pw_lock() | |
124 | { | |
6dbe3af9 | 125 | int lockfd, fd, ret; |
6dbe3af9 KZ |
126 | |
127 | /* | |
128 | * If the password file doesn't exist, the system is hosed. | |
129 | * Might as well try to build one. Set the close-on-exec bit so | |
130 | * that users can't get at the encrypted passwords while editing. | |
131 | * Open should allow flock'ing the file; see 4.4BSD. XXX | |
132 | */ | |
fd6b7a7f KZ |
133 | lockfd = open(orig_file, O_RDONLY, 0); |
134 | ||
6dbe3af9 KZ |
135 | if (lockfd < 0) { |
136 | (void)fprintf(stderr, "%s: %s: %s\n", | |
fd6b7a7f | 137 | progname, orig_file, strerror(errno)); |
6dbe3af9 KZ |
138 | exit(1); |
139 | } | |
726f69e2 | 140 | #if 0 /* flock()ing is superfluous here, with the ptmp/ptmptmp system. */ |
6dbe3af9 KZ |
141 | if (flock(lockfd, LOCK_EX|LOCK_NB)) { |
142 | (void)fprintf(stderr, | |
fd6b7a7f KZ |
143 | "%s: the %s file is busy.\n", progname, |
144 | program == VIPW ? "password" : "group" ); | |
6dbe3af9 KZ |
145 | exit(1); |
146 | } | |
726f69e2 | 147 | #endif |
6dbe3af9 | 148 | |
fd6b7a7f KZ |
149 | if ((fd = open(tmptmp_file, O_WRONLY|O_CREAT, 0644)) == -1) { |
150 | (void)fprintf(stderr, | |
151 | "%s: %s: %s\n", progname, tmptmp_file, strerror(errno)); | |
152 | exit(1); | |
6dbe3af9 | 153 | } |
fd6b7a7f KZ |
154 | ret = link(tmptmp_file, tmp_file); |
155 | (void)unlink(tmptmp_file); | |
6dbe3af9 KZ |
156 | if (ret == -1) { |
157 | if (errno == EEXIST) | |
158 | (void)fprintf(stderr, | |
2b6fc908 KZ |
159 | "%s: the %s file is busy (%s present)\n", |
160 | progname, | |
161 | program == VIPW ? "password" : "group", | |
162 | tmp_file); | |
6dbe3af9 KZ |
163 | else |
164 | (void)fprintf(stderr, "%s: can't link %s: %s\n", progname, | |
fd6b7a7f | 165 | tmp_file, strerror(errno)); |
6dbe3af9 KZ |
166 | exit(1); |
167 | } | |
168 | copyfile(lockfd, fd); | |
169 | (void)close(lockfd); | |
170 | (void)close(fd); | |
171 | return(1); | |
172 | } | |
173 | ||
174 | void | |
175 | pw_unlock() | |
176 | { | |
fd6b7a7f KZ |
177 | char tmp[FILENAMELEN]; |
178 | ||
179 | sprintf(tmp, "%s%s", orig_file, ".OLD"); | |
180 | unlink(tmp); | |
181 | link(orig_file, tmp); | |
182 | if (rename(tmp_file, orig_file) == -1) { | |
183 | (void)fprintf(stderr, | |
184 | "%s: can't unlock %s: %s (your changes are still in %s)\n", | |
185 | progname, orig_file, strerror(errno), tmp_file); | |
186 | exit(1); | |
187 | } | |
188 | (void)unlink(tmp_file); | |
6dbe3af9 KZ |
189 | } |
190 | ||
191 | ||
192 | void | |
5c36a0eb | 193 | pw_edit(int notsetuid) |
6dbe3af9 KZ |
194 | { |
195 | int pstat; | |
196 | pid_t pid; | |
197 | char *p, *editor; | |
198 | ||
199 | if (!(editor = getenv("EDITOR"))) | |
5c36a0eb | 200 | editor = strdup(_PATH_VI); /* adia@egnatia.ee.auth.gr */ |
726f69e2 | 201 | if ((p = strrchr(strtok(editor," \t"), '/')) != NULL) |
6dbe3af9 KZ |
202 | ++p; |
203 | else | |
204 | p = editor; | |
205 | ||
2b6fc908 KZ |
206 | pid = fork(); |
207 | if (pid < 0) { | |
208 | (void)fprintf(stderr, "%s: Cannot fork\n", progname); | |
209 | exit(1); | |
210 | } | |
211 | if (!pid) { | |
6dbe3af9 KZ |
212 | if (notsetuid) { |
213 | (void)setgid(getgid()); | |
214 | (void)setuid(getuid()); | |
215 | } | |
fd6b7a7f | 216 | execlp(editor, p, tmp_file, NULL); |
6dbe3af9 KZ |
217 | _exit(1); |
218 | } | |
219 | for (;;) { | |
220 | pid = waitpid(pid, &pstat, WUNTRACED); | |
221 | if (WIFSTOPPED(pstat)) { | |
222 | /* the editor suspended, so suspend us as well */ | |
223 | kill(getpid(), SIGSTOP); | |
224 | kill(pid, SIGCONT); | |
225 | } else { | |
226 | break; | |
227 | } | |
228 | } | |
229 | if (pid == -1 || !WIFEXITED(pstat) || WEXITSTATUS(pstat) != 0) | |
230 | pw_error(editor, 1, 1); | |
231 | } | |
232 | ||
233 | void | |
234 | pw_error(name, err, eval) | |
235 | char *name; | |
236 | int err, eval; | |
237 | { | |
238 | int sverrno; | |
239 | ||
240 | if (err) { | |
241 | sverrno = errno; | |
242 | (void)fprintf(stderr, "%s: ", progname); | |
243 | if (name) | |
244 | (void)fprintf(stderr, "%s: ", name); | |
245 | (void)fprintf(stderr, "%s\n", strerror(sverrno)); | |
246 | } | |
247 | (void)fprintf(stderr, | |
fd6b7a7f KZ |
248 | "%s: %s unchanged\n", progname, orig_file); |
249 | (void)unlink(tmp_file); | |
6dbe3af9 KZ |
250 | exit(eval); |
251 | } | |
252 | ||
fd6b7a7f | 253 | int main(int argc, char *argv[]) |
6dbe3af9 | 254 | { |
fd6b7a7f KZ |
255 | struct stat begin, end; |
256 | ||
257 | bzero(tmp_file, FILENAMELEN); | |
258 | progname = (rindex(argv[0], '/')) ? rindex(argv[0], '/') + 1 : argv[0]; | |
259 | if (!strcmp(progname, "vigr")) { | |
260 | program = VIGR; | |
261 | strncpy(orig_file, GROUP_FILE, FILENAMELEN-1); | |
262 | strncpy(tmp_file, GTMP_FILE, FILENAMELEN-1); | |
263 | strncpy(tmptmp_file, GTMPTMP_FILE, FILENAMELEN-1); | |
264 | } | |
265 | else { | |
266 | program = VIPW; | |
267 | strncpy(orig_file, PASSWD_FILE, FILENAMELEN-1); | |
268 | strncpy(tmp_file, PTMP_FILE, FILENAMELEN-1); | |
269 | strncpy(tmptmp_file, PTMPTMP_FILE, FILENAMELEN-1); | |
270 | } | |
271 | ||
272 | if ((argc > 1) && | |
273 | (!strcmp(argv[1], "-V") || !strcmp(argv[1], "--version"))) { | |
274 | printf("%s\n", version_string); | |
275 | exit(0); | |
276 | } | |
277 | ||
278 | pw_init(); | |
279 | pw_lock(); | |
280 | ||
281 | if (stat(tmp_file, &begin)) | |
282 | pw_error(tmp_file, 1, 1); | |
283 | pw_edit(0); | |
284 | if (stat(tmp_file, &end)) | |
285 | pw_error(tmp_file, 1, 1); | |
286 | if (begin.st_mtime == end.st_mtime) { | |
287 | (void)fprintf(stderr, "%s: no changes made\n", progname); | |
288 | pw_error((char *)NULL, 0, 0); | |
289 | } | |
290 | pw_unlock(); | |
291 | exit(0); | |
6dbe3af9 | 292 | } |