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("checking \"%s\" for existence", partial);
173 error = access(partial, F_OK);
177 log_err(1, "cannot access %s", partial);
178 log_debugx("directory %s does not exist, creating",
180 error = mkdir(partial, 0755);
182 log_err(1, "cannot create %s", partial);
193 n = calloc(1, sizeof(*n));
195 log_err(1, "calloc");
197 n->n_key = checked_strdup("/");
198 n->n_options = checked_strdup("");
200 TAILQ_INIT(&n->n_children);
206 node_new(struct node *parent, char *key, char *options, char *location,
207 const char *config_file, int config_line)
211 n = calloc(1, sizeof(*n));
213 log_err(1, "calloc");
215 TAILQ_INIT(&n->n_children);
217 assert(key[0] != '\0');
220 n->n_options = options;
222 n->n_options = strdup("");
223 n->n_location = location;
224 assert(config_file != NULL);
225 n->n_config_file = config_file;
226 assert(config_line >= 0);
227 n->n_config_line = config_line;
229 assert(parent != NULL);
230 n->n_parent = parent;
231 TAILQ_INSERT_TAIL(&parent->n_children, n, n_next);
237 node_new_map(struct node *parent, char *key, char *options, char *map,
238 const char *config_file, int config_line)
242 n = calloc(1, sizeof(*n));
244 log_err(1, "calloc");
246 TAILQ_INIT(&n->n_children);
248 assert(key[0] != '\0');
251 n->n_options = options;
253 n->n_options = strdup("");
255 assert(config_file != NULL);
256 n->n_config_file = config_file;
257 assert(config_line >= 0);
258 n->n_config_line = config_line;
260 assert(parent != NULL);
261 n->n_parent = parent;
262 TAILQ_INSERT_TAIL(&parent->n_children, n, n_next);
268 node_duplicate(const struct node *o, struct node *parent)
270 const struct node *child;
274 parent = o->n_parent;
276 n = node_new(parent, o->n_key, o->n_options, o->n_location,
277 o->n_config_file, o->n_config_line);
279 TAILQ_FOREACH(child, &o->n_children, n_next)
280 node_duplicate(child, n);
286 node_delete(struct node *n)
288 struct node *child, *tmp;
292 TAILQ_FOREACH_SAFE(child, &n->n_children, n_next, tmp)
295 if (n->n_parent != NULL)
296 TAILQ_REMOVE(&n->n_parent->n_children, n, n_next);
302 * Move (reparent) node 'n' to make it sibling of 'previous', placed
306 node_move_after(struct node *n, struct node *previous)
309 TAILQ_REMOVE(&n->n_parent->n_children, n, n_next);
310 n->n_parent = previous->n_parent;
311 TAILQ_INSERT_AFTER(&previous->n_parent->n_children, previous, n, n_next);
315 node_expand_includes(struct node *root, bool is_master)
317 struct node *n, *n2, *tmp, *tmp2, *tmproot;
320 TAILQ_FOREACH_SAFE(n, &root->n_children, n_next, tmp) {
321 if (n->n_key[0] != '+')
324 error = access(AUTO_INCLUDE_PATH, F_OK);
326 log_errx(1, "directory services not configured; "
327 "%s does not exist", AUTO_INCLUDE_PATH);
331 * "+1" to skip leading "+".
333 yyin = auto_popen(AUTO_INCLUDE_PATH, n->n_key + 1, NULL);
334 assert(yyin != NULL);
336 tmproot = node_new_root();
338 parse_master_yyin(tmproot, n->n_key);
340 parse_map_yyin(tmproot, n->n_key, NULL);
342 error = auto_pclose(yyin);
345 log_errx(1, "failed to handle include \"%s\"",
350 * Entries to be included are now in tmproot. We need to merge
351 * them with the rest, preserving their place and ordering.
353 TAILQ_FOREACH_REVERSE_SAFE(n2,
354 &tmproot->n_children, nodehead, n_next, tmp2) {
355 node_move_after(n2, n);
359 node_delete(tmproot);
364 expand_ampersand(char *string, const char *key)
367 int i, ret, before_len = 0;
368 bool backslashed = false;
370 assert(key[0] != '\0');
372 expanded = checked_strdup(string);
374 for (i = 0; string[i] != '\0'; i++) {
376 if (c == '\\' && backslashed == false) {
389 * The 'before_len' variable contains the number
390 * of characters before the '&'.
393 //assert(i + 1 < (int)strlen(string));
395 ret = asprintf(&expanded, "%.*s%s%s",
396 before_len, string, key, string + before_len + 1);
398 log_err(1, "asprintf");
400 //log_debugx("\"%s\" expanded with key \"%s\" to \"%s\"",
401 // string, key, expanded);
404 * Figure out where to start searching for next variable.
407 i = before_len + strlen(key);
409 //assert(i < (int)strlen(string));
416 * Expand "&" in n_location. If the key is NULL, try to use
417 * key from map entries themselves. Keep in mind that maps
418 * consist of tho levels of node structures, the key is one
421 * Variant with NULL key is for "automount -LL".
424 node_expand_ampersand(struct node *n, const char *key)
428 if (n->n_location != NULL) {
430 if (n->n_parent != NULL &&
431 strcmp(n->n_parent->n_key, "*") != 0) {
432 n->n_location = expand_ampersand(n->n_location,
436 n->n_location = expand_ampersand(n->n_location, key);
440 TAILQ_FOREACH(child, &n->n_children, n_next)
441 node_expand_ampersand(child, key);
445 * Expand "*" in n_key.
448 node_expand_wildcard(struct node *n, const char *key)
450 struct node *child, *expanded;
454 if (strcmp(n->n_key, "*") == 0) {
455 expanded = node_duplicate(n, NULL);
456 expanded->n_key = checked_strdup(key);
457 node_move_after(expanded, n);
460 TAILQ_FOREACH(child, &n->n_children, n_next)
461 node_expand_wildcard(child, key);
465 node_expand_defined(struct node *n)
468 int error, cumulated_error = 0;
470 if (n->n_location != NULL) {
471 n->n_location = defined_expand(n->n_location);
472 if (n->n_location == NULL) {
473 log_warnx("failed to expand location for %s",
479 TAILQ_FOREACH(child, &n->n_children, n_next) {
480 error = node_expand_defined(child);
481 if (error != 0 && cumulated_error == 0)
482 cumulated_error = error;
485 return (cumulated_error);
489 node_is_direct_map(const struct node *n)
493 assert(n->n_parent != NULL);
494 if (n->n_parent->n_parent == NULL)
499 assert(n->n_key != NULL);
500 if (strcmp(n->n_key, "/-") != 0)
507 node_expand_maps(struct node *n, bool indirect)
509 struct node *child, *tmp;
511 TAILQ_FOREACH_SAFE(child, &n->n_children, n_next, tmp) {
512 if (node_is_direct_map(child)) {
516 if (indirect == false)
521 * This is the first-level map node; the one that contains
522 * the key and subnodes with mountpoints and actual map names.
524 if (child->n_map == NULL)
528 log_debugx("map \"%s\" is an indirect map, parsing",
531 log_debugx("map \"%s\" is a direct map, parsing",
534 parse_map(child, child->n_map, NULL);
539 node_expand_direct_maps(struct node *n)
542 node_expand_maps(n, false);
546 node_expand_indirect_maps(struct node *n)
549 node_expand_maps(n, true);
553 node_path_x(const struct node *n, char *x)
558 if (n->n_parent == NULL)
562 * Return "/-" for direct maps only if we were asked for path
563 * to the "/-" node itself, not to any of its subnodes.
565 if (n->n_parent->n_parent == NULL &&
566 strcmp(n->n_key, "/-") == 0 &&
571 assert(n->n_key[0] != '\0');
572 path = separated_concat(n->n_key, x, '/');
576 * Strip trailing slash.
580 if (path[len - 1] == '/')
581 path[len - 1] = '\0';
583 return (node_path_x(n->n_parent, path));
587 * Return full path for node, consisting of concatenated
588 * paths of node itself and all its parents, up to the root.
591 node_path(const struct node *n)
594 return (node_path_x(n, checked_strdup("")));
598 node_options_x(const struct node *n, char *x)
602 options = separated_concat(x, n->n_options, ',');
603 if (n->n_parent == NULL)
606 return (node_options_x(n->n_parent, options));
610 * Return options for node, consisting of concatenated
611 * options from the node itself and all its parents,
615 node_options(const struct node *n)
618 return (node_options_x(n, checked_strdup("")));
622 node_print_indent(const struct node *n, int indent)
624 const struct node *child, *first_child;
625 char *path, *options;
628 options = node_options(n);
631 * Do not show both parent and child node if they have the same
632 * mountpoint; only show the child node. This means the typical,
633 * "key location", map entries are shown in a single line;
634 * the "key mountpoint1 location2 mountpoint2 location2" entries
635 * take multiple lines.
637 first_child = TAILQ_FIRST(&n->n_children);
638 if (first_child == NULL || TAILQ_NEXT(first_child, n_next) != NULL ||
639 strcmp(path, node_path(first_child)) != 0) {
640 assert(n->n_location == NULL || n->n_map == NULL);
641 printf("%*.s%-*s %s%-*s %-*s # %s map %s at %s:%d\n",
645 options[0] != '\0' ? "-" : " ",
647 options[0] != '\0' ? options : "",
649 n->n_location != NULL ? n->n_location : n->n_map != NULL ? n->n_map : "",
650 node_is_direct_map(n) ? "direct" : "indirect",
651 indent == 0 ? "referenced" : "defined",
652 n->n_config_file, n->n_config_line);
658 TAILQ_FOREACH(child, &n->n_children, n_next)
659 node_print_indent(child, indent + 2);
663 node_print(const struct node *n)
665 const struct node *child;
667 TAILQ_FOREACH(child, &n->n_children, n_next)
668 node_print_indent(child, 0);
672 node_find(struct node *node, const char *path)
674 struct node *child, *found;
677 //log_debugx("looking up %s in %s", path, node->n_key);
679 tmp = node_path(node);
680 if (strncmp(tmp, path, strlen(tmp)) != 0) {
686 TAILQ_FOREACH(child, &node->n_children, n_next) {
687 found = node_find(child, path);
696 * Canonical form of a map entry looks like this:
698 * key [-options] [ [/mountpoint] [-options2] location ... ]
700 * Entries for executable maps are slightly different, as they
701 * lack the 'key' field and are always single-line; the key field
702 * for those maps is taken from 'executable_key' argument.
704 * We parse it in such a way that a map always has two levels - first
705 * for key, and the second, for the mountpoint.
708 parse_map_yyin(struct node *parent, const char *map, const char *executable_key)
710 char *key = NULL, *options = NULL, *mountpoint = NULL,
711 *options2 = NULL, *location = NULL;
717 if (executable_key != NULL)
718 key = checked_strdup(executable_key);
722 if (ret == 0 || ret == NEWLINE) {
724 * In case of executable map, the key is always
725 * non-NULL, even if the map is empty. So, make sure
726 * we don't fail empty maps here.
728 if ((key != NULL && executable_key == NULL) ||
730 log_errx(1, "truncated entry at %s, line %d",
733 if (ret == 0 || executable_key != NULL) {
739 key = options = NULL;
744 key = checked_strdup(yytext);
746 node_new(parent, key, NULL, NULL, map, lineno);
747 key = options = NULL;
751 } else if (yytext[0] == '-') {
752 if (options != NULL) {
753 log_errx(1, "duplicated options at %s, line %d",
757 * +1 to skip leading "-".
759 options = checked_strdup(yytext + 1);
764 * We cannot properly handle a situation where the map key
765 * is "/". Ignore such entries.
767 * XXX: According to Piete Brooks, Linux automounter uses
768 * "/" as a wildcard character in LDAP maps. Perhaps
769 * we should work around this braindamage by substituting
772 if (strcmp(key, "/") == 0) {
773 log_warnx("nonsensical map key \"/\" at %s, line %d; "
774 "ignoring map entry ", map, lineno);
777 * Skip the rest of the entry.
781 } while (ret != 0 && ret != NEWLINE);
783 key = options = NULL;
787 //log_debugx("adding map node, %s", key);
788 node = node_new(parent, key, options, NULL, map, lineno);
789 key = options = NULL;
792 if (yytext[0] == '/') {
793 if (mountpoint != NULL) {
794 log_errx(1, "duplicated mountpoint "
795 "in %s, line %d", map, lineno);
797 if (options2 != NULL || location != NULL) {
798 log_errx(1, "mountpoint out of order "
799 "in %s, line %d", map, lineno);
801 mountpoint = checked_strdup(yytext);
805 if (yytext[0] == '-') {
806 if (options2 != NULL) {
807 log_errx(1, "duplicated options "
808 "in %s, line %d", map, lineno);
810 if (location != NULL) {
811 log_errx(1, "options out of order "
812 "in %s, line %d", map, lineno);
814 options2 = checked_strdup(yytext + 1);
818 if (location != NULL) {
819 log_errx(1, "too many arguments "
820 "in %s, line %d", map, lineno);
824 * If location field starts with colon, e.g. ":/dev/cd0",
827 if (yytext[0] == ':') {
828 location = checked_strdup(yytext + 1);
829 if (location[0] == '\0') {
830 log_errx(1, "empty location in %s, "
831 "line %d", map, lineno);
834 location = checked_strdup(yytext);
837 if (mountpoint == NULL)
838 mountpoint = checked_strdup("/");
839 if (options2 == NULL)
840 options2 = checked_strdup("");
843 log_debugx("adding map node, %s %s %s",
844 mountpoint, options2, location);
846 node_new(node, mountpoint, options2, location,
848 mountpoint = options2 = location = NULL;
851 if (ret == 0 || ret == NEWLINE) {
852 if (mountpoint != NULL || options2 != NULL ||
854 log_errx(1, "truncated entry "
855 "in %s, line %d", map, lineno);
864 * Parse output of a special map called without argument. It is a list
865 * of keys, separated by newlines. They can contain whitespace, so use
866 * getline(3) instead of lexer used for maps.
869 parse_map_keys_yyin(struct node *parent, const char *map)
871 char *line = NULL, *key;
878 linelen = getline(&line, &linecap, yyin);
887 * Empty line, consisting of just the newline.
893 * "-1" to strip the trailing newline.
895 key = strndup(line, linelen - 1);
897 log_debugx("adding key \"%s\"", key);
898 node_new(parent, key, NULL, NULL, map, lineno);
905 file_is_executable(const char *path)
910 error = stat(path, &sb);
912 log_err(1, "cannot stat %s", path);
913 if ((sb.st_mode & S_IXUSR) || (sb.st_mode & S_IXGRP) ||
914 (sb.st_mode & S_IXOTH))
920 * Parse a special map, e.g. "-hosts".
923 parse_special_map(struct node *parent, const char *map, const char *key)
928 assert(map[0] == '-');
931 * +1 to skip leading "-" in map name.
933 ret = asprintf(&path, "%s/special_%s", AUTO_SPECIAL_PREFIX, map + 1);
935 log_err(1, "asprintf");
937 yyin = auto_popen(path, key, NULL);
938 assert(yyin != NULL);
941 parse_map_keys_yyin(parent, map);
943 parse_map_yyin(parent, map, key);
946 error = auto_pclose(yyin);
949 log_errx(1, "failed to handle special map \"%s\"", map);
951 node_expand_includes(parent, false);
952 node_expand_direct_maps(parent);
958 * Retrieve and parse map from directory services, e.g. LDAP.
959 * Note that it is different from executable maps, in that
960 * the include script outputs the whole map to standard output
961 * (as opposed to executable maps that only output a single
962 * entry, without the key), and it takes the map name as an
963 * argument, instead of key.
966 parse_included_map(struct node *parent, const char *map)
970 assert(map[0] != '-');
971 assert(map[0] != '/');
973 error = access(AUTO_INCLUDE_PATH, F_OK);
975 log_errx(1, "directory services not configured;"
976 " %s does not exist", AUTO_INCLUDE_PATH);
979 yyin = auto_popen(AUTO_INCLUDE_PATH, map, NULL);
980 assert(yyin != NULL);
982 parse_map_yyin(parent, map, NULL);
984 error = auto_pclose(yyin);
987 log_errx(1, "failed to handle remote map \"%s\"", map);
989 node_expand_includes(parent, false);
990 node_expand_direct_maps(parent);
994 parse_map(struct node *parent, const char *map, const char *key)
1000 assert(map != NULL);
1001 assert(map[0] != '\0');
1003 log_debugx("parsing map \"%s\"", map);
1006 return (parse_special_map(parent, map, key));
1008 if (map[0] == '/') {
1009 path = checked_strdup(map);
1011 ret = asprintf(&path, "%s/%s", AUTO_MAP_PREFIX, map);
1013 log_err(1, "asprintf");
1014 log_debugx("map \"%s\" maps to \"%s\"", map, path);
1017 * See if the file exists. If not, try to obtain the map
1018 * from directory services.
1020 error = access(path, F_OK);
1022 log_debugx("map file \"%s\" does not exist; falling "
1023 "back to directory services", path);
1024 return (parse_included_map(parent, map));
1028 executable = file_is_executable(path);
1031 log_debugx("map \"%s\" is executable", map);
1034 yyin = auto_popen(path, key, NULL);
1036 yyin = auto_popen(path, NULL);
1038 assert(yyin != NULL);
1040 yyin = fopen(path, "r");
1042 log_err(1, "unable to open \"%s\"", path);
1048 parse_map_yyin(parent, map, executable ? key : NULL);
1051 error = auto_pclose(yyin);
1054 log_errx(1, "failed to handle executable map \"%s\"",
1062 log_debugx("done parsing map \"%s\"", map);
1064 node_expand_includes(parent, false);
1065 node_expand_direct_maps(parent);
1069 parse_master_yyin(struct node *root, const char *master)
1071 char *mountpoint = NULL, *map = NULL, *options = NULL;
1075 * XXX: 1 gives incorrect values; wtf?
1081 if (ret == 0 || ret == NEWLINE) {
1082 if (mountpoint != NULL) {
1083 //log_debugx("adding map for %s", mountpoint);
1084 node_new_map(root, mountpoint, options, map,
1090 mountpoint = map = options = NULL;
1094 if (mountpoint == NULL) {
1095 mountpoint = checked_strdup(yytext);
1096 } else if (map == NULL) {
1097 map = checked_strdup(yytext);
1098 } else if (options == NULL) {
1100 * +1 to skip leading "-".
1102 options = checked_strdup(yytext + 1);
1104 log_errx(1, "too many arguments at %s, line %d",
1111 parse_master(struct node *root, const char *master)
1114 log_debugx("parsing auto_master file at \"%s\"", master);
1116 yyin = fopen(master, "r");
1118 err(1, "unable to open %s", master);
1120 parse_master_yyin(root, master);
1125 log_debugx("done parsing \"%s\"", master);
1127 node_expand_includes(root, true);
1128 node_expand_direct_maps(root);
1132 * Two things daemon(3) does, that we actually also want to do
1133 * when running in foreground, is closing the stdin and chdiring
1134 * to "/". This is what we do here.
1145 fd = open(_PATH_DEVNULL, O_RDWR, 0);
1147 log_warn("cannot open %s", _PATH_DEVNULL);
1151 error = dup2(fd, STDIN_FILENO);
1163 main(int argc, char **argv)
1167 if (argv[0] == NULL)
1168 log_errx(1, "NULL command name");
1170 cmdname = basename(argv[0]);
1172 if (strcmp(cmdname, "automount") == 0)
1173 return (main_automount(argc, argv));
1174 else if (strcmp(cmdname, "automountd") == 0)
1175 return (main_automountd(argc, argv));
1176 else if (strcmp(cmdname, "autounmountd") == 0)
1177 return (main_autounmountd(argc, argv));
1179 log_errx(1, "binary name should be either \"automount\", "
1180 "\"automountd\", or \"autounmountd\"");