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