1 /* $OpenBSD: mount_msdos.c,v 1.36 2022/12/04 23:50:47 cheloha 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/types.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 <limits.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
main(int argc,char ** argv)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[PATH_MAX];
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, "sl9u: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 'u':
86 args.uid = a_uid(optarg);
87 set_uid = 1;
88 break;
89 case 'g':
90 args.gid = a_gid(optarg);
91 set_gid = 1;
92 break;
93 case 'm':
94 args.mask = a_mask(optarg);
95 set_mask = 1;
96 break;
97 case 'o':
98 getmntopts(optarg, mopts, &mntflags);
99 break;
100 default:
101 usage();
102 break;
103 }
104 }
105
106 if (optind + 2 != argc)
107 usage();
108
109 dev = argv[optind];
110 if (realpath(argv[optind + 1], dir) == NULL)
111 err(1, "realpath %s", argv[optind + 1]);
112
113 args.fspec = dev;
114 args.export_info.ex_root = -2; /* unchecked anyway on DOS fs */
115 if (mntflags & MNT_RDONLY)
116 args.export_info.ex_flags = MNT_EXRDONLY;
117 else
118 args.export_info.ex_flags = 0;
119 if (!set_gid || !set_uid || !set_mask) {
120 if (stat(dir, &sb) == -1)
121 err(1, "stat %s", dir);
122
123 if (!set_uid)
124 args.uid = sb.st_uid;
125 if (!set_gid)
126 args.gid = sb.st_gid;
127 if (!set_mask)
128 args.mask = sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
129 }
130
131 if (mount(MOUNT_MSDOS, dir, mntflags, &args) == -1) {
132 switch (errno) {
133 case EOPNOTSUPP:
134 errcause = "filesystem not supported by kernel";
135 break;
136 case EMFILE:
137 errcause = "mount table full";
138 break;
139 case EINVAL:
140 errcause =
141 "not an MSDOS filesystem";
142 break;
143 default:
144 errcause = strerror(errno);
145 break;
146 }
147 errx(1, "%s on %s: %s", args.fspec, dir, errcause);
148 }
149
150 exit (0);
151 }
152
153 gid_t
a_gid(char * s)154 a_gid(char *s)
155 {
156 struct group *gr;
157 const char *errstr;
158 gid_t gid;
159
160 if ((gr = getgrnam(s)) != NULL)
161 return gr->gr_gid;
162 gid = strtonum(s, 0, GID_MAX, &errstr);
163 if (errstr)
164 errx(1, "group is %s: %s", errstr, s);
165 return (gid);
166 }
167
168 uid_t
a_uid(char * s)169 a_uid(char *s)
170 {
171 struct passwd *pw;
172 const char *errstr;
173 uid_t uid;
174
175 if ((pw = getpwnam(s)) != NULL)
176 return pw->pw_uid;
177 uid = strtonum(s, 0, UID_MAX, &errstr);
178 if (errstr)
179 errx(1, "user is %s: %s", errstr, s);
180 return (uid);
181 }
182
183 mode_t
a_mask(char * s)184 a_mask(char *s)
185 {
186 int done, rv;
187 char *ep;
188
189 done = 0;
190 if (*s >= '0' && *s <= '7') {
191 done = 1;
192 rv = strtol(optarg, &ep, 8);
193 }
194 if (!done || rv < 0 || *ep)
195 errx(1, "invalid file mode: %s", s);
196 return (rv);
197 }
198
199 void
usage(void)200 usage(void)
201 {
202
203 fprintf(stderr,
204 "usage: mount_msdos [-9ls] [-g group] [-m mask] [-o options] [-u user] special node\n");
205 exit(1);
206 }
207