xref: /netbsd-src/sbin/mount_msdos/mount_msdos.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /* $NetBSD: mount_msdos.c,v 1.44 2007/12/15 19:44:46 perry 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.44 2007/12/15 19:44:46 perry 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 #include <fattr.h>
58 
59 static const struct mntopt mopts[] = {
60 	MOPT_STDOPTS,
61 	MOPT_ASYNC,
62 	MOPT_SYNC,
63 	MOPT_UPDATE,
64 	MOPT_GETARGS,
65 	MOPT_NULL,
66 };
67 
68 int	mount_msdos(int argc, char **argv);
69 static void	usage(void) __dead;
70 
71 #ifndef MOUNT_NOMAIN
72 int
73 main(int argc, char **argv)
74 {
75 	return mount_msdos(argc, argv);
76 }
77 #endif
78 
79 int
80 mount_msdos(int argc, char **argv)
81 {
82 	struct msdosfs_args args;
83 	struct stat sb;
84 	int c, mntflags, set_gid, set_uid, set_mask, set_dirmask, set_gmtoff;
85 	char *dev, *dir, canon_dev[MAXPATHLEN], canon_dir[MAXPATHLEN];
86 	time_t now;
87 	struct tm *tm;
88 	mntoptparse_t mp;
89 
90 	mntflags = set_gid = set_uid = set_mask = set_dirmask = set_gmtoff = 0;
91 	(void)memset(&args, '\0', sizeof(args));
92 
93 	while ((c = getopt(argc, argv, "Gsl9u:g:m:M:o:t:")) != -1) {
94 		switch (c) {
95 		case 'G':
96 			args.flags |= MSDOSFSMNT_GEMDOSFS;
97 			break;
98 		case 's':
99 			args.flags |= MSDOSFSMNT_SHORTNAME;
100 			break;
101 		case 'l':
102 			args.flags |= MSDOSFSMNT_LONGNAME;
103 			break;
104 		case '9':
105 			args.flags |= MSDOSFSMNT_NOWIN95;
106 			break;
107 		case 'u':
108 			args.uid = a_uid(optarg);
109 			set_uid = 1;
110 			break;
111 		case 'g':
112 			args.gid = a_gid(optarg);
113 			set_gid = 1;
114 			break;
115 		case 'm':
116 			args.mask = a_mask(optarg);
117 			set_mask = 1;
118 			break;
119 		case 'M':
120 			args.dirmask = a_mask(optarg);
121 			set_dirmask = 1;
122 			break;
123 		case 'o':
124 			mp = getmntopts(optarg, mopts, &mntflags, 0);
125 			if (mp == NULL)
126 				err(1, "getmntopts");
127 			freemntopts(mp);
128 			break;
129 		case 't':
130 			args.gmtoff = atoi(optarg);
131 			set_gmtoff = 1;
132 			break;
133 		case '?':
134 		default:
135 			usage();
136 			break;
137 		}
138 	}
139 
140 	if (optind + 2 != argc)
141 		usage();
142 
143 	if (set_mask && !set_dirmask) {
144 		args.dirmask = args.mask;
145 		set_dirmask = 1;
146 	} else if (set_dirmask && !set_mask) {
147 		args.mask = args.dirmask;
148 		set_mask = 1;
149 	}
150 
151 	dev = argv[optind];
152 	dir = argv[optind + 1];
153 
154 	if (realpath(dev, canon_dev) == NULL)        /* Check device path */
155 		err(1, "realpath %s", dev);
156 	if (strncmp(dev, canon_dev, MAXPATHLEN)) {
157 		warnx("\"%s\" is a relative path.", dev);
158 		dev = canon_dev;
159 		warnx("using \"%s\" instead.", dev);
160 	}
161 
162 	if (realpath(dir, canon_dir) == NULL)        /* Check mounton path */
163 		err(1, "realpath %s", dir);
164 	if (strncmp(dir, canon_dir, MAXPATHLEN)) {
165 		warnx("\"%s\" is a relative path.", dir);
166 		dir = canon_dir;
167 		warnx("using \"%s\" instead.", dir);
168 	}
169 
170 	args.fspec = dev;
171 	if (!set_gid || !set_uid || !set_mask) {
172 		if (stat(dir, &sb) == -1)
173 			err(1, "stat %s", dir);
174 
175 		if (!set_uid)
176 			args.uid = sb.st_uid;
177 		if (!set_gid)
178 			args.gid = sb.st_gid;
179 		if (!set_mask) {
180 			args.mask = args.dirmask =
181 				sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
182 		}
183 	}
184 
185 	if (!set_gmtoff) {
186 		/* use user's time zone as default */
187 		time(&now);
188 		tm = localtime(&now);
189 		args.gmtoff = tm->tm_gmtoff;
190 
191 	}
192 	args.flags |= MSDOSFSMNT_VERSIONED;
193 	args.version = MSDOSFSMNT_VERSION;
194 
195 	if (mount(MOUNT_MSDOS, dir, mntflags, &args, sizeof args) == -1)
196 		err(1, "%s on %s", dev, dir);
197 
198 	if (mntflags & MNT_GETARGS) {
199 		char buf[1024];
200 		(void)snprintb(buf, sizeof(buf), MSDOSFSMNT_BITS, args.flags);
201 		printf("uid=%d, gid=%d, mask=0%o, dirmask=0%o, flags=%s\n",
202 		    args.uid, args.gid, args.mask, args.dirmask, buf);
203 	}
204 
205 	exit (0);
206 }
207 
208 static void
209 usage(void)
210 {
211 
212 	fprintf(stderr, "usage: mount_msdos [-9Gls] [-g gid] [-M mask] [-m mask] [-o options]\n"
213 			"\t[-t gmtoff] [-u uid] special node\n");
214 	exit(1);
215 }
216