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 + 1 < (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);
384 //assert(i < (int)strlen(string));
391 * Expand "&" in n_location. If the key is NULL, try to use
392 * key from map entries themselves. Keep in mind that maps
393 * consist of tho levels of node structures, the key is one
396 * Variant with NULL key is for "automount -LL".
399 node_expand_ampersand(struct node *n, const char *key)
403 if (n->n_location != NULL) {
405 if (n->n_parent != NULL &&
406 strcmp(n->n_parent->n_key, "*") != 0) {
407 n->n_location = expand_ampersand(n->n_location,
411 n->n_location = expand_ampersand(n->n_location, key);
415 TAILQ_FOREACH(child, &n->n_children, n_next)
416 node_expand_ampersand(child, key);
420 * Expand "*" in n_key.
423 node_expand_wildcard(struct node *n, const char *key)
425 struct node *child, *expanded;
429 if (strcmp(n->n_key, "*") == 0) {
430 expanded = node_duplicate(n, NULL);
431 expanded->n_key = checked_strdup(key);
432 node_move_after(expanded, n);
435 TAILQ_FOREACH(child, &n->n_children, n_next)
436 node_expand_wildcard(child, key);
440 node_expand_defined(struct node *n)
443 int error, cumulated_error = 0;
445 if (n->n_location != NULL) {
446 n->n_location = defined_expand(n->n_location);
447 if (n->n_location == NULL) {
448 log_warnx("failed to expand location for %s",
454 TAILQ_FOREACH(child, &n->n_children, n_next) {
455 error = node_expand_defined(child);
456 if (error != 0 && cumulated_error == 0)
457 cumulated_error = error;
460 return (cumulated_error);
464 node_is_direct_key(const struct node *n)
467 if (n->n_parent != NULL && n->n_parent->n_parent == NULL &&
468 strcmp(n->n_key, "/-") == 0) {
476 node_is_direct_map(const struct node *n)
480 assert(n->n_parent != NULL);
481 if (n->n_parent->n_parent == NULL)
486 return (node_is_direct_key(n));
490 node_has_wildcards(const struct node *n)
492 const struct node *child;
494 TAILQ_FOREACH(child, &n->n_children, n_next) {
495 if (strcmp(child->n_key, "*") == 0)
503 node_expand_maps(struct node *n, bool indirect)
505 struct node *child, *tmp;
507 TAILQ_FOREACH_SAFE(child, &n->n_children, n_next, tmp) {
508 if (node_is_direct_map(child)) {
512 if (indirect == false)
517 * This is the first-level map node; the one that contains
518 * the key and subnodes with mountpoints and actual map names.
520 if (child->n_map == NULL)
524 log_debugx("map \"%s\" is an indirect map, parsing",
527 log_debugx("map \"%s\" is a direct map, parsing",
530 parse_map(child, child->n_map, NULL, NULL);
535 node_expand_direct_maps(struct node *n)
538 node_expand_maps(n, false);
542 node_expand_indirect_maps(struct node *n)
545 node_expand_maps(n, true);
549 node_path_x(const struct node *n, char *x)
553 if (n->n_parent == NULL)
557 * Return "/-" for direct maps only if we were asked for path
558 * to the "/-" node itself, not to any of its subnodes.
560 if (node_is_direct_key(n) && x[0] != '\0')
563 assert(n->n_key[0] != '\0');
564 path = concat(n->n_key, '/', x);
567 return (node_path_x(n->n_parent, path));
571 * Return full path for node, consisting of concatenated
572 * paths of node itself and all its parents, up to the root.
575 node_path(const struct node *n)
580 path = node_path_x(n, checked_strdup(""));
583 * Strip trailing slash, unless the whole path is "/".
586 if (len > 1 && path[len - 1] == '/')
587 path[len - 1] = '\0';
593 node_options_x(const struct node *n, char *x)
600 options = concat(x, ',', n->n_options);
603 return (node_options_x(n->n_parent, options));
607 * Return options for node, consisting of concatenated
608 * options from the node itself and all its parents,
612 node_options(const struct node *n)
615 return (node_options_x(n, checked_strdup("")));
619 node_print_indent(const struct node *n, const char *cmdline_options,
622 const struct node *child, *first_child;
623 char *path, *options, *tmp;
626 tmp = node_options(n);
627 options = concat(cmdline_options, ',', tmp);
631 * Do not show both parent and child node if they have the same
632 * mountpoint; only show the child node. This means the typical,
633 * "key location", map entries are shown in a single line;
634 * the "key mountpoint1 location2 mountpoint2 location2" entries
635 * take multiple lines.
637 first_child = TAILQ_FIRST(&n->n_children);
638 if (first_child == NULL || TAILQ_NEXT(first_child, n_next) != NULL ||
639 strcmp(path, node_path(first_child)) != 0) {
640 assert(n->n_location == NULL || n->n_map == NULL);
641 printf("%*.s%-*s %s%-*s %-*s # %s map %s at %s:%d\n",
645 options[0] != '\0' ? "-" : " ",
647 options[0] != '\0' ? options : "",
649 n->n_location != NULL ? n->n_location : n->n_map != NULL ? n->n_map : "",
650 node_is_direct_map(n) ? "direct" : "indirect",
651 indent == 0 ? "referenced" : "defined",
652 n->n_config_file, n->n_config_line);
658 TAILQ_FOREACH(child, &n->n_children, n_next)
659 node_print_indent(child, cmdline_options, indent + 2);
663 * Recursively print node with all its children. The cmdline_options
664 * argument is used for additional options to be prepended to all the
665 * others - usually those are the options passed by command line.
668 node_print(const struct node *n, const char *cmdline_options)
670 const struct node *child;
672 TAILQ_FOREACH(child, &n->n_children, n_next)
673 node_print_indent(child, cmdline_options, 0);
677 node_find_x(struct node *node, const char *path)
679 struct node *child, *found;
683 //log_debugx("looking up %s in %s", path, node_path(node));
685 if (!node_is_direct_key(node)) {
686 tmp = node_path(node);
687 tmplen = strlen(tmp);
688 if (strncmp(tmp, path, tmplen) != 0) {
692 if (path[tmplen] != '/' && path[tmplen] != '\0') {
694 * If we have two map entries like 'foo' and 'foobar', make
695 * sure the search for 'foobar' won't match 'foo' instead.
703 TAILQ_FOREACH(child, &node->n_children, n_next) {
704 found = node_find_x(child, path);
709 if (node->n_parent == NULL || node_is_direct_key(node))
716 node_find(struct node *root, const char *path)
720 assert(root->n_parent == NULL);
722 node = node_find_x(root, path);
724 assert(node != root);
730 * Canonical form of a map entry looks like this:
732 * key [-options] [ [/mountpoint] [-options2] location ... ]
734 * Entries for executable maps are slightly different, as they
735 * lack the 'key' field and are always single-line; the key field
736 * for those maps is taken from 'executable_key' argument.
738 * We parse it in such a way that a map always has two levels - first
739 * for key, and the second, for the mountpoint.
742 parse_map_yyin(struct node *parent, const char *map, const char *executable_key)
744 char *key = NULL, *options = NULL, *mountpoint = NULL,
745 *options2 = NULL, *location = NULL;
751 if (executable_key != NULL)
752 key = checked_strdup(executable_key);
756 if (ret == 0 || ret == NEWLINE) {
758 * In case of executable map, the key is always
759 * non-NULL, even if the map is empty. So, make sure
760 * we don't fail empty maps here.
762 if ((key != NULL && executable_key == NULL) ||
764 log_errx(1, "truncated entry at %s, line %d",
767 if (ret == 0 || executable_key != NULL) {
773 key = options = NULL;
778 key = checked_strdup(yytext);
780 node_new(parent, key, NULL, NULL, map, lineno);
781 key = options = NULL;
785 } else if (yytext[0] == '-') {
786 if (options != NULL) {
787 log_errx(1, "duplicated options at %s, line %d",
791 * +1 to skip leading "-".
793 options = checked_strdup(yytext + 1);
798 * We cannot properly handle a situation where the map key
799 * is "/". Ignore such entries.
801 * XXX: According to Piete Brooks, Linux automounter uses
802 * "/" as a wildcard character in LDAP maps. Perhaps
803 * we should work around this braindamage by substituting
806 if (strcmp(key, "/") == 0) {
807 log_warnx("nonsensical map key \"/\" at %s, line %d; "
808 "ignoring map entry ", map, lineno);
811 * Skip the rest of the entry.
815 } while (ret != 0 && ret != NEWLINE);
817 key = options = NULL;
821 //log_debugx("adding map node, %s", key);
822 node = node_new(parent, key, options, NULL, map, lineno);
823 key = options = NULL;
826 if (yytext[0] == '/') {
827 if (mountpoint != NULL) {
828 log_errx(1, "duplicated mountpoint "
829 "in %s, line %d", map, lineno);
831 if (options2 != NULL || location != NULL) {
832 log_errx(1, "mountpoint out of order "
833 "in %s, line %d", map, lineno);
835 mountpoint = checked_strdup(yytext);
839 if (yytext[0] == '-') {
840 if (options2 != NULL) {
841 log_errx(1, "duplicated options "
842 "in %s, line %d", map, lineno);
844 if (location != NULL) {
845 log_errx(1, "options out of order "
846 "in %s, line %d", map, lineno);
848 options2 = checked_strdup(yytext + 1);
852 if (location != NULL) {
853 log_errx(1, "too many arguments "
854 "in %s, line %d", map, lineno);
858 * If location field starts with colon, e.g. ":/dev/cd0",
861 if (yytext[0] == ':') {
862 location = checked_strdup(yytext + 1);
863 if (location[0] == '\0') {
864 log_errx(1, "empty location in %s, "
865 "line %d", map, lineno);
868 location = checked_strdup(yytext);
871 if (mountpoint == NULL)
872 mountpoint = checked_strdup("/");
873 if (options2 == NULL)
874 options2 = checked_strdup("");
877 log_debugx("adding map node, %s %s %s",
878 mountpoint, options2, location);
880 node_new(node, mountpoint, options2, location,
882 mountpoint = options2 = location = NULL;
885 if (ret == 0 || ret == NEWLINE) {
886 if (mountpoint != NULL || options2 != NULL ||
888 log_errx(1, "truncated entry "
889 "in %s, line %d", map, lineno);
898 * Parse output of a special map called without argument. It is a list
899 * of keys, separated by newlines. They can contain whitespace, so use
900 * getline(3) instead of lexer used for maps.
903 parse_map_keys_yyin(struct node *parent, const char *map)
905 char *line = NULL, *key;
912 linelen = getline(&line, &linecap, yyin);
921 * Empty line, consisting of just the newline.
927 * "-1" to strip the trailing newline.
929 key = strndup(line, linelen - 1);
931 log_debugx("adding key \"%s\"", key);
932 node_new(parent, key, NULL, NULL, map, lineno);
939 file_is_executable(const char *path)
944 error = stat(path, &sb);
946 log_err(1, "cannot stat %s", path);
947 if ((sb.st_mode & S_IXUSR) || (sb.st_mode & S_IXGRP) ||
948 (sb.st_mode & S_IXOTH))
954 * Parse a special map, e.g. "-hosts".
957 parse_special_map(struct node *parent, const char *map, const char *key)
962 assert(map[0] == '-');
965 * +1 to skip leading "-" in map name.
967 ret = asprintf(&path, "%s/special_%s", AUTO_SPECIAL_PREFIX, map + 1);
969 log_err(1, "asprintf");
971 yyin = auto_popen(path, key, NULL);
972 assert(yyin != NULL);
975 parse_map_keys_yyin(parent, map);
977 parse_map_yyin(parent, map, key);
980 error = auto_pclose(yyin);
983 log_errx(1, "failed to handle special map \"%s\"", map);
985 node_expand_includes(parent, false);
986 node_expand_direct_maps(parent);
992 * Retrieve and parse map from directory services, e.g. LDAP.
993 * Note that it is different from executable maps, in that
994 * the include script outputs the whole map to standard output
995 * (as opposed to executable maps that only output a single
996 * entry, without the key), and it takes the map name as an
997 * argument, instead of key.
1000 parse_included_map(struct node *parent, const char *map)
1004 assert(map[0] != '-');
1005 assert(map[0] != '/');
1007 error = access(AUTO_INCLUDE_PATH, F_OK);
1009 log_errx(1, "directory services not configured;"
1010 " %s does not exist", AUTO_INCLUDE_PATH);
1013 yyin = auto_popen(AUTO_INCLUDE_PATH, map, NULL);
1014 assert(yyin != NULL);
1016 parse_map_yyin(parent, map, NULL);
1018 error = auto_pclose(yyin);
1021 log_errx(1, "failed to handle remote map \"%s\"", map);
1023 node_expand_includes(parent, false);
1024 node_expand_direct_maps(parent);
1028 parse_map(struct node *parent, const char *map, const char *key,
1035 assert(map != NULL);
1036 assert(map[0] != '\0');
1038 log_debugx("parsing map \"%s\"", map);
1040 if (wildcards != NULL)
1043 if (map[0] == '-') {
1044 if (wildcards != NULL)
1046 return (parse_special_map(parent, map, key));
1049 if (map[0] == '/') {
1050 path = checked_strdup(map);
1052 ret = asprintf(&path, "%s/%s", AUTO_MAP_PREFIX, map);
1054 log_err(1, "asprintf");
1055 log_debugx("map \"%s\" maps to \"%s\"", map, path);
1058 * See if the file exists. If not, try to obtain the map
1059 * from directory services.
1061 error = access(path, F_OK);
1063 log_debugx("map file \"%s\" does not exist; falling "
1064 "back to directory services", path);
1065 return (parse_included_map(parent, map));
1069 executable = file_is_executable(path);
1072 log_debugx("map \"%s\" is executable", map);
1074 if (wildcards != NULL)
1078 yyin = auto_popen(path, key, NULL);
1080 yyin = auto_popen(path, NULL);
1082 assert(yyin != NULL);
1084 yyin = fopen(path, "r");
1086 log_err(1, "unable to open \"%s\"", path);
1092 parse_map_yyin(parent, map, executable ? key : NULL);
1095 error = auto_pclose(yyin);
1098 log_errx(1, "failed to handle executable map \"%s\"",
1106 log_debugx("done parsing map \"%s\"", map);
1108 node_expand_includes(parent, false);
1109 node_expand_direct_maps(parent);
1113 parse_master_yyin(struct node *root, const char *master)
1115 char *mountpoint = NULL, *map = NULL, *options = NULL;
1119 * XXX: 1 gives incorrect values; wtf?
1125 if (ret == 0 || ret == NEWLINE) {
1126 if (mountpoint != NULL) {
1127 //log_debugx("adding map for %s", mountpoint);
1128 node_new_map(root, mountpoint, options, map,
1134 mountpoint = map = options = NULL;
1138 if (mountpoint == NULL) {
1139 mountpoint = checked_strdup(yytext);
1140 } else if (map == NULL) {
1141 map = checked_strdup(yytext);
1142 } else if (options == NULL) {
1144 * +1 to skip leading "-".
1146 options = checked_strdup(yytext + 1);
1148 log_errx(1, "too many arguments at %s, line %d",
1155 parse_master(struct node *root, const char *master)
1158 log_debugx("parsing auto_master file at \"%s\"", master);
1160 yyin = fopen(master, "r");
1162 err(1, "unable to open %s", master);
1164 parse_master_yyin(root, master);
1169 log_debugx("done parsing \"%s\"", master);
1171 node_expand_includes(root, true);
1172 node_expand_direct_maps(root);
1176 * Two things daemon(3) does, that we actually also want to do
1177 * when running in foreground, is closing the stdin and chdiring
1178 * to "/". This is what we do here.
1189 fd = open(_PATH_DEVNULL, O_RDWR, 0);
1191 log_warn("cannot open %s", _PATH_DEVNULL);
1195 error = dup2(fd, STDIN_FILENO);
1207 main(int argc, char **argv)
1211 if (argv[0] == NULL)
1212 log_errx(1, "NULL command name");
1214 cmdname = basename(argv[0]);
1216 if (strcmp(cmdname, "automount") == 0)
1217 return (main_automount(argc, argv));
1218 else if (strcmp(cmdname, "automountd") == 0)
1219 return (main_automountd(argc, argv));
1220 else if (strcmp(cmdname, "autounmountd") == 0)
1221 return (main_autounmountd(argc, argv));
1223 log_errx(1, "binary name should be either \"automount\", "
1224 "\"automountd\", or \"autounmountd\"");