xref: /openbsd-src/sbin/mount_msdos/mount_msdos.c (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1 /*	$OpenBSD: mount_msdos.c,v 1.12 1999/04/20 23:06:47 millert Exp $	*/
2 /*	$NetBSD: mount_msdos.c,v 1.16 1996/10/24 00:12:50 cgd Exp $	*/
3 
4 /*
5  * Copyright (c) 1994 Christopher G. Demetriou
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *      This product includes software developed by Christopher G. Demetriou.
19  * 4. The name of the author may not be used to endorse or promote products
20  *    derived from this software without specific prior written permission
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #ifndef lint
35 static char rcsid[] = "$OpenBSD: mount_msdos.c,v 1.12 1999/04/20 23:06:47 millert Exp $";
36 #endif /* not lint */
37 
38 #include <sys/cdefs.h>
39 #include <sys/param.h>
40 #include <sys/mount.h>
41 #include <sys/stat.h>
42 #include <ctype.h>
43 #include <err.h>
44 #include <grp.h>
45 #include <pwd.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <unistd.h>
50 #include <errno.h>
51 
52 #include "mntopts.h"
53 
54 const struct mntopt mopts[] = {
55 	MOPT_STDOPTS,
56 	MOPT_UPDATE,
57 	{ NULL }
58 };
59 
60 gid_t	a_gid __P((char *));
61 uid_t	a_uid __P((char *));
62 mode_t	a_mask __P((char *));
63 void	usage __P((void));
64 
65 int
66 main(argc, argv)
67 	int argc;
68 	char **argv;
69 {
70 	struct msdosfs_args args;
71 	struct stat sb;
72 	int c, mntflags, set_gid, set_uid, set_mask;
73 	char *dev, *dir, ndir[MAXPATHLEN+1];
74 	char *errcause;
75 
76 	mntflags = set_gid = set_uid = set_mask = 0;
77 	(void)memset(&args, '\0', sizeof(args));
78 
79 	while ((c = getopt(argc, argv, "Gsl9u:g:m:o:")) != -1) {
80 		switch (c) {
81 		case 'G':
82 			args.flags |= MSDOSFSMNT_GEMDOSFS;
83 			break;
84 		case 's':
85 			args.flags |= MSDOSFSMNT_SHORTNAME;
86 			break;
87 		case 'l':
88 			args.flags |= MSDOSFSMNT_LONGNAME;
89 			break;
90 		case '9':
91 			args.flags |= MSDOSFSMNT_NOWIN95;
92 			break;
93 		case 'u':
94 			args.uid = a_uid(optarg);
95 			set_uid = 1;
96 			break;
97 		case 'g':
98 			args.gid = a_gid(optarg);
99 			set_gid = 1;
100 			break;
101 		case 'm':
102 			args.mask = a_mask(optarg);
103 			set_mask = 1;
104 			break;
105 		case 'o':
106 			getmntopts(optarg, mopts, &mntflags);
107 			break;
108 		case '?':
109 		default:
110 			usage();
111 			break;
112 		}
113 	}
114 
115 	if (optind + 2 != argc)
116 		usage();
117 
118 	dev = argv[optind];
119 	dir = argv[optind + 1];
120 	if (dir[0] != '/') {
121 		warnx("\"%s\" is a relative path.", dir);
122 		if (getcwd(ndir, sizeof(ndir)) == NULL)
123 			err(1, "getcwd");
124 		strncat(ndir, "/", sizeof(ndir) - strlen(ndir));
125 		strncat(ndir, dir, sizeof(ndir) - strlen(ndir));
126 		dir = ndir;
127 		warnx("using \"%s\" instead.", dir);
128 	}
129 
130 	args.fspec = dev;
131 	args.export.ex_root = -2;	/* unchecked anyway on DOS fs */
132 	if (mntflags & MNT_RDONLY)
133 		args.export.ex_flags = MNT_EXRDONLY;
134 	else
135 		args.export.ex_flags = 0;
136 	if (!set_gid || !set_uid || !set_mask) {
137 		if (stat(dir, &sb) == -1)
138 			err(1, "stat %s", dir);
139 
140 		if (!set_uid)
141 			args.uid = sb.st_uid;
142 		if (!set_gid)
143 			args.gid = sb.st_gid;
144 		if (!set_mask)
145 			args.mask = sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
146 	}
147 
148 	if (mount(MOUNT_MSDOS, dir, mntflags, &args) < 0) {
149 		switch (errno) {
150 		case EOPNOTSUPP:
151 			errcause = "filesystem not supported by kernel";
152 			break;
153 		case EMFILE:
154 			errcause = "mount table full";
155 			break;
156 		case EINVAL:
157 			errcause =
158 			    "not an MSDOS filesystem";
159 			break;
160 		default:
161 			errcause = strerror(errno);
162 			break;
163 		}
164 		errx(1, "%s on %s: %s", args.fspec, dir, errcause);
165 	}
166 
167 	exit (0);
168 }
169 
170 gid_t
171 a_gid(s)
172 	char *s;
173 {
174 	struct group *gr;
175 	char *gname;
176 	gid_t gid;
177 
178 	if ((gr = getgrnam(s)) != NULL)
179 		gid = gr->gr_gid;
180 	else {
181 		for (gname = s; *s && isdigit(*s); ++s);
182 		if (!*s)
183 			gid = atoi(gname);
184 		else
185 			errx(1, "unknown group id: %s", gname);
186 	}
187 	return (gid);
188 }
189 
190 uid_t
191 a_uid(s)
192 	char *s;
193 {
194 	struct passwd *pw;
195 	char *uname;
196 	uid_t uid;
197 
198 	if ((pw = getpwnam(s)) != NULL)
199 		uid = pw->pw_uid;
200 	else {
201 		for (uname = s; *s && isdigit(*s); ++s);
202 		if (!*s)
203 			uid = atoi(uname);
204 		else
205 			errx(1, "unknown user id: %s", uname);
206 	}
207 	return (uid);
208 }
209 
210 mode_t
211 a_mask(s)
212 	char *s;
213 {
214 	int done, rv;
215 	char *ep;
216 
217 	done = 0;
218 	if (*s >= '0' && *s <= '7') {
219 		done = 1;
220 		rv = strtol(optarg, &ep, 8);
221 	}
222 	if (!done || rv < 0 || *ep)
223 		errx(1, "invalid file mode: %s", s);
224 	return (rv);
225 }
226 
227 void
228 usage()
229 {
230 
231 	fprintf(stderr, "usage: mount_msdos [-o options] [-u user] [-g group] [-m mask] bdev dir\n");
232 	exit(1);
233 }
234