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