]>
git.saurik.com Git - apple/file_cmds.git/blob - pax/cache.c
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
[] __attribute__((__unused__
)) = "$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
222 if ((ptr
!= NULL
) && (ptr
->valid
> 0) && (ptr
->uid
== uid
)) {
224 * have an entry for this uid
226 if (frc
|| (ptr
->valid
== VALID
))
232 * No entry for this uid, we will add it
239 ptr
= (UIDC
*)malloc(sizeof(UIDC
));
241 if ((pw
= getpwuid(uid
)) == NULL
) {
243 * no match for this uid in the local password file
244 * a string that is the uid in numberic format
249 ptr
->valid
= INVALID
;
251 (void)snprintf(ptr
->name
, sizeof(ptr
->name
), "%u", uid
);
253 (void)snprintf(ptr
->name
, sizeof(ptr
->name
), "%lu",
261 * there is an entry for this uid in the password file
266 (void)strncpy(ptr
->name
, pw
->pw_name
, UNMLEN
-1);
267 ptr
->name
[UNMLEN
-1] = '\0';
276 * caches the name (if any) for the gid. If frc set, we always return the
277 * the stored name (if valid or invalid match). We use a simple hash table.
279 * Pointer to stored name (or a empty string)
284 name_gid(gid_t gid
, int frc
)
292 register struct group
*gr
;
296 if ((gidtb
== NULL
) && (gidtb_start() < 0))
300 * see if we have this gid cached
304 if ((ptr
!= NULL
) && (ptr
->valid
> 0) && (ptr
->gid
== gid
)) {
306 * have an entry for this gid
308 if (frc
|| (ptr
->valid
== VALID
))
314 * No entry for this gid, we will add it
321 ptr
= (GIDC
*)malloc(sizeof(GIDC
));
323 if ((gr
= getgrgid(gid
)) == NULL
) {
325 * no match for this gid in the local group file, put in
326 * a string that is the gid in numberic format
331 ptr
->valid
= INVALID
;
333 (void)snprintf(ptr
->name
, sizeof(ptr
->name
), "%u", gid
);
335 (void)snprintf(ptr
->name
, sizeof(ptr
->name
), "%lu",
343 * there is an entry for this group in the group file
348 (void)strncpy(ptr
->name
, gr
->gr_name
, GNMLEN
-1);
349 ptr
->name
[GNMLEN
-1] = '\0';
358 * caches the uid for a given user name. We use a simple hash table.
360 * the uid (if any) for a user name, or a -1 if no match can be found
365 uid_name(char *name
, uid_t
*uid
)
373 register struct passwd
*pw
;
375 register int namelen
;
379 * return -1 for mangled names
381 if (((namelen
= strlen(name
)) == 0) || (name
[0] == '\0'))
383 if ((usrtb
== NULL
) && (usrtb_start() < 0))
387 * look up in hash table, if found and valid return the uid,
388 * if found and invalid, return a -1
390 hash
= st_hash(name
, namelen
, UNM_SZ
);
392 if ((ptr
!= NULL
) && (ptr
->valid
> 0) && !strcmp(name
, ptr
->name
)) {
393 if (ptr
->valid
== INVALID
)
405 ptr
= (UIDC
*)malloc(sizeof(UIDC
));
408 * no match, look it up, if no match store it as an invalid entry,
409 * or store the matching uid
412 if ((pw
= getpwnam(name
)) == NULL
)
418 (void)strncpy(ptr
->name
, name
, UNMLEN
-1);
419 ptr
->name
[UNMLEN
-1] = '\0';
420 if ((pw
= getpwnam(name
)) == NULL
) {
421 ptr
->valid
= INVALID
;
425 *uid
= ptr
->uid
= pw
->pw_uid
;
431 * caches the gid for a given group name. We use a simple hash table.
433 * the gid (if any) for a group name, or a -1 if no match can be found
438 gid_name(char *name
, gid_t
*gid
)
446 register struct group
*gr
;
448 register int namelen
;
452 * return -1 for mangled names
454 if (((namelen
= strlen(name
)) == 0) || (name
[0] == '\0'))
456 if ((grptb
== NULL
) && (grptb_start() < 0))
460 * look up in hash table, if found and valid return the uid,
461 * if found and invalid, return a -1
463 hash
= st_hash(name
, namelen
, GID_SZ
);
465 if ((ptr
!= NULL
) && (ptr
->valid
> 0) && !strcmp(name
, ptr
->name
)) {
466 if (ptr
->valid
== INVALID
)
477 ptr
= (GIDC
*)malloc(sizeof(GIDC
));
480 * no match, look it up, if no match store it as an invalid entry,
481 * or store the matching gid
484 if ((gr
= getgrnam(name
)) == NULL
)
491 (void)strncpy(ptr
->name
, name
, GNMLEN
-1);
492 ptr
->name
[GNMLEN
-1] = '\0';
493 if ((gr
= getgrnam(name
)) == NULL
) {
494 ptr
->valid
= INVALID
;
498 *gid
= ptr
->gid
= gr
->gr_gid
;