pwcache.c revision 1.21.2.1 1 /* $NetBSD: pwcache.c,v 1.21.2.1 2004/07/23 14:33:14 tron Exp $ */
2
3 /*-
4 * Copyright (c) 1992 Keith Muller.
5 * Copyright (c) 1992, 1993
6 * The Regents of the University of California. All rights reserved.
7 *
8 * This code is derived from software contributed to Berkeley by
9 * Keith Muller of the University of California, San Diego.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
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. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
38 */
39
40 /*-
41 * Copyright (c) 2002 The NetBSD Foundation, Inc.
42 * All rights reserved.
43 *
44 * Redistribution and use in source and binary forms, with or without
45 * modification, are permitted provided that the following conditions
46 * are met:
47 * 1. Redistributions of source code must retain the above copyright
48 * notice, this list of conditions and the following disclaimer.
49 * 2. Redistributions in binary form must reproduce the above copyright
50 * notice, this list of conditions and the following disclaimer in the
51 * documentation and/or other materials provided with the distribution.
52 * 3. All advertising materials mentioning features or use of this software
53 * must display the following acknowledgement:
54 * This product includes software developed by the NetBSD
55 * Foundation, Inc. and its contributors.
56 * 4. Neither the name of The NetBSD Foundation nor the names of its
57 * contributors may be used to endorse or promote products derived
58 * from this software without specific prior written permission.
59 *
60 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
61 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
62 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
63 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
64 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
65 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
66 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
67 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
68 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
69 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
70 * POSSIBILITY OF SUCH DAMAGE.
71 */
72
73 #if HAVE_CONFIG_H
74 #include "config.h"
75 /*
76 * XXX Undefine the renames of these functions so that we don't
77 * XXX rename the versions found in the host's <pwd.h> by mistake!
78 */
79 #undef group_from_gid
80 #undef user_from_uid
81 #endif
82
83
84 #include <sys/cdefs.h>
85 #if defined(LIBC_SCCS) && !defined(lint)
86 #if 0
87 static char sccsid[] = "@(#)cache.c 8.1 (Berkeley) 5/31/93";
88 #else
89 __RCSID("$NetBSD: pwcache.c,v 1.21.2.1 2004/07/23 14:33:14 tron Exp $");
90 #endif
91 #endif /* LIBC_SCCS and not lint */
92
93 #include "namespace.h"
94
95 #include <sys/types.h>
96 #include <sys/param.h>
97
98 #include <assert.h>
99 #include <grp.h>
100 #include <pwd.h>
101 #include <stdio.h>
102 #include <stdlib.h>
103 #include <string.h>
104 #include <unistd.h>
105
106 #if HAVE_CONFIG_H
107 /* XXX Now, re-apply the renaming that we undid above. */
108 #define group_from_gid __nbcompat_group_from_gid
109 #define user_from_uid __nbcompat_user_from_uid
110 #endif
111
112 #ifdef __weak_alias
113 __weak_alias(user_from_uid,_user_from_uid)
114 __weak_alias(group_from_gid,_group_from_gid)
115 __weak_alias(pwcache_userdb,_pwcache_userdb)
116 __weak_alias(pwcache_groupdb,_pwcache_groupdb)
117 #endif
118
119 #if !HAVE_PWCACHE_USERDB || HAVE_CONFIG_H
120 #include "pwcache.h"
121
122 /*
123 * routines that control user, group, uid and gid caches (for the archive
124 * member print routine).
125 * IMPORTANT:
126 * these routines cache BOTH hits and misses, a major performance improvement
127 */
128
129 /*
130 * function pointers to various name lookup routines.
131 * these may be changed as necessary.
132 */
133 static int (*_pwcache_setgroupent)(int) = setgroupent;
134 static void (*_pwcache_endgrent)(void) = endgrent;
135 static struct group * (*_pwcache_getgrnam)(const char *) = getgrnam;
136 static struct group * (*_pwcache_getgrgid)(gid_t) = getgrgid;
137 static int (*_pwcache_setpassent)(int) = setpassent;
138 static void (*_pwcache_endpwent)(void) = endpwent;
139 static struct passwd * (*_pwcache_getpwnam)(const char *) = getpwnam;
140 static struct passwd * (*_pwcache_getpwuid)(uid_t) = getpwuid;
141
142 /*
143 * internal state
144 */
145 static int pwopn; /* is password file open */
146 static int gropn; /* is group file open */
147 static UIDC **uidtb; /* uid to name cache */
148 static GIDC **gidtb; /* gid to name cache */
149 static UIDC **usrtb; /* user name to uid cache */
150 static GIDC **grptb; /* group name to gid cache */
151
152 static int uidtb_fail; /* uidtb_start() failed ? */
153 static int gidtb_fail; /* gidtb_start() failed ? */
154 static int usrtb_fail; /* usrtb_start() failed ? */
155 static int grptb_fail; /* grptb_start() failed ? */
156
157
158 static u_int st_hash(const char *, size_t, int);
159 static int uidtb_start(void);
160 static int gidtb_start(void);
161 static int usrtb_start(void);
162 static int grptb_start(void);
163
164
165 static u_int
166 st_hash(const char *name, size_t len, int tabsz)
167 {
168 u_int key = 0;
169
170 _DIAGASSERT(name != NULL);
171
172 while (len--) {
173 key += *name++;
174 key = (key << 8) | (key >> 24);
175 }
176
177 return (key % tabsz);
178 }
179
180 /*
181 * uidtb_start
182 * creates an an empty uidtb
183 * Return:
184 * 0 if ok, -1 otherwise
185 */
186 static int
187 uidtb_start(void)
188 {
189
190 if (uidtb != NULL)
191 return (0);
192 if (uidtb_fail)
193 return (-1);
194 if ((uidtb = (UIDC **)calloc(UID_SZ, sizeof(UIDC *))) == NULL) {
195 ++uidtb_fail;
196 return (-1);
197 }
198 return (0);
199 }
200
201 /*
202 * gidtb_start
203 * creates an an empty gidtb
204 * Return:
205 * 0 if ok, -1 otherwise
206 */
207 static int
208 gidtb_start(void)
209 {
210
211 if (gidtb != NULL)
212 return (0);
213 if (gidtb_fail)
214 return (-1);
215 if ((gidtb = (GIDC **)calloc(GID_SZ, sizeof(GIDC *))) == NULL) {
216 ++gidtb_fail;
217 return (-1);
218 }
219 return (0);
220 }
221
222 /*
223 * usrtb_start
224 * creates an an empty usrtb
225 * Return:
226 * 0 if ok, -1 otherwise
227 */
228 static int
229 usrtb_start(void)
230 {
231
232 if (usrtb != NULL)
233 return (0);
234 if (usrtb_fail)
235 return (-1);
236 if ((usrtb = (UIDC **)calloc(UNM_SZ, sizeof(UIDC *))) == NULL) {
237 ++usrtb_fail;
238 return (-1);
239 }
240 return (0);
241 }
242
243 /*
244 * grptb_start
245 * creates an an empty grptb
246 * Return:
247 * 0 if ok, -1 otherwise
248 */
249 static int
250 grptb_start(void)
251 {
252
253 if (grptb != NULL)
254 return (0);
255 if (grptb_fail)
256 return (-1);
257 if ((grptb = (GIDC **)calloc(GNM_SZ, sizeof(GIDC *))) == NULL) {
258 ++grptb_fail;
259 return (-1);
260 }
261 return (0);
262 }
263
264 /*
265 * user_from_uid()
266 * caches the name (if any) for the uid. If noname clear, we always
267 * return the the stored name (if valid or invalid match).
268 * We use a simple hash table.
269 * Return
270 * Pointer to stored name (or a empty string)
271 */
272
273 const char *
274 user_from_uid(uid_t uid, int noname)
275 {
276 struct passwd *pw;
277 UIDC *ptr, **pptr;
278
279 if ((uidtb == NULL) && (uidtb_start() < 0))
280 return (NULL);
281
282 /*
283 * see if we have this uid cached
284 */
285 pptr = uidtb + (uid % UID_SZ);
286 ptr = *pptr;
287
288 if ((ptr != NULL) && (ptr->valid > 0) && (ptr->uid == uid)) {
289 /*
290 * have an entry for this uid
291 */
292 if (!noname || (ptr->valid == VALID))
293 return (ptr->name);
294 return (NULL);
295 }
296
297 /*
298 * No entry for this uid, we will add it
299 */
300 if (!pwopn) {
301 if (_pwcache_setpassent != NULL)
302 (*_pwcache_setpassent)(1);
303 ++pwopn;
304 }
305
306 if (ptr == NULL)
307 *pptr = ptr = (UIDC *)malloc(sizeof(UIDC));
308
309 if ((pw = (*_pwcache_getpwuid)(uid)) == NULL) {
310 /*
311 * no match for this uid in the local password file
312 * a string that is the uid in numberic format
313 */
314 if (ptr == NULL)
315 return (NULL);
316 ptr->uid = uid;
317 (void)snprintf(ptr->name, UNMLEN, "%lu", (long) uid);
318 ptr->valid = INVALID;
319 if (noname)
320 return (NULL);
321 } else {
322 /*
323 * there is an entry for this uid in the password file
324 */
325 if (ptr == NULL)
326 return (pw->pw_name);
327 ptr->uid = uid;
328 (void)strlcpy(ptr->name, pw->pw_name, UNMLEN);
329 ptr->valid = VALID;
330 }
331 return (ptr->name);
332 }
333
334 /*
335 * group_from_gid()
336 * caches the name (if any) for the gid. If noname clear, we always
337 * return the the stored name (if valid or invalid match).
338 * We use a simple hash table.
339 * Return
340 * Pointer to stored name (or a empty string)
341 */
342
343 const char *
344 group_from_gid(gid_t gid, int noname)
345 {
346 struct group *gr;
347 GIDC *ptr, **pptr;
348
349 if ((gidtb == NULL) && (gidtb_start() < 0))
350 return (NULL);
351
352 /*
353 * see if we have this gid cached
354 */
355 pptr = gidtb + (gid % GID_SZ);
356 ptr = *pptr;
357
358 if ((ptr != NULL) && (ptr->valid > 0) && (ptr->gid == gid)) {
359 /*
360 * have an entry for this gid
361 */
362 if (!noname || (ptr->valid == VALID))
363 return (ptr->name);
364 return (NULL);
365 }
366
367 /*
368 * No entry for this gid, we will add it
369 */
370 if (!gropn) {
371 if (_pwcache_setgroupent != NULL)
372 (*_pwcache_setgroupent)(1);
373 ++gropn;
374 }
375
376 if (ptr == NULL)
377 *pptr = ptr = (GIDC *)malloc(sizeof(GIDC));
378
379 if ((gr = (*_pwcache_getgrgid)(gid)) == NULL) {
380 /*
381 * no match for this gid in the local group file, put in
382 * a string that is the gid in numberic format
383 */
384 if (ptr == NULL)
385 return (NULL);
386 ptr->gid = gid;
387 (void)snprintf(ptr->name, GNMLEN, "%lu", (long) gid);
388 ptr->valid = INVALID;
389 if (noname)
390 return (NULL);
391 } else {
392 /*
393 * there is an entry for this group in the group file
394 */
395 if (ptr == NULL)
396 return (gr->gr_name);
397 ptr->gid = gid;
398 (void)strlcpy(ptr->name, gr->gr_name, GNMLEN);
399 ptr->valid = VALID;
400 }
401 return (ptr->name);
402 }
403
404 /*
405 * uid_from_user()
406 * caches the uid for a given user name. We use a simple hash table.
407 * Return
408 * the uid (if any) for a user name, or a -1 if no match can be found
409 */
410
411 int
412 uid_from_user(const char *name, uid_t *uid)
413 {
414 struct passwd *pw;
415 UIDC *ptr, **pptr;
416 size_t namelen;
417
418 /*
419 * return -1 for mangled names
420 */
421 if (name == NULL || ((namelen = strlen(name)) == 0))
422 return (-1);
423 if ((usrtb == NULL) && (usrtb_start() < 0))
424 return (-1);
425
426 /*
427 * look up in hash table, if found and valid return the uid,
428 * if found and invalid, return a -1
429 */
430 pptr = usrtb + st_hash(name, namelen, UNM_SZ);
431 ptr = *pptr;
432
433 if ((ptr != NULL) && (ptr->valid > 0) && !strcmp(name, ptr->name)) {
434 if (ptr->valid == INVALID)
435 return (-1);
436 *uid = ptr->uid;
437 return (0);
438 }
439
440 if (!pwopn) {
441 if (_pwcache_setpassent != NULL)
442 (*_pwcache_setpassent)(1);
443 ++pwopn;
444 }
445
446 if (ptr == NULL)
447 *pptr = ptr = (UIDC *)malloc(sizeof(UIDC));
448
449 /*
450 * no match, look it up, if no match store it as an invalid entry,
451 * or store the matching uid
452 */
453 if (ptr == NULL) {
454 if ((pw = (*_pwcache_getpwnam)(name)) == NULL)
455 return (-1);
456 *uid = pw->pw_uid;
457 return (0);
458 }
459 (void)strlcpy(ptr->name, name, UNMLEN);
460 if ((pw = (*_pwcache_getpwnam)(name)) == NULL) {
461 ptr->valid = INVALID;
462 return (-1);
463 }
464 ptr->valid = VALID;
465 *uid = ptr->uid = pw->pw_uid;
466 return (0);
467 }
468
469 /*
470 * gid_from_group()
471 * caches the gid for a given group name. We use a simple hash table.
472 * Return
473 * the gid (if any) for a group name, or a -1 if no match can be found
474 */
475
476 int
477 gid_from_group(const char *name, gid_t *gid)
478 {
479 struct group *gr;
480 GIDC *ptr, **pptr;
481 size_t namelen;
482
483 /*
484 * return -1 for mangled names
485 */
486 if (name == NULL || ((namelen = strlen(name)) == 0))
487 return (-1);
488 if ((grptb == NULL) && (grptb_start() < 0))
489 return (-1);
490
491 /*
492 * look up in hash table, if found and valid return the uid,
493 * if found and invalid, return a -1
494 */
495 pptr = grptb + st_hash(name, namelen, GID_SZ);
496 ptr = *pptr;
497
498 if ((ptr != NULL) && (ptr->valid > 0) && !strcmp(name, ptr->name)) {
499 if (ptr->valid == INVALID)
500 return (-1);
501 *gid = ptr->gid;
502 return (0);
503 }
504
505 if (!gropn) {
506 if (_pwcache_setgroupent != NULL)
507 (*_pwcache_setgroupent)(1);
508 ++gropn;
509 }
510
511 if (ptr == NULL)
512 *pptr = ptr = (GIDC *)malloc(sizeof(GIDC));
513
514 /*
515 * no match, look it up, if no match store it as an invalid entry,
516 * or store the matching gid
517 */
518 if (ptr == NULL) {
519 if ((gr = (*_pwcache_getgrnam)(name)) == NULL)
520 return (-1);
521 *gid = gr->gr_gid;
522 return (0);
523 }
524
525 (void)strlcpy(ptr->name, name, GNMLEN);
526 if ((gr = (*_pwcache_getgrnam)(name)) == NULL) {
527 ptr->valid = INVALID;
528 return (-1);
529 }
530 ptr->valid = VALID;
531 *gid = ptr->gid = gr->gr_gid;
532 return (0);
533 }
534
535 #define FLUSHTB(arr, len, fail) \
536 do { \
537 if (arr != NULL) { \
538 for (i = 0; i < len; i++) \
539 if (arr[i] != NULL) \
540 free(arr[i]); \
541 arr = NULL; \
542 } \
543 fail = 0; \
544 } while (/* CONSTCOND */0);
545
546 int
547 pwcache_userdb(
548 int (*a_setpassent)(int),
549 void (*a_endpwent)(void),
550 struct passwd * (*a_getpwnam)(const char *),
551 struct passwd * (*a_getpwuid)(uid_t))
552 {
553 int i;
554
555 /* a_setpassent and a_endpwent may be NULL */
556 if (a_getpwnam == NULL || a_getpwuid == NULL)
557 return (-1);
558
559 if (_pwcache_endpwent != NULL)
560 (*_pwcache_endpwent)();
561 FLUSHTB(uidtb, UID_SZ, uidtb_fail);
562 FLUSHTB(usrtb, UNM_SZ, usrtb_fail);
563 pwopn = 0;
564 _pwcache_setpassent = a_setpassent;
565 _pwcache_endpwent = a_endpwent;
566 _pwcache_getpwnam = a_getpwnam;
567 _pwcache_getpwuid = a_getpwuid;
568
569 return (0);
570 }
571
572 int
573 pwcache_groupdb(
574 int (*a_setgroupent)(int),
575 void (*a_endgrent)(void),
576 struct group * (*a_getgrnam)(const char *),
577 struct group * (*a_getgrgid)(gid_t))
578 {
579 int i;
580
581 /* a_setgroupent and a_endgrent may be NULL */
582 if (a_getgrnam == NULL || a_getgrgid == NULL)
583 return (-1);
584
585 if (_pwcache_endgrent != NULL)
586 (*_pwcache_endgrent)();
587 FLUSHTB(gidtb, GID_SZ, gidtb_fail);
588 FLUSHTB(grptb, GNM_SZ, grptb_fail);
589 gropn = 0;
590 _pwcache_setgroupent = a_setgroupent;
591 _pwcache_endgrent = a_endgrent;
592 _pwcache_getgrnam = a_getgrnam;
593 _pwcache_getgrgid = a_getgrgid;
594
595 return (0);
596 }
597
598
599 #ifdef TEST_PWCACHE
600
601 struct passwd *
602 test_getpwnam(const char *name)
603 {
604 static struct passwd foo;
605
606 memset(&foo, 0, sizeof(foo));
607 if (strcmp(name, "toor") == 0) {
608 foo.pw_uid = 666;
609 return &foo;
610 }
611 return (getpwnam(name));
612 }
613
614 int
615 main(int argc, char *argv[])
616 {
617 uid_t u;
618 int r, i;
619
620 printf("pass 1 (default userdb)\n");
621 for (i = 1; i < argc; i++) {
622 printf("i: %d, pwopn %d usrtb_fail %d usrtb %p\n",
623 i, pwopn, usrtb_fail, usrtb);
624 r = uid_from_user(argv[i], &u);
625 if (r == -1)
626 printf(" uid_from_user %s: failed\n", argv[i]);
627 else
628 printf(" uid_from_user %s: %d\n", argv[i], u);
629 }
630 printf("pass 1 finish: pwopn %d usrtb_fail %d usrtb %p\n",
631 pwopn, usrtb_fail, usrtb);
632
633 puts("");
634 printf("pass 2 (replacement userdb)\n");
635 printf("pwcache_userdb returned %d\n",
636 pwcache_userdb(setpassent, test_getpwnam, getpwuid));
637 printf("pwopn %d usrtb_fail %d usrtb %p\n", pwopn, usrtb_fail, usrtb);
638
639 for (i = 1; i < argc; i++) {
640 printf("i: %d, pwopn %d usrtb_fail %d usrtb %p\n",
641 i, pwopn, usrtb_fail, usrtb);
642 u = -1;
643 r = uid_from_user(argv[i], &u);
644 if (r == -1)
645 printf(" uid_from_user %s: failed\n", argv[i]);
646 else
647 printf(" uid_from_user %s: %d\n", argv[i], u);
648 }
649 printf("pass 2 finish: pwopn %d usrtb_fail %d usrtb %p\n",
650 pwopn, usrtb_fail, usrtb);
651
652 puts("");
653 printf("pass 3 (null pointers)\n");
654 printf("pwcache_userdb returned %d\n",
655 pwcache_userdb(NULL, NULL, NULL));
656
657 return (0);
658 }
659 #endif /* TEST_PWCACHE */
660 #endif /* !HAVE_PWCACHE_USERDB */
661