Debian Codegraph @

cache.c

raw symbols SCIP
1 /* $OpenBSD: cache.c,v 1.23 2016/08/26 04:08:18 guenther Exp $ */
2 /* $NetBSD: cache.c,v 1.4 1995/03/21 09:07:10 cgd Exp $ */
3
4 /*-
5 * Copyright (c) 2018 mirabilos
6 * Copyright (c) 1992 Keith Muller.
7 * Copyright (c) 1992, 1993
8 * The Regents of the University of California. All rights reserved.
9 *
10 * This code is derived from software contributed to Berkeley by
11 * Keith Muller of the University of California, San Diego.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. 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.
24 *
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
35 * SUCH DAMAGE.
36 */
37
38 #include <sys/types.h>
39 #include <sys/stat.h>
40 #if HAVE_GRP_H
41 #include <grp.h>
42 #endif
43 #include <pwd.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #if HAVE_STRINGS_H
48 #include <strings.h>
49 #endif
50
51 #include "pax.h"
52 #include "extern.h"
53
54 #if HAVE_UGID_FROM_UG
55 # error do not include this file if you do not need it
56 #endif
57
58 __RCSID("$MirOS: src/bin/pax/cache.c,v 1.11 2018/12/13 07:09:09 tg Exp $");
59
60 /*
61 * Constants and data structures used to implement group and password file
62 * caches. Traditional passwd/group cache routines perform quite poorly with
63 * archives. The chances of hitting a valid lookup with an archive is quite a
64 * bit worse than with files already resident on the filesystem. These misses
65 * create a MAJOR performance cost. To address this problem, these routines
66 * cache both hits and misses.
67 *
68 * NOTE: name lengths must be as large as those stored in ANY PROTOCOL and
69 * as stored in the passwd and group files. CACHE SIZES MUST BE PRIME
70 */
71 #define UNMLEN 32 /* >= user name found in any protocol */
72 #define GNMLEN 32 /* >= group name found in any protocol */
73 #define UID_SZ 317 /* size of user_name/uid cache */
74 #define UNM_SZ 317 /* size of user_name/uid cache */
75 #define GID_SZ 251 /* size of gid cache */
76 #define GNM_SZ 317 /* size of group name cache */
77 #define VALID 1 /* entry and name are valid */
78 #define INVALID 2 /* entry valid, name NOT valid */
79
80 /*
81 * Node structures used in the user, group, uid, and gid caches.
82 */
83
84 typedef struct uidc {
85 int valid; /* is this a valid or a miss entry */
86 char name[UNMLEN]; /* uid name */
87 uid_t uid; /* cached uid */
88 } UIDC;
89
90 typedef struct gidc {
91 int valid; /* is this a valid or a miss entry */
92 char name[GNMLEN]; /* gid name */
93 gid_t gid; /* cached gid */
94 } GIDC;
95
96 /*
97 * routines that control user, group, uid and gid caches (for the archive
98 * member print routine).
99 * IMPORTANT:
100 * these routines cache BOTH hits and misses, a major performance improvement
101 */
102
103 static int pwopn = 0; /* is password file open */
104 static int gropn = 0; /* is group file open */
105 #if !HAVE_UG_FROM_UGID
106 static UIDC **uidtb = NULL; /* uid to name cache */
107 static GIDC **gidtb = NULL; /* gid to name cache */
108 #endif
109 static UIDC **usrtb = NULL; /* user name to uid cache */
110 static GIDC **grptb = NULL; /* group name to gid cache */
111
112 #if !HAVE_UG_FROM_UGID
113 /*
114 * uidtb_start
115 * creates an empty uidtb
116 * Return:
117 * 0 if ok, -1 otherwise
118 */
119
120 int
121 uidtb_start(void)
122 {
123 static int fail = 0;
124
125 if (uidtb != NULL)
126 return(0);
127 if (fail)
128 return(-1);
129 if ((uidtb = calloc(UID_SZ, sizeof(UIDC *))) == NULL) {
130 ++fail;
131 paxwarn(1, "%s for %s", "Out of memory",
132 "user id cache table");
133 return (-1);
134 }
135 return(0);
136 }
137
138 /*
139 * gidtb_start
140 * creates an empty gidtb
141 * Return:
142 * 0 if ok, -1 otherwise
143 */
144
145 int
146 gidtb_start(void)
147 {
148 static int fail = 0;
149
150 if (gidtb != NULL)
151 return(0);
152 if (fail)
153 return(-1);
154 if ((gidtb = calloc(GID_SZ, sizeof(GIDC *))) == NULL) {
155 ++fail;
156 paxwarn(1, "%s for %s", "Out of memory",
157 "group id cache table");
158 return (-1);
159 }
160 return(0);
161 }
162 #endif
163
164 /*
165 * usrtb_start
166 * creates an empty usrtb
167 * Return:
168 * 0 if ok, -1 otherwise
169 */
170
171 int
172 usrtb_start(void)
173 {
174 static int fail = 0;
175
176 if (usrtb != NULL)
177 return(0);
178 if (fail)
179 return(-1);
180 if ((usrtb = calloc(UNM_SZ, sizeof(UIDC *))) == NULL) {
181 ++fail;
182 paxwarn(1, "%s for %s", "Out of memory",
183 "user name cache table");
184 return (-1);
185 }
186 return(0);
187 }
188
189 /*
190 * grptb_start
191 * creates an empty grptb
192 * Return:
193 * 0 if ok, -1 otherwise
194 */
195
196 int
197 grptb_start(void)
198 {
199 static int fail = 0;
200
201 if (grptb != NULL)
202 return(0);
203 if (fail)
204 return(-1);
205 if ((grptb = calloc(GNM_SZ, sizeof(GIDC *))) == NULL) {
206 ++fail;
207 paxwarn(1,"%s for %s", "Out of memory",
208 "group name cache table");
209 return (-1);
210 }
211 return(0);
212 }
213
214 #if !HAVE_UG_FROM_UGID
215 /*
216 * name_uid()
217 * caches the name (if any) for the uid. If frc set, we always return the
218 * the stored name (if valid or invalid match). We use a simple hash table.
219 * Return
220 * Pointer to stored name (or a empty string)
221 */
222
223 const char *
224 name_uid(uid_t uid, int frc)
225 {
226 struct passwd *pw;
227 UIDC *ptr;
228
229 if ((uidtb == NULL) && (uidtb_start() < 0))
230 return("");
231
232 /*
233 * see if we have this uid cached
234 */
235 ptr = uidtb[uid % UID_SZ];
236 if ((ptr != NULL) && (ptr->valid > 0) && (ptr->uid == uid)) {
237 /*
238 * have an entry for this uid
239 */
240 if (frc || (ptr->valid == VALID))
241 return(ptr->name);
242 return("");
243 }
244
245 /*
246 * No entry for this uid, we will add it
247 */
248 if (!pwopn) {
249 #if HAVE_SETPGENT
250 setpassent(1);
251 #else
252 setpwent();
253 #endif
254 ++pwopn;
255 }
256 if (ptr == NULL)
257 ptr = uidtb[uid % UID_SZ] = malloc(sizeof(UIDC));
258
259 if ((pw = getpwuid(uid)) == NULL) {
260 /*
261 * no match for this uid in the local password file
262 * a string that is the uid in numeric format
263 */
264 if (ptr == NULL)
265 return("");
266 ptr->uid = uid;
267 ptr->valid = INVALID;
268 (void)snprintf(ptr->name, sizeof(ptr->name), "%lu",
269 (unsigned long)uid);
270 if (frc == 0)
271 return("");
272 } else {
273 /*
274 * there is an entry for this uid in the password file
275 */
276 if (ptr == NULL)
277 return(pw->pw_name);
278 ptr->uid = uid;
279 (void)strlcpy(ptr->name, pw->pw_name, sizeof(ptr->name));
280 ptr->valid = VALID;
281 }
282 return(ptr->name);
283 }
284
285 /*
286 * name_gid()
287 * caches the name (if any) for the gid. If frc set, we always return the
288 * the stored name (if valid or invalid match). We use a simple hash table.
289 * Return
290 * Pointer to stored name (or a empty string)
291 */
292
293 const char *
294 name_gid(gid_t gid, int frc)
295 {
296 struct group *gr;
297 GIDC *ptr;
298
299 if ((gidtb == NULL) && (gidtb_start() < 0))
300 return("");
301
302 /*
303 * see if we have this gid cached
304 */
305 ptr = gidtb[gid % GID_SZ];
306 if ((ptr != NULL) && (ptr->valid > 0) && (ptr->gid == gid)) {
307 /*
308 * have an entry for this gid
309 */
310 if (frc || (ptr->valid == VALID))
311 return(ptr->name);
312 return("");
313 }
314
315 /*
316 * No entry for this gid, we will add it
317 */
318 if (!gropn) {
319 #if HAVE_SETPGENT
320 setgroupent(1);
321 #else
322 setgrent();
323 #endif
324 ++gropn;
325 }
326 if (ptr == NULL)
327 ptr = gidtb[gid % GID_SZ] = malloc(sizeof(GIDC));
328
329 if ((gr = getgrgid(gid)) == NULL) {
330 /*
331 * no match for this gid in the local group file, put in
332 * a string that is the gid in numeric format
333 */
334 if (ptr == NULL)
335 return("");
336 ptr->gid = gid;
337 ptr->valid = INVALID;
338 (void)snprintf(ptr->name, sizeof(ptr->name), "%lu",
339 (unsigned long)gid);
340 if (frc == 0)
341 return("");
342 } else {
343 /*
344 * there is an entry for this group in the group file
345 */
346 if (ptr == NULL)
347 return(gr->gr_name);
348 ptr->gid = gid;
349 (void)strlcpy(ptr->name, gr->gr_name, sizeof(ptr->name));
350 ptr->valid = VALID;
351 }
352 return(ptr->name);
353 }
354 #endif
355
356 /*
357 * uid_name()
358 * caches the uid for a given user name. We use a simple hash table.
359 * Return
360 * the uid (if any) for a user name, or a -1 if no match can be found
361 */
362
363 int
364 uid_name(const char *name, uid_t *uid)
365 {
366 struct passwd *pw;
367 UIDC *ptr;
368 int namelen;
369
370 /*
371 * return -1 for mangled names
372 */
373 if (((namelen = strlen(name)) == 0) || (name[0] == '\0'))
374 return(-1);
375 if ((usrtb == NULL) && (usrtb_start() < 0))
376 return(-1);
377
378 /*
379 * look up in hash table, if found and valid return the uid,
380 * if found and invalid, return a -1
381 */
382 ptr = usrtb[st_hash(name, namelen, UNM_SZ)];
383 if ((ptr != NULL) && (ptr->valid > 0) && !strcmp(name, ptr->name)) {
384 if (ptr->valid == INVALID)
385 return(-1);
386 *uid = ptr->uid;
387 return(0);
388 }
389
390 if (!pwopn) {
391 #if HAVE_SETPGENT
392 setpassent(1);
393 #else
394 setpwent();
395 #endif
396 ++pwopn;
397 }
398
399 if (ptr == NULL)
400 ptr = usrtb[st_hash(name, namelen, UNM_SZ)] =
401 malloc(sizeof(UIDC));
402
403 /*
404 * no match, look it up, if no match store it as an invalid entry,
405 * or store the matching uid
406 */
407 if (ptr == NULL) {
408 if ((pw = getpwnam(name)) == NULL)
409 return(-1);
410 *uid = pw->pw_uid;
411 return(0);
412 }
413 (void)strlcpy(ptr->name, name, sizeof(ptr->name));
414 if ((pw = getpwnam(name)) == NULL) {
415 ptr->valid = INVALID;
416 return(-1);
417 }
418 ptr->valid = VALID;
419 *uid = ptr->uid = pw->pw_uid;
420 return(0);
421 }
422
423 /*
424 * gid_name()
425 * caches the gid for a given group name. We use a simple hash table.
426 * Return
427 * the gid (if any) for a group name, or a -1 if no match can be found
428 */
429
430 int
431 gid_name(const char *name, gid_t *gid)
432 {
433 struct group *gr;
434 GIDC *ptr;
435 int namelen;
436
437 /*
438 * return -1 for mangled names
439 */
440 if (((namelen = strlen(name)) == 0) || (name[0] == '\0'))
441 return(-1);
442 if ((grptb == NULL) && (grptb_start() < 0))
443 return(-1);
444
445 /*
446 * look up in hash table, if found and valid return the uid,
447 * if found and invalid, return a -1
448 */
449 ptr = grptb[st_hash(name, namelen, GID_SZ)];
450 if ((ptr != NULL) && (ptr->valid > 0) && !strcmp(name, ptr->name)) {
451 if (ptr->valid == INVALID)
452 return(-1);
453 *gid = ptr->gid;
454 return(0);
455 }
456
457 if (!gropn) {
458 #if HAVE_SETPGENT
459 setgroupent(1);
460 #else
461 setgrent();
462 #endif
463 ++gropn;
464 }
465 if (ptr == NULL)
466 ptr = grptb[st_hash(name, namelen, GID_SZ)] =
467 malloc(sizeof(GIDC));
468
469 /*
470 * no match, look it up, if no match store it as an invalid entry,
471 * or store the matching gid
472 */
473 if (ptr == NULL) {
474 if ((gr = getgrnam(name)) == NULL)
475 return(-1);
476 *gid = gr->gr_gid;
477 return(0);
478 }
479
480 (void)strlcpy(ptr->name, name, sizeof(ptr->name));
481 if ((gr = getgrnam(name)) == NULL) {
482 ptr->valid = INVALID;
483 return(-1);
484 }
485 ptr->valid = VALID;
486 *gid = ptr->gid = gr->gr_gid;
487 return(0);
488 }
489
490 int
491 uid_uncached(const char *name, uid_t *uid)
492 {
493 struct passwd *pw;
494
495 if ((pw = getpwnam(name)) == NULL)
496 return (-1);
497 *uid = pw->pw_uid;
498 return (0);
499 }
500
501 int
502 gid_uncached(const char *name, gid_t *gid)
503 {
504 struct group *gr;
505
506 if ((gr = getgrnam(name)) == NULL)
507 return (-1);
508 *gid = gr->gr_gid;
509 return (0);
510 }