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