getpwent.c revision 1.5 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.5 deraadt static int
68 1.5 deraadt __ypparse(pw, s)
69 1.4 deraadt struct passwd *pw;
70 1.4 deraadt char *s;
71 1.4 deraadt {
72 1.4 deraadt char *bp, *cp;
73 1.4 deraadt
74 1.4 deraadt bp = s;
75 1.4 deraadt pw->pw_name = strsep(&bp, ":\n");
76 1.4 deraadt pw->pw_passwd = strsep(&bp, ":\n");
77 1.4 deraadt if (!(cp = strsep(&bp, ":\n")))
78 1.4 deraadt return 1;
79 1.4 deraadt pw->pw_uid = atoi(cp);
80 1.4 deraadt if (!(cp = strsep(&bp, ":\n")))
81 1.4 deraadt return 0;
82 1.4 deraadt pw->pw_gid = atoi(cp);
83 1.4 deraadt pw->pw_change = 0;
84 1.4 deraadt pw->pw_class = "";
85 1.4 deraadt pw->pw_gecos = strsep(&bp, ":\n");
86 1.4 deraadt pw->pw_dir = strsep(&bp, ":\n");
87 1.4 deraadt pw->pw_shell = strsep(&bp, ":\n");
88 1.4 deraadt pw->pw_expire = 0;
89 1.4 deraadt return 0;
90 1.4 deraadt }
91 1.4 deraadt #endif
92 1.4 deraadt
93 1.1 cgd struct passwd *
94 1.1 cgd getpwent()
95 1.1 cgd {
96 1.1 cgd DBT key;
97 1.1 cgd char bf[sizeof(_pw_keynum) + 1];
98 1.4 deraadt #ifdef YP
99 1.4 deraadt char *bp, *cp;
100 1.4 deraadt #endif
101 1.1 cgd
102 1.1 cgd if (!_pw_db && !__initdb())
103 1.1 cgd return((struct passwd *)NULL);
104 1.1 cgd
105 1.4 deraadt #ifdef YP
106 1.4 deraadt again:
107 1.4 deraadt if(__ypmode) {
108 1.4 deraadt char *key, *data;
109 1.4 deraadt int keylen, datalen;
110 1.4 deraadt int r;
111 1.4 deraadt
112 1.4 deraadt if(!__ypdomain) {
113 1.4 deraadt if( _yp_check(&__ypdomain) == 0) {
114 1.4 deraadt __ypmode = 0;
115 1.4 deraadt goto again;
116 1.4 deraadt }
117 1.4 deraadt }
118 1.4 deraadt if(__ypcurrent) {
119 1.4 deraadt r = yp_next(__ypdomain, "passwd.byname",
120 1.4 deraadt __ypcurrent, __ypcurrentlen,
121 1.4 deraadt &key, &keylen, &data, &datalen);
122 1.4 deraadt free(__ypcurrent);
123 1.4 deraadt __ypcurrent = NULL;
124 1.4 deraadt /*printf("yp_next %d\n", r);*/
125 1.4 deraadt switch(r) {
126 1.4 deraadt case 0:
127 1.4 deraadt break;
128 1.4 deraadt default:
129 1.4 deraadt __ypcurrent = NULL;
130 1.4 deraadt __ypmode = 0;
131 1.4 deraadt free(data);
132 1.4 deraadt data = NULL;
133 1.4 deraadt goto again;
134 1.4 deraadt }
135 1.4 deraadt __ypcurrent = key;
136 1.4 deraadt __ypcurrentlen = keylen;
137 1.4 deraadt bcopy(data, line, datalen);
138 1.4 deraadt free(data);
139 1.4 deraadt data = NULL;
140 1.4 deraadt } else {
141 1.4 deraadt r = yp_first(__ypdomain, "passwd.byname",
142 1.4 deraadt &__ypcurrent, &__ypcurrentlen,
143 1.4 deraadt &data, &datalen);
144 1.4 deraadt /*printf("yp_first %d\n", r);*/
145 1.4 deraadt switch(r) {
146 1.4 deraadt case 0:
147 1.4 deraadt break;
148 1.4 deraadt default:
149 1.4 deraadt __ypmode = 0;
150 1.4 deraadt free(data);
151 1.4 deraadt goto again;
152 1.4 deraadt }
153 1.4 deraadt bcopy(data, line, datalen);
154 1.4 deraadt free(data);
155 1.4 deraadt data = NULL;
156 1.4 deraadt }
157 1.4 deraadt line[datalen] = '\0';
158 1.4 deraadt /*printf("line = %s\n", line);*/
159 1.4 deraadt bp = line;
160 1.4 deraadt goto parse;
161 1.4 deraadt }
162 1.4 deraadt #endif
163 1.4 deraadt
164 1.1 cgd ++_pw_keynum;
165 1.1 cgd bf[0] = _PW_KEYBYNUM;
166 1.1 cgd bcopy((char *)&_pw_keynum, bf + 1, sizeof(_pw_keynum));
167 1.1 cgd key.data = (u_char *)bf;
168 1.1 cgd key.size = sizeof(_pw_keynum) + 1;
169 1.4 deraadt if(__hashpw(&key)) {
170 1.4 deraadt #ifdef YP
171 1.4 deraadt if(strcmp(_pw_passwd.pw_name, "+") == 0) {
172 1.4 deraadt __ypmode = 1;
173 1.4 deraadt goto again;
174 1.4 deraadt }
175 1.4 deraadt #endif
176 1.4 deraadt return &_pw_passwd;
177 1.4 deraadt }
178 1.4 deraadt return (struct passwd *)NULL;
179 1.4 deraadt
180 1.4 deraadt #ifdef YP
181 1.4 deraadt parse:
182 1.4 deraadt _pw_passwd.pw_name = strsep(&bp, ":\n");
183 1.4 deraadt _pw_passwd.pw_passwd = strsep(&bp, ":\n");
184 1.4 deraadt if (!(cp = strsep(&bp, ":\n")))
185 1.4 deraadt goto again;
186 1.4 deraadt _pw_passwd.pw_uid = atoi(cp);
187 1.4 deraadt if (!(cp = strsep(&bp, ":\n")))
188 1.4 deraadt goto again;
189 1.4 deraadt _pw_passwd.pw_gid = atoi(cp);
190 1.4 deraadt _pw_passwd.pw_change = 0;
191 1.4 deraadt _pw_passwd.pw_class = "";
192 1.4 deraadt _pw_passwd.pw_gecos = strsep(&bp, ":\n");
193 1.4 deraadt _pw_passwd.pw_dir = strsep(&bp, ":\n");
194 1.4 deraadt _pw_passwd.pw_shell = strsep(&bp, ":\n");
195 1.4 deraadt _pw_passwd.pw_expire = 0;
196 1.4 deraadt return &_pw_passwd;
197 1.4 deraadt #endif
198 1.1 cgd }
199 1.1 cgd
200 1.1 cgd struct passwd *
201 1.1 cgd getpwnam(name)
202 1.1 cgd const char *name;
203 1.1 cgd {
204 1.1 cgd DBT key;
205 1.4 deraadt
206 1.4 deraadt #ifdef YP
207 1.4 deraadt char bf[sizeof(_pw_keynum) + 1];
208 1.4 deraadt int r;
209 1.4 deraadt
210 1.4 deraadt if (!_pw_db && !__initdb())
211 1.4 deraadt return((struct passwd *)NULL);
212 1.4 deraadt
213 1.4 deraadt for(_pw_keynum=1; _pw_keynum; _pw_keynum++) {
214 1.4 deraadt bf[0] = _PW_KEYBYNUM;
215 1.4 deraadt bcopy((char *)&_pw_keynum, bf + 1, sizeof(_pw_keynum));
216 1.4 deraadt key.data = (u_char *)bf;
217 1.4 deraadt key.size = sizeof(_pw_keynum) + 1;
218 1.4 deraadt if(__hashpw(&key) == 0)
219 1.4 deraadt break;
220 1.4 deraadt if(strcmp(_pw_passwd.pw_name, "+") == 0) {
221 1.4 deraadt if(!__ypdomain) {
222 1.4 deraadt if(_yp_check(&__ypdomain) == 0) {
223 1.4 deraadt continue;
224 1.4 deraadt }
225 1.4 deraadt }
226 1.4 deraadt if(__ypcurrent) {
227 1.4 deraadt free(__ypcurrent);
228 1.4 deraadt __ypcurrent = NULL;
229 1.4 deraadt }
230 1.4 deraadt r = yp_match(__ypdomain, "passwd.byname",
231 1.4 deraadt name, strlen(name),
232 1.4 deraadt &__ypcurrent, &__ypcurrentlen);
233 1.4 deraadt switch(r) {
234 1.4 deraadt case 0:
235 1.4 deraadt break;
236 1.4 deraadt default:
237 1.4 deraadt free(__ypcurrent);
238 1.4 deraadt __ypcurrent = NULL;
239 1.4 deraadt continue;
240 1.4 deraadt }
241 1.5 deraadt bcopy(__ypcurrent, line, __ypcurrentlen);
242 1.5 deraadt line[__ypcurrentlen] = '\0';
243 1.5 deraadt if(__ypparse(&_pw_passwd, line))
244 1.4 deraadt continue;
245 1.4 deraadt }
246 1.4 deraadt if( strcmp(_pw_passwd.pw_name, name) == 0) {
247 1.4 deraadt if (!_pw_stayopen) {
248 1.4 deraadt (void)(_pw_db->close)(_pw_db);
249 1.4 deraadt _pw_db = (DB *)NULL;
250 1.4 deraadt }
251 1.4 deraadt return &_pw_passwd;
252 1.4 deraadt }
253 1.4 deraadt continue;
254 1.4 deraadt }
255 1.4 deraadt if (!_pw_stayopen) {
256 1.4 deraadt (void)(_pw_db->close)(_pw_db);
257 1.4 deraadt _pw_db = (DB *)NULL;
258 1.4 deraadt }
259 1.4 deraadt return (struct passwd *)NULL;
260 1.4 deraadt #else
261 1.1 cgd int len, rval;
262 1.1 cgd char bf[UT_NAMESIZE + 1];
263 1.1 cgd
264 1.1 cgd if (!_pw_db && !__initdb())
265 1.1 cgd return((struct passwd *)NULL);
266 1.1 cgd
267 1.1 cgd bf[0] = _PW_KEYBYNAME;
268 1.1 cgd len = strlen(name);
269 1.1 cgd bcopy(name, bf + 1, MIN(len, UT_NAMESIZE));
270 1.1 cgd key.data = (u_char *)bf;
271 1.1 cgd key.size = len + 1;
272 1.1 cgd rval = __hashpw(&key);
273 1.1 cgd
274 1.1 cgd if (!_pw_stayopen) {
275 1.1 cgd (void)(_pw_db->close)(_pw_db);
276 1.1 cgd _pw_db = (DB *)NULL;
277 1.1 cgd }
278 1.1 cgd return(rval ? &_pw_passwd : (struct passwd *)NULL);
279 1.4 deraadt #endif
280 1.1 cgd }
281 1.1 cgd
282 1.1 cgd struct passwd *
283 1.1 cgd #ifdef __STDC__
284 1.1 cgd getpwuid(uid_t uid)
285 1.1 cgd #else
286 1.1 cgd getpwuid(uid)
287 1.1 cgd int uid;
288 1.1 cgd #endif
289 1.1 cgd {
290 1.1 cgd DBT key;
291 1.4 deraadt
292 1.4 deraadt #ifdef YP
293 1.4 deraadt char bf[sizeof(_pw_keynum) + 1], uidbuf[20];
294 1.4 deraadt int r;
295 1.4 deraadt
296 1.4 deraadt if (!_pw_db && !__initdb())
297 1.4 deraadt return((struct passwd *)NULL);
298 1.4 deraadt
299 1.4 deraadt for(_pw_keynum=1; _pw_keynum; _pw_keynum++) {
300 1.4 deraadt bf[0] = _PW_KEYBYNUM;
301 1.4 deraadt bcopy((char *)&_pw_keynum, bf + 1, sizeof(_pw_keynum));
302 1.4 deraadt key.data = (u_char *)bf;
303 1.4 deraadt key.size = sizeof(_pw_keynum) + 1;
304 1.4 deraadt if(__hashpw(&key) == 0)
305 1.4 deraadt break;
306 1.4 deraadt if(strcmp(_pw_passwd.pw_name, "+") == 0) {
307 1.4 deraadt if(!__ypdomain) {
308 1.4 deraadt if(_yp_check(&__ypdomain) == 0) {
309 1.4 deraadt continue;
310 1.4 deraadt }
311 1.4 deraadt }
312 1.4 deraadt if(__ypcurrent) {
313 1.4 deraadt free(__ypcurrent);
314 1.4 deraadt __ypcurrent = NULL;
315 1.4 deraadt }
316 1.4 deraadt sprintf(uidbuf, "%d", uid);
317 1.4 deraadt r = yp_match(__ypdomain, "passwd.byuid",
318 1.4 deraadt uidbuf, strlen(uidbuf),
319 1.4 deraadt &__ypcurrent, &__ypcurrentlen);
320 1.4 deraadt switch(r) {
321 1.4 deraadt case 0:
322 1.4 deraadt break;
323 1.4 deraadt default:
324 1.4 deraadt free(__ypcurrent);
325 1.4 deraadt __ypcurrent = NULL;
326 1.4 deraadt continue;
327 1.4 deraadt }
328 1.5 deraadt bcopy(__ypcurrent, line, __ypcurrentlen);
329 1.5 deraadt line[__ypcurrentlen] = '\0';
330 1.5 deraadt if(__ypparse(&_pw_passwd, line))
331 1.4 deraadt continue;
332 1.4 deraadt }
333 1.4 deraadt if( _pw_passwd.pw_uid == uid) {
334 1.4 deraadt if (!_pw_stayopen) {
335 1.4 deraadt (void)(_pw_db->close)(_pw_db);
336 1.4 deraadt _pw_db = (DB *)NULL;
337 1.4 deraadt }
338 1.4 deraadt return &_pw_passwd;
339 1.4 deraadt }
340 1.4 deraadt continue;
341 1.4 deraadt }
342 1.4 deraadt if (!_pw_stayopen) {
343 1.4 deraadt (void)(_pw_db->close)(_pw_db);
344 1.4 deraadt _pw_db = (DB *)NULL;
345 1.4 deraadt }
346 1.4 deraadt return (struct passwd *)NULL;
347 1.4 deraadt #else
348 1.1 cgd int keyuid, rval;
349 1.1 cgd char bf[sizeof(keyuid) + 1];
350 1.1 cgd
351 1.1 cgd if (!_pw_db && !__initdb())
352 1.1 cgd return((struct passwd *)NULL);
353 1.1 cgd
354 1.1 cgd bf[0] = _PW_KEYBYUID;
355 1.1 cgd keyuid = uid;
356 1.1 cgd bcopy(&keyuid, bf + 1, sizeof(keyuid));
357 1.1 cgd key.data = (u_char *)bf;
358 1.1 cgd key.size = sizeof(keyuid) + 1;
359 1.1 cgd rval = __hashpw(&key);
360 1.1 cgd
361 1.1 cgd if (!_pw_stayopen) {
362 1.1 cgd (void)(_pw_db->close)(_pw_db);
363 1.1 cgd _pw_db = (DB *)NULL;
364 1.1 cgd }
365 1.1 cgd return(rval ? &_pw_passwd : (struct passwd *)NULL);
366 1.4 deraadt #endif
367 1.1 cgd }
368 1.1 cgd
369 1.1 cgd int
370 1.1 cgd setpassent(stayopen)
371 1.1 cgd int stayopen;
372 1.1 cgd {
373 1.1 cgd _pw_keynum = 0;
374 1.1 cgd _pw_stayopen = stayopen;
375 1.1 cgd return(1);
376 1.1 cgd }
377 1.1 cgd
378 1.1 cgd int
379 1.1 cgd setpwent()
380 1.1 cgd {
381 1.1 cgd _pw_keynum = 0;
382 1.1 cgd _pw_stayopen = 0;
383 1.4 deraadt #ifdef YP
384 1.4 deraadt __ypmode = 0;
385 1.4 deraadt if(__ypcurrent)
386 1.4 deraadt free(__ypcurrent);
387 1.4 deraadt __ypcurrent = NULL;
388 1.4 deraadt #endif
389 1.1 cgd return(1);
390 1.1 cgd }
391 1.1 cgd
392 1.1 cgd void
393 1.1 cgd endpwent()
394 1.1 cgd {
395 1.1 cgd _pw_keynum = 0;
396 1.1 cgd if (_pw_db) {
397 1.1 cgd (void)(_pw_db->close)(_pw_db);
398 1.1 cgd _pw_db = (DB *)NULL;
399 1.1 cgd }
400 1.4 deraadt #ifdef YP
401 1.4 deraadt __ypmode = 0;
402 1.4 deraadt if(__ypcurrent)
403 1.4 deraadt free(__ypcurrent);
404 1.4 deraadt __ypcurrent = NULL;
405 1.4 deraadt #endif
406 1.1 cgd }
407 1.1 cgd
408 1.4 deraadt static int
409 1.1 cgd __initdb()
410 1.1 cgd {
411 1.1 cgd static int warned;
412 1.1 cgd char *p;
413 1.1 cgd
414 1.1 cgd p = (geteuid()) ? _PATH_MP_DB : _PATH_SMP_DB;
415 1.3 proven _pw_db = dbopen(p, O_RDONLY, 0, DB_HASH, NULL);
416 1.1 cgd if (_pw_db)
417 1.1 cgd return(1);
418 1.1 cgd if (!warned)
419 1.1 cgd syslog(LOG_ERR, "%s: %m", p);
420 1.1 cgd return(0);
421 1.1 cgd }
422 1.1 cgd
423 1.4 deraadt static int
424 1.1 cgd __hashpw(key)
425 1.1 cgd DBT *key;
426 1.1 cgd {
427 1.1 cgd register char *p, *t;
428 1.1 cgd static u_int max;
429 1.1 cgd static char *line;
430 1.1 cgd DBT data;
431 1.1 cgd
432 1.1 cgd if ((_pw_db->get)(_pw_db, key, &data, 0))
433 1.1 cgd return(0);
434 1.1 cgd p = (char *)data.data;
435 1.1 cgd if (data.size > max && !(line = realloc(line, max += 1024)))
436 1.1 cgd return(0);
437 1.1 cgd
438 1.1 cgd t = line;
439 1.1 cgd #define EXPAND(e) e = t; while (*t++ = *p++);
440 1.1 cgd EXPAND(_pw_passwd.pw_name);
441 1.1 cgd EXPAND(_pw_passwd.pw_passwd);
442 1.1 cgd bcopy(p, (char *)&_pw_passwd.pw_uid, sizeof(int));
443 1.1 cgd p += sizeof(int);
444 1.1 cgd bcopy(p, (char *)&_pw_passwd.pw_gid, sizeof(int));
445 1.1 cgd p += sizeof(int);
446 1.1 cgd bcopy(p, (char *)&_pw_passwd.pw_change, sizeof(time_t));
447 1.1 cgd p += sizeof(time_t);
448 1.1 cgd EXPAND(_pw_passwd.pw_class);
449 1.1 cgd EXPAND(_pw_passwd.pw_gecos);
450 1.1 cgd EXPAND(_pw_passwd.pw_dir);
451 1.1 cgd EXPAND(_pw_passwd.pw_shell);
452 1.1 cgd bcopy(p, (char *)&_pw_passwd.pw_expire, sizeof(time_t));
453 1.1 cgd p += sizeof(time_t);
454 1.1 cgd return(1);
455 1.1 cgd }
456