common.c revision 283227
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: stable/10/usr.sbin/autofs/common.c 283227 2015-05-21 13:18:02Z trasz $"); 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 67extern FILE *yyin; 68extern char *yytext; 69extern int yylex(void); 70 71static void parse_master_yyin(struct node *root, const char *master); 72static void parse_map_yyin(struct node *parent, const char *map, 73 const char *executable_key); 74 75char * 76checked_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 */ 95static void 96concat(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 */ 130char * 131separated_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 153void 154create_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 183struct node * 184node_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 200struct node * 201node_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 231struct node * 232node_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 262static struct node * 263node_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 280static void 281node_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 */ 300static void 301node_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 309static void 310node_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 358static char * 359expand_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 */ 418void 419node_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 */ 442void 443node_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 459int 460node_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 483bool 484node_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 501bool 502node_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 514static void 515node_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 546static void 547node_expand_direct_maps(struct node *n) 548{ 549 550 node_expand_maps(n, false); 551} 552 553void 554node_expand_indirect_maps(struct node *n) 555{ 556 557 node_expand_maps(n, true); 558} 559 560static char * 561node_path_x(const struct node *n, char *x) 562{ 563 char *path; 564 565 if (n->n_parent == NULL) 566 return (x); 567 568 /* 569 * Return "/-" for direct maps only if we were asked for path 570 * to the "/-" node itself, not to any of its subnodes. 571 */ 572 if (n->n_parent->n_parent == NULL && 573 strcmp(n->n_key, "/-") == 0 && 574 x[0] != '\0') { 575 return (x); 576 } 577 578 assert(n->n_key[0] != '\0'); 579 path = separated_concat(n->n_key, x, '/'); 580 free(x); 581 582 return (node_path_x(n->n_parent, path)); 583} 584 585/* 586 * Return full path for node, consisting of concatenated 587 * paths of node itself and all its parents, up to the root. 588 */ 589char * 590node_path(const struct node *n) 591{ 592 char *path; 593 size_t len; 594 595 path = node_path_x(n, checked_strdup("")); 596 597 /* 598 * Strip trailing slash, unless the whole path is "/". 599 */ 600 len = strlen(path); 601 if (len > 1 && path[len - 1] == '/') 602 path[len - 1] = '\0'; 603 604 return (path); 605} 606 607static char * 608node_options_x(const struct node *n, char *x) 609{ 610 char *options; 611 612 options = separated_concat(x, n->n_options, ','); 613 if (n->n_parent == NULL) 614 return (options); 615 616 return (node_options_x(n->n_parent, options)); 617} 618 619/* 620 * Return options for node, consisting of concatenated 621 * options from the node itself and all its parents, 622 * up to the root. 623 */ 624char * 625node_options(const struct node *n) 626{ 627 628 return (node_options_x(n, checked_strdup(""))); 629} 630 631static void 632node_print_indent(const struct node *n, int indent) 633{ 634 const struct node *child, *first_child; 635 char *path, *options; 636 637 path = node_path(n); 638 options = node_options(n); 639 640 /* 641 * Do not show both parent and child node if they have the same 642 * mountpoint; only show the child node. This means the typical, 643 * "key location", map entries are shown in a single line; 644 * the "key mountpoint1 location2 mountpoint2 location2" entries 645 * take multiple lines. 646 */ 647 first_child = TAILQ_FIRST(&n->n_children); 648 if (first_child == NULL || TAILQ_NEXT(first_child, n_next) != NULL || 649 strcmp(path, node_path(first_child)) != 0) { 650 assert(n->n_location == NULL || n->n_map == NULL); 651 printf("%*.s%-*s %s%-*s %-*s # %s map %s at %s:%d\n", 652 indent, "", 653 25 - indent, 654 path, 655 options[0] != '\0' ? "-" : " ", 656 20, 657 options[0] != '\0' ? options : "", 658 20, 659 n->n_location != NULL ? n->n_location : n->n_map != NULL ? n->n_map : "", 660 node_is_direct_map(n) ? "direct" : "indirect", 661 indent == 0 ? "referenced" : "defined", 662 n->n_config_file, n->n_config_line); 663 } 664 665 free(path); 666 free(options); 667 668 TAILQ_FOREACH(child, &n->n_children, n_next) 669 node_print_indent(child, indent + 2); 670} 671 672void 673node_print(const struct node *n) 674{ 675 const struct node *child; 676 677 TAILQ_FOREACH(child, &n->n_children, n_next) 678 node_print_indent(child, 0); 679} 680 681static struct node * 682node_find_x(struct node *node, const char *path) 683{ 684 struct node *child, *found; 685 char *tmp; 686 size_t tmplen; 687 688 //log_debugx("looking up %s in %s", path, node->n_key); 689 690 tmp = node_path(node); 691 tmplen = strlen(tmp); 692 if (strncmp(tmp, path, tmplen) != 0) { 693 free(tmp); 694 return (NULL); 695 } 696 if (path[tmplen] != '/' && path[tmplen] != '\0') { 697 /* 698 * If we have two map entries like 'foo' and 'foobar', make 699 * sure the search for 'foobar' won't match 'foo' instead. 700 */ 701 free(tmp); 702 return (NULL); 703 } 704 free(tmp); 705 706 TAILQ_FOREACH(child, &node->n_children, n_next) { 707 found = node_find_x(child, path); 708 if (found != NULL) 709 return (found); 710 } 711 712 return (node); 713} 714 715struct node * 716node_find(struct node *root, const char *path) 717{ 718 struct node *node; 719 720 node = node_find_x(root, path); 721 if (node == root) 722 return (NULL); 723 return (node); 724} 725 726/* 727 * Canonical form of a map entry looks like this: 728 * 729 * key [-options] [ [/mountpoint] [-options2] location ... ] 730 * 731 * Entries for executable maps are slightly different, as they 732 * lack the 'key' field and are always single-line; the key field 733 * for those maps is taken from 'executable_key' argument. 734 * 735 * We parse it in such a way that a map always has two levels - first 736 * for key, and the second, for the mountpoint. 737 */ 738static void 739parse_map_yyin(struct node *parent, const char *map, const char *executable_key) 740{ 741 char *key = NULL, *options = NULL, *mountpoint = NULL, 742 *options2 = NULL, *location = NULL; 743 int ret; 744 struct node *node; 745 746 lineno = 1; 747 748 if (executable_key != NULL) 749 key = checked_strdup(executable_key); 750 751 for (;;) { 752 ret = yylex(); 753 if (ret == 0 || ret == NEWLINE) { 754 /* 755 * In case of executable map, the key is always 756 * non-NULL, even if the map is empty. So, make sure 757 * we don't fail empty maps here. 758 */ 759 if ((key != NULL && executable_key == NULL) || 760 options != NULL) { 761 log_errx(1, "truncated entry at %s, line %d", 762 map, lineno); 763 } 764 if (ret == 0 || executable_key != NULL) { 765 /* 766 * End of file. 767 */ 768 break; 769 } else { 770 key = options = NULL; 771 continue; 772 } 773 } 774 if (key == NULL) { 775 key = checked_strdup(yytext); 776 if (key[0] == '+') { 777 node_new(parent, key, NULL, NULL, map, lineno); 778 key = options = NULL; 779 continue; 780 } 781 continue; 782 } else if (yytext[0] == '-') { 783 if (options != NULL) { 784 log_errx(1, "duplicated options at %s, line %d", 785 map, lineno); 786 } 787 /* 788 * +1 to skip leading "-". 789 */ 790 options = checked_strdup(yytext + 1); 791 continue; 792 } 793 794 /* 795 * We cannot properly handle a situation where the map key 796 * is "/". Ignore such entries. 797 * 798 * XXX: According to Piete Brooks, Linux automounter uses 799 * "/" as a wildcard character in LDAP maps. Perhaps 800 * we should work around this braindamage by substituting 801 * "*" for "/"? 802 */ 803 if (strcmp(key, "/") == 0) { 804 log_warnx("nonsensical map key \"/\" at %s, line %d; " 805 "ignoring map entry ", map, lineno); 806 807 /* 808 * Skip the rest of the entry. 809 */ 810 do { 811 ret = yylex(); 812 } while (ret != 0 && ret != NEWLINE); 813 814 key = options = NULL; 815 continue; 816 } 817 818 //log_debugx("adding map node, %s", key); 819 node = node_new(parent, key, options, NULL, map, lineno); 820 key = options = NULL; 821 822 for (;;) { 823 if (yytext[0] == '/') { 824 if (mountpoint != NULL) { 825 log_errx(1, "duplicated mountpoint " 826 "in %s, line %d", map, lineno); 827 } 828 if (options2 != NULL || location != NULL) { 829 log_errx(1, "mountpoint out of order " 830 "in %s, line %d", map, lineno); 831 } 832 mountpoint = checked_strdup(yytext); 833 goto again; 834 } 835 836 if (yytext[0] == '-') { 837 if (options2 != NULL) { 838 log_errx(1, "duplicated options " 839 "in %s, line %d", map, lineno); 840 } 841 if (location != NULL) { 842 log_errx(1, "options out of order " 843 "in %s, line %d", map, lineno); 844 } 845 options2 = checked_strdup(yytext + 1); 846 goto again; 847 } 848 849 if (location != NULL) { 850 log_errx(1, "too many arguments " 851 "in %s, line %d", map, lineno); 852 } 853 854 /* 855 * If location field starts with colon, e.g. ":/dev/cd0", 856 * then strip it. 857 */ 858 if (yytext[0] == ':') { 859 location = checked_strdup(yytext + 1); 860 if (location[0] == '\0') { 861 log_errx(1, "empty location in %s, " 862 "line %d", map, lineno); 863 } 864 } else { 865 location = checked_strdup(yytext); 866 } 867 868 if (mountpoint == NULL) 869 mountpoint = checked_strdup("/"); 870 if (options2 == NULL) 871 options2 = checked_strdup(""); 872 873#if 0 874 log_debugx("adding map node, %s %s %s", 875 mountpoint, options2, location); 876#endif 877 node_new(node, mountpoint, options2, location, 878 map, lineno); 879 mountpoint = options2 = location = NULL; 880again: 881 ret = yylex(); 882 if (ret == 0 || ret == NEWLINE) { 883 if (mountpoint != NULL || options2 != NULL || 884 location != NULL) { 885 log_errx(1, "truncated entry " 886 "in %s, line %d", map, lineno); 887 } 888 break; 889 } 890 } 891 } 892} 893 894/* 895 * Parse output of a special map called without argument. It is a list 896 * of keys, separated by newlines. They can contain whitespace, so use 897 * getline(3) instead of lexer used for maps. 898 */ 899static void 900parse_map_keys_yyin(struct node *parent, const char *map) 901{ 902 char *line = NULL, *key; 903 size_t linecap = 0; 904 ssize_t linelen; 905 906 lineno = 1; 907 908 for (;;) { 909 linelen = getline(&line, &linecap, yyin); 910 if (linelen < 0) { 911 /* 912 * End of file. 913 */ 914 break; 915 } 916 if (linelen <= 1) { 917 /* 918 * Empty line, consisting of just the newline. 919 */ 920 continue; 921 } 922 923 /* 924 * "-1" to strip the trailing newline. 925 */ 926 key = strndup(line, linelen - 1); 927 928 log_debugx("adding key \"%s\"", key); 929 node_new(parent, key, NULL, NULL, map, lineno); 930 lineno++; 931 } 932 free(line); 933} 934 935static bool 936file_is_executable(const char *path) 937{ 938 struct stat sb; 939 int error; 940 941 error = stat(path, &sb); 942 if (error != 0) 943 log_err(1, "cannot stat %s", path); 944 if ((sb.st_mode & S_IXUSR) || (sb.st_mode & S_IXGRP) || 945 (sb.st_mode & S_IXOTH)) 946 return (true); 947 return (false); 948} 949 950/* 951 * Parse a special map, e.g. "-hosts". 952 */ 953static void 954parse_special_map(struct node *parent, const char *map, const char *key) 955{ 956 char *path; 957 int error, ret; 958 959 assert(map[0] == '-'); 960 961 /* 962 * +1 to skip leading "-" in map name. 963 */ 964 ret = asprintf(&path, "%s/special_%s", AUTO_SPECIAL_PREFIX, map + 1); 965 if (ret < 0) 966 log_err(1, "asprintf"); 967 968 yyin = auto_popen(path, key, NULL); 969 assert(yyin != NULL); 970 971 if (key == NULL) { 972 parse_map_keys_yyin(parent, map); 973 } else { 974 parse_map_yyin(parent, map, key); 975 } 976 977 error = auto_pclose(yyin); 978 yyin = NULL; 979 if (error != 0) 980 log_errx(1, "failed to handle special map \"%s\"", map); 981 982 node_expand_includes(parent, false); 983 node_expand_direct_maps(parent); 984 985 free(path); 986} 987 988/* 989 * Retrieve and parse map from directory services, e.g. LDAP. 990 * Note that it is different from executable maps, in that 991 * the include script outputs the whole map to standard output 992 * (as opposed to executable maps that only output a single 993 * entry, without the key), and it takes the map name as an 994 * argument, instead of key. 995 */ 996static void 997parse_included_map(struct node *parent, const char *map) 998{ 999 int error; 1000 1001 assert(map[0] != '-'); 1002 assert(map[0] != '/'); 1003 1004 error = access(AUTO_INCLUDE_PATH, F_OK); 1005 if (error != 0) { 1006 log_errx(1, "directory services not configured;" 1007 " %s does not exist", AUTO_INCLUDE_PATH); 1008 } 1009 1010 yyin = auto_popen(AUTO_INCLUDE_PATH, map, NULL); 1011 assert(yyin != NULL); 1012 1013 parse_map_yyin(parent, map, NULL); 1014 1015 error = auto_pclose(yyin); 1016 yyin = NULL; 1017 if (error != 0) 1018 log_errx(1, "failed to handle remote map \"%s\"", map); 1019 1020 node_expand_includes(parent, false); 1021 node_expand_direct_maps(parent); 1022} 1023 1024void 1025parse_map(struct node *parent, const char *map, const char *key, 1026 bool *wildcards) 1027{ 1028 char *path = NULL; 1029 int error, ret; 1030 bool executable; 1031 1032 assert(map != NULL); 1033 assert(map[0] != '\0'); 1034 1035 log_debugx("parsing map \"%s\"", map); 1036 1037 if (wildcards != NULL) 1038 *wildcards = false; 1039 1040 if (map[0] == '-') { 1041 if (wildcards != NULL) 1042 *wildcards = true; 1043 return (parse_special_map(parent, map, key)); 1044 } 1045 1046 if (map[0] == '/') { 1047 path = checked_strdup(map); 1048 } else { 1049 ret = asprintf(&path, "%s/%s", AUTO_MAP_PREFIX, map); 1050 if (ret < 0) 1051 log_err(1, "asprintf"); 1052 log_debugx("map \"%s\" maps to \"%s\"", map, path); 1053 1054 /* 1055 * See if the file exists. If not, try to obtain the map 1056 * from directory services. 1057 */ 1058 error = access(path, F_OK); 1059 if (error != 0) { 1060 log_debugx("map file \"%s\" does not exist; falling " 1061 "back to directory services", path); 1062 return (parse_included_map(parent, map)); 1063 } 1064 } 1065 1066 executable = file_is_executable(path); 1067 1068 if (executable) { 1069 log_debugx("map \"%s\" is executable", map); 1070 1071 if (wildcards != NULL) 1072 *wildcards = true; 1073 1074 if (key != NULL) { 1075 yyin = auto_popen(path, key, NULL); 1076 } else { 1077 yyin = auto_popen(path, NULL); 1078 } 1079 assert(yyin != NULL); 1080 } else { 1081 yyin = fopen(path, "r"); 1082 if (yyin == NULL) 1083 log_err(1, "unable to open \"%s\"", path); 1084 } 1085 1086 free(path); 1087 path = NULL; 1088 1089 parse_map_yyin(parent, map, executable ? key : NULL); 1090 1091 if (executable) { 1092 error = auto_pclose(yyin); 1093 yyin = NULL; 1094 if (error != 0) { 1095 log_errx(1, "failed to handle executable map \"%s\"", 1096 map); 1097 } 1098 } else { 1099 fclose(yyin); 1100 } 1101 yyin = NULL; 1102 1103 log_debugx("done parsing map \"%s\"", map); 1104 1105 node_expand_includes(parent, false); 1106 node_expand_direct_maps(parent); 1107} 1108 1109static void 1110parse_master_yyin(struct node *root, const char *master) 1111{ 1112 char *mountpoint = NULL, *map = NULL, *options = NULL; 1113 int ret; 1114 1115 /* 1116 * XXX: 1 gives incorrect values; wtf? 1117 */ 1118 lineno = 0; 1119 1120 for (;;) { 1121 ret = yylex(); 1122 if (ret == 0 || ret == NEWLINE) { 1123 if (mountpoint != NULL) { 1124 //log_debugx("adding map for %s", mountpoint); 1125 node_new_map(root, mountpoint, options, map, 1126 master, lineno); 1127 } 1128 if (ret == 0) { 1129 break; 1130 } else { 1131 mountpoint = map = options = NULL; 1132 continue; 1133 } 1134 } 1135 if (mountpoint == NULL) { 1136 mountpoint = checked_strdup(yytext); 1137 } else if (map == NULL) { 1138 map = checked_strdup(yytext); 1139 } else if (options == NULL) { 1140 /* 1141 * +1 to skip leading "-". 1142 */ 1143 options = checked_strdup(yytext + 1); 1144 } else { 1145 log_errx(1, "too many arguments at %s, line %d", 1146 master, lineno); 1147 } 1148 } 1149} 1150 1151void 1152parse_master(struct node *root, const char *master) 1153{ 1154 1155 log_debugx("parsing auto_master file at \"%s\"", master); 1156 1157 yyin = fopen(master, "r"); 1158 if (yyin == NULL) 1159 err(1, "unable to open %s", master); 1160 1161 parse_master_yyin(root, master); 1162 1163 fclose(yyin); 1164 yyin = NULL; 1165 1166 log_debugx("done parsing \"%s\"", master); 1167 1168 node_expand_includes(root, true); 1169 node_expand_direct_maps(root); 1170} 1171 1172/* 1173 * Two things daemon(3) does, that we actually also want to do 1174 * when running in foreground, is closing the stdin and chdiring 1175 * to "/". This is what we do here. 1176 */ 1177void 1178lesser_daemon(void) 1179{ 1180 int error, fd; 1181 1182 error = chdir("/"); 1183 if (error != 0) 1184 log_warn("chdir"); 1185 1186 fd = open(_PATH_DEVNULL, O_RDWR, 0); 1187 if (fd < 0) { 1188 log_warn("cannot open %s", _PATH_DEVNULL); 1189 return; 1190 } 1191 1192 error = dup2(fd, STDIN_FILENO); 1193 if (error != 0) 1194 log_warn("dup2"); 1195 1196 error = close(fd); 1197 if (error != 0) { 1198 /* Bloody hell. */ 1199 log_warn("close"); 1200 } 1201} 1202 1203int 1204main(int argc, char **argv) 1205{ 1206 char *cmdname; 1207 1208 if (argv[0] == NULL) 1209 log_errx(1, "NULL command name"); 1210 1211 cmdname = basename(argv[0]); 1212 1213 if (strcmp(cmdname, "automount") == 0) 1214 return (main_automount(argc, argv)); 1215 else if (strcmp(cmdname, "automountd") == 0) 1216 return (main_automountd(argc, argv)); 1217 else if (strcmp(cmdname, "autounmountd") == 0) 1218 return (main_autounmountd(argc, argv)); 1219 else 1220 log_errx(1, "binary name should be either \"automount\", " 1221 "\"automountd\", or \"autounmountd\""); 1222} 1223