1 /*-
2 * Copyright (c) 1990, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 *
5 * %sccs.include.redist.c%
6 */
7
8 #ifndef lint
9 static char sccsid[] = "@(#)pw_scan.c 8.3 (Berkeley) 04/02/94";
10 #endif /* not lint */
11
12 /*
13 * This module is used to "verify" password entries by chpass(1) and
14 * pwd_mkdb(8).
15 */
16
17 #include <sys/param.h>
18
19 #include <err.h>
20 #include <fcntl.h>
21 #include <pwd.h>
22 #include <errno.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27
28 #include "pw_scan.h"
29
30 int
pw_scan(bp,pw)31 pw_scan(bp, pw)
32 char *bp;
33 struct passwd *pw;
34 {
35 long id;
36 int root;
37 char *p, *sh;
38
39 if (!(pw->pw_name = strsep(&bp, ":"))) /* login */
40 goto fmt;
41 root = !strcmp(pw->pw_name, "root");
42
43 if (!(pw->pw_passwd = strsep(&bp, ":"))) /* passwd */
44 goto fmt;
45
46 if (!(p = strsep(&bp, ":"))) /* uid */
47 goto fmt;
48 id = atol(p);
49 if (root && id) {
50 warnx("root uid should be 0");
51 return (0);
52 }
53 if (id > USHRT_MAX) {
54 warnx("%s > max uid value (%d)", p, USHRT_MAX);
55 return (0);
56 }
57 pw->pw_uid = id;
58
59 if (!(p = strsep(&bp, ":"))) /* gid */
60 goto fmt;
61 id = atol(p);
62 if (id > USHRT_MAX) {
63 warnx("%s > max gid value (%d)", p, USHRT_MAX);
64 return (0);
65 }
66 pw->pw_gid = id;
67
68 pw->pw_class = strsep(&bp, ":"); /* class */
69 if (!(p = strsep(&bp, ":"))) /* change */
70 goto fmt;
71 pw->pw_change = atol(p);
72 if (!(p = strsep(&bp, ":"))) /* expire */
73 goto fmt;
74 pw->pw_expire = atol(p);
75 pw->pw_gecos = strsep(&bp, ":"); /* gecos */
76 pw->pw_dir = strsep(&bp, ":"); /* directory */
77 if (!(pw->pw_shell = strsep(&bp, ":"))) /* shell */
78 goto fmt;
79
80 p = pw->pw_shell;
81 if (root && *p) /* empty == /bin/sh */
82 for (setusershell();;) {
83 if (!(sh = getusershell())) {
84 warnx("warning, unknown root shell");
85 break;
86 }
87 if (!strcmp(p, sh))
88 break;
89 }
90
91 if (p = strsep(&bp, ":")) { /* too many */
92 fmt: warnx("corrupted entry");
93 return (0);
94 }
95 return (1);
96 }
97