xref: /freebsd/usr.sbin/autofs/automount.c (revision 313376588638950ba1e93c403dd8c97bc52fd3a2)
1 /*-
2  * Copyright (c) 2014 The FreeBSD Foundation
3  * All rights reserved.
4  *
5  * This software was developed by Edward Tomasz Napierala under sponsorship
6  * from the FreeBSD Foundation.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  */
30 
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33 
34 #include <sys/types.h>
35 #include <sys/time.h>
36 #include <sys/ioctl.h>
37 #include <sys/param.h>
38 #include <sys/linker.h>
39 #include <sys/mount.h>
40 #include <sys/socket.h>
41 #include <sys/stat.h>
42 #include <sys/wait.h>
43 #include <sys/utsname.h>
44 #include <assert.h>
45 #include <ctype.h>
46 #include <errno.h>
47 #include <fcntl.h>
48 #include <libgen.h>
49 #include <netdb.h>
50 #include <signal.h>
51 #include <stdbool.h>
52 #include <stdint.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #include <unistd.h>
57 
58 #include <libutil.h>
59 
60 #include "common.h"
61 #include "mntopts.h"
62 
63 static int
64 unmount_by_statfs(const struct statfs *sb, bool force)
65 {
66 	char *fsid_str;
67 	int error, ret, flags;
68 
69 	ret = asprintf(&fsid_str, "FSID:%d:%d",
70 	    sb->f_fsid.val[0], sb->f_fsid.val[1]);
71 	if (ret < 0)
72 		log_err(1, "asprintf");
73 
74 	log_debugx("unmounting %s using %s", sb->f_mntonname, fsid_str);
75 
76 	flags = MNT_BYFSID;
77 	if (force)
78 		flags |= MNT_FORCE;
79 	error = unmount(fsid_str, flags);
80 	free(fsid_str);
81 	if (error != 0)
82 		log_warn("cannot unmount %s", sb->f_mntonname);
83 
84 	return (error);
85 }
86 
87 static const struct statfs *
88 find_statfs(const struct statfs *mntbuf, int nitems, const char *mountpoint)
89 {
90 	int i;
91 
92 	for (i = 0; i < nitems; i++) {
93 		if (strcmp(mntbuf[i].f_mntonname, mountpoint) == 0)
94 			return (mntbuf + i);
95 	}
96 
97 	return (NULL);
98 }
99 
100 static void
101 mount_autofs(const char *from, const char *fspath, const char *options,
102     const char *prefix)
103 {
104 	struct iovec *iov = NULL;
105 	char errmsg[255];
106 	int error, iovlen = 0;
107 
108 	create_directory(fspath);
109 
110 	log_debugx("mounting %s on %s, prefix \"%s\", options \"%s\"",
111 	    from, fspath, prefix, options);
112 	memset(errmsg, 0, sizeof(errmsg));
113 
114 	build_iovec(&iov, &iovlen, "fstype",
115 	    __DECONST(void *, "autofs"), (size_t)-1);
116 	build_iovec(&iov, &iovlen, "fspath",
117 	    __DECONST(void *, fspath), (size_t)-1);
118 	build_iovec(&iov, &iovlen, "from",
119 	    __DECONST(void *, from), (size_t)-1);
120 	build_iovec(&iov, &iovlen, "errmsg",
121 	    errmsg, sizeof(errmsg));
122 
123 	/*
124 	 * Append the options and mountpoint defined in auto_master(5);
125 	 * this way automountd(8) does not need to parse it.
126 	 */
127 	build_iovec(&iov, &iovlen, "master_options",
128 	    __DECONST(void *, options), (size_t)-1);
129 	build_iovec(&iov, &iovlen, "master_prefix",
130 	    __DECONST(void *, prefix), (size_t)-1);
131 
132 	error = nmount(iov, iovlen, 0);
133 	if (error != 0) {
134 		if (*errmsg != '\0') {
135 			log_err(1, "cannot mount %s on %s: %s",
136 			    from, fspath, errmsg);
137 		} else {
138 			log_err(1, "cannot mount %s on %s", from, fspath);
139 		}
140 	}
141 }
142 
143 static void
144 mount_if_not_already(const struct node *n, const char *map,
145     const struct statfs *mntbuf, int nitems)
146 {
147 	const struct statfs *sb;
148 	char *mountpoint;
149 	char *from;
150 	int ret;
151 
152 	ret = asprintf(&from, "map %s", map);
153 	if (ret < 0)
154 		log_err(1, "asprintf");
155 
156 	mountpoint = node_path(n);
157 	sb = find_statfs(mntbuf, nitems, mountpoint);
158 	if (sb != NULL) {
159 		if (strcmp(sb->f_fstypename, "autofs") != 0) {
160 			log_debugx("unknown filesystem mounted "
161 			    "on %s; mounting", mountpoint);
162 			/*
163 			 * XXX: Compare options and 'from',
164 			 *	and update the mount if necessary.
165 			 */
166 		} else {
167 			log_debugx("autofs already mounted "
168 			    "on %s", mountpoint);
169 			free(from);
170 			free(mountpoint);
171 			return;
172 		}
173 	} else {
174 		log_debugx("nothing mounted on %s; mounting",
175 		    mountpoint);
176 	}
177 
178 	mount_autofs(from, mountpoint, n->n_options, n->n_key);
179 	free(from);
180 	free(mountpoint);
181 }
182 
183 static void
184 mount_unmount(struct node *root)
185 {
186 	struct statfs *mntbuf;
187 	struct node *n, *n2, *n3;
188 	int i, nitems;
189 
190 	nitems = getmntinfo(&mntbuf, MNT_WAIT);
191 	if (nitems <= 0)
192 		log_err(1, "getmntinfo");
193 
194 	log_debugx("unmounting stale autofs mounts");
195 
196 	for (i = 0; i < nitems; i++) {
197 		if (strcmp(mntbuf[i].f_fstypename, "autofs") != 0) {
198 			log_debugx("skipping %s, filesystem type is not autofs",
199 			    mntbuf[i].f_mntonname);
200 			continue;
201 		}
202 
203 		n = node_find(root, mntbuf[i].f_mntonname);
204 		if (n != NULL) {
205 			log_debugx("leaving autofs mounted on %s",
206 			    mntbuf[i].f_mntonname);
207 			continue;
208 		}
209 
210 		log_debugx("autofs mounted on %s not found "
211 		    "in new configuration; unmounting", mntbuf[i].f_mntonname);
212 		unmount_by_statfs(&(mntbuf[i]), false);
213 	}
214 
215 	log_debugx("mounting new autofs mounts");
216 
217 	TAILQ_FOREACH(n, &root->n_children, n_next) {
218 		if (!node_is_direct_map(n)) {
219 			mount_if_not_already(n, n->n_map, mntbuf, nitems);
220 			continue;
221 		}
222 
223 		TAILQ_FOREACH(n2, &n->n_children, n_next) {
224 			TAILQ_FOREACH(n3, &n2->n_children, n_next) {
225 				mount_if_not_already(n3, n->n_map,
226 				    mntbuf, nitems);
227 			}
228 		}
229 	}
230 }
231 
232 static void
233 unmount_automounted(bool force)
234 {
235 	struct statfs *mntbuf;
236 	int i, nitems;
237 
238 	nitems = getmntinfo(&mntbuf, MNT_WAIT);
239 	if (nitems <= 0)
240 		log_err(1, "getmntinfo");
241 
242 	log_debugx("unmounting automounted filesystems");
243 
244 	for (i = 0; i < nitems; i++) {
245 		if (strcmp(mntbuf[i].f_fstypename, "autofs") == 0) {
246 			log_debugx("skipping %s, filesystem type is autofs",
247 			    mntbuf[i].f_mntonname);
248 			continue;
249 		}
250 
251 		if ((mntbuf[i].f_flags & MNT_AUTOMOUNTED) == 0) {
252 			log_debugx("skipping %s, not automounted",
253 			    mntbuf[i].f_mntonname);
254 			continue;
255 		}
256 
257 		unmount_by_statfs(&(mntbuf[i]), force);
258 	}
259 }
260 
261 static void
262 usage_automount(void)
263 {
264 
265 	fprintf(stderr, "usage: automount [-D name=value][-o opts][-Lfuv]\n");
266 	exit(1);
267 }
268 
269 int
270 main_automount(int argc, char **argv)
271 {
272 	struct node *root;
273 	int ch, debug = 0, show_maps = 0;
274 	char *options = NULL;
275 	bool do_unmount = false, force_unmount = false;
276 
277 	/*
278 	 * Note that in automount(8), the only purpose of variable
279 	 * handling is to aid in debugging maps (automount -L).
280 	 */
281 	defined_init();
282 
283 	while ((ch = getopt(argc, argv, "D:Lfo:uv")) != -1) {
284 		switch (ch) {
285 		case 'D':
286 			defined_parse_and_add(optarg);
287 			break;
288 		case 'L':
289 			show_maps++;
290 			break;
291 		case 'f':
292 			force_unmount = true;
293 			break;
294 		case 'o':
295 			if (options == NULL) {
296 				options = checked_strdup(optarg);
297 			} else {
298 				options =
299 				    separated_concat(options, optarg, ',');
300 			}
301 			break;
302 		case 'u':
303 			do_unmount = true;
304 			break;
305 		case 'v':
306 			debug++;
307 			break;
308 		case '?':
309 		default:
310 			usage_automount();
311 		}
312 	}
313 	argc -= optind;
314 	if (argc != 0)
315 		usage_automount();
316 
317 	if (force_unmount && !do_unmount)
318 		usage_automount();
319 
320 	log_init(debug);
321 
322 	if (do_unmount) {
323 		unmount_automounted(force_unmount);
324 		return (0);
325 	}
326 
327 	root = node_new_root();
328 	parse_master(root, AUTO_MASTER_PATH);
329 
330 	if (show_maps) {
331 		if (options != NULL) {
332 			root->n_options = separated_concat(options,
333 			    root->n_options, ',');
334 		}
335 		if (show_maps > 1) {
336 			node_expand_indirect_maps(root);
337 			node_expand_ampersand(root, NULL);
338 		}
339 		node_expand_defined(root);
340 		node_print(root);
341 		return (0);
342 	}
343 
344 	mount_unmount(root);
345 
346 	return (0);
347 }
348