xref: /freebsd/sbin/mount_cd9660/mount_cd9660.c (revision 7bd6fde3)
1 /*
2  * Copyright (c) 1992, 1993, 1994
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley
6  * by Pace Willisson (pace@blitz.com).  The Rock Ridge Extension
7  * Support code is derived from software contributed to Berkeley
8  * by Atsushi Murai (amurai@spec.co.jp).
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 4. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  *      @(#)mount_cd9660.c	8.7 (Berkeley) 5/1/95
35  */
36 
37 #ifndef lint
38 static const char copyright[] =
39 "@(#) Copyright (c) 1992, 1993, 1994\n\
40         The Regents of the University of California.  All rights reserved.\n";
41 #endif /* not lint */
42 
43 #ifndef lint
44 /*
45 static char sccsid[] = "@(#)mount_cd9660.c	8.7 (Berkeley) 5/1/95";
46 */
47 static const char rcsid[] =
48   "$FreeBSD$";
49 #endif /* not lint */
50 
51 #include <sys/cdio.h>
52 #include <sys/file.h>
53 #include <sys/param.h>
54 #include <sys/mount.h>
55 #include <sys/module.h>
56 #include <sys/iconv.h>
57 #include <sys/linker.h>
58 
59 #include <arpa/inet.h>
60 
61 #include <err.h>
62 #include <errno.h>
63 #include <stdlib.h>
64 #include <stdio.h>
65 #include <string.h>
66 #include <sysexits.h>
67 #include <unistd.h>
68 
69 #include "mntopts.h"
70 
71 struct mntopt mopts[] = {
72 	MOPT_STDOPTS,
73 	MOPT_UPDATE,
74 	MOPT_END
75 };
76 
77 static int	get_ssector(const char *dev);
78 static int	set_charset(struct iovec **, int *iovlen, const char *);
79 void	usage(void);
80 
81 int
82 main(int argc, char **argv)
83 {
84 	struct iovec *iov;
85 	int iovlen;
86 	int ch, mntflags, opts;
87 	char *dev, *dir, *p, *val, mntpath[MAXPATHLEN];
88 	int verbose;
89 	int ssector;		/* starting sector, 0 for 1st session */
90 	char fstype[] = "cd9660";
91 
92 	iov = NULL;
93 	iovlen = 0;
94 	mntflags = opts = verbose = 0;
95 	ssector = -1;
96 
97 	while ((ch = getopt(argc, argv, "begjo:rs:vC:")) != -1)
98 		switch (ch) {
99 		case 'b':
100 			build_iovec(&iov, &iovlen, "brokenjoliet", NULL, (size_t)-1);
101 			break;
102 		case 'e':
103 			build_iovec(&iov, &iovlen, "extatt", NULL, (size_t)-1);
104 			break;
105 		case 'g':
106 			build_iovec(&iov, &iovlen, "gens", NULL, (size_t)-1);
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, &opts);
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 'v':
128 			verbose++;
129 			break;
130 		case 'C':
131 			if (set_charset(&iov, &iovlen, optarg) == -1)
132 				err(EX_OSERR, "cd9660_iconv");
133 			build_iovec(&iov, &iovlen, "kiconv", NULL, (size_t)-1);
134 			break;
135 		case '?':
136 		default:
137 			usage();
138 		}
139 	argc -= optind;
140 	argv += optind;
141 
142 	if (argc != 2)
143 		usage();
144 
145 	dev = argv[0];
146 	dir = argv[1];
147 
148 	/*
149 	 * Resolve the mountpoint with realpath(3) and remove unnecessary
150 	 * slashes from the devicename if there are any.
151 	 */
152 	(void)checkpath(dir, mntpath);
153 	(void)rmslashes(dev, dev);
154 
155 	if (ssector == -1) {
156 		/*
157 		 * The start of the session has not been specified on
158 		 * the command line.  If we can successfully read the
159 		 * TOC of a CD-ROM, use the last data track we find.
160 		 * Otherwise, just use 0, in order to mount the very
161 		 * first session.  This is compatible with the
162 		 * historic behaviour of mount_cd9660(8).  If the user
163 		 * has specified -s <ssector> above, we don't get here
164 		 * and leave the user's will.
165 		 */
166 		if ((ssector = get_ssector(dev)) == -1) {
167 			if (verbose)
168 				printf("could not determine starting sector, "
169 				       "using very first session\n");
170 			ssector = 0;
171 		} else if (verbose)
172 			printf("using starting sector %d\n", ssector);
173 	}
174 	mntflags |= MNT_RDONLY;
175 	build_iovec(&iov, &iovlen, "fstype", fstype, (size_t)-1);
176 	build_iovec(&iov, &iovlen, "fspath", mntpath, (size_t)-1);
177 	build_iovec(&iov, &iovlen, "from", dev, (size_t)-1);
178 	build_iovec_argf(&iov, &iovlen, "ssector", "%d", ssector);
179 
180 	if (nmount(iov, iovlen, mntflags) < 0)
181 		err(1, "%s", dev);
182 	exit(0);
183 }
184 
185 void
186 usage(void)
187 {
188 	(void)fprintf(stderr,
189 "usage: mount_cd9660 [-begjrv] [-C charset] [-o options] [-s startsector]\n"
190 "                    special node\n");
191 	exit(EX_USAGE);
192 }
193 
194 static int
195 get_ssector(const char *dev)
196 {
197 	struct ioc_toc_header h;
198 	struct ioc_read_toc_entry t;
199 	struct cd_toc_entry toc_buffer[100];
200 	int fd, ntocentries, i;
201 
202 	if ((fd = open(dev, O_RDONLY)) == -1)
203 		return -1;
204 	if (ioctl(fd, CDIOREADTOCHEADER, &h) == -1) {
205 		close(fd);
206 		return -1;
207 	}
208 
209 	ntocentries = h.ending_track - h.starting_track + 1;
210 	if (ntocentries > 100) {
211 		/* unreasonable, only 100 allowed */
212 		close(fd);
213 		return -1;
214 	}
215 	t.address_format = CD_LBA_FORMAT;
216 	t.starting_track = 0;
217 	t.data_len = ntocentries * sizeof(struct cd_toc_entry);
218 	t.data = toc_buffer;
219 
220 	if (ioctl(fd, CDIOREADTOCENTRYS, (char *) &t) == -1) {
221 		close(fd);
222 		return -1;
223 	}
224 	close(fd);
225 
226 	for (i = ntocentries - 1; i >= 0; i--)
227 		if ((toc_buffer[i].control & 4) != 0)
228 			/* found a data track */
229 			break;
230 	if (i < 0)
231 		return -1;
232 
233 	return ntohl(toc_buffer[i].addr.lba);
234 }
235 
236 static int
237 set_charset(struct iovec **iov, int *iovlen, const char *localcs)
238 {
239 	int error;
240 	char *cs_disk;	/* disk charset for Joliet cs conversion */
241 	char *cs_local;	/* local charset for Joliet cs conversion */
242 
243 	cs_disk = NULL;
244 	cs_local = NULL;
245 
246 	if (modfind("cd9660_iconv") < 0)
247 		if (kldload("cd9660_iconv") < 0 || modfind("cd9660_iconv") < 0) {
248 			warnx( "cannot find or load \"cd9660_iconv\" kernel module");
249 			return (-1);
250 		}
251 
252 	if ((cs_disk = malloc(ICONV_CSNMAXLEN)) == NULL)
253 		return (-1);
254 	if ((cs_local = malloc(ICONV_CSNMAXLEN)) == NULL)
255 		return (-1);
256 	strncpy(cs_disk, ENCODING_UNICODE, ICONV_CSNMAXLEN);
257 	strncpy(cs_local, kiconv_quirkcs(localcs, KICONV_VENDOR_MICSFT),
258 	    ICONV_CSNMAXLEN);
259 	error = kiconv_add_xlat16_cspairs(cs_disk, cs_local);
260 	if (error)
261 		return (-1);
262 
263 	build_iovec(iov, iovlen, "cs_disk", cs_disk, (size_t)-1);
264 	build_iovec(iov, iovlen, "cs_local", cs_local, (size_t)-1);
265 
266 	return (0);
267 }
268