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 * Take two pointers to strings, concatenate the contents with "/" in the
90 * middle, make the first pointer point to the result, the second pointer
91 * to NULL, and free the old strings.
93 * Concatenate pathnames, basically.
96 concat(char **p1, char **p2)
105 *p1 = checked_strdup("");
108 *p2 = checked_strdup("");
110 ret = asprintf(&path, "%s/%s", *p1, *p2);
112 log_err(1, "asprintf");
125 * Concatenate two strings, inserting separator between them, unless not needed.
127 * This function is very convenient to use when you do not care about freeing
128 * memory - which is okay here, because we are a short running process.
131 separated_concat(const char *s1, const char *s2, char separator)
139 if (s1[0] == '\0' || s2[0] == '\0' ||
140 s1[strlen(s1) - 1] == separator || s2[0] == separator) {
141 ret = asprintf(&result, "%s%s", s1, s2);
143 ret = asprintf(&result, "%s%c%s", s1, separator, s2);
146 log_err(1, "asprintf");
148 //log_debugx("separated_concat: got %s and %s, returning %s", s1, s2, result);
154 create_directory(const char *path)
156 char *component, *copy, *tofree, *partial;
159 assert(path[0] == '/');
162 * +1 to skip the leading slash.
164 copy = tofree = checked_strdup(path + 1);
168 component = strsep(©, "/");
169 if (component == NULL)
171 concat(&partial, &component);
172 //log_debugx("creating \"%s\"", partial);
173 error = mkdir(partial, 0755);
174 if (error != 0 && errno != EEXIST) {
175 log_warn("cannot create %s", partial);
188 n = calloc(1, sizeof(*n));
190 log_err(1, "calloc");
192 n->n_key = checked_strdup("/");
193 n->n_options = checked_strdup("");
195 TAILQ_INIT(&n->n_children);
201 node_new(struct node *parent, char *key, char *options, char *location,
202 const char *config_file, int config_line)
206 n = calloc(1, sizeof(*n));
208 log_err(1, "calloc");
210 TAILQ_INIT(&n->n_children);
212 assert(key[0] != '\0');
215 n->n_options = options;
217 n->n_options = strdup("");
218 n->n_location = location;
219 assert(config_file != NULL);
220 n->n_config_file = config_file;
221 assert(config_line >= 0);
222 n->n_config_line = config_line;
224 assert(parent != NULL);
225 n->n_parent = parent;
226 TAILQ_INSERT_TAIL(&parent->n_children, n, n_next);
232 node_new_map(struct node *parent, char *key, char *options, char *map,
233 const char *config_file, int config_line)
237 n = calloc(1, sizeof(*n));
239 log_err(1, "calloc");
241 TAILQ_INIT(&n->n_children);
243 assert(key[0] != '\0');
246 n->n_options = options;
248 n->n_options = strdup("");
250 assert(config_file != NULL);
251 n->n_config_file = config_file;
252 assert(config_line >= 0);
253 n->n_config_line = config_line;
255 assert(parent != NULL);
256 n->n_parent = parent;
257 TAILQ_INSERT_TAIL(&parent->n_children, n, n_next);
263 node_duplicate(const struct node *o, struct node *parent)
265 const struct node *child;
269 parent = o->n_parent;
271 n = node_new(parent, o->n_key, o->n_options, o->n_location,
272 o->n_config_file, o->n_config_line);
274 TAILQ_FOREACH(child, &o->n_children, n_next)
275 node_duplicate(child, n);
281 node_delete(struct node *n)
283 struct node *child, *tmp;
287 TAILQ_FOREACH_SAFE(child, &n->n_children, n_next, tmp)
290 if (n->n_parent != NULL)
291 TAILQ_REMOVE(&n->n_parent->n_children, n, n_next);
297 * Move (reparent) node 'n' to make it sibling of 'previous', placed
301 node_move_after(struct node *n, struct node *previous)
304 TAILQ_REMOVE(&n->n_parent->n_children, n, n_next);
305 n->n_parent = previous->n_parent;
306 TAILQ_INSERT_AFTER(&previous->n_parent->n_children, previous, n, n_next);
310 node_expand_includes(struct node *root, bool is_master)
312 struct node *n, *n2, *tmp, *tmp2, *tmproot;
315 TAILQ_FOREACH_SAFE(n, &root->n_children, n_next, tmp) {
316 if (n->n_key[0] != '+')
319 error = access(AUTO_INCLUDE_PATH, F_OK);
321 log_errx(1, "directory services not configured; "
322 "%s does not exist", AUTO_INCLUDE_PATH);
326 * "+1" to skip leading "+".
328 yyin = auto_popen(AUTO_INCLUDE_PATH, n->n_key + 1, NULL);
329 assert(yyin != NULL);
331 tmproot = node_new_root();
333 parse_master_yyin(tmproot, n->n_key);
335 parse_map_yyin(tmproot, n->n_key, NULL);
337 error = auto_pclose(yyin);
340 log_errx(1, "failed to handle include \"%s\"",
345 * Entries to be included are now in tmproot. We need to merge
346 * them with the rest, preserving their place and ordering.
348 TAILQ_FOREACH_REVERSE_SAFE(n2,
349 &tmproot->n_children, nodehead, n_next, tmp2) {
350 node_move_after(n2, n);
354 node_delete(tmproot);
359 expand_ampersand(char *string, const char *key)
362 int i, ret, before_len = 0;
363 bool backslashed = false;
365 assert(key[0] != '\0');
367 expanded = checked_strdup(string);
369 for (i = 0; string[i] != '\0'; i++) {
371 if (c == '\\' && backslashed == false) {
384 * The 'before_len' variable contains the number
385 * of characters before the '&'.
388 //assert(i + 1 < (int)strlen(string));
390 ret = asprintf(&expanded, "%.*s%s%s",
391 before_len, string, key, string + before_len + 1);
393 log_err(1, "asprintf");
395 //log_debugx("\"%s\" expanded with key \"%s\" to \"%s\"",
396 // string, key, expanded);
399 * Figure out where to start searching for next variable.
402 i = before_len + strlen(key);
404 //assert(i < (int)strlen(string));
411 * Expand "&" in n_location. If the key is NULL, try to use
412 * key from map entries themselves. Keep in mind that maps
413 * consist of tho levels of node structures, the key is one
416 * Variant with NULL key is for "automount -LL".
419 node_expand_ampersand(struct node *n, const char *key)
423 if (n->n_location != NULL) {
425 if (n->n_parent != NULL &&
426 strcmp(n->n_parent->n_key, "*") != 0) {
427 n->n_location = expand_ampersand(n->n_location,
431 n->n_location = expand_ampersand(n->n_location, key);
435 TAILQ_FOREACH(child, &n->n_children, n_next)
436 node_expand_ampersand(child, key);
440 * Expand "*" in n_key.
443 node_expand_wildcard(struct node *n, const char *key)
445 struct node *child, *expanded;
449 if (strcmp(n->n_key, "*") == 0) {
450 expanded = node_duplicate(n, NULL);
451 expanded->n_key = checked_strdup(key);
452 node_move_after(expanded, n);
455 TAILQ_FOREACH(child, &n->n_children, n_next)
456 node_expand_wildcard(child, key);
460 node_expand_defined(struct node *n)
463 int error, cumulated_error = 0;
465 if (n->n_location != NULL) {
466 n->n_location = defined_expand(n->n_location);
467 if (n->n_location == NULL) {
468 log_warnx("failed to expand location for %s",
474 TAILQ_FOREACH(child, &n->n_children, n_next) {
475 error = node_expand_defined(child);
476 if (error != 0 && cumulated_error == 0)
477 cumulated_error = error;
480 return (cumulated_error);
484 node_is_direct_map(const struct node *n)
488 assert(n->n_parent != NULL);
489 if (n->n_parent->n_parent == NULL)
494 assert(n->n_key != NULL);
495 if (strcmp(n->n_key, "/-") != 0)
502 node_has_wildcards(const struct node *n)
504 const struct node *child;
506 TAILQ_FOREACH(child, &n->n_children, n_next) {
507 if (strcmp(child->n_key, "*") == 0)
515 node_expand_maps(struct node *n, bool indirect)
517 struct node *child, *tmp;
519 TAILQ_FOREACH_SAFE(child, &n->n_children, n_next, tmp) {
520 if (node_is_direct_map(child)) {
524 if (indirect == false)
529 * This is the first-level map node; the one that contains
530 * the key and subnodes with mountpoints and actual map names.
532 if (child->n_map == NULL)
536 log_debugx("map \"%s\" is an indirect map, parsing",
539 log_debugx("map \"%s\" is a direct map, parsing",
542 parse_map(child, child->n_map, NULL, NULL);
547 node_expand_direct_maps(struct node *n)
550 node_expand_maps(n, false);
554 node_expand_indirect_maps(struct node *n)
557 node_expand_maps(n, true);
561 node_path_x(const struct node *n, char *x)
566 if (n->n_parent == NULL)
570 * Return "/-" for direct maps only if we were asked for path
571 * to the "/-" node itself, not to any of its subnodes.
573 if (n->n_parent->n_parent == NULL &&
574 strcmp(n->n_key, "/-") == 0 &&
579 assert(n->n_key[0] != '\0');
580 path = separated_concat(n->n_key, x, '/');
584 * Strip trailing slash.
588 if (path[len - 1] == '/')
589 path[len - 1] = '\0';
591 return (node_path_x(n->n_parent, path));
595 * Return full path for node, consisting of concatenated
596 * paths of node itself and all its parents, up to the root.
599 node_path(const struct node *n)
602 return (node_path_x(n, checked_strdup("")));
606 node_options_x(const struct node *n, char *x)
610 options = separated_concat(x, n->n_options, ',');
611 if (n->n_parent == NULL)
614 return (node_options_x(n->n_parent, options));
618 * Return options for node, consisting of concatenated
619 * options from the node itself and all its parents,
623 node_options(const struct node *n)
626 return (node_options_x(n, checked_strdup("")));
630 node_print_indent(const struct node *n, int indent)
632 const struct node *child, *first_child;
633 char *path, *options;
636 options = node_options(n);
639 * Do not show both parent and child node if they have the same
640 * mountpoint; only show the child node. This means the typical,
641 * "key location", map entries are shown in a single line;
642 * the "key mountpoint1 location2 mountpoint2 location2" entries
643 * take multiple lines.
645 first_child = TAILQ_FIRST(&n->n_children);
646 if (first_child == NULL || TAILQ_NEXT(first_child, n_next) != NULL ||
647 strcmp(path, node_path(first_child)) != 0) {
648 assert(n->n_location == NULL || n->n_map == NULL);
649 printf("%*.s%-*s %s%-*s %-*s # %s map %s at %s:%d\n",
653 options[0] != '\0' ? "-" : " ",
655 options[0] != '\0' ? options : "",
657 n->n_location != NULL ? n->n_location : n->n_map != NULL ? n->n_map : "",
658 node_is_direct_map(n) ? "direct" : "indirect",
659 indent == 0 ? "referenced" : "defined",
660 n->n_config_file, n->n_config_line);
666 TAILQ_FOREACH(child, &n->n_children, n_next)
667 node_print_indent(child, indent + 2);
671 node_print(const struct node *n)
673 const struct node *child;
675 TAILQ_FOREACH(child, &n->n_children, n_next)
676 node_print_indent(child, 0);
680 node_find_x(struct node *node, const char *path)
682 struct node *child, *found;
686 //log_debugx("looking up %s in %s", path, node->n_key);
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.
704 TAILQ_FOREACH(child, &node->n_children, n_next) {
705 found = node_find_x(child, path);
714 node_find(struct node *root, const char *path)
718 node = node_find_x(root, path);
725 * Canonical form of a map entry looks like this:
727 * key [-options] [ [/mountpoint] [-options2] location ... ]
729 * Entries for executable maps are slightly different, as they
730 * lack the 'key' field and are always single-line; the key field
731 * for those maps is taken from 'executable_key' argument.
733 * We parse it in such a way that a map always has two levels - first
734 * for key, and the second, for the mountpoint.
737 parse_map_yyin(struct node *parent, const char *map, const char *executable_key)
739 char *key = NULL, *options = NULL, *mountpoint = NULL,
740 *options2 = NULL, *location = NULL;
746 if (executable_key != NULL)
747 key = checked_strdup(executable_key);
751 if (ret == 0 || ret == NEWLINE) {
753 * In case of executable map, the key is always
754 * non-NULL, even if the map is empty. So, make sure
755 * we don't fail empty maps here.
757 if ((key != NULL && executable_key == NULL) ||
759 log_errx(1, "truncated entry at %s, line %d",
762 if (ret == 0 || executable_key != NULL) {
768 key = options = NULL;
773 key = checked_strdup(yytext);
775 node_new(parent, key, NULL, NULL, map, lineno);
776 key = options = NULL;
780 } else if (yytext[0] == '-') {
781 if (options != NULL) {
782 log_errx(1, "duplicated options at %s, line %d",
786 * +1 to skip leading "-".
788 options = checked_strdup(yytext + 1);
793 * We cannot properly handle a situation where the map key
794 * is "/". Ignore such entries.
796 * XXX: According to Piete Brooks, Linux automounter uses
797 * "/" as a wildcard character in LDAP maps. Perhaps
798 * we should work around this braindamage by substituting
801 if (strcmp(key, "/") == 0) {
802 log_warnx("nonsensical map key \"/\" at %s, line %d; "
803 "ignoring map entry ", map, lineno);
806 * Skip the rest of the entry.
810 } while (ret != 0 && ret != NEWLINE);
812 key = options = NULL;
816 //log_debugx("adding map node, %s", key);
817 node = node_new(parent, key, options, NULL, map, lineno);
818 key = options = NULL;
821 if (yytext[0] == '/') {
822 if (mountpoint != NULL) {
823 log_errx(1, "duplicated mountpoint "
824 "in %s, line %d", map, lineno);
826 if (options2 != NULL || location != NULL) {
827 log_errx(1, "mountpoint out of order "
828 "in %s, line %d", map, lineno);
830 mountpoint = checked_strdup(yytext);
834 if (yytext[0] == '-') {
835 if (options2 != NULL) {
836 log_errx(1, "duplicated options "
837 "in %s, line %d", map, lineno);
839 if (location != NULL) {
840 log_errx(1, "options out of order "
841 "in %s, line %d", map, lineno);
843 options2 = checked_strdup(yytext + 1);
847 if (location != NULL) {
848 log_errx(1, "too many arguments "
849 "in %s, line %d", map, lineno);
853 * If location field starts with colon, e.g. ":/dev/cd0",
856 if (yytext[0] == ':') {
857 location = checked_strdup(yytext + 1);
858 if (location[0] == '\0') {
859 log_errx(1, "empty location in %s, "
860 "line %d", map, lineno);
863 location = checked_strdup(yytext);
866 if (mountpoint == NULL)
867 mountpoint = checked_strdup("/");
868 if (options2 == NULL)
869 options2 = checked_strdup("");
872 log_debugx("adding map node, %s %s %s",
873 mountpoint, options2, location);
875 node_new(node, mountpoint, options2, location,
877 mountpoint = options2 = location = NULL;
880 if (ret == 0 || ret == NEWLINE) {
881 if (mountpoint != NULL || options2 != NULL ||
883 log_errx(1, "truncated entry "
884 "in %s, line %d", map, lineno);
893 * Parse output of a special map called without argument. It is a list
894 * of keys, separated by newlines. They can contain whitespace, so use
895 * getline(3) instead of lexer used for maps.
898 parse_map_keys_yyin(struct node *parent, const char *map)
900 char *line = NULL, *key;
907 linelen = getline(&line, &linecap, yyin);
916 * Empty line, consisting of just the newline.
922 * "-1" to strip the trailing newline.
924 key = strndup(line, linelen - 1);
926 log_debugx("adding key \"%s\"", key);
927 node_new(parent, key, NULL, NULL, map, lineno);
934 file_is_executable(const char *path)
939 error = stat(path, &sb);
941 log_err(1, "cannot stat %s", path);
942 if ((sb.st_mode & S_IXUSR) || (sb.st_mode & S_IXGRP) ||
943 (sb.st_mode & S_IXOTH))
949 * Parse a special map, e.g. "-hosts".
952 parse_special_map(struct node *parent, const char *map, const char *key)
957 assert(map[0] == '-');
960 * +1 to skip leading "-" in map name.
962 ret = asprintf(&path, "%s/special_%s", AUTO_SPECIAL_PREFIX, map + 1);
964 log_err(1, "asprintf");
966 yyin = auto_popen(path, key, NULL);
967 assert(yyin != NULL);
970 parse_map_keys_yyin(parent, map);
972 parse_map_yyin(parent, map, key);
975 error = auto_pclose(yyin);
978 log_errx(1, "failed to handle special map \"%s\"", map);
980 node_expand_includes(parent, false);
981 node_expand_direct_maps(parent);
987 * Retrieve and parse map from directory services, e.g. LDAP.
988 * Note that it is different from executable maps, in that
989 * the include script outputs the whole map to standard output
990 * (as opposed to executable maps that only output a single
991 * entry, without the key), and it takes the map name as an
992 * argument, instead of key.
995 parse_included_map(struct node *parent, const char *map)
999 assert(map[0] != '-');
1000 assert(map[0] != '/');
1002 error = access(AUTO_INCLUDE_PATH, F_OK);
1004 log_errx(1, "directory services not configured;"
1005 " %s does not exist", AUTO_INCLUDE_PATH);
1008 yyin = auto_popen(AUTO_INCLUDE_PATH, map, NULL);
1009 assert(yyin != NULL);
1011 parse_map_yyin(parent, map, NULL);
1013 error = auto_pclose(yyin);
1016 log_errx(1, "failed to handle remote map \"%s\"", map);
1018 node_expand_includes(parent, false);
1019 node_expand_direct_maps(parent);
1023 parse_map(struct node *parent, const char *map, const char *key,
1030 assert(map != NULL);
1031 assert(map[0] != '\0');
1033 log_debugx("parsing map \"%s\"", map);
1035 if (wildcards != NULL)
1038 if (map[0] == '-') {
1039 if (wildcards != NULL)
1041 return (parse_special_map(parent, map, key));
1044 if (map[0] == '/') {
1045 path = checked_strdup(map);
1047 ret = asprintf(&path, "%s/%s", AUTO_MAP_PREFIX, map);
1049 log_err(1, "asprintf");
1050 log_debugx("map \"%s\" maps to \"%s\"", map, path);
1053 * See if the file exists. If not, try to obtain the map
1054 * from directory services.
1056 error = access(path, F_OK);
1058 log_debugx("map file \"%s\" does not exist; falling "
1059 "back to directory services", path);
1060 return (parse_included_map(parent, map));
1064 executable = file_is_executable(path);
1067 log_debugx("map \"%s\" is executable", map);
1069 if (wildcards != NULL)
1073 yyin = auto_popen(path, key, NULL);
1075 yyin = auto_popen(path, NULL);
1077 assert(yyin != NULL);
1079 yyin = fopen(path, "r");
1081 log_err(1, "unable to open \"%s\"", path);
1087 parse_map_yyin(parent, map, executable ? key : NULL);
1090 error = auto_pclose(yyin);
1093 log_errx(1, "failed to handle executable map \"%s\"",
1101 log_debugx("done parsing map \"%s\"", map);
1103 node_expand_includes(parent, false);
1104 node_expand_direct_maps(parent);
1108 parse_master_yyin(struct node *root, const char *master)
1110 char *mountpoint = NULL, *map = NULL, *options = NULL;
1114 * XXX: 1 gives incorrect values; wtf?
1120 if (ret == 0 || ret == NEWLINE) {
1121 if (mountpoint != NULL) {
1122 //log_debugx("adding map for %s", mountpoint);
1123 node_new_map(root, mountpoint, options, map,
1129 mountpoint = map = options = NULL;
1133 if (mountpoint == NULL) {
1134 mountpoint = checked_strdup(yytext);
1135 } else if (map == NULL) {
1136 map = checked_strdup(yytext);
1137 } else if (options == NULL) {
1139 * +1 to skip leading "-".
1141 options = checked_strdup(yytext + 1);
1143 log_errx(1, "too many arguments at %s, line %d",
1150 parse_master(struct node *root, const char *master)
1153 log_debugx("parsing auto_master file at \"%s\"", master);
1155 yyin = fopen(master, "r");
1157 err(1, "unable to open %s", master);
1159 parse_master_yyin(root, master);
1164 log_debugx("done parsing \"%s\"", master);
1166 node_expand_includes(root, true);
1167 node_expand_direct_maps(root);
1171 * Two things daemon(3) does, that we actually also want to do
1172 * when running in foreground, is closing the stdin and chdiring
1173 * to "/". This is what we do here.
1184 fd = open(_PATH_DEVNULL, O_RDWR, 0);
1186 log_warn("cannot open %s", _PATH_DEVNULL);
1190 error = dup2(fd, STDIN_FILENO);
1202 main(int argc, char **argv)
1206 if (argv[0] == NULL)
1207 log_errx(1, "NULL command name");
1209 cmdname = basename(argv[0]);
1211 if (strcmp(cmdname, "automount") == 0)
1212 return (main_automount(argc, argv));
1213 else if (strcmp(cmdname, "automountd") == 0)
1214 return (main_automountd(argc, argv));
1215 else if (strcmp(cmdname, "autounmountd") == 0)
1216 return (main_autounmountd(argc, argv));
1218 log_errx(1, "binary name should be either \"automount\", "
1219 "\"automountd\", or \"autounmountd\"");