xref: /freebsd/sbin/mount_msdosfs/mount_msdosfs.c (revision a316b26e50bbed7cf655fbba726ab87d8ab7599d)
1 /*
2  * Copyright (c) 1994 Christopher G. Demetriou
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by Christopher G. Demetriou.
16  * 4. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #ifndef lint
32 static char rcsid[] = "$Id: mount_msdos.c,v 1.2 1994/09/22 22:16:35 wollman Exp $";
33 #endif /* not lint */
34 
35 #include <sys/cdefs.h>
36 #include <sys/param.h>
37 #define MSDOSFS
38 #include <sys/mount.h>
39 #include <sys/stat.h>
40 #include <ctype.h>
41 #include <err.h>
42 #include <grp.h>
43 #include <pwd.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <unistd.h>
48 
49 #include "mntopts.h"
50 
51 struct mntopt mopts[] = {
52 	MOPT_STDOPTS,
53 	{ NULL }
54 };
55 
56 gid_t	a_gid __P((char *));
57 uid_t	a_uid __P((char *));
58 mode_t	a_mask __P((char *));
59 void	usage __P((void));
60 
61 int
62 main(argc, argv)
63 	int argc;
64 	char **argv;
65 {
66 	struct msdosfs_args args;
67 	struct stat sb;
68 	int c, mntflags, set_gid, set_uid, set_mask;
69 	char *dev, *dir, ndir[MAXPATHLEN+1];
70 	struct vfsconf *vfc;
71 
72 	mntflags = set_gid = set_uid = set_mask = 0;
73 	(void)memset(&args, '\0', sizeof(args));
74 
75 	while ((c = getopt(argc, argv, "u:g:m:o:")) != EOF) {
76 		switch (c) {
77 		case 'u':
78 			args.uid = a_uid(optarg);
79 			set_uid = 1;
80 			break;
81 		case 'g':
82 			args.gid = a_gid(optarg);
83 			set_gid = 1;
84 			break;
85 		case 'm':
86 			args.mask = a_mask(optarg);
87 			set_mask = 1;
88 			break;
89 		case 'o':
90 			getmntopts(optarg, mopts, &mntflags, 0);
91 			break;
92 		case '?':
93 		default:
94 			usage();
95 			break;
96 		}
97 	}
98 
99 	if (optind + 2 != argc)
100 		usage();
101 
102 	dev = argv[optind];
103 	dir = argv[optind + 1];
104 	if (dir[0] != '/') {
105 		warnx("\"%s\" is a relative path.", dir);
106 		if (getcwd(ndir, sizeof(ndir)) == NULL)
107 			err(1, "getcwd");
108 		strncat(ndir, "/", sizeof(ndir) - strlen(ndir) - 1);
109 		strncat(ndir, dir, sizeof(ndir) - strlen(ndir) - 1);
110 		dir = ndir;
111 		warnx("using \"%s\" instead.", dir);
112 	}
113 
114 	args.fspec = dev;
115 	args.export.ex_root = -2;	/* unchecked anyway on DOS fs */
116 	if (mntflags & MNT_RDONLY)
117 		args.export.ex_flags = MNT_EXRDONLY;
118 	else
119 		args.export.ex_flags = 0;
120 	if (!set_gid || !set_uid || !set_mask) {
121 		if (stat(dir, &sb) == -1)
122 			err(1, "stat %s", dir);
123 
124 		if (!set_uid)
125 			args.uid = sb.st_uid;
126 		if (!set_gid)
127 			args.gid = sb.st_gid;
128 		if (!set_mask)
129 			args.mask = sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
130 	}
131 
132 	vfc = getvfsbyname("msdos");
133 	if(!vfc && vfsisloadable("msdos")) {
134 		if(vfsload("msdos"))
135 			err(1, "vfsload(msdos)");
136 		endvfsent();	/* clear cache */
137 		vfc = getvfsbyname("msdos");
138 	}
139 
140 	if (mount(vfc ? vfc->vfc_index : MOUNT_MSDOS, dir, mntflags, &args) < 0)
141 		err(1, "mount");
142 
143 	exit (0);
144 }
145 
146 gid_t
147 a_gid(s)
148 	char *s;
149 {
150 	struct group *gr;
151 	char *gname;
152 	gid_t gid;
153 
154 	if ((gr = getgrnam(s)) != NULL)
155 		gid = gr->gr_gid;
156 	else {
157 		for (gname = s; *s && isdigit(*s); ++s);
158 		if (!*s)
159 			gid = atoi(gname);
160 		else
161 			errx(1, "unknown group id: %s", gname);
162 	}
163 	return (gid);
164 }
165 
166 uid_t
167 a_uid(s)
168 	char *s;
169 {
170 	struct passwd *pw;
171 	char *uname;
172 	uid_t uid;
173 
174 	if ((pw = getpwnam(s)) != NULL)
175 		uid = pw->pw_uid;
176 	else {
177 		for (uname = s; *s && isdigit(*s); ++s);
178 		if (!*s)
179 			uid = atoi(uname);
180 		else
181 			errx(1, "unknown user id: %s", uname);
182 	}
183 	return (uid);
184 }
185 
186 mode_t
187 a_mask(s)
188 	char *s;
189 {
190 	int done, rv;
191 	char *ep;
192 
193 	done = 0;
194 	if (*s >= '0' && *s <= '7') {
195 		done = 1;
196 		rv = strtol(optarg, &ep, 8);
197 	}
198 	if (!done || rv < 0 || *ep)
199 		errx(1, "invalid file mode: %s", s);
200 	return (rv);
201 }
202 
203 void
204 usage()
205 {
206 	fprintf(stderr, "usage: mount_msdos [-F flags] [-u user] [-g group] [-m mask] bdev dir\n");
207 	exit(1);
208 }
209