2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1990, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
33 __SCCSID("@(#)pw_scan.c 8.3 (Berkeley) 4/2/94");
34 __FBSDID("$FreeBSD$");
37 * This module is used to "verify" password entries by chpass(1) and
41 #include <sys/param.h>
53 * Some software assumes that IDs are short. We should emit warnings
54 * for id's which cannot be stored in a short, but we are more liberal
55 * by default, warning for IDs greater than USHRT_MAX.
57 * If pw_big_ids_warning is -1 on entry to pw_scan(), it will be set based
58 * on the existence of PW_SCAN_BIG_IDS in the environment.
60 * It is believed all baseline system software that can not handle the
61 * normal ID sizes is now gone so pw_big_ids_warning is disabled for now.
62 * But the code has been left in place in case end-users want to re-enable
63 * it and/or for the next time the ID sizes get bigger but pieces of the
66 static int pw_big_ids_warning = 0;
69 __pw_initpwd(struct passwd *pwd)
71 static char nul[] = "";
73 memset(pwd, 0, sizeof(*pwd));
74 pwd->pw_uid = (uid_t)-1; /* Considered least likely to lead to */
75 pwd->pw_gid = (gid_t)-1; /* a security issue. */
85 __pw_scan(char *bp, struct passwd *pw, int flags)
92 if (pw_big_ids_warning == -1)
93 pw_big_ids_warning = getenv("PW_SCAN_BIG_IDS") == NULL ? 1 : 0;
96 if (!(pw->pw_name = strsep(&bp, ":"))) /* login */
98 root = !strcmp(pw->pw_name, "root");
99 if (pw->pw_name[0] && (pw->pw_name[0] != '+' || pw->pw_name[1] == '\0'))
100 pw->pw_fields |= _PWF_NAME;
102 if (!(pw->pw_passwd = strsep(&bp, ":"))) /* passwd */
104 if (pw->pw_passwd[0])
105 pw->pw_fields |= _PWF_PASSWD;
107 if (!(p = strsep(&bp, ":"))) /* uid */
110 pw->pw_fields |= _PWF_UID;
112 if (pw->pw_name[0] != '+' && pw->pw_name[0] != '-') {
113 if (flags & _PWSCAN_WARN)
114 warnx("no uid for user %s", pw->pw_name);
119 temp = strtoul(p, &ep, 10);
120 if ((temp == ULONG_MAX && errno == ERANGE) || temp > UID_MAX) {
121 if (flags & _PWSCAN_WARN)
122 warnx("%s > max uid value (%u)", p, UID_MAX);
127 if (flags & _PWSCAN_WARN)
128 warnx("%s uid is incorrect", p);
132 if (flags & _PWSCAN_WARN)
133 warnx("root uid should be 0");
136 if (flags & _PWSCAN_WARN && pw_big_ids_warning && id > USHRT_MAX) {
137 warnx("%s > recommended max uid value (%u)", p, USHRT_MAX);
138 /*return (0);*/ /* THIS SHOULD NOT BE FATAL! */
142 if (!(p = strsep(&bp, ":"))) /* gid */
145 pw->pw_fields |= _PWF_GID;
147 if (pw->pw_name[0] != '+' && pw->pw_name[0] != '-') {
148 if (flags & _PWSCAN_WARN)
149 warnx("no gid for user %s", pw->pw_name);
154 temp = strtoul(p, &ep, 10);
155 if ((temp == ULONG_MAX && errno == ERANGE) || temp > GID_MAX) {
156 if (flags & _PWSCAN_WARN)
157 warnx("%s > max gid value (%u)", p, GID_MAX);
162 if (flags & _PWSCAN_WARN)
163 warnx("%s gid is incorrect", p);
166 if (flags & _PWSCAN_WARN && pw_big_ids_warning && id > USHRT_MAX) {
167 warnx("%s > recommended max gid value (%u)", p, USHRT_MAX);
168 /* return (0); This should not be fatal! */
172 if (flags & _PWSCAN_MASTER ) {
173 if (!(pw->pw_class = strsep(&bp, ":"))) /* class */
176 pw->pw_fields |= _PWF_CLASS;
178 if (!(p = strsep(&bp, ":"))) /* change */
181 pw->pw_fields |= _PWF_CHANGE;
182 pw->pw_change = atol(p);
184 if (!(p = strsep(&bp, ":"))) /* expire */
187 pw->pw_fields |= _PWF_EXPIRE;
188 pw->pw_expire = atol(p);
190 if (!(pw->pw_gecos = strsep(&bp, ":"))) /* gecos */
193 pw->pw_fields |= _PWF_GECOS;
195 if (!(pw->pw_dir = strsep(&bp, ":"))) /* directory */
198 pw->pw_fields |= _PWF_DIR;
200 if (!(pw->pw_shell = strsep(&bp, ":"))) /* shell */
204 if (root && *p) { /* empty == /bin/sh */
205 for (setusershell();;) {
206 if (!(sh = getusershell())) {
207 if (flags & _PWSCAN_WARN)
208 warnx("warning, unknown root shell");
217 pw->pw_fields |= _PWF_SHELL;
219 if ((p = strsep(&bp, ":"))) { /* too many */
221 if (flags & _PWSCAN_WARN)
222 warnx("corrupted entry");