getpwent.c revision 1.18 1 /* $NetBSD: getpwent.c,v 1.18 1997/05/21 01:51:40 lukem Exp $ */
2
3 /*
4 * Copyright (c) 1988, 1993
5 * The Regents of the University of California. All rights reserved.
6 * Portions Copyright (c) 1994, 1995, Jason Downs. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37 #if defined(LIBC_SCCS) && !defined(lint)
38 #if 0
39 static char sccsid[] = "@(#)getpwent.c 8.1 (Berkeley) 6/4/93";
40 #else
41 static char rcsid[] = "$NetBSD: getpwent.c,v 1.18 1997/05/21 01:51:40 lukem Exp $";
42 #endif
43 #endif /* LIBC_SCCS and not lint */
44
45 #include <sys/param.h>
46 #include <fcntl.h>
47 #include <db.h>
48 #include <syslog.h>
49 #include <pwd.h>
50 #include <utmp.h>
51 #include <errno.h>
52 #include <unistd.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <limits.h>
56 #include <netgroup.h>
57 #ifdef YP
58 #include <machine/param.h>
59 #include <stdio.h>
60 #include <rpc/rpc.h>
61 #include <rpcsvc/yp_prot.h>
62 #include <rpcsvc/ypclnt.h>
63 #endif
64
65 static struct passwd _pw_passwd; /* password structure */
66 static DB *_pw_db; /* password database */
67 static int _pw_keynum; /* key counter */
68 static int _pw_stayopen; /* keep fd's open */
69 static int _pw_flags; /* password flags */
70 static int __hashpw __P((DBT *));
71 static int __initdb __P((void));
72
73 const char __yp_token[] = "__YP!"; /* Let pwd_mkdb pull this in. */
74
75 #ifdef YP
76 enum _ypmode { YPMODE_NONE, YPMODE_FULL, YPMODE_USER, YPMODE_NETGRP };
77 static enum _ypmode __ypmode;
78
79 static char *__ypcurrent, *__ypdomain;
80 static int __ypcurrentlen;
81 static struct passwd *__ypproto = (struct passwd *)NULL;
82 static int __ypflags;
83 static char line[1024];
84 static long prbuf[1024 / sizeof(long)];
85 static DB *__ypexclude = (DB *)NULL;
86
87 static int __has_yppw __P((void));
88 static int __ypexclude_add __P((const char *));
89 static int __ypexclude_is __P((const char *));
90 static void __ypproto_set __P((void));
91
92 static int
93 __ypexclude_add(name)
94 const char *name;
95 {
96 DBT key, data;
97
98 /* initialize the exclusion table if needed. */
99 if(__ypexclude == (DB *)NULL) {
100 __ypexclude = dbopen(NULL, O_RDWR, 600, DB_HASH, NULL);
101 if(__ypexclude == (DB *)NULL)
102 return(1);
103 }
104
105 /* set up the key */
106 key.data = (char *)name;
107 key.size = strlen(name);
108
109 /* data is nothing. */
110 data.data = NULL;
111 data.size = 0;
112
113 /* store it */
114 if((__ypexclude->put)(__ypexclude, &key, &data, 0) == -1)
115 return(1);
116
117 return(0);
118 }
119
120 static int
121 __ypexclude_is(name)
122 const char *name;
123 {
124 DBT key, data;
125
126 if(__ypexclude == (DB *)NULL)
127 return(0); /* nothing excluded */
128
129 /* set up the key */
130 key.data = (char *)name;
131 key.size = strlen(name);
132
133 if((__ypexclude->get)(__ypexclude, &key, &data, 0) == 0)
134 return(1); /* excluded */
135
136 return(0);
137 }
138
139 static void
140 __ypproto_set()
141 {
142 char *ptr;
143 struct passwd *pw = &_pw_passwd;
144
145 /* make this the new prototype */
146 ptr = (char *)prbuf;
147
148 /* first allocate the struct. */
149 __ypproto = (struct passwd *)ptr;
150 ptr += sizeof(struct passwd);
151
152 /* name */
153 if(pw->pw_name && (pw->pw_name)[0]) {
154 ptr = (char *)ALIGN(ptr);
155 bcopy(pw->pw_name, ptr, strlen(pw->pw_name) + 1);
156 __ypproto->pw_name = ptr;
157 ptr += (strlen(pw->pw_name) + 1);
158 } else
159 __ypproto->pw_name = (char *)NULL;
160
161 /* password */
162 if(pw->pw_passwd && (pw->pw_passwd)[0]) {
163 ptr = (char *)ALIGN(ptr);
164 bcopy(pw->pw_passwd, ptr, strlen(pw->pw_passwd) + 1);
165 __ypproto->pw_passwd = ptr;
166 ptr += (strlen(pw->pw_passwd) + 1);
167 } else
168 __ypproto->pw_passwd = (char *)NULL;
169
170 /* uid */
171 __ypproto->pw_uid = pw->pw_uid;
172
173 /* gid */
174 __ypproto->pw_gid = pw->pw_gid;
175
176 /* change (ignored anyway) */
177 __ypproto->pw_change = pw->pw_change;
178
179 /* class (ignored anyway) */
180 __ypproto->pw_class = "";
181
182 /* gecos */
183 if(pw->pw_gecos && (pw->pw_gecos)[0]) {
184 ptr = (char *)ALIGN(ptr);
185 bcopy(pw->pw_gecos, ptr, strlen(pw->pw_gecos) + 1);
186 __ypproto->pw_gecos = ptr;
187 ptr += (strlen(pw->pw_gecos) + 1);
188 } else
189 __ypproto->pw_gecos = (char *)NULL;
190
191 /* dir */
192 if(pw->pw_dir && (pw->pw_dir)[0]) {
193 ptr = (char *)ALIGN(ptr);
194 bcopy(pw->pw_dir, ptr, strlen(pw->pw_dir) + 1);
195 __ypproto->pw_dir = ptr;
196 ptr += (strlen(pw->pw_dir) + 1);
197 } else
198 __ypproto->pw_dir = (char *)NULL;
199
200 /* shell */
201 if(pw->pw_shell && (pw->pw_shell)[0]) {
202 ptr = (char *)ALIGN(ptr);
203 bcopy(pw->pw_shell, ptr, strlen(pw->pw_shell) + 1);
204 __ypproto->pw_shell = ptr;
205 ptr += (strlen(pw->pw_shell) + 1);
206 } else
207 __ypproto->pw_shell = (char *)NULL;
208
209 /* expire (ignored anyway) */
210 __ypproto->pw_expire = pw->pw_expire;
211
212 /* flags */
213 __ypflags = _pw_flags;
214 }
215
216 static int
217 __ypparse(pw, s)
218 struct passwd *pw;
219 char *s;
220 {
221 char *bp, *cp;
222
223 /* since this is currently using strsep(), parse it first */
224 bp = s;
225 pw->pw_name = strsep(&bp, ":\n");
226 pw->pw_passwd = strsep(&bp, ":\n");
227 if (!(cp = strsep(&bp, ":\n")))
228 return 1;
229 pw->pw_uid = atoi(cp);
230 if (!(cp = strsep(&bp, ":\n")))
231 return 1;
232 pw->pw_gid = atoi(cp);
233 pw->pw_change = 0;
234 pw->pw_class = "";
235 pw->pw_gecos = strsep(&bp, ":\n");
236 pw->pw_dir = strsep(&bp, ":\n");
237 pw->pw_shell = strsep(&bp, ":\n");
238 pw->pw_expire = 0;
239
240 /* now let the prototype override, if set. */
241 if(__ypproto != (struct passwd *)NULL) {
242 #ifdef YP_OVERRIDE_PASSWD
243 if(__ypproto->pw_passwd != (char *)NULL)
244 pw->pw_passwd = __ypproto->pw_passwd;
245 #endif
246 if(!(__ypflags & _PASSWORD_NOUID))
247 pw->pw_uid = __ypproto->pw_uid;
248 if(!(__ypflags & _PASSWORD_NOGID))
249 pw->pw_gid = __ypproto->pw_gid;
250 if(__ypproto->pw_gecos != (char *)NULL)
251 pw->pw_gecos = __ypproto->pw_gecos;
252 if(__ypproto->pw_dir != (char *)NULL)
253 pw->pw_dir = __ypproto->pw_dir;
254 if(__ypproto->pw_shell != (char *)NULL)
255 pw->pw_shell = __ypproto->pw_shell;
256 }
257 return 0;
258 }
259 #endif
260
261 struct passwd *
262 getpwent()
263 {
264 DBT key;
265 char bf[sizeof(_pw_keynum) + 1];
266 #ifdef YP
267 char *cp;
268 static char *name = (char *)NULL;
269 const char *user, *host, *dom;
270 int has_yppw;
271 #endif
272
273 if (!_pw_db && !__initdb())
274 return((struct passwd *)NULL);
275
276 #ifdef YP
277 has_yppw = __has_yppw();
278
279 again:
280 if(has_yppw && (__ypmode != YPMODE_NONE)) {
281 char *key, *data;
282 int keylen, datalen;
283 int r, s;
284
285 if(!__ypdomain) {
286 if( _yp_check(&__ypdomain) == 0) {
287 __ypmode = YPMODE_NONE;
288 goto again;
289 }
290 }
291 switch(__ypmode) {
292 case YPMODE_FULL:
293 data = NULL;
294 if(__ypcurrent) {
295 key = NULL;
296 r = yp_next(__ypdomain, "passwd.byname",
297 __ypcurrent, __ypcurrentlen,
298 &key, &keylen, &data, &datalen);
299 free(__ypcurrent);
300 if(r != 0) {
301 __ypcurrent = NULL;
302 if (key)
303 free(key);
304 }
305 else {
306 __ypcurrent = key;
307 __ypcurrentlen = keylen;
308 }
309 } else {
310 r = yp_first(__ypdomain, "passwd.byname",
311 &__ypcurrent, &__ypcurrentlen,
312 &data, &datalen);
313 }
314 if(r != 0) {
315 __ypmode = YPMODE_NONE;
316 if(data)
317 free(data);
318 data = NULL;
319 goto again;
320 }
321 bcopy(data, line, datalen);
322 free(data);
323 data = NULL;
324 break;
325 case YPMODE_NETGRP:
326 s = getnetgrent(&host, &user, &dom);
327 if(s == 0) { /* end of group */
328 endnetgrent();
329 __ypmode = YPMODE_NONE;
330 goto again;
331 }
332 if(user && *user) {
333 data = NULL;
334 r = yp_match(__ypdomain, "passwd.byname",
335 user, strlen(user),
336 &data, &datalen);
337 } else
338 goto again;
339 if(r != 0) {
340 /*
341 * if the netgroup is invalid, keep looking
342 * as there may be valid users later on.
343 */
344 if(data)
345 free(data);
346 goto again;
347 }
348 bcopy(data, line, datalen);
349 free(data);
350 data = NULL;
351 break;
352 case YPMODE_USER:
353 if(name != (char *)NULL) {
354 data = NULL;
355 r = yp_match(__ypdomain, "passwd.byname",
356 name, strlen(name),
357 &data, &datalen);
358 __ypmode = YPMODE_NONE;
359 free(name);
360 name = NULL;
361 if(r != 0) {
362 if(data)
363 free(data);
364 goto again;
365 }
366 bcopy(data, line, datalen);
367 free(data);
368 data = (char *)NULL;
369 } else { /* XXX */
370 __ypmode = YPMODE_NONE;
371 goto again;
372 }
373 break;
374 }
375
376 line[datalen] = '\0';
377 if (__ypparse(&_pw_passwd, line))
378 goto again;
379 return &_pw_passwd;
380 }
381 #endif
382
383 ++_pw_keynum;
384 bf[0] = _PW_KEYBYNUM;
385 bcopy((char *)&_pw_keynum, bf + 1, sizeof(_pw_keynum));
386 key.data = (u_char *)bf;
387 key.size = sizeof(_pw_keynum) + 1;
388 if(__hashpw(&key)) {
389 #ifdef YP
390 /* if we don't have YP at all, don't bother. */
391 if(has_yppw) {
392 if(_pw_passwd.pw_name[0] == '+') {
393 /* set the mode */
394 switch(_pw_passwd.pw_name[1]) {
395 case '\0':
396 __ypmode = YPMODE_FULL;
397 break;
398 case '@':
399 __ypmode = YPMODE_NETGRP;
400 setnetgrent(_pw_passwd.pw_name + 2);
401 break;
402 default:
403 __ypmode = YPMODE_USER;
404 name = strdup(_pw_passwd.pw_name + 1);
405 break;
406 }
407
408 /* save the prototype */
409 __ypproto_set();
410 goto again;
411 } else if(_pw_passwd.pw_name[0] == '-') {
412 /* an attempted exclusion */
413 switch(_pw_passwd.pw_name[1]) {
414 case '\0':
415 break;
416 case '@':
417 setnetgrent(_pw_passwd.pw_name + 2);
418 while(getnetgrent(&host, &user, &dom)) {
419 if(user && *user)
420 __ypexclude_add(user);
421 }
422 endnetgrent();
423 break;
424 default:
425 __ypexclude_add(_pw_passwd.pw_name + 1);
426 break;
427 }
428 goto again;
429 }
430 }
431 #endif
432 return &_pw_passwd;
433 }
434 return (struct passwd *)NULL;
435 }
436
437 #ifdef YP
438
439 /*
440 * See if the YP token is in the database. Only works if pwd_mkdb knows
441 * about the token.
442 */
443 static int
444 __has_yppw()
445 {
446 DBT key, data;
447 DBT pkey, pdata;
448 int len;
449 char bf[MAXLOGNAME];
450
451 key.data = (u_char *)__yp_token;
452 key.size = strlen(__yp_token);
453
454 /* Pre-token database support. */
455 bf[0] = _PW_KEYBYNAME;
456 bf[1] = '+';
457 pkey.data = (u_char *)bf;
458 pkey.size = 2;
459
460 if ((_pw_db->get)(_pw_db, &key, &data, 0)
461 && (_pw_db->get)(_pw_db, &pkey, &pdata, 0))
462 return(0); /* No YP. */
463 return(1);
464 }
465 #endif
466
467 struct passwd *
468 getpwnam(name)
469 const char *name;
470 {
471 DBT key;
472 int len, rval;
473 char bf[MAXLOGNAME + 1];
474
475 if (!_pw_db && !__initdb())
476 return((struct passwd *)NULL);
477
478 #ifdef YP
479 /*
480 * If YP is active, we must sequence through the passwd file
481 * in sequence.
482 */
483 if (__has_yppw()) {
484 int r;
485 int s = -1;
486 const char *host, *user, *dom;
487
488 for(_pw_keynum=1; _pw_keynum; _pw_keynum++) {
489 bf[0] = _PW_KEYBYNUM;
490 bcopy((char *)&_pw_keynum, bf + 1, sizeof(_pw_keynum));
491 key.data = (u_char *)bf;
492 key.size = sizeof(_pw_keynum) + 1;
493 if(__hashpw(&key) == 0)
494 break;
495 switch(_pw_passwd.pw_name[0]) {
496 case '+':
497 if(!__ypdomain) {
498 if(_yp_check(&__ypdomain) == 0) {
499 continue;
500 }
501 }
502 /* save the prototype */
503 __ypproto_set();
504
505 switch(_pw_passwd.pw_name[1]) {
506 case '\0':
507 if(__ypcurrent) {
508 free(__ypcurrent);
509 __ypcurrent = NULL;
510 }
511 r = yp_match(__ypdomain,
512 "passwd.byname",
513 name, strlen(name),
514 &__ypcurrent, &__ypcurrentlen);
515 if(r != 0) {
516 if(__ypcurrent)
517 free(__ypcurrent);
518 __ypcurrent = NULL;
519 continue;
520 }
521 break;
522 case '@':
523 pwnam_netgrp:
524 if(__ypcurrent) {
525 free(__ypcurrent);
526 __ypcurrent = NULL;
527 }
528 if(s == -1) /* first time */
529 setnetgrent(_pw_passwd.pw_name + 2);
530 s = getnetgrent(&host, &user, &dom);
531 if(s == 0) { /* end of group */
532 endnetgrent();
533 s = -1;
534 continue;
535 } else {
536 if(user && *user) {
537 r = yp_match(__ypdomain,
538 "passwd.byname",
539 user, strlen(user),
540 &__ypcurrent,
541 &__ypcurrentlen);
542 } else
543 goto pwnam_netgrp;
544 if(r != 0) {
545 if(__ypcurrent)
546 free(__ypcurrent);
547 __ypcurrent = NULL;
548 /*
549 * just because this
550 * user is bad, doesn't
551 * mean they all are.
552 */
553 goto pwnam_netgrp;
554 }
555 }
556 break;
557 default:
558 if(__ypcurrent) {
559 free(__ypcurrent);
560 __ypcurrent = NULL;
561 }
562 user = _pw_passwd.pw_name + 1;
563 r = yp_match(__ypdomain,
564 "passwd.byname",
565 user, strlen(user),
566 &__ypcurrent,
567 &__ypcurrentlen);
568 if(r != 0) {
569 if(__ypcurrent)
570 free(__ypcurrent);
571 __ypcurrent = NULL;
572 continue;
573 }
574 break;
575 }
576 bcopy(__ypcurrent, line, __ypcurrentlen);
577 line[__ypcurrentlen] = '\0';
578 if(__ypparse(&_pw_passwd, line)
579 || __ypexclude_is(_pw_passwd.pw_name)) {
580 if(s == 1) /* inside netgrp */
581 goto pwnam_netgrp;
582 continue;
583 }
584 break;
585 case '-':
586 /* attempted exclusion */
587 switch(_pw_passwd.pw_name[1]) {
588 case '\0':
589 break;
590 case '@':
591 setnetgrent(_pw_passwd.pw_name + 2);
592 while(getnetgrent(&host, &user, &dom)) {
593 if(user && *user)
594 __ypexclude_add(user);
595 }
596 endnetgrent();
597 break;
598 default:
599 __ypexclude_add(_pw_passwd.pw_name + 1);
600 break;
601 }
602 break;
603
604 continue;
605 }
606 if(strcmp(_pw_passwd.pw_name, name) == 0) {
607 if (!_pw_stayopen) {
608 (void)(_pw_db->close)(_pw_db);
609 _pw_db = (DB *)NULL;
610 }
611 if(__ypexclude != (DB *)NULL) {
612 (void)(__ypexclude->close)(__ypexclude);
613 __ypexclude = (DB *)NULL;
614 }
615 __ypproto = (struct passwd *)NULL;
616 return &_pw_passwd;
617 }
618 if(s == 1) /* inside netgrp */
619 goto pwnam_netgrp;
620 continue;
621 }
622 if (!_pw_stayopen) {
623 (void)(_pw_db->close)(_pw_db);
624 _pw_db = (DB *)NULL;
625 }
626 if(__ypexclude != (DB *)NULL) {
627 (void)(__ypexclude->close)(__ypexclude);
628 __ypexclude = (DB *)NULL;
629 }
630 __ypproto = (struct passwd *)NULL;
631 return (struct passwd *)NULL;
632 }
633 #endif /* YP */
634
635 bf[0] = _PW_KEYBYNAME;
636 len = strlen(name);
637 len = MIN(len, MAXLOGNAME);
638 bcopy(name, bf + 1, len);
639 key.data = (u_char *)bf;
640 key.size = len + 1;
641 rval = __hashpw(&key);
642
643 if (!_pw_stayopen) {
644 (void)(_pw_db->close)(_pw_db);
645 _pw_db = (DB *)NULL;
646 }
647 return(rval ? &_pw_passwd : (struct passwd *)NULL);
648 }
649
650 struct passwd *
651 #ifdef __STDC__
652 getpwuid(uid_t uid)
653 #else
654 getpwuid(uid)
655 int uid;
656 #endif
657 {
658 DBT key;
659 char bf[sizeof(_pw_keynum) + 1];
660 int keyuid, rval;
661
662 if (!_pw_db && !__initdb())
663 return((struct passwd *)NULL);
664
665 #ifdef YP
666 /*
667 * If YP is active, we must sequence through the passwd file
668 * in sequence.
669 */
670 if (__has_yppw()) {
671 char uidbuf[20];
672 int r;
673 int s = -1;
674 const char *host, *user, *dom;
675
676 snprintf(uidbuf, sizeof uidbuf, "%d", uid);
677 for(_pw_keynum=1; _pw_keynum; _pw_keynum++) {
678 bf[0] = _PW_KEYBYNUM;
679 bcopy((char *)&_pw_keynum, bf + 1, sizeof(_pw_keynum));
680 key.data = (u_char *)bf;
681 key.size = sizeof(_pw_keynum) + 1;
682 if(__hashpw(&key) == 0)
683 break;
684 switch(_pw_passwd.pw_name[0]) {
685 case '+':
686 if(!__ypdomain) {
687 if(_yp_check(&__ypdomain) == 0) {
688 continue;
689 }
690 }
691 /* save the prototype */
692 __ypproto_set();
693
694 switch(_pw_passwd.pw_name[1]) {
695 case '\0':
696 if(__ypcurrent) {
697 free(__ypcurrent);
698 __ypcurrent = NULL;
699 }
700 r = yp_match(__ypdomain, "passwd.byuid",
701 uidbuf, strlen(uidbuf),
702 &__ypcurrent, &__ypcurrentlen);
703 if(r != 0) {
704 if(__ypcurrent)
705 free(__ypcurrent);
706 __ypcurrent = NULL;
707 continue;
708 }
709 break;
710 case '@':
711 pwuid_netgrp:
712 if(__ypcurrent) {
713 free(__ypcurrent);
714 __ypcurrent = NULL;
715 }
716 if(s == -1) /* first time */
717 setnetgrent(_pw_passwd.pw_name + 2);
718 s = getnetgrent(&host, &user, &dom);
719 if(s == 0) { /* end of group */
720 endnetgrent();
721 s = -1;
722 continue;
723 } else {
724 if(user && *user) {
725 r = yp_match(__ypdomain,
726 "passwd.byname",
727 user, strlen(user),
728 &__ypcurrent,
729 &__ypcurrentlen);
730 } else
731 goto pwuid_netgrp;
732 if(r != 0) {
733 if(__ypcurrent)
734 free(__ypcurrent);
735 __ypcurrent = NULL;
736 /*
737 * just because this
738 * user is bad, doesn't
739 * mean they all are.
740 */
741 goto pwuid_netgrp;
742 }
743 }
744 break;
745 default:
746 if(__ypcurrent) {
747 free(__ypcurrent);
748 __ypcurrent = NULL;
749 }
750 user = _pw_passwd.pw_name + 1;
751 r = yp_match(__ypdomain,
752 "passwd.byname",
753 user, strlen(user),
754 &__ypcurrent,
755 &__ypcurrentlen);
756 if(r != 0) {
757 if(__ypcurrent)
758 free(__ypcurrent);
759 __ypcurrent = NULL;
760 continue;
761 }
762 break;
763 }
764 bcopy(__ypcurrent, line, __ypcurrentlen);
765 line[__ypcurrentlen] = '\0';
766 if(__ypparse(&_pw_passwd, line)
767 || __ypexclude_is(_pw_passwd.pw_name)) {
768 if(s == 1) /* inside netgroup */
769 goto pwuid_netgrp;
770 continue;
771 }
772 break;
773 case '-':
774 /* attempted exclusion */
775 switch(_pw_passwd.pw_name[1]) {
776 case '\0':
777 break;
778 case '@':
779 setnetgrent(_pw_passwd.pw_name + 2);
780 while(getnetgrent(&host, &user, &dom)) {
781 if(user && *user)
782 __ypexclude_add(user);
783 }
784 endnetgrent();
785 break;
786 default:
787 __ypexclude_add(_pw_passwd.pw_name + 1);
788 break;
789 }
790 break;
791
792 continue;
793 }
794 if( _pw_passwd.pw_uid == uid) {
795 if (!_pw_stayopen) {
796 (void)(_pw_db->close)(_pw_db);
797 _pw_db = (DB *)NULL;
798 }
799 if (__ypexclude != (DB *)NULL) {
800 (void)(__ypexclude->close)(__ypexclude);
801 __ypexclude = (DB *)NULL;
802 }
803 __ypproto = NULL;
804 return &_pw_passwd;
805 }
806 if(s == 1) /* inside netgroup */
807 goto pwuid_netgrp;
808 continue;
809 }
810 if (!_pw_stayopen) {
811 (void)(_pw_db->close)(_pw_db);
812 _pw_db = (DB *)NULL;
813 }
814 if(__ypexclude != (DB *)NULL) {
815 (void)(__ypexclude->close)(__ypexclude);
816 __ypexclude = (DB *)NULL;
817 }
818 __ypproto = (struct passwd *)NULL;
819 return (struct passwd *)NULL;
820 }
821 #endif /* YP */
822
823 bf[0] = _PW_KEYBYUID;
824 keyuid = uid;
825 bcopy(&keyuid, bf + 1, sizeof(keyuid));
826 key.data = (u_char *)bf;
827 key.size = sizeof(keyuid) + 1;
828 rval = __hashpw(&key);
829
830 if (!_pw_stayopen) {
831 (void)(_pw_db->close)(_pw_db);
832 _pw_db = (DB *)NULL;
833 }
834 return(rval ? &_pw_passwd : (struct passwd *)NULL);
835 }
836
837 int
838 setpassent(stayopen)
839 int stayopen;
840 {
841 _pw_keynum = 0;
842 _pw_stayopen = stayopen;
843 #ifdef YP
844 __ypmode = YPMODE_NONE;
845 if(__ypcurrent)
846 free(__ypcurrent);
847 __ypcurrent = NULL;
848 if(__ypexclude != (DB *)NULL) {
849 (void)(__ypexclude->close)(__ypexclude);
850 __ypexclude = (DB *)NULL;
851 }
852 __ypproto = (struct passwd *)NULL;
853 #endif
854 return(1);
855 }
856
857 void
858 setpwent()
859 {
860 (void) setpassent(0);
861 }
862
863 void
864 endpwent()
865 {
866 _pw_keynum = 0;
867 if (_pw_db) {
868 (void)(_pw_db->close)(_pw_db);
869 _pw_db = (DB *)NULL;
870 }
871 #ifdef YP
872 __ypmode = YPMODE_NONE;
873 if(__ypcurrent)
874 free(__ypcurrent);
875 __ypcurrent = NULL;
876 if(__ypexclude != (DB *)NULL) {
877 (void)(__ypexclude->close)(__ypexclude);
878 __ypexclude = (DB *)NULL;
879 }
880 __ypproto = (struct passwd *)NULL;
881 #endif
882 }
883
884 static int
885 __initdb()
886 {
887 static int warned;
888 char *p;
889
890 #ifdef YP
891 __ypmode = YPMODE_NONE;
892 #endif
893 p = (geteuid()) ? _PATH_MP_DB : _PATH_SMP_DB;
894 _pw_db = dbopen(p, O_RDONLY, 0, DB_HASH, NULL);
895 if (_pw_db)
896 return(1);
897 if (!warned)
898 syslog(LOG_ERR, "%s: %m", p);
899 warned = 1;
900 return(0);
901 }
902
903 static int
904 __hashpw(key)
905 DBT *key;
906 {
907 char *p, *t;
908 static u_int max;
909 static char *line;
910 DBT data;
911
912 if ((_pw_db->get)(_pw_db, key, &data, 0))
913 return(0);
914 p = (char *)data.data;
915 if (data.size > max && !(line = realloc(line, (max += 1024))))
916 return(0);
917
918 t = line;
919 #define EXPAND(e) e = t; while ((*t++ = *p++));
920 EXPAND(_pw_passwd.pw_name);
921 EXPAND(_pw_passwd.pw_passwd);
922 bcopy(p, (char *)&_pw_passwd.pw_uid, sizeof(int));
923 p += sizeof(int);
924 bcopy(p, (char *)&_pw_passwd.pw_gid, sizeof(int));
925 p += sizeof(int);
926 bcopy(p, (char *)&_pw_passwd.pw_change, sizeof(time_t));
927 p += sizeof(time_t);
928 EXPAND(_pw_passwd.pw_class);
929 EXPAND(_pw_passwd.pw_gecos);
930 EXPAND(_pw_passwd.pw_dir);
931 EXPAND(_pw_passwd.pw_shell);
932 bcopy(p, (char *)&_pw_passwd.pw_expire, sizeof(time_t));
933 p += sizeof(time_t);
934
935 /* See if there's any data left. If so, read in flags. */
936 if (data.size > (p - (char *)data.data)) {
937 bcopy(p, (char *)&_pw_flags, sizeof(int));
938 p += sizeof(int);
939 } else
940 _pw_flags = _PASSWORD_NOUID|_PASSWORD_NOGID; /* default */
941
942 return(1);
943 }
944