]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libc/gen/pw_scan.c
libsys: fix sleep(3)/usleep(3) cancel behavior
[FreeBSD/FreeBSD.git] / lib / libc / gen / pw_scan.c
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1990, 1993, 1994
5  *      The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
18  *
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
29  * SUCH DAMAGE.
30  */
31
32 /*
33  * This module is used to "verify" password entries by chpass(1) and
34  * pwd_mkdb(8).
35  */
36
37 #include <sys/param.h>
38
39 #include <err.h>
40 #include <errno.h>
41 #include <pwd.h>
42 #include <string.h>
43 #include <stdlib.h>
44 #include <unistd.h>
45
46 #include "pw_scan.h"
47
48 /*
49  * Some software assumes that IDs are short.  We should emit warnings
50  * for id's which cannot be stored in a short, but we are more liberal
51  * by default, warning for IDs greater than USHRT_MAX.
52  *
53  * If pw_big_ids_warning is -1 on entry to pw_scan(), it will be set based
54  * on the existence of PW_SCAN_BIG_IDS in the environment.
55  *
56  * It is believed all baseline system software that can not handle the
57  * normal ID sizes is now gone so pw_big_ids_warning is disabled for now.
58  * But the code has been left in place in case end-users want to re-enable
59  * it and/or for the next time the ID sizes get bigger but pieces of the
60  * system lag behind.
61  */
62 static int      pw_big_ids_warning = 0;
63
64 void
65 __pw_initpwd(struct passwd *pwd)
66 {
67         static char nul[] = "";
68
69         memset(pwd, 0, sizeof(*pwd));
70         pwd->pw_uid = (uid_t)-1;  /* Considered least likely to lead to */
71         pwd->pw_gid = (gid_t)-1;  /* a security issue.                  */
72         pwd->pw_name = nul;
73         pwd->pw_passwd = nul;
74         pwd->pw_class = nul;
75         pwd->pw_gecos = nul;
76         pwd->pw_dir = nul;
77         pwd->pw_shell = nul;
78 }
79
80 int
81 __pw_scan(char *bp, struct passwd *pw, int flags)
82 {
83         uid_t id;
84         int root;
85         char *ep, *p, *sh;
86         unsigned long temp;
87
88         if (pw_big_ids_warning == -1)
89                 pw_big_ids_warning = getenv("PW_SCAN_BIG_IDS") == NULL ? 1 : 0;
90
91         pw->pw_fields = 0;
92         if (!(pw->pw_name = strsep(&bp, ":")))          /* login */
93                 goto fmt;
94         root = !strcmp(pw->pw_name, "root");
95         if (pw->pw_name[0] && (pw->pw_name[0] != '+' || pw->pw_name[1] == '\0'))
96                 pw->pw_fields |= _PWF_NAME;
97
98         if (!(pw->pw_passwd = strsep(&bp, ":")))        /* passwd */
99                 goto fmt;
100         if (pw->pw_passwd[0])
101                 pw->pw_fields |= _PWF_PASSWD;
102
103         if (!(p = strsep(&bp, ":")))                    /* uid */
104                 goto fmt;
105         if (p[0])
106                 pw->pw_fields |= _PWF_UID;
107         else {
108                 if (pw->pw_name[0] != '+' && pw->pw_name[0] != '-') {
109                         if (flags & _PWSCAN_WARN)
110                                 warnx("no uid for user %s", pw->pw_name);
111                         return (0);
112                 }
113         }
114         errno = 0;
115         temp = strtoul(p, &ep, 10);
116         if ((temp == ULONG_MAX && errno == ERANGE) || temp > UID_MAX) {
117                 if (flags & _PWSCAN_WARN)
118                         warnx("%s > max uid value (%u)", p, UID_MAX);
119                 return (0);
120         }
121         id = temp;
122         if (*ep != '\0') {
123                 if (flags & _PWSCAN_WARN)
124                         warnx("%s uid is incorrect", p);
125                 return (0);
126         }
127         if (root && id) {
128                 if (flags & _PWSCAN_WARN)
129                         warnx("root uid should be 0");
130                 return (0);
131         }
132         if (flags & _PWSCAN_WARN && pw_big_ids_warning && id > USHRT_MAX) {
133                 warnx("%s > recommended max uid value (%u)", p, USHRT_MAX);
134                 /*return (0);*/ /* THIS SHOULD NOT BE FATAL! */
135         }
136         pw->pw_uid = id;
137
138         if (!(p = strsep(&bp, ":")))                    /* gid */
139                 goto fmt;
140         if (p[0])
141                 pw->pw_fields |= _PWF_GID;
142         else {
143                 if (pw->pw_name[0] != '+' && pw->pw_name[0] != '-') {
144                         if (flags & _PWSCAN_WARN)
145                                 warnx("no gid for user %s", pw->pw_name);
146                         return (0);
147                 }
148         }
149         errno = 0;
150         temp = strtoul(p, &ep, 10);
151         if ((temp == ULONG_MAX && errno == ERANGE) || temp > GID_MAX) {
152                 if (flags & _PWSCAN_WARN)
153                         warnx("%s > max gid value (%u)", p, GID_MAX);
154                 return (0);
155         }
156         id = temp;
157         if (*ep != '\0') {
158                 if (flags & _PWSCAN_WARN)
159                         warnx("%s gid is incorrect", p);
160                 return (0);
161         }
162         if (flags & _PWSCAN_WARN && pw_big_ids_warning && id > USHRT_MAX) {
163                 warnx("%s > recommended max gid value (%u)", p, USHRT_MAX);
164                 /* return (0); This should not be fatal! */
165         }
166         pw->pw_gid = id;
167
168         if (flags & _PWSCAN_MASTER ) {
169                 if (!(pw->pw_class = strsep(&bp, ":"))) /* class */
170                         goto fmt;
171                 if (pw->pw_class[0])
172                         pw->pw_fields |= _PWF_CLASS;
173                 
174                 if (!(p = strsep(&bp, ":")))            /* change */
175                         goto fmt;
176                 if (p[0])
177                         pw->pw_fields |= _PWF_CHANGE;
178                 pw->pw_change = atol(p);
179                 
180                 if (!(p = strsep(&bp, ":")))            /* expire */
181                         goto fmt;
182                 if (p[0])
183                         pw->pw_fields |= _PWF_EXPIRE;
184                 pw->pw_expire = atol(p);
185         }
186         if (!(pw->pw_gecos = strsep(&bp, ":")))         /* gecos */
187                 goto fmt;
188         if (pw->pw_gecos[0])
189                 pw->pw_fields |= _PWF_GECOS;
190
191         if (!(pw->pw_dir = strsep(&bp, ":")))           /* directory */
192                 goto fmt;
193         if (pw->pw_dir[0])
194                 pw->pw_fields |= _PWF_DIR;
195
196         if (!(pw->pw_shell = strsep(&bp, ":")))         /* shell */
197                 goto fmt;
198
199         p = pw->pw_shell;
200         if (root && *p) {                               /* empty == /bin/sh */
201                 for (setusershell();;) {
202                         if (!(sh = getusershell())) {
203                                 if (flags & _PWSCAN_WARN)
204                                         warnx("warning, unknown root shell");
205                                 break;
206                         }
207                         if (!strcmp(p, sh))
208                                 break;
209                 }
210                 endusershell();
211         }
212         if (p[0])
213                 pw->pw_fields |= _PWF_SHELL;
214
215         if ((p = strsep(&bp, ":"))) {                   /* too many */
216 fmt:            
217                 if (flags & _PWSCAN_WARN)
218                         warnx("corrupted entry");
219                 return (0);
220         }
221         return (1);
222 }