pw_scan.c revision 1.11 1 /* $NetBSD: pw_scan.c,v 1.11 2002/01/29 10:20:30 tv Exp $ */
2
3 /*
4 * Copyright (c) 1987, 1993, 1994, 1995
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. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #if HAVE_CONFIG_H
37 #include "config.h"
38 #include "compat_pwd.h"
39 #else
40 #include <sys/cdefs.h>
41 #if defined(LIBC_SCCS) && !defined(lint)
42 __RCSID("$NetBSD: pw_scan.c,v 1.11 2002/01/29 10:20:30 tv Exp $");
43 #endif /* LIBC_SCCS and not lint */
44
45 #if defined(_LIBC)
46 #include "namespace.h"
47 #endif
48 #include <sys/types.h>
49
50 #include <assert.h>
51 #include <err.h>
52 #include <limits.h>
53 #include <pwd.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <unistd.h>
58
59 #ifdef _LIBC
60 #include "pw_private.h"
61 #endif
62 #endif /* !HAVE_CONFIG_H */
63
64 int
65 #ifdef _LIBC
66 __pw_scan(bp, pw, flags)
67 #else
68 pw_scan(bp, pw, flags)
69 #endif
70 char *bp;
71 struct passwd *pw;
72 int *flags;
73 {
74 unsigned long id;
75 int root, inflags;
76 char *ep;
77 const char *p, *sh;
78
79 _DIAGASSERT(bp != NULL);
80 _DIAGASSERT(pw != NULL);
81
82 inflags = 0;
83 if (flags != (int *)NULL) {
84 inflags = *flags;
85 *flags = 0;
86 }
87
88 if (!(pw->pw_name = strsep(&bp, ":"))) /* login */
89 goto fmt;
90 root = !strcmp(pw->pw_name, "root");
91
92 if (!(pw->pw_passwd = strsep(&bp, ":"))) /* passwd */
93 goto fmt;
94
95 if (!(p = strsep(&bp, ":"))) /* uid */
96 goto fmt;
97 id = strtoul(p, &ep, 10);
98 if (root && id) {
99 if (!(inflags & _PASSWORD_NOWARN))
100 warnx("root uid should be 0");
101 return (0);
102 }
103 if (id > UID_MAX || *ep != '\0') {
104 if (!(inflags & _PASSWORD_NOWARN))
105 warnx("invalid uid '%s'", p);
106 return (0);
107 }
108 pw->pw_uid = (uid_t)id;
109 if ((*p == '\0') && (flags != (int *)NULL))
110 *flags |= _PASSWORD_NOUID;
111
112 if (!(p = strsep(&bp, ":"))) /* gid */
113 goto fmt;
114 id = strtoul(p, &ep, 10);
115 if (id > GID_MAX || *ep != '\0') {
116 if (!(inflags & _PASSWORD_NOWARN))
117 warnx("invalid gid '%s'", p);
118 return (0);
119 }
120 pw->pw_gid = (gid_t)id;
121 if ((*p == '\0') && (flags != (int *)NULL))
122 *flags |= _PASSWORD_NOGID;
123
124 if (inflags & _PASSWORD_OLDFMT) {
125 pw->pw_class = "";
126 pw->pw_change = 0;
127 pw->pw_expire = 0;
128 *flags |= (_PASSWORD_NOCHG | _PASSWORD_NOEXP);
129 } else {
130 pw->pw_class = strsep(&bp, ":"); /* class */
131 if (!(p = strsep(&bp, ":"))) /* change */
132 goto fmt;
133 pw->pw_change = atol(p);
134 if ((*p == '\0') && (flags != (int *)NULL))
135 *flags |= _PASSWORD_NOCHG;
136 if (!(p = strsep(&bp, ":"))) /* expire */
137 goto fmt;
138 pw->pw_expire = atol(p);
139 if ((*p == '\0') && (flags != (int *)NULL))
140 *flags |= _PASSWORD_NOEXP;
141 }
142 pw->pw_gecos = strsep(&bp, ":"); /* gecos */
143 pw->pw_dir = strsep(&bp, ":"); /* directory */
144 if (!(pw->pw_shell = strsep(&bp, ":"))) /* shell */
145 goto fmt;
146
147 #if !HAVE_CONFIG_H
148 p = pw->pw_shell;
149 if (root && *p) /* empty == /bin/sh */
150 for (setusershell();;) {
151 if (!(sh = getusershell())) {
152 if (!(inflags & _PASSWORD_NOWARN))
153 warnx("warning, unknown root shell");
154 break;
155 }
156 if (!strcmp(p, sh))
157 break;
158 }
159 #endif
160
161 if ((p = strsep(&bp, ":")) != NULL) { /* too many */
162 fmt:
163 if (!(inflags & _PASSWORD_NOWARN))
164 warnx("corrupted entry");
165 return (0);
166 }
167
168 return (1);
169 }
170