]>
git.cameronkatri.com Git - pw-darwin.git/blob - libc/gen/pw_scan.c
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 #if defined(LIBC_SCCS) && !defined(lint)
33 static char sccsid
[] = "@(#)pw_scan.c 8.3 (Berkeley) 4/2/94";
34 #endif /* LIBC_SCCS and not lint */
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
39 * This module is used to "verify" password entries by chpass(1) and
43 #include <sys/param.h>
57 * Some software assumes that IDs are short. We should emit warnings
58 * for id's which cannot be stored in a short, but we are more liberal
59 * by default, warning for IDs greater than USHRT_MAX.
61 * If pw_big_ids_warning is -1 on entry to pw_scan(), it will be set based
62 * on the existence of PW_SCAN_BIG_IDS in the environment.
64 * It is believed all baseline system software that can not handle the
65 * normal ID sizes is now gone so pw_big_ids_warning is disabled for now.
66 * But the code has been left in place in case end-users want to re-enable
67 * it and/or for the next time the ID sizes get bigger but pieces of the
70 static int pw_big_ids_warning
= 0;
73 __pw_scan(char *bp
, struct passwd
*pw
, int flags
)
80 if (pw_big_ids_warning
== -1)
81 pw_big_ids_warning
= getenv("PW_SCAN_BIG_IDS") == NULL
? 1 : 0;
84 if (!(pw
->pw_name
= strsep(&bp
, ":"))) /* login */
86 root
= !strcmp(pw
->pw_name
, "root");
87 if (pw
->pw_name
[0] && (pw
->pw_name
[0] != '+' || pw
->pw_name
[1] == '\0'))
88 pw
->pw_fields
|= _PWF_NAME
;
90 if (!(pw
->pw_passwd
= strsep(&bp
, ":"))) /* passwd */
93 pw
->pw_fields
|= _PWF_PASSWD
;
95 if (!(p
= strsep(&bp
, ":"))) /* uid */
98 pw
->pw_fields
|= _PWF_UID
;
100 if (pw
->pw_name
[0] != '+' && pw
->pw_name
[0] != '-') {
101 if (flags
& _PWSCAN_WARN
)
102 warnx("no uid for user %s", pw
->pw_name
);
107 temp
= strtoul(p
, &ep
, 10);
108 if ((temp
== ULONG_MAX
&& errno
== ERANGE
) || temp
> UID_MAX
) {
109 if (flags
& _PWSCAN_WARN
)
110 warnx("%s > max uid value (%u)", p
, UID_MAX
);
115 if (flags
& _PWSCAN_WARN
)
116 warnx("%s uid is incorrect", p
);
120 if (flags
& _PWSCAN_WARN
)
121 warnx("root uid should be 0");
124 if (flags
& _PWSCAN_WARN
&& pw_big_ids_warning
&& id
> USHRT_MAX
) {
125 warnx("%s > recommended max uid value (%u)", p
, USHRT_MAX
);
126 /*return (0);*/ /* THIS SHOULD NOT BE FATAL! */
130 if (!(p
= strsep(&bp
, ":"))) /* gid */
133 pw
->pw_fields
|= _PWF_GID
;
135 if (pw
->pw_name
[0] != '+' && pw
->pw_name
[0] != '-') {
136 if (flags
& _PWSCAN_WARN
)
137 warnx("no gid for user %s", pw
->pw_name
);
142 temp
= strtoul(p
, &ep
, 10);
143 if ((temp
== ULONG_MAX
&& errno
== ERANGE
) || temp
> GID_MAX
) {
144 if (flags
& _PWSCAN_WARN
)
145 warnx("%s > max gid value (%u)", p
, GID_MAX
);
150 if (flags
& _PWSCAN_WARN
)
151 warnx("%s gid is incorrect", p
);
154 if (flags
& _PWSCAN_WARN
&& pw_big_ids_warning
&& id
> USHRT_MAX
) {
155 warnx("%s > recommended max gid value (%u)", p
, USHRT_MAX
);
156 /* return (0); This should not be fatal! */
160 if (flags
& _PWSCAN_MASTER
) {
161 if (!(pw
->pw_class
= strsep(&bp
, ":"))) /* class */
164 pw
->pw_fields
|= _PWF_CLASS
;
166 if (!(p
= strsep(&bp
, ":"))) /* change */
169 pw
->pw_fields
|= _PWF_CHANGE
;
170 pw
->pw_change
= atol(p
);
172 if (!(p
= strsep(&bp
, ":"))) /* expire */
175 pw
->pw_fields
|= _PWF_EXPIRE
;
176 pw
->pw_expire
= atol(p
);
178 if (!(pw
->pw_gecos
= strsep(&bp
, ":"))) /* gecos */
181 pw
->pw_fields
|= _PWF_GECOS
;
183 if (!(pw
->pw_dir
= strsep(&bp
, ":"))) /* directory */
186 pw
->pw_fields
|= _PWF_DIR
;
188 if (!(pw
->pw_shell
= strsep(&bp
, ":"))) /* shell */
192 if (root
&& *p
) { /* empty == /bin/sh */
193 for (setusershell();;) {
194 if (!(sh
= getusershell())) {
195 if (flags
& _PWSCAN_WARN
)
196 warnx("warning, unknown root shell");
205 pw
->pw_fields
|= _PWF_SHELL
;
207 if ((p
= strsep(&bp
, ":"))) { /* too many */
209 if (flags
& _PWSCAN_WARN
)
210 warnx("corrupted entry");