| 1 |
/** $MirOS: src/bin/pax/cache.c,v 1.4 2007/02/17 04:52:40 tg Exp $ */ |
| 2 |
/* $OpenBSD: cache.c,v 1.17 2004/03/16 03:28:34 tedu Exp $ */ |
| 3 |
/* $NetBSD: cache.c,v 1.4 1995/03/21 09:07:10 cgd Exp $ */ |
| 4 |
|
| 5 |
/*- |
| 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/param.h> |
| 39 |
#include <sys/time.h> |
| 40 |
#include <sys/stat.h> |
| 41 |
#include <string.h> |
| 42 |
#include <stdio.h> |
| 43 |
#include <pwd.h> |
| 44 |
#include <grp.h> |
| 45 |
#include <unistd.h> |
| 46 |
#include <stdlib.h> |
| 47 |
#include "pax.h" |
| 48 |
#include "cache.h" |
| 49 |
#include "extern.h" |
| 50 |
|
| 51 |
__SCCSID("@(#)cache.c 8.1 (Berkeley) 5/31/93"); |
| 52 |
__RCSID("$MirOS: src/bin/pax/cache.c,v 1.4 2007/02/17 04:52:40 tg Exp $"); |
| 53 |
|
| 54 |
/* |
| 55 |
* routines that control user, group, uid and gid caches (for the archive |
| 56 |
* member print routine). |
| 57 |
* IMPORTANT: |
| 58 |
* these routines cache BOTH hits and misses, a major performance improvement |
| 59 |
*/ |
| 60 |
|
| 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 */ |
| 67 |
|
| 68 |
/* |
| 69 |
* uidtb_start |
| 70 |
* creates an empty uidtb |
| 71 |
* Return: |
| 72 |
* 0 if ok, -1 otherwise |
| 73 |
*/ |
| 74 |
|
| 75 |
int |
| 76 |
uidtb_start(void) |
| 77 |
{ |
| 78 |
static int fail = 0; |
| 79 |
|
| 80 |
if (uidtb != NULL) |
| 81 |
return(0); |
| 82 |
if (fail) |
| 83 |
return(-1); |
| 84 |
if ((uidtb = (UIDC **)calloc(UID_SZ, sizeof(UIDC *))) == NULL) { |
| 85 |
++fail; |
| 86 |
paxwarn(1, "Unable to allocate memory for user id cache table"); |
| 87 |
return(-1); |
| 88 |
} |
| 89 |
return(0); |
| 90 |
} |
| 91 |
|
| 92 |
/* |
| 93 |
* gidtb_start |
| 94 |
* creates an empty gidtb |
| 95 |
* Return: |
| 96 |
* 0 if ok, -1 otherwise |
| 97 |
*/ |
| 98 |
|
| 99 |
int |
| 100 |
gidtb_start(void) |
| 101 |
{ |
| 102 |
static int fail = 0; |
| 103 |
|
| 104 |
if (gidtb != NULL) |
| 105 |
return(0); |
| 106 |
if (fail) |
| 107 |
return(-1); |
| 108 |
if ((gidtb = (GIDC **)calloc(GID_SZ, sizeof(GIDC *))) == NULL) { |
| 109 |
++fail; |
| 110 |
paxwarn(1, "Unable to allocate memory for group id cache table"); |
| 111 |
return(-1); |
| 112 |
} |
| 113 |
return(0); |
| 114 |
} |
| 115 |
|
| 116 |
/* |
| 117 |
* usrtb_start |
| 118 |
* creates an empty usrtb |
| 119 |
* Return: |
| 120 |
* 0 if ok, -1 otherwise |
| 121 |
*/ |
| 122 |
|
| 123 |
int |
| 124 |
usrtb_start(void) |
| 125 |
{ |
| 126 |
static int fail = 0; |
| 127 |
|
| 128 |
if (usrtb != NULL) |
| 129 |
return(0); |
| 130 |
if (fail) |
| 131 |
return(-1); |
| 132 |
if ((usrtb = (UIDC **)calloc(UNM_SZ, sizeof(UIDC *))) == NULL) { |
| 133 |
++fail; |
| 134 |
paxwarn(1, "Unable to allocate memory for user name cache table"); |
| 135 |
return(-1); |
| 136 |
} |
| 137 |
return(0); |
| 138 |
} |
| 139 |
|
| 140 |
/* |
| 141 |
* grptb_start |
| 142 |
* creates an empty grptb |
| 143 |
* Return: |
| 144 |
* 0 if ok, -1 otherwise |
| 145 |
*/ |
| 146 |
|
| 147 |
int |
| 148 |
grptb_start(void) |
| 149 |
{ |
| 150 |
static int fail = 0; |
| 151 |
|
| 152 |
if (grptb != NULL) |
| 153 |
return(0); |
| 154 |
if (fail) |
| 155 |
return(-1); |
| 156 |
if ((grptb = (GIDC **)calloc(GNM_SZ, sizeof(GIDC *))) == NULL) { |
| 157 |
++fail; |
| 158 |
paxwarn(1,"Unable to allocate memory for group name cache table"); |
| 159 |
return(-1); |
| 160 |
} |
| 161 |
return(0); |
| 162 |
} |
| 163 |
|
| 164 |
/* |
| 165 |
* name_uid() |
| 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. |
| 168 |
* Return |
| 169 |
* Pointer to stored name (or a empty string) |
| 170 |
*/ |
| 171 |
|
| 172 |
const char * |
| 173 |
name_uid(uid_t uid, int frc) |
| 174 |
{ |
| 175 |
struct passwd *pw; |
| 176 |
UIDC *ptr; |
| 177 |
|
| 178 |
if ((uidtb == NULL) && (uidtb_start() < 0)) |
| 179 |
return(""); |
| 180 |
|
| 181 |
/* |
| 182 |
* see if we have this uid cached |
| 183 |
*/ |
| 184 |
ptr = uidtb[uid % UID_SZ]; |
| 185 |
if ((ptr != NULL) && (ptr->valid > 0) && (ptr->uid == uid)) { |
| 186 |
/* |
| 187 |
* have an entry for this uid |
| 188 |
*/ |
| 189 |
if (frc || (ptr->valid == VALID)) |
| 190 |
return(ptr->name); |
| 191 |
return(""); |
| 192 |
} |
| 193 |
|
| 194 |
/* |
| 195 |
* No entry for this uid, we will add it |
| 196 |
*/ |
| 197 |
if (!pwopn) { |
| 198 |
#if !defined(__INTERIX) && !defined(__GLIBC__) |
| 199 |
setpassent(1); |
| 200 |
#endif |
| 201 |
++pwopn; |
| 202 |
} |
| 203 |
if (ptr == NULL) |
| 204 |
ptr = uidtb[uid % UID_SZ] = malloc(sizeof(UIDC)); |
| 205 |
|
| 206 |
if ((pw = getpwuid(uid)) == NULL) { |
| 207 |
/* |
| 208 |
* no match for this uid in the local password file |
| 209 |
* a string that is the uid in numeric format |
| 210 |
*/ |
| 211 |
if (ptr == NULL) |
| 212 |
return(""); |
| 213 |
ptr->uid = uid; |
| 214 |
ptr->valid = INVALID; |
| 215 |
(void)snprintf(ptr->name, sizeof(ptr->name), "%lu", |
| 216 |
(unsigned long)uid); |
| 217 |
if (frc == 0) |
| 218 |
return(""); |
| 219 |
} else { |
| 220 |
/* |
| 221 |
* there is an entry for this uid in the password file |
| 222 |
*/ |
| 223 |
if (ptr == NULL) |
| 224 |
return(pw->pw_name); |
| 225 |
ptr->uid = uid; |
| 226 |
(void)strlcpy(ptr->name, pw->pw_name, sizeof(ptr->name)); |
| 227 |
ptr->valid = VALID; |
| 228 |
} |
| 229 |
return(ptr->name); |
| 230 |
} |
| 231 |
|
| 232 |
/* |
| 233 |
* name_gid() |
| 234 |
* caches the name (if any) for the gid. If frc set, we always return the |
| 235 |
* the stored name (if valid or invalid match). We use a simple hash table. |
| 236 |
* Return |
| 237 |
* Pointer to stored name (or a empty string) |
| 238 |
*/ |
| 239 |
|
| 240 |
const char * |
| 241 |
name_gid(gid_t gid, int frc) |
| 242 |
{ |
| 243 |
struct group *gr; |
| 244 |
GIDC *ptr; |
| 245 |
|
| 246 |
if ((gidtb == NULL) && (gidtb_start() < 0)) |
| 247 |
return(""); |
| 248 |
|
| 249 |
/* |
| 250 |
* see if we have this gid cached |
| 251 |
*/ |
| 252 |
ptr = gidtb[gid % GID_SZ]; |
| 253 |
if ((ptr != NULL) && (ptr->valid > 0) && (ptr->gid == gid)) { |
| 254 |
/* |
| 255 |
* have an entry for this gid |
| 256 |
*/ |
| 257 |
if (frc || (ptr->valid == VALID)) |
| 258 |
return(ptr->name); |
| 259 |
return(""); |
| 260 |
} |
| 261 |
|
| 262 |
/* |
| 263 |
* No entry for this gid, we will add it |
| 264 |
*/ |
| 265 |
if (!gropn) { |
| 266 |
#if !defined(__INTERIX) && !defined(__GLIBC__) |
| 267 |
setgroupent(1); |
| 268 |
#endif |
| 269 |
++gropn; |
| 270 |
} |
| 271 |
if (ptr == NULL) |
| 272 |
ptr = gidtb[gid % GID_SZ] = malloc(sizeof(GIDC)); |
| 273 |
|
| 274 |
if ((gr = getgrgid(gid)) == NULL) { |
| 275 |
/* |
| 276 |
* no match for this gid in the local group file, put in |
| 277 |
* a string that is the gid in numberic format |
| 278 |
*/ |
| 279 |
if (ptr == NULL) |
| 280 |
return(""); |
| 281 |
ptr->gid = gid; |
| 282 |
ptr->valid = INVALID; |
| 283 |
(void)snprintf(ptr->name, sizeof(ptr->name), "%lu", |
| 284 |
(unsigned long)gid); |
| 285 |
if (frc == 0) |
| 286 |
return(""); |
| 287 |
} else { |
| 288 |
/* |
| 289 |
* there is an entry for this group in the group file |
| 290 |
*/ |
| 291 |
if (ptr == NULL) |
| 292 |
return(gr->gr_name); |
| 293 |
ptr->gid = gid; |
| 294 |
(void)strlcpy(ptr->name, gr->gr_name, sizeof(ptr->name)); |
| 295 |
ptr->valid = VALID; |
| 296 |
} |
| 297 |
return(ptr->name); |
| 298 |
} |
| 299 |
|
| 300 |
/* |
| 301 |
* uid_name() |
| 302 |
* caches the uid for a given user name. We use a simple hash table. |
| 303 |
* Return |
| 304 |
* the uid (if any) for a user name, or a -1 if no match can be found |
| 305 |
*/ |
| 306 |
|
| 307 |
int |
| 308 |
uid_name(const char *name, uid_t *uid) |
| 309 |
{ |
| 310 |
struct passwd *pw; |
| 311 |
UIDC *ptr; |
| 312 |
int namelen; |
| 313 |
|
| 314 |
/* |
| 315 |
* return -1 for mangled names |
| 316 |
*/ |
| 317 |
if (((namelen = strlen(name)) == 0) || (name[0] == '\0')) |
| 318 |
return(-1); |
| 319 |
if ((usrtb == NULL) && (usrtb_start() < 0)) |
| 320 |
return(-1); |
| 321 |
|
| 322 |
/* |
| 323 |
* look up in hash table, if found and valid return the uid, |
| 324 |
* if found and invalid, return a -1 |
| 325 |
*/ |
| 326 |
ptr = usrtb[st_hash(name, namelen, UNM_SZ)]; |
| 327 |
if ((ptr != NULL) && (ptr->valid > 0) && !strcmp(name, ptr->name)) { |
| 328 |
if (ptr->valid == INVALID) |
| 329 |
return(-1); |
| 330 |
*uid = ptr->uid; |
| 331 |
return(0); |
| 332 |
} |
| 333 |
|
| 334 |
if (!pwopn) { |
| 335 |
#if !defined(__INTERIX) && !defined(__GLIBC__) |
| 336 |
setpassent(1); |
| 337 |
#endif |
| 338 |
++pwopn; |
| 339 |
} |
| 340 |
|
| 341 |
if (ptr == NULL) |
| 342 |
ptr = usrtb[st_hash(name, namelen, UNM_SZ)] = |
| 343 |
(UIDC *)malloc(sizeof(UIDC)); |
| 344 |
|
| 345 |
/* |
| 346 |
* no match, look it up, if no match store it as an invalid entry, |
| 347 |
* or store the matching uid |
| 348 |
*/ |
| 349 |
if (ptr == NULL) { |
| 350 |
if ((pw = getpwnam(name)) == NULL) |
| 351 |
return(-1); |
| 352 |
*uid = pw->pw_uid; |
| 353 |
return(0); |
| 354 |
} |
| 355 |
(void)strlcpy(ptr->name, name, sizeof(ptr->name)); |
| 356 |
if ((pw = getpwnam(name)) == NULL) { |
| 357 |
ptr->valid = INVALID; |
| 358 |
return(-1); |
| 359 |
} |
| 360 |
ptr->valid = VALID; |
| 361 |
*uid = ptr->uid = pw->pw_uid; |
| 362 |
return(0); |
| 363 |
} |
| 364 |
|
| 365 |
/* |
| 366 |
* gid_name() |
| 367 |
* caches the gid for a given group name. We use a simple hash table. |
| 368 |
* Return |
| 369 |
* the gid (if any) for a group name, or a -1 if no match can be found |
| 370 |
*/ |
| 371 |
|
| 372 |
int |
| 373 |
gid_name(const char *name, gid_t *gid) |
| 374 |
{ |
| 375 |
struct group *gr; |
| 376 |
GIDC *ptr; |
| 377 |
int namelen; |
| 378 |
|
| 379 |
/* |
| 380 |
* return -1 for mangled names |
| 381 |
*/ |
| 382 |
if (((namelen = strlen(name)) == 0) || (name[0] == '\0')) |
| 383 |
return(-1); |
| 384 |
if ((grptb == NULL) && (grptb_start() < 0)) |
| 385 |
return(-1); |
| 386 |
|
| 387 |
/* |
| 388 |
* look up in hash table, if found and valid return the uid, |
| 389 |
* if found and invalid, return a -1 |
| 390 |
*/ |
| 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) |
| 394 |
return(-1); |
| 395 |
*gid = ptr->gid; |
| 396 |
return(0); |
| 397 |
} |
| 398 |
|
| 399 |
if (!gropn) { |
| 400 |
#if !defined(__INTERIX) && !defined(__GLIBC__) |
| 401 |
setgroupent(1); |
| 402 |
#endif |
| 403 |
++gropn; |
| 404 |
} |
| 405 |
if (ptr == NULL) |
| 406 |
ptr = grptb[st_hash(name, namelen, GID_SZ)] = |
| 407 |
(GIDC *)malloc(sizeof(GIDC)); |
| 408 |
|
| 409 |
/* |
| 410 |
* no match, look it up, if no match store it as an invalid entry, |
| 411 |
* or store the matching gid |
| 412 |
*/ |
| 413 |
if (ptr == NULL) { |
| 414 |
if ((gr = getgrnam(name)) == NULL) |
| 415 |
return(-1); |
| 416 |
*gid = gr->gr_gid; |
| 417 |
return(0); |
| 418 |
} |
| 419 |
|
| 420 |
(void)strlcpy(ptr->name, name, sizeof(ptr->name)); |
| 421 |
if ((gr = getgrnam(name)) == NULL) { |
| 422 |
ptr->valid = INVALID; |
| 423 |
return(-1); |
| 424 |
} |
| 425 |
ptr->valid = VALID; |
| 426 |
*gid = ptr->gid = gr->gr_gid; |
| 427 |
return(0); |
| 428 |
} |