mount_msdos.c revision 1.36 1 1.36 xtraeme /* $NetBSD: mount_msdos.c,v 1.36 2005/02/05 15:02:20 xtraeme Exp $ */
2 1.10 cgd
3 1.1 cgd /*
4 1.5 cgd * Copyright (c) 1994 Christopher G. Demetriou
5 1.5 cgd * All rights reserved.
6 1.23 cgd *
7 1.5 cgd * Redistribution and use in source and binary forms, with or without
8 1.5 cgd * modification, are permitted provided that the following conditions
9 1.5 cgd * are met:
10 1.5 cgd * 1. Redistributions of source code must retain the above copyright
11 1.5 cgd * notice, this list of conditions and the following disclaimer.
12 1.5 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.5 cgd * notice, this list of conditions and the following disclaimer in the
14 1.5 cgd * documentation and/or other materials provided with the distribution.
15 1.5 cgd * 3. All advertising materials mentioning features or use of this software
16 1.5 cgd * must display the following acknowledgement:
17 1.23 cgd * This product includes software developed for the
18 1.26 grant * NetBSD Project. See http://www.NetBSD.org/ for
19 1.23 cgd * information about NetBSD.
20 1.5 cgd * 4. The name of the author may not be used to endorse or promote products
21 1.23 cgd * derived from this software without specific prior written permission.
22 1.23 cgd *
23 1.5 cgd * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 1.5 cgd * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 1.5 cgd * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 1.5 cgd * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 1.5 cgd * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 1.5 cgd * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 1.5 cgd * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 1.5 cgd * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 1.5 cgd * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 1.5 cgd * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 1.23 cgd *
34 1.23 cgd * <<Id: LICENSE,v 1.2 2000/06/14 15:57:33 cgd Exp>>
35 1.1 cgd */
36 1.2 mycroft
37 1.17 lukem #include <sys/cdefs.h>
38 1.2 mycroft #ifndef lint
39 1.36 xtraeme __RCSID("$NetBSD: mount_msdos.c,v 1.36 2005/02/05 15:02:20 xtraeme Exp $");
40 1.2 mycroft #endif /* not lint */
41 1.2 mycroft
42 1.6 cgd #include <sys/cdefs.h>
43 1.7 cgd #include <sys/param.h>
44 1.1 cgd #include <sys/mount.h>
45 1.6 cgd #include <sys/stat.h>
46 1.19 fvdl #include <msdosfs/msdosfsmount.h>
47 1.6 cgd #include <ctype.h>
48 1.6 cgd #include <err.h>
49 1.6 cgd #include <grp.h>
50 1.6 cgd #include <pwd.h>
51 1.5 cgd #include <stdio.h>
52 1.5 cgd #include <stdlib.h>
53 1.5 cgd #include <string.h>
54 1.31 itojun #include <time.h>
55 1.7 cgd #include <unistd.h>
56 1.25 christos #include <util.h>
57 1.1 cgd
58 1.27 jdolecek #include <mntopts.h>
59 1.24 jdolecek #include <fattr.h>
60 1.8 cgd
61 1.24 jdolecek static const struct mntopt mopts[] = {
62 1.8 cgd MOPT_STDOPTS,
63 1.21 jdolecek MOPT_ASYNC,
64 1.21 jdolecek MOPT_SYNC,
65 1.16 cgd MOPT_UPDATE,
66 1.30 jdolecek MOPT_GETARGS,
67 1.8 cgd { NULL }
68 1.8 cgd };
69 1.8 cgd
70 1.36 xtraeme int mount_msdos(int argc, char **argv);
71 1.36 xtraeme static void usage(void) __attribute__((__noreturn__));
72 1.8 cgd
73 1.24 jdolecek #ifndef MOUNT_NOMAIN
74 1.1 cgd int
75 1.36 xtraeme main(int argc, char **argv)
76 1.1 cgd {
77 1.24 jdolecek return mount_msdos(argc, argv);
78 1.24 jdolecek }
79 1.24 jdolecek #endif
80 1.24 jdolecek
81 1.24 jdolecek int
82 1.36 xtraeme mount_msdos(int argc, char **argv)
83 1.24 jdolecek {
84 1.3 mycroft struct msdosfs_args args;
85 1.6 cgd struct stat sb;
86 1.31 itojun int c, mntflags, set_gid, set_uid, set_mask, set_dirmask, set_gmtoff;
87 1.35 erh char *dev, *dir, canon_dev[MAXPATHLEN], canon_dir[MAXPATHLEN];
88 1.31 itojun time_t now;
89 1.31 itojun struct tm *tm;
90 1.1 cgd
91 1.31 itojun mntflags = set_gid = set_uid = set_mask = set_dirmask = set_gmtoff = 0;
92 1.6 cgd (void)memset(&args, '\0', sizeof(args));
93 1.1 cgd
94 1.31 itojun while ((c = getopt(argc, argv, "Gsl9u:g:m:M:o:t:")) != -1) {
95 1.1 cgd switch (c) {
96 1.12 leo case 'G':
97 1.12 leo args.flags |= MSDOSFSMNT_GEMDOSFS;
98 1.12 leo break;
99 1.11 ws case 's':
100 1.11 ws args.flags |= MSDOSFSMNT_SHORTNAME;
101 1.11 ws break;
102 1.11 ws case 'l':
103 1.11 ws args.flags |= MSDOSFSMNT_LONGNAME;
104 1.11 ws break;
105 1.11 ws case '9':
106 1.11 ws args.flags |= MSDOSFSMNT_NOWIN95;
107 1.11 ws break;
108 1.6 cgd case 'u':
109 1.6 cgd args.uid = a_uid(optarg);
110 1.6 cgd set_uid = 1;
111 1.6 cgd break;
112 1.6 cgd case 'g':
113 1.6 cgd args.gid = a_gid(optarg);
114 1.6 cgd set_gid = 1;
115 1.1 cgd break;
116 1.6 cgd case 'm':
117 1.6 cgd args.mask = a_mask(optarg);
118 1.6 cgd set_mask = 1;
119 1.6 cgd break;
120 1.29 jdolecek case 'M':
121 1.29 jdolecek args.dirmask = a_mask(optarg);
122 1.29 jdolecek set_dirmask = 1;
123 1.29 jdolecek break;
124 1.8 cgd case 'o':
125 1.18 lukem getmntopts(optarg, mopts, &mntflags, 0);
126 1.8 cgd break;
127 1.31 itojun case 't':
128 1.31 itojun args.gmtoff = atoi(optarg);
129 1.31 itojun set_gmtoff = 1;
130 1.31 itojun break;
131 1.6 cgd case '?':
132 1.1 cgd default:
133 1.6 cgd usage();
134 1.6 cgd break;
135 1.1 cgd }
136 1.1 cgd }
137 1.1 cgd
138 1.1 cgd if (optind + 2 != argc)
139 1.6 cgd usage();
140 1.1 cgd
141 1.29 jdolecek if (set_mask && !set_dirmask) {
142 1.29 jdolecek args.dirmask = args.mask;
143 1.29 jdolecek set_dirmask = 1;
144 1.29 jdolecek } else if (set_dirmask && !set_mask) {
145 1.29 jdolecek args.mask = args.dirmask;
146 1.29 jdolecek set_mask = 1;
147 1.29 jdolecek }
148 1.29 jdolecek
149 1.1 cgd dev = argv[optind];
150 1.1 cgd dir = argv[optind + 1];
151 1.35 erh
152 1.35 erh if (realpath(dev, canon_dev) == NULL) /* Check device path */
153 1.35 erh err(1, "realpath %s", dev);
154 1.35 erh if (strncmp(dev, canon_dev, MAXPATHLEN)) {
155 1.35 erh warnx("\"%s\" is a relative path.", dev);
156 1.35 erh dev = canon_dev;
157 1.35 erh warnx("using \"%s\" instead.", dev);
158 1.35 erh }
159 1.35 erh
160 1.35 erh if (realpath(dir, canon_dir) == NULL) /* Check mounton path */
161 1.35 erh err(1, "realpath %s", dir);
162 1.35 erh if (strncmp(dir, canon_dir, MAXPATHLEN)) {
163 1.7 cgd warnx("\"%s\" is a relative path.", dir);
164 1.35 erh dir = canon_dir;
165 1.7 cgd warnx("using \"%s\" instead.", dir);
166 1.7 cgd }
167 1.1 cgd
168 1.1 cgd args.fspec = dev;
169 1.8 cgd args.export.ex_root = -2; /* unchecked anyway on DOS fs */
170 1.8 cgd if (mntflags & MNT_RDONLY)
171 1.8 cgd args.export.ex_flags = MNT_EXRDONLY;
172 1.8 cgd else
173 1.8 cgd args.export.ex_flags = 0;
174 1.6 cgd if (!set_gid || !set_uid || !set_mask) {
175 1.6 cgd if (stat(dir, &sb) == -1)
176 1.6 cgd err(1, "stat %s", dir);
177 1.6 cgd
178 1.6 cgd if (!set_uid)
179 1.6 cgd args.uid = sb.st_uid;
180 1.6 cgd if (!set_gid)
181 1.6 cgd args.gid = sb.st_gid;
182 1.29 jdolecek if (!set_mask) {
183 1.29 jdolecek args.mask = args.dirmask =
184 1.29 jdolecek sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
185 1.29 jdolecek }
186 1.31 itojun }
187 1.31 itojun
188 1.31 itojun if (!set_gmtoff) {
189 1.31 itojun /* use user's time zone as default */
190 1.31 itojun time(&now);
191 1.31 itojun tm = localtime(&now);
192 1.31 itojun args.gmtoff = tm->tm_gmtoff;
193 1.31 itojun
194 1.6 cgd }
195 1.29 jdolecek args.flags |= MSDOSFSMNT_VERSIONED;
196 1.29 jdolecek args.version = MSDOSFSMNT_VERSION;
197 1.6 cgd
198 1.8 cgd if (mount(MOUNT_MSDOS, dir, mntflags, &args) < 0)
199 1.20 perseant err(1, "%s on %s", dev, dir);
200 1.25 christos
201 1.25 christos if (mntflags & MNT_GETARGS) {
202 1.25 christos char buf[1024];
203 1.25 christos (void)snprintb(buf, sizeof(buf), MSDOSFSMNT_BITS, args.flags);
204 1.29 jdolecek printf("uid=%d, gid=%d, mask=0%o, dirmask=0%o, flags=%s\n",
205 1.29 jdolecek args.uid, args.gid, args.mask, args.dirmask, buf);
206 1.25 christos }
207 1.6 cgd
208 1.6 cgd exit (0);
209 1.6 cgd }
210 1.6 cgd
211 1.24 jdolecek static void
212 1.36 xtraeme usage(void)
213 1.6 cgd {
214 1.9 mycroft
215 1.34 jmmv fprintf(stderr, "usage: mount_msdos [-9Gls] [-g gid] [-M mask] [-m mask] [-o options]\n"
216 1.32 wiz "\t[-t gmtoff] [-u uid] special node\n");
217 1.6 cgd exit(1);
218 1.1 cgd }
219