mount_msdos.c revision 1.10 1 1.10 cgd /* $NetBSD: mount_msdos.c,v 1.10 1995/03/18 14:57:38 cgd Exp $ */
2 1.10 cgd
3 1.1 cgd /*
4 1.5 cgd * Copyright (c) 1994 Christopher G. Demetriou
5 1.5 cgd * All rights reserved.
6 1.5 cgd *
7 1.5 cgd * Redistribution and use in source and binary forms, with or without
8 1.5 cgd * modification, are permitted provided that the following conditions
9 1.5 cgd * are met:
10 1.5 cgd * 1. Redistributions of source code must retain the above copyright
11 1.5 cgd * notice, this list of conditions and the following disclaimer.
12 1.5 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.5 cgd * notice, this list of conditions and the following disclaimer in the
14 1.5 cgd * documentation and/or other materials provided with the distribution.
15 1.5 cgd * 3. All advertising materials mentioning features or use of this software
16 1.5 cgd * must display the following acknowledgement:
17 1.5 cgd * This product includes software developed by Christopher G. Demetriou.
18 1.5 cgd * 4. The name of the author may not be used to endorse or promote products
19 1.5 cgd * derived from this software without specific prior written permission
20 1.5 cgd *
21 1.5 cgd * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 1.5 cgd * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 1.5 cgd * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 1.5 cgd * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 1.5 cgd * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 1.5 cgd * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 1.5 cgd * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 1.5 cgd * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 1.5 cgd * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 1.5 cgd * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 1.1 cgd */
32 1.2 mycroft
33 1.2 mycroft #ifndef lint
34 1.10 cgd static char rcsid[] = "$NetBSD: mount_msdos.c,v 1.10 1995/03/18 14:57:38 cgd Exp $";
35 1.2 mycroft #endif /* not lint */
36 1.2 mycroft
37 1.6 cgd #include <sys/cdefs.h>
38 1.7 cgd #include <sys/param.h>
39 1.1 cgd #include <sys/mount.h>
40 1.6 cgd #include <sys/stat.h>
41 1.6 cgd #include <ctype.h>
42 1.6 cgd #include <err.h>
43 1.6 cgd #include <grp.h>
44 1.6 cgd #include <pwd.h>
45 1.5 cgd #include <stdio.h>
46 1.5 cgd #include <stdlib.h>
47 1.5 cgd #include <string.h>
48 1.7 cgd #include <unistd.h>
49 1.1 cgd
50 1.8 cgd #include "mntopts.h"
51 1.8 cgd
52 1.8 cgd struct mntopt mopts[] = {
53 1.8 cgd MOPT_STDOPTS,
54 1.8 cgd { NULL }
55 1.8 cgd };
56 1.8 cgd
57 1.6 cgd gid_t a_gid __P((char *));
58 1.6 cgd uid_t a_uid __P((char *));
59 1.6 cgd mode_t a_mask __P((char *));
60 1.6 cgd void usage __P((void));
61 1.8 cgd
62 1.1 cgd int
63 1.6 cgd main(argc, argv)
64 1.6 cgd int argc;
65 1.6 cgd char **argv;
66 1.1 cgd {
67 1.3 mycroft struct msdosfs_args args;
68 1.6 cgd struct stat sb;
69 1.8 cgd int c, mntflags, set_gid, set_uid, set_mask;
70 1.7 cgd char *dev, *dir, ndir[MAXPATHLEN+1];
71 1.1 cgd
72 1.8 cgd mntflags = set_gid = set_uid = set_mask = 0;
73 1.6 cgd (void)memset(&args, '\0', sizeof(args));
74 1.1 cgd
75 1.8 cgd while ((c = getopt(argc, argv, "u:g:m:o:")) != EOF) {
76 1.1 cgd switch (c) {
77 1.6 cgd case 'u':
78 1.6 cgd args.uid = a_uid(optarg);
79 1.6 cgd set_uid = 1;
80 1.6 cgd break;
81 1.6 cgd case 'g':
82 1.6 cgd args.gid = a_gid(optarg);
83 1.6 cgd set_gid = 1;
84 1.1 cgd break;
85 1.6 cgd case 'm':
86 1.6 cgd args.mask = a_mask(optarg);
87 1.6 cgd set_mask = 1;
88 1.6 cgd break;
89 1.8 cgd case 'o':
90 1.8 cgd getmntopts(optarg, mopts, &mntflags);
91 1.8 cgd break;
92 1.6 cgd case '?':
93 1.1 cgd default:
94 1.6 cgd usage();
95 1.6 cgd break;
96 1.1 cgd }
97 1.1 cgd }
98 1.1 cgd
99 1.1 cgd if (optind + 2 != argc)
100 1.6 cgd usage();
101 1.1 cgd
102 1.1 cgd dev = argv[optind];
103 1.1 cgd dir = argv[optind + 1];
104 1.7 cgd if (dir[0] != '/') {
105 1.7 cgd warnx("\"%s\" is a relative path.", dir);
106 1.7 cgd if (getcwd(ndir, sizeof(ndir)) == NULL)
107 1.7 cgd err(1, "getcwd");
108 1.7 cgd strncat(ndir, "/", sizeof(ndir) - strlen(ndir) - 1);
109 1.7 cgd strncat(ndir, dir, sizeof(ndir) - strlen(ndir) - 1);
110 1.7 cgd dir = ndir;
111 1.7 cgd warnx("using \"%s\" instead.", dir);
112 1.7 cgd }
113 1.1 cgd
114 1.1 cgd args.fspec = dev;
115 1.8 cgd args.export.ex_root = -2; /* unchecked anyway on DOS fs */
116 1.8 cgd if (mntflags & MNT_RDONLY)
117 1.8 cgd args.export.ex_flags = MNT_EXRDONLY;
118 1.8 cgd else
119 1.8 cgd args.export.ex_flags = 0;
120 1.6 cgd if (!set_gid || !set_uid || !set_mask) {
121 1.6 cgd if (stat(dir, &sb) == -1)
122 1.6 cgd err(1, "stat %s", dir);
123 1.6 cgd
124 1.6 cgd if (!set_uid)
125 1.6 cgd args.uid = sb.st_uid;
126 1.6 cgd if (!set_gid)
127 1.6 cgd args.gid = sb.st_gid;
128 1.6 cgd if (!set_mask)
129 1.6 cgd args.mask = sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
130 1.6 cgd }
131 1.6 cgd
132 1.8 cgd if (mount(MOUNT_MSDOS, dir, mntflags, &args) < 0)
133 1.6 cgd err(1, "mount");
134 1.6 cgd
135 1.6 cgd exit (0);
136 1.6 cgd }
137 1.6 cgd
138 1.6 cgd gid_t
139 1.6 cgd a_gid(s)
140 1.6 cgd char *s;
141 1.6 cgd {
142 1.6 cgd struct group *gr;
143 1.6 cgd char *gname;
144 1.6 cgd gid_t gid;
145 1.6 cgd
146 1.6 cgd if ((gr = getgrnam(s)) != NULL)
147 1.6 cgd gid = gr->gr_gid;
148 1.6 cgd else {
149 1.6 cgd for (gname = s; *s && isdigit(*s); ++s);
150 1.6 cgd if (!*s)
151 1.6 cgd gid = atoi(gname);
152 1.6 cgd else
153 1.6 cgd errx(1, "unknown group id: %s", gname);
154 1.6 cgd }
155 1.6 cgd return (gid);
156 1.6 cgd }
157 1.1 cgd
158 1.6 cgd uid_t
159 1.6 cgd a_uid(s)
160 1.6 cgd char *s;
161 1.6 cgd {
162 1.6 cgd struct passwd *pw;
163 1.6 cgd char *uname;
164 1.6 cgd uid_t uid;
165 1.6 cgd
166 1.6 cgd if ((pw = getpwnam(s)) != NULL)
167 1.6 cgd uid = pw->pw_uid;
168 1.6 cgd else {
169 1.6 cgd for (uname = s; *s && isdigit(*s); ++s);
170 1.6 cgd if (!*s)
171 1.6 cgd uid = atoi(uname);
172 1.6 cgd else
173 1.6 cgd errx(1, "unknown user id: %s", uname);
174 1.1 cgd }
175 1.6 cgd return (uid);
176 1.6 cgd }
177 1.1 cgd
178 1.6 cgd mode_t
179 1.6 cgd a_mask(s)
180 1.6 cgd char *s;
181 1.6 cgd {
182 1.6 cgd int done, rv;
183 1.6 cgd char *ep;
184 1.6 cgd
185 1.6 cgd done = 0;
186 1.6 cgd if (*s >= '0' && *s <= '7') {
187 1.6 cgd done = 1;
188 1.6 cgd rv = strtol(optarg, &ep, 8);
189 1.6 cgd }
190 1.6 cgd if (!done || rv < 0 || *ep)
191 1.6 cgd errx(1, "invalid file mode: %s", s);
192 1.6 cgd return (rv);
193 1.6 cgd }
194 1.6 cgd
195 1.6 cgd void
196 1.6 cgd usage()
197 1.6 cgd {
198 1.9 mycroft
199 1.9 mycroft fprintf(stderr, "usage: mount_msdos [-o options] [-u user] [-g group] [-m mask] bdev dir\n");
200 1.6 cgd exit(1);
201 1.1 cgd }
202