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