xref: /freebsd/sbin/mount_cd9660/mount_cd9660.c (revision 06c3fb2749bda94cb5201f81ffdb8fa6c3161b2e)
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 <err.h>
48 #include <errno.h>
49 #include <stdlib.h>
50 #include <stdio.h>
51 #include <string.h>
52 #include <sysexits.h>
53 #include <unistd.h>
54 
55 #include "mntopts.h"
56 
57 static struct mntopt mopts[] = {
58 	MOPT_STDOPTS,
59 	MOPT_UPDATE,
60 	MOPT_END
61 };
62 
63 static int	get_ssector(const char *dev);
64 static int	set_charset(struct iovec **, int *iovlen, const char *);
65 void	usage(void);
66 
67 int
68 main(int argc, char **argv)
69 {
70 	struct iovec *iov;
71 	int iovlen;
72 	int ch, mntflags;
73 	char *dev, *dir, *p, *val, mntpath[MAXPATHLEN];
74 	int verbose;
75 	int ssector;		/* starting sector, 0 for 1st session */
76 	char fstype[] = "cd9660";
77 
78 	iov = NULL;
79 	iovlen = 0;
80 	mntflags = verbose = 0;
81 	ssector = -1;
82 
83 	while ((ch = getopt(argc, argv, "begjo:rs:vC:")) != -1)
84 		switch (ch) {
85 		case 'b':
86 			build_iovec(&iov, &iovlen, "brokenjoliet", NULL, (size_t)-1);
87 			break;
88 		case 'e':
89 			build_iovec(&iov, &iovlen, "extatt", NULL, (size_t)-1);
90 			break;
91 		case 'g':
92 			build_iovec(&iov, &iovlen, "gens", NULL, (size_t)-1);
93 			break;
94 		case 'j':
95 			build_iovec(&iov, &iovlen, "nojoliet", NULL, (size_t)-1);
96 			break;
97 		case 'o':
98 			getmntopts(optarg, mopts, &mntflags, NULL);
99 			p = strchr(optarg, '=');
100 			val = NULL;
101 			if (p != NULL) {
102 				*p = '\0';
103 				val = p + 1;
104 			}
105 			build_iovec(&iov, &iovlen, optarg, val, (size_t)-1);
106 			break;
107 		case 'r':
108 			build_iovec(&iov, &iovlen, "norrip", NULL, (size_t)-1);
109 			break;
110 		case 's':
111 			ssector = atoi(optarg);
112 			break;
113 		case 'v':
114 			verbose++;
115 			break;
116 		case 'C':
117 			if (set_charset(&iov, &iovlen, optarg) == -1)
118 				err(EX_OSERR, "cd9660_iconv");
119 			build_iovec(&iov, &iovlen, "kiconv", NULL, (size_t)-1);
120 			break;
121 		case '?':
122 		default:
123 			usage();
124 		}
125 	argc -= optind;
126 	argv += optind;
127 
128 	if (argc != 2)
129 		usage();
130 
131 	dev = argv[0];
132 	dir = argv[1];
133 
134 	/*
135 	 * Resolve the mountpoint with realpath(3) and remove unnecessary
136 	 * slashes from the devicename if there are any.
137 	 */
138 	if (checkpath(dir, mntpath) != 0)
139 		err(1, "%s", mntpath);
140 	(void)rmslashes(dev, dev);
141 
142 	if (ssector == -1) {
143 		/*
144 		 * The start of the session has not been specified on
145 		 * the command line.  If we can successfully read the
146 		 * TOC of a CD-ROM, use the last data track we find.
147 		 * Otherwise, just use 0, in order to mount the very
148 		 * first session.  This is compatible with the
149 		 * historic behaviour of mount_cd9660(8).  If the user
150 		 * has specified -s <ssector> above, we don't get here
151 		 * and leave the user's will.
152 		 */
153 		if ((ssector = get_ssector(dev)) == -1) {
154 			if (verbose)
155 				printf("could not determine starting sector, "
156 				       "using very first session\n");
157 			ssector = 0;
158 		} else if (verbose)
159 			printf("using starting sector %d\n", ssector);
160 	}
161 	mntflags |= MNT_RDONLY;
162 	build_iovec(&iov, &iovlen, "fstype", fstype, (size_t)-1);
163 	build_iovec(&iov, &iovlen, "fspath", mntpath, (size_t)-1);
164 	build_iovec(&iov, &iovlen, "from", dev, (size_t)-1);
165 	build_iovec_argf(&iov, &iovlen, "ssector", "%d", ssector);
166 
167 	if (nmount(iov, iovlen, mntflags) < 0)
168 		err(1, "%s", dev);
169 	exit(0);
170 }
171 
172 void
173 usage(void)
174 {
175 	(void)fprintf(stderr,
176 "usage: mount_cd9660 [-begjrv] [-C charset] [-o options] [-s startsector]\n"
177 "                    special node\n");
178 	exit(EX_USAGE);
179 }
180 
181 static int
182 get_ssector(const char *dev)
183 {
184 	struct ioc_toc_header h;
185 	struct ioc_read_toc_entry t;
186 	struct cd_toc_entry toc_buffer[100];
187 	int fd, ntocentries, i;
188 
189 	if ((fd = open(dev, O_RDONLY)) == -1)
190 		return -1;
191 	if (ioctl(fd, CDIOREADTOCHEADER, &h) == -1) {
192 		close(fd);
193 		return -1;
194 	}
195 
196 	ntocentries = h.ending_track - h.starting_track + 1;
197 	if (ntocentries > 100) {
198 		/* unreasonable, only 100 allowed */
199 		close(fd);
200 		return -1;
201 	}
202 	t.address_format = CD_LBA_FORMAT;
203 	t.starting_track = 0;
204 	t.data_len = ntocentries * sizeof(struct cd_toc_entry);
205 	t.data = toc_buffer;
206 
207 	if (ioctl(fd, CDIOREADTOCENTRYS, (char *) &t) == -1) {
208 		close(fd);
209 		return -1;
210 	}
211 	close(fd);
212 
213 	for (i = ntocentries - 1; i >= 0; i--)
214 		if ((toc_buffer[i].control & 4) != 0)
215 			/* found a data track */
216 			break;
217 	if (i < 0)
218 		return -1;
219 
220 	return ntohl(toc_buffer[i].addr.lba);
221 }
222 
223 static int
224 set_charset(struct iovec **iov, int *iovlen, const char *localcs)
225 {
226 	int error;
227 	char *cs_disk;	/* disk charset for Joliet cs conversion */
228 	char *cs_local;	/* local charset for Joliet cs conversion */
229 
230 	cs_disk = NULL;
231 	cs_local = NULL;
232 
233 	if (modfind("cd9660_iconv") < 0)
234 		if (kldload("cd9660_iconv") < 0 || modfind("cd9660_iconv") < 0) {
235 			warnx( "cannot find or load \"cd9660_iconv\" kernel module");
236 			return (-1);
237 		}
238 
239 	if ((cs_disk = malloc(ICONV_CSNMAXLEN)) == NULL)
240 		return (-1);
241 	if ((cs_local = malloc(ICONV_CSNMAXLEN)) == NULL) {
242 		free(cs_disk);
243 		return (-1);
244 	}
245 	strncpy(cs_disk, ENCODING_UNICODE, ICONV_CSNMAXLEN);
246 	strncpy(cs_local, kiconv_quirkcs(localcs, KICONV_VENDOR_MICSFT),
247 	    ICONV_CSNMAXLEN);
248 	error = kiconv_add_xlat16_cspairs(cs_disk, cs_local);
249 	if (error)
250 		return (-1);
251 
252 	build_iovec(iov, iovlen, "cs_disk", cs_disk, (size_t)-1);
253 	build_iovec(iov, iovlen, "cs_local", cs_local, (size_t)-1);
254 
255 	return (0);
256 }
257