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