xref: /freebsd/lib/libc/gen/pw_scan.c (revision aa510d6741622bd40bae9c6c3f4e9d1599c033ee)
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>
50dea673e9SRodney W. Grimes #include <fcntl.h>
51dea673e9SRodney W. Grimes #include <pwd.h>
52dea673e9SRodney W. Grimes #include <stdio.h>
53dea673e9SRodney W. Grimes #include <string.h>
54dea673e9SRodney W. Grimes #include <stdlib.h>
55dea673e9SRodney W. Grimes #include <unistd.h>
56dea673e9SRodney W. Grimes 
57dea673e9SRodney W. Grimes #include "pw_scan.h"
58dea673e9SRodney W. Grimes 
59dea673e9SRodney W. Grimes int
60dea673e9SRodney W. Grimes pw_scan(bp, pw)
61dea673e9SRodney W. Grimes 	char *bp;
62dea673e9SRodney W. Grimes 	struct passwd *pw;
63dea673e9SRodney W. Grimes {
64dea673e9SRodney W. Grimes 	long id;
65dea673e9SRodney W. Grimes 	int root;
66dea673e9SRodney W. Grimes 	char *p, *sh;
67dea673e9SRodney W. Grimes 
6828ca3091SGarrett Wollman 	pw->pw_fields = 0;
69dea673e9SRodney W. Grimes 	if (!(pw->pw_name = strsep(&bp, ":")))		/* login */
70dea673e9SRodney W. Grimes 		goto fmt;
71dea673e9SRodney W. Grimes 	root = !strcmp(pw->pw_name, "root");
7228ca3091SGarrett Wollman 	if(pw->pw_name[0] && (pw->pw_name[0] != '+' || pw->pw_name[1] == '\0'))
7328ca3091SGarrett Wollman 		pw->pw_fields |= _PWF_NAME;
74dea673e9SRodney W. Grimes 
75dea673e9SRodney W. Grimes 	if (!(pw->pw_passwd = strsep(&bp, ":")))	/* passwd */
76dea673e9SRodney W. Grimes 		goto fmt;
7728ca3091SGarrett Wollman 	if(pw->pw_passwd[0]) pw->pw_fields |= _PWF_PASSWD;
78dea673e9SRodney W. Grimes 
79dea673e9SRodney W. Grimes 	if (!(p = strsep(&bp, ":")))			/* uid */
80dea673e9SRodney W. Grimes 		goto fmt;
81aa510d67SEivind Eklund 	if (p[0])
82aa510d67SEivind Eklund 		pw->pw_fields |= _PWF_UID;
83aa510d67SEivind Eklund 	else {
84aa510d67SEivind Eklund 		warnx("no uid for user %s", pw->pw_name);
85aa510d67SEivind Eklund 		return (0);
86aa510d67SEivind Eklund 	}
87dea673e9SRodney W. Grimes 	id = atol(p);
88dea673e9SRodney W. Grimes 	if (root && id) {
89dea673e9SRodney W. Grimes 		warnx("root uid should be 0");
90dea673e9SRodney W. Grimes 		return (0);
91dea673e9SRodney W. Grimes 	}
9233d37c13SSheldon Hearn 	if (id > USHRT_MAX) {
9333d37c13SSheldon Hearn 		warnx("%s > max uid value (%d)", p, USHRT_MAX);
9433d37c13SSheldon Hearn 		/*return (0);*/ /* THIS SHOULD NOT BE FATAL! */
95dea673e9SRodney W. Grimes 	}
96dea673e9SRodney W. Grimes 	pw->pw_uid = id;
97dea673e9SRodney W. Grimes 
98dea673e9SRodney W. Grimes 	if (!(p = strsep(&bp, ":")))			/* gid */
99dea673e9SRodney W. Grimes 		goto fmt;
10028ca3091SGarrett Wollman 	if(p[0]) pw->pw_fields |= _PWF_GID;
101dea673e9SRodney W. Grimes 	id = atol(p);
10233d37c13SSheldon Hearn 	if (id > USHRT_MAX) {
10333d37c13SSheldon Hearn 		warnx("%s > max gid value (%d)", p, USHRT_MAX);
10433d37c13SSheldon Hearn 		/* return (0); This should not be fatal! */
105dea673e9SRodney W. Grimes 	}
106dea673e9SRodney W. Grimes 	pw->pw_gid = id;
107dea673e9SRodney W. Grimes 
108dea673e9SRodney W. Grimes 	pw->pw_class = strsep(&bp, ":");		/* class */
10928ca3091SGarrett Wollman 	if(pw->pw_class[0]) pw->pw_fields |= _PWF_CLASS;
11028ca3091SGarrett Wollman 
111dea673e9SRodney W. Grimes 	if (!(p = strsep(&bp, ":")))			/* change */
112dea673e9SRodney W. Grimes 		goto fmt;
11328ca3091SGarrett Wollman 	if(p[0]) pw->pw_fields |= _PWF_CHANGE;
114dea673e9SRodney W. Grimes 	pw->pw_change = atol(p);
11528ca3091SGarrett Wollman 
116dea673e9SRodney W. Grimes 	if (!(p = strsep(&bp, ":")))			/* expire */
117dea673e9SRodney W. Grimes 		goto fmt;
11828ca3091SGarrett Wollman 	if(p[0]) pw->pw_fields |= _PWF_EXPIRE;
119dea673e9SRodney W. Grimes 	pw->pw_expire = atol(p);
12028ca3091SGarrett Wollman 
121cc6f6281SDavid Greenman 	if (!(pw->pw_gecos = strsep(&bp, ":")))		/* gecos */
122cc6f6281SDavid Greenman 		goto fmt;
12328ca3091SGarrett Wollman 	if(pw->pw_gecos[0]) pw->pw_fields |= _PWF_GECOS;
12428ca3091SGarrett Wollman 
125cc6f6281SDavid Greenman 	if (!(pw->pw_dir = strsep(&bp, ":")))			/* directory */
126cc6f6281SDavid Greenman 		goto fmt;
12728ca3091SGarrett Wollman 	if(pw->pw_dir[0]) pw->pw_fields |= _PWF_DIR;
12828ca3091SGarrett Wollman 
129dea673e9SRodney W. Grimes 	if (!(pw->pw_shell = strsep(&bp, ":")))		/* shell */
130dea673e9SRodney W. Grimes 		goto fmt;
131dea673e9SRodney W. Grimes 
132dea673e9SRodney W. Grimes 	p = pw->pw_shell;
133dea673e9SRodney W. Grimes 	if (root && *p)					/* empty == /bin/sh */
134dea673e9SRodney W. Grimes 		for (setusershell();;) {
135dea673e9SRodney W. Grimes 			if (!(sh = getusershell())) {
136dea673e9SRodney W. Grimes 				warnx("warning, unknown root shell");
137dea673e9SRodney W. Grimes 				break;
138dea673e9SRodney W. Grimes 			}
139dea673e9SRodney W. Grimes 			if (!strcmp(p, sh))
140dea673e9SRodney W. Grimes 				break;
141dea673e9SRodney W. Grimes 		}
14228ca3091SGarrett Wollman 	if(p[0]) pw->pw_fields |= _PWF_SHELL;
143dea673e9SRodney W. Grimes 
1448b76e1d7SPhilippe Charnier 	if ((p = strsep(&bp, ":"))) {			/* too many */
145dea673e9SRodney W. Grimes fmt:		warnx("corrupted entry");
146dea673e9SRodney W. Grimes 		return (0);
147dea673e9SRodney W. Grimes 	}
148dea673e9SRodney W. Grimes 	return (1);
149dea673e9SRodney W. Grimes }
150