xref: /freebsd/lib/libc/gen/pw_scan.c (revision 28ca30918a89f3e5efa3a3402656ea766be3b4ad)
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
35dea673e9SRodney W. Grimes static char sccsid[] = "@(#)pw_scan.c	8.3 (Berkeley) 4/2/94";
36dea673e9SRodney W. Grimes #endif /* not lint */
37dea673e9SRodney W. Grimes 
38dea673e9SRodney W. Grimes /*
39dea673e9SRodney W. Grimes  * This module is used to "verify" password entries by chpass(1) and
40dea673e9SRodney W. Grimes  * pwd_mkdb(8).
41dea673e9SRodney W. Grimes  */
42dea673e9SRodney W. Grimes 
43dea673e9SRodney W. Grimes #include <sys/param.h>
44dea673e9SRodney W. Grimes 
45dea673e9SRodney W. Grimes #include <err.h>
46dea673e9SRodney W. Grimes #include <fcntl.h>
47dea673e9SRodney W. Grimes #include <pwd.h>
48dea673e9SRodney W. Grimes #include <errno.h>
49dea673e9SRodney W. Grimes #include <stdio.h>
50dea673e9SRodney W. Grimes #include <string.h>
51dea673e9SRodney W. Grimes #include <stdlib.h>
52dea673e9SRodney W. Grimes #include <unistd.h>
53dea673e9SRodney W. Grimes 
54dea673e9SRodney W. Grimes #include "pw_scan.h"
55dea673e9SRodney W. Grimes 
56dea673e9SRodney W. Grimes int
57dea673e9SRodney W. Grimes pw_scan(bp, pw)
58dea673e9SRodney W. Grimes 	char *bp;
59dea673e9SRodney W. Grimes 	struct passwd *pw;
60dea673e9SRodney W. Grimes {
61dea673e9SRodney W. Grimes 	long id;
62dea673e9SRodney W. Grimes 	int root;
63dea673e9SRodney W. Grimes 	char *p, *sh;
64dea673e9SRodney W. Grimes 
6528ca3091SGarrett Wollman 	pw->pw_fields = 0;
66dea673e9SRodney W. Grimes 	if (!(pw->pw_name = strsep(&bp, ":")))		/* login */
67dea673e9SRodney W. Grimes 		goto fmt;
68dea673e9SRodney W. Grimes 	root = !strcmp(pw->pw_name, "root");
6928ca3091SGarrett Wollman 	if(pw->pw_name[0] && (pw->pw_name[0] != '+' || pw->pw_name[1] == '\0'))
7028ca3091SGarrett Wollman 		pw->pw_fields |= _PWF_NAME;
71dea673e9SRodney W. Grimes 
72dea673e9SRodney W. Grimes 	if (!(pw->pw_passwd = strsep(&bp, ":")))	/* passwd */
73dea673e9SRodney W. Grimes 		goto fmt;
7428ca3091SGarrett Wollman 	if(pw->pw_passwd[0]) pw->pw_fields |= _PWF_PASSWD;
75dea673e9SRodney W. Grimes 
76dea673e9SRodney W. Grimes 	if (!(p = strsep(&bp, ":")))			/* uid */
77dea673e9SRodney W. Grimes 		goto fmt;
7828ca3091SGarrett Wollman 	if(p[0]) pw->pw_fields |= _PWF_UID;
79dea673e9SRodney W. Grimes 	id = atol(p);
80dea673e9SRodney W. Grimes 	if (root && id) {
81dea673e9SRodney W. Grimes 		warnx("root uid should be 0");
82dea673e9SRodney W. Grimes 		return (0);
83dea673e9SRodney W. Grimes 	}
84dea673e9SRodney W. Grimes 	if (id > USHRT_MAX) {
85dea673e9SRodney W. Grimes 		warnx("%s > max uid value (%d)", p, USHRT_MAX);
86dea673e9SRodney W. Grimes 		return (0);
87dea673e9SRodney W. Grimes 	}
88dea673e9SRodney W. Grimes 	pw->pw_uid = id;
89dea673e9SRodney W. Grimes 
90dea673e9SRodney W. Grimes 	if (!(p = strsep(&bp, ":")))			/* gid */
91dea673e9SRodney W. Grimes 		goto fmt;
9228ca3091SGarrett Wollman 	if(p[0]) pw->pw_fields |= _PWF_GID;
93dea673e9SRodney W. Grimes 	id = atol(p);
94dea673e9SRodney W. Grimes 	if (id > USHRT_MAX) {
95dea673e9SRodney W. Grimes 		warnx("%s > max gid value (%d)", p, USHRT_MAX);
96dea673e9SRodney W. Grimes 		return (0);
97dea673e9SRodney W. Grimes 	}
98dea673e9SRodney W. Grimes 	pw->pw_gid = id;
99dea673e9SRodney W. Grimes 
100dea673e9SRodney W. Grimes 	pw->pw_class = strsep(&bp, ":");		/* class */
10128ca3091SGarrett Wollman 	if(pw->pw_class[0]) pw->pw_fields |= _PWF_CLASS;
10228ca3091SGarrett Wollman 
103dea673e9SRodney W. Grimes 	if (!(p = strsep(&bp, ":")))			/* change */
104dea673e9SRodney W. Grimes 		goto fmt;
10528ca3091SGarrett Wollman 	if(p[0]) pw->pw_fields |= _PWF_CHANGE;
106dea673e9SRodney W. Grimes 	pw->pw_change = atol(p);
10728ca3091SGarrett Wollman 
108dea673e9SRodney W. Grimes 	if (!(p = strsep(&bp, ":")))			/* expire */
109dea673e9SRodney W. Grimes 		goto fmt;
11028ca3091SGarrett Wollman 	if(p[0]) pw->pw_fields |= _PWF_EXPIRE;
111dea673e9SRodney W. Grimes 	pw->pw_expire = atol(p);
11228ca3091SGarrett Wollman 
113dea673e9SRodney W. Grimes 	pw->pw_gecos = strsep(&bp, ":");		/* gecos */
11428ca3091SGarrett Wollman 	if(pw->pw_gecos[0]) pw->pw_fields |= _PWF_GECOS;
11528ca3091SGarrett Wollman 
116dea673e9SRodney W. Grimes 	pw->pw_dir = strsep(&bp, ":");			/* directory */
11728ca3091SGarrett Wollman 	if(pw->pw_dir[0]) pw->pw_fields |= _PWF_DIR;
11828ca3091SGarrett Wollman 
119dea673e9SRodney W. Grimes 	if (!(pw->pw_shell = strsep(&bp, ":")))		/* shell */
120dea673e9SRodney W. Grimes 		goto fmt;
121dea673e9SRodney W. Grimes 
122dea673e9SRodney W. Grimes 	p = pw->pw_shell;
123dea673e9SRodney W. Grimes 	if (root && *p)					/* empty == /bin/sh */
124dea673e9SRodney W. Grimes 		for (setusershell();;) {
125dea673e9SRodney W. Grimes 			if (!(sh = getusershell())) {
126dea673e9SRodney W. Grimes 				warnx("warning, unknown root shell");
127dea673e9SRodney W. Grimes 				break;
128dea673e9SRodney W. Grimes 			}
129dea673e9SRodney W. Grimes 			if (!strcmp(p, sh))
130dea673e9SRodney W. Grimes 				break;
131dea673e9SRodney W. Grimes 		}
13228ca3091SGarrett Wollman 	if(p[0]) pw->pw_fields |= _PWF_SHELL;
133dea673e9SRodney W. Grimes 
134dea673e9SRodney W. Grimes 	if (p = strsep(&bp, ":")) {			/* too many */
135dea673e9SRodney W. Grimes fmt:		warnx("corrupted entry");
136dea673e9SRodney W. Grimes 		return (0);
137dea673e9SRodney W. Grimes 	}
138dea673e9SRodney W. Grimes 	return (1);
139dea673e9SRodney W. Grimes }
140