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