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