pw_scan.c revision 1.9 1 /* $NetBSD: pw_scan.c,v 1.9 1999/09/16 11:45:02 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: pw_scan.c,v 1.9 1999/09/16 11:45:02 lukem Exp $");
39 #endif /* LIBC_SCCS and not lint */
40
41 #if defined(_LIBC)
42 #include "namespace.h"
43 #endif
44 #include <sys/types.h>
45
46 #include <assert.h>
47 #include <err.h>
48 #include <limits.h>
49 #include <pwd.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <unistd.h>
54
55 #ifdef _LIBC
56 #include "pw_private.h"
57 #endif
58
59 int
60 #ifdef _LIBC
61 __pw_scan(bp, pw, flags)
62 #else
63 pw_scan(bp, pw, flags)
64 #endif
65 char *bp;
66 struct passwd *pw;
67 int *flags;
68 {
69 unsigned long id;
70 int root, inflags;
71 char *ep;
72 const char *p, *sh;
73
74 _DIAGASSERT(bp != NULL);
75 _DIAGASSERT(pw != NULL);
76 #ifdef _DIAGNOSTIC
77 if (bp == NULL || pw == NULL)
78 return (0);
79 #endif
80
81 inflags = 0;
82 if (flags != (int *)NULL) {
83 inflags = *flags;
84 *flags = 0;
85 }
86
87 if (!(pw->pw_name = strsep(&bp, ":"))) /* login */
88 goto fmt;
89 root = !strcmp(pw->pw_name, "root");
90
91 if (!(pw->pw_passwd = strsep(&bp, ":"))) /* passwd */
92 goto fmt;
93
94 if (!(p = strsep(&bp, ":"))) /* uid */
95 goto fmt;
96 id = strtoul(p, &ep, 10);
97 if (root && id) {
98 if (!(inflags & _PASSWORD_NOWARN))
99 warnx("root uid should be 0");
100 return (0);
101 }
102 if (id > UID_MAX || *ep != '\0') {
103 if (!(inflags & _PASSWORD_NOWARN))
104 warnx("invalid uid '%s'", p);
105 return (0);
106 }
107 pw->pw_uid = (uid_t)id;
108 if ((*p == '\0') && (flags != (int *)NULL))
109 *flags |= _PASSWORD_NOUID;
110
111 if (!(p = strsep(&bp, ":"))) /* gid */
112 goto fmt;
113 id = strtoul(p, &ep, 10);
114 if (id > GID_MAX || *ep != '\0') {
115 if (!(inflags & _PASSWORD_NOWARN))
116 warnx("invalid gid '%s'", p);
117 return (0);
118 }
119 pw->pw_gid = (gid_t)id;
120 if ((*p == '\0') && (flags != (int *)NULL))
121 *flags |= _PASSWORD_NOGID;
122
123 if (inflags & _PASSWORD_OLDFMT) {
124 pw->pw_class = "";
125 pw->pw_change = 0;
126 pw->pw_expire = 0;
127 *flags |= (_PASSWORD_NOCHG | _PASSWORD_NOEXP);
128 } else {
129 pw->pw_class = strsep(&bp, ":"); /* class */
130 if (!(p = strsep(&bp, ":"))) /* change */
131 goto fmt;
132 pw->pw_change = atol(p);
133 if ((*p == '\0') && (flags != (int *)NULL))
134 *flags |= _PASSWORD_NOCHG;
135 if (!(p = strsep(&bp, ":"))) /* expire */
136 goto fmt;
137 pw->pw_expire = atol(p);
138 if ((*p == '\0') && (flags != (int *)NULL))
139 *flags |= _PASSWORD_NOEXP;
140 }
141 pw->pw_gecos = strsep(&bp, ":"); /* gecos */
142 pw->pw_dir = strsep(&bp, ":"); /* directory */
143 if (!(pw->pw_shell = strsep(&bp, ":"))) /* shell */
144 goto fmt;
145
146 p = pw->pw_shell;
147 if (root && *p) /* empty == /bin/sh */
148 for (setusershell();;) {
149 if (!(sh = getusershell())) {
150 if (!(inflags & _PASSWORD_NOWARN))
151 warnx("warning, unknown root shell");
152 break;
153 }
154 if (!strcmp(p, sh))
155 break;
156 }
157
158 if ((p = strsep(&bp, ":")) != NULL) { /* too many */
159 fmt:
160 if (!(inflags & _PASSWORD_NOWARN))
161 warnx("corrupted entry");
162 return (0);
163 }
164
165 return (1);
166 }
167