Home | History | Annotate | Line # | Download | only in gen
pwcache.c revision 1.25
      1  1.25       agc /*	$NetBSD: pwcache.c,v 1.25 2003/10/13 07:41:22 agc Exp $	*/
      2   1.4       cgd 
      3  1.10   mycroft /*-
      4  1.25       agc  * Copyright (c) 1992 Keith Muller.
      5  1.10   mycroft  * Copyright (c) 1992, 1993
      6   1.4       cgd  *	The Regents of the University of California.  All rights reserved.
      7   1.1       cgd  *
      8  1.10   mycroft  * This code is derived from software contributed to Berkeley by
      9  1.10   mycroft  * Keith Muller of the University of California, San Diego.
     10  1.10   mycroft  *
     11   1.1       cgd  * Redistribution and use in source and binary forms, with or without
     12   1.1       cgd  * modification, are permitted provided that the following conditions
     13   1.1       cgd  * are met:
     14   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     15   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     16   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     17   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     18   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     19  1.24       agc  * 3. Neither the name of the University nor the names of its contributors
     20  1.24       agc  *    may be used to endorse or promote products derived from this software
     21  1.24       agc  *    without specific prior written permission.
     22  1.24       agc  *
     23  1.24       agc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  1.24       agc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  1.24       agc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  1.24       agc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  1.24       agc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  1.24       agc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  1.24       agc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  1.24       agc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  1.24       agc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  1.24       agc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  1.24       agc  * SUCH DAMAGE.
     34  1.24       agc  */
     35  1.24       agc 
     36  1.24       agc /*-
     37  1.17     lukem  * Copyright (c) 2002 The NetBSD Foundation, Inc.
     38  1.17     lukem  * All rights reserved.
     39  1.17     lukem  *
     40  1.17     lukem  * Redistribution and use in source and binary forms, with or without
     41  1.17     lukem  * modification, are permitted provided that the following conditions
     42  1.17     lukem  * are met:
     43  1.17     lukem  * 1. Redistributions of source code must retain the above copyright
     44  1.17     lukem  *    notice, this list of conditions and the following disclaimer.
     45  1.17     lukem  * 2. Redistributions in binary form must reproduce the above copyright
     46  1.17     lukem  *    notice, this list of conditions and the following disclaimer in the
     47  1.17     lukem  *    documentation and/or other materials provided with the distribution.
     48  1.17     lukem  * 3. All advertising materials mentioning features or use of this software
     49  1.17     lukem  *    must display the following acknowledgement:
     50  1.17     lukem  *        This product includes software developed by the NetBSD
     51  1.17     lukem  *        Foundation, Inc. and its contributors.
     52  1.17     lukem  * 4. Neither the name of The NetBSD Foundation nor the names of its
     53  1.17     lukem  *    contributors may be used to endorse or promote products derived
     54  1.17     lukem  *    from this software without specific prior written permission.
     55  1.17     lukem  *
     56  1.17     lukem  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     57  1.17     lukem  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     58  1.17     lukem  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     59  1.17     lukem  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     60  1.17     lukem  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     61  1.17     lukem  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     62  1.17     lukem  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     63  1.17     lukem  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     64  1.17     lukem  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     65  1.17     lukem  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     66  1.17     lukem  * POSSIBILITY OF SUCH DAMAGE.
     67  1.17     lukem  */
     68  1.17     lukem 
     69   1.7  christos #include <sys/cdefs.h>
     70  1.15   msaitoh #if defined(LIBC_SCCS) && !defined(lint)
     71   1.4       cgd #if 0
     72  1.10   mycroft static char sccsid[] = "@(#)cache.c	8.1 (Berkeley) 5/31/93";
     73   1.4       cgd #else
     74  1.25       agc __RCSID("$NetBSD: pwcache.c,v 1.25 2003/10/13 07:41:22 agc Exp $");
     75   1.4       cgd #endif
     76  1.15   msaitoh #endif /* LIBC_SCCS and not lint */
     77   1.1       cgd 
     78   1.8       jtc #include "namespace.h"
     79  1.10   mycroft 
     80   1.1       cgd #include <sys/types.h>
     81   1.6  sommerfe #include <sys/param.h>
     82   1.4       cgd 
     83  1.13     lukem #include <assert.h>
     84   1.4       cgd #include <grp.h>
     85   1.1       cgd #include <pwd.h>
     86   1.1       cgd #include <stdio.h>
     87  1.10   mycroft #include <stdlib.h>
     88   1.5       jtc #include <string.h>
     89  1.10   mycroft #include <unistd.h>
     90  1.10   mycroft 
     91   1.8       jtc #ifdef __weak_alias
     92  1.14   mycroft __weak_alias(user_from_uid,_user_from_uid)
     93  1.14   mycroft __weak_alias(group_from_gid,_group_from_gid)
     94  1.17     lukem __weak_alias(pwcache_userdb,_pwcache_userdb)
     95  1.17     lukem __weak_alias(pwcache_groupdb,_pwcache_groupdb)
     96   1.8       jtc #endif
     97  1.19     lukem 
     98  1.19     lukem #if !HAVE_PWCACHE_USERDB
     99  1.19     lukem #include "pwcache.h"
    100   1.1       cgd 
    101  1.10   mycroft /*
    102  1.10   mycroft  * routines that control user, group, uid and gid caches (for the archive
    103  1.10   mycroft  * member print routine).
    104  1.10   mycroft  * IMPORTANT:
    105  1.10   mycroft  * these routines cache BOTH hits and misses, a major performance improvement
    106  1.10   mycroft  */
    107  1.10   mycroft 
    108  1.17     lukem /*
    109  1.17     lukem  * function pointers to various name lookup routines.
    110  1.17     lukem  * these may be changed as necessary.
    111  1.17     lukem  */
    112  1.17     lukem static	int		(*_pwcache_setgroupent)(int)		= setgroupent;
    113  1.17     lukem static	void		(*_pwcache_endgrent)(void)		= endgrent;
    114  1.17     lukem static	struct group *	(*_pwcache_getgrnam)(const char *)	= getgrnam;
    115  1.17     lukem static	struct group *	(*_pwcache_getgrgid)(gid_t)		= getgrgid;
    116  1.17     lukem static	int		(*_pwcache_setpassent)(int)		= setpassent;
    117  1.17     lukem static	void		(*_pwcache_endpwent)(void)		= endpwent;
    118  1.17     lukem static	struct passwd *	(*_pwcache_getpwnam)(const char *)	= getpwnam;
    119  1.17     lukem static	struct passwd *	(*_pwcache_getpwuid)(uid_t)		= getpwuid;
    120  1.17     lukem 
    121  1.17     lukem /*
    122  1.17     lukem  * internal state
    123  1.17     lukem  */
    124  1.17     lukem static	int	pwopn;		/* is password file open */
    125  1.17     lukem static	int	gropn;		/* is group file open */
    126  1.17     lukem static	UIDC	**uidtb;	/* uid to name cache */
    127  1.17     lukem static	GIDC	**gidtb;	/* gid to name cache */
    128  1.17     lukem static	UIDC	**usrtb;	/* user name to uid cache */
    129  1.17     lukem static	GIDC	**grptb;	/* group name to gid cache */
    130  1.17     lukem 
    131  1.17     lukem static	int	uidtb_fail;	/* uidtb_start() failed ? */
    132  1.17     lukem static	int	gidtb_fail;	/* gidtb_start() failed ? */
    133  1.17     lukem static	int	usrtb_fail;	/* usrtb_start() failed ? */
    134  1.17     lukem static	int	grptb_fail;	/* grptb_start() failed ? */
    135  1.17     lukem 
    136  1.17     lukem 
    137  1.17     lukem static	u_int	st_hash(const char *, size_t, int);
    138  1.17     lukem static	int	uidtb_start(void);
    139  1.17     lukem static	int	gidtb_start(void);
    140  1.17     lukem static	int	usrtb_start(void);
    141  1.17     lukem static	int	grptb_start(void);
    142  1.17     lukem 
    143  1.10   mycroft 
    144  1.10   mycroft static u_int
    145  1.16     lukem st_hash(const char *name, size_t len, int tabsz)
    146  1.10   mycroft {
    147  1.10   mycroft 	u_int key = 0;
    148  1.10   mycroft 
    149  1.13     lukem 	_DIAGASSERT(name != NULL);
    150  1.13     lukem 
    151  1.10   mycroft 	while (len--) {
    152  1.10   mycroft 		key += *name++;
    153  1.10   mycroft 		key = (key << 8) | (key >> 24);
    154  1.10   mycroft 	}
    155  1.10   mycroft 
    156  1.10   mycroft 	return (key % tabsz);
    157  1.10   mycroft }
    158  1.10   mycroft 
    159  1.10   mycroft /*
    160  1.10   mycroft  * uidtb_start
    161  1.10   mycroft  *	creates an an empty uidtb
    162  1.10   mycroft  * Return:
    163  1.10   mycroft  *	0 if ok, -1 otherwise
    164  1.10   mycroft  */
    165  1.10   mycroft static int
    166  1.10   mycroft uidtb_start(void)
    167  1.10   mycroft {
    168  1.10   mycroft 
    169  1.10   mycroft 	if (uidtb != NULL)
    170  1.10   mycroft 		return (0);
    171  1.17     lukem 	if (uidtb_fail)
    172  1.10   mycroft 		return (-1);
    173  1.10   mycroft 	if ((uidtb = (UIDC **)calloc(UID_SZ, sizeof(UIDC *))) == NULL) {
    174  1.17     lukem 		++uidtb_fail;
    175  1.10   mycroft 		return (-1);
    176  1.10   mycroft 	}
    177  1.10   mycroft 	return (0);
    178  1.10   mycroft }
    179  1.10   mycroft 
    180  1.10   mycroft /*
    181  1.10   mycroft  * gidtb_start
    182  1.10   mycroft  *	creates an an empty gidtb
    183  1.10   mycroft  * Return:
    184  1.10   mycroft  *	0 if ok, -1 otherwise
    185  1.10   mycroft  */
    186  1.17     lukem static int
    187  1.10   mycroft gidtb_start(void)
    188  1.10   mycroft {
    189  1.10   mycroft 
    190  1.10   mycroft 	if (gidtb != NULL)
    191  1.10   mycroft 		return (0);
    192  1.17     lukem 	if (gidtb_fail)
    193  1.10   mycroft 		return (-1);
    194  1.10   mycroft 	if ((gidtb = (GIDC **)calloc(GID_SZ, sizeof(GIDC *))) == NULL) {
    195  1.17     lukem 		++gidtb_fail;
    196  1.10   mycroft 		return (-1);
    197  1.10   mycroft 	}
    198  1.10   mycroft 	return (0);
    199  1.10   mycroft }
    200  1.10   mycroft 
    201  1.10   mycroft /*
    202  1.10   mycroft  * usrtb_start
    203  1.10   mycroft  *	creates an an empty usrtb
    204  1.10   mycroft  * Return:
    205  1.10   mycroft  *	0 if ok, -1 otherwise
    206  1.10   mycroft  */
    207  1.17     lukem static int
    208  1.10   mycroft usrtb_start(void)
    209  1.10   mycroft {
    210  1.10   mycroft 
    211  1.10   mycroft 	if (usrtb != NULL)
    212  1.10   mycroft 		return (0);
    213  1.17     lukem 	if (usrtb_fail)
    214  1.10   mycroft 		return (-1);
    215  1.10   mycroft 	if ((usrtb = (UIDC **)calloc(UNM_SZ, sizeof(UIDC *))) == NULL) {
    216  1.17     lukem 		++usrtb_fail;
    217  1.10   mycroft 		return (-1);
    218  1.10   mycroft 	}
    219  1.10   mycroft 	return (0);
    220  1.10   mycroft }
    221  1.10   mycroft 
    222  1.10   mycroft /*
    223  1.10   mycroft  * grptb_start
    224  1.10   mycroft  *	creates an an empty grptb
    225  1.10   mycroft  * Return:
    226  1.10   mycroft  *	0 if ok, -1 otherwise
    227  1.10   mycroft  */
    228  1.17     lukem static int
    229  1.10   mycroft grptb_start(void)
    230  1.10   mycroft {
    231  1.10   mycroft 
    232  1.10   mycroft 	if (grptb != NULL)
    233  1.10   mycroft 		return (0);
    234  1.17     lukem 	if (grptb_fail)
    235  1.10   mycroft 		return (-1);
    236  1.10   mycroft 	if ((grptb = (GIDC **)calloc(GNM_SZ, sizeof(GIDC *))) == NULL) {
    237  1.17     lukem 		++grptb_fail;
    238  1.10   mycroft 		return (-1);
    239  1.10   mycroft 	}
    240  1.10   mycroft 	return (0);
    241  1.10   mycroft }
    242  1.10   mycroft 
    243  1.22     pooka #if !HAVE_USER_FROM_UID
    244  1.10   mycroft /*
    245  1.11   mycroft  * user_from_uid()
    246  1.16     lukem  *	caches the name (if any) for the uid. If noname clear, we always
    247  1.16     lukem  *	return the the stored name (if valid or invalid match).
    248  1.16     lukem  *	We use a simple hash table.
    249  1.10   mycroft  * Return
    250  1.10   mycroft  *	Pointer to stored name (or a empty string)
    251  1.10   mycroft  */
    252  1.10   mycroft 
    253  1.10   mycroft const char *
    254  1.10   mycroft user_from_uid(uid_t uid, int noname)
    255   1.1       cgd {
    256   1.9     perry 	struct passwd *pw;
    257  1.10   mycroft 	UIDC *ptr, **pptr;
    258   1.1       cgd 
    259  1.10   mycroft 	if ((uidtb == NULL) && (uidtb_start() < 0))
    260  1.10   mycroft 		return (NULL);
    261  1.10   mycroft 
    262  1.10   mycroft 	/*
    263  1.10   mycroft 	 * see if we have this uid cached
    264  1.10   mycroft 	 */
    265  1.10   mycroft 	pptr = uidtb + (uid % UID_SZ);
    266  1.10   mycroft 	ptr = *pptr;
    267  1.10   mycroft 
    268  1.10   mycroft 	if ((ptr != NULL) && (ptr->valid > 0) && (ptr->uid == uid)) {
    269  1.10   mycroft 		/*
    270  1.10   mycroft 		 * have an entry for this uid
    271  1.10   mycroft 		 */
    272  1.10   mycroft 		if (!noname || (ptr->valid == VALID))
    273  1.10   mycroft 			return (ptr->name);
    274  1.10   mycroft 		return (NULL);
    275  1.10   mycroft 	}
    276  1.10   mycroft 
    277  1.10   mycroft 	/*
    278  1.10   mycroft 	 * No entry for this uid, we will add it
    279  1.10   mycroft 	 */
    280  1.10   mycroft 	if (!pwopn) {
    281  1.17     lukem 		if (_pwcache_setpassent != NULL)
    282  1.17     lukem 			(*_pwcache_setpassent)(1);
    283  1.10   mycroft 		++pwopn;
    284   1.1       cgd 	}
    285  1.10   mycroft 
    286  1.10   mycroft 	if (ptr == NULL)
    287  1.12   mycroft 		*pptr = ptr = (UIDC *)malloc(sizeof(UIDC));
    288  1.10   mycroft 
    289  1.17     lukem 	if ((pw = (*_pwcache_getpwuid)(uid)) == NULL) {
    290  1.10   mycroft 		/*
    291  1.10   mycroft 		 * no match for this uid in the local password file
    292  1.23     grant 		 * a string that is the uid in numeric format
    293  1.10   mycroft 		 */
    294  1.10   mycroft 		if (ptr == NULL)
    295  1.10   mycroft 			return (NULL);
    296  1.10   mycroft 		ptr->uid = uid;
    297  1.10   mycroft 		(void)snprintf(ptr->name, UNMLEN, "%lu", (long) uid);
    298  1.10   mycroft 		ptr->valid = INVALID;
    299  1.10   mycroft 		if (noname)
    300  1.10   mycroft 			return (NULL);
    301  1.10   mycroft 	} else {
    302  1.10   mycroft 		/*
    303  1.10   mycroft 		 * there is an entry for this uid in the password file
    304  1.10   mycroft 		 */
    305  1.10   mycroft 		if (ptr == NULL)
    306  1.10   mycroft 			return (pw->pw_name);
    307  1.10   mycroft 		ptr->uid = uid;
    308  1.17     lukem 		(void)strlcpy(ptr->name, pw->pw_name, UNMLEN);
    309  1.10   mycroft 		ptr->valid = VALID;
    310  1.10   mycroft 	}
    311  1.10   mycroft 	return (ptr->name);
    312   1.1       cgd }
    313   1.1       cgd 
    314  1.10   mycroft /*
    315  1.10   mycroft  * group_from_gid()
    316  1.16     lukem  *	caches the name (if any) for the gid. If noname clear, we always
    317  1.16     lukem  *	return the the stored name (if valid or invalid match).
    318  1.16     lukem  *	We use a simple hash table.
    319  1.10   mycroft  * Return
    320  1.10   mycroft  *	Pointer to stored name (or a empty string)
    321  1.10   mycroft  */
    322  1.10   mycroft 
    323  1.10   mycroft const char *
    324  1.10   mycroft group_from_gid(gid_t gid, int noname)
    325  1.10   mycroft {
    326  1.10   mycroft 	struct group *gr;
    327  1.10   mycroft 	GIDC *ptr, **pptr;
    328  1.10   mycroft 
    329  1.10   mycroft 	if ((gidtb == NULL) && (gidtb_start() < 0))
    330  1.10   mycroft 		return (NULL);
    331  1.10   mycroft 
    332  1.10   mycroft 	/*
    333  1.10   mycroft 	 * see if we have this gid cached
    334  1.10   mycroft 	 */
    335  1.10   mycroft 	pptr = gidtb + (gid % GID_SZ);
    336  1.10   mycroft 	ptr = *pptr;
    337  1.10   mycroft 
    338  1.10   mycroft 	if ((ptr != NULL) && (ptr->valid > 0) && (ptr->gid == gid)) {
    339  1.10   mycroft 		/*
    340  1.10   mycroft 		 * have an entry for this gid
    341  1.10   mycroft 		 */
    342  1.10   mycroft 		if (!noname || (ptr->valid == VALID))
    343  1.10   mycroft 			return (ptr->name);
    344  1.10   mycroft 		return (NULL);
    345  1.10   mycroft 	}
    346  1.10   mycroft 
    347  1.10   mycroft 	/*
    348  1.10   mycroft 	 * No entry for this gid, we will add it
    349  1.10   mycroft 	 */
    350  1.10   mycroft 	if (!gropn) {
    351  1.17     lukem 		if (_pwcache_setgroupent != NULL)
    352  1.17     lukem 			(*_pwcache_setgroupent)(1);
    353  1.10   mycroft 		++gropn;
    354  1.10   mycroft 	}
    355  1.10   mycroft 
    356  1.10   mycroft 	if (ptr == NULL)
    357  1.12   mycroft 		*pptr = ptr = (GIDC *)malloc(sizeof(GIDC));
    358  1.10   mycroft 
    359  1.17     lukem 	if ((gr = (*_pwcache_getgrgid)(gid)) == NULL) {
    360  1.10   mycroft 		/*
    361  1.10   mycroft 		 * no match for this gid in the local group file, put in
    362  1.10   mycroft 		 * a string that is the gid in numberic format
    363  1.10   mycroft 		 */
    364  1.10   mycroft 		if (ptr == NULL)
    365  1.10   mycroft 			return (NULL);
    366  1.10   mycroft 		ptr->gid = gid;
    367  1.10   mycroft 		(void)snprintf(ptr->name, GNMLEN, "%lu", (long) gid);
    368  1.10   mycroft 		ptr->valid = INVALID;
    369  1.10   mycroft 		if (noname)
    370  1.10   mycroft 			return (NULL);
    371  1.10   mycroft 	} else {
    372  1.10   mycroft 		/*
    373  1.10   mycroft 		 * there is an entry for this group in the group file
    374  1.10   mycroft 		 */
    375  1.10   mycroft 		if (ptr == NULL)
    376  1.10   mycroft 			return (gr->gr_name);
    377  1.10   mycroft 		ptr->gid = gid;
    378  1.17     lukem 		(void)strlcpy(ptr->name, gr->gr_name, GNMLEN);
    379  1.10   mycroft 		ptr->valid = VALID;
    380  1.10   mycroft 	}
    381  1.10   mycroft 	return (ptr->name);
    382  1.10   mycroft }
    383  1.22     pooka #endif /* HAVE_USER_FROM_UID */
    384  1.10   mycroft 
    385  1.10   mycroft /*
    386  1.10   mycroft  * uid_from_user()
    387  1.10   mycroft  *	caches the uid for a given user name. We use a simple hash table.
    388  1.10   mycroft  * Return
    389  1.10   mycroft  *	the uid (if any) for a user name, or a -1 if no match can be found
    390  1.10   mycroft  */
    391  1.10   mycroft 
    392  1.10   mycroft int
    393  1.10   mycroft uid_from_user(const char *name, uid_t *uid)
    394  1.10   mycroft {
    395  1.10   mycroft 	struct passwd *pw;
    396  1.10   mycroft 	UIDC *ptr, **pptr;
    397  1.10   mycroft 	size_t namelen;
    398  1.10   mycroft 
    399  1.10   mycroft 	/*
    400  1.10   mycroft 	 * return -1 for mangled names
    401  1.10   mycroft 	 */
    402  1.13     lukem 	if (name == NULL || ((namelen = strlen(name)) == 0))
    403  1.10   mycroft 		return (-1);
    404  1.10   mycroft 	if ((usrtb == NULL) && (usrtb_start() < 0))
    405  1.10   mycroft 		return (-1);
    406  1.10   mycroft 
    407  1.10   mycroft 	/*
    408  1.10   mycroft 	 * look up in hash table, if found and valid return the uid,
    409  1.10   mycroft 	 * if found and invalid, return a -1
    410  1.10   mycroft 	 */
    411  1.10   mycroft 	pptr = usrtb + st_hash(name, namelen, UNM_SZ);
    412  1.10   mycroft 	ptr = *pptr;
    413  1.10   mycroft 
    414  1.10   mycroft 	if ((ptr != NULL) && (ptr->valid > 0) && !strcmp(name, ptr->name)) {
    415  1.10   mycroft 		if (ptr->valid == INVALID)
    416  1.10   mycroft 			return (-1);
    417  1.10   mycroft 		*uid = ptr->uid;
    418  1.10   mycroft 		return (0);
    419  1.10   mycroft 	}
    420  1.10   mycroft 
    421  1.10   mycroft 	if (!pwopn) {
    422  1.17     lukem 		if (_pwcache_setpassent != NULL)
    423  1.17     lukem 			(*_pwcache_setpassent)(1);
    424  1.10   mycroft 		++pwopn;
    425  1.10   mycroft 	}
    426  1.10   mycroft 
    427  1.10   mycroft 	if (ptr == NULL)
    428  1.12   mycroft 		*pptr = ptr = (UIDC *)malloc(sizeof(UIDC));
    429  1.10   mycroft 
    430  1.10   mycroft 	/*
    431  1.10   mycroft 	 * no match, look it up, if no match store it as an invalid entry,
    432  1.10   mycroft 	 * or store the matching uid
    433  1.10   mycroft 	 */
    434  1.10   mycroft 	if (ptr == NULL) {
    435  1.17     lukem 		if ((pw = (*_pwcache_getpwnam)(name)) == NULL)
    436  1.10   mycroft 			return (-1);
    437  1.10   mycroft 		*uid = pw->pw_uid;
    438  1.10   mycroft 		return (0);
    439  1.10   mycroft 	}
    440  1.17     lukem 	(void)strlcpy(ptr->name, name, UNMLEN);
    441  1.17     lukem 	if ((pw = (*_pwcache_getpwnam)(name)) == NULL) {
    442  1.10   mycroft 		ptr->valid = INVALID;
    443  1.10   mycroft 		return (-1);
    444  1.10   mycroft 	}
    445  1.10   mycroft 	ptr->valid = VALID;
    446  1.10   mycroft 	*uid = ptr->uid = pw->pw_uid;
    447  1.10   mycroft 	return (0);
    448  1.10   mycroft }
    449  1.10   mycroft 
    450  1.10   mycroft /*
    451  1.10   mycroft  * gid_from_group()
    452  1.10   mycroft  *	caches the gid for a given group name. We use a simple hash table.
    453  1.10   mycroft  * Return
    454  1.10   mycroft  *	the gid (if any) for a group name, or a -1 if no match can be found
    455  1.10   mycroft  */
    456  1.10   mycroft 
    457  1.10   mycroft int
    458  1.10   mycroft gid_from_group(const char *name, gid_t *gid)
    459   1.1       cgd {
    460   1.4       cgd 	struct group *gr;
    461  1.10   mycroft 	GIDC *ptr, **pptr;
    462  1.10   mycroft 	size_t namelen;
    463  1.10   mycroft 
    464  1.10   mycroft 	/*
    465  1.10   mycroft 	 * return -1 for mangled names
    466  1.10   mycroft 	 */
    467  1.13     lukem 	if (name == NULL || ((namelen = strlen(name)) == 0))
    468  1.10   mycroft 		return (-1);
    469  1.10   mycroft 	if ((grptb == NULL) && (grptb_start() < 0))
    470  1.10   mycroft 		return (-1);
    471  1.10   mycroft 
    472  1.10   mycroft 	/*
    473  1.10   mycroft 	 * look up in hash table, if found and valid return the uid,
    474  1.10   mycroft 	 * if found and invalid, return a -1
    475  1.10   mycroft 	 */
    476  1.10   mycroft 	pptr = grptb + st_hash(name, namelen, GID_SZ);
    477  1.10   mycroft 	ptr = *pptr;
    478  1.10   mycroft 
    479  1.10   mycroft 	if ((ptr != NULL) && (ptr->valid > 0) && !strcmp(name, ptr->name)) {
    480  1.10   mycroft 		if (ptr->valid == INVALID)
    481  1.10   mycroft 			return (-1);
    482  1.10   mycroft 		*gid = ptr->gid;
    483  1.10   mycroft 		return (0);
    484  1.10   mycroft 	}
    485  1.10   mycroft 
    486  1.10   mycroft 	if (!gropn) {
    487  1.17     lukem 		if (_pwcache_setgroupent != NULL)
    488  1.17     lukem 			(*_pwcache_setgroupent)(1);
    489  1.10   mycroft 		++gropn;
    490  1.10   mycroft 	}
    491  1.10   mycroft 
    492  1.10   mycroft 	if (ptr == NULL)
    493  1.12   mycroft 		*pptr = ptr = (GIDC *)malloc(sizeof(GIDC));
    494  1.10   mycroft 
    495  1.10   mycroft 	/*
    496  1.10   mycroft 	 * no match, look it up, if no match store it as an invalid entry,
    497  1.10   mycroft 	 * or store the matching gid
    498  1.10   mycroft 	 */
    499  1.10   mycroft 	if (ptr == NULL) {
    500  1.17     lukem 		if ((gr = (*_pwcache_getgrnam)(name)) == NULL)
    501  1.10   mycroft 			return (-1);
    502  1.10   mycroft 		*gid = gr->gr_gid;
    503  1.10   mycroft 		return (0);
    504  1.10   mycroft 	}
    505   1.1       cgd 
    506  1.17     lukem 	(void)strlcpy(ptr->name, name, GNMLEN);
    507  1.17     lukem 	if ((gr = (*_pwcache_getgrnam)(name)) == NULL) {
    508  1.10   mycroft 		ptr->valid = INVALID;
    509  1.10   mycroft 		return (-1);
    510   1.1       cgd 	}
    511  1.10   mycroft 	ptr->valid = VALID;
    512  1.10   mycroft 	*gid = ptr->gid = gr->gr_gid;
    513  1.10   mycroft 	return (0);
    514   1.1       cgd }
    515  1.17     lukem 
    516  1.17     lukem #define FLUSHTB(arr, len, fail)				\
    517  1.17     lukem 	do {						\
    518  1.17     lukem 		if (arr != NULL) {			\
    519  1.17     lukem 			for (i = 0; i < len; i++)	\
    520  1.17     lukem 				if (arr[i] != NULL)	\
    521  1.17     lukem 					free(arr[i]);	\
    522  1.17     lukem 			arr = NULL;			\
    523  1.17     lukem 		}					\
    524  1.17     lukem 		fail = 0;				\
    525  1.17     lukem 	} while (/* CONSTCOND */0);
    526  1.17     lukem 
    527  1.17     lukem int
    528  1.17     lukem pwcache_userdb(
    529  1.17     lukem 	int		(*a_setpassent)(int),
    530  1.17     lukem 	void		(*a_endpwent)(void),
    531  1.17     lukem 	struct passwd *	(*a_getpwnam)(const char *),
    532  1.17     lukem 	struct passwd *	(*a_getpwuid)(uid_t))
    533  1.17     lukem {
    534  1.17     lukem 	int i;
    535  1.17     lukem 
    536  1.17     lukem 		/* a_setpassent and a_endpwent may be NULL */
    537  1.17     lukem 	if (a_getpwnam == NULL || a_getpwuid == NULL)
    538  1.17     lukem 		return (-1);
    539  1.17     lukem 
    540  1.17     lukem 	if (_pwcache_endpwent != NULL)
    541  1.17     lukem 		(*_pwcache_endpwent)();
    542  1.17     lukem 	FLUSHTB(uidtb, UID_SZ, uidtb_fail);
    543  1.17     lukem 	FLUSHTB(usrtb, UNM_SZ, usrtb_fail);
    544  1.17     lukem 	pwopn = 0;
    545  1.17     lukem 	_pwcache_setpassent = a_setpassent;
    546  1.17     lukem 	_pwcache_endpwent = a_endpwent;
    547  1.17     lukem 	_pwcache_getpwnam = a_getpwnam;
    548  1.17     lukem 	_pwcache_getpwuid = a_getpwuid;
    549  1.17     lukem 
    550  1.17     lukem 	return (0);
    551  1.17     lukem }
    552  1.17     lukem 
    553  1.17     lukem int
    554  1.17     lukem pwcache_groupdb(
    555  1.17     lukem 	int		(*a_setgroupent)(int),
    556  1.17     lukem 	void		(*a_endgrent)(void),
    557  1.17     lukem 	struct group *	(*a_getgrnam)(const char *),
    558  1.17     lukem 	struct group *	(*a_getgrgid)(gid_t))
    559  1.17     lukem {
    560  1.17     lukem 	int i;
    561  1.17     lukem 
    562  1.17     lukem 		/* a_setgroupent and a_endgrent may be NULL */
    563  1.17     lukem 	if (a_getgrnam == NULL || a_getgrgid == NULL)
    564  1.17     lukem 		return (-1);
    565  1.17     lukem 
    566  1.17     lukem 	if (_pwcache_endgrent != NULL)
    567  1.17     lukem 		(*_pwcache_endgrent)();
    568  1.17     lukem 	FLUSHTB(gidtb, GID_SZ, gidtb_fail);
    569  1.17     lukem 	FLUSHTB(grptb, GNM_SZ, grptb_fail);
    570  1.17     lukem 	gropn = 0;
    571  1.17     lukem 	_pwcache_setgroupent = a_setgroupent;
    572  1.17     lukem 	_pwcache_endgrent = a_endgrent;
    573  1.17     lukem 	_pwcache_getgrnam = a_getgrnam;
    574  1.17     lukem 	_pwcache_getgrgid = a_getgrgid;
    575  1.17     lukem 
    576  1.17     lukem 	return (0);
    577  1.17     lukem }
    578  1.17     lukem 
    579  1.17     lukem 
    580  1.17     lukem #ifdef TEST_PWCACHE
    581  1.17     lukem 
    582  1.17     lukem struct passwd *
    583  1.17     lukem test_getpwnam(const char *name)
    584  1.17     lukem {
    585  1.17     lukem 	static struct passwd foo;
    586  1.17     lukem 
    587  1.17     lukem 	memset(&foo, 0, sizeof(foo));
    588  1.17     lukem 	if (strcmp(name, "toor") == 0) {
    589  1.17     lukem 		foo.pw_uid = 666;
    590  1.17     lukem 		return &foo;
    591  1.17     lukem 	}
    592  1.17     lukem 	return (getpwnam(name));
    593  1.17     lukem }
    594  1.17     lukem 
    595  1.17     lukem int
    596  1.17     lukem main(int argc, char *argv[])
    597  1.17     lukem {
    598  1.17     lukem 	uid_t	u;
    599  1.17     lukem 	int	r, i;
    600  1.17     lukem 
    601  1.17     lukem 	printf("pass 1 (default userdb)\n");
    602  1.17     lukem 	for (i = 1; i < argc; i++) {
    603  1.17     lukem 		printf("i: %d, pwopn %d usrtb_fail %d usrtb %p\n",
    604  1.17     lukem 		    i, pwopn, usrtb_fail, usrtb);
    605  1.17     lukem 		r = uid_from_user(argv[i], &u);
    606  1.17     lukem 		if (r == -1)
    607  1.17     lukem 			printf("  uid_from_user %s: failed\n", argv[i]);
    608  1.17     lukem 		else
    609  1.17     lukem 			printf("  uid_from_user %s: %d\n", argv[i], u);
    610  1.17     lukem 	}
    611  1.17     lukem 	printf("pass 1 finish: pwopn %d usrtb_fail %d usrtb %p\n",
    612  1.17     lukem 		    pwopn, usrtb_fail, usrtb);
    613  1.17     lukem 
    614  1.17     lukem 	puts("");
    615  1.17     lukem 	printf("pass 2 (replacement userdb)\n");
    616  1.17     lukem 	printf("pwcache_userdb returned %d\n",
    617  1.17     lukem 	    pwcache_userdb(setpassent, test_getpwnam, getpwuid));
    618  1.17     lukem 	printf("pwopn %d usrtb_fail %d usrtb %p\n", pwopn, usrtb_fail, usrtb);
    619  1.17     lukem 
    620  1.17     lukem 	for (i = 1; i < argc; i++) {
    621  1.17     lukem 		printf("i: %d, pwopn %d usrtb_fail %d usrtb %p\n",
    622  1.17     lukem 		    i, pwopn, usrtb_fail, usrtb);
    623  1.17     lukem 		u = -1;
    624  1.17     lukem 		r = uid_from_user(argv[i], &u);
    625  1.17     lukem 		if (r == -1)
    626  1.17     lukem 			printf("  uid_from_user %s: failed\n", argv[i]);
    627  1.17     lukem 		else
    628  1.17     lukem 			printf("  uid_from_user %s: %d\n", argv[i], u);
    629  1.17     lukem 	}
    630  1.17     lukem 	printf("pass 2 finish: pwopn %d usrtb_fail %d usrtb %p\n",
    631  1.17     lukem 		    pwopn, usrtb_fail, usrtb);
    632  1.17     lukem 
    633  1.17     lukem 	puts("");
    634  1.17     lukem 	printf("pass 3 (null pointers)\n");
    635  1.17     lukem 	printf("pwcache_userdb returned %d\n",
    636  1.17     lukem 	    pwcache_userdb(NULL, NULL, NULL));
    637  1.17     lukem 
    638  1.17     lukem 	return (0);
    639  1.17     lukem }
    640  1.17     lukem #endif	/* TEST_PWCACHE */
    641  1.18     lukem #endif	/* !HAVE_PWCACHE_USERDB */
    642