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