2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1992 Keith Muller.
5 * Copyright (c) 1992, 1993
6 * The Regents of the University of California. All rights reserved.
8 * This code is derived from software contributed to Berkeley by
9 * Keith Muller of the University of California, San Diego.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 static char sccsid[] = "@(#)cache.c 8.1 (Berkeley) 5/31/93";
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
44 #include <sys/types.h>
56 * routines that control user, group, uid and gid caches (for the archive
57 * member print routine).
59 * these routines cache BOTH hits and misses, a major performance improvement
62 static int pwopn = 0; /* is password file open */
63 static int gropn = 0; /* is group file open */
64 static UIDC **uidtb = NULL; /* uid to name cache */
65 static GIDC **gidtb = NULL; /* gid to name cache */
66 static UIDC **usrtb = NULL; /* user name to uid cache */
67 static GIDC **grptb = NULL; /* group name to gid cache */
71 * creates an an empty uidtb
73 * 0 if ok, -1 otherwise
85 if ((uidtb = (UIDC **)calloc(UID_SZ, sizeof(UIDC *))) == NULL) {
87 paxwarn(1, "Unable to allocate memory for user id cache table");
95 * creates an an empty gidtb
97 * 0 if ok, -1 otherwise
109 if ((gidtb = (GIDC **)calloc(GID_SZ, sizeof(GIDC *))) == NULL) {
111 paxwarn(1, "Unable to allocate memory for group id cache table");
119 * creates an an empty usrtb
121 * 0 if ok, -1 otherwise
133 if ((usrtb = (UIDC **)calloc(UNM_SZ, sizeof(UIDC *))) == NULL) {
135 paxwarn(1, "Unable to allocate memory for user name cache table");
143 * creates an an empty grptb
145 * 0 if ok, -1 otherwise
157 if ((grptb = (GIDC **)calloc(GNM_SZ, sizeof(GIDC *))) == NULL) {
159 paxwarn(1,"Unable to allocate memory for group name cache table");
167 * caches the name (if any) for the uid. If frc set, we always return the
168 * the stored name (if valid or invalid match). We use a simple hash table.
170 * Pointer to stored name (or an empty string).
174 name_uid(uid_t uid, int frc)
179 if ((uidtb == NULL) && (uidtb_start() < 0))
183 * see if we have this uid cached
185 ptr = uidtb[uid % UID_SZ];
186 if ((ptr != NULL) && (ptr->valid > 0) && (ptr->uid == uid)) {
188 * have an entry for this uid
190 if (frc || (ptr->valid == VALID))
196 * No entry for this uid, we will add it
203 ptr = uidtb[uid % UID_SZ] = (UIDC *)malloc(sizeof(UIDC));
205 if ((pw = getpwuid(uid)) == NULL) {
207 * no match for this uid in the local password file
208 * a string that is the uid in numeric format
213 ptr->valid = INVALID;
215 (void)snprintf(ptr->name, sizeof(ptr->name), "%u", uid);
217 (void)snprintf(ptr->name, sizeof(ptr->name), "%lu",
224 * there is an entry for this uid in the password file
229 (void)strncpy(ptr->name, pw->pw_name, UNMLEN - 1);
230 ptr->name[UNMLEN-1] = '\0';
238 * caches the name (if any) for the gid. If frc set, we always return the
239 * the stored name (if valid or invalid match). We use a simple hash table.
241 * Pointer to stored name (or an empty string).
245 name_gid(gid_t gid, int frc)
250 if ((gidtb == NULL) && (gidtb_start() < 0))
254 * see if we have this gid cached
256 ptr = gidtb[gid % GID_SZ];
257 if ((ptr != NULL) && (ptr->valid > 0) && (ptr->gid == gid)) {
259 * have an entry for this gid
261 if (frc || (ptr->valid == VALID))
267 * No entry for this gid, we will add it
274 ptr = gidtb[gid % GID_SZ] = (GIDC *)malloc(sizeof(GIDC));
276 if ((gr = getgrgid(gid)) == NULL) {
278 * no match for this gid in the local group file, put in
279 * a string that is the gid in numeric format
284 ptr->valid = INVALID;
286 (void)snprintf(ptr->name, sizeof(ptr->name), "%u", gid);
288 (void)snprintf(ptr->name, sizeof(ptr->name), "%lu",
295 * there is an entry for this group in the group file
300 (void)strncpy(ptr->name, gr->gr_name, GNMLEN - 1);
301 ptr->name[GNMLEN-1] = '\0';
309 * caches the uid for a given user name. We use a simple hash table.
311 * the uid (if any) for a user name, or a -1 if no match can be found
315 uid_name(char *name, uid_t *uid)
322 * return -1 for mangled names
324 if (((namelen = strlen(name)) == 0) || (name[0] == '\0'))
326 if ((usrtb == NULL) && (usrtb_start() < 0))
330 * look up in hash table, if found and valid return the uid,
331 * if found and invalid, return a -1
333 ptr = usrtb[st_hash(name, namelen, UNM_SZ)];
334 if ((ptr != NULL) && (ptr->valid > 0) && !strcmp(name, ptr->name)) {
335 if (ptr->valid == INVALID)
347 ptr = usrtb[st_hash(name, namelen, UNM_SZ)] =
348 (UIDC *)malloc(sizeof(UIDC));
351 * no match, look it up, if no match store it as an invalid entry,
352 * or store the matching uid
355 if ((pw = getpwnam(name)) == NULL)
360 (void)strncpy(ptr->name, name, UNMLEN - 1);
361 ptr->name[UNMLEN-1] = '\0';
362 if ((pw = getpwnam(name)) == NULL) {
363 ptr->valid = INVALID;
367 *uid = ptr->uid = pw->pw_uid;
373 * caches the gid for a given group name. We use a simple hash table.
375 * the gid (if any) for a group name, or a -1 if no match can be found
379 gid_name(char *name, gid_t *gid)
386 * return -1 for mangled names
388 if (((namelen = strlen(name)) == 0) || (name[0] == '\0'))
390 if ((grptb == NULL) && (grptb_start() < 0))
394 * look up in hash table, if found and valid return the uid,
395 * if found and invalid, return a -1
397 ptr = grptb[st_hash(name, namelen, GID_SZ)];
398 if ((ptr != NULL) && (ptr->valid > 0) && !strcmp(name, ptr->name)) {
399 if (ptr->valid == INVALID)
410 ptr = grptb[st_hash(name, namelen, GID_SZ)] =
411 (GIDC *)malloc(sizeof(GIDC));
414 * no match, look it up, if no match store it as an invalid entry,
415 * or store the matching gid
418 if ((gr = getgrnam(name)) == NULL)
424 (void)strncpy(ptr->name, name, GNMLEN - 1);
425 ptr->name[GNMLEN-1] = '\0';
426 if ((gr = getgrnam(name)) == NULL) {
427 ptr->valid = INVALID;
431 *gid = ptr->gid = gr->gr_gid;