xref: /openbsd-src/sbin/mount_msdos/mount_msdos.c (revision 2b0358df1d88d06ef4139321dd05bd5e05d91eaf)
1 /*	$OpenBSD: mount_msdos.c,v 1.22 2007/03/20 03:45:54 tedu 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.22 2007/03/20 03:45:54 tedu 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(char *);
61 uid_t	a_uid(char *);
62 mode_t	a_mask(char *);
63 void	usage(void);
64 
65 int
66 main(int argc, char **argv)
67 {
68 	struct msdosfs_args args;
69 	struct stat sb;
70 	int c, mntflags, set_gid, set_uid, set_mask;
71 	char *dev, dir[MAXPATHLEN];
72 	char *errcause;
73 
74 	mntflags = set_gid = set_uid = set_mask = 0;
75 	(void)memset(&args, '\0', sizeof(args));
76 
77 	while ((c = getopt(argc, argv, "sl9xu:g:m:o:")) != -1) {
78 		switch (c) {
79 		case 's':
80 			args.flags |= MSDOSFSMNT_SHORTNAME;
81 			break;
82 		case 'l':
83 			args.flags |= MSDOSFSMNT_LONGNAME;
84 			break;
85 		case '9':
86 			args.flags |= MSDOSFSMNT_NOWIN95;
87 			break;
88 		case 'x':
89 			args.flags |= MSDOSFSMNT_ALLOWDIRX;
90 			break;
91 		case 'u':
92 			args.uid = a_uid(optarg);
93 			set_uid = 1;
94 			break;
95 		case 'g':
96 			args.gid = a_gid(optarg);
97 			set_gid = 1;
98 			break;
99 		case 'm':
100 			args.mask = a_mask(optarg);
101 			set_mask = 1;
102 			break;
103 		case 'o':
104 			getmntopts(optarg, mopts, &mntflags);
105 			break;
106 		case '?':
107 		default:
108 			usage();
109 			break;
110 		}
111 	}
112 
113 	if (optind + 2 != argc)
114 		usage();
115 
116 	dev = argv[optind];
117 	if (realpath(argv[optind + 1], dir) == NULL)
118 		err(1, "realpath %s", argv[optind + 1]);
119 
120 	args.fspec = dev;
121 	args.export_info.ex_root = -2;	/* unchecked anyway on DOS fs */
122 	if (mntflags & MNT_RDONLY)
123 		args.export_info.ex_flags = MNT_EXRDONLY;
124 	else
125 		args.export_info.ex_flags = 0;
126 	if (!set_gid || !set_uid || !set_mask) {
127 		if (stat(dir, &sb) == -1)
128 			err(1, "stat %s", dir);
129 
130 		if (!set_uid)
131 			args.uid = sb.st_uid;
132 		if (!set_gid)
133 			args.gid = sb.st_gid;
134 		if (!set_mask)
135 			args.mask = sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
136 	}
137 
138 	if (mount(MOUNT_MSDOS, dir, mntflags, &args) < 0) {
139 		switch (errno) {
140 		case EOPNOTSUPP:
141 			errcause = "filesystem not supported by kernel";
142 			break;
143 		case EMFILE:
144 			errcause = "mount table full";
145 			break;
146 		case EINVAL:
147 			errcause =
148 			    "not an MSDOS filesystem";
149 			break;
150 		default:
151 			errcause = strerror(errno);
152 			break;
153 		}
154 		errx(1, "%s on %s: %s", args.fspec, dir, errcause);
155 	}
156 
157 	exit (0);
158 }
159 
160 gid_t
161 a_gid(char *s)
162 {
163 	struct group *gr;
164 	char *gname;
165 	gid_t gid;
166 
167 	if ((gr = getgrnam(s)) != NULL)
168 		gid = gr->gr_gid;
169 	else {
170 		for (gname = s; isdigit(*s); ++s);
171 		if (!*s)
172 			gid = atoi(gname);
173 		else
174 			errx(1, "unknown group id: %s", gname);
175 	}
176 	return (gid);
177 }
178 
179 uid_t
180 a_uid(char *s)
181 {
182 	struct passwd *pw;
183 	char *uname;
184 	uid_t uid;
185 
186 	if ((pw = getpwnam(s)) != NULL)
187 		uid = pw->pw_uid;
188 	else {
189 		for (uname = s; isdigit(*s); ++s);
190 		if (!*s)
191 			uid = atoi(uname);
192 		else
193 			errx(1, "unknown user id: %s", uname);
194 	}
195 	return (uid);
196 }
197 
198 mode_t
199 a_mask(char *s)
200 {
201 	int done, rv;
202 	char *ep;
203 
204 	done = 0;
205 	if (*s >= '0' && *s <= '7') {
206 		done = 1;
207 		rv = strtol(optarg, &ep, 8);
208 	}
209 	if (!done || rv < 0 || *ep)
210 		errx(1, "invalid file mode: %s", s);
211 	return (rv);
212 }
213 
214 void
215 usage(void)
216 {
217 
218 	fprintf(stderr,
219 	    "usage: mount_msdos [-9lsx] [-g gid] [-m mask] [-o options] [-u uid] special node\n");
220 	exit(1);
221 }
222