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