/*- * Copyright (c) 2005-2006 Ulf Lilleengen. * Copyright (c) 1992 Keith Muller. * Copyright (c) 1992, 1993 * The Regents of the University of California. All rights reserved. * * This code is derived from software contributed to Berkeley by * Keith Muller of the University of California, San Diego. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by the University of * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include "cache.h" #include "misc.h" /* * Constants and data structures used to implement group and password file * caches. Traditional passwd/group cache routines perform quite poorly with * archives. The chances of hitting a valid lookup with an archive is quite a * bit worse than with files already resident on the file system. These misses * create a MAJOR performance cost. To address this problem, these routines * cache both hits and misses. * * NOTE: name lengths must be as large as those stored in ANY PROTOCOL and * as stored in the passwd and group files. CACHE SIZES MUST BE PRIME */ #define UNMLEN 32 /* >= user name found in any protocol */ #define GNMLEN 32 /* >= group name found in any protocol */ #define UID_SZ 317 /* size of user_name/uid cache */ #define UNM_SZ 317 /* size of user_name/uid cache */ #define GID_SZ 251 /* size of gid cache */ #define GNM_SZ 317 /* size of group name cache */ #define VALID 1 /* entry and name are valid */ #define INVALID 2 /* entry valid, name NOT valid */ #define BLEN 64 /* buffer length */ /* * Node structures used in the user, group, uid, and gid caches. */ typedef struct uidc { char name[UNMLEN]; /* uid name */ uid_t uid; /* cached uid */ int valid; /* is this a valid or a miss entry */ } UIDC; typedef struct gidc { char name[GNMLEN]; /* gid name */ gid_t gid; /* cached gid */ int valid; /* is this a valid or a miss entry */ } GIDC; /* * routines that control user, group, uid and gid caches (for the archive * member print routine). * IMPORTANT: * these routines cache BOTH hits and misses, a major performance improvement */ static UIDC **uidtb = NULL; /* uid to name cache */ static GIDC **gidtb = NULL; /* gid to name cache */ static UIDC **usrtb = NULL; /* user name to uid cache */ static GIDC **grptb = NULL; /* group name to gid cache */ static pthread_mutex_t uidtb_lock; static pthread_mutex_t gidtb_lock; static pthread_mutex_t usrtb_lock; static pthread_mutex_t grptb_lock; static unsigned int st_hash(const char *, size_t, int); static int uidtb_start(void); static int gidtb_start(void); static int usrtb_start(void); static int grptb_start(void); static void cache_lock(pthread_mutex_t*); static void cache_unlock(pthread_mutex_t*); /* Hashes a string */ static unsigned int st_hash(const char *name, size_t len, int tabsz) { unsigned int key = 0; while (len--) { key += *name++; key = (key << 8) | (key >> 24); } return (key % tabsz); } /* * uidtb_start * creates an an empty uidtb * Return: * 0 if ok, -1 otherwise */ static int uidtb_start(void) { if (uidtb != NULL) return (0); pthread_mutex_init(&uidtb_lock, NULL); uidtb = xmalloc(UID_SZ*sizeof(UIDC *)); return (0); } /* * gidtb_start * creates an an empty gidtb * Return: * 0 if ok, -1 otherwise */ static int gidtb_start(void) { if (gidtb != NULL) return (0); pthread_mutex_init(&gidtb_lock, NULL); gidtb = xmalloc(GID_SZ*sizeof(GIDC *)); return (0); } /* * usrtb_start * creates an an empty usrtb * Return: * 0 if ok, -1 otherwise */ static int usrtb_start(void) { if (usrtb != NULL) return (0); pthread_mutex_init(&usrtb_lock, NULL); usrtb = xmalloc(UNM_SZ*sizeof(UIDC *)); return (0); } /* * grptb_start * creates an an empty grptb * Return: * 0 if ok, -1 otherwise */ static int grptb_start(void) { if (grptb != NULL) return (0); pthread_mutex_init(&grptb_lock, NULL); grptb = xmalloc(GNM_SZ*sizeof(GIDC *)); return (0); } /* * name_uid() * caches the name (if any) for the uid. If frc set, we always return the * the stored name (if valid or invalid match). We use a simple hash table. * Return * Pointer to stored name (or a empty string) */ char * name_uid(uid_t uid) { struct passwd *pw; UIDC *ptr; char *nam; cache_lock(&uidtb_lock); if ((uidtb == NULL) && (uidtb_start() < 0)) goto bad; /* * see if we have this uid cached */ ptr = uidtb[uid % UID_SZ]; if ((ptr != NULL) && (ptr->valid > 0) && (ptr->uid == uid)) { /* * have an entry for this uid */ if (ptr->valid == VALID) { nam = xstrdup(ptr->name); cache_unlock(&uidtb_lock); return (nam); } goto bad; } /* * No entry for this uid, we will add it */ if (ptr == NULL) ptr = uidtb[uid % UID_SZ] = (UIDC *)xmalloc(sizeof(UIDC)); pw = getpwuid(uid); if (pw == NULL) { ptr->uid = uid; ptr->valid = INVALID; goto bad; } else { /* * there is an entry for this uid in the password file */ ptr->uid = pw->pw_uid; strncpy(ptr->name, pw->pw_name, UNMLEN - 1); ptr->name[UNMLEN - 1] = '\0'; nam = xstrdup(ptr->name); ptr->valid = VALID; } cache_unlock(&uidtb_lock); return (nam); bad: cache_unlock(&uidtb_lock); return (NULL); } /* * name_gid() * caches the name (if any) for the gid. If frc set, we always return the * the stored name (if valid or invalid match). We use a simple hash table. * Return * Pointer to stored name (or a empty string) */ char * name_gid(gid_t gid) { struct group *gr; GIDC *ptr; char *nam; cache_lock(&gidtb_lock); if ((gidtb == NULL) && (gidtb_start() < 0)) goto bad; /* * see if we have this gid cached */ ptr = gidtb[gid % GID_SZ]; if ((ptr != NULL) && (ptr->valid > 0) && (ptr->gid == gid)) { /* * have an entry for this gid */ if (ptr->valid == VALID) { nam = xstrdup(ptr->name); cache_unlock(&gidtb_lock); return (nam); } goto bad; } /* * No entry for this gid, we will add it */ if (ptr == NULL) ptr = gidtb[gid % GID_SZ] = (GIDC *)xmalloc(sizeof(GIDC)); gr = getgrgid(gid); if (gr == NULL) { ptr->gid = gid; ptr->valid = INVALID; goto bad; } else { /* * there is an entry for this group in the group file */ ptr->gid = gr->gr_gid; strncpy(ptr->name, gr->gr_name, GNMLEN - 1); ptr->name[GNMLEN - 1] = '\0'; nam = xstrdup(ptr->name); ptr->valid = VALID; } cache_unlock(&gidtb_lock); return (nam); bad: cache_unlock(&gidtb_lock); return (NULL); } /* * uid_name() * caches the uid for a given user name. We use a simple hash table. * Return * the uid (if any) for a user name, or a -1 if no match can be found */ int uid_name(char *name, uid_t *uid) { struct passwd *pw; UIDC *ptr; int namelen; /* * return -1 for mangled names */ if (((namelen = strlen(name)) == 0) || (name[0] == '\0')) return (1); cache_lock(&usrtb_lock); if ((usrtb == NULL) && (usrtb_start() < 0)) goto bad; /* * look up in hash table, if found and valid return the uid, * if found and invalid, return a -1 */ ptr = usrtb[st_hash(name, namelen, UNM_SZ)]; if ((ptr != NULL) && (ptr->valid > 0) && !strcmp(name, ptr->name)) { if (ptr->valid == INVALID) goto bad; *uid = ptr->uid; cache_unlock(&usrtb_lock); return (0); } if (ptr == NULL) ptr = usrtb[st_hash(name, namelen, UNM_SZ)] = (UIDC *)xmalloc(sizeof(UIDC)); /* * no match, look it up, if no match store it as an invalid entry, * or store the matching uid */ pw = getpwnam(name); if (pw == NULL) { strncpy(ptr->name, name, UNMLEN - 1); ptr->valid = INVALID; goto bad; } else { strncpy(ptr->name, name, UNMLEN - 1); ptr->name[UNMLEN - 1] = '\0'; ptr->valid = VALID; *uid = ptr->uid = pw->pw_uid; } cache_unlock(&usrtb_lock); return (0); bad: cache_unlock(&usrtb_lock); return (1); } /* * gid_name() * caches the gid for a given group name. We use a simple hash table. * Return * the gid (if any) for a group name, or a -1 if no match can be found */ int gid_name(char *name, gid_t *gid) { struct group *gr; GIDC *ptr; int namelen; /* * return -1 for mangled names */ if (((namelen = strlen(name)) == 0) || (name[0] == '\0')) return (1); cache_lock(&grptb_lock); if ((grptb == NULL) && (grptb_start() < 0)) goto bad; /* * look up in hash table, if found and valid return the uid, * if found and invalid, return a -1 */ ptr = grptb[st_hash(name, namelen, GID_SZ)]; if ((ptr != NULL) && (ptr->valid > 0) && !strcmp(name, ptr->name)) { if (ptr->valid == INVALID) goto bad; *gid = ptr->gid; return (0); } if (ptr == NULL) ptr = grptb[st_hash(name, namelen, GID_SZ)] = (GIDC *)xmalloc(sizeof(GIDC)); /* * no match, look it up, if no match store it as an invalid entry, * or store the matching gid */ gr = getgrnam(name); if (gr == NULL) { strncpy(ptr->name, name, GNMLEN - 1); ptr->valid = INVALID; goto bad; } else { strncpy(ptr->name, name, GNMLEN - 1); ptr->name[GNMLEN - 1] = '\0'; ptr->valid = VALID; *gid = ptr->gid = gr->gr_gid; } cache_unlock(&grptb_lock); return (0); bad: cache_unlock(&grptb_lock); return (1); } /* * Inititalizes cache. */ void cache_init(void) { uidtb_start(); gidtb_start(); usrtb_start(); grptb_start(); } /* * Clean up by destroying mutexes. */ void cache_fini(void) { pthread_mutex_destroy(&uidtb_lock); pthread_mutex_destroy(&gidtb_lock); pthread_mutex_destroy(&usrtb_lock); pthread_mutex_destroy(&grptb_lock); free(uidtb); free(gidtb); free(usrtb); free(grptb); } static void cache_lock(pthread_mutex_t *lock) { int error; error = pthread_mutex_lock(lock); assert(!error); } static void cache_unlock(pthread_mutex_t *lock) { int error; error = pthread_mutex_unlock(lock); assert(!error); }