2 * Copyright (c) 1990, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #if defined(LIBC_SCCS) && !defined(lint)
31 static char sccsid[] = "@(#)pw_scan.c 8.3 (Berkeley) 4/2/94";
32 #endif /* LIBC_SCCS and not lint */
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
37 * This module is used to "verify" password entries by chpass(1) and
41 #include <sys/param.h>
55 * Some software assumes that IDs are short. We should emit warnings
56 * for id's which cannot be stored in a short, but we are more liberal
57 * by default, warning for IDs greater than USHRT_MAX.
59 * If pw_big_ids_warning is -1 on entry to pw_scan(), it will be set based
60 * on the existence of PW_SCAN_BIG_IDS in the environment.
62 static int pw_big_ids_warning = -1;
65 __pw_scan(char *bp, struct passwd *pw, int flags)
71 if (pw_big_ids_warning == -1)
72 pw_big_ids_warning = getenv("PW_SCAN_BIG_IDS") == NULL ? 1 : 0;
75 if (!(pw->pw_name = strsep(&bp, ":"))) /* login */
77 root = !strcmp(pw->pw_name, "root");
78 if (pw->pw_name[0] && (pw->pw_name[0] != '+' || pw->pw_name[1] == '\0'))
79 pw->pw_fields |= _PWF_NAME;
81 if (!(pw->pw_passwd = strsep(&bp, ":"))) /* passwd */
84 pw->pw_fields |= _PWF_PASSWD;
86 if (!(p = strsep(&bp, ":"))) /* uid */
89 pw->pw_fields |= _PWF_UID;
91 if (pw->pw_name[0] != '+' && pw->pw_name[0] != '-') {
92 if (flags & _PWSCAN_WARN)
93 warnx("no uid for user %s", pw->pw_name);
97 id = strtoul(p, &ep, 10);
98 if (errno == ERANGE) {
99 if (flags & _PWSCAN_WARN)
100 warnx("%s > max uid value (%lu)", p, ULONG_MAX);
104 if (flags & _PWSCAN_WARN)
105 warnx("%s uid is incorrect", p);
109 if (flags & _PWSCAN_WARN)
110 warnx("root uid should be 0");
113 if (flags & _PWSCAN_WARN && pw_big_ids_warning && id > USHRT_MAX) {
114 warnx("%s > recommended max uid value (%u)", p, USHRT_MAX);
115 /*return (0);*/ /* THIS SHOULD NOT BE FATAL! */
119 if (!(p = strsep(&bp, ":"))) /* gid */
122 pw->pw_fields |= _PWF_GID;
124 if (pw->pw_name[0] != '+' && pw->pw_name[0] != '-') {
125 if (flags & _PWSCAN_WARN)
126 warnx("no gid for user %s", pw->pw_name);
130 id = strtoul(p, &ep, 10);
131 if (errno == ERANGE) {
132 if (flags & _PWSCAN_WARN)
133 warnx("%s > max gid value (%lu)", p, ULONG_MAX);
137 if (flags & _PWSCAN_WARN)
138 warnx("%s gid is incorrect", p);
141 if (flags & _PWSCAN_WARN && pw_big_ids_warning && id > USHRT_MAX) {
142 warnx("%s > recommended max gid value (%u)", p, USHRT_MAX);
143 /* return (0); This should not be fatal! */
147 if (flags & _PWSCAN_MASTER ) {
148 if (!(pw->pw_class = strsep(&bp, ":"))) /* class */
151 pw->pw_fields |= _PWF_CLASS;
153 if (!(p = strsep(&bp, ":"))) /* change */
156 pw->pw_fields |= _PWF_CHANGE;
157 pw->pw_change = atol(p);
159 if (!(p = strsep(&bp, ":"))) /* expire */
162 pw->pw_fields |= _PWF_EXPIRE;
163 pw->pw_expire = atol(p);
165 if (!(pw->pw_gecos = strsep(&bp, ":"))) /* gecos */
168 pw->pw_fields |= _PWF_GECOS;
170 if (!(pw->pw_dir = strsep(&bp, ":"))) /* directory */
173 pw->pw_fields |= _PWF_DIR;
175 if (!(pw->pw_shell = strsep(&bp, ":"))) /* shell */
179 if (root && *p) { /* empty == /bin/sh */
180 for (setusershell();;) {
181 if (!(sh = getusershell())) {
182 if (flags & _PWSCAN_WARN)
183 warnx("warning, unknown root shell");
192 pw->pw_fields |= _PWF_SHELL;
194 if ((p = strsep(&bp, ":"))) { /* too many */
196 if (flags & _PWSCAN_WARN)
197 warnx("corrupted entry");