1 /* $NetBSD: mount_msdos.c,v 1.50 2016/08/21 22:27:20 jdolecek 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 for the
18  *          NetBSD Project.  See http://www.NetBSD.org/ for
19  *          information about NetBSD.
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: LICENSE,v 1.2 2000/06/14 15:57:33 cgd Exp>>
35  */
36 
37 #include <sys/cdefs.h>
38 #ifndef lint
39 __RCSID("$NetBSD: mount_msdos.c,v 1.50 2016/08/21 22:27:20 jdolecek Exp $");
40 #endif /* not lint */
41 
42 #include <sys/param.h>
43 #include <sys/mount.h>
44 #include <sys/stat.h>
45 #include <msdosfs/msdosfsmount.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 <time.h>
53 #include <unistd.h>
54 #include <util.h>
55 
56 #include <mntopts.h>
57 
58 #include "mountprog.h"
59 #include "mount_msdos.h"
60 
61 static const struct mntopt mopts[] = {
62           MOPT_STDOPTS,
63           MOPT_ASYNC,
64           MOPT_SYNC,
65           MOPT_UPDATE,
66           MOPT_GETARGS,
67           MOPT_NULL,
68 };
69 
70 static void         usage(void) __dead;
71 
72 #ifndef MOUNT_NOMAIN
73 int
main(int argc,char ** argv)74 main(int argc, char **argv)
75 {
76 
77           setprogname(argv[0]);
78           return mount_msdos(argc, argv);
79 }
80 #endif
81 
82 void
mount_msdos_parseargs(int argc,char ** argv,struct msdosfs_args * args,int * mntflags,char * canon_dev,char * canon_dir)83 mount_msdos_parseargs(int argc, char **argv,
84           struct msdosfs_args *args, int *mntflags,
85           char *canon_dev, char *canon_dir)
86 {
87           struct stat sb;
88           int c, set_gid, set_uid, set_mask, set_dirmask, set_gmtoff;
89           char *dev, *dir;
90           time_t now;
91           struct tm *tm;
92           mntoptparse_t mp;
93 
94           *mntflags = set_gid = set_uid = set_mask = set_dirmask = set_gmtoff = 0;
95           (void)memset(args, '\0', sizeof(*args));
96 
97           while ((c = getopt(argc, argv, "Gsl9Uu:g:m:M:o:t:")) != -1) {
98                     switch (c) {
99                     case 'G':
100                               args->flags |= MSDOSFSMNT_GEMDOSFS;
101                               break;
102                     case 's':
103                               args->flags |= MSDOSFSMNT_SHORTNAME;
104                               break;
105                     case 'l':
106                               args->flags |= MSDOSFSMNT_LONGNAME;
107                               break;
108                     case '9':
109                               args->flags |= MSDOSFSMNT_NOWIN95;
110                               break;
111                     case 'U':
112                               args->flags |= MSDOSFSMNT_UTF8;
113                               break;
114                     case 'u':
115                               args->uid = a_uid(optarg);
116                               set_uid = 1;
117                               break;
118                     case 'g':
119                               args->gid = a_gid(optarg);
120                               set_gid = 1;
121                               break;
122                     case 'm':
123                               args->mask = a_mask(optarg);
124                               set_mask = 1;
125                               break;
126                     case 'M':
127                               args->dirmask = a_mask(optarg);
128                               set_dirmask = 1;
129                               break;
130                     case 'o':
131                               mp = getmntopts(optarg, mopts, mntflags, 0);
132                               if (mp == NULL)
133                                         err(1, "getmntopts");
134                               freemntopts(mp);
135                               break;
136                     case 't':
137                               args->gmtoff = atoi(optarg);
138                               set_gmtoff = 1;
139                               break;
140                     case '?':
141                     default:
142                               usage();
143                               break;
144                     }
145           }
146 
147           if (optind + 2 != argc)
148                     usage();
149 
150           if (set_mask && !set_dirmask) {
151                     args->dirmask = args->mask;
152                     set_dirmask = 1;
153           } else if (set_dirmask && !set_mask) {
154                     args->mask = args->dirmask;
155                     set_mask = 1;
156           }
157 
158           dev = argv[optind];
159           dir = argv[optind + 1];
160 
161           pathadj(dev, canon_dev);
162           pathadj(dir, canon_dir);
163 
164           args->fspec = canon_dev;
165           if (!set_gid || !set_uid || !set_mask) {
166                     if (stat(dir, &sb) == -1)
167                               err(1, "stat %s", dir);
168 
169                     if (!set_uid)
170                               args->uid = sb.st_uid;
171                     if (!set_gid)
172                               args->gid = sb.st_gid;
173                     if (!set_mask) {
174                               args->mask = args->dirmask =
175                                         sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
176                     }
177           }
178 
179           if (!set_gmtoff) {
180                     /* use user's time zone as default */
181                     time(&now);
182                     tm = localtime(&now);
183                     args->gmtoff = tm->tm_gmtoff;
184 
185           }
186           args->flags |= MSDOSFSMNT_VERSIONED;
187           args->version = MSDOSFSMNT_VERSION;
188 }
189 
190 int
mount_msdos(int argc,char ** argv)191 mount_msdos(int argc, char **argv)
192 {
193           struct msdosfs_args args;
194           char canon_dev[MAXPATHLEN], canon_dir[MAXPATHLEN];
195           int mntflags;
196 
197           mount_msdos_parseargs(argc, argv, &args, &mntflags,
198               canon_dev, canon_dir);
199 
200           if (mount(MOUNT_MSDOS, canon_dir, mntflags, &args, sizeof args) == -1)
201                     err(1, "%s on %s", canon_dev, canon_dir);
202 
203           if (mntflags & MNT_GETARGS) {
204                     char buf[1024];
205                     (void)snprintb(buf, sizeof(buf), MSDOSFSMNT_BITS, args.flags);
206                     printf("uid=%d, gid=%d, mask=0%o, dirmask=0%o, gmtoff=%d, flags=%s\n",
207                         args.uid, args.gid, args.mask, args.dirmask,
208                         args.gmtoff, buf);
209           }
210 
211           exit (0);
212 }
213 
214 static void
usage(void)215 usage(void)
216 {
217 
218           fprintf(stderr, "usage: %s [-9GlsU] [-g gid] [-M mask] [-m mask] "
219               "[-o options]\n\t[-t gmtoff] [-u uid] special mountpath\n",
220               getprogname());
221           exit(1);
222 }
223