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