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>
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)
103 * If s2 starts with separator - skip it; otherwise concatenating
104 * "/" and "/foo" would end up returning "//foo".
106 if (s2[0] == separator)
109 if (s1[0] == '\0' || s2[0] == '\0' || s1[strlen(s1) - 1] == separator) {
110 ret = asprintf(&result, "%s%s", s1, s2);
112 ret = asprintf(&result, "%s%c%s", s1, separator, s2);
115 log_err(1, "asprintf");
117 //log_debugx("%s: got %s and %s, returning %s", __func__, s1, s2, result);
123 create_directory(const char *path)
125 char *component, *copy, *tofree, *partial, *tmp;
128 assert(path[0] == '/');
131 * +1 to skip the leading slash.
133 copy = tofree = checked_strdup(path + 1);
135 partial = checked_strdup("");
137 component = strsep(©, "/");
138 if (component == NULL)
140 tmp = concat(partial, '/', component);
143 //log_debugx("creating \"%s\"", partial);
144 error = mkdir(partial, 0755);
145 if (error != 0 && errno != EEXIST) {
146 log_warn("cannot create %s", partial);
159 n = calloc(1, sizeof(*n));
161 log_err(1, "calloc");
163 n->n_key = checked_strdup("/");
164 n->n_options = checked_strdup("");
166 TAILQ_INIT(&n->n_children);
172 node_new(struct node *parent, char *key, char *options, char *location,
173 const char *config_file, int config_line)
177 n = calloc(1, sizeof(*n));
179 log_err(1, "calloc");
181 TAILQ_INIT(&n->n_children);
183 assert(key[0] != '\0');
186 n->n_options = options;
188 n->n_options = strdup("");
189 n->n_location = location;
190 assert(config_file != NULL);
191 n->n_config_file = config_file;
192 assert(config_line >= 0);
193 n->n_config_line = config_line;
195 assert(parent != NULL);
196 n->n_parent = parent;
197 TAILQ_INSERT_TAIL(&parent->n_children, n, n_next);
203 node_new_map(struct node *parent, char *key, char *options, char *map,
204 const char *config_file, int config_line)
208 n = calloc(1, sizeof(*n));
210 log_err(1, "calloc");
212 TAILQ_INIT(&n->n_children);
214 assert(key[0] != '\0');
217 n->n_options = options;
219 n->n_options = strdup("");
221 assert(config_file != NULL);
222 n->n_config_file = config_file;
223 assert(config_line >= 0);
224 n->n_config_line = config_line;
226 assert(parent != NULL);
227 n->n_parent = parent;
228 TAILQ_INSERT_TAIL(&parent->n_children, n, n_next);
234 node_duplicate(const struct node *o, struct node *parent)
236 const struct node *child;
240 parent = o->n_parent;
242 n = node_new(parent, o->n_key, o->n_options, o->n_location,
243 o->n_config_file, o->n_config_line);
245 TAILQ_FOREACH(child, &o->n_children, n_next)
246 node_duplicate(child, n);
252 node_delete(struct node *n)
254 struct node *child, *tmp;
258 TAILQ_FOREACH_SAFE(child, &n->n_children, n_next, tmp)
261 if (n->n_parent != NULL)
262 TAILQ_REMOVE(&n->n_parent->n_children, n, n_next);
268 * Move (reparent) node 'n' to make it sibling of 'previous', placed
272 node_move_after(struct node *n, struct node *previous)
275 TAILQ_REMOVE(&n->n_parent->n_children, n, n_next);
276 n->n_parent = previous->n_parent;
277 TAILQ_INSERT_AFTER(&previous->n_parent->n_children, previous, n, n_next);
281 node_expand_includes(struct node *root, bool is_master)
283 struct node *n, *n2, *tmp, *tmp2, *tmproot;
286 TAILQ_FOREACH_SAFE(n, &root->n_children, n_next, tmp) {
287 if (n->n_key[0] != '+')
290 error = access(AUTO_INCLUDE_PATH, F_OK);
292 log_errx(1, "directory services not configured; "
293 "%s does not exist", AUTO_INCLUDE_PATH);
297 * "+1" to skip leading "+".
299 yyin = auto_popen(AUTO_INCLUDE_PATH, n->n_key + 1, NULL);
300 assert(yyin != NULL);
302 tmproot = node_new_root();
304 parse_master_yyin(tmproot, n->n_key);
306 parse_map_yyin(tmproot, n->n_key, NULL);
308 error = auto_pclose(yyin);
311 log_errx(1, "failed to handle include \"%s\"",
316 * Entries to be included are now in tmproot. We need to merge
317 * them with the rest, preserving their place and ordering.
319 TAILQ_FOREACH_REVERSE_SAFE(n2,
320 &tmproot->n_children, nodehead, n_next, tmp2) {
321 node_move_after(n2, n);
325 node_delete(tmproot);
330 expand_ampersand(char *string, const char *key)
333 int i, ret, before_len = 0;
334 bool backslashed = false;
336 assert(key[0] != '\0');
338 expanded = checked_strdup(string);
340 for (i = 0; string[i] != '\0'; i++) {
342 if (c == '\\' && backslashed == false) {
355 * The 'before_len' variable contains the number
356 * of characters before the '&'.
359 //assert(i + 1 < (int)strlen(string));
361 ret = asprintf(&expanded, "%.*s%s%s",
362 before_len, string, key, string + before_len + 1);
364 log_err(1, "asprintf");
366 //log_debugx("\"%s\" expanded with key \"%s\" to \"%s\"",
367 // string, key, expanded);
370 * Figure out where to start searching for next variable.
373 i = before_len + strlen(key);
375 //assert(i < (int)strlen(string));
382 * Expand "&" in n_location. If the key is NULL, try to use
383 * key from map entries themselves. Keep in mind that maps
384 * consist of tho levels of node structures, the key is one
387 * Variant with NULL key is for "automount -LL".
390 node_expand_ampersand(struct node *n, const char *key)
394 if (n->n_location != NULL) {
396 if (n->n_parent != NULL &&
397 strcmp(n->n_parent->n_key, "*") != 0) {
398 n->n_location = expand_ampersand(n->n_location,
402 n->n_location = expand_ampersand(n->n_location, key);
406 TAILQ_FOREACH(child, &n->n_children, n_next)
407 node_expand_ampersand(child, key);
411 * Expand "*" in n_key.
414 node_expand_wildcard(struct node *n, const char *key)
416 struct node *child, *expanded;
420 if (strcmp(n->n_key, "*") == 0) {
421 expanded = node_duplicate(n, NULL);
422 expanded->n_key = checked_strdup(key);
423 node_move_after(expanded, n);
426 TAILQ_FOREACH(child, &n->n_children, n_next)
427 node_expand_wildcard(child, key);
431 node_expand_defined(struct node *n)
434 int error, cumulated_error = 0;
436 if (n->n_location != NULL) {
437 n->n_location = defined_expand(n->n_location);
438 if (n->n_location == NULL) {
439 log_warnx("failed to expand location for %s",
445 TAILQ_FOREACH(child, &n->n_children, n_next) {
446 error = node_expand_defined(child);
447 if (error != 0 && cumulated_error == 0)
448 cumulated_error = error;
451 return (cumulated_error);
455 node_is_direct_key(const struct node *n)
458 if (n->n_parent != NULL && n->n_parent->n_parent == NULL &&
459 strcmp(n->n_key, "/-") == 0) {
467 node_is_direct_map(const struct node *n)
471 assert(n->n_parent != NULL);
472 if (n->n_parent->n_parent == NULL)
477 return (node_is_direct_key(n));
481 node_has_wildcards(const struct node *n)
483 const struct node *child;
485 TAILQ_FOREACH(child, &n->n_children, n_next) {
486 if (strcmp(child->n_key, "*") == 0)
494 node_expand_maps(struct node *n, bool indirect)
496 struct node *child, *tmp;
498 TAILQ_FOREACH_SAFE(child, &n->n_children, n_next, tmp) {
499 if (node_is_direct_map(child)) {
503 if (indirect == false)
508 * This is the first-level map node; the one that contains
509 * the key and subnodes with mountpoints and actual map names.
511 if (child->n_map == NULL)
515 log_debugx("map \"%s\" is an indirect map, parsing",
518 log_debugx("map \"%s\" is a direct map, parsing",
521 parse_map(child, child->n_map, NULL, NULL);
526 node_expand_direct_maps(struct node *n)
529 node_expand_maps(n, false);
533 node_expand_indirect_maps(struct node *n)
536 node_expand_maps(n, true);
540 node_path_x(const struct node *n, char *x)
544 if (n->n_parent == NULL)
548 * Return "/-" for direct maps only if we were asked for path
549 * to the "/-" node itself, not to any of its subnodes.
551 if (node_is_direct_key(n) && x[0] != '\0')
554 assert(n->n_key[0] != '\0');
555 path = concat(n->n_key, '/', x);
558 return (node_path_x(n->n_parent, path));
562 * Return full path for node, consisting of concatenated
563 * paths of node itself and all its parents, up to the root.
566 node_path(const struct node *n)
571 path = node_path_x(n, checked_strdup(""));
574 * Strip trailing slash, unless the whole path is "/".
577 if (len > 1 && path[len - 1] == '/')
578 path[len - 1] = '\0';
584 node_options_x(const struct node *n, char *x)
591 options = concat(x, ',', n->n_options);
594 return (node_options_x(n->n_parent, options));
598 * Return options for node, consisting of concatenated
599 * options from the node itself and all its parents,
603 node_options(const struct node *n)
606 return (node_options_x(n, checked_strdup("")));
610 node_print_indent(const struct node *n, int indent)
612 const struct node *child, *first_child;
613 char *path, *options;
616 options = node_options(n);
619 * Do not show both parent and child node if they have the same
620 * mountpoint; only show the child node. This means the typical,
621 * "key location", map entries are shown in a single line;
622 * the "key mountpoint1 location2 mountpoint2 location2" entries
623 * take multiple lines.
625 first_child = TAILQ_FIRST(&n->n_children);
626 if (first_child == NULL || TAILQ_NEXT(first_child, n_next) != NULL ||
627 strcmp(path, node_path(first_child)) != 0) {
628 assert(n->n_location == NULL || n->n_map == NULL);
629 printf("%*.s%-*s %s%-*s %-*s # %s map %s at %s:%d\n",
633 options[0] != '\0' ? "-" : " ",
635 options[0] != '\0' ? options : "",
637 n->n_location != NULL ? n->n_location : n->n_map != NULL ? n->n_map : "",
638 node_is_direct_map(n) ? "direct" : "indirect",
639 indent == 0 ? "referenced" : "defined",
640 n->n_config_file, n->n_config_line);
646 TAILQ_FOREACH(child, &n->n_children, n_next)
647 node_print_indent(child, indent + 2);
651 node_print(const struct node *n)
653 const struct node *child;
655 TAILQ_FOREACH(child, &n->n_children, n_next)
656 node_print_indent(child, 0);
660 node_find_x(struct node *node, const char *path)
662 struct node *child, *found;
666 //log_debugx("looking up %s in %s", path, node_path(node));
668 if (!node_is_direct_key(node)) {
669 tmp = node_path(node);
670 tmplen = strlen(tmp);
671 if (strncmp(tmp, path, tmplen) != 0) {
675 if (path[tmplen] != '/' && path[tmplen] != '\0') {
677 * If we have two map entries like 'foo' and 'foobar', make
678 * sure the search for 'foobar' won't match 'foo' instead.
686 TAILQ_FOREACH(child, &node->n_children, n_next) {
687 found = node_find_x(child, path);
692 if (node->n_parent == NULL || node_is_direct_key(node))
699 node_find(struct node *root, const char *path)
703 assert(root->n_parent == NULL);
705 node = node_find_x(root, path);
707 assert(node != root);
713 * Canonical form of a map entry looks like this:
715 * key [-options] [ [/mountpoint] [-options2] location ... ]
717 * Entries for executable maps are slightly different, as they
718 * lack the 'key' field and are always single-line; the key field
719 * for those maps is taken from 'executable_key' argument.
721 * We parse it in such a way that a map always has two levels - first
722 * for key, and the second, for the mountpoint.
725 parse_map_yyin(struct node *parent, const char *map, const char *executable_key)
727 char *key = NULL, *options = NULL, *mountpoint = NULL,
728 *options2 = NULL, *location = NULL;
734 if (executable_key != NULL)
735 key = checked_strdup(executable_key);
739 if (ret == 0 || ret == NEWLINE) {
741 * In case of executable map, the key is always
742 * non-NULL, even if the map is empty. So, make sure
743 * we don't fail empty maps here.
745 if ((key != NULL && executable_key == NULL) ||
747 log_errx(1, "truncated entry at %s, line %d",
750 if (ret == 0 || executable_key != NULL) {
756 key = options = NULL;
761 key = checked_strdup(yytext);
763 node_new(parent, key, NULL, NULL, map, lineno);
764 key = options = NULL;
768 } else if (yytext[0] == '-') {
769 if (options != NULL) {
770 log_errx(1, "duplicated options at %s, line %d",
774 * +1 to skip leading "-".
776 options = checked_strdup(yytext + 1);
781 * We cannot properly handle a situation where the map key
782 * is "/". Ignore such entries.
784 * XXX: According to Piete Brooks, Linux automounter uses
785 * "/" as a wildcard character in LDAP maps. Perhaps
786 * we should work around this braindamage by substituting
789 if (strcmp(key, "/") == 0) {
790 log_warnx("nonsensical map key \"/\" at %s, line %d; "
791 "ignoring map entry ", map, lineno);
794 * Skip the rest of the entry.
798 } while (ret != 0 && ret != NEWLINE);
800 key = options = NULL;
804 //log_debugx("adding map node, %s", key);
805 node = node_new(parent, key, options, NULL, map, lineno);
806 key = options = NULL;
809 if (yytext[0] == '/') {
810 if (mountpoint != NULL) {
811 log_errx(1, "duplicated mountpoint "
812 "in %s, line %d", map, lineno);
814 if (options2 != NULL || location != NULL) {
815 log_errx(1, "mountpoint out of order "
816 "in %s, line %d", map, lineno);
818 mountpoint = checked_strdup(yytext);
822 if (yytext[0] == '-') {
823 if (options2 != NULL) {
824 log_errx(1, "duplicated options "
825 "in %s, line %d", map, lineno);
827 if (location != NULL) {
828 log_errx(1, "options out of order "
829 "in %s, line %d", map, lineno);
831 options2 = checked_strdup(yytext + 1);
835 if (location != NULL) {
836 log_errx(1, "too many arguments "
837 "in %s, line %d", map, lineno);
841 * If location field starts with colon, e.g. ":/dev/cd0",
844 if (yytext[0] == ':') {
845 location = checked_strdup(yytext + 1);
846 if (location[0] == '\0') {
847 log_errx(1, "empty location in %s, "
848 "line %d", map, lineno);
851 location = checked_strdup(yytext);
854 if (mountpoint == NULL)
855 mountpoint = checked_strdup("/");
856 if (options2 == NULL)
857 options2 = checked_strdup("");
860 log_debugx("adding map node, %s %s %s",
861 mountpoint, options2, location);
863 node_new(node, mountpoint, options2, location,
865 mountpoint = options2 = location = NULL;
868 if (ret == 0 || ret == NEWLINE) {
869 if (mountpoint != NULL || options2 != NULL ||
871 log_errx(1, "truncated entry "
872 "in %s, line %d", map, lineno);
881 * Parse output of a special map called without argument. It is a list
882 * of keys, separated by newlines. They can contain whitespace, so use
883 * getline(3) instead of lexer used for maps.
886 parse_map_keys_yyin(struct node *parent, const char *map)
888 char *line = NULL, *key;
895 linelen = getline(&line, &linecap, yyin);
904 * Empty line, consisting of just the newline.
910 * "-1" to strip the trailing newline.
912 key = strndup(line, linelen - 1);
914 log_debugx("adding key \"%s\"", key);
915 node_new(parent, key, NULL, NULL, map, lineno);
922 file_is_executable(const char *path)
927 error = stat(path, &sb);
929 log_err(1, "cannot stat %s", path);
930 if ((sb.st_mode & S_IXUSR) || (sb.st_mode & S_IXGRP) ||
931 (sb.st_mode & S_IXOTH))
937 * Parse a special map, e.g. "-hosts".
940 parse_special_map(struct node *parent, const char *map, const char *key)
945 assert(map[0] == '-');
948 * +1 to skip leading "-" in map name.
950 ret = asprintf(&path, "%s/special_%s", AUTO_SPECIAL_PREFIX, map + 1);
952 log_err(1, "asprintf");
954 yyin = auto_popen(path, key, NULL);
955 assert(yyin != NULL);
958 parse_map_keys_yyin(parent, map);
960 parse_map_yyin(parent, map, key);
963 error = auto_pclose(yyin);
966 log_errx(1, "failed to handle special map \"%s\"", map);
968 node_expand_includes(parent, false);
969 node_expand_direct_maps(parent);
975 * Retrieve and parse map from directory services, e.g. LDAP.
976 * Note that it is different from executable maps, in that
977 * the include script outputs the whole map to standard output
978 * (as opposed to executable maps that only output a single
979 * entry, without the key), and it takes the map name as an
980 * argument, instead of key.
983 parse_included_map(struct node *parent, const char *map)
987 assert(map[0] != '-');
988 assert(map[0] != '/');
990 error = access(AUTO_INCLUDE_PATH, F_OK);
992 log_errx(1, "directory services not configured;"
993 " %s does not exist", AUTO_INCLUDE_PATH);
996 yyin = auto_popen(AUTO_INCLUDE_PATH, map, NULL);
997 assert(yyin != NULL);
999 parse_map_yyin(parent, map, NULL);
1001 error = auto_pclose(yyin);
1004 log_errx(1, "failed to handle remote map \"%s\"", map);
1006 node_expand_includes(parent, false);
1007 node_expand_direct_maps(parent);
1011 parse_map(struct node *parent, const char *map, const char *key,
1018 assert(map != NULL);
1019 assert(map[0] != '\0');
1021 log_debugx("parsing map \"%s\"", map);
1023 if (wildcards != NULL)
1026 if (map[0] == '-') {
1027 if (wildcards != NULL)
1029 return (parse_special_map(parent, map, key));
1032 if (map[0] == '/') {
1033 path = checked_strdup(map);
1035 ret = asprintf(&path, "%s/%s", AUTO_MAP_PREFIX, map);
1037 log_err(1, "asprintf");
1038 log_debugx("map \"%s\" maps to \"%s\"", map, path);
1041 * See if the file exists. If not, try to obtain the map
1042 * from directory services.
1044 error = access(path, F_OK);
1046 log_debugx("map file \"%s\" does not exist; falling "
1047 "back to directory services", path);
1048 return (parse_included_map(parent, map));
1052 executable = file_is_executable(path);
1055 log_debugx("map \"%s\" is executable", map);
1057 if (wildcards != NULL)
1061 yyin = auto_popen(path, key, NULL);
1063 yyin = auto_popen(path, NULL);
1065 assert(yyin != NULL);
1067 yyin = fopen(path, "r");
1069 log_err(1, "unable to open \"%s\"", path);
1075 parse_map_yyin(parent, map, executable ? key : NULL);
1078 error = auto_pclose(yyin);
1081 log_errx(1, "failed to handle executable map \"%s\"",
1089 log_debugx("done parsing map \"%s\"", map);
1091 node_expand_includes(parent, false);
1092 node_expand_direct_maps(parent);
1096 parse_master_yyin(struct node *root, const char *master)
1098 char *mountpoint = NULL, *map = NULL, *options = NULL;
1102 * XXX: 1 gives incorrect values; wtf?
1108 if (ret == 0 || ret == NEWLINE) {
1109 if (mountpoint != NULL) {
1110 //log_debugx("adding map for %s", mountpoint);
1111 node_new_map(root, mountpoint, options, map,
1117 mountpoint = map = options = NULL;
1121 if (mountpoint == NULL) {
1122 mountpoint = checked_strdup(yytext);
1123 } else if (map == NULL) {
1124 map = checked_strdup(yytext);
1125 } else if (options == NULL) {
1127 * +1 to skip leading "-".
1129 options = checked_strdup(yytext + 1);
1131 log_errx(1, "too many arguments at %s, line %d",
1138 parse_master(struct node *root, const char *master)
1141 log_debugx("parsing auto_master file at \"%s\"", master);
1143 yyin = fopen(master, "r");
1145 err(1, "unable to open %s", master);
1147 parse_master_yyin(root, master);
1152 log_debugx("done parsing \"%s\"", master);
1154 node_expand_includes(root, true);
1155 node_expand_direct_maps(root);
1159 * Two things daemon(3) does, that we actually also want to do
1160 * when running in foreground, is closing the stdin and chdiring
1161 * to "/". This is what we do here.
1172 fd = open(_PATH_DEVNULL, O_RDWR, 0);
1174 log_warn("cannot open %s", _PATH_DEVNULL);
1178 error = dup2(fd, STDIN_FILENO);
1190 main(int argc, char **argv)
1194 if (argv[0] == NULL)
1195 log_errx(1, "NULL command name");
1197 cmdname = basename(argv[0]);
1199 if (strcmp(cmdname, "automount") == 0)
1200 return (main_automount(argc, argv));
1201 else if (strcmp(cmdname, "automountd") == 0)
1202 return (main_automountd(argc, argv));
1203 else if (strcmp(cmdname, "autounmountd") == 0)
1204 return (main_autounmountd(argc, argv));
1206 log_errx(1, "binary name should be either \"automount\", "
1207 "\"automountd\", or \"autounmountd\"");