]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - lib/libc/gen/pw_scan.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / lib / libc / gen / pw_scan.c
1 /*-
2  * Copyright (c) 1990, 1993, 1994
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
16  *
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
27  * SUCH DAMAGE.
28  */
29
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$");
35
36 /*
37  * This module is used to "verify" password entries by chpass(1) and
38  * pwd_mkdb(8).
39  */
40
41 #include <sys/param.h>
42
43 #include <err.h>
44 #include <errno.h>
45 #include <fcntl.h>
46 #include <pwd.h>
47 #include <stdio.h>
48 #include <string.h>
49 #include <stdlib.h>
50 #include <unistd.h>
51
52 #include "pw_scan.h"
53
54 /*
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.
58  *
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.
61  */
62 static int      pw_big_ids_warning = -1;
63
64 int
65 __pw_scan(char *bp, struct passwd *pw, int flags)
66 {
67         uid_t id;
68         int root;
69         char *ep, *p, *sh;
70
71         if (pw_big_ids_warning == -1)
72                 pw_big_ids_warning = getenv("PW_SCAN_BIG_IDS") == NULL ? 1 : 0;
73
74         pw->pw_fields = 0;
75         if (!(pw->pw_name = strsep(&bp, ":")))          /* login */
76                 goto fmt;
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;
80
81         if (!(pw->pw_passwd = strsep(&bp, ":")))        /* passwd */
82                 goto fmt;
83         if (pw->pw_passwd[0])
84                 pw->pw_fields |= _PWF_PASSWD;
85
86         if (!(p = strsep(&bp, ":")))                    /* uid */
87                 goto fmt;
88         if (p[0])
89                 pw->pw_fields |= _PWF_UID;
90         else {
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);
94                         return (0);
95                 }
96         }
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);
101                 return (0);
102         }
103         if (*ep != '\0') {
104                 if (flags & _PWSCAN_WARN)
105                         warnx("%s uid is incorrect", p);
106                 return (0);
107         }
108         if (root && id) {
109                 if (flags & _PWSCAN_WARN)
110                         warnx("root uid should be 0");
111                 return (0);
112         }
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! */
116         }
117         pw->pw_uid = id;
118
119         if (!(p = strsep(&bp, ":")))                    /* gid */
120                 goto fmt;
121         if (p[0])
122                 pw->pw_fields |= _PWF_GID;
123         else {
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);
127                         return (0);
128                 }
129         }
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);
134                 return (0);
135         }
136         if (*ep != '\0') {
137                 if (flags & _PWSCAN_WARN)
138                         warnx("%s gid is incorrect", p);
139                 return (0);
140         }
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! */
144         }
145         pw->pw_gid = id;
146
147         if (flags & _PWSCAN_MASTER ) {
148                 if (!(pw->pw_class = strsep(&bp, ":"))) /* class */
149                         goto fmt;
150                 if (pw->pw_class[0])
151                         pw->pw_fields |= _PWF_CLASS;
152                 
153                 if (!(p = strsep(&bp, ":")))            /* change */
154                         goto fmt;
155                 if (p[0])
156                         pw->pw_fields |= _PWF_CHANGE;
157                 pw->pw_change = atol(p);
158                 
159                 if (!(p = strsep(&bp, ":")))            /* expire */
160                         goto fmt;
161                 if (p[0])
162                         pw->pw_fields |= _PWF_EXPIRE;
163                 pw->pw_expire = atol(p);
164         }
165         if (!(pw->pw_gecos = strsep(&bp, ":")))         /* gecos */
166                 goto fmt;
167         if (pw->pw_gecos[0])
168                 pw->pw_fields |= _PWF_GECOS;
169
170         if (!(pw->pw_dir = strsep(&bp, ":")))           /* directory */
171                 goto fmt;
172         if (pw->pw_dir[0])
173                 pw->pw_fields |= _PWF_DIR;
174
175         if (!(pw->pw_shell = strsep(&bp, ":")))         /* shell */
176                 goto fmt;
177
178         p = pw->pw_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");
184                                 break;
185                         }
186                         if (!strcmp(p, sh))
187                                 break;
188                 }
189                 endusershell();
190         }
191         if (p[0])
192                 pw->pw_fields |= _PWF_SHELL;
193
194         if ((p = strsep(&bp, ":"))) {                   /* too many */
195 fmt:            
196                 if (flags & _PWSCAN_WARN)
197                         warnx("corrupted entry");
198                 return (0);
199         }
200         return (1);
201 }