1 /* $OpenBSD: ldconfig.c,v 1.27 2010/03/30 17:42:50 zinovik Exp $ */ 2 3 /* 4 * Copyright (c) 1993,1995 Paul Kranenburg 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by Paul Kranenburg. 18 * 4. The name of the author may not be used to endorse or promote products 19 * derived from this software without specific prior written permission 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 #include <sys/param.h> 34 #include <sys/types.h> 35 #include <sys/stat.h> 36 #include <sys/file.h> 37 #include <sys/time.h> 38 #include <sys/mman.h> 39 #include <sys/resource.h> 40 #include <ctype.h> 41 #include <dirent.h> 42 #include <err.h> 43 #include <errno.h> 44 #include <fcntl.h> 45 #include <ar.h> 46 #include <ranlib.h> 47 #include <a.out.h> 48 #include <stab.h> 49 #include <stdio.h> 50 #include <stdlib.h> 51 #include <string.h> 52 #include <unistd.h> 53 54 #include "prebind.h" 55 56 #include "ld.h" 57 58 #undef major 59 #undef minor 60 61 extern char *__progname; 62 63 int verbose; 64 static int delete; 65 static int doprebind; 66 static int nostd; 67 static int justread; 68 int merge; 69 int safe; 70 static int rescan; 71 static int unconfig; 72 73 struct shlib_list { 74 /* Internal list of shared libraries found */ 75 char *name; 76 char *path; 77 int dewey[MAXDEWEY]; 78 int ndewey; 79 #define major dewey[0] 80 #define minor dewey[1] 81 struct shlib_list *next; 82 }; 83 84 static struct shlib_list *shlib_head = NULL, **shlib_tail = &shlib_head; 85 static char *dir_list; 86 87 static void enter(char *, char *, char *, int *, int); 88 static int dodir(char *, int); 89 static int buildhints(void); 90 static int readhints(void); 91 static void listhints(void); 92 93 void 94 usage(void) 95 { 96 fprintf(stderr, 97 "usage: %s [-DmPRrSsUv] [path ...]\n", __progname); 98 exit(1); 99 } 100 101 int 102 main(int argc, char *argv[]) 103 { 104 int i, c; 105 int rval = 0; 106 107 while ((c = getopt(argc, argv, "DmPrRsSUv")) != -1) { 108 switch (c) { 109 case 'R': 110 rescan = 1; 111 break; 112 case 'U': 113 rescan = unconfig = 1; 114 break; 115 case 'm': 116 merge = 1; 117 break; 118 case 'r': 119 justread = 1; 120 break; 121 case 's': 122 nostd = 1; 123 break; 124 case 'S': 125 safe = 1; 126 break; 127 case 'v': 128 verbose = 1; 129 break; 130 case 'D': 131 delete = 1; 132 break; 133 case 'P': 134 doprebind = 1; 135 break; 136 default: 137 usage(); 138 break; 139 } 140 } 141 142 if (unconfig && merge) 143 errx(1, "cannot use -U with -m"); 144 145 dir_list = xmalloc(1); 146 *dir_list = '\0'; 147 148 if (justread || merge || rescan) { 149 if ((rval = readhints()) != 0) 150 return rval; 151 if (justread) { 152 listhints(); 153 return 0; 154 } 155 add_search_path(dir_list); 156 dir_list = xrealloc(dir_list, 1); 157 *dir_list = '\0'; 158 } else if (!nostd) 159 std_search_path(); 160 161 if (delete) { 162 if (rescan || unconfig || merge || justread || nostd || doprebind) 163 errx(1, "cannot mix -U -R -r -s -P options with -D"); 164 exit(prebind_delete(&argv[optind])); 165 } else if (doprebind) { 166 if (rescan || unconfig || justread || nostd) 167 errx(1, "cannot mix other options with -P"); 168 exit(prebind(&argv[optind])); 169 } 170 171 if (unconfig) { 172 if (optind < argc) 173 for (i = optind; i < argc; i++) 174 remove_search_dir(argv[i]); 175 else { 176 i = 0; 177 while (i < n_search_dirs) { 178 if (access(search_dirs[i], R_OK) < 0) 179 remove_search_dir(search_dirs[i]); 180 else 181 i++; 182 } 183 } 184 } else 185 for (i = optind; i < argc; i++) 186 add_search_dir(argv[i]); 187 188 for (i = 0; i < n_search_dirs; i++) { 189 char *cp = concat(dir_list, *dir_list?":":"", search_dirs[i]); 190 191 free(dir_list); 192 dir_list = cp; 193 rval |= dodir(search_dirs[i], 0); 194 } 195 196 rval |= buildhints(); 197 198 return rval; 199 } 200 201 int 202 dodir(char *dir, int silent) 203 { 204 DIR *dd; 205 struct dirent *dp; 206 char name[MAXPATHLEN]; 207 int dewey[MAXDEWEY], ndewey; 208 209 if ((dd = opendir(dir)) == NULL) { 210 if (!silent || errno != ENOENT) 211 warn("%s", dir); 212 return -1; 213 } 214 215 while ((dp = readdir(dd)) != NULL) { 216 size_t n; 217 char *cp; 218 219 /* Check for `lib' prefix */ 220 if (dp->d_name[0] != 'l' || 221 dp->d_name[1] != 'i' || 222 dp->d_name[2] != 'b') 223 continue; 224 225 /* Copy the entry minus prefix */ 226 (void)strlcpy(name, dp->d_name + 3, sizeof name); 227 n = strlen(name); 228 if (n < 4) 229 continue; 230 231 /* Find ".so." in name */ 232 for (cp = name + n - 4; cp > name; --cp) { 233 if (cp[0] == '.' && 234 cp[1] == 's' && 235 cp[2] == 'o' && 236 cp[3] == '.') 237 break; 238 } 239 if (cp <= name) 240 continue; 241 242 *cp = '\0'; 243 if (!isdigit(*(cp+4))) 244 continue; 245 246 bzero((caddr_t)dewey, sizeof(dewey)); 247 ndewey = getdewey(dewey, cp + 4); 248 enter(dir, dp->d_name, name, dewey, ndewey); 249 } 250 closedir(dd); 251 return 0; 252 } 253 254 static void 255 enter(char *dir, char *file, char *name, int dewey[], int ndewey) 256 { 257 struct shlib_list *shp; 258 259 for (shp = shlib_head; shp; shp = shp->next) { 260 if (strcmp(name, shp->name) != 0 || major != shp->major) 261 continue; 262 263 /* Name matches existing entry */ 264 if (cmpndewey(dewey, ndewey, shp->dewey, shp->ndewey) > 0) { 265 266 /* Update this entry with higher versioned lib */ 267 if (verbose) 268 printf("Updating lib%s.%d.%d to %s/%s\n", 269 shp->name, shp->major, shp->minor, 270 dir, file); 271 272 free(shp->name); 273 shp->name = xstrdup(name); 274 free(shp->path); 275 shp->path = concat(dir, "/", file); 276 bcopy(dewey, shp->dewey, sizeof(shp->dewey)); 277 shp->ndewey = ndewey; 278 } 279 break; 280 } 281 282 if (shp) 283 /* Name exists: older version or just updated */ 284 return; 285 286 /* Allocate new list element */ 287 if (verbose) 288 printf("Adding %s/%s\n", dir, file); 289 290 shp = (struct shlib_list *)xmalloc(sizeof *shp); 291 shp->name = xstrdup(name); 292 shp->path = concat(dir, "/", file); 293 bcopy(dewey, shp->dewey, sizeof(shp->dewey)); 294 shp->ndewey = ndewey; 295 shp->next = NULL; 296 297 *shlib_tail = shp; 298 shlib_tail = &shp->next; 299 } 300 301 302 #if DEBUG 303 /* test */ 304 #undef _PATH_LD_HINTS 305 #define _PATH_LD_HINTS "./ld.so.hints" 306 #endif 307 308 static int 309 hinthash(char *cp, int vmajor, int vminor) 310 { 311 int k = 0; 312 313 while (*cp) 314 k = (((k << 1) + (k >> 14)) ^ (*cp++)) & 0x3fff; 315 316 k = (((k << 1) + (k >> 14)) ^ (vmajor*257)) & 0x3fff; 317 #if 0 318 k = (((k << 1) + (k >> 14)) ^ (vminor*167)) & 0x3fff; 319 #endif 320 321 return k; 322 } 323 324 int 325 buildhints(void) 326 { 327 int strtab_sz = 0, nhints = 0, fd, i, ret = -1, str_index = 0; 328 struct hints_bucket *blist; 329 struct hints_header hdr; 330 struct shlib_list *shp; 331 char *strtab, *tmpfilenam; 332 size_t n; 333 334 for (shp = shlib_head; shp; shp = shp->next) { 335 strtab_sz += 1 + strlen(shp->name); 336 strtab_sz += 1 + strlen(shp->path); 337 nhints++; 338 } 339 340 /* Fill hints file header */ 341 hdr.hh_magic = HH_MAGIC; 342 hdr.hh_version = LD_HINTS_VERSION_2; 343 hdr.hh_nbucket = 1 * nhints; 344 n = hdr.hh_nbucket * sizeof(struct hints_bucket); 345 hdr.hh_hashtab = sizeof(struct hints_header); 346 hdr.hh_strtab = hdr.hh_hashtab + n; 347 hdr.hh_dirlist = strtab_sz; 348 strtab_sz += 1 + strlen(dir_list); 349 hdr.hh_strtab_sz = strtab_sz; 350 hdr.hh_ehints = hdr.hh_strtab + hdr.hh_strtab_sz; 351 352 if (verbose) 353 printf("Totals: entries %d, buckets %ld, string size %d\n", 354 nhints, hdr.hh_nbucket, strtab_sz); 355 356 /* Allocate buckets and string table */ 357 blist = (struct hints_bucket *)xmalloc(n); 358 bzero(blist, n); 359 for (i = 0; i < hdr.hh_nbucket; i++) 360 /* Empty all buckets */ 361 blist[i].hi_next = -1; 362 363 strtab = xmalloc(strtab_sz); 364 365 /* Enter all */ 366 for (shp = shlib_head; shp; shp = shp->next) { 367 struct hints_bucket *bp; 368 369 bp = blist + (hinthash(shp->name, shp->major, shp->minor) % 370 hdr.hh_nbucket); 371 372 if (bp->hi_pathx) { 373 int j; 374 375 for (j = 0; j < hdr.hh_nbucket; j++) { 376 if (blist[j].hi_pathx == 0) 377 break; 378 } 379 if (j == hdr.hh_nbucket) { 380 warnx("Bummer!"); 381 goto out; 382 } 383 while (bp->hi_next != -1) 384 bp = &blist[bp->hi_next]; 385 bp->hi_next = j; 386 bp = blist + j; 387 } 388 389 /* Insert strings in string table */ 390 bp->hi_namex = str_index; 391 strlcpy(strtab + str_index, shp->name, strtab_sz - str_index); 392 str_index += 1 + strlen(shp->name); 393 394 bp->hi_pathx = str_index; 395 strlcpy(strtab + str_index, shp->path, strtab_sz - str_index); 396 str_index += 1 + strlen(shp->path); 397 398 /* Copy versions */ 399 bcopy(shp->dewey, bp->hi_dewey, sizeof(bp->hi_dewey)); 400 bp->hi_ndewey = shp->ndewey; 401 } 402 403 /* Copy search directories */ 404 strlcpy(strtab + str_index, dir_list, strtab_sz - str_index); 405 str_index += 1 + strlen(dir_list); 406 407 /* Sanity check */ 408 if (str_index != strtab_sz) 409 errx(1, "str_index(%d) != strtab_sz(%d)", str_index, strtab_sz); 410 411 tmpfilenam = concat(_PATH_LD_HINTS, ".XXXXXXXXXX", ""); 412 if ((fd = mkstemp(tmpfilenam)) == -1) { 413 warn("%s", tmpfilenam); 414 goto out; 415 } 416 fchmod(fd, 0444); 417 418 if (write(fd, &hdr, sizeof(struct hints_header)) != 419 sizeof(struct hints_header)) { 420 warn("%s", _PATH_LD_HINTS); 421 goto out; 422 } 423 if (write(fd, blist, hdr.hh_nbucket * sizeof(struct hints_bucket)) != 424 hdr.hh_nbucket * sizeof(struct hints_bucket)) { 425 warn("%s", _PATH_LD_HINTS); 426 goto out; 427 } 428 if (write(fd, strtab, strtab_sz) != strtab_sz) { 429 warn("%s", _PATH_LD_HINTS); 430 goto out; 431 } 432 if (close(fd) != 0) { 433 warn("%s", _PATH_LD_HINTS); 434 goto out; 435 } 436 437 /* Install it */ 438 if (unlink(_PATH_LD_HINTS) != 0 && errno != ENOENT) { 439 warn("%s", _PATH_LD_HINTS); 440 goto out; 441 } 442 443 if (rename(tmpfilenam, _PATH_LD_HINTS) != 0) { 444 warn("%s", _PATH_LD_HINTS); 445 goto out; 446 } 447 448 ret = 0; 449 out: 450 free(blist); 451 free(strtab); 452 return (ret); 453 } 454 455 static int 456 readhints(void) 457 { 458 struct stat sb; 459 struct hints_bucket *blist; 460 struct hints_header *hdr; 461 struct shlib_list *shp; 462 caddr_t addr; 463 char *strtab; 464 long msize; 465 int fd, i; 466 467 if ((fd = open(_PATH_LD_HINTS, O_RDONLY, 0)) == -1) { 468 warn("%s", _PATH_LD_HINTS); 469 return -1; 470 } 471 if (fstat(fd, &sb) != 0 || !S_ISREG(sb.st_mode) || 472 sb.st_size < sizeof(struct hints_header) || sb.st_size > LONG_MAX) { 473 warn("%s", _PATH_LD_HINTS); 474 return -1; 475 } 476 477 msize = (long)sb.st_size; 478 addr = mmap(0, msize, PROT_READ, MAP_PRIVATE, fd, 0); 479 480 if (addr == MAP_FAILED) { 481 warn("%s", _PATH_LD_HINTS); 482 return -1; 483 } 484 485 hdr = (struct hints_header *)addr; 486 if (HH_BADMAG(*hdr)) { 487 warnx("%s: Bad magic: %lo", 488 _PATH_LD_HINTS, hdr->hh_magic); 489 return -1; 490 } 491 492 if (hdr->hh_ehints > msize) { 493 warnx("%s: hintsize greater than filesize: 0x%lx > 0x%lx ", 494 _PATH_LD_HINTS, hdr->hh_ehints, msize); 495 return -1; 496 } 497 498 if (hdr->hh_version != LD_HINTS_VERSION_2) { 499 warnx("Unsupported version: %ld", hdr->hh_version); 500 return -1; 501 } 502 503 close(fd); 504 505 blist = (struct hints_bucket *)(addr + hdr->hh_hashtab); 506 strtab = (char *)(addr + hdr->hh_strtab); 507 508 dir_list = xstrdup(strtab + hdr->hh_dirlist); 509 510 if (rescan) 511 return (0); 512 513 for (i = 0; i < hdr->hh_nbucket; i++) { 514 struct hints_bucket *bp = &blist[i]; 515 516 /* Sanity check */ 517 if (bp->hi_namex >= hdr->hh_strtab_sz) { 518 warnx("Bad name index: %#x", bp->hi_namex); 519 return -1; 520 } 521 if (bp->hi_pathx >= hdr->hh_strtab_sz) { 522 warnx("Bad path index: %#x", bp->hi_pathx); 523 return -1; 524 } 525 526 /* Allocate new list element */ 527 shp = (struct shlib_list *)xmalloc(sizeof *shp); 528 shp->name = xstrdup(strtab + bp->hi_namex); 529 shp->path = xstrdup(strtab + bp->hi_pathx); 530 bcopy(bp->hi_dewey, shp->dewey, sizeof(shp->dewey)); 531 shp->ndewey = bp->hi_ndewey; 532 shp->next = NULL; 533 534 *shlib_tail = shp; 535 shlib_tail = &shp->next; 536 } 537 return 0; 538 } 539 540 static void 541 listhints(void) 542 { 543 struct shlib_list *shp; 544 int i; 545 546 printf("%s:\n", _PATH_LD_HINTS); 547 printf("\tsearch directories: %s\n", dir_list); 548 549 for (i = 0, shp = shlib_head; shp; i++, shp = shp->next) 550 printf("\t%d:-l%s.%d.%d => %s\n", 551 i, shp->name, shp->major, shp->minor, shp->path); 552 } 553