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