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