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. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the University of
20 * California, Berkeley and its contributors.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * @(#)cache.c 8.1 (Berkeley) 5/31/93
38 * $FreeBSD: src/bin/pax/cache.c,v 1.12.2.1 2001/08/01 05:03:11 obrien Exp $
39 * $DragonFly: src/bin/pax/cache.c,v 1.3 2003/09/28 14:39:14 hmp Exp $
42 #include <sys/types.h>
55 * routines that control user, group, uid and gid caches (for the archive
56 * member print routine).
58 * these routines cache BOTH hits and misses, a major performance improvement
61 static int pwopn = 0; /* is password file open */
62 static int gropn = 0; /* is group file open */
63 static UIDC **uidtb = NULL; /* uid to name cache */
64 static GIDC **gidtb = NULL; /* gid to name cache */
65 static UIDC **usrtb = NULL; /* user name to uid cache */
66 static GIDC **grptb = NULL; /* group name to gid cache */
70 * creates an an empty uidtb
72 * 0 if ok, -1 otherwise
84 if ((uidtb = (UIDC **)calloc(UID_SZ, sizeof(UIDC *))) == NULL) {
86 paxwarn(1, "Unable to allocate memory for user id cache table");
94 * creates an an empty gidtb
96 * 0 if ok, -1 otherwise
108 if ((gidtb = (GIDC **)calloc(GID_SZ, sizeof(GIDC *))) == NULL) {
110 paxwarn(1, "Unable to allocate memory for group id cache table");
118 * creates an an empty usrtb
120 * 0 if ok, -1 otherwise
132 if ((usrtb = (UIDC **)calloc(UNM_SZ, sizeof(UIDC *))) == NULL) {
134 paxwarn(1, "Unable to allocate memory for user name cache table");
142 * creates an an empty grptb
144 * 0 if ok, -1 otherwise
156 if ((grptb = (GIDC **)calloc(GNM_SZ, sizeof(GIDC *))) == NULL) {
158 paxwarn(1,"Unable to allocate memory for group name cache table");
166 * caches the name (if any) for the uid. If frc set, we always return the
167 * the stored name (if valid or invalid match). We use a simple hash table.
169 * Pointer to stored name (or a empty string)
173 name_uid(uid_t uid, int frc)
175 register struct passwd *pw;
178 if ((uidtb == NULL) && (uidtb_start() < 0))
182 * see if we have this uid cached
184 ptr = uidtb[uid % UID_SZ];
185 if ((ptr != NULL) && (ptr->valid > 0) && (ptr->uid == uid)) {
187 * have an entry for this uid
189 if (frc || (ptr->valid == VALID))
195 * No entry for this uid, we will add it
202 ptr = (UIDC *)malloc(sizeof(UIDC));
204 if ((pw = getpwuid(uid)) == NULL) {
206 * no match for this uid in the local password file
207 * a string that is the uid in numeric format
212 ptr->valid = INVALID;
214 (void)snprintf(ptr->name, sizeof(ptr->name), "%u", uid);
216 (void)snprintf(ptr->name, sizeof(ptr->name), "%lu",
223 * there is an entry for this uid in the password file
228 (void)strncpy(ptr->name, pw->pw_name, UNMLEN - 1);
229 ptr->name[UNMLEN-1] = '\0';
237 * caches the name (if any) for the gid. If frc set, we always return the
238 * the stored name (if valid or invalid match). We use a simple hash table.
240 * Pointer to stored name (or a empty string)
244 name_gid(gid_t gid, int frc)
246 register struct group *gr;
249 if ((gidtb == NULL) && (gidtb_start() < 0))
253 * see if we have this gid cached
255 ptr = gidtb[gid % GID_SZ];
256 if ((ptr != NULL) && (ptr->valid > 0) && (ptr->gid == gid)) {
258 * have an entry for this gid
260 if (frc || (ptr->valid == VALID))
266 * No entry for this gid, we will add it
273 ptr = (GIDC *)malloc(sizeof(GIDC));
275 if ((gr = getgrgid(gid)) == NULL) {
277 * no match for this gid in the local group file, put in
278 * a string that is the gid in numeric format
283 ptr->valid = INVALID;
285 (void)snprintf(ptr->name, sizeof(ptr->name), "%u", gid);
287 (void)snprintf(ptr->name, sizeof(ptr->name), "%lu",
294 * there is an entry for this group in the group file
299 (void)strncpy(ptr->name, gr->gr_name, GNMLEN - 1);
300 ptr->name[GNMLEN-1] = '\0';
308 * caches the uid for a given user name. We use a simple hash table.
310 * the uid (if any) for a user name, or a -1 if no match can be found
314 uid_name(char *name, uid_t *uid)
316 register struct passwd *pw;
318 register int namelen;
321 * return -1 for mangled names
323 if (((namelen = strlen(name)) == 0) || (name[0] == '\0'))
325 if ((usrtb == NULL) && (usrtb_start() < 0))
329 * look up in hash table, if found and valid return the uid,
330 * if found and invalid, return a -1
332 ptr = usrtb[st_hash(name, namelen, UNM_SZ)];
333 if ((ptr != NULL) && (ptr->valid > 0) && !strcmp(name, ptr->name)) {
334 if (ptr->valid == INVALID)
346 ptr = usrtb[st_hash(name, namelen, UNM_SZ)] =
347 (UIDC *)malloc(sizeof(UIDC));
350 * no match, look it up, if no match store it as an invalid entry,
351 * or store the matching uid
354 if ((pw = getpwnam(name)) == NULL)
359 (void)strncpy(ptr->name, name, UNMLEN - 1);
360 ptr->name[UNMLEN-1] = '\0';
361 if ((pw = getpwnam(name)) == NULL) {
362 ptr->valid = INVALID;
366 *uid = ptr->uid = pw->pw_uid;
372 * caches the gid for a given group name. We use a simple hash table.
374 * the gid (if any) for a group name, or a -1 if no match can be found
378 gid_name(char *name, gid_t *gid)
380 register struct group *gr;
382 register int namelen;
385 * return -1 for mangled names
387 if (((namelen = strlen(name)) == 0) || (name[0] == '\0'))
389 if ((grptb == NULL) && (grptb_start() < 0))
393 * look up in hash table, if found and valid return the uid,
394 * if found and invalid, return a -1
396 ptr = grptb[st_hash(name, namelen, GID_SZ)];
397 if ((ptr != NULL) && (ptr->valid > 0) && !strcmp(name, ptr->name)) {
398 if (ptr->valid == INVALID)
409 ptr = grptb[st_hash(name, namelen, GID_SZ)] =
410 (GIDC *)malloc(sizeof(GIDC));
413 * no match, look it up, if no match store it as an invalid entry,
414 * or store the matching gid
417 if ((gr = getgrnam(name)) == NULL)
423 (void)strncpy(ptr->name, name, GNMLEN - 1);
424 ptr->name[GNMLEN-1] = '\0';
425 if ((gr = getgrnam(name)) == NULL) {
426 ptr->valid = INVALID;
430 *gid = ptr->gid = gr->gr_gid;