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