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