| 1 | /* $OpenBSD: cache.c,v 1.19 2009/12/22 12:09:36 jasper Exp $ */
|
|---|
| 2 | /* $NetBSD: cache.c,v 1.4 1995/03/21 09:07:10 cgd Exp $ */
|
|---|
| 3 |
|
|---|
| 4 | /*-
|
|---|
| 5 | * Copyright (c) 1992 Keith Muller.
|
|---|
| 6 | * Copyright (c) 1992, 1993
|
|---|
| 7 | * The Regents of the University of California. All rights reserved.
|
|---|
| 8 | *
|
|---|
| 9 | * This code is derived from software contributed to Berkeley by
|
|---|
| 10 | * Keith Muller of the University of California, San Diego.
|
|---|
| 11 | *
|
|---|
| 12 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 13 | * modification, are permitted provided that the following conditions
|
|---|
| 14 | * are met:
|
|---|
| 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.
|
|---|
| 23 | *
|
|---|
| 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
|
|---|
| 34 | * SUCH DAMAGE.
|
|---|
| 35 | */
|
|---|
| 36 |
|
|---|
| 37 | #include <sys/param.h>
|
|---|
| 38 | #include <sys/time.h>
|
|---|
| 39 | #include <sys/stat.h>
|
|---|
| 40 | #include <string.h>
|
|---|
| 41 | #include <stdio.h>
|
|---|
| 42 | #include <pwd.h>
|
|---|
| 43 | #include <grp.h>
|
|---|
| 44 | #include <unistd.h>
|
|---|
| 45 | #include <stdlib.h>
|
|---|
| 46 | #include "pax.h"
|
|---|
| 47 | #include "cache.h"
|
|---|
| 48 | #include "extern.h"
|
|---|
| 49 |
|
|---|
| 50 | __RCSID("$MirOS: src/bin/pax/cache.c,v 1.6 2012/02/12 01:02:05 tg Exp $");
|
|---|
| 51 |
|
|---|
| 52 | /*
|
|---|
| 53 | * routines that control user, group, uid and gid caches (for the archive
|
|---|
| 54 | * member print routine).
|
|---|
| 55 | * IMPORTANT:
|
|---|
| 56 | * these routines cache BOTH hits and misses, a major performance improvement
|
|---|
| 57 | */
|
|---|
| 58 |
|
|---|
| 59 | static int pwopn = 0; /* is password file open */
|
|---|
| 60 | static int gropn = 0; /* is group file open */
|
|---|
| 61 | static UIDC **uidtb = NULL; /* uid to name cache */
|
|---|
| 62 | static GIDC **gidtb = NULL; /* gid to name cache */
|
|---|
| 63 | static UIDC **usrtb = NULL; /* user name to uid cache */
|
|---|
| 64 | static GIDC **grptb = NULL; /* group name to gid cache */
|
|---|
| 65 |
|
|---|
| 66 | /*
|
|---|
| 67 | * uidtb_start
|
|---|
| 68 | * creates an empty uidtb
|
|---|
| 69 | * Return:
|
|---|
| 70 | * 0 if ok, -1 otherwise
|
|---|
| 71 | */
|
|---|
| 72 |
|
|---|
| 73 | int
|
|---|
| 74 | uidtb_start(void)
|
|---|
| 75 | {
|
|---|
| 76 | static int fail = 0;
|
|---|
| 77 |
|
|---|
| 78 | if (uidtb != NULL)
|
|---|
| 79 | return(0);
|
|---|
| 80 | if (fail)
|
|---|
| 81 | return(-1);
|
|---|
| 82 | if ((uidtb = (UIDC **)calloc(UID_SZ, sizeof(UIDC *))) == NULL) {
|
|---|
| 83 | ++fail;
|
|---|
| 84 | paxwarn(1, "Unable to allocate memory for user id cache table");
|
|---|
| 85 | return(-1);
|
|---|
| 86 | }
|
|---|
| 87 | return(0);
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | /*
|
|---|
| 91 | * gidtb_start
|
|---|
| 92 | * creates an empty gidtb
|
|---|
| 93 | * Return:
|
|---|
| 94 | * 0 if ok, -1 otherwise
|
|---|
| 95 | */
|
|---|
| 96 |
|
|---|
| 97 | int
|
|---|
| 98 | gidtb_start(void)
|
|---|
| 99 | {
|
|---|
| 100 | static int fail = 0;
|
|---|
| 101 |
|
|---|
| 102 | if (gidtb != NULL)
|
|---|
| 103 | return(0);
|
|---|
| 104 | if (fail)
|
|---|
| 105 | return(-1);
|
|---|
| 106 | if ((gidtb = (GIDC **)calloc(GID_SZ, sizeof(GIDC *))) == NULL) {
|
|---|
| 107 | ++fail;
|
|---|
| 108 | paxwarn(1, "Unable to allocate memory for group id cache table");
|
|---|
| 109 | return(-1);
|
|---|
| 110 | }
|
|---|
| 111 | return(0);
|
|---|
| 112 | }
|
|---|
| 113 |
|
|---|
| 114 | /*
|
|---|
| 115 | * usrtb_start
|
|---|
| 116 | * creates an empty usrtb
|
|---|
| 117 | * Return:
|
|---|
| 118 | * 0 if ok, -1 otherwise
|
|---|
| 119 | */
|
|---|
| 120 |
|
|---|
| 121 | int
|
|---|
| 122 | usrtb_start(void)
|
|---|
| 123 | {
|
|---|
| 124 | static int fail = 0;
|
|---|
| 125 |
|
|---|
| 126 | if (usrtb != NULL)
|
|---|
| 127 | return(0);
|
|---|
| 128 | if (fail)
|
|---|
| 129 | return(-1);
|
|---|
| 130 | if ((usrtb = (UIDC **)calloc(UNM_SZ, sizeof(UIDC *))) == NULL) {
|
|---|
| 131 | ++fail;
|
|---|
| 132 | paxwarn(1, "Unable to allocate memory for user name cache table");
|
|---|
| 133 | return(-1);
|
|---|
| 134 | }
|
|---|
| 135 | return(0);
|
|---|
| 136 | }
|
|---|
| 137 |
|
|---|
| 138 | /*
|
|---|
| 139 | * grptb_start
|
|---|
| 140 | * creates an empty grptb
|
|---|
| 141 | * Return:
|
|---|
| 142 | * 0 if ok, -1 otherwise
|
|---|
| 143 | */
|
|---|
| 144 |
|
|---|
| 145 | int
|
|---|
| 146 | grptb_start(void)
|
|---|
| 147 | {
|
|---|
| 148 | static int fail = 0;
|
|---|
| 149 |
|
|---|
| 150 | if (grptb != NULL)
|
|---|
| 151 | return(0);
|
|---|
| 152 | if (fail)
|
|---|
| 153 | return(-1);
|
|---|
| 154 | if ((grptb = (GIDC **)calloc(GNM_SZ, sizeof(GIDC *))) == NULL) {
|
|---|
| 155 | ++fail;
|
|---|
| 156 | paxwarn(1,"Unable to allocate memory for group name cache table");
|
|---|
| 157 | return(-1);
|
|---|
| 158 | }
|
|---|
| 159 | return(0);
|
|---|
| 160 | }
|
|---|
| 161 |
|
|---|
| 162 | /*
|
|---|
| 163 | * name_uid()
|
|---|
| 164 | * caches the name (if any) for the uid. If frc set, we always return the
|
|---|
| 165 | * the stored name (if valid or invalid match). We use a simple hash table.
|
|---|
| 166 | * Return
|
|---|
| 167 | * Pointer to stored name (or a empty string)
|
|---|
| 168 | */
|
|---|
| 169 |
|
|---|
| 170 | const char *
|
|---|
| 171 | name_uid(uid_t uid, int frc)
|
|---|
| 172 | {
|
|---|
| 173 | struct passwd *pw;
|
|---|
| 174 | UIDC *ptr;
|
|---|
| 175 |
|
|---|
| 176 | if ((uidtb == NULL) && (uidtb_start() < 0))
|
|---|
| 177 | return("");
|
|---|
| 178 |
|
|---|
| 179 | /*
|
|---|
| 180 | * see if we have this uid cached
|
|---|
| 181 | */
|
|---|
| 182 | ptr = uidtb[uid % UID_SZ];
|
|---|
| 183 | if ((ptr != NULL) && (ptr->valid > 0) && (ptr->uid == uid)) {
|
|---|
| 184 | /*
|
|---|
| 185 | * have an entry for this uid
|
|---|
| 186 | */
|
|---|
| 187 | if (frc || (ptr->valid == VALID))
|
|---|
| 188 | return(ptr->name);
|
|---|
| 189 | return("");
|
|---|
| 190 | }
|
|---|
| 191 |
|
|---|
| 192 | /*
|
|---|
| 193 | * No entry for this uid, we will add it
|
|---|
| 194 | */
|
|---|
| 195 | if (!pwopn) {
|
|---|
| 196 | #if defined(__GLIBC__)
|
|---|
| 197 | setpwent();
|
|---|
| 198 | #elif !defined(__INTERIX)
|
|---|
| 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(__GLIBC__)
|
|---|
| 267 | setgrent();
|
|---|
| 268 | #elif !defined(__INTERIX)
|
|---|
| 269 | setgroupent(1);
|
|---|
| 270 | #endif
|
|---|
| 271 | ++gropn;
|
|---|
| 272 | }
|
|---|
| 273 | if (ptr == NULL)
|
|---|
| 274 | ptr = gidtb[gid % GID_SZ] = malloc(sizeof(GIDC));
|
|---|
| 275 |
|
|---|
| 276 | if ((gr = getgrgid(gid)) == NULL) {
|
|---|
| 277 | /*
|
|---|
| 278 | * no match for this gid in the local group file, put in
|
|---|
| 279 | * a string that is the gid in numeric format
|
|---|
| 280 | */
|
|---|
| 281 | if (ptr == NULL)
|
|---|
| 282 | return("");
|
|---|
| 283 | ptr->gid = gid;
|
|---|
| 284 | ptr->valid = INVALID;
|
|---|
| 285 | (void)snprintf(ptr->name, sizeof(ptr->name), "%lu",
|
|---|
| 286 | (unsigned long)gid);
|
|---|
| 287 | if (frc == 0)
|
|---|
| 288 | return("");
|
|---|
| 289 | } else {
|
|---|
| 290 | /*
|
|---|
| 291 | * there is an entry for this group in the group file
|
|---|
| 292 | */
|
|---|
| 293 | if (ptr == NULL)
|
|---|
| 294 | return(gr->gr_name);
|
|---|
| 295 | ptr->gid = gid;
|
|---|
| 296 | (void)strlcpy(ptr->name, gr->gr_name, sizeof(ptr->name));
|
|---|
| 297 | ptr->valid = VALID;
|
|---|
| 298 | }
|
|---|
| 299 | return(ptr->name);
|
|---|
| 300 | }
|
|---|
| 301 |
|
|---|
| 302 | /*
|
|---|
| 303 | * uid_name()
|
|---|
| 304 | * caches the uid for a given user name. We use a simple hash table.
|
|---|
| 305 | * Return
|
|---|
| 306 | * the uid (if any) for a user name, or a -1 if no match can be found
|
|---|
| 307 | */
|
|---|
| 308 |
|
|---|
| 309 | int
|
|---|
| 310 | uid_name(const char *name, uid_t *uid)
|
|---|
| 311 | {
|
|---|
| 312 | struct passwd *pw;
|
|---|
| 313 | UIDC *ptr;
|
|---|
| 314 | int namelen;
|
|---|
| 315 |
|
|---|
| 316 | /*
|
|---|
| 317 | * return -1 for mangled names
|
|---|
| 318 | */
|
|---|
| 319 | if (((namelen = strlen(name)) == 0) || (name[0] == '\0'))
|
|---|
| 320 | return(-1);
|
|---|
| 321 | if ((usrtb == NULL) && (usrtb_start() < 0))
|
|---|
| 322 | return(-1);
|
|---|
| 323 |
|
|---|
| 324 | /*
|
|---|
| 325 | * look up in hash table, if found and valid return the uid,
|
|---|
| 326 | * if found and invalid, return a -1
|
|---|
| 327 | */
|
|---|
| 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)
|
|---|
| 331 | return(-1);
|
|---|
| 332 | *uid = ptr->uid;
|
|---|
| 333 | return(0);
|
|---|
| 334 | }
|
|---|
| 335 |
|
|---|
| 336 | if (!pwopn) {
|
|---|
| 337 | #if defined(__GLIBC__)
|
|---|
| 338 | setpwent();
|
|---|
| 339 | #elif !defined(__INTERIX)
|
|---|
| 340 | setpassent(1);
|
|---|
| 341 | #endif
|
|---|
| 342 | ++pwopn;
|
|---|
| 343 | }
|
|---|
| 344 |
|
|---|
| 345 | if (ptr == NULL)
|
|---|
| 346 | ptr = usrtb[st_hash(name, namelen, UNM_SZ)] =
|
|---|
| 347 | (UIDC *)malloc(sizeof(UIDC));
|
|---|
| 348 |
|
|---|
| 349 | /*
|
|---|
| 350 | * no match, look it up, if no match store it as an invalid entry,
|
|---|
| 351 | * or store the matching uid
|
|---|
| 352 | */
|
|---|
| 353 | if (ptr == NULL) {
|
|---|
| 354 | if ((pw = getpwnam(name)) == NULL)
|
|---|
| 355 | return(-1);
|
|---|
| 356 | *uid = pw->pw_uid;
|
|---|
| 357 | return(0);
|
|---|
| 358 | }
|
|---|
| 359 | (void)strlcpy(ptr->name, name, sizeof(ptr->name));
|
|---|
| 360 | if ((pw = getpwnam(name)) == NULL) {
|
|---|
| 361 | ptr->valid = INVALID;
|
|---|
| 362 | return(-1);
|
|---|
| 363 | }
|
|---|
| 364 | ptr->valid = VALID;
|
|---|
| 365 | *uid = ptr->uid = pw->pw_uid;
|
|---|
| 366 | return(0);
|
|---|
| 367 | }
|
|---|
| 368 |
|
|---|
| 369 | /*
|
|---|
| 370 | * gid_name()
|
|---|
| 371 | * caches the gid for a given group name. We use a simple hash table.
|
|---|
| 372 | * Return
|
|---|
| 373 | * the gid (if any) for a group name, or a -1 if no match can be found
|
|---|
| 374 | */
|
|---|
| 375 |
|
|---|
| 376 | int
|
|---|
| 377 | gid_name(const char *name, gid_t *gid)
|
|---|
| 378 | {
|
|---|
| 379 | struct group *gr;
|
|---|
| 380 | GIDC *ptr;
|
|---|
| 381 | int namelen;
|
|---|
| 382 |
|
|---|
| 383 | /*
|
|---|
| 384 | * return -1 for mangled names
|
|---|
| 385 | */
|
|---|
| 386 | if (((namelen = strlen(name)) == 0) || (name[0] == '\0'))
|
|---|
| 387 | return(-1);
|
|---|
| 388 | if ((grptb == NULL) && (grptb_start() < 0))
|
|---|
| 389 | return(-1);
|
|---|
| 390 |
|
|---|
| 391 | /*
|
|---|
| 392 | * look up in hash table, if found and valid return the uid,
|
|---|
| 393 | * if found and invalid, return a -1
|
|---|
| 394 | */
|
|---|
| 395 | ptr = grptb[st_hash(name, namelen, GID_SZ)];
|
|---|
| 396 | if ((ptr != NULL) && (ptr->valid > 0) && !strcmp(name, ptr->name)) {
|
|---|
| 397 | if (ptr->valid == INVALID)
|
|---|
| 398 | return(-1);
|
|---|
| 399 | *gid = ptr->gid;
|
|---|
| 400 | return(0);
|
|---|
| 401 | }
|
|---|
| 402 |
|
|---|
| 403 | if (!gropn) {
|
|---|
| 404 | #if defined(__GLIBC__)
|
|---|
| 405 | setgrent();
|
|---|
| 406 | #elif !defined(__INTERIX)
|
|---|
| 407 | setgroupent(1);
|
|---|
| 408 | #endif
|
|---|
| 409 | ++gropn;
|
|---|
| 410 | }
|
|---|
| 411 | if (ptr == NULL)
|
|---|
| 412 | ptr = grptb[st_hash(name, namelen, GID_SZ)] =
|
|---|
| 413 | (GIDC *)malloc(sizeof(GIDC));
|
|---|
| 414 |
|
|---|
| 415 | /*
|
|---|
| 416 | * no match, look it up, if no match store it as an invalid entry,
|
|---|
| 417 | * or store the matching gid
|
|---|
| 418 | */
|
|---|
| 419 | if (ptr == NULL) {
|
|---|
| 420 | if ((gr = getgrnam(name)) == NULL)
|
|---|
| 421 | return(-1);
|
|---|
| 422 | *gid = gr->gr_gid;
|
|---|
| 423 | return(0);
|
|---|
| 424 | }
|
|---|
| 425 |
|
|---|
| 426 | (void)strlcpy(ptr->name, name, sizeof(ptr->name));
|
|---|
| 427 | if ((gr = getgrnam(name)) == NULL) {
|
|---|
| 428 | ptr->valid = INVALID;
|
|---|
| 429 | return(-1);
|
|---|
| 430 | }
|
|---|
| 431 | ptr->valid = VALID;
|
|---|
| 432 | *gid = ptr->gid = gr->gr_gid;
|
|---|
| 433 | return(0);
|
|---|
| 434 | }
|
|---|