]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sbin/mount_ntfs/mount_ntfs.c
style(9) sweep.
[FreeBSD/FreeBSD.git] / sbin / mount_ntfs / mount_ntfs.c
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$
32  *
33  */
34
35 #include <sys/cdefs.h>
36 #include <sys/param.h>
37 #define NTFS
38 #include <sys/mount.h>
39 #include <sys/stat.h>
40 #include <fs/ntfs/ntfsmount.h>
41 #include <ctype.h>
42 #include <err.h>
43 #include <grp.h>
44 #include <pwd.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <sysexits.h>
49 #include <unistd.h>
50 #include <libutil.h>
51
52 #include "mntopts.h"
53
54 static struct mntopt mopts[] = {
55         MOPT_STDOPTS,
56         { NULL }
57 };
58
59 static gid_t    a_gid(char *);
60 static uid_t    a_uid(char *);
61 static mode_t   a_mask(char *);
62 static void     usage(void) __dead2;
63
64 static void     load_u2wtable(struct ntfs_args *, char *);
65
66 int
67 main(argc, argv)
68         int argc;
69         char **argv;
70 {
71         struct ntfs_args args;
72         struct stat sb;
73         int c, mntflags, set_gid, set_uid, set_mask;
74         char *dev, *dir, mntpath[MAXPATHLEN];
75
76         mntflags = set_gid = set_uid = set_mask = 0;
77         (void)memset(&args, '\0', sizeof(args));
78
79         while ((c = getopt(argc, argv, "aiu:g:m:o:W:")) !=  -1) {
80                 switch (c) {
81                 case 'u':
82                         args.uid = a_uid(optarg);
83                         set_uid = 1;
84                         break;
85                 case 'g':
86                         args.gid = a_gid(optarg);
87                         set_gid = 1;
88                         break;
89                 case 'm':
90                         args.mode = a_mask(optarg);
91                         set_mask = 1;
92                         break;
93                 case 'i':
94                         args.flag |= NTFS_MFLAG_CASEINS;
95                         break;
96                 case 'a':
97                         args.flag |= NTFS_MFLAG_ALLNAMES;
98                         break;
99                 case 'o':
100                         getmntopts(optarg, mopts, &mntflags, 0);
101                         break;
102                 case 'W':
103                         load_u2wtable(&args, optarg);
104                         args.flag |= NTFSMNT_U2WTABLE;
105                         break;
106                 case '?':
107                 default:
108                         usage();
109                         break;
110                 }
111         }
112
113         if (optind + 2 != argc)
114                 usage();
115
116         dev = argv[optind];
117         dir = argv[optind + 1];
118
119         /*
120          * Resolve the mountpoint with realpath(3) and remove unnecessary 
121          * slashes from the devicename if there are any.
122          */
123         (void)checkpath(dir, mntpath);
124         (void)rmslashes(dev, dev);
125
126         args.fspec = dev;
127         args.export.ex_root = 65534;    /* unchecked anyway on DOS fs */
128         if (mntflags & MNT_RDONLY)
129                 args.export.ex_flags = MNT_EXRDONLY;
130         else
131                 args.export.ex_flags = 0;
132         if (!set_gid || !set_uid || !set_mask) {
133                 if (stat(mntpath, &sb) == -1)
134                         err(EX_OSERR, "stat %s", mntpath);
135
136                 if (!set_uid)
137                         args.uid = sb.st_uid;
138                 if (!set_gid)
139                         args.gid = sb.st_gid;
140                 if (!set_mask)
141                         args.mode = sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
142         }
143
144         if (mount("ntfs", mntpath, mntflags, &args) < 0)
145                 err(EX_OSERR, "%s", dev);
146
147         exit (0);
148 }
149
150 gid_t
151 a_gid(s)
152         char *s;
153 {
154         struct group *gr;
155         char *gname;
156         gid_t gid;
157
158         if ((gr = getgrnam(s)) != NULL)
159                 gid = gr->gr_gid;
160         else {
161                 for (gname = s; *s && isdigit(*s); ++s);
162                 if (!*s)
163                         gid = atoi(gname);
164                 else
165                         errx(EX_NOUSER, "unknown group id: %s", gname);
166         }
167         return (gid);
168 }
169
170 uid_t
171 a_uid(s)
172         char *s;
173 {
174         struct passwd *pw;
175         char *uname;
176         uid_t uid;
177
178         if ((pw = getpwnam(s)) != NULL)
179                 uid = pw->pw_uid;
180         else {
181                 for (uname = s; *s && isdigit(*s); ++s);
182                 if (!*s)
183                         uid = atoi(uname);
184                 else
185                         errx(EX_NOUSER, "unknown user id: %s", uname);
186         }
187         return (uid);
188 }
189
190 mode_t
191 a_mask(s)
192         char *s;
193 {
194         int done, rv=0;
195         char *ep;
196
197         done = 0;
198         if (*s >= '0' && *s <= '7') {
199                 done = 1;
200                 rv = strtol(optarg, &ep, 8);
201         }
202         if (!done || rv < 0 || *ep)
203                 errx(EX_USAGE, "invalid file mode: %s", s);
204         return (rv);
205 }
206
207 void
208 usage()
209 {
210         fprintf(stderr, "usage: mount_ntfs [-a] [-i] [-u user] [-g group] [-m mask] [-W u2wtable] bdev dir\n");
211         exit(EX_USAGE);
212 }
213
214 void
215 load_u2wtable (pargs, name)
216         struct ntfs_args *pargs;
217         char *name;
218 {
219         FILE *f;
220         int i, j, code[8];
221         size_t line = 0;
222         char buf[128];
223         char *fn, *s, *p;
224
225         if (*name == '/')
226                 fn = name;
227         else {
228                 snprintf(buf, sizeof(buf), "/usr/libdata/msdosfs/%s", name);
229                 buf[127] = '\0';
230                 fn = buf;
231         }
232         if ((f = fopen(fn, "r")) == NULL)
233                 err(EX_NOINPUT, "%s", fn);
234         p = NULL;
235         for (i = 0; i < 16; i++) {
236                 do {
237                         if (p != NULL) free(p);
238                         if ((p = s = fparseln(f, NULL, &line, NULL, 0)) == NULL)
239                                 errx(EX_DATAERR, "can't read u2w table row %d near line %d", i, line);
240                         while (isspace((unsigned char)*s))
241                                 s++;
242                 } while (*s == '\0');
243                 if (sscanf(s, "%i%i%i%i%i%i%i%i",
244 code, code + 1, code + 2, code + 3, code + 4, code + 5, code + 6, code + 7) != 8)
245                         errx(EX_DATAERR, "u2w table: missing item(s) in row %d, line %d", i, line);
246                 for (j = 0; j < 8; j++)
247                         pargs->u2w[i * 8 + j] = code[j];
248         }
249         for (i = 0; i < 16; i++) {
250                 do {
251                         free(p);
252                         if ((p = s = fparseln(f, NULL, &line, NULL, 0)) == NULL)
253                                 errx(EX_DATAERR, "can't read d2u table row %d near line %d", i, line);
254                         while (isspace((unsigned char)*s))
255                                 s++;
256                 } while (*s == '\0');
257                 if (sscanf(s, "%i%i%i%i%i%i%i%i",
258 code, code + 1, code + 2, code + 3, code + 4, code + 5, code + 6, code + 7) != 8)
259                         errx(EX_DATAERR, "d2u table: missing item(s) in row %d, line %d", i, line);
260                 for (j = 0; j < 8; j++)
261                         /* pargs->d2u[i * 8 + j] = code[j] */;
262         }
263         for (i = 0; i < 16; i++) {
264                 do {
265                         free(p);
266                         if ((p = s = fparseln(f, NULL, &line, NULL, 0)) == NULL)
267                                 errx(EX_DATAERR, "can't read u2d table row %d near line %d", i, line);
268                         while (isspace((unsigned char)*s))
269                                 s++;
270                 } while (*s == '\0');
271                 if (sscanf(s, "%i%i%i%i%i%i%i%i",
272 code, code + 1, code + 2, code + 3, code + 4, code + 5, code + 6, code + 7) != 8)
273                         errx(EX_DATAERR, "u2d table: missing item(s) in row %d, line %d", i, line);
274                 for (j = 0; j < 8; j++)
275                         /* pargs->u2d[i * 8 + j] = code[j] */;
276         }
277         free(p);
278         fclose(f);
279 }
280