passwd.c revision 1.34 1 /* $NetBSD: passwd.c,v 1.34 2002/04/17 11:14:28 ad 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.34 2002/04/17 11:14:28 ad Exp $");
39 #endif /* LIBC_SCCS and not lint */
40
41 #include <sys/types.h>
42 #include <sys/param.h>
43 #include <sys/stat.h>
44 #include <sys/time.h>
45 #include <sys/resource.h>
46 #include <sys/wait.h>
47
48 #include <assert.h>
49 #include <ctype.h>
50 #include <err.h>
51 #include <errno.h>
52 #include <fcntl.h>
53 #include <limits.h>
54 #include <paths.h>
55 #include <pwd.h>
56 #include <signal.h>
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <string.h>
60 #include <unistd.h>
61 #include <util.h>
62
63 static const char *pw_filename(const char *filename);
64 static void pw_cont(int sig);
65 static int pw_equal(char *buf, struct passwd *old_pw);
66 static const char *pw_default(const char *option);
67 static int read_line(FILE *fp, char *line, int max);
68 static void trim_whitespace(char *line);
69
70 static char pw_prefix[MAXPATHLEN];
71
72 const char *
73 pw_getprefix(void)
74 {
75
76 return(pw_prefix);
77 }
78
79 int
80 pw_setprefix(const char *new_prefix)
81 {
82 size_t length;
83
84 _DIAGASSERT(new_prefix != NULL);
85
86 length = strlen(new_prefix);
87 if (length < sizeof(pw_prefix)) {
88 (void)strcpy(pw_prefix, new_prefix);
89 while (length > 0 && pw_prefix[length - 1] == '/')
90 pw_prefix[--length] = '\0';
91 return(0);
92 }
93 errno = ENAMETOOLONG;
94 return(-1);
95 }
96
97 static const char *
98 pw_filename(const char *filename)
99 {
100 static char newfilename[MAXPATHLEN];
101
102 _DIAGASSERT(filename != NULL);
103
104 if (pw_prefix[0] == '\0')
105 return filename;
106
107 if (strlen(pw_prefix) + strlen(filename) < sizeof(newfilename))
108 return strcat(strcpy(newfilename, pw_prefix), filename);
109
110 errno = ENAMETOOLONG;
111 return(NULL);
112 }
113
114 int
115 pw_lock(int retries)
116 {
117 const char *filename;
118 int i, fd;
119 mode_t old_mode;
120 int oerrno;
121
122 /* Acquire the lock file. */
123 filename = pw_filename(_PATH_MASTERPASSWD_LOCK);
124 if (filename == NULL)
125 return(-1);
126 old_mode = umask(0);
127 fd = open(filename, O_WRONLY|O_CREAT|O_EXCL, 0600);
128 for (i = 0; i < retries && fd < 0 && errno == EEXIST; i++) {
129 sleep(1);
130 fd = open(filename, O_WRONLY|O_CREAT|O_EXCL,
131 0600);
132 }
133 oerrno = errno;
134 (void)umask(old_mode);
135 errno = oerrno;
136 return(fd);
137 }
138
139 int
140 pw_mkdb(username, secureonly)
141 const char *username;
142 int secureonly;
143 {
144 const char *args[9];
145 int pstat, i;
146 pid_t pid;
147
148 pid = vfork();
149 if (pid == -1)
150 return (-1);
151
152 if (pid == 0) {
153 args[0] = "pwd_mkdb";
154 args[1] = "-d";
155 args[2] = pw_prefix;
156 args[3] = "-p";
157 i = 4;
158
159 if (secureonly)
160 args[i++] = "-s";
161 if (username != NULL) {
162 args[i++] = "-u";
163 args[i++] = username;
164 }
165
166 args[i++] = pw_filename(_PATH_MASTERPASSWD_LOCK);
167 args[i] = NULL;
168 execv(_PATH_PWD_MKDB, (char * const *)args);
169 _exit(1);
170 }
171 pid = waitpid(pid, &pstat, 0);
172 if (pid == -1 || !WIFEXITED(pstat) || WEXITSTATUS(pstat) != 0)
173 return(-1);
174 return(0);
175 }
176
177 int
178 pw_abort(void)
179 {
180 const char *filename;
181
182 filename = pw_filename(_PATH_MASTERPASSWD_LOCK);
183 return((filename == NULL) ? -1 : unlink(filename));
184 }
185
186 /* Everything below this point is intended for the convenience of programs
187 * which allow a user to interactively edit the passwd file. Errors in the
188 * routines below will cause the process to abort. */
189
190 static pid_t editpid = -1;
191
192 static void
193 pw_cont(int sig)
194 {
195
196 if (editpid != -1)
197 kill(editpid, sig);
198 }
199
200 void
201 pw_init(void)
202 {
203 struct rlimit rlim;
204
205 /* Unlimited resource limits. */
206 rlim.rlim_cur = rlim.rlim_max = RLIM_INFINITY;
207 (void)setrlimit(RLIMIT_CPU, &rlim);
208 (void)setrlimit(RLIMIT_FSIZE, &rlim);
209 (void)setrlimit(RLIMIT_STACK, &rlim);
210 (void)setrlimit(RLIMIT_DATA, &rlim);
211 (void)setrlimit(RLIMIT_RSS, &rlim);
212
213 /* Don't drop core (not really necessary, but GP's). */
214 rlim.rlim_cur = rlim.rlim_max = 0;
215 (void)setrlimit(RLIMIT_CORE, &rlim);
216
217 /* Turn off signals. */
218 (void)signal(SIGALRM, SIG_IGN);
219 (void)signal(SIGHUP, SIG_IGN);
220 (void)signal(SIGINT, SIG_IGN);
221 (void)signal(SIGPIPE, SIG_IGN);
222 (void)signal(SIGQUIT, SIG_IGN);
223 (void)signal(SIGTERM, SIG_IGN);
224 (void)signal(SIGCONT, pw_cont);
225 }
226
227 void
228 pw_edit(int notsetuid, const char *filename)
229 {
230 int pstat;
231 char *p, *editor;
232 char *argp[] = { "sh", "-c", NULL, NULL };
233
234 #ifdef __GNUC__
235 (void) &editor;
236 #endif
237
238 if (filename == NULL)
239 filename = _PATH_MASTERPASSWD_LOCK;
240
241 filename = pw_filename(filename);
242 if (filename == NULL)
243 return;
244
245 if ((editor = getenv("EDITOR")) == NULL)
246 editor = _PATH_VI;
247
248 p = malloc(strlen(editor) + 1 + strlen(filename) + 1);
249 if (p == NULL)
250 return;
251
252 sprintf(p, "%s %s", editor, filename);
253 argp[2] = p;
254
255 switch(editpid = vfork()) {
256 case -1:
257 free(p);
258 return;
259 case 0:
260 if (notsetuid) {
261 setgid(getgid());
262 setuid(getuid());
263 }
264 execvp(_PATH_BSHELL, argp);
265 _exit(1);
266 }
267
268 free(p);
269
270 for (;;) {
271 editpid = waitpid(editpid, (int *)&pstat, WUNTRACED);
272 if (editpid == -1)
273 pw_error(editor, 1, 1);
274 else if (WIFSTOPPED(pstat))
275 raise(WSTOPSIG(pstat));
276 else if (WIFEXITED(pstat) && WEXITSTATUS(pstat) == 0)
277 break;
278 else
279 pw_error(editor, 1, 1);
280 }
281 editpid = -1;
282 }
283
284 void
285 pw_prompt(void)
286 {
287 int c;
288
289 (void)printf("re-edit the password file? [y]: ");
290 (void)fflush(stdout);
291 c = getchar();
292 if (c != EOF && c != '\n')
293 while (getchar() != '\n');
294 if (c == 'n')
295 pw_error(NULL, 0, 0);
296 }
297
298 /* for use in pw_copy(). Compare a pw entry to a pw struct. */
299 static int
300 pw_equal(char *buf, struct passwd *pw)
301 {
302 struct passwd buf_pw;
303 int len;
304
305 _DIAGASSERT(buf != NULL);
306 _DIAGASSERT(pw != NULL);
307
308 len = strlen (buf);
309 if (buf[len-1] == '\n')
310 buf[len-1] = '\0';
311 if (!pw_scan(buf, &buf_pw, NULL))
312 return 0;
313 return !strcmp(pw->pw_name, buf_pw.pw_name)
314 && pw->pw_uid == buf_pw.pw_uid
315 && pw->pw_gid == buf_pw.pw_gid
316 && !strcmp(pw->pw_class, buf_pw.pw_class)
317 && (long)pw->pw_change == (long)buf_pw.pw_change
318 && (long)pw->pw_expire == (long)buf_pw.pw_expire
319 && !strcmp(pw->pw_gecos, buf_pw.pw_gecos)
320 && !strcmp(pw->pw_dir, buf_pw.pw_dir)
321 && !strcmp(pw->pw_shell, buf_pw.pw_shell);
322 }
323
324 void
325 pw_copy(int ffd, int tfd, struct passwd *pw, struct passwd *old_pw)
326 {
327 const char *filename;
328 char mpwd[MAXPATHLEN], mpwdl[MAXPATHLEN], *p, buf[8192];
329 FILE *from, *to;
330 int done;
331
332 _DIAGASSERT(pw != NULL);
333 /* old_pw may be NULL */
334
335 if ((filename = pw_filename(_PATH_MASTERPASSWD)) == NULL)
336 pw_error(pw_prefix, 1,1);
337 (void)strcpy(mpwd, filename);
338 if ((filename = pw_filename(_PATH_MASTERPASSWD_LOCK)) == NULL)
339 pw_error(pw_prefix, 1,1);
340 (void)strcpy(mpwdl, filename);
341
342 if (!(from = fdopen(ffd, "r")))
343 pw_error(mpwd, 1, 1);
344 if (!(to = fdopen(tfd, "w")))
345 pw_error(mpwdl, 1, 1);
346
347 for (done = 0; fgets(buf, sizeof(buf), from);) {
348 if (!strchr(buf, '\n')) {
349 warnx("%s: line too long", mpwd);
350 pw_error(NULL, 0, 1);
351 }
352 if (done) {
353 (void)fprintf(to, "%s", buf);
354 if (ferror(to))
355 goto err;
356 continue;
357 }
358 if (!(p = strchr(buf, ':'))) {
359 warnx("%s: corrupted entry", mpwd);
360 pw_error(NULL, 0, 1);
361 }
362 *p = '\0';
363 if (strcmp(buf, pw->pw_name)) {
364 *p = ':';
365 (void)fprintf(to, "%s", buf);
366 if (ferror(to))
367 goto err;
368 continue;
369 }
370 *p = ':';
371 if (old_pw && !pw_equal(buf, old_pw)) {
372 warnx("%s: entry inconsistent", mpwd);
373 pw_error(NULL, 0, 1);
374 }
375 (void)fprintf(to, "%s:%s:%d:%d:%s:%ld:%ld:%s:%s:%s\n",
376 pw->pw_name, pw->pw_passwd, pw->pw_uid, pw->pw_gid,
377 pw->pw_class, (long)pw->pw_change, (long)pw->pw_expire,
378 pw->pw_gecos, pw->pw_dir, pw->pw_shell);
379 done = 1;
380 if (ferror(to))
381 goto err;
382 }
383 /* Only append a new entry if real uid is root! */
384 if (!done) {
385 if (getuid() == 0)
386 (void)fprintf(to, "%s:%s:%d:%d:%s:%ld:%ld:%s:%s:%s\n",
387 pw->pw_name, pw->pw_passwd, pw->pw_uid, pw->pw_gid,
388 pw->pw_class, (long)pw->pw_change,
389 (long)pw->pw_expire, pw->pw_gecos, pw->pw_dir,
390 pw->pw_shell);
391 else
392 warnx("%s: changes not made, no such entry", mpwd);
393 }
394
395 if (ferror(to))
396 err: pw_error(NULL, 1, 1);
397 (void)fclose(to);
398 }
399
400 void
401 pw_error(const char *name, int error, int eval)
402 {
403
404 if (error) {
405 if (name)
406 warn("%s", name);
407 else
408 warn(NULL);
409 }
410
411 warnx("%s%s: unchanged", pw_prefix, _PATH_MASTERPASSWD);
412 pw_abort();
413 exit(eval);
414 }
415
416 /* Removes head and/or tail spaces. */
417 static void
418 trim_whitespace(char *line)
419 {
420 char *p;
421
422 _DIAGASSERT(line != NULL);
423
424 /* Remove leading spaces */
425 p = line;
426 while (isspace((unsigned char) *p))
427 p++;
428 memmove(line, p, strlen(p) + 1);
429
430 /* Remove trailing spaces */
431 p = line + strlen(line) - 1;
432 while (isspace((unsigned char) *p))
433 p--;
434 *(p + 1) = '\0';
435 }
436
437
438 /* Get one line, remove spaces from front and tail */
439 static int
440 read_line(FILE *fp, char *line, int max)
441 {
442 char *p;
443
444 _DIAGASSERT(fp != NULL);
445 _DIAGASSERT(line != NULL);
446
447 /* Read one line of config */
448 if (fgets(line, max, fp) == NULL)
449 return (0);
450
451 if ((p = strchr(line, '\n')) == NULL) {
452 warnx("line too long");
453 return (0);
454 }
455 *p = '\0';
456
457 /* Remove comments */
458 if ((p = strchr(line, '#')) != NULL)
459 *p = '\0';
460
461 trim_whitespace(line);
462 return (1);
463 }
464
465 static const char *
466 pw_default(const char *option)
467 {
468 static const char *options[][2] = {
469 { "localcipher", "old" },
470 { "ypcipher", "old" },
471 };
472 int i;
473
474 _DIAGASSERT(option != NULL);
475 for (i = 0; i < sizeof(options) / sizeof(options[0]); i++)
476 if (strcmp(options[i][0], option) == 0)
477 return (options[i][1]);
478
479 return (NULL);
480 }
481
482 /*
483 * Retrieve password information from the /etc/passwd.conf file, at the
484 * moment this is only for choosing the cipher to use. It could easily be
485 * used for other authentication methods as well.
486 */
487 void
488 pw_getconf(char *data, size_t max, const char *key, const char *option)
489 {
490 FILE *fp;
491 char line[LINE_MAX], *p, *p2;
492 static char result[LINE_MAX];
493 int got, found;
494 const char *cp;
495
496 _DIAGASSERT(data != NULL);
497 _DIAGASSERT(key != NULL);
498 _DIAGASSERT(option != NULL);
499
500 got = 0;
501 found = 0;
502 result[0] = '\0';
503
504 if ((fp = fopen(_PATH_PASSWD_CONF, "r")) == NULL) {
505 if ((cp = pw_default(option)) != NULL)
506 strlcpy(data, cp, max);
507 else
508 data[0] = '\0';
509 return;
510 }
511
512 while (!found && (got || read_line(fp, line, LINE_MAX))) {
513 got = 0;
514
515 if (strncmp(key, line, strlen(key)) != 0 ||
516 line[strlen(key)] != ':')
517 continue;
518
519 /* Now we found our specified key */
520 while (read_line(fp, line, LINE_MAX)) {
521 /* Leaving key field */
522 if (line[0] != '\0' && strchr(line + 1, ':') != NULL) {
523 got = 1;
524 break;
525 }
526 p2 = line;
527 if ((p = strsep(&p2, "=")) == NULL || p2 == NULL)
528 continue;
529 trim_whitespace(p);
530
531 if (!strncmp(p, option, strlen(option))) {
532 trim_whitespace(p2);
533 strcpy(result, p2);
534 found = 1;
535 break;
536 }
537 }
538 }
539 fclose(fp);
540
541 /*
542 * If we got no result and were looking for a default
543 * value, try hard coded defaults.
544 */
545
546 if (strlen(result) == 0 && strcmp(key, "default") == 0 &&
547 (cp = pw_default(option)) != NULL)
548 strlcpy(data, cp, max);
549 else
550 strlcpy(data, result, max);
551 }
552