1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2005 Jean-Sebastien Pedron
5 * Copyright (c) 2005 Csaba Henk
6 * All rights reserved.
7 *
8 * Copyright (c) 2019 The FreeBSD Foundation
9 *
10 * Portions of this software were developed by BFF Storage Systems under
11 * sponsorship from the FreeBSD Foundation.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 */
35
36 #include <sys/param.h>
37 #include <sys/mount.h>
38 #include <sys/uio.h>
39 #include <sys/stat.h>
40 #include <sys/sysctl.h>
41
42 #include <err.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <sysexits.h>
47 #include <unistd.h>
48 #include <fcntl.h>
49 #include <signal.h>
50 #include <getopt.h>
51 #include <limits.h>
52 #include <mntopts.h>
53 #include <osreldate.h>
54 #include <paths.h>
55
56 #ifndef FUSE4BSD_VERSION
57 #define FUSE4BSD_VERSION "0.3.9-pre1"
58 #endif
59
60 void __usage_short(void);
61 void usage(void);
62 void helpmsg(void);
63 void showversion(void);
64
65 static struct mntopt mopts[] = {
66 #define ALTF_PRIVATE 0x01
67 { "private", 0, ALTF_PRIVATE, 1 },
68 { "neglect_shares", 0, 0x02, 1 },
69 { "push_symlinks_in", 0, 0x04, 1 },
70 { "allow_other", 0, 0x08, 1 },
71 { "default_permissions", 0, 0x10, 1 },
72 #define ALTF_MAXREAD 0x20
73 { "max_read=", 0, ALTF_MAXREAD, 1 },
74 #define ALTF_SUBTYPE 0x40
75 { "subtype=", 0, ALTF_SUBTYPE, 1 },
76 #define ALTF_FSNAME 0x80
77 { "fsname=", 0, ALTF_FSNAME, 1 },
78 /*
79 * MOPT_AUTOMOUNTED, included by MOPT_STDOPTS, does not fit into
80 * the 'flags' argument to nmount(2). We have to abuse altflags
81 * to pass it, as string, via iovec.
82 */
83 #define ALTF_AUTOMOUNTED 0x100
84 { "automounted", 0, ALTF_AUTOMOUNTED, 1 },
85 #define ALTF_INTR 0x200
86 { "intr", 0, ALTF_INTR, 1 },
87 /* Linux specific options, we silently ignore them */
88 { "fd=", 0, 0x00, 1 },
89 { "rootmode=", 0, 0x00, 1 },
90 { "user_id=", 0, 0x00, 1 },
91 { "group_id=", 0, 0x00, 1 },
92 { "large_read", 0, 0x00, 1 },
93 /* "nonempty", just the first two chars are stripped off during parsing */
94 { "nempty", 0, 0x00, 1 },
95 { "async", 0, MNT_ASYNC, 0},
96 { "noasync", 1, MNT_ASYNC, 0},
97 MOPT_STDOPTS,
98 MOPT_END
99 };
100
101 struct mntval {
102 int mv_flag;
103 void *mv_value;
104 int mv_len;
105 };
106
107 static struct mntval mvals[] = {
108 { ALTF_MAXREAD, NULL, 0 },
109 { ALTF_SUBTYPE, NULL, 0 },
110 { ALTF_FSNAME, NULL, 0 },
111 { 0, NULL, 0 }
112 };
113
114 #define DEFAULT_MOUNT_FLAGS ALTF_PRIVATE
115
116 int
main(int argc,char * argv[])117 main(int argc, char *argv[])
118 {
119 struct iovec *iov;
120 int mntflags, iovlen, verbose = 0;
121 char *dev = NULL, *dir = NULL, mntpath[MAXPATHLEN];
122 char *devo = NULL, *diro = NULL;
123 char ndev[128], fdstr[15];
124 int i, done = 0, reject_allow_other = 0, safe_level = 0;
125 int altflags = DEFAULT_MOUNT_FLAGS;
126 int __altflags = DEFAULT_MOUNT_FLAGS;
127 int ch = 0;
128 struct mntopt *mo;
129 struct mntval *mv;
130 static struct option longopts[] = {
131 {"reject-allow_other", no_argument, NULL, 'A'},
132 {"safe", no_argument, NULL, 'S'},
133 {"daemon", required_argument, NULL, 'D'},
134 {"daemon_opts", required_argument, NULL, 'O'},
135 {"special", required_argument, NULL, 's'},
136 {"mountpath", required_argument, NULL, 'm'},
137 {"version", no_argument, NULL, 'V'},
138 {"help", no_argument, NULL, 'h'},
139 {0,0,0,0}
140 };
141 int pid = 0;
142 int fd = -1, fdx;
143 char *ep;
144 char *daemon_str = NULL, *daemon_opts = NULL;
145
146 /*
147 * We want a parsing routine which is not sensitive to
148 * the position of args/opts; it should extract the
149 * first two args and stop at the beginning of the rest.
150 * (This makes it easier to call mount_fusefs from external
151 * utils than it is with a strict "util flags args" syntax.)
152 */
153
154 iov = NULL;
155 iovlen = 0;
156 mntflags = 0;
157 /* All in all, I feel it more robust this way... */
158 unsetenv("POSIXLY_CORRECT");
159 if (getenv("MOUNT_FUSEFS_IGNORE_UNKNOWN"))
160 getmnt_silent = 1;
161 if (getenv("MOUNT_FUSEFS_VERBOSE"))
162 verbose = 1;
163
164 do {
165 for (i = 0; i < 3; i++) {
166 if (optind < argc && argv[optind][0] != '-') {
167 if (dir) {
168 done = 1;
169 break;
170 }
171 if (dev)
172 dir = argv[optind];
173 else
174 dev = argv[optind];
175 optind++;
176 }
177 }
178 switch(ch) {
179 case 'A':
180 reject_allow_other = 1;
181 break;
182 case 'S':
183 safe_level = 1;
184 break;
185 case 'D':
186 if (daemon_str)
187 errx(1, "daemon specified inconsistently");
188 daemon_str = optarg;
189 break;
190 case 'O':
191 if (daemon_opts)
192 errx(1, "daemon opts specified inconsistently");
193 daemon_opts = optarg;
194 break;
195 case 'o':
196 getmntopts(optarg, mopts, &mntflags, &altflags);
197 for (mv = mvals; mv->mv_flag; ++mv) {
198 if (! (altflags & mv->mv_flag))
199 continue;
200 for (mo = mopts; mo->m_flag; ++mo) {
201 char *p, *q;
202
203 if (mo->m_flag != mv->mv_flag)
204 continue;
205 p = strstr(optarg, mo->m_option);
206 if (p) {
207 p += strlen(mo->m_option);
208 q = p;
209 while (*q != '\0' && *q != ',')
210 q++;
211 mv->mv_len = q - p + 1;
212 mv->mv_value = malloc(mv->mv_len);
213 if (mv->mv_value == NULL)
214 err(1, "malloc");
215 memcpy(mv->mv_value, p, mv->mv_len - 1);
216 ((char *)mv->mv_value)[mv->mv_len - 1] = '\0';
217 break;
218 }
219 }
220 }
221 break;
222 case 's':
223 if (devo)
224 errx(1, "special specified inconsistently");
225 devo = optarg;
226 break;
227 case 'm':
228 if (diro)
229 errx(1, "mount path specified inconsistently");
230 diro = optarg;
231 break;
232 case 'v':
233 verbose = 1;
234 break;
235 case 'h':
236 helpmsg();
237 break;
238 case 'V':
239 showversion();
240 break;
241 case '\0':
242 break;
243 case '?':
244 default:
245 usage();
246 }
247 if (done)
248 break;
249 } while ((ch = getopt_long(argc, argv, "AvVho:SD:O:s:m:", longopts, NULL)) != -1);
250
251 argc -= optind;
252 argv += optind;
253
254 if (devo) {
255 if (dev)
256 errx(1, "special specified inconsistently");
257 dev = devo;
258 } else if (diro)
259 errx(1, "if mountpoint is given via an option, special should also be given via an option");
260
261 if (diro) {
262 if (dir)
263 errx(1, "mount path specified inconsistently");
264 dir = diro;
265 }
266
267 if ((! dev) && argc > 0) {
268 dev = *argv++;
269 argc--;
270 }
271
272 if ((! dir) && argc > 0) {
273 dir = *argv++;
274 argc--;
275 }
276
277 if (! (dev && dir))
278 errx(1, "missing special and/or mountpoint");
279
280 for (mo = mopts; mo->m_flag; ++mo) {
281 if (altflags & mo->m_flag) {
282 int iov_done = 0;
283
284 if (reject_allow_other &&
285 strcmp(mo->m_option, "allow_other") == 0)
286 /*
287 * reject_allow_other is stronger than a
288 * negative of allow_other: if this is set,
289 * allow_other is blocked, period.
290 */
291 errx(1, "\"allow_other\" usage is banned by respective option");
292
293 for (mv = mvals; mv->mv_flag; ++mv) {
294 if (mo->m_flag != mv->mv_flag)
295 continue;
296 if (mv->mv_value) {
297 build_iovec(&iov, &iovlen, mo->m_option, mv->mv_value, mv->mv_len);
298 iov_done = 1;
299 break;
300 }
301 }
302 if (! iov_done)
303 build_iovec(&iov, &iovlen, mo->m_option,
304 __DECONST(void *, ""), -1);
305 }
306 if (__altflags & mo->m_flag) {
307 char *uscore_opt;
308
309 if (asprintf(&uscore_opt, "__%s", mo->m_option) == -1)
310 err(1, "failed to allocate memory");
311 build_iovec(&iov, &iovlen, uscore_opt,
312 __DECONST(void *, ""), -1);
313 free(uscore_opt);
314 }
315 }
316
317 if (getenv("MOUNT_FUSEFS_SAFE"))
318 safe_level = 1;
319
320 if (safe_level > 0 && (argc > 0 || daemon_str || daemon_opts))
321 errx(1, "safe mode, spawning daemon not allowed");
322
323 if ((argc > 0 && (daemon_str || daemon_opts)) ||
324 (daemon_opts && ! daemon_str))
325 errx(1, "daemon specified inconsistently");
326
327 /*
328 * Resolve the mountpoint with realpath(3) and remove unnecessary
329 * slashes from the devicename if there are any.
330 */
331 if (checkpath(dir, mntpath) != 0)
332 err(1, "%s", mntpath);
333 (void)rmslashes(dev, dev);
334
335 if (strcmp(dev, "auto") == 0)
336 dev = __DECONST(char *, "/dev/fuse");
337
338 if (strcmp(dev, "/dev/fuse") == 0) {
339 if (! (argc > 0 || daemon_str)) {
340 fprintf(stderr, "Please also specify the fuse daemon to run when mounting via the multiplexer!\n");
341 usage();
342 }
343 if ((fd = open(dev, O_RDWR)) < 0)
344 err(1, "failed to open fuse device");
345 } else {
346 fdx = strtol(dev, &ep, 10);
347 if (*ep == '\0')
348 fd = fdx;
349 }
350
351 /* Identifying device */
352 if (fd >= 0) {
353 struct stat sbuf;
354 char *ndevbas, *lep;
355
356 if (fstat(fd, &sbuf) == -1)
357 err(1, "cannot stat device file descriptor");
358
359 strcpy(ndev, _PATH_DEV);
360 ndevbas = ndev + strlen(_PATH_DEV);
361 devname_r(sbuf.st_rdev, S_IFCHR, ndevbas,
362 sizeof(ndev) - strlen(_PATH_DEV));
363
364 if (strncmp(ndevbas, "fuse", 4))
365 errx(1, "mounting inappropriate device");
366
367 strtol(ndevbas + 4, &lep, 10);
368 if (*lep != '\0')
369 errx(1, "mounting inappropriate device");
370
371 dev = ndev;
372 }
373
374 if (argc > 0 || daemon_str) {
375 char *fds;
376
377 if (fd < 0 && (fd = open(dev, O_RDWR)) < 0)
378 err(1, "failed to open fuse device");
379
380 if (asprintf(&fds, "%d", fd) == -1)
381 err(1, "failed to allocate memory");
382 setenv("FUSE_DEV_FD", fds, 1);
383 free(fds);
384 setenv("FUSE_NO_MOUNT", "1", 1);
385
386 if (daemon_str) {
387 char *bgdaemon;
388 int len;
389
390 if (! daemon_opts)
391 daemon_opts = __DECONST(char *, "");
392
393 len = strlen(daemon_str) + 1 + strlen(daemon_opts) +
394 2 + 1;
395 bgdaemon = calloc(1, len);
396
397 if (! bgdaemon)
398 err(1, "failed to allocate memory");
399
400 strlcpy(bgdaemon, daemon_str, len);
401 strlcat(bgdaemon, " ", len);
402 strlcat(bgdaemon, daemon_opts, len);
403 strlcat(bgdaemon, " &", len);
404
405 if (system(bgdaemon))
406 err(1, "failed to call fuse daemon");
407 } else {
408 if ((pid = fork()) < 0)
409 err(1, "failed to fork for fuse daemon");
410
411 if (pid == 0) {
412 execvp(argv[0], argv);
413 err(1, "failed to exec fuse daemon");
414 }
415 }
416 }
417
418 /* Prepare the options vector for nmount(). build_iovec() is declared
419 * in mntopts.h. */
420 sprintf(fdstr, "%d", fd);
421 build_iovec(&iov, &iovlen, "fstype", __DECONST(void *, "fusefs"), -1);
422 build_iovec(&iov, &iovlen, "fspath", mntpath, -1);
423 build_iovec(&iov, &iovlen, "from", dev, -1);
424 build_iovec(&iov, &iovlen, "fd", fdstr, -1);
425
426 if (verbose)
427 fprintf(stderr, "mounting fuse daemon on device %s\n", dev);
428
429 if (nmount(iov, iovlen, mntflags) < 0)
430 err(EX_OSERR, "%s on %s", dev, mntpath);
431
432 exit(0);
433 }
434
435 void
__usage_short(void)436 __usage_short(void) {
437 fprintf(stderr,
438 "usage:\n%s [-A|-S|-v|-V|-h|-D daemon|-O args|-s special|-m node|-o option...] special node [daemon args...]\n\n",
439 getprogname());
440 }
441
442 void
usage(void)443 usage(void)
444 {
445 struct mntopt *mo;
446
447 __usage_short();
448
449 fprintf(stderr, "known options:\n");
450 for (mo = mopts; mo->m_flag; ++mo)
451 fprintf(stderr, "\t%s\n", mo->m_option);
452
453 fprintf(stderr, "\n(use -h for a detailed description of these options)\n");
454 exit(EX_USAGE);
455 }
456
457 void
helpmsg(void)458 helpmsg(void)
459 {
460 if (! getenv("MOUNT_FUSEFS_CALL_BY_LIB")) {
461 __usage_short();
462 fprintf(stderr, "description of options:\n");
463 }
464
465 /*
466 * The main use case of this function is giving info embedded in general
467 * FUSE lib help output. Therefore the style and the content of the output
468 * tries to fit there as much as possible.
469 */
470 fprintf(stderr,
471 " -o allow_other allow access to other users\n"
472 /* " -o nonempty allow mounts over non-empty file/dir\n" */
473 " -o default_permissions enable permission checking by kernel\n"
474 " -o intr interruptible mount\n"
475 " -o fsname=NAME set filesystem name\n"
476 /*
477 " -o large_read issue large read requests (2.4 only)\n"
478 */
479 " -o subtype=NAME set filesystem type\n"
480 " -o max_read=N set maximum size of read requests\n"
481 " -o noprivate allow secondary mounting of the filesystem\n"
482 " -o neglect_shares don't report EBUSY when unmount attempted\n"
483 " in presence of secondary mounts\n"
484 " -o push_symlinks_in prefix absolute symlinks with mountpoint\n"
485 );
486 exit(EX_USAGE);
487 }
488
489 void
showversion(void)490 showversion(void)
491 {
492 puts("mount_fusefs [fuse4bsd] version: " FUSE4BSD_VERSION);
493 exit(EX_USAGE);
494 }
495