getpwent.c revision 1.17 1 /* $NetBSD: getpwent.c,v 1.17 1997/05/20 15:59:59 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.17 1997/05/20 15:59:59 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 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 else {
302 __ypcurrent = key;
303 __ypcurrentlen = keylen;
304 }
305 } else {
306 r = yp_first(__ypdomain, "passwd.byname",
307 &__ypcurrent, &__ypcurrentlen,
308 &data, &datalen);
309 }
310 if(r != 0) {
311 __ypmode = YPMODE_NONE;
312 if(data)
313 free(data);
314 data = NULL;
315 goto again;
316 }
317 bcopy(data, line, datalen);
318 free(data);
319 data = NULL;
320 break;
321 case YPMODE_NETGRP:
322 s = getnetgrent(&host, &user, &dom);
323 if(s == 0) { /* end of group */
324 endnetgrent();
325 __ypmode = YPMODE_NONE;
326 goto again;
327 }
328 if(user && *user) {
329 data = NULL;
330 r = yp_match(__ypdomain, "passwd.byname",
331 user, strlen(user),
332 &data, &datalen);
333 } else
334 goto again;
335 if(r != 0) {
336 /*
337 * if the netgroup is invalid, keep looking
338 * as there may be valid users later on.
339 */
340 if(data)
341 free(data);
342 goto again;
343 }
344 bcopy(data, line, datalen);
345 free(data);
346 data = NULL;
347 break;
348 case YPMODE_USER:
349 if(name != (char *)NULL) {
350 data = NULL;
351 r = yp_match(__ypdomain, "passwd.byname",
352 name, strlen(name),
353 &data, &datalen);
354 __ypmode = YPMODE_NONE;
355 free(name);
356 name = NULL;
357 if(r != 0) {
358 if(data)
359 free(data);
360 goto again;
361 }
362 bcopy(data, line, datalen);
363 free(data);
364 data = (char *)NULL;
365 } else { /* XXX */
366 __ypmode = YPMODE_NONE;
367 goto again;
368 }
369 break;
370 }
371
372 line[datalen] = '\0';
373 if (__ypparse(&_pw_passwd, line))
374 goto again;
375 return &_pw_passwd;
376 }
377 #endif
378
379 ++_pw_keynum;
380 bf[0] = _PW_KEYBYNUM;
381 bcopy((char *)&_pw_keynum, bf + 1, sizeof(_pw_keynum));
382 key.data = (u_char *)bf;
383 key.size = sizeof(_pw_keynum) + 1;
384 if(__hashpw(&key)) {
385 #ifdef YP
386 /* if we don't have YP at all, don't bother. */
387 if(has_yppw) {
388 if(_pw_passwd.pw_name[0] == '+') {
389 /* set the mode */
390 switch(_pw_passwd.pw_name[1]) {
391 case '\0':
392 __ypmode = YPMODE_FULL;
393 break;
394 case '@':
395 __ypmode = YPMODE_NETGRP;
396 setnetgrent(_pw_passwd.pw_name + 2);
397 break;
398 default:
399 __ypmode = YPMODE_USER;
400 name = strdup(_pw_passwd.pw_name + 1);
401 break;
402 }
403
404 /* save the prototype */
405 __ypproto_set();
406 goto again;
407 } else if(_pw_passwd.pw_name[0] == '-') {
408 /* an attempted exclusion */
409 switch(_pw_passwd.pw_name[1]) {
410 case '\0':
411 break;
412 case '@':
413 setnetgrent(_pw_passwd.pw_name + 2);
414 while(getnetgrent(&host, &user, &dom)) {
415 if(user && *user)
416 __ypexclude_add(user);
417 }
418 endnetgrent();
419 break;
420 default:
421 __ypexclude_add(_pw_passwd.pw_name + 1);
422 break;
423 }
424 goto again;
425 }
426 }
427 #endif
428 return &_pw_passwd;
429 }
430 return (struct passwd *)NULL;
431 }
432
433 #ifdef YP
434
435 /*
436 * See if the YP token is in the database. Only works if pwd_mkdb knows
437 * about the token.
438 */
439 static int
440 __has_yppw()
441 {
442 DBT key, data;
443 DBT pkey, pdata;
444 int len;
445 char bf[MAXLOGNAME];
446
447 key.data = (u_char *)__yp_token;
448 key.size = strlen(__yp_token);
449
450 /* Pre-token database support. */
451 bf[0] = _PW_KEYBYNAME;
452 bf[1] = '+';
453 pkey.data = (u_char *)bf;
454 pkey.size = 2;
455
456 if ((_pw_db->get)(_pw_db, &key, &data, 0)
457 && (_pw_db->get)(_pw_db, &pkey, &pdata, 0))
458 return(0); /* No YP. */
459 return(1);
460 }
461 #endif
462
463 struct passwd *
464 getpwnam(name)
465 const char *name;
466 {
467 DBT key;
468 int len, rval;
469 char bf[MAXLOGNAME + 1];
470
471 if (!_pw_db && !__initdb())
472 return((struct passwd *)NULL);
473
474 #ifdef YP
475 /*
476 * If YP is active, we must sequence through the passwd file
477 * in sequence.
478 */
479 if (__has_yppw()) {
480 int r;
481 int s = -1;
482 const char *host, *user, *dom;
483
484 for(_pw_keynum=1; _pw_keynum; _pw_keynum++) {
485 bf[0] = _PW_KEYBYNUM;
486 bcopy((char *)&_pw_keynum, bf + 1, sizeof(_pw_keynum));
487 key.data = (u_char *)bf;
488 key.size = sizeof(_pw_keynum) + 1;
489 if(__hashpw(&key) == 0)
490 break;
491 switch(_pw_passwd.pw_name[0]) {
492 case '+':
493 if(!__ypdomain) {
494 if(_yp_check(&__ypdomain) == 0) {
495 continue;
496 }
497 }
498 /* save the prototype */
499 __ypproto_set();
500
501 switch(_pw_passwd.pw_name[1]) {
502 case '\0':
503 if(__ypcurrent) {
504 free(__ypcurrent);
505 __ypcurrent = NULL;
506 }
507 r = yp_match(__ypdomain,
508 "passwd.byname",
509 name, strlen(name),
510 &__ypcurrent, &__ypcurrentlen);
511 if(r != 0) {
512 if(__ypcurrent)
513 free(__ypcurrent);
514 __ypcurrent = NULL;
515 continue;
516 }
517 break;
518 case '@':
519 pwnam_netgrp:
520 if(__ypcurrent) {
521 free(__ypcurrent);
522 __ypcurrent = NULL;
523 }
524 if(s == -1) /* first time */
525 setnetgrent(_pw_passwd.pw_name + 2);
526 s = getnetgrent(&host, &user, &dom);
527 if(s == 0) { /* end of group */
528 endnetgrent();
529 s = -1;
530 continue;
531 } else {
532 if(user && *user) {
533 r = yp_match(__ypdomain,
534 "passwd.byname",
535 user, strlen(user),
536 &__ypcurrent,
537 &__ypcurrentlen);
538 } else
539 goto pwnam_netgrp;
540 if(r != 0) {
541 if(__ypcurrent)
542 free(__ypcurrent);
543 __ypcurrent = NULL;
544 /*
545 * just because this
546 * user is bad, doesn't
547 * mean they all are.
548 */
549 goto pwnam_netgrp;
550 }
551 }
552 break;
553 default:
554 if(__ypcurrent) {
555 free(__ypcurrent);
556 __ypcurrent = NULL;
557 }
558 user = _pw_passwd.pw_name + 1;
559 r = yp_match(__ypdomain,
560 "passwd.byname",
561 user, strlen(user),
562 &__ypcurrent,
563 &__ypcurrentlen);
564 if(r != 0) {
565 if(__ypcurrent)
566 free(__ypcurrent);
567 __ypcurrent = NULL;
568 continue;
569 }
570 break;
571 }
572 bcopy(__ypcurrent, line, __ypcurrentlen);
573 line[__ypcurrentlen] = '\0';
574 if(__ypparse(&_pw_passwd, line)
575 || __ypexclude_is(_pw_passwd.pw_name)) {
576 if(s == 1) /* inside netgrp */
577 goto pwnam_netgrp;
578 continue;
579 }
580 break;
581 case '-':
582 /* attempted exclusion */
583 switch(_pw_passwd.pw_name[1]) {
584 case '\0':
585 break;
586 case '@':
587 setnetgrent(_pw_passwd.pw_name + 2);
588 while(getnetgrent(&host, &user, &dom)) {
589 if(user && *user)
590 __ypexclude_add(user);
591 }
592 endnetgrent();
593 break;
594 default:
595 __ypexclude_add(_pw_passwd.pw_name + 1);
596 break;
597 }
598 break;
599
600 continue;
601 }
602 if(strcmp(_pw_passwd.pw_name, name) == 0) {
603 if (!_pw_stayopen) {
604 (void)(_pw_db->close)(_pw_db);
605 _pw_db = (DB *)NULL;
606 }
607 if(__ypexclude != (DB *)NULL) {
608 (void)(__ypexclude->close)(__ypexclude);
609 __ypexclude = (DB *)NULL;
610 }
611 __ypproto = (struct passwd *)NULL;
612 return &_pw_passwd;
613 }
614 if(s == 1) /* inside netgrp */
615 goto pwnam_netgrp;
616 continue;
617 }
618 if (!_pw_stayopen) {
619 (void)(_pw_db->close)(_pw_db);
620 _pw_db = (DB *)NULL;
621 }
622 if(__ypexclude != (DB *)NULL) {
623 (void)(__ypexclude->close)(__ypexclude);
624 __ypexclude = (DB *)NULL;
625 }
626 __ypproto = (struct passwd *)NULL;
627 return (struct passwd *)NULL;
628 }
629 #endif /* YP */
630
631 bf[0] = _PW_KEYBYNAME;
632 len = strlen(name);
633 len = MIN(len, MAXLOGNAME);
634 bcopy(name, bf + 1, len);
635 key.data = (u_char *)bf;
636 key.size = len + 1;
637 rval = __hashpw(&key);
638
639 if (!_pw_stayopen) {
640 (void)(_pw_db->close)(_pw_db);
641 _pw_db = (DB *)NULL;
642 }
643 return(rval ? &_pw_passwd : (struct passwd *)NULL);
644 }
645
646 struct passwd *
647 #ifdef __STDC__
648 getpwuid(uid_t uid)
649 #else
650 getpwuid(uid)
651 int uid;
652 #endif
653 {
654 DBT key;
655 char bf[sizeof(_pw_keynum) + 1];
656 int keyuid, rval;
657
658 if (!_pw_db && !__initdb())
659 return((struct passwd *)NULL);
660
661 #ifdef YP
662 /*
663 * If YP is active, we must sequence through the passwd file
664 * in sequence.
665 */
666 if (__has_yppw()) {
667 char uidbuf[20];
668 int r;
669 int s = -1;
670 const char *host, *user, *dom;
671
672 snprintf(uidbuf, sizeof uidbuf, "%d", uid);
673 for(_pw_keynum=1; _pw_keynum; _pw_keynum++) {
674 bf[0] = _PW_KEYBYNUM;
675 bcopy((char *)&_pw_keynum, bf + 1, sizeof(_pw_keynum));
676 key.data = (u_char *)bf;
677 key.size = sizeof(_pw_keynum) + 1;
678 if(__hashpw(&key) == 0)
679 break;
680 switch(_pw_passwd.pw_name[0]) {
681 case '+':
682 if(!__ypdomain) {
683 if(_yp_check(&__ypdomain) == 0) {
684 continue;
685 }
686 }
687 /* save the prototype */
688 __ypproto_set();
689
690 switch(_pw_passwd.pw_name[1]) {
691 case '\0':
692 if(__ypcurrent) {
693 free(__ypcurrent);
694 __ypcurrent = NULL;
695 }
696 r = yp_match(__ypdomain, "passwd.byuid",
697 uidbuf, strlen(uidbuf),
698 &__ypcurrent, &__ypcurrentlen);
699 if(r != 0) {
700 if(__ypcurrent)
701 free(__ypcurrent);
702 __ypcurrent = NULL;
703 continue;
704 }
705 break;
706 case '@':
707 pwuid_netgrp:
708 if(__ypcurrent) {
709 free(__ypcurrent);
710 __ypcurrent = NULL;
711 }
712 if(s == -1) /* first time */
713 setnetgrent(_pw_passwd.pw_name + 2);
714 s = getnetgrent(&host, &user, &dom);
715 if(s == 0) { /* end of group */
716 endnetgrent();
717 s = -1;
718 continue;
719 } else {
720 if(user && *user) {
721 r = yp_match(__ypdomain,
722 "passwd.byname",
723 user, strlen(user),
724 &__ypcurrent,
725 &__ypcurrentlen);
726 } else
727 goto pwuid_netgrp;
728 if(r != 0) {
729 if(__ypcurrent)
730 free(__ypcurrent);
731 __ypcurrent = NULL;
732 /*
733 * just because this
734 * user is bad, doesn't
735 * mean they all are.
736 */
737 goto pwuid_netgrp;
738 }
739 }
740 break;
741 default:
742 if(__ypcurrent) {
743 free(__ypcurrent);
744 __ypcurrent = NULL;
745 }
746 user = _pw_passwd.pw_name + 1;
747 r = yp_match(__ypdomain,
748 "passwd.byname",
749 user, strlen(user),
750 &__ypcurrent,
751 &__ypcurrentlen);
752 if(r != 0) {
753 if(__ypcurrent)
754 free(__ypcurrent);
755 __ypcurrent = NULL;
756 continue;
757 }
758 break;
759 }
760 bcopy(__ypcurrent, line, __ypcurrentlen);
761 line[__ypcurrentlen] = '\0';
762 if(__ypparse(&_pw_passwd, line)
763 || __ypexclude_is(_pw_passwd.pw_name)) {
764 if(s == 1) /* inside netgroup */
765 goto pwuid_netgrp;
766 continue;
767 }
768 break;
769 case '-':
770 /* attempted exclusion */
771 switch(_pw_passwd.pw_name[1]) {
772 case '\0':
773 break;
774 case '@':
775 setnetgrent(_pw_passwd.pw_name + 2);
776 while(getnetgrent(&host, &user, &dom)) {
777 if(user && *user)
778 __ypexclude_add(user);
779 }
780 endnetgrent();
781 break;
782 default:
783 __ypexclude_add(_pw_passwd.pw_name + 1);
784 break;
785 }
786 break;
787
788 continue;
789 }
790 if( _pw_passwd.pw_uid == uid) {
791 if (!_pw_stayopen) {
792 (void)(_pw_db->close)(_pw_db);
793 _pw_db = (DB *)NULL;
794 }
795 if (__ypexclude != (DB *)NULL) {
796 (void)(__ypexclude->close)(__ypexclude);
797 __ypexclude = (DB *)NULL;
798 }
799 __ypproto = NULL;
800 return &_pw_passwd;
801 }
802 if(s == 1) /* inside netgroup */
803 goto pwuid_netgrp;
804 continue;
805 }
806 if (!_pw_stayopen) {
807 (void)(_pw_db->close)(_pw_db);
808 _pw_db = (DB *)NULL;
809 }
810 if(__ypexclude != (DB *)NULL) {
811 (void)(__ypexclude->close)(__ypexclude);
812 __ypexclude = (DB *)NULL;
813 }
814 __ypproto = (struct passwd *)NULL;
815 return (struct passwd *)NULL;
816 }
817 #endif /* YP */
818
819 bf[0] = _PW_KEYBYUID;
820 keyuid = uid;
821 bcopy(&keyuid, bf + 1, sizeof(keyuid));
822 key.data = (u_char *)bf;
823 key.size = sizeof(keyuid) + 1;
824 rval = __hashpw(&key);
825
826 if (!_pw_stayopen) {
827 (void)(_pw_db->close)(_pw_db);
828 _pw_db = (DB *)NULL;
829 }
830 return(rval ? &_pw_passwd : (struct passwd *)NULL);
831 }
832
833 int
834 setpassent(stayopen)
835 int stayopen;
836 {
837 _pw_keynum = 0;
838 _pw_stayopen = stayopen;
839 #ifdef YP
840 __ypmode = YPMODE_NONE;
841 if(__ypcurrent)
842 free(__ypcurrent);
843 __ypcurrent = NULL;
844 if(__ypexclude != (DB *)NULL) {
845 (void)(__ypexclude->close)(__ypexclude);
846 __ypexclude = (DB *)NULL;
847 }
848 __ypproto = (struct passwd *)NULL;
849 #endif
850 return(1);
851 }
852
853 void
854 setpwent()
855 {
856 (void) setpassent(0);
857 }
858
859 void
860 endpwent()
861 {
862 _pw_keynum = 0;
863 if (_pw_db) {
864 (void)(_pw_db->close)(_pw_db);
865 _pw_db = (DB *)NULL;
866 }
867 #ifdef YP
868 __ypmode = YPMODE_NONE;
869 if(__ypcurrent)
870 free(__ypcurrent);
871 __ypcurrent = NULL;
872 if(__ypexclude != (DB *)NULL) {
873 (void)(__ypexclude->close)(__ypexclude);
874 __ypexclude = (DB *)NULL;
875 }
876 __ypproto = (struct passwd *)NULL;
877 #endif
878 }
879
880 static int
881 __initdb()
882 {
883 static int warned;
884 char *p;
885
886 #ifdef YP
887 __ypmode = YPMODE_NONE;
888 #endif
889 p = (geteuid()) ? _PATH_MP_DB : _PATH_SMP_DB;
890 _pw_db = dbopen(p, O_RDONLY, 0, DB_HASH, NULL);
891 if (_pw_db)
892 return(1);
893 if (!warned)
894 syslog(LOG_ERR, "%s: %m", p);
895 warned = 1;
896 return(0);
897 }
898
899 static int
900 __hashpw(key)
901 DBT *key;
902 {
903 char *p, *t;
904 static u_int max;
905 static char *line;
906 DBT data;
907
908 if ((_pw_db->get)(_pw_db, key, &data, 0))
909 return(0);
910 p = (char *)data.data;
911 if (data.size > max && !(line = realloc(line, (max += 1024))))
912 return(0);
913
914 t = line;
915 #define EXPAND(e) e = t; while ((*t++ = *p++));
916 EXPAND(_pw_passwd.pw_name);
917 EXPAND(_pw_passwd.pw_passwd);
918 bcopy(p, (char *)&_pw_passwd.pw_uid, sizeof(int));
919 p += sizeof(int);
920 bcopy(p, (char *)&_pw_passwd.pw_gid, sizeof(int));
921 p += sizeof(int);
922 bcopy(p, (char *)&_pw_passwd.pw_change, sizeof(time_t));
923 p += sizeof(time_t);
924 EXPAND(_pw_passwd.pw_class);
925 EXPAND(_pw_passwd.pw_gecos);
926 EXPAND(_pw_passwd.pw_dir);
927 EXPAND(_pw_passwd.pw_shell);
928 bcopy(p, (char *)&_pw_passwd.pw_expire, sizeof(time_t));
929 p += sizeof(time_t);
930
931 /* See if there's any data left. If so, read in flags. */
932 if (data.size > (p - (char *)data.data)) {
933 bcopy(p, (char *)&_pw_flags, sizeof(int));
934 p += sizeof(int);
935 } else
936 _pw_flags = _PASSWORD_NOUID|_PASSWORD_NOGID; /* default */
937
938 return(1);
939 }
940