[9] | 1 | /*-
|
---|
| 2 | * Copyright (c) 1992 Keith Muller.
|
---|
| 3 | * Copyright (c) 1992, 1993
|
---|
| 4 | * The Regents of the University of California. All rights reserved.
|
---|
| 5 | *
|
---|
| 6 | * This code is derived from software contributed to Berkeley by
|
---|
| 7 | * Keith Muller of the University of California, San Diego.
|
---|
| 8 | *
|
---|
| 9 | * Redistribution and use in source and binary forms, with or without
|
---|
| 10 | * modification, are permitted provided that the following conditions
|
---|
| 11 | * are met:
|
---|
| 12 | * 1. Redistributions of source code must retain the above copyright
|
---|
| 13 | * notice, this list of conditions and the following disclaimer.
|
---|
| 14 | * 2. Redistributions in binary form must reproduce the above copyright
|
---|
| 15 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 16 | * documentation and/or other materials provided with the distribution.
|
---|
| 17 | * 4. Neither the name of the University nor the names of its contributors
|
---|
| 18 | * may be used to endorse or promote products derived from this software
|
---|
| 19 | * without specific prior written permission.
|
---|
| 20 | *
|
---|
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
---|
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
---|
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
---|
| 24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
---|
| 25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
---|
| 26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
---|
| 27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
---|
| 28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
---|
| 29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
---|
| 30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
---|
| 31 | * SUCH DAMAGE.
|
---|
| 32 | */
|
---|
| 33 |
|
---|
| 34 | #ifndef lint
|
---|
| 35 | #if 0
|
---|
| 36 | static char sccsid[] = "@(#)cache.c 8.1 (Berkeley) 5/31/93";
|
---|
| 37 | #endif
|
---|
| 38 | #endif /* not lint */
|
---|
| 39 |
|
---|
| 40 | #include <sys/types.h>
|
---|
| 41 | #include <sys/stat.h>
|
---|
| 42 | #include <string.h>
|
---|
| 43 | #include <stdio.h>
|
---|
| 44 | #include <pwd.h>
|
---|
| 45 | #include <grp.h>
|
---|
| 46 | #include <unistd.h>
|
---|
| 47 | #include <stdlib.h>
|
---|
| 48 | #include "pax.h"
|
---|
| 49 | #include "cache.h"
|
---|
| 50 | #include "extern.h"
|
---|
| 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 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 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 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 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 an 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 | setpassent(1);
|
---|
| 197 | ++pwopn;
|
---|
| 198 | }
|
---|
| 199 | if (ptr == NULL)
|
---|
| 200 | ptr = uidtb[uid % UID_SZ] = (UIDC *)malloc(sizeof(UIDC));
|
---|
| 201 |
|
---|
| 202 | if ((pw = getpwuid(uid)) == NULL) {
|
---|
| 203 | /*
|
---|
| 204 | * no match for this uid in the local password file
|
---|
| 205 | * a string that is the uid in numeric format
|
---|
| 206 | */
|
---|
| 207 | if (ptr == NULL)
|
---|
| 208 | return("");
|
---|
| 209 | ptr->uid = uid;
|
---|
| 210 | ptr->valid = INVALID;
|
---|
| 211 | # ifdef NET2_STAT
|
---|
| 212 | (void)snprintf(ptr->name, sizeof(ptr->name), "%u", uid);
|
---|
| 213 | # else
|
---|
| 214 | (void)snprintf(ptr->name, sizeof(ptr->name), "%lu",
|
---|
| 215 | (unsigned long)uid);
|
---|
| 216 | # endif
|
---|
| 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)strncpy(ptr->name, pw->pw_name, UNMLEN - 1);
|
---|
| 227 | ptr->name[UNMLEN-1] = '\0';
|
---|
| 228 | ptr->valid = VALID;
|
---|
| 229 | }
|
---|
| 230 | return(ptr->name);
|
---|
| 231 | }
|
---|
| 232 |
|
---|
| 233 | /*
|
---|
| 234 | * name_gid()
|
---|
| 235 | * caches the name (if any) for the gid. If frc set, we always return the
|
---|
| 236 | * the stored name (if valid or invalid match). We use a simple hash table.
|
---|
| 237 | * Return
|
---|
| 238 | * Pointer to stored name (or an empty string).
|
---|
| 239 | */
|
---|
| 240 |
|
---|
| 241 | const char *
|
---|
| 242 | name_gid(gid_t gid, int frc)
|
---|
| 243 | {
|
---|
| 244 | struct group *gr;
|
---|
| 245 | GIDC *ptr;
|
---|
| 246 |
|
---|
| 247 | if ((gidtb == NULL) && (gidtb_start() < 0))
|
---|
| 248 | return("");
|
---|
| 249 |
|
---|
| 250 | /*
|
---|
| 251 | * see if we have this gid cached
|
---|
| 252 | */
|
---|
| 253 | ptr = gidtb[gid % GID_SZ];
|
---|
| 254 | if ((ptr != NULL) && (ptr->valid > 0) && (ptr->gid == gid)) {
|
---|
| 255 | /*
|
---|
| 256 | * have an entry for this gid
|
---|
| 257 | */
|
---|
| 258 | if (frc || (ptr->valid == VALID))
|
---|
| 259 | return(ptr->name);
|
---|
| 260 | return("");
|
---|
| 261 | }
|
---|
| 262 |
|
---|
| 263 | /*
|
---|
| 264 | * No entry for this gid, we will add it
|
---|
| 265 | */
|
---|
| 266 | if (!gropn) {
|
---|
| 267 | setgroupent(1);
|
---|
| 268 | ++gropn;
|
---|
| 269 | }
|
---|
| 270 | if (ptr == NULL)
|
---|
| 271 | ptr = gidtb[gid % GID_SZ] = (GIDC *)malloc(sizeof(GIDC));
|
---|
| 272 |
|
---|
| 273 | if ((gr = getgrgid(gid)) == NULL) {
|
---|
| 274 | /*
|
---|
| 275 | * no match for this gid in the local group file, put in
|
---|
| 276 | * a string that is the gid in numeric format
|
---|
| 277 | */
|
---|
| 278 | if (ptr == NULL)
|
---|
| 279 | return("");
|
---|
| 280 | ptr->gid = gid;
|
---|
| 281 | ptr->valid = INVALID;
|
---|
| 282 | # ifdef NET2_STAT
|
---|
| 283 | (void)snprintf(ptr->name, sizeof(ptr->name), "%u", gid);
|
---|
| 284 | # else
|
---|
| 285 | (void)snprintf(ptr->name, sizeof(ptr->name), "%lu",
|
---|
| 286 | (unsigned long)gid);
|
---|
| 287 | # endif
|
---|
| 288 | if (frc == 0)
|
---|
| 289 | return("");
|
---|
| 290 | } else {
|
---|
| 291 | /*
|
---|
| 292 | * there is an entry for this group in the group file
|
---|
| 293 | */
|
---|
| 294 | if (ptr == NULL)
|
---|
| 295 | return(gr->gr_name);
|
---|
| 296 | ptr->gid = gid;
|
---|
| 297 | (void)strncpy(ptr->name, gr->gr_name, GNMLEN - 1);
|
---|
| 298 | ptr->name[GNMLEN-1] = '\0';
|
---|
| 299 | ptr->valid = VALID;
|
---|
| 300 | }
|
---|
| 301 | return(ptr->name);
|
---|
| 302 | }
|
---|
| 303 |
|
---|
| 304 | /*
|
---|
| 305 | * uid_name()
|
---|
| 306 | * caches the uid for a given user name. We use a simple hash table.
|
---|
| 307 | * Return
|
---|
| 308 | * the uid (if any) for a user name, or a -1 if no match can be found
|
---|
| 309 | */
|
---|
| 310 |
|
---|
| 311 | int
|
---|
| 312 | uid_name(char *name, uid_t *uid)
|
---|
| 313 | {
|
---|
| 314 | struct passwd *pw;
|
---|
| 315 | UIDC *ptr;
|
---|
| 316 | int namelen;
|
---|
| 317 |
|
---|
| 318 | /*
|
---|
| 319 | * return -1 for mangled names
|
---|
| 320 | */
|
---|
| 321 | if (((namelen = strlen(name)) == 0) || (name[0] == '\0'))
|
---|
| 322 | return(-1);
|
---|
| 323 | if ((usrtb == NULL) && (usrtb_start() < 0))
|
---|
| 324 | return(-1);
|
---|
| 325 |
|
---|
| 326 | /*
|
---|
| 327 | * look up in hash table, if found and valid return the uid,
|
---|
| 328 | * if found and invalid, return a -1
|
---|
| 329 | */
|
---|
| 330 | ptr = usrtb[st_hash(name, namelen, UNM_SZ)];
|
---|
| 331 | if ((ptr != NULL) && (ptr->valid > 0) && !strcmp(name, ptr->name)) {
|
---|
| 332 | if (ptr->valid == INVALID)
|
---|
| 333 | return(-1);
|
---|
| 334 | *uid = ptr->uid;
|
---|
| 335 | return(0);
|
---|
| 336 | }
|
---|
| 337 |
|
---|
| 338 | if (!pwopn) {
|
---|
| 339 | setpassent(1);
|
---|
| 340 | ++pwopn;
|
---|
| 341 | }
|
---|
| 342 |
|
---|
| 343 | if (ptr == NULL)
|
---|
| 344 | ptr = usrtb[st_hash(name, namelen, UNM_SZ)] =
|
---|
| 345 | (UIDC *)malloc(sizeof(UIDC));
|
---|
| 346 |
|
---|
| 347 | /*
|
---|
| 348 | * no match, look it up, if no match store it as an invalid entry,
|
---|
| 349 | * or store the matching uid
|
---|
| 350 | */
|
---|
| 351 | if (ptr == NULL) {
|
---|
| 352 | if ((pw = getpwnam(name)) == NULL)
|
---|
| 353 | return(-1);
|
---|
| 354 | *uid = pw->pw_uid;
|
---|
| 355 | return(0);
|
---|
| 356 | }
|
---|
| 357 | (void)strncpy(ptr->name, name, UNMLEN - 1);
|
---|
| 358 | ptr->name[UNMLEN-1] = '\0';
|
---|
| 359 | if ((pw = getpwnam(name)) == NULL) {
|
---|
| 360 | ptr->valid = INVALID;
|
---|
| 361 | return(-1);
|
---|
| 362 | }
|
---|
| 363 | ptr->valid = VALID;
|
---|
| 364 | *uid = ptr->uid = pw->pw_uid;
|
---|
| 365 | return(0);
|
---|
| 366 | }
|
---|
| 367 |
|
---|
| 368 | /*
|
---|
| 369 | * gid_name()
|
---|
| 370 | * caches the gid for a given group name. We use a simple hash table.
|
---|
| 371 | * Return
|
---|
| 372 | * the gid (if any) for a group name, or a -1 if no match can be found
|
---|
| 373 | */
|
---|
| 374 |
|
---|
| 375 | int
|
---|
| 376 | gid_name(char *name, gid_t *gid)
|
---|
| 377 | {
|
---|
| 378 | struct group *gr;
|
---|
| 379 | GIDC *ptr;
|
---|
| 380 | int namelen;
|
---|
| 381 |
|
---|
| 382 | /*
|
---|
| 383 | * return -1 for mangled names
|
---|
| 384 | */
|
---|
| 385 | if (((namelen = strlen(name)) == 0) || (name[0] == '\0'))
|
---|
| 386 | return(-1);
|
---|
| 387 | if ((grptb == NULL) && (grptb_start() < 0))
|
---|
| 388 | return(-1);
|
---|
| 389 |
|
---|
| 390 | /*
|
---|
| 391 | * look up in hash table, if found and valid return the uid,
|
---|
| 392 | * if found and invalid, return a -1
|
---|
| 393 | */
|
---|
| 394 | ptr = grptb[st_hash(name, namelen, GID_SZ)];
|
---|
| 395 | if ((ptr != NULL) && (ptr->valid > 0) && !strcmp(name, ptr->name)) {
|
---|
| 396 | if (ptr->valid == INVALID)
|
---|
| 397 | return(-1);
|
---|
| 398 | *gid = ptr->gid;
|
---|
| 399 | return(0);
|
---|
| 400 | }
|
---|
| 401 |
|
---|
| 402 | if (!gropn) {
|
---|
| 403 | setgroupent(1);
|
---|
| 404 | ++gropn;
|
---|
| 405 | }
|
---|
| 406 | if (ptr == NULL)
|
---|
| 407 | ptr = grptb[st_hash(name, namelen, GID_SZ)] =
|
---|
| 408 | (GIDC *)malloc(sizeof(GIDC));
|
---|
| 409 |
|
---|
| 410 | /*
|
---|
| 411 | * no match, look it up, if no match store it as an invalid entry,
|
---|
| 412 | * or store the matching gid
|
---|
| 413 | */
|
---|
| 414 | if (ptr == NULL) {
|
---|
| 415 | if ((gr = getgrnam(name)) == NULL)
|
---|
| 416 | return(-1);
|
---|
| 417 | *gid = gr->gr_gid;
|
---|
| 418 | return(0);
|
---|
| 419 | }
|
---|
| 420 |
|
---|
| 421 | (void)strncpy(ptr->name, name, GNMLEN - 1);
|
---|
| 422 | ptr->name[GNMLEN-1] = '\0';
|
---|
| 423 | if ((gr = getgrnam(name)) == NULL) {
|
---|
| 424 | ptr->valid = INVALID;
|
---|
| 425 | return(-1);
|
---|
| 426 | }
|
---|
| 427 | ptr->valid = VALID;
|
---|
| 428 | *gid = ptr->gid = gr->gr_gid;
|
---|
| 429 | return(0);
|
---|
| 430 | }
|
---|