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