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