xref: /dragonfly/sbin/mount_ntfs/mount_ntfs.c (revision 5868d2b9)
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: src/sbin/mount_ntfs/mount_ntfs.c,v 1.3.2.2 2001/10/12 22:08:43 semenu Exp $
32  *
33  */
34 
35 #include <sys/param.h>
36 #define NTFS
37 #include <sys/mount.h>
38 #include <sys/module.h>
39 #include <sys/linker.h>
40 #include <sys/iconv.h>
41 #include <sys/stat.h>
42 #include <vfs/ntfs/ntfsmount.h>
43 #include <ctype.h>
44 #include <err.h>
45 #include <grp.h>
46 #include <mntopts.h>
47 #include <pwd.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <sysexits.h>
52 #include <unistd.h>
53 #include <libutil.h>
54 
55 static struct mntopt mopts[] = {
56 	MOPT_STDOPTS,
57 	MOPT_NULL
58 };
59 
60 static gid_t	a_gid(char *);
61 static uid_t	a_uid(char *);
62 static mode_t	a_mask(char *);
63 static void	usage(void) __dead2;
64 
65 int
66 set_charset(struct ntfs_args *pargs, const char *cs_local);
67 
68 int
69 main(int argc, 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 || defined(__DragonFly__)
76 	struct vfsconf vfc;
77 #else
78 	struct vfsconf *vfc;
79 #endif
80 	const char *quirk;
81 	char *cs_local = NULL;
82 
83 	mntflags = set_gid = set_uid = set_mask = 0;
84 	memset(&args, '\0', sizeof(args));
85 
86 	while ((c = getopt(argc, argv, "aiu:g:m:o:C:")) !=  -1) {
87 		switch (c) {
88 		case 'u':
89 			args.uid = a_uid(optarg);
90 			set_uid = 1;
91 			break;
92 		case 'g':
93 			args.gid = a_gid(optarg);
94 			set_gid = 1;
95 			break;
96 		case 'm':
97 			args.mode = a_mask(optarg);
98 			set_mask = 1;
99 			break;
100 		case 'i':
101 			args.flag |= NTFS_MFLAG_CASEINS;
102 			break;
103 		case 'a':
104 			args.flag |= NTFS_MFLAG_ALLNAMES;
105 			break;
106 		case 'o':
107 			getmntopts(optarg, mopts, &mntflags, 0);
108 			break;
109 		case 'C':
110 			quirk = kiconv_quirkcs(optarg, KICONV_VENDOR_MICSFT);
111 			cs_local = strdup(quirk);
112 			if (set_charset(&args, cs_local) == -1)
113 				err(EX_OSERR, "ntfs_iconv");
114 			args.flag |= NTFS_MFLAG_KICONV;
115 			mntflags |= MNT_RDONLY;
116 			break;
117 		case '?':
118 		default:
119 			usage();
120 			break;
121 		}
122 	}
123 
124 	if (optind + 2 != argc)
125 		usage();
126 
127 	dev = argv[optind];
128 	dir = argv[optind + 1];
129 
130 	/*
131 	 * Resolve the mountpoint with realpath(3) and remove unnecessary
132 	 * slashes from the devicename if there are any.
133 	 */
134 	checkpath(dir, mntpath);
135 	rmslashes(dev, dev);
136 
137 	args.fspec = dev;
138 	args.export.ex_root = 65534;	/* unchecked anyway on DOS fs */
139 	if (mntflags & MNT_RDONLY)
140 		args.export.ex_flags = MNT_EXRDONLY;
141 	else
142 		args.export.ex_flags = 0;
143 	if (!set_gid || !set_uid || !set_mask) {
144 		if (stat(mntpath, &sb) == -1)
145 			err(EX_OSERR, "stat %s", mntpath);
146 
147 		if (!set_uid)
148 			args.uid = sb.st_uid;
149 		if (!set_gid)
150 			args.gid = sb.st_gid;
151 		if (!set_mask)
152 			args.mode = sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
153 	}
154 
155 #if __FreeBSD_version >= 300000 || defined(__DragonFly__)
156 	error = getvfsbyname("ntfs", &vfc);
157 	if(error && vfsisloadable("ntfs")) {
158 		if(vfsload("ntfs"))
159 #else
160 	vfc = getvfsbyname("ntfs");
161 	if(!vfc && vfsisloadable("ntfs")) {
162 		if(vfsload("ntfs"))
163 #endif
164 			err(EX_OSERR, "vfsload(ntfs)");
165 		endvfsent();	/* clear cache */
166 #if __FreeBSD_version >= 300000 || defined(__DragonFly__)
167 		error = getvfsbyname("ntfs", &vfc);
168 #else
169 		vfc = getvfsbyname("ntfs");
170 #endif
171 	}
172 #if __FreeBSD_version >= 300000 || defined(__DragonFly__)
173 	if (error)
174 #else
175 	if (!vfc)
176 #endif
177 		errx(EX_OSERR, "ntfs filesystem is not available");
178 
179 #if __FreeBSD_version >= 300000 || defined(__DragonFly__)
180 	if (mount(vfc.vfc_name, mntpath, mntflags, &args) < 0)
181 #else
182 	if (mount(vfc->vfc_index, mntpath, mntflags, &args) < 0)
183 #endif
184 		err(EX_OSERR, "%s", dev);
185 
186 	exit (0);
187 }
188 
189 gid_t
190 a_gid(char *s)
191 {
192 	struct group *gr;
193 	char *gname;
194 	gid_t gid;
195 
196 	if ((gr = getgrnam(s)) != NULL)
197 		gid = gr->gr_gid;
198 	else {
199 		for (gname = s; *s && isdigit(*s); ++s);
200 		if (!*s)
201 			gid = atoi(gname);
202 		else
203 			errx(EX_NOUSER, "unknown group id: %s", gname);
204 	}
205 	return (gid);
206 }
207 
208 uid_t
209 a_uid(char *s)
210 {
211 	struct passwd *pw;
212 	char *uname;
213 	uid_t uid;
214 
215 	if ((pw = getpwnam(s)) != NULL)
216 		uid = pw->pw_uid;
217 	else {
218 		for (uname = s; *s && isdigit(*s); ++s);
219 		if (!*s)
220 			uid = atoi(uname);
221 		else
222 			errx(EX_NOUSER, "unknown user id: %s", uname);
223 	}
224 	return (uid);
225 }
226 
227 mode_t
228 a_mask(char *s)
229 {
230 	int done, rv=0;
231 	char *ep;
232 
233 	done = 0;
234 	if (*s >= '0' && *s <= '7') {
235 		done = 1;
236 		rv = strtol(optarg, &ep, 8);
237 	}
238 	if (!done || rv < 0 || *ep)
239 		errx(EX_USAGE, "invalid file mode: %s", s);
240 	return (rv);
241 }
242 
243 void
244 usage(void)
245 {
246 	fprintf(stderr, "usage: mount_ntfs [-a] [-i] [-u user] [-g group] [-m mask] [-C charset] bdev dir\n");
247 	exit(EX_USAGE);
248 }
249 
250 int
251 set_charset(struct ntfs_args *pargs, const char *cs_local)
252 {
253 	int error;
254 
255 	if (modfind("ntfs_iconv") < 0)
256 		if (kldload("ntfs_iconv") < 0 || modfind("ntfs_iconv") < 0) {
257 			warnx( "cannot find or load \"ntfs_iconv\" kernel module");
258 			return (-1);
259 		}
260 	strncpy(pargs->cs_ntfs, ENCODING_UNICODE, ICONV_CSNMAXLEN);
261 	strncpy(pargs->cs_local, cs_local, ICONV_CSNMAXLEN);
262 	error = kiconv_add_xlat16_cspairs(pargs->cs_ntfs, cs_local);
263 	if (error)
264 		return (-1);
265 
266 	return (0);
267 }
268