1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
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
37 #include <sys/cdio.h>
38 #include <sys/file.h>
39 #include <sys/param.h>
40 #include <sys/mount.h>
41 #include <sys/module.h>
42 #include <sys/iconv.h>
43 #include <sys/linker.h>
44
45 #include <arpa/inet.h>
46
47 #include <ctype.h>
48 #include <err.h>
49 #include <errno.h>
50 #include <grp.h>
51 #include <mntopts.h>
52 #include <pwd.h>
53 #include <stdlib.h>
54 #include <stdio.h>
55 #include <string.h>
56 #include <sysexits.h>
57 #include <unistd.h>
58
59 static struct mntopt mopts[] = {
60 MOPT_STDOPTS,
61 MOPT_UPDATE,
62 MOPT_END
63 };
64
65 static gid_t a_gid(const char *);
66 static uid_t a_uid(const char *);
67 static mode_t a_mask(const char *);
68 static int get_ssector(const char *dev);
69 static int set_charset(struct iovec **, int *iovlen, const char *);
70 void usage(void);
71
72 int
main(int argc,char ** argv)73 main(int argc, char **argv)
74 {
75 struct iovec *iov;
76 int iovlen;
77 int ch, mntflags;
78 char *dev, *dir, *p, *val, mntpath[MAXPATHLEN];
79 int verbose;
80 int ssector; /* starting sector, 0 for 1st session */
81 char fstype[] = "cd9660";
82
83 iov = NULL;
84 iovlen = 0;
85 mntflags = verbose = 0;
86 ssector = -1;
87
88 while ((ch = getopt(argc, argv, "begG:jm:M:o:rs:U:vC:")) != -1)
89 switch (ch) {
90 case 'b':
91 build_iovec(&iov, &iovlen, "brokenjoliet", NULL, (size_t)-1);
92 break;
93 case 'e':
94 build_iovec(&iov, &iovlen, "extatt", NULL, (size_t)-1);
95 break;
96 case 'g':
97 build_iovec(&iov, &iovlen, "gens", NULL, (size_t)-1);
98 break;
99 case 'G':
100 build_iovec_argf(&iov, &iovlen, "gid", "%d", a_gid(optarg));
101 break;
102 case 'm':
103 build_iovec_argf(&iov, &iovlen, "mask", "%u", a_mask(optarg));
104 break;
105 case 'M':
106 build_iovec_argf(&iov, &iovlen, "dirmask", "%u", a_mask(optarg));
107 break;
108 case 'j':
109 build_iovec(&iov, &iovlen, "nojoliet", NULL, (size_t)-1);
110 break;
111 case 'o':
112 getmntopts(optarg, mopts, &mntflags, NULL);
113 p = strchr(optarg, '=');
114 val = NULL;
115 if (p != NULL) {
116 *p = '\0';
117 val = p + 1;
118 }
119 build_iovec(&iov, &iovlen, optarg, val, (size_t)-1);
120 break;
121 case 'r':
122 build_iovec(&iov, &iovlen, "norrip", NULL, (size_t)-1);
123 break;
124 case 's':
125 ssector = atoi(optarg);
126 break;
127 case 'U':
128 build_iovec_argf(&iov, &iovlen, "uid", "%d", a_uid(optarg));
129 break;
130 case 'v':
131 verbose++;
132 break;
133 case 'C':
134 if (set_charset(&iov, &iovlen, optarg) == -1)
135 err(EX_OSERR, "cd9660_iconv");
136 build_iovec(&iov, &iovlen, "kiconv", NULL, (size_t)-1);
137 break;
138 case '?':
139 default:
140 usage();
141 }
142 argc -= optind;
143 argv += optind;
144
145 if (argc != 2)
146 usage();
147
148 dev = argv[0];
149 dir = argv[1];
150
151 /*
152 * Resolve the mountpoint with realpath(3) and remove unnecessary
153 * slashes from the devicename if there are any.
154 */
155 if (checkpath(dir, mntpath) != 0)
156 err(1, "%s", mntpath);
157 (void)rmslashes(dev, dev);
158
159 if (ssector == -1) {
160 /*
161 * The start of the session has not been specified on
162 * the command line. If we can successfully read the
163 * TOC of a CD-ROM, use the last data track we find.
164 * Otherwise, just use 0, in order to mount the very
165 * first session. This is compatible with the
166 * historic behaviour of mount_cd9660(8). If the user
167 * has specified -s <ssector> above, we don't get here
168 * and leave the user's will.
169 */
170 if ((ssector = get_ssector(dev)) == -1) {
171 if (verbose)
172 printf("could not determine starting sector, "
173 "using very first session\n");
174 ssector = 0;
175 } else if (verbose)
176 printf("using starting sector %d\n", ssector);
177 }
178 mntflags |= MNT_RDONLY;
179 build_iovec(&iov, &iovlen, "fstype", fstype, (size_t)-1);
180 build_iovec(&iov, &iovlen, "fspath", mntpath, (size_t)-1);
181 build_iovec(&iov, &iovlen, "from", dev, (size_t)-1);
182 build_iovec_argf(&iov, &iovlen, "ssector", "%d", ssector);
183
184 if (nmount(iov, iovlen, mntflags) < 0)
185 err(1, "%s", dev);
186 exit(0);
187 }
188
189 void
usage(void)190 usage(void)
191 {
192 (void)fprintf(stderr,
193 "usage: mount_cd9660 [-begjrv] [-C charset] [-G gid] [-m mask] [-M mask]\n"
194 " [-o options] [-U uid] [-s startsector] special node\n");
195 exit(EX_USAGE);
196 }
197
198 static int
get_ssector(const char * dev)199 get_ssector(const char *dev)
200 {
201 struct ioc_toc_header h;
202 struct ioc_read_toc_entry t;
203 struct cd_toc_entry toc_buffer[100];
204 int fd, ntocentries, i;
205
206 if ((fd = open(dev, O_RDONLY)) == -1)
207 return -1;
208 if (ioctl(fd, CDIOREADTOCHEADER, &h) == -1) {
209 close(fd);
210 return -1;
211 }
212
213 ntocentries = h.ending_track - h.starting_track + 1;
214 if (ntocentries > 100) {
215 /* unreasonable, only 100 allowed */
216 close(fd);
217 return -1;
218 }
219 t.address_format = CD_LBA_FORMAT;
220 t.starting_track = 0;
221 t.data_len = ntocentries * sizeof(struct cd_toc_entry);
222 t.data = toc_buffer;
223
224 if (ioctl(fd, CDIOREADTOCENTRYS, (char *) &t) == -1) {
225 close(fd);
226 return -1;
227 }
228 close(fd);
229
230 for (i = ntocentries - 1; i >= 0; i--)
231 if ((toc_buffer[i].control & 4) != 0)
232 /* found a data track */
233 break;
234 if (i < 0)
235 return -1;
236
237 return ntohl(toc_buffer[i].addr.lba);
238 }
239
240 static int
set_charset(struct iovec ** iov,int * iovlen,const char * localcs)241 set_charset(struct iovec **iov, int *iovlen, const char *localcs)
242 {
243 int error;
244 char *cs_disk; /* disk charset for Joliet cs conversion */
245 char *cs_local; /* local charset for Joliet cs conversion */
246
247 cs_disk = NULL;
248 cs_local = NULL;
249
250 if (modfind("cd9660_iconv") < 0)
251 if (kldload("cd9660_iconv") < 0 || modfind("cd9660_iconv") < 0) {
252 warnx( "cannot find or load \"cd9660_iconv\" kernel module");
253 return (-1);
254 }
255
256 if ((cs_disk = malloc(ICONV_CSNMAXLEN)) == NULL)
257 return (-1);
258 if ((cs_local = malloc(ICONV_CSNMAXLEN)) == NULL) {
259 free(cs_disk);
260 return (-1);
261 }
262 strncpy(cs_disk, ENCODING_UNICODE, ICONV_CSNMAXLEN);
263 strncpy(cs_local, kiconv_quirkcs(localcs, KICONV_VENDOR_MICSFT),
264 ICONV_CSNMAXLEN);
265 error = kiconv_add_xlat16_cspairs(cs_disk, cs_local);
266 if (error)
267 return (-1);
268
269 build_iovec(iov, iovlen, "cs_disk", cs_disk, (size_t)-1);
270 build_iovec(iov, iovlen, "cs_local", cs_local, (size_t)-1);
271
272 return (0);
273 }
274
275 static gid_t
a_gid(const char * s)276 a_gid(const char *s)
277 {
278 struct group *gr;
279 const char *gname;
280 gid_t gid;
281
282 if ((gr = getgrnam(s)) != NULL)
283 gid = gr->gr_gid;
284 else {
285 for (gname = s; *s && isdigit(*s); ++s);
286 if (!*s)
287 gid = atoi(gname);
288 else
289 errx(EX_NOUSER, "unknown group id: %s", gname);
290 }
291 return (gid);
292 }
293
294 static uid_t
a_uid(const char * s)295 a_uid(const char *s)
296 {
297 struct passwd *pw;
298 const char *uname;
299 uid_t uid;
300
301 if ((pw = getpwnam(s)) != NULL)
302 uid = pw->pw_uid;
303 else {
304 for (uname = s; *s && isdigit(*s); ++s);
305 if (!*s)
306 uid = atoi(uname);
307 else
308 errx(EX_NOUSER, "unknown user id: %s", uname);
309 }
310 return (uid);
311 }
312
313 static mode_t
a_mask(const char * s)314 a_mask(const char *s)
315 {
316 int done, rv;
317 char *ep;
318
319 done = 0;
320 rv = -1;
321 if (*s >= '0' && *s <= '7') {
322 done = 1;
323 rv = strtol(optarg, &ep, 8);
324 }
325 if (!done || rv < 0 || *ep)
326 errx(EX_USAGE, "invalid file mode: %s", s);
327 return (rv);
328 }
329