xref: /freebsd/lib/libc/gen/pw_scan.c (revision 9b7c7ff8ce873feef4c147d5923d0ece613bbaaf)
1dea673e9SRodney W. Grimes /*-
2dea673e9SRodney W. Grimes  * Copyright (c) 1990, 1993, 1994
3dea673e9SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4dea673e9SRodney W. Grimes  *
5dea673e9SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
6dea673e9SRodney W. Grimes  * modification, are permitted provided that the following conditions
7dea673e9SRodney W. Grimes  * are met:
8dea673e9SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
9dea673e9SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
10dea673e9SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
11dea673e9SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
12dea673e9SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
13dea673e9SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
14dea673e9SRodney W. Grimes  *    must display the following acknowledgement:
15dea673e9SRodney W. Grimes  *	This product includes software developed by the University of
16dea673e9SRodney W. Grimes  *	California, Berkeley and its contributors.
17dea673e9SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
18dea673e9SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
19dea673e9SRodney W. Grimes  *    without specific prior written permission.
20dea673e9SRodney W. Grimes  *
21dea673e9SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22dea673e9SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23dea673e9SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24dea673e9SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25dea673e9SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26dea673e9SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27dea673e9SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28dea673e9SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29dea673e9SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30dea673e9SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31dea673e9SRodney W. Grimes  * SUCH DAMAGE.
32dea673e9SRodney W. Grimes  */
33dea673e9SRodney W. Grimes 
34ea8d448aSDavid E. O'Brien #if defined(LIBC_SCCS) && !defined(lint)
35dea673e9SRodney W. Grimes static char sccsid[] = "@(#)pw_scan.c	8.3 (Berkeley) 4/2/94";
36ea8d448aSDavid E. O'Brien #endif /* LIBC_SCCS and not lint */
37ea8d448aSDavid E. O'Brien #include <sys/cdefs.h>
38ea8d448aSDavid E. O'Brien __FBSDID("$FreeBSD$");
39dea673e9SRodney W. Grimes 
40dea673e9SRodney W. Grimes /*
41dea673e9SRodney W. Grimes  * This module is used to "verify" password entries by chpass(1) and
42dea673e9SRodney W. Grimes  * pwd_mkdb(8).
43dea673e9SRodney W. Grimes  */
44dea673e9SRodney W. Grimes 
45dea673e9SRodney W. Grimes #include <sys/param.h>
46dea673e9SRodney W. Grimes 
47dea673e9SRodney W. Grimes #include <err.h>
48cd7b8d78SPaul Richards #include <errno.h>
49dea673e9SRodney W. Grimes #include <fcntl.h>
50dea673e9SRodney W. Grimes #include <pwd.h>
51dea673e9SRodney W. Grimes #include <stdio.h>
52dea673e9SRodney W. Grimes #include <string.h>
53dea673e9SRodney W. Grimes #include <stdlib.h>
54dea673e9SRodney W. Grimes #include <unistd.h>
55dea673e9SRodney W. Grimes 
56dea673e9SRodney W. Grimes #include "pw_scan.h"
57dea673e9SRodney W. Grimes 
5818138b08SSheldon Hearn /*
5918138b08SSheldon Hearn  * Some software assumes that IDs are short.  We should emit warnings
6018138b08SSheldon Hearn  * for id's which cannot be stored in a short, but we are more liberal
6118138b08SSheldon Hearn  * by default, warning for IDs greater than USHRT_MAX.
629a602accSSheldon Hearn  *
63235d6772SDima Dorfman  * If pw_big_ids_warning is -1 on entry to pw_scan(), it will be set based
64235d6772SDima Dorfman  * on the existence of PW_SCAN_BIG_IDS in the environment.
6518138b08SSheldon Hearn  */
66248aee62SJacques Vidrine static int	pw_big_ids_warning = -1;
6718138b08SSheldon Hearn 
68dea673e9SRodney W. Grimes int
69248aee62SJacques Vidrine __pw_scan(char *bp, struct passwd *pw, int flags)
70dea673e9SRodney W. Grimes {
71cd7b8d78SPaul Richards 	uid_t id;
72dea673e9SRodney W. Grimes 	int root;
7305a7daf5SMaxim Konovalov 	char *ep, *p, *sh;
74dea673e9SRodney W. Grimes 
759a602accSSheldon Hearn 	if (pw_big_ids_warning == -1)
769a602accSSheldon Hearn 		pw_big_ids_warning = getenv("PW_SCAN_BIG_IDS") == NULL ? 1 : 0;
779a602accSSheldon Hearn 
7828ca3091SGarrett Wollman 	pw->pw_fields = 0;
79dea673e9SRodney W. Grimes 	if (!(pw->pw_name = strsep(&bp, ":")))		/* login */
80dea673e9SRodney W. Grimes 		goto fmt;
81dea673e9SRodney W. Grimes 	root = !strcmp(pw->pw_name, "root");
8228ca3091SGarrett Wollman 	if (pw->pw_name[0] && (pw->pw_name[0] != '+' || pw->pw_name[1] == '\0'))
8328ca3091SGarrett Wollman 		pw->pw_fields |= _PWF_NAME;
84dea673e9SRodney W. Grimes 
85dea673e9SRodney W. Grimes 	if (!(pw->pw_passwd = strsep(&bp, ":")))	/* passwd */
86dea673e9SRodney W. Grimes 		goto fmt;
876478822fSDag-Erling Smørgrav 	if (pw->pw_passwd[0])
886478822fSDag-Erling Smørgrav 		pw->pw_fields |= _PWF_PASSWD;
89dea673e9SRodney W. Grimes 
90dea673e9SRodney W. Grimes 	if (!(p = strsep(&bp, ":")))			/* uid */
91dea673e9SRodney W. Grimes 		goto fmt;
92aa510d67SEivind Eklund 	if (p[0])
93aa510d67SEivind Eklund 		pw->pw_fields |= _PWF_UID;
94aa510d67SEivind Eklund 	else {
95637bc596SEivind Eklund 		if (pw->pw_name[0] != '+' && pw->pw_name[0] != '-') {
96248aee62SJacques Vidrine 			if (flags & _PWSCAN_WARN)
97aa510d67SEivind Eklund 				warnx("no uid for user %s", pw->pw_name);
98aa510d67SEivind Eklund 			return (0);
99aa510d67SEivind Eklund 		}
100637bc596SEivind Eklund 	}
10105a7daf5SMaxim Konovalov 	id = strtoul(p, &ep, 10);
102cd7b8d78SPaul Richards 	if (errno == ERANGE) {
103248aee62SJacques Vidrine 		if (flags & _PWSCAN_WARN)
104aa9ab917SDavid Malone 			warnx("%s > max uid value (%lu)", p, ULONG_MAX);
105cd7b8d78SPaul Richards 		return (0);
106cd7b8d78SPaul Richards 	}
1079b7c7ff8SMaxim Konovalov 	if (*ep != '\0') {
10805a7daf5SMaxim Konovalov 		if (flags & _PWSCAN_WARN)
10905a7daf5SMaxim Konovalov 			warnx("%s uid is incorrect", p);
11005a7daf5SMaxim Konovalov 		return (0);
11105a7daf5SMaxim Konovalov 	}
112dea673e9SRodney W. Grimes 	if (root && id) {
113248aee62SJacques Vidrine 		if (flags & _PWSCAN_WARN)
114dea673e9SRodney W. Grimes 			warnx("root uid should be 0");
115dea673e9SRodney W. Grimes 		return (0);
116dea673e9SRodney W. Grimes 	}
117248aee62SJacques Vidrine 	if (flags & _PWSCAN_WARN && pw_big_ids_warning && id > USHRT_MAX) {
118cd7b8d78SPaul Richards 		warnx("%s > recommended max uid value (%u)", p, USHRT_MAX);
11933d37c13SSheldon Hearn 		/*return (0);*/ /* THIS SHOULD NOT BE FATAL! */
120dea673e9SRodney W. Grimes 	}
121dea673e9SRodney W. Grimes 	pw->pw_uid = id;
122dea673e9SRodney W. Grimes 
123dea673e9SRodney W. Grimes 	if (!(p = strsep(&bp, ":")))			/* gid */
124dea673e9SRodney W. Grimes 		goto fmt;
1256478822fSDag-Erling Smørgrav 	if (p[0])
1266478822fSDag-Erling Smørgrav 		pw->pw_fields |= _PWF_GID;
12705a7daf5SMaxim Konovalov 	id = strtoul(p, &ep, 10);
128cd7b8d78SPaul Richards 	if (errno == ERANGE) {
129248aee62SJacques Vidrine 		if (flags & _PWSCAN_WARN)
130aa9ab917SDavid Malone 			warnx("%s > max gid value (%lu)", p, ULONG_MAX);
131cd7b8d78SPaul Richards 		return (0);
132cd7b8d78SPaul Richards 	}
1339b7c7ff8SMaxim Konovalov 	if (*ep != '\0') {
13405a7daf5SMaxim Konovalov 		if (flags & _PWSCAN_WARN)
13505a7daf5SMaxim Konovalov 			warnx("%s gid is incorrect", p);
13605a7daf5SMaxim Konovalov 		return (0);
13705a7daf5SMaxim Konovalov 	}
138248aee62SJacques Vidrine 	if (flags & _PWSCAN_WARN && pw_big_ids_warning && id > USHRT_MAX) {
139cd7b8d78SPaul Richards 		warnx("%s > recommended max gid value (%u)", p, USHRT_MAX);
14033d37c13SSheldon Hearn 		/* return (0); This should not be fatal! */
141dea673e9SRodney W. Grimes 	}
142dea673e9SRodney W. Grimes 	pw->pw_gid = id;
143dea673e9SRodney W. Grimes 
144248aee62SJacques Vidrine 	if (flags & _PWSCAN_MASTER ) {
145a8adfe18SDag-Erling Smørgrav 		if (!(pw->pw_class = strsep(&bp, ":")))	/* class */
146a8adfe18SDag-Erling Smørgrav 			goto fmt;
1476478822fSDag-Erling Smørgrav 		if (pw->pw_class[0])
1486478822fSDag-Erling Smørgrav 			pw->pw_fields |= _PWF_CLASS;
14928ca3091SGarrett Wollman 
150dea673e9SRodney W. Grimes 		if (!(p = strsep(&bp, ":")))		/* change */
151dea673e9SRodney W. Grimes 			goto fmt;
1526478822fSDag-Erling Smørgrav 		if (p[0])
1536478822fSDag-Erling Smørgrav 			pw->pw_fields |= _PWF_CHANGE;
154dea673e9SRodney W. Grimes 		pw->pw_change = atol(p);
15528ca3091SGarrett Wollman 
156dea673e9SRodney W. Grimes 		if (!(p = strsep(&bp, ":")))		/* expire */
157dea673e9SRodney W. Grimes 			goto fmt;
1586478822fSDag-Erling Smørgrav 		if (p[0])
1596478822fSDag-Erling Smørgrav 			pw->pw_fields |= _PWF_EXPIRE;
160dea673e9SRodney W. Grimes 		pw->pw_expire = atol(p);
161248aee62SJacques Vidrine 	}
162cc6f6281SDavid Greenman 	if (!(pw->pw_gecos = strsep(&bp, ":")))		/* gecos */
163cc6f6281SDavid Greenman 		goto fmt;
1646478822fSDag-Erling Smørgrav 	if (pw->pw_gecos[0])
1656478822fSDag-Erling Smørgrav 		pw->pw_fields |= _PWF_GECOS;
16628ca3091SGarrett Wollman 
167cc6f6281SDavid Greenman 	if (!(pw->pw_dir = strsep(&bp, ":")))		/* directory */
168cc6f6281SDavid Greenman 		goto fmt;
1696478822fSDag-Erling Smørgrav 	if (pw->pw_dir[0])
1706478822fSDag-Erling Smørgrav 		pw->pw_fields |= _PWF_DIR;
17128ca3091SGarrett Wollman 
172dea673e9SRodney W. Grimes 	if (!(pw->pw_shell = strsep(&bp, ":")))		/* shell */
173dea673e9SRodney W. Grimes 		goto fmt;
174dea673e9SRodney W. Grimes 
175dea673e9SRodney W. Grimes 	p = pw->pw_shell;
176dea673e9SRodney W. Grimes 	if (root && *p)					/* empty == /bin/sh */
177dea673e9SRodney W. Grimes 		for (setusershell();;) {
178dea673e9SRodney W. Grimes 			if (!(sh = getusershell())) {
179248aee62SJacques Vidrine 				if (flags & _PWSCAN_WARN)
180dea673e9SRodney W. Grimes 					warnx("warning, unknown root shell");
181dea673e9SRodney W. Grimes 				break;
182dea673e9SRodney W. Grimes 			}
183dea673e9SRodney W. Grimes 			if (!strcmp(p, sh))
184dea673e9SRodney W. Grimes 				break;
185dea673e9SRodney W. Grimes 		}
1866478822fSDag-Erling Smørgrav 	if (p[0])
1876478822fSDag-Erling Smørgrav 		pw->pw_fields |= _PWF_SHELL;
188dea673e9SRodney W. Grimes 
1898b76e1d7SPhilippe Charnier 	if ((p = strsep(&bp, ":"))) {			/* too many */
190248aee62SJacques Vidrine fmt:
191248aee62SJacques Vidrine 		if (flags & _PWSCAN_WARN)
192248aee62SJacques Vidrine 			warnx("corrupted entry");
193dea673e9SRodney W. Grimes 		return (0);
194dea673e9SRodney W. Grimes 	}
195dea673e9SRodney W. Grimes 	return (1);
196dea673e9SRodney W. Grimes }
197