1 /*- 2 * Copyright (c) 2014 The FreeBSD Foundation 3 * All rights reserved. 4 * 5 * This software was developed by Edward Tomasz Napierala under sponsorship 6 * from the FreeBSD Foundation. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 */ 30 31 #include <sys/cdefs.h> 32 __FBSDID("$FreeBSD$"); 33 34 #include <sys/types.h> 35 #include <sys/time.h> 36 #include <sys/ioctl.h> 37 #include <sys/param.h> 38 #include <sys/linker.h> 39 #include <sys/mount.h> 40 #include <sys/socket.h> 41 #include <sys/stat.h> 42 #include <sys/wait.h> 43 #include <sys/utsname.h> 44 #include <assert.h> 45 #include <ctype.h> 46 #include <err.h> 47 #include <errno.h> 48 #include <fcntl.h> 49 #include <libgen.h> 50 #include <netdb.h> 51 #include <paths.h> 52 #include <signal.h> 53 #include <stdbool.h> 54 #include <stdint.h> 55 #define _WITH_GETLINE 56 #include <stdio.h> 57 #include <stdlib.h> 58 #include <string.h> 59 #include <unistd.h> 60 61 #include <libutil.h> 62 63 #include "autofs_ioctl.h" 64 65 #include "common.h" 66 67 extern FILE *yyin; 68 extern char *yytext; 69 extern int yylex(void); 70 71 static void parse_master_yyin(struct node *root, const char *master); 72 static void parse_map_yyin(struct node *parent, const char *map, 73 const char *executable_key); 74 75 char * 76 checked_strdup(const char *s) 77 { 78 char *c; 79 80 assert(s != NULL); 81 82 c = strdup(s); 83 if (c == NULL) 84 log_err(1, "strdup"); 85 return (c); 86 } 87 88 /* 89 * Take two pointers to strings, concatenate the contents with "/" in the 90 * middle, make the first pointer point to the result, the second pointer 91 * to NULL, and free the old strings. 92 * 93 * Concatenate pathnames, basically. 94 */ 95 static void 96 concat(char **p1, char **p2) 97 { 98 int ret; 99 char *path; 100 101 assert(p1 != NULL); 102 assert(p2 != NULL); 103 104 if (*p1 == NULL) 105 *p1 = checked_strdup(""); 106 107 if (*p2 == NULL) 108 *p2 = checked_strdup(""); 109 110 ret = asprintf(&path, "%s/%s", *p1, *p2); 111 if (ret < 0) 112 log_err(1, "asprintf"); 113 114 /* 115 * XXX 116 */ 117 //free(*p1); 118 //free(*p2); 119 120 *p1 = path; 121 *p2 = NULL; 122 } 123 124 /* 125 * Concatenate two strings, inserting separator between them, unless not needed. 126 * 127 * This function is very convenient to use when you do not care about freeing 128 * memory - which is okay here, because we are a short running process. 129 */ 130 char * 131 separated_concat(const char *s1, const char *s2, char separator) 132 { 133 char *result; 134 int ret; 135 136 assert(s1 != NULL); 137 assert(s2 != NULL); 138 139 if (s1[0] == '\0' || s2[0] == '\0' || 140 s1[strlen(s1) - 1] == separator || s2[0] == separator) { 141 ret = asprintf(&result, "%s%s", s1, s2); 142 } else { 143 ret = asprintf(&result, "%s%c%s", s1, separator, s2); 144 } 145 if (ret < 0) 146 log_err(1, "asprintf"); 147 148 //log_debugx("separated_concat: got %s and %s, returning %s", s1, s2, result); 149 150 return (result); 151 } 152 153 void 154 create_directory(const char *path) 155 { 156 char *component, *copy, *tofree, *partial; 157 int error; 158 159 assert(path[0] == '/'); 160 161 /* 162 * +1 to skip the leading slash. 163 */ 164 copy = tofree = checked_strdup(path + 1); 165 166 partial = NULL; 167 for (;;) { 168 component = strsep(©, "/"); 169 if (component == NULL) 170 break; 171 concat(&partial, &component); 172 //log_debugx("creating \"%s\"", partial); 173 error = mkdir(partial, 0755); 174 if (error != 0 && errno != EEXIST) { 175 log_warn("cannot create %s", partial); 176 return; 177 } 178 } 179 180 free(tofree); 181 } 182 183 struct node * 184 node_new_root(void) 185 { 186 struct node *n; 187 188 n = calloc(1, sizeof(*n)); 189 if (n == NULL) 190 log_err(1, "calloc"); 191 // XXX 192 n->n_key = checked_strdup("/"); 193 n->n_options = checked_strdup(""); 194 195 TAILQ_INIT(&n->n_children); 196 197 return (n); 198 } 199 200 struct node * 201 node_new(struct node *parent, char *key, char *options, char *location, 202 const char *config_file, int config_line) 203 { 204 struct node *n; 205 206 n = calloc(1, sizeof(*n)); 207 if (n == NULL) 208 log_err(1, "calloc"); 209 210 TAILQ_INIT(&n->n_children); 211 assert(key != NULL); 212 assert(key[0] != '\0'); 213 n->n_key = key; 214 if (options != NULL) 215 n->n_options = options; 216 else 217 n->n_options = strdup(""); 218 n->n_location = location; 219 assert(config_file != NULL); 220 n->n_config_file = config_file; 221 assert(config_line >= 0); 222 n->n_config_line = config_line; 223 224 assert(parent != NULL); 225 n->n_parent = parent; 226 TAILQ_INSERT_TAIL(&parent->n_children, n, n_next); 227 228 return (n); 229 } 230 231 struct node * 232 node_new_map(struct node *parent, char *key, char *options, char *map, 233 const char *config_file, int config_line) 234 { 235 struct node *n; 236 237 n = calloc(1, sizeof(*n)); 238 if (n == NULL) 239 log_err(1, "calloc"); 240 241 TAILQ_INIT(&n->n_children); 242 assert(key != NULL); 243 assert(key[0] != '\0'); 244 n->n_key = key; 245 if (options != NULL) 246 n->n_options = options; 247 else 248 n->n_options = strdup(""); 249 n->n_map = map; 250 assert(config_file != NULL); 251 n->n_config_file = config_file; 252 assert(config_line >= 0); 253 n->n_config_line = config_line; 254 255 assert(parent != NULL); 256 n->n_parent = parent; 257 TAILQ_INSERT_TAIL(&parent->n_children, n, n_next); 258 259 return (n); 260 } 261 262 static struct node * 263 node_duplicate(const struct node *o, struct node *parent) 264 { 265 const struct node *child; 266 struct node *n; 267 268 if (parent == NULL) 269 parent = o->n_parent; 270 271 n = node_new(parent, o->n_key, o->n_options, o->n_location, 272 o->n_config_file, o->n_config_line); 273 274 TAILQ_FOREACH(child, &o->n_children, n_next) 275 node_duplicate(child, n); 276 277 return (n); 278 } 279 280 static void 281 node_delete(struct node *n) 282 { 283 struct node *child, *tmp; 284 285 assert (n != NULL); 286 287 TAILQ_FOREACH_SAFE(child, &n->n_children, n_next, tmp) 288 node_delete(child); 289 290 if (n->n_parent != NULL) 291 TAILQ_REMOVE(&n->n_parent->n_children, n, n_next); 292 293 free(n); 294 } 295 296 /* 297 * Move (reparent) node 'n' to make it sibling of 'previous', placed 298 * just after it. 299 */ 300 static void 301 node_move_after(struct node *n, struct node *previous) 302 { 303 304 TAILQ_REMOVE(&n->n_parent->n_children, n, n_next); 305 n->n_parent = previous->n_parent; 306 TAILQ_INSERT_AFTER(&previous->n_parent->n_children, previous, n, n_next); 307 } 308 309 static void 310 node_expand_includes(struct node *root, bool is_master) 311 { 312 struct node *n, *n2, *tmp, *tmp2, *tmproot; 313 int error; 314 315 TAILQ_FOREACH_SAFE(n, &root->n_children, n_next, tmp) { 316 if (n->n_key[0] != '+') 317 continue; 318 319 error = access(AUTO_INCLUDE_PATH, F_OK); 320 if (error != 0) { 321 log_errx(1, "directory services not configured; " 322 "%s does not exist", AUTO_INCLUDE_PATH); 323 } 324 325 /* 326 * "+1" to skip leading "+". 327 */ 328 yyin = auto_popen(AUTO_INCLUDE_PATH, n->n_key + 1, NULL); 329 assert(yyin != NULL); 330 331 tmproot = node_new_root(); 332 if (is_master) 333 parse_master_yyin(tmproot, n->n_key); 334 else 335 parse_map_yyin(tmproot, n->n_key, NULL); 336 337 error = auto_pclose(yyin); 338 yyin = NULL; 339 if (error != 0) { 340 log_errx(1, "failed to handle include \"%s\"", 341 n->n_key); 342 } 343 344 /* 345 * Entries to be included are now in tmproot. We need to merge 346 * them with the rest, preserving their place and ordering. 347 */ 348 TAILQ_FOREACH_REVERSE_SAFE(n2, 349 &tmproot->n_children, nodehead, n_next, tmp2) { 350 node_move_after(n2, n); 351 } 352 353 node_delete(n); 354 node_delete(tmproot); 355 } 356 } 357 358 static char * 359 expand_ampersand(char *string, const char *key) 360 { 361 char c, *expanded; 362 int i, ret, before_len = 0; 363 bool backslashed = false; 364 365 assert(key[0] != '\0'); 366 367 expanded = checked_strdup(string); 368 369 for (i = 0; string[i] != '\0'; i++) { 370 c = string[i]; 371 if (c == '\\' && backslashed == false) { 372 backslashed = true; 373 continue; 374 } 375 if (backslashed) { 376 backslashed = false; 377 continue; 378 } 379 backslashed = false; 380 if (c != '&') 381 continue; 382 383 /* 384 * The 'before_len' variable contains the number 385 * of characters before the '&'. 386 */ 387 before_len = i; 388 //assert(i + 1 < (int)strlen(string)); 389 390 ret = asprintf(&expanded, "%.*s%s%s", 391 before_len, string, key, string + before_len + 1); 392 if (ret < 0) 393 log_err(1, "asprintf"); 394 395 //log_debugx("\"%s\" expanded with key \"%s\" to \"%s\"", 396 // string, key, expanded); 397 398 /* 399 * Figure out where to start searching for next variable. 400 */ 401 string = expanded; 402 i = before_len + strlen(key); 403 backslashed = false; 404 //assert(i < (int)strlen(string)); 405 } 406 407 return (expanded); 408 } 409 410 /* 411 * Expand "&" in n_location. If the key is NULL, try to use 412 * key from map entries themselves. Keep in mind that maps 413 * consist of tho levels of node structures, the key is one 414 * level up. 415 * 416 * Variant with NULL key is for "automount -LL". 417 */ 418 void 419 node_expand_ampersand(struct node *n, const char *key) 420 { 421 struct node *child; 422 423 if (n->n_location != NULL) { 424 if (key == NULL) { 425 if (n->n_parent != NULL && 426 strcmp(n->n_parent->n_key, "*") != 0) { 427 n->n_location = expand_ampersand(n->n_location, 428 n->n_parent->n_key); 429 } 430 } else { 431 n->n_location = expand_ampersand(n->n_location, key); 432 } 433 } 434 435 TAILQ_FOREACH(child, &n->n_children, n_next) 436 node_expand_ampersand(child, key); 437 } 438 439 /* 440 * Expand "*" in n_key. 441 */ 442 void 443 node_expand_wildcard(struct node *n, const char *key) 444 { 445 struct node *child, *expanded; 446 447 assert(key != NULL); 448 449 if (strcmp(n->n_key, "*") == 0) { 450 expanded = node_duplicate(n, NULL); 451 expanded->n_key = checked_strdup(key); 452 node_move_after(expanded, n); 453 } 454 455 TAILQ_FOREACH(child, &n->n_children, n_next) 456 node_expand_wildcard(child, key); 457 } 458 459 int 460 node_expand_defined(struct node *n) 461 { 462 struct node *child; 463 int error, cumulated_error = 0; 464 465 if (n->n_location != NULL) { 466 n->n_location = defined_expand(n->n_location); 467 if (n->n_location == NULL) { 468 log_warnx("failed to expand location for %s", 469 node_path(n)); 470 return (EINVAL); 471 } 472 } 473 474 TAILQ_FOREACH(child, &n->n_children, n_next) { 475 error = node_expand_defined(child); 476 if (error != 0 && cumulated_error == 0) 477 cumulated_error = error; 478 } 479 480 return (cumulated_error); 481 } 482 483 bool 484 node_is_direct_map(const struct node *n) 485 { 486 487 for (;;) { 488 assert(n->n_parent != NULL); 489 if (n->n_parent->n_parent == NULL) 490 break; 491 n = n->n_parent; 492 } 493 494 assert(n->n_key != NULL); 495 if (strcmp(n->n_key, "/-") != 0) 496 return (false); 497 498 return (true); 499 } 500 501 bool 502 node_has_wildcards(const struct node *n) 503 { 504 const struct node *child; 505 506 TAILQ_FOREACH(child, &n->n_children, n_next) { 507 if (strcmp(child->n_key, "*") == 0) 508 return (true); 509 } 510 511 return (false); 512 } 513 514 static void 515 node_expand_maps(struct node *n, bool indirect) 516 { 517 struct node *child, *tmp; 518 519 TAILQ_FOREACH_SAFE(child, &n->n_children, n_next, tmp) { 520 if (node_is_direct_map(child)) { 521 if (indirect) 522 continue; 523 } else { 524 if (indirect == false) 525 continue; 526 } 527 528 /* 529 * This is the first-level map node; the one that contains 530 * the key and subnodes with mountpoints and actual map names. 531 */ 532 if (child->n_map == NULL) 533 continue; 534 535 if (indirect) { 536 log_debugx("map \"%s\" is an indirect map, parsing", 537 child->n_map); 538 } else { 539 log_debugx("map \"%s\" is a direct map, parsing", 540 child->n_map); 541 } 542 parse_map(child, child->n_map, NULL, NULL); 543 } 544 } 545 546 static void 547 node_expand_direct_maps(struct node *n) 548 { 549 550 node_expand_maps(n, false); 551 } 552 553 void 554 node_expand_indirect_maps(struct node *n) 555 { 556 557 node_expand_maps(n, true); 558 } 559 560 static char * 561 node_path_x(const struct node *n, char *x) 562 { 563 char *path; 564 size_t len; 565 566 if (n->n_parent == NULL) 567 return (x); 568 569 /* 570 * Return "/-" for direct maps only if we were asked for path 571 * to the "/-" node itself, not to any of its subnodes. 572 */ 573 if (n->n_parent->n_parent == NULL && 574 strcmp(n->n_key, "/-") == 0 && 575 x[0] != '\0') { 576 return (x); 577 } 578 579 assert(n->n_key[0] != '\0'); 580 path = separated_concat(n->n_key, x, '/'); 581 free(x); 582 583 /* 584 * Strip trailing slash. 585 */ 586 len = strlen(path); 587 assert(len > 0); 588 if (path[len - 1] == '/') 589 path[len - 1] = '\0'; 590 591 return (node_path_x(n->n_parent, path)); 592 } 593 594 /* 595 * Return full path for node, consisting of concatenated 596 * paths of node itself and all its parents, up to the root. 597 */ 598 char * 599 node_path(const struct node *n) 600 { 601 602 return (node_path_x(n, checked_strdup(""))); 603 } 604 605 static char * 606 node_options_x(const struct node *n, char *x) 607 { 608 char *options; 609 610 options = separated_concat(x, n->n_options, ','); 611 if (n->n_parent == NULL) 612 return (options); 613 614 return (node_options_x(n->n_parent, options)); 615 } 616 617 /* 618 * Return options for node, consisting of concatenated 619 * options from the node itself and all its parents, 620 * up to the root. 621 */ 622 char * 623 node_options(const struct node *n) 624 { 625 626 return (node_options_x(n, checked_strdup(""))); 627 } 628 629 static void 630 node_print_indent(const struct node *n, int indent) 631 { 632 const struct node *child, *first_child; 633 char *path, *options; 634 635 path = node_path(n); 636 options = node_options(n); 637 638 /* 639 * Do not show both parent and child node if they have the same 640 * mountpoint; only show the child node. This means the typical, 641 * "key location", map entries are shown in a single line; 642 * the "key mountpoint1 location2 mountpoint2 location2" entries 643 * take multiple lines. 644 */ 645 first_child = TAILQ_FIRST(&n->n_children); 646 if (first_child == NULL || TAILQ_NEXT(first_child, n_next) != NULL || 647 strcmp(path, node_path(first_child)) != 0) { 648 assert(n->n_location == NULL || n->n_map == NULL); 649 printf("%*.s%-*s %s%-*s %-*s # %s map %s at %s:%d\n", 650 indent, "", 651 25 - indent, 652 path, 653 options[0] != '\0' ? "-" : " ", 654 20, 655 options[0] != '\0' ? options : "", 656 20, 657 n->n_location != NULL ? n->n_location : n->n_map != NULL ? n->n_map : "", 658 node_is_direct_map(n) ? "direct" : "indirect", 659 indent == 0 ? "referenced" : "defined", 660 n->n_config_file, n->n_config_line); 661 } 662 663 free(path); 664 free(options); 665 666 TAILQ_FOREACH(child, &n->n_children, n_next) 667 node_print_indent(child, indent + 2); 668 } 669 670 void 671 node_print(const struct node *n) 672 { 673 const struct node *child; 674 675 TAILQ_FOREACH(child, &n->n_children, n_next) 676 node_print_indent(child, 0); 677 } 678 679 struct node * 680 node_find(struct node *node, const char *path) 681 { 682 struct node *child, *found; 683 char *tmp; 684 size_t tmplen; 685 686 //log_debugx("looking up %s in %s", path, node->n_key); 687 688 tmp = node_path(node); 689 tmplen = strlen(tmp); 690 if (strncmp(tmp, path, tmplen) != 0) { 691 free(tmp); 692 return (NULL); 693 } 694 if (path[tmplen] != '/' && path[tmplen] != '\0') { 695 /* 696 * If we have two map entries like 'foo' and 'foobar', make 697 * sure the search for 'foobar' won't match 'foo' instead. 698 */ 699 free(tmp); 700 return (NULL); 701 } 702 free(tmp); 703 704 TAILQ_FOREACH(child, &node->n_children, n_next) { 705 found = node_find(child, path); 706 if (found != NULL) 707 return (found); 708 } 709 710 return (node); 711 } 712 713 /* 714 * Canonical form of a map entry looks like this: 715 * 716 * key [-options] [ [/mountpoint] [-options2] location ... ] 717 * 718 * Entries for executable maps are slightly different, as they 719 * lack the 'key' field and are always single-line; the key field 720 * for those maps is taken from 'executable_key' argument. 721 * 722 * We parse it in such a way that a map always has two levels - first 723 * for key, and the second, for the mountpoint. 724 */ 725 static void 726 parse_map_yyin(struct node *parent, const char *map, const char *executable_key) 727 { 728 char *key = NULL, *options = NULL, *mountpoint = NULL, 729 *options2 = NULL, *location = NULL; 730 int ret; 731 struct node *node; 732 733 lineno = 1; 734 735 if (executable_key != NULL) 736 key = checked_strdup(executable_key); 737 738 for (;;) { 739 ret = yylex(); 740 if (ret == 0 || ret == NEWLINE) { 741 /* 742 * In case of executable map, the key is always 743 * non-NULL, even if the map is empty. So, make sure 744 * we don't fail empty maps here. 745 */ 746 if ((key != NULL && executable_key == NULL) || 747 options != NULL) { 748 log_errx(1, "truncated entry at %s, line %d", 749 map, lineno); 750 } 751 if (ret == 0 || executable_key != NULL) { 752 /* 753 * End of file. 754 */ 755 break; 756 } else { 757 key = options = NULL; 758 continue; 759 } 760 } 761 if (key == NULL) { 762 key = checked_strdup(yytext); 763 if (key[0] == '+') { 764 node_new(parent, key, NULL, NULL, map, lineno); 765 key = options = NULL; 766 continue; 767 } 768 continue; 769 } else if (yytext[0] == '-') { 770 if (options != NULL) { 771 log_errx(1, "duplicated options at %s, line %d", 772 map, lineno); 773 } 774 /* 775 * +1 to skip leading "-". 776 */ 777 options = checked_strdup(yytext + 1); 778 continue; 779 } 780 781 /* 782 * We cannot properly handle a situation where the map key 783 * is "/". Ignore such entries. 784 * 785 * XXX: According to Piete Brooks, Linux automounter uses 786 * "/" as a wildcard character in LDAP maps. Perhaps 787 * we should work around this braindamage by substituting 788 * "*" for "/"? 789 */ 790 if (strcmp(key, "/") == 0) { 791 log_warnx("nonsensical map key \"/\" at %s, line %d; " 792 "ignoring map entry ", map, lineno); 793 794 /* 795 * Skip the rest of the entry. 796 */ 797 do { 798 ret = yylex(); 799 } while (ret != 0 && ret != NEWLINE); 800 801 key = options = NULL; 802 continue; 803 } 804 805 //log_debugx("adding map node, %s", key); 806 node = node_new(parent, key, options, NULL, map, lineno); 807 key = options = NULL; 808 809 for (;;) { 810 if (yytext[0] == '/') { 811 if (mountpoint != NULL) { 812 log_errx(1, "duplicated mountpoint " 813 "in %s, line %d", map, lineno); 814 } 815 if (options2 != NULL || location != NULL) { 816 log_errx(1, "mountpoint out of order " 817 "in %s, line %d", map, lineno); 818 } 819 mountpoint = checked_strdup(yytext); 820 goto again; 821 } 822 823 if (yytext[0] == '-') { 824 if (options2 != NULL) { 825 log_errx(1, "duplicated options " 826 "in %s, line %d", map, lineno); 827 } 828 if (location != NULL) { 829 log_errx(1, "options out of order " 830 "in %s, line %d", map, lineno); 831 } 832 options2 = checked_strdup(yytext + 1); 833 goto again; 834 } 835 836 if (location != NULL) { 837 log_errx(1, "too many arguments " 838 "in %s, line %d", map, lineno); 839 } 840 841 /* 842 * If location field starts with colon, e.g. ":/dev/cd0", 843 * then strip it. 844 */ 845 if (yytext[0] == ':') { 846 location = checked_strdup(yytext + 1); 847 if (location[0] == '\0') { 848 log_errx(1, "empty location in %s, " 849 "line %d", map, lineno); 850 } 851 } else { 852 location = checked_strdup(yytext); 853 } 854 855 if (mountpoint == NULL) 856 mountpoint = checked_strdup("/"); 857 if (options2 == NULL) 858 options2 = checked_strdup(""); 859 860 #if 0 861 log_debugx("adding map node, %s %s %s", 862 mountpoint, options2, location); 863 #endif 864 node_new(node, mountpoint, options2, location, 865 map, lineno); 866 mountpoint = options2 = location = NULL; 867 again: 868 ret = yylex(); 869 if (ret == 0 || ret == NEWLINE) { 870 if (mountpoint != NULL || options2 != NULL || 871 location != NULL) { 872 log_errx(1, "truncated entry " 873 "in %s, line %d", map, lineno); 874 } 875 break; 876 } 877 } 878 } 879 } 880 881 /* 882 * Parse output of a special map called without argument. It is a list 883 * of keys, separated by newlines. They can contain whitespace, so use 884 * getline(3) instead of lexer used for maps. 885 */ 886 static void 887 parse_map_keys_yyin(struct node *parent, const char *map) 888 { 889 char *line = NULL, *key; 890 size_t linecap = 0; 891 ssize_t linelen; 892 893 lineno = 1; 894 895 for (;;) { 896 linelen = getline(&line, &linecap, yyin); 897 if (linelen < 0) { 898 /* 899 * End of file. 900 */ 901 break; 902 } 903 if (linelen <= 1) { 904 /* 905 * Empty line, consisting of just the newline. 906 */ 907 continue; 908 } 909 910 /* 911 * "-1" to strip the trailing newline. 912 */ 913 key = strndup(line, linelen - 1); 914 915 log_debugx("adding key \"%s\"", key); 916 node_new(parent, key, NULL, NULL, map, lineno); 917 lineno++; 918 } 919 free(line); 920 } 921 922 static bool 923 file_is_executable(const char *path) 924 { 925 struct stat sb; 926 int error; 927 928 error = stat(path, &sb); 929 if (error != 0) 930 log_err(1, "cannot stat %s", path); 931 if ((sb.st_mode & S_IXUSR) || (sb.st_mode & S_IXGRP) || 932 (sb.st_mode & S_IXOTH)) 933 return (true); 934 return (false); 935 } 936 937 /* 938 * Parse a special map, e.g. "-hosts". 939 */ 940 static void 941 parse_special_map(struct node *parent, const char *map, const char *key) 942 { 943 char *path; 944 int error, ret; 945 946 assert(map[0] == '-'); 947 948 /* 949 * +1 to skip leading "-" in map name. 950 */ 951 ret = asprintf(&path, "%s/special_%s", AUTO_SPECIAL_PREFIX, map + 1); 952 if (ret < 0) 953 log_err(1, "asprintf"); 954 955 yyin = auto_popen(path, key, NULL); 956 assert(yyin != NULL); 957 958 if (key == NULL) { 959 parse_map_keys_yyin(parent, map); 960 } else { 961 parse_map_yyin(parent, map, key); 962 } 963 964 error = auto_pclose(yyin); 965 yyin = NULL; 966 if (error != 0) 967 log_errx(1, "failed to handle special map \"%s\"", map); 968 969 node_expand_includes(parent, false); 970 node_expand_direct_maps(parent); 971 972 free(path); 973 } 974 975 /* 976 * Retrieve and parse map from directory services, e.g. LDAP. 977 * Note that it is different from executable maps, in that 978 * the include script outputs the whole map to standard output 979 * (as opposed to executable maps that only output a single 980 * entry, without the key), and it takes the map name as an 981 * argument, instead of key. 982 */ 983 static void 984 parse_included_map(struct node *parent, const char *map) 985 { 986 int error; 987 988 assert(map[0] != '-'); 989 assert(map[0] != '/'); 990 991 error = access(AUTO_INCLUDE_PATH, F_OK); 992 if (error != 0) { 993 log_errx(1, "directory services not configured;" 994 " %s does not exist", AUTO_INCLUDE_PATH); 995 } 996 997 yyin = auto_popen(AUTO_INCLUDE_PATH, map, NULL); 998 assert(yyin != NULL); 999 1000 parse_map_yyin(parent, map, NULL); 1001 1002 error = auto_pclose(yyin); 1003 yyin = NULL; 1004 if (error != 0) 1005 log_errx(1, "failed to handle remote map \"%s\"", map); 1006 1007 node_expand_includes(parent, false); 1008 node_expand_direct_maps(parent); 1009 } 1010 1011 void 1012 parse_map(struct node *parent, const char *map, const char *key, 1013 bool *wildcards) 1014 { 1015 char *path = NULL; 1016 int error, ret; 1017 bool executable; 1018 1019 assert(map != NULL); 1020 assert(map[0] != '\0'); 1021 1022 log_debugx("parsing map \"%s\"", map); 1023 1024 if (wildcards != NULL) 1025 *wildcards = false; 1026 1027 if (map[0] == '-') { 1028 if (wildcards != NULL) 1029 *wildcards = true; 1030 return (parse_special_map(parent, map, key)); 1031 } 1032 1033 if (map[0] == '/') { 1034 path = checked_strdup(map); 1035 } else { 1036 ret = asprintf(&path, "%s/%s", AUTO_MAP_PREFIX, map); 1037 if (ret < 0) 1038 log_err(1, "asprintf"); 1039 log_debugx("map \"%s\" maps to \"%s\"", map, path); 1040 1041 /* 1042 * See if the file exists. If not, try to obtain the map 1043 * from directory services. 1044 */ 1045 error = access(path, F_OK); 1046 if (error != 0) { 1047 log_debugx("map file \"%s\" does not exist; falling " 1048 "back to directory services", path); 1049 return (parse_included_map(parent, map)); 1050 } 1051 } 1052 1053 executable = file_is_executable(path); 1054 1055 if (executable) { 1056 log_debugx("map \"%s\" is executable", map); 1057 1058 if (wildcards != NULL) 1059 *wildcards = true; 1060 1061 if (key != NULL) { 1062 yyin = auto_popen(path, key, NULL); 1063 } else { 1064 yyin = auto_popen(path, NULL); 1065 } 1066 assert(yyin != NULL); 1067 } else { 1068 yyin = fopen(path, "r"); 1069 if (yyin == NULL) 1070 log_err(1, "unable to open \"%s\"", path); 1071 } 1072 1073 free(path); 1074 path = NULL; 1075 1076 parse_map_yyin(parent, map, executable ? key : NULL); 1077 1078 if (executable) { 1079 error = auto_pclose(yyin); 1080 yyin = NULL; 1081 if (error != 0) { 1082 log_errx(1, "failed to handle executable map \"%s\"", 1083 map); 1084 } 1085 } else { 1086 fclose(yyin); 1087 } 1088 yyin = NULL; 1089 1090 log_debugx("done parsing map \"%s\"", map); 1091 1092 node_expand_includes(parent, false); 1093 node_expand_direct_maps(parent); 1094 } 1095 1096 static void 1097 parse_master_yyin(struct node *root, const char *master) 1098 { 1099 char *mountpoint = NULL, *map = NULL, *options = NULL; 1100 int ret; 1101 1102 /* 1103 * XXX: 1 gives incorrect values; wtf? 1104 */ 1105 lineno = 0; 1106 1107 for (;;) { 1108 ret = yylex(); 1109 if (ret == 0 || ret == NEWLINE) { 1110 if (mountpoint != NULL) { 1111 //log_debugx("adding map for %s", mountpoint); 1112 node_new_map(root, mountpoint, options, map, 1113 master, lineno); 1114 } 1115 if (ret == 0) { 1116 break; 1117 } else { 1118 mountpoint = map = options = NULL; 1119 continue; 1120 } 1121 } 1122 if (mountpoint == NULL) { 1123 mountpoint = checked_strdup(yytext); 1124 } else if (map == NULL) { 1125 map = checked_strdup(yytext); 1126 } else if (options == NULL) { 1127 /* 1128 * +1 to skip leading "-". 1129 */ 1130 options = checked_strdup(yytext + 1); 1131 } else { 1132 log_errx(1, "too many arguments at %s, line %d", 1133 master, lineno); 1134 } 1135 } 1136 } 1137 1138 void 1139 parse_master(struct node *root, const char *master) 1140 { 1141 1142 log_debugx("parsing auto_master file at \"%s\"", master); 1143 1144 yyin = fopen(master, "r"); 1145 if (yyin == NULL) 1146 err(1, "unable to open %s", master); 1147 1148 parse_master_yyin(root, master); 1149 1150 fclose(yyin); 1151 yyin = NULL; 1152 1153 log_debugx("done parsing \"%s\"", master); 1154 1155 node_expand_includes(root, true); 1156 node_expand_direct_maps(root); 1157 } 1158 1159 /* 1160 * Two things daemon(3) does, that we actually also want to do 1161 * when running in foreground, is closing the stdin and chdiring 1162 * to "/". This is what we do here. 1163 */ 1164 void 1165 lesser_daemon(void) 1166 { 1167 int error, fd; 1168 1169 error = chdir("/"); 1170 if (error != 0) 1171 log_warn("chdir"); 1172 1173 fd = open(_PATH_DEVNULL, O_RDWR, 0); 1174 if (fd < 0) { 1175 log_warn("cannot open %s", _PATH_DEVNULL); 1176 return; 1177 } 1178 1179 error = dup2(fd, STDIN_FILENO); 1180 if (error != 0) 1181 log_warn("dup2"); 1182 1183 error = close(fd); 1184 if (error != 0) { 1185 /* Bloody hell. */ 1186 log_warn("close"); 1187 } 1188 } 1189 1190 int 1191 main(int argc, char **argv) 1192 { 1193 char *cmdname; 1194 1195 if (argv[0] == NULL) 1196 log_errx(1, "NULL command name"); 1197 1198 cmdname = basename(argv[0]); 1199 1200 if (strcmp(cmdname, "automount") == 0) 1201 return (main_automount(argc, argv)); 1202 else if (strcmp(cmdname, "automountd") == 0) 1203 return (main_automountd(argc, argv)); 1204 else if (strcmp(cmdname, "autounmountd") == 0) 1205 return (main_autounmountd(argc, argv)); 1206 else 1207 log_errx(1, "binary name should be either \"automount\", " 1208 "\"automountd\", or \"autounmountd\""); 1209 } 1210