Home | History | Annotate | Line # | Download | only in gen
getpwent.c revision 1.12
      1  1.12      cgd /*	$NetBSD: getpwent.c,v 1.12 1995/02/27 04:13:05 cgd 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.1      cgd  *
      7   1.1      cgd  * Redistribution and use in source and binary forms, with or without
      8   1.1      cgd  * modification, are permitted provided that the following conditions
      9   1.1      cgd  * are met:
     10   1.1      cgd  * 1. Redistributions of source code must retain the above copyright
     11   1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     12   1.1      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1      cgd  *    notice, this list of conditions and the following disclaimer in the
     14   1.1      cgd  *    documentation and/or other materials provided with the distribution.
     15   1.1      cgd  * 3. All advertising materials mentioning features or use of this software
     16   1.1      cgd  *    must display the following acknowledgement:
     17   1.1      cgd  *	This product includes software developed by the University of
     18   1.1      cgd  *	California, Berkeley and its contributors.
     19   1.1      cgd  * 4. Neither the name of the University nor the names of its contributors
     20   1.1      cgd  *    may be used to endorse or promote products derived from this software
     21   1.1      cgd  *    without specific prior written permission.
     22   1.1      cgd  *
     23   1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24   1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25   1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26   1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27   1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28   1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29   1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30   1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31   1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32   1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33   1.1      cgd  * SUCH DAMAGE.
     34   1.1      cgd  */
     35   1.1      cgd 
     36   1.1      cgd #if defined(LIBC_SCCS) && !defined(lint)
     37  1.12      cgd #if 0
     38  1.12      cgd static char sccsid[] = "@(#)getpwent.c	8.1 (Berkeley) 6/4/93";
     39  1.12      cgd #else
     40  1.12      cgd static char rcsid[] = "$NetBSD: getpwent.c,v 1.12 1995/02/27 04:13:05 cgd Exp $";
     41  1.12      cgd #endif
     42   1.1      cgd #endif /* LIBC_SCCS and not lint */
     43   1.1      cgd 
     44   1.1      cgd #include <sys/param.h>
     45   1.1      cgd #include <fcntl.h>
     46   1.1      cgd #include <db.h>
     47   1.1      cgd #include <syslog.h>
     48   1.1      cgd #include <pwd.h>
     49   1.1      cgd #include <utmp.h>
     50   1.1      cgd #include <errno.h>
     51   1.1      cgd #include <unistd.h>
     52   1.1      cgd #include <stdlib.h>
     53   1.1      cgd #include <string.h>
     54   1.1      cgd #include <limits.h>
     55   1.4  deraadt #ifdef YP
     56   1.4  deraadt #include <stdio.h>
     57   1.4  deraadt #include <rpc/rpc.h>
     58   1.4  deraadt #include <rpcsvc/yp_prot.h>
     59   1.4  deraadt #include <rpcsvc/ypclnt.h>
     60   1.4  deraadt #endif
     61   1.1      cgd 
     62   1.1      cgd static struct passwd _pw_passwd;	/* password structure */
     63   1.1      cgd static DB *_pw_db;			/* password database */
     64   1.1      cgd static int _pw_keynum;			/* key counter */
     65   1.1      cgd static int _pw_stayopen;		/* keep fd's open */
     66   1.1      cgd static int __hashpw(), __initdb();
     67   1.1      cgd 
     68   1.4  deraadt #ifdef YP
     69   1.4  deraadt static char     *__ypcurrent, *__ypdomain;
     70   1.4  deraadt static int      __ypcurrentlen, __ypmode=0;
     71   1.4  deraadt static char	line[1024];
     72   1.4  deraadt 
     73   1.5  deraadt static int
     74   1.5  deraadt __ypparse(pw, s)
     75   1.4  deraadt struct passwd *pw;
     76   1.4  deraadt char *s;
     77   1.4  deraadt {
     78   1.4  deraadt 	char *bp, *cp;
     79   1.4  deraadt 
     80   1.4  deraadt 	bp = s;
     81   1.4  deraadt 	pw->pw_name = strsep(&bp, ":\n");
     82   1.4  deraadt 	pw->pw_passwd = strsep(&bp, ":\n");
     83   1.4  deraadt 	if (!(cp = strsep(&bp, ":\n")))
     84   1.4  deraadt 		return 1;
     85   1.4  deraadt 	pw->pw_uid = atoi(cp);
     86   1.4  deraadt 	if (!(cp = strsep(&bp, ":\n")))
     87   1.4  deraadt 		return 0;
     88   1.4  deraadt 	pw->pw_gid = atoi(cp);
     89   1.4  deraadt 	pw->pw_change = 0;
     90   1.4  deraadt 	pw->pw_class = "";
     91   1.4  deraadt 	pw->pw_gecos = strsep(&bp, ":\n");
     92   1.4  deraadt 	pw->pw_dir = strsep(&bp, ":\n");
     93   1.4  deraadt 	pw->pw_shell = strsep(&bp, ":\n");
     94   1.4  deraadt 	pw->pw_expire = 0;
     95   1.4  deraadt 	return 0;
     96   1.4  deraadt }
     97   1.4  deraadt #endif
     98   1.4  deraadt 
     99   1.1      cgd struct passwd *
    100   1.1      cgd getpwent()
    101   1.1      cgd {
    102   1.1      cgd 	DBT key;
    103   1.1      cgd 	char bf[sizeof(_pw_keynum) + 1];
    104   1.4  deraadt #ifdef YP
    105   1.4  deraadt 	char *bp, *cp;
    106   1.4  deraadt #endif
    107   1.1      cgd 
    108   1.1      cgd 	if (!_pw_db && !__initdb())
    109   1.1      cgd 		return((struct passwd *)NULL);
    110   1.1      cgd 
    111   1.4  deraadt #ifdef YP
    112   1.4  deraadt again:
    113   1.4  deraadt 	if(__ypmode) {
    114   1.4  deraadt 		char *key, *data;
    115   1.4  deraadt 		int keylen, datalen;
    116   1.4  deraadt 		int r;
    117   1.4  deraadt 
    118   1.4  deraadt 		if(!__ypdomain) {
    119   1.4  deraadt 			if( _yp_check(&__ypdomain) == 0) {
    120   1.4  deraadt 				__ypmode = 0;
    121   1.4  deraadt 				goto again;
    122   1.4  deraadt 			}
    123   1.4  deraadt 		}
    124   1.4  deraadt 		if(__ypcurrent) {
    125   1.4  deraadt 			r = yp_next(__ypdomain, "passwd.byname",
    126   1.4  deraadt 				__ypcurrent, __ypcurrentlen,
    127   1.4  deraadt 				&key, &keylen, &data, &datalen);
    128   1.4  deraadt 			free(__ypcurrent);
    129   1.4  deraadt 			__ypcurrent = NULL;
    130   1.4  deraadt 			/*printf("yp_next %d\n", r);*/
    131   1.4  deraadt 			switch(r) {
    132   1.4  deraadt 			case 0:
    133   1.4  deraadt 				break;
    134   1.4  deraadt 			default:
    135   1.4  deraadt 				__ypcurrent = NULL;
    136   1.4  deraadt 				__ypmode = 0;
    137   1.4  deraadt 				free(data);
    138   1.4  deraadt 				data = NULL;
    139   1.4  deraadt 				goto again;
    140   1.4  deraadt 			}
    141   1.4  deraadt 			__ypcurrent = key;
    142   1.4  deraadt 			__ypcurrentlen = keylen;
    143   1.4  deraadt 			bcopy(data, line, datalen);
    144   1.4  deraadt 			free(data);
    145   1.4  deraadt 			data = NULL;
    146   1.4  deraadt 		} else {
    147   1.4  deraadt 			r = yp_first(__ypdomain, "passwd.byname",
    148   1.4  deraadt 				&__ypcurrent, &__ypcurrentlen,
    149   1.4  deraadt 				&data, &datalen);
    150   1.4  deraadt 			/*printf("yp_first %d\n", r);*/
    151   1.4  deraadt 			switch(r) {
    152   1.4  deraadt 			case 0:
    153   1.4  deraadt 				break;
    154   1.4  deraadt 			default:
    155   1.4  deraadt 				__ypmode = 0;
    156   1.4  deraadt 				free(data);
    157   1.4  deraadt 				goto again;
    158   1.4  deraadt 			}
    159   1.4  deraadt 			bcopy(data, line, datalen);
    160   1.4  deraadt 			free(data);
    161   1.4  deraadt 			data = NULL;
    162   1.4  deraadt 		}
    163   1.4  deraadt 		line[datalen] = '\0';
    164   1.4  deraadt 		/*printf("line = %s\n", line);*/
    165   1.4  deraadt 		bp = line;
    166   1.4  deraadt 		goto parse;
    167   1.4  deraadt 	}
    168   1.4  deraadt #endif
    169   1.4  deraadt 
    170   1.1      cgd 	++_pw_keynum;
    171   1.1      cgd 	bf[0] = _PW_KEYBYNUM;
    172   1.1      cgd 	bcopy((char *)&_pw_keynum, bf + 1, sizeof(_pw_keynum));
    173   1.1      cgd 	key.data = (u_char *)bf;
    174   1.1      cgd 	key.size = sizeof(_pw_keynum) + 1;
    175   1.4  deraadt 	if(__hashpw(&key)) {
    176   1.4  deraadt #ifdef YP
    177   1.4  deraadt 		if(strcmp(_pw_passwd.pw_name, "+") == 0) {
    178   1.4  deraadt 			__ypmode = 1;
    179   1.4  deraadt 			goto again;
    180   1.4  deraadt 		}
    181   1.4  deraadt #endif
    182   1.4  deraadt 		return &_pw_passwd;
    183   1.4  deraadt 	}
    184   1.4  deraadt 	return (struct passwd *)NULL;
    185   1.4  deraadt 
    186   1.4  deraadt #ifdef YP
    187   1.4  deraadt parse:
    188   1.4  deraadt 	_pw_passwd.pw_name = strsep(&bp, ":\n");
    189   1.4  deraadt 	_pw_passwd.pw_passwd = strsep(&bp, ":\n");
    190   1.4  deraadt 	if (!(cp = strsep(&bp, ":\n")))
    191   1.4  deraadt 		goto again;
    192   1.4  deraadt 	_pw_passwd.pw_uid = atoi(cp);
    193   1.4  deraadt 	if (!(cp = strsep(&bp, ":\n")))
    194   1.4  deraadt 		goto again;
    195   1.4  deraadt 	_pw_passwd.pw_gid = atoi(cp);
    196   1.4  deraadt 	_pw_passwd.pw_change = 0;
    197   1.4  deraadt 	_pw_passwd.pw_class = "";
    198   1.4  deraadt 	_pw_passwd.pw_gecos = strsep(&bp, ":\n");
    199   1.4  deraadt 	_pw_passwd.pw_dir = strsep(&bp, ":\n");
    200   1.4  deraadt 	_pw_passwd.pw_shell = strsep(&bp, ":\n");
    201   1.4  deraadt 	_pw_passwd.pw_expire = 0;
    202   1.4  deraadt 	return &_pw_passwd;
    203   1.4  deraadt #endif
    204   1.1      cgd }
    205   1.1      cgd 
    206   1.1      cgd struct passwd *
    207   1.1      cgd getpwnam(name)
    208   1.1      cgd 	const char *name;
    209   1.1      cgd {
    210   1.1      cgd 	DBT key;
    211  1.10  deraadt 	int len, rval;
    212  1.10  deraadt 	char bf[UT_NAMESIZE + 1];
    213  1.10  deraadt 
    214  1.10  deraadt 	if (!_pw_db && !__initdb())
    215  1.10  deraadt 		return((struct passwd *)NULL);
    216   1.4  deraadt 
    217   1.4  deraadt #ifdef YP
    218  1.10  deraadt 	bf[0] = _PW_KEYBYNAME;
    219  1.10  deraadt 	len = strlen("+");
    220  1.10  deraadt 	bcopy("+", bf + 1, MIN(len, UT_NAMESIZE));
    221  1.10  deraadt 	key.data = (u_char *)bf;
    222  1.10  deraadt 	key.size = len + 1;
    223   1.4  deraadt 
    224  1.10  deraadt 	/*
    225  1.10  deraadt 	 * If there is a user called "+", then YP is active. In that
    226  1.10  deraadt 	 * case we must sequence through the passwd file in sequence.
    227  1.10  deraadt 	 */
    228  1.10  deraadt 	if ( __hashpw(&key)) {
    229  1.10  deraadt 		int r;
    230   1.4  deraadt 
    231  1.10  deraadt 		for(_pw_keynum=1; _pw_keynum; _pw_keynum++) {
    232  1.10  deraadt 			bf[0] = _PW_KEYBYNUM;
    233  1.10  deraadt 			bcopy((char *)&_pw_keynum, bf + 1, sizeof(_pw_keynum));
    234  1.10  deraadt 			key.data = (u_char *)bf;
    235  1.10  deraadt 			key.size = sizeof(_pw_keynum) + 1;
    236  1.10  deraadt 			if(__hashpw(&key) == 0)
    237  1.10  deraadt 				break;
    238  1.10  deraadt 			if(strcmp(_pw_passwd.pw_name, "+") == 0) {
    239  1.10  deraadt 				if(!__ypdomain) {
    240  1.10  deraadt 					if(_yp_check(&__ypdomain) == 0) {
    241  1.10  deraadt 						continue;
    242  1.10  deraadt 					}
    243  1.10  deraadt 				}
    244  1.10  deraadt 				if(__ypcurrent) {
    245  1.10  deraadt 					free(__ypcurrent);
    246  1.10  deraadt 					__ypcurrent = NULL;
    247  1.10  deraadt 				}
    248  1.10  deraadt 				r = yp_match(__ypdomain, "passwd.byname",
    249  1.10  deraadt 					name, strlen(name),
    250  1.10  deraadt 					&__ypcurrent, &__ypcurrentlen);
    251  1.10  deraadt 				switch(r) {
    252  1.10  deraadt 				case 0:
    253  1.10  deraadt 					break;
    254  1.10  deraadt 				default:
    255  1.10  deraadt 					free(__ypcurrent);
    256  1.10  deraadt 					__ypcurrent = NULL;
    257   1.4  deraadt 					continue;
    258   1.4  deraadt 				}
    259  1.10  deraadt 				bcopy(__ypcurrent, line, __ypcurrentlen);
    260  1.10  deraadt 				line[__ypcurrentlen] = '\0';
    261  1.10  deraadt 				if(__ypparse(&_pw_passwd, line))
    262  1.10  deraadt 					continue;
    263   1.4  deraadt 			}
    264  1.10  deraadt 			if( strcmp(_pw_passwd.pw_name, name) == 0) {
    265  1.10  deraadt 				if (!_pw_stayopen) {
    266  1.10  deraadt 					(void)(_pw_db->close)(_pw_db);
    267  1.10  deraadt 					_pw_db = (DB *)NULL;
    268  1.10  deraadt 				}
    269  1.10  deraadt 				return &_pw_passwd;
    270   1.4  deraadt 			}
    271  1.10  deraadt 			continue;
    272   1.4  deraadt 		}
    273  1.10  deraadt 		if (!_pw_stayopen) {
    274  1.10  deraadt 			(void)(_pw_db->close)(_pw_db);
    275  1.10  deraadt 			_pw_db = (DB *)NULL;
    276   1.4  deraadt 		}
    277  1.10  deraadt 		return (struct passwd *)NULL;
    278   1.4  deraadt 	}
    279  1.10  deraadt #endif /* YP */
    280   1.1      cgd 
    281   1.1      cgd 	bf[0] = _PW_KEYBYNAME;
    282   1.1      cgd 	len = strlen(name);
    283   1.1      cgd 	bcopy(name, bf + 1, MIN(len, UT_NAMESIZE));
    284   1.1      cgd 	key.data = (u_char *)bf;
    285   1.1      cgd 	key.size = len + 1;
    286   1.1      cgd 	rval = __hashpw(&key);
    287   1.1      cgd 
    288   1.1      cgd 	if (!_pw_stayopen) {
    289   1.1      cgd 		(void)(_pw_db->close)(_pw_db);
    290   1.1      cgd 		_pw_db = (DB *)NULL;
    291   1.1      cgd 	}
    292   1.1      cgd 	return(rval ? &_pw_passwd : (struct passwd *)NULL);
    293   1.1      cgd }
    294   1.1      cgd 
    295   1.1      cgd struct passwd *
    296   1.1      cgd #ifdef __STDC__
    297   1.1      cgd getpwuid(uid_t uid)
    298   1.1      cgd #else
    299   1.1      cgd getpwuid(uid)
    300   1.1      cgd 	int uid;
    301   1.1      cgd #endif
    302   1.1      cgd {
    303   1.1      cgd 	DBT key;
    304  1.10  deraadt 	char bf[sizeof(_pw_keynum) + 1];
    305  1.10  deraadt 	int keyuid, rval, len;
    306  1.10  deraadt 
    307  1.10  deraadt 	if (!_pw_db && !__initdb())
    308  1.10  deraadt 		return((struct passwd *)NULL);
    309   1.4  deraadt 
    310   1.4  deraadt #ifdef YP
    311  1.10  deraadt 	bf[0] = _PW_KEYBYNAME;
    312  1.10  deraadt 	len = strlen("+");
    313  1.10  deraadt 	bcopy("+", bf + 1, MIN(len, UT_NAMESIZE));
    314  1.10  deraadt 	key.data = (u_char *)bf;
    315  1.10  deraadt 	key.size = len + 1;
    316   1.4  deraadt 
    317  1.10  deraadt 	/*
    318  1.10  deraadt 	 * If there is a user called "+", then YP is active. In that
    319  1.10  deraadt 	 * case we must sequence through the passwd file in sequence.
    320  1.10  deraadt 	 */
    321  1.10  deraadt 	if ( __hashpw(&key)) {
    322  1.10  deraadt 		char uidbuf[20];
    323  1.10  deraadt 		int r;
    324   1.4  deraadt 
    325  1.10  deraadt 		for(_pw_keynum=1; _pw_keynum; _pw_keynum++) {
    326  1.10  deraadt 			bf[0] = _PW_KEYBYNUM;
    327  1.10  deraadt 			bcopy((char *)&_pw_keynum, bf + 1, sizeof(_pw_keynum));
    328  1.10  deraadt 			key.data = (u_char *)bf;
    329  1.10  deraadt 			key.size = sizeof(_pw_keynum) + 1;
    330  1.10  deraadt 			if(__hashpw(&key) == 0)
    331  1.10  deraadt 				break;
    332  1.10  deraadt 			if(strcmp(_pw_passwd.pw_name, "+") == 0) {
    333  1.10  deraadt 				if(!__ypdomain) {
    334  1.10  deraadt 					if(_yp_check(&__ypdomain) == 0) {
    335  1.10  deraadt 						continue;
    336  1.10  deraadt 					}
    337  1.10  deraadt 				}
    338  1.10  deraadt 				if(__ypcurrent) {
    339  1.10  deraadt 					free(__ypcurrent);
    340  1.10  deraadt 					__ypcurrent = NULL;
    341  1.10  deraadt 				}
    342  1.10  deraadt 				sprintf(uidbuf, "%d", uid);
    343  1.10  deraadt 				r = yp_match(__ypdomain, "passwd.byuid",
    344  1.10  deraadt 					uidbuf, strlen(uidbuf),
    345  1.10  deraadt 					&__ypcurrent, &__ypcurrentlen);
    346  1.10  deraadt 				switch(r) {
    347  1.10  deraadt 				case 0:
    348  1.10  deraadt 					break;
    349  1.10  deraadt 				default:
    350  1.10  deraadt 					free(__ypcurrent);
    351  1.10  deraadt 					__ypcurrent = NULL;
    352   1.4  deraadt 					continue;
    353   1.4  deraadt 				}
    354  1.10  deraadt 				bcopy(__ypcurrent, line, __ypcurrentlen);
    355  1.10  deraadt 				line[__ypcurrentlen] = '\0';
    356  1.10  deraadt 				if(__ypparse(&_pw_passwd, line))
    357  1.10  deraadt 					continue;
    358   1.4  deraadt 			}
    359  1.10  deraadt 			if( _pw_passwd.pw_uid == uid) {
    360  1.10  deraadt 				if (!_pw_stayopen) {
    361  1.10  deraadt 					(void)(_pw_db->close)(_pw_db);
    362  1.10  deraadt 					_pw_db = (DB *)NULL;
    363  1.10  deraadt 				}
    364  1.10  deraadt 				return &_pw_passwd;
    365   1.4  deraadt 			}
    366  1.10  deraadt 			continue;
    367   1.4  deraadt 		}
    368  1.10  deraadt 		if (!_pw_stayopen) {
    369  1.10  deraadt 			(void)(_pw_db->close)(_pw_db);
    370  1.10  deraadt 			_pw_db = (DB *)NULL;
    371   1.4  deraadt 		}
    372  1.10  deraadt 		return (struct passwd *)NULL;
    373   1.4  deraadt 	}
    374  1.10  deraadt #endif /* YP */
    375   1.1      cgd 
    376   1.1      cgd 	bf[0] = _PW_KEYBYUID;
    377   1.1      cgd 	keyuid = uid;
    378   1.1      cgd 	bcopy(&keyuid, bf + 1, sizeof(keyuid));
    379   1.1      cgd 	key.data = (u_char *)bf;
    380   1.1      cgd 	key.size = sizeof(keyuid) + 1;
    381   1.1      cgd 	rval = __hashpw(&key);
    382   1.1      cgd 
    383   1.1      cgd 	if (!_pw_stayopen) {
    384   1.1      cgd 		(void)(_pw_db->close)(_pw_db);
    385   1.1      cgd 		_pw_db = (DB *)NULL;
    386   1.1      cgd 	}
    387   1.1      cgd 	return(rval ? &_pw_passwd : (struct passwd *)NULL);
    388   1.1      cgd }
    389   1.1      cgd 
    390   1.1      cgd int
    391   1.1      cgd setpassent(stayopen)
    392   1.1      cgd 	int stayopen;
    393   1.1      cgd {
    394   1.1      cgd 	_pw_keynum = 0;
    395   1.1      cgd 	_pw_stayopen = stayopen;
    396   1.9      jtc #ifdef YP
    397   1.9      jtc 	__ypmode = 0;
    398   1.9      jtc 	if(__ypcurrent)
    399   1.9      jtc 		free(__ypcurrent);
    400   1.9      jtc 	__ypcurrent = NULL;
    401   1.9      jtc #endif
    402   1.1      cgd 	return(1);
    403   1.1      cgd }
    404   1.1      cgd 
    405   1.8      jtc void
    406   1.1      cgd setpwent()
    407   1.1      cgd {
    408   1.9      jtc 	(void) setpassent(0);
    409   1.1      cgd }
    410   1.1      cgd 
    411   1.1      cgd void
    412   1.1      cgd endpwent()
    413   1.1      cgd {
    414   1.1      cgd 	_pw_keynum = 0;
    415   1.1      cgd 	if (_pw_db) {
    416   1.1      cgd 		(void)(_pw_db->close)(_pw_db);
    417   1.1      cgd 		_pw_db = (DB *)NULL;
    418   1.1      cgd 	}
    419   1.4  deraadt #ifdef YP
    420   1.4  deraadt 	__ypmode = 0;
    421   1.4  deraadt 	if(__ypcurrent)
    422   1.4  deraadt 		free(__ypcurrent);
    423   1.4  deraadt 	__ypcurrent = NULL;
    424   1.4  deraadt #endif
    425   1.1      cgd }
    426   1.1      cgd 
    427   1.4  deraadt static int
    428   1.1      cgd __initdb()
    429   1.1      cgd {
    430   1.1      cgd 	static int warned;
    431   1.1      cgd 	char *p;
    432   1.1      cgd 
    433   1.1      cgd 	p = (geteuid()) ? _PATH_MP_DB : _PATH_SMP_DB;
    434   1.3   proven 	_pw_db = dbopen(p, O_RDONLY, 0, DB_HASH, NULL);
    435   1.1      cgd 	if (_pw_db)
    436   1.1      cgd 		return(1);
    437   1.1      cgd 	if (!warned)
    438   1.1      cgd 		syslog(LOG_ERR, "%s: %m", p);
    439  1.11  deraadt 	warned = 1;
    440   1.1      cgd 	return(0);
    441   1.1      cgd }
    442   1.1      cgd 
    443   1.4  deraadt static int
    444   1.1      cgd __hashpw(key)
    445   1.1      cgd 	DBT *key;
    446   1.1      cgd {
    447   1.1      cgd 	register char *p, *t;
    448   1.1      cgd 	static u_int max;
    449   1.1      cgd 	static char *line;
    450   1.1      cgd 	DBT data;
    451   1.1      cgd 
    452   1.1      cgd 	if ((_pw_db->get)(_pw_db, key, &data, 0))
    453   1.1      cgd 		return(0);
    454   1.1      cgd 	p = (char *)data.data;
    455   1.1      cgd 	if (data.size > max && !(line = realloc(line, max += 1024)))
    456   1.1      cgd 		return(0);
    457   1.1      cgd 
    458   1.1      cgd 	t = line;
    459   1.1      cgd #define	EXPAND(e)	e = t; while (*t++ = *p++);
    460   1.1      cgd 	EXPAND(_pw_passwd.pw_name);
    461   1.1      cgd 	EXPAND(_pw_passwd.pw_passwd);
    462   1.1      cgd 	bcopy(p, (char *)&_pw_passwd.pw_uid, sizeof(int));
    463   1.1      cgd 	p += sizeof(int);
    464   1.1      cgd 	bcopy(p, (char *)&_pw_passwd.pw_gid, sizeof(int));
    465   1.1      cgd 	p += sizeof(int);
    466   1.1      cgd 	bcopy(p, (char *)&_pw_passwd.pw_change, sizeof(time_t));
    467   1.1      cgd 	p += sizeof(time_t);
    468   1.1      cgd 	EXPAND(_pw_passwd.pw_class);
    469   1.1      cgd 	EXPAND(_pw_passwd.pw_gecos);
    470   1.1      cgd 	EXPAND(_pw_passwd.pw_dir);
    471   1.1      cgd 	EXPAND(_pw_passwd.pw_shell);
    472   1.1      cgd 	bcopy(p, (char *)&_pw_passwd.pw_expire, sizeof(time_t));
    473   1.1      cgd 	p += sizeof(time_t);
    474   1.1      cgd 	return(1);
    475   1.1      cgd }
    476