1 /* 2 * $FreeBSD$ 3 */ 4 5 #include <sys/types.h> 6 #include <sys/param.h> 7 #include <sys/fcntl.h> 8 #include <sys/mman.h> 9 #include <sys/queue.h> 10 #include <sys/stat.h> 11 #include <dirent.h> 12 #include <errno.h> 13 #include <stdlib.h> 14 #include <string.h> 15 16 #include "debug.h" 17 #include "rtld.h" 18 #include "libmap.h" 19 20 #ifndef _PATH_LIBMAP_CONF 21 #define _PATH_LIBMAP_CONF "/etc/libmap.conf" 22 #endif 23 24 TAILQ_HEAD(lm_list, lm); 25 struct lm { 26 char *f; 27 char *t; 28 TAILQ_ENTRY(lm) lm_link; 29 }; 30 31 TAILQ_HEAD(lmp_list, lmp) lmp_head = TAILQ_HEAD_INITIALIZER(lmp_head); 32 struct lmp { 33 char *p; 34 enum { T_EXACT=0, T_BASENAME, T_DIRECTORY } type; 35 struct lm_list lml; 36 TAILQ_ENTRY(lmp) lmp_link; 37 }; 38 39 static TAILQ_HEAD(lmc_list, lmc) lmc_head = TAILQ_HEAD_INITIALIZER(lmc_head); 40 struct lmc { 41 char *path; 42 TAILQ_ENTRY(lmc) next; 43 }; 44 45 static int lm_count; 46 47 static void lmc_parse(char *, size_t); 48 static void lmc_parse_file(char *); 49 static void lmc_parse_dir(char *); 50 static void lm_add(const char *, const char *, const char *); 51 static void lm_free(struct lm_list *); 52 static char *lml_find(struct lm_list *, const char *); 53 static struct lm_list *lmp_find(const char *); 54 static struct lm_list *lmp_init(char *); 55 static const char *quickbasename(const char *); 56 57 #define iseol(c) (((c) == '#') || ((c) == '\0') || \ 58 ((c) == '\n') || ((c) == '\r')) 59 60 /* 61 * Do not use ctype.h macros, which rely on working TLS. It is 62 * too early to have thread-local variables functional. 63 */ 64 #define rtld_isspace(c) ((c) == ' ' || (c) == '\t') 65 66 int 67 lm_init(char *libmap_override) 68 { 69 char *p; 70 71 dbg("lm_init(\"%s\")", libmap_override); 72 TAILQ_INIT(&lmp_head); 73 74 lmc_parse_file(_PATH_LIBMAP_CONF); 75 76 if (libmap_override) { 77 /* 78 * Do some character replacement to make $LIBMAP look 79 * like a text file, then parse it. 80 */ 81 libmap_override = xstrdup(libmap_override); 82 for (p = libmap_override; *p; p++) { 83 switch (*p) { 84 case '=': 85 *p = ' '; 86 break; 87 case ',': 88 *p = '\n'; 89 break; 90 } 91 } 92 lmc_parse(p, strlen(p)); 93 free(p); 94 } 95 96 return (lm_count == 0); 97 } 98 99 static void 100 lmc_parse_file(char *path) 101 { 102 struct lmc *p; 103 struct stat st; 104 int fd; 105 char *rpath; 106 char *lm_map; 107 char resolved[MAXPATHLEN]; 108 109 rpath = realpath(path, resolved); 110 if (rpath == NULL) 111 return; 112 113 TAILQ_FOREACH(p, &lmc_head, next) { 114 if (strcmp(p->path, rpath) == 0) { 115 free(rpath); 116 return; 117 } 118 } 119 120 fd = open(rpath, O_RDONLY); 121 if (fd == -1) { 122 dbg("lm_parse_file: open(\"%s\") failed, %s", rpath, 123 rtld_strerror(errno)); 124 free(rpath); 125 return; 126 } 127 if (fstat(fd, &st) == -1) { 128 close(fd); 129 dbg("lm_parse_file: fstat(\"%s\") failed, %s", rpath, 130 rtld_strerror(errno)); 131 free(rpath); 132 return; 133 } 134 lm_map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0); 135 if (lm_map == (const char *)MAP_FAILED) { 136 close(fd); 137 dbg("lm_parse_file: mmap(\"%s\") failed, %s", rpath, 138 rtld_strerror(errno)); 139 free(rpath); 140 return; 141 } 142 close(fd); 143 p = xmalloc(sizeof(struct lmc)); 144 p->path = xmalloc(strlen(rpath)+1); 145 strcpy(p->path, rpath); 146 TAILQ_INSERT_HEAD(&lmc_head, p, next); 147 lmc_parse(lm_map, st.st_size); 148 munmap(lm_map, st.st_size); 149 } 150 151 static void 152 lmc_parse_dir(char *idir) 153 { 154 DIR *d; 155 struct dirent *dp; 156 struct lmc *p; 157 char conffile[MAXPATHLEN]; 158 char resolved[MAXPATHLEN]; 159 char *ext; 160 char *rpath; 161 162 rpath = realpath(idir, resolved); 163 if (rpath == NULL) 164 return; 165 166 TAILQ_FOREACH(p, &lmc_head, next) { 167 if (strcmp(p->path, rpath) == 0) { 168 free(rpath); 169 return; 170 } 171 } 172 d = opendir(idir); 173 if (d == NULL) { 174 free(rpath); 175 return; 176 } 177 178 p = xmalloc(sizeof(struct lmc)); 179 p->path = rpath; 180 TAILQ_INSERT_HEAD(&lmc_head, p, next); 181 182 while ((dp = readdir(d)) != NULL) { 183 if (dp->d_ino == 0) 184 continue; 185 if (dp->d_type != DT_REG) 186 continue; 187 ext = strrchr(dp->d_name, '.'); 188 if (ext == NULL) 189 continue; 190 if (strcmp(ext, ".conf") != 0) 191 continue; 192 if (strlcpy(conffile, idir, MAXPATHLEN) >= MAXPATHLEN) 193 continue; /* too long */ 194 if (strlcat(conffile, "/", MAXPATHLEN) >= MAXPATHLEN) 195 continue; /* too long */ 196 if (strlcat(conffile, dp->d_name, MAXPATHLEN) >= MAXPATHLEN) 197 continue; /* too long */ 198 lmc_parse_file(conffile); 199 } 200 closedir(d); 201 } 202 203 static void 204 lmc_parse(char *lm_p, size_t lm_len) 205 { 206 char *cp, *f, *t, *c, *p; 207 char prog[MAXPATHLEN]; 208 /* allow includedir + full length path */ 209 char line[MAXPATHLEN + 13]; 210 size_t cnt; 211 int i; 212 213 cnt = 0; 214 p = NULL; 215 while (cnt < lm_len) { 216 i = 0; 217 while (lm_p[cnt] != '\n' && cnt < lm_len && 218 i < sizeof(line) - 1) { 219 line[i] = lm_p[cnt]; 220 cnt++; 221 i++; 222 } 223 line[i] = '\0'; 224 while (lm_p[cnt] != '\n' && cnt < lm_len) 225 cnt++; 226 /* skip over nl */ 227 cnt++; 228 229 cp = &line[0]; 230 t = f = c = NULL; 231 232 /* Skip over leading space */ 233 while (rtld_isspace(*cp)) cp++; 234 235 /* Found a comment or EOL */ 236 if (iseol(*cp)) continue; 237 238 /* Found a constraint selector */ 239 if (*cp == '[') { 240 cp++; 241 242 /* Skip leading space */ 243 while (rtld_isspace(*cp)) cp++; 244 245 /* Found comment, EOL or end of selector */ 246 if (iseol(*cp) || *cp == ']') 247 continue; 248 249 c = cp++; 250 /* Skip to end of word */ 251 while (!rtld_isspace(*cp) && !iseol(*cp) && *cp != ']') 252 cp++; 253 254 /* Skip and zero out trailing space */ 255 while (rtld_isspace(*cp)) *cp++ = '\0'; 256 257 /* Check if there is a closing brace */ 258 if (*cp != ']') continue; 259 260 /* Terminate string if there was no trailing space */ 261 *cp++ = '\0'; 262 263 /* 264 * There should be nothing except whitespace or comment 265 from this point to the end of the line. 266 */ 267 while(rtld_isspace(*cp)) cp++; 268 if (!iseol(*cp)) continue; 269 270 if (strlcpy(prog, c, sizeof prog) >= sizeof prog) 271 continue; 272 p = prog; 273 continue; 274 } 275 276 /* Parse the 'from' candidate. */ 277 f = cp++; 278 while (!rtld_isspace(*cp) && !iseol(*cp)) cp++; 279 280 /* Skip and zero out the trailing whitespace */ 281 while (rtld_isspace(*cp)) *cp++ = '\0'; 282 283 /* Found a comment or EOL */ 284 if (iseol(*cp)) continue; 285 286 /* Parse 'to' mapping */ 287 t = cp++; 288 while (!rtld_isspace(*cp) && !iseol(*cp)) cp++; 289 290 /* Skip and zero out the trailing whitespace */ 291 while (rtld_isspace(*cp)) *cp++ = '\0'; 292 293 /* Should be no extra tokens at this point */ 294 if (!iseol(*cp)) continue; 295 296 *cp = '\0'; 297 if (strcmp(f, "includedir") == 0) 298 lmc_parse_dir(t); 299 else if (strcmp(f, "include") == 0) 300 lmc_parse_file(t); 301 else 302 lm_add(p, f, t); 303 } 304 } 305 306 static void 307 lm_free (struct lm_list *lml) 308 { 309 struct lm *lm; 310 311 dbg("%s(%p)", __func__, lml); 312 313 while (!TAILQ_EMPTY(lml)) { 314 lm = TAILQ_FIRST(lml); 315 TAILQ_REMOVE(lml, lm, lm_link); 316 free(lm->f); 317 free(lm->t); 318 free(lm); 319 } 320 return; 321 } 322 323 void 324 lm_fini (void) 325 { 326 struct lmp *lmp; 327 struct lmc *p; 328 329 dbg("%s()", __func__); 330 331 while (!TAILQ_EMPTY(&lmc_head)) { 332 p = TAILQ_FIRST(&lmc_head); 333 TAILQ_REMOVE(&lmc_head, p, next); 334 free(p->path); 335 free(p); 336 } 337 338 while (!TAILQ_EMPTY(&lmp_head)) { 339 lmp = TAILQ_FIRST(&lmp_head); 340 TAILQ_REMOVE(&lmp_head, lmp, lmp_link); 341 free(lmp->p); 342 lm_free(&lmp->lml); 343 free(lmp); 344 } 345 return; 346 } 347 348 static void 349 lm_add (const char *p, const char *f, const char *t) 350 { 351 struct lm_list *lml; 352 struct lm *lm; 353 354 if (p == NULL) 355 p = "$DEFAULT$"; 356 357 dbg("%s(\"%s\", \"%s\", \"%s\")", __func__, p, f, t); 358 359 if ((lml = lmp_find(p)) == NULL) 360 lml = lmp_init(xstrdup(p)); 361 362 lm = xmalloc(sizeof(struct lm)); 363 lm->f = xstrdup(f); 364 lm->t = xstrdup(t); 365 TAILQ_INSERT_HEAD(lml, lm, lm_link); 366 lm_count++; 367 } 368 369 char * 370 lm_find (const char *p, const char *f) 371 { 372 struct lm_list *lml; 373 char *t; 374 375 dbg("%s(\"%s\", \"%s\")", __func__, p, f); 376 377 if (p != NULL && (lml = lmp_find(p)) != NULL) { 378 t = lml_find(lml, f); 379 if (t != NULL) { 380 /* 381 * Add a global mapping if we have 382 * a successful constrained match. 383 */ 384 lm_add(NULL, f, t); 385 return (t); 386 } 387 } 388 lml = lmp_find("$DEFAULT$"); 389 if (lml != NULL) 390 return (lml_find(lml, f)); 391 else 392 return (NULL); 393 } 394 395 static char * 396 lml_find (struct lm_list *lmh, const char *f) 397 { 398 struct lm *lm; 399 400 dbg("%s(%p, \"%s\")", __func__, lmh, f); 401 402 TAILQ_FOREACH(lm, lmh, lm_link) 403 if (strcmp(f, lm->f) == 0) 404 return (lm->t); 405 return (NULL); 406 } 407 408 /* Given an executable name, return a pointer to the translation list or 409 NULL if no matches */ 410 static struct lm_list * 411 lmp_find (const char *n) 412 { 413 struct lmp *lmp; 414 415 dbg("%s(\"%s\")", __func__, n); 416 417 TAILQ_FOREACH(lmp, &lmp_head, lmp_link) 418 if ((lmp->type == T_EXACT && strcmp(n, lmp->p) == 0) || 419 (lmp->type == T_DIRECTORY && strncmp(n, lmp->p, strlen(lmp->p)) == 0) || 420 (lmp->type == T_BASENAME && strcmp(quickbasename(n), lmp->p) == 0)) 421 return (&lmp->lml); 422 return (NULL); 423 } 424 425 static struct lm_list * 426 lmp_init (char *n) 427 { 428 struct lmp *lmp; 429 430 dbg("%s(\"%s\")", __func__, n); 431 432 lmp = xmalloc(sizeof(struct lmp)); 433 lmp->p = n; 434 if (n[strlen(n)-1] == '/') 435 lmp->type = T_DIRECTORY; 436 else if (strchr(n,'/') == NULL) 437 lmp->type = T_BASENAME; 438 else 439 lmp->type = T_EXACT; 440 TAILQ_INIT(&lmp->lml); 441 TAILQ_INSERT_HEAD(&lmp_head, lmp, lmp_link); 442 443 return (&lmp->lml); 444 } 445 446 /* libc basename is overkill. Return a pointer to the character after the 447 last /, or the original string if there are no slashes. */ 448 static const char * 449 quickbasename (const char *path) 450 { 451 const char *p = path; 452 for (; *path; path++) { 453 if (*path == '/') 454 p = path+1; 455 } 456 return (p); 457 } 458