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>
61 #include "autofs_ioctl.h"
67 extern int yylex(void);
69 static void parse_master_yyin(struct node *root, const char *master);
70 static void parse_map_yyin(struct node *parent, const char *map,
71 const char *executable_key);
74 checked_strdup(const char *s)
87 * Concatenate two strings, inserting separator between them, unless not needed.
90 concat(const char *s1, char separator, const char *s2)
104 s1last = s1[strlen(s1) - 1];
108 if (s1last == separator && s2first == separator) {
110 * If s1 ends with the separator and s2 begins with
111 * it - skip the latter; otherwise concatenating "/"
112 * and "/foo" would end up returning "//foo".
114 ret = asprintf(&result, "%s%s", s1, s2 + 1);
115 } else if (s1last == separator || s2first == separator ||
116 s1[0] == '\0' || s2[0] == '\0') {
117 ret = asprintf(&result, "%s%s", s1, s2);
119 ret = asprintf(&result, "%s%c%s", s1, separator, s2);
122 log_err(1, "asprintf");
124 //log_debugx("%s: got %s and %s, returning %s", __func__, s1, s2, result);
130 create_directory(const char *path)
132 char *component, *copy, *tofree, *partial, *tmp;
135 assert(path[0] == '/');
138 * +1 to skip the leading slash.
140 copy = tofree = checked_strdup(path + 1);
142 partial = checked_strdup("");
144 component = strsep(©, "/");
145 if (component == NULL)
147 tmp = concat(partial, '/', component);
150 //log_debugx("creating \"%s\"", partial);
151 error = mkdir(partial, 0755);
152 if (error != 0 && errno != EEXIST) {
153 log_warn("cannot create %s", partial);
166 n = calloc(1, sizeof(*n));
168 log_err(1, "calloc");
170 n->n_key = checked_strdup("/");
171 n->n_options = checked_strdup("");
173 TAILQ_INIT(&n->n_children);
179 node_new(struct node *parent, char *key, char *options, char *location,
180 const char *config_file, int config_line)
184 n = calloc(1, sizeof(*n));
186 log_err(1, "calloc");
188 TAILQ_INIT(&n->n_children);
190 assert(key[0] != '\0');
193 n->n_options = options;
195 n->n_options = strdup("");
196 n->n_location = location;
197 assert(config_file != NULL);
198 n->n_config_file = config_file;
199 assert(config_line >= 0);
200 n->n_config_line = config_line;
202 assert(parent != NULL);
203 n->n_parent = parent;
204 TAILQ_INSERT_TAIL(&parent->n_children, n, n_next);
210 node_new_map(struct node *parent, char *key, char *options, char *map,
211 const char *config_file, int config_line)
215 n = calloc(1, sizeof(*n));
217 log_err(1, "calloc");
219 TAILQ_INIT(&n->n_children);
221 assert(key[0] != '\0');
224 n->n_options = options;
226 n->n_options = strdup("");
228 assert(config_file != NULL);
229 n->n_config_file = config_file;
230 assert(config_line >= 0);
231 n->n_config_line = config_line;
233 assert(parent != NULL);
234 n->n_parent = parent;
235 TAILQ_INSERT_TAIL(&parent->n_children, n, n_next);
241 node_duplicate(const struct node *o, struct node *parent)
243 const struct node *child;
247 parent = o->n_parent;
249 n = node_new(parent, o->n_key, o->n_options, o->n_location,
250 o->n_config_file, o->n_config_line);
252 TAILQ_FOREACH(child, &o->n_children, n_next)
253 node_duplicate(child, n);
259 node_delete(struct node *n)
261 struct node *child, *tmp;
265 TAILQ_FOREACH_SAFE(child, &n->n_children, n_next, tmp)
268 if (n->n_parent != NULL)
269 TAILQ_REMOVE(&n->n_parent->n_children, n, n_next);
275 * Move (reparent) node 'n' to make it sibling of 'previous', placed
279 node_move_after(struct node *n, struct node *previous)
282 TAILQ_REMOVE(&n->n_parent->n_children, n, n_next);
283 n->n_parent = previous->n_parent;
284 TAILQ_INSERT_AFTER(&previous->n_parent->n_children, previous, n, n_next);
288 node_expand_includes(struct node *root, bool is_master)
290 struct node *n, *n2, *tmp, *tmp2, *tmproot;
293 TAILQ_FOREACH_SAFE(n, &root->n_children, n_next, tmp) {
294 if (n->n_key[0] != '+')
297 error = access(AUTO_INCLUDE_PATH, F_OK);
299 log_errx(1, "directory services not configured; "
300 "%s does not exist", AUTO_INCLUDE_PATH);
304 * "+1" to skip leading "+".
306 yyin = auto_popen(AUTO_INCLUDE_PATH, n->n_key + 1, NULL);
307 assert(yyin != NULL);
309 tmproot = node_new_root();
311 parse_master_yyin(tmproot, n->n_key);
313 parse_map_yyin(tmproot, n->n_key, NULL);
315 error = auto_pclose(yyin);
318 log_errx(1, "failed to handle include \"%s\"",
323 * Entries to be included are now in tmproot. We need to merge
324 * them with the rest, preserving their place and ordering.
326 TAILQ_FOREACH_REVERSE_SAFE(n2,
327 &tmproot->n_children, nodehead, n_next, tmp2) {
328 node_move_after(n2, n);
332 node_delete(tmproot);
337 expand_ampersand(char *string, const char *key)
340 int i, ret, before_len = 0;
341 bool backslashed = false;
343 assert(key[0] != '\0');
345 expanded = checked_strdup(string);
347 for (i = 0; string[i] != '\0'; i++) {
349 if (c == '\\' && backslashed == false) {
362 * The 'before_len' variable contains the number
363 * of characters before the '&'.
366 //assert(i + 1 < (int)strlen(string));
368 ret = asprintf(&expanded, "%.*s%s%s",
369 before_len, string, key, string + before_len + 1);
371 log_err(1, "asprintf");
373 //log_debugx("\"%s\" expanded with key \"%s\" to \"%s\"",
374 // string, key, expanded);
377 * Figure out where to start searching for next variable.
380 i = before_len + strlen(key);
382 //assert(i < (int)strlen(string));
389 * Expand "&" in n_location. If the key is NULL, try to use
390 * key from map entries themselves. Keep in mind that maps
391 * consist of tho levels of node structures, the key is one
394 * Variant with NULL key is for "automount -LL".
397 node_expand_ampersand(struct node *n, const char *key)
401 if (n->n_location != NULL) {
403 if (n->n_parent != NULL &&
404 strcmp(n->n_parent->n_key, "*") != 0) {
405 n->n_location = expand_ampersand(n->n_location,
409 n->n_location = expand_ampersand(n->n_location, key);
413 TAILQ_FOREACH(child, &n->n_children, n_next)
414 node_expand_ampersand(child, key);
418 * Expand "*" in n_key.
421 node_expand_wildcard(struct node *n, const char *key)
423 struct node *child, *expanded;
427 if (strcmp(n->n_key, "*") == 0) {
428 expanded = node_duplicate(n, NULL);
429 expanded->n_key = checked_strdup(key);
430 node_move_after(expanded, n);
433 TAILQ_FOREACH(child, &n->n_children, n_next)
434 node_expand_wildcard(child, key);
438 node_expand_defined(struct node *n)
441 int error, cumulated_error = 0;
443 if (n->n_location != NULL) {
444 n->n_location = defined_expand(n->n_location);
445 if (n->n_location == NULL) {
446 log_warnx("failed to expand location for %s",
452 TAILQ_FOREACH(child, &n->n_children, n_next) {
453 error = node_expand_defined(child);
454 if (error != 0 && cumulated_error == 0)
455 cumulated_error = error;
458 return (cumulated_error);
462 node_is_direct_key(const struct node *n)
465 if (n->n_parent != NULL && n->n_parent->n_parent == NULL &&
466 strcmp(n->n_key, "/-") == 0) {
474 node_is_direct_map(const struct node *n)
478 assert(n->n_parent != NULL);
479 if (n->n_parent->n_parent == NULL)
484 return (node_is_direct_key(n));
488 node_has_wildcards(const struct node *n)
490 const struct node *child;
492 TAILQ_FOREACH(child, &n->n_children, n_next) {
493 if (strcmp(child->n_key, "*") == 0)
501 node_expand_maps(struct node *n, bool indirect)
503 struct node *child, *tmp;
505 TAILQ_FOREACH_SAFE(child, &n->n_children, n_next, tmp) {
506 if (node_is_direct_map(child)) {
510 if (indirect == false)
515 * This is the first-level map node; the one that contains
516 * the key and subnodes with mountpoints and actual map names.
518 if (child->n_map == NULL)
522 log_debugx("map \"%s\" is an indirect map, parsing",
525 log_debugx("map \"%s\" is a direct map, parsing",
528 parse_map(child, child->n_map, NULL, NULL);
533 node_expand_direct_maps(struct node *n)
536 node_expand_maps(n, false);
540 node_expand_indirect_maps(struct node *n)
543 node_expand_maps(n, true);
547 node_path_x(const struct node *n, char *x)
551 if (n->n_parent == NULL)
555 * Return "/-" for direct maps only if we were asked for path
556 * to the "/-" node itself, not to any of its subnodes.
558 if (node_is_direct_key(n) && x[0] != '\0')
561 assert(n->n_key[0] != '\0');
562 path = concat(n->n_key, '/', x);
565 return (node_path_x(n->n_parent, path));
569 * Return full path for node, consisting of concatenated
570 * paths of node itself and all its parents, up to the root.
573 node_path(const struct node *n)
578 path = node_path_x(n, checked_strdup(""));
581 * Strip trailing slash, unless the whole path is "/".
584 if (len > 1 && path[len - 1] == '/')
585 path[len - 1] = '\0';
591 node_options_x(const struct node *n, char *x)
598 options = concat(x, ',', n->n_options);
601 return (node_options_x(n->n_parent, options));
605 * Return options for node, consisting of concatenated
606 * options from the node itself and all its parents,
610 node_options(const struct node *n)
613 return (node_options_x(n, checked_strdup("")));
617 node_print_indent(const struct node *n, const char *cmdline_options,
620 const struct node *child, *first_child;
621 char *path, *options, *tmp;
624 tmp = node_options(n);
625 options = concat(cmdline_options, ',', tmp);
629 * Do not show both parent and child node if they have the same
630 * mountpoint; only show the child node. This means the typical,
631 * "key location", map entries are shown in a single line;
632 * the "key mountpoint1 location2 mountpoint2 location2" entries
633 * take multiple lines.
635 first_child = TAILQ_FIRST(&n->n_children);
636 if (first_child == NULL || TAILQ_NEXT(first_child, n_next) != NULL ||
637 strcmp(path, node_path(first_child)) != 0) {
638 assert(n->n_location == NULL || n->n_map == NULL);
639 printf("%*.s%-*s %s%-*s %-*s # %s map %s at %s:%d\n",
643 options[0] != '\0' ? "-" : " ",
645 options[0] != '\0' ? options : "",
647 n->n_location != NULL ? n->n_location : n->n_map != NULL ? n->n_map : "",
648 node_is_direct_map(n) ? "direct" : "indirect",
649 indent == 0 ? "referenced" : "defined",
650 n->n_config_file, n->n_config_line);
656 TAILQ_FOREACH(child, &n->n_children, n_next)
657 node_print_indent(child, cmdline_options, indent + 2);
661 * Recursively print node with all its children. The cmdline_options
662 * argument is used for additional options to be prepended to all the
663 * others - usually those are the options passed by command line.
666 node_print(const struct node *n, const char *cmdline_options)
668 const struct node *child;
670 TAILQ_FOREACH(child, &n->n_children, n_next)
671 node_print_indent(child, cmdline_options, 0);
675 node_find_x(struct node *node, const char *path)
677 struct node *child, *found;
681 //log_debugx("looking up %s in %s", path, node_path(node));
683 if (!node_is_direct_key(node)) {
684 tmp = node_path(node);
685 tmplen = strlen(tmp);
686 if (strncmp(tmp, path, tmplen) != 0) {
690 if (path[tmplen] != '/' && path[tmplen] != '\0') {
692 * If we have two map entries like 'foo' and 'foobar', make
693 * sure the search for 'foobar' won't match 'foo' instead.
701 TAILQ_FOREACH(child, &node->n_children, n_next) {
702 found = node_find_x(child, path);
707 if (node->n_parent == NULL || node_is_direct_key(node))
714 node_find(struct node *root, const char *path)
718 assert(root->n_parent == NULL);
720 node = node_find_x(root, path);
722 assert(node != root);
728 * Canonical form of a map entry looks like this:
730 * key [-options] [ [/mountpoint] [-options2] location ... ]
732 * Entries for executable maps are slightly different, as they
733 * lack the 'key' field and are always single-line; the key field
734 * for those maps is taken from 'executable_key' argument.
736 * We parse it in such a way that a map always has two levels - first
737 * for key, and the second, for the mountpoint.
740 parse_map_yyin(struct node *parent, const char *map, const char *executable_key)
742 char *key = NULL, *options = NULL, *mountpoint = NULL,
743 *options2 = NULL, *location = NULL;
749 if (executable_key != NULL)
750 key = checked_strdup(executable_key);
754 if (ret == 0 || ret == NEWLINE) {
756 * In case of executable map, the key is always
757 * non-NULL, even if the map is empty. So, make sure
758 * we don't fail empty maps here.
760 if ((key != NULL && executable_key == NULL) ||
762 log_errx(1, "truncated entry at %s, line %d",
765 if (ret == 0 || executable_key != NULL) {
771 key = options = NULL;
776 key = checked_strdup(yytext);
778 node_new(parent, key, NULL, NULL, map, lineno);
779 key = options = NULL;
783 } else if (yytext[0] == '-') {
784 if (options != NULL) {
785 log_errx(1, "duplicated options at %s, line %d",
789 * +1 to skip leading "-".
791 options = checked_strdup(yytext + 1);
796 * We cannot properly handle a situation where the map key
797 * is "/". Ignore such entries.
799 * XXX: According to Piete Brooks, Linux automounter uses
800 * "/" as a wildcard character in LDAP maps. Perhaps
801 * we should work around this braindamage by substituting
804 if (strcmp(key, "/") == 0) {
805 log_warnx("nonsensical map key \"/\" at %s, line %d; "
806 "ignoring map entry ", map, lineno);
809 * Skip the rest of the entry.
813 } while (ret != 0 && ret != NEWLINE);
815 key = options = NULL;
819 //log_debugx("adding map node, %s", key);
820 node = node_new(parent, key, options, NULL, map, lineno);
821 key = options = NULL;
824 if (yytext[0] == '/') {
825 if (mountpoint != NULL) {
826 log_errx(1, "duplicated mountpoint "
827 "in %s, line %d", map, lineno);
829 if (options2 != NULL || location != NULL) {
830 log_errx(1, "mountpoint out of order "
831 "in %s, line %d", map, lineno);
833 mountpoint = checked_strdup(yytext);
837 if (yytext[0] == '-') {
838 if (options2 != NULL) {
839 log_errx(1, "duplicated options "
840 "in %s, line %d", map, lineno);
842 if (location != NULL) {
843 log_errx(1, "options out of order "
844 "in %s, line %d", map, lineno);
846 options2 = checked_strdup(yytext + 1);
850 if (location != NULL) {
851 log_errx(1, "too many arguments "
852 "in %s, line %d", map, lineno);
856 * If location field starts with colon, e.g. ":/dev/cd0",
859 if (yytext[0] == ':') {
860 location = checked_strdup(yytext + 1);
861 if (location[0] == '\0') {
862 log_errx(1, "empty location in %s, "
863 "line %d", map, lineno);
866 location = checked_strdup(yytext);
869 if (mountpoint == NULL)
870 mountpoint = checked_strdup("/");
871 if (options2 == NULL)
872 options2 = checked_strdup("");
875 log_debugx("adding map node, %s %s %s",
876 mountpoint, options2, location);
878 node_new(node, mountpoint, options2, location,
880 mountpoint = options2 = location = NULL;
883 if (ret == 0 || ret == NEWLINE) {
884 if (mountpoint != NULL || options2 != NULL ||
886 log_errx(1, "truncated entry "
887 "in %s, line %d", map, lineno);
896 * Parse output of a special map called without argument. It is a list
897 * of keys, separated by newlines. They can contain whitespace, so use
898 * getline(3) instead of lexer used for maps.
901 parse_map_keys_yyin(struct node *parent, const char *map)
903 char *line = NULL, *key;
910 linelen = getline(&line, &linecap, yyin);
919 * Empty line, consisting of just the newline.
925 * "-1" to strip the trailing newline.
927 key = strndup(line, linelen - 1);
929 log_debugx("adding key \"%s\"", key);
930 node_new(parent, key, NULL, NULL, map, lineno);
937 file_is_executable(const char *path)
942 error = stat(path, &sb);
944 log_err(1, "cannot stat %s", path);
945 if ((sb.st_mode & S_IXUSR) || (sb.st_mode & S_IXGRP) ||
946 (sb.st_mode & S_IXOTH))
952 * Parse a special map, e.g. "-hosts".
955 parse_special_map(struct node *parent, const char *map, const char *key)
960 assert(map[0] == '-');
963 * +1 to skip leading "-" in map name.
965 ret = asprintf(&path, "%s/special_%s", AUTO_SPECIAL_PREFIX, map + 1);
967 log_err(1, "asprintf");
969 yyin = auto_popen(path, key, NULL);
970 assert(yyin != NULL);
973 parse_map_keys_yyin(parent, map);
975 parse_map_yyin(parent, map, key);
978 error = auto_pclose(yyin);
981 log_errx(1, "failed to handle special map \"%s\"", map);
983 node_expand_includes(parent, false);
984 node_expand_direct_maps(parent);
990 * Retrieve and parse map from directory services, e.g. LDAP.
991 * Note that it is different from executable maps, in that
992 * the include script outputs the whole map to standard output
993 * (as opposed to executable maps that only output a single
994 * entry, without the key), and it takes the map name as an
995 * argument, instead of key.
998 parse_included_map(struct node *parent, const char *map)
1002 assert(map[0] != '-');
1003 assert(map[0] != '/');
1005 error = access(AUTO_INCLUDE_PATH, F_OK);
1007 log_errx(1, "directory services not configured;"
1008 " %s does not exist", AUTO_INCLUDE_PATH);
1011 yyin = auto_popen(AUTO_INCLUDE_PATH, map, NULL);
1012 assert(yyin != NULL);
1014 parse_map_yyin(parent, map, NULL);
1016 error = auto_pclose(yyin);
1019 log_errx(1, "failed to handle remote map \"%s\"", map);
1021 node_expand_includes(parent, false);
1022 node_expand_direct_maps(parent);
1026 parse_map(struct node *parent, const char *map, const char *key,
1033 assert(map != NULL);
1034 assert(map[0] != '\0');
1036 log_debugx("parsing map \"%s\"", map);
1038 if (wildcards != NULL)
1041 if (map[0] == '-') {
1042 if (wildcards != NULL)
1044 return (parse_special_map(parent, map, key));
1047 if (map[0] == '/') {
1048 path = checked_strdup(map);
1050 ret = asprintf(&path, "%s/%s", AUTO_MAP_PREFIX, map);
1052 log_err(1, "asprintf");
1053 log_debugx("map \"%s\" maps to \"%s\"", map, path);
1056 * See if the file exists. If not, try to obtain the map
1057 * from directory services.
1059 error = access(path, F_OK);
1061 log_debugx("map file \"%s\" does not exist; falling "
1062 "back to directory services", path);
1063 return (parse_included_map(parent, map));
1067 executable = file_is_executable(path);
1070 log_debugx("map \"%s\" is executable", map);
1072 if (wildcards != NULL)
1076 yyin = auto_popen(path, key, NULL);
1078 yyin = auto_popen(path, NULL);
1080 assert(yyin != NULL);
1082 yyin = fopen(path, "r");
1084 log_err(1, "unable to open \"%s\"", path);
1090 parse_map_yyin(parent, map, executable ? key : NULL);
1093 error = auto_pclose(yyin);
1096 log_errx(1, "failed to handle executable map \"%s\"",
1104 log_debugx("done parsing map \"%s\"", map);
1106 node_expand_includes(parent, false);
1107 node_expand_direct_maps(parent);
1111 parse_master_yyin(struct node *root, const char *master)
1113 char *mountpoint = NULL, *map = NULL, *options = NULL;
1117 * XXX: 1 gives incorrect values; wtf?
1123 if (ret == 0 || ret == NEWLINE) {
1124 if (mountpoint != NULL) {
1125 //log_debugx("adding map for %s", mountpoint);
1126 node_new_map(root, mountpoint, options, map,
1132 mountpoint = map = options = NULL;
1136 if (mountpoint == NULL) {
1137 mountpoint = checked_strdup(yytext);
1138 } else if (map == NULL) {
1139 map = checked_strdup(yytext);
1140 } else if (options == NULL) {
1142 * +1 to skip leading "-".
1144 options = checked_strdup(yytext + 1);
1146 log_errx(1, "too many arguments at %s, line %d",
1153 parse_master(struct node *root, const char *master)
1156 log_debugx("parsing auto_master file at \"%s\"", master);
1158 yyin = fopen(master, "r");
1160 err(1, "unable to open %s", master);
1162 parse_master_yyin(root, master);
1167 log_debugx("done parsing \"%s\"", master);
1169 node_expand_includes(root, true);
1170 node_expand_direct_maps(root);
1174 * Two things daemon(3) does, that we actually also want to do
1175 * when running in foreground, is closing the stdin and chdiring
1176 * to "/". This is what we do here.
1187 fd = open(_PATH_DEVNULL, O_RDWR, 0);
1189 log_warn("cannot open %s", _PATH_DEVNULL);
1193 error = dup2(fd, STDIN_FILENO);
1205 main(int argc, char **argv)
1209 if (argv[0] == NULL)
1210 log_errx(1, "NULL command name");
1212 cmdname = basename(argv[0]);
1214 if (strcmp(cmdname, "automount") == 0)
1215 return (main_automount(argc, argv));
1216 else if (strcmp(cmdname, "automountd") == 0)
1217 return (main_automountd(argc, argv));
1218 else if (strcmp(cmdname, "autounmountd") == 0)
1219 return (main_autounmountd(argc, argv));
1221 log_errx(1, "binary name should be either \"automount\", "
1222 "\"automountd\", or \"autounmountd\"");