xref: /freebsd/lib/libc/gen/pw_scan.c (revision 8a0a413e)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1990, 1993, 1994
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 defined(LIBC_SCCS) && !defined(lint)
33 static char sccsid[] = "@(#)pw_scan.c	8.3 (Berkeley) 4/2/94";
34 #endif /* LIBC_SCCS and not lint */
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37 
38 /*
39  * This module is used to "verify" password entries by chpass(1) and
40  * pwd_mkdb(8).
41  */
42 
43 #include <sys/param.h>
44 
45 #include <err.h>
46 #include <errno.h>
47 #include <fcntl.h>
48 #include <pwd.h>
49 #include <stdio.h>
50 #include <string.h>
51 #include <stdlib.h>
52 #include <unistd.h>
53 
54 #include "pw_scan.h"
55 
56 /*
57  * Some software assumes that IDs are short.  We should emit warnings
58  * for id's which cannot be stored in a short, but we are more liberal
59  * by default, warning for IDs greater than USHRT_MAX.
60  *
61  * If pw_big_ids_warning is -1 on entry to pw_scan(), it will be set based
62  * on the existence of PW_SCAN_BIG_IDS in the environment.
63  *
64  * It is believed all baseline system software that can not handle the
65  * normal ID sizes is now gone so pw_big_ids_warning is disabled for now.
66  * But the code has been left in place in case end-users want to re-enable
67  * it and/or for the next time the ID sizes get bigger but pieces of the
68  * system lag behind.
69  */
70 static int	pw_big_ids_warning = 0;
71 
72 int
73 __pw_scan(char *bp, struct passwd *pw, int flags)
74 {
75 	uid_t id;
76 	int root;
77 	char *ep, *p, *sh;
78 	unsigned long temp;
79 
80 	if (pw_big_ids_warning == -1)
81 		pw_big_ids_warning = getenv("PW_SCAN_BIG_IDS") == NULL ? 1 : 0;
82 
83 	pw->pw_fields = 0;
84 	if (!(pw->pw_name = strsep(&bp, ":")))		/* login */
85 		goto fmt;
86 	root = !strcmp(pw->pw_name, "root");
87 	if (pw->pw_name[0] && (pw->pw_name[0] != '+' || pw->pw_name[1] == '\0'))
88 		pw->pw_fields |= _PWF_NAME;
89 
90 	if (!(pw->pw_passwd = strsep(&bp, ":")))	/* passwd */
91 		goto fmt;
92 	if (pw->pw_passwd[0])
93 		pw->pw_fields |= _PWF_PASSWD;
94 
95 	if (!(p = strsep(&bp, ":")))			/* uid */
96 		goto fmt;
97 	if (p[0])
98 		pw->pw_fields |= _PWF_UID;
99 	else {
100 		if (pw->pw_name[0] != '+' && pw->pw_name[0] != '-') {
101 			if (flags & _PWSCAN_WARN)
102 				warnx("no uid for user %s", pw->pw_name);
103 			return (0);
104 		}
105 	}
106 	errno = 0;
107 	temp = strtoul(p, &ep, 10);
108 	if ((temp == ULONG_MAX && errno == ERANGE) || temp > UID_MAX) {
109 		if (flags & _PWSCAN_WARN)
110 			warnx("%s > max uid value (%u)", p, UID_MAX);
111 		return (0);
112 	}
113 	id = temp;
114 	if (*ep != '\0') {
115 		if (flags & _PWSCAN_WARN)
116 			warnx("%s uid is incorrect", p);
117 		return (0);
118 	}
119 	if (root && id) {
120 		if (flags & _PWSCAN_WARN)
121 			warnx("root uid should be 0");
122 		return (0);
123 	}
124 	if (flags & _PWSCAN_WARN && pw_big_ids_warning && id > USHRT_MAX) {
125 		warnx("%s > recommended max uid value (%u)", p, USHRT_MAX);
126 		/*return (0);*/ /* THIS SHOULD NOT BE FATAL! */
127 	}
128 	pw->pw_uid = id;
129 
130 	if (!(p = strsep(&bp, ":")))			/* gid */
131 		goto fmt;
132 	if (p[0])
133 		pw->pw_fields |= _PWF_GID;
134 	else {
135 		if (pw->pw_name[0] != '+' && pw->pw_name[0] != '-') {
136 			if (flags & _PWSCAN_WARN)
137 				warnx("no gid for user %s", pw->pw_name);
138 			return (0);
139 		}
140 	}
141 	errno = 0;
142 	temp = strtoul(p, &ep, 10);
143 	if ((temp == ULONG_MAX && errno == ERANGE) || temp > GID_MAX) {
144 		if (flags & _PWSCAN_WARN)
145 			warnx("%s > max gid value (%u)", p, GID_MAX);
146 		return (0);
147 	}
148 	id = temp;
149 	if (*ep != '\0') {
150 		if (flags & _PWSCAN_WARN)
151 			warnx("%s gid is incorrect", p);
152 		return (0);
153 	}
154 	if (flags & _PWSCAN_WARN && pw_big_ids_warning && id > USHRT_MAX) {
155 		warnx("%s > recommended max gid value (%u)", p, USHRT_MAX);
156 		/* return (0); This should not be fatal! */
157 	}
158 	pw->pw_gid = id;
159 
160 	if (flags & _PWSCAN_MASTER ) {
161 		if (!(pw->pw_class = strsep(&bp, ":")))	/* class */
162 			goto fmt;
163 		if (pw->pw_class[0])
164 			pw->pw_fields |= _PWF_CLASS;
165 
166 		if (!(p = strsep(&bp, ":")))		/* change */
167 			goto fmt;
168 		if (p[0])
169 			pw->pw_fields |= _PWF_CHANGE;
170 		pw->pw_change = atol(p);
171 
172 		if (!(p = strsep(&bp, ":")))		/* expire */
173 			goto fmt;
174 		if (p[0])
175 			pw->pw_fields |= _PWF_EXPIRE;
176 		pw->pw_expire = atol(p);
177 	}
178 	if (!(pw->pw_gecos = strsep(&bp, ":")))		/* gecos */
179 		goto fmt;
180 	if (pw->pw_gecos[0])
181 		pw->pw_fields |= _PWF_GECOS;
182 
183 	if (!(pw->pw_dir = strsep(&bp, ":")))		/* directory */
184 		goto fmt;
185 	if (pw->pw_dir[0])
186 		pw->pw_fields |= _PWF_DIR;
187 
188 	if (!(pw->pw_shell = strsep(&bp, ":")))		/* shell */
189 		goto fmt;
190 
191 	p = pw->pw_shell;
192 	if (root && *p) {				/* empty == /bin/sh */
193 		for (setusershell();;) {
194 			if (!(sh = getusershell())) {
195 				if (flags & _PWSCAN_WARN)
196 					warnx("warning, unknown root shell");
197 				break;
198 			}
199 			if (!strcmp(p, sh))
200 				break;
201 		}
202 		endusershell();
203 	}
204 	if (p[0])
205 		pw->pw_fields |= _PWF_SHELL;
206 
207 	if ((p = strsep(&bp, ":"))) {			/* too many */
208 fmt:
209 		if (flags & _PWSCAN_WARN)
210 			warnx("corrupted entry");
211 		return (0);
212 	}
213 	return (1);
214 }
215