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)
140 * If s2 starts with separator - skip it; otherwise concatenating
141 * "/" and "/foo" would end up returning "//foo".
143 if (s2[0] == separator)
146 if (s1[0] == '\0' || s2[0] == '\0' || s1[strlen(s1) - 1] == separator) {
147 ret = asprintf(&result, "%s%s", s1, s2);
149 ret = asprintf(&result, "%s%c%s", s1, separator, s2);
152 log_err(1, "asprintf");
154 //log_debugx("separated_concat: got %s and %s, returning %s", s1, s2, result);
160 create_directory(const char *path)
162 char *component, *copy, *tofree, *partial;
165 assert(path[0] == '/');
168 * +1 to skip the leading slash.
170 copy = tofree = checked_strdup(path + 1);
174 component = strsep(©, "/");
175 if (component == NULL)
177 concat(&partial, &component);
178 //log_debugx("creating \"%s\"", partial);
179 error = mkdir(partial, 0755);
180 if (error != 0 && errno != EEXIST) {
181 log_warn("cannot create %s", partial);
194 n = calloc(1, sizeof(*n));
196 log_err(1, "calloc");
198 n->n_key = checked_strdup("/");
199 n->n_options = checked_strdup("");
201 TAILQ_INIT(&n->n_children);
207 node_new(struct node *parent, char *key, char *options, char *location,
208 const char *config_file, int config_line)
212 n = calloc(1, sizeof(*n));
214 log_err(1, "calloc");
216 TAILQ_INIT(&n->n_children);
218 assert(key[0] != '\0');
221 n->n_options = options;
223 n->n_options = strdup("");
224 n->n_location = location;
225 assert(config_file != NULL);
226 n->n_config_file = config_file;
227 assert(config_line >= 0);
228 n->n_config_line = config_line;
230 assert(parent != NULL);
231 n->n_parent = parent;
232 TAILQ_INSERT_TAIL(&parent->n_children, n, n_next);
238 node_new_map(struct node *parent, char *key, char *options, char *map,
239 const char *config_file, int config_line)
243 n = calloc(1, sizeof(*n));
245 log_err(1, "calloc");
247 TAILQ_INIT(&n->n_children);
249 assert(key[0] != '\0');
252 n->n_options = options;
254 n->n_options = strdup("");
256 assert(config_file != NULL);
257 n->n_config_file = config_file;
258 assert(config_line >= 0);
259 n->n_config_line = config_line;
261 assert(parent != NULL);
262 n->n_parent = parent;
263 TAILQ_INSERT_TAIL(&parent->n_children, n, n_next);
269 node_duplicate(const struct node *o, struct node *parent)
271 const struct node *child;
275 parent = o->n_parent;
277 n = node_new(parent, o->n_key, o->n_options, o->n_location,
278 o->n_config_file, o->n_config_line);
280 TAILQ_FOREACH(child, &o->n_children, n_next)
281 node_duplicate(child, n);
287 node_delete(struct node *n)
289 struct node *child, *tmp;
293 TAILQ_FOREACH_SAFE(child, &n->n_children, n_next, tmp)
296 if (n->n_parent != NULL)
297 TAILQ_REMOVE(&n->n_parent->n_children, n, n_next);
303 * Move (reparent) node 'n' to make it sibling of 'previous', placed
307 node_move_after(struct node *n, struct node *previous)
310 TAILQ_REMOVE(&n->n_parent->n_children, n, n_next);
311 n->n_parent = previous->n_parent;
312 TAILQ_INSERT_AFTER(&previous->n_parent->n_children, previous, n, n_next);
316 node_expand_includes(struct node *root, bool is_master)
318 struct node *n, *n2, *tmp, *tmp2, *tmproot;
321 TAILQ_FOREACH_SAFE(n, &root->n_children, n_next, tmp) {
322 if (n->n_key[0] != '+')
325 error = access(AUTO_INCLUDE_PATH, F_OK);
327 log_errx(1, "directory services not configured; "
328 "%s does not exist", AUTO_INCLUDE_PATH);
332 * "+1" to skip leading "+".
334 yyin = auto_popen(AUTO_INCLUDE_PATH, n->n_key + 1, NULL);
335 assert(yyin != NULL);
337 tmproot = node_new_root();
339 parse_master_yyin(tmproot, n->n_key);
341 parse_map_yyin(tmproot, n->n_key, NULL);
343 error = auto_pclose(yyin);
346 log_errx(1, "failed to handle include \"%s\"",
351 * Entries to be included are now in tmproot. We need to merge
352 * them with the rest, preserving their place and ordering.
354 TAILQ_FOREACH_REVERSE_SAFE(n2,
355 &tmproot->n_children, nodehead, n_next, tmp2) {
356 node_move_after(n2, n);
360 node_delete(tmproot);
365 expand_ampersand(char *string, const char *key)
368 int i, ret, before_len = 0;
369 bool backslashed = false;
371 assert(key[0] != '\0');
373 expanded = checked_strdup(string);
375 for (i = 0; string[i] != '\0'; i++) {
377 if (c == '\\' && backslashed == false) {
390 * The 'before_len' variable contains the number
391 * of characters before the '&'.
394 //assert(i + 1 < (int)strlen(string));
396 ret = asprintf(&expanded, "%.*s%s%s",
397 before_len, string, key, string + before_len + 1);
399 log_err(1, "asprintf");
401 //log_debugx("\"%s\" expanded with key \"%s\" to \"%s\"",
402 // string, key, expanded);
405 * Figure out where to start searching for next variable.
408 i = before_len + strlen(key);
410 //assert(i < (int)strlen(string));
417 * Expand "&" in n_location. If the key is NULL, try to use
418 * key from map entries themselves. Keep in mind that maps
419 * consist of tho levels of node structures, the key is one
422 * Variant with NULL key is for "automount -LL".
425 node_expand_ampersand(struct node *n, const char *key)
429 if (n->n_location != NULL) {
431 if (n->n_parent != NULL &&
432 strcmp(n->n_parent->n_key, "*") != 0) {
433 n->n_location = expand_ampersand(n->n_location,
437 n->n_location = expand_ampersand(n->n_location, key);
441 TAILQ_FOREACH(child, &n->n_children, n_next)
442 node_expand_ampersand(child, key);
446 * Expand "*" in n_key.
449 node_expand_wildcard(struct node *n, const char *key)
451 struct node *child, *expanded;
455 if (strcmp(n->n_key, "*") == 0) {
456 expanded = node_duplicate(n, NULL);
457 expanded->n_key = checked_strdup(key);
458 node_move_after(expanded, n);
461 TAILQ_FOREACH(child, &n->n_children, n_next)
462 node_expand_wildcard(child, key);
466 node_expand_defined(struct node *n)
469 int error, cumulated_error = 0;
471 if (n->n_location != NULL) {
472 n->n_location = defined_expand(n->n_location);
473 if (n->n_location == NULL) {
474 log_warnx("failed to expand location for %s",
480 TAILQ_FOREACH(child, &n->n_children, n_next) {
481 error = node_expand_defined(child);
482 if (error != 0 && cumulated_error == 0)
483 cumulated_error = error;
486 return (cumulated_error);
490 node_is_direct_map(const struct node *n)
494 assert(n->n_parent != NULL);
495 if (n->n_parent->n_parent == NULL)
500 assert(n->n_key != NULL);
501 if (strcmp(n->n_key, "/-") != 0)
508 node_has_wildcards(const struct node *n)
510 const struct node *child;
512 TAILQ_FOREACH(child, &n->n_children, n_next) {
513 if (strcmp(child->n_key, "*") == 0)
521 node_expand_maps(struct node *n, bool indirect)
523 struct node *child, *tmp;
525 TAILQ_FOREACH_SAFE(child, &n->n_children, n_next, tmp) {
526 if (node_is_direct_map(child)) {
530 if (indirect == false)
535 * This is the first-level map node; the one that contains
536 * the key and subnodes with mountpoints and actual map names.
538 if (child->n_map == NULL)
542 log_debugx("map \"%s\" is an indirect map, parsing",
545 log_debugx("map \"%s\" is a direct map, parsing",
548 parse_map(child, child->n_map, NULL, NULL);
553 node_expand_direct_maps(struct node *n)
556 node_expand_maps(n, false);
560 node_expand_indirect_maps(struct node *n)
563 node_expand_maps(n, true);
567 node_path_x(const struct node *n, char *x)
571 if (n->n_parent == NULL)
575 * Return "/-" for direct maps only if we were asked for path
576 * to the "/-" node itself, not to any of its subnodes.
578 if (n->n_parent->n_parent == NULL &&
579 strcmp(n->n_key, "/-") == 0 &&
584 assert(n->n_key[0] != '\0');
585 path = separated_concat(n->n_key, x, '/');
588 return (node_path_x(n->n_parent, path));
592 * Return full path for node, consisting of concatenated
593 * paths of node itself and all its parents, up to the root.
596 node_path(const struct node *n)
601 path = node_path_x(n, checked_strdup(""));
604 * Strip trailing slash, unless the whole path is "/".
607 if (len > 1 && path[len - 1] == '/')
608 path[len - 1] = '\0';
614 node_options_x(const struct node *n, char *x)
621 options = separated_concat(x, n->n_options, ',');
624 return (node_options_x(n->n_parent, options));
628 * Return options for node, consisting of concatenated
629 * options from the node itself and all its parents,
633 node_options(const struct node *n)
636 return (node_options_x(n, checked_strdup("")));
640 node_print_indent(const struct node *n, int indent)
642 const struct node *child, *first_child;
643 char *path, *options;
646 options = node_options(n);
649 * Do not show both parent and child node if they have the same
650 * mountpoint; only show the child node. This means the typical,
651 * "key location", map entries are shown in a single line;
652 * the "key mountpoint1 location2 mountpoint2 location2" entries
653 * take multiple lines.
655 first_child = TAILQ_FIRST(&n->n_children);
656 if (first_child == NULL || TAILQ_NEXT(first_child, n_next) != NULL ||
657 strcmp(path, node_path(first_child)) != 0) {
658 assert(n->n_location == NULL || n->n_map == NULL);
659 printf("%*.s%-*s %s%-*s %-*s # %s map %s at %s:%d\n",
663 options[0] != '\0' ? "-" : " ",
665 options[0] != '\0' ? options : "",
667 n->n_location != NULL ? n->n_location : n->n_map != NULL ? n->n_map : "",
668 node_is_direct_map(n) ? "direct" : "indirect",
669 indent == 0 ? "referenced" : "defined",
670 n->n_config_file, n->n_config_line);
676 TAILQ_FOREACH(child, &n->n_children, n_next)
677 node_print_indent(child, indent + 2);
681 node_print(const struct node *n)
683 const struct node *child;
685 TAILQ_FOREACH(child, &n->n_children, n_next)
686 node_print_indent(child, 0);
690 node_find_x(struct node *node, const char *path)
692 struct node *child, *found;
696 //log_debugx("looking up %s in %s", path, node->n_key);
698 tmp = node_path(node);
699 tmplen = strlen(tmp);
700 if (strncmp(tmp, path, tmplen) != 0) {
704 if (path[tmplen] != '/' && path[tmplen] != '\0') {
706 * If we have two map entries like 'foo' and 'foobar', make
707 * sure the search for 'foobar' won't match 'foo' instead.
714 TAILQ_FOREACH(child, &node->n_children, n_next) {
715 found = node_find_x(child, path);
724 node_find(struct node *root, const char *path)
728 node = node_find_x(root, path);
735 * Canonical form of a map entry looks like this:
737 * key [-options] [ [/mountpoint] [-options2] location ... ]
739 * Entries for executable maps are slightly different, as they
740 * lack the 'key' field and are always single-line; the key field
741 * for those maps is taken from 'executable_key' argument.
743 * We parse it in such a way that a map always has two levels - first
744 * for key, and the second, for the mountpoint.
747 parse_map_yyin(struct node *parent, const char *map, const char *executable_key)
749 char *key = NULL, *options = NULL, *mountpoint = NULL,
750 *options2 = NULL, *location = NULL;
756 if (executable_key != NULL)
757 key = checked_strdup(executable_key);
761 if (ret == 0 || ret == NEWLINE) {
763 * In case of executable map, the key is always
764 * non-NULL, even if the map is empty. So, make sure
765 * we don't fail empty maps here.
767 if ((key != NULL && executable_key == NULL) ||
769 log_errx(1, "truncated entry at %s, line %d",
772 if (ret == 0 || executable_key != NULL) {
778 key = options = NULL;
783 key = checked_strdup(yytext);
785 node_new(parent, key, NULL, NULL, map, lineno);
786 key = options = NULL;
790 } else if (yytext[0] == '-') {
791 if (options != NULL) {
792 log_errx(1, "duplicated options at %s, line %d",
796 * +1 to skip leading "-".
798 options = checked_strdup(yytext + 1);
803 * We cannot properly handle a situation where the map key
804 * is "/". Ignore such entries.
806 * XXX: According to Piete Brooks, Linux automounter uses
807 * "/" as a wildcard character in LDAP maps. Perhaps
808 * we should work around this braindamage by substituting
811 if (strcmp(key, "/") == 0) {
812 log_warnx("nonsensical map key \"/\" at %s, line %d; "
813 "ignoring map entry ", map, lineno);
816 * Skip the rest of the entry.
820 } while (ret != 0 && ret != NEWLINE);
822 key = options = NULL;
826 //log_debugx("adding map node, %s", key);
827 node = node_new(parent, key, options, NULL, map, lineno);
828 key = options = NULL;
831 if (yytext[0] == '/') {
832 if (mountpoint != NULL) {
833 log_errx(1, "duplicated mountpoint "
834 "in %s, line %d", map, lineno);
836 if (options2 != NULL || location != NULL) {
837 log_errx(1, "mountpoint out of order "
838 "in %s, line %d", map, lineno);
840 mountpoint = checked_strdup(yytext);
844 if (yytext[0] == '-') {
845 if (options2 != NULL) {
846 log_errx(1, "duplicated options "
847 "in %s, line %d", map, lineno);
849 if (location != NULL) {
850 log_errx(1, "options out of order "
851 "in %s, line %d", map, lineno);
853 options2 = checked_strdup(yytext + 1);
857 if (location != NULL) {
858 log_errx(1, "too many arguments "
859 "in %s, line %d", map, lineno);
863 * If location field starts with colon, e.g. ":/dev/cd0",
866 if (yytext[0] == ':') {
867 location = checked_strdup(yytext + 1);
868 if (location[0] == '\0') {
869 log_errx(1, "empty location in %s, "
870 "line %d", map, lineno);
873 location = checked_strdup(yytext);
876 if (mountpoint == NULL)
877 mountpoint = checked_strdup("/");
878 if (options2 == NULL)
879 options2 = checked_strdup("");
882 log_debugx("adding map node, %s %s %s",
883 mountpoint, options2, location);
885 node_new(node, mountpoint, options2, location,
887 mountpoint = options2 = location = NULL;
890 if (ret == 0 || ret == NEWLINE) {
891 if (mountpoint != NULL || options2 != NULL ||
893 log_errx(1, "truncated entry "
894 "in %s, line %d", map, lineno);
903 * Parse output of a special map called without argument. It is a list
904 * of keys, separated by newlines. They can contain whitespace, so use
905 * getline(3) instead of lexer used for maps.
908 parse_map_keys_yyin(struct node *parent, const char *map)
910 char *line = NULL, *key;
917 linelen = getline(&line, &linecap, yyin);
926 * Empty line, consisting of just the newline.
932 * "-1" to strip the trailing newline.
934 key = strndup(line, linelen - 1);
936 log_debugx("adding key \"%s\"", key);
937 node_new(parent, key, NULL, NULL, map, lineno);
944 file_is_executable(const char *path)
949 error = stat(path, &sb);
951 log_err(1, "cannot stat %s", path);
952 if ((sb.st_mode & S_IXUSR) || (sb.st_mode & S_IXGRP) ||
953 (sb.st_mode & S_IXOTH))
959 * Parse a special map, e.g. "-hosts".
962 parse_special_map(struct node *parent, const char *map, const char *key)
967 assert(map[0] == '-');
970 * +1 to skip leading "-" in map name.
972 ret = asprintf(&path, "%s/special_%s", AUTO_SPECIAL_PREFIX, map + 1);
974 log_err(1, "asprintf");
976 yyin = auto_popen(path, key, NULL);
977 assert(yyin != NULL);
980 parse_map_keys_yyin(parent, map);
982 parse_map_yyin(parent, map, key);
985 error = auto_pclose(yyin);
988 log_errx(1, "failed to handle special map \"%s\"", map);
990 node_expand_includes(parent, false);
991 node_expand_direct_maps(parent);
997 * Retrieve and parse map from directory services, e.g. LDAP.
998 * Note that it is different from executable maps, in that
999 * the include script outputs the whole map to standard output
1000 * (as opposed to executable maps that only output a single
1001 * entry, without the key), and it takes the map name as an
1002 * argument, instead of key.
1005 parse_included_map(struct node *parent, const char *map)
1009 assert(map[0] != '-');
1010 assert(map[0] != '/');
1012 error = access(AUTO_INCLUDE_PATH, F_OK);
1014 log_errx(1, "directory services not configured;"
1015 " %s does not exist", AUTO_INCLUDE_PATH);
1018 yyin = auto_popen(AUTO_INCLUDE_PATH, map, NULL);
1019 assert(yyin != NULL);
1021 parse_map_yyin(parent, map, NULL);
1023 error = auto_pclose(yyin);
1026 log_errx(1, "failed to handle remote map \"%s\"", map);
1028 node_expand_includes(parent, false);
1029 node_expand_direct_maps(parent);
1033 parse_map(struct node *parent, const char *map, const char *key,
1040 assert(map != NULL);
1041 assert(map[0] != '\0');
1043 log_debugx("parsing map \"%s\"", map);
1045 if (wildcards != NULL)
1048 if (map[0] == '-') {
1049 if (wildcards != NULL)
1051 return (parse_special_map(parent, map, key));
1054 if (map[0] == '/') {
1055 path = checked_strdup(map);
1057 ret = asprintf(&path, "%s/%s", AUTO_MAP_PREFIX, map);
1059 log_err(1, "asprintf");
1060 log_debugx("map \"%s\" maps to \"%s\"", map, path);
1063 * See if the file exists. If not, try to obtain the map
1064 * from directory services.
1066 error = access(path, F_OK);
1068 log_debugx("map file \"%s\" does not exist; falling "
1069 "back to directory services", path);
1070 return (parse_included_map(parent, map));
1074 executable = file_is_executable(path);
1077 log_debugx("map \"%s\" is executable", map);
1079 if (wildcards != NULL)
1083 yyin = auto_popen(path, key, NULL);
1085 yyin = auto_popen(path, NULL);
1087 assert(yyin != NULL);
1089 yyin = fopen(path, "r");
1091 log_err(1, "unable to open \"%s\"", path);
1097 parse_map_yyin(parent, map, executable ? key : NULL);
1100 error = auto_pclose(yyin);
1103 log_errx(1, "failed to handle executable map \"%s\"",
1111 log_debugx("done parsing map \"%s\"", map);
1113 node_expand_includes(parent, false);
1114 node_expand_direct_maps(parent);
1118 parse_master_yyin(struct node *root, const char *master)
1120 char *mountpoint = NULL, *map = NULL, *options = NULL;
1124 * XXX: 1 gives incorrect values; wtf?
1130 if (ret == 0 || ret == NEWLINE) {
1131 if (mountpoint != NULL) {
1132 //log_debugx("adding map for %s", mountpoint);
1133 node_new_map(root, mountpoint, options, map,
1139 mountpoint = map = options = NULL;
1143 if (mountpoint == NULL) {
1144 mountpoint = checked_strdup(yytext);
1145 } else if (map == NULL) {
1146 map = checked_strdup(yytext);
1147 } else if (options == NULL) {
1149 * +1 to skip leading "-".
1151 options = checked_strdup(yytext + 1);
1153 log_errx(1, "too many arguments at %s, line %d",
1160 parse_master(struct node *root, const char *master)
1163 log_debugx("parsing auto_master file at \"%s\"", master);
1165 yyin = fopen(master, "r");
1167 err(1, "unable to open %s", master);
1169 parse_master_yyin(root, master);
1174 log_debugx("done parsing \"%s\"", master);
1176 node_expand_includes(root, true);
1177 node_expand_direct_maps(root);
1181 * Two things daemon(3) does, that we actually also want to do
1182 * when running in foreground, is closing the stdin and chdiring
1183 * to "/". This is what we do here.
1194 fd = open(_PATH_DEVNULL, O_RDWR, 0);
1196 log_warn("cannot open %s", _PATH_DEVNULL);
1200 error = dup2(fd, STDIN_FILENO);
1212 main(int argc, char **argv)
1216 if (argv[0] == NULL)
1217 log_errx(1, "NULL command name");
1219 cmdname = basename(argv[0]);
1221 if (strcmp(cmdname, "automount") == 0)
1222 return (main_automount(argc, argv));
1223 else if (strcmp(cmdname, "automountd") == 0)
1224 return (main_automountd(argc, argv));
1225 else if (strcmp(cmdname, "autounmountd") == 0)
1226 return (main_autounmountd(argc, argv));
1228 log_errx(1, "binary name should be either \"automount\", "
1229 "\"automountd\", or \"autounmountd\"");