route_output: Fix RTM_DELETE rtentry leakage
[dragonfly.git] / sbin / mount_ntfs / mount_ntfs.c
blob3b99e4a44093812dc50d6210b8fbf3b1c1877d51
1 /*
2 * Copyright (c) 1994 Christopher G. Demetriou
3 * Copyright (c) 1999 Semen Ustimenko
4 * All rights reserved.
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
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 $
32 * $DragonFly: src/sbin/mount_ntfs/mount_ntfs.c,v 1.9 2008/11/02 21:52:46 swildner Exp $
36 #include <sys/cdefs.h>
37 #include <sys/param.h>
38 #define NTFS
39 #include <sys/mount.h>
40 #include <sys/stat.h>
41 #include <vfs/ntfs/ntfsmount.h>
42 #include <ctype.h>
43 #include <err.h>
44 #include <grp.h>
45 #include <pwd.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <sysexits.h>
50 #include <unistd.h>
51 #include <libutil.h>
53 #include "mntopts.h"
55 static struct mntopt mopts[] = {
56 MOPT_STDOPTS,
57 MOPT_NULL
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;
65 static void load_u2wtable(struct ntfs_args *, char *);
67 int
68 main(int argc, char **argv)
70 struct ntfs_args args;
71 struct stat sb;
72 int c, mntflags, set_gid, set_uid, set_mask, error;
73 char *dev, *dir, mntpath[MAXPATHLEN];
74 #if __FreeBSD_version >= 300000 || defined(__DragonFly__)
75 struct vfsconf vfc;
76 #else
77 struct vfsconf *vfc;
78 #endif
80 mntflags = set_gid = set_uid = set_mask = 0;
81 memset(&args, '\0', sizeof(args));
83 while ((c = getopt(argc, argv, "aiu:g:m:o:W:")) != -1) {
84 switch (c) {
85 case 'u':
86 args.uid = a_uid(optarg);
87 set_uid = 1;
88 break;
89 case 'g':
90 args.gid = a_gid(optarg);
91 set_gid = 1;
92 break;
93 case 'm':
94 args.mode = a_mask(optarg);
95 set_mask = 1;
96 break;
97 case 'i':
98 args.flag |= NTFS_MFLAG_CASEINS;
99 break;
100 case 'a':
101 args.flag |= NTFS_MFLAG_ALLNAMES;
102 break;
103 case 'o':
104 getmntopts(optarg, mopts, &mntflags, 0);
105 break;
106 case 'W':
107 load_u2wtable(&args, optarg);
108 args.flag |= NTFSMNT_U2WTABLE;
109 break;
110 case '?':
111 default:
112 usage();
113 break;
117 if (optind + 2 != argc)
118 usage();
120 dev = argv[optind];
121 dir = argv[optind + 1];
124 * Resolve the mountpoint with realpath(3) and remove unnecessary
125 * slashes from the devicename if there are any.
127 checkpath(dir, mntpath);
128 rmslashes(dev, dev);
130 args.fspec = dev;
131 args.export.ex_root = 65534; /* unchecked anyway on DOS fs */
132 if (mntflags & MNT_RDONLY)
133 args.export.ex_flags = MNT_EXRDONLY;
134 else
135 args.export.ex_flags = 0;
136 if (!set_gid || !set_uid || !set_mask) {
137 if (stat(mntpath, &sb) == -1)
138 err(EX_OSERR, "stat %s", mntpath);
140 if (!set_uid)
141 args.uid = sb.st_uid;
142 if (!set_gid)
143 args.gid = sb.st_gid;
144 if (!set_mask)
145 args.mode = sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
148 #if __FreeBSD_version >= 300000 || defined(__DragonFly__)
149 error = getvfsbyname("ntfs", &vfc);
150 if(error && vfsisloadable("ntfs")) {
151 if(vfsload("ntfs"))
152 #else
153 vfc = getvfsbyname("ntfs");
154 if(!vfc && vfsisloadable("ntfs")) {
155 if(vfsload("ntfs"))
156 #endif
157 err(EX_OSERR, "vfsload(ntfs)");
158 endvfsent(); /* clear cache */
159 #if __FreeBSD_version >= 300000 || defined(__DragonFly__)
160 error = getvfsbyname("ntfs", &vfc);
161 #else
162 vfc = getvfsbyname("ntfs");
163 #endif
165 #if __FreeBSD_version >= 300000 || defined(__DragonFly__)
166 if (error)
167 #else
168 if (!vfc)
169 #endif
170 errx(EX_OSERR, "ntfs filesystem is not available");
172 #if __FreeBSD_version >= 300000 || defined(__DragonFly__)
173 if (mount(vfc.vfc_name, mntpath, mntflags, &args) < 0)
174 #else
175 if (mount(vfc->vfc_index, mntpath, mntflags, &args) < 0)
176 #endif
177 err(EX_OSERR, "%s", dev);
179 exit (0);
182 gid_t
183 a_gid(char *s)
185 struct group *gr;
186 char *gname;
187 gid_t gid;
189 if ((gr = getgrnam(s)) != NULL)
190 gid = gr->gr_gid;
191 else {
192 for (gname = s; *s && isdigit(*s); ++s);
193 if (!*s)
194 gid = atoi(gname);
195 else
196 errx(EX_NOUSER, "unknown group id: %s", gname);
198 return (gid);
201 uid_t
202 a_uid(char *s)
204 struct passwd *pw;
205 char *uname;
206 uid_t uid;
208 if ((pw = getpwnam(s)) != NULL)
209 uid = pw->pw_uid;
210 else {
211 for (uname = s; *s && isdigit(*s); ++s);
212 if (!*s)
213 uid = atoi(uname);
214 else
215 errx(EX_NOUSER, "unknown user id: %s", uname);
217 return (uid);
220 mode_t
221 a_mask(char *s)
223 int done, rv=0;
224 char *ep;
226 done = 0;
227 if (*s >= '0' && *s <= '7') {
228 done = 1;
229 rv = strtol(optarg, &ep, 8);
231 if (!done || rv < 0 || *ep)
232 errx(EX_USAGE, "invalid file mode: %s", s);
233 return (rv);
236 void
237 usage(void)
239 fprintf(stderr, "usage: mount_ntfs [-a] [-i] [-u user] [-g group] [-m mask] [-W u2wtable] bdev dir\n");
240 exit(EX_USAGE);
243 void
244 load_u2wtable (struct ntfs_args *pargs, char *name)
246 FILE *f;
247 int i, j, code[8];
248 size_t line = 0;
249 char buf[128];
250 char *fn, *s, *p;
252 if (*name == '/')
253 fn = name;
254 else {
255 snprintf(buf, sizeof(buf), "/usr/libdata/msdosfs/%s", name);
256 buf[127] = '\0';
257 fn = buf;
259 if ((f = fopen(fn, "r")) == NULL)
260 err(EX_NOINPUT, "%s", fn);
261 p = NULL;
262 for (i = 0; i < 16; i++) {
263 do {
264 if (p != NULL) free(p);
265 if ((p = s = fparseln(f, NULL, &line, NULL, 0)) == NULL)
266 errx(EX_DATAERR, "can't read u2w table row %d near line %zu", i, line);
267 while (isspace((unsigned char)*s))
268 s++;
269 } while (*s == '\0');
270 if (sscanf(s, "%i%i%i%i%i%i%i%i",
271 code, code + 1, code + 2, code + 3, code + 4, code + 5, code + 6, code + 7) != 8)
272 errx(EX_DATAERR, "u2w table: missing item(s) in row %d, line %zu", i, line);
273 for (j = 0; j < 8; j++)
274 pargs->u2w[i * 8 + j] = code[j];
276 for (i = 0; i < 16; i++) {
277 do {
278 free(p);
279 if ((p = s = fparseln(f, NULL, &line, NULL, 0)) == NULL)
280 errx(EX_DATAERR, "can't read d2u table row %d near line %zu", i, line);
281 while (isspace((unsigned char)*s))
282 s++;
283 } while (*s == '\0');
284 if (sscanf(s, "%i%i%i%i%i%i%i%i",
285 code, code + 1, code + 2, code + 3, code + 4, code + 5, code + 6, code + 7) != 8)
286 errx(EX_DATAERR, "d2u table: missing item(s) in row %d, line %zu", i, line);
287 for (j = 0; j < 8; j++)
288 /* pargs->d2u[i * 8 + j] = code[j] */;
290 for (i = 0; i < 16; i++) {
291 do {
292 free(p);
293 if ((p = s = fparseln(f, NULL, &line, NULL, 0)) == NULL)
294 errx(EX_DATAERR, "can't read u2d table row %d near line %zu", i, line);
295 while (isspace((unsigned char)*s))
296 s++;
297 } while (*s == '\0');
298 if (sscanf(s, "%i%i%i%i%i%i%i%i",
299 code, code + 1, code + 2, code + 3, code + 4, code + 5, code + 6, code + 7) != 8)
300 errx(EX_DATAERR, "u2d table: missing item(s) in row %d, line %zu", i, line);
301 for (j = 0; j < 8; j++)
302 /* pargs->u2d[i * 8 + j] = code[j] */;
304 free(p);
305 fclose(f);