pwcache.c revision 1.17 1 /* $NetBSD: pwcache.c,v 1.17 2002/01/24 02:46:35 lukem 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 #include <sys/cdefs.h>
74 #if defined(LIBC_SCCS) && !defined(lint)
75 #if 0
76 static char sccsid[] = "@(#)cache.c 8.1 (Berkeley) 5/31/93";
77 #else
78 __RCSID("$NetBSD: pwcache.c,v 1.17 2002/01/24 02:46:35 lukem Exp $");
79 #endif
80 #endif /* LIBC_SCCS and not lint */
81
82 #include "namespace.h"
83
84 #include <sys/types.h>
85 #include <sys/param.h>
86
87 #include <assert.h>
88 #include <grp.h>
89 #include <pwd.h>
90 #include <stdio.h>
91 #include <stdlib.h>
92 #include <string.h>
93 #include <unistd.h>
94
95 #include "pwcache.h"
96
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_userdb,_pwcache_userdb)
101 __weak_alias(pwcache_groupdb,_pwcache_groupdb)
102 #endif
103
104 /*
105 * routines that control user, group, uid and gid caches (for the archive
106 * member print routine).
107 * IMPORTANT:
108 * these routines cache BOTH hits and misses, a major performance improvement
109 */
110
111 /*
112 * function pointers to various name lookup routines.
113 * these may be changed as necessary.
114 */
115 static int (*_pwcache_setgroupent)(int) = setgroupent;
116 static void (*_pwcache_endgrent)(void) = endgrent;
117 static struct group * (*_pwcache_getgrnam)(const char *) = getgrnam;
118 static struct group * (*_pwcache_getgrgid)(gid_t) = getgrgid;
119 static int (*_pwcache_setpassent)(int) = setpassent;
120 static void (*_pwcache_endpwent)(void) = endpwent;
121 static struct passwd * (*_pwcache_getpwnam)(const char *) = getpwnam;
122 static struct passwd * (*_pwcache_getpwuid)(uid_t) = getpwuid;
123
124 /*
125 * internal state
126 */
127 static int pwopn; /* is password file open */
128 static int gropn; /* is group file open */
129 static UIDC **uidtb; /* uid to name cache */
130 static GIDC **gidtb; /* gid to name cache */
131 static UIDC **usrtb; /* user name to uid cache */
132 static GIDC **grptb; /* group name to gid cache */
133
134 static int uidtb_fail; /* uidtb_start() failed ? */
135 static int gidtb_fail; /* gidtb_start() failed ? */
136 static int usrtb_fail; /* usrtb_start() failed ? */
137 static int grptb_fail; /* grptb_start() failed ? */
138
139
140 static u_int st_hash(const char *, size_t, int);
141 static int uidtb_start(void);
142 static int gidtb_start(void);
143 static int usrtb_start(void);
144 static int grptb_start(void);
145
146
147 static u_int
148 st_hash(const char *name, size_t len, int tabsz)
149 {
150 u_int key = 0;
151
152 _DIAGASSERT(name != NULL);
153
154 while (len--) {
155 key += *name++;
156 key = (key << 8) | (key >> 24);
157 }
158
159 return (key % tabsz);
160 }
161
162 /*
163 * uidtb_start
164 * creates an an empty uidtb
165 * Return:
166 * 0 if ok, -1 otherwise
167 */
168 static int
169 uidtb_start(void)
170 {
171
172 if (uidtb != NULL)
173 return (0);
174 if (uidtb_fail)
175 return (-1);
176 if ((uidtb = (UIDC **)calloc(UID_SZ, sizeof(UIDC *))) == NULL) {
177 ++uidtb_fail;
178 return (-1);
179 }
180 return (0);
181 }
182
183 /*
184 * gidtb_start
185 * creates an an empty gidtb
186 * Return:
187 * 0 if ok, -1 otherwise
188 */
189 static int
190 gidtb_start(void)
191 {
192
193 if (gidtb != NULL)
194 return (0);
195 if (gidtb_fail)
196 return (-1);
197 if ((gidtb = (GIDC **)calloc(GID_SZ, sizeof(GIDC *))) == NULL) {
198 ++gidtb_fail;
199 return (-1);
200 }
201 return (0);
202 }
203
204 /*
205 * usrtb_start
206 * creates an an empty usrtb
207 * Return:
208 * 0 if ok, -1 otherwise
209 */
210 static int
211 usrtb_start(void)
212 {
213
214 if (usrtb != NULL)
215 return (0);
216 if (usrtb_fail)
217 return (-1);
218 if ((usrtb = (UIDC **)calloc(UNM_SZ, sizeof(UIDC *))) == NULL) {
219 ++usrtb_fail;
220 return (-1);
221 }
222 return (0);
223 }
224
225 /*
226 * grptb_start
227 * creates an an empty grptb
228 * Return:
229 * 0 if ok, -1 otherwise
230 */
231 static int
232 grptb_start(void)
233 {
234
235 if (grptb != NULL)
236 return (0);
237 if (grptb_fail)
238 return (-1);
239 if ((grptb = (GIDC **)calloc(GNM_SZ, sizeof(GIDC *))) == NULL) {
240 ++grptb_fail;
241 return (-1);
242 }
243 return (0);
244 }
245
246 /*
247 * user_from_uid()
248 * caches the name (if any) for the uid. If noname clear, we always
249 * return the the stored name (if valid or invalid match).
250 * We use a simple hash table.
251 * Return
252 * Pointer to stored name (or a empty string)
253 */
254
255 const char *
256 user_from_uid(uid_t uid, int noname)
257 {
258 struct passwd *pw;
259 UIDC *ptr, **pptr;
260
261 if ((uidtb == NULL) && (uidtb_start() < 0))
262 return (NULL);
263
264 /*
265 * see if we have this uid cached
266 */
267 pptr = uidtb + (uid % UID_SZ);
268 ptr = *pptr;
269
270 if ((ptr != NULL) && (ptr->valid > 0) && (ptr->uid == uid)) {
271 /*
272 * have an entry for this uid
273 */
274 if (!noname || (ptr->valid == VALID))
275 return (ptr->name);
276 return (NULL);
277 }
278
279 /*
280 * No entry for this uid, we will add it
281 */
282 if (!pwopn) {
283 if (_pwcache_setpassent != NULL)
284 (*_pwcache_setpassent)(1);
285 ++pwopn;
286 }
287
288 if (ptr == NULL)
289 *pptr = ptr = (UIDC *)malloc(sizeof(UIDC));
290
291 if ((pw = (*_pwcache_getpwuid)(uid)) == NULL) {
292 /*
293 * no match for this uid in the local password file
294 * a string that is the uid in numberic format
295 */
296 if (ptr == NULL)
297 return (NULL);
298 ptr->uid = uid;
299 (void)snprintf(ptr->name, UNMLEN, "%lu", (long) uid);
300 ptr->valid = INVALID;
301 if (noname)
302 return (NULL);
303 } else {
304 /*
305 * there is an entry for this uid in the password file
306 */
307 if (ptr == NULL)
308 return (pw->pw_name);
309 ptr->uid = uid;
310 (void)strlcpy(ptr->name, pw->pw_name, UNMLEN);
311 ptr->valid = VALID;
312 }
313 return (ptr->name);
314 }
315
316 /*
317 * group_from_gid()
318 * caches the name (if any) for the gid. If noname clear, we always
319 * return the the stored name (if valid or invalid match).
320 * We use a simple hash table.
321 * Return
322 * Pointer to stored name (or a empty string)
323 */
324
325 const char *
326 group_from_gid(gid_t gid, int noname)
327 {
328 struct group *gr;
329 GIDC *ptr, **pptr;
330
331 if ((gidtb == NULL) && (gidtb_start() < 0))
332 return (NULL);
333
334 /*
335 * see if we have this gid cached
336 */
337 pptr = gidtb + (gid % GID_SZ);
338 ptr = *pptr;
339
340 if ((ptr != NULL) && (ptr->valid > 0) && (ptr->gid == gid)) {
341 /*
342 * have an entry for this gid
343 */
344 if (!noname || (ptr->valid == VALID))
345 return (ptr->name);
346 return (NULL);
347 }
348
349 /*
350 * No entry for this gid, we will add it
351 */
352 if (!gropn) {
353 if (_pwcache_setgroupent != NULL)
354 (*_pwcache_setgroupent)(1);
355 ++gropn;
356 }
357
358 if (ptr == NULL)
359 *pptr = ptr = (GIDC *)malloc(sizeof(GIDC));
360
361 if ((gr = (*_pwcache_getgrgid)(gid)) == NULL) {
362 /*
363 * no match for this gid in the local group file, put in
364 * a string that is the gid in numberic format
365 */
366 if (ptr == NULL)
367 return (NULL);
368 ptr->gid = gid;
369 (void)snprintf(ptr->name, GNMLEN, "%lu", (long) gid);
370 ptr->valid = INVALID;
371 if (noname)
372 return (NULL);
373 } else {
374 /*
375 * there is an entry for this group in the group file
376 */
377 if (ptr == NULL)
378 return (gr->gr_name);
379 ptr->gid = gid;
380 (void)strlcpy(ptr->name, gr->gr_name, GNMLEN);
381 ptr->valid = VALID;
382 }
383 return (ptr->name);
384 }
385
386 /*
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
391 */
392
393 int
394 uid_from_user(const char *name, uid_t *uid)
395 {
396 struct passwd *pw;
397 UIDC *ptr, **pptr;
398 size_t namelen;
399
400 /*
401 * return -1 for mangled names
402 */
403 if (name == NULL || ((namelen = strlen(name)) == 0))
404 return (-1);
405 if ((usrtb == NULL) && (usrtb_start() < 0))
406 return (-1);
407
408 /*
409 * look up in hash table, if found and valid return the uid,
410 * if found and invalid, return a -1
411 */
412 pptr = usrtb + st_hash(name, namelen, UNM_SZ);
413 ptr = *pptr;
414
415 if ((ptr != NULL) && (ptr->valid > 0) && !strcmp(name, ptr->name)) {
416 if (ptr->valid == INVALID)
417 return (-1);
418 *uid = ptr->uid;
419 return (0);
420 }
421
422 if (!pwopn) {
423 if (_pwcache_setpassent != NULL)
424 (*_pwcache_setpassent)(1);
425 ++pwopn;
426 }
427
428 if (ptr == NULL)
429 *pptr = ptr = (UIDC *)malloc(sizeof(UIDC));
430
431 /*
432 * no match, look it up, if no match store it as an invalid entry,
433 * or store the matching uid
434 */
435 if (ptr == NULL) {
436 if ((pw = (*_pwcache_getpwnam)(name)) == NULL)
437 return (-1);
438 *uid = pw->pw_uid;
439 return (0);
440 }
441 (void)strlcpy(ptr->name, name, UNMLEN);
442 if ((pw = (*_pwcache_getpwnam)(name)) == NULL) {
443 ptr->valid = INVALID;
444 return (-1);
445 }
446 ptr->valid = VALID;
447 *uid = ptr->uid = pw->pw_uid;
448 return (0);
449 }
450
451 /*
452 * gid_from_group()
453 * caches the gid for a given group name. We use a simple hash table.
454 * Return
455 * the gid (if any) for a group name, or a -1 if no match can be found
456 */
457
458 int
459 gid_from_group(const char *name, gid_t *gid)
460 {
461 struct group *gr;
462 GIDC *ptr, **pptr;
463 size_t namelen;
464
465 /*
466 * return -1 for mangled names
467 */
468 if (name == NULL || ((namelen = strlen(name)) == 0))
469 return (-1);
470 if ((grptb == NULL) && (grptb_start() < 0))
471 return (-1);
472
473 /*
474 * look up in hash table, if found and valid return the uid,
475 * if found and invalid, return a -1
476 */
477 pptr = grptb + st_hash(name, namelen, GID_SZ);
478 ptr = *pptr;
479
480 if ((ptr != NULL) && (ptr->valid > 0) && !strcmp(name, ptr->name)) {
481 if (ptr->valid == INVALID)
482 return (-1);
483 *gid = ptr->gid;
484 return (0);
485 }
486
487 if (!gropn) {
488 if (_pwcache_setgroupent != NULL)
489 (*_pwcache_setgroupent)(1);
490 ++gropn;
491 }
492
493 if (ptr == NULL)
494 *pptr = ptr = (GIDC *)malloc(sizeof(GIDC));
495
496 /*
497 * no match, look it up, if no match store it as an invalid entry,
498 * or store the matching gid
499 */
500 if (ptr == NULL) {
501 if ((gr = (*_pwcache_getgrnam)(name)) == NULL)
502 return (-1);
503 *gid = gr->gr_gid;
504 return (0);
505 }
506
507 (void)strlcpy(ptr->name, name, GNMLEN);
508 if ((gr = (*_pwcache_getgrnam)(name)) == NULL) {
509 ptr->valid = INVALID;
510 return (-1);
511 }
512 ptr->valid = VALID;
513 *gid = ptr->gid = gr->gr_gid;
514 return (0);
515 }
516
517 #define FLUSHTB(arr, len, fail) \
518 do { \
519 if (arr != NULL) { \
520 for (i = 0; i < len; i++) \
521 if (arr[i] != NULL) \
522 free(arr[i]); \
523 arr = NULL; \
524 } \
525 fail = 0; \
526 } while (/* CONSTCOND */0);
527
528 int
529 pwcache_userdb(
530 int (*a_setpassent)(int),
531 void (*a_endpwent)(void),
532 struct passwd * (*a_getpwnam)(const char *),
533 struct passwd * (*a_getpwuid)(uid_t))
534 {
535 int i;
536
537 /* a_setpassent and a_endpwent may be NULL */
538 if (a_getpwnam == NULL || a_getpwuid == NULL)
539 return (-1);
540
541 if (_pwcache_endpwent != NULL)
542 (*_pwcache_endpwent)();
543 FLUSHTB(uidtb, UID_SZ, uidtb_fail);
544 FLUSHTB(usrtb, UNM_SZ, usrtb_fail);
545 pwopn = 0;
546 _pwcache_setpassent = a_setpassent;
547 _pwcache_endpwent = a_endpwent;
548 _pwcache_getpwnam = a_getpwnam;
549 _pwcache_getpwuid = a_getpwuid;
550
551 return (0);
552 }
553
554 int
555 pwcache_groupdb(
556 int (*a_setgroupent)(int),
557 void (*a_endgrent)(void),
558 struct group * (*a_getgrnam)(const char *),
559 struct group * (*a_getgrgid)(gid_t))
560 {
561 int i;
562
563 /* a_setgroupent and a_endgrent may be NULL */
564 if (a_getgrnam == NULL || a_getgrgid == NULL)
565 return (-1);
566
567 if (_pwcache_endgrent != NULL)
568 (*_pwcache_endgrent)();
569 FLUSHTB(gidtb, GID_SZ, gidtb_fail);
570 FLUSHTB(grptb, GNM_SZ, grptb_fail);
571 gropn = 0;
572 _pwcache_setgroupent = a_setgroupent;
573 _pwcache_endgrent = a_endgrent;
574 _pwcache_getgrnam = a_getgrnam;
575 _pwcache_getgrgid = a_getgrgid;
576
577 return (0);
578 }
579
580
581 #ifdef TEST_PWCACHE
582
583 struct passwd *
584 test_getpwnam(const char *name)
585 {
586 static struct passwd foo;
587
588 memset(&foo, 0, sizeof(foo));
589 if (strcmp(name, "toor") == 0) {
590 foo.pw_uid = 666;
591 return &foo;
592 }
593 return (getpwnam(name));
594 }
595
596 int
597 main(int argc, char *argv[])
598 {
599 uid_t u;
600 int r, i;
601
602 printf("pass 1 (default userdb)\n");
603 for (i = 1; i < argc; i++) {
604 printf("i: %d, pwopn %d usrtb_fail %d usrtb %p\n",
605 i, pwopn, usrtb_fail, usrtb);
606 r = uid_from_user(argv[i], &u);
607 if (r == -1)
608 printf(" uid_from_user %s: failed\n", argv[i]);
609 else
610 printf(" uid_from_user %s: %d\n", argv[i], u);
611 }
612 printf("pass 1 finish: pwopn %d usrtb_fail %d usrtb %p\n",
613 pwopn, usrtb_fail, usrtb);
614
615 puts("");
616 printf("pass 2 (replacement userdb)\n");
617 printf("pwcache_userdb returned %d\n",
618 pwcache_userdb(setpassent, test_getpwnam, getpwuid));
619 printf("pwopn %d usrtb_fail %d usrtb %p\n", pwopn, usrtb_fail, usrtb);
620
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 u = -1;
625 r = uid_from_user(argv[i], &u);
626 if (r == -1)
627 printf(" uid_from_user %s: failed\n", argv[i]);
628 else
629 printf(" uid_from_user %s: %d\n", argv[i], u);
630 }
631 printf("pass 2 finish: pwopn %d usrtb_fail %d usrtb %p\n",
632 pwopn, usrtb_fail, usrtb);
633
634 puts("");
635 printf("pass 3 (null pointers)\n");
636 printf("pwcache_userdb returned %d\n",
637 pwcache_userdb(NULL, NULL, NULL));
638
639 return (0);
640 }
641 #endif /* TEST_PWCACHE */
642