1*5628820aStkusumi /* $NetBSD: common.c,v 1.5 2022/05/04 11:27:54 tkusumi Exp $ */
2b985414bSchristos
3b985414bSchristos /*-
4b985414bSchristos * Copyright (c) 2017 The NetBSD Foundation, Inc.
5b985414bSchristos * Copyright (c) 2016 The DragonFly Project
6b985414bSchristos * Copyright (c) 2014 The FreeBSD Foundation
7b985414bSchristos * All rights reserved.
8b985414bSchristos *
9b985414bSchristos * This code is derived from software contributed to The NetBSD Foundation
10b985414bSchristos * by Tomohiro Kusumi <kusumi.tomohiro@gmail.com>.
11b985414bSchristos *
12b985414bSchristos * This software was developed by Edward Tomasz Napierala under sponsorship
13b985414bSchristos * from the FreeBSD Foundation.
14b985414bSchristos *
15b985414bSchristos * Redistribution and use in source and binary forms, with or without
16b985414bSchristos * modification, are permitted provided that the following conditions
17b985414bSchristos * are met:
18b985414bSchristos * 1. Redistributions of source code must retain the above copyright
19b985414bSchristos * notice, this list of conditions and the following disclaimer.
20b985414bSchristos * 2. Redistributions in binary form must reproduce the above copyright
21b985414bSchristos * notice, this list of conditions and the following disclaimer in the
22b985414bSchristos * documentation and/or other materials provided with the distribution.
23b985414bSchristos *
24b985414bSchristos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
25b985414bSchristos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26b985414bSchristos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27b985414bSchristos * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
28b985414bSchristos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29b985414bSchristos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30b985414bSchristos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31b985414bSchristos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32b985414bSchristos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33b985414bSchristos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34b985414bSchristos * SUCH DAMAGE.
35b985414bSchristos *
36b985414bSchristos * $FreeBSD: head/usr.sbin/autofs/common.c 303527 2016-07-30 01:10:05Z bapt $
37b985414bSchristos */
38b985414bSchristos #include <sys/cdefs.h>
39*5628820aStkusumi __RCSID("$NetBSD: common.c,v 1.5 2022/05/04 11:27:54 tkusumi Exp $");
40b985414bSchristos
41b985414bSchristos #include <sys/types.h>
42b985414bSchristos #include <sys/stat.h>
43b985414bSchristos #include <assert.h>
44b985414bSchristos #include <err.h>
45b985414bSchristos #include <errno.h>
46b985414bSchristos #include <fcntl.h>
47b985414bSchristos #include <libgen.h>
48b985414bSchristos #include <paths.h>
49b985414bSchristos #include <stdio.h>
50b985414bSchristos #include <stdlib.h>
51b985414bSchristos #include <string.h>
52b985414bSchristos #include <unistd.h>
53b985414bSchristos
54b985414bSchristos #include "common.h"
55b985414bSchristos
56b985414bSchristos extern FILE *yyin;
57b985414bSchristos extern char *yytext;
58b985414bSchristos extern int yylex(void);
59b985414bSchristos
60b985414bSchristos static void parse_master_yyin(struct node *root, const char *master);
61b985414bSchristos static void parse_map_yyin(struct node *parent, const char *map,
62b985414bSchristos const char *executable_key);
63b985414bSchristos
64b985414bSchristos char *
checked_strdup(const char * s)65b985414bSchristos checked_strdup(const char *s)
66b985414bSchristos {
67b985414bSchristos char *c;
68b985414bSchristos
69b985414bSchristos assert(s != NULL);
70b985414bSchristos
71b985414bSchristos c = strdup(s);
72b985414bSchristos if (c == NULL)
73b985414bSchristos log_err(1, "strdup");
74b985414bSchristos return c;
75b985414bSchristos }
76b985414bSchristos
77b985414bSchristos /*
78b985414bSchristos * Concatenate two strings, inserting separator between them, unless not needed.
79b985414bSchristos */
80b985414bSchristos char *
concat(const char * s1,char separator,const char * s2)81b985414bSchristos concat(const char *s1, char separator, const char *s2)
82b985414bSchristos {
83b985414bSchristos char *result;
84b985414bSchristos char s1last, s2first;
85b985414bSchristos int ret;
86b985414bSchristos
87b985414bSchristos if (s1 == NULL)
88b985414bSchristos s1 = "";
89b985414bSchristos if (s2 == NULL)
90b985414bSchristos s2 = "";
91b985414bSchristos
92b985414bSchristos if (s1[0] == '\0')
93b985414bSchristos s1last = '\0';
94b985414bSchristos else
95b985414bSchristos s1last = s1[strlen(s1) - 1];
96b985414bSchristos
97b985414bSchristos s2first = s2[0];
98b985414bSchristos
99b985414bSchristos if (s1last == separator && s2first == separator) {
100b985414bSchristos /*
101b985414bSchristos * If s1 ends with the separator and s2 begins with
102b985414bSchristos * it - skip the latter; otherwise concatenating "/"
103b985414bSchristos * and "/foo" would end up returning "//foo".
104b985414bSchristos */
105b985414bSchristos ret = asprintf(&result, "%s%s", s1, s2 + 1);
106b985414bSchristos } else if (s1last == separator || s2first == separator ||
107b985414bSchristos s1[0] == '\0' || s2[0] == '\0') {
108b985414bSchristos ret = asprintf(&result, "%s%s", s1, s2);
109b985414bSchristos } else {
110b985414bSchristos ret = asprintf(&result, "%s%c%s", s1, separator, s2);
111b985414bSchristos }
112b985414bSchristos if (ret < 0)
113b985414bSchristos log_err(1, "asprintf");
114b985414bSchristos
115b985414bSchristos #if 0
116b985414bSchristos log_debugx("%s: got %s and %s, returning %s", __func__, s1, s2, result);
117b985414bSchristos #endif
118b985414bSchristos
119b985414bSchristos return result;
120b985414bSchristos }
121b985414bSchristos
122b985414bSchristos void
create_directory(const char * path)123b985414bSchristos create_directory(const char *path)
124b985414bSchristos {
125b985414bSchristos char *component, *copy, *tofree, *partial, *tmp;
126b985414bSchristos int error;
127b985414bSchristos
128b985414bSchristos assert(path[0] == '/');
129b985414bSchristos
130b985414bSchristos /*
131b985414bSchristos * +1 to skip the leading slash.
132b985414bSchristos */
133b985414bSchristos copy = tofree = checked_strdup(path + 1);
134b985414bSchristos
135*5628820aStkusumi partial = checked_strdup("/");
136b985414bSchristos for (;;) {
137b985414bSchristos component = strsep(©, "/");
138b985414bSchristos if (component == NULL)
139b985414bSchristos break;
140b985414bSchristos tmp = concat(partial, '/', component);
141b985414bSchristos free(partial);
142b985414bSchristos partial = tmp;
143b985414bSchristos //log_debugx("creating \"%s\"", partial);
144b985414bSchristos error = mkdir(partial, 0755);
145b985414bSchristos if (error != 0 && errno != EEXIST) {
146b985414bSchristos log_warn("cannot create %s", partial);
147b985414bSchristos return;
148b985414bSchristos }
149b985414bSchristos }
150b985414bSchristos
151b985414bSchristos free(tofree);
152b985414bSchristos }
153b985414bSchristos
154b985414bSchristos struct node *
node_new_root(void)155b985414bSchristos node_new_root(void)
156b985414bSchristos {
157b985414bSchristos struct node *n;
158b985414bSchristos
159b985414bSchristos n = calloc(1, sizeof(*n));
160b985414bSchristos if (n == NULL)
161b985414bSchristos log_err(1, "calloc");
162b985414bSchristos // XXX
163b985414bSchristos n->n_key = checked_strdup("/");
164b985414bSchristos n->n_options = checked_strdup("");
165b985414bSchristos
166b985414bSchristos TAILQ_INIT(&n->n_children);
167b985414bSchristos
168b985414bSchristos return n;
169b985414bSchristos }
170b985414bSchristos
171b985414bSchristos struct node *
node_new(struct node * parent,char * key,char * options,char * location,const char * config_file,int config_line)172b985414bSchristos node_new(struct node *parent, char *key, char *options, char *location,
173b985414bSchristos const char *config_file, int config_line)
174b985414bSchristos {
175b985414bSchristos struct node *n;
176b985414bSchristos
177b985414bSchristos n = calloc(1, sizeof(*n));
178b985414bSchristos if (n == NULL)
179b985414bSchristos log_err(1, "calloc");
180b985414bSchristos
181b985414bSchristos TAILQ_INIT(&n->n_children);
182b985414bSchristos assert(key != NULL);
183b985414bSchristos assert(key[0] != '\0');
184b985414bSchristos n->n_key = key;
185b985414bSchristos if (options != NULL)
186b985414bSchristos n->n_options = options;
187b985414bSchristos else
188b985414bSchristos n->n_options = strdup("");
189b985414bSchristos n->n_location = location;
190b985414bSchristos assert(config_file != NULL);
191b985414bSchristos n->n_config_file = config_file;
192b985414bSchristos assert(config_line >= 0);
193b985414bSchristos n->n_config_line = config_line;
194b985414bSchristos
195b985414bSchristos assert(parent != NULL);
196b985414bSchristos n->n_parent = parent;
197b985414bSchristos TAILQ_INSERT_TAIL(&parent->n_children, n, n_next);
198b985414bSchristos
199b985414bSchristos return n;
200b985414bSchristos }
201b985414bSchristos
202b985414bSchristos struct node *
node_new_map(struct node * parent,char * key,char * options,char * map,const char * config_file,int config_line)203b985414bSchristos node_new_map(struct node *parent, char *key, char *options, char *map,
204b985414bSchristos const char *config_file, int config_line)
205b985414bSchristos {
206b985414bSchristos struct node *n;
207b985414bSchristos
208b985414bSchristos n = calloc(1, sizeof(*n));
209b985414bSchristos if (n == NULL)
210b985414bSchristos log_err(1, "calloc");
211b985414bSchristos
212b985414bSchristos TAILQ_INIT(&n->n_children);
213b985414bSchristos assert(key != NULL);
214b985414bSchristos assert(key[0] != '\0');
215b985414bSchristos n->n_key = key;
216b985414bSchristos if (options != NULL)
217b985414bSchristos n->n_options = options;
218b985414bSchristos else
219b985414bSchristos n->n_options = strdup("");
220b985414bSchristos n->n_map = map;
221b985414bSchristos assert(config_file != NULL);
222b985414bSchristos n->n_config_file = config_file;
223b985414bSchristos assert(config_line >= 0);
224b985414bSchristos n->n_config_line = config_line;
225b985414bSchristos
226b985414bSchristos assert(parent != NULL);
227b985414bSchristos n->n_parent = parent;
228b985414bSchristos TAILQ_INSERT_TAIL(&parent->n_children, n, n_next);
229b985414bSchristos
230b985414bSchristos return n;
231b985414bSchristos }
232b985414bSchristos
233b985414bSchristos static struct node *
node_duplicate(const struct node * o,struct node * parent)234b985414bSchristos node_duplicate(const struct node *o, struct node *parent)
235b985414bSchristos {
236b985414bSchristos const struct node *child;
237b985414bSchristos struct node *n;
238b985414bSchristos
239b985414bSchristos if (parent == NULL)
240b985414bSchristos parent = o->n_parent;
241b985414bSchristos
242b985414bSchristos n = node_new(parent, o->n_key, o->n_options, o->n_location,
243b985414bSchristos o->n_config_file, o->n_config_line);
244b985414bSchristos
245b985414bSchristos TAILQ_FOREACH(child, &o->n_children, n_next)
246b985414bSchristos node_duplicate(child, n);
247b985414bSchristos
248b985414bSchristos return n;
249b985414bSchristos }
250b985414bSchristos
251b985414bSchristos static void
node_delete(struct node * n)252b985414bSchristos node_delete(struct node *n)
253b985414bSchristos {
254b985414bSchristos struct node *child, *tmp;
255b985414bSchristos
256b985414bSchristos assert (n != NULL);
257b985414bSchristos
258b985414bSchristos TAILQ_FOREACH_SAFE(child, &n->n_children, n_next, tmp)
259b985414bSchristos node_delete(child);
260b985414bSchristos
261b985414bSchristos if (n->n_parent != NULL)
262b985414bSchristos TAILQ_REMOVE(&n->n_parent->n_children, n, n_next);
263b985414bSchristos
264b985414bSchristos free(n);
265b985414bSchristos }
266b985414bSchristos
267b985414bSchristos /*
268b985414bSchristos * Move (reparent) node 'n' to make it sibling of 'previous', placed
269b985414bSchristos * just after it.
270b985414bSchristos */
271b985414bSchristos static void
node_move_after(struct node * n,struct node * previous)272b985414bSchristos node_move_after(struct node *n, struct node *previous)
273b985414bSchristos {
274b985414bSchristos
275b985414bSchristos TAILQ_REMOVE(&n->n_parent->n_children, n, n_next);
276b985414bSchristos n->n_parent = previous->n_parent;
277b985414bSchristos TAILQ_INSERT_AFTER(&previous->n_parent->n_children, previous, n, n_next);
278b985414bSchristos }
279b985414bSchristos
280b985414bSchristos static void
node_expand_includes(struct node * root,bool is_master)281b985414bSchristos node_expand_includes(struct node *root, bool is_master)
282b985414bSchristos {
283b985414bSchristos struct node *n, *n2, *tmp, *tmp2, *tmproot;
284b985414bSchristos int error;
285b985414bSchristos
286b985414bSchristos TAILQ_FOREACH_SAFE(n, &root->n_children, n_next, tmp) {
287b985414bSchristos if (n->n_key[0] != '+')
288b985414bSchristos continue;
289b985414bSchristos
290b985414bSchristos error = access(AUTO_INCLUDE_PATH, F_OK);
291b985414bSchristos if (error != 0) {
292b985414bSchristos log_errx(1, "directory services not configured; "
293b985414bSchristos "%s does not exist", AUTO_INCLUDE_PATH);
294b985414bSchristos }
295b985414bSchristos
296b985414bSchristos /*
297b985414bSchristos * "+1" to skip leading "+".
298b985414bSchristos */
299b985414bSchristos yyin = auto_popen(AUTO_INCLUDE_PATH, n->n_key + 1, NULL);
300b985414bSchristos assert(yyin != NULL);
301b985414bSchristos
302b985414bSchristos tmproot = node_new_root();
303b985414bSchristos if (is_master)
304b985414bSchristos parse_master_yyin(tmproot, n->n_key);
305b985414bSchristos else
306b985414bSchristos parse_map_yyin(tmproot, n->n_key, NULL);
307b985414bSchristos
308b985414bSchristos error = auto_pclose(yyin);
309b985414bSchristos yyin = NULL;
310b985414bSchristos if (error != 0) {
311b985414bSchristos log_errx(1, "failed to handle include \"%s\"",
312b985414bSchristos n->n_key);
313b985414bSchristos }
314b985414bSchristos
315b985414bSchristos /*
316b985414bSchristos * Entries to be included are now in tmproot. We need to merge
317b985414bSchristos * them with the rest, preserving their place and ordering.
318b985414bSchristos */
319b985414bSchristos TAILQ_FOREACH_REVERSE_SAFE(n2,
320b985414bSchristos &tmproot->n_children, nodehead, n_next, tmp2) {
321b985414bSchristos node_move_after(n2, n);
322b985414bSchristos }
323b985414bSchristos
324b985414bSchristos node_delete(n);
325b985414bSchristos node_delete(tmproot);
326b985414bSchristos }
327b985414bSchristos }
328b985414bSchristos
329b985414bSchristos static char *
expand_ampersand(char * string,const char * key)330b985414bSchristos expand_ampersand(char *string, const char *key)
331b985414bSchristos {
332b985414bSchristos char c, *expanded;
333b985414bSchristos int ret;
334b985414bSchristos size_t i, before_len = 0;
335b985414bSchristos bool backslashed = false;
336b985414bSchristos
337b985414bSchristos assert(key[0] != '\0');
338b985414bSchristos
339b985414bSchristos expanded = checked_strdup(string);
340b985414bSchristos
341b985414bSchristos for (i = 0; string[i] != '\0'; i++) {
342b985414bSchristos c = string[i];
343b985414bSchristos if (c == '\\' && backslashed == false) {
344b985414bSchristos backslashed = true;
345b985414bSchristos continue;
346b985414bSchristos }
347b985414bSchristos if (backslashed) {
348b985414bSchristos backslashed = false;
349b985414bSchristos continue;
350b985414bSchristos }
351b985414bSchristos backslashed = false;
352b985414bSchristos if (c != '&')
353b985414bSchristos continue;
354b985414bSchristos
355b985414bSchristos /*
356b985414bSchristos * The 'before_len' variable contains the number
357b985414bSchristos * of characters before the '&'.
358b985414bSchristos */
359b985414bSchristos before_len = i;
360b650fd96Stkusumi //assert(i < strlen(string));
361b985414bSchristos
362b985414bSchristos ret = asprintf(&expanded, "%.*s%s%s",
363b985414bSchristos (int)before_len, string, key, string + before_len + 1);
364b985414bSchristos if (ret < 0)
365b985414bSchristos log_err(1, "asprintf");
366b985414bSchristos
367b985414bSchristos //log_debugx("\"%s\" expanded with key \"%s\" to \"%s\"",
368b985414bSchristos // string, key, expanded);
369b985414bSchristos
370b985414bSchristos /*
371b985414bSchristos * Figure out where to start searching for next variable.
372b985414bSchristos */
373b985414bSchristos string = expanded;
374b985414bSchristos i = before_len + strlen(key);
375b650fd96Stkusumi if (i == strlen(string))
376b650fd96Stkusumi break;
377b985414bSchristos backslashed = false;
378b985414bSchristos //assert(i < strlen(string));
379b985414bSchristos }
380b985414bSchristos
381b985414bSchristos return expanded;
382b985414bSchristos }
383b985414bSchristos
384b985414bSchristos /*
385b985414bSchristos * Expand "&" in n_location. If the key is NULL, try to use
386b985414bSchristos * key from map entries themselves. Keep in mind that maps
387b985414bSchristos * consist of tho levels of node structures, the key is one
388b985414bSchristos * level up.
389b985414bSchristos *
390b985414bSchristos * Variant with NULL key is for "automount -LL".
391b985414bSchristos */
392b985414bSchristos void
node_expand_ampersand(struct node * n,const char * key)393b985414bSchristos node_expand_ampersand(struct node *n, const char *key)
394b985414bSchristos {
395b985414bSchristos struct node *child;
396b985414bSchristos
397b985414bSchristos if (n->n_location != NULL) {
398b985414bSchristos if (key == NULL) {
399b985414bSchristos if (n->n_parent != NULL &&
400b985414bSchristos strcmp(n->n_parent->n_key, "*") != 0) {
401b985414bSchristos n->n_location = expand_ampersand(n->n_location,
402b985414bSchristos n->n_parent->n_key);
403b985414bSchristos }
404b985414bSchristos } else {
405b985414bSchristos n->n_location = expand_ampersand(n->n_location, key);
406b985414bSchristos }
407b985414bSchristos }
408b985414bSchristos
409b985414bSchristos TAILQ_FOREACH(child, &n->n_children, n_next)
410b985414bSchristos node_expand_ampersand(child, key);
411b985414bSchristos }
412b985414bSchristos
413b985414bSchristos /*
414b985414bSchristos * Expand "*" in n_key.
415b985414bSchristos */
416b985414bSchristos void
node_expand_wildcard(struct node * n,const char * key)417b985414bSchristos node_expand_wildcard(struct node *n, const char *key)
418b985414bSchristos {
419b985414bSchristos struct node *child, *expanded;
420b985414bSchristos
421b985414bSchristos assert(key != NULL);
422b985414bSchristos
423b985414bSchristos if (strcmp(n->n_key, "*") == 0) {
424b985414bSchristos expanded = node_duplicate(n, NULL);
425b985414bSchristos expanded->n_key = checked_strdup(key);
426b985414bSchristos node_move_after(expanded, n);
427b985414bSchristos }
428b985414bSchristos
429b985414bSchristos TAILQ_FOREACH(child, &n->n_children, n_next)
430b985414bSchristos node_expand_wildcard(child, key);
431b985414bSchristos }
432b985414bSchristos
433b985414bSchristos int
node_expand_defined(struct node * n)434b985414bSchristos node_expand_defined(struct node *n)
435b985414bSchristos {
436b985414bSchristos struct node *child;
437b985414bSchristos int error, cummulative_error = 0;
438b985414bSchristos
439b985414bSchristos if (n->n_location != NULL) {
440b985414bSchristos n->n_location = defined_expand(n->n_location);
441b985414bSchristos if (n->n_location == NULL) {
442b985414bSchristos log_warnx("failed to expand location for %s",
443b985414bSchristos node_path(n));
444b985414bSchristos return EINVAL;
445b985414bSchristos }
446b985414bSchristos }
447b985414bSchristos
448b985414bSchristos TAILQ_FOREACH(child, &n->n_children, n_next) {
449b985414bSchristos error = node_expand_defined(child);
450b985414bSchristos if (error != 0 && cummulative_error == 0)
451b985414bSchristos cummulative_error = error;
452b985414bSchristos }
453b985414bSchristos
454b985414bSchristos return cummulative_error;
455b985414bSchristos }
456b985414bSchristos
457b985414bSchristos static bool
node_is_direct_key(const struct node * n)458b985414bSchristos node_is_direct_key(const struct node *n)
459b985414bSchristos {
460b985414bSchristos
461b985414bSchristos return n->n_parent != NULL && n->n_parent->n_parent == NULL &&
462b985414bSchristos strcmp(n->n_key, "/-") == 0;
463b985414bSchristos }
464b985414bSchristos
465b985414bSchristos bool
node_is_direct_map(const struct node * n)466b985414bSchristos node_is_direct_map(const struct node *n)
467b985414bSchristos {
468b985414bSchristos
469b985414bSchristos for (;;) {
470b985414bSchristos assert(n->n_parent != NULL);
471b985414bSchristos if (n->n_parent->n_parent == NULL)
472b985414bSchristos break;
473b985414bSchristos n = n->n_parent;
474b985414bSchristos }
475b985414bSchristos
476b985414bSchristos return node_is_direct_key(n);
477b985414bSchristos }
478b985414bSchristos
479b985414bSchristos bool
node_has_wildcards(const struct node * n)480b985414bSchristos node_has_wildcards(const struct node *n)
481b985414bSchristos {
482b985414bSchristos const struct node *child;
483b985414bSchristos
484b985414bSchristos TAILQ_FOREACH(child, &n->n_children, n_next) {
485b985414bSchristos if (strcmp(child->n_key, "*") == 0)
486b985414bSchristos return true;
487b985414bSchristos }
488b985414bSchristos
489b985414bSchristos return false;
490b985414bSchristos }
491b985414bSchristos
492b985414bSchristos static void
node_expand_maps(struct node * n,bool indirect)493b985414bSchristos node_expand_maps(struct node *n, bool indirect)
494b985414bSchristos {
495b985414bSchristos struct node *child, *tmp;
496b985414bSchristos
497b985414bSchristos TAILQ_FOREACH_SAFE(child, &n->n_children, n_next, tmp) {
498b985414bSchristos if (node_is_direct_map(child)) {
499b985414bSchristos if (indirect)
500b985414bSchristos continue;
501b985414bSchristos } else {
502b985414bSchristos if (indirect == false)
503b985414bSchristos continue;
504b985414bSchristos }
505b985414bSchristos
506b985414bSchristos /*
507b985414bSchristos * This is the first-level map node; the one that contains
508b985414bSchristos * the key and subnodes with mountpoints and actual map names.
509b985414bSchristos */
510b985414bSchristos if (child->n_map == NULL)
511b985414bSchristos continue;
512b985414bSchristos
513b985414bSchristos if (indirect) {
514b985414bSchristos log_debugx("map \"%s\" is an indirect map, parsing",
515b985414bSchristos child->n_map);
516b985414bSchristos } else {
517b985414bSchristos log_debugx("map \"%s\" is a direct map, parsing",
518b985414bSchristos child->n_map);
519b985414bSchristos }
520b985414bSchristos parse_map(child, child->n_map, NULL, NULL);
521b985414bSchristos }
522b985414bSchristos }
523b985414bSchristos
524b985414bSchristos static void
node_expand_direct_maps(struct node * n)525b985414bSchristos node_expand_direct_maps(struct node *n)
526b985414bSchristos {
527b985414bSchristos
528b985414bSchristos node_expand_maps(n, false);
529b985414bSchristos }
530b985414bSchristos
531b985414bSchristos void
node_expand_indirect_maps(struct node * n)532b985414bSchristos node_expand_indirect_maps(struct node *n)
533b985414bSchristos {
534b985414bSchristos
535b985414bSchristos node_expand_maps(n, true);
536b985414bSchristos }
537b985414bSchristos
538b985414bSchristos static char *
node_path_x(const struct node * n,char * x)539b985414bSchristos node_path_x(const struct node *n, char *x)
540b985414bSchristos {
541b985414bSchristos char *path;
542b985414bSchristos
543b985414bSchristos if (n->n_parent == NULL)
544b985414bSchristos return x;
545b985414bSchristos
546b985414bSchristos /*
547b985414bSchristos * Return "/-" for direct maps only if we were asked for path
548b985414bSchristos * to the "/-" node itself, not to any of its subnodes.
549b985414bSchristos */
550b985414bSchristos if (node_is_direct_key(n) && x[0] != '\0')
551b985414bSchristos return x;
552b985414bSchristos
553b985414bSchristos assert(n->n_key[0] != '\0');
554b985414bSchristos path = concat(n->n_key, '/', x);
555b985414bSchristos free(x);
556b985414bSchristos
557b985414bSchristos return node_path_x(n->n_parent, path);
558b985414bSchristos }
559b985414bSchristos
560b985414bSchristos /*
561b985414bSchristos * Return full path for node, consisting of concatenated
562b985414bSchristos * paths of node itself and all its parents, up to the root.
563b985414bSchristos */
564b985414bSchristos char *
node_path(const struct node * n)565b985414bSchristos node_path(const struct node *n)
566b985414bSchristos {
567b985414bSchristos char *path;
568b985414bSchristos size_t len;
569b985414bSchristos
570b985414bSchristos path = node_path_x(n, checked_strdup(""));
571b985414bSchristos
572b985414bSchristos /*
573b985414bSchristos * Strip trailing slash, unless the whole path is "/".
574b985414bSchristos */
575b985414bSchristos len = strlen(path);
576b985414bSchristos if (len > 1 && path[len - 1] == '/')
577b985414bSchristos path[len - 1] = '\0';
578b985414bSchristos
579b985414bSchristos return path;
580b985414bSchristos }
581b985414bSchristos
582b985414bSchristos static char *
node_options_x(const struct node * n,char * x)583b985414bSchristos node_options_x(const struct node *n, char *x)
584b985414bSchristos {
585b985414bSchristos char *options;
586b985414bSchristos
587b985414bSchristos if (n == NULL)
588b985414bSchristos return x;
589b985414bSchristos
590b985414bSchristos options = concat(x, ',', n->n_options);
591b985414bSchristos free(x);
592b985414bSchristos
593b985414bSchristos return node_options_x(n->n_parent, options);
594b985414bSchristos }
595b985414bSchristos
596b985414bSchristos /*
597b985414bSchristos * Return options for node, consisting of concatenated
598b985414bSchristos * options from the node itself and all its parents,
599b985414bSchristos * up to the root.
600b985414bSchristos */
601b985414bSchristos char *
node_options(const struct node * n)602b985414bSchristos node_options(const struct node *n)
603b985414bSchristos {
604b985414bSchristos
605b985414bSchristos return node_options_x(n, checked_strdup(""));
606b985414bSchristos }
607b985414bSchristos
608b985414bSchristos static void
node_print_indent(const struct node * n,const char * cmdline_options,int indent)609b985414bSchristos node_print_indent(const struct node *n, const char *cmdline_options,
610b985414bSchristos int indent)
611b985414bSchristos {
612b985414bSchristos const struct node *child, *first_child;
613b985414bSchristos char *path, *options, *tmp;
614b985414bSchristos
615b985414bSchristos path = node_path(n);
616b985414bSchristos tmp = node_options(n);
617b985414bSchristos options = concat(cmdline_options, ',', tmp);
618b985414bSchristos free(tmp);
619b985414bSchristos
620b985414bSchristos /*
621b985414bSchristos * Do not show both parent and child node if they have the same
622b985414bSchristos * mountpoint; only show the child node. This means the typical,
623b985414bSchristos * "key location", map entries are shown in a single line;
624b985414bSchristos * the "key mountpoint1 location2 mountpoint2 location2" entries
625b985414bSchristos * take multiple lines.
626b985414bSchristos */
627b985414bSchristos first_child = TAILQ_FIRST(&n->n_children);
628b985414bSchristos if (first_child == NULL || TAILQ_NEXT(first_child, n_next) != NULL ||
629b985414bSchristos strcmp(path, node_path(first_child)) != 0) {
630b985414bSchristos assert(n->n_location == NULL || n->n_map == NULL);
631b985414bSchristos printf("%*.s%-*s %s%-*s %-*s # %s map %s at %s:%d\n",
632b985414bSchristos indent, "",
633b985414bSchristos 25 - indent,
634b985414bSchristos path,
635b985414bSchristos options[0] != '\0' ? "-" : " ",
636b985414bSchristos 20,
637b985414bSchristos options[0] != '\0' ? options : "",
638b985414bSchristos 20,
639b985414bSchristos n->n_location != NULL ? n->n_location : n->n_map != NULL ? n->n_map : "",
640b985414bSchristos node_is_direct_map(n) ? "direct" : "indirect",
641b985414bSchristos indent == 0 ? "referenced" : "defined",
642b985414bSchristos n->n_config_file, n->n_config_line);
643b985414bSchristos }
644b985414bSchristos
645b985414bSchristos free(path);
646b985414bSchristos free(options);
647b985414bSchristos
648b985414bSchristos TAILQ_FOREACH(child, &n->n_children, n_next)
649b985414bSchristos node_print_indent(child, cmdline_options, indent + 2);
650b985414bSchristos }
651b985414bSchristos
652b985414bSchristos /*
653b985414bSchristos * Recursively print node with all its children. The cmdline_options
654b985414bSchristos * argument is used for additional options to be prepended to all the
655b985414bSchristos * others - usually those are the options passed by command line.
656b985414bSchristos */
657b985414bSchristos void
node_print(const struct node * n,const char * cmdline_options)658b985414bSchristos node_print(const struct node *n, const char *cmdline_options)
659b985414bSchristos {
660b985414bSchristos const struct node *child;
661b985414bSchristos
662b985414bSchristos TAILQ_FOREACH(child, &n->n_children, n_next)
663b985414bSchristos node_print_indent(child, cmdline_options, 0);
664b985414bSchristos }
665b985414bSchristos
666b985414bSchristos static struct node *
node_find_x(struct node * node,const char * path)667b985414bSchristos node_find_x(struct node *node, const char *path)
668b985414bSchristos {
669b985414bSchristos struct node *child, *found;
670b985414bSchristos char *tmp;
671b985414bSchristos size_t tmplen;
672b985414bSchristos
673b985414bSchristos //log_debugx("looking up %s in %s", path, node_path(node));
674b985414bSchristos
675b985414bSchristos if (!node_is_direct_key(node)) {
676b985414bSchristos tmp = node_path(node);
677b985414bSchristos tmplen = strlen(tmp);
678b985414bSchristos if (strncmp(tmp, path, tmplen) != 0) {
679b985414bSchristos free(tmp);
680b985414bSchristos return NULL;
681b985414bSchristos }
682b985414bSchristos if (path[tmplen] != '/' && path[tmplen] != '\0') {
683b985414bSchristos /*
684b985414bSchristos * If we have two map entries like 'foo' and 'foobar', make
685b985414bSchristos * sure the search for 'foobar' won't match 'foo' instead.
686b985414bSchristos */
687b985414bSchristos free(tmp);
688b985414bSchristos return NULL;
689b985414bSchristos }
690b985414bSchristos free(tmp);
691b985414bSchristos }
692b985414bSchristos
693b985414bSchristos TAILQ_FOREACH(child, &node->n_children, n_next) {
694b985414bSchristos found = node_find_x(child, path);
695b985414bSchristos if (found != NULL)
696b985414bSchristos return found;
697b985414bSchristos }
698b985414bSchristos
699b985414bSchristos if (node->n_parent == NULL || node_is_direct_key(node))
700b985414bSchristos return NULL;
701b985414bSchristos
702b985414bSchristos return node;
703b985414bSchristos }
704b985414bSchristos
705b985414bSchristos struct node *
node_find(struct node * root,const char * path)706b985414bSchristos node_find(struct node *root, const char *path)
707b985414bSchristos {
708b985414bSchristos struct node *node;
709b985414bSchristos
710b985414bSchristos assert(root->n_parent == NULL);
711b985414bSchristos
712b985414bSchristos node = node_find_x(root, path);
713b985414bSchristos if (node != NULL)
714b985414bSchristos assert(node != root);
715b985414bSchristos
716b985414bSchristos return node;
717b985414bSchristos }
718b985414bSchristos
719b985414bSchristos /*
720b985414bSchristos * Canonical form of a map entry looks like this:
721b985414bSchristos *
722b985414bSchristos * key [-options] [ [/mountpoint] [-options2] location ... ]
723b985414bSchristos *
724b985414bSchristos * Entries for executable maps are slightly different, as they
725b985414bSchristos * lack the 'key' field and are always single-line; the key field
726b985414bSchristos * for those maps is taken from 'executable_key' argument.
727b985414bSchristos *
728b985414bSchristos * We parse it in such a way that a map always has two levels - first
729b985414bSchristos * for key, and the second, for the mountpoint.
730b985414bSchristos */
731b985414bSchristos static void
parse_map_yyin(struct node * parent,const char * map,const char * executable_key)732b985414bSchristos parse_map_yyin(struct node *parent, const char *map, const char *executable_key)
733b985414bSchristos {
734b985414bSchristos char *key = NULL, *options = NULL, *mountpoint = NULL,
735b985414bSchristos *options2 = NULL, *location = NULL;
736b985414bSchristos int ret;
737b985414bSchristos struct node *node;
738b985414bSchristos
739b985414bSchristos lineno = 1;
740b985414bSchristos
741b985414bSchristos if (executable_key != NULL)
742b985414bSchristos key = checked_strdup(executable_key);
743b985414bSchristos
744b985414bSchristos for (;;) {
745b985414bSchristos ret = yylex();
746b985414bSchristos if (ret == 0 || ret == NEWLINE) {
747b985414bSchristos /*
748b985414bSchristos * In case of executable map, the key is always
749b985414bSchristos * non-NULL, even if the map is empty. So, make sure
750b985414bSchristos * we don't fail empty maps here.
751b985414bSchristos */
752b985414bSchristos if ((key != NULL && executable_key == NULL) ||
753b985414bSchristos options != NULL) {
754b985414bSchristos log_errx(1, "truncated entry at %s, line %d",
755b985414bSchristos map, lineno);
756b985414bSchristos }
757b985414bSchristos if (ret == 0 || executable_key != NULL) {
758b985414bSchristos /*
759b985414bSchristos * End of file.
760b985414bSchristos */
761b985414bSchristos break;
762b985414bSchristos } else {
763b985414bSchristos key = options = NULL;
764b985414bSchristos continue;
765b985414bSchristos }
766b985414bSchristos }
767b985414bSchristos if (key == NULL) {
768b985414bSchristos key = checked_strdup(yytext);
769b985414bSchristos if (key[0] == '+') {
770b985414bSchristos node_new(parent, key, NULL, NULL, map, lineno);
771b985414bSchristos key = options = NULL;
772b985414bSchristos continue;
773b985414bSchristos }
774b985414bSchristos continue;
775b985414bSchristos } else if (yytext[0] == '-') {
776b985414bSchristos if (options != NULL) {
777b985414bSchristos log_errx(1, "duplicated options at %s, line %d",
778b985414bSchristos map, lineno);
779b985414bSchristos }
780b985414bSchristos /*
781b985414bSchristos * +1 to skip leading "-".
782b985414bSchristos */
783b985414bSchristos options = checked_strdup(yytext + 1);
784b985414bSchristos continue;
785b985414bSchristos }
786b985414bSchristos
787b985414bSchristos /*
788b985414bSchristos * We cannot properly handle a situation where the map key
789b985414bSchristos * is "/". Ignore such entries.
790b985414bSchristos *
791b985414bSchristos * XXX: According to Piete Brooks, Linux automounter uses
792b985414bSchristos * "/" as a wildcard character in LDAP maps. Perhaps
793b985414bSchristos * we should work around this braindamage by substituting
794b985414bSchristos * "*" for "/"?
795b985414bSchristos */
796b985414bSchristos if (strcmp(key, "/") == 0) {
797b985414bSchristos log_warnx("nonsensical map key \"/\" at %s, line %d; "
798b985414bSchristos "ignoring map entry ", map, lineno);
799b985414bSchristos
800b985414bSchristos /*
801b985414bSchristos * Skip the rest of the entry.
802b985414bSchristos */
803b985414bSchristos do {
804b985414bSchristos ret = yylex();
805b985414bSchristos } while (ret != 0 && ret != NEWLINE);
806b985414bSchristos
807b985414bSchristos key = options = NULL;
808b985414bSchristos continue;
809b985414bSchristos }
810b985414bSchristos
811b985414bSchristos //log_debugx("adding map node, %s", key);
812b985414bSchristos node = node_new(parent, key, options, NULL, map, lineno);
813b985414bSchristos key = options = NULL;
814b985414bSchristos
815b985414bSchristos for (;;) {
816b985414bSchristos if (yytext[0] == '/') {
817b985414bSchristos if (mountpoint != NULL) {
818b985414bSchristos log_errx(1, "duplicated mountpoint "
819b985414bSchristos "in %s, line %d", map, lineno);
820b985414bSchristos }
821b985414bSchristos if (options2 != NULL || location != NULL) {
822b985414bSchristos log_errx(1, "mountpoint out of order "
823b985414bSchristos "in %s, line %d", map, lineno);
824b985414bSchristos }
825b985414bSchristos mountpoint = checked_strdup(yytext);
826b985414bSchristos goto again;
827b985414bSchristos }
828b985414bSchristos
829b985414bSchristos if (yytext[0] == '-') {
830b985414bSchristos if (options2 != NULL) {
831b985414bSchristos log_errx(1, "duplicated options "
832b985414bSchristos "in %s, line %d", map, lineno);
833b985414bSchristos }
834b985414bSchristos if (location != NULL) {
835b985414bSchristos log_errx(1, "options out of order "
836b985414bSchristos "in %s, line %d", map, lineno);
837b985414bSchristos }
838b985414bSchristos options2 = checked_strdup(yytext + 1);
839b985414bSchristos goto again;
840b985414bSchristos }
841b985414bSchristos
842b985414bSchristos if (location != NULL) {
843b985414bSchristos log_errx(1, "too many arguments "
844b985414bSchristos "in %s, line %d", map, lineno);
845b985414bSchristos }
846b985414bSchristos
847b985414bSchristos /*
848b985414bSchristos * If location field starts with colon, e.g. ":/dev/cd0",
849b985414bSchristos * then strip it.
850b985414bSchristos */
851b985414bSchristos if (yytext[0] == ':') {
852b985414bSchristos location = checked_strdup(yytext + 1);
853b985414bSchristos if (location[0] == '\0') {
854b985414bSchristos log_errx(1, "empty location in %s, "
855b985414bSchristos "line %d", map, lineno);
856b985414bSchristos }
857b985414bSchristos } else {
858b985414bSchristos location = checked_strdup(yytext);
859b985414bSchristos }
860b985414bSchristos
861b985414bSchristos if (mountpoint == NULL)
862b985414bSchristos mountpoint = checked_strdup("/");
863b985414bSchristos if (options2 == NULL)
864b985414bSchristos options2 = checked_strdup("");
865b985414bSchristos
866b985414bSchristos #if 0
867b985414bSchristos log_debugx("adding map node, %s %s %s",
868b985414bSchristos mountpoint, options2, location);
869b985414bSchristos #endif
870b985414bSchristos node_new(node, mountpoint, options2, location,
871b985414bSchristos map, lineno);
872b985414bSchristos mountpoint = options2 = location = NULL;
873b985414bSchristos again:
874b985414bSchristos ret = yylex();
875b985414bSchristos if (ret == 0 || ret == NEWLINE) {
876b985414bSchristos if (mountpoint != NULL || options2 != NULL ||
877b985414bSchristos location != NULL) {
878b985414bSchristos log_errx(1, "truncated entry "
879b985414bSchristos "in %s, line %d", map, lineno);
880b985414bSchristos }
881b985414bSchristos break;
882b985414bSchristos }
883b985414bSchristos }
884b985414bSchristos }
885b985414bSchristos }
886b985414bSchristos
887b985414bSchristos /*
888b985414bSchristos * Parse output of a special map called without argument. It is a list
889b985414bSchristos * of keys, separated by newlines. They can contain whitespace, so use
890b985414bSchristos * getline(3) instead of lexer used for maps.
891b985414bSchristos */
892b985414bSchristos static void
parse_map_keys_yyin(struct node * parent,const char * map)893b985414bSchristos parse_map_keys_yyin(struct node *parent, const char *map)
894b985414bSchristos {
895b985414bSchristos char *line = NULL, *key;
896b985414bSchristos size_t linecap = 0;
897b985414bSchristos ssize_t linelen;
898b985414bSchristos
899b985414bSchristos lineno = 1;
900b985414bSchristos
901b985414bSchristos for (;;) {
902b985414bSchristos linelen = getline(&line, &linecap, yyin);
903b985414bSchristos if (linelen < 0) {
904b985414bSchristos /*
905b985414bSchristos * End of file.
906b985414bSchristos */
907b985414bSchristos break;
908b985414bSchristos }
909b985414bSchristos if (linelen <= 1) {
910b985414bSchristos /*
911b985414bSchristos * Empty line, consisting of just the newline.
912b985414bSchristos */
913b985414bSchristos continue;
914b985414bSchristos }
915b985414bSchristos
916b985414bSchristos /*
917b985414bSchristos * "-1" to strip the trailing newline.
918b985414bSchristos */
919b985414bSchristos key = strndup(line, (size_t)linelen - 1);
920b985414bSchristos
921b985414bSchristos log_debugx("adding key \"%s\"", key);
922b985414bSchristos node_new(parent, key, NULL, NULL, map, lineno);
923b985414bSchristos lineno++;
924b985414bSchristos }
925b985414bSchristos free(line);
926b985414bSchristos }
927b985414bSchristos
928b985414bSchristos static bool
file_is_executable(const char * path)929b985414bSchristos file_is_executable(const char *path)
930b985414bSchristos {
931b985414bSchristos struct stat sb;
932b985414bSchristos int error;
933b985414bSchristos
934b985414bSchristos error = stat(path, &sb);
935b985414bSchristos if (error != 0)
936b985414bSchristos log_err(1, "cannot stat %s", path);
937b985414bSchristos return (sb.st_mode & S_IXUSR) || (sb.st_mode & S_IXGRP) ||
938b985414bSchristos (sb.st_mode & S_IXOTH);
939b985414bSchristos }
940b985414bSchristos
941b985414bSchristos /*
942b985414bSchristos * Parse a special map, e.g. "-hosts".
943b985414bSchristos */
944b985414bSchristos static void
parse_special_map(struct node * parent,const char * map,const char * key)945b985414bSchristos parse_special_map(struct node *parent, const char *map, const char *key)
946b985414bSchristos {
947b985414bSchristos char *path;
948b985414bSchristos int error, ret;
949b985414bSchristos
950b985414bSchristos assert(map[0] == '-');
951b985414bSchristos
952b985414bSchristos /*
953b985414bSchristos * +1 to skip leading "-" in map name.
954b985414bSchristos */
955b985414bSchristos ret = asprintf(&path, "%s/special_%s", AUTO_SPECIAL_PREFIX, map + 1);
956b985414bSchristos if (ret < 0)
957b985414bSchristos log_err(1, "asprintf");
958b985414bSchristos
959b985414bSchristos yyin = auto_popen(path, key, NULL);
960b985414bSchristos assert(yyin != NULL);
961b985414bSchristos
962b985414bSchristos if (key == NULL) {
963b985414bSchristos parse_map_keys_yyin(parent, map);
964b985414bSchristos } else {
965b985414bSchristos parse_map_yyin(parent, map, key);
966b985414bSchristos }
967b985414bSchristos
968b985414bSchristos error = auto_pclose(yyin);
969b985414bSchristos yyin = NULL;
970b985414bSchristos if (error != 0)
971b985414bSchristos log_errx(1, "failed to handle special map \"%s\"", map);
972b985414bSchristos
973b985414bSchristos node_expand_includes(parent, false);
974b985414bSchristos node_expand_direct_maps(parent);
975b985414bSchristos
976b985414bSchristos free(path);
977b985414bSchristos }
978b985414bSchristos
979b985414bSchristos /*
980b985414bSchristos * Retrieve and parse map from directory services, e.g. LDAP.
981b985414bSchristos * Note that it is different from executable maps, in that
982b985414bSchristos * the include script outputs the whole map to standard output
983b985414bSchristos * (as opposed to executable maps that only output a single
984b985414bSchristos * entry, without the key), and it takes the map name as an
985b985414bSchristos * argument, instead of key.
986b985414bSchristos */
987b985414bSchristos static void
parse_included_map(struct node * parent,const char * map)988b985414bSchristos parse_included_map(struct node *parent, const char *map)
989b985414bSchristos {
990b985414bSchristos int error;
991b985414bSchristos
992b985414bSchristos assert(map[0] != '-');
993b985414bSchristos assert(map[0] != '/');
994b985414bSchristos
995b985414bSchristos error = access(AUTO_INCLUDE_PATH, F_OK);
996b985414bSchristos if (error != 0) {
997b985414bSchristos log_errx(1, "directory services not configured;"
998b985414bSchristos " %s does not exist", AUTO_INCLUDE_PATH);
999b985414bSchristos }
1000b985414bSchristos
1001b985414bSchristos yyin = auto_popen(AUTO_INCLUDE_PATH, map, NULL);
1002b985414bSchristos assert(yyin != NULL);
1003b985414bSchristos
1004b985414bSchristos parse_map_yyin(parent, map, NULL);
1005b985414bSchristos
1006b985414bSchristos error = auto_pclose(yyin);
1007b985414bSchristos yyin = NULL;
1008b985414bSchristos if (error != 0)
1009b985414bSchristos log_errx(1, "failed to handle remote map \"%s\"", map);
1010b985414bSchristos
1011b985414bSchristos node_expand_includes(parent, false);
1012b985414bSchristos node_expand_direct_maps(parent);
1013b985414bSchristos }
1014b985414bSchristos
1015b985414bSchristos void
parse_map(struct node * parent,const char * map,const char * key,bool * wildcards)1016b985414bSchristos parse_map(struct node *parent, const char *map, const char *key,
1017b985414bSchristos bool *wildcards)
1018b985414bSchristos {
1019b985414bSchristos char *path = NULL;
1020b985414bSchristos int error, ret;
1021b985414bSchristos bool executable;
1022b985414bSchristos
1023b985414bSchristos assert(map != NULL);
1024b985414bSchristos assert(map[0] != '\0');
1025b985414bSchristos
1026b985414bSchristos log_debugx("parsing map \"%s\"", map);
1027b985414bSchristos
1028b985414bSchristos if (wildcards != NULL)
1029b985414bSchristos *wildcards = false;
1030b985414bSchristos
1031b985414bSchristos if (map[0] == '-') {
1032b985414bSchristos if (wildcards != NULL)
1033b985414bSchristos *wildcards = true;
1034cca06e9cSrillig parse_special_map(parent, map, key);
1035cca06e9cSrillig return;
1036b985414bSchristos }
1037b985414bSchristos
1038b985414bSchristos if (map[0] == '/') {
1039b985414bSchristos path = checked_strdup(map);
1040b985414bSchristos } else {
1041b985414bSchristos ret = asprintf(&path, "%s/%s", AUTO_MAP_PREFIX, map);
1042b985414bSchristos if (ret < 0)
1043b985414bSchristos log_err(1, "asprintf");
1044b985414bSchristos log_debugx("map \"%s\" maps to \"%s\"", map, path);
1045b985414bSchristos
1046b985414bSchristos /*
1047b985414bSchristos * See if the file exists. If not, try to obtain the map
1048b985414bSchristos * from directory services.
1049b985414bSchristos */
1050b985414bSchristos error = access(path, F_OK);
1051b985414bSchristos if (error != 0) {
1052b985414bSchristos log_debugx("map file \"%s\" does not exist; falling "
1053b985414bSchristos "back to directory services", path);
1054cca06e9cSrillig parse_included_map(parent, map);
1055cca06e9cSrillig return;
1056b985414bSchristos }
1057b985414bSchristos }
1058b985414bSchristos
1059b985414bSchristos executable = file_is_executable(path);
1060b985414bSchristos
1061b985414bSchristos if (executable) {
1062b985414bSchristos log_debugx("map \"%s\" is executable", map);
1063b985414bSchristos
1064b985414bSchristos if (wildcards != NULL)
1065b985414bSchristos *wildcards = true;
1066b985414bSchristos
1067b985414bSchristos if (key != NULL) {
1068b985414bSchristos yyin = auto_popen(path, key, NULL);
1069b985414bSchristos } else {
1070b985414bSchristos yyin = auto_popen(path, NULL);
1071b985414bSchristos }
1072b985414bSchristos assert(yyin != NULL);
1073b985414bSchristos } else {
1074b985414bSchristos yyin = fopen(path, "r");
1075b985414bSchristos if (yyin == NULL)
1076b985414bSchristos log_err(1, "unable to open \"%s\"", path);
1077b985414bSchristos }
1078b985414bSchristos
1079b985414bSchristos free(path);
1080b985414bSchristos path = NULL;
1081b985414bSchristos
1082b985414bSchristos parse_map_yyin(parent, map, executable ? key : NULL);
1083b985414bSchristos
1084b985414bSchristos if (executable) {
1085b985414bSchristos error = auto_pclose(yyin);
1086b985414bSchristos yyin = NULL;
1087b985414bSchristos if (error != 0) {
1088b985414bSchristos log_errx(1, "failed to handle executable map \"%s\"",
1089b985414bSchristos map);
1090b985414bSchristos }
1091b985414bSchristos } else {
1092b985414bSchristos fclose(yyin);
1093b985414bSchristos }
1094b985414bSchristos yyin = NULL;
1095b985414bSchristos
1096b985414bSchristos log_debugx("done parsing map \"%s\"", map);
1097b985414bSchristos
1098b985414bSchristos node_expand_includes(parent, false);
1099b985414bSchristos node_expand_direct_maps(parent);
1100b985414bSchristos }
1101b985414bSchristos
1102b985414bSchristos static void
parse_master_yyin(struct node * root,const char * master)1103b985414bSchristos parse_master_yyin(struct node *root, const char *master)
1104b985414bSchristos {
1105b985414bSchristos char *mountpoint = NULL, *map = NULL, *options = NULL;
1106b985414bSchristos int ret;
1107b985414bSchristos
1108b985414bSchristos /*
1109b985414bSchristos * XXX: 1 gives incorrect values; wtf?
1110b985414bSchristos */
1111b985414bSchristos lineno = 0;
1112b985414bSchristos
1113b985414bSchristos for (;;) {
1114b985414bSchristos ret = yylex();
1115b985414bSchristos if (ret == 0 || ret == NEWLINE) {
1116b985414bSchristos if (mountpoint != NULL) {
1117b985414bSchristos //log_debugx("adding map for %s", mountpoint);
1118b985414bSchristos node_new_map(root, mountpoint, options, map,
1119b985414bSchristos master, lineno);
1120b985414bSchristos }
1121b985414bSchristos if (ret == 0) {
1122b985414bSchristos break;
1123b985414bSchristos } else {
1124b985414bSchristos mountpoint = map = options = NULL;
1125b985414bSchristos continue;
1126b985414bSchristos }
1127b985414bSchristos }
1128b985414bSchristos if (mountpoint == NULL) {
1129b985414bSchristos mountpoint = checked_strdup(yytext);
1130b985414bSchristos } else if (map == NULL) {
1131b985414bSchristos map = checked_strdup(yytext);
1132b985414bSchristos } else if (options == NULL) {
1133b985414bSchristos /*
1134b985414bSchristos * +1 to skip leading "-".
1135b985414bSchristos */
1136b985414bSchristos options = checked_strdup(yytext + 1);
1137b985414bSchristos } else {
1138b985414bSchristos log_errx(1, "too many arguments at %s, line %d",
1139b985414bSchristos master, lineno);
1140b985414bSchristos }
1141b985414bSchristos }
1142b985414bSchristos }
1143b985414bSchristos
1144b985414bSchristos void
parse_master(struct node * root,const char * master)1145b985414bSchristos parse_master(struct node *root, const char *master)
1146b985414bSchristos {
1147b985414bSchristos
1148b985414bSchristos log_debugx("parsing auto_master file at \"%s\"", master);
1149b985414bSchristos
1150b985414bSchristos yyin = fopen(master, "r");
1151b985414bSchristos if (yyin == NULL)
1152b985414bSchristos err(1, "unable to open %s", master);
1153b985414bSchristos
1154b985414bSchristos parse_master_yyin(root, master);
1155b985414bSchristos
1156b985414bSchristos fclose(yyin);
1157b985414bSchristos yyin = NULL;
1158b985414bSchristos
1159b985414bSchristos log_debugx("done parsing \"%s\"", master);
1160b985414bSchristos
1161b985414bSchristos node_expand_includes(root, true);
1162b985414bSchristos node_expand_direct_maps(root);
1163b985414bSchristos }
1164b985414bSchristos
1165b985414bSchristos /*
1166b985414bSchristos * Two things daemon(3) does, that we actually also want to do
1167b985414bSchristos * when running in foreground, is closing the stdin and chdiring
1168b985414bSchristos * to "/". This is what we do here.
1169b985414bSchristos */
1170b985414bSchristos void
lesser_daemon(void)1171b985414bSchristos lesser_daemon(void)
1172b985414bSchristos {
1173b985414bSchristos int error, fd;
1174b985414bSchristos
1175b985414bSchristos error = chdir("/");
1176b985414bSchristos if (error != 0)
1177b985414bSchristos log_warn("chdir");
1178b985414bSchristos
1179b985414bSchristos fd = open(_PATH_DEVNULL, O_RDWR, 0);
1180b985414bSchristos if (fd < 0) {
1181b985414bSchristos log_warn("cannot open %s", _PATH_DEVNULL);
1182b985414bSchristos return;
1183b985414bSchristos }
1184b985414bSchristos
1185b985414bSchristos error = dup2(fd, STDIN_FILENO);
1186b985414bSchristos if (error != 0)
1187b985414bSchristos log_warn("dup2");
1188b985414bSchristos
1189b985414bSchristos error = close(fd);
1190b985414bSchristos if (error != 0) {
1191b985414bSchristos /* Bloody hell. */
1192b985414bSchristos log_warn("close");
1193b985414bSchristos }
1194b985414bSchristos }
1195b985414bSchristos
1196b985414bSchristos int
main(int argc,char ** argv)1197b985414bSchristos main(int argc, char **argv)
1198b985414bSchristos {
1199b985414bSchristos char *cmdname;
1200b985414bSchristos
1201b985414bSchristos if (argv[0] == NULL)
1202b985414bSchristos log_errx(1, "NULL command name");
1203b985414bSchristos
1204b985414bSchristos cmdname = basename(argv[0]);
1205b985414bSchristos
1206b985414bSchristos if (strcmp(cmdname, "automount") == 0)
1207b985414bSchristos return main_automount(argc, argv);
1208b985414bSchristos else if (strcmp(cmdname, "automountd") == 0)
1209b985414bSchristos return main_automountd(argc, argv);
1210b985414bSchristos else if (strcmp(cmdname, "autounmountd") == 0)
1211b985414bSchristos return main_autounmountd(argc, argv);
1212b985414bSchristos else
1213b985414bSchristos log_errx(1, "binary name should be either \"automount\", "
1214b985414bSchristos "\"automountd\", or \"autounmountd\"");
1215b985414bSchristos }
1216