/* $NetBSD: pwcache.c,v 1.31 2010/03/23 20:28:59 drochner Exp $ */12/*-3* Copyright (c) 1992 Keith Muller.4* Copyright (c) 1992, 19935* The Regents of the University of California. All rights reserved.6*7* This code is derived from software contributed to Berkeley by8* Keith Muller of the University of California, San Diego.9*10* Redistribution and use in source and binary forms, with or without11* modification, are permitted provided that the following conditions12* are met:13* 1. Redistributions of source code must retain the above copyright14* notice, this list of conditions and the following disclaimer.15* 2. Redistributions in binary form must reproduce the above copyright16* notice, this list of conditions and the following disclaimer in the17* documentation and/or other materials provided with the distribution.18* 3. Neither the name of the University nor the names of its contributors19* may be used to endorse or promote products derived from this software20* without specific prior written permission.21*22* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND23* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE24* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE25* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE26* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL27* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS28* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)29* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT30* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY31* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF32* SUCH DAMAGE.33*/3435/*-36* Copyright (c) 2002 The NetBSD Foundation, Inc.37* All rights reserved.38*39* Redistribution and use in source and binary forms, with or without40* modification, are permitted provided that the following conditions41* are met:42* 1. Redistributions of source code must retain the above copyright43* notice, this list of conditions and the following disclaimer.44* 2. Redistributions in binary form must reproduce the above copyright45* notice, this list of conditions and the following disclaimer in the46* documentation and/or other materials provided with the distribution.47*48* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS49* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED50* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR51* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS52* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR53* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF54* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS55* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN56* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)57* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE58* POSSIBILITY OF SUCH DAMAGE.59*/6061#if HAVE_NBTOOL_CONFIG_H62#include "nbtool_config.h"63/*64* XXX Undefine the renames of these functions so that we don't65* XXX rename the versions found in the host's <pwd.h> by mistake!66*/67#undef group_from_gid68#undef user_from_uid69#endif7071#include <sys/cdefs.h>72#if defined(LIBC_SCCS) && !defined(lint)73#if 074static char sccsid[] = "@(#)cache.c 8.1 (Berkeley) 5/31/93";75#else76__RCSID("$NetBSD: pwcache.c,v 1.31 2010/03/23 20:28:59 drochner Exp $");77#endif78#endif /* LIBC_SCCS and not lint */7980#include "namespace.h"8182#include <sys/types.h>83#include <sys/param.h>8485#include <assert.h>86#include <grp.h>87#include <pwd.h>88#include <stdio.h>89#include <stdlib.h>90#include <string.h>91#include <unistd.h>9293#define _DIAGASSERT(x) assert((x))9495#if HAVE_NBTOOL_CONFIG_H96/* XXX Now, re-apply the renaming that we undid above. */97#define group_from_gid __nbcompat_group_from_gid98#define user_from_uid __nbcompat_user_from_uid99#endif100101#ifdef __weak_alias102__weak_alias(user_from_uid,_user_from_uid)103__weak_alias(group_from_gid,_group_from_gid)104__weak_alias(pwcache_groupdb,_pwcache_groupdb)105#endif106107#if !HAVE_PWCACHE_USERDB || HAVE_NBTOOL_CONFIG_H108#include "pwcache.h"109110/*111* routines that control user, group, uid and gid caches (for the archive112* member print routine).113* IMPORTANT:114* these routines cache BOTH hits and misses, a major performance improvement115*/116117/*118* function pointers to various name lookup routines.119* these may be changed as necessary.120*/121static int (*_pwcache_setgroupent)(int) = setgroupent;122static void (*_pwcache_endgrent)(void) = endgrent;123static struct group * (*_pwcache_getgrnam)(const char *) = getgrnam;124static struct group * (*_pwcache_getgrgid)(gid_t) = getgrgid;125static int (*_pwcache_setpassent)(int) = setpassent;126static void (*_pwcache_endpwent)(void) = endpwent;127static struct passwd * (*_pwcache_getpwnam)(const char *) = getpwnam;128static struct passwd * (*_pwcache_getpwuid)(uid_t) = getpwuid;129130/*131* internal state132*/133static int pwopn; /* is password file open */134static int gropn; /* is group file open */135static UIDC **uidtb; /* uid to name cache */136static GIDC **gidtb; /* gid to name cache */137static UIDC **usrtb; /* user name to uid cache */138static GIDC **grptb; /* group name to gid cache */139140static int uidtb_fail; /* uidtb_start() failed ? */141static int gidtb_fail; /* gidtb_start() failed ? */142static int usrtb_fail; /* usrtb_start() failed ? */143static int grptb_fail; /* grptb_start() failed ? */144145146static u_int st_hash(const char *, size_t, int);147static int uidtb_start(void);148static int gidtb_start(void);149static int usrtb_start(void);150static int grptb_start(void);151152153static u_int154st_hash(const char *name, size_t len, int tabsz)155{156u_int key = 0;157158_DIAGASSERT(name != NULL);159160while (len--) {161key += *name++;162key = (key << 8) | (key >> 24);163}164165return (key % tabsz);166}167168/*169* uidtb_start170* creates an an empty uidtb171* Return:172* 0 if ok, -1 otherwise173*/174static int175uidtb_start(void)176{177178if (uidtb != NULL)179return (0);180if (uidtb_fail)181return (-1);182if ((uidtb = (UIDC **)calloc(UID_SZ, sizeof(UIDC *))) == NULL) {183++uidtb_fail;184return (-1);185}186return (0);187}188189/*190* gidtb_start191* creates an an empty gidtb192* Return:193* 0 if ok, -1 otherwise194*/195static int196gidtb_start(void)197{198199if (gidtb != NULL)200return (0);201if (gidtb_fail)202return (-1);203if ((gidtb = (GIDC **)calloc(GID_SZ, sizeof(GIDC *))) == NULL) {204++gidtb_fail;205return (-1);206}207return (0);208}209210/*211* usrtb_start212* creates an an empty usrtb213* Return:214* 0 if ok, -1 otherwise215*/216static int217usrtb_start(void)218{219220if (usrtb != NULL)221return (0);222if (usrtb_fail)223return (-1);224if ((usrtb = (UIDC **)calloc(UNM_SZ, sizeof(UIDC *))) == NULL) {225++usrtb_fail;226return (-1);227}228return (0);229}230231/*232* grptb_start233* creates an an empty grptb234* Return:235* 0 if ok, -1 otherwise236*/237static int238grptb_start(void)239{240241if (grptb != NULL)242return (0);243if (grptb_fail)244return (-1);245if ((grptb = (GIDC **)calloc(GNM_SZ, sizeof(GIDC *))) == NULL) {246++grptb_fail;247return (-1);248}249return (0);250}251252/*253* user_from_uid()254* caches the name (if any) for the uid. If noname clear, we always255* return the stored name (if valid or invalid match).256* We use a simple hash table.257* Return258* Pointer to stored name (or a empty string)259*/260const char *261user_from_uid(uid_t uid, int noname)262{263struct passwd *pw;264UIDC *ptr, **pptr;265266if ((uidtb == NULL) && (uidtb_start() < 0))267return (NULL);268269/*270* see if we have this uid cached271*/272pptr = uidtb + (uid % UID_SZ);273ptr = *pptr;274275if ((ptr != NULL) && (ptr->valid > 0) && (ptr->uid == uid)) {276/*277* have an entry for this uid278*/279if (!noname || (ptr->valid == VALID))280return (ptr->name);281return (NULL);282}283284/*285* No entry for this uid, we will add it286*/287if (!pwopn) {288if (_pwcache_setpassent != NULL)289(*_pwcache_setpassent)(1);290++pwopn;291}292293if (ptr == NULL)294*pptr = ptr = (UIDC *)malloc(sizeof(UIDC));295296if ((pw = (*_pwcache_getpwuid)(uid)) == NULL) {297/*298* no match for this uid in the local password file299* a string that is the uid in numeric format300*/301if (ptr == NULL)302return (NULL);303ptr->uid = uid;304(void)snprintf(ptr->name, UNMLEN, "%lu", (long) uid);305ptr->valid = INVALID;306if (noname)307return (NULL);308} else {309/*310* there is an entry for this uid in the password file311*/312if (ptr == NULL)313return (pw->pw_name);314ptr->uid = uid;315(void)strlcpy(ptr->name, pw->pw_name, UNMLEN);316ptr->valid = VALID;317}318return (ptr->name);319}320321/*322* group_from_gid()323* caches the name (if any) for the gid. If noname clear, we always324* return the stored name (if valid or invalid match).325* We use a simple hash table.326* Return327* Pointer to stored name (or a empty string)328*/329const char *330group_from_gid(gid_t gid, int noname)331{332struct group *gr;333GIDC *ptr, **pptr;334335if ((gidtb == NULL) && (gidtb_start() < 0))336return (NULL);337338/*339* see if we have this gid cached340*/341pptr = gidtb + (gid % GID_SZ);342ptr = *pptr;343344if ((ptr != NULL) && (ptr->valid > 0) && (ptr->gid == gid)) {345/*346* have an entry for this gid347*/348if (!noname || (ptr->valid == VALID))349return (ptr->name);350return (NULL);351}352353/*354* No entry for this gid, we will add it355*/356if (!gropn) {357if (_pwcache_setgroupent != NULL)358(*_pwcache_setgroupent)(1);359++gropn;360}361362if (ptr == NULL)363*pptr = ptr = (GIDC *)malloc(sizeof(GIDC));364365if ((gr = (*_pwcache_getgrgid)(gid)) == NULL) {366/*367* no match for this gid in the local group file, put in368* a string that is the gid in numberic format369*/370if (ptr == NULL)371return (NULL);372ptr->gid = gid;373(void)snprintf(ptr->name, GNMLEN, "%lu", (long) gid);374ptr->valid = INVALID;375if (noname)376return (NULL);377} else {378/*379* there is an entry for this group in the group file380*/381if (ptr == NULL)382return (gr->gr_name);383ptr->gid = gid;384(void)strlcpy(ptr->name, gr->gr_name, GNMLEN);385ptr->valid = VALID;386}387return (ptr->name);388}389390/*391* uid_from_user()392* caches the uid for a given user name. We use a simple hash table.393* Return394* the uid (if any) for a user name, or a -1 if no match can be found395*/396int397uid_from_user(const char *name, uid_t *uid)398{399struct passwd *pw;400UIDC *ptr, **pptr;401size_t namelen;402403/*404* return -1 for mangled names405*/406if (name == NULL || ((namelen = strlen(name)) == 0))407return (-1);408if ((usrtb == NULL) && (usrtb_start() < 0))409return (-1);410411/*412* look up in hash table, if found and valid return the uid,413* if found and invalid, return a -1414*/415pptr = usrtb + st_hash(name, namelen, UNM_SZ);416ptr = *pptr;417418if ((ptr != NULL) && (ptr->valid > 0) && !strcmp(name, ptr->name)) {419if (ptr->valid == INVALID)420return (-1);421*uid = ptr->uid;422return (0);423}424425if (!pwopn) {426if (_pwcache_setpassent != NULL)427(*_pwcache_setpassent)(1);428++pwopn;429}430431if (ptr == NULL)432*pptr = ptr = (UIDC *)malloc(sizeof(UIDC));433434/*435* no match, look it up, if no match store it as an invalid entry,436* or store the matching uid437*/438if (ptr == NULL) {439if ((pw = (*_pwcache_getpwnam)(name)) == NULL)440return (-1);441*uid = pw->pw_uid;442return (0);443}444(void)strlcpy(ptr->name, name, UNMLEN);445if ((pw = (*_pwcache_getpwnam)(name)) == NULL) {446ptr->valid = INVALID;447return (-1);448}449ptr->valid = VALID;450*uid = ptr->uid = pw->pw_uid;451return (0);452}453454/*455* gid_from_group()456* caches the gid for a given group name. We use a simple hash table.457* Return458* the gid (if any) for a group name, or a -1 if no match can be found459*/460int461gid_from_group(const char *name, gid_t *gid)462{463struct group *gr;464GIDC *ptr, **pptr;465size_t namelen;466467/*468* return -1 for mangled names469*/470if (name == NULL || ((namelen = strlen(name)) == 0))471return (-1);472if ((grptb == NULL) && (grptb_start() < 0))473return (-1);474475/*476* look up in hash table, if found and valid return the uid,477* if found and invalid, return a -1478*/479pptr = grptb + st_hash(name, namelen, GID_SZ);480ptr = *pptr;481482if ((ptr != NULL) && (ptr->valid > 0) && !strcmp(name, ptr->name)) {483if (ptr->valid == INVALID)484return (-1);485*gid = ptr->gid;486return (0);487}488489if (!gropn) {490if (_pwcache_setgroupent != NULL)491(*_pwcache_setgroupent)(1);492++gropn;493}494495if (ptr == NULL)496*pptr = ptr = (GIDC *)malloc(sizeof(GIDC));497498/*499* no match, look it up, if no match store it as an invalid entry,500* or store the matching gid501*/502if (ptr == NULL) {503if ((gr = (*_pwcache_getgrnam)(name)) == NULL)504return (-1);505*gid = gr->gr_gid;506return (0);507}508509(void)strlcpy(ptr->name, name, GNMLEN);510if ((gr = (*_pwcache_getgrnam)(name)) == NULL) {511ptr->valid = INVALID;512return (-1);513}514ptr->valid = VALID;515*gid = ptr->gid = gr->gr_gid;516return (0);517}518519#define FLUSHTB(arr, len, fail) \520do { \521if (arr != NULL) { \522for (i = 0; i < len; i++) \523if (arr[i] != NULL) \524free(arr[i]); \525arr = NULL; \526} \527fail = 0; \528} while (/* CONSTCOND */0);529530int531pwcache_userdb(532int (*a_setpassent)(int),533void (*a_endpwent)(void),534struct passwd * (*a_getpwnam)(const char *),535struct passwd * (*a_getpwuid)(uid_t))536{537int i;538539/* a_setpassent and a_endpwent may be NULL */540if (a_getpwnam == NULL || a_getpwuid == NULL)541return (-1);542543if (_pwcache_endpwent != NULL)544(*_pwcache_endpwent)();545FLUSHTB(uidtb, UID_SZ, uidtb_fail);546FLUSHTB(usrtb, UNM_SZ, usrtb_fail);547pwopn = 0;548_pwcache_setpassent = a_setpassent;549_pwcache_endpwent = a_endpwent;550_pwcache_getpwnam = a_getpwnam;551_pwcache_getpwuid = a_getpwuid;552553return (0);554}555556int557pwcache_groupdb(558int (*a_setgroupent)(int),559void (*a_endgrent)(void),560struct group * (*a_getgrnam)(const char *),561struct group * (*a_getgrgid)(gid_t))562{563int i;564565/* a_setgroupent and a_endgrent may be NULL */566if (a_getgrnam == NULL || a_getgrgid == NULL)567return (-1);568569if (_pwcache_endgrent != NULL)570(*_pwcache_endgrent)();571FLUSHTB(gidtb, GID_SZ, gidtb_fail);572FLUSHTB(grptb, GNM_SZ, grptb_fail);573gropn = 0;574_pwcache_setgroupent = a_setgroupent;575_pwcache_endgrent = a_endgrent;576_pwcache_getgrnam = a_getgrnam;577_pwcache_getgrgid = a_getgrgid;578579return (0);580}581582583#ifdef TEST_PWCACHE584585struct passwd *586test_getpwnam(const char *name)587{588static struct passwd foo;589590memset(&foo, 0, sizeof(foo));591if (strcmp(name, "toor") == 0) {592foo.pw_uid = 666;593return &foo;594}595return (getpwnam(name));596}597598int599main(int argc, char *argv[])600{601uid_t u;602int r, i;603604printf("pass 1 (default userdb)\n");605for (i = 1; i < argc; i++) {606printf("i: %d, pwopn %d usrtb_fail %d usrtb %p\n",607i, pwopn, usrtb_fail, usrtb);608r = uid_from_user(argv[i], &u);609if (r == -1)610printf(" uid_from_user %s: failed\n", argv[i]);611else612printf(" uid_from_user %s: %d\n", argv[i], u);613}614printf("pass 1 finish: pwopn %d usrtb_fail %d usrtb %p\n",615pwopn, usrtb_fail, usrtb);616617puts("");618printf("pass 2 (replacement userdb)\n");619printf("pwcache_userdb returned %d\n",620pwcache_userdb(setpassent, test_getpwnam, getpwuid));621printf("pwopn %d usrtb_fail %d usrtb %p\n", pwopn, usrtb_fail, usrtb);622623for (i = 1; i < argc; i++) {624printf("i: %d, pwopn %d usrtb_fail %d usrtb %p\n",625i, pwopn, usrtb_fail, usrtb);626u = -1;627r = uid_from_user(argv[i], &u);628if (r == -1)629printf(" uid_from_user %s: failed\n", argv[i]);630else631printf(" uid_from_user %s: %d\n", argv[i], u);632}633printf("pass 2 finish: pwopn %d usrtb_fail %d usrtb %p\n",634pwopn, usrtb_fail, usrtb);635636puts("");637printf("pass 3 (null pointers)\n");638printf("pwcache_userdb returned %d\n",639pwcache_userdb(NULL, NULL, NULL));640641return (0);642}643#endif /* TEST_PWCACHE */644#endif /* !HAVE_PWCACHE_USERDB */645646647