mount_ntfs.c revision 83229
1/*
2 * Copyright (c) 1994 Christopher G. Demetriou
3 * Copyright (c) 1999 Semen Ustimenko
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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
19 *
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.
30 *
31 * $FreeBSD: head/sbin/mount_ntfs/mount_ntfs.c 83229 2001-09-08 23:03:52Z semenu $
32 *
33 */
34
35#include <sys/cdefs.h>
36#include <sys/param.h>
37#define NTFS
38#include <sys/mount.h>
39#include <sys/stat.h>
40#include <fs/ntfs/ntfsmount.h>
41#include <ctype.h>
42#include <err.h>
43#include <grp.h>
44#include <pwd.h>
45#include <stdio.h>
46#include <stdlib.h>
47#include <string.h>
48#include <sysexits.h>
49#include <unistd.h>
50#include <libutil.h>
51
52#include "mntopts.h"
53
54static struct mntopt mopts[] = {
55	MOPT_STDOPTS,
56	{ NULL }
57};
58
59static gid_t	a_gid __P((char *));
60static uid_t	a_uid __P((char *));
61static mode_t	a_mask __P((char *));
62static void	usage __P((void)) __dead2;
63
64static void     load_u2wtable __P((struct ntfs_args *, char *));
65
66int
67main(argc, argv)
68	int argc;
69	char **argv;
70{
71	struct ntfs_args args;
72	struct stat sb;
73	int c, mntflags, set_gid, set_uid, set_mask, error;
74	char *dev, *dir, mntpath[MAXPATHLEN];
75#if __FreeBSD_version >= 300000
76	struct vfsconf vfc;
77#else
78	struct vfsconf *vfc;
79#endif
80
81	mntflags = set_gid = set_uid = set_mask = 0;
82	(void)memset(&args, '\0', sizeof(args));
83
84	while ((c = getopt(argc, argv, "aiu:g:m:o:W:")) !=  -1) {
85		switch (c) {
86		case 'u':
87			args.uid = a_uid(optarg);
88			set_uid = 1;
89			break;
90		case 'g':
91			args.gid = a_gid(optarg);
92			set_gid = 1;
93			break;
94		case 'm':
95			args.mode = a_mask(optarg);
96			set_mask = 1;
97			break;
98		case 'i':
99			args.flag |= NTFS_MFLAG_CASEINS;
100			break;
101		case 'a':
102			args.flag |= NTFS_MFLAG_ALLNAMES;
103			break;
104		case 'o':
105			getmntopts(optarg, mopts, &mntflags, 0);
106			break;
107		case 'W':
108			load_u2wtable(&args, optarg);
109			args.flag |= NTFSMNT_U2WTABLE;
110			break;
111		case '?':
112		default:
113			usage();
114			break;
115		}
116	}
117
118	if (optind + 2 != argc)
119		usage();
120
121	dev = argv[optind];
122	dir = argv[optind + 1];
123
124	/*
125	 * Resolve the mountpoint with realpath(3) and remove unnecessary
126	 * slashes from the devicename if there are any.
127	 */
128	(void)checkpath(dir, mntpath);
129	(void)rmslashes(dev, dev);
130
131	args.fspec = dev;
132	args.export.ex_root = 65534;	/* unchecked anyway on DOS fs */
133	if (mntflags & MNT_RDONLY)
134		args.export.ex_flags = MNT_EXRDONLY;
135	else
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);
140
141		if (!set_uid)
142			args.uid = sb.st_uid;
143		if (!set_gid)
144			args.gid = sb.st_gid;
145		if (!set_mask)
146			args.mode = sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
147	}
148
149#if __FreeBSD_version >= 300000
150	error = getvfsbyname("ntfs", &vfc);
151	if(error && vfsisloadable("ntfs")) {
152		if(vfsload("ntfs"))
153#else
154	vfc = getvfsbyname("ntfs");
155	if(!vfc && vfsisloadable("ntfs")) {
156		if(vfsload("ntfs"))
157#endif
158			err(EX_OSERR, "vfsload(ntfs)");
159		endvfsent();	/* clear cache */
160#if __FreeBSD_version >= 300000
161		error = getvfsbyname("ntfs", &vfc);
162#else
163		vfc = getvfsbyname("ntfs");
164#endif
165	}
166#if __FreeBSD_version >= 300000
167	if (error)
168#else
169	if (!vfc)
170#endif
171		errx(EX_OSERR, "ntfs filesystem is not available");
172
173#if __FreeBSD_version >= 300000
174	if (mount(vfc.vfc_name, mntpath, mntflags, &args) < 0)
175#else
176	if (mount(vfc->vfc_index, mntpath, mntflags, &args) < 0)
177#endif
178		err(EX_OSERR, "%s", dev);
179
180	exit (0);
181}
182
183gid_t
184a_gid(s)
185	char *s;
186{
187	struct group *gr;
188	char *gname;
189	gid_t gid;
190
191	if ((gr = getgrnam(s)) != NULL)
192		gid = gr->gr_gid;
193	else {
194		for (gname = s; *s && isdigit(*s); ++s);
195		if (!*s)
196			gid = atoi(gname);
197		else
198			errx(EX_NOUSER, "unknown group id: %s", gname);
199	}
200	return (gid);
201}
202
203uid_t
204a_uid(s)
205	char *s;
206{
207	struct passwd *pw;
208	char *uname;
209	uid_t uid;
210
211	if ((pw = getpwnam(s)) != NULL)
212		uid = pw->pw_uid;
213	else {
214		for (uname = s; *s && isdigit(*s); ++s);
215		if (!*s)
216			uid = atoi(uname);
217		else
218			errx(EX_NOUSER, "unknown user id: %s", uname);
219	}
220	return (uid);
221}
222
223mode_t
224a_mask(s)
225	char *s;
226{
227	int done, rv=0;
228	char *ep;
229
230	done = 0;
231	if (*s >= '0' && *s <= '7') {
232		done = 1;
233		rv = strtol(optarg, &ep, 8);
234	}
235	if (!done || rv < 0 || *ep)
236		errx(EX_USAGE, "invalid file mode: %s", s);
237	return (rv);
238}
239
240void
241usage()
242{
243	fprintf(stderr, "usage: mount_ntfs [-a] [-i] [-u user] [-g group] [-m mask] [-W u2wtable] bdev dir\n");
244	exit(EX_USAGE);
245}
246
247void
248load_u2wtable (pargs, name)
249	struct ntfs_args *pargs;
250	char *name;
251{
252	FILE *f;
253	int i, j, code[8];
254	size_t line = 0;
255	char buf[128];
256	char *fn, *s, *p;
257
258	if (*name == '/')
259		fn = name;
260	else {
261		snprintf(buf, sizeof(buf), "/usr/libdata/msdosfs/%s", name);
262		buf[127] = '\0';
263		fn = buf;
264	}
265	if ((f = fopen(fn, "r")) == NULL)
266		err(EX_NOINPUT, "%s", fn);
267	p = NULL;
268	for (i = 0; i < 16; i++) {
269		do {
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))
274				s++;
275		} while (*s == '\0');
276		if (sscanf(s, "%i%i%i%i%i%i%i%i",
277code, 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];
281	}
282	for (i = 0; i < 16; i++) {
283		do {
284			free(p);
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))
288				s++;
289		} while (*s == '\0');
290		if (sscanf(s, "%i%i%i%i%i%i%i%i",
291code, 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] */;
295	}
296	for (i = 0; i < 16; i++) {
297		do {
298			free(p);
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))
302				s++;
303		} while (*s == '\0');
304		if (sscanf(s, "%i%i%i%i%i%i%i%i",
305code, 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] */;
309	}
310	free(p);
311	fclose(f);
312}
313
314