mount_ntfs.c revision 1.11 1 /* $NetBSD: mount_ntfs.c,v 1.11 2005/01/31 05:19:19 erh 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.11 2005/01/31 05:19:19 erh Exp $");
39 #endif
40
41 #include <sys/cdefs.h>
42 #include <sys/param.h>
43 #include <sys/mount.h>
44 #include <sys/stat.h>
45 #include <ntfs/ntfsmount.h>
46 #include <ctype.h>
47 #include <err.h>
48 #include <grp.h>
49 #include <pwd.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <sysexits.h>
54 #include <unistd.h>
55 #include <util.h>
56
57 #include <mntopts.h>
58 #include <fattr.h>
59
60 static const struct mntopt mopts[] = {
61 MOPT_STDOPTS,
62 MOPT_GETARGS,
63 { NULL }
64 };
65
66 static void usage __P((void)) __attribute__((__noreturn__));
67 int main __P((int, char **));
68 int mount_ntfs __P((int argc, char **argv));
69
70 #ifndef MOUNT_NOMAIN
71 int
72 main(argc, argv)
73 int argc;
74 char **argv;
75 {
76 return mount_ntfs(argc, argv);
77 }
78 #endif
79
80 int
81 mount_ntfs(argc, argv)
82 int argc;
83 char **argv;
84 {
85 struct ntfs_args args;
86 struct stat sb;
87 int c, mntflags, set_gid, set_uid, set_mask;
88 char *dev, *dir, canon_dev[MAXPATHLEN], canon_dir[MAXPATHLEN];
89
90 mntflags = set_gid = set_uid = set_mask = 0;
91 (void)memset(&args, '\0', sizeof(args));
92
93 while ((c = getopt(argc, argv, "aiu:g:m:o:")) != -1) {
94 switch (c) {
95 case 'u':
96 args.uid = a_uid(optarg);
97 set_uid = 1;
98 break;
99 case 'g':
100 args.gid = a_gid(optarg);
101 set_gid = 1;
102 break;
103 case 'm':
104 args.mode = a_mask(optarg);
105 set_mask = 1;
106 break;
107 case 'i':
108 args.flag |= NTFS_MFLAG_CASEINS;
109 break;
110 case 'a':
111 args.flag |= NTFS_MFLAG_ALLNAMES;
112 break;
113 case 'o':
114 getmntopts(optarg, mopts, &mntflags, 0);
115 break;
116 case '?':
117 default:
118 usage();
119 break;
120 }
121 }
122
123 if (optind + 2 != argc)
124 usage();
125
126 dev = argv[optind];
127 dir = argv[optind + 1];
128
129 if (realpath(dev, canon_dev) == NULL) /* Check device path */
130 err(1, "realpath %s", dev);
131 if (strncmp(dev, canon_dev, MAXPATHLEN)) {
132 warnx("\"%s\" is a relative path.", dev);
133 dev = canon_dev;
134 warnx("using \"%s\" instead.", dev);
135 }
136
137 if (realpath(dir, canon_dir) == NULL) /* Check mounton path */
138 err(1, "realpath %s", dir);
139 if (strncmp(dir, canon_dir, MAXPATHLEN)) {
140 warnx("\"%s\" is a relative path.", dir);
141 dir = canon_dir;
142 warnx("using \"%s\" instead.", dir);
143 }
144
145 args.fspec = dev;
146 args.export.ex_root = 65534; /* unchecked anyway on DOS fs */
147 if (mntflags & MNT_RDONLY)
148 args.export.ex_flags = MNT_EXRDONLY;
149 else
150 args.export.ex_flags = 0;
151 if (!set_gid || !set_uid || !set_mask) {
152 if (stat(dir, &sb) == -1)
153 err(EX_OSERR, "stat %s", dir);
154
155 if (!set_uid)
156 args.uid = sb.st_uid;
157 if (!set_gid)
158 args.gid = sb.st_gid;
159 if (!set_mask)
160 args.mode = sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
161 }
162
163 if (mount(MOUNT_NTFS, dir, mntflags, &args) < 0)
164 err(EX_OSERR, "%s on %s", dev, dir);
165
166 if (mntflags & MNT_GETARGS) {
167 char buf[1024];
168 (void)snprintb(buf, sizeof(buf), NTFS_MFLAG_BITS, args.flag);
169 printf("uid=%d, gid=%d, mode=0%o, flags=%s\n", args.uid,
170 args.gid, args.mode, buf);
171 }
172 exit (0);
173 }
174
175 static void
176 usage()
177 {
178 fprintf(stderr, "usage: mount_ntfs [-a] [-i] [-u user] [-g group] [-m mask] bdev dir\n");
179 exit(EX_USAGE);
180 }
181