getpwent.c revision 1.11 1 1.11 he /* $NetBSD: getpwent.c,v 1.11 2009/01/16 14:48:04 he Exp $ */
2 1.1 gwr
3 1.1 gwr /*
4 1.5 lukem * Copyright (c) 1987, 1988, 1989, 1993, 1994, 1995
5 1.5 lukem * The Regents of the University of California. All rights reserved.
6 1.1 gwr *
7 1.1 gwr * Redistribution and use in source and binary forms, with or without
8 1.1 gwr * modification, are permitted provided that the following conditions
9 1.1 gwr * are met:
10 1.1 gwr * 1. Redistributions of source code must retain the above copyright
11 1.1 gwr * notice, this list of conditions and the following disclaimer.
12 1.1 gwr * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 gwr * notice, this list of conditions and the following disclaimer in the
14 1.1 gwr * documentation and/or other materials provided with the distribution.
15 1.7 agc * 3. Neither the name of the University nor the names of its contributors
16 1.5 lukem * may be used to endorse or promote products derived from this software
17 1.5 lukem * without specific prior written permission.
18 1.1 gwr *
19 1.5 lukem * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.5 lukem * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.5 lukem * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.5 lukem * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.5 lukem * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.5 lukem * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.5 lukem * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.5 lukem * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.5 lukem * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.5 lukem * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.5 lukem * SUCH DAMAGE.
30 1.1 gwr */
31 1.1 gwr
32 1.1 gwr /*
33 1.5 lukem * Copied from: lib/libc/gen/getpwent.c
34 1.5 lukem * NetBSD: getpwent.c,v 1.48 2000/10/03 03:22:26 enami Exp
35 1.5 lukem * and then gutted, leaving only /etc/master.passwd support.
36 1.1 gwr */
37 1.1 gwr
38 1.4 tsutsui #include <sys/cdefs.h>
39 1.4 tsutsui
40 1.4 tsutsui #ifdef __weak_alias
41 1.4 tsutsui #define endpwent _endpwent
42 1.4 tsutsui #define getpwent _getpwent
43 1.10 sborrill #define getpwent_r _getpwent_r
44 1.5 lukem #define getpwuid _getpwuid
45 1.4 tsutsui #define getpwnam _getpwnam
46 1.5 lukem #define setpwent _setpwent
47 1.4 tsutsui #define setpassent _setpassent
48 1.9 he #define getpwuid_r _getpwuid_r
49 1.9 he #define getpwnam_r _getpwnam_r
50 1.4 tsutsui
51 1.4 tsutsui __weak_alias(endpwent,_endpwent)
52 1.4 tsutsui __weak_alias(getpwent,_getpwent)
53 1.10 sborrill __weak_alias(getpwent_r,_getpwent_r)
54 1.5 lukem __weak_alias(getpwuid,_getpwuid)
55 1.4 tsutsui __weak_alias(getpwnam,_getpwnam)
56 1.5 lukem __weak_alias(setpwent,_setpwent)
57 1.4 tsutsui __weak_alias(setpassent,_setpassent)
58 1.9 he __weak_alias(getpwuid_r,_getpwuid_r)
59 1.9 he __weak_alias(getpwnam_r,_getpwnam_r)
60 1.11 he
61 1.11 he __weak_alias(__getpwent50,_getpwent)
62 1.11 he __weak_alias(__getpwent_r50,_getpwent_r)
63 1.11 he __weak_alias(__getpwuid50,_getpwuid)
64 1.11 he __weak_alias(__getpwnam50,_getpwnam)
65 1.11 he __weak_alias(__getpwuid_r50,_getpwuid_r)
66 1.11 he __weak_alias(__getpwnam_r50,_getpwnam_r)
67 1.4 tsutsui #endif
68 1.1 gwr
69 1.5 lukem #include <sys/param.h>
70 1.5 lukem
71 1.5 lukem #include <limits.h>
72 1.5 lukem #include <pwd.h>
73 1.5 lukem #include <stdlib.h>
74 1.5 lukem #include <stdio.h>
75 1.5 lukem #include <string.h>
76 1.5 lukem
77 1.5 lukem static int pwstart(void);
78 1.8 christos static int pwscan(int, uid_t, const char *, struct passwd *,
79 1.8 christos char *, size_t);
80 1.8 christos static int pwmatchline(int, uid_t, const char *, struct passwd *,
81 1.8 christos char *);
82 1.5 lukem
83 1.5 lukem static FILE *_pw_fp;
84 1.5 lukem static struct passwd _pw_passwd; /* password structure */
85 1.5 lukem static int _pw_stayopen; /* keep fd's open */
86 1.5 lukem static int _pw_filesdone;
87 1.1 gwr
88 1.5 lukem #define MAXLINELENGTH 1024
89 1.5 lukem
90 1.5 lukem static char pwline[MAXLINELENGTH];
91 1.1 gwr
92 1.1 gwr struct passwd *
93 1.5 lukem getpwent(void)
94 1.1 gwr {
95 1.1 gwr
96 1.8 christos if ((!_pw_fp && !pwstart()) ||
97 1.8 christos !pwscan(0, 0, NULL, &_pw_passwd, pwline, sizeof(pwline)))
98 1.5 lukem return (NULL);
99 1.5 lukem return (&_pw_passwd);
100 1.5 lukem }
101 1.1 gwr
102 1.10 sborrill int
103 1.10 sborrill getpwent_r(struct passwd *pwres, char *buf, size_t bufsiz,
104 1.10 sborrill struct passwd **pwd)
105 1.10 sborrill {
106 1.10 sborrill int rval;
107 1.10 sborrill
108 1.10 sborrill if (!_pw_fp && !pwstart())
109 1.10 sborrill return 1;
110 1.10 sborrill rval = !pwscan(0, 0, NULL, pwres, buf, bufsiz);
111 1.10 sborrill if (rval)
112 1.10 sborrill *pwd = NULL;
113 1.10 sborrill else
114 1.10 sborrill *pwd = pwres;
115 1.10 sborrill return rval;
116 1.10 sborrill }
117 1.10 sborrill
118 1.5 lukem struct passwd *
119 1.5 lukem getpwnam(const char *name)
120 1.5 lukem {
121 1.8 christos struct passwd *pwd;
122 1.8 christos return getpwnam_r(name, &_pw_passwd, pwline, sizeof(pwline),
123 1.8 christos &pwd) == 0 ? pwd : NULL;
124 1.8 christos }
125 1.8 christos
126 1.8 christos int
127 1.8 christos getpwnam_r(const char *name, struct passwd *pwres, char *buf, size_t bufsiz,
128 1.8 christos struct passwd **pwd)
129 1.8 christos {
130 1.5 lukem int rval;
131 1.5 lukem
132 1.5 lukem if (!pwstart())
133 1.8 christos return 1;
134 1.8 christos rval = !pwscan(1, 0, name, pwres, buf, bufsiz);
135 1.5 lukem if (!_pw_stayopen)
136 1.5 lukem endpwent();
137 1.8 christos if (rval)
138 1.8 christos *pwd = NULL;
139 1.8 christos else
140 1.8 christos *pwd = pwres;
141 1.8 christos return rval;
142 1.1 gwr }
143 1.1 gwr
144 1.5 lukem struct passwd *
145 1.5 lukem getpwuid(uid_t uid)
146 1.1 gwr {
147 1.8 christos struct passwd *pwd;
148 1.8 christos return getpwuid_r(uid, &_pw_passwd, pwline, sizeof(pwline),
149 1.8 christos &pwd) == 0 ? pwd : NULL;
150 1.8 christos }
151 1.8 christos
152 1.8 christos int
153 1.8 christos getpwuid_r(uid_t uid, struct passwd *pwres, char *buf, size_t bufsiz,
154 1.8 christos struct passwd **pwd)
155 1.8 christos {
156 1.5 lukem int rval;
157 1.5 lukem
158 1.5 lukem if (!pwstart())
159 1.8 christos return 1;
160 1.8 christos rval = !pwscan(1, uid, NULL, pwres, buf, bufsiz);
161 1.5 lukem if (!_pw_stayopen)
162 1.5 lukem endpwent();
163 1.8 christos if (rval)
164 1.8 christos *pwd = NULL;
165 1.8 christos else
166 1.8 christos *pwd = pwres;
167 1.8 christos return rval;
168 1.1 gwr }
169 1.1 gwr
170 1.1 gwr void
171 1.5 lukem setpwent(void)
172 1.1 gwr {
173 1.5 lukem
174 1.1 gwr (void) setpassent(0);
175 1.1 gwr }
176 1.1 gwr
177 1.5 lukem int
178 1.5 lukem setpassent(int stayopen)
179 1.5 lukem {
180 1.5 lukem
181 1.5 lukem if (!pwstart())
182 1.5 lukem return 0;
183 1.5 lukem _pw_stayopen = stayopen;
184 1.5 lukem return 1;
185 1.5 lukem }
186 1.5 lukem
187 1.1 gwr void
188 1.5 lukem endpwent(void)
189 1.1 gwr {
190 1.5 lukem
191 1.5 lukem _pw_filesdone = 0;
192 1.5 lukem if (_pw_fp) {
193 1.5 lukem (void)fclose(_pw_fp);
194 1.5 lukem _pw_fp = NULL;
195 1.2 gwr }
196 1.1 gwr }
197 1.1 gwr
198 1.5 lukem static int
199 1.5 lukem pwstart(void)
200 1.1 gwr {
201 1.1 gwr
202 1.5 lukem _pw_filesdone = 0;
203 1.5 lukem if (_pw_fp) {
204 1.5 lukem rewind(_pw_fp);
205 1.5 lukem return 1;
206 1.5 lukem }
207 1.5 lukem return (_pw_fp = fopen(_PATH_MASTERPASSWD, "r")) ? 1 : 0;
208 1.5 lukem }
209 1.1 gwr
210 1.1 gwr
211 1.5 lukem static int
212 1.8 christos pwscan(int search, uid_t uid, const char *name, struct passwd *pwd, char *buf,
213 1.8 christos size_t bufsiz)
214 1.1 gwr {
215 1.1 gwr
216 1.5 lukem if (_pw_filesdone)
217 1.5 lukem return 0;
218 1.5 lukem for (;;) {
219 1.8 christos if (!fgets(buf, bufsiz, _pw_fp)) {
220 1.5 lukem if (!search)
221 1.5 lukem _pw_filesdone = 1;
222 1.5 lukem return 0;
223 1.5 lukem }
224 1.5 lukem /* skip lines that are too big */
225 1.8 christos if (!strchr(buf, '\n')) {
226 1.5 lukem int ch;
227 1.5 lukem
228 1.5 lukem while ((ch = getc(_pw_fp)) != '\n' && ch != EOF)
229 1.5 lukem ;
230 1.5 lukem continue;
231 1.5 lukem }
232 1.8 christos if (pwmatchline(search, uid, name, pwd, buf))
233 1.5 lukem return 1;
234 1.5 lukem }
235 1.5 lukem /* NOTREACHED */
236 1.5 lukem }
237 1.1 gwr
238 1.5 lukem static int
239 1.8 christos pwmatchline(int search, uid_t uid, const char *name, struct passwd *pwd,
240 1.8 christos char *buf)
241 1.5 lukem {
242 1.5 lukem unsigned long id;
243 1.5 lukem char *cp, *bp, *ep;
244 1.1 gwr
245 1.5 lukem /* name may be NULL if search is nonzero */
246 1.5 lukem
247 1.8 christos bp = buf;
248 1.8 christos memset(pwd, 0, sizeof(*pwd));
249 1.8 christos pwd->pw_name = strsep(&bp, ":\n"); /* name */
250 1.8 christos if (search && name && strcmp(pwd->pw_name, name))
251 1.5 lukem return 0;
252 1.5 lukem
253 1.8 christos pwd->pw_passwd = strsep(&bp, ":\n"); /* passwd */
254 1.5 lukem
255 1.5 lukem if (!(cp = strsep(&bp, ":\n"))) /* uid */
256 1.5 lukem return 0;
257 1.5 lukem id = strtoul(cp, &ep, 10);
258 1.5 lukem if (id > UID_MAX || *ep != '\0')
259 1.5 lukem return 0;
260 1.8 christos pwd->pw_uid = (uid_t)id;
261 1.8 christos if (search && name == NULL && pwd->pw_uid != uid)
262 1.5 lukem return 0;
263 1.5 lukem
264 1.5 lukem if (!(cp = strsep(&bp, ":\n"))) /* gid */
265 1.5 lukem return 0;
266 1.5 lukem id = strtoul(cp, &ep, 10);
267 1.5 lukem if (id > GID_MAX || *ep != '\0')
268 1.5 lukem return 0;
269 1.8 christos pwd->pw_gid = (gid_t)id;
270 1.5 lukem
271 1.8 christos if (!(pwd->pw_class = strsep(&bp, ":"))) /* class */
272 1.5 lukem return 0;
273 1.5 lukem if (!(ep = strsep(&bp, ":"))) /* change */
274 1.5 lukem return 0;
275 1.5 lukem if (!(ep = strsep(&bp, ":"))) /* expire */
276 1.5 lukem return 0;
277 1.5 lukem
278 1.8 christos if (!(pwd->pw_gecos = strsep(&bp, ":\n"))) /* gecos */
279 1.5 lukem return 0;
280 1.8 christos if (!(pwd->pw_dir = strsep(&bp, ":\n"))) /* directory */
281 1.5 lukem return 0;
282 1.8 christos if (!(pwd->pw_shell = strsep(&bp, ":\n"))) /* shell */
283 1.5 lukem return 0;
284 1.1 gwr
285 1.5 lukem if (strchr(bp, ':') != NULL)
286 1.5 lukem return 0;
287 1.1 gwr
288 1.5 lukem return 1;
289 1.1 gwr }
290