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