periodic(8): Sync with FreeBSD current
[dragonfly.git] / usr.sbin / autofs / common.c
blob38382e1cac78d8a86bed0adbe760b660e066c550
1 /*-
2 * Copyright (c) 2016 The DragonFly Project
3 * Copyright (c) 2014 The FreeBSD Foundation
4 * All rights reserved.
6 * This software was developed by Edward Tomasz Napierala under sponsorship
7 * from the FreeBSD Foundation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
30 * $FreeBSD: head/usr.sbin/autofs/common.c 303527 2016-07-30 01:10:05Z bapt $
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <assert.h>
36 #include <err.h>
37 #include <errno.h>
38 #include <fcntl.h>
39 #include <libgen.h>
40 #include <paths.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <unistd.h>
46 #include "common.h"
48 extern FILE *yyin;
49 extern char *yytext;
50 extern int yylex(void);
52 static void parse_master_yyin(struct node *root, const char *master);
53 static void parse_map_yyin(struct node *parent, const char *map,
54 const char *executable_key);
56 char *
57 checked_strdup(const char *s)
59 char *c;
61 assert(s != NULL);
63 c = strdup(s);
64 if (c == NULL)
65 log_err(1, "strdup");
66 return (c);
70 * Concatenate two strings, inserting separator between them, unless not needed.
72 char *
73 concat(const char *s1, char separator, const char *s2)
75 char *result;
76 char s1last, s2first;
77 int ret;
79 if (s1 == NULL)
80 s1 = "";
81 if (s2 == NULL)
82 s2 = "";
84 if (s1[0] == '\0')
85 s1last = '\0';
86 else
87 s1last = s1[strlen(s1) - 1];
89 s2first = s2[0];
91 if (s1last == separator && s2first == separator) {
93 * If s1 ends with the separator and s2 begins with
94 * it - skip the latter; otherwise concatenating "/"
95 * and "/foo" would end up returning "//foo".
97 ret = asprintf(&result, "%s%s", s1, s2 + 1);
98 } else if (s1last == separator || s2first == separator ||
99 s1[0] == '\0' || s2[0] == '\0') {
100 ret = asprintf(&result, "%s%s", s1, s2);
101 } else {
102 ret = asprintf(&result, "%s%c%s", s1, separator, s2);
104 if (ret < 0)
105 log_err(1, "asprintf");
107 //log_debugx("%s: got %s and %s, returning %s", __func__, s1, s2, result);
109 return (result);
112 void
113 create_directory(const char *path)
115 char *component, *copy, *tofree, *partial, *tmp;
116 int error;
118 assert(path[0] == '/');
121 * +1 to skip the leading slash.
123 copy = tofree = checked_strdup(path + 1);
125 partial = checked_strdup("");
126 for (;;) {
127 component = strsep(&copy, "/");
128 if (component == NULL)
129 break;
130 tmp = concat(partial, '/', component);
131 free(partial);
132 partial = tmp;
133 //log_debugx("creating \"%s\"", partial);
134 error = mkdir(partial, 0755);
135 if (error != 0 && errno != EEXIST) {
136 log_warn("cannot create %s", partial);
137 return;
141 free(tofree);
144 struct node *
145 node_new_root(void)
147 struct node *n;
149 n = calloc(1, sizeof(*n));
150 if (n == NULL)
151 log_err(1, "calloc");
152 // XXX
153 n->n_key = checked_strdup("/");
154 n->n_options = checked_strdup("");
156 TAILQ_INIT(&n->n_children);
158 return (n);
161 struct node *
162 node_new(struct node *parent, char *key, char *options, char *location,
163 const char *config_file, int config_line)
165 struct node *n;
167 n = calloc(1, sizeof(*n));
168 if (n == NULL)
169 log_err(1, "calloc");
171 TAILQ_INIT(&n->n_children);
172 assert(key != NULL);
173 assert(key[0] != '\0');
174 n->n_key = key;
175 if (options != NULL)
176 n->n_options = options;
177 else
178 n->n_options = strdup("");
179 n->n_location = location;
180 assert(config_file != NULL);
181 n->n_config_file = config_file;
182 assert(config_line >= 0);
183 n->n_config_line = config_line;
185 assert(parent != NULL);
186 n->n_parent = parent;
187 TAILQ_INSERT_TAIL(&parent->n_children, n, n_next);
189 return (n);
192 struct node *
193 node_new_map(struct node *parent, char *key, char *options, char *map,
194 const char *config_file, int config_line)
196 struct node *n;
198 n = calloc(1, sizeof(*n));
199 if (n == NULL)
200 log_err(1, "calloc");
202 TAILQ_INIT(&n->n_children);
203 assert(key != NULL);
204 assert(key[0] != '\0');
205 n->n_key = key;
206 if (options != NULL)
207 n->n_options = options;
208 else
209 n->n_options = strdup("");
210 n->n_map = map;
211 assert(config_file != NULL);
212 n->n_config_file = config_file;
213 assert(config_line >= 0);
214 n->n_config_line = config_line;
216 assert(parent != NULL);
217 n->n_parent = parent;
218 TAILQ_INSERT_TAIL(&parent->n_children, n, n_next);
220 return (n);
223 static struct node *
224 node_duplicate(const struct node *o, struct node *parent)
226 const struct node *child;
227 struct node *n;
229 if (parent == NULL)
230 parent = o->n_parent;
232 n = node_new(parent, o->n_key, o->n_options, o->n_location,
233 o->n_config_file, o->n_config_line);
235 TAILQ_FOREACH(child, &o->n_children, n_next)
236 node_duplicate(child, n);
238 return (n);
241 static void
242 node_delete(struct node *n)
244 struct node *child, *tmp;
246 assert (n != NULL);
248 TAILQ_FOREACH_SAFE(child, &n->n_children, n_next, tmp)
249 node_delete(child);
251 if (n->n_parent != NULL)
252 TAILQ_REMOVE(&n->n_parent->n_children, n, n_next);
254 free(n);
258 * Move (reparent) node 'n' to make it sibling of 'previous', placed
259 * just after it.
261 static void
262 node_move_after(struct node *n, struct node *previous)
265 TAILQ_REMOVE(&n->n_parent->n_children, n, n_next);
266 n->n_parent = previous->n_parent;
267 TAILQ_INSERT_AFTER(&previous->n_parent->n_children, previous, n, n_next);
270 static void
271 node_expand_includes(struct node *root, bool is_master)
273 struct node *n, *n2, *tmp, *tmp2, *tmproot;
274 int error;
276 TAILQ_FOREACH_SAFE(n, &root->n_children, n_next, tmp) {
277 if (n->n_key[0] != '+')
278 continue;
280 error = access(AUTO_INCLUDE_PATH, F_OK);
281 if (error != 0) {
282 log_errx(1, "directory services not configured; "
283 "%s does not exist", AUTO_INCLUDE_PATH);
287 * "+1" to skip leading "+".
289 yyin = auto_popen(AUTO_INCLUDE_PATH, n->n_key + 1, NULL);
290 assert(yyin != NULL);
292 tmproot = node_new_root();
293 if (is_master)
294 parse_master_yyin(tmproot, n->n_key);
295 else
296 parse_map_yyin(tmproot, n->n_key, NULL);
298 error = auto_pclose(yyin);
299 yyin = NULL;
300 if (error != 0) {
301 log_errx(1, "failed to handle include \"%s\"",
302 n->n_key);
306 * Entries to be included are now in tmproot. We need to merge
307 * them with the rest, preserving their place and ordering.
309 TAILQ_FOREACH_REVERSE_SAFE(n2,
310 &tmproot->n_children, nodehead, n_next, tmp2) {
311 node_move_after(n2, n);
314 node_delete(n);
315 node_delete(tmproot);
319 static char *
320 expand_ampersand(char *string, const char *key)
322 char c, *expanded;
323 int i, ret, before_len = 0;
324 bool backslashed = false;
326 assert(key[0] != '\0');
328 expanded = checked_strdup(string);
330 for (i = 0; string[i] != '\0'; i++) {
331 c = string[i];
332 if (c == '\\' && backslashed == false) {
333 backslashed = true;
334 continue;
336 if (backslashed) {
337 backslashed = false;
338 continue;
340 backslashed = false;
341 if (c != '&')
342 continue;
345 * The 'before_len' variable contains the number
346 * of characters before the '&'.
348 before_len = i;
349 //assert(i + 1 < (int)strlen(string));
351 ret = asprintf(&expanded, "%.*s%s%s",
352 before_len, string, key, string + before_len + 1);
353 if (ret < 0)
354 log_err(1, "asprintf");
356 //log_debugx("\"%s\" expanded with key \"%s\" to \"%s\"",
357 // string, key, expanded);
360 * Figure out where to start searching for next variable.
362 string = expanded;
363 i = before_len + strlen(key);
364 backslashed = false;
365 //assert(i < (int)strlen(string));
368 return (expanded);
372 * Expand "&" in n_location. If the key is NULL, try to use
373 * key from map entries themselves. Keep in mind that maps
374 * consist of tho levels of node structures, the key is one
375 * level up.
377 * Variant with NULL key is for "automount -LL".
379 void
380 node_expand_ampersand(struct node *n, const char *key)
382 struct node *child;
384 if (n->n_location != NULL) {
385 if (key == NULL) {
386 if (n->n_parent != NULL &&
387 strcmp(n->n_parent->n_key, "*") != 0) {
388 n->n_location = expand_ampersand(n->n_location,
389 n->n_parent->n_key);
391 } else {
392 n->n_location = expand_ampersand(n->n_location, key);
396 TAILQ_FOREACH(child, &n->n_children, n_next)
397 node_expand_ampersand(child, key);
401 * Expand "*" in n_key.
403 void
404 node_expand_wildcard(struct node *n, const char *key)
406 struct node *child, *expanded;
408 assert(key != NULL);
410 if (strcmp(n->n_key, "*") == 0) {
411 expanded = node_duplicate(n, NULL);
412 expanded->n_key = checked_strdup(key);
413 node_move_after(expanded, n);
416 TAILQ_FOREACH(child, &n->n_children, n_next)
417 node_expand_wildcard(child, key);
421 node_expand_defined(struct node *n)
423 struct node *child;
424 int error, cumulated_error = 0;
426 if (n->n_location != NULL) {
427 n->n_location = defined_expand(n->n_location);
428 if (n->n_location == NULL) {
429 log_warnx("failed to expand location for %s",
430 node_path(n));
431 return (EINVAL);
435 TAILQ_FOREACH(child, &n->n_children, n_next) {
436 error = node_expand_defined(child);
437 if (error != 0 && cumulated_error == 0)
438 cumulated_error = error;
441 return (cumulated_error);
444 static bool
445 node_is_direct_key(const struct node *n)
448 if (n->n_parent != NULL && n->n_parent->n_parent == NULL &&
449 strcmp(n->n_key, "/-") == 0) {
450 return (true);
453 return (false);
456 bool
457 node_is_direct_map(const struct node *n)
460 for (;;) {
461 assert(n->n_parent != NULL);
462 if (n->n_parent->n_parent == NULL)
463 break;
464 n = n->n_parent;
467 return (node_is_direct_key(n));
470 bool
471 node_has_wildcards(const struct node *n)
473 const struct node *child;
475 TAILQ_FOREACH(child, &n->n_children, n_next) {
476 if (strcmp(child->n_key, "*") == 0)
477 return (true);
480 return (false);
483 static void
484 node_expand_maps(struct node *n, bool indirect)
486 struct node *child, *tmp;
488 TAILQ_FOREACH_SAFE(child, &n->n_children, n_next, tmp) {
489 if (node_is_direct_map(child)) {
490 if (indirect)
491 continue;
492 } else {
493 if (indirect == false)
494 continue;
498 * This is the first-level map node; the one that contains
499 * the key and subnodes with mountpoints and actual map names.
501 if (child->n_map == NULL)
502 continue;
504 if (indirect) {
505 log_debugx("map \"%s\" is an indirect map, parsing",
506 child->n_map);
507 } else {
508 log_debugx("map \"%s\" is a direct map, parsing",
509 child->n_map);
511 parse_map(child, child->n_map, NULL, NULL);
515 static void
516 node_expand_direct_maps(struct node *n)
519 node_expand_maps(n, false);
522 void
523 node_expand_indirect_maps(struct node *n)
526 node_expand_maps(n, true);
529 static char *
530 node_path_x(const struct node *n, char *x)
532 char *path;
534 if (n->n_parent == NULL)
535 return (x);
538 * Return "/-" for direct maps only if we were asked for path
539 * to the "/-" node itself, not to any of its subnodes.
541 if (node_is_direct_key(n) && x[0] != '\0')
542 return (x);
544 assert(n->n_key[0] != '\0');
545 path = concat(n->n_key, '/', x);
546 free(x);
548 return (node_path_x(n->n_parent, path));
552 * Return full path for node, consisting of concatenated
553 * paths of node itself and all its parents, up to the root.
555 char *
556 node_path(const struct node *n)
558 char *path;
559 size_t len;
561 path = node_path_x(n, checked_strdup(""));
564 * Strip trailing slash, unless the whole path is "/".
566 len = strlen(path);
567 if (len > 1 && path[len - 1] == '/')
568 path[len - 1] = '\0';
570 return (path);
573 static char *
574 node_options_x(const struct node *n, char *x)
576 char *options;
578 if (n == NULL)
579 return (x);
581 options = concat(x, ',', n->n_options);
582 free(x);
584 return (node_options_x(n->n_parent, options));
588 * Return options for node, consisting of concatenated
589 * options from the node itself and all its parents,
590 * up to the root.
592 char *
593 node_options(const struct node *n)
596 return (node_options_x(n, checked_strdup("")));
599 static void
600 node_print_indent(const struct node *n, const char *cmdline_options,
601 int indent)
603 const struct node *child, *first_child;
604 char *path, *options, *tmp;
606 path = node_path(n);
607 tmp = node_options(n);
608 options = concat(cmdline_options, ',', tmp);
609 free(tmp);
612 * Do not show both parent and child node if they have the same
613 * mountpoint; only show the child node. This means the typical,
614 * "key location", map entries are shown in a single line;
615 * the "key mountpoint1 location2 mountpoint2 location2" entries
616 * take multiple lines.
618 first_child = TAILQ_FIRST(&n->n_children);
619 if (first_child == NULL || TAILQ_NEXT(first_child, n_next) != NULL ||
620 strcmp(path, node_path(first_child)) != 0) {
621 assert(n->n_location == NULL || n->n_map == NULL);
622 printf("%*.s%-*s %s%-*s %-*s # %s map %s at %s:%d\n",
623 indent, "",
624 25 - indent,
625 path,
626 options[0] != '\0' ? "-" : " ",
628 options[0] != '\0' ? options : "",
630 n->n_location != NULL ? n->n_location : n->n_map != NULL ? n->n_map : "",
631 node_is_direct_map(n) ? "direct" : "indirect",
632 indent == 0 ? "referenced" : "defined",
633 n->n_config_file, n->n_config_line);
636 free(path);
637 free(options);
639 TAILQ_FOREACH(child, &n->n_children, n_next)
640 node_print_indent(child, cmdline_options, indent + 2);
644 * Recursively print node with all its children. The cmdline_options
645 * argument is used for additional options to be prepended to all the
646 * others - usually those are the options passed by command line.
648 void
649 node_print(const struct node *n, const char *cmdline_options)
651 const struct node *child;
653 TAILQ_FOREACH(child, &n->n_children, n_next)
654 node_print_indent(child, cmdline_options, 0);
657 static struct node *
658 node_find_x(struct node *node, const char *path)
660 struct node *child, *found;
661 char *tmp;
662 size_t tmplen;
664 //log_debugx("looking up %s in %s", path, node_path(node));
666 if (!node_is_direct_key(node)) {
667 tmp = node_path(node);
668 tmplen = strlen(tmp);
669 if (strncmp(tmp, path, tmplen) != 0) {
670 free(tmp);
671 return (NULL);
673 if (path[tmplen] != '/' && path[tmplen] != '\0') {
675 * If we have two map entries like 'foo' and 'foobar', make
676 * sure the search for 'foobar' won't match 'foo' instead.
678 free(tmp);
679 return (NULL);
681 free(tmp);
684 TAILQ_FOREACH(child, &node->n_children, n_next) {
685 found = node_find_x(child, path);
686 if (found != NULL)
687 return (found);
690 if (node->n_parent == NULL || node_is_direct_key(node))
691 return (NULL);
693 return (node);
696 struct node *
697 node_find(struct node *root, const char *path)
699 struct node *node;
701 assert(root->n_parent == NULL);
703 node = node_find_x(root, path);
704 if (node != NULL)
705 assert(node != root);
707 return (node);
711 * Canonical form of a map entry looks like this:
713 * key [-options] [ [/mountpoint] [-options2] location ... ]
715 * Entries for executable maps are slightly different, as they
716 * lack the 'key' field and are always single-line; the key field
717 * for those maps is taken from 'executable_key' argument.
719 * We parse it in such a way that a map always has two levels - first
720 * for key, and the second, for the mountpoint.
722 static void
723 parse_map_yyin(struct node *parent, const char *map, const char *executable_key)
725 char *key = NULL, *options = NULL, *mountpoint = NULL,
726 *options2 = NULL, *location = NULL;
727 int ret;
728 struct node *node;
730 lineno = 1;
732 if (executable_key != NULL)
733 key = checked_strdup(executable_key);
735 for (;;) {
736 ret = yylex();
737 if (ret == 0 || ret == NEWLINE) {
739 * In case of executable map, the key is always
740 * non-NULL, even if the map is empty. So, make sure
741 * we don't fail empty maps here.
743 if ((key != NULL && executable_key == NULL) ||
744 options != NULL) {
745 log_errx(1, "truncated entry at %s, line %d",
746 map, lineno);
748 if (ret == 0 || executable_key != NULL) {
750 * End of file.
752 break;
753 } else {
754 key = options = NULL;
755 continue;
758 if (key == NULL) {
759 key = checked_strdup(yytext);
760 if (key[0] == '+') {
761 node_new(parent, key, NULL, NULL, map, lineno);
762 key = options = NULL;
763 continue;
765 continue;
766 } else if (yytext[0] == '-') {
767 if (options != NULL) {
768 log_errx(1, "duplicated options at %s, line %d",
769 map, lineno);
772 * +1 to skip leading "-".
774 options = checked_strdup(yytext + 1);
775 continue;
779 * We cannot properly handle a situation where the map key
780 * is "/". Ignore such entries.
782 * XXX: According to Piete Brooks, Linux automounter uses
783 * "/" as a wildcard character in LDAP maps. Perhaps
784 * we should work around this braindamage by substituting
785 * "*" for "/"?
787 if (strcmp(key, "/") == 0) {
788 log_warnx("nonsensical map key \"/\" at %s, line %d; "
789 "ignoring map entry ", map, lineno);
792 * Skip the rest of the entry.
794 do {
795 ret = yylex();
796 } while (ret != 0 && ret != NEWLINE);
798 key = options = NULL;
799 continue;
802 //log_debugx("adding map node, %s", key);
803 node = node_new(parent, key, options, NULL, map, lineno);
804 key = options = NULL;
806 for (;;) {
807 if (yytext[0] == '/') {
808 if (mountpoint != NULL) {
809 log_errx(1, "duplicated mountpoint "
810 "in %s, line %d", map, lineno);
812 if (options2 != NULL || location != NULL) {
813 log_errx(1, "mountpoint out of order "
814 "in %s, line %d", map, lineno);
816 mountpoint = checked_strdup(yytext);
817 goto again;
820 if (yytext[0] == '-') {
821 if (options2 != NULL) {
822 log_errx(1, "duplicated options "
823 "in %s, line %d", map, lineno);
825 if (location != NULL) {
826 log_errx(1, "options out of order "
827 "in %s, line %d", map, lineno);
829 options2 = checked_strdup(yytext + 1);
830 goto again;
833 if (location != NULL) {
834 log_errx(1, "too many arguments "
835 "in %s, line %d", map, lineno);
839 * If location field starts with colon, e.g. ":/dev/cd0",
840 * then strip it.
842 if (yytext[0] == ':') {
843 location = checked_strdup(yytext + 1);
844 if (location[0] == '\0') {
845 log_errx(1, "empty location in %s, "
846 "line %d", map, lineno);
848 } else {
849 location = checked_strdup(yytext);
852 if (mountpoint == NULL)
853 mountpoint = checked_strdup("/");
854 if (options2 == NULL)
855 options2 = checked_strdup("");
857 #if 0
858 log_debugx("adding map node, %s %s %s",
859 mountpoint, options2, location);
860 #endif
861 node_new(node, mountpoint, options2, location,
862 map, lineno);
863 mountpoint = options2 = location = NULL;
864 again:
865 ret = yylex();
866 if (ret == 0 || ret == NEWLINE) {
867 if (mountpoint != NULL || options2 != NULL ||
868 location != NULL) {
869 log_errx(1, "truncated entry "
870 "in %s, line %d", map, lineno);
872 break;
879 * Parse output of a special map called without argument. It is a list
880 * of keys, separated by newlines. They can contain whitespace, so use
881 * getline(3) instead of lexer used for maps.
883 static void
884 parse_map_keys_yyin(struct node *parent, const char *map)
886 char *line = NULL, *key;
887 size_t linecap = 0;
888 ssize_t linelen;
890 lineno = 1;
892 for (;;) {
893 linelen = getline(&line, &linecap, yyin);
894 if (linelen < 0) {
896 * End of file.
898 break;
900 if (linelen <= 1) {
902 * Empty line, consisting of just the newline.
904 continue;
908 * "-1" to strip the trailing newline.
910 key = strndup(line, linelen - 1);
912 log_debugx("adding key \"%s\"", key);
913 node_new(parent, key, NULL, NULL, map, lineno);
914 lineno++;
916 free(line);
919 static bool
920 file_is_executable(const char *path)
922 struct stat sb;
923 int error;
925 error = stat(path, &sb);
926 if (error != 0)
927 log_err(1, "cannot stat %s", path);
928 if ((sb.st_mode & S_IXUSR) || (sb.st_mode & S_IXGRP) ||
929 (sb.st_mode & S_IXOTH))
930 return (true);
931 return (false);
935 * Parse a special map, e.g. "-hosts".
937 static void
938 parse_special_map(struct node *parent, const char *map, const char *key)
940 char *path;
941 int error, ret;
943 assert(map[0] == '-');
946 * +1 to skip leading "-" in map name.
948 ret = asprintf(&path, "%s/special_%s", AUTO_SPECIAL_PREFIX, map + 1);
949 if (ret < 0)
950 log_err(1, "asprintf");
952 yyin = auto_popen(path, key, NULL);
953 assert(yyin != NULL);
955 if (key == NULL) {
956 parse_map_keys_yyin(parent, map);
957 } else {
958 parse_map_yyin(parent, map, key);
961 error = auto_pclose(yyin);
962 yyin = NULL;
963 if (error != 0)
964 log_errx(1, "failed to handle special map \"%s\"", map);
966 node_expand_includes(parent, false);
967 node_expand_direct_maps(parent);
969 free(path);
973 * Retrieve and parse map from directory services, e.g. LDAP.
974 * Note that it is different from executable maps, in that
975 * the include script outputs the whole map to standard output
976 * (as opposed to executable maps that only output a single
977 * entry, without the key), and it takes the map name as an
978 * argument, instead of key.
980 static void
981 parse_included_map(struct node *parent, const char *map)
983 int error;
985 assert(map[0] != '-');
986 assert(map[0] != '/');
988 error = access(AUTO_INCLUDE_PATH, F_OK);
989 if (error != 0) {
990 log_errx(1, "directory services not configured;"
991 " %s does not exist", AUTO_INCLUDE_PATH);
994 yyin = auto_popen(AUTO_INCLUDE_PATH, map, NULL);
995 assert(yyin != NULL);
997 parse_map_yyin(parent, map, NULL);
999 error = auto_pclose(yyin);
1000 yyin = NULL;
1001 if (error != 0)
1002 log_errx(1, "failed to handle remote map \"%s\"", map);
1004 node_expand_includes(parent, false);
1005 node_expand_direct_maps(parent);
1008 void
1009 parse_map(struct node *parent, const char *map, const char *key,
1010 bool *wildcards)
1012 char *path = NULL;
1013 int error, ret;
1014 bool executable;
1016 assert(map != NULL);
1017 assert(map[0] != '\0');
1019 log_debugx("parsing map \"%s\"", map);
1021 if (wildcards != NULL)
1022 *wildcards = false;
1024 if (map[0] == '-') {
1025 if (wildcards != NULL)
1026 *wildcards = true;
1027 return (parse_special_map(parent, map, key));
1030 if (map[0] == '/') {
1031 path = checked_strdup(map);
1032 } else {
1033 ret = asprintf(&path, "%s/%s", AUTO_MAP_PREFIX, map);
1034 if (ret < 0)
1035 log_err(1, "asprintf");
1036 log_debugx("map \"%s\" maps to \"%s\"", map, path);
1039 * See if the file exists. If not, try to obtain the map
1040 * from directory services.
1042 error = access(path, F_OK);
1043 if (error != 0) {
1044 log_debugx("map file \"%s\" does not exist; falling "
1045 "back to directory services", path);
1046 return (parse_included_map(parent, map));
1050 executable = file_is_executable(path);
1052 if (executable) {
1053 log_debugx("map \"%s\" is executable", map);
1055 if (wildcards != NULL)
1056 *wildcards = true;
1058 if (key != NULL) {
1059 yyin = auto_popen(path, key, NULL);
1060 } else {
1061 yyin = auto_popen(path, NULL);
1063 assert(yyin != NULL);
1064 } else {
1065 yyin = fopen(path, "r");
1066 if (yyin == NULL)
1067 log_err(1, "unable to open \"%s\"", path);
1070 free(path);
1071 path = NULL;
1073 parse_map_yyin(parent, map, executable ? key : NULL);
1075 if (executable) {
1076 error = auto_pclose(yyin);
1077 yyin = NULL;
1078 if (error != 0) {
1079 log_errx(1, "failed to handle executable map \"%s\"",
1080 map);
1082 } else {
1083 fclose(yyin);
1085 yyin = NULL;
1087 log_debugx("done parsing map \"%s\"", map);
1089 node_expand_includes(parent, false);
1090 node_expand_direct_maps(parent);
1093 static void
1094 parse_master_yyin(struct node *root, const char *master)
1096 char *mountpoint = NULL, *map = NULL, *options = NULL;
1097 int ret;
1100 * XXX: 1 gives incorrect values; wtf?
1102 lineno = 0;
1104 for (;;) {
1105 ret = yylex();
1106 if (ret == 0 || ret == NEWLINE) {
1107 if (mountpoint != NULL) {
1108 //log_debugx("adding map for %s", mountpoint);
1109 node_new_map(root, mountpoint, options, map,
1110 master, lineno);
1112 if (ret == 0) {
1113 break;
1114 } else {
1115 mountpoint = map = options = NULL;
1116 continue;
1119 if (mountpoint == NULL) {
1120 mountpoint = checked_strdup(yytext);
1121 } else if (map == NULL) {
1122 map = checked_strdup(yytext);
1123 } else if (options == NULL) {
1125 * +1 to skip leading "-".
1127 options = checked_strdup(yytext + 1);
1128 } else {
1129 log_errx(1, "too many arguments at %s, line %d",
1130 master, lineno);
1135 void
1136 parse_master(struct node *root, const char *master)
1139 log_debugx("parsing auto_master file at \"%s\"", master);
1141 yyin = fopen(master, "r");
1142 if (yyin == NULL)
1143 err(1, "unable to open %s", master);
1145 parse_master_yyin(root, master);
1147 fclose(yyin);
1148 yyin = NULL;
1150 log_debugx("done parsing \"%s\"", master);
1152 node_expand_includes(root, true);
1153 node_expand_direct_maps(root);
1157 * Two things daemon(3) does, that we actually also want to do
1158 * when running in foreground, is closing the stdin and chdiring
1159 * to "/". This is what we do here.
1161 void
1162 lesser_daemon(void)
1164 int error, fd;
1166 error = chdir("/");
1167 if (error != 0)
1168 log_warn("chdir");
1170 fd = open(_PATH_DEVNULL, O_RDWR, 0);
1171 if (fd < 0) {
1172 log_warn("cannot open %s", _PATH_DEVNULL);
1173 return;
1176 error = dup2(fd, STDIN_FILENO);
1177 if (error != 0)
1178 log_warn("dup2");
1180 error = close(fd);
1181 if (error != 0) {
1182 /* Bloody hell. */
1183 log_warn("close");
1188 main(int argc, char **argv)
1190 char *cmdname;
1192 if (argv[0] == NULL)
1193 log_errx(1, "NULL command name");
1195 cmdname = basename(argv[0]);
1197 if (strcmp(cmdname, "automount") == 0)
1198 return (main_automount(argc, argv));
1199 else if (strcmp(cmdname, "automountd") == 0)
1200 main_automountd(argc, argv);
1201 else if (strcmp(cmdname, "autounmountd") == 0)
1202 main_autounmountd(argc, argv);
1203 else
1204 log_errx(1, "binary name should be either \"automount\", "
1205 "\"automountd\", or \"autounmountd\"");