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