xref: /dragonfly/sbin/mount_hpfs/mount_hpfs.c (revision 52f9f0d9)
1 /*
2  * Copyright (c) 1994 Christopher G. Demetriou
3  * Copyright (c) 1999 Semen Ustimenko (semenu@FreeBSD.org)
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *      This product includes software developed by Christopher G. Demetriou.
17  * 4. The name of the author may not be used to endorse or promote products
18  *    derived from this software without specific prior written permission
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  *
31  * $FreeBSD: src/sbin/mount_hpfs/mount_hpfs.c,v 1.1 1999/12/09 19:09:15 semenu Exp $
32  */
33 
34 #include <sys/cdefs.h>
35 #include <sys/param.h>
36 #include <sys/mount.h>
37 #include <sys/stat.h>
38 #include <vfs/hpfs/hpfsmount.h>
39 #include <ctype.h>
40 #include <err.h>
41 #include <grp.h>
42 #include <mntopts.h>
43 #include <pwd.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <sysexits.h>
48 #include <unistd.h>
49 
50 static struct mntopt mopts[] = {
51 	MOPT_STDOPTS,
52 	MOPT_NULL
53 };
54 
55 static gid_t	a_gid(char *);
56 static uid_t	a_uid(char *);
57 static mode_t	a_mask(char *);
58 static void	usage(void) __dead2;
59 #if 0
60 static void	load_u2wtable(struct hpfs_args *, char *);
61 #endif
62 
63 int
64 main(int argc, char **argv)
65 {
66 	struct hpfs_args args;
67 	struct stat sb;
68 	int c, mntflags, set_gid, set_uid, set_mask,error;
69 	int forcerw = 0;
70 	char *dev, *dir, ndir[MAXPATHLEN+1];
71 	struct vfsconf vfc;
72 
73 	mntflags = set_gid = set_uid = set_mask = 0;
74 	memset(&args, '\0', sizeof(args));
75 
76 	while ((c = getopt(argc, argv, "u:g:m:o:c:W:F")) !=  -1) {
77 		switch (c) {
78 		case 'F':
79 			forcerw=1;
80 			break;
81 		case 'u':
82 			args.uid = a_uid(optarg);
83 			set_uid = 1;
84 			break;
85 		case 'g':
86 			args.gid = a_gid(optarg);
87 			set_gid = 1;
88 			break;
89 		case 'm':
90 			args.mode = a_mask(optarg);
91 			set_mask = 1;
92 			break;
93 		case 'o':
94 			getmntopts(optarg, mopts, &mntflags, 0);
95 			break;
96 #if 0
97 		case 'W':
98 			load_u2wtable(&args, optarg);
99 			args.flags |= HPFSMNT_TABLES;
100 			break;
101 #endif
102 		case '?':
103 		default:
104 			usage();
105 			break;
106 		}
107 	}
108 
109 	if (optind + 2 != argc)
110 		usage();
111 
112 	if (!(mntflags & MNT_RDONLY) && !forcerw) {
113 		warnx("Write support is BETA, you need -F flag to enable RW mount!");
114 		exit (111);
115 	}
116 
117 	dev = argv[optind];
118 	dir = argv[optind + 1];
119 	if (dir[0] != '/') {
120 		warnx("\"%s\" is a relative path", dir);
121 		if (getcwd(ndir, sizeof(ndir)) == NULL)
122 			err(EX_OSERR, "getcwd");
123 		strncat(ndir, "/", sizeof(ndir) - strlen(ndir) - 1);
124 		strncat(ndir, dir, sizeof(ndir) - strlen(ndir) - 1);
125 		dir = ndir;
126 		warnx("using \"%s\" instead", dir);
127 	}
128 
129 	args.fspec = dev;
130 	args.export.ex_root = 65534;	/* unchecked anyway on DOS fs */
131 	if (mntflags & MNT_RDONLY)
132 		args.export.ex_flags = MNT_EXRDONLY;
133 	else
134 		args.export.ex_flags = 0;
135 
136 	if (!set_gid || !set_uid || !set_mask) {
137 		if (stat(dir, &sb) == -1)
138 			err(EX_OSERR, "stat %s", dir);
139 
140 		if (!set_uid)
141 			args.uid = sb.st_uid;
142 		if (!set_gid)
143 			args.gid = sb.st_gid;
144 		if (!set_mask)
145 			args.mode = sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
146 	}
147 
148 	error = getvfsbyname("hpfs", &vfc);
149 	if(error && vfsisloadable("hpfs")) {
150 		if(vfsload("hpfs"))
151 			err(EX_OSERR, "vfsload(hpfs)");
152 		endvfsent();	/* clear cache */
153 		error = getvfsbyname("hpfs", &vfc);
154 	}
155 	if (error)
156 		errx(EX_OSERR, "hpfs filesystem is not available");
157 
158 	if (mount(vfc.vfc_name, dir, mntflags, &args) < 0)
159 		err(EX_OSERR, "%s", dev);
160 
161 	exit (0);
162 }
163 
164 gid_t
165 a_gid(char *s)
166 {
167 	struct group *gr;
168 	char *gname;
169 	gid_t gid;
170 
171 	if ((gr = getgrnam(s)) != NULL)
172 		gid = gr->gr_gid;
173 	else {
174 		for (gname = s; *s && isdigit(*s); ++s);
175 		if (!*s)
176 			gid = atoi(gname);
177 		else
178 			errx(EX_NOUSER, "unknown group id: %s", gname);
179 	}
180 	return (gid);
181 }
182 
183 uid_t
184 a_uid(char *s)
185 {
186 	struct passwd *pw;
187 	char *uname;
188 	uid_t uid;
189 
190 	if ((pw = getpwnam(s)) != NULL)
191 		uid = pw->pw_uid;
192 	else {
193 		for (uname = s; *s && isdigit(*s); ++s);
194 		if (!*s)
195 			uid = atoi(uname);
196 		else
197 			errx(EX_NOUSER, "unknown user id: %s", uname);
198 	}
199 	return (uid);
200 }
201 
202 mode_t
203 a_mask(char *s)
204 {
205 	int done, rv=0;
206 	char *ep;
207 
208 	done = 0;
209 	if (*s >= '0' && *s <= '7') {
210 		done = 1;
211 		rv = strtol(optarg, &ep, 8);
212 	}
213 	if (!done || rv < 0 || *ep)
214 		errx(EX_USAGE, "invalid file mode: %s", s);
215 	return (rv);
216 }
217 
218 void
219 usage(void)
220 {
221 	fprintf(stderr, "usage: mount_hpfs [-u user] [-g group] [-m mask] bdev dir\n");
222 	exit(EX_USAGE);
223 }
224 
225 #if 0
226 void
227 load_u2wtable (struct hpfs_args *pargs, char *name)
228 {
229 	FILE *f;
230 	int i, code;
231 	char buf[128];
232 	char *fn;
233 
234 	if (*name == '/')
235 		fn = name;
236 	else {
237 		snprintf(buf, sizeof(buf), "/usr/libdata/msdosfs/%s", name);
238 		buf[127] = '\0';
239 		fn = buf;
240 	}
241 	if ((f = fopen(fn, "r")) == NULL)
242 		err(EX_NOINPUT, "%s", fn);
243 	for (i = 0; i < 128; i++) {
244 		if (fscanf(f, "%i", &code) != 1)
245 			errx(EX_DATAERR, "u2w: missing item number %d", i);
246 		/* pargs->u2w[i] = code; */
247 	}
248 	for (i = 0; i < 128; i++) {
249 		if (fscanf(f, "%i", &code) != 1)
250 			errx(EX_DATAERR, "d2u: missing item number %d", i);
251 		pargs->d2u[i] = code;
252 	}
253 	for (i = 0; i < 128; i++) {
254 		if (fscanf(f, "%i", &code) != 1)
255 			errx(EX_DATAERR, "u2d: missing item number %d", i);
256 		pargs->u2d[i] = code;
257 	}
258 	fclose(f);
259 }
260 #endif
261