xref: /freebsd/lib/libc/gen/pw_scan.c (revision 18138b08d80e286f69bf00b26bc4c8f9c77dd4c8)
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 
5918138b08SSheldon Hearn /*
6018138b08SSheldon Hearn  * Some software assumes that IDs are short.  We should emit warnings
6118138b08SSheldon Hearn  * for id's which can not be stored in a short, but we are more liberal
6218138b08SSheldon Hearn  * by default, warning for IDs greater than USHRT_MAX.
6318138b08SSheldon Hearn  */
6418138b08SSheldon Hearn int	pw_big_ids_warning = 1;
6518138b08SSheldon Hearn 
66dea673e9SRodney W. Grimes int
67dea673e9SRodney W. Grimes pw_scan(bp, pw)
68dea673e9SRodney W. Grimes 	char *bp;
69dea673e9SRodney W. Grimes 	struct passwd *pw;
70dea673e9SRodney W. Grimes {
71dea673e9SRodney W. Grimes 	long id;
72dea673e9SRodney W. Grimes 	int root;
73dea673e9SRodney W. Grimes 	char *p, *sh;
74dea673e9SRodney W. Grimes 
7528ca3091SGarrett Wollman 	pw->pw_fields = 0;
76dea673e9SRodney W. Grimes 	if (!(pw->pw_name = strsep(&bp, ":")))		/* login */
77dea673e9SRodney W. Grimes 		goto fmt;
78dea673e9SRodney W. Grimes 	root = !strcmp(pw->pw_name, "root");
7928ca3091SGarrett Wollman 	if(pw->pw_name[0] && (pw->pw_name[0] != '+' || pw->pw_name[1] == '\0'))
8028ca3091SGarrett Wollman 		pw->pw_fields |= _PWF_NAME;
81dea673e9SRodney W. Grimes 
82dea673e9SRodney W. Grimes 	if (!(pw->pw_passwd = strsep(&bp, ":")))	/* passwd */
83dea673e9SRodney W. Grimes 		goto fmt;
8428ca3091SGarrett Wollman 	if(pw->pw_passwd[0]) pw->pw_fields |= _PWF_PASSWD;
85dea673e9SRodney W. Grimes 
86dea673e9SRodney W. Grimes 	if (!(p = strsep(&bp, ":")))			/* uid */
87dea673e9SRodney W. Grimes 		goto fmt;
88aa510d67SEivind Eklund 	if (p[0])
89aa510d67SEivind Eklund 		pw->pw_fields |= _PWF_UID;
90aa510d67SEivind Eklund 	else {
91aa510d67SEivind Eklund 		warnx("no uid for user %s", pw->pw_name);
92aa510d67SEivind Eklund 		return (0);
93aa510d67SEivind Eklund 	}
94dea673e9SRodney W. Grimes 	id = atol(p);
95dea673e9SRodney W. Grimes 	if (root && id) {
96dea673e9SRodney W. Grimes 		warnx("root uid should be 0");
97dea673e9SRodney W. Grimes 		return (0);
98dea673e9SRodney W. Grimes 	}
9918138b08SSheldon Hearn 	if (pw_big_ids_warning && id > USHRT_MAX) {
10018138b08SSheldon Hearn 		warnx("%s > max uid value (%u)", p, USHRT_MAX);
10133d37c13SSheldon Hearn 		/*return (0);*/ /* THIS SHOULD NOT BE FATAL! */
102dea673e9SRodney W. Grimes 	}
103dea673e9SRodney W. Grimes 	pw->pw_uid = id;
104dea673e9SRodney W. Grimes 
105dea673e9SRodney W. Grimes 	if (!(p = strsep(&bp, ":")))			/* gid */
106dea673e9SRodney W. Grimes 		goto fmt;
10728ca3091SGarrett Wollman 	if(p[0]) pw->pw_fields |= _PWF_GID;
108dea673e9SRodney W. Grimes 	id = atol(p);
10918138b08SSheldon Hearn 	if (pw_big_ids_warning && id > USHRT_MAX) {
11018138b08SSheldon Hearn 		warnx("%s > max gid value (%u)", p, USHRT_MAX);
11133d37c13SSheldon Hearn 		/* return (0); This should not be fatal! */
112dea673e9SRodney W. Grimes 	}
113dea673e9SRodney W. Grimes 	pw->pw_gid = id;
114dea673e9SRodney W. Grimes 
115dea673e9SRodney W. Grimes 	pw->pw_class = strsep(&bp, ":");		/* class */
11628ca3091SGarrett Wollman 	if(pw->pw_class[0]) pw->pw_fields |= _PWF_CLASS;
11728ca3091SGarrett Wollman 
118dea673e9SRodney W. Grimes 	if (!(p = strsep(&bp, ":")))			/* change */
119dea673e9SRodney W. Grimes 		goto fmt;
12028ca3091SGarrett Wollman 	if(p[0]) pw->pw_fields |= _PWF_CHANGE;
121dea673e9SRodney W. Grimes 	pw->pw_change = atol(p);
12228ca3091SGarrett Wollman 
123dea673e9SRodney W. Grimes 	if (!(p = strsep(&bp, ":")))			/* expire */
124dea673e9SRodney W. Grimes 		goto fmt;
12528ca3091SGarrett Wollman 	if(p[0]) pw->pw_fields |= _PWF_EXPIRE;
126dea673e9SRodney W. Grimes 	pw->pw_expire = atol(p);
12728ca3091SGarrett Wollman 
128cc6f6281SDavid Greenman 	if (!(pw->pw_gecos = strsep(&bp, ":")))		/* gecos */
129cc6f6281SDavid Greenman 		goto fmt;
13028ca3091SGarrett Wollman 	if(pw->pw_gecos[0]) pw->pw_fields |= _PWF_GECOS;
13128ca3091SGarrett Wollman 
132cc6f6281SDavid Greenman 	if (!(pw->pw_dir = strsep(&bp, ":")))			/* directory */
133cc6f6281SDavid Greenman 		goto fmt;
13428ca3091SGarrett Wollman 	if(pw->pw_dir[0]) pw->pw_fields |= _PWF_DIR;
13528ca3091SGarrett Wollman 
136dea673e9SRodney W. Grimes 	if (!(pw->pw_shell = strsep(&bp, ":")))		/* shell */
137dea673e9SRodney W. Grimes 		goto fmt;
138dea673e9SRodney W. Grimes 
139dea673e9SRodney W. Grimes 	p = pw->pw_shell;
140dea673e9SRodney W. Grimes 	if (root && *p)					/* empty == /bin/sh */
141dea673e9SRodney W. Grimes 		for (setusershell();;) {
142dea673e9SRodney W. Grimes 			if (!(sh = getusershell())) {
143dea673e9SRodney W. Grimes 				warnx("warning, unknown root shell");
144dea673e9SRodney W. Grimes 				break;
145dea673e9SRodney W. Grimes 			}
146dea673e9SRodney W. Grimes 			if (!strcmp(p, sh))
147dea673e9SRodney W. Grimes 				break;
148dea673e9SRodney W. Grimes 		}
14928ca3091SGarrett Wollman 	if(p[0]) pw->pw_fields |= _PWF_SHELL;
150dea673e9SRodney W. Grimes 
1518b76e1d7SPhilippe Charnier 	if ((p = strsep(&bp, ":"))) {			/* too many */
152dea673e9SRodney W. Grimes fmt:		warnx("corrupted entry");
153dea673e9SRodney W. Grimes 		return (0);
154dea673e9SRodney W. Grimes 	}
155dea673e9SRodney W. Grimes 	return (1);
156dea673e9SRodney W. Grimes }
157