1 /* $NetBSD: mount_cd9660.c,v 1.35 2024/02/03 15:47:44 wiz Exp $ */
2
3 /*
4 * Copyright (c) 1992, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley
8 * by Pace Willisson (pace@blitz.com). The Rock Ridge Extension
9 * Support code is derived from software contributed to Berkeley
10 * by Atsushi Murai (amurai@spec.co.jp).
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * @(#)mount_cd9660.c 8.7 (Berkeley) 5/1/95
37 */
38
39 #include <sys/cdefs.h>
40 #ifndef lint
41 __COPYRIGHT("@(#) Copyright (c) 1992, 1993, 1994\
42 The Regents of the University of California. All rights reserved.");
43 #endif /* not lint */
44
45 #ifndef lint
46 #if 0
47 static char sccsid[] = "@(#)mount_cd9660.c 8.7 (Berkeley) 5/1/95";
48 #else
49 __RCSID("$NetBSD: mount_cd9660.c,v 1.35 2024/02/03 15:47:44 wiz Exp $");
50 #endif
51 #endif /* not lint */
52
53 #include <sys/param.h>
54 #include <sys/mount.h>
55
56 #include <err.h>
57 #include <stdlib.h>
58 #include <stdio.h>
59 #include <string.h>
60 #include <unistd.h>
61 #include <util.h>
62
63 #include <isofs/cd9660/cd9660_mount.h>
64
65 #include <mntopts.h>
66
67 #include "mountprog.h"
68 #include "mount_cd9660.h"
69
70 static const struct mntopt mopts[] = {
71 MOPT_STDOPTS,
72 MOPT_UPDATE,
73 MOPT_GETARGS,
74 { "extatt", 0, ISOFSMNT_EXTATT, 1 },
75 { "gens", 0, ISOFSMNT_GENS, 1 },
76 { "maplcase", 1, ISOFSMNT_NOCASETRANS, 1 },
77 { "casetrans", 1, ISOFSMNT_NOCASETRANS, 1 },
78 { "nrr", 0, ISOFSMNT_NORRIP, 1 },
79 { "rrip", 1, ISOFSMNT_NORRIP, 1 },
80 { "joliet", 1, ISOFSMNT_NOJOLIET, 1 },
81 { "rrcaseins", 0, ISOFSMNT_RRCASEINS, 1 },
82 MOPT_NULL,
83 };
84
85 __dead static void usage(void);
86
87 #ifndef MOUNT_NOMAIN
88 int
main(int argc,char ** argv)89 main(int argc, char **argv)
90 {
91
92 setprogname(argv[0]);
93 return mount_cd9660(argc, argv);
94 }
95 #endif
96
97 void
mount_cd9660_parseargs(int argc,char ** argv,struct iso_args * args,int * mntflags,char * canon_dev,char * canon_dir)98 mount_cd9660_parseargs(int argc, char **argv,
99 struct iso_args *args, int *mntflags,
100 char *canon_dev, char *canon_dir)
101 {
102 struct stat sb;
103 int ch, opts, set_gid, set_uid, set_mask, set_dirmask;
104 mntoptparse_t mp;
105 char *dev, *dir;
106
107 memset(args, 0, sizeof(*args));
108 *mntflags = opts = set_gid = set_uid = set_mask = set_dirmask = 0;
109 optind = optreset = 1;
110 while ((ch = getopt(argc, argv, "egG:jM:m:o:rU:")) != -1)
111 switch (ch) {
112 case 'e':
113 /* obsolete, retained for compatibility only, use
114 * -o extatt */
115 opts |= ISOFSMNT_EXTATT;
116 break;
117 case 'g':
118 /* obsolete, retained for compatibility only, use
119 * -o gens */
120 opts |= ISOFSMNT_GENS;
121 break;
122 case 'G':
123 opts |= ISOFSMNT_GID;
124 args->gid = a_gid(optarg);
125 set_gid = 1;
126 break;
127 case 'm':
128 args->fmask = a_mask(optarg);
129 set_mask = 1;
130 break;
131 case 'M':
132 args->dmask = a_mask(optarg);
133 set_dirmask = 1;
134 break;
135 case 'j':
136 /* obsolete, retained fo compatibility only, use
137 * -o nojoliet */
138 opts |= ISOFSMNT_NOJOLIET;
139 break;
140 case 'o':
141 mp = getmntopts(optarg, mopts, mntflags, &opts);
142 if (mp == NULL)
143 err(1, "getmntopts");
144 freemntopts(mp);
145 break;
146 case 'r':
147 /* obsolete, retained for compatibility only, use
148 * -o norrip */
149 opts |= ISOFSMNT_NORRIP;
150 break;
151 case 'U':
152 opts |= ISOFSMNT_UID;
153 args->uid = a_uid(optarg);
154 set_uid = 1;
155 break;
156 case '?':
157 default:
158 usage();
159 }
160
161 argc -= optind;
162 argv += optind;
163
164 if (argc != 2)
165 usage();
166
167 if (set_mask && !set_dirmask) {
168 args->dmask = args->fmask;
169 set_dirmask = 1;
170 } else if (set_dirmask && !set_mask) {
171 args->fmask = args->dmask;
172 set_mask = 1;
173 }
174
175 dev = argv[0];
176 dir = argv[1];
177
178 pathadj(dev, canon_dev);
179 pathadj(dir, canon_dir);
180
181 #define DEFAULT_ROOTUID -2
182 /*
183 * ISO 9660 filesystems are not writable.
184 */
185 if ((*mntflags & MNT_GETARGS) == 0)
186 *mntflags |= MNT_RDONLY;
187 args->fspec = canon_dev;
188 args->flags = opts;
189
190 if (!set_gid || !set_uid || !set_mask) {
191 if (stat(dir, &sb) == -1)
192 err(1, "stat %s", dir);
193
194 if (!set_uid)
195 args->uid = sb.st_uid;
196 if (!set_gid)
197 args->gid = sb.st_gid;
198 if (!set_mask) {
199 args->fmask = args->dmask =
200 sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
201 }
202 }
203 }
204
205 int
mount_cd9660(int argc,char ** argv)206 mount_cd9660(int argc, char **argv)
207 {
208 struct iso_args args;
209 char canon_dev[MAXPATHLEN], canon_dir[MAXPATHLEN];
210 int mntflags;
211
212 mount_cd9660_parseargs(argc, argv, &args, &mntflags,
213 canon_dev, canon_dir);
214
215 if (mount(MOUNT_CD9660, canon_dir, mntflags, &args, sizeof args) == -1)
216 err(1, "%s on %s", canon_dev, canon_dir);
217 if (mntflags & MNT_GETARGS) {
218 char buf[2048];
219 (void)snprintb(buf, sizeof(buf), ISOFSMNT_BITS, args.flags);
220 printf("%s\n", buf);
221 }
222
223 exit(0);
224 /* NOTREACHED */
225 }
226
227 static void
usage(void)228 usage(void)
229 {
230 (void)fprintf(stderr,
231 "usage: %s [-G gid] [-M mask] [-m mask] [-o options] [-U uid] special node\n", getprogname());
232 exit(1);
233 }
234