2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2016 The DragonFly Project
5 * Copyright (c) 2014 The FreeBSD Foundation
8 * This software was developed by Edward Tomasz Napierala under sponsorship
9 * from the FreeBSD Foundation.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * $FreeBSD: head/usr.sbin/autofs/common.c 303527 2016-07-30 01:10:05Z bapt $
35 #include <sys/types.h>
52 extern int yylex(void);
54 static void parse_master_yyin(struct node *root, const char *master);
55 static void parse_map_yyin(struct node *parent, const char *map,
56 const char *executable_key);
59 checked_strdup(const char *s)
72 * Concatenate two strings, inserting separator between them, unless not needed.
75 concat(const char *s1, char separator, const char *s2)
89 s1last = s1[strlen(s1) - 1];
93 if (s1last == separator && s2first == separator) {
95 * If s1 ends with the separator and s2 begins with
96 * it - skip the latter; otherwise concatenating "/"
97 * and "/foo" would end up returning "//foo".
99 ret = asprintf(&result, "%s%s", s1, s2 + 1);
100 } else if (s1last == separator || s2first == separator ||
101 s1[0] == '\0' || s2[0] == '\0') {
102 ret = asprintf(&result, "%s%s", s1, s2);
104 ret = asprintf(&result, "%s%c%s", s1, separator, s2);
107 log_err(1, "asprintf");
109 //log_debugx("%s: got %s and %s, returning %s", __func__, s1, s2, result);
115 create_directory(const char *path)
117 char *component, *copy, *tofree, *partial, *tmp;
120 assert(path[0] == '/');
123 * +1 to skip the leading slash.
125 copy = tofree = checked_strdup(path + 1);
127 partial = checked_strdup("");
129 component = strsep(©, "/");
130 if (component == NULL)
132 tmp = concat(partial, '/', component);
135 //log_debugx("creating \"%s\"", partial);
136 error = mkdir(partial, 0755);
137 if (error != 0 && errno != EEXIST) {
138 log_warn("cannot create %s", partial);
151 n = calloc(1, sizeof(*n));
153 log_err(1, "calloc");
155 n->n_key = checked_strdup("/");
156 n->n_options = checked_strdup("");
158 TAILQ_INIT(&n->n_children);
164 node_new(struct node *parent, char *key, char *options, char *location,
165 const char *config_file, int config_line)
169 n = calloc(1, sizeof(*n));
171 log_err(1, "calloc");
173 TAILQ_INIT(&n->n_children);
175 assert(key[0] != '\0');
178 n->n_options = options;
180 n->n_options = strdup("");
181 n->n_location = location;
182 assert(config_file != NULL);
183 n->n_config_file = config_file;
184 assert(config_line >= 0);
185 n->n_config_line = config_line;
187 assert(parent != NULL);
188 n->n_parent = parent;
189 TAILQ_INSERT_TAIL(&parent->n_children, n, n_next);
195 node_new_map(struct node *parent, char *key, char *options, char *map,
196 const char *config_file, int config_line)
200 n = calloc(1, sizeof(*n));
202 log_err(1, "calloc");
204 TAILQ_INIT(&n->n_children);
206 assert(key[0] != '\0');
209 n->n_options = options;
211 n->n_options = strdup("");
213 assert(config_file != NULL);
214 n->n_config_file = config_file;
215 assert(config_line >= 0);
216 n->n_config_line = config_line;
218 assert(parent != NULL);
219 n->n_parent = parent;
220 TAILQ_INSERT_TAIL(&parent->n_children, n, n_next);
226 node_duplicate(const struct node *o, struct node *parent)
228 const struct node *child;
232 parent = o->n_parent;
234 n = node_new(parent, o->n_key, o->n_options, o->n_location,
235 o->n_config_file, o->n_config_line);
237 TAILQ_FOREACH(child, &o->n_children, n_next)
238 node_duplicate(child, n);
244 node_delete(struct node *n)
246 struct node *child, *tmp;
250 TAILQ_FOREACH_SAFE(child, &n->n_children, n_next, tmp)
253 if (n->n_parent != NULL)
254 TAILQ_REMOVE(&n->n_parent->n_children, n, n_next);
260 * Move (reparent) node 'n' to make it sibling of 'previous', placed
264 node_move_after(struct node *n, struct node *previous)
267 TAILQ_REMOVE(&n->n_parent->n_children, n, n_next);
268 n->n_parent = previous->n_parent;
269 TAILQ_INSERT_AFTER(&previous->n_parent->n_children, previous, n, n_next);
273 node_expand_includes(struct node *root, bool is_master)
275 struct node *n, *n2, *tmp, *tmp2, *tmproot;
278 TAILQ_FOREACH_SAFE(n, &root->n_children, n_next, tmp) {
279 if (n->n_key[0] != '+')
282 error = access(AUTO_INCLUDE_PATH, F_OK);
284 log_errx(1, "directory services not configured; "
285 "%s does not exist", AUTO_INCLUDE_PATH);
289 * "+1" to skip leading "+".
291 yyin = auto_popen(AUTO_INCLUDE_PATH, n->n_key + 1, NULL);
292 assert(yyin != NULL);
294 tmproot = node_new_root();
296 parse_master_yyin(tmproot, n->n_key);
298 parse_map_yyin(tmproot, n->n_key, NULL);
300 error = auto_pclose(yyin);
303 log_errx(1, "failed to handle include \"%s\"",
308 * Entries to be included are now in tmproot. We need to merge
309 * them with the rest, preserving their place and ordering.
311 TAILQ_FOREACH_REVERSE_SAFE(n2,
312 &tmproot->n_children, nodehead, n_next, tmp2) {
313 node_move_after(n2, n);
317 node_delete(tmproot);
322 expand_ampersand(char *string, const char *key)
325 int i, ret, before_len = 0;
326 bool backslashed = false;
328 assert(key[0] != '\0');
330 expanded = checked_strdup(string);
332 for (i = 0; string[i] != '\0'; i++) {
334 if (c == '\\' && backslashed == false) {
347 * The 'before_len' variable contains the number
348 * of characters before the '&'.
351 //assert(i < (int)strlen(string));
353 ret = asprintf(&expanded, "%.*s%s%s",
354 before_len, string, key, string + before_len + 1);
356 log_err(1, "asprintf");
358 //log_debugx("\"%s\" expanded with key \"%s\" to \"%s\"",
359 // string, key, expanded);
362 * Figure out where to start searching for next variable.
365 i = before_len + strlen(key);
366 if (i == (int)strlen(string))
369 //assert(i < (int)strlen(string));
376 * Expand "&" in n_location. If the key is NULL, try to use
377 * key from map entries themselves. Keep in mind that maps
378 * consist of tho levels of node structures, the key is one
381 * Variant with NULL key is for "automount -LL".
384 node_expand_ampersand(struct node *n, const char *key)
388 if (n->n_location != NULL) {
390 if (n->n_parent != NULL &&
391 strcmp(n->n_parent->n_key, "*") != 0) {
392 n->n_location = expand_ampersand(n->n_location,
396 n->n_location = expand_ampersand(n->n_location, key);
400 TAILQ_FOREACH(child, &n->n_children, n_next)
401 node_expand_ampersand(child, key);
405 * Expand "*" in n_key.
408 node_expand_wildcard(struct node *n, const char *key)
410 struct node *child, *expanded;
414 if (strcmp(n->n_key, "*") == 0) {
415 expanded = node_duplicate(n, NULL);
416 expanded->n_key = checked_strdup(key);
417 node_move_after(expanded, n);
420 TAILQ_FOREACH(child, &n->n_children, n_next)
421 node_expand_wildcard(child, key);
425 node_expand_defined(struct node *n)
428 int error, cumulated_error = 0;
430 if (n->n_location != NULL) {
431 n->n_location = defined_expand(n->n_location);
432 if (n->n_location == NULL) {
433 log_warnx("failed to expand location for %s",
439 TAILQ_FOREACH(child, &n->n_children, n_next) {
440 error = node_expand_defined(child);
441 if (error != 0 && cumulated_error == 0)
442 cumulated_error = error;
445 return (cumulated_error);
449 node_is_direct_key(const struct node *n)
452 if (n->n_parent != NULL && n->n_parent->n_parent == NULL &&
453 strcmp(n->n_key, "/-") == 0) {
461 node_is_direct_map(const struct node *n)
465 assert(n->n_parent != NULL);
466 if (n->n_parent->n_parent == NULL)
471 return (node_is_direct_key(n));
475 node_has_wildcards(const struct node *n)
477 const struct node *child;
479 TAILQ_FOREACH(child, &n->n_children, n_next) {
480 if (strcmp(child->n_key, "*") == 0)
488 node_expand_maps(struct node *n, bool indirect)
490 struct node *child, *tmp;
492 TAILQ_FOREACH_SAFE(child, &n->n_children, n_next, tmp) {
493 if (node_is_direct_map(child)) {
497 if (indirect == false)
502 * This is the first-level map node; the one that contains
503 * the key and subnodes with mountpoints and actual map names.
505 if (child->n_map == NULL)
509 log_debugx("map \"%s\" is an indirect map, parsing",
512 log_debugx("map \"%s\" is a direct map, parsing",
515 parse_map(child, child->n_map, NULL, NULL);
520 node_expand_direct_maps(struct node *n)
523 node_expand_maps(n, false);
527 node_expand_indirect_maps(struct node *n)
530 node_expand_maps(n, true);
534 node_path_x(const struct node *n, char *x)
538 if (n->n_parent == NULL)
542 * Return "/-" for direct maps only if we were asked for path
543 * to the "/-" node itself, not to any of its subnodes.
545 if (node_is_direct_key(n) && x[0] != '\0')
548 assert(n->n_key[0] != '\0');
549 path = concat(n->n_key, '/', x);
552 return (node_path_x(n->n_parent, path));
556 * Return full path for node, consisting of concatenated
557 * paths of node itself and all its parents, up to the root.
560 node_path(const struct node *n)
565 path = node_path_x(n, checked_strdup(""));
568 * Strip trailing slash, unless the whole path is "/".
571 if (len > 1 && path[len - 1] == '/')
572 path[len - 1] = '\0';
578 node_options_x(const struct node *n, char *x)
585 options = concat(x, ',', n->n_options);
588 return (node_options_x(n->n_parent, options));
592 * Return options for node, consisting of concatenated
593 * options from the node itself and all its parents,
597 node_options(const struct node *n)
600 return (node_options_x(n, checked_strdup("")));
604 node_print_indent(const struct node *n, const char *cmdline_options,
607 const struct node *child, *first_child;
608 char *path, *options, *tmp;
611 tmp = node_options(n);
612 options = concat(cmdline_options, ',', tmp);
616 * Do not show both parent and child node if they have the same
617 * mountpoint; only show the child node. This means the typical,
618 * "key location", map entries are shown in a single line;
619 * the "key mountpoint1 location2 mountpoint2 location2" entries
620 * take multiple lines.
622 first_child = TAILQ_FIRST(&n->n_children);
623 if (first_child == NULL || TAILQ_NEXT(first_child, n_next) != NULL ||
624 strcmp(path, node_path(first_child)) != 0) {
625 assert(n->n_location == NULL || n->n_map == NULL);
626 printf("%*.s%-*s %s%-*s %-*s # %s map %s at %s:%d\n",
630 options[0] != '\0' ? "-" : " ",
632 options[0] != '\0' ? options : "",
634 n->n_location != NULL ? n->n_location : n->n_map != NULL ? n->n_map : "",
635 node_is_direct_map(n) ? "direct" : "indirect",
636 indent == 0 ? "referenced" : "defined",
637 n->n_config_file, n->n_config_line);
643 TAILQ_FOREACH(child, &n->n_children, n_next)
644 node_print_indent(child, cmdline_options, indent + 2);
648 * Recursively print node with all its children. The cmdline_options
649 * argument is used for additional options to be prepended to all the
650 * others - usually those are the options passed by command line.
653 node_print(const struct node *n, const char *cmdline_options)
655 const struct node *child;
657 TAILQ_FOREACH(child, &n->n_children, n_next)
658 node_print_indent(child, cmdline_options, 0);
662 node_find_x(struct node *node, const char *path)
664 struct node *child, *found;
668 //log_debugx("looking up %s in %s", path, node_path(node));
670 if (!node_is_direct_key(node)) {
671 tmp = node_path(node);
672 tmplen = strlen(tmp);
673 if (strncmp(tmp, path, tmplen) != 0) {
677 if (path[tmplen] != '/' && path[tmplen] != '\0') {
679 * If we have two map entries like 'foo' and 'foobar', make
680 * sure the search for 'foobar' won't match 'foo' instead.
688 TAILQ_FOREACH(child, &node->n_children, n_next) {
689 found = node_find_x(child, path);
694 if (node->n_parent == NULL || node_is_direct_key(node))
701 node_find(struct node *root, const char *path)
705 assert(root->n_parent == NULL);
707 node = node_find_x(root, path);
709 assert(node != root);
715 * Canonical form of a map entry looks like this:
717 * key [-options] [ [/mountpoint] [-options2] location ... ]
719 * Entries for executable maps are slightly different, as they
720 * lack the 'key' field and are always single-line; the key field
721 * for those maps is taken from 'executable_key' argument.
723 * We parse it in such a way that a map always has two levels - first
724 * for key, and the second, for the mountpoint.
727 parse_map_yyin(struct node *parent, const char *map, const char *executable_key)
729 char *key = NULL, *options = NULL, *mountpoint = NULL,
730 *options2 = NULL, *location = NULL;
736 if (executable_key != NULL)
737 key = checked_strdup(executable_key);
741 if (ret == 0 || ret == NEWLINE) {
743 * In case of executable map, the key is always
744 * non-NULL, even if the map is empty. So, make sure
745 * we don't fail empty maps here.
747 if ((key != NULL && executable_key == NULL) ||
749 log_errx(1, "truncated entry at %s, line %d",
752 if (ret == 0 || executable_key != NULL) {
758 key = options = NULL;
763 key = checked_strdup(yytext);
765 node_new(parent, key, NULL, NULL, map, lineno);
766 key = options = NULL;
770 } else if (yytext[0] == '-') {
771 if (options != NULL) {
772 log_errx(1, "duplicated options at %s, line %d",
776 * +1 to skip leading "-".
778 options = checked_strdup(yytext + 1);
783 * We cannot properly handle a situation where the map key
784 * is "/". Ignore such entries.
786 * XXX: According to Piete Brooks, Linux automounter uses
787 * "/" as a wildcard character in LDAP maps. Perhaps
788 * we should work around this braindamage by substituting
791 if (strcmp(key, "/") == 0) {
792 log_warnx("nonsensical map key \"/\" at %s, line %d; "
793 "ignoring map entry ", map, lineno);
796 * Skip the rest of the entry.
800 } while (ret != 0 && ret != NEWLINE);
802 key = options = NULL;
806 //log_debugx("adding map node, %s", key);
807 node = node_new(parent, key, options, NULL, map, lineno);
808 key = options = NULL;
811 if (yytext[0] == '/') {
812 if (mountpoint != NULL) {
813 log_errx(1, "duplicated mountpoint "
814 "in %s, line %d", map, lineno);
816 if (options2 != NULL || location != NULL) {
817 log_errx(1, "mountpoint out of order "
818 "in %s, line %d", map, lineno);
820 mountpoint = checked_strdup(yytext);
824 if (yytext[0] == '-') {
825 if (options2 != NULL) {
826 log_errx(1, "duplicated options "
827 "in %s, line %d", map, lineno);
829 if (location != NULL) {
830 log_errx(1, "options out of order "
831 "in %s, line %d", map, lineno);
833 options2 = checked_strdup(yytext + 1);
837 if (location != NULL) {
838 log_errx(1, "too many arguments "
839 "in %s, line %d", map, lineno);
843 * If location field starts with colon, e.g. ":/dev/cd0",
846 if (yytext[0] == ':') {
847 location = checked_strdup(yytext + 1);
848 if (location[0] == '\0') {
849 log_errx(1, "empty location in %s, "
850 "line %d", map, lineno);
853 location = checked_strdup(yytext);
856 if (mountpoint == NULL)
857 mountpoint = checked_strdup("/");
858 if (options2 == NULL)
859 options2 = checked_strdup("");
862 log_debugx("adding map node, %s %s %s",
863 mountpoint, options2, location);
865 node_new(node, mountpoint, options2, location,
867 mountpoint = options2 = location = NULL;
870 if (ret == 0 || ret == NEWLINE) {
871 if (mountpoint != NULL || options2 != NULL ||
873 log_errx(1, "truncated entry "
874 "in %s, line %d", map, lineno);
883 * Parse output of a special map called without argument. It is a list
884 * of keys, separated by newlines. They can contain whitespace, so use
885 * getline(3) instead of lexer used for maps.
888 parse_map_keys_yyin(struct node *parent, const char *map)
890 char *line = NULL, *key;
897 linelen = getline(&line, &linecap, yyin);
906 * Empty line, consisting of just the newline.
912 * "-1" to strip the trailing newline.
914 key = strndup(line, linelen - 1);
916 log_debugx("adding key \"%s\"", key);
917 node_new(parent, key, NULL, NULL, map, lineno);
924 file_is_executable(const char *path)
929 error = stat(path, &sb);
931 log_err(1, "cannot stat %s", path);
932 if ((sb.st_mode & S_IXUSR) || (sb.st_mode & S_IXGRP) ||
933 (sb.st_mode & S_IXOTH))
939 * Parse a special map, e.g. "-hosts".
942 parse_special_map(struct node *parent, const char *map, const char *key)
947 assert(map[0] == '-');
950 * +1 to skip leading "-" in map name.
952 ret = asprintf(&path, "%s/special_%s", AUTO_SPECIAL_PREFIX, map + 1);
954 log_err(1, "asprintf");
956 yyin = auto_popen(path, key, NULL);
957 assert(yyin != NULL);
960 parse_map_keys_yyin(parent, map);
962 parse_map_yyin(parent, map, key);
965 error = auto_pclose(yyin);
968 log_errx(1, "failed to handle special map \"%s\"", map);
970 node_expand_includes(parent, false);
971 node_expand_direct_maps(parent);
977 * Retrieve and parse map from directory services, e.g. LDAP.
978 * Note that it is different from executable maps, in that
979 * the include script outputs the whole map to standard output
980 * (as opposed to executable maps that only output a single
981 * entry, without the key), and it takes the map name as an
982 * argument, instead of key.
985 parse_included_map(struct node *parent, const char *map)
989 assert(map[0] != '-');
990 assert(map[0] != '/');
992 error = access(AUTO_INCLUDE_PATH, F_OK);
994 log_errx(1, "directory services not configured;"
995 " %s does not exist", AUTO_INCLUDE_PATH);
998 yyin = auto_popen(AUTO_INCLUDE_PATH, map, NULL);
999 assert(yyin != NULL);
1001 parse_map_yyin(parent, map, NULL);
1003 error = auto_pclose(yyin);
1006 log_errx(1, "failed to handle remote map \"%s\"", map);
1008 node_expand_includes(parent, false);
1009 node_expand_direct_maps(parent);
1013 parse_map(struct node *parent, const char *map, const char *key,
1020 assert(map != NULL);
1021 assert(map[0] != '\0');
1023 log_debugx("parsing map \"%s\"", map);
1025 if (wildcards != NULL)
1028 if (map[0] == '-') {
1029 if (wildcards != NULL)
1031 return (parse_special_map(parent, map, key));
1034 if (map[0] == '/') {
1035 path = checked_strdup(map);
1037 ret = asprintf(&path, "%s/%s", AUTO_MAP_PREFIX, map);
1039 log_err(1, "asprintf");
1040 log_debugx("map \"%s\" maps to \"%s\"", map, path);
1043 * See if the file exists. If not, try to obtain the map
1044 * from directory services.
1046 error = access(path, F_OK);
1048 log_debugx("map file \"%s\" does not exist; falling "
1049 "back to directory services", path);
1050 return (parse_included_map(parent, map));
1054 executable = file_is_executable(path);
1057 log_debugx("map \"%s\" is executable", map);
1059 if (wildcards != NULL)
1063 yyin = auto_popen(path, key, NULL);
1065 yyin = auto_popen(path, NULL);
1067 assert(yyin != NULL);
1069 yyin = fopen(path, "r");
1071 log_err(1, "unable to open \"%s\"", path);
1077 parse_map_yyin(parent, map, executable ? key : NULL);
1080 error = auto_pclose(yyin);
1083 log_errx(1, "failed to handle executable map \"%s\"",
1091 log_debugx("done parsing map \"%s\"", map);
1093 node_expand_includes(parent, false);
1094 node_expand_direct_maps(parent);
1098 parse_master_yyin(struct node *root, const char *master)
1100 char *mountpoint = NULL, *map = NULL, *options = NULL;
1104 * XXX: 1 gives incorrect values; wtf?
1110 if (ret == 0 || ret == NEWLINE) {
1111 if (mountpoint != NULL) {
1112 //log_debugx("adding map for %s", mountpoint);
1113 node_new_map(root, mountpoint, options, map,
1119 mountpoint = map = options = NULL;
1123 if (mountpoint == NULL) {
1124 mountpoint = checked_strdup(yytext);
1125 } else if (map == NULL) {
1126 map = checked_strdup(yytext);
1127 } else if (options == NULL) {
1129 * +1 to skip leading "-".
1131 options = checked_strdup(yytext + 1);
1133 log_errx(1, "too many arguments at %s, line %d",
1140 parse_master(struct node *root, const char *master)
1143 log_debugx("parsing auto_master file at \"%s\"", master);
1145 yyin = fopen(master, "r");
1147 err(1, "unable to open %s", master);
1149 parse_master_yyin(root, master);
1154 log_debugx("done parsing \"%s\"", master);
1156 node_expand_includes(root, true);
1157 node_expand_direct_maps(root);
1161 * Two things daemon(3) does, that we actually also want to do
1162 * when running in foreground, is closing the stdin and chdiring
1163 * to "/". This is what we do here.
1174 fd = open(_PATH_DEVNULL, O_RDWR, 0);
1176 log_warn("cannot open %s", _PATH_DEVNULL);
1180 error = dup2(fd, STDIN_FILENO);
1192 main(int argc, char **argv)
1196 if (argv[0] == NULL)
1197 log_errx(1, "NULL command name");
1199 cmdname = basename(argv[0]);
1201 if (strcmp(cmdname, "automount") == 0)
1202 return (main_automount(argc, argv));
1203 else if (strcmp(cmdname, "automountd") == 0)
1204 main_automountd(argc, argv);
1205 else if (strcmp(cmdname, "autounmountd") == 0)
1206 main_autounmountd(argc, argv);
1208 log_errx(1, "binary name should be either \"automount\", "
1209 "\"automountd\", or \"autounmountd\"");