xref: /freebsd/lib/libc/gen/pw_scan.c (revision a8adfe18cef55ce04d8d219a53c6fb3f971542ab)
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 
34dea673e9SRodney W. Grimes #ifndef lint
358b76e1d7SPhilippe Charnier #if 0
36dea673e9SRodney W. Grimes static char sccsid[] = "@(#)pw_scan.c	8.3 (Berkeley) 4/2/94";
378b76e1d7SPhilippe Charnier #endif
388b76e1d7SPhilippe Charnier static const char rcsid[] =
3997d92980SPeter Wemm   "$FreeBSD$";
40dea673e9SRodney W. Grimes #endif /* not lint */
41dea673e9SRodney W. Grimes 
42dea673e9SRodney W. Grimes /*
43dea673e9SRodney W. Grimes  * This module is used to "verify" password entries by chpass(1) and
44dea673e9SRodney W. Grimes  * pwd_mkdb(8).
45dea673e9SRodney W. Grimes  */
46dea673e9SRodney W. Grimes 
47dea673e9SRodney W. Grimes #include <sys/param.h>
48dea673e9SRodney W. Grimes 
49dea673e9SRodney W. Grimes #include <err.h>
50cd7b8d78SPaul Richards #include <errno.h>
51dea673e9SRodney W. Grimes #include <fcntl.h>
52dea673e9SRodney W. Grimes #include <pwd.h>
53dea673e9SRodney W. Grimes #include <stdio.h>
54dea673e9SRodney W. Grimes #include <string.h>
55dea673e9SRodney W. Grimes #include <stdlib.h>
56dea673e9SRodney W. Grimes #include <unistd.h>
57dea673e9SRodney W. Grimes 
58dea673e9SRodney W. Grimes #include "pw_scan.h"
59dea673e9SRodney W. Grimes 
6018138b08SSheldon Hearn /*
6118138b08SSheldon Hearn  * Some software assumes that IDs are short.  We should emit warnings
6218138b08SSheldon Hearn  * for id's which can not be stored in a short, but we are more liberal
6318138b08SSheldon Hearn  * by default, warning for IDs greater than USHRT_MAX.
649a602accSSheldon Hearn  *
659a602accSSheldon Hearn  * If pw_big_ids_warning is anything other than -1 on entry to pw_scan()
669a602accSSheldon Hearn  * it will be set based on the existance of PW_SCAN_BIG_IDS in the
679a602accSSheldon Hearn  * environment.
6818138b08SSheldon Hearn  */
69248aee62SJacques Vidrine static int	pw_big_ids_warning = -1;
7018138b08SSheldon Hearn 
71dea673e9SRodney W. Grimes int
72248aee62SJacques Vidrine __pw_scan(char *bp, struct passwd *pw, int flags)
73dea673e9SRodney W. Grimes {
74cd7b8d78SPaul Richards 	uid_t id;
75dea673e9SRodney W. Grimes 	int root;
76dea673e9SRodney W. Grimes 	char *p, *sh;
77dea673e9SRodney W. Grimes 
789a602accSSheldon Hearn 	if (pw_big_ids_warning == -1)
799a602accSSheldon Hearn 		pw_big_ids_warning = getenv("PW_SCAN_BIG_IDS") == NULL ? 1 : 0;
809a602accSSheldon Hearn 
8128ca3091SGarrett Wollman 	pw->pw_fields = 0;
82dea673e9SRodney W. Grimes 	if (!(pw->pw_name = strsep(&bp, ":")))		/* login */
83dea673e9SRodney W. Grimes 		goto fmt;
84dea673e9SRodney W. Grimes 	root = !strcmp(pw->pw_name, "root");
8528ca3091SGarrett Wollman 	if(pw->pw_name[0] && (pw->pw_name[0] != '+' || pw->pw_name[1] == '\0'))
8628ca3091SGarrett Wollman 		pw->pw_fields |= _PWF_NAME;
87dea673e9SRodney W. Grimes 
88dea673e9SRodney W. Grimes 	if (!(pw->pw_passwd = strsep(&bp, ":")))	/* passwd */
89dea673e9SRodney W. Grimes 		goto fmt;
9028ca3091SGarrett Wollman 	if(pw->pw_passwd[0]) pw->pw_fields |= _PWF_PASSWD;
91dea673e9SRodney W. Grimes 
92dea673e9SRodney W. Grimes 	if (!(p = strsep(&bp, ":")))			/* uid */
93dea673e9SRodney W. Grimes 		goto fmt;
94aa510d67SEivind Eklund 	if (p[0])
95aa510d67SEivind Eklund 		pw->pw_fields |= _PWF_UID;
96aa510d67SEivind Eklund 	else {
97637bc596SEivind Eklund 		if (pw->pw_name[0] != '+' && pw->pw_name[0] != '-') {
98248aee62SJacques Vidrine 			if (flags & _PWSCAN_WARN)
99aa510d67SEivind Eklund 				warnx("no uid for user %s", pw->pw_name);
100aa510d67SEivind Eklund 			return (0);
101aa510d67SEivind Eklund 		}
102637bc596SEivind Eklund 	}
103cd7b8d78SPaul Richards 	id = strtoul(p, (char **)NULL, 10);
104cd7b8d78SPaul Richards 	if (errno == ERANGE) {
105248aee62SJacques Vidrine 		if (flags & _PWSCAN_WARN)
106aa9ab917SDavid Malone 			warnx("%s > max uid value (%lu)", p, ULONG_MAX);
107cd7b8d78SPaul Richards 		return (0);
108cd7b8d78SPaul Richards 	}
109dea673e9SRodney W. Grimes 	if (root && id) {
110248aee62SJacques Vidrine 		if (flags & _PWSCAN_WARN)
111dea673e9SRodney W. Grimes 			warnx("root uid should be 0");
112dea673e9SRodney W. Grimes 		return (0);
113dea673e9SRodney W. Grimes 	}
114248aee62SJacques Vidrine 	if (flags & _PWSCAN_WARN && pw_big_ids_warning && id > USHRT_MAX) {
115cd7b8d78SPaul Richards 		warnx("%s > recommended max uid value (%u)", p, USHRT_MAX);
11633d37c13SSheldon Hearn 		/*return (0);*/ /* THIS SHOULD NOT BE FATAL! */
117dea673e9SRodney W. Grimes 	}
118dea673e9SRodney W. Grimes 	pw->pw_uid = id;
119dea673e9SRodney W. Grimes 
120dea673e9SRodney W. Grimes 	if (!(p = strsep(&bp, ":")))			/* gid */
121dea673e9SRodney W. Grimes 		goto fmt;
12228ca3091SGarrett Wollman 	if(p[0]) pw->pw_fields |= _PWF_GID;
123cd7b8d78SPaul Richards 	id = strtoul(p, (char **)NULL, 10);
124cd7b8d78SPaul Richards 	if (errno == ERANGE) {
125248aee62SJacques Vidrine 		if (flags & _PWSCAN_WARN)
126aa9ab917SDavid Malone 			warnx("%s > max gid value (%lu)", p, ULONG_MAX);
127cd7b8d78SPaul Richards 		return (0);
128cd7b8d78SPaul Richards 	}
129248aee62SJacques Vidrine 	if (flags & _PWSCAN_WARN && pw_big_ids_warning && id > USHRT_MAX) {
130cd7b8d78SPaul Richards 		warnx("%s > recommended max gid value (%u)", p, USHRT_MAX);
13133d37c13SSheldon Hearn 		/* return (0); This should not be fatal! */
132dea673e9SRodney W. Grimes 	}
133dea673e9SRodney W. Grimes 	pw->pw_gid = id;
134dea673e9SRodney W. Grimes 
135248aee62SJacques Vidrine 	if (flags & _PWSCAN_MASTER ) {
136a8adfe18SDag-Erling Smørgrav 		if (!(pw->pw_class = strsep(&bp, ":")))		/* class */
137a8adfe18SDag-Erling Smørgrav 			goto fmt;
13828ca3091SGarrett Wollman 		if(pw->pw_class[0]) pw->pw_fields |= _PWF_CLASS;
13928ca3091SGarrett Wollman 
140dea673e9SRodney W. Grimes 		if (!(p = strsep(&bp, ":")))			/* change */
141dea673e9SRodney W. Grimes 			goto fmt;
14228ca3091SGarrett Wollman 		if(p[0]) pw->pw_fields |= _PWF_CHANGE;
143dea673e9SRodney W. Grimes 		pw->pw_change = atol(p);
14428ca3091SGarrett Wollman 
145dea673e9SRodney W. Grimes 		if (!(p = strsep(&bp, ":")))			/* expire */
146dea673e9SRodney W. Grimes 			goto fmt;
14728ca3091SGarrett Wollman 		if(p[0]) pw->pw_fields |= _PWF_EXPIRE;
148dea673e9SRodney W. Grimes 		pw->pw_expire = atol(p);
149248aee62SJacques Vidrine 	}
150cc6f6281SDavid Greenman 	if (!(pw->pw_gecos = strsep(&bp, ":")))		/* gecos */
151cc6f6281SDavid Greenman 		goto fmt;
15228ca3091SGarrett Wollman 	if(pw->pw_gecos[0]) pw->pw_fields |= _PWF_GECOS;
15328ca3091SGarrett Wollman 
154cc6f6281SDavid Greenman 	if (!(pw->pw_dir = strsep(&bp, ":")))			/* directory */
155cc6f6281SDavid Greenman 		goto fmt;
15628ca3091SGarrett Wollman 	if(pw->pw_dir[0]) pw->pw_fields |= _PWF_DIR;
15728ca3091SGarrett Wollman 
158dea673e9SRodney W. Grimes 	if (!(pw->pw_shell = strsep(&bp, ":")))		/* shell */
159dea673e9SRodney W. Grimes 		goto fmt;
160dea673e9SRodney W. Grimes 
161dea673e9SRodney W. Grimes 	p = pw->pw_shell;
162dea673e9SRodney W. Grimes 	if (root && *p)					/* empty == /bin/sh */
163dea673e9SRodney W. Grimes 		for (setusershell();;) {
164dea673e9SRodney W. Grimes 			if (!(sh = getusershell())) {
165248aee62SJacques Vidrine 				if (flags & _PWSCAN_WARN)
166dea673e9SRodney W. Grimes 					warnx("warning, unknown root shell");
167dea673e9SRodney W. Grimes 				break;
168dea673e9SRodney W. Grimes 			}
169dea673e9SRodney W. Grimes 			if (!strcmp(p, sh))
170dea673e9SRodney W. Grimes 				break;
171dea673e9SRodney W. Grimes 		}
17228ca3091SGarrett Wollman 	if(p[0]) pw->pw_fields |= _PWF_SHELL;
173dea673e9SRodney W. Grimes 
1748b76e1d7SPhilippe Charnier 	if ((p = strsep(&bp, ":"))) {			/* too many */
175248aee62SJacques Vidrine fmt:
176248aee62SJacques Vidrine 		if (flags & _PWSCAN_WARN)
177248aee62SJacques Vidrine 			warnx("corrupted entry");
178dea673e9SRodney W. Grimes 		return (0);
179dea673e9SRodney W. Grimes 	}
180dea673e9SRodney W. Grimes 	return (1);
181dea673e9SRodney W. Grimes }
182