1 /* @(#)cache.c 8.1 (Berkeley) 5/31/93 */
2 /* $NetBSD: pwcache.c,v 1.31 2010/03/23 20:28:59 drochner Exp $ */
5 * Copyright (c) 1992 Keith Muller.
6 * Copyright (c) 1992, 1993
7 * The Regents of the University of California. All rights reserved.
9 * This code is derived from software contributed to Berkeley by
10 * Keith Muller of the University of California, San Diego.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * Copyright (c) 2002 The NetBSD Foundation, Inc.
39 * All rights reserved.
41 * Redistribution and use in source and binary forms, with or without
42 * modification, are permitted provided that the following conditions
44 * 1. Redistributions of source code must retain the above copyright
45 * notice, this list of conditions and the following disclaimer.
46 * 2. Redistributions in binary form must reproduce the above copyright
47 * notice, this list of conditions and the following disclaimer in the
48 * documentation and/or other materials provided with the distribution.
50 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
51 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
52 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
53 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
54 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
55 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
56 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
57 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
58 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
59 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
60 * POSSIBILITY OF SUCH DAMAGE.
63 #if HAVE_NBTOOL_CONFIG_H
64 #include "nbtool_config.h"
66 * XXX Undefine the renames of these functions so that we don't
67 * XXX rename the versions found in the host's <pwd.h> by mistake!
73 #include "namespace.h"
75 #include <sys/types.h>
76 #include <sys/param.h>
85 #include "un-namespace.h"
87 #if HAVE_NBTOOL_CONFIG_H
88 /* XXX Now, re-apply the renaming that we undid above. */
89 #define group_from_gid __nbcompat_group_from_gid
90 #define user_from_uid __nbcompat_user_from_uid
94 __weak_alias(user_from_uid,_user_from_uid)
95 __weak_alias(group_from_gid,_group_from_gid)
96 __weak_alias(pwcache_groupdb,_pwcache_groupdb)
99 #if !HAVE_PWCACHE_USERDB || HAVE_NBTOOL_CONFIG_H
103 * routines that control user, group, uid and gid caches (for the archive
104 * member print routine).
106 * these routines cache BOTH hits and misses, a major performance improvement
110 * function pointers to various name lookup routines.
111 * these may be changed as necessary.
113 static int (*_pwcache_setgroupent)(int) = setgroupent;
114 static void (*_pwcache_endgrent)(void) = endgrent;
115 static struct group * (*_pwcache_getgrnam)(const char *) = getgrnam;
116 static struct group * (*_pwcache_getgrgid)(gid_t) = getgrgid;
117 static int (*_pwcache_setpassent)(int) = setpassent;
118 static void (*_pwcache_endpwent)(void) = endpwent;
119 static struct passwd * (*_pwcache_getpwnam)(const char *) = getpwnam;
120 static struct passwd * (*_pwcache_getpwuid)(uid_t) = getpwuid;
125 static int pwopn; /* is password file open */
126 static int gropn; /* is group file open */
127 static UIDC **uidtb; /* uid to name cache */
128 static GIDC **gidtb; /* gid to name cache */
129 static UIDC **usrtb; /* user name to uid cache */
130 static GIDC **grptb; /* group name to gid cache */
132 static int uidtb_fail; /* uidtb_start() failed ? */
133 static int gidtb_fail; /* gidtb_start() failed ? */
134 static int usrtb_fail; /* usrtb_start() failed ? */
135 static int grptb_fail; /* grptb_start() failed ? */
138 static u_int st_hash(const char *, size_t, int);
139 static int uidtb_start(void);
140 static int gidtb_start(void);
141 static int usrtb_start(void);
142 static int grptb_start(void);
146 st_hash(const char *name, size_t len, int tabsz)
150 _DIAGASSERT(name != NULL);
154 key = (key << 8) | (key >> 24);
157 return (key % tabsz);
162 * creates an an empty uidtb
164 * 0 if ok, -1 otherwise
174 if ((uidtb = (UIDC **)calloc(UID_SZ, sizeof(UIDC *))) == NULL) {
183 * creates an an empty gidtb
185 * 0 if ok, -1 otherwise
195 if ((gidtb = (GIDC **)calloc(GID_SZ, sizeof(GIDC *))) == NULL) {
204 * creates an an empty usrtb
206 * 0 if ok, -1 otherwise
216 if ((usrtb = (UIDC **)calloc(UNM_SZ, sizeof(UIDC *))) == NULL) {
225 * creates an an empty grptb
227 * 0 if ok, -1 otherwise
237 if ((grptb = (GIDC **)calloc(GNM_SZ, sizeof(GIDC *))) == NULL) {
246 * caches the name (if any) for the uid. If noname clear, we always
247 * return the stored name (if valid or invalid match).
248 * We use a simple hash table.
250 * Pointer to stored name (or a empty string)
253 user_from_uid(uid_t uid, int noname)
258 if ((uidtb == NULL) && (uidtb_start() < 0))
262 * see if we have this uid cached
264 pptr = uidtb + (uid % UID_SZ);
267 if ((ptr != NULL) && (ptr->valid > 0) && (ptr->uid == uid)) {
269 * have an entry for this uid
271 if (!noname || (ptr->valid == VALID))
277 * No entry for this uid, we will add it
280 if (_pwcache_setpassent != NULL)
281 (*_pwcache_setpassent)(1);
286 *pptr = ptr = (UIDC *)malloc(sizeof(UIDC));
288 if ((pw = (*_pwcache_getpwuid)(uid)) == NULL) {
290 * no match for this uid in the local password file
291 * a string that is the uid in numeric format
296 snprintf(ptr->name, UNMLEN, "%lu", (long) uid);
297 ptr->valid = INVALID;
302 * there is an entry for this uid in the password file
305 return (pw->pw_name);
307 strlcpy(ptr->name, pw->pw_name, UNMLEN);
315 * caches the name (if any) for the gid. If noname clear, we always
316 * return the stored name (if valid or invalid match).
317 * We use a simple hash table.
319 * Pointer to stored name (or a empty string)
322 group_from_gid(gid_t gid, int noname)
327 if ((gidtb == NULL) && (gidtb_start() < 0))
331 * see if we have this gid cached
333 pptr = gidtb + (gid % GID_SZ);
336 if ((ptr != NULL) && (ptr->valid > 0) && (ptr->gid == gid)) {
338 * have an entry for this gid
340 if (!noname || (ptr->valid == VALID))
346 * No entry for this gid, we will add it
349 if (_pwcache_setgroupent != NULL)
350 (*_pwcache_setgroupent)(1);
355 *pptr = ptr = (GIDC *)malloc(sizeof(GIDC));
357 if ((gr = (*_pwcache_getgrgid)(gid)) == NULL) {
359 * no match for this gid in the local group file, put in
360 * a string that is the gid in numberic format
365 snprintf(ptr->name, GNMLEN, "%lu", (long) gid);
366 ptr->valid = INVALID;
371 * there is an entry for this group in the group file
374 return (gr->gr_name);
376 strlcpy(ptr->name, gr->gr_name, GNMLEN);
384 * caches the uid for a given user name. We use a simple hash table.
386 * the uid (if any) for a user name, or a -1 if no match can be found
389 uid_from_user(const char *name, uid_t *uid)
396 * return -1 for mangled names
398 if (name == NULL || ((namelen = strlen(name)) == 0))
400 if ((usrtb == NULL) && (usrtb_start() < 0))
404 * look up in hash table, if found and valid return the uid,
405 * if found and invalid, return a -1
407 pptr = usrtb + st_hash(name, namelen, UNM_SZ);
410 if ((ptr != NULL) && (ptr->valid > 0) && !strcmp(name, ptr->name)) {
411 if (ptr->valid == INVALID)
418 if (_pwcache_setpassent != NULL)
419 (*_pwcache_setpassent)(1);
424 *pptr = ptr = (UIDC *)malloc(sizeof(UIDC));
427 * no match, look it up, if no match store it as an invalid entry,
428 * or store the matching uid
431 if ((pw = (*_pwcache_getpwnam)(name)) == NULL)
436 strlcpy(ptr->name, name, UNMLEN);
437 if ((pw = (*_pwcache_getpwnam)(name)) == NULL) {
438 ptr->valid = INVALID;
442 *uid = ptr->uid = pw->pw_uid;
448 * caches the gid for a given group name. We use a simple hash table.
450 * the gid (if any) for a group name, or a -1 if no match can be found
453 gid_from_group(const char *name, gid_t *gid)
460 * return -1 for mangled names
462 if (name == NULL || ((namelen = strlen(name)) == 0))
464 if ((grptb == NULL) && (grptb_start() < 0))
468 * look up in hash table, if found and valid return the uid,
469 * if found and invalid, return a -1
471 pptr = grptb + st_hash(name, namelen, GID_SZ);
474 if ((ptr != NULL) && (ptr->valid > 0) && !strcmp(name, ptr->name)) {
475 if (ptr->valid == INVALID)
482 if (_pwcache_setgroupent != NULL)
483 (*_pwcache_setgroupent)(1);
488 *pptr = ptr = (GIDC *)malloc(sizeof(GIDC));
491 * no match, look it up, if no match store it as an invalid entry,
492 * or store the matching gid
495 if ((gr = (*_pwcache_getgrnam)(name)) == NULL)
501 strlcpy(ptr->name, name, GNMLEN);
502 if ((gr = (*_pwcache_getgrnam)(name)) == NULL) {
503 ptr->valid = INVALID;
507 *gid = ptr->gid = gr->gr_gid;
511 #define FLUSHTB(arr, len, fail) \
514 for (i = 0; i < len; i++) \
515 if (arr[i] != NULL) \
520 } while (/* CONSTCOND */0);
524 int (*a_setpassent)(int),
525 void (*a_endpwent)(void),
526 struct passwd * (*a_getpwnam)(const char *),
527 struct passwd * (*a_getpwuid)(uid_t))
531 /* a_setpassent and a_endpwent may be NULL */
532 if (a_getpwnam == NULL || a_getpwuid == NULL)
535 if (_pwcache_endpwent != NULL)
536 (*_pwcache_endpwent)();
537 FLUSHTB(uidtb, UID_SZ, uidtb_fail);
538 FLUSHTB(usrtb, UNM_SZ, usrtb_fail);
540 _pwcache_setpassent = a_setpassent;
541 _pwcache_endpwent = a_endpwent;
542 _pwcache_getpwnam = a_getpwnam;
543 _pwcache_getpwuid = a_getpwuid;
550 int (*a_setgroupent)(int),
551 void (*a_endgrent)(void),
552 struct group * (*a_getgrnam)(const char *),
553 struct group * (*a_getgrgid)(gid_t))
557 /* a_setgroupent and a_endgrent may be NULL */
558 if (a_getgrnam == NULL || a_getgrgid == NULL)
561 if (_pwcache_endgrent != NULL)
562 (*_pwcache_endgrent)();
563 FLUSHTB(gidtb, GID_SZ, gidtb_fail);
564 FLUSHTB(grptb, GNM_SZ, grptb_fail);
566 _pwcache_setgroupent = a_setgroupent;
567 _pwcache_endgrent = a_endgrent;
568 _pwcache_getgrnam = a_getgrnam;
569 _pwcache_getgrgid = a_getgrgid;
578 test_getpwnam(const char *name)
580 static struct passwd foo;
582 memset(&foo, 0, sizeof(foo));
583 if (strcmp(name, "toor") == 0) {
587 return (getpwnam(name));
591 main(int argc, char *argv[])
596 printf("pass 1 (default userdb)\n");
597 for (i = 1; i < argc; i++) {
598 printf("i: %d, pwopn %d usrtb_fail %d usrtb %p\n",
599 i, pwopn, usrtb_fail, usrtb);
600 r = uid_from_user(argv[i], &u);
602 printf(" uid_from_user %s: failed\n", argv[i]);
604 printf(" uid_from_user %s: %d\n", argv[i], u);
606 printf("pass 1 finish: pwopn %d usrtb_fail %d usrtb %p\n",
607 pwopn, usrtb_fail, usrtb);
610 printf("pass 2 (replacement userdb)\n");
611 printf("pwcache_userdb returned %d\n",
612 pwcache_userdb(setpassent, test_getpwnam, getpwuid));
613 printf("pwopn %d usrtb_fail %d usrtb %p\n", pwopn, usrtb_fail, usrtb);
615 for (i = 1; i < argc; i++) {
616 printf("i: %d, pwopn %d usrtb_fail %d usrtb %p\n",
617 i, pwopn, usrtb_fail, usrtb);
619 r = uid_from_user(argv[i], &u);
621 printf(" uid_from_user %s: failed\n", argv[i]);
623 printf(" uid_from_user %s: %d\n", argv[i], u);
625 printf("pass 2 finish: pwopn %d usrtb_fail %d usrtb %p\n",
626 pwopn, usrtb_fail, usrtb);
629 printf("pass 3 (null pointers)\n");
630 printf("pwcache_userdb returned %d\n",
631 pwcache_userdb(NULL, NULL, NULL));
635 #endif /* TEST_PWCACHE */
636 #endif /* !HAVE_PWCACHE_USERDB */