1460f24ecSagc /*
29c194566Slukem * Copyright (c) 2007 Alistair Crooks. All rights reserved.
3460f24ecSagc *
4460f24ecSagc * Redistribution and use in source and binary forms, with or without
5460f24ecSagc * modification, are permitted provided that the following conditions
6460f24ecSagc * are met:
7460f24ecSagc * 1. Redistributions of source code must retain the above copyright
8460f24ecSagc * notice, this list of conditions and the following disclaimer.
9460f24ecSagc * 2. Redistributions in binary form must reproduce the above copyright
10460f24ecSagc * notice, this list of conditions and the following disclaimer in the
11460f24ecSagc * documentation and/or other materials provided with the distribution.
12460f24ecSagc * 3. The name of the author may not be used to endorse or promote
13460f24ecSagc * products derived from this software without specific prior written
14460f24ecSagc * permission.
15460f24ecSagc *
16460f24ecSagc * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
17460f24ecSagc * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18460f24ecSagc * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19460f24ecSagc * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
20460f24ecSagc * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21460f24ecSagc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
22460f24ecSagc * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23460f24ecSagc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24460f24ecSagc * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25460f24ecSagc * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26460f24ecSagc * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27460f24ecSagc */
28460f24ecSagc #include <sys/cdefs.h>
29460f24ecSagc
30460f24ecSagc #ifndef lint
319c194566Slukem __COPYRIGHT("@(#) Copyright (c) 2007\
32460f24ecSagc The NetBSD Foundation, Inc. All rights reserved.");
33*e31f1e95Sandvar __RCSID("$NetBSD: fusermount.c,v 1.4 2023/04/05 21:53:56 andvar Exp $");
34460f24ecSagc #endif
35460f24ecSagc
36460f24ecSagc #include <sys/types.h>
37460f24ecSagc #include <sys/param.h>
38460f24ecSagc #include <sys/mount.h>
39460f24ecSagc
40460f24ecSagc #include <err.h>
41460f24ecSagc #include <stdio.h>
42460f24ecSagc #include <stdlib.h>
43460f24ecSagc #include <string.h>
44460f24ecSagc #include <unistd.h>
45460f24ecSagc
46c3f5008eSagc #ifndef FUSERMOUNT_VERSION
47c3f5008eSagc #define FUSERMOUNT_VERSION "2.6.0"
48c3f5008eSagc #endif
49c3f5008eSagc
50460f24ecSagc enum {
51460f24ecSagc FlagCheckPerm = 1,
52460f24ecSagc FlagKernelCache,
53460f24ecSagc FlagNonrootUsers,
54460f24ecSagc
55460f24ecSagc ActionMount,
56460f24ecSagc ActionUnmount
57460f24ecSagc };
58460f24ecSagc
59460f24ecSagc /* unmount mount point(s) */
60460f24ecSagc static int
refuse_unmount(int argc,char ** argv)61460f24ecSagc refuse_unmount(int argc, char **argv)
62460f24ecSagc {
63460f24ecSagc int ret;
64460f24ecSagc int i;
65460f24ecSagc
66460f24ecSagc for (ret = 1, i = 0 ; i < argc ; i++) {
67460f24ecSagc if (unmount(argv[i], 0) < 0) {
68460f24ecSagc warn("can't unmount `%s'", argv[i]);
69460f24ecSagc ret = 0;
70460f24ecSagc }
71460f24ecSagc }
72460f24ecSagc return ret;
73460f24ecSagc }
74460f24ecSagc
75*e31f1e95Sandvar /* print the usage message */
76460f24ecSagc static void
usage(const char * prog)77460f24ecSagc usage(const char *prog)
78460f24ecSagc {
79460f24ecSagc (void) fprintf(stderr,
80460f24ecSagc "Usage: %s [-c] [-d name] [-h] [-p] [-u] [-x] mountpoint...\n",
81460f24ecSagc prog);
82460f24ecSagc (void) fprintf(stderr, "\t-c\tuse kernel cache\n");
83460f24ecSagc (void) fprintf(stderr, "\t-d name\tuse name in mount information\n");
84460f24ecSagc (void) fprintf(stderr, "\t-h\tprint help information\n");
85460f24ecSagc (void) fprintf(stderr, "\t-p\tcheck file permissions\n");
86460f24ecSagc (void) fprintf(stderr, "\t-u\tunmount mount point(s)\n");
87460f24ecSagc (void) fprintf(stderr,
88460f24ecSagc "\t-x\tallow access to mortal (non-root) users\n");
89460f24ecSagc }
90460f24ecSagc
91460f24ecSagc int
main(int argc,char ** argv)92460f24ecSagc main(int argc, char **argv)
93460f24ecSagc {
94460f24ecSagc char *progname;
95460f24ecSagc char *execme;
96460f24ecSagc int flags;
97460f24ecSagc int action;
98460f24ecSagc int i;
99460f24ecSagc
100460f24ecSagc progname = NULL;
101460f24ecSagc flags = 0;
102460f24ecSagc action = ActionMount;
103c3f5008eSagc while ((i = getopt(argc, argv, "Vcd:hpux")) != -1) {
104460f24ecSagc switch(i) {
105c3f5008eSagc case 'V':
106c3f5008eSagc printf("fusermount version: %s\n", FUSERMOUNT_VERSION);
107c3f5008eSagc exit(EXIT_SUCCESS);
108c3f5008eSagc /* NOTREACHED */
109460f24ecSagc case 'c':
110460f24ecSagc flags |= FlagKernelCache;
111460f24ecSagc break;
112460f24ecSagc case 'd':
113460f24ecSagc progname = optarg;
114460f24ecSagc break;
115460f24ecSagc case 'h':
116460f24ecSagc usage(*argv);
117460f24ecSagc exit(EXIT_SUCCESS);
118460f24ecSagc case 'p':
119460f24ecSagc flags |= FlagCheckPerm;
120460f24ecSagc break;
121460f24ecSagc case 'u':
122460f24ecSagc action = ActionUnmount;
123460f24ecSagc break;
124460f24ecSagc case 'x':
125460f24ecSagc if (geteuid() != 0) {
126460f24ecSagc err(EXIT_FAILURE,
127460f24ecSagc "-x option is only allowed for use by root");
128460f24ecSagc }
129460f24ecSagc flags |= FlagNonrootUsers;
130460f24ecSagc break;
131460f24ecSagc default:
132460f24ecSagc warnx("Unrecognised argument `%c'", i);
133460f24ecSagc usage(*argv);
134460f24ecSagc exit(EXIT_FAILURE);
135460f24ecSagc }
136460f24ecSagc }
137460f24ecSagc if (optind >= argc - 2) {
138460f24ecSagc warnx("Not enough command line arguments");
139460f24ecSagc usage(*argv);
140460f24ecSagc exit(EXIT_FAILURE);
141460f24ecSagc }
142460f24ecSagc execme = argv[optind + 1];
143460f24ecSagc if (progname) {
144460f24ecSagc argv[optind + 1] = progname;
145460f24ecSagc }
146460f24ecSagc /* mountpoint = argv[optind]; */
147460f24ecSagc switch(action) {
148460f24ecSagc case ActionMount:
149460f24ecSagc execvp(execme, &argv[optind + 1]);
150460f24ecSagc break;
151460f24ecSagc case ActionUnmount:
152460f24ecSagc if (!refuse_unmount(argc - optind, argv + optind)) {
153460f24ecSagc exit(EXIT_FAILURE);
154460f24ecSagc }
155460f24ecSagc break;
156460f24ecSagc }
157460f24ecSagc exit(EXIT_SUCCESS);
158460f24ecSagc }
159