passwd.c revision 1.29 1 /* $NetBSD: passwd.c,v 1.29 2001/01/03 15:41:19 lukem 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.29 2001/01/03 15:41:19 lukem 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(void)
141 {
142 int pstat;
143 pid_t pid;
144
145 pid = vfork();
146 if (pid == -1)
147 return (-1);
148
149 if (pid == 0) {
150 execl(_PATH_PWD_MKDB, "pwd_mkdb", "-d", pw_prefix,
151 "-p", pw_filename(_PATH_MASTERPASSWD_LOCK), NULL);
152 _exit(1);
153 }
154 pid = waitpid(pid, &pstat, 0);
155 if (pid == -1 || !WIFEXITED(pstat) || WEXITSTATUS(pstat) != 0)
156 return(-1);
157 return(0);
158 }
159
160 int
161 pw_abort(void)
162 {
163 const char *filename;
164
165 filename = pw_filename(_PATH_MASTERPASSWD_LOCK);
166 return((filename == NULL) ? -1 : unlink(filename));
167 }
168
169 /* Everything below this point is intended for the convenience of programs
170 * which allow a user to interactively edit the passwd file. Errors in the
171 * routines below will cause the process to abort. */
172
173 static pid_t editpid = -1;
174
175 static void
176 pw_cont(int sig)
177 {
178
179 if (editpid != -1)
180 kill(editpid, sig);
181 }
182
183 void
184 pw_init(void)
185 {
186 struct rlimit rlim;
187
188 /* Unlimited resource limits. */
189 rlim.rlim_cur = rlim.rlim_max = RLIM_INFINITY;
190 (void)setrlimit(RLIMIT_CPU, &rlim);
191 (void)setrlimit(RLIMIT_FSIZE, &rlim);
192 (void)setrlimit(RLIMIT_STACK, &rlim);
193 (void)setrlimit(RLIMIT_DATA, &rlim);
194 (void)setrlimit(RLIMIT_RSS, &rlim);
195
196 /* Don't drop core (not really necessary, but GP's). */
197 rlim.rlim_cur = rlim.rlim_max = 0;
198 (void)setrlimit(RLIMIT_CORE, &rlim);
199
200 /* Turn off signals. */
201 (void)signal(SIGALRM, SIG_IGN);
202 (void)signal(SIGHUP, SIG_IGN);
203 (void)signal(SIGINT, SIG_IGN);
204 (void)signal(SIGPIPE, SIG_IGN);
205 (void)signal(SIGQUIT, SIG_IGN);
206 (void)signal(SIGTERM, SIG_IGN);
207 (void)signal(SIGCONT, pw_cont);
208 }
209
210 void
211 pw_edit(int notsetuid, const char *filename)
212 {
213 int pstat;
214 char *p, *editor;
215 char *argp[] = { "sh", "-c", NULL, NULL };
216
217 #ifdef __GNUC__
218 (void) &editor;
219 #endif
220
221 if (filename == NULL)
222 filename = _PATH_MASTERPASSWD_LOCK;
223
224 filename = pw_filename(filename);
225 if (filename == NULL)
226 return;
227
228 if ((editor = getenv("EDITOR")) == NULL)
229 editor = _PATH_VI;
230
231 p = malloc(strlen(editor) + 1 + strlen(filename) + 1);
232 if (p == NULL)
233 return;
234
235 sprintf(p, "%s %s", editor, filename);
236 argp[2] = p;
237
238 switch(editpid = vfork()) {
239 case -1:
240 free(p);
241 return;
242 case 0:
243 if (notsetuid) {
244 setgid(getgid());
245 setuid(getuid());
246 }
247 execvp(_PATH_BSHELL, argp);
248 _exit(1);
249 }
250
251 free(p);
252
253 for (;;) {
254 editpid = waitpid(editpid, (int *)&pstat, WUNTRACED);
255 if (editpid == -1)
256 pw_error(editor, 1, 1);
257 else if (WIFSTOPPED(pstat))
258 raise(WSTOPSIG(pstat));
259 else if (WIFEXITED(pstat) && WEXITSTATUS(pstat) == 0)
260 break;
261 else
262 pw_error(editor, 1, 1);
263 }
264 editpid = -1;
265 }
266
267 void
268 pw_prompt(void)
269 {
270 int c;
271
272 (void)printf("re-edit the password file? [y]: ");
273 (void)fflush(stdout);
274 c = getchar();
275 if (c != EOF && c != '\n')
276 while (getchar() != '\n');
277 if (c == 'n')
278 pw_error(NULL, 0, 0);
279 }
280
281 /* for use in pw_copy(). Compare a pw entry to a pw struct. */
282 static int
283 pw_equal(char *buf, struct passwd *pw)
284 {
285 struct passwd buf_pw;
286 int len;
287
288 _DIAGASSERT(buf != NULL);
289 _DIAGASSERT(pw != NULL);
290
291 len = strlen (buf);
292 if (buf[len-1] == '\n')
293 buf[len-1] = '\0';
294 if (!pw_scan(buf, &buf_pw, NULL))
295 return 0;
296 return !strcmp(pw->pw_name, buf_pw.pw_name)
297 && pw->pw_uid == buf_pw.pw_uid
298 && pw->pw_gid == buf_pw.pw_gid
299 && !strcmp(pw->pw_class, buf_pw.pw_class)
300 && (long)pw->pw_change == (long)buf_pw.pw_change
301 && (long)pw->pw_expire == (long)buf_pw.pw_expire
302 && !strcmp(pw->pw_gecos, buf_pw.pw_gecos)
303 && !strcmp(pw->pw_dir, buf_pw.pw_dir)
304 && !strcmp(pw->pw_shell, buf_pw.pw_shell);
305 }
306
307 void
308 pw_copy(int ffd, int tfd, struct passwd *pw, struct passwd *old_pw)
309 {
310 const char *filename;
311 char mpwd[MAXPATHLEN], mpwdl[MAXPATHLEN], *p, buf[8192];
312 FILE *from, *to;
313 int done;
314
315 _DIAGASSERT(pw != NULL);
316 /* old_pw may be NULL */
317
318 if ((filename = pw_filename(_PATH_MASTERPASSWD)) == NULL)
319 pw_error(pw_prefix, 1,1);
320 (void)strcpy(mpwd, filename);
321 if ((filename = pw_filename(_PATH_MASTERPASSWD_LOCK)) == NULL)
322 pw_error(pw_prefix, 1,1);
323 (void)strcpy(mpwdl, filename);
324
325 if (!(from = fdopen(ffd, "r")))
326 pw_error(mpwd, 1, 1);
327 if (!(to = fdopen(tfd, "w")))
328 pw_error(mpwdl, 1, 1);
329
330 for (done = 0; fgets(buf, sizeof(buf), from);) {
331 if (!strchr(buf, '\n')) {
332 warnx("%s: line too long", mpwd);
333 pw_error(NULL, 0, 1);
334 }
335 if (done) {
336 (void)fprintf(to, "%s", buf);
337 if (ferror(to))
338 goto err;
339 continue;
340 }
341 if (!(p = strchr(buf, ':'))) {
342 warnx("%s: corrupted entry", mpwd);
343 pw_error(NULL, 0, 1);
344 }
345 *p = '\0';
346 if (strcmp(buf, pw->pw_name)) {
347 *p = ':';
348 (void)fprintf(to, "%s", buf);
349 if (ferror(to))
350 goto err;
351 continue;
352 }
353 *p = ':';
354 if (old_pw && !pw_equal(buf, old_pw)) {
355 warnx("%s: entry inconsistent", mpwd);
356 pw_error(NULL, 0, 1);
357 }
358 (void)fprintf(to, "%s:%s:%d:%d:%s:%ld:%ld:%s:%s:%s\n",
359 pw->pw_name, pw->pw_passwd, pw->pw_uid, pw->pw_gid,
360 pw->pw_class, (long)pw->pw_change, (long)pw->pw_expire,
361 pw->pw_gecos, pw->pw_dir, pw->pw_shell);
362 done = 1;
363 if (ferror(to))
364 goto err;
365 }
366 /* Only append a new entry if real uid is root! */
367 if (!done) {
368 if (getuid() == 0)
369 (void)fprintf(to, "%s:%s:%d:%d:%s:%ld:%ld:%s:%s:%s\n",
370 pw->pw_name, pw->pw_passwd, pw->pw_uid, pw->pw_gid,
371 pw->pw_class, (long)pw->pw_change,
372 (long)pw->pw_expire, pw->pw_gecos, pw->pw_dir,
373 pw->pw_shell);
374 else
375 warnx("%s: changes not made, no such entry", mpwd);
376 }
377
378 if (ferror(to))
379 err: pw_error(NULL, 1, 1);
380 (void)fclose(to);
381 }
382
383 void
384 pw_error(const char *name, int err, int eval)
385 {
386
387 if (err) {
388 if (name)
389 warn("%s", name);
390 else
391 warn(NULL);
392 }
393
394 warnx("%s%s: unchanged", pw_prefix, _PATH_MASTERPASSWD);
395 pw_abort();
396 exit(eval);
397 }
398
399 /* Removes head and/or tail spaces. */
400 static void
401 trim_whitespace(char *line)
402 {
403 char *p;
404
405 _DIAGASSERT(line != NULL);
406
407 /* Remove leading spaces */
408 p = line;
409 while (isspace((unsigned char) *p))
410 p++;
411 memmove(line, p, strlen(p) + 1);
412
413 /* Remove trailing spaces */
414 p = line + strlen(line) - 1;
415 while (isspace((unsigned char) *p))
416 p--;
417 *(p + 1) = '\0';
418 }
419
420
421 /* Get one line, remove spaces from front and tail */
422 static int
423 read_line(FILE *fp, char *line, int max)
424 {
425 char *p;
426
427 _DIAGASSERT(fp != NULL);
428 _DIAGASSERT(line != NULL);
429
430 /* Read one line of config */
431 if (fgets(line, max, fp) == NULL)
432 return (0);
433
434 if ((p = strchr(line, '\n')) == NULL) {
435 warnx("line too long");
436 return (0);
437 }
438 *p = '\0';
439
440 /* Remove comments */
441 if ((p = strchr(line, '#')) != NULL)
442 *p = '\0';
443
444 trim_whitespace(line);
445 return (1);
446 }
447
448 static const char *
449 pw_default(const char *option)
450 {
451 static const char *options[][2] = {
452 { "localcipher", "old" },
453 { "ypcipher", "old" },
454 };
455 int i;
456
457 _DIAGASSERT(option != NULL);
458 for (i = 0; i < sizeof(options) / sizeof(options[0]); i++)
459 if (strcmp(options[i][0], option) == 0)
460 return (options[i][1]);
461
462 return (NULL);
463 }
464
465 /*
466 * Retrieve password information from the /etc/passwd.conf file, at the
467 * moment this is only for choosing the cipher to use. It could easily be
468 * used for other authentication methods as well.
469 */
470 void
471 pw_getconf(char *data, size_t max, const char *key, const char *option)
472 {
473 FILE *fp;
474 char line[LINE_MAX], *p, *p2;
475 static char result[LINE_MAX];
476 int got, found;
477 const char *cp;
478
479 _DIAGASSERT(data != NULL);
480 _DIAGASSERT(key != NULL);
481 _DIAGASSERT(option != NULL);
482
483 got = 0;
484 found = 0;
485 result[0] = '\0';
486
487 if ((fp = fopen(_PATH_PASSWDCONF, "r")) == NULL) {
488 if ((cp = pw_default(option)) != NULL)
489 strlcpy(data, cp, max);
490 else
491 data[0] = '\0';
492 return;
493 }
494
495 while (!found && (got || read_line(fp, line, LINE_MAX))) {
496 got = 0;
497
498 if (strncmp(key, line, strlen(key)) != 0 ||
499 line[strlen(key)] != ':')
500 continue;
501
502 /* Now we found our specified key */
503 while (read_line(fp, line, LINE_MAX)) {
504 /* Leaving key field */
505 if (line[0] != '\0' && strchr(line + 1, ':') != NULL) {
506 got = 1;
507 break;
508 }
509 p2 = line;
510 if ((p = strsep(&p2, "=")) == NULL || p2 == NULL)
511 continue;
512 trim_whitespace(p);
513
514 if (!strncmp(p, option, strlen(option))) {
515 trim_whitespace(p2);
516 strcpy(result, p2);
517 found = 1;
518 break;
519 }
520 }
521 }
522 fclose(fp);
523
524 /*
525 * If we got no result and were looking for a default
526 * value, try hard coded defaults.
527 */
528
529 if (strlen(result) == 0 && strcmp(key, "default") == 0 &&
530 (cp = pw_default(option)) != NULL)
531 strlcpy(data, cp, max);
532 else
533 strlcpy(data, result, max);
534 }
535