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