getpwent.c revision 1.42 1 /* $NetBSD: getpwent.c,v 1.42 1999/04/25 07:54:01 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 #include <sys/cdefs.h>
38 #if defined(LIBC_SCCS) && !defined(lint)
39 #if 0
40 static char sccsid[] = "@(#)getpwent.c 8.2 (Berkeley) 4/27/95";
41 #else
42 __RCSID("$NetBSD: getpwent.c,v 1.42 1999/04/25 07:54:01 lukem Exp $");
43 #endif
44 #endif /* LIBC_SCCS and not lint */
45
46 #include "namespace.h"
47 #include <sys/param.h>
48 #include <fcntl.h>
49 #include <db.h>
50 #include <syslog.h>
51 #include <pwd.h>
52 #include <utmp.h>
53 #include <errno.h>
54 #include <unistd.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <limits.h>
58 #include <netgroup.h>
59 #include <nsswitch.h>
60 #ifdef HESIOD
61 #include <hesiod.h>
62 #endif
63 #ifdef YP
64 #include <machine/param.h>
65 #include <stdio.h>
66 #include <rpc/rpc.h>
67 #include <rpcsvc/yp_prot.h>
68 #include <rpcsvc/ypclnt.h>
69 #endif
70
71 #ifdef __STDC__
72 #include <stdarg.h>
73 #else
74 #include <varargs.h>
75 #endif
76
77 #include "pw_private.h"
78
79 #if defined(YP) || defined(HESIOD)
80 #define _PASSWD_COMPAT
81 #endif
82
83 #ifdef __weak_alias
84 __weak_alias(endpwent,_endpwent);
85 __weak_alias(getpwent,_getpwent);
86 __weak_alias(getpwnam,_getpwnam);
87 __weak_alias(getpwuid,_getpwuid);
88 __weak_alias(setpassent,_setpassent);
89 __weak_alias(setpwent,_setpwent);
90 #endif
91
92
93 /*
94 * The lookup techniques and data extraction code here must be kept
95 * in sync with that in `pwd_mkdb'.
96 */
97
98 static struct passwd _pw_passwd; /* password structure */
99 static DB *_pw_db; /* password database */
100 static int _pw_keynum; /* key counter. no more records if -1 */
101 static int _pw_stayopen; /* keep fd's open */
102 static int _pw_flags; /* password flags */
103
104 static int __hashpw __P((DBT *));
105 static int __initdb __P((void));
106
107 const char __yp_token[] = "__YP!"; /* Let pwd_mkdb pull this in. */
108 static const ns_src compatsrc[] = {
109 { NSSRC_COMPAT, NS_SUCCESS },
110 { 0 }
111 };
112
113 #ifdef YP
114 static char *__ypcurrent, *__ypdomain;
115 static int __ypcurrentlen;
116 static int _pw_ypdone; /* non-zero if no more yp records */
117 #endif
118
119 #ifdef HESIOD
120 static int _pw_hesnum; /* hes counter. no more records if -1 */
121 #endif
122
123 #ifdef _PASSWD_COMPAT
124 enum _pwmode { PWMODE_NONE, PWMODE_FULL, PWMODE_USER, PWMODE_NETGRP };
125 static enum _pwmode __pwmode;
126
127 enum _ypmap { YPMAP_NONE, YPMAP_ADJUNCT, YPMAP_MASTER };
128
129 static struct passwd *__pwproto = (struct passwd *)NULL;
130 static int __pwproto_flags;
131 static char line[1024];
132 static long prbuf[1024 / sizeof(long)];
133 static DB *__pwexclude = (DB *)NULL;
134
135 static int __pwexclude_add __P((const char *));
136 static int __pwexclude_is __P((const char *));
137 static void __pwproto_set __P((void));
138 static int __ypmaptype __P((void));
139 static int __pwparse __P((struct passwd *, char *));
140
141 /* macros for deciding which YP maps to use. */
142 #define PASSWD_BYNAME (__ypmaptype() == YPMAP_MASTER \
143 ? "master.passwd.byname" : "passwd.byname")
144 #define PASSWD_BYUID (__ypmaptype() == YPMAP_MASTER \
145 ? "master.passwd.byuid" : "passwd.byuid")
146
147 /*
148 * add a name to the compat mode exclude list
149 */
150 static int
151 __pwexclude_add(name)
152 const char *name;
153 {
154 DBT key;
155 DBT data;
156
157 /* initialize the exclusion table if needed. */
158 if(__pwexclude == (DB *)NULL) {
159 __pwexclude = dbopen(NULL, O_RDWR, 600, DB_HASH, NULL);
160 if(__pwexclude == (DB *)NULL)
161 return 1;
162 }
163
164 /* set up the key */
165 key.size = strlen(name);
166 /* LINTED key does not get modified */
167 key.data = (char *)name;
168
169 /* data is nothing. */
170 data.data = NULL;
171 data.size = 0;
172
173 /* store it */
174 if((__pwexclude->put)(__pwexclude, &key, &data, 0) == -1)
175 return 1;
176
177 return 0;
178 }
179
180 /*
181 * test if a name is on the compat mode exclude list
182 */
183 static int
184 __pwexclude_is(name)
185 const char *name;
186 {
187 DBT key;
188 DBT data;
189
190 if(__pwexclude == (DB *)NULL)
191 return 0; /* nothing excluded */
192
193 /* set up the key */
194 key.size = strlen(name);
195 /* LINTED key does not get modified */
196 key.data = (char *)name;
197
198 if((__pwexclude->get)(__pwexclude, &key, &data, 0) == 0)
199 return 1; /* excluded */
200
201 return 0;
202 }
203
204 /*
205 * setup the compat mode prototype template
206 */
207 static void
208 __pwproto_set()
209 {
210 char *ptr;
211 struct passwd *pw = &_pw_passwd;
212
213 /* make this the new prototype */
214 ptr = (char *)(void *)prbuf;
215
216 /* first allocate the struct. */
217 __pwproto = (struct passwd *)(void *)ptr;
218 ptr += sizeof(struct passwd);
219
220 /* name */
221 if(pw->pw_name && (pw->pw_name)[0]) {
222 ptr = (char *)ALIGN((u_long)ptr);
223 memmove(ptr, pw->pw_name, strlen(pw->pw_name) + 1);
224 __pwproto->pw_name = ptr;
225 ptr += (strlen(pw->pw_name) + 1);
226 } else
227 __pwproto->pw_name = (char *)NULL;
228
229 /* password */
230 if(pw->pw_passwd && (pw->pw_passwd)[0]) {
231 ptr = (char *)ALIGN((u_long)ptr);
232 memmove(ptr, pw->pw_passwd, strlen(pw->pw_passwd) + 1);
233 __pwproto->pw_passwd = ptr;
234 ptr += (strlen(pw->pw_passwd) + 1);
235 } else
236 __pwproto->pw_passwd = (char *)NULL;
237
238 /* uid */
239 __pwproto->pw_uid = pw->pw_uid;
240
241 /* gid */
242 __pwproto->pw_gid = pw->pw_gid;
243
244 /* change (ignored anyway) */
245 __pwproto->pw_change = pw->pw_change;
246
247 /* class (ignored anyway) */
248 __pwproto->pw_class = "";
249
250 /* gecos */
251 if(pw->pw_gecos && (pw->pw_gecos)[0]) {
252 ptr = (char *)ALIGN((u_long)ptr);
253 memmove(ptr, pw->pw_gecos, strlen(pw->pw_gecos) + 1);
254 __pwproto->pw_gecos = ptr;
255 ptr += (strlen(pw->pw_gecos) + 1);
256 } else
257 __pwproto->pw_gecos = (char *)NULL;
258
259 /* dir */
260 if(pw->pw_dir && (pw->pw_dir)[0]) {
261 ptr = (char *)ALIGN((u_long)ptr);
262 memmove(ptr, pw->pw_dir, strlen(pw->pw_dir) + 1);
263 __pwproto->pw_dir = ptr;
264 ptr += (strlen(pw->pw_dir) + 1);
265 } else
266 __pwproto->pw_dir = (char *)NULL;
267
268 /* shell */
269 if(pw->pw_shell && (pw->pw_shell)[0]) {
270 ptr = (char *)ALIGN((u_long)ptr);
271 memmove(ptr, pw->pw_shell, strlen(pw->pw_shell) + 1);
272 __pwproto->pw_shell = ptr;
273 ptr += (strlen(pw->pw_shell) + 1);
274 } else
275 __pwproto->pw_shell = (char *)NULL;
276
277 /* expire (ignored anyway) */
278 __pwproto->pw_expire = pw->pw_expire;
279
280 /* flags */
281 __pwproto_flags = _pw_flags;
282 }
283
284 static int
285 __ypmaptype()
286 {
287 static int maptype = -1;
288 int order, r;
289
290 if (maptype != -1)
291 return (maptype);
292
293 maptype = YPMAP_NONE;
294 if (geteuid() != 0)
295 return (maptype);
296
297 if (!__ypdomain) {
298 if( _yp_check(&__ypdomain) == 0)
299 return (maptype);
300 }
301
302 r = yp_order(__ypdomain, "master.passwd.byname", &order);
303 if (r == 0) {
304 maptype = YPMAP_MASTER;
305 return (maptype);
306 }
307
308 /*
309 * NIS+ in YP compat mode doesn't support
310 * YPPROC_ORDER -- no point in continuing.
311 */
312 if (r == YPERR_YPERR)
313 return (maptype);
314
315 /* master.passwd doesn't exist -- try passwd.adjunct */
316 if (r == YPERR_MAP) {
317 r = yp_order(__ypdomain, "passwd.adjunct.byname", &order);
318 if (r == 0)
319 maptype = YPMAP_ADJUNCT;
320 return (maptype);
321 }
322
323 return (maptype);
324 }
325
326 /*
327 * parse a passwd file line (from NIS or HESIOD).
328 * assumed to be `old-style' if maptype != YPMAP_MASTER.
329 */
330 static int
331 __pwparse(pw, s)
332 struct passwd *pw;
333 char *s;
334 {
335 static char adjunctpw[YPMAXRECORD + 2];
336 int flags, maptype;
337
338 maptype = __ypmaptype();
339 flags = _PASSWORD_NOWARN;
340 if (maptype != YPMAP_MASTER)
341 flags |= _PASSWORD_OLDFMT;
342 if (! __pw_scan(s, pw, &flags))
343 return 1;
344
345 /* now let the prototype override, if set. */
346 if(__pwproto != (struct passwd *)NULL) {
347 #ifdef PW_OVERRIDE_PASSWD
348 if(__pwproto->pw_passwd != (char *)NULL)
349 pw->pw_passwd = __pwproto->pw_passwd;
350 #endif
351 if(!(__pwproto_flags & _PASSWORD_NOUID))
352 pw->pw_uid = __pwproto->pw_uid;
353 if(!(__pwproto_flags & _PASSWORD_NOGID))
354 pw->pw_gid = __pwproto->pw_gid;
355 if(__pwproto->pw_gecos != (char *)NULL)
356 pw->pw_gecos = __pwproto->pw_gecos;
357 if(__pwproto->pw_dir != (char *)NULL)
358 pw->pw_dir = __pwproto->pw_dir;
359 if(__pwproto->pw_shell != (char *)NULL)
360 pw->pw_shell = __pwproto->pw_shell;
361 }
362 if ((maptype == YPMAP_ADJUNCT) &&
363 (strstr(pw->pw_passwd, "##") != NULL)) {
364 char *data, *bp;
365 int datalen;
366
367 if (yp_match(__ypdomain, "passwd.adjunct.byname", pw->pw_name,
368 (int)strlen(pw->pw_name), &data, &datalen) == 0) {
369 if (datalen > sizeof(adjunctpw) - 1)
370 datalen = sizeof(adjunctpw) - 1;
371 strncpy(adjunctpw, data, (size_t)datalen);
372
373 /* skip name to get password */
374 if ((bp = strsep(&data, ":")) != NULL &&
375 (bp = strsep(&data, ":")) != NULL)
376 pw->pw_passwd = bp;
377 }
378 }
379 return 0;
380 }
381 #endif /* _PASSWD_COMPAT */
382
383 /*
384 * local files implementation of getpw*()
385 * varargs: type, [ uid (type == _PW_KEYBYUID) | name (type == _PW_KEYBYNAME) ]
386 */
387 static int _local_getpw __P((void *, void *, va_list));
388
389 /*ARGSUSED*/
390 static int
391 _local_getpw(rv, cb_data, ap)
392 void *rv;
393 void *cb_data;
394 va_list ap;
395 {
396 DBT key;
397 char bf[/*CONSTCOND*/ MAX(MAXLOGNAME, sizeof(_pw_keynum)) + 1];
398 uid_t uid;
399 int search, len, rval;
400 const char *name;
401
402 if (!_pw_db && !__initdb())
403 return NS_UNAVAIL;
404
405 search = va_arg(ap, int);
406 bf[0] = search;
407 switch (search) {
408 case _PW_KEYBYNUM:
409 if (_pw_keynum == -1)
410 return NS_NOTFOUND; /* no more local records */
411 ++_pw_keynum;
412 memmove(bf + 1, &_pw_keynum, sizeof(_pw_keynum));
413 key.size = sizeof(_pw_keynum) + 1;
414 break;
415 case _PW_KEYBYNAME:
416 name = va_arg(ap, const char *);
417 len = strlen(name);
418 memmove(bf + 1, name, (size_t)MIN(len, MAXLOGNAME));
419 key.size = len + 1;
420 break;
421 case _PW_KEYBYUID:
422 uid = va_arg(ap, uid_t);
423 memmove(bf + 1, &uid, sizeof(len));
424 key.size = sizeof(uid) + 1;
425 break;
426 default:
427 abort();
428 }
429
430 key.data = (u_char *)bf;
431 rval = __hashpw(&key);
432 if (rval == NS_NOTFOUND && search == _PW_KEYBYNUM)
433 _pw_keynum = -1; /* flag `no more local records' */
434
435 if (!_pw_stayopen && (search != _PW_KEYBYNUM)) {
436 (void)(_pw_db->close)(_pw_db);
437 _pw_db = (DB *)NULL;
438 }
439 return (rval);
440 }
441
442 #ifdef HESIOD
443 /*
444 * hesiod implementation of getpw*()
445 * varargs: type, [ uid (type == _PW_KEYBYUID) | name (type == _PW_KEYBYNAME) ]
446 */
447 static int _dns_getpw __P((void *, void *, va_list));
448
449 /*ARGSUSED*/
450 static int
451 _dns_getpw(rv, cb_data, ap)
452 void *rv;
453 void *cb_data;
454 va_list ap;
455 {
456 const char *name;
457 uid_t uid;
458 int search;
459
460 const char *map;
461 char **hp;
462 void *context;
463 int r;
464
465 search = va_arg(ap, int);
466 switch (search) {
467 case _PW_KEYBYNUM:
468 if (_pw_hesnum == -1)
469 return NS_NOTFOUND; /* no more hesiod records */
470 snprintf(line, sizeof(line) - 1, "passwd-%u", _pw_hesnum);
471 _pw_hesnum++;
472 map = "passwd";
473 break;
474 case _PW_KEYBYNAME:
475 name = va_arg(ap, const char *);
476 strncpy(line, name, sizeof(line));
477 map = "passwd";
478 break;
479 case _PW_KEYBYUID:
480 uid = va_arg(ap, uid_t);
481 snprintf(line, sizeof(line), "%u", (unsigned int)uid);
482 map = "uid"; /* XXX this is `passwd' on ultrix */
483 break;
484 default:
485 abort();
486 }
487 line[sizeof(line) - 1] = '\0';
488
489 r = NS_UNAVAIL;
490 if (hesiod_init(&context) == -1)
491 return (r);
492
493 hp = hesiod_resolve(context, line, map);
494 if (hp == NULL) {
495 if (errno == ENOENT) {
496 /* flag `no more hesiod records' */
497 if (search == _PW_KEYBYNUM)
498 _pw_hesnum = -1;
499 r = NS_NOTFOUND;
500 }
501 goto cleanup_dns_getpw;
502 }
503
504 strncpy(line, hp[0], sizeof(line)); /* only check first elem */
505 line[sizeof(line) - 1] = '\0';
506 hesiod_free_list(context, hp);
507 if (__pwparse(&_pw_passwd, line))
508 r = NS_UNAVAIL;
509 else
510 r = NS_SUCCESS;
511 cleanup_dns_getpw:
512 hesiod_end(context);
513 return (r);
514 }
515 #endif
516
517 #ifdef YP
518 /*
519 * nis implementation of getpw*()
520 * varargs: type, [ uid (type == _PW_KEYBYUID) | name (type == _PW_KEYBYNAME) ]
521 */
522 static int _nis_getpw __P((void *, void *, va_list));
523
524 /*ARGSUSED*/
525 static int
526 _nis_getpw(rv, cb_data, ap)
527 void *rv;
528 void *cb_data;
529 va_list ap;
530 {
531 const char *name;
532 uid_t uid;
533 int search;
534 char *key, *data;
535 const char *map;
536 int keylen, datalen, r, rval;
537
538 if(__ypdomain == NULL) {
539 if(_yp_check(&__ypdomain) == 0)
540 return NS_UNAVAIL;
541 }
542
543 map = PASSWD_BYNAME;
544 search = va_arg(ap, int);
545 switch (search) {
546 case _PW_KEYBYNUM:
547 break;
548 case _PW_KEYBYNAME:
549 name = va_arg(ap, const char *);
550 strncpy(line, name, sizeof(line));
551 break;
552 case _PW_KEYBYUID:
553 uid = va_arg(ap, uid_t);
554 snprintf(line, sizeof(line), "%u", (unsigned int)uid);
555 map = PASSWD_BYUID;
556 break;
557 default:
558 abort();
559 }
560 line[sizeof(line) - 1] = '\0';
561 rval = NS_UNAVAIL;
562 if (search != _PW_KEYBYNUM) {
563 data = NULL;
564 r = yp_match(__ypdomain, map, line, (int)strlen(line),
565 &data, &datalen);
566 if (r == YPERR_KEY)
567 rval = NS_NOTFOUND;
568 if (r != 0) {
569 if (data)
570 free(data);
571 return (rval);
572 }
573 data[datalen] = '\0'; /* clear trailing \n */
574 strncpy(line, data, sizeof(line));
575 line[sizeof(line) - 1] = '\0';
576 free(data);
577 if (__pwparse(&_pw_passwd, line))
578 return NS_UNAVAIL;
579 return NS_SUCCESS;
580 }
581
582 if (_pw_ypdone)
583 return NS_NOTFOUND;
584 for (;;) {
585 data = key = NULL;
586 if (__ypcurrent) {
587 r = yp_next(__ypdomain, map,
588 __ypcurrent, __ypcurrentlen,
589 &key, &keylen, &data, &datalen);
590 free(__ypcurrent);
591 switch (r) {
592 case 0:
593 __ypcurrent = key;
594 __ypcurrentlen = keylen;
595 break;
596 case YPERR_NOMORE:
597 __ypcurrent = NULL;
598 /* flag `no more yp records' */
599 _pw_ypdone = 1;
600 rval = NS_NOTFOUND;
601 }
602 } else {
603 r = yp_first(__ypdomain, map, &__ypcurrent,
604 &__ypcurrentlen, &data, &datalen);
605 }
606 if (r != 0) {
607 if (key)
608 free(key);
609 if (data)
610 free(data);
611 return (rval);
612 }
613 data[datalen] = '\0'; /* clear trailing \n */
614 strncpy(line, data, sizeof(line));
615 line[sizeof(line) - 1] = '\0';
616 free(data);
617 if (! __pwparse(&_pw_passwd, line))
618 return NS_SUCCESS;
619 }
620 /* NOTREACHED */
621 } /* _nis_getpw */
622 #endif
623
624 #ifdef _PASSWD_COMPAT
625 /*
626 * See if the compat token is in the database. Only works if pwd_mkdb knows
627 * about the token.
628 */
629 static int __has_compatpw __P((void));
630
631 static int
632 __has_compatpw()
633 {
634 DBT key, data;
635 DBT pkey, pdata;
636 char bf[MAXLOGNAME];
637
638 /*LINTED*/
639 key.data = (u_char *)__yp_token;
640 key.size = strlen(__yp_token);
641
642 /* Pre-token database support. */
643 bf[0] = _PW_KEYBYNAME;
644 bf[1] = '+';
645 pkey.data = (u_char *)bf;
646 pkey.size = 2;
647
648 if ((_pw_db->get)(_pw_db, &key, &data, 0)
649 && (_pw_db->get)(_pw_db, &pkey, &pdata, 0))
650 return 0; /* No compat token */
651 return 1;
652 }
653
654 /*
655 * log an error if "files" or "compat" is specified in passwd_compat database
656 */
657 static int _bad_getpw __P((void *, void *, va_list));
658
659 /*ARGSUSED*/
660 static int
661 _bad_getpw(rv, cb_data, ap)
662 void *rv;
663 void *cb_data;
664 va_list ap;
665 {
666 static int warned;
667 if (!warned) {
668 syslog(LOG_ERR,
669 "nsswitch.conf passwd_compat database can't use '%s'",
670 (char *)cb_data);
671 }
672 warned = 1;
673 return NS_UNAVAIL;
674 }
675
676 /*
677 * when a name lookup in compat mode is required (e.g., '+name', or a name in
678 * '+@netgroup'), look it up in the 'passwd_compat' nsswitch database.
679 * only Hesiod and NIS is supported - it doesn't make sense to lookup
680 * compat names from 'files' or 'compat'.
681 */
682 static int __getpwcompat __P((int, uid_t, const char *));
683
684 static int
685 __getpwcompat(type, uid, name)
686 int type;
687 uid_t uid;
688 const char *name;
689 {
690 static const ns_dtab dtab[] = {
691 NS_FILES_CB(_bad_getpw, "files")
692 NS_DNS_CB(_dns_getpw, NULL)
693 NS_NIS_CB(_nis_getpw, NULL)
694 NS_COMPAT_CB(_bad_getpw, "compat")
695 { 0 }
696 };
697 static const ns_src defaultnis[] = {
698 { NSSRC_NIS, NS_SUCCESS },
699 { 0 }
700 };
701
702 switch (type) {
703 case _PW_KEYBYNUM:
704 return nsdispatch(NULL, dtab, NSDB_PASSWD_COMPAT, "getpwcompat",
705 defaultnis, type);
706 case _PW_KEYBYNAME:
707 return nsdispatch(NULL, dtab, NSDB_PASSWD_COMPAT, "getpwcompat",
708 defaultnis, type, name);
709 case _PW_KEYBYUID:
710 return nsdispatch(NULL, dtab, NSDB_PASSWD_COMPAT, "getpwcompat",
711 defaultnis, type, uid);
712 default:
713 abort();
714 /*NOTREACHED*/
715 }
716 }
717 #endif /* _PASSWD_COMPAT */
718
719 /*
720 * compat implementation of getpwent()
721 * varargs (ignored):
722 * type, [ uid (type == _PW_KEYBYUID) | name (type == _PW_KEYBYNAME) ]
723 */
724 static int _compat_getpwent __P((void *, void *, va_list));
725
726 /*ARGSUSED*/
727 static int
728 _compat_getpwent(rv, cb_data, ap)
729 void *rv;
730 void *cb_data;
731 va_list ap;
732 {
733 DBT key;
734 char bf[sizeof(_pw_keynum) + 1];
735 #ifdef _PASSWD_COMPAT
736 static char *name = NULL;
737 const char *user, *host, *dom;
738 int has_compatpw, rval;
739 #endif
740
741 if (!_pw_db && !__initdb())
742 return NS_UNAVAIL;
743
744 #ifdef _PASSWD_COMPAT
745 has_compatpw = __has_compatpw();
746
747 again:
748 if (has_compatpw && (__pwmode != PWMODE_NONE)) {
749 int r;
750
751 switch (__pwmode) {
752 case PWMODE_FULL:
753 r = __getpwcompat(_PW_KEYBYNUM, 0, NULL);
754 if (r == NS_SUCCESS)
755 return r;
756 __pwmode = PWMODE_NONE;
757 break;
758
759 case PWMODE_NETGRP:
760 r = getnetgrent(&host, &user, &dom);
761 if (r == 0) { /* end of group */
762 endnetgrent();
763 __pwmode = PWMODE_NONE;
764 break;
765 }
766 if (!user || !*user)
767 break;
768 r = __getpwcompat(_PW_KEYBYNAME, 0, user);
769 if (r == NS_SUCCESS)
770 return r;
771 break;
772
773 case PWMODE_USER:
774 if (name == NULL) {
775 __pwmode = PWMODE_NONE;
776 break;
777 }
778 r = __getpwcompat(_PW_KEYBYNAME, 0, name);
779 free(name);
780 name = NULL;
781 if (r == NS_SUCCESS)
782 return r;
783 break;
784
785 case PWMODE_NONE:
786 abort();
787 }
788 goto again;
789 }
790 #endif
791
792 if (_pw_keynum == -1)
793 return NS_NOTFOUND; /* no more local records */
794 ++_pw_keynum;
795 bf[0] = _PW_KEYBYNUM;
796 memmove(bf + 1, &_pw_keynum, sizeof(_pw_keynum));
797 key.data = (u_char *)bf;
798 key.size = sizeof(_pw_keynum) + 1;
799 rval = __hashpw(&key);
800 if (rval == NS_NOTFOUND)
801 _pw_keynum = -1; /* flag `no more local records' */
802 else if (rval == NS_SUCCESS) {
803 #ifdef _PASSWD_COMPAT
804 /* if we don't have YP at all, don't bother. */
805 if (has_compatpw) {
806 if(_pw_passwd.pw_name[0] == '+') {
807 /* set the mode */
808 switch(_pw_passwd.pw_name[1]) {
809 case '\0':
810 __pwmode = PWMODE_FULL;
811 break;
812 case '@':
813 __pwmode = PWMODE_NETGRP;
814 setnetgrent(_pw_passwd.pw_name + 2);
815 break;
816 default:
817 __pwmode = PWMODE_USER;
818 name = strdup(_pw_passwd.pw_name + 1);
819 break;
820 }
821
822 /* save the prototype */
823 __pwproto_set();
824 goto again;
825 } else if(_pw_passwd.pw_name[0] == '-') {
826 /* an attempted exclusion */
827 switch(_pw_passwd.pw_name[1]) {
828 case '\0':
829 break;
830 case '@':
831 setnetgrent(_pw_passwd.pw_name + 2);
832 while(getnetgrent(&host, &user, &dom)) {
833 if(user && *user)
834 __pwexclude_add(user);
835 }
836 endnetgrent();
837 break;
838 default:
839 __pwexclude_add(_pw_passwd.pw_name + 1);
840 break;
841 }
842 goto again;
843 }
844 }
845 #endif
846 }
847 return (rval);
848 }
849
850 /*
851 * compat implementation of getpwnam() and getpwuid()
852 * varargs: type, [ uid (type == _PW_KEYBYUID) | name (type == _PW_KEYBYNAME) ]
853 */
854 static int _compat_getpw __P((void *, void *, va_list));
855
856 static int
857 _compat_getpw(rv, cb_data, ap)
858 void *rv;
859 void *cb_data;
860 va_list ap;
861 {
862 #ifdef _PASSWD_COMPAT
863 DBT key;
864 int search, rval, r, s, keynum;
865 uid_t uid;
866 char bf[sizeof(keynum) + 1];
867 const char *name, *host, *user, *dom;
868 #endif
869
870 if (!_pw_db && !__initdb())
871 return NS_UNAVAIL;
872
873 /*
874 * If there isn't a compat token in the database, use files.
875 */
876 #ifdef _PASSWD_COMPAT
877 if (! __has_compatpw())
878 #endif
879 return (_local_getpw(rv, cb_data, ap));
880
881 #ifdef _PASSWD_COMPAT
882 search = va_arg(ap, int);
883 uid = 0;
884 name = NULL;
885 rval = NS_NOTFOUND;
886 switch (search) {
887 case _PW_KEYBYNAME:
888 name = va_arg(ap, const char *);
889 break;
890 case _PW_KEYBYUID:
891 uid = va_arg(ap, uid_t);
892 break;
893 default:
894 abort();
895 }
896
897 for (s = -1, keynum = 1 ; ; keynum++) {
898 bf[0] = _PW_KEYBYNUM;
899 memmove(bf + 1, &keynum, sizeof(keynum));
900 key.data = (u_char *)bf;
901 key.size = sizeof(keynum) + 1;
902 if(__hashpw(&key) != NS_SUCCESS)
903 break;
904 switch(_pw_passwd.pw_name[0]) {
905 case '+':
906 /* save the prototype */
907 __pwproto_set();
908
909 switch(_pw_passwd.pw_name[1]) {
910 case '\0':
911 r = __getpwcompat(search, uid, name);
912 if (r != NS_SUCCESS)
913 continue;
914 break;
915 case '@':
916 pwnam_netgrp:
917 #if 0 /* XXX: is this a hangover from pre-nsswitch? */
918 if(__ypcurrent) {
919 free(__ypcurrent);
920 __ypcurrent = NULL;
921 }
922 #endif
923 if (s == -1) /* first time */
924 setnetgrent(_pw_passwd.pw_name + 2);
925 s = getnetgrent(&host, &user, &dom);
926 if (s == 0) { /* end of group */
927 endnetgrent();
928 s = -1;
929 continue;
930 }
931 if (!user || !*user)
932 goto pwnam_netgrp;
933
934 r = __getpwcompat(_PW_KEYBYNAME, 0, user);
935
936 if (r == NS_UNAVAIL)
937 return r;
938 if (r == NS_NOTFOUND) {
939 /*
940 * just because this user is bad
941 * it doesn't mean they all are.
942 */
943 goto pwnam_netgrp;
944 }
945 break;
946 default:
947 user = _pw_passwd.pw_name + 1;
948 r = __getpwcompat(_PW_KEYBYNAME, 0, user);
949
950 if (r == NS_UNAVAIL)
951 return r;
952 if (r == NS_NOTFOUND)
953 continue;
954 break;
955 }
956 if(__pwexclude_is(_pw_passwd.pw_name)) {
957 if(s == 1) /* inside netgroup */
958 goto pwnam_netgrp;
959 continue;
960 }
961 break;
962 case '-':
963 /* attempted exclusion */
964 switch(_pw_passwd.pw_name[1]) {
965 case '\0':
966 break;
967 case '@':
968 setnetgrent(_pw_passwd.pw_name + 2);
969 while(getnetgrent(&host, &user, &dom)) {
970 if(user && *user)
971 __pwexclude_add(user);
972 }
973 endnetgrent();
974 break;
975 default:
976 __pwexclude_add(_pw_passwd.pw_name + 1);
977 break;
978 }
979 break;
980 }
981 if ((search == _PW_KEYBYNAME &&
982 strcmp(_pw_passwd.pw_name, name) == 0)
983 || (search == _PW_KEYBYUID && _pw_passwd.pw_uid == uid)) {
984 rval = NS_SUCCESS;
985 break;
986 }
987 if(s == 1) /* inside netgroup */
988 goto pwnam_netgrp;
989 continue;
990 }
991 __pwproto = (struct passwd *)NULL;
992
993 if (!_pw_stayopen) {
994 (void)(_pw_db->close)(_pw_db);
995 _pw_db = (DB *)NULL;
996 }
997 if(__pwexclude != (DB *)NULL) {
998 (void)(__pwexclude->close)(__pwexclude);
999 __pwexclude = (DB *)NULL;
1000 }
1001 return rval;
1002 #endif /* _PASSWD_COMPAT */
1003 }
1004
1005 struct passwd *
1006 getpwent()
1007 {
1008 int r;
1009 static const ns_dtab dtab[] = {
1010 NS_FILES_CB(_local_getpw, NULL)
1011 NS_DNS_CB(_dns_getpw, NULL)
1012 NS_NIS_CB(_nis_getpw, NULL)
1013 NS_COMPAT_CB(_compat_getpwent, NULL)
1014 { 0 }
1015 };
1016
1017 r = nsdispatch(NULL, dtab, NSDB_PASSWD, "getpwent", compatsrc,
1018 _PW_KEYBYNUM);
1019 if (r != NS_SUCCESS)
1020 return (struct passwd *)NULL;
1021 return &_pw_passwd;
1022 }
1023
1024 struct passwd *
1025 getpwnam(name)
1026 const char *name;
1027 {
1028 int r;
1029 static const ns_dtab dtab[] = {
1030 NS_FILES_CB(_local_getpw, NULL)
1031 NS_DNS_CB(_dns_getpw, NULL)
1032 NS_NIS_CB(_nis_getpw, NULL)
1033 NS_COMPAT_CB(_compat_getpw, NULL)
1034 { 0 }
1035 };
1036
1037 if (name == NULL || name[0] == '\0')
1038 return (struct passwd *)NULL;
1039
1040 r = nsdispatch(NULL, dtab, NSDB_PASSWD, "getpwnam", compatsrc,
1041 _PW_KEYBYNAME, name);
1042 return (r == NS_SUCCESS ? &_pw_passwd : (struct passwd *)NULL);
1043 }
1044
1045 struct passwd *
1046 getpwuid(uid)
1047 uid_t uid;
1048 {
1049 int r;
1050 static const ns_dtab dtab[] = {
1051 NS_FILES_CB(_local_getpw, NULL)
1052 NS_DNS_CB(_dns_getpw, NULL)
1053 NS_NIS_CB(_nis_getpw, NULL)
1054 NS_COMPAT_CB(_compat_getpw, NULL)
1055 { 0 }
1056 };
1057
1058 r = nsdispatch(NULL, dtab, NSDB_PASSWD, "getpwuid", compatsrc,
1059 _PW_KEYBYUID, uid);
1060 return (r == NS_SUCCESS ? &_pw_passwd : (struct passwd *)NULL);
1061 }
1062
1063 int
1064 setpassent(stayopen)
1065 int stayopen;
1066 {
1067 _pw_keynum = 0;
1068 _pw_stayopen = stayopen;
1069 #ifdef YP
1070 __pwmode = PWMODE_NONE;
1071 if(__ypcurrent)
1072 free(__ypcurrent);
1073 __ypcurrent = NULL;
1074 _pw_ypdone = 0;
1075 #endif
1076 #ifdef HESIOD
1077 _pw_hesnum = 0;
1078 #endif
1079 #ifdef _PASSWD_COMPAT
1080 if(__pwexclude != (DB *)NULL) {
1081 (void)(__pwexclude->close)(__pwexclude);
1082 __pwexclude = (DB *)NULL;
1083 }
1084 __pwproto = (struct passwd *)NULL;
1085 #endif
1086 return 1;
1087 }
1088
1089 void
1090 setpwent()
1091 {
1092 (void) setpassent(0);
1093 }
1094
1095 void
1096 endpwent()
1097 {
1098 _pw_keynum = 0;
1099 if (_pw_db) {
1100 (void)(_pw_db->close)(_pw_db);
1101 _pw_db = (DB *)NULL;
1102 }
1103 #ifdef _PASSWD_COMPAT
1104 __pwmode = PWMODE_NONE;
1105 #endif
1106 #ifdef YP
1107 if(__ypcurrent)
1108 free(__ypcurrent);
1109 __ypcurrent = NULL;
1110 _pw_ypdone = 0;
1111 #endif
1112 #ifdef HESIOD
1113 _pw_hesnum = 0;
1114 #endif
1115 #ifdef _PASSWD_COMPAT
1116 if(__pwexclude != (DB *)NULL) {
1117 (void)(__pwexclude->close)(__pwexclude);
1118 __pwexclude = (DB *)NULL;
1119 }
1120 __pwproto = (struct passwd *)NULL;
1121 #endif
1122 }
1123
1124 static int
1125 __initdb()
1126 {
1127 static int warned;
1128 char *p;
1129
1130 #ifdef _PASSWD_COMPAT
1131 __pwmode = PWMODE_NONE;
1132 #endif
1133 if (geteuid() == 0) {
1134 _pw_db = dbopen((p = _PATH_SMP_DB), O_RDONLY, 0, DB_HASH, NULL);
1135 if (_pw_db)
1136 return(1);
1137 }
1138 _pw_db = dbopen((p = _PATH_MP_DB), O_RDONLY, 0, DB_HASH, NULL);
1139 if (_pw_db)
1140 return 1;
1141 if (!warned)
1142 syslog(LOG_ERR, "%s: %m", p);
1143 warned = 1;
1144 return 0;
1145 }
1146
1147 static int
1148 __hashpw(key)
1149 DBT *key;
1150 {
1151 char *p, *t;
1152 static u_int max;
1153 static char *buf;
1154 DBT data;
1155
1156 switch ((_pw_db->get)(_pw_db, key, &data, 0)) {
1157 case 0:
1158 break; /* found */
1159 case 1:
1160 return NS_NOTFOUND;
1161 case -1:
1162 return NS_UNAVAIL; /* error in db routines */
1163 default:
1164 abort();
1165 }
1166
1167 p = (char *)data.data;
1168 if (data.size > max && !(buf = realloc(buf, (max += 1024))))
1169 return NS_UNAVAIL;
1170
1171 /* THIS CODE MUST MATCH THAT IN pwd_mkdb. */
1172 t = buf;
1173 #define EXPAND(e) e = t; while ((*t++ = *p++));
1174 #define SCALAR(v) memmove(&(v), p, sizeof v); p += sizeof v
1175 EXPAND(_pw_passwd.pw_name);
1176 EXPAND(_pw_passwd.pw_passwd);
1177 SCALAR(_pw_passwd.pw_uid);
1178 SCALAR(_pw_passwd.pw_gid);
1179 SCALAR(_pw_passwd.pw_change);
1180 EXPAND(_pw_passwd.pw_class);
1181 EXPAND(_pw_passwd.pw_gecos);
1182 EXPAND(_pw_passwd.pw_dir);
1183 EXPAND(_pw_passwd.pw_shell);
1184 SCALAR(_pw_passwd.pw_expire);
1185
1186 /* See if there's any data left. If so, read in flags. */
1187 if (data.size > (p - (char *)data.data)) {
1188 SCALAR(_pw_flags);
1189 } else
1190 _pw_flags = _PASSWORD_NOUID|_PASSWORD_NOGID; /* default */
1191
1192 return NS_SUCCESS;
1193 }
1194