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