xref: /dragonfly/sbin/mount_hpfs/mount_hpfs.c (revision 678e8cc6)
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  * $DragonFly: src/sbin/mount_hpfs/mount_hpfs.c,v 1.8 2005/04/03 18:59:24 joerg Exp $
33  */
34 
35 #include <sys/cdefs.h>
36 #include <sys/param.h>
37 #include <sys/mount.h>
38 #include <sys/stat.h>
39 #include <vfs/hpfs/hpfsmount.h>
40 #include <ctype.h>
41 #include <err.h>
42 #include <grp.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 #include "mntopts.h"
51 
52 static struct mntopt mopts[] = {
53 	MOPT_STDOPTS,
54 	MOPT_NULL
55 };
56 
57 static gid_t	a_gid(char *);
58 static uid_t	a_uid(char *);
59 static mode_t	a_mask(char *);
60 static void	usage(void) __dead2;
61 #if 0
62 static void	load_u2wtable(struct hpfs_args *, char *);
63 #endif
64 
65 int
66 main(int argc, char **argv)
67 {
68 	struct hpfs_args args;
69 	struct stat sb;
70 	int c, mntflags, set_gid, set_uid, set_mask,error;
71 	int forcerw = 0;
72 	char *dev, *dir, ndir[MAXPATHLEN+1];
73 	struct vfsconf vfc;
74 
75 	mntflags = set_gid = set_uid = set_mask = 0;
76 	memset(&args, '\0', sizeof(args));
77 
78 	while ((c = getopt(argc, argv, "u:g:m:o:c:W:F")) !=  -1) {
79 		switch (c) {
80 		case 'F':
81 			forcerw=1;
82 			break;
83 		case 'u':
84 			args.uid = a_uid(optarg);
85 			set_uid = 1;
86 			break;
87 		case 'g':
88 			args.gid = a_gid(optarg);
89 			set_gid = 1;
90 			break;
91 		case 'm':
92 			args.mode = a_mask(optarg);
93 			set_mask = 1;
94 			break;
95 		case 'o':
96 			getmntopts(optarg, mopts, &mntflags, 0);
97 			break;
98 #if 0
99 		case 'W':
100 			load_u2wtable(&args, optarg);
101 			args.flags |= HPFSMNT_TABLES;
102 			break;
103 #endif
104 		case '?':
105 		default:
106 			usage();
107 			break;
108 		}
109 	}
110 
111 	if (optind + 2 != argc)
112 		usage();
113 
114 	if (!(mntflags & MNT_RDONLY) && !forcerw) {
115 		warnx("Write support is BETA, you need -F flag to enable RW mount!");
116 		exit (111);
117 	}
118 
119 	dev = argv[optind];
120 	dir = argv[optind + 1];
121 	if (dir[0] != '/') {
122 		warnx("\"%s\" is a relative path", dir);
123 		if (getcwd(ndir, sizeof(ndir)) == NULL)
124 			err(EX_OSERR, "getcwd");
125 		strncat(ndir, "/", sizeof(ndir) - strlen(ndir) - 1);
126 		strncat(ndir, dir, sizeof(ndir) - strlen(ndir) - 1);
127 		dir = ndir;
128 		warnx("using \"%s\" instead", dir);
129 	}
130 
131 	args.fspec = dev;
132 	args.export.ex_root = 65534;	/* unchecked anyway on DOS fs */
133 	if (mntflags & MNT_RDONLY)
134 		args.export.ex_flags = MNT_EXRDONLY;
135 	else
136 		args.export.ex_flags = 0;
137 
138 	if (!set_gid || !set_uid || !set_mask) {
139 		if (stat(dir, &sb) == -1)
140 			err(EX_OSERR, "stat %s", dir);
141 
142 		if (!set_uid)
143 			args.uid = sb.st_uid;
144 		if (!set_gid)
145 			args.gid = sb.st_gid;
146 		if (!set_mask)
147 			args.mode = sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
148 	}
149 
150 	error = getvfsbyname("hpfs", &vfc);
151 	if(error && vfsisloadable("hpfs")) {
152 		if(vfsload("hpfs"))
153 			err(EX_OSERR, "vfsload(hpfs)");
154 		endvfsent();	/* clear cache */
155 		error = getvfsbyname("hpfs", &vfc);
156 	}
157 	if (error)
158 		errx(EX_OSERR, "hpfs filesystem is not available");
159 
160 	if (mount(vfc.vfc_name, dir, mntflags, &args) < 0)
161 		err(EX_OSERR, "%s", dev);
162 
163 	exit (0);
164 }
165 
166 gid_t
167 a_gid(char *s)
168 {
169 	struct group *gr;
170 	char *gname;
171 	gid_t gid;
172 
173 	if ((gr = getgrnam(s)) != NULL)
174 		gid = gr->gr_gid;
175 	else {
176 		for (gname = s; *s && isdigit(*s); ++s);
177 		if (!*s)
178 			gid = atoi(gname);
179 		else
180 			errx(EX_NOUSER, "unknown group id: %s", gname);
181 	}
182 	return (gid);
183 }
184 
185 uid_t
186 a_uid(char *s)
187 {
188 	struct passwd *pw;
189 	char *uname;
190 	uid_t uid;
191 
192 	if ((pw = getpwnam(s)) != NULL)
193 		uid = pw->pw_uid;
194 	else {
195 		for (uname = s; *s && isdigit(*s); ++s);
196 		if (!*s)
197 			uid = atoi(uname);
198 		else
199 			errx(EX_NOUSER, "unknown user id: %s", uname);
200 	}
201 	return (uid);
202 }
203 
204 mode_t
205 a_mask(char *s)
206 {
207 	int done, rv=0;
208 	char *ep;
209 
210 	done = 0;
211 	if (*s >= '0' && *s <= '7') {
212 		done = 1;
213 		rv = strtol(optarg, &ep, 8);
214 	}
215 	if (!done || rv < 0 || *ep)
216 		errx(EX_USAGE, "invalid file mode: %s", s);
217 	return (rv);
218 }
219 
220 void
221 usage(void)
222 {
223 	fprintf(stderr, "usage: mount_hpfs [-u user] [-g group] [-m mask] bdev dir\n");
224 	exit(EX_USAGE);
225 }
226 
227 #if 0
228 void
229 load_u2wtable (struct hpfs_args *pargs, char *name)
230 {
231 	FILE *f;
232 	int i, code;
233 	char buf[128];
234 	char *fn;
235 
236 	if (*name == '/')
237 		fn = name;
238 	else {
239 		snprintf(buf, sizeof(buf), "/usr/libdata/msdosfs/%s", name);
240 		buf[127] = '\0';
241 		fn = buf;
242 	}
243 	if ((f = fopen(fn, "r")) == NULL)
244 		err(EX_NOINPUT, "%s", fn);
245 	for (i = 0; i < 128; i++) {
246 		if (fscanf(f, "%i", &code) != 1)
247 			errx(EX_DATAERR, "u2w: missing item number %d", i);
248 		/* pargs->u2w[i] = code; */
249 	}
250 	for (i = 0; i < 128; i++) {
251 		if (fscanf(f, "%i", &code) != 1)
252 			errx(EX_DATAERR, "d2u: missing item number %d", i);
253 		pargs->d2u[i] = code;
254 	}
255 	for (i = 0; i < 128; i++) {
256 		if (fscanf(f, "%i", &code) != 1)
257 			errx(EX_DATAERR, "u2d: missing item number %d", i);
258 		pargs->u2d[i] = code;
259 	}
260 	fclose(f);
261 }
262 #endif
263