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