1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1992 Keith Muller. 5 * Copyright (c) 1992, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * This code is derived from software contributed to Berkeley by 9 * Keith Muller of the University of California, San Diego. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #ifndef lint 37 #if 0 38 static char sccsid[] = "@(#)cache.c 8.1 (Berkeley) 5/31/93"; 39 #endif 40 #endif /* not lint */ 41 #include <sys/cdefs.h> 42 #include <sys/types.h> 43 #include <sys/stat.h> 44 #include <string.h> 45 #include <stdio.h> 46 #include <pwd.h> 47 #include <grp.h> 48 #include <stdlib.h> 49 #include "pax.h" 50 #include "cache.h" 51 #include "extern.h" 52 53 /* 54 * routines that control user, group, uid and gid caches (for the archive 55 * member print routine). 56 * IMPORTANT: 57 * these routines cache BOTH hits and misses, a major performance improvement 58 */ 59 60 static int pwopn = 0; /* is password file open */ 61 static int gropn = 0; /* is group file open */ 62 static UIDC **uidtb = NULL; /* uid to name cache */ 63 static GIDC **gidtb = NULL; /* gid to name cache */ 64 static UIDC **usrtb = NULL; /* user name to uid cache */ 65 static GIDC **grptb = NULL; /* group name to gid cache */ 66 67 /* 68 * uidtb_start 69 * creates an empty uidtb 70 * Return: 71 * 0 if ok, -1 otherwise 72 */ 73 74 int 75 uidtb_start(void) 76 { 77 static int fail = 0; 78 79 if (uidtb != NULL) 80 return(0); 81 if (fail) 82 return(-1); 83 if ((uidtb = (UIDC **)calloc(UID_SZ, sizeof(UIDC *))) == NULL) { 84 ++fail; 85 paxwarn(1, "Unable to allocate memory for user id cache table"); 86 return(-1); 87 } 88 return(0); 89 } 90 91 /* 92 * gidtb_start 93 * creates an empty gidtb 94 * Return: 95 * 0 if ok, -1 otherwise 96 */ 97 98 int 99 gidtb_start(void) 100 { 101 static int fail = 0; 102 103 if (gidtb != NULL) 104 return(0); 105 if (fail) 106 return(-1); 107 if ((gidtb = (GIDC **)calloc(GID_SZ, sizeof(GIDC *))) == NULL) { 108 ++fail; 109 paxwarn(1, "Unable to allocate memory for group id cache table"); 110 return(-1); 111 } 112 return(0); 113 } 114 115 /* 116 * usrtb_start 117 * creates an empty usrtb 118 * Return: 119 * 0 if ok, -1 otherwise 120 */ 121 122 int 123 usrtb_start(void) 124 { 125 static int fail = 0; 126 127 if (usrtb != NULL) 128 return(0); 129 if (fail) 130 return(-1); 131 if ((usrtb = (UIDC **)calloc(UNM_SZ, sizeof(UIDC *))) == NULL) { 132 ++fail; 133 paxwarn(1, "Unable to allocate memory for user name cache table"); 134 return(-1); 135 } 136 return(0); 137 } 138 139 /* 140 * grptb_start 141 * creates an empty grptb 142 * Return: 143 * 0 if ok, -1 otherwise 144 */ 145 146 int 147 grptb_start(void) 148 { 149 static int fail = 0; 150 151 if (grptb != NULL) 152 return(0); 153 if (fail) 154 return(-1); 155 if ((grptb = (GIDC **)calloc(GNM_SZ, sizeof(GIDC *))) == NULL) { 156 ++fail; 157 paxwarn(1,"Unable to allocate memory for group name cache table"); 158 return(-1); 159 } 160 return(0); 161 } 162 163 /* 164 * name_uid() 165 * caches the name (if any) for the uid. If frc set, we always return the 166 * the stored name (if valid or invalid match). We use a simple hash table. 167 * Return 168 * Pointer to stored name (or an empty string). 169 */ 170 171 const char * 172 name_uid(uid_t uid, int frc) 173 { 174 struct passwd *pw; 175 UIDC *ptr; 176 177 if ((uidtb == NULL) && (uidtb_start() < 0)) 178 return(""); 179 180 /* 181 * see if we have this uid cached 182 */ 183 ptr = uidtb[uid % UID_SZ]; 184 if ((ptr != NULL) && (ptr->valid > 0) && (ptr->uid == uid)) { 185 /* 186 * have an entry for this uid 187 */ 188 if (frc || (ptr->valid == VALID)) 189 return(ptr->name); 190 return(""); 191 } 192 193 /* 194 * No entry for this uid, we will add it 195 */ 196 if (!pwopn) { 197 setpassent(1); 198 ++pwopn; 199 } 200 if (ptr == NULL) 201 ptr = uidtb[uid % UID_SZ] = (UIDC *)malloc(sizeof(UIDC)); 202 203 if ((pw = getpwuid(uid)) == NULL) { 204 /* 205 * no match for this uid in the local password file 206 * a string that is the uid in numeric format 207 */ 208 if (ptr == NULL) 209 return(""); 210 ptr->uid = uid; 211 ptr->valid = INVALID; 212 (void)snprintf(ptr->name, sizeof(ptr->name), "%lu", 213 (unsigned long)uid); 214 if (frc == 0) 215 return(""); 216 } else { 217 /* 218 * there is an entry for this uid in the password file 219 */ 220 if (ptr == NULL) 221 return(pw->pw_name); 222 ptr->uid = uid; 223 (void)strncpy(ptr->name, pw->pw_name, UNMLEN - 1); 224 ptr->name[UNMLEN-1] = '\0'; 225 ptr->valid = VALID; 226 } 227 return(ptr->name); 228 } 229 230 /* 231 * name_gid() 232 * caches the name (if any) for the gid. If frc set, we always return the 233 * the stored name (if valid or invalid match). We use a simple hash table. 234 * Return 235 * Pointer to stored name (or an empty string). 236 */ 237 238 const char * 239 name_gid(gid_t gid, int frc) 240 { 241 struct group *gr; 242 GIDC *ptr; 243 244 if ((gidtb == NULL) && (gidtb_start() < 0)) 245 return(""); 246 247 /* 248 * see if we have this gid cached 249 */ 250 ptr = gidtb[gid % GID_SZ]; 251 if ((ptr != NULL) && (ptr->valid > 0) && (ptr->gid == gid)) { 252 /* 253 * have an entry for this gid 254 */ 255 if (frc || (ptr->valid == VALID)) 256 return(ptr->name); 257 return(""); 258 } 259 260 /* 261 * No entry for this gid, we will add it 262 */ 263 if (!gropn) { 264 setgroupent(1); 265 ++gropn; 266 } 267 if (ptr == NULL) 268 ptr = gidtb[gid % GID_SZ] = (GIDC *)malloc(sizeof(GIDC)); 269 270 if ((gr = getgrgid(gid)) == NULL) { 271 /* 272 * no match for this gid in the local group file, put in 273 * a string that is the gid in numeric format 274 */ 275 if (ptr == NULL) 276 return(""); 277 ptr->gid = gid; 278 ptr->valid = INVALID; 279 (void)snprintf(ptr->name, sizeof(ptr->name), "%lu", 280 (unsigned long)gid); 281 if (frc == 0) 282 return(""); 283 } else { 284 /* 285 * there is an entry for this group in the group file 286 */ 287 if (ptr == NULL) 288 return(gr->gr_name); 289 ptr->gid = gid; 290 (void)strncpy(ptr->name, gr->gr_name, GNMLEN - 1); 291 ptr->name[GNMLEN-1] = '\0'; 292 ptr->valid = VALID; 293 } 294 return(ptr->name); 295 } 296 297 /* 298 * uid_name() 299 * caches the uid for a given user name. We use a simple hash table. 300 * Return 301 * the uid (if any) for a user name, or a -1 if no match can be found 302 */ 303 304 int 305 uid_name(char *name, uid_t *uid) 306 { 307 struct passwd *pw; 308 UIDC *ptr; 309 int namelen; 310 311 /* 312 * return -1 for mangled names 313 */ 314 if (((namelen = strlen(name)) == 0) || (name[0] == '\0')) 315 return(-1); 316 if ((usrtb == NULL) && (usrtb_start() < 0)) 317 return(-1); 318 319 /* 320 * look up in hash table, if found and valid return the uid, 321 * if found and invalid, return a -1 322 */ 323 ptr = usrtb[st_hash(name, namelen, UNM_SZ)]; 324 if ((ptr != NULL) && (ptr->valid > 0) && !strcmp(name, ptr->name)) { 325 if (ptr->valid == INVALID) 326 return(-1); 327 *uid = ptr->uid; 328 return(0); 329 } 330 331 if (!pwopn) { 332 setpassent(1); 333 ++pwopn; 334 } 335 336 if (ptr == NULL) 337 ptr = usrtb[st_hash(name, namelen, UNM_SZ)] = 338 (UIDC *)malloc(sizeof(UIDC)); 339 340 /* 341 * no match, look it up, if no match store it as an invalid entry, 342 * or store the matching uid 343 */ 344 if (ptr == NULL) { 345 if ((pw = getpwnam(name)) == NULL) 346 return(-1); 347 *uid = pw->pw_uid; 348 return(0); 349 } 350 (void)strncpy(ptr->name, name, UNMLEN - 1); 351 ptr->name[UNMLEN-1] = '\0'; 352 if ((pw = getpwnam(name)) == NULL) { 353 ptr->valid = INVALID; 354 return(-1); 355 } 356 ptr->valid = VALID; 357 *uid = ptr->uid = pw->pw_uid; 358 return(0); 359 } 360 361 /* 362 * gid_name() 363 * caches the gid for a given group name. We use a simple hash table. 364 * Return 365 * the gid (if any) for a group name, or a -1 if no match can be found 366 */ 367 368 int 369 gid_name(char *name, gid_t *gid) 370 { 371 struct group *gr; 372 GIDC *ptr; 373 int namelen; 374 375 /* 376 * return -1 for mangled names 377 */ 378 if (((namelen = strlen(name)) == 0) || (name[0] == '\0')) 379 return(-1); 380 if ((grptb == NULL) && (grptb_start() < 0)) 381 return(-1); 382 383 /* 384 * look up in hash table, if found and valid return the uid, 385 * if found and invalid, return a -1 386 */ 387 ptr = grptb[st_hash(name, namelen, GID_SZ)]; 388 if ((ptr != NULL) && (ptr->valid > 0) && !strcmp(name, ptr->name)) { 389 if (ptr->valid == INVALID) 390 return(-1); 391 *gid = ptr->gid; 392 return(0); 393 } 394 395 if (!gropn) { 396 setgroupent(1); 397 ++gropn; 398 } 399 if (ptr == NULL) 400 ptr = grptb[st_hash(name, namelen, GID_SZ)] = 401 (GIDC *)malloc(sizeof(GIDC)); 402 403 /* 404 * no match, look it up, if no match store it as an invalid entry, 405 * or store the matching gid 406 */ 407 if (ptr == NULL) { 408 if ((gr = getgrnam(name)) == NULL) 409 return(-1); 410 *gid = gr->gr_gid; 411 return(0); 412 } 413 414 (void)strncpy(ptr->name, name, GNMLEN - 1); 415 ptr->name[GNMLEN-1] = '\0'; 416 if ((gr = getgrnam(name)) == NULL) { 417 ptr->valid = INVALID; 418 return(-1); 419 } 420 ptr->valid = VALID; 421 *gid = ptr->gid = gr->gr_gid; 422 return(0); 423 } 424