getpwent.c revision 1.4 1 1.1 cgd /*
2 1.1 cgd * Copyright (c) 1988 The Regents of the University of California.
3 1.1 cgd * All rights reserved.
4 1.1 cgd *
5 1.1 cgd * Redistribution and use in source and binary forms, with or without
6 1.1 cgd * modification, are permitted provided that the following conditions
7 1.1 cgd * are met:
8 1.1 cgd * 1. Redistributions of source code must retain the above copyright
9 1.1 cgd * notice, this list of conditions and the following disclaimer.
10 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer in the
12 1.1 cgd * documentation and/or other materials provided with the distribution.
13 1.1 cgd * 3. All advertising materials mentioning features or use of this software
14 1.1 cgd * must display the following acknowledgement:
15 1.1 cgd * This product includes software developed by the University of
16 1.1 cgd * California, Berkeley and its contributors.
17 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
18 1.1 cgd * may be used to endorse or promote products derived from this software
19 1.1 cgd * without specific prior written permission.
20 1.1 cgd *
21 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 1.1 cgd * SUCH DAMAGE.
32 1.1 cgd */
33 1.1 cgd
34 1.1 cgd #if defined(LIBC_SCCS) && !defined(lint)
35 1.1 cgd static char sccsid[] = "@(#)getpwent.c 5.21 (Berkeley) 3/14/91";
36 1.1 cgd #endif /* LIBC_SCCS and not lint */
37 1.1 cgd
38 1.1 cgd #include <sys/param.h>
39 1.1 cgd #include <fcntl.h>
40 1.1 cgd #include <db.h>
41 1.1 cgd #include <syslog.h>
42 1.1 cgd #include <pwd.h>
43 1.1 cgd #include <utmp.h>
44 1.1 cgd #include <errno.h>
45 1.1 cgd #include <unistd.h>
46 1.1 cgd #include <stdlib.h>
47 1.1 cgd #include <string.h>
48 1.1 cgd #include <limits.h>
49 1.4 deraadt #ifdef YP
50 1.4 deraadt #include <stdio.h>
51 1.4 deraadt #include <rpc/rpc.h>
52 1.4 deraadt #include <rpcsvc/yp_prot.h>
53 1.4 deraadt #include <rpcsvc/ypclnt.h>
54 1.4 deraadt #endif
55 1.1 cgd
56 1.1 cgd static struct passwd _pw_passwd; /* password structure */
57 1.1 cgd static DB *_pw_db; /* password database */
58 1.1 cgd static int _pw_keynum; /* key counter */
59 1.1 cgd static int _pw_stayopen; /* keep fd's open */
60 1.1 cgd static int __hashpw(), __initdb();
61 1.1 cgd
62 1.4 deraadt #ifdef YP
63 1.4 deraadt static char *__ypcurrent, *__ypdomain;
64 1.4 deraadt static int __ypcurrentlen, __ypmode=0;
65 1.4 deraadt static char line[1024];
66 1.4 deraadt
67 1.4 deraadt int
68 1.4 deraadt __ypparse(pw, s, l)
69 1.4 deraadt struct passwd *pw;
70 1.4 deraadt char *s;
71 1.4 deraadt int l;
72 1.4 deraadt {
73 1.4 deraadt char *bp, *cp;
74 1.4 deraadt
75 1.4 deraadt bp = s;
76 1.4 deraadt pw->pw_name = strsep(&bp, ":\n");
77 1.4 deraadt pw->pw_passwd = strsep(&bp, ":\n");
78 1.4 deraadt if (!(cp = strsep(&bp, ":\n")))
79 1.4 deraadt return 1;
80 1.4 deraadt pw->pw_uid = atoi(cp);
81 1.4 deraadt if (!(cp = strsep(&bp, ":\n")))
82 1.4 deraadt return 0;
83 1.4 deraadt pw->pw_gid = atoi(cp);
84 1.4 deraadt pw->pw_change = 0;
85 1.4 deraadt pw->pw_class = "";
86 1.4 deraadt pw->pw_gecos = strsep(&bp, ":\n");
87 1.4 deraadt pw->pw_dir = strsep(&bp, ":\n");
88 1.4 deraadt pw->pw_shell = strsep(&bp, ":\n");
89 1.4 deraadt pw->pw_expire = 0;
90 1.4 deraadt return 0;
91 1.4 deraadt }
92 1.4 deraadt #endif
93 1.4 deraadt
94 1.1 cgd struct passwd *
95 1.1 cgd getpwent()
96 1.1 cgd {
97 1.1 cgd DBT key;
98 1.1 cgd char bf[sizeof(_pw_keynum) + 1];
99 1.4 deraadt #ifdef YP
100 1.4 deraadt char *bp, *cp;
101 1.4 deraadt #endif
102 1.1 cgd
103 1.1 cgd if (!_pw_db && !__initdb())
104 1.1 cgd return((struct passwd *)NULL);
105 1.1 cgd
106 1.4 deraadt #ifdef YP
107 1.4 deraadt again:
108 1.4 deraadt if(__ypmode) {
109 1.4 deraadt char *key, *data;
110 1.4 deraadt int keylen, datalen;
111 1.4 deraadt int r;
112 1.4 deraadt
113 1.4 deraadt if(!__ypdomain) {
114 1.4 deraadt if( _yp_check(&__ypdomain) == 0) {
115 1.4 deraadt __ypmode = 0;
116 1.4 deraadt goto again;
117 1.4 deraadt }
118 1.4 deraadt }
119 1.4 deraadt if(__ypcurrent) {
120 1.4 deraadt r = yp_next(__ypdomain, "passwd.byname",
121 1.4 deraadt __ypcurrent, __ypcurrentlen,
122 1.4 deraadt &key, &keylen, &data, &datalen);
123 1.4 deraadt free(__ypcurrent);
124 1.4 deraadt __ypcurrent = NULL;
125 1.4 deraadt /*printf("yp_next %d\n", r);*/
126 1.4 deraadt switch(r) {
127 1.4 deraadt case 0:
128 1.4 deraadt break;
129 1.4 deraadt default:
130 1.4 deraadt __ypcurrent = NULL;
131 1.4 deraadt __ypmode = 0;
132 1.4 deraadt free(data);
133 1.4 deraadt data = NULL;
134 1.4 deraadt goto again;
135 1.4 deraadt }
136 1.4 deraadt __ypcurrent = key;
137 1.4 deraadt __ypcurrentlen = keylen;
138 1.4 deraadt bcopy(data, line, datalen);
139 1.4 deraadt free(data);
140 1.4 deraadt data = NULL;
141 1.4 deraadt } else {
142 1.4 deraadt r = yp_first(__ypdomain, "passwd.byname",
143 1.4 deraadt &__ypcurrent, &__ypcurrentlen,
144 1.4 deraadt &data, &datalen);
145 1.4 deraadt /*printf("yp_first %d\n", r);*/
146 1.4 deraadt switch(r) {
147 1.4 deraadt case 0:
148 1.4 deraadt break;
149 1.4 deraadt default:
150 1.4 deraadt __ypmode = 0;
151 1.4 deraadt free(data);
152 1.4 deraadt goto again;
153 1.4 deraadt }
154 1.4 deraadt bcopy(data, line, datalen);
155 1.4 deraadt free(data);
156 1.4 deraadt data = NULL;
157 1.4 deraadt }
158 1.4 deraadt line[datalen] = '\0';
159 1.4 deraadt /*printf("line = %s\n", line);*/
160 1.4 deraadt bp = line;
161 1.4 deraadt goto parse;
162 1.4 deraadt }
163 1.4 deraadt #endif
164 1.4 deraadt
165 1.1 cgd ++_pw_keynum;
166 1.1 cgd bf[0] = _PW_KEYBYNUM;
167 1.1 cgd bcopy((char *)&_pw_keynum, bf + 1, sizeof(_pw_keynum));
168 1.1 cgd key.data = (u_char *)bf;
169 1.1 cgd key.size = sizeof(_pw_keynum) + 1;
170 1.4 deraadt if(__hashpw(&key)) {
171 1.4 deraadt #ifdef YP
172 1.4 deraadt if(strcmp(_pw_passwd.pw_name, "+") == 0) {
173 1.4 deraadt __ypmode = 1;
174 1.4 deraadt goto again;
175 1.4 deraadt }
176 1.4 deraadt #endif
177 1.4 deraadt return &_pw_passwd;
178 1.4 deraadt }
179 1.4 deraadt return (struct passwd *)NULL;
180 1.4 deraadt
181 1.4 deraadt #ifdef YP
182 1.4 deraadt parse:
183 1.4 deraadt _pw_passwd.pw_name = strsep(&bp, ":\n");
184 1.4 deraadt _pw_passwd.pw_passwd = strsep(&bp, ":\n");
185 1.4 deraadt if (!(cp = strsep(&bp, ":\n")))
186 1.4 deraadt goto again;
187 1.4 deraadt _pw_passwd.pw_uid = atoi(cp);
188 1.4 deraadt if (!(cp = strsep(&bp, ":\n")))
189 1.4 deraadt goto again;
190 1.4 deraadt _pw_passwd.pw_gid = atoi(cp);
191 1.4 deraadt _pw_passwd.pw_change = 0;
192 1.4 deraadt _pw_passwd.pw_class = "";
193 1.4 deraadt _pw_passwd.pw_gecos = strsep(&bp, ":\n");
194 1.4 deraadt _pw_passwd.pw_dir = strsep(&bp, ":\n");
195 1.4 deraadt _pw_passwd.pw_shell = strsep(&bp, ":\n");
196 1.4 deraadt _pw_passwd.pw_expire = 0;
197 1.4 deraadt return &_pw_passwd;
198 1.4 deraadt #endif
199 1.1 cgd }
200 1.1 cgd
201 1.1 cgd struct passwd *
202 1.1 cgd getpwnam(name)
203 1.1 cgd const char *name;
204 1.1 cgd {
205 1.1 cgd DBT key;
206 1.4 deraadt
207 1.4 deraadt #ifdef YP
208 1.4 deraadt char bf[sizeof(_pw_keynum) + 1];
209 1.4 deraadt int r;
210 1.4 deraadt
211 1.4 deraadt if (!_pw_db && !__initdb())
212 1.4 deraadt return((struct passwd *)NULL);
213 1.4 deraadt
214 1.4 deraadt for(_pw_keynum=1; _pw_keynum; _pw_keynum++) {
215 1.4 deraadt bf[0] = _PW_KEYBYNUM;
216 1.4 deraadt bcopy((char *)&_pw_keynum, bf + 1, sizeof(_pw_keynum));
217 1.4 deraadt key.data = (u_char *)bf;
218 1.4 deraadt key.size = sizeof(_pw_keynum) + 1;
219 1.4 deraadt if(__hashpw(&key) == 0)
220 1.4 deraadt break;
221 1.4 deraadt if(strcmp(_pw_passwd.pw_name, "+") == 0) {
222 1.4 deraadt if(!__ypdomain) {
223 1.4 deraadt if(_yp_check(&__ypdomain) == 0) {
224 1.4 deraadt continue;
225 1.4 deraadt }
226 1.4 deraadt }
227 1.4 deraadt if(__ypcurrent) {
228 1.4 deraadt free(__ypcurrent);
229 1.4 deraadt __ypcurrent = NULL;
230 1.4 deraadt }
231 1.4 deraadt r = yp_match(__ypdomain, "passwd.byname",
232 1.4 deraadt name, strlen(name),
233 1.4 deraadt &__ypcurrent, &__ypcurrentlen);
234 1.4 deraadt switch(r) {
235 1.4 deraadt case 0:
236 1.4 deraadt break;
237 1.4 deraadt default:
238 1.4 deraadt free(__ypcurrent);
239 1.4 deraadt __ypcurrent = NULL;
240 1.4 deraadt continue;
241 1.4 deraadt }
242 1.4 deraadt if(__ypparse(&_pw_passwd, __ypcurrent, __ypcurrentlen))
243 1.4 deraadt continue;
244 1.4 deraadt }
245 1.4 deraadt if( strcmp(_pw_passwd.pw_name, name) == 0) {
246 1.4 deraadt if (!_pw_stayopen) {
247 1.4 deraadt (void)(_pw_db->close)(_pw_db);
248 1.4 deraadt _pw_db = (DB *)NULL;
249 1.4 deraadt }
250 1.4 deraadt return &_pw_passwd;
251 1.4 deraadt }
252 1.4 deraadt continue;
253 1.4 deraadt }
254 1.4 deraadt if (!_pw_stayopen) {
255 1.4 deraadt (void)(_pw_db->close)(_pw_db);
256 1.4 deraadt _pw_db = (DB *)NULL;
257 1.4 deraadt }
258 1.4 deraadt return (struct passwd *)NULL;
259 1.4 deraadt #else
260 1.1 cgd int len, rval;
261 1.1 cgd char bf[UT_NAMESIZE + 1];
262 1.1 cgd
263 1.1 cgd if (!_pw_db && !__initdb())
264 1.1 cgd return((struct passwd *)NULL);
265 1.1 cgd
266 1.1 cgd bf[0] = _PW_KEYBYNAME;
267 1.1 cgd len = strlen(name);
268 1.1 cgd bcopy(name, bf + 1, MIN(len, UT_NAMESIZE));
269 1.1 cgd key.data = (u_char *)bf;
270 1.1 cgd key.size = len + 1;
271 1.1 cgd rval = __hashpw(&key);
272 1.1 cgd
273 1.1 cgd if (!_pw_stayopen) {
274 1.1 cgd (void)(_pw_db->close)(_pw_db);
275 1.1 cgd _pw_db = (DB *)NULL;
276 1.1 cgd }
277 1.1 cgd return(rval ? &_pw_passwd : (struct passwd *)NULL);
278 1.4 deraadt #endif
279 1.1 cgd }
280 1.1 cgd
281 1.1 cgd struct passwd *
282 1.1 cgd #ifdef __STDC__
283 1.1 cgd getpwuid(uid_t uid)
284 1.1 cgd #else
285 1.1 cgd getpwuid(uid)
286 1.1 cgd int uid;
287 1.1 cgd #endif
288 1.1 cgd {
289 1.1 cgd DBT key;
290 1.4 deraadt
291 1.4 deraadt #ifdef YP
292 1.4 deraadt char bf[sizeof(_pw_keynum) + 1], uidbuf[20];
293 1.4 deraadt int r;
294 1.4 deraadt
295 1.4 deraadt if (!_pw_db && !__initdb())
296 1.4 deraadt return((struct passwd *)NULL);
297 1.4 deraadt
298 1.4 deraadt for(_pw_keynum=1; _pw_keynum; _pw_keynum++) {
299 1.4 deraadt bf[0] = _PW_KEYBYNUM;
300 1.4 deraadt bcopy((char *)&_pw_keynum, bf + 1, sizeof(_pw_keynum));
301 1.4 deraadt key.data = (u_char *)bf;
302 1.4 deraadt key.size = sizeof(_pw_keynum) + 1;
303 1.4 deraadt if(__hashpw(&key) == 0)
304 1.4 deraadt break;
305 1.4 deraadt if(strcmp(_pw_passwd.pw_name, "+") == 0) {
306 1.4 deraadt if(!__ypdomain) {
307 1.4 deraadt if(_yp_check(&__ypdomain) == 0) {
308 1.4 deraadt continue;
309 1.4 deraadt }
310 1.4 deraadt }
311 1.4 deraadt if(__ypcurrent) {
312 1.4 deraadt free(__ypcurrent);
313 1.4 deraadt __ypcurrent = NULL;
314 1.4 deraadt }
315 1.4 deraadt sprintf(uidbuf, "%d", uid);
316 1.4 deraadt r = yp_match(__ypdomain, "passwd.byuid",
317 1.4 deraadt uidbuf, strlen(uidbuf),
318 1.4 deraadt &__ypcurrent, &__ypcurrentlen);
319 1.4 deraadt switch(r) {
320 1.4 deraadt case 0:
321 1.4 deraadt break;
322 1.4 deraadt default:
323 1.4 deraadt free(__ypcurrent);
324 1.4 deraadt __ypcurrent = NULL;
325 1.4 deraadt continue;
326 1.4 deraadt }
327 1.4 deraadt if(__ypparse(&_pw_passwd, __ypcurrent, __ypcurrentlen))
328 1.4 deraadt continue;
329 1.4 deraadt }
330 1.4 deraadt if( _pw_passwd.pw_uid == uid) {
331 1.4 deraadt if (!_pw_stayopen) {
332 1.4 deraadt (void)(_pw_db->close)(_pw_db);
333 1.4 deraadt _pw_db = (DB *)NULL;
334 1.4 deraadt }
335 1.4 deraadt return &_pw_passwd;
336 1.4 deraadt }
337 1.4 deraadt continue;
338 1.4 deraadt }
339 1.4 deraadt if (!_pw_stayopen) {
340 1.4 deraadt (void)(_pw_db->close)(_pw_db);
341 1.4 deraadt _pw_db = (DB *)NULL;
342 1.4 deraadt }
343 1.4 deraadt return (struct passwd *)NULL;
344 1.4 deraadt #else
345 1.1 cgd int keyuid, rval;
346 1.1 cgd char bf[sizeof(keyuid) + 1];
347 1.1 cgd
348 1.1 cgd if (!_pw_db && !__initdb())
349 1.1 cgd return((struct passwd *)NULL);
350 1.1 cgd
351 1.1 cgd bf[0] = _PW_KEYBYUID;
352 1.1 cgd keyuid = uid;
353 1.1 cgd bcopy(&keyuid, bf + 1, sizeof(keyuid));
354 1.1 cgd key.data = (u_char *)bf;
355 1.1 cgd key.size = sizeof(keyuid) + 1;
356 1.1 cgd rval = __hashpw(&key);
357 1.1 cgd
358 1.1 cgd if (!_pw_stayopen) {
359 1.1 cgd (void)(_pw_db->close)(_pw_db);
360 1.1 cgd _pw_db = (DB *)NULL;
361 1.1 cgd }
362 1.1 cgd return(rval ? &_pw_passwd : (struct passwd *)NULL);
363 1.4 deraadt #endif
364 1.1 cgd }
365 1.1 cgd
366 1.1 cgd int
367 1.1 cgd setpassent(stayopen)
368 1.1 cgd int stayopen;
369 1.1 cgd {
370 1.1 cgd _pw_keynum = 0;
371 1.1 cgd _pw_stayopen = stayopen;
372 1.1 cgd return(1);
373 1.1 cgd }
374 1.1 cgd
375 1.1 cgd int
376 1.1 cgd setpwent()
377 1.1 cgd {
378 1.1 cgd _pw_keynum = 0;
379 1.1 cgd _pw_stayopen = 0;
380 1.4 deraadt #ifdef YP
381 1.4 deraadt __ypmode = 0;
382 1.4 deraadt if(__ypcurrent)
383 1.4 deraadt free(__ypcurrent);
384 1.4 deraadt __ypcurrent = NULL;
385 1.4 deraadt #endif
386 1.1 cgd return(1);
387 1.1 cgd }
388 1.1 cgd
389 1.1 cgd void
390 1.1 cgd endpwent()
391 1.1 cgd {
392 1.1 cgd _pw_keynum = 0;
393 1.1 cgd if (_pw_db) {
394 1.1 cgd (void)(_pw_db->close)(_pw_db);
395 1.1 cgd _pw_db = (DB *)NULL;
396 1.1 cgd }
397 1.4 deraadt #ifdef YP
398 1.4 deraadt __ypmode = 0;
399 1.4 deraadt if(__ypcurrent)
400 1.4 deraadt free(__ypcurrent);
401 1.4 deraadt __ypcurrent = NULL;
402 1.4 deraadt #endif
403 1.1 cgd }
404 1.1 cgd
405 1.4 deraadt static int
406 1.1 cgd __initdb()
407 1.1 cgd {
408 1.1 cgd static int warned;
409 1.1 cgd char *p;
410 1.1 cgd
411 1.1 cgd p = (geteuid()) ? _PATH_MP_DB : _PATH_SMP_DB;
412 1.3 proven _pw_db = dbopen(p, O_RDONLY, 0, DB_HASH, NULL);
413 1.1 cgd if (_pw_db)
414 1.1 cgd return(1);
415 1.1 cgd if (!warned)
416 1.1 cgd syslog(LOG_ERR, "%s: %m", p);
417 1.1 cgd return(0);
418 1.1 cgd }
419 1.1 cgd
420 1.4 deraadt static int
421 1.1 cgd __hashpw(key)
422 1.1 cgd DBT *key;
423 1.1 cgd {
424 1.1 cgd register char *p, *t;
425 1.1 cgd static u_int max;
426 1.1 cgd static char *line;
427 1.1 cgd DBT data;
428 1.1 cgd
429 1.1 cgd if ((_pw_db->get)(_pw_db, key, &data, 0))
430 1.1 cgd return(0);
431 1.1 cgd p = (char *)data.data;
432 1.1 cgd if (data.size > max && !(line = realloc(line, max += 1024)))
433 1.1 cgd return(0);
434 1.1 cgd
435 1.1 cgd t = line;
436 1.1 cgd #define EXPAND(e) e = t; while (*t++ = *p++);
437 1.1 cgd EXPAND(_pw_passwd.pw_name);
438 1.1 cgd EXPAND(_pw_passwd.pw_passwd);
439 1.1 cgd bcopy(p, (char *)&_pw_passwd.pw_uid, sizeof(int));
440 1.1 cgd p += sizeof(int);
441 1.1 cgd bcopy(p, (char *)&_pw_passwd.pw_gid, sizeof(int));
442 1.1 cgd p += sizeof(int);
443 1.1 cgd bcopy(p, (char *)&_pw_passwd.pw_change, sizeof(time_t));
444 1.1 cgd p += sizeof(time_t);
445 1.1 cgd EXPAND(_pw_passwd.pw_class);
446 1.1 cgd EXPAND(_pw_passwd.pw_gecos);
447 1.1 cgd EXPAND(_pw_passwd.pw_dir);
448 1.1 cgd EXPAND(_pw_passwd.pw_shell);
449 1.1 cgd bcopy(p, (char *)&_pw_passwd.pw_expire, sizeof(time_t));
450 1.1 cgd p += sizeof(time_t);
451 1.1 cgd return(1);
452 1.1 cgd }
453