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