xref: /netbsd-src/lib/libc/gen/pw_scan.c (revision 404fbe5fb94ca1e054339640cabb2801ce52dd30)
1 /*	$NetBSD: pw_scan.c,v 1.21 2009/01/11 02:46:27 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.21 2009/01/11 02:46:27 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 long l;
66 	char *ep;
67 	const char *vp;
68 
69 	if (*p == '\0') {
70 		*flags |= flag;
71 		*res = 0;
72 		return 1;
73 	}
74 	l = strtoll(p, &ep, 0);
75 	if (p == ep || *ep != '\0') {
76 		vp = "Invalid number";
77 		goto done;
78 	}
79 	if (errno == ERANGE && (l == LLONG_MAX || l == LLONG_MIN)) {
80 		vp = strerror(errno);
81 		goto done;
82 	}
83 
84 	*res = (time_t)l;
85 	return 1;
86 done:
87 	if (dowarn) {
88 		warnx("%s `%s' for %s time", vp, p,
89 		    flag == _PASSWORD_NOEXP ? "expiration" : "change");
90 	}
91 	return 0;
92 
93 }
94 
95 static int
96 getid(unsigned long *res, const char *p, int *flags, int dowarn, int flag)
97 {
98 	unsigned long ul;
99 	char *ep;
100 
101 	if (*p == '\0') {
102 		*flags |= flag;
103 		*res = 0;
104 		return 1;
105 	}
106 	ul = strtoul(p, &ep, 0);
107 	if (p == ep || *ep != '\0') {
108 		ep = __UNCONST("Invalid number");
109 		goto done;
110 	}
111 	if (errno == ERANGE && ul == ULONG_MAX) {
112 		ep = strerror(errno);
113 		goto done;
114 	}
115 	if (ul > *res) {
116 		ep = strerror(ERANGE);
117 		goto done;
118 	}
119 
120 	*res = ul;
121 	return 1;
122 done:
123 	if (dowarn)
124 		warnx("%s %s `%s'", ep,
125 		    flag == _PASSWORD_NOUID ? "uid" : "gid", p);
126 	return 0;
127 
128 }
129 
130 int
131 #ifdef _LIBC
132 __pw_scan(char *bp, struct passwd *pw, int *flags)
133 #else
134 pw_scan( char *bp, struct passwd *pw, int *flags)
135 #endif
136 {
137 	unsigned long id;
138 	int root, inflags;
139 	int dowarn;
140 	const char *p, *sh;
141 
142 	_DIAGASSERT(bp != NULL);
143 	_DIAGASSERT(pw != NULL);
144 
145 	if (flags) {
146 		inflags = *flags;
147 		*flags = 0;
148 	} else {
149 		inflags = 0;
150 		flags = &inflags;
151 	}
152 	dowarn = !(inflags & _PASSWORD_NOWARN);
153 
154 	if (!(pw->pw_name = strsep(&bp, ":")))		/* login */
155 		goto fmt;
156 	if (strlen(pw->pw_name) > (LOGIN_NAME_MAX - 1)) {
157 		if (dowarn)
158 			warnx("username too long, `%s' > %d", pw->pw_name,
159 			    LOGIN_NAME_MAX - 1);
160 		return 0;
161 	}
162 
163 	root = !strcmp(pw->pw_name, "root");
164 
165 	if (!(pw->pw_passwd = strsep(&bp, ":")))	/* passwd */
166 		goto fmt;
167 
168 	if (!(p = strsep(&bp, ":")))			/* uid */
169 		goto fmt;
170 
171 	id = UID_MAX;
172 	if (!getid(&id, p, flags, dowarn, _PASSWORD_NOUID))
173 		return 0;
174 
175 	if (root && id) {
176 		if (dowarn)
177 			warnx("root uid should be 0");
178 		return 0;
179 	}
180 
181 	pw->pw_uid = (uid_t)id;
182 
183 	if (!(p = strsep(&bp, ":")))			/* gid */
184 		goto fmt;
185 
186 	id = GID_MAX;
187 	if (!getid(&id, p, flags, dowarn, _PASSWORD_NOGID))
188 		return 0;
189 
190 	pw->pw_gid = (gid_t)id;
191 
192 	if (inflags & _PASSWORD_OLDFMT) {
193 		pw->pw_class = __UNCONST("");
194 		pw->pw_change = 0;
195 		pw->pw_expire = 0;
196 		*flags |= (_PASSWORD_NOCHG | _PASSWORD_NOEXP);
197 	} else {
198 		pw->pw_class = strsep(&bp, ":");	/* class */
199 		if (!(p = strsep(&bp, ":")))		/* change */
200 			goto fmt;
201 		if (!gettime(&pw->pw_change, p, flags, dowarn, _PASSWORD_NOCHG))
202 			return 0;
203 
204 		if (!(p = strsep(&bp, ":")))		/* expire */
205 			goto fmt;
206 		if (!gettime(&pw->pw_expire, p, flags, dowarn, _PASSWORD_NOEXP))
207 			return 0;
208 	}
209 
210 	pw->pw_gecos = strsep(&bp, ":");		/* gecos */
211 	pw->pw_dir = strsep(&bp, ":");			/* directory */
212 	if (!(pw->pw_shell = strsep(&bp, ":")))		/* shell */
213 		goto fmt;
214 
215 #if ! HAVE_NBTOOL_CONFIG_H
216 	p = pw->pw_shell;
217 	if (root && *p)					/* empty == /bin/sh */
218 		for (setusershell();;) {
219 			if (!(sh = getusershell())) {
220 				if (dowarn)
221 					warnx("warning, unknown root shell");
222 				break;
223 			}
224 			if (!strcmp(p, sh))
225 				break;
226 		}
227 #endif
228 
229 	if ((p = strsep(&bp, ":")) != NULL) {			/* too many */
230 fmt:
231 		if (dowarn)
232 			warnx("corrupted entry");
233 		return 0;
234 	}
235 
236 	return 1;
237 }
238