common.c revision 279744
1/*-
2 * Copyright (c) 2014 The FreeBSD Foundation
3 * All rights reserved.
4 *
5 * This software was developed by Edward Tomasz Napierala under sponsorship
6 * from the FreeBSD Foundation.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
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.
16 *
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
27 * SUCH DAMAGE.
28 *
29 */
30
31#include <sys/cdefs.h>
32__FBSDID("$FreeBSD: stable/10/usr.sbin/autofs/common.c 279744 2015-03-07 19:41:58Z trasz $");
33
34#include <sys/types.h>
35#include <sys/time.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>
41#include <sys/stat.h>
42#include <sys/wait.h>
43#include <sys/utsname.h>
44#include <assert.h>
45#include <ctype.h>
46#include <err.h>
47#include <errno.h>
48#include <fcntl.h>
49#include <libgen.h>
50#include <netdb.h>
51#include <paths.h>
52#include <signal.h>
53#include <stdbool.h>
54#include <stdint.h>
55#define	_WITH_GETLINE
56#include <stdio.h>
57#include <stdlib.h>
58#include <string.h>
59#include <unistd.h>
60
61#include <libutil.h>
62
63#include "autofs_ioctl.h"
64
65#include "common.h"
66
67extern FILE *yyin;
68extern char *yytext;
69extern int yylex(void);
70
71static void	parse_master_yyin(struct node *root, const char *master);
72static void	parse_map_yyin(struct node *parent, const char *map,
73		    const char *executable_key);
74
75char *
76checked_strdup(const char *s)
77{
78	char *c;
79
80	assert(s != NULL);
81
82	c = strdup(s);
83	if (c == NULL)
84		log_err(1, "strdup");
85	return (c);
86}
87
88/*
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.
92 *
93 * Concatenate pathnames, basically.
94 */
95static void
96concat(char **p1, char **p2)
97{
98	int ret;
99	char *path;
100
101	assert(p1 != NULL);
102	assert(p2 != NULL);
103
104	if (*p1 == NULL)
105		*p1 = checked_strdup("");
106
107	if (*p2 == NULL)
108		*p2 = checked_strdup("");
109
110	ret = asprintf(&path, "%s/%s", *p1, *p2);
111	if (ret < 0)
112		log_err(1, "asprintf");
113
114	/*
115	 * XXX
116	 */
117	//free(*p1);
118	//free(*p2);
119
120	*p1 = path;
121	*p2 = NULL;
122}
123
124/*
125 * Concatenate two strings, inserting separator between them, unless not needed.
126 *
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.
129 */
130char *
131separated_concat(const char *s1, const char *s2, char separator)
132{
133	char *result;
134	int ret;
135
136	assert(s1 != NULL);
137	assert(s2 != NULL);
138
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);
142	} else {
143		ret = asprintf(&result, "%s%c%s", s1, separator, s2);
144	}
145	if (ret < 0)
146		log_err(1, "asprintf");
147
148	//log_debugx("separated_concat: got %s and %s, returning %s", s1, s2, result);
149
150	return (result);
151}
152
153void
154create_directory(const char *path)
155{
156	char *component, *copy, *tofree, *partial;
157	int error;
158
159	assert(path[0] == '/');
160
161	/*
162	 * +1 to skip the leading slash.
163	 */
164	copy = tofree = checked_strdup(path + 1);
165
166	partial = NULL;
167	for (;;) {
168		component = strsep(&copy, "/");
169		if (component == NULL)
170			break;
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);
176			return;
177		}
178	}
179
180	free(tofree);
181}
182
183struct node *
184node_new_root(void)
185{
186	struct node *n;
187
188	n = calloc(1, sizeof(*n));
189	if (n == NULL)
190		log_err(1, "calloc");
191	// XXX
192	n->n_key = checked_strdup("/");
193	n->n_options = checked_strdup("");
194
195	TAILQ_INIT(&n->n_children);
196
197	return (n);
198}
199
200struct node *
201node_new(struct node *parent, char *key, char *options, char *location,
202    const char *config_file, int config_line)
203{
204	struct node *n;
205
206	n = calloc(1, sizeof(*n));
207	if (n == NULL)
208		log_err(1, "calloc");
209
210	TAILQ_INIT(&n->n_children);
211	assert(key != NULL);
212	assert(key[0] != '\0');
213	n->n_key = key;
214	if (options != NULL)
215		n->n_options = options;
216	else
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;
223
224	assert(parent != NULL);
225	n->n_parent = parent;
226	TAILQ_INSERT_TAIL(&parent->n_children, n, n_next);
227
228	return (n);
229}
230
231struct node *
232node_new_map(struct node *parent, char *key, char *options, char *map,
233    const char *config_file, int config_line)
234{
235	struct node *n;
236
237	n = calloc(1, sizeof(*n));
238	if (n == NULL)
239		log_err(1, "calloc");
240
241	TAILQ_INIT(&n->n_children);
242	assert(key != NULL);
243	assert(key[0] != '\0');
244	n->n_key = key;
245	if (options != NULL)
246		n->n_options = options;
247	else
248		n->n_options = strdup("");
249	n->n_map = map;
250	assert(config_file != NULL);
251	n->n_config_file = config_file;
252	assert(config_line >= 0);
253	n->n_config_line = config_line;
254
255	assert(parent != NULL);
256	n->n_parent = parent;
257	TAILQ_INSERT_TAIL(&parent->n_children, n, n_next);
258
259	return (n);
260}
261
262static struct node *
263node_duplicate(const struct node *o, struct node *parent)
264{
265	const struct node *child;
266	struct node *n;
267
268	if (parent == NULL)
269		parent = o->n_parent;
270
271	n = node_new(parent, o->n_key, o->n_options, o->n_location,
272	    o->n_config_file, o->n_config_line);
273
274	TAILQ_FOREACH(child, &o->n_children, n_next)
275		node_duplicate(child, n);
276
277	return (n);
278}
279
280static void
281node_delete(struct node *n)
282{
283	struct node *child, *tmp;
284
285	assert (n != NULL);
286
287	TAILQ_FOREACH_SAFE(child, &n->n_children, n_next, tmp)
288		node_delete(child);
289
290	if (n->n_parent != NULL)
291		TAILQ_REMOVE(&n->n_parent->n_children, n, n_next);
292
293	free(n);
294}
295
296/*
297 * Move (reparent) node 'n' to make it sibling of 'previous', placed
298 * just after it.
299 */
300static void
301node_move_after(struct node *n, struct node *previous)
302{
303
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);
307}
308
309static void
310node_expand_includes(struct node *root, bool is_master)
311{
312	struct node *n, *n2, *tmp, *tmp2, *tmproot;
313	int error;
314
315	TAILQ_FOREACH_SAFE(n, &root->n_children, n_next, tmp) {
316		if (n->n_key[0] != '+')
317			continue;
318
319		error = access(AUTO_INCLUDE_PATH, F_OK);
320		if (error != 0) {
321			log_errx(1, "directory services not configured; "
322			    "%s does not exist", AUTO_INCLUDE_PATH);
323		}
324
325		/*
326		 * "+1" to skip leading "+".
327		 */
328		yyin = auto_popen(AUTO_INCLUDE_PATH, n->n_key + 1, NULL);
329		assert(yyin != NULL);
330
331		tmproot = node_new_root();
332		if (is_master)
333			parse_master_yyin(tmproot, n->n_key);
334		else
335			parse_map_yyin(tmproot, n->n_key, NULL);
336
337		error = auto_pclose(yyin);
338		yyin = NULL;
339		if (error != 0) {
340			log_errx(1, "failed to handle include \"%s\"",
341			    n->n_key);
342		}
343
344		/*
345		 * Entries to be included are now in tmproot.  We need to merge
346		 * them with the rest, preserving their place and ordering.
347		 */
348		TAILQ_FOREACH_REVERSE_SAFE(n2,
349		    &tmproot->n_children, nodehead, n_next, tmp2) {
350			node_move_after(n2, n);
351		}
352
353		node_delete(n);
354		node_delete(tmproot);
355	}
356}
357
358static char *
359expand_ampersand(char *string, const char *key)
360{
361	char c, *expanded;
362	int i, ret, before_len = 0;
363	bool backslashed = false;
364
365	assert(key[0] != '\0');
366
367	expanded = checked_strdup(string);
368
369	for (i = 0; string[i] != '\0'; i++) {
370		c = string[i];
371		if (c == '\\' && backslashed == false) {
372			backslashed = true;
373			continue;
374		}
375		if (backslashed) {
376			backslashed = false;
377			continue;
378		}
379		backslashed = false;
380		if (c != '&')
381			continue;
382
383		/*
384		 * The 'before_len' variable contains the number
385		 * of characters before the '&'.
386		 */
387		before_len = i;
388		//assert(i + 1 < (int)strlen(string));
389
390		ret = asprintf(&expanded, "%.*s%s%s",
391		    before_len, string, key, string + before_len + 1);
392		if (ret < 0)
393			log_err(1, "asprintf");
394
395		//log_debugx("\"%s\" expanded with key \"%s\" to \"%s\"",
396		//    string, key, expanded);
397
398		/*
399		 * Figure out where to start searching for next variable.
400		 */
401		string = expanded;
402		i = before_len + strlen(key);
403		backslashed = false;
404		//assert(i < (int)strlen(string));
405	}
406
407	return (expanded);
408}
409
410/*
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
414 * level up.
415 *
416 * Variant with NULL key is for "automount -LL".
417 */
418void
419node_expand_ampersand(struct node *n, const char *key)
420{
421	struct node *child;
422
423	if (n->n_location != NULL) {
424		if (key == NULL) {
425			if (n->n_parent != NULL &&
426			    strcmp(n->n_parent->n_key, "*") != 0) {
427				n->n_location = expand_ampersand(n->n_location,
428				    n->n_parent->n_key);
429			}
430		} else {
431			n->n_location = expand_ampersand(n->n_location, key);
432		}
433	}
434
435	TAILQ_FOREACH(child, &n->n_children, n_next)
436		node_expand_ampersand(child, key);
437}
438
439/*
440 * Expand "*" in n_key.
441 */
442void
443node_expand_wildcard(struct node *n, const char *key)
444{
445	struct node *child, *expanded;
446
447	assert(key != NULL);
448
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);
453	}
454
455	TAILQ_FOREACH(child, &n->n_children, n_next)
456		node_expand_wildcard(child, key);
457}
458
459int
460node_expand_defined(struct node *n)
461{
462	struct node *child;
463	int error, cumulated_error = 0;
464
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",
469			    node_path(n));
470			return (EINVAL);
471		}
472	}
473
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;
478	}
479
480	return (cumulated_error);
481}
482
483bool
484node_is_direct_map(const struct node *n)
485{
486
487	for (;;) {
488		assert(n->n_parent != NULL);
489		if (n->n_parent->n_parent == NULL)
490			break;
491		n = n->n_parent;
492	}
493
494	assert(n->n_key != NULL);
495	if (strcmp(n->n_key, "/-") != 0)
496		return (false);
497
498	return (true);
499}
500
501bool
502node_has_wildcards(const struct node *n)
503{
504	const struct node *child;
505
506	TAILQ_FOREACH(child, &n->n_children, n_next) {
507		if (strcmp(child->n_key, "*") == 0)
508			return (true);
509	}
510
511	return (false);
512}
513
514static void
515node_expand_maps(struct node *n, bool indirect)
516{
517	struct node *child, *tmp;
518
519	TAILQ_FOREACH_SAFE(child, &n->n_children, n_next, tmp) {
520		if (node_is_direct_map(child)) {
521			if (indirect)
522				continue;
523		} else {
524			if (indirect == false)
525				continue;
526		}
527
528		/*
529		 * This is the first-level map node; the one that contains
530		 * the key and subnodes with mountpoints and actual map names.
531		 */
532		if (child->n_map == NULL)
533			continue;
534
535		if (indirect) {
536			log_debugx("map \"%s\" is an indirect map, parsing",
537			    child->n_map);
538		} else {
539			log_debugx("map \"%s\" is a direct map, parsing",
540			    child->n_map);
541		}
542		parse_map(child, child->n_map, NULL, NULL);
543	}
544}
545
546static void
547node_expand_direct_maps(struct node *n)
548{
549
550	node_expand_maps(n, false);
551}
552
553void
554node_expand_indirect_maps(struct node *n)
555{
556
557	node_expand_maps(n, true);
558}
559
560static char *
561node_path_x(const struct node *n, char *x)
562{
563	char *path;
564	size_t len;
565
566	if (n->n_parent == NULL)
567		return (x);
568
569	/*
570	 * Return "/-" for direct maps only if we were asked for path
571	 * to the "/-" node itself, not to any of its subnodes.
572	 */
573	if (n->n_parent->n_parent == NULL &&
574	    strcmp(n->n_key, "/-") == 0 &&
575	    x[0] != '\0') {
576		return (x);
577	}
578
579	assert(n->n_key[0] != '\0');
580	path = separated_concat(n->n_key, x, '/');
581	free(x);
582
583	/*
584	 * Strip trailing slash.
585	 */
586	len = strlen(path);
587	assert(len > 0);
588	if (path[len - 1] == '/')
589		path[len - 1] = '\0';
590
591	return (node_path_x(n->n_parent, path));
592}
593
594/*
595 * Return full path for node, consisting of concatenated
596 * paths of node itself and all its parents, up to the root.
597 */
598char *
599node_path(const struct node *n)
600{
601
602	return (node_path_x(n, checked_strdup("")));
603}
604
605static char *
606node_options_x(const struct node *n, char *x)
607{
608	char *options;
609
610	options = separated_concat(x, n->n_options, ',');
611	if (n->n_parent == NULL)
612		return (options);
613
614	return (node_options_x(n->n_parent, options));
615}
616
617/*
618 * Return options for node, consisting of concatenated
619 * options from the node itself and all its parents,
620 * up to the root.
621 */
622char *
623node_options(const struct node *n)
624{
625
626	return (node_options_x(n, checked_strdup("")));
627}
628
629static void
630node_print_indent(const struct node *n, int indent)
631{
632	const struct node *child, *first_child;
633	char *path, *options;
634
635	path = node_path(n);
636	options = node_options(n);
637
638	/*
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.
644	 */
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",
650		    indent, "",
651		    25 - indent,
652		    path,
653		    options[0] != '\0' ? "-" : " ",
654		    20,
655		    options[0] != '\0' ? options : "",
656		    20,
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);
661	}
662
663	free(path);
664	free(options);
665
666	TAILQ_FOREACH(child, &n->n_children, n_next)
667		node_print_indent(child, indent + 2);
668}
669
670void
671node_print(const struct node *n)
672{
673	const struct node *child;
674
675	TAILQ_FOREACH(child, &n->n_children, n_next)
676		node_print_indent(child, 0);
677}
678
679static struct node *
680node_find_x(struct node *node, const char *path)
681{
682	struct node *child, *found;
683	char *tmp;
684	size_t tmplen;
685
686	//log_debugx("looking up %s in %s", path, node->n_key);
687
688	tmp = node_path(node);
689	tmplen = strlen(tmp);
690	if (strncmp(tmp, path, tmplen) != 0) {
691		free(tmp);
692		return (NULL);
693	}
694	if (path[tmplen] != '/' && path[tmplen] != '\0') {
695		/*
696		 * If we have two map entries like 'foo' and 'foobar', make
697		 * sure the search for 'foobar' won't match 'foo' instead.
698		 */
699		free(tmp);
700		return (NULL);
701	}
702	free(tmp);
703
704	TAILQ_FOREACH(child, &node->n_children, n_next) {
705		found = node_find_x(child, path);
706		if (found != NULL)
707			return (found);
708	}
709
710	return (node);
711}
712
713struct node *
714node_find(struct node *root, const char *path)
715{
716	struct node *node;
717
718	node = node_find_x(root, path);
719	if (node == root)
720		return (NULL);
721	return (node);
722}
723
724/*
725 * Canonical form of a map entry looks like this:
726 *
727 * key [-options] [ [/mountpoint] [-options2] location ... ]
728 *
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.
732 *
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.
735 */
736static void
737parse_map_yyin(struct node *parent, const char *map, const char *executable_key)
738{
739	char *key = NULL, *options = NULL, *mountpoint = NULL,
740	    *options2 = NULL, *location = NULL;
741	int ret;
742	struct node *node;
743
744	lineno = 1;
745
746	if (executable_key != NULL)
747		key = checked_strdup(executable_key);
748
749	for (;;) {
750		ret = yylex();
751		if (ret == 0 || ret == NEWLINE) {
752			/*
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.
756			 */
757			if ((key != NULL && executable_key == NULL) ||
758			    options != NULL) {
759				log_errx(1, "truncated entry at %s, line %d",
760				    map, lineno);
761			}
762			if (ret == 0 || executable_key != NULL) {
763				/*
764				 * End of file.
765				 */
766				break;
767			} else {
768				key = options = NULL;
769				continue;
770			}
771		}
772		if (key == NULL) {
773			key = checked_strdup(yytext);
774			if (key[0] == '+') {
775				node_new(parent, key, NULL, NULL, map, lineno);
776				key = options = NULL;
777				continue;
778			}
779			continue;
780		} else if (yytext[0] == '-') {
781			if (options != NULL) {
782				log_errx(1, "duplicated options at %s, line %d",
783				    map, lineno);
784			}
785			/*
786			 * +1 to skip leading "-".
787			 */
788			options = checked_strdup(yytext + 1);
789			continue;
790		}
791
792		/*
793		 * We cannot properly handle a situation where the map key
794		 * is "/".  Ignore such entries.
795		 *
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
799		 *	"*" for "/"?
800		 */
801		if (strcmp(key, "/") == 0) {
802			log_warnx("nonsensical map key \"/\" at %s, line %d; "
803			    "ignoring map entry ", map, lineno);
804
805			/*
806			 * Skip the rest of the entry.
807			 */
808			do {
809				ret = yylex();
810			} while (ret != 0 && ret != NEWLINE);
811
812			key = options = NULL;
813			continue;
814		}
815
816		//log_debugx("adding map node, %s", key);
817		node = node_new(parent, key, options, NULL, map, lineno);
818		key = options = NULL;
819
820		for (;;) {
821			if (yytext[0] == '/') {
822				if (mountpoint != NULL) {
823					log_errx(1, "duplicated mountpoint "
824					    "in %s, line %d", map, lineno);
825				}
826				if (options2 != NULL || location != NULL) {
827					log_errx(1, "mountpoint out of order "
828					    "in %s, line %d", map, lineno);
829				}
830				mountpoint = checked_strdup(yytext);
831				goto again;
832			}
833
834			if (yytext[0] == '-') {
835				if (options2 != NULL) {
836					log_errx(1, "duplicated options "
837					    "in %s, line %d", map, lineno);
838				}
839				if (location != NULL) {
840					log_errx(1, "options out of order "
841					    "in %s, line %d", map, lineno);
842				}
843				options2 = checked_strdup(yytext + 1);
844				goto again;
845			}
846
847			if (location != NULL) {
848				log_errx(1, "too many arguments "
849				    "in %s, line %d", map, lineno);
850			}
851
852			/*
853			 * If location field starts with colon, e.g. ":/dev/cd0",
854			 * then strip it.
855			 */
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);
861				}
862			} else {
863				location = checked_strdup(yytext);
864			}
865
866			if (mountpoint == NULL)
867				mountpoint = checked_strdup("/");
868			if (options2 == NULL)
869				options2 = checked_strdup("");
870
871#if 0
872			log_debugx("adding map node, %s %s %s",
873			    mountpoint, options2, location);
874#endif
875			node_new(node, mountpoint, options2, location,
876			    map, lineno);
877			mountpoint = options2 = location = NULL;
878again:
879			ret = yylex();
880			if (ret == 0 || ret == NEWLINE) {
881				if (mountpoint != NULL || options2 != NULL ||
882				    location != NULL) {
883					log_errx(1, "truncated entry "
884					    "in %s, line %d", map, lineno);
885				}
886				break;
887			}
888		}
889	}
890}
891
892/*
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.
896 */
897static void
898parse_map_keys_yyin(struct node *parent, const char *map)
899{
900	char *line = NULL, *key;
901	size_t linecap = 0;
902	ssize_t linelen;
903
904	lineno = 1;
905
906	for (;;) {
907		linelen = getline(&line, &linecap, yyin);
908		if (linelen < 0) {
909			/*
910			 * End of file.
911			 */
912			break;
913		}
914		if (linelen <= 1) {
915			/*
916			 * Empty line, consisting of just the newline.
917			 */
918			continue;
919		}
920
921		/*
922		 * "-1" to strip the trailing newline.
923		 */
924		key = strndup(line, linelen - 1);
925
926		log_debugx("adding key \"%s\"", key);
927		node_new(parent, key, NULL, NULL, map, lineno);
928		lineno++;
929	}
930	free(line);
931}
932
933static bool
934file_is_executable(const char *path)
935{
936	struct stat sb;
937	int error;
938
939	error = stat(path, &sb);
940	if (error != 0)
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))
944		return (true);
945	return (false);
946}
947
948/*
949 * Parse a special map, e.g. "-hosts".
950 */
951static void
952parse_special_map(struct node *parent, const char *map, const char *key)
953{
954	char *path;
955	int error, ret;
956
957	assert(map[0] == '-');
958
959	/*
960	 * +1 to skip leading "-" in map name.
961	 */
962	ret = asprintf(&path, "%s/special_%s", AUTO_SPECIAL_PREFIX, map + 1);
963	if (ret < 0)
964		log_err(1, "asprintf");
965
966	yyin = auto_popen(path, key, NULL);
967	assert(yyin != NULL);
968
969	if (key == NULL) {
970		parse_map_keys_yyin(parent, map);
971	} else {
972		parse_map_yyin(parent, map, key);
973	}
974
975	error = auto_pclose(yyin);
976	yyin = NULL;
977	if (error != 0)
978		log_errx(1, "failed to handle special map \"%s\"", map);
979
980	node_expand_includes(parent, false);
981	node_expand_direct_maps(parent);
982
983	free(path);
984}
985
986/*
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.
993 */
994static void
995parse_included_map(struct node *parent, const char *map)
996{
997	int error;
998
999	assert(map[0] != '-');
1000	assert(map[0] != '/');
1001
1002	error = access(AUTO_INCLUDE_PATH, F_OK);
1003	if (error != 0) {
1004		log_errx(1, "directory services not configured;"
1005		    " %s does not exist", AUTO_INCLUDE_PATH);
1006	}
1007
1008	yyin = auto_popen(AUTO_INCLUDE_PATH, map, NULL);
1009	assert(yyin != NULL);
1010
1011	parse_map_yyin(parent, map, NULL);
1012
1013	error = auto_pclose(yyin);
1014	yyin = NULL;
1015	if (error != 0)
1016		log_errx(1, "failed to handle remote map \"%s\"", map);
1017
1018	node_expand_includes(parent, false);
1019	node_expand_direct_maps(parent);
1020}
1021
1022void
1023parse_map(struct node *parent, const char *map, const char *key,
1024    bool *wildcards)
1025{
1026	char *path = NULL;
1027	int error, ret;
1028	bool executable;
1029
1030	assert(map != NULL);
1031	assert(map[0] != '\0');
1032
1033	log_debugx("parsing map \"%s\"", map);
1034
1035	if (wildcards != NULL)
1036		*wildcards = false;
1037
1038	if (map[0] == '-') {
1039		if (wildcards != NULL)
1040			*wildcards = true;
1041		return (parse_special_map(parent, map, key));
1042	}
1043
1044	if (map[0] == '/') {
1045		path = checked_strdup(map);
1046	} else {
1047		ret = asprintf(&path, "%s/%s", AUTO_MAP_PREFIX, map);
1048		if (ret < 0)
1049			log_err(1, "asprintf");
1050		log_debugx("map \"%s\" maps to \"%s\"", map, path);
1051
1052		/*
1053		 * See if the file exists.  If not, try to obtain the map
1054		 * from directory services.
1055		 */
1056		error = access(path, F_OK);
1057		if (error != 0) {
1058			log_debugx("map file \"%s\" does not exist; falling "
1059			    "back to directory services", path);
1060			return (parse_included_map(parent, map));
1061		}
1062	}
1063
1064	executable = file_is_executable(path);
1065
1066	if (executable) {
1067		log_debugx("map \"%s\" is executable", map);
1068
1069		if (wildcards != NULL)
1070			*wildcards = true;
1071
1072		if (key != NULL) {
1073			yyin = auto_popen(path, key, NULL);
1074		} else {
1075			yyin = auto_popen(path, NULL);
1076		}
1077		assert(yyin != NULL);
1078	} else {
1079		yyin = fopen(path, "r");
1080		if (yyin == NULL)
1081			log_err(1, "unable to open \"%s\"", path);
1082	}
1083
1084	free(path);
1085	path = NULL;
1086
1087	parse_map_yyin(parent, map, executable ? key : NULL);
1088
1089	if (executable) {
1090		error = auto_pclose(yyin);
1091		yyin = NULL;
1092		if (error != 0) {
1093			log_errx(1, "failed to handle executable map \"%s\"",
1094			    map);
1095		}
1096	} else {
1097		fclose(yyin);
1098	}
1099	yyin = NULL;
1100
1101	log_debugx("done parsing map \"%s\"", map);
1102
1103	node_expand_includes(parent, false);
1104	node_expand_direct_maps(parent);
1105}
1106
1107static void
1108parse_master_yyin(struct node *root, const char *master)
1109{
1110	char *mountpoint = NULL, *map = NULL, *options = NULL;
1111	int ret;
1112
1113	/*
1114	 * XXX: 1 gives incorrect values; wtf?
1115	 */
1116	lineno = 0;
1117
1118	for (;;) {
1119		ret = yylex();
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,
1124				    master, lineno);
1125			}
1126			if (ret == 0) {
1127				break;
1128			} else {
1129				mountpoint = map = options = NULL;
1130				continue;
1131			}
1132		}
1133		if (mountpoint == NULL) {
1134			mountpoint = checked_strdup(yytext);
1135		} else if (map == NULL) {
1136			map = checked_strdup(yytext);
1137		} else if (options == NULL) {
1138			/*
1139			 * +1 to skip leading "-".
1140			 */
1141			options = checked_strdup(yytext + 1);
1142		} else {
1143			log_errx(1, "too many arguments at %s, line %d",
1144			    master, lineno);
1145		}
1146	}
1147}
1148
1149void
1150parse_master(struct node *root, const char *master)
1151{
1152
1153	log_debugx("parsing auto_master file at \"%s\"", master);
1154
1155	yyin = fopen(master, "r");
1156	if (yyin == NULL)
1157		err(1, "unable to open %s", master);
1158
1159	parse_master_yyin(root, master);
1160
1161	fclose(yyin);
1162	yyin = NULL;
1163
1164	log_debugx("done parsing \"%s\"", master);
1165
1166	node_expand_includes(root, true);
1167	node_expand_direct_maps(root);
1168}
1169
1170/*
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.
1174 */
1175void
1176lesser_daemon(void)
1177{
1178	int error, fd;
1179
1180	error = chdir("/");
1181	if (error != 0)
1182		log_warn("chdir");
1183
1184	fd = open(_PATH_DEVNULL, O_RDWR, 0);
1185	if (fd < 0) {
1186		log_warn("cannot open %s", _PATH_DEVNULL);
1187		return;
1188	}
1189
1190	error = dup2(fd, STDIN_FILENO);
1191	if (error != 0)
1192		log_warn("dup2");
1193
1194	error = close(fd);
1195	if (error != 0) {
1196		/* Bloody hell. */
1197		log_warn("close");
1198	}
1199}
1200
1201int
1202main(int argc, char **argv)
1203{
1204	char *cmdname;
1205
1206	if (argv[0] == NULL)
1207		log_errx(1, "NULL command name");
1208
1209	cmdname = basename(argv[0]);
1210
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));
1217	else
1218		log_errx(1, "binary name should be either \"automount\", "
1219		    "\"automountd\", or \"autounmountd\"");
1220}
1221