xref: /openbsd-src/sbin/mount_ntfs/mount_ntfs.c (revision f2da64fbbbf1b03f09f390ab01267c93dfd77c4c)
1 /* $OpenBSD: mount_ntfs.c,v 1.16 2015/12/30 21:38:28 millert Exp $ */
2 /* $NetBSD: mount_ntfs.c,v 1.9 2003/05/03 15:37:08 christos Exp $ */
3 
4 /*
5  * Copyright (c) 1994 Christopher G. Demetriou
6  * Copyright (c) 1999 Semen Ustimenko
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *      This product includes software developed by Christopher G. Demetriou.
20  * 4. The name of the author may not be used to endorse or promote products
21  *    derived from this software without specific prior written permission
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  *
34  * Id: mount_ntfs.c,v 1.1.1.1 1999/02/03 03:51:19 semenu Exp
35  */
36 
37 #include <sys/types.h>
38 #include <sys/mount.h>
39 #include <sys/stat.h>
40 
41 #include <err.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <unistd.h>
46 #include <limits.h>
47 
48 #include <mntopts.h>
49 
50 static const struct mntopt mopts[] = {
51 	MOPT_STDOPTS,
52 	{ NULL }
53 };
54 
55 static __dead void usage(void);
56 static mode_t a_mask(char *);
57 
58 int
59 main(int argc, char *argv[])
60 {
61 	struct ntfs_args args;
62 	struct stat sb;
63 	int c, mntflags, set_gid, set_uid, set_mask;
64 	char *dev, dir[PATH_MAX];
65 
66 	mntflags = set_gid = set_uid = set_mask = 0;
67 	memset(&args, 0, sizeof(args));
68 
69 	while ((c = getopt(argc, argv, "aiu:g:m:o:")) !=  -1) {
70 		switch (c) {
71 		case 'u':
72 			args.uid = strtoul(optarg, NULL, 10);
73 			set_uid = 1;
74 			break;
75 		case 'g':
76 			args.gid = strtoul(optarg, NULL, 10);
77 			set_gid = 1;
78 			break;
79 		case 'm':
80 			args.mode =  a_mask(optarg);
81 			set_mask = 1;
82 			break;
83 		case 'i':
84 			args.flag |= NTFS_MFLAG_CASEINS;
85 			break;
86 		case 'a':
87 			args.flag |= NTFS_MFLAG_ALLNAMES;
88 			break;
89 		case 'o':
90 			getmntopts(optarg, mopts, &mntflags);
91 			break;
92 		default:
93 			usage();
94 			break;
95 		}
96 	}
97 
98 	if (optind + 2 != argc)
99 		usage();
100 
101 	dev = argv[optind];
102 	if (realpath(argv[optind + 1], dir) == NULL)
103 		err(1, "realpath %s", argv[optind + 1]);
104 
105 	args.fspec = dev;
106 	args.export_info.ex_root = 65534;	/* unchecked anyway on NTFS */
107 
108 	mntflags |= MNT_RDONLY;
109 	if (mntflags & MNT_RDONLY)
110 		args.export_info.ex_flags = MNT_EXRDONLY;
111 	else
112 		args.export_info.ex_flags = 0;
113 	if (!set_gid || !set_uid || !set_mask) {
114 		if (stat(dir, &sb) == -1)
115 			err(1, "stat %s", dir);
116 
117 		if (!set_uid)
118 			args.uid = sb.st_uid;
119 		if (!set_gid)
120 			args.gid = sb.st_gid;
121 		if (!set_mask)
122 			args.mode = sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
123 	}
124 	if (mount(MOUNT_NTFS, dir, mntflags, &args) < 0)
125 		err(1, "%s on %s", dev, dir);
126 
127 	exit(0);
128 }
129 
130 static mode_t
131 a_mask(char *s)
132 {
133 	int done, rv;
134 	char *ep;
135 
136 	done = 0;
137 	if (*s >= '0' && *s <= '7') {
138 		done = 1;
139 		rv = strtol(optarg, &ep, 8);
140 	}
141 	if (!done || rv < 0 || *ep)
142 		errx(1, "invalid file mode: %s", s);
143 	return (rv);
144 }
145 
146 static void
147 usage(void)
148 {
149 	fprintf(stderr,
150 	    "usage: mount_ntfs [-ai] [-g gid] [-m mask] [-o options] [-u uid]"
151 	    " special node\n");
152 	exit(1);
153 }
154