2 * Copyright (c) 1992 Keith Muller.
3 * Copyright (c) 1992, 1993
4 * The Regents of the University of California. All rights reserved.
6 * This code is derived from software contributed to Berkeley by
7 * Keith Muller of the University of California, San Diego.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#)cache.c 8.1 (Berkeley) 5/31/93
34 * $FreeBSD: src/bin/pax/cache.c,v 1.12.2.1 2001/08/01 05:03:11 obrien Exp $
35 * $DragonFly: src/bin/pax/cache.c,v 1.7 2006/09/27 21:58:08 pavalos Exp $
38 #include <sys/types.h>
51 * routines that control user, group, uid and gid caches (for the archive
52 * member print routine).
54 * these routines cache BOTH hits and misses, a major performance improvement
57 static int pwopn = 0; /* is password file open */
58 static int gropn = 0; /* is group file open */
59 static UIDC **uidtb = NULL; /* uid to name cache */
60 static GIDC **gidtb = NULL; /* gid to name cache */
61 static UIDC **usrtb = NULL; /* user name to uid cache */
62 static GIDC **grptb = NULL; /* group name to gid cache */
66 * creates an empty uidtb
68 * 0 if ok, -1 otherwise
80 if ((uidtb = (UIDC **)calloc(UID_SZ, sizeof(UIDC *))) == NULL) {
82 paxwarn(1, "Unable to allocate memory for user id cache table");
90 * creates an empty gidtb
92 * 0 if ok, -1 otherwise
104 if ((gidtb = (GIDC **)calloc(GID_SZ, sizeof(GIDC *))) == NULL) {
106 paxwarn(1, "Unable to allocate memory for group id cache table");
114 * creates an empty usrtb
116 * 0 if ok, -1 otherwise
128 if ((usrtb = (UIDC **)calloc(UNM_SZ, sizeof(UIDC *))) == NULL) {
130 paxwarn(1, "Unable to allocate memory for user name cache table");
138 * creates an empty grptb
140 * 0 if ok, -1 otherwise
152 if ((grptb = (GIDC **)calloc(GNM_SZ, sizeof(GIDC *))) == NULL) {
154 paxwarn(1,"Unable to allocate memory for group name cache table");
162 * caches the name (if any) for the uid. If frc set, we always return the
163 * the stored name (if valid or invalid match). We use a simple hash table.
165 * Pointer to stored name (or a empty string)
169 name_uid(uid_t uid, int frc)
174 if ((uidtb == NULL) && (uidtb_start() < 0))
178 * see if we have this uid cached
180 ptr = uidtb[uid % UID_SZ];
181 if ((ptr != NULL) && (ptr->valid > 0) && (ptr->uid == uid)) {
183 * have an entry for this uid
185 if (frc || (ptr->valid == VALID))
191 * No entry for this uid, we will add it
198 ptr = (UIDC *)malloc(sizeof(UIDC));
200 if ((pw = getpwuid(uid)) == NULL) {
202 * no match for this uid in the local password file
203 * a string that is the uid in numeric format
208 ptr->valid = INVALID;
209 snprintf(ptr->name, sizeof(ptr->name), "%lu",
215 * there is an entry for this uid in the password file
220 strncpy(ptr->name, pw->pw_name, UNMLEN - 1);
221 ptr->name[UNMLEN-1] = '\0';
229 * caches the name (if any) for the gid. If frc set, we always return the
230 * the stored name (if valid or invalid match). We use a simple hash table.
232 * Pointer to stored name (or a empty string)
236 name_gid(gid_t gid, int frc)
241 if ((gidtb == NULL) && (gidtb_start() < 0))
245 * see if we have this gid cached
247 ptr = gidtb[gid % GID_SZ];
248 if ((ptr != NULL) && (ptr->valid > 0) && (ptr->gid == gid)) {
250 * have an entry for this gid
252 if (frc || (ptr->valid == VALID))
258 * No entry for this gid, we will add it
265 ptr = (GIDC *)malloc(sizeof(GIDC));
267 if ((gr = getgrgid(gid)) == NULL) {
269 * no match for this gid in the local group file, put in
270 * a string that is the gid in numeric format
275 ptr->valid = INVALID;
276 snprintf(ptr->name, sizeof(ptr->name), "%lu",
282 * there is an entry for this group in the group file
287 strncpy(ptr->name, gr->gr_name, GNMLEN - 1);
288 ptr->name[GNMLEN-1] = '\0';
296 * caches the uid for a given user name. We use a simple hash table.
298 * the uid (if any) for a user name, or a -1 if no match can be found
302 uid_name(char *name, uid_t *uid)
309 * return -1 for mangled names
311 if (((namelen = strlen(name)) == 0) || (name[0] == '\0'))
313 if ((usrtb == NULL) && (usrtb_start() < 0))
317 * look up in hash table, if found and valid return the uid,
318 * if found and invalid, return a -1
320 ptr = usrtb[st_hash(name, namelen, UNM_SZ)];
321 if ((ptr != NULL) && (ptr->valid > 0) && !strcmp(name, ptr->name)) {
322 if (ptr->valid == INVALID)
334 ptr = usrtb[st_hash(name, namelen, UNM_SZ)] =
335 (UIDC *)malloc(sizeof(UIDC));
338 * no match, look it up, if no match store it as an invalid entry,
339 * or store the matching uid
342 if ((pw = getpwnam(name)) == NULL)
347 strncpy(ptr->name, name, UNMLEN - 1);
348 ptr->name[UNMLEN-1] = '\0';
349 if ((pw = getpwnam(name)) == NULL) {
350 ptr->valid = INVALID;
354 *uid = ptr->uid = pw->pw_uid;
360 * caches the gid for a given group name. We use a simple hash table.
362 * the gid (if any) for a group name, or a -1 if no match can be found
366 gid_name(char *name, gid_t *gid)
373 * return -1 for mangled names
375 if (((namelen = strlen(name)) == 0) || (name[0] == '\0'))
377 if ((grptb == NULL) && (grptb_start() < 0))
381 * look up in hash table, if found and valid return the uid,
382 * if found and invalid, return a -1
384 ptr = grptb[st_hash(name, namelen, GID_SZ)];
385 if ((ptr != NULL) && (ptr->valid > 0) && !strcmp(name, ptr->name)) {
386 if (ptr->valid == INVALID)
397 ptr = grptb[st_hash(name, namelen, GID_SZ)] =
398 (GIDC *)malloc(sizeof(GIDC));
401 * no match, look it up, if no match store it as an invalid entry,
402 * or store the matching gid
405 if ((gr = getgrnam(name)) == NULL)
411 strncpy(ptr->name, name, GNMLEN - 1);
412 ptr->name[GNMLEN-1] = '\0';
413 if ((gr = getgrnam(name)) == NULL) {
414 ptr->valid = INVALID;
418 *gid = ptr->gid = gr->gr_gid;