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