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