getpwent.c revision 1.14.4.1 1 /* $NetBSD: getpwent.c,v 1.14.4.1 1996/09/16 18:40:27 jtc 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.14.4.1 1996/09/16 18:40:27 jtc Exp $";
42 #endif
43 #endif /* LIBC_SCCS and not lint */
44
45 #include "namespace.h"
46 #include <sys/param.h>
47 #include <fcntl.h>
48 #include <db.h>
49 #include <syslog.h>
50 #include <pwd.h>
51 #include <utmp.h>
52 #include <errno.h>
53 #include <unistd.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #include <limits.h>
57 #include <netgroup.h>
58 #ifdef YP
59 #include <machine/param.h>
60 #include <stdio.h>
61 #include <rpc/rpc.h>
62 #include <rpcsvc/yp_prot.h>
63 #include <rpcsvc/ypclnt.h>
64 #endif
65
66 static struct passwd _pw_passwd; /* password structure */
67 static DB *_pw_db; /* password database */
68 static int _pw_keynum; /* key counter */
69 static int _pw_stayopen; /* keep fd's open */
70 static int _pw_flags; /* password flags */
71 static int __hashpw __P((DBT *));
72 static int __initdb __P((void));
73
74 const char __yp_token[] = "__YP!"; /* Let pwd_mkdb pull this in. */
75
76 #ifdef YP
77 enum _ypmode { YPMODE_NONE, YPMODE_FULL, YPMODE_USER, YPMODE_NETGRP };
78 static enum _ypmode __ypmode;
79
80 static char *__ypcurrent, *__ypdomain;
81 static int __ypcurrentlen;
82 static struct passwd *__ypproto = (struct passwd *)NULL;
83 static int __ypflags;
84 static char line[1024];
85 static long prbuf[1024 / sizeof(long)];
86 static DB *__ypexclude = (DB *)NULL;
87
88 static int __has_yppw __P((void));
89 static int __ypexclude_add __P((const char *));
90 static int __ypexclude_is __P((const char *));
91 static void __ypproto_set __P((void));
92
93 static int
94 __ypexclude_add(name)
95 const char *name;
96 {
97 DBT key, data;
98
99 /* initialize the exclusion table if needed. */
100 if(__ypexclude == (DB *)NULL) {
101 __ypexclude = dbopen(NULL, O_RDWR, 600, DB_HASH, NULL);
102 if(__ypexclude == (DB *)NULL)
103 return(1);
104 }
105
106 /* set up the key */
107 key.data = (char *)name;
108 key.size = strlen(name);
109
110 /* data is nothing. */
111 data.data = NULL;
112 data.size = 0;
113
114 /* store it */
115 if((__ypexclude->put)(__ypexclude, &key, &data, 0) == -1)
116 return(1);
117
118 return(0);
119 }
120
121 static int
122 __ypexclude_is(name)
123 const char *name;
124 {
125 DBT key, data;
126
127 if(__ypexclude == (DB *)NULL)
128 return(0); /* nothing excluded */
129
130 /* set up the key */
131 key.data = (char *)name;
132 key.size = strlen(name);
133
134 if((__ypexclude->get)(__ypexclude, &key, &data, 0) == 0)
135 return(1); /* excluded */
136
137 return(0);
138 }
139
140 static void
141 __ypproto_set()
142 {
143 register char *ptr;
144 register struct passwd *pw = &_pw_passwd;
145
146 /* make this the new prototype */
147 ptr = (char *)prbuf;
148
149 /* first allocate the struct. */
150 __ypproto = (struct passwd *)ptr;
151 ptr += sizeof(struct passwd);
152
153 /* name */
154 if(pw->pw_name && (pw->pw_name)[0]) {
155 ptr = (char *)ALIGN(ptr);
156 bcopy(pw->pw_name, ptr, strlen(pw->pw_name) + 1);
157 __ypproto->pw_name = ptr;
158 ptr += (strlen(pw->pw_name) + 1);
159 } else
160 __ypproto->pw_name = (char *)NULL;
161
162 /* password */
163 if(pw->pw_passwd && (pw->pw_passwd)[0]) {
164 ptr = (char *)ALIGN(ptr);
165 bcopy(pw->pw_passwd, ptr, strlen(pw->pw_passwd) + 1);
166 __ypproto->pw_passwd = ptr;
167 ptr += (strlen(pw->pw_passwd) + 1);
168 } else
169 __ypproto->pw_passwd = (char *)NULL;
170
171 /* uid */
172 __ypproto->pw_uid = pw->pw_uid;
173
174 /* gid */
175 __ypproto->pw_gid = pw->pw_gid;
176
177 /* change (ignored anyway) */
178 __ypproto->pw_change = pw->pw_change;
179
180 /* class (ignored anyway) */
181 __ypproto->pw_class = "";
182
183 /* gecos */
184 if(pw->pw_gecos && (pw->pw_gecos)[0]) {
185 ptr = (char *)ALIGN(ptr);
186 bcopy(pw->pw_gecos, ptr, strlen(pw->pw_gecos) + 1);
187 __ypproto->pw_gecos = ptr;
188 ptr += (strlen(pw->pw_gecos) + 1);
189 } else
190 __ypproto->pw_gecos = (char *)NULL;
191
192 /* dir */
193 if(pw->pw_dir && (pw->pw_dir)[0]) {
194 ptr = (char *)ALIGN(ptr);
195 bcopy(pw->pw_dir, ptr, strlen(pw->pw_dir) + 1);
196 __ypproto->pw_dir = ptr;
197 ptr += (strlen(pw->pw_dir) + 1);
198 } else
199 __ypproto->pw_dir = (char *)NULL;
200
201 /* shell */
202 if(pw->pw_shell && (pw->pw_shell)[0]) {
203 ptr = (char *)ALIGN(ptr);
204 bcopy(pw->pw_shell, ptr, strlen(pw->pw_shell) + 1);
205 __ypproto->pw_shell = ptr;
206 ptr += (strlen(pw->pw_shell) + 1);
207 } else
208 __ypproto->pw_shell = (char *)NULL;
209
210 /* expire (ignored anyway) */
211 __ypproto->pw_expire = pw->pw_expire;
212
213 /* flags */
214 __ypflags = _pw_flags;
215 }
216
217 static int
218 __ypparse(pw, s)
219 struct passwd *pw;
220 char *s;
221 {
222 char *bp, *cp;
223
224 /* since this is currently using strsep(), parse it first */
225 bp = s;
226 pw->pw_name = strsep(&bp, ":\n");
227 pw->pw_passwd = strsep(&bp, ":\n");
228 if (!(cp = strsep(&bp, ":\n")))
229 return 1;
230 pw->pw_uid = atoi(cp);
231 if (!(cp = strsep(&bp, ":\n")))
232 return 1;
233 pw->pw_gid = atoi(cp);
234 pw->pw_change = 0;
235 pw->pw_class = "";
236 pw->pw_gecos = strsep(&bp, ":\n");
237 pw->pw_dir = strsep(&bp, ":\n");
238 pw->pw_shell = strsep(&bp, ":\n");
239 pw->pw_expire = 0;
240
241 /* now let the prototype override, if set. */
242 if(__ypproto != (struct passwd *)NULL) {
243 #ifdef YP_OVERRIDE_PASSWD
244 if(__ypproto->pw_passwd != (char *)NULL)
245 pw->pw_passwd = __ypproto->pw_passwd;
246 #endif
247 if(!(__ypflags & _PASSWORD_NOUID))
248 pw->pw_uid = __ypproto->pw_uid;
249 if(!(__ypflags & _PASSWORD_NOGID))
250 pw->pw_gid = __ypproto->pw_gid;
251 if(__ypproto->pw_gecos != (char *)NULL)
252 pw->pw_gecos = __ypproto->pw_gecos;
253 if(__ypproto->pw_dir != (char *)NULL)
254 pw->pw_dir = __ypproto->pw_dir;
255 if(__ypproto->pw_shell != (char *)NULL)
256 pw->pw_shell = __ypproto->pw_shell;
257 }
258 return 0;
259 }
260 #endif
261
262 struct passwd *
263 getpwent()
264 {
265 DBT key;
266 char bf[sizeof(_pw_keynum) + 1];
267 #ifdef YP
268 char *cp;
269 static char *name = (char *)NULL;
270 const char *user, *host, *dom;
271 int has_yppw;
272 #endif
273
274 if (!_pw_db && !__initdb())
275 return((struct passwd *)NULL);
276
277 #ifdef YP
278 has_yppw = __has_yppw();
279
280 again:
281 if(has_yppw && (__ypmode != YPMODE_NONE)) {
282 char *key, *data;
283 int keylen, datalen;
284 int r, s;
285
286 if(!__ypdomain) {
287 if( _yp_check(&__ypdomain) == 0) {
288 __ypmode = YPMODE_NONE;
289 goto again;
290 }
291 }
292 switch(__ypmode) {
293 case YPMODE_FULL:
294 if(__ypcurrent) {
295 r = yp_next(__ypdomain, "passwd.byname",
296 __ypcurrent, __ypcurrentlen,
297 &key, &keylen, &data, &datalen);
298 free(__ypcurrent);
299 if(r != 0) {
300 __ypcurrent = NULL;
301 __ypmode = YPMODE_NONE;
302 if(data)
303 free(data);
304 data = NULL;
305 goto again;
306 }
307 __ypcurrent = key;
308 __ypcurrentlen = keylen;
309 bcopy(data, line, datalen);
310 free(data);
311 data = NULL;
312 } else {
313 r = yp_first(__ypdomain, "passwd.byname",
314 &__ypcurrent, &__ypcurrentlen,
315 &data, &datalen);
316 if(r != 0) {
317 __ypmode = YPMODE_NONE;
318 if(data)
319 free(data);
320 goto again;
321 }
322 bcopy(data, line, datalen);
323 free(data);
324 data = NULL;
325 }
326 break;
327 case YPMODE_NETGRP:
328 s = getnetgrent(&host, &user, &dom);
329 if(s == 0) { /* end of group */
330 endnetgrent();
331 __ypmode = YPMODE_NONE;
332 goto again;
333 }
334 if(user && *user) {
335 r = yp_match(__ypdomain, "passwd.byname",
336 user, strlen(user),
337 &data, &datalen);
338 } else
339 goto again;
340 if(r != 0) {
341 /*
342 * if the netgroup is invalid, keep looking
343 * as there may be valid users later on.
344 */
345 if(data)
346 free(data);
347 goto again;
348 }
349 bcopy(data, line, datalen);
350 free(data);
351 data = (char *)NULL;
352 break;
353 case YPMODE_USER:
354 if(name != (char *)NULL) {
355 r = yp_match(__ypdomain, "passwd.byname",
356 name, strlen(name),
357 &data, &datalen);
358 __ypmode = YPMODE_NONE;
359 free(name);
360 name = (char *)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[UT_NAMESIZE];
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 len = strlen("+");
457 bcopy("+", bf + 1, MIN(len, UT_NAMESIZE));
458 pkey.data = (u_char *)bf;
459 pkey.size = len + 1;
460
461 if ((_pw_db->get)(_pw_db, &key, &data, 0)
462 && (_pw_db->get)(_pw_db, &pkey, &pdata, 0))
463 return(0); /* No YP. */
464 return(1);
465 }
466 #endif
467
468 struct passwd *
469 getpwnam(name)
470 const char *name;
471 {
472 DBT key;
473 int len, rval;
474 char bf[UT_NAMESIZE + 1];
475
476 if (!_pw_db && !__initdb())
477 return((struct passwd *)NULL);
478
479 #ifdef YP
480 /*
481 * If YP is active, we must sequence through the passwd file
482 * in sequence.
483 */
484 if (__has_yppw()) {
485 int r;
486 int s = -1;
487 const char *host, *user, *dom;
488
489 for(_pw_keynum=1; _pw_keynum; _pw_keynum++) {
490 bf[0] = _PW_KEYBYNUM;
491 bcopy((char *)&_pw_keynum, bf + 1, sizeof(_pw_keynum));
492 key.data = (u_char *)bf;
493 key.size = sizeof(_pw_keynum) + 1;
494 if(__hashpw(&key) == 0)
495 break;
496 switch(_pw_passwd.pw_name[0]) {
497 case '+':
498 if(!__ypdomain) {
499 if(_yp_check(&__ypdomain) == 0) {
500 continue;
501 }
502 }
503 /* save the prototype */
504 __ypproto_set();
505
506 switch(_pw_passwd.pw_name[1]) {
507 case '\0':
508 if(__ypcurrent) {
509 free(__ypcurrent);
510 __ypcurrent = NULL;
511 }
512 r = yp_match(__ypdomain,
513 "passwd.byname",
514 name, strlen(name),
515 &__ypcurrent, &__ypcurrentlen);
516 if(r != 0) {
517 if(__ypcurrent)
518 free(__ypcurrent);
519 __ypcurrent = NULL;
520 continue;
521 }
522 break;
523 case '@':
524 pwnam_netgrp:
525 if(__ypcurrent) {
526 free(__ypcurrent);
527 __ypcurrent = NULL;
528 }
529 if(s == -1) /* first time */
530 setnetgrent(_pw_passwd.pw_name + 2);
531 s = getnetgrent(&host, &user, &dom);
532 if(s == 0) { /* end of group */
533 endnetgrent();
534 s = -1;
535 continue;
536 } else {
537 if(user && *user) {
538 r = yp_match(__ypdomain,
539 "passwd.byname",
540 user, strlen(user),
541 &__ypcurrent,
542 &__ypcurrentlen);
543 } else
544 goto pwnam_netgrp;
545 if(r != 0) {
546 if(__ypcurrent)
547 free(__ypcurrent);
548 __ypcurrent = NULL;
549 /*
550 * just because this
551 * user is bad, doesn't
552 * mean they all are.
553 */
554 goto pwnam_netgrp;
555 }
556 }
557 break;
558 default:
559 if(__ypcurrent) {
560 free(__ypcurrent);
561 __ypcurrent = NULL;
562 }
563 user = _pw_passwd.pw_name + 1;
564 r = yp_match(__ypdomain,
565 "passwd.byname",
566 user, strlen(user),
567 &__ypcurrent,
568 &__ypcurrentlen);
569 if(r != 0) {
570 if(__ypcurrent)
571 free(__ypcurrent);
572 __ypcurrent = NULL;
573 continue;
574 }
575 break;
576 }
577 bcopy(__ypcurrent, line, __ypcurrentlen);
578 line[__ypcurrentlen] = '\0';
579 if(__ypparse(&_pw_passwd, line)
580 || __ypexclude_is(_pw_passwd.pw_name)) {
581 if(s == 1) /* inside netgrp */
582 goto pwnam_netgrp;
583 continue;
584 }
585 break;
586 case '-':
587 /* attempted exclusion */
588 switch(_pw_passwd.pw_name[1]) {
589 case '\0':
590 break;
591 case '@':
592 setnetgrent(_pw_passwd.pw_name + 2);
593 while(getnetgrent(&host, &user, &dom)) {
594 if(user && *user)
595 __ypexclude_add(user);
596 }
597 endnetgrent();
598 break;
599 default:
600 __ypexclude_add(_pw_passwd.pw_name + 1);
601 break;
602 }
603 break;
604
605 continue;
606 }
607 if(strcmp(_pw_passwd.pw_name, name) == 0) {
608 if (!_pw_stayopen) {
609 (void)(_pw_db->close)(_pw_db);
610 _pw_db = (DB *)NULL;
611 }
612 if(__ypexclude != (DB *)NULL) {
613 (void)(__ypexclude->close)(__ypexclude);
614 __ypexclude = (DB *)NULL;
615 }
616 __ypproto = (struct passwd *)NULL;
617 return &_pw_passwd;
618 }
619 if(s == 1) /* inside netgrp */
620 goto pwnam_netgrp;
621 continue;
622 }
623 if (!_pw_stayopen) {
624 (void)(_pw_db->close)(_pw_db);
625 _pw_db = (DB *)NULL;
626 }
627 if(__ypexclude != (DB *)NULL) {
628 (void)(__ypexclude->close)(__ypexclude);
629 __ypexclude = (DB *)NULL;
630 }
631 __ypproto = (struct passwd *)NULL;
632 return (struct passwd *)NULL;
633 }
634 #endif /* YP */
635
636 bf[0] = _PW_KEYBYNAME;
637 len = strlen(name);
638 bcopy(name, bf + 1, MIN(len, UT_NAMESIZE));
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 sprintf(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 register 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