2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2014 The FreeBSD Foundation
7 * This software was developed by Edward Tomasz Napierala under sponsorship
8 * from the FreeBSD Foundation.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <sys/types.h>
38 #include <sys/ioctl.h>
39 #include <sys/param.h>
40 #include <sys/linker.h>
41 #include <sys/mount.h>
42 #include <sys/socket.h>
45 #include <sys/utsname.h>
63 #include "autofs_ioctl.h"
69 extern int yylex(void);
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);
76 checked_strdup(const char *s)
89 * Concatenate two strings, inserting separator between them, unless not needed.
92 concat(const char *s1, char separator, const char *s2)
106 s1last = s1[strlen(s1) - 1];
110 if (s1last == separator && s2first == separator) {
112 * If s1 ends with the separator and s2 begins with
113 * it - skip the latter; otherwise concatenating "/"
114 * and "/foo" would end up returning "//foo".
116 ret = asprintf(&result, "%s%s", s1, s2 + 1);
117 } else if (s1last == separator || s2first == separator ||
118 s1[0] == '\0' || s2[0] == '\0') {
119 ret = asprintf(&result, "%s%s", s1, s2);
121 ret = asprintf(&result, "%s%c%s", s1, separator, s2);
124 log_err(1, "asprintf");
126 //log_debugx("%s: got %s and %s, returning %s", __func__, s1, s2, result);
132 create_directory(const char *path)
134 char *component, *copy, *tofree, *partial, *tmp;
137 assert(path[0] == '/');
140 * +1 to skip the leading slash.
142 copy = tofree = checked_strdup(path + 1);
144 partial = checked_strdup("");
146 component = strsep(©, "/");
147 if (component == NULL)
149 tmp = concat(partial, '/', component);
152 //log_debugx("creating \"%s\"", partial);
153 error = mkdir(partial, 0755);
154 if (error != 0 && errno != EEXIST) {
155 log_warn("cannot create %s", partial);
168 n = calloc(1, sizeof(*n));
170 log_err(1, "calloc");
172 n->n_key = checked_strdup("/");
173 n->n_options = checked_strdup("");
175 TAILQ_INIT(&n->n_children);
181 node_new(struct node *parent, char *key, char *options, char *location,
182 const char *config_file, int config_line)
186 n = calloc(1, sizeof(*n));
188 log_err(1, "calloc");
190 TAILQ_INIT(&n->n_children);
192 assert(key[0] != '\0');
195 n->n_options = options;
197 n->n_options = strdup("");
198 n->n_location = location;
199 assert(config_file != NULL);
200 n->n_config_file = config_file;
201 assert(config_line >= 0);
202 n->n_config_line = config_line;
204 assert(parent != NULL);
205 n->n_parent = parent;
206 TAILQ_INSERT_TAIL(&parent->n_children, n, n_next);
212 node_new_map(struct node *parent, char *key, char *options, char *map,
213 const char *config_file, int config_line)
217 n = calloc(1, sizeof(*n));
219 log_err(1, "calloc");
221 TAILQ_INIT(&n->n_children);
223 assert(key[0] != '\0');
226 n->n_options = options;
228 n->n_options = strdup("");
230 assert(config_file != NULL);
231 n->n_config_file = config_file;
232 assert(config_line >= 0);
233 n->n_config_line = config_line;
235 assert(parent != NULL);
236 n->n_parent = parent;
237 TAILQ_INSERT_TAIL(&parent->n_children, n, n_next);
243 node_duplicate(const struct node *o, struct node *parent)
245 const struct node *child;
249 parent = o->n_parent;
251 n = node_new(parent, o->n_key, o->n_options, o->n_location,
252 o->n_config_file, o->n_config_line);
254 TAILQ_FOREACH(child, &o->n_children, n_next)
255 node_duplicate(child, n);
261 node_delete(struct node *n)
263 struct node *child, *tmp;
267 TAILQ_FOREACH_SAFE(child, &n->n_children, n_next, tmp)
270 if (n->n_parent != NULL)
271 TAILQ_REMOVE(&n->n_parent->n_children, n, n_next);
277 * Move (reparent) node 'n' to make it sibling of 'previous', placed
281 node_move_after(struct node *n, struct node *previous)
284 TAILQ_REMOVE(&n->n_parent->n_children, n, n_next);
285 n->n_parent = previous->n_parent;
286 TAILQ_INSERT_AFTER(&previous->n_parent->n_children, previous, n, n_next);
290 node_expand_includes(struct node *root, bool is_master)
292 struct node *n, *n2, *tmp, *tmp2, *tmproot;
295 TAILQ_FOREACH_SAFE(n, &root->n_children, n_next, tmp) {
296 if (n->n_key[0] != '+')
299 error = access(AUTO_INCLUDE_PATH, F_OK);
301 log_errx(1, "directory services not configured; "
302 "%s does not exist", AUTO_INCLUDE_PATH);
306 * "+1" to skip leading "+".
308 yyin = auto_popen(AUTO_INCLUDE_PATH, n->n_key + 1, NULL);
309 assert(yyin != NULL);
311 tmproot = node_new_root();
313 parse_master_yyin(tmproot, n->n_key);
315 parse_map_yyin(tmproot, n->n_key, NULL);
317 error = auto_pclose(yyin);
320 log_errx(1, "failed to handle include \"%s\"",
325 * Entries to be included are now in tmproot. We need to merge
326 * them with the rest, preserving their place and ordering.
328 TAILQ_FOREACH_REVERSE_SAFE(n2,
329 &tmproot->n_children, nodehead, n_next, tmp2) {
330 node_move_after(n2, n);
334 node_delete(tmproot);
339 expand_ampersand(char *string, const char *key)
342 int i, ret, before_len = 0;
343 bool backslashed = false;
345 assert(key[0] != '\0');
347 expanded = checked_strdup(string);
349 for (i = 0; string[i] != '\0'; i++) {
351 if (c == '\\' && backslashed == false) {
364 * The 'before_len' variable contains the number
365 * of characters before the '&'.
368 //assert(i < (int)strlen(string));
370 ret = asprintf(&expanded, "%.*s%s%s",
371 before_len, string, key, string + before_len + 1);
373 log_err(1, "asprintf");
375 //log_debugx("\"%s\" expanded with key \"%s\" to \"%s\"",
376 // string, key, expanded);
379 * Figure out where to start searching for next variable.
382 i = before_len + strlen(key);
383 if (i == (int)strlen(string))
386 //assert(i < (int)strlen(string));
393 * Expand "&" in n_location. If the key is NULL, try to use
394 * key from map entries themselves. Keep in mind that maps
395 * consist of tho levels of node structures, the key is one
398 * Variant with NULL key is for "automount -LL".
401 node_expand_ampersand(struct node *n, const char *key)
405 if (n->n_location != NULL) {
407 if (n->n_parent != NULL &&
408 strcmp(n->n_parent->n_key, "*") != 0) {
409 n->n_location = expand_ampersand(n->n_location,
413 n->n_location = expand_ampersand(n->n_location, key);
417 TAILQ_FOREACH(child, &n->n_children, n_next)
418 node_expand_ampersand(child, key);
422 * Expand "*" in n_key.
425 node_expand_wildcard(struct node *n, const char *key)
427 struct node *child, *expanded;
431 if (strcmp(n->n_key, "*") == 0) {
432 expanded = node_duplicate(n, NULL);
433 expanded->n_key = checked_strdup(key);
434 node_move_after(expanded, n);
437 TAILQ_FOREACH(child, &n->n_children, n_next)
438 node_expand_wildcard(child, key);
442 node_expand_defined(struct node *n)
445 int error, cumulated_error = 0;
447 if (n->n_location != NULL) {
448 n->n_location = defined_expand(n->n_location);
449 if (n->n_location == NULL) {
450 log_warnx("failed to expand location for %s",
456 TAILQ_FOREACH(child, &n->n_children, n_next) {
457 error = node_expand_defined(child);
458 if (error != 0 && cumulated_error == 0)
459 cumulated_error = error;
462 return (cumulated_error);
466 node_is_direct_key(const struct node *n)
469 if (n->n_parent != NULL && n->n_parent->n_parent == NULL &&
470 strcmp(n->n_key, "/-") == 0) {
478 node_is_direct_map(const struct node *n)
482 assert(n->n_parent != NULL);
483 if (n->n_parent->n_parent == NULL)
488 return (node_is_direct_key(n));
492 node_has_wildcards(const struct node *n)
494 const struct node *child;
496 TAILQ_FOREACH(child, &n->n_children, n_next) {
497 if (strcmp(child->n_key, "*") == 0)
505 node_expand_maps(struct node *n, bool indirect)
507 struct node *child, *tmp;
509 TAILQ_FOREACH_SAFE(child, &n->n_children, n_next, tmp) {
510 if (node_is_direct_map(child)) {
514 if (indirect == false)
519 * This is the first-level map node; the one that contains
520 * the key and subnodes with mountpoints and actual map names.
522 if (child->n_map == NULL)
526 log_debugx("map \"%s\" is an indirect map, parsing",
529 log_debugx("map \"%s\" is a direct map, parsing",
532 parse_map(child, child->n_map, NULL, NULL);
537 node_expand_direct_maps(struct node *n)
540 node_expand_maps(n, false);
544 node_expand_indirect_maps(struct node *n)
547 node_expand_maps(n, true);
551 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 (node_is_direct_key(n) && x[0] != '\0')
565 assert(n->n_key[0] != '\0');
566 path = concat(n->n_key, '/', x);
569 return (node_path_x(n->n_parent, path));
573 * Return full path for node, consisting of concatenated
574 * paths of node itself and all its parents, up to the root.
577 node_path(const struct node *n)
582 path = node_path_x(n, checked_strdup(""));
585 * Strip trailing slash, unless the whole path is "/".
588 if (len > 1 && path[len - 1] == '/')
589 path[len - 1] = '\0';
595 node_options_x(const struct node *n, char *x)
602 options = concat(x, ',', n->n_options);
605 return (node_options_x(n->n_parent, options));
609 * Return options for node, consisting of concatenated
610 * options from the node itself and all its parents,
614 node_options(const struct node *n)
617 return (node_options_x(n, checked_strdup("")));
621 node_print_indent(const struct node *n, const char *cmdline_options,
624 const struct node *child, *first_child;
625 char *path, *options, *tmp;
628 tmp = node_options(n);
629 options = concat(cmdline_options, ',', tmp);
633 * Do not show both parent and child node if they have the same
634 * mountpoint; only show the child node. This means the typical,
635 * "key location", map entries are shown in a single line;
636 * the "key mountpoint1 location2 mountpoint2 location2" entries
637 * take multiple lines.
639 first_child = TAILQ_FIRST(&n->n_children);
640 if (first_child == NULL || TAILQ_NEXT(first_child, n_next) != NULL ||
641 strcmp(path, node_path(first_child)) != 0) {
642 assert(n->n_location == NULL || n->n_map == NULL);
643 printf("%*.s%-*s %s%-*s %-*s # %s map %s at %s:%d\n",
647 options[0] != '\0' ? "-" : " ",
649 options[0] != '\0' ? options : "",
651 n->n_location != NULL ? n->n_location : n->n_map != NULL ? n->n_map : "",
652 node_is_direct_map(n) ? "direct" : "indirect",
653 indent == 0 ? "referenced" : "defined",
654 n->n_config_file, n->n_config_line);
660 TAILQ_FOREACH(child, &n->n_children, n_next)
661 node_print_indent(child, cmdline_options, indent + 2);
665 * Recursively print node with all its children. The cmdline_options
666 * argument is used for additional options to be prepended to all the
667 * others - usually those are the options passed by command line.
670 node_print(const struct node *n, const char *cmdline_options)
672 const struct node *child;
674 TAILQ_FOREACH(child, &n->n_children, n_next)
675 node_print_indent(child, cmdline_options, 0);
679 node_find_x(struct node *node, const char *path)
681 struct node *child, *found;
685 //log_debugx("looking up %s in %s", path, node_path(node));
687 if (!node_is_direct_key(node)) {
688 tmp = node_path(node);
689 tmplen = strlen(tmp);
690 if (strncmp(tmp, path, tmplen) != 0) {
694 if (path[tmplen] != '/' && path[tmplen] != '\0') {
696 * If we have two map entries like 'foo' and 'foobar', make
697 * sure the search for 'foobar' won't match 'foo' instead.
705 TAILQ_FOREACH(child, &node->n_children, n_next) {
706 found = node_find_x(child, path);
711 if (node->n_parent == NULL || node_is_direct_key(node))
718 node_find(struct node *root, const char *path)
722 assert(root->n_parent == NULL);
724 node = node_find_x(root, path);
726 assert(node != root);
732 * Canonical form of a map entry looks like this:
734 * key [-options] [ [/mountpoint] [-options2] location ... ]
736 * Entries for executable maps are slightly different, as they
737 * lack the 'key' field and are always single-line; the key field
738 * for those maps is taken from 'executable_key' argument.
740 * We parse it in such a way that a map always has two levels - first
741 * for key, and the second, for the mountpoint.
744 parse_map_yyin(struct node *parent, const char *map, const char *executable_key)
746 char *key = NULL, *options = NULL, *mountpoint = NULL,
747 *options2 = NULL, *location = NULL;
753 if (executable_key != NULL)
754 key = checked_strdup(executable_key);
758 if (ret == 0 || ret == NEWLINE) {
760 * In case of executable map, the key is always
761 * non-NULL, even if the map is empty. So, make sure
762 * we don't fail empty maps here.
764 if ((key != NULL && executable_key == NULL) ||
766 log_errx(1, "truncated entry at %s, line %d",
769 if (ret == 0 || executable_key != NULL) {
775 key = options = NULL;
780 key = checked_strdup(yytext);
782 node_new(parent, key, NULL, NULL, map, lineno);
783 key = options = NULL;
787 } else if (yytext[0] == '-') {
788 if (options != NULL) {
789 log_errx(1, "duplicated options at %s, line %d",
793 * +1 to skip leading "-".
795 options = checked_strdup(yytext + 1);
800 * We cannot properly handle a situation where the map key
801 * is "/". Ignore such entries.
803 * XXX: According to Piete Brooks, Linux automounter uses
804 * "/" as a wildcard character in LDAP maps. Perhaps
805 * we should work around this braindamage by substituting
808 if (strcmp(key, "/") == 0) {
809 log_warnx("nonsensical map key \"/\" at %s, line %d; "
810 "ignoring map entry ", map, lineno);
813 * Skip the rest of the entry.
817 } while (ret != 0 && ret != NEWLINE);
819 key = options = NULL;
823 //log_debugx("adding map node, %s", key);
824 node = node_new(parent, key, options, NULL, map, lineno);
825 key = options = NULL;
828 if (yytext[0] == '/') {
829 if (mountpoint != NULL) {
830 log_errx(1, "duplicated mountpoint "
831 "in %s, line %d", map, lineno);
833 if (options2 != NULL || location != NULL) {
834 log_errx(1, "mountpoint out of order "
835 "in %s, line %d", map, lineno);
837 mountpoint = checked_strdup(yytext);
841 if (yytext[0] == '-') {
842 if (options2 != NULL) {
843 log_errx(1, "duplicated options "
844 "in %s, line %d", map, lineno);
846 if (location != NULL) {
847 log_errx(1, "options out of order "
848 "in %s, line %d", map, lineno);
850 options2 = checked_strdup(yytext + 1);
854 if (location != NULL) {
855 log_errx(1, "too many arguments "
856 "in %s, line %d", map, lineno);
860 * If location field starts with colon, e.g. ":/dev/cd0",
863 if (yytext[0] == ':') {
864 location = checked_strdup(yytext + 1);
865 if (location[0] == '\0') {
866 log_errx(1, "empty location in %s, "
867 "line %d", map, lineno);
870 location = checked_strdup(yytext);
873 if (mountpoint == NULL)
874 mountpoint = checked_strdup("/");
875 if (options2 == NULL)
876 options2 = checked_strdup("");
879 log_debugx("adding map node, %s %s %s",
880 mountpoint, options2, location);
882 node_new(node, mountpoint, options2, location,
884 mountpoint = options2 = location = NULL;
887 if (ret == 0 || ret == NEWLINE) {
888 if (mountpoint != NULL || options2 != NULL ||
890 log_errx(1, "truncated entry "
891 "in %s, line %d", map, lineno);
900 * Parse output of a special map called without argument. It is a list
901 * of keys, separated by newlines. They can contain whitespace, so use
902 * getline(3) instead of lexer used for maps.
905 parse_map_keys_yyin(struct node *parent, const char *map)
907 char *line = NULL, *key;
914 linelen = getline(&line, &linecap, yyin);
923 * Empty line, consisting of just the newline.
929 * "-1" to strip the trailing newline.
931 key = strndup(line, linelen - 1);
933 log_debugx("adding key \"%s\"", key);
934 node_new(parent, key, NULL, NULL, map, lineno);
941 file_is_executable(const char *path)
946 error = stat(path, &sb);
948 log_err(1, "cannot stat %s", path);
949 if ((sb.st_mode & S_IXUSR) || (sb.st_mode & S_IXGRP) ||
950 (sb.st_mode & S_IXOTH))
956 * Parse a special map, e.g. "-hosts".
959 parse_special_map(struct node *parent, const char *map, const char *key)
964 assert(map[0] == '-');
967 * +1 to skip leading "-" in map name.
969 ret = asprintf(&path, "%s/special_%s", AUTO_SPECIAL_PREFIX, map + 1);
971 log_err(1, "asprintf");
973 yyin = auto_popen(path, key, NULL);
974 assert(yyin != NULL);
977 parse_map_keys_yyin(parent, map);
979 parse_map_yyin(parent, map, key);
982 error = auto_pclose(yyin);
985 log_errx(1, "failed to handle special map \"%s\"", map);
987 node_expand_includes(parent, false);
988 node_expand_direct_maps(parent);
994 * Retrieve and parse map from directory services, e.g. LDAP.
995 * Note that it is different from executable maps, in that
996 * the include script outputs the whole map to standard output
997 * (as opposed to executable maps that only output a single
998 * entry, without the key), and it takes the map name as an
999 * argument, instead of key.
1002 parse_included_map(struct node *parent, const char *map)
1006 assert(map[0] != '-');
1007 assert(map[0] != '/');
1009 error = access(AUTO_INCLUDE_PATH, F_OK);
1011 log_errx(1, "directory services not configured;"
1012 " %s does not exist", AUTO_INCLUDE_PATH);
1015 yyin = auto_popen(AUTO_INCLUDE_PATH, map, NULL);
1016 assert(yyin != NULL);
1018 parse_map_yyin(parent, map, NULL);
1020 error = auto_pclose(yyin);
1023 log_errx(1, "failed to handle remote map \"%s\"", map);
1025 node_expand_includes(parent, false);
1026 node_expand_direct_maps(parent);
1030 parse_map(struct node *parent, const char *map, const char *key,
1037 assert(map != NULL);
1038 assert(map[0] != '\0');
1040 log_debugx("parsing map \"%s\"", map);
1042 if (wildcards != NULL)
1045 if (map[0] == '-') {
1046 if (wildcards != NULL)
1048 return (parse_special_map(parent, map, key));
1051 if (map[0] == '/') {
1052 path = checked_strdup(map);
1054 ret = asprintf(&path, "%s/%s", AUTO_MAP_PREFIX, map);
1056 log_err(1, "asprintf");
1057 log_debugx("map \"%s\" maps to \"%s\"", map, path);
1060 * See if the file exists. If not, try to obtain the map
1061 * from directory services.
1063 error = access(path, F_OK);
1065 log_debugx("map file \"%s\" does not exist; falling "
1066 "back to directory services", path);
1067 return (parse_included_map(parent, map));
1071 executable = file_is_executable(path);
1074 log_debugx("map \"%s\" is executable", map);
1076 if (wildcards != NULL)
1080 yyin = auto_popen(path, key, NULL);
1082 yyin = auto_popen(path, NULL);
1084 assert(yyin != NULL);
1086 yyin = fopen(path, "r");
1088 log_err(1, "unable to open \"%s\"", path);
1094 parse_map_yyin(parent, map, executable ? key : NULL);
1097 error = auto_pclose(yyin);
1100 log_errx(1, "failed to handle executable map \"%s\"",
1108 log_debugx("done parsing map \"%s\"", map);
1110 node_expand_includes(parent, false);
1111 node_expand_direct_maps(parent);
1115 parse_master_yyin(struct node *root, const char *master)
1117 char *mountpoint = NULL, *map = NULL, *options = NULL;
1121 * XXX: 1 gives incorrect values; wtf?
1127 if (ret == 0 || ret == NEWLINE) {
1128 if (mountpoint != NULL) {
1129 //log_debugx("adding map for %s", mountpoint);
1130 node_new_map(root, mountpoint, options, map,
1136 mountpoint = map = options = NULL;
1140 if (mountpoint == NULL) {
1141 mountpoint = checked_strdup(yytext);
1142 } else if (map == NULL) {
1143 map = checked_strdup(yytext);
1144 } else if (options == NULL) {
1146 * +1 to skip leading "-".
1148 options = checked_strdup(yytext + 1);
1150 log_errx(1, "too many arguments at %s, line %d",
1157 parse_master(struct node *root, const char *master)
1160 log_debugx("parsing auto_master file at \"%s\"", master);
1162 yyin = fopen(master, "r");
1164 err(1, "unable to open %s", master);
1166 parse_master_yyin(root, master);
1171 log_debugx("done parsing \"%s\"", master);
1173 node_expand_includes(root, true);
1174 node_expand_direct_maps(root);
1178 * Two things daemon(3) does, that we actually also want to do
1179 * when running in foreground, is closing the stdin and chdiring
1180 * to "/". This is what we do here.
1191 fd = open(_PATH_DEVNULL, O_RDWR, 0);
1193 log_warn("cannot open %s", _PATH_DEVNULL);
1197 error = dup2(fd, STDIN_FILENO);
1209 main(int argc, char **argv)
1213 if (argv[0] == NULL)
1214 log_errx(1, "NULL command name");
1216 cmdname = basename(argv[0]);
1218 if (strcmp(cmdname, "automount") == 0)
1219 return (main_automount(argc, argv));
1220 else if (strcmp(cmdname, "automountd") == 0)
1221 return (main_automountd(argc, argv));
1222 else if (strcmp(cmdname, "autounmountd") == 0)
1223 return (main_autounmountd(argc, argv));
1225 log_errx(1, "binary name should be either \"automount\", "
1226 "\"automountd\", or \"autounmountd\"");