]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sbin/mount_msdosfs/mount_msdosfs.c
This commit was generated by cvs2svn to compensate for changes in r149749,
[FreeBSD/FreeBSD.git] / sbin / mount_msdosfs / mount_msdosfs.c
1 /*      $NetBSD: mount_msdos.c,v 1.18 1997/09/16 12:24:18 lukem Exp $   */
2
3 /*
4  * Copyright (c) 1994 Christopher G. Demetriou
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *      This product includes software developed by Christopher G. Demetriou.
18  * 4. The name of the author may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #ifndef lint
34 static const char rcsid[] =
35   "$FreeBSD$";
36 #endif /* not lint */
37
38 #include <sys/param.h>
39 #include <sys/mount.h>
40 #include <sys/stat.h>
41 #include <sys/iconv.h>
42 #include <sys/linker.h>
43 #include <sys/module.h>
44
45 #include <fs/msdosfs/msdosfsmount.h>
46
47 #include <ctype.h>
48 #include <err.h>
49 #include <grp.h>
50 #include <locale.h>
51 #include <pwd.h>
52 #include <stdio.h>
53 /* must be after stdio to declare fparseln */
54 #include <libutil.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <sysexits.h>
58 #include <unistd.h>
59
60 #include "mntopts.h"
61
62 #define TRANSITION_PERIOD_HACK
63
64 /*
65  * XXX - no way to specify "foo=<bar>"-type options; that's what we'd
66  * want for "-u", "-g", "-m", "-M", "-L", "-D", and "-W".
67  */
68 static struct mntopt mopts[] = {
69         MOPT_STDOPTS,
70         MOPT_FORCE,
71         MOPT_SYNC,
72         MOPT_UPDATE,
73         { "shortnames", 0, MSDOSFSMNT_SHORTNAME, 1 },
74         { "longnames", 0, MSDOSFSMNT_LONGNAME, 1 },
75         { "nowin95", 0, MSDOSFSMNT_NOWIN95, 1 },
76         MOPT_END
77 };
78
79 static gid_t    a_gid(char *);
80 static uid_t    a_uid(char *);
81 static mode_t   a_mask(char *);
82 static void     usage(void) __dead2;
83 static int      set_charset(struct msdosfs_args *);
84
85 int
86 main(int argc, char **argv)
87 {
88         struct msdosfs_args args;
89         struct stat sb;
90         int c, mntflags, set_gid, set_uid, set_mask, set_dirmask;
91         char *dev, *dir, mntpath[MAXPATHLEN], *csp;
92
93         mntflags = set_gid = set_uid = set_mask = set_dirmask = 0;
94         (void)memset(&args, '\0', sizeof(args));
95         args.magic = MSDOSFS_ARGSMAGIC;
96
97         args.cs_win = NULL;
98         args.cs_dos = NULL;
99         args.cs_local = NULL;
100 #ifdef TRANSITION_PERIOD_HACK
101         while ((c = getopt(argc, argv, "sl9u:g:m:M:o:L:D:W:")) != -1) {
102 #else
103         while ((c = getopt(argc, argv, "sl9u:g:m:M:o:L:D:")) != -1) {
104 #endif
105                 switch (c) {
106                 case 's':
107                         args.flags |= MSDOSFSMNT_SHORTNAME;
108                         break;
109                 case 'l':
110                         args.flags |= MSDOSFSMNT_LONGNAME;
111                         break;
112                 case '9':
113                         args.flags |= MSDOSFSMNT_NOWIN95;
114                         break;
115                 case 'u':
116                         args.uid = a_uid(optarg);
117                         set_uid = 1;
118                         break;
119                 case 'g':
120                         args.gid = a_gid(optarg);
121                         set_gid = 1;
122                         break;
123                 case 'm':
124                         args.mask = a_mask(optarg);
125                         set_mask = 1;
126                         break;
127                 case 'M':
128                         args.dirmask = a_mask(optarg);
129                         set_dirmask = 1;
130                         break;
131                 case 'L':
132                         if (setlocale(LC_CTYPE, optarg) == NULL)
133                                 err(EX_CONFIG, "%s", optarg);
134                         csp = strchr(optarg,'.');
135                         if (!csp)
136                                 err(EX_CONFIG, "%s", optarg);
137                         args.cs_local = malloc(ICONV_CSNMAXLEN);
138                         if (args.cs_local == NULL)
139                                 err(EX_OSERR, "malloc()");
140                         strncpy(args.cs_local,
141                             kiconv_quirkcs(csp + 1, KICONV_VENDOR_MICSFT),
142                             ICONV_CSNMAXLEN);
143                         break;
144                 case 'D':
145                         args.cs_dos = malloc(ICONV_CSNMAXLEN);
146                         if (args.cs_dos == NULL)
147                                 err(EX_OSERR, "malloc()");
148                         strncpy(args.cs_dos, optarg, ICONV_CSNMAXLEN);
149                         break;
150                 case 'o':
151                         getmntopts(optarg, mopts, &mntflags, &args.flags);
152                         break;
153 #ifdef TRANSITION_PERIOD_HACK
154                 case 'W':
155                         args.cs_local = malloc(ICONV_CSNMAXLEN);
156                         if (args.cs_local == NULL)
157                                 err(EX_OSERR, "malloc()");
158                         args.cs_dos = malloc(ICONV_CSNMAXLEN);
159                         if (args.cs_dos == NULL)
160                                 err(EX_OSERR, "malloc()");
161                         if (strcmp(optarg, "iso22dos") == 0) {
162                                 strcpy(args.cs_local, "ISO8859-2");
163                                 strcpy(args.cs_dos, "CP852");
164                         } else if (strcmp(optarg, "iso72dos") == 0) {
165                                 strcpy(args.cs_local, "ISO8859-7");
166                                 strcpy(args.cs_dos, "CP737");
167                         } else if (strcmp(optarg, "koi2dos") == 0) {
168                                 strcpy(args.cs_local, "KOI8-R");
169                                 strcpy(args.cs_dos, "CP866");
170                         } else if (strcmp(optarg, "koi8u2dos") == 0) {
171                                 strcpy(args.cs_local, "KOI8-U");
172                                 strcpy(args.cs_dos, "CP866");
173                         } else {
174                                 err(EX_NOINPUT, "%s", optarg);
175                         }
176                         break;
177 #endif /* TRANSITION_PERIOD_HACK */
178                 case '?':
179                 default:
180                         usage();
181                         break;
182                 }
183         }
184
185         if (optind + 2 != argc)
186                 usage();
187
188         if (set_mask && !set_dirmask) {
189                 args.dirmask = args.mask;
190                 set_dirmask = 1;
191         }
192         else if (set_dirmask && !set_mask) {
193                 args.mask = args.dirmask;
194                 set_mask = 1;
195         }
196
197         dev = argv[optind];
198         dir = argv[optind + 1];
199
200         if (args.cs_local) {
201                 if (set_charset(&args) == -1)
202                         err(EX_OSERR, "msdosfs_iconv");
203                 args.flags |= MSDOSFSMNT_KICONV;
204         } else if (args.cs_dos) {
205                 if ((args.cs_local = malloc(ICONV_CSNMAXLEN)) == NULL)
206                         err(EX_OSERR, "malloc()");
207                 strcpy(args.cs_local, "ISO8859-1");
208                 if (set_charset(&args) == -1)
209                         err(EX_OSERR, "msdosfs_iconv");
210                 args.flags |= MSDOSFSMNT_KICONV;
211         }
212
213         /*
214          * Resolve the mountpoint with realpath(3) and remove unnecessary
215          * slashes from the devicename if there are any.
216          */
217         (void)checkpath(dir, mntpath);
218         (void)rmslashes(dev, dev);
219
220         args.fspec = dev;
221         args.export.ex_root = -2;       /* unchecked anyway on DOS fs */
222         if (mntflags & MNT_RDONLY)
223                 args.export.ex_flags = MNT_EXRDONLY;
224         else
225                 args.export.ex_flags = 0;
226         if (!set_gid || !set_uid || !set_mask) {
227                 if (stat(mntpath, &sb) == -1)
228                         err(EX_OSERR, "stat %s", mntpath);
229
230                 if (!set_uid)
231                         args.uid = sb.st_uid;
232                 if (!set_gid)
233                         args.gid = sb.st_gid;
234                 if (!set_mask)
235                         args.mask = args.dirmask =
236                                 sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
237         }
238
239         if (mount("msdosfs", mntpath, mntflags, &args) < 0)
240                 err(EX_OSERR, "%s", dev);
241
242         exit (0);
243 }
244
245 gid_t
246 a_gid(s)
247         char *s;
248 {
249         struct group *gr;
250         char *gname;
251         gid_t gid;
252
253         if ((gr = getgrnam(s)) != NULL)
254                 gid = gr->gr_gid;
255         else {
256                 for (gname = s; *s && isdigit(*s); ++s);
257                 if (!*s)
258                         gid = atoi(gname);
259                 else
260                         errx(EX_NOUSER, "unknown group id: %s", gname);
261         }
262         return (gid);
263 }
264
265 uid_t
266 a_uid(s)
267         char *s;
268 {
269         struct passwd *pw;
270         char *uname;
271         uid_t uid;
272
273         if ((pw = getpwnam(s)) != NULL)
274                 uid = pw->pw_uid;
275         else {
276                 for (uname = s; *s && isdigit(*s); ++s);
277                 if (!*s)
278                         uid = atoi(uname);
279                 else
280                         errx(EX_NOUSER, "unknown user id: %s", uname);
281         }
282         return (uid);
283 }
284
285 mode_t
286 a_mask(s)
287         char *s;
288 {
289         int done, rv;
290         char *ep;
291
292         done = 0;
293         rv = -1;
294         if (*s >= '0' && *s <= '7') {
295                 done = 1;
296                 rv = strtol(optarg, &ep, 8);
297         }
298         if (!done || rv < 0 || *ep)
299                 errx(EX_USAGE, "invalid file mode: %s", s);
300         return (rv);
301 }
302
303 void
304 usage()
305 {
306 #ifdef TRANSITION_PERIOD_HACK
307         fprintf(stderr, "%s\n%s\n%s\n",
308         "usage: mount_msdosfs [-9ls] [-D DOS_codepage] [-g gid] [-L locale]",
309         "                     [-M mask] [-m mask] [-o options] [-u uid]",
310         "                     [-W table] special node");
311 #else
312         fprintf(stderr, "%s\n%s\n%s\n",
313         "usage: mount_msdosfs [-9ls] [-D DOS_codepage] [-g gid] [-L locale]",
314         "                     [-M mask] [-m mask] [-o options] [-u uid]",
315         "                     special node");
316 #endif
317         exit(EX_USAGE);
318 }
319
320 int
321 set_charset(struct msdosfs_args *args)
322 {
323         int error;
324
325         if (modfind("msdosfs_iconv") < 0)
326                 if (kldload("msdosfs_iconv") < 0 || modfind("msdosfs_iconv") < 0) {
327                         warnx("cannot find or load \"msdosfs_iconv\" kernel module");
328                         return (-1);
329                 }
330
331         if ((args->cs_win = malloc(ICONV_CSNMAXLEN)) == NULL)
332                 return (-1);
333         strncpy(args->cs_win, ENCODING_UNICODE, ICONV_CSNMAXLEN);
334         error = kiconv_add_xlat16_cspairs(args->cs_win, args->cs_local);
335         if (error)
336                 return (-1);
337         if (args->cs_dos) {
338                 error = kiconv_add_xlat16_cspairs(args->cs_dos, args->cs_local);
339                 if (error)
340                         return (-1);
341         } else {
342                 if ((args->cs_dos = malloc(ICONV_CSNMAXLEN)) == NULL)
343                         return (-1);
344                 strcpy(args->cs_dos, args->cs_local);
345                 error = kiconv_add_xlat16_cspair(args->cs_local, args->cs_local,
346                                 KICONV_FROM_UPPER | KICONV_LOWER);
347                 if (error)
348                         return (-1);
349         }
350
351         return (0);
352 }