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