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