2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2014 The FreeBSD Foundation
6 * This software was developed by Edward Tomasz Napierala under sponsorship
7 * from the FreeBSD Foundation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/types.h>
37 #include <sys/ioctl.h>
38 #include <sys/param.h>
39 #include <sys/linker.h>
40 #include <sys/mount.h>
41 #include <sys/socket.h>
44 #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 * Concatenate two strings, inserting separator between them, unless not needed.
91 concat(const char *s1, char separator, const char *s2)
105 s1last = s1[strlen(s1) - 1];
109 if (s1last == separator && s2first == separator) {
111 * If s1 ends with the separator and s2 begins with
112 * it - skip the latter; otherwise concatenating "/"
113 * and "/foo" would end up returning "//foo".
115 ret = asprintf(&result, "%s%s", s1, s2 + 1);
116 } else if (s1last == separator || s2first == separator ||
117 s1[0] == '\0' || s2[0] == '\0') {
118 ret = asprintf(&result, "%s%s", s1, s2);
120 ret = asprintf(&result, "%s%c%s", s1, separator, s2);
123 log_err(1, "asprintf");
125 //log_debugx("%s: got %s and %s, returning %s", __func__, s1, s2, result);
131 create_directory(const char *path)
133 char *component, *copy, *tofree, *partial, *tmp;
136 assert(path[0] == '/');
139 * +1 to skip the leading slash.
141 copy = tofree = checked_strdup(path + 1);
143 partial = checked_strdup("");
145 component = strsep(©, "/");
146 if (component == NULL)
148 tmp = concat(partial, '/', component);
151 //log_debugx("creating \"%s\"", partial);
152 error = mkdir(partial, 0755);
153 if (error != 0 && errno != EEXIST) {
154 log_warn("cannot create %s", partial);
167 n = calloc(1, sizeof(*n));
169 log_err(1, "calloc");
171 n->n_key = checked_strdup("/");
172 n->n_options = checked_strdup("");
174 TAILQ_INIT(&n->n_children);
180 node_new(struct node *parent, char *key, char *options, char *location,
181 const char *config_file, int config_line)
185 n = calloc(1, sizeof(*n));
187 log_err(1, "calloc");
189 TAILQ_INIT(&n->n_children);
191 assert(key[0] != '\0');
194 n->n_options = options;
196 n->n_options = strdup("");
197 n->n_location = location;
198 assert(config_file != NULL);
199 n->n_config_file = config_file;
200 assert(config_line >= 0);
201 n->n_config_line = config_line;
203 assert(parent != NULL);
204 n->n_parent = parent;
205 TAILQ_INSERT_TAIL(&parent->n_children, n, n_next);
211 node_new_map(struct node *parent, char *key, char *options, char *map,
212 const char *config_file, int config_line)
216 n = calloc(1, sizeof(*n));
218 log_err(1, "calloc");
220 TAILQ_INIT(&n->n_children);
222 assert(key[0] != '\0');
225 n->n_options = options;
227 n->n_options = strdup("");
229 assert(config_file != NULL);
230 n->n_config_file = config_file;
231 assert(config_line >= 0);
232 n->n_config_line = config_line;
234 assert(parent != NULL);
235 n->n_parent = parent;
236 TAILQ_INSERT_TAIL(&parent->n_children, n, n_next);
242 node_duplicate(const struct node *o, struct node *parent)
244 const struct node *child;
248 parent = o->n_parent;
250 n = node_new(parent, o->n_key, o->n_options, o->n_location,
251 o->n_config_file, o->n_config_line);
253 TAILQ_FOREACH(child, &o->n_children, n_next)
254 node_duplicate(child, n);
260 node_delete(struct node *n)
262 struct node *child, *tmp;
266 TAILQ_FOREACH_SAFE(child, &n->n_children, n_next, tmp)
269 if (n->n_parent != NULL)
270 TAILQ_REMOVE(&n->n_parent->n_children, n, n_next);
276 * Move (reparent) node 'n' to make it sibling of 'previous', placed
280 node_move_after(struct node *n, struct node *previous)
283 TAILQ_REMOVE(&n->n_parent->n_children, n, n_next);
284 n->n_parent = previous->n_parent;
285 TAILQ_INSERT_AFTER(&previous->n_parent->n_children, previous, n, n_next);
289 node_expand_includes(struct node *root, bool is_master)
291 struct node *n, *n2, *tmp, *tmp2, *tmproot;
294 TAILQ_FOREACH_SAFE(n, &root->n_children, n_next, tmp) {
295 if (n->n_key[0] != '+')
298 error = access(AUTO_INCLUDE_PATH, F_OK);
300 log_errx(1, "directory services not configured; "
301 "%s does not exist", AUTO_INCLUDE_PATH);
305 * "+1" to skip leading "+".
307 yyin = auto_popen(AUTO_INCLUDE_PATH, n->n_key + 1, NULL);
308 assert(yyin != NULL);
310 tmproot = node_new_root();
312 parse_master_yyin(tmproot, n->n_key);
314 parse_map_yyin(tmproot, n->n_key, NULL);
316 error = auto_pclose(yyin);
319 log_errx(1, "failed to handle include \"%s\"",
324 * Entries to be included are now in tmproot. We need to merge
325 * them with the rest, preserving their place and ordering.
327 TAILQ_FOREACH_REVERSE_SAFE(n2,
328 &tmproot->n_children, nodehead, n_next, tmp2) {
329 node_move_after(n2, n);
333 node_delete(tmproot);
338 expand_ampersand(char *string, const char *key)
341 int i, ret, before_len = 0;
342 bool backslashed = false;
344 assert(key[0] != '\0');
346 expanded = checked_strdup(string);
348 for (i = 0; string[i] != '\0'; i++) {
350 if (c == '\\' && backslashed == false) {
363 * The 'before_len' variable contains the number
364 * of characters before the '&'.
367 //assert(i < (int)strlen(string));
369 ret = asprintf(&expanded, "%.*s%s%s",
370 before_len, string, key, string + before_len + 1);
372 log_err(1, "asprintf");
374 //log_debugx("\"%s\" expanded with key \"%s\" to \"%s\"",
375 // string, key, expanded);
378 * Figure out where to start searching for next variable.
381 i = before_len + strlen(key);
382 if (i == (int)strlen(string))
385 //assert(i < (int)strlen(string));
392 * Expand "&" in n_location. If the key is NULL, try to use
393 * key from map entries themselves. Keep in mind that maps
394 * consist of tho levels of node structures, the key is one
397 * Variant with NULL key is for "automount -LL".
400 node_expand_ampersand(struct node *n, const char *key)
404 if (n->n_location != NULL) {
406 if (n->n_parent != NULL &&
407 strcmp(n->n_parent->n_key, "*") != 0) {
408 n->n_location = expand_ampersand(n->n_location,
412 n->n_location = expand_ampersand(n->n_location, key);
416 TAILQ_FOREACH(child, &n->n_children, n_next)
417 node_expand_ampersand(child, key);
421 * Expand "*" in n_key.
424 node_expand_wildcard(struct node *n, const char *key)
426 struct node *child, *expanded;
430 if (strcmp(n->n_key, "*") == 0) {
431 expanded = node_duplicate(n, NULL);
432 expanded->n_key = checked_strdup(key);
433 node_move_after(expanded, n);
436 TAILQ_FOREACH(child, &n->n_children, n_next)
437 node_expand_wildcard(child, key);
441 node_expand_defined(struct node *n)
444 int error, cumulated_error = 0;
446 if (n->n_location != NULL) {
447 n->n_location = defined_expand(n->n_location);
448 if (n->n_location == NULL) {
449 log_warnx("failed to expand location for %s",
455 TAILQ_FOREACH(child, &n->n_children, n_next) {
456 error = node_expand_defined(child);
457 if (error != 0 && cumulated_error == 0)
458 cumulated_error = error;
461 return (cumulated_error);
465 node_is_direct_key(const struct node *n)
468 if (n->n_parent != NULL && n->n_parent->n_parent == NULL &&
469 strcmp(n->n_key, "/-") == 0) {
477 node_is_direct_map(const struct node *n)
481 assert(n->n_parent != NULL);
482 if (n->n_parent->n_parent == NULL)
487 return (node_is_direct_key(n));
491 node_has_wildcards(const struct node *n)
493 const struct node *child;
495 TAILQ_FOREACH(child, &n->n_children, n_next) {
496 if (strcmp(child->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, 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)
554 if (n->n_parent == NULL)
558 * Return "/-" for direct maps only if we were asked for path
559 * to the "/-" node itself, not to any of its subnodes.
561 if (node_is_direct_key(n) && x[0] != '\0')
564 assert(n->n_key[0] != '\0');
565 path = concat(n->n_key, '/', x);
568 return (node_path_x(n->n_parent, path));
572 * Return full path for node, consisting of concatenated
573 * paths of node itself and all its parents, up to the root.
576 node_path(const struct node *n)
581 path = node_path_x(n, checked_strdup(""));
584 * Strip trailing slash, unless the whole path is "/".
587 if (len > 1 && path[len - 1] == '/')
588 path[len - 1] = '\0';
594 node_options_x(const struct node *n, char *x)
601 options = concat(x, ',', n->n_options);
604 return (node_options_x(n->n_parent, options));
608 * Return options for node, consisting of concatenated
609 * options from the node itself and all its parents,
613 node_options(const struct node *n)
616 return (node_options_x(n, checked_strdup("")));
620 node_print_indent(const struct node *n, const char *cmdline_options,
623 const struct node *child, *first_child;
624 char *path, *options, *tmp;
627 tmp = node_options(n);
628 options = concat(cmdline_options, ',', tmp);
632 * Do not show both parent and child node if they have the same
633 * mountpoint; only show the child node. This means the typical,
634 * "key location", map entries are shown in a single line;
635 * the "key mountpoint1 location2 mountpoint2 location2" entries
636 * take multiple lines.
638 first_child = TAILQ_FIRST(&n->n_children);
639 if (first_child == NULL || TAILQ_NEXT(first_child, n_next) != NULL ||
640 strcmp(path, node_path(first_child)) != 0) {
641 assert(n->n_location == NULL || n->n_map == NULL);
642 printf("%*.s%-*s %s%-*s %-*s # %s map %s at %s:%d\n",
646 options[0] != '\0' ? "-" : " ",
648 options[0] != '\0' ? options : "",
650 n->n_location != NULL ? n->n_location : n->n_map != NULL ? n->n_map : "",
651 node_is_direct_map(n) ? "direct" : "indirect",
652 indent == 0 ? "referenced" : "defined",
653 n->n_config_file, n->n_config_line);
659 TAILQ_FOREACH(child, &n->n_children, n_next)
660 node_print_indent(child, cmdline_options, indent + 2);
664 * Recursively print node with all its children. The cmdline_options
665 * argument is used for additional options to be prepended to all the
666 * others - usually those are the options passed by command line.
669 node_print(const struct node *n, const char *cmdline_options)
671 const struct node *child;
673 TAILQ_FOREACH(child, &n->n_children, n_next)
674 node_print_indent(child, cmdline_options, 0);
678 node_find_x(struct node *node, const char *path)
680 struct node *child, *found;
684 //log_debugx("looking up %s in %s", path, node_path(node));
686 if (!node_is_direct_key(node)) {
687 tmp = node_path(node);
688 tmplen = strlen(tmp);
689 if (strncmp(tmp, path, tmplen) != 0) {
693 if (path[tmplen] != '/' && path[tmplen] != '\0') {
695 * If we have two map entries like 'foo' and 'foobar', make
696 * sure the search for 'foobar' won't match 'foo' instead.
704 TAILQ_FOREACH(child, &node->n_children, n_next) {
705 found = node_find_x(child, path);
710 if (node->n_parent == NULL || node_is_direct_key(node))
717 node_find(struct node *root, const char *path)
721 assert(root->n_parent == NULL);
723 node = node_find_x(root, path);
725 assert(node != root);
731 * Canonical form of a map entry looks like this:
733 * key [-options] [ [/mountpoint] [-options2] location ... ]
735 * Entries for executable maps are slightly different, as they
736 * lack the 'key' field and are always single-line; the key field
737 * for those maps is taken from 'executable_key' argument.
739 * We parse it in such a way that a map always has two levels - first
740 * for key, and the second, for the mountpoint.
743 parse_map_yyin(struct node *parent, const char *map, const char *executable_key)
745 char *key = NULL, *options = NULL, *mountpoint = NULL,
746 *options2 = NULL, *location = NULL;
752 if (executable_key != NULL)
753 key = checked_strdup(executable_key);
757 if (ret == 0 || ret == NEWLINE) {
759 * In case of executable map, the key is always
760 * non-NULL, even if the map is empty. So, make sure
761 * we don't fail empty maps here.
763 if ((key != NULL && executable_key == NULL) ||
765 log_errx(1, "truncated entry at %s, line %d",
768 if (ret == 0 || executable_key != NULL) {
774 key = options = NULL;
779 key = checked_strdup(yytext);
781 node_new(parent, key, NULL, NULL, map, lineno);
782 key = options = NULL;
786 } else if (yytext[0] == '-') {
787 if (options != NULL) {
788 log_errx(1, "duplicated options at %s, line %d",
792 * +1 to skip leading "-".
794 options = checked_strdup(yytext + 1);
799 * We cannot properly handle a situation where the map key
800 * is "/". Ignore such entries.
802 * XXX: According to Piete Brooks, Linux automounter uses
803 * "/" as a wildcard character in LDAP maps. Perhaps
804 * we should work around this braindamage by substituting
807 if (strcmp(key, "/") == 0) {
808 log_warnx("nonsensical map key \"/\" at %s, line %d; "
809 "ignoring map entry ", map, lineno);
812 * Skip the rest of the entry.
816 } while (ret != 0 && ret != NEWLINE);
818 key = options = NULL;
822 //log_debugx("adding map node, %s", key);
823 node = node_new(parent, key, options, NULL, map, lineno);
824 key = options = NULL;
827 if (yytext[0] == '/') {
828 if (mountpoint != NULL) {
829 log_errx(1, "duplicated mountpoint "
830 "in %s, line %d", map, lineno);
832 if (options2 != NULL || location != NULL) {
833 log_errx(1, "mountpoint out of order "
834 "in %s, line %d", map, lineno);
836 mountpoint = checked_strdup(yytext);
840 if (yytext[0] == '-') {
841 if (options2 != NULL) {
842 log_errx(1, "duplicated options "
843 "in %s, line %d", map, lineno);
845 if (location != NULL) {
846 log_errx(1, "options out of order "
847 "in %s, line %d", map, lineno);
849 options2 = checked_strdup(yytext + 1);
853 if (location != NULL) {
854 log_errx(1, "too many arguments "
855 "in %s, line %d", map, lineno);
859 * If location field starts with colon, e.g. ":/dev/cd0",
862 if (yytext[0] == ':') {
863 location = checked_strdup(yytext + 1);
864 if (location[0] == '\0') {
865 log_errx(1, "empty location in %s, "
866 "line %d", map, lineno);
869 location = checked_strdup(yytext);
872 if (mountpoint == NULL)
873 mountpoint = checked_strdup("/");
874 if (options2 == NULL)
875 options2 = checked_strdup("");
878 log_debugx("adding map node, %s %s %s",
879 mountpoint, options2, location);
881 node_new(node, mountpoint, options2, location,
883 mountpoint = options2 = location = NULL;
886 if (ret == 0 || ret == NEWLINE) {
887 if (mountpoint != NULL || options2 != NULL ||
889 log_errx(1, "truncated entry "
890 "in %s, line %d", map, lineno);
899 * Parse output of a special map called without argument. It is a list
900 * of keys, separated by newlines. They can contain whitespace, so use
901 * getline(3) instead of lexer used for maps.
904 parse_map_keys_yyin(struct node *parent, const char *map)
906 char *line = NULL, *key;
913 linelen = getline(&line, &linecap, yyin);
922 * Empty line, consisting of just the newline.
928 * "-1" to strip the trailing newline.
930 key = strndup(line, linelen - 1);
932 log_debugx("adding key \"%s\"", key);
933 node_new(parent, key, NULL, NULL, map, lineno);
940 file_is_executable(const char *path)
945 error = stat(path, &sb);
947 log_err(1, "cannot stat %s", path);
948 if ((sb.st_mode & S_IXUSR) || (sb.st_mode & S_IXGRP) ||
949 (sb.st_mode & S_IXOTH))
955 * Parse a special map, e.g. "-hosts".
958 parse_special_map(struct node *parent, const char *map, const char *key)
963 assert(map[0] == '-');
966 * +1 to skip leading "-" in map name.
968 ret = asprintf(&path, "%s/special_%s", AUTO_SPECIAL_PREFIX, map + 1);
970 log_err(1, "asprintf");
972 yyin = auto_popen(path, key, NULL);
973 assert(yyin != NULL);
976 parse_map_keys_yyin(parent, map);
978 parse_map_yyin(parent, map, key);
981 error = auto_pclose(yyin);
984 log_errx(1, "failed to handle special map \"%s\"", map);
986 node_expand_includes(parent, false);
987 node_expand_direct_maps(parent);
993 * Retrieve and parse map from directory services, e.g. LDAP.
994 * Note that it is different from executable maps, in that
995 * the include script outputs the whole map to standard output
996 * (as opposed to executable maps that only output a single
997 * entry, without the key), and it takes the map name as an
998 * argument, instead of key.
1001 parse_included_map(struct node *parent, const char *map)
1005 assert(map[0] != '-');
1006 assert(map[0] != '/');
1008 error = access(AUTO_INCLUDE_PATH, F_OK);
1010 log_errx(1, "directory services not configured;"
1011 " %s does not exist", AUTO_INCLUDE_PATH);
1014 yyin = auto_popen(AUTO_INCLUDE_PATH, map, NULL);
1015 assert(yyin != NULL);
1017 parse_map_yyin(parent, map, NULL);
1019 error = auto_pclose(yyin);
1022 log_errx(1, "failed to handle remote map \"%s\"", map);
1024 node_expand_includes(parent, false);
1025 node_expand_direct_maps(parent);
1029 parse_map(struct node *parent, const char *map, const char *key,
1036 assert(map != NULL);
1037 assert(map[0] != '\0');
1039 log_debugx("parsing map \"%s\"", map);
1041 if (wildcards != NULL)
1044 if (map[0] == '-') {
1045 if (wildcards != NULL)
1047 return (parse_special_map(parent, map, key));
1050 if (map[0] == '/') {
1051 path = checked_strdup(map);
1053 ret = asprintf(&path, "%s/%s", AUTO_MAP_PREFIX, map);
1055 log_err(1, "asprintf");
1056 log_debugx("map \"%s\" maps to \"%s\"", map, path);
1059 * See if the file exists. If not, try to obtain the map
1060 * from directory services.
1062 error = access(path, F_OK);
1064 log_debugx("map file \"%s\" does not exist; falling "
1065 "back to directory services", path);
1066 return (parse_included_map(parent, map));
1070 executable = file_is_executable(path);
1073 log_debugx("map \"%s\" is executable", map);
1075 if (wildcards != NULL)
1079 yyin = auto_popen(path, key, NULL);
1081 yyin = auto_popen(path, NULL);
1083 assert(yyin != NULL);
1085 yyin = fopen(path, "r");
1087 log_err(1, "unable to open \"%s\"", path);
1093 parse_map_yyin(parent, map, executable ? key : NULL);
1096 error = auto_pclose(yyin);
1099 log_errx(1, "failed to handle executable map \"%s\"",
1107 log_debugx("done parsing map \"%s\"", map);
1109 node_expand_includes(parent, false);
1110 node_expand_direct_maps(parent);
1114 parse_master_yyin(struct node *root, const char *master)
1116 char *mountpoint = NULL, *map = NULL, *options = NULL;
1120 * XXX: 1 gives incorrect values; wtf?
1126 if (ret == 0 || ret == NEWLINE) {
1127 if (mountpoint != NULL) {
1128 //log_debugx("adding map for %s", mountpoint);
1129 node_new_map(root, mountpoint, options, map,
1135 mountpoint = map = options = NULL;
1139 if (mountpoint == NULL) {
1140 mountpoint = checked_strdup(yytext);
1141 } else if (map == NULL) {
1142 map = checked_strdup(yytext);
1143 } else if (options == NULL) {
1145 * +1 to skip leading "-".
1147 options = checked_strdup(yytext + 1);
1149 log_errx(1, "too many arguments at %s, line %d",
1156 parse_master(struct node *root, const char *master)
1159 log_debugx("parsing auto_master file at \"%s\"", master);
1161 yyin = fopen(master, "r");
1163 err(1, "unable to open %s", master);
1165 parse_master_yyin(root, master);
1170 log_debugx("done parsing \"%s\"", master);
1172 node_expand_includes(root, true);
1173 node_expand_direct_maps(root);
1177 * Two things daemon(3) does, that we actually also want to do
1178 * when running in foreground, is closing the stdin and chdiring
1179 * to "/". This is what we do here.
1190 fd = open(_PATH_DEVNULL, O_RDWR, 0);
1192 log_warn("cannot open %s", _PATH_DEVNULL);
1196 error = dup2(fd, STDIN_FILENO);
1208 main(int argc, char **argv)
1212 if (argv[0] == NULL)
1213 log_errx(1, "NULL command name");
1215 cmdname = basename(argv[0]);
1217 if (strcmp(cmdname, "automount") == 0)
1218 return (main_automount(argc, argv));
1219 else if (strcmp(cmdname, "automountd") == 0)
1220 return (main_automountd(argc, argv));
1221 else if (strcmp(cmdname, "autounmountd") == 0)
1222 return (main_autounmountd(argc, argv));
1224 log_errx(1, "binary name should be either \"automount\", "
1225 "\"automountd\", or \"autounmountd\"");