1 /* $NetBSD: pwcache.c,v 1.31 2010/03/23 20:28:59 drochner Exp $ */
4 * Copyright (c) 1992 Keith Muller.
5 * Copyright (c) 1992, 1993
6 * The Regents of the University of California. All rights reserved.
8 * This code is derived from software contributed to Berkeley by
9 * Keith Muller of the University of California, San Diego.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * Copyright (c) 2002 The NetBSD Foundation, Inc.
38 * All rights reserved.
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the distribution.
49 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
50 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
51 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
52 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
53 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
54 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
55 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
56 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
57 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
58 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
59 * POSSIBILITY OF SUCH DAMAGE.
62 #if HAVE_NBTOOL_CONFIG_H
63 #include "nbtool_config.h"
65 * XXX Undefine the renames of these functions so that we don't
66 * XXX rename the versions found in the host's <pwd.h> by mistake!
72 #include <sys/cdefs.h>
73 #if defined(LIBC_SCCS) && !defined(lint)
75 static char sccsid[] = "@(#)cache.c 8.1 (Berkeley) 5/31/93";
77 __RCSID("$NetBSD: pwcache.c,v 1.31 2010/03/23 20:28:59 drochner Exp $");
79 #endif /* LIBC_SCCS and not lint */
81 #include "namespace.h"
83 #include <sys/types.h>
84 #include <sys/param.h>
94 #define _DIAGASSERT(x) assert((x))
96 #if HAVE_NBTOOL_CONFIG_H
97 /* XXX Now, re-apply the renaming that we undid above. */
98 #define group_from_gid __nbcompat_group_from_gid
99 #define user_from_uid __nbcompat_user_from_uid
103 __weak_alias(user_from_uid,_user_from_uid)
104 __weak_alias(group_from_gid,_group_from_gid)
105 __weak_alias(pwcache_groupdb,_pwcache_groupdb)
108 #if !HAVE_PWCACHE_USERDB || HAVE_NBTOOL_CONFIG_H
112 * routines that control user, group, uid and gid caches (for the archive
113 * member print routine).
115 * these routines cache BOTH hits and misses, a major performance improvement
119 * function pointers to various name lookup routines.
120 * these may be changed as necessary.
122 static int (*_pwcache_setgroupent)(int) = setgroupent;
123 static void (*_pwcache_endgrent)(void) = endgrent;
124 static struct group * (*_pwcache_getgrnam)(const char *) = getgrnam;
125 static struct group * (*_pwcache_getgrgid)(gid_t) = getgrgid;
126 static int (*_pwcache_setpassent)(int) = setpassent;
127 static void (*_pwcache_endpwent)(void) = endpwent;
128 static struct passwd * (*_pwcache_getpwnam)(const char *) = getpwnam;
129 static struct passwd * (*_pwcache_getpwuid)(uid_t) = getpwuid;
134 static int pwopn; /* is password file open */
135 static int gropn; /* is group file open */
136 static UIDC **uidtb; /* uid to name cache */
137 static GIDC **gidtb; /* gid to name cache */
138 static UIDC **usrtb; /* user name to uid cache */
139 static GIDC **grptb; /* group name to gid cache */
141 static int uidtb_fail; /* uidtb_start() failed ? */
142 static int gidtb_fail; /* gidtb_start() failed ? */
143 static int usrtb_fail; /* usrtb_start() failed ? */
144 static int grptb_fail; /* grptb_start() failed ? */
147 static u_int st_hash(const char *, size_t, int);
148 static int uidtb_start(void);
149 static int gidtb_start(void);
150 static int usrtb_start(void);
151 static int grptb_start(void);
155 st_hash(const char *name, size_t len, int tabsz)
159 _DIAGASSERT(name != NULL);
163 key = (key << 8) | (key >> 24);
166 return (key % tabsz);
171 * creates an an empty uidtb
173 * 0 if ok, -1 otherwise
183 if ((uidtb = (UIDC **)calloc(UID_SZ, sizeof(UIDC *))) == NULL) {
192 * creates an an empty gidtb
194 * 0 if ok, -1 otherwise
204 if ((gidtb = (GIDC **)calloc(GID_SZ, sizeof(GIDC *))) == NULL) {
213 * creates an an empty usrtb
215 * 0 if ok, -1 otherwise
225 if ((usrtb = (UIDC **)calloc(UNM_SZ, sizeof(UIDC *))) == NULL) {
234 * creates an an empty grptb
236 * 0 if ok, -1 otherwise
246 if ((grptb = (GIDC **)calloc(GNM_SZ, sizeof(GIDC *))) == NULL) {
255 * caches the name (if any) for the uid. If noname clear, we always
256 * return the stored name (if valid or invalid match).
257 * We use a simple hash table.
259 * Pointer to stored name (or a empty string)
262 user_from_uid(uid_t uid, int noname)
267 if ((uidtb == NULL) && (uidtb_start() < 0))
271 * see if we have this uid cached
273 pptr = uidtb + (uid % UID_SZ);
276 if ((ptr != NULL) && (ptr->valid > 0) && (ptr->uid == uid)) {
278 * have an entry for this uid
280 if (!noname || (ptr->valid == VALID))
286 * No entry for this uid, we will add it
289 if (_pwcache_setpassent != NULL)
290 (*_pwcache_setpassent)(1);
295 *pptr = ptr = (UIDC *)malloc(sizeof(UIDC));
297 if ((pw = (*_pwcache_getpwuid)(uid)) == NULL) {
299 * no match for this uid in the local password file
300 * a string that is the uid in numeric format
305 (void)snprintf(ptr->name, UNMLEN, "%lu", (long) uid);
306 ptr->valid = INVALID;
311 * there is an entry for this uid in the password file
314 return (pw->pw_name);
316 (void)strlcpy(ptr->name, pw->pw_name, UNMLEN);
324 * caches the name (if any) for the gid. If noname clear, we always
325 * return the stored name (if valid or invalid match).
326 * We use a simple hash table.
328 * Pointer to stored name (or a empty string)
331 group_from_gid(gid_t gid, int noname)
336 if ((gidtb == NULL) && (gidtb_start() < 0))
340 * see if we have this gid cached
342 pptr = gidtb + (gid % GID_SZ);
345 if ((ptr != NULL) && (ptr->valid > 0) && (ptr->gid == gid)) {
347 * have an entry for this gid
349 if (!noname || (ptr->valid == VALID))
355 * No entry for this gid, we will add it
358 if (_pwcache_setgroupent != NULL)
359 (*_pwcache_setgroupent)(1);
364 *pptr = ptr = (GIDC *)malloc(sizeof(GIDC));
366 if ((gr = (*_pwcache_getgrgid)(gid)) == NULL) {
368 * no match for this gid in the local group file, put in
369 * a string that is the gid in numberic format
374 (void)snprintf(ptr->name, GNMLEN, "%lu", (long) gid);
375 ptr->valid = INVALID;
380 * there is an entry for this group in the group file
383 return (gr->gr_name);
385 (void)strlcpy(ptr->name, gr->gr_name, GNMLEN);
393 * caches the uid for a given user name. We use a simple hash table.
395 * the uid (if any) for a user name, or a -1 if no match can be found
398 uid_from_user(const char *name, uid_t *uid)
405 * return -1 for mangled names
407 if (name == NULL || ((namelen = strlen(name)) == 0))
409 if ((usrtb == NULL) && (usrtb_start() < 0))
413 * look up in hash table, if found and valid return the uid,
414 * if found and invalid, return a -1
416 pptr = usrtb + st_hash(name, namelen, UNM_SZ);
419 if ((ptr != NULL) && (ptr->valid > 0) && !strcmp(name, ptr->name)) {
420 if (ptr->valid == INVALID)
427 if (_pwcache_setpassent != NULL)
428 (*_pwcache_setpassent)(1);
433 *pptr = ptr = (UIDC *)malloc(sizeof(UIDC));
436 * no match, look it up, if no match store it as an invalid entry,
437 * or store the matching uid
440 if ((pw = (*_pwcache_getpwnam)(name)) == NULL)
445 (void)strlcpy(ptr->name, name, UNMLEN);
446 if ((pw = (*_pwcache_getpwnam)(name)) == NULL) {
447 ptr->valid = INVALID;
451 *uid = ptr->uid = pw->pw_uid;
457 * caches the gid for a given group name. We use a simple hash table.
459 * the gid (if any) for a group name, or a -1 if no match can be found
462 gid_from_group(const char *name, gid_t *gid)
469 * return -1 for mangled names
471 if (name == NULL || ((namelen = strlen(name)) == 0))
473 if ((grptb == NULL) && (grptb_start() < 0))
477 * look up in hash table, if found and valid return the uid,
478 * if found and invalid, return a -1
480 pptr = grptb + st_hash(name, namelen, GID_SZ);
483 if ((ptr != NULL) && (ptr->valid > 0) && !strcmp(name, ptr->name)) {
484 if (ptr->valid == INVALID)
491 if (_pwcache_setgroupent != NULL)
492 (*_pwcache_setgroupent)(1);
497 *pptr = ptr = (GIDC *)malloc(sizeof(GIDC));
500 * no match, look it up, if no match store it as an invalid entry,
501 * or store the matching gid
504 if ((gr = (*_pwcache_getgrnam)(name)) == NULL)
510 (void)strlcpy(ptr->name, name, GNMLEN);
511 if ((gr = (*_pwcache_getgrnam)(name)) == NULL) {
512 ptr->valid = INVALID;
516 *gid = ptr->gid = gr->gr_gid;
520 #define FLUSHTB(arr, len, fail) \
523 for (i = 0; i < len; i++) \
524 if (arr[i] != NULL) \
529 } while (/* CONSTCOND */0);
533 int (*a_setpassent)(int),
534 void (*a_endpwent)(void),
535 struct passwd * (*a_getpwnam)(const char *),
536 struct passwd * (*a_getpwuid)(uid_t))
540 /* a_setpassent and a_endpwent may be NULL */
541 if (a_getpwnam == NULL || a_getpwuid == NULL)
544 if (_pwcache_endpwent != NULL)
545 (*_pwcache_endpwent)();
546 FLUSHTB(uidtb, UID_SZ, uidtb_fail);
547 FLUSHTB(usrtb, UNM_SZ, usrtb_fail);
549 _pwcache_setpassent = a_setpassent;
550 _pwcache_endpwent = a_endpwent;
551 _pwcache_getpwnam = a_getpwnam;
552 _pwcache_getpwuid = a_getpwuid;
559 int (*a_setgroupent)(int),
560 void (*a_endgrent)(void),
561 struct group * (*a_getgrnam)(const char *),
562 struct group * (*a_getgrgid)(gid_t))
566 /* a_setgroupent and a_endgrent may be NULL */
567 if (a_getgrnam == NULL || a_getgrgid == NULL)
570 if (_pwcache_endgrent != NULL)
571 (*_pwcache_endgrent)();
572 FLUSHTB(gidtb, GID_SZ, gidtb_fail);
573 FLUSHTB(grptb, GNM_SZ, grptb_fail);
575 _pwcache_setgroupent = a_setgroupent;
576 _pwcache_endgrent = a_endgrent;
577 _pwcache_getgrnam = a_getgrnam;
578 _pwcache_getgrgid = a_getgrgid;
587 test_getpwnam(const char *name)
589 static struct passwd foo;
591 memset(&foo, 0, sizeof(foo));
592 if (strcmp(name, "toor") == 0) {
596 return (getpwnam(name));
600 main(int argc, char *argv[])
605 printf("pass 1 (default userdb)\n");
606 for (i = 1; i < argc; i++) {
607 printf("i: %d, pwopn %d usrtb_fail %d usrtb %p\n",
608 i, pwopn, usrtb_fail, usrtb);
609 r = uid_from_user(argv[i], &u);
611 printf(" uid_from_user %s: failed\n", argv[i]);
613 printf(" uid_from_user %s: %d\n", argv[i], u);
615 printf("pass 1 finish: pwopn %d usrtb_fail %d usrtb %p\n",
616 pwopn, usrtb_fail, usrtb);
619 printf("pass 2 (replacement userdb)\n");
620 printf("pwcache_userdb returned %d\n",
621 pwcache_userdb(setpassent, test_getpwnam, getpwuid));
622 printf("pwopn %d usrtb_fail %d usrtb %p\n", pwopn, usrtb_fail, usrtb);
624 for (i = 1; i < argc; i++) {
625 printf("i: %d, pwopn %d usrtb_fail %d usrtb %p\n",
626 i, pwopn, usrtb_fail, usrtb);
628 r = uid_from_user(argv[i], &u);
630 printf(" uid_from_user %s: failed\n", argv[i]);
632 printf(" uid_from_user %s: %d\n", argv[i], u);
634 printf("pass 2 finish: pwopn %d usrtb_fail %d usrtb %p\n",
635 pwopn, usrtb_fail, usrtb);
638 printf("pass 3 (null pointers)\n");
639 printf("pwcache_userdb returned %d\n",
640 pwcache_userdb(NULL, NULL, NULL));
644 #endif /* TEST_PWCACHE */
645 #endif /* !HAVE_PWCACHE_USERDB */