mount_ados.c revision 1.19 1 1.19 xtraeme /* $NetBSD: mount_ados.c,v 1.19 2005/02/05 14:47:18 xtraeme Exp $ */
2 1.4 cgd
3 1.1 chopps /*
4 1.1 chopps * Copyright (c) 1994 Christopher G. Demetriou
5 1.1 chopps * All rights reserved.
6 1.11 cgd *
7 1.1 chopps * Redistribution and use in source and binary forms, with or without
8 1.1 chopps * modification, are permitted provided that the following conditions
9 1.1 chopps * are met:
10 1.1 chopps * 1. Redistributions of source code must retain the above copyright
11 1.1 chopps * notice, this list of conditions and the following disclaimer.
12 1.1 chopps * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 chopps * notice, this list of conditions and the following disclaimer in the
14 1.1 chopps * documentation and/or other materials provided with the distribution.
15 1.1 chopps * 3. All advertising materials mentioning features or use of this software
16 1.1 chopps * must display the following acknowledgement:
17 1.11 cgd * This product includes software developed for the
18 1.15 grant * NetBSD Project. See http://www.NetBSD.org/ for
19 1.11 cgd * information about NetBSD.
20 1.1 chopps * 4. The name of the author may not be used to endorse or promote products
21 1.11 cgd * derived from this software without specific prior written permission.
22 1.11 cgd *
23 1.1 chopps * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 1.1 chopps * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 1.1 chopps * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 1.1 chopps * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 1.1 chopps * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 1.1 chopps * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 1.1 chopps * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 1.1 chopps * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 1.1 chopps * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 1.1 chopps * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 1.11 cgd *
34 1.11 cgd * <<Id: LICENSE,v 1.2 2000/06/14 15:57:33 cgd Exp>>
35 1.1 chopps */
36 1.1 chopps
37 1.6 lukem #include <sys/cdefs.h>
38 1.1 chopps #ifndef lint
39 1.19 xtraeme __RCSID("$NetBSD: mount_ados.c,v 1.19 2005/02/05 14:47:18 xtraeme Exp $");
40 1.1 chopps #endif /* not lint */
41 1.1 chopps
42 1.1 chopps #include <sys/cdefs.h>
43 1.1 chopps #include <sys/param.h>
44 1.1 chopps #include <sys/mount.h>
45 1.1 chopps #include <sys/stat.h>
46 1.1 chopps #include <ctype.h>
47 1.1 chopps #include <err.h>
48 1.1 chopps #include <grp.h>
49 1.1 chopps #include <pwd.h>
50 1.1 chopps #include <stdio.h>
51 1.1 chopps #include <stdlib.h>
52 1.1 chopps #include <string.h>
53 1.1 chopps #include <unistd.h>
54 1.8 fvdl
55 1.8 fvdl #include <adosfs/adosfs.h>
56 1.1 chopps
57 1.16 jdolecek #include <mntopts.h>
58 1.13 is #include <errno.h>
59 1.12 jdolecek #include <fattr.h>
60 1.3 mycroft
61 1.12 jdolecek static const struct mntopt mopts[] = {
62 1.3 mycroft MOPT_STDOPTS,
63 1.14 christos MOPT_GETARGS,
64 1.3 mycroft { NULL }
65 1.3 mycroft };
66 1.3 mycroft
67 1.19 xtraeme int mount_ados(int argc, char **argv);
68 1.19 xtraeme static void usage(void);
69 1.3 mycroft
70 1.12 jdolecek #ifndef MOUNT_NOMAIN
71 1.1 chopps int
72 1.12 jdolecek main(int argc, char **argv)
73 1.12 jdolecek {
74 1.12 jdolecek return mount_ados(argc, argv);
75 1.12 jdolecek }
76 1.12 jdolecek #endif
77 1.12 jdolecek
78 1.12 jdolecek int
79 1.19 xtraeme mount_ados(int argc, char **argv)
80 1.1 chopps {
81 1.1 chopps struct adosfs_args args;
82 1.1 chopps struct stat sb;
83 1.3 mycroft int c, mntflags, set_gid, set_uid, set_mask;
84 1.18 erh char *dev, *dir, canon_dir[MAXPATHLEN], canon_dev[MAXPATHLEN];
85 1.1 chopps
86 1.3 mycroft mntflags = set_gid = set_uid = set_mask = 0;
87 1.1 chopps (void)memset(&args, '\0', sizeof(args));
88 1.1 chopps
89 1.6 lukem while ((c = getopt(argc, argv, "u:g:m:o:")) != -1) {
90 1.1 chopps switch (c) {
91 1.1 chopps case 'u':
92 1.1 chopps args.uid = a_uid(optarg);
93 1.1 chopps set_uid = 1;
94 1.1 chopps break;
95 1.1 chopps case 'g':
96 1.1 chopps args.gid = a_gid(optarg);
97 1.1 chopps set_gid = 1;
98 1.1 chopps break;
99 1.1 chopps case 'm':
100 1.1 chopps args.mask = a_mask(optarg);
101 1.1 chopps set_mask = 1;
102 1.1 chopps break;
103 1.3 mycroft case 'o':
104 1.7 lukem getmntopts(optarg, mopts, &mntflags, 0);
105 1.3 mycroft break;
106 1.1 chopps case '?':
107 1.1 chopps default:
108 1.1 chopps usage();
109 1.1 chopps break;
110 1.1 chopps }
111 1.1 chopps }
112 1.1 chopps
113 1.1 chopps if (optind + 2 != argc)
114 1.1 chopps usage();
115 1.1 chopps
116 1.1 chopps dev = argv[optind];
117 1.1 chopps dir = argv[optind + 1];
118 1.18 erh
119 1.18 erh if (realpath(dev, canon_dev) == NULL) /* Check device path */
120 1.18 erh err(1, "realpath %s", dev);
121 1.18 erh if (strncmp(dev, canon_dev, MAXPATHLEN)) {
122 1.18 erh warnx("\"%s\" is a relative path.", dev);
123 1.18 erh dev = canon_dev;
124 1.18 erh warnx("using \"%s\" instead.", dev);
125 1.18 erh }
126 1.18 erh
127 1.18 erh if (realpath(dir, canon_dir) == NULL) /* Check mounton path */
128 1.18 erh err(1, "realpath %s", dir);
129 1.18 erh if (strncmp(dir, canon_dir, MAXPATHLEN)) {
130 1.1 chopps warnx("\"%s\" is a relative path.", dir);
131 1.18 erh dir = canon_dir;
132 1.1 chopps warnx("using \"%s\" instead.", dir);
133 1.1 chopps }
134 1.1 chopps
135 1.1 chopps args.fspec = dev;
136 1.3 mycroft args.export.ex_root = -2; /* unchecked anyway on DOS fs */
137 1.3 mycroft if (mntflags & MNT_RDONLY)
138 1.3 mycroft args.export.ex_flags = MNT_EXRDONLY;
139 1.3 mycroft else
140 1.3 mycroft args.export.ex_flags = 0;
141 1.1 chopps if (!set_gid || !set_uid || !set_mask) {
142 1.1 chopps if (stat(dir, &sb) == -1)
143 1.1 chopps err(1, "stat %s", dir);
144 1.1 chopps
145 1.1 chopps if (!set_uid)
146 1.1 chopps args.uid = sb.st_uid;
147 1.1 chopps if (!set_gid)
148 1.1 chopps args.gid = sb.st_gid;
149 1.1 chopps if (!set_mask)
150 1.1 chopps args.mask = sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
151 1.1 chopps }
152 1.1 chopps
153 1.13 is if (mount(MOUNT_ADOSFS, dir, mntflags, &args) >= 0)
154 1.13 is exit (0);
155 1.13 is
156 1.13 is if (errno != EROFS)
157 1.9 perseant err(1, "%s on %s", dev, dir);
158 1.1 chopps
159 1.13 is mntflags |= MNT_RDONLY;
160 1.13 is args.export.ex_flags = MNT_EXRDONLY;
161 1.13 is
162 1.14 christos if (mount(MOUNT_ADOSFS, dir, mntflags, &args) == -1)
163 1.14 christos err(1, "%s on %s", dev, dir);
164 1.14 christos
165 1.14 christos if (mntflags & MNT_GETARGS)
166 1.17 christos printf("uid=%d, gid=%d, mask=0%o\n", args.uid, args.gid,
167 1.14 christos args.mask);
168 1.13 is
169 1.14 christos exit (0);
170 1.1 chopps }
171 1.1 chopps
172 1.12 jdolecek static void
173 1.19 xtraeme usage(void)
174 1.1 chopps {
175 1.2 chopps
176 1.3 mycroft fprintf(stderr, "usage: mount_ados [-o options] [-u user] [-g group] [-m mask] bdev dir\n");
177 1.2 chopps exit(1);
178 1.2 chopps }
179