-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathfusermount.c
More file actions
2086 lines (1817 loc) · 46.8 KB
/
fusermount.c
File metadata and controls
2086 lines (1817 loc) · 46.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
FUSE: Filesystem in Userspace
Copyright (C) 2001-2007 Miklos Szeredi <[email protected]>
This program can be distributed under the terms of the GNU GPLv2.
See the file GPL2.txt.
*/
/* This program does the mounting and unmounting of FUSE filesystems */
#define _GNU_SOURCE /* for clone,strchrnul and close_range */
#include "fuse_config.h"
#include "mount_util.h"
#include "util.h"
#if __linux__
#include "mount_i_linux.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <getopt.h>
#include <errno.h>
#include <fcntl.h>
#include <pwd.h>
#include <paths.h>
#include <mntent.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/param.h>
#include "fuse_mount_compat.h"
#include <sys/fsuid.h>
#include <sys/socket.h>
#include <sys/utsname.h>
#include <sched.h>
#include <stdbool.h>
#include <sys/vfs.h>
#if defined HAVE_CLOSE_RANGE && defined linux
#include <linux/close_range.h>
#endif
#if defined HAVE_LISTMOUNT
#include <linux/mount.h>
#include <syscall.h>
#include <stdint.h>
#endif
#define FUSE_COMMFD_ENV "_FUSE_COMMFD"
static const char *progname;
static int user_allow_other = 0;
static int mount_max = 1000;
static int auto_unmount = 0;
#ifdef GETMNTENT_NEEDS_UNESCAPING
// Older versions of musl libc don't unescape entries in /etc/mtab
// unescapes octal sequences like \040 in-place
// That's ok, because unescaping can not extend the length of the string.
static void unescape(char *buf) {
char *src = buf;
char *dest = buf;
while (1) {
char *next_src = strchrnul(src, '\\');
int offset = next_src - src;
memmove(dest, src, offset);
src = next_src;
dest += offset;
if(*src == '\0') {
*dest = *src;
return;
}
src++;
if('0' <= src[0] && src[0] < '2' &&
'0' <= src[1] && src[1] < '8' &&
'0' <= src[2] && src[2] < '8') {
*dest++ = (src[0] - '0') << 6
| (src[1] - '0') << 3
| (src[2] - '0') << 0;
src += 3;
} else if (src[0] == '\\') {
*dest++ = '\\';
src += 1;
} else {
*dest++ = '\\';
}
}
}
static struct mntent *GETMNTENT(FILE *stream)
{
struct mntent *entp = getmntent(stream);
if(entp != NULL) {
unescape(entp->mnt_fsname);
unescape(entp->mnt_dir);
unescape(entp->mnt_type);
unescape(entp->mnt_opts);
}
return entp;
}
#else
#define GETMNTENT getmntent
#endif // GETMNTENT_NEEDS_UNESCAPING
/*
* Take a ',' separated option string and extract "x-" options
* @original: The original option string
* @regular_opts: The regular options
* @x_prefixed_opts: The "x-" options
*/
static int extract_x_options(const char *original, char **regular_opts,
char **x_prefixed_opts)
{
size_t orig_len;
const char *opt, *opt_end;
orig_len = strlen(original) + 1;
if (*regular_opts != NULL || *x_prefixed_opts != NULL) {
fprintf(stderr, "%s: regular_opts or x_prefixed_opts not NULL\n",
__func__);
return -EINVAL;
}
*regular_opts = calloc(1, orig_len);
*x_prefixed_opts = calloc(1, orig_len);
size_t regular_opts_len = orig_len;
size_t x_prefixed_opts_len = orig_len;
if (*regular_opts == NULL || *x_prefixed_opts == NULL) {
fprintf(stderr, "%s: Failed to allocate %zuB.\n",
__func__, orig_len);
return -ENOMEM;
}
for (opt = original; opt < original + orig_len; opt = opt_end + 1) {
char *opt_buf;
opt_end = strchr(opt, ',');
if (opt_end == NULL)
opt_end = original + orig_len;
size_t opt_len = opt_end - opt;
size_t opt_len_left = orig_len - (opt - original);
size_t buf_len;
bool is_x_prefixed_opts;
if (strncmp(opt, "x-", MIN(2, opt_len_left)) == 0) {
buf_len = x_prefixed_opts_len;
is_x_prefixed_opts = true;
opt_buf = *x_prefixed_opts;
} else {
buf_len = regular_opts_len;
is_x_prefixed_opts = false;
opt_buf = *regular_opts;
}
if (buf_len < orig_len) {
strncat(opt_buf, ",", 2);
buf_len -= 1;
}
/* omits ',' */
if ((ssize_t)(buf_len - opt_len) < 0) {
/* This would be a bug */
fprintf(stderr,
"%s: no buf space left in copy, orig='%s'\n",
__func__, original);
return -EIO;
}
strncat(opt_buf, opt, opt_end - opt);
buf_len -= opt_len;
if (is_x_prefixed_opts)
x_prefixed_opts_len = buf_len;
else
regular_opts_len = buf_len;
}
return 0;
}
static const char *get_user_name(void)
{
struct passwd *pw = getpwuid(getuid());
if (pw != NULL && pw->pw_name != NULL)
return pw->pw_name;
else {
fprintf(stderr, "%s: could not determine username\n", progname);
return NULL;
}
}
static uid_t oldfsuid;
static gid_t oldfsgid;
static void drop_privs(void)
{
if (getuid() != 0) {
oldfsuid = setfsuid(getuid());
oldfsgid = setfsgid(getgid());
}
}
static void restore_privs(void)
{
if (getuid() != 0) {
setfsuid(oldfsuid);
setfsgid(oldfsgid);
}
}
#ifndef IGNORE_MTAB
/*
* Make sure that /etc/mtab is checked and updated atomically
*/
static int lock_umount(void)
{
const char *mtab_lock = _PATH_MOUNTED ".fuselock";
int mtablock;
int res;
struct stat mtab_stat;
/* /etc/mtab could be a symlink to /proc/mounts */
if (lstat(_PATH_MOUNTED, &mtab_stat) == 0 && S_ISLNK(mtab_stat.st_mode))
return -1;
mtablock = open(mtab_lock, O_RDWR | O_CREAT, 0600);
if (mtablock == -1) {
fprintf(stderr, "%s: unable to open fuse lock file: %s\n",
progname, strerror(errno));
return -1;
}
res = lockf(mtablock, F_LOCK, 0);
if (res < 0) {
fprintf(stderr, "%s: error getting lock: %s\n", progname,
strerror(errno));
close(mtablock);
return -1;
}
return mtablock;
}
static void unlock_umount(int mtablock)
{
if (mtablock >= 0) {
int res;
res = lockf(mtablock, F_ULOCK, 0);
if (res < 0) {
fprintf(stderr, "%s: error releasing lock: %s\n",
progname, strerror(errno));
}
close(mtablock);
}
}
static int add_mount(const char *source, const char *mnt, const char *type,
const char *opts)
{
return fuse_mnt_add_mount(progname, source, mnt, type, opts);
}
static int may_unmount(const char *mnt, int quiet)
{
struct mntent *entp;
FILE *fp;
const char *user = NULL;
char uidstr[32];
unsigned uidlen = 0;
int found;
const char *mtab = _PATH_MOUNTED;
user = get_user_name();
if (user == NULL)
return -1;
fp = setmntent(mtab, "r");
if (fp == NULL) {
fprintf(stderr, "%s: failed to open %s: %s\n", progname, mtab,
strerror(errno));
return -1;
}
uidlen = sprintf(uidstr, "%u", getuid());
found = 0;
while ((entp = GETMNTENT(fp)) != NULL) {
if (!found && strcmp(entp->mnt_dir, mnt) == 0 &&
(strcmp(entp->mnt_type, "fuse") == 0 ||
strcmp(entp->mnt_type, "fuseblk") == 0 ||
strncmp(entp->mnt_type, "fuse.", 5) == 0 ||
strncmp(entp->mnt_type, "fuseblk.", 8) == 0)) {
char *p = strstr(entp->mnt_opts, "user=");
if (p &&
(p == entp->mnt_opts || *(p-1) == ',') &&
strcmp(p + 5, user) == 0) {
found = 1;
break;
}
/* /etc/mtab is a link pointing to
/proc/mounts: */
else if ((p =
strstr(entp->mnt_opts, "user_id=")) &&
(p == entp->mnt_opts ||
*(p-1) == ',') &&
strncmp(p + 8, uidstr, uidlen) == 0 &&
(*(p+8+uidlen) == ',' ||
*(p+8+uidlen) == '\0')) {
found = 1;
break;
}
}
}
endmntent(fp);
if (!found) {
if (!quiet)
fprintf(stderr,
"%s: entry for %s not found in %s\n",
progname, mnt, mtab);
return -1;
}
return 0;
}
#endif
/*
* Check whether the file specified in "fusermount3 -u" is really a
* mountpoint and not a symlink. This is necessary otherwise the user
* could move the mountpoint away and replace it with a symlink
* pointing to an arbitrary mount, thereby tricking fusermount3 into
* unmounting that (umount(2) will follow symlinks).
*
* This is the child process running in a separate mount namespace, so
* we don't mess with the global namespace and if the process is
* killed for any reason, mounts are automatically cleaned up.
*
* First make sure nothing is propagated back into the parent
* namespace by marking all mounts "private".
*
* Then bind mount parent onto a stable base where the user can't move
* it around.
*
* Finally check /proc/mounts for an entry matching the requested
* mountpoint. If it's found then we are OK, and the user can't move
* it around within the parent directory as rename() will return
* EBUSY. Be careful to ignore any mounts that existed before the
* bind.
*/
static int check_is_mount_child(void *p)
{
const char **a = p;
const char *last = a[0];
const char *mnt = a[1];
const char *type = a[2];
int res;
const char *procmounts = "/proc/mounts";
int found;
FILE *fp;
const struct mntent *entp;
int count;
res = mount("", "/", "", MS_PRIVATE | MS_REC, NULL);
if (res == -1) {
fprintf(stderr, "%s: failed to mark mounts private: %s\n",
progname, strerror(errno));
return 1;
}
fp = setmntent(procmounts, "r");
if (fp == NULL) {
fprintf(stderr, "%s: failed to open %s: %s\n", progname,
procmounts, strerror(errno));
return 1;
}
count = 0;
while (GETMNTENT(fp) != NULL)
count++;
endmntent(fp);
fp = setmntent(procmounts, "r");
if (fp == NULL) {
fprintf(stderr, "%s: failed to open %s: %s\n", progname,
procmounts, strerror(errno));
return 1;
}
res = mount(".", "/", "", MS_BIND | MS_REC, NULL);
if (res == -1) {
fprintf(stderr, "%s: failed to bind parent to /: %s\n",
progname, strerror(errno));
return 1;
}
found = 0;
while ((entp = GETMNTENT(fp)) != NULL) {
if (count > 0) {
count--;
continue;
}
if (entp->mnt_dir[0] == '/' &&
strcmp(entp->mnt_dir + 1, last) == 0 &&
(!type || strcmp(entp->mnt_type, type) == 0)) {
found = 1;
break;
}
}
endmntent(fp);
if (!found) {
fprintf(stderr, "%s: %s not mounted\n", progname, mnt);
return 1;
}
return 0;
}
static pid_t clone_newns(void *a)
{
char buf[131072];
char *stack = buf + (sizeof(buf) / 2 - ((size_t) buf & 15));
#ifdef __ia64__
extern int __clone2(int (*fn)(void *),
void *child_stack_base, size_t stack_size,
int flags, void *arg, pid_t *ptid,
void *tls, pid_t *ctid);
return __clone2(check_is_mount_child, stack, sizeof(buf) / 2,
CLONE_NEWNS, a, NULL, NULL, NULL);
#else
return clone(check_is_mount_child, stack, CLONE_NEWNS, a);
#endif
}
static int check_is_mount(const char *last, const char *mnt, const char *type)
{
pid_t pid, p;
int status;
const char *a[3] = { last, mnt, type };
pid = clone_newns((void *) a);
if (pid == (pid_t) -1) {
fprintf(stderr, "%s: failed to clone namespace: %s\n",
progname, strerror(errno));
return -1;
}
p = waitpid(pid, &status, __WCLONE);
if (p == (pid_t) -1) {
fprintf(stderr, "%s: waitpid failed: %s\n",
progname, strerror(errno));
return -1;
}
if (!WIFEXITED(status)) {
fprintf(stderr, "%s: child terminated abnormally (status %i)\n",
progname, status);
return -1;
}
if (WEXITSTATUS(status) != 0)
return -1;
return 0;
}
static int chdir_to_parent(char *copy, const char **lastp)
{
char *tmp;
const char *parent;
char buf[65536];
int res;
tmp = strrchr(copy, '/');
if (tmp == NULL || tmp[1] == '\0') {
fprintf(stderr, "%s: internal error: invalid abs path: <%s>\n",
progname, copy);
return -1;
}
if (tmp != copy) {
*tmp = '\0';
parent = copy;
*lastp = tmp + 1;
} else if (tmp[1] != '\0') {
*lastp = tmp + 1;
parent = "/";
} else {
*lastp = ".";
parent = "/";
}
res = chdir(parent);
if (res == -1) {
fprintf(stderr, "%s: failed to chdir to %s: %s\n",
progname, parent, strerror(errno));
return -1;
}
if (getcwd(buf, sizeof(buf)) == NULL) {
fprintf(stderr, "%s: failed to obtain current directory: %s\n",
progname, strerror(errno));
return -1;
}
if (strcmp(buf, parent) != 0) {
fprintf(stderr, "%s: mountpoint moved (%s -> %s)\n", progname,
parent, buf);
return -1;
}
return 0;
}
#ifndef IGNORE_MTAB
static int unmount_fuse_locked(const char *mnt, int quiet, int lazy)
{
int res;
char *copy;
const char *last;
int umount_flags = (lazy ? UMOUNT_DETACH : 0) | UMOUNT_NOFOLLOW;
if (getuid() != 0) {
res = may_unmount(mnt, quiet);
if (res == -1)
return -1;
}
copy = strdup(mnt);
if (copy == NULL) {
fprintf(stderr, "%s: failed to allocate memory\n", progname);
return -1;
}
drop_privs();
res = chdir_to_parent(copy, &last);
if (res == -1) {
restore_privs();
goto out;
}
res = umount2(last, umount_flags);
restore_privs();
if (res == -1 && !quiet) {
fprintf(stderr, "%s: failed to unmount %s: %s\n",
progname, mnt, strerror(errno));
}
out:
free(copy);
if (res == -1)
return -1;
res = chdir("/");
if (res == -1) {
fprintf(stderr, "%s: failed to chdir to '/'\n", progname);
return -1;
}
return fuse_mnt_remove_mount(progname, mnt);
}
static int unmount_fuse(const char *mnt, int quiet, int lazy)
{
int res;
int mtablock = lock_umount();
res = unmount_fuse_locked(mnt, quiet, lazy);
unlock_umount(mtablock);
return res;
}
static int count_fuse_fs_mtab(void)
{
const struct mntent *entp;
int count = 0;
const char *mtab = _PATH_MOUNTED;
FILE *fp = setmntent(mtab, "r");
if (fp == NULL) {
fprintf(stderr, "%s: failed to open %s: %s\n", progname, mtab,
strerror(errno));
return -1;
}
while ((entp = GETMNTENT(fp)) != NULL) {
if (strcmp(entp->mnt_type, "fuse") == 0 ||
strncmp(entp->mnt_type, "fuse.", 5) == 0)
count ++;
}
endmntent(fp);
return count;
}
#ifdef HAVE_LISTMOUNT
static int count_fuse_fs_ls_mnt(void)
{
#define SMBUF_SIZE 1024
#define MNT_ID_LEN 128
int fuse_count = 0;
int n_mounts = 0;
int ret = 0;
uint64_t mnt_ids[MNT_ID_LEN];
unsigned char smbuf[SMBUF_SIZE];
struct mnt_id_req req = {
.size = sizeof(struct mnt_id_req),
};
struct statmount *sm;
for (;;) {
req.mnt_id = LSMT_ROOT;
n_mounts = syscall(SYS_listmount, &req, &mnt_ids, MNT_ID_LEN, 0);
if (n_mounts == -1) {
if (errno != ENOSYS) {
fprintf(stderr, "%s: failed to list mounts: %s\n", progname,
strerror(errno));
}
return -1;
}
for (int i = 0; i < n_mounts; i++) {
req.mnt_id = mnt_ids[i];
req.param = STATMOUNT_FS_TYPE;
ret = syscall(SYS_statmount, &req, &smbuf, SMBUF_SIZE, 0);
if (ret) {
if (errno == ENOENT)
continue;
fprintf(stderr, "%s: failed to stat mount %lld: %s\n", progname,
req.mnt_id, strerror(errno));
return -1;
}
sm = (struct statmount *)smbuf;
if (sm->mask & STATMOUNT_FS_TYPE &&
strcmp(&sm->str[sm->fs_type], "fuse") == 0)
fuse_count++;
}
if (n_mounts < MNT_ID_LEN)
break;
req.param = mnt_ids[MNT_ID_LEN - 1];
}
return fuse_count;
}
static int count_fuse_fs(void)
{
int count = count_fuse_fs_ls_mnt();
return count >= 0 ? count : count_fuse_fs_mtab();
}
#else
static int count_fuse_fs(void)
{
return count_fuse_fs_mtab();
}
#endif
#else /* IGNORE_MTAB */
static int count_fuse_fs(void)
{
return 0;
}
static int add_mount(const char *source, const char *mnt, const char *type,
const char *opts)
{
(void) source;
(void) mnt;
(void) type;
(void) opts;
return 0;
}
static int unmount_fuse(const char *mnt, int quiet, int lazy)
{
(void) quiet;
return fuse_mnt_umount(progname, mnt, mnt, lazy);
}
#endif /* IGNORE_MTAB */
static void strip_line(char *line)
{
char *s = strchr(line, '#');
if (s != NULL)
s[0] = '\0';
for (s = line + strlen(line) - 1;
s >= line && isspace((unsigned char) *s); s--);
s[1] = '\0';
for (s = line; isspace((unsigned char) *s); s++);
if (s != line)
memmove(line, s, strlen(s)+1);
}
static void parse_line(const char *line, int linenum)
{
int tmp;
if (strcmp(line, "user_allow_other") == 0)
user_allow_other = 1;
else if (sscanf(line, "mount_max = %i", &tmp) == 1)
mount_max = tmp;
else if(line[0])
fprintf(stderr,
"%s: unknown parameter in %s at line %i: '%s'\n",
progname, FUSE_CONF, linenum, line);
}
static void read_conf(void)
{
FILE *fp = fopen(FUSE_CONF, "r");
if (fp != NULL) {
int linenum = 1;
char line[256];
int isnewline = 1;
while (fgets(line, sizeof(line), fp) != NULL) {
if (isnewline) {
if (line[strlen(line)-1] == '\n') {
strip_line(line);
parse_line(line, linenum);
} else {
isnewline = 0;
}
} else if(line[strlen(line)-1] == '\n') {
fprintf(stderr, "%s: reading %s: line %i too long\n", progname, FUSE_CONF, linenum);
isnewline = 1;
}
if (isnewline)
linenum ++;
}
if (!isnewline) {
fprintf(stderr, "%s: reading %s: missing newline at end of file\n", progname, FUSE_CONF);
}
if (ferror(fp)) {
fprintf(stderr, "%s: reading %s: read failed\n", progname, FUSE_CONF);
exit(1);
}
fclose(fp);
} else if (errno != ENOENT) {
bool fatal = (errno != EACCES && errno != ELOOP &&
errno != ENAMETOOLONG && errno != ENOTDIR &&
errno != EOVERFLOW);
fprintf(stderr, "%s: failed to open %s: %s\n",
progname, FUSE_CONF, strerror(errno));
if (fatal)
exit(1);
}
}
static int begins_with(const char *s, const char *beg)
{
if (strncmp(s, beg, strlen(beg)) == 0)
return 1;
else
return 0;
}
static int find_mount_flag(const char *s, unsigned len, int *on, int *flag)
{
int i;
for (i = 0; mount_flags[i].opt != NULL; i++) {
const char *opt = mount_flags[i].opt;
if (strlen(opt) == len && strncmp(opt, s, len) == 0) {
*on = mount_flags[i].on;
*flag = mount_flags[i].flag;
if (!mount_flags[i].safe && getuid() != 0) {
*flag = 0;
fprintf(stderr,
"%s: unsafe option %s ignored\n",
progname, opt);
}
return 1;
}
}
return 0;
}
static int add_option(char **optsp, const char *opt, unsigned expand)
{
char *newopts;
if (*optsp == NULL)
newopts = strdup(opt);
else {
unsigned oldsize = strlen(*optsp);
unsigned newsize = oldsize + 1 + strlen(opt) + expand + 1;
newopts = (char *) realloc(*optsp, newsize);
if (newopts)
sprintf(newopts + oldsize, ",%s", opt);
}
if (newopts == NULL) {
fprintf(stderr, "%s: failed to allocate memory\n", progname);
return -1;
}
*optsp = newopts;
return 0;
}
static int get_mnt_opts(int flags, const char *opts, char **mnt_optsp)
{
int i;
int l;
if (!(flags & MS_RDONLY) && add_option(mnt_optsp, "rw", 0) == -1)
return -1;
for (i = 0; mount_flags[i].opt != NULL; i++) {
if (mount_flags[i].on && (flags & mount_flags[i].flag) &&
add_option(mnt_optsp, mount_flags[i].opt, 0) == -1)
return -1;
}
if (add_option(mnt_optsp, opts, 0) == -1)
return -1;
/* remove comma from end of opts*/
l = strlen(*mnt_optsp);
if ((*mnt_optsp)[l-1] == ',')
(*mnt_optsp)[l-1] = '\0';
if (getuid() != 0) {
const char *user = get_user_name();
if (user == NULL)
return -1;
if (add_option(mnt_optsp, "user=", strlen(user)) == -1)
return -1;
strcat(*mnt_optsp, user);
}
return 0;
}
static int opt_eq(const char *s, unsigned len, const char *opt)
{
if(strlen(opt) == len && strncmp(s, opt, len) == 0)
return 1;
else
return 0;
}
static int get_string_opt(const char *s, unsigned len, const char *opt,
char **val)
{
int i;
unsigned opt_len = strlen(opt);
char *d;
if (*val)
free(*val);
*val = (char *) malloc(len - opt_len + 1);
if (!*val) {
fprintf(stderr, "%s: failed to allocate memory\n", progname);
return 0;
}
d = *val;
s += opt_len;
len -= opt_len;
for (i = 0; i < len; i++) {
if (s[i] == '\\' && i + 1 < len)
i++;
*d++ = s[i];
}
*d = '\0';
return 1;
}
/* The kernel silently truncates the "data" argument to PAGE_SIZE-1 characters.
* This can be dangerous if it e.g. truncates the option "group_id=1000" to
* "group_id=1".
* This wrapper detects this case and bails out with an error.
*/
static int mount_notrunc(const char *source, const char *target,
const char *filesystemtype, unsigned long mountflags,
const char *data) {
if (strlen(data) > sysconf(_SC_PAGESIZE) - 1) {
fprintf(stderr, "%s: mount options too long\n", progname);
errno = EINVAL;
return -1;
}
return mount(source, target, filesystemtype, mountflags, data);
}
struct mount_params {
/* Input parameters */
int fd; /* /dev/fuse file descriptor */
mode_t rootmode; /* Root mode from stat */
const char *dev; /* Device path (/dev/fuse) */
/* Parsed mount options */
unsigned long flags; /* Mount flags (MS_NOSUID, etc.) */
char *optbuf; /* Kernel mount options buffer */
char *fsname; /* Filesystem name from options */
char *subtype; /* Subtype from options */
int blkdev; /* Block device flag */
/* Generated mount parameters */
char *source; /* Mount source string */
char *type; /* Filesystem type string */
char *mnt_opts; /* Mount table options */
/* Pointer for optbuf manipulation */
char *optbuf_end; /* Points to end of optbuf for sprintf */
};
static void free_mount_params(struct mount_params *mp)
{
free(mp->optbuf);
free(mp->fsname);
free(mp->subtype);
free(mp->source);
free(mp->type);
free(mp->mnt_opts);
memset(mp, 0, sizeof(*mp));
}
/*
* Check if user has permission to use allow_other or allow_root options.
*
* Returns -1 if permission denied, 0 if allowed or option is not
* allow_other/allow_root.
*/
static int check_allow_permission(const char *opt, unsigned int len)
{
if (getuid() != 0 && !user_allow_other &&
(opt_eq(opt, len, "allow_other") || opt_eq(opt, len, "allow_root"))) {
fprintf(stderr, "%s: option %.*s only allowed if 'user_allow_other' is set in %s\n",
progname, len, opt, FUSE_CONF);
return -1;
}
return 0;
}
/*
* Process generic mount option.
*
* Handles mount flags (ro, rw, suid, etc.), kernel options
* (default_permissions, allow_other, max_read, blksize), or exits on
* unknown options.
*/
static int process_generic_option(const char *opt, unsigned int len,
unsigned long *flags, char **dest)
{
int on;
int flag;
if (find_mount_flag(opt, len, &on, &flag)) {
if (on)
*flags |= flag;
else
*flags &= ~flag;
return 0;
}
if (opt_eq(opt, len, "default_permissions") ||
opt_eq(opt, len, "allow_other") ||
begins_with(opt, "max_read=") ||
begins_with(opt, "blksize=")) {
memcpy(*dest, opt, len);
*dest += len;
**dest = ',';
(*dest)++;
return 0;
}
fprintf(stderr, "%s: unknown option '%.*s'\n", progname, len, opt);
exit(1);
}
static int prepare_mount(const char *opts, struct mount_params *mp)
{
int res;
const char *s;
char *d;
mp->flags = MS_NOSUID | MS_NODEV;
mp->optbuf = (char *) malloc(strlen(opts) + 128);
if (!mp->optbuf) {
fprintf(stderr, "%s: failed to allocate memory\n", progname);
return -1;
}
for (s = opts, d = mp->optbuf; *s;) {
unsigned len;
const char *fsname_str = "fsname=";
const char *subtype_str = "subtype=";
bool escape_ok = begins_with(s, fsname_str) ||
begins_with(s, subtype_str);