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.
35 #include <sys/cdefs.h>
36 #include <sys/param.h>
38 #include <sys/mount.h>
40 #include <sys/module.h>
41 #include <sys/iconv.h>
42 #include <sys/linker.h>
43 #include <fs/ntfs/ntfsmount.h>
57 #define TRANSITION_PERIOD_HACK
59 static struct mntopt mopts[] = {
64 static gid_t a_gid(char *);
65 static uid_t a_uid(char *);
66 static mode_t a_mask(char *);
67 static void usage(void) __dead2;
69 static int set_charset(struct ntfs_args *);
72 main(int argc, char *argv[])
74 struct ntfs_args args;
76 int c, mntflags, set_gid, set_uid, set_mask;
77 char *dev, *dir, mntpath[MAXPATHLEN];
79 mntflags = set_gid = set_uid = set_mask = 0;
80 (void)memset(&args, '\0', sizeof(args));
84 #ifdef TRANSITION_PERIOD_HACK
85 while ((c = getopt(argc, argv, "aiu:g:m:o:C:W:")) != -1) {
87 while ((c = getopt(argc, argv, "aiu:g:m:o:C:")) != -1) {
91 args.uid = a_uid(optarg);
95 args.gid = a_gid(optarg);
99 args.mode = a_mask(optarg);
103 args.flag |= NTFS_MFLAG_CASEINS;
106 args.flag |= NTFS_MFLAG_ALLNAMES;
109 getmntopts(optarg, mopts, &mntflags, 0);
112 args.cs_local = malloc(ICONV_CSNMAXLEN);
113 if (args.cs_local == NULL)
114 err(EX_OSERR, "malloc()");
115 strncpy(args.cs_local,
116 kiconv_quirkcs(optarg, KICONV_VENDOR_MICSFT),
119 #ifdef TRANSITION_PERIOD_HACK
121 args.cs_local = malloc(ICONV_CSNMAXLEN);
122 if (args.cs_local == NULL)
123 err(EX_OSERR, "malloc()");
124 if (strcmp(optarg, "iso22dos") == 0) {
125 strcpy(args.cs_local, "ISO8859-2");
126 } else if (strcmp(optarg, "iso72dos") == 0) {
127 strcpy(args.cs_local, "ISO8859-7");
128 } else if (strcmp(optarg, "koi2dos") == 0) {
129 strcpy(args.cs_local, "KOI8-R");
130 } else if (strcmp(optarg, "koi8u2dos") == 0) {
131 strcpy(args.cs_local, "KOI8-U");
133 err(EX_NOINPUT, "%s", optarg);
136 #endif /* TRANSITION_PERIOD_HACK */
144 if (optind + 2 != argc)
148 dir = argv[optind + 1];
151 if (set_charset(&args) == -1)
152 err(EX_OSERR, "ntfs_iconv");
153 args.flag |= NTFS_MFLAG_KICONV;
156 * Force to be MNT_RDONLY,
157 * since only reading is supported right now,
159 mntflags |= MNT_RDONLY;
163 * Resolve the mountpoint with realpath(3) and remove unnecessary
164 * slashes from the devicename if there are any.
166 (void)checkpath(dir, mntpath);
167 (void)rmslashes(dev, dev);
170 args.export.ex_root = 65534; /* unchecked anyway on DOS fs */
171 if (mntflags & MNT_RDONLY)
172 args.export.ex_flags = MNT_EXRDONLY;
174 args.export.ex_flags = 0;
175 if (!set_gid || !set_uid || !set_mask) {
176 if (stat(mntpath, &sb) == -1)
177 err(EX_OSERR, "stat %s", mntpath);
180 args.uid = sb.st_uid;
182 args.gid = sb.st_gid;
184 args.mode = sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
187 if (mount("ntfs", mntpath, mntflags, &args) < 0)
188 err(EX_OSERR, "%s", dev);
200 if ((gr = getgrnam(s)) != NULL)
203 for (gname = s; *s && isdigit(*s); ++s);
207 errx(EX_NOUSER, "unknown group id: %s", gname);
219 if ((pw = getpwnam(s)) != NULL)
222 for (uname = s; *s && isdigit(*s); ++s);
226 errx(EX_NOUSER, "unknown user id: %s", uname);
238 if (*s >= '0' && *s <= '7') {
240 rv = strtol(optarg, &ep, 8);
242 if (!done || rv < 0 || *ep)
243 errx(EX_USAGE, "invalid file mode: %s", s);
250 #ifdef TRANSITION_PERIOD_HACK
251 fprintf(stderr, "%s\n%s\n",
252 "usage: mount_ntfs [-a] [-i] [-u user] [-g group] [-m mask]",
253 " [-C charset] [-W u2wtable] special node");
255 fprintf(stderr, "usage: mount_ntfs [-a] [-i] [-u user] [-g group] [-m mask] [-C charset] special node\n");
261 set_charset(struct ntfs_args *pargs)
265 if (modfind("ntfs_iconv") < 0)
266 if (kldload("ntfs_iconv") < 0 || modfind("ntfs_iconv") < 0) {
267 warnx( "cannot find or load \"ntfs_iconv\" kernel module");
271 if ((pargs->cs_ntfs = malloc(ICONV_CSNMAXLEN)) == NULL)
273 strncpy(pargs->cs_ntfs, ENCODING_UNICODE, ICONV_CSNMAXLEN);
274 error = kiconv_add_xlat16_cspairs(pargs->cs_ntfs, pargs->cs_local);