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