1 /* $NetBSD: passwd.c,v 1.52 2012/06/25 22:32:47 abs 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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 #if defined(LIBC_SCCS) && !defined(lint)
34 __RCSID("$NetBSD: passwd.c,v 1.52 2012/06/25 22:32:47 abs Exp $");
35 #endif /* LIBC_SCCS and not lint */
36
37 #include <sys/types.h>
38 #include <sys/param.h>
39 #include <sys/stat.h>
40 #include <sys/time.h>
41 #include <sys/resource.h>
42 #include <sys/wait.h>
43
44 #include <assert.h>
45 #include <ctype.h>
46 #include <err.h>
47 #include <errno.h>
48 #include <fcntl.h>
49 #include <limits.h>
50 #include <paths.h>
51 #include <pwd.h>
52 #include <grp.h>
53 #include <signal.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <unistd.h>
58 #include <util.h>
59
60 static const char *pw_filename(const char *filename);
61 static void pw_cont(int sig);
62 static const char * pw_equal(char *buf, struct passwd *old_pw);
63 static const char *pw_default(const char *option);
64 static int read_line(FILE *fp, char *line, int max);
65 static void trim_whitespace(char *line);
66
67 static char pw_prefix[MAXPATHLEN];
68
69 const char *
pw_getprefix(void)70 pw_getprefix(void)
71 {
72
73 return(pw_prefix);
74 }
75
76 int
pw_setprefix(const char * new_prefix)77 pw_setprefix(const char *new_prefix)
78 {
79 size_t length;
80
81 _DIAGASSERT(new_prefix != NULL);
82
83 length = strlen(new_prefix);
84 if (length < sizeof(pw_prefix)) {
85 (void)strcpy(pw_prefix, new_prefix);
86 while (length > 0 && pw_prefix[length - 1] == '/')
87 pw_prefix[--length] = '\0';
88 return(0);
89 }
90 errno = ENAMETOOLONG;
91 return(-1);
92 }
93
94 static const char *
pw_filename(const char * filename)95 pw_filename(const char *filename)
96 {
97 static char newfilename[MAXPATHLEN];
98
99 _DIAGASSERT(filename != NULL);
100
101 if (pw_prefix[0] == '\0')
102 return filename;
103
104 if (strlen(pw_prefix) + strlen(filename) < sizeof(newfilename))
105 return strcat(strcpy(newfilename, pw_prefix), filename);
106
107 errno = ENAMETOOLONG;
108 return(NULL);
109 }
110
111 int
pw_lock(int retries)112 pw_lock(int retries)
113 {
114 const char *filename;
115 int i, fd;
116 mode_t old_mode;
117 int oerrno;
118
119 /* Acquire the lock file. */
120 filename = pw_filename(_PATH_MASTERPASSWD_LOCK);
121 if (filename == NULL)
122 return(-1);
123 old_mode = umask(0);
124 fd = open(filename, O_WRONLY|O_CREAT|O_EXCL, 0600);
125 for (i = 0; i < retries && fd < 0 && errno == EEXIST; i++) {
126 sleep(1);
127 fd = open(filename, O_WRONLY|O_CREAT|O_EXCL,
128 0600);
129 }
130 oerrno = errno;
131 (void)umask(old_mode);
132 errno = oerrno;
133 return(fd);
134 }
135
136 int
pw_mkdb(const char * username,int secureonly)137 pw_mkdb(const char *username, int secureonly)
138 {
139 const char *args[9];
140 int pstat, i;
141 pid_t pid;
142
143 pid = vfork();
144 if (pid == -1)
145 return -1;
146
147 if (pid == 0) {
148 args[0] = "pwd_mkdb";
149 args[1] = "-d";
150 args[2] = pw_prefix;
151 args[3] = "-pl";
152 i = 4;
153
154 if (secureonly)
155 args[i++] = "-s";
156 if (username != NULL) {
157 args[i++] = "-u";
158 args[i++] = username;
159 }
160
161 args[i++] = pw_filename(_PATH_MASTERPASSWD_LOCK);
162 args[i] = NULL;
163 execv(_PATH_PWD_MKDB, (char * const *)__UNCONST(args));
164 _exit(1);
165 }
166 pid = waitpid(pid, &pstat, 0);
167 if (pid == -1) {
168 warn("error waiting for pid %lu", (unsigned long)pid);
169 return -1;
170 }
171 if (WIFEXITED(pstat)) {
172 if (WEXITSTATUS(pstat) != 0) {
173 warnx("pwd_mkdb exited with status %d",
174 WEXITSTATUS(pstat));
175 return -1;
176 }
177 } else if (WIFSIGNALED(pstat)) {
178 warnx("pwd_mkdb exited with signal %d", WTERMSIG(pstat));
179 return -1;
180 }
181 return 0;
182 }
183
184 int
pw_abort(void)185 pw_abort(void)
186 {
187 const char *filename;
188
189 filename = pw_filename(_PATH_MASTERPASSWD_LOCK);
190 return((filename == NULL) ? -1 : unlink(filename));
191 }
192
193 /* Everything below this point is intended for the convenience of programs
194 * which allow a user to interactively edit the passwd file. Errors in the
195 * routines below will cause the process to abort. */
196
197 static pid_t editpid = -1;
198
199 static void
pw_cont(int sig)200 pw_cont(int sig)
201 {
202
203 if (editpid != -1)
204 kill(editpid, sig);
205 }
206
207 void
pw_init(void)208 pw_init(void)
209 {
210 struct rlimit rlim;
211
212 /* Unlimited resource limits. */
213 rlim.rlim_cur = rlim.rlim_max = RLIM_INFINITY;
214 (void)setrlimit(RLIMIT_CPU, &rlim);
215 (void)setrlimit(RLIMIT_FSIZE, &rlim);
216 (void)setrlimit(RLIMIT_STACK, &rlim);
217 (void)setrlimit(RLIMIT_DATA, &rlim);
218 (void)setrlimit(RLIMIT_RSS, &rlim);
219
220 /* Don't drop core (not really necessary, but GP's). */
221 rlim.rlim_cur = rlim.rlim_max = 0;
222 (void)setrlimit(RLIMIT_CORE, &rlim);
223
224 /* Turn off signals. */
225 (void)signal(SIGALRM, SIG_IGN);
226 (void)signal(SIGHUP, SIG_IGN);
227 (void)signal(SIGINT, SIG_IGN);
228 (void)signal(SIGPIPE, SIG_IGN);
229 (void)signal(SIGQUIT, SIG_IGN);
230 (void)signal(SIGTERM, SIG_IGN);
231 (void)signal(SIGCONT, pw_cont);
232 }
233
234 void
pw_edit(int notsetuid,const char * filename)235 pw_edit(int notsetuid, const char *filename)
236 {
237 int pstat;
238 char *p;
239 const char * volatile editor;
240 const char *argp[] = { "sh", "-c", NULL, NULL };
241
242 if (filename == NULL)
243 filename = _PATH_MASTERPASSWD_LOCK;
244
245 filename = pw_filename(filename);
246 if (filename == NULL)
247 return;
248
249 if ((editor = getenv("EDITOR")) == NULL)
250 editor = _PATH_VI;
251
252 p = malloc(strlen(editor) + 1 + strlen(filename) + 1);
253 if (p == NULL)
254 return;
255
256 sprintf(p, "%s %s", editor, filename);
257 argp[2] = p;
258
259 switch(editpid = vfork()) {
260 case -1:
261 free(p);
262 return;
263 case 0:
264 if (notsetuid) {
265 setgid(getgid());
266 setuid(getuid());
267 }
268 execvp(_PATH_BSHELL, (char *const *)__UNCONST(argp));
269 _exit(1);
270 }
271
272 free(p);
273
274 for (;;) {
275 editpid = waitpid(editpid, (int *)&pstat, WUNTRACED);
276 if (editpid == -1)
277 pw_error(editor, 1, 1);
278 else if (WIFSTOPPED(pstat))
279 raise(WSTOPSIG(pstat));
280 else if (WIFEXITED(pstat) && WEXITSTATUS(pstat) == 0)
281 break;
282 else
283 pw_error(editor, 1, 1);
284 }
285 editpid = -1;
286 }
287
288 void
pw_prompt(void)289 pw_prompt(void)
290 {
291 int c;
292
293 (void)printf("re-edit the password file? [y]: ");
294 (void)fflush(stdout);
295 c = getchar();
296 if (c != EOF && c != '\n')
297 while (getchar() != '\n');
298 if (c == 'n')
299 pw_error(NULL, 0, 0);
300 }
301
302 /* for use in pw_copy(). Compare a pw entry to a pw struct. */
303 /* returns a character string labelling the miscompared field or 0 */
304 static const char *
pw_equal(char * buf,struct passwd * pw)305 pw_equal(char *buf, struct passwd *pw)
306 {
307 struct passwd buf_pw;
308 size_t len;
309
310 _DIAGASSERT(buf != NULL);
311 _DIAGASSERT(pw != NULL);
312
313 len = strlen (buf);
314 if (buf[len-1] == '\n')
315 buf[len-1] = '\0';
316 if (!pw_scan(buf, &buf_pw, NULL))
317 return "corrupt line";
318 if (strcmp(pw->pw_name, buf_pw.pw_name) != 0)
319 return "name";
320 if (pw->pw_uid != buf_pw.pw_uid)
321 return "uid";
322 if (pw->pw_gid != buf_pw.pw_gid)
323 return "gid";
324 if (strcmp( pw->pw_class, buf_pw.pw_class) != 0)
325 return "class";
326 if (pw->pw_change != buf_pw.pw_change)
327 return "change";
328 if (pw->pw_expire != buf_pw.pw_expire)
329 return "expire";
330 if (strcmp( pw->pw_gecos, buf_pw.pw_gecos) != 0)
331 return "gecos";
332 if (strcmp( pw->pw_dir, buf_pw.pw_dir) != 0)
333 return "dir";
334 if (strcmp( pw->pw_shell, buf_pw.pw_shell) != 0)
335 return "shell";
336 return (char *)0;
337 }
338
339 void
pw_copy(int ffd,int tfd,struct passwd * pw,struct passwd * old_pw)340 pw_copy(int ffd, int tfd, struct passwd *pw, struct passwd *old_pw)
341 {
342 char errbuf[200];
343 int rv;
344
345 rv = pw_copyx(ffd, tfd, pw, old_pw, errbuf, sizeof(errbuf));
346 if (rv == 0) {
347 warnx("%s", errbuf);
348 pw_error(NULL, 0, 1);
349 }
350 }
351
352 static void
pw_print(FILE * to,const struct passwd * pw)353 pw_print(FILE *to, const struct passwd *pw)
354 {
355 (void)fprintf(to, "%s:%s:%d:%d:%s:%lld:%lld:%s:%s:%s\n",
356 pw->pw_name, pw->pw_passwd, pw->pw_uid, pw->pw_gid,
357 pw->pw_class, (long long)pw->pw_change,
358 (long long)pw->pw_expire,
359 pw->pw_gecos, pw->pw_dir, pw->pw_shell);
360 }
361
362 int
pw_copyx(int ffd,int tfd,struct passwd * pw,struct passwd * old_pw,char * errbuf,size_t errbufsz)363 pw_copyx(int ffd, int tfd, struct passwd *pw, struct passwd *old_pw,
364 char *errbuf, size_t errbufsz)
365 {
366 const char *filename;
367 char mpwd[MAXPATHLEN], mpwdl[MAXPATHLEN], *p, buf[8192];
368 FILE *from, *to;
369 int done;
370
371 _DIAGASSERT(pw != NULL);
372 _DIAGASSERT(errbuf != NULL);
373 /* old_pw may be NULL */
374
375 if ((filename = pw_filename(_PATH_MASTERPASSWD)) == NULL) {
376 snprintf(errbuf, errbufsz, "%s: %s", pw_prefix,
377 strerror(errno));
378 return (0);
379 }
380 (void)strcpy(mpwd, filename);
381 if ((filename = pw_filename(_PATH_MASTERPASSWD_LOCK)) == NULL) {
382 snprintf(errbuf, errbufsz, "%s: %s", pw_prefix,
383 strerror(errno));
384 return (0);
385 }
386 (void)strcpy(mpwdl, filename);
387
388 if (!(from = fdopen(ffd, "r"))) {
389 snprintf(errbuf, errbufsz, "%s: %s", mpwd, strerror(errno));
390 return (0);
391 }
392 if (!(to = fdopen(tfd, "w"))) {
393 snprintf(errbuf, errbufsz, "%s: %s", mpwdl, strerror(errno));
394 (void)fclose(from);
395 return (0);
396 }
397
398 for (done = 0; fgets(buf, (int)sizeof(buf), from);) {
399 const char *neq;
400 if (!strchr(buf, '\n')) {
401 snprintf(errbuf, errbufsz, "%s: line too long", mpwd);
402 (void)fclose(from);
403 (void)fclose(to);
404 return (0);
405 }
406 if (done) {
407 (void)fprintf(to, "%s", buf);
408 if (ferror(to)) {
409 snprintf(errbuf, errbufsz, "%s",
410 strerror(errno));
411 (void)fclose(from);
412 (void)fclose(to);
413 return (0);
414 }
415 continue;
416 }
417 if (!(p = strchr(buf, ':'))) {
418 snprintf(errbuf, errbufsz, "%s: corrupted entry", mpwd);
419 (void)fclose(from);
420 (void)fclose(to);
421 return (0);
422 }
423 *p = '\0';
424 if (strcmp(buf, pw->pw_name)) {
425 *p = ':';
426 (void)fprintf(to, "%s", buf);
427 if (ferror(to)) {
428 snprintf(errbuf, errbufsz, "%s",
429 strerror(errno));
430 (void)fclose(from);
431 (void)fclose(to);
432 return (0);
433 }
434 continue;
435 }
436 *p = ':';
437 if (old_pw && (neq = pw_equal(buf, old_pw)) != NULL) {
438 if (strcmp(neq, "corrupt line") == 0)
439 (void)snprintf(errbuf, errbufsz,
440 "%s: entry %s corrupted", mpwd,
441 pw->pw_name);
442 else
443 (void)snprintf(errbuf, errbufsz,
444 "%s: entry %s inconsistent %s",
445 mpwd, pw->pw_name, neq);
446 (void)fclose(from);
447 (void)fclose(to);
448 return (0);
449 }
450 pw_print(to, pw);
451 done = 1;
452 if (ferror(to)) {
453 snprintf(errbuf, errbufsz, "%s", strerror(errno));
454 (void)fclose(from);
455 (void)fclose(to);
456 return (0);
457 }
458 }
459 /* Only append a new entry if real uid is root! */
460 if (!done) {
461 if (getuid() == 0) {
462 pw_print(to, pw);
463 done = 1;
464 } else {
465 snprintf(errbuf, errbufsz,
466 "%s: changes not made, no such entry", mpwd);
467 }
468 }
469
470 if (ferror(to)) {
471 snprintf(errbuf, errbufsz, "%s", strerror(errno));
472 (void)fclose(from);
473 (void)fclose(to);
474 return (0);
475 }
476 (void)fclose(from);
477 (void)fclose(to);
478
479 return (done);
480 }
481
482 void
pw_error(const char * name,int error,int eval)483 pw_error(const char *name, int error, int eval)
484 {
485
486 if (error) {
487 if (name)
488 warn("%s", name);
489 else
490 warn(NULL);
491 }
492
493 warnx("%s%s: unchanged", pw_prefix, _PATH_MASTERPASSWD);
494 pw_abort();
495 exit(eval);
496 }
497
498 /* Removes head and/or tail spaces. */
499 static void
trim_whitespace(char * line)500 trim_whitespace(char *line)
501 {
502 char *p;
503
504 _DIAGASSERT(line != NULL);
505
506 /* Remove leading spaces */
507 p = line;
508 while (isspace((unsigned char) *p))
509 p++;
510 memmove(line, p, strlen(p) + 1);
511
512 /* Remove trailing spaces */
513 p = line + strlen(line) - 1;
514 while (isspace((unsigned char) *p))
515 p--;
516 *(p + 1) = '\0';
517 }
518
519
520 /* Get one line, remove spaces from front and tail */
521 static int
read_line(FILE * fp,char * line,int max)522 read_line(FILE *fp, char *line, int max)
523 {
524 char *p;
525
526 _DIAGASSERT(fp != NULL);
527 _DIAGASSERT(line != NULL);
528
529 /* Read one line of config */
530 if (fgets(line, max, fp) == NULL)
531 return (0);
532
533 if ((p = strchr(line, '\n')) == NULL) {
534 warnx("line too long");
535 return (0);
536 }
537 *p = '\0';
538
539 /* Remove comments */
540 if ((p = strchr(line, '#')) != NULL)
541 *p = '\0';
542
543 trim_whitespace(line);
544 return (1);
545 }
546
547 static const char *
pw_default(const char * option)548 pw_default(const char *option)
549 {
550 static const char *options[][2] = {
551 { "localcipher", "old" },
552 { "ypcipher", "old" },
553 };
554 size_t i;
555
556 _DIAGASSERT(option != NULL);
557 for (i = 0; i < sizeof(options) / sizeof(options[0]); i++)
558 if (strcmp(options[i][0], option) == 0)
559 return (options[i][1]);
560
561 return (NULL);
562 }
563
564 /*
565 * Retrieve password information from the /etc/passwd.conf file, at the
566 * moment this is only for choosing the cipher to use. It could easily be
567 * used for other authentication methods as well.
568 */
569 void
pw_getconf(char * data,size_t max,const char * key,const char * option)570 pw_getconf(char *data, size_t max, const char *key, const char *option)
571 {
572 FILE *fp;
573 char line[LINE_MAX], *p, *p2;
574 static char result[LINE_MAX];
575 int got, found;
576 const char *cp;
577
578 _DIAGASSERT(data != NULL);
579 _DIAGASSERT(key != NULL);
580 _DIAGASSERT(option != NULL);
581
582 got = 0;
583 found = 0;
584 result[0] = '\0';
585
586 if ((fp = fopen(_PATH_PASSWD_CONF, "r")) == NULL) {
587 if ((cp = pw_default(option)) != NULL)
588 strlcpy(data, cp, max);
589 else
590 data[0] = '\0';
591 return;
592 }
593
594 while (!found && (got || read_line(fp, line, LINE_MAX))) {
595 got = 0;
596
597 if (strncmp(key, line, strlen(key)) != 0 ||
598 line[strlen(key)] != ':')
599 continue;
600
601 /* Now we found our specified key */
602 while (read_line(fp, line, LINE_MAX)) {
603 /* Leaving key field */
604 if (line[0] != '\0' && strchr(line + 1, ':') != NULL) {
605 got = 1;
606 break;
607 }
608 p2 = line;
609 if ((p = strsep(&p2, "=")) == NULL || p2 == NULL)
610 continue;
611 trim_whitespace(p);
612
613 if (!strncmp(p, option, strlen(option))) {
614 trim_whitespace(p2);
615 strcpy(result, p2);
616 found = 1;
617 break;
618 }
619 }
620 }
621 fclose(fp);
622
623 if (!found)
624 errno = ENOENT;
625 if (!got)
626 errno = ENOTDIR;
627
628 /*
629 * If we got no result and were looking for a default
630 * value, try hard coded defaults.
631 */
632
633 if (strlen(result) == 0 && strcmp(key, "default") == 0 &&
634 (cp = pw_default(option)) != NULL)
635 strlcpy(data, cp, max);
636 else
637 strlcpy(data, result, max);
638 }
639
640 void
pw_getpwconf(char * data,size_t max,const struct passwd * pwd,const char * option)641 pw_getpwconf(char *data, size_t max, const struct passwd *pwd,
642 const char *option)
643 {
644 char grpkey[LINE_MAX];
645 struct group grs, *grp;
646 char grbuf[1024];
647
648 pw_getconf(data, max, pwd->pw_name, option);
649
650 /* Try to find an entry for the group */
651 if (*data == '\0') {
652 (void)getgrgid_r(pwd->pw_gid, &grs, grbuf, sizeof(grbuf), &grp);
653 if (grp != NULL) {
654 (void)snprintf(grpkey, sizeof(grpkey), ":%s",
655 grp->gr_name);
656 pw_getconf(data, max, grpkey, option);
657 }
658 if (*data == '\0')
659 pw_getconf(data, max, "default", option);
660 }
661 }
662