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