2 * Copyright (c) 1994 Christopher G. Demetriou
3 * Copyright (c) 1999 Semen Ustimenko
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by Christopher G. Demetriou.
17 * 4. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 * $FreeBSD: src/sbin/mount_ntfs/mount_ntfs.c,v 1.3.2.2 2001/10/12 22:08:43 semenu Exp $
35 #include <sys/cdefs.h>
36 #include <sys/param.h>
38 #include <sys/mount.h>
40 #include <ntfs/ntfsmount.h>
54 static struct mntopt mopts[] = {
59 static gid_t a_gid __P((char *));
60 static uid_t a_uid __P((char *));
61 static mode_t a_mask __P((char *));
62 static void usage __P((void)) __dead2;
64 static void load_u2wtable __P((struct ntfs_args *, char *));
71 struct ntfs_args args;
73 int c, mntflags, set_gid, set_uid, set_mask, error;
74 char *dev, *dir, mntpath[MAXPATHLEN];
75 #if __FreeBSD_version >= 300000
81 mntflags = set_gid = set_uid = set_mask = 0;
82 (void)memset(&args, '\0', sizeof(args));
84 while ((c = getopt(argc, argv, "aiu:g:m:o:W:")) != -1) {
87 args.uid = a_uid(optarg);
91 args.gid = a_gid(optarg);
95 args.mode = a_mask(optarg);
99 args.flag |= NTFS_MFLAG_CASEINS;
102 args.flag |= NTFS_MFLAG_ALLNAMES;
105 getmntopts(optarg, mopts, &mntflags, 0);
108 load_u2wtable(&args, optarg);
109 args.flag |= NTFSMNT_U2WTABLE;
118 if (optind + 2 != argc)
122 dir = argv[optind + 1];
125 * Resolve the mountpoint with realpath(3) and remove unnecessary
126 * slashes from the devicename if there are any.
128 (void)checkpath(dir, mntpath);
129 (void)rmslashes(dev, dev);
132 args.export.ex_root = 65534; /* unchecked anyway on DOS fs */
133 if (mntflags & MNT_RDONLY)
134 args.export.ex_flags = MNT_EXRDONLY;
136 args.export.ex_flags = 0;
137 if (!set_gid || !set_uid || !set_mask) {
138 if (stat(mntpath, &sb) == -1)
139 err(EX_OSERR, "stat %s", mntpath);
142 args.uid = sb.st_uid;
144 args.gid = sb.st_gid;
146 args.mode = sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
149 #if __FreeBSD_version >= 300000
150 error = getvfsbyname("ntfs", &vfc);
151 if(error && vfsisloadable("ntfs")) {
154 vfc = getvfsbyname("ntfs");
155 if(!vfc && vfsisloadable("ntfs")) {
158 err(EX_OSERR, "vfsload(ntfs)");
159 endvfsent(); /* clear cache */
160 #if __FreeBSD_version >= 300000
161 error = getvfsbyname("ntfs", &vfc);
163 vfc = getvfsbyname("ntfs");
166 #if __FreeBSD_version >= 300000
171 errx(EX_OSERR, "ntfs filesystem is not available");
173 #if __FreeBSD_version >= 300000
174 if (mount(vfc.vfc_name, mntpath, mntflags, &args) < 0)
176 if (mount(vfc->vfc_index, mntpath, mntflags, &args) < 0)
178 err(EX_OSERR, "%s", dev);
191 if ((gr = getgrnam(s)) != NULL)
194 for (gname = s; *s && isdigit(*s); ++s);
198 errx(EX_NOUSER, "unknown group id: %s", gname);
211 if ((pw = getpwnam(s)) != NULL)
214 for (uname = s; *s && isdigit(*s); ++s);
218 errx(EX_NOUSER, "unknown user id: %s", uname);
231 if (*s >= '0' && *s <= '7') {
233 rv = strtol(optarg, &ep, 8);
235 if (!done || rv < 0 || *ep)
236 errx(EX_USAGE, "invalid file mode: %s", s);
243 fprintf(stderr, "usage: mount_ntfs [-a] [-i] [-u user] [-g group] [-m mask] [-W u2wtable] bdev dir\n");
248 load_u2wtable (pargs, name)
249 struct ntfs_args *pargs;
261 snprintf(buf, sizeof(buf), "/usr/libdata/msdosfs/%s", name);
265 if ((f = fopen(fn, "r")) == NULL)
266 err(EX_NOINPUT, "%s", fn);
268 for (i = 0; i < 16; i++) {
270 if (p != NULL) free(p);
271 if ((p = s = fparseln(f, NULL, &line, NULL, 0)) == NULL)
272 errx(EX_DATAERR, "can't read u2w table row %d near line %d", i, line);
273 while (isspace((unsigned char)*s))
275 } while (*s == '\0');
276 if (sscanf(s, "%i%i%i%i%i%i%i%i",
277 code, code + 1, code + 2, code + 3, code + 4, code + 5, code + 6, code + 7) != 8)
278 errx(EX_DATAERR, "u2w table: missing item(s) in row %d, line %d", i, line);
279 for (j = 0; j < 8; j++)
280 pargs->u2w[i * 8 + j] = code[j];
282 for (i = 0; i < 16; i++) {
285 if ((p = s = fparseln(f, NULL, &line, NULL, 0)) == NULL)
286 errx(EX_DATAERR, "can't read d2u table row %d near line %d", i, line);
287 while (isspace((unsigned char)*s))
289 } while (*s == '\0');
290 if (sscanf(s, "%i%i%i%i%i%i%i%i",
291 code, code + 1, code + 2, code + 3, code + 4, code + 5, code + 6, code + 7) != 8)
292 errx(EX_DATAERR, "d2u table: missing item(s) in row %d, line %d", i, line);
293 for (j = 0; j < 8; j++)
294 /* pargs->d2u[i * 8 + j] = code[j] */;
296 for (i = 0; i < 16; i++) {
299 if ((p = s = fparseln(f, NULL, &line, NULL, 0)) == NULL)
300 errx(EX_DATAERR, "can't read u2d table row %d near line %d", i, line);
301 while (isspace((unsigned char)*s))
303 } while (*s == '\0');
304 if (sscanf(s, "%i%i%i%i%i%i%i%i",
305 code, code + 1, code + 2, code + 3, code + 4, code + 5, code + 6, code + 7) != 8)
306 errx(EX_DATAERR, "u2d table: missing item(s) in row %d, line %d", i, line);
307 for (j = 0; j < 8; j++)
308 /* pargs->u2d[i * 8 + j] = code[j] */;