2 * Copyright (c) 2014 The FreeBSD Foundation
5 * This software was developed by Edward Tomasz Napierala under sponsorship
6 * from the FreeBSD Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/types.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>
43 #include <sys/utsname.h>
62 #include "autofs_ioctl.h"
68 extern int yylex(void);
70 static void parse_master_yyin(struct node *root, const char *master);
71 static void parse_map_yyin(struct node *parent, const char *map,
72 const char *executable_key);
75 checked_strdup(const char *s)
88 * Take two pointers to strings, concatenate the contents with "/" in the
89 * middle, make the first pointer point to the result, the second pointer
90 * to NULL, and free the old strings.
92 * Concatenate pathnames, basically.
95 concat(char **p1, char **p2)
104 *p1 = checked_strdup("");
107 *p2 = checked_strdup("");
109 ret = asprintf(&path, "%s/%s", *p1, *p2);
111 log_err(1, "asprintf");
124 * Concatenate two strings, inserting separator between them, unless not needed.
126 * This function is very convenient to use when you do not care about freeing
127 * memory - which is okay here, because we are a short running process.
130 separated_concat(const char *s1, const char *s2, char separator)
138 if (s1[0] == '\0' || s2[0] == '\0' ||
139 s1[strlen(s1) - 1] == separator || s2[0] == separator) {
140 ret = asprintf(&result, "%s%s", s1, s2);
142 ret = asprintf(&result, "%s%c%s", s1, separator, s2);
145 log_err(1, "asprintf");
147 //log_debugx("separated_concat: got %s and %s, returning %s", s1, s2, result);
153 create_directory(const char *path)
155 char *component, *copy, *tofree, *partial;
158 assert(path[0] == '/');
161 * +1 to skip the leading slash.
163 copy = tofree = checked_strdup(path + 1);
167 component = strsep(©, "/");
168 if (component == NULL)
170 concat(&partial, &component);
171 //log_debugx("checking \"%s\" for existence", partial);
172 error = access(partial, F_OK);
176 log_err(1, "cannot access %s", partial);
177 log_debugx("directory %s does not exist, creating",
179 error = mkdir(partial, 0755);
181 log_err(1, "cannot create %s", partial);
192 n = calloc(1, sizeof(*n));
194 log_err(1, "calloc");
196 n->n_key = checked_strdup("/");
197 n->n_options = checked_strdup("");
199 TAILQ_INIT(&n->n_children);
205 node_new(struct node *parent, char *key, char *options, char *location,
206 const char *config_file, int config_line)
210 n = calloc(1, sizeof(*n));
212 log_err(1, "calloc");
214 TAILQ_INIT(&n->n_children);
218 n->n_options = options;
220 n->n_options = strdup("");
221 n->n_location = location;
222 assert(config_file != NULL);
223 n->n_config_file = config_file;
224 assert(config_line >= 0);
225 n->n_config_line = config_line;
227 assert(parent != NULL);
228 n->n_parent = parent;
229 TAILQ_INSERT_TAIL(&parent->n_children, n, n_next);
235 node_new_map(struct node *parent, char *key, char *options, char *map,
236 const char *config_file, int config_line)
240 n = calloc(1, sizeof(*n));
242 log_err(1, "calloc");
244 TAILQ_INIT(&n->n_children);
248 n->n_options = options;
250 n->n_options = strdup("");
252 assert(config_file != NULL);
253 n->n_config_file = config_file;
254 assert(config_line >= 0);
255 n->n_config_line = config_line;
257 assert(parent != NULL);
258 n->n_parent = parent;
259 TAILQ_INSERT_TAIL(&parent->n_children, n, n_next);
265 node_duplicate(const struct node *o, struct node *parent)
267 const struct node *child;
271 parent = o->n_parent;
273 n = node_new(parent, o->n_key, o->n_options, o->n_location,
274 o->n_config_file, o->n_config_line);
276 TAILQ_FOREACH(child, &o->n_children, n_next)
277 node_duplicate(child, n);
283 node_delete(struct node *n)
285 struct node *child, *tmp;
289 TAILQ_FOREACH_SAFE(child, &n->n_children, n_next, tmp)
292 if (n->n_parent != NULL)
293 TAILQ_REMOVE(&n->n_parent->n_children, n, n_next);
299 * Move (reparent) node 'n' to make it sibling of 'previous', placed
303 node_move_after(struct node *n, struct node *previous)
306 TAILQ_REMOVE(&n->n_parent->n_children, n, n_next);
307 n->n_parent = previous->n_parent;
308 TAILQ_INSERT_AFTER(&previous->n_parent->n_children, previous, n, n_next);
312 node_expand_includes(struct node *root, bool is_master)
314 struct node *n, *n2, *tmp, *tmp2, *tmproot;
317 TAILQ_FOREACH_SAFE(n, &root->n_children, n_next, tmp) {
318 if (n->n_key[0] != '+')
321 error = access(AUTO_INCLUDE_PATH, F_OK);
323 log_errx(1, "directory services not configured; "
324 "%s does not exist", AUTO_INCLUDE_PATH);
328 * "+1" to skip leading "+".
330 yyin = auto_popen(AUTO_INCLUDE_PATH, n->n_key + 1, NULL);
331 assert(yyin != NULL);
333 tmproot = node_new_root();
335 parse_master_yyin(tmproot, n->n_key);
337 parse_map_yyin(tmproot, n->n_key, NULL);
339 error = auto_pclose(yyin);
342 log_errx(1, "failed to handle include \"%s\"",
347 * Entries to be included are now in tmproot. We need to merge
348 * them with the rest, preserving their place and ordering.
350 TAILQ_FOREACH_REVERSE_SAFE(n2,
351 &tmproot->n_children, nodehead, n_next, tmp2) {
352 node_move_after(n2, n);
356 node_delete(tmproot);
361 expand_ampersand(char *string, const char *key)
364 int i, ret, before_len = 0;
365 bool backslashed = false;
367 assert(key[0] != '\0');
369 expanded = checked_strdup(string);
371 for (i = 0; string[i] != '\0'; i++) {
373 if (c == '\\' && backslashed == false) {
386 * The 'before_len' variable contains the number
387 * of characters before the '&'.
390 //assert(i + 1 < (int)strlen(string));
392 ret = asprintf(&expanded, "%.*s%s%s",
393 before_len, string, key, string + before_len + 1);
395 log_err(1, "asprintf");
397 //log_debugx("\"%s\" expanded with key \"%s\" to \"%s\"",
398 // string, key, expanded);
401 * Figure out where to start searching for next variable.
404 i = before_len + strlen(key);
406 //assert(i < (int)strlen(string));
413 * Expand "&" in n_location. If the key is NULL, try to use
414 * key from map entries themselves. Keep in mind that maps
415 * consist of tho levels of node structures, the key is one
418 * Variant with NULL key is for "automount -LL".
421 node_expand_ampersand(struct node *n, const char *key)
425 if (n->n_location != NULL) {
427 if (n->n_parent != NULL &&
428 strcmp(n->n_parent->n_key, "*") != 0) {
429 n->n_location = expand_ampersand(n->n_location,
433 n->n_location = expand_ampersand(n->n_location, key);
437 TAILQ_FOREACH(child, &n->n_children, n_next)
438 node_expand_ampersand(child, key);
442 * Expand "*" in n_key.
445 node_expand_wildcard(struct node *n, const char *key)
447 struct node *child, *expanded;
451 if (strcmp(n->n_key, "*") == 0) {
452 expanded = node_duplicate(n, NULL);
453 expanded->n_key = checked_strdup(key);
454 node_move_after(expanded, n);
457 TAILQ_FOREACH(child, &n->n_children, n_next)
458 node_expand_wildcard(child, key);
462 node_expand_defined(struct node *n)
465 int error, cumulated_error = 0;
467 if (n->n_location != NULL) {
468 n->n_location = defined_expand(n->n_location);
469 if (n->n_location == NULL) {
470 log_warnx("failed to expand location for %s",
476 TAILQ_FOREACH(child, &n->n_children, n_next) {
477 error = node_expand_defined(child);
478 if (error != 0 && cumulated_error == 0)
479 cumulated_error = error;
482 return (cumulated_error);
486 node_is_direct_map(const struct node *n)
490 assert(n->n_parent != NULL);
491 if (n->n_parent->n_parent == NULL)
496 assert(n->n_key != NULL);
497 if (strcmp(n->n_key, "/-") != 0)
504 node_expand_maps(struct node *n, bool indirect)
506 struct node *child, *tmp;
508 TAILQ_FOREACH_SAFE(child, &n->n_children, n_next, tmp) {
509 if (node_is_direct_map(child)) {
513 if (indirect == false)
518 * This is the first-level map node; the one that contains
519 * the key and subnodes with mountpoints and actual map names.
521 if (child->n_map == NULL)
525 log_debugx("map \"%s\" is an indirect map, parsing",
528 log_debugx("map \"%s\" is a direct map, parsing",
531 parse_map(child, child->n_map, NULL);
536 node_expand_direct_maps(struct node *n)
539 node_expand_maps(n, false);
543 node_expand_indirect_maps(struct node *n)
546 node_expand_maps(n, true);
550 node_path_x(const struct node *n, char *x)
555 if (n->n_parent == NULL)
559 * Return "/-" for direct maps only if we were asked for path
560 * to the "/-" node itself, not to any of its subnodes.
562 if (n->n_parent->n_parent == NULL &&
563 strcmp(n->n_key, "/-") == 0 &&
568 path = separated_concat(n->n_key, x, '/');
572 * Strip trailing slash.
576 if (path[len - 1] == '/')
577 path[len - 1] = '\0';
579 return (node_path_x(n->n_parent, path));
583 * Return full path for node, consisting of concatenated
584 * paths of node itself and all its parents, up to the root.
587 node_path(const struct node *n)
590 return (node_path_x(n, checked_strdup("")));
594 node_options_x(const struct node *n, char *x)
598 options = separated_concat(x, n->n_options, ',');
599 if (n->n_parent == NULL)
602 return (node_options_x(n->n_parent, options));
606 * Return options for node, consisting of concatenated
607 * options from the node itself and all its parents,
611 node_options(const struct node *n)
614 return (node_options_x(n, checked_strdup("")));
618 node_print_indent(const struct node *n, int indent)
620 const struct node *child, *first_child;
621 char *path, *options;
624 options = node_options(n);
627 * Do not show both parent and child node if they have the same
628 * mountpoint; only show the child node. This means the typical,
629 * "key location", map entries are shown in a single line;
630 * the "key mountpoint1 location2 mountpoint2 location2" entries
631 * take multiple lines.
633 first_child = TAILQ_FIRST(&n->n_children);
634 if (first_child == NULL || TAILQ_NEXT(first_child, n_next) != NULL ||
635 strcmp(path, node_path(first_child)) != 0) {
636 assert(n->n_location == NULL || n->n_map == NULL);
637 printf("%*.s%-*s %s%-*s %-*s # %s map %s at %s:%d\n",
641 options[0] != '\0' ? "-" : " ",
643 options[0] != '\0' ? options : "",
645 n->n_location != NULL ? n->n_location : n->n_map != NULL ? n->n_map : "",
646 node_is_direct_map(n) ? "direct" : "indirect",
647 indent == 0 ? "referenced" : "defined",
648 n->n_config_file, n->n_config_line);
654 TAILQ_FOREACH(child, &n->n_children, n_next)
655 node_print_indent(child, indent + 2);
659 node_print(const struct node *n)
661 const struct node *child;
663 TAILQ_FOREACH(child, &n->n_children, n_next)
664 node_print_indent(child, 0);
668 node_find(struct node *node, const char *path)
670 struct node *child, *found;
673 //log_debugx("looking up %s in %s", path, node->n_key);
675 tmp = node_path(node);
676 if (strncmp(tmp, path, strlen(tmp)) != 0) {
682 TAILQ_FOREACH(child, &node->n_children, n_next) {
683 found = node_find(child, path);
692 * Canonical form of a map entry looks like this:
694 * key [-options] [ [/mountpoint] [-options2] location ... ]
696 * Entries for executable maps are slightly different, as they
697 * lack the 'key' field and are always single-line; the key field
698 * for those maps is taken from 'executable_key' argument.
700 * We parse it in such a way that a map always has two levels - first
701 * for key, and the second, for the mountpoint.
704 parse_map_yyin(struct node *parent, const char *map, const char *executable_key)
706 char *key = NULL, *options = NULL, *mountpoint = NULL,
707 *options2 = NULL, *location = NULL;
713 if (executable_key != NULL)
714 key = checked_strdup(executable_key);
718 if (ret == 0 || ret == NEWLINE) {
720 * In case of executable map, the key is always
721 * non-NULL, even if the map is empty. So, make sure
722 * we don't fail empty maps here.
724 if ((key != NULL && executable_key == NULL) ||
726 log_errx(1, "truncated entry at %s, line %d",
729 if (ret == 0 || executable_key != NULL) {
735 key = options = NULL;
740 key = checked_strdup(yytext);
742 node_new(parent, key, NULL, NULL, map, lineno);
743 key = options = NULL;
747 } else if (yytext[0] == '-') {
748 if (options != NULL) {
749 log_errx(1, "duplicated options at %s, line %d",
753 * +1 to skip leading "-".
755 options = checked_strdup(yytext + 1);
760 * We cannot properly handle a situation where the map key
761 * is "/". Ignore such entries.
763 * XXX: According to Piete Brooks, Linux automounter uses
764 * "/" as a wildcard character in LDAP maps. Perhaps
765 * we should work around this braindamage by substituting
768 if (strcmp(key, "/") == 0) {
769 log_warnx("nonsensical map key \"/\" at %s, line %d; "
770 "ignoring map entry ", map, lineno);
773 * Skip the rest of the entry.
777 } while (ret != 0 && ret != NEWLINE);
779 key = options = NULL;
783 //log_debugx("adding map node, %s", key);
784 node = node_new(parent, key, options, NULL, map, lineno);
785 key = options = NULL;
788 if (yytext[0] == '/') {
789 if (mountpoint != NULL) {
790 log_errx(1, "duplicated mountpoint "
791 "in %s, line %d", map, lineno);
793 if (options2 != NULL || location != NULL) {
794 log_errx(1, "mountpoint out of order "
795 "in %s, line %d", map, lineno);
797 mountpoint = checked_strdup(yytext);
801 if (yytext[0] == '-') {
802 if (options2 != NULL) {
803 log_errx(1, "duplicated options "
804 "in %s, line %d", map, lineno);
806 if (location != NULL) {
807 log_errx(1, "options out of order "
808 "in %s, line %d", map, lineno);
810 options2 = checked_strdup(yytext + 1);
814 if (location != NULL) {
815 log_errx(1, "too many arguments "
816 "in %s, line %d", map, lineno);
820 * If location field starts with colon, e.g. ":/dev/cd0",
823 if (yytext[0] == ':') {
824 location = checked_strdup(yytext + 1);
825 if (location[0] == '\0') {
826 log_errx(1, "empty location in %s, "
827 "line %d", map, lineno);
830 location = checked_strdup(yytext);
833 if (mountpoint == NULL)
834 mountpoint = checked_strdup("/");
835 if (options2 == NULL)
836 options2 = checked_strdup("");
839 log_debugx("adding map node, %s %s %s",
840 mountpoint, options2, location);
842 node_new(node, mountpoint, options2, location,
844 mountpoint = options2 = location = NULL;
847 if (ret == 0 || ret == NEWLINE) {
848 if (mountpoint != NULL || options2 != NULL ||
850 log_errx(1, "truncated entry "
851 "in %s, line %d", map, lineno);
860 * Parse output of a special map called without argument. This is just
864 parse_map_keys_yyin(struct node *parent, const char *map)
884 key = checked_strdup(yytext);
885 node_new(parent, key, NULL, NULL, map, lineno);
890 file_is_executable(const char *path)
895 error = stat(path, &sb);
897 log_err(1, "cannot stat %s", path);
898 if ((sb.st_mode & S_IXUSR) || (sb.st_mode & S_IXGRP) ||
899 (sb.st_mode & S_IXOTH))
905 * Parse a special map, e.g. "-hosts".
908 parse_special_map(struct node *parent, const char *map, const char *key)
913 assert(map[0] == '-');
916 * +1 to skip leading "-" in map name.
918 ret = asprintf(&path, "%s/special_%s", AUTO_SPECIAL_PREFIX, map + 1);
920 log_err(1, "asprintf");
922 yyin = auto_popen(path, key, NULL);
923 assert(yyin != NULL);
926 parse_map_keys_yyin(parent, map);
928 parse_map_yyin(parent, map, key);
931 error = auto_pclose(yyin);
934 log_errx(1, "failed to handle special map \"%s\"", map);
936 node_expand_includes(parent, false);
937 node_expand_direct_maps(parent);
943 * Retrieve and parse map from directory services, e.g. LDAP.
944 * Note that it is different from executable maps, in that
945 * the include script outputs the whole map to standard output
946 * (as opposed to executable maps that only output a single
947 * entry, without the key), and it takes the map name as an
948 * argument, instead of key.
951 parse_included_map(struct node *parent, const char *map)
955 assert(map[0] != '-');
956 assert(map[0] != '/');
958 error = access(AUTO_INCLUDE_PATH, F_OK);
960 log_errx(1, "directory services not configured;"
961 " %s does not exist", AUTO_INCLUDE_PATH);
964 yyin = auto_popen(AUTO_INCLUDE_PATH, map, NULL);
965 assert(yyin != NULL);
967 parse_map_yyin(parent, map, NULL);
969 error = auto_pclose(yyin);
972 log_errx(1, "failed to handle remote map \"%s\"", map);
974 node_expand_includes(parent, false);
975 node_expand_direct_maps(parent);
979 parse_map(struct node *parent, const char *map, const char *key)
986 assert(map[0] != '\0');
988 log_debugx("parsing map \"%s\"", map);
991 return (parse_special_map(parent, map, key));
994 path = checked_strdup(map);
996 ret = asprintf(&path, "%s/%s", AUTO_MAP_PREFIX, map);
998 log_err(1, "asprintf");
999 log_debugx("map \"%s\" maps to \"%s\"", map, path);
1002 * See if the file exists. If not, try to obtain the map
1003 * from directory services.
1005 error = access(path, F_OK);
1007 log_debugx("map file \"%s\" does not exist; falling "
1008 "back to directory services", path);
1009 return (parse_included_map(parent, map));
1013 executable = file_is_executable(path);
1016 log_debugx("map \"%s\" is executable", map);
1019 yyin = auto_popen(path, key, NULL);
1021 yyin = auto_popen(path, NULL);
1023 assert(yyin != NULL);
1025 yyin = fopen(path, "r");
1027 log_err(1, "unable to open \"%s\"", path);
1033 parse_map_yyin(parent, map, executable ? key : NULL);
1036 error = auto_pclose(yyin);
1039 log_errx(1, "failed to handle executable map \"%s\"",
1047 log_debugx("done parsing map \"%s\"", map);
1049 node_expand_includes(parent, false);
1050 node_expand_direct_maps(parent);
1054 parse_master_yyin(struct node *root, const char *master)
1056 char *mountpoint = NULL, *map = NULL, *options = NULL;
1060 * XXX: 1 gives incorrect values; wtf?
1066 if (ret == 0 || ret == NEWLINE) {
1067 if (mountpoint != NULL) {
1068 //log_debugx("adding map for %s", mountpoint);
1069 node_new_map(root, mountpoint, options, map,
1075 mountpoint = map = options = NULL;
1079 if (mountpoint == NULL) {
1080 mountpoint = checked_strdup(yytext);
1081 } else if (map == NULL) {
1082 map = checked_strdup(yytext);
1083 } else if (options == NULL) {
1085 * +1 to skip leading "-".
1087 options = checked_strdup(yytext + 1);
1089 log_errx(1, "too many arguments at %s, line %d",
1096 parse_master(struct node *root, const char *master)
1099 log_debugx("parsing auto_master file at \"%s\"", master);
1101 yyin = fopen(master, "r");
1103 err(1, "unable to open %s", master);
1105 parse_master_yyin(root, master);
1110 log_debugx("done parsing \"%s\"", master);
1112 node_expand_includes(root, true);
1113 node_expand_direct_maps(root);
1117 * Two things daemon(3) does, that we actually also want to do
1118 * when running in foreground, is closing the stdin and chdiring
1119 * to "/". This is what we do here.
1130 fd = open(_PATH_DEVNULL, O_RDWR, 0);
1132 log_warn("cannot open %s", _PATH_DEVNULL);
1136 error = dup2(fd, STDIN_FILENO);
1148 main(int argc, char **argv)
1152 if (argv[0] == NULL)
1153 log_errx(1, "NULL command name");
1155 cmdname = basename(argv[0]);
1157 if (strcmp(cmdname, "automount") == 0)
1158 return (main_automount(argc, argv));
1159 else if (strcmp(cmdname, "automountd") == 0)
1160 return (main_automountd(argc, argv));
1161 else if (strcmp(cmdname, "autounmountd") == 0)
1162 return (main_autounmountd(argc, argv));
1164 log_errx(1, "binary name should be either \"automount\", "
1165 "\"automountd\", or \"autounmountd\"");