]>
git.saurik.com Git - apple/file_cmds.git/blob - pax/cache.c
1 /* $OpenBSD: cache.c,v 1.17 2004/03/16 03:28:34 tedu 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. 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.
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
37 #include <sys/cdefs.h>
40 static const char sccsid
[] = "@(#)cache.c 8.1 (Berkeley) 5/31/93";
42 __used
static const char rcsid
[] = "$OpenBSD: cache.c,v 1.17 2004/03/16 03:28:34 tedu Exp $";
46 #include <sys/types.h>
49 #include <sys/param.h>
61 * routines that control user, group, uid and gid caches (for the archive
62 * member print routine).
64 * these routines cache BOTH hits and misses, a major performance improvement
67 static int pwopn
= 0; /* is password file open */
68 static int gropn
= 0; /* is group file open */
69 static UIDC
**uidtb
= NULL
; /* uid to name cache */
70 static GIDC
**gidtb
= NULL
; /* gid to name cache */
71 static UIDC
**usrtb
= NULL
; /* user name to uid cache */
72 static GIDC
**grptb
= NULL
; /* group name to gid cache */
76 * creates an empty uidtb
78 * 0 if ok, -1 otherwise
90 if ((uidtb
= (UIDC
**)calloc(UID_SZ
, sizeof(UIDC
*))) == NULL
) {
92 paxwarn(1, "Unable to allocate memory for user id cache table");
100 * creates an empty gidtb
102 * 0 if ok, -1 otherwise
114 if ((gidtb
= (GIDC
**)calloc(GID_SZ
, sizeof(GIDC
*))) == NULL
) {
116 paxwarn(1, "Unable to allocate memory for group id cache table");
124 * creates an empty usrtb
126 * 0 if ok, -1 otherwise
138 if ((usrtb
= (UIDC
**)calloc(UNM_SZ
, sizeof(UIDC
*))) == NULL
) {
140 paxwarn(1, "Unable to allocate memory for user name cache table");
148 * creates an empty grptb
150 * 0 if ok, -1 otherwise
162 if ((grptb
= (GIDC
**)calloc(GNM_SZ
, sizeof(GIDC
*))) == NULL
) {
164 paxwarn(1,"Unable to allocate memory for group name cache table");
172 * caches the name (if any) for the uid. If frc set, we always return the
173 * the stored name (if valid or invalid match). We use a simple hash table.
175 * Pointer to stored name (or a empty string)
179 name_uid(uid_t uid
, int frc
)
184 if ((uidtb
== NULL
) && (uidtb_start() < 0))
188 * see if we have this uid cached
190 ptr
= uidtb
[uid
% UID_SZ
];
191 if ((ptr
!= NULL
) && (ptr
->valid
> 0) && (ptr
->uid
== uid
)) {
193 * have an entry for this uid
195 if (frc
|| (ptr
->valid
== VALID
))
201 * No entry for this uid, we will add it
208 ptr
= uidtb
[uid
% UID_SZ
] = malloc(sizeof(UIDC
));
210 if ((pw
= getpwuid(uid
)) == NULL
) {
212 * no match for this uid in the local password file
213 * a string that is the uid in numeric format
218 ptr
->valid
= INVALID
;
219 (void)snprintf(ptr
->name
, sizeof(ptr
->name
), "%lu",
225 * there is an entry for this uid in the password file
230 (void)strlcpy(ptr
->name
, pw
->pw_name
, sizeof(ptr
->name
));
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 a 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
] = 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 numberic format
284 ptr
->valid
= INVALID
;
285 (void)snprintf(ptr
->name
, sizeof(ptr
->name
), "%lu",
291 * there is an entry for this group in the group file
296 (void)strlcpy(ptr
->name
, gr
->gr_name
, sizeof(ptr
->name
));
304 * caches the uid for a given user name. We use a simple hash table.
306 * the uid (if any) for a user name, or a -1 if no match can be found
310 uid_name(char *name
, uid_t
*uid
)
317 * return -1 for mangled names
319 if (((namelen
= strlen(name
)) == 0) || (name
[0] == '\0'))
321 if ((usrtb
== NULL
) && (usrtb_start() < 0))
325 * look up in hash table, if found and valid return the uid,
326 * if found and invalid, return a -1
328 ptr
= usrtb
[st_hash(name
, namelen
, UNM_SZ
)];
329 if ((ptr
!= NULL
) && (ptr
->valid
> 0) && !strcmp(name
, ptr
->name
)) {
330 if (ptr
->valid
== INVALID
)
342 ptr
= usrtb
[st_hash(name
, namelen
, UNM_SZ
)] =
343 (UIDC
*)malloc(sizeof(UIDC
));
346 * no match, look it up, if no match store it as an invalid entry,
347 * or store the matching uid
350 if ((pw
= getpwnam(name
)) == NULL
)
355 (void)strlcpy(ptr
->name
, name
, sizeof(ptr
->name
));
356 if ((pw
= getpwnam(name
)) == NULL
) {
357 ptr
->valid
= INVALID
;
361 *uid
= ptr
->uid
= pw
->pw_uid
;
367 * caches the gid for a given group name. We use a simple hash table.
369 * the gid (if any) for a group name, or a -1 if no match can be found
373 gid_name(char *name
, gid_t
*gid
)
380 * return -1 for mangled names
382 if (((namelen
= strlen(name
)) == 0) || (name
[0] == '\0'))
384 if ((grptb
== NULL
) && (grptb_start() < 0))
388 * look up in hash table, if found and valid return the uid,
389 * if found and invalid, return a -1
391 ptr
= grptb
[st_hash(name
, namelen
, GID_SZ
)];
392 if ((ptr
!= NULL
) && (ptr
->valid
> 0) && !strcmp(name
, ptr
->name
)) {
393 if (ptr
->valid
== INVALID
)
404 ptr
= grptb
[st_hash(name
, namelen
, GID_SZ
)] =
405 (GIDC
*)malloc(sizeof(GIDC
));
408 * no match, look it up, if no match store it as an invalid entry,
409 * or store the matching gid
412 if ((gr
= getgrnam(name
)) == NULL
)
418 (void)strlcpy(ptr
->name
, name
, sizeof(ptr
->name
));
419 if ((gr
= getgrnam(name
)) == NULL
) {
420 ptr
->valid
= INVALID
;
424 *gid
= ptr
->gid
= gr
->gr_gid
;