1 /* $OpenBSD: cache.c,v 1.6 1997/07/25 18:58:27 mickey Exp $ */
2 /* $NetBSD: cache.c,v 1.4 1995/03/21 09:07:10 cgd 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. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43 static char sccsid[] = "@(#)cache.c 8.1 (Berkeley) 5/31/93";
45 static char rcsid[] = "$OpenBSD: cache.c,v 1.6 1997/07/25 18:58:27 mickey Exp $";
49 #include <sys/types.h>
52 #include <sys/param.h>
64 * routines that control user, group, uid and gid caches (for the archive
65 * member print routine).
67 * these routines cache BOTH hits and misses, a major performance improvement
70 static int pwopn = 0; /* is password file open */
71 static int gropn = 0; /* is group file open */
72 static UIDC **uidtb = NULL; /* uid to name cache */
73 static GIDC **gidtb = NULL; /* gid to name cache */
74 static UIDC **usrtb = NULL; /* user name to uid cache */
75 static GIDC **grptb = NULL; /* group name to gid cache */
79 * creates an an empty uidtb
81 * 0 if ok, -1 otherwise
98 if ((uidtb = (UIDC **)calloc(UID_SZ, sizeof(UIDC *))) == NULL) {
100 paxwarn(1, "Unable to allocate memory for user id cache table");
108 * creates an an empty gidtb
110 * 0 if ok, -1 otherwise
127 if ((gidtb = (GIDC **)calloc(GID_SZ, sizeof(GIDC *))) == NULL) {
129 paxwarn(1, "Unable to allocate memory for group id cache table");
137 * creates an an empty usrtb
139 * 0 if ok, -1 otherwise
156 if ((usrtb = (UIDC **)calloc(UNM_SZ, sizeof(UIDC *))) == NULL) {
158 paxwarn(1, "Unable to allocate memory for user name cache table");
166 * creates an an empty grptb
168 * 0 if ok, -1 otherwise
185 if ((grptb = (GIDC **)calloc(GNM_SZ, sizeof(GIDC *))) == NULL) {
187 paxwarn(1,"Unable to allocate memory for group name cache table");
195 * caches the name (if any) for the uid. If frc set, we always return the
196 * the stored name (if valid or invalid match). We use a simple hash table.
198 * Pointer to stored name (or a empty string)
203 name_uid(uid_t uid, int frc)
211 register struct passwd *pw;
214 if ((uidtb == NULL) && (uidtb_start() < 0))
218 * see if we have this uid cached
220 ptr = uidtb[uid % UID_SZ];
221 if ((ptr != NULL) && (ptr->valid > 0) && (ptr->uid == uid)) {
223 * have an entry for this uid
225 if (frc || (ptr->valid == VALID))
231 * No entry for this uid, we will add it
238 ptr = (UIDC *)malloc(sizeof(UIDC));
240 if ((pw = getpwuid(uid)) == NULL) {
242 * no match for this uid in the local password file
243 * a string that is the uid in numberic format
248 ptr->valid = INVALID;
250 (void)snprintf(ptr->name, sizeof(ptr->name), "%u", uid);
252 (void)snprintf(ptr->name, sizeof(ptr->name), "%lu",
259 * there is an entry for this uid in the password file
264 (void)strncpy(ptr->name, pw->pw_name, UNMLEN-1);
265 ptr->name[UNMLEN-1] = '\0';
273 * caches the name (if any) for the gid. If frc set, we always return the
274 * the stored name (if valid or invalid match). We use a simple hash table.
276 * Pointer to stored name (or a empty string)
281 name_gid(gid_t gid, int frc)
289 register struct group *gr;
292 if ((gidtb == NULL) && (gidtb_start() < 0))
296 * see if we have this gid cached
298 ptr = gidtb[gid % GID_SZ];
299 if ((ptr != NULL) && (ptr->valid > 0) && (ptr->gid == gid)) {
301 * have an entry for this gid
303 if (frc || (ptr->valid == VALID))
309 * No entry for this gid, we will add it
316 ptr = (GIDC *)malloc(sizeof(GIDC));
318 if ((gr = getgrgid(gid)) == NULL) {
320 * no match for this gid in the local group file, put in
321 * a string that is the gid in numberic format
326 ptr->valid = INVALID;
328 (void)snprintf(ptr->name, sizeof(ptr->name), "%u", gid);
330 (void)snprintf(ptr->name, sizeof(ptr->name), "%lu",
337 * there is an entry for this group in the group file
342 (void)strncpy(ptr->name, gr->gr_name, GNMLEN-1);
343 ptr->name[GNMLEN-1] = '\0';
351 * caches the uid for a given user name. We use a simple hash table.
353 * the uid (if any) for a user name, or a -1 if no match can be found
358 uid_name(char *name, uid_t *uid)
366 register struct passwd *pw;
368 register int namelen;
371 * return -1 for mangled names
373 if (((namelen = strlen(name)) == 0) || (name[0] == '\0'))
375 if ((usrtb == NULL) && (usrtb_start() < 0))
379 * look up in hash table, if found and valid return the uid,
380 * if found and invalid, return a -1
382 ptr = usrtb[st_hash(name, namelen, UNM_SZ)];
383 if ((ptr != NULL) && (ptr->valid > 0) && !strcmp(name, ptr->name)) {
384 if (ptr->valid == INVALID)
396 ptr = (UIDC *)malloc(sizeof(UIDC));
399 * no match, look it up, if no match store it as an invalid entry,
400 * or store the matching uid
403 if ((pw = getpwnam(name)) == NULL)
408 (void)strncpy(ptr->name, name, UNMLEN-1);
409 ptr->name[UNMLEN-1] = '\0';
410 if ((pw = getpwnam(name)) == NULL) {
411 ptr->valid = INVALID;
415 *uid = ptr->uid = pw->pw_uid;
421 * caches the gid for a given group name. We use a simple hash table.
423 * the gid (if any) for a group name, or a -1 if no match can be found
428 gid_name(char *name, gid_t *gid)
436 register struct group *gr;
438 register int namelen;
441 * return -1 for mangled names
443 if (((namelen = strlen(name)) == 0) || (name[0] == '\0'))
445 if ((grptb == NULL) && (grptb_start() < 0))
449 * look up in hash table, if found and valid return the uid,
450 * if found and invalid, return a -1
452 ptr = grptb[st_hash(name, namelen, GID_SZ)];
453 if ((ptr != NULL) && (ptr->valid > 0) && !strcmp(name, ptr->name)) {
454 if (ptr->valid == INVALID)
465 ptr = (GIDC *)malloc(sizeof(GIDC));
468 * no match, look it up, if no match store it as an invalid entry,
469 * or store the matching gid
472 if ((gr = getgrnam(name)) == NULL)
478 (void)strncpy(ptr->name, name, GNMLEN-1);
479 ptr->name[GNMLEN-1] = '\0';
480 if ((gr = getgrnam(name)) == NULL) {
481 ptr->valid = INVALID;
485 *gid = ptr->gid = gr->gr_gid;