passwd.c revision 1.26 1 /* $NetBSD: passwd.c,v 1.26 2000/10/04 08:40:49 itojun Exp $ */
2
3 /*
4 * Copyright (c) 1987, 1993, 1994, 1995
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 #if defined(LIBC_SCCS) && !defined(lint)
38 __RCSID("$NetBSD: passwd.c,v 1.26 2000/10/04 08:40:49 itojun Exp $");
39 #endif /* LIBC_SCCS and not lint */
40
41 #include <sys/types.h>
42 #include <sys/stat.h>
43 #include <sys/time.h>
44 #include <sys/resource.h>
45 #include <sys/wait.h>
46
47 #include <assert.h>
48 #include <ctype.h>
49 #include <err.h>
50 #include <errno.h>
51 #include <fcntl.h>
52 #include <limits.h>
53 #include <paths.h>
54 #include <pwd.h>
55 #include <signal.h>
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <string.h>
59 #include <unistd.h>
60 #include <util.h>
61
62 static void pw_cont(int sig);
63 static int pw_equal(char *buf, struct passwd *old_pw);
64 static const char *pw_default(const char *option);
65 static int read_line(FILE *fp, char *line, int max);
66 static void trim_whitespace(char *line);
67
68 int
69 pw_lock(int retries)
70 {
71 int i, fd;
72 mode_t old_mode;
73 int oerrno;
74
75 /* Acquire the lock file. */
76 old_mode = umask(0);
77 fd = open(_PATH_MASTERPASSWD_LOCK, O_WRONLY|O_CREAT|O_EXCL, 0600);
78 for (i = 0; i < retries && fd < 0 && errno == EEXIST; i++) {
79 sleep(1);
80 fd = open(_PATH_MASTERPASSWD_LOCK, O_WRONLY|O_CREAT|O_EXCL,
81 0600);
82 }
83 oerrno = errno;
84 (void)umask(old_mode);
85 errno = oerrno;
86 return(fd);
87 }
88
89 int
90 pw_mkdb(void)
91 {
92 int pstat;
93 pid_t pid;
94
95 pid = vfork();
96 if (pid == -1)
97 return (-1);
98
99 if (pid == 0) {
100 execl(_PATH_PWD_MKDB, "pwd_mkdb", "-p",
101 _PATH_MASTERPASSWD_LOCK, NULL);
102 _exit(1);
103 }
104 pid = waitpid(pid, &pstat, 0);
105 if (pid == -1 || !WIFEXITED(pstat) || WEXITSTATUS(pstat) != 0)
106 return(-1);
107 return(0);
108 }
109
110 int
111 pw_abort(void)
112 {
113
114 return(unlink(_PATH_MASTERPASSWD_LOCK));
115 }
116
117 /* Everything below this point is intended for the convenience of programs
118 * which allow a user to interactively edit the passwd file. Errors in the
119 * routines below will cause the process to abort. */
120
121 static pid_t editpid = -1;
122
123 static void
124 pw_cont(int sig)
125 {
126
127 if (editpid != -1)
128 kill(editpid, sig);
129 }
130
131 void
132 pw_init(void)
133 {
134 struct rlimit rlim;
135
136 /* Unlimited resource limits. */
137 rlim.rlim_cur = rlim.rlim_max = RLIM_INFINITY;
138 (void)setrlimit(RLIMIT_CPU, &rlim);
139 (void)setrlimit(RLIMIT_FSIZE, &rlim);
140 (void)setrlimit(RLIMIT_STACK, &rlim);
141 (void)setrlimit(RLIMIT_DATA, &rlim);
142 (void)setrlimit(RLIMIT_RSS, &rlim);
143
144 /* Don't drop core (not really necessary, but GP's). */
145 rlim.rlim_cur = rlim.rlim_max = 0;
146 (void)setrlimit(RLIMIT_CORE, &rlim);
147
148 /* Turn off signals. */
149 (void)signal(SIGALRM, SIG_IGN);
150 (void)signal(SIGHUP, SIG_IGN);
151 (void)signal(SIGINT, SIG_IGN);
152 (void)signal(SIGPIPE, SIG_IGN);
153 (void)signal(SIGQUIT, SIG_IGN);
154 (void)signal(SIGTERM, SIG_IGN);
155 (void)signal(SIGCONT, pw_cont);
156 }
157
158 void
159 pw_edit(int notsetuid, const char *filename)
160 {
161 int pstat;
162 char *p, *editor;
163 char *argp[] = { "sh", "-c", NULL, NULL };
164
165 #ifdef __GNUC__
166 (void) &editor;
167 #endif
168
169 if (filename == NULL)
170 filename = _PATH_MASTERPASSWD_LOCK;
171
172 if ((editor = getenv("EDITOR")) == NULL)
173 editor = _PATH_VI;
174
175 p = malloc(strlen(editor) + 1 + strlen(filename) + 1);
176 if (p == NULL)
177 return;
178
179 sprintf(p, "%s %s", editor, filename);
180 argp[2] = p;
181
182 switch(editpid = vfork()) {
183 case -1:
184 free(p);
185 return;
186 case 0:
187 if (notsetuid) {
188 setgid(getgid());
189 setuid(getuid());
190 }
191 execvp(_PATH_BSHELL, argp);
192 _exit(1);
193 }
194
195 free(p);
196
197 for (;;) {
198 editpid = waitpid(editpid, (int *)&pstat, WUNTRACED);
199 if (editpid == -1)
200 pw_error(editor, 1, 1);
201 else if (WIFSTOPPED(pstat))
202 raise(WSTOPSIG(pstat));
203 else if (WIFEXITED(pstat) && WEXITSTATUS(pstat) == 0)
204 break;
205 else
206 pw_error(editor, 1, 1);
207 }
208 editpid = -1;
209 }
210
211 void
212 pw_prompt(void)
213 {
214 int c;
215
216 (void)printf("re-edit the password file? [y]: ");
217 (void)fflush(stdout);
218 c = getchar();
219 if (c != EOF && c != '\n')
220 while (getchar() != '\n');
221 if (c == 'n')
222 pw_error(NULL, 0, 0);
223 }
224
225 /* for use in pw_copy(). Compare a pw entry to a pw struct. */
226 static int
227 pw_equal(char *buf, struct passwd *pw)
228 {
229 struct passwd buf_pw;
230 int len;
231
232 _DIAGASSERT(buf != NULL);
233 _DIAGASSERT(pw != NULL);
234
235 len = strlen (buf);
236 if (buf[len-1] == '\n')
237 buf[len-1] = '\0';
238 if (!pw_scan(buf, &buf_pw, NULL))
239 return 0;
240 return !strcmp(pw->pw_name, buf_pw.pw_name)
241 && pw->pw_uid == buf_pw.pw_uid
242 && pw->pw_gid == buf_pw.pw_gid
243 && !strcmp(pw->pw_class, buf_pw.pw_class)
244 && (long)pw->pw_change == (long)buf_pw.pw_change
245 && (long)pw->pw_expire == (long)buf_pw.pw_expire
246 && !strcmp(pw->pw_gecos, buf_pw.pw_gecos)
247 && !strcmp(pw->pw_dir, buf_pw.pw_dir)
248 && !strcmp(pw->pw_shell, buf_pw.pw_shell);
249 }
250
251 void
252 pw_copy(int ffd, int tfd, struct passwd *pw, struct passwd *old_pw)
253 {
254 FILE *from, *to;
255 int done;
256 char *p, buf[8192];
257
258 _DIAGASSERT(pw != NULL);
259 /* old_pw may be NULL */
260
261 if (!(from = fdopen(ffd, "r")))
262 pw_error(_PATH_MASTERPASSWD, 1, 1);
263 if (!(to = fdopen(tfd, "w")))
264 pw_error(_PATH_MASTERPASSWD_LOCK, 1, 1);
265
266 for (done = 0; fgets(buf, sizeof(buf), from);) {
267 if (!strchr(buf, '\n')) {
268 warnx("%s: line too long", _PATH_MASTERPASSWD);
269 pw_error(NULL, 0, 1);
270 }
271 if (done) {
272 (void)fprintf(to, "%s", buf);
273 if (ferror(to))
274 goto err;
275 continue;
276 }
277 if (!(p = strchr(buf, ':'))) {
278 warnx("%s: corrupted entry", _PATH_MASTERPASSWD);
279 pw_error(NULL, 0, 1);
280 }
281 *p = '\0';
282 if (strcmp(buf, pw->pw_name)) {
283 *p = ':';
284 (void)fprintf(to, "%s", buf);
285 if (ferror(to))
286 goto err;
287 continue;
288 }
289 *p = ':';
290 if (old_pw && !pw_equal(buf, old_pw)) {
291 warnx("%s: entry inconsistent",
292 _PATH_MASTERPASSWD);
293 pw_error(NULL, 0, 1);
294 }
295 (void)fprintf(to, "%s:%s:%d:%d:%s:%ld:%ld:%s:%s:%s\n",
296 pw->pw_name, pw->pw_passwd, pw->pw_uid, pw->pw_gid,
297 pw->pw_class, (long)pw->pw_change, (long)pw->pw_expire,
298 pw->pw_gecos, pw->pw_dir, pw->pw_shell);
299 done = 1;
300 if (ferror(to))
301 goto err;
302 }
303 /* Only append a new entry if real uid is root! */
304 if (!done) {
305 if (getuid() == 0)
306 (void)fprintf(to, "%s:%s:%d:%d:%s:%ld:%ld:%s:%s:%s\n",
307 pw->pw_name, pw->pw_passwd, pw->pw_uid, pw->pw_gid,
308 pw->pw_class, (long)pw->pw_change,
309 (long)pw->pw_expire, pw->pw_gecos, pw->pw_dir,
310 pw->pw_shell);
311 else
312 warnx("%s: changes not made, no such entry",
313 _PATH_MASTERPASSWD);
314 }
315
316 if (ferror(to))
317 err: pw_error(NULL, 1, 1);
318 (void)fclose(to);
319 }
320
321 void
322 pw_error(const char *name, int err, int eval)
323 {
324
325 if (err) {
326 if (name)
327 warn("%s", name);
328 else
329 warn(NULL);
330 }
331
332 warnx("%s: unchanged", _PATH_MASTERPASSWD);
333 pw_abort();
334 exit(eval);
335 }
336
337 /* Removes head and/or tail spaces. */
338 static void
339 trim_whitespace(char *line)
340 {
341 char *p;
342
343 /* Remove leading spaces */
344 p = line;
345 while (isspace((unsigned char) *p))
346 p++;
347 memmove(line, p, strlen(p) + 1);
348
349 /* Remove trailing spaces */
350 p = line + strlen(line) - 1;
351 while (isspace((unsigned char) *p))
352 p--;
353 *(p + 1) = '\0';
354 }
355
356
357 /* Get one line, remove spaces from front and tail */
358 static int
359 read_line(FILE *fp, char *line, int max)
360 {
361 char *p;
362
363 /* Read one line of config */
364 if (fgets(line, max, fp) == NULL)
365 return (0);
366
367 if ((p = strchr(line, '\n')) == NULL) {
368 warnx("line too long");
369 return (0);
370 }
371 *p = '\0';
372
373 /* Remove comments */
374 if ((p = strchr(line, '#')) != NULL)
375 *p = '\0';
376
377 trim_whitespace(line);
378 return (1);
379 }
380
381 static const char *
382 pw_default(const char *option)
383 {
384 static const char *options[][2] = {
385 { "localcipher", "old" },
386 { "ypcipher", "old" },
387 };
388 int i;
389
390 for (i = 0; i < sizeof(options) / sizeof(options[0]); i++)
391 if (strcmp(options[i][0], option) == 0)
392 return (options[i][1]);
393
394 return (NULL);
395 }
396
397 /*
398 * Retrieve password information from the /etc/passwd.conf file, at the
399 * moment this is only for choosing the cipher to use. It could easily be
400 * used for other authentication methods as well.
401 */
402 void
403 pw_getconf(char *data, size_t max, const char *key, const char *option)
404 {
405 FILE *fp;
406 char line[LINE_MAX], *p, *p2;
407 static char result[LINE_MAX];
408 int got, found;
409 const char *cp;
410
411 got = 0;
412 found = 0;
413 result[0] = '\0';
414
415 if ((fp = fopen(_PATH_PASSWDCONF, "r")) == NULL) {
416 if ((cp = pw_default(option)) != NULL)
417 strlcpy(data, cp, max);
418 else
419 data[0] = '\0';
420 return;
421 }
422
423 while (!found && (got || read_line(fp, line, LINE_MAX))) {
424 got = 0;
425
426 if (strncmp(key, line, strlen(key)) != 0 ||
427 line[strlen(key)] != ':')
428 continue;
429
430 /* Now we found our specified key */
431 while (read_line(fp, line, LINE_MAX)) {
432 /* Leaving key field */
433 if (line[0] != '\0' && strchr(line + 1, ':') != NULL) {
434 got = 1;
435 break;
436 }
437 p2 = line;
438 if ((p = strsep(&p2, "=")) == NULL || p2 == NULL)
439 continue;
440 trim_whitespace(p);
441
442 if (!strncmp(p, option, strlen(option))) {
443 trim_whitespace(p2);
444 strcpy(result, p2);
445 found = 1;
446 break;
447 }
448 }
449 }
450 fclose(fp);
451
452 /*
453 * If we got no result and were looking for a default
454 * value, try hard coded defaults.
455 */
456
457 if (strlen(result) == 0 && strcmp(key, "default") == 0 &&
458 (cp = pw_default(option)) != NULL)
459 strlcpy(data, cp, max);
460 else
461 strlcpy(data, result, max);
462 }
463