acpi: Narrow workaround for broken interrupt settings
[dragonfly.git] / lib / libc / gen / pwcache.c
blob0bb82a165d1231af0080b7057bf927af56c96be8
1 /* @(#)cache.c 8.1 (Berkeley) 5/31/93 */
2 /* $NetBSD: pwcache.c,v 1.31 2010/03/23 20:28:59 drochner Exp $ */
4 /*-
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
14 * are met:
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
34 * SUCH DAMAGE.
37 /*-
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
43 * are met:
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!
69 #undef group_from_gid
70 #undef user_from_uid
71 #endif
73 #ifndef EMBED_LIB_SRC
74 #include "namespace.h"
75 #endif
77 #include <sys/types.h>
78 #include <sys/param.h>
80 #include <assert.h>
81 #include <grp.h>
82 #include <pwd.h>
83 #include <stdio.h>
84 #include <stdlib.h>
85 #include <string.h>
86 #include <unistd.h>
87 #ifndef EMBED_LIB_SRC
88 #include "un-namespace.h"
89 #endif
91 #if HAVE_NBTOOL_CONFIG_H
92 /* XXX Now, re-apply the renaming that we undid above. */
93 #define group_from_gid __nbcompat_group_from_gid
94 #define user_from_uid __nbcompat_user_from_uid
95 #endif
97 #ifdef __weak_alias
98 __weak_alias(user_from_uid,_user_from_uid)
99 __weak_alias(group_from_gid,_group_from_gid)
100 __weak_alias(pwcache_groupdb,_pwcache_groupdb)
101 #endif
103 #if !HAVE_PWCACHE_USERDB || HAVE_NBTOOL_CONFIG_H
104 #include "pwcache.h"
107 * routines that control user, group, uid and gid caches (for the archive
108 * member print routine).
109 * IMPORTANT:
110 * these routines cache BOTH hits and misses, a major performance improvement
114 * function pointers to various name lookup routines.
115 * these may be changed as necessary.
117 static int (*_pwcache_setgroupent)(int) = setgroupent;
118 static void (*_pwcache_endgrent)(void) = endgrent;
119 static struct group * (*_pwcache_getgrnam)(const char *) = getgrnam;
120 static struct group * (*_pwcache_getgrgid)(gid_t) = getgrgid;
121 static int (*_pwcache_setpassent)(int) = setpassent;
122 static void (*_pwcache_endpwent)(void) = endpwent;
123 static struct passwd * (*_pwcache_getpwnam)(const char *) = getpwnam;
124 static struct passwd * (*_pwcache_getpwuid)(uid_t) = getpwuid;
127 * internal state
129 static int pwopn; /* is password file open */
130 static int gropn; /* is group file open */
131 static UIDC **uidtb; /* uid to name cache */
132 static GIDC **gidtb; /* gid to name cache */
133 static UIDC **usrtb; /* user name to uid cache */
134 static GIDC **grptb; /* group name to gid cache */
136 static int uidtb_fail; /* uidtb_start() failed ? */
137 static int gidtb_fail; /* gidtb_start() failed ? */
138 static int usrtb_fail; /* usrtb_start() failed ? */
139 static int grptb_fail; /* grptb_start() failed ? */
142 static u_int st_hash(const char *, size_t, int);
143 static int uidtb_start(void);
144 static int gidtb_start(void);
145 static int usrtb_start(void);
146 static int grptb_start(void);
149 static u_int
150 st_hash(const char *name, size_t len, int tabsz)
152 u_int key = 0;
154 _DIAGASSERT(name != NULL);
156 while (len--) {
157 key += *name++;
158 key = (key << 8) | (key >> 24);
161 return (key % tabsz);
165 * uidtb_start
166 * creates an an empty uidtb
167 * Return:
168 * 0 if ok, -1 otherwise
170 static int
171 uidtb_start(void)
174 if (uidtb != NULL)
175 return (0);
176 if (uidtb_fail)
177 return (-1);
178 if ((uidtb = (UIDC **)calloc(UID_SZ, sizeof(UIDC *))) == NULL) {
179 ++uidtb_fail;
180 return (-1);
182 return (0);
186 * gidtb_start
187 * creates an an empty gidtb
188 * Return:
189 * 0 if ok, -1 otherwise
191 static int
192 gidtb_start(void)
195 if (gidtb != NULL)
196 return (0);
197 if (gidtb_fail)
198 return (-1);
199 if ((gidtb = (GIDC **)calloc(GID_SZ, sizeof(GIDC *))) == NULL) {
200 ++gidtb_fail;
201 return (-1);
203 return (0);
207 * usrtb_start
208 * creates an an empty usrtb
209 * Return:
210 * 0 if ok, -1 otherwise
212 static int
213 usrtb_start(void)
216 if (usrtb != NULL)
217 return (0);
218 if (usrtb_fail)
219 return (-1);
220 if ((usrtb = (UIDC **)calloc(UNM_SZ, sizeof(UIDC *))) == NULL) {
221 ++usrtb_fail;
222 return (-1);
224 return (0);
228 * grptb_start
229 * creates an an empty grptb
230 * Return:
231 * 0 if ok, -1 otherwise
233 static int
234 grptb_start(void)
237 if (grptb != NULL)
238 return (0);
239 if (grptb_fail)
240 return (-1);
241 if ((grptb = (GIDC **)calloc(GNM_SZ, sizeof(GIDC *))) == NULL) {
242 ++grptb_fail;
243 return (-1);
245 return (0);
249 * user_from_uid()
250 * caches the name (if any) for the uid. If noname clear, we always
251 * return the stored name (if valid or invalid match).
252 * We use a simple hash table.
253 * Return
254 * Pointer to stored name (or a empty string)
256 const char *
257 user_from_uid(uid_t uid, int noname)
259 struct passwd *pw;
260 UIDC *ptr, **pptr;
262 if ((uidtb == NULL) && (uidtb_start() < 0))
263 return (NULL);
266 * see if we have this uid cached
268 pptr = uidtb + (uid % UID_SZ);
269 ptr = *pptr;
271 if ((ptr != NULL) && (ptr->valid > 0) && (ptr->uid == uid)) {
273 * have an entry for this uid
275 if (!noname || (ptr->valid == VALID))
276 return (ptr->name);
277 return (NULL);
281 * No entry for this uid, we will add it
283 if (!pwopn) {
284 if (_pwcache_setpassent != NULL)
285 (*_pwcache_setpassent)(1);
286 ++pwopn;
289 if (ptr == NULL)
290 *pptr = ptr = (UIDC *)malloc(sizeof(UIDC));
292 if ((pw = (*_pwcache_getpwuid)(uid)) == NULL) {
294 * no match for this uid in the local password file
295 * a string that is the uid in numeric format
297 if (ptr == NULL)
298 return (NULL);
299 ptr->uid = uid;
300 snprintf(ptr->name, UNMLEN, "%lu", (long) uid);
301 ptr->valid = INVALID;
302 if (noname)
303 return (NULL);
304 } else {
306 * there is an entry for this uid in the password file
308 if (ptr == NULL)
309 return (pw->pw_name);
310 ptr->uid = uid;
311 strlcpy(ptr->name, pw->pw_name, UNMLEN);
312 ptr->valid = VALID;
314 return (ptr->name);
318 * group_from_gid()
319 * caches the name (if any) for the gid. If noname clear, we always
320 * return the stored name (if valid or invalid match).
321 * We use a simple hash table.
322 * Return
323 * Pointer to stored name (or a empty string)
325 const char *
326 group_from_gid(gid_t gid, int noname)
328 struct group *gr;
329 GIDC *ptr, **pptr;
331 if ((gidtb == NULL) && (gidtb_start() < 0))
332 return (NULL);
335 * see if we have this gid cached
337 pptr = gidtb + (gid % GID_SZ);
338 ptr = *pptr;
340 if ((ptr != NULL) && (ptr->valid > 0) && (ptr->gid == gid)) {
342 * have an entry for this gid
344 if (!noname || (ptr->valid == VALID))
345 return (ptr->name);
346 return (NULL);
350 * No entry for this gid, we will add it
352 if (!gropn) {
353 if (_pwcache_setgroupent != NULL)
354 (*_pwcache_setgroupent)(1);
355 ++gropn;
358 if (ptr == NULL)
359 *pptr = ptr = (GIDC *)malloc(sizeof(GIDC));
361 if ((gr = (*_pwcache_getgrgid)(gid)) == NULL) {
363 * no match for this gid in the local group file, put in
364 * a string that is the gid in numberic format
366 if (ptr == NULL)
367 return (NULL);
368 ptr->gid = gid;
369 snprintf(ptr->name, GNMLEN, "%lu", (long) gid);
370 ptr->valid = INVALID;
371 if (noname)
372 return (NULL);
373 } else {
375 * there is an entry for this group in the group file
377 if (ptr == NULL)
378 return (gr->gr_name);
379 ptr->gid = gid;
380 strlcpy(ptr->name, gr->gr_name, GNMLEN);
381 ptr->valid = VALID;
383 return (ptr->name);
387 * uid_from_user()
388 * caches the uid for a given user name. We use a simple hash table.
389 * Return
390 * the uid (if any) for a user name, or a -1 if no match can be found
393 uid_from_user(const char *name, uid_t *uid)
395 struct passwd *pw;
396 UIDC *ptr, **pptr;
397 size_t namelen;
400 * return -1 for mangled names
402 if (name == NULL || ((namelen = strlen(name)) == 0))
403 return (-1);
404 if ((usrtb == NULL) && (usrtb_start() < 0))
405 return (-1);
408 * look up in hash table, if found and valid return the uid,
409 * if found and invalid, return a -1
411 pptr = usrtb + st_hash(name, namelen, UNM_SZ);
412 ptr = *pptr;
414 if ((ptr != NULL) && (ptr->valid > 0) && !strcmp(name, ptr->name)) {
415 if (ptr->valid == INVALID)
416 return (-1);
417 *uid = ptr->uid;
418 return (0);
421 if (!pwopn) {
422 if (_pwcache_setpassent != NULL)
423 (*_pwcache_setpassent)(1);
424 ++pwopn;
427 if (ptr == NULL)
428 *pptr = ptr = (UIDC *)malloc(sizeof(UIDC));
431 * no match, look it up, if no match store it as an invalid entry,
432 * or store the matching uid
434 if (ptr == NULL) {
435 if ((pw = (*_pwcache_getpwnam)(name)) == NULL)
436 return (-1);
437 *uid = pw->pw_uid;
438 return (0);
440 strlcpy(ptr->name, name, UNMLEN);
441 if ((pw = (*_pwcache_getpwnam)(name)) == NULL) {
442 ptr->valid = INVALID;
443 return (-1);
445 ptr->valid = VALID;
446 *uid = ptr->uid = pw->pw_uid;
447 return (0);
451 * gid_from_group()
452 * caches the gid for a given group name. We use a simple hash table.
453 * Return
454 * the gid (if any) for a group name, or a -1 if no match can be found
457 gid_from_group(const char *name, gid_t *gid)
459 struct group *gr;
460 GIDC *ptr, **pptr;
461 size_t namelen;
464 * return -1 for mangled names
466 if (name == NULL || ((namelen = strlen(name)) == 0))
467 return (-1);
468 if ((grptb == NULL) && (grptb_start() < 0))
469 return (-1);
472 * look up in hash table, if found and valid return the uid,
473 * if found and invalid, return a -1
475 pptr = grptb + st_hash(name, namelen, GID_SZ);
476 ptr = *pptr;
478 if ((ptr != NULL) && (ptr->valid > 0) && !strcmp(name, ptr->name)) {
479 if (ptr->valid == INVALID)
480 return (-1);
481 *gid = ptr->gid;
482 return (0);
485 if (!gropn) {
486 if (_pwcache_setgroupent != NULL)
487 (*_pwcache_setgroupent)(1);
488 ++gropn;
491 if (ptr == NULL)
492 *pptr = ptr = (GIDC *)malloc(sizeof(GIDC));
495 * no match, look it up, if no match store it as an invalid entry,
496 * or store the matching gid
498 if (ptr == NULL) {
499 if ((gr = (*_pwcache_getgrnam)(name)) == NULL)
500 return (-1);
501 *gid = gr->gr_gid;
502 return (0);
505 strlcpy(ptr->name, name, GNMLEN);
506 if ((gr = (*_pwcache_getgrnam)(name)) == NULL) {
507 ptr->valid = INVALID;
508 return (-1);
510 ptr->valid = VALID;
511 *gid = ptr->gid = gr->gr_gid;
512 return (0);
515 #define FLUSHTB(arr, len, fail) \
516 do { \
517 if (arr != NULL) { \
518 for (i = 0; i < len; i++) \
519 if (arr[i] != NULL) \
520 free(arr[i]); \
521 arr = NULL; \
523 fail = 0; \
524 } while (/* CONSTCOND */0);
527 pwcache_userdb(
528 int (*a_setpassent)(int),
529 void (*a_endpwent)(void),
530 struct passwd * (*a_getpwnam)(const char *),
531 struct passwd * (*a_getpwuid)(uid_t))
533 int i;
535 /* a_setpassent and a_endpwent may be NULL */
536 if (a_getpwnam == NULL || a_getpwuid == NULL)
537 return (-1);
539 if (_pwcache_endpwent != NULL)
540 (*_pwcache_endpwent)();
541 FLUSHTB(uidtb, UID_SZ, uidtb_fail);
542 FLUSHTB(usrtb, UNM_SZ, usrtb_fail);
543 pwopn = 0;
544 _pwcache_setpassent = a_setpassent;
545 _pwcache_endpwent = a_endpwent;
546 _pwcache_getpwnam = a_getpwnam;
547 _pwcache_getpwuid = a_getpwuid;
549 return (0);
553 pwcache_groupdb(
554 int (*a_setgroupent)(int),
555 void (*a_endgrent)(void),
556 struct group * (*a_getgrnam)(const char *),
557 struct group * (*a_getgrgid)(gid_t))
559 int i;
561 /* a_setgroupent and a_endgrent may be NULL */
562 if (a_getgrnam == NULL || a_getgrgid == NULL)
563 return (-1);
565 if (_pwcache_endgrent != NULL)
566 (*_pwcache_endgrent)();
567 FLUSHTB(gidtb, GID_SZ, gidtb_fail);
568 FLUSHTB(grptb, GNM_SZ, grptb_fail);
569 gropn = 0;
570 _pwcache_setgroupent = a_setgroupent;
571 _pwcache_endgrent = a_endgrent;
572 _pwcache_getgrnam = a_getgrnam;
573 _pwcache_getgrgid = a_getgrgid;
575 return (0);
579 #ifdef TEST_PWCACHE
581 struct passwd *
582 test_getpwnam(const char *name)
584 static struct passwd foo;
586 memset(&foo, 0, sizeof(foo));
587 if (strcmp(name, "toor") == 0) {
588 foo.pw_uid = 666;
589 return &foo;
591 return (getpwnam(name));
595 main(int argc, char *argv[])
597 uid_t u;
598 int r, i;
600 printf("pass 1 (default userdb)\n");
601 for (i = 1; i < argc; i++) {
602 printf("i: %d, pwopn %d usrtb_fail %d usrtb %p\n",
603 i, pwopn, usrtb_fail, usrtb);
604 r = uid_from_user(argv[i], &u);
605 if (r == -1)
606 printf(" uid_from_user %s: failed\n", argv[i]);
607 else
608 printf(" uid_from_user %s: %d\n", argv[i], u);
610 printf("pass 1 finish: pwopn %d usrtb_fail %d usrtb %p\n",
611 pwopn, usrtb_fail, usrtb);
613 puts("");
614 printf("pass 2 (replacement userdb)\n");
615 printf("pwcache_userdb returned %d\n",
616 pwcache_userdb(setpassent, test_getpwnam, getpwuid));
617 printf("pwopn %d usrtb_fail %d usrtb %p\n", pwopn, usrtb_fail, usrtb);
619 for (i = 1; i < argc; i++) {
620 printf("i: %d, pwopn %d usrtb_fail %d usrtb %p\n",
621 i, pwopn, usrtb_fail, usrtb);
622 u = -1;
623 r = uid_from_user(argv[i], &u);
624 if (r == -1)
625 printf(" uid_from_user %s: failed\n", argv[i]);
626 else
627 printf(" uid_from_user %s: %d\n", argv[i], u);
629 printf("pass 2 finish: pwopn %d usrtb_fail %d usrtb %p\n",
630 pwopn, usrtb_fail, usrtb);
632 puts("");
633 printf("pass 3 (null pointers)\n");
634 printf("pwcache_userdb returned %d\n",
635 pwcache_userdb(NULL, NULL, NULL));
637 return (0);
639 #endif /* TEST_PWCACHE */
640 #endif /* !HAVE_PWCACHE_USERDB */