1 /* @(#)cache.c 8.1 (Berkeley) 5/31/93 */ 2 /* $NetBSD: pwcache.c,v 1.31 2010/03/23 20:28:59 drochner 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 /*- 38 * Copyright (c) 2002 The NetBSD Foundation, Inc. 39 * All rights reserved. 40 * 41 * Redistribution and use in source and binary forms, with or without 42 * modification, are permitted provided that the following conditions 43 * are met: 44 * 1. Redistributions of source code must retain the above copyright 45 * notice, this list of conditions and the following disclaimer. 46 * 2. Redistributions in binary form must reproduce the above copyright 47 * notice, this list of conditions and the following disclaimer in the 48 * documentation and/or other materials provided with the distribution. 49 * 50 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 51 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 52 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 53 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 54 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 55 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 56 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 57 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 58 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 59 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 60 * POSSIBILITY OF SUCH DAMAGE. 61 */ 62 63 #if HAVE_NBTOOL_CONFIG_H 64 #include "nbtool_config.h" 65 /* 66 * XXX Undefine the renames of these functions so that we don't 67 * XXX rename the versions found in the host's <pwd.h> by mistake! 68 */ 69 #undef group_from_gid 70 #undef user_from_uid 71 #endif 72 73 #ifndef EMBED_LIB_SRC 74 #include "namespace.h" 75 #endif 76 77 #include <sys/types.h> 78 #include <sys/param.h> 79 80 #include <assert.h> 81 #include <grp.h> 82 #include <pwd.h> 83 #include <stdio.h> 84 #include <stdlib.h> 85 #include <string.h> 86 #include <unistd.h> 87 #ifndef EMBED_LIB_SRC 88 #include "un-namespace.h" 89 #endif 90 91 #if HAVE_NBTOOL_CONFIG_H 92 /* XXX Now, re-apply the renaming that we undid above. */ 93 #define group_from_gid __nbcompat_group_from_gid 94 #define user_from_uid __nbcompat_user_from_uid 95 #endif 96 97 #ifdef __weak_alias 98 __weak_alias(user_from_uid,_user_from_uid) 99 __weak_alias(group_from_gid,_group_from_gid) 100 __weak_alias(pwcache_groupdb,_pwcache_groupdb) 101 #endif 102 103 #if !HAVE_PWCACHE_USERDB || HAVE_NBTOOL_CONFIG_H 104 #include "pwcache.h" 105 106 /* 107 * routines that control user, group, uid and gid caches (for the archive 108 * member print routine). 109 * IMPORTANT: 110 * these routines cache BOTH hits and misses, a major performance improvement 111 */ 112 113 /* 114 * function pointers to various name lookup routines. 115 * these may be changed as necessary. 116 */ 117 static int (*_pwcache_setgroupent)(int) = setgroupent; 118 static void (*_pwcache_endgrent)(void) = endgrent; 119 static struct group * (*_pwcache_getgrnam)(const char *) = getgrnam; 120 static struct group * (*_pwcache_getgrgid)(gid_t) = getgrgid; 121 static int (*_pwcache_setpassent)(int) = setpassent; 122 static void (*_pwcache_endpwent)(void) = endpwent; 123 static struct passwd * (*_pwcache_getpwnam)(const char *) = getpwnam; 124 static struct passwd * (*_pwcache_getpwuid)(uid_t) = getpwuid; 125 126 /* 127 * internal state 128 */ 129 static int pwopn; /* is password file open */ 130 static int gropn; /* is group file open */ 131 static UIDC **uidtb; /* uid to name cache */ 132 static GIDC **gidtb; /* gid to name cache */ 133 static UIDC **usrtb; /* user name to uid cache */ 134 static GIDC **grptb; /* group name to gid cache */ 135 136 static int uidtb_fail; /* uidtb_start() failed ? */ 137 static int gidtb_fail; /* gidtb_start() failed ? */ 138 static int usrtb_fail; /* usrtb_start() failed ? */ 139 static int grptb_fail; /* grptb_start() failed ? */ 140 141 142 static u_int st_hash(const char *, size_t, int); 143 static int uidtb_start(void); 144 static int gidtb_start(void); 145 static int usrtb_start(void); 146 static int grptb_start(void); 147 148 149 static u_int 150 st_hash(const char *name, size_t len, int tabsz) 151 { 152 u_int key = 0; 153 154 _DIAGASSERT(name != NULL); 155 156 while (len--) { 157 key += *name++; 158 key = (key << 8) | (key >> 24); 159 } 160 161 return (key % tabsz); 162 } 163 164 /* 165 * uidtb_start 166 * creates an an empty uidtb 167 * Return: 168 * 0 if ok, -1 otherwise 169 */ 170 static int 171 uidtb_start(void) 172 { 173 174 if (uidtb != NULL) 175 return (0); 176 if (uidtb_fail) 177 return (-1); 178 if ((uidtb = (UIDC **)calloc(UID_SZ, sizeof(UIDC *))) == NULL) { 179 ++uidtb_fail; 180 return (-1); 181 } 182 return (0); 183 } 184 185 /* 186 * gidtb_start 187 * creates an an empty gidtb 188 * Return: 189 * 0 if ok, -1 otherwise 190 */ 191 static int 192 gidtb_start(void) 193 { 194 195 if (gidtb != NULL) 196 return (0); 197 if (gidtb_fail) 198 return (-1); 199 if ((gidtb = (GIDC **)calloc(GID_SZ, sizeof(GIDC *))) == NULL) { 200 ++gidtb_fail; 201 return (-1); 202 } 203 return (0); 204 } 205 206 /* 207 * usrtb_start 208 * creates an an empty usrtb 209 * Return: 210 * 0 if ok, -1 otherwise 211 */ 212 static int 213 usrtb_start(void) 214 { 215 216 if (usrtb != NULL) 217 return (0); 218 if (usrtb_fail) 219 return (-1); 220 if ((usrtb = (UIDC **)calloc(UNM_SZ, sizeof(UIDC *))) == NULL) { 221 ++usrtb_fail; 222 return (-1); 223 } 224 return (0); 225 } 226 227 /* 228 * grptb_start 229 * creates an an empty grptb 230 * Return: 231 * 0 if ok, -1 otherwise 232 */ 233 static int 234 grptb_start(void) 235 { 236 237 if (grptb != NULL) 238 return (0); 239 if (grptb_fail) 240 return (-1); 241 if ((grptb = (GIDC **)calloc(GNM_SZ, sizeof(GIDC *))) == NULL) { 242 ++grptb_fail; 243 return (-1); 244 } 245 return (0); 246 } 247 248 /* 249 * user_from_uid() 250 * caches the name (if any) for the uid. If noname clear, we always 251 * return the stored name (if valid or invalid match). 252 * We use a simple hash table. 253 * Return 254 * Pointer to stored name (or a empty string) 255 */ 256 const char * 257 user_from_uid(uid_t uid, int noname) 258 { 259 struct passwd *pw; 260 UIDC *ptr, **pptr; 261 262 if ((uidtb == NULL) && (uidtb_start() < 0)) 263 return (NULL); 264 265 /* 266 * see if we have this uid cached 267 */ 268 pptr = uidtb + (uid % UID_SZ); 269 ptr = *pptr; 270 271 if ((ptr != NULL) && (ptr->valid > 0) && (ptr->uid == uid)) { 272 /* 273 * have an entry for this uid 274 */ 275 if (!noname || (ptr->valid == VALID)) 276 return (ptr->name); 277 return (NULL); 278 } 279 280 /* 281 * No entry for this uid, we will add it 282 */ 283 if (!pwopn) { 284 if (_pwcache_setpassent != NULL) 285 (*_pwcache_setpassent)(1); 286 ++pwopn; 287 } 288 289 if (ptr == NULL) 290 *pptr = ptr = (UIDC *)malloc(sizeof(UIDC)); 291 292 if ((pw = (*_pwcache_getpwuid)(uid)) == NULL) { 293 /* 294 * no match for this uid in the local password file 295 * a string that is the uid in numeric format 296 */ 297 if (ptr == NULL) 298 return (NULL); 299 ptr->uid = uid; 300 snprintf(ptr->name, UNMLEN, "%lu", (long) uid); 301 ptr->valid = INVALID; 302 if (noname) 303 return (NULL); 304 } else { 305 /* 306 * there is an entry for this uid in the password file 307 */ 308 if (ptr == NULL) 309 return (pw->pw_name); 310 ptr->uid = uid; 311 strlcpy(ptr->name, pw->pw_name, UNMLEN); 312 ptr->valid = VALID; 313 } 314 return (ptr->name); 315 } 316 317 /* 318 * group_from_gid() 319 * caches the name (if any) for the gid. If noname clear, we always 320 * return the stored name (if valid or invalid match). 321 * We use a simple hash table. 322 * Return 323 * Pointer to stored name (or a empty string) 324 */ 325 const char * 326 group_from_gid(gid_t gid, int noname) 327 { 328 struct group *gr; 329 GIDC *ptr, **pptr; 330 331 if ((gidtb == NULL) && (gidtb_start() < 0)) 332 return (NULL); 333 334 /* 335 * see if we have this gid cached 336 */ 337 pptr = gidtb + (gid % GID_SZ); 338 ptr = *pptr; 339 340 if ((ptr != NULL) && (ptr->valid > 0) && (ptr->gid == gid)) { 341 /* 342 * have an entry for this gid 343 */ 344 if (!noname || (ptr->valid == VALID)) 345 return (ptr->name); 346 return (NULL); 347 } 348 349 /* 350 * No entry for this gid, we will add it 351 */ 352 if (!gropn) { 353 if (_pwcache_setgroupent != NULL) 354 (*_pwcache_setgroupent)(1); 355 ++gropn; 356 } 357 358 if (ptr == NULL) 359 *pptr = ptr = (GIDC *)malloc(sizeof(GIDC)); 360 361 if ((gr = (*_pwcache_getgrgid)(gid)) == NULL) { 362 /* 363 * no match for this gid in the local group file, put in 364 * a string that is the gid in numberic format 365 */ 366 if (ptr == NULL) 367 return (NULL); 368 ptr->gid = gid; 369 snprintf(ptr->name, GNMLEN, "%lu", (long) gid); 370 ptr->valid = INVALID; 371 if (noname) 372 return (NULL); 373 } else { 374 /* 375 * there is an entry for this group in the group file 376 */ 377 if (ptr == NULL) 378 return (gr->gr_name); 379 ptr->gid = gid; 380 strlcpy(ptr->name, gr->gr_name, GNMLEN); 381 ptr->valid = VALID; 382 } 383 return (ptr->name); 384 } 385 386 /* 387 * uid_from_user() 388 * caches the uid for a given user name. We use a simple hash table. 389 * Return 390 * the uid (if any) for a user name, or a -1 if no match can be found 391 */ 392 int 393 uid_from_user(const char *name, uid_t *uid) 394 { 395 struct passwd *pw; 396 UIDC *ptr, **pptr; 397 size_t namelen; 398 399 /* 400 * return -1 for mangled names 401 */ 402 if (name == NULL || ((namelen = strlen(name)) == 0)) 403 return (-1); 404 if ((usrtb == NULL) && (usrtb_start() < 0)) 405 return (-1); 406 407 /* 408 * look up in hash table, if found and valid return the uid, 409 * if found and invalid, return a -1 410 */ 411 pptr = usrtb + st_hash(name, namelen, UNM_SZ); 412 ptr = *pptr; 413 414 if ((ptr != NULL) && (ptr->valid > 0) && !strcmp(name, ptr->name)) { 415 if (ptr->valid == INVALID) 416 return (-1); 417 *uid = ptr->uid; 418 return (0); 419 } 420 421 if (!pwopn) { 422 if (_pwcache_setpassent != NULL) 423 (*_pwcache_setpassent)(1); 424 ++pwopn; 425 } 426 427 if (ptr == NULL) 428 *pptr = ptr = (UIDC *)malloc(sizeof(UIDC)); 429 430 /* 431 * no match, look it up, if no match store it as an invalid entry, 432 * or store the matching uid 433 */ 434 if (ptr == NULL) { 435 if ((pw = (*_pwcache_getpwnam)(name)) == NULL) 436 return (-1); 437 *uid = pw->pw_uid; 438 return (0); 439 } 440 strlcpy(ptr->name, name, UNMLEN); 441 if ((pw = (*_pwcache_getpwnam)(name)) == NULL) { 442 ptr->valid = INVALID; 443 return (-1); 444 } 445 ptr->valid = VALID; 446 *uid = ptr->uid = pw->pw_uid; 447 return (0); 448 } 449 450 /* 451 * gid_from_group() 452 * caches the gid for a given group name. We use a simple hash table. 453 * Return 454 * the gid (if any) for a group name, or a -1 if no match can be found 455 */ 456 int 457 gid_from_group(const char *name, gid_t *gid) 458 { 459 struct group *gr; 460 GIDC *ptr, **pptr; 461 size_t namelen; 462 463 /* 464 * return -1 for mangled names 465 */ 466 if (name == NULL || ((namelen = strlen(name)) == 0)) 467 return (-1); 468 if ((grptb == NULL) && (grptb_start() < 0)) 469 return (-1); 470 471 /* 472 * look up in hash table, if found and valid return the uid, 473 * if found and invalid, return a -1 474 */ 475 pptr = grptb + st_hash(name, namelen, GID_SZ); 476 ptr = *pptr; 477 478 if ((ptr != NULL) && (ptr->valid > 0) && !strcmp(name, ptr->name)) { 479 if (ptr->valid == INVALID) 480 return (-1); 481 *gid = ptr->gid; 482 return (0); 483 } 484 485 if (!gropn) { 486 if (_pwcache_setgroupent != NULL) 487 (*_pwcache_setgroupent)(1); 488 ++gropn; 489 } 490 491 if (ptr == NULL) 492 *pptr = ptr = (GIDC *)malloc(sizeof(GIDC)); 493 494 /* 495 * no match, look it up, if no match store it as an invalid entry, 496 * or store the matching gid 497 */ 498 if (ptr == NULL) { 499 if ((gr = (*_pwcache_getgrnam)(name)) == NULL) 500 return (-1); 501 *gid = gr->gr_gid; 502 return (0); 503 } 504 505 strlcpy(ptr->name, name, GNMLEN); 506 if ((gr = (*_pwcache_getgrnam)(name)) == NULL) { 507 ptr->valid = INVALID; 508 return (-1); 509 } 510 ptr->valid = VALID; 511 *gid = ptr->gid = gr->gr_gid; 512 return (0); 513 } 514 515 #define FLUSHTB(arr, len, fail) \ 516 do { \ 517 if (arr != NULL) { \ 518 for (i = 0; i < len; i++) \ 519 if (arr[i] != NULL) \ 520 free(arr[i]); \ 521 arr = NULL; \ 522 } \ 523 fail = 0; \ 524 } while (/* CONSTCOND */0); 525 526 int 527 pwcache_userdb( 528 int (*a_setpassent)(int), 529 void (*a_endpwent)(void), 530 struct passwd * (*a_getpwnam)(const char *), 531 struct passwd * (*a_getpwuid)(uid_t)) 532 { 533 int i; 534 535 /* a_setpassent and a_endpwent may be NULL */ 536 if (a_getpwnam == NULL || a_getpwuid == NULL) 537 return (-1); 538 539 if (_pwcache_endpwent != NULL) 540 (*_pwcache_endpwent)(); 541 FLUSHTB(uidtb, UID_SZ, uidtb_fail); 542 FLUSHTB(usrtb, UNM_SZ, usrtb_fail); 543 pwopn = 0; 544 _pwcache_setpassent = a_setpassent; 545 _pwcache_endpwent = a_endpwent; 546 _pwcache_getpwnam = a_getpwnam; 547 _pwcache_getpwuid = a_getpwuid; 548 549 return (0); 550 } 551 552 int 553 pwcache_groupdb( 554 int (*a_setgroupent)(int), 555 void (*a_endgrent)(void), 556 struct group * (*a_getgrnam)(const char *), 557 struct group * (*a_getgrgid)(gid_t)) 558 { 559 int i; 560 561 /* a_setgroupent and a_endgrent may be NULL */ 562 if (a_getgrnam == NULL || a_getgrgid == NULL) 563 return (-1); 564 565 if (_pwcache_endgrent != NULL) 566 (*_pwcache_endgrent)(); 567 FLUSHTB(gidtb, GID_SZ, gidtb_fail); 568 FLUSHTB(grptb, GNM_SZ, grptb_fail); 569 gropn = 0; 570 _pwcache_setgroupent = a_setgroupent; 571 _pwcache_endgrent = a_endgrent; 572 _pwcache_getgrnam = a_getgrnam; 573 _pwcache_getgrgid = a_getgrgid; 574 575 return (0); 576 } 577 578 579 #ifdef TEST_PWCACHE 580 581 struct passwd * 582 test_getpwnam(const char *name) 583 { 584 static struct passwd foo; 585 586 memset(&foo, 0, sizeof(foo)); 587 if (strcmp(name, "toor") == 0) { 588 foo.pw_uid = 666; 589 return &foo; 590 } 591 return (getpwnam(name)); 592 } 593 594 int 595 main(int argc, char *argv[]) 596 { 597 uid_t u; 598 int r, i; 599 600 printf("pass 1 (default userdb)\n"); 601 for (i = 1; i < argc; i++) { 602 printf("i: %d, pwopn %d usrtb_fail %d usrtb %p\n", 603 i, pwopn, usrtb_fail, usrtb); 604 r = uid_from_user(argv[i], &u); 605 if (r == -1) 606 printf(" uid_from_user %s: failed\n", argv[i]); 607 else 608 printf(" uid_from_user %s: %d\n", argv[i], u); 609 } 610 printf("pass 1 finish: pwopn %d usrtb_fail %d usrtb %p\n", 611 pwopn, usrtb_fail, usrtb); 612 613 puts(""); 614 printf("pass 2 (replacement userdb)\n"); 615 printf("pwcache_userdb returned %d\n", 616 pwcache_userdb(setpassent, test_getpwnam, getpwuid)); 617 printf("pwopn %d usrtb_fail %d usrtb %p\n", pwopn, usrtb_fail, usrtb); 618 619 for (i = 1; i < argc; i++) { 620 printf("i: %d, pwopn %d usrtb_fail %d usrtb %p\n", 621 i, pwopn, usrtb_fail, usrtb); 622 u = -1; 623 r = uid_from_user(argv[i], &u); 624 if (r == -1) 625 printf(" uid_from_user %s: failed\n", argv[i]); 626 else 627 printf(" uid_from_user %s: %d\n", argv[i], u); 628 } 629 printf("pass 2 finish: pwopn %d usrtb_fail %d usrtb %p\n", 630 pwopn, usrtb_fail, usrtb); 631 632 puts(""); 633 printf("pass 3 (null pointers)\n"); 634 printf("pwcache_userdb returned %d\n", 635 pwcache_userdb(NULL, NULL, NULL)); 636 637 return (0); 638 } 639 #endif /* TEST_PWCACHE */ 640 #endif /* !HAVE_PWCACHE_USERDB */ 641