xref: /netbsd-src/lib/libc/gen/pw_scan.c (revision b51e7f1581e52830eafeba9ab4e1944cf30be2d0)
1 /*	$NetBSD: pw_scan.c,v 1.16 2005/01/19 22:40:37 christos 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 #if HAVE_NBTOOL_CONFIG_H
33 #include "nbtool_config.h"
34 #include "compat_pwd.h"
35 
36 #else
37 #include <sys/cdefs.h>
38 #if defined(LIBC_SCCS) && !defined(lint)
39 __RCSID("$NetBSD: pw_scan.c,v 1.16 2005/01/19 22:40:37 christos Exp $");
40 #endif /* LIBC_SCCS and not lint */
41 
42 #if defined(_LIBC)
43 #include "namespace.h"
44 #endif
45 #include <sys/types.h>
46 
47 #include <assert.h>
48 #include <err.h>
49 #include <limits.h>
50 #include <pwd.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <unistd.h>
55 #include <errno.h>
56 
57 #ifdef _LIBC
58 #include "pw_private.h"
59 #endif
60 #endif /* ! HAVE_NBTOOL_CONFIG_H */
61 
62 static int
63 gettime(time_t *res, const char *p, int *flags, int dowarn, int flag)
64 {
65 	long l;
66 	char *ep;
67 
68 	if (*p == '\0') {
69 		*flags |= flag;
70 		*res = 0;
71 		return 1;
72 	}
73 	l = strtol(p, &ep, 0);
74 	if (p == ep || *ep != '\0') {
75 		ep = __UNCONST("Invalid number");
76 		goto done;
77 	}
78 	if (errno == ERANGE && (l == LONG_MAX || l == LONG_MIN)) {
79 		ep = strerror(errno);
80 		goto done;
81 	}
82 
83 	*res = (time_t)l;
84 	return 1;
85 done:
86 	if (dowarn) {
87 		warnx("%s value `%s' for %s time", ep, p,
88 		    flag == _PASSWORD_NOEXP ? "expiration" : "change");
89 	}
90 	return 0;
91 
92 }
93 
94 static int
95 getid(unsigned long *res, const char *p, int *flags, int dowarn, int flag)
96 {
97 	long ul;
98 	char *ep;
99 
100 	if (*p == '\0') {
101 		*flags |= flag;
102 		*res = 0;
103 		return 1;
104 	}
105 	ul = strtoul(p, &ep, 0);
106 	if (p == ep || *ep != '\0') {
107 		ep = __UNCONST("Invalid number");
108 		goto done;
109 	}
110 	if (errno == ERANGE && ul == ULONG_MAX) {
111 		ep = strerror(errno);
112 		goto done;
113 	}
114 	if (ul > *res) {
115 		ep = strerror(ERANGE);
116 		goto done;
117 	}
118 
119 	*res = ul;
120 	return 1;
121 done:
122 	if (dowarn)
123 		warnx("%s %s value `%s'", ep,
124 		    flag == _PASSWORD_NOUID ? "uid" : "gid", p);
125 	return 0;
126 
127 }
128 int
129 #ifdef _LIBC
130 __pw_scan(char *bp, struct passwd *pw, int *flags)
131 #else
132 pw_scan( char *bp, struct passwd *pw, int *flags)
133 #endif
134 {
135 	unsigned long id;
136 	int root, inflags;
137 	int dowarn;
138 	const char *p, *sh;
139 
140 	_DIAGASSERT(bp != NULL);
141 	_DIAGASSERT(pw != NULL);
142 
143 	if (flags) {
144 		inflags = *flags;
145 		*flags = 0;
146 	} else {
147 		inflags = 0;
148 		flags = &inflags;
149 	}
150 	dowarn = !(inflags & _PASSWORD_NOWARN);
151 
152 	if (!(pw->pw_name = strsep(&bp, ":")))		/* login */
153 		goto fmt;
154 	if (strlen(pw->pw_name) > (LOGIN_NAME_MAX - 1)) {
155 		if (dowarn)
156 			warnx("username too long, `%s' > %d", pw->pw_name,
157 			    LOGIN_NAME_MAX - 1);
158 		return 0;
159 	}
160 
161 	root = !strcmp(pw->pw_name, "root");
162 
163 	if (!(pw->pw_passwd = strsep(&bp, ":")))	/* passwd */
164 		goto fmt;
165 
166 	if (!(p = strsep(&bp, ":")))			/* uid */
167 		goto fmt;
168 
169 	id = UID_MAX;
170 	if (!getid(&id, p, flags, dowarn, _PASSWORD_NOUID))
171 		return 0;
172 
173 	if (root && id) {
174 		if (dowarn)
175 			warnx("root uid should be 0");
176 		return 0;
177 	}
178 
179 	pw->pw_uid = (uid_t)id;
180 
181 	if (!(p = strsep(&bp, ":")))			/* gid */
182 		goto fmt;
183 
184 	id = GID_MAX;
185 	if (!getid(&id, p, flags, dowarn, _PASSWORD_NOGID))
186 		return 0;
187 
188 	pw->pw_gid = (gid_t)id;
189 
190 	if (inflags & _PASSWORD_OLDFMT) {
191 		pw->pw_class = __UNCONST("");
192 		pw->pw_change = 0;
193 		pw->pw_expire = 0;
194 		*flags |= (_PASSWORD_NOCHG | _PASSWORD_NOEXP);
195 	} else {
196 		pw->pw_class = strsep(&bp, ":");	/* class */
197 		if (!(p = strsep(&bp, ":")))		/* change */
198 			goto fmt;
199 		if (!gettime(&pw->pw_change, p, flags, dowarn, _PASSWORD_NOCHG))
200 			return 0;
201 		if (!(p = strsep(&bp, ":")))		/* expire */
202 			goto fmt;
203 		if (!gettime(&pw->pw_expire, p, flags, dowarn, _PASSWORD_NOEXP))
204 			return 0;
205 	}
206 
207 	pw->pw_gecos = strsep(&bp, ":");		/* gecos */
208 	pw->pw_dir = strsep(&bp, ":");			/* directory */
209 	if (!(pw->pw_shell = strsep(&bp, ":")))		/* shell */
210 		goto fmt;
211 
212 #if ! HAVE_NBTOOL_CONFIG_H
213 	p = pw->pw_shell;
214 	if (root && *p)					/* empty == /bin/sh */
215 		for (setusershell();;) {
216 			if (!(sh = getusershell())) {
217 				if (dowarn)
218 					warnx("warning, unknown root shell");
219 				break;
220 			}
221 			if (!strcmp(p, sh))
222 				break;
223 		}
224 #endif
225 
226 	if ((p = strsep(&bp, ":")) != NULL) {			/* too many */
227 fmt:
228 		if (dowarn)
229 			warnx("corrupted entry");
230 		return 0;
231 	}
232 
233 	return 1;
234 }
235