mount_cd9660.c revision 122080
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 * 3. All advertising materials mentioning features or use of this software
19 *    must display the following acknowledgement:
20 *	This product includes software developed by the University of
21 *	California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 *    may be used to endorse or promote products derived from this software
24 *    without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 *      @(#)mount_cd9660.c	8.7 (Berkeley) 5/1/95
39 */
40
41#ifndef lint
42static const char copyright[] =
43"@(#) Copyright (c) 1992, 1993, 1994\n\
44        The Regents of the University of California.  All rights reserved.\n";
45#endif /* not lint */
46
47#ifndef lint
48/*
49static char sccsid[] = "@(#)mount_cd9660.c	8.7 (Berkeley) 5/1/95";
50*/
51static const char rcsid[] =
52  "$FreeBSD: head/sbin/mount_cd9660/mount_cd9660.c 122080 2003-11-04 21:04:14Z trhodes $";
53#endif /* not lint */
54
55#include <sys/cdio.h>
56#include <sys/file.h>
57#include <sys/param.h>
58#include <sys/mount.h>
59#include <sys/../isofs/cd9660/cd9660_mount.h>
60#include <sys/module.h>
61#include <sys/iconv.h>
62#include <sys/linker.h>
63
64#include <arpa/inet.h>
65
66#include <err.h>
67#include <errno.h>
68#include <stdlib.h>
69#include <stdio.h>
70#include <string.h>
71#include <sysexits.h>
72#include <unistd.h>
73
74#include "mntopts.h"
75
76struct mntopt mopts[] = {
77	MOPT_STDOPTS,
78	MOPT_UPDATE,
79	{ "extatt", 0, ISOFSMNT_EXTATT, 1 },
80	{ "gens", 0, ISOFSMNT_GENS, 1 },
81	{ "rrip", 1, ISOFSMNT_NORRIP, 1 },
82	{ "joliet", 1, ISOFSMNT_NOJOLIET, 1 },
83	{ "strictjoliet", 1, ISOFSMNT_BROKENJOLIET, 1 },
84	{ NULL }
85};
86
87int	get_ssector(const char *dev);
88int	set_charset(struct iso_args *, const char *);
89void	usage(void);
90
91int
92main(int argc, char **argv)
93{
94	struct iso_args args;
95	int ch, mntflags, opts;
96	char *dev, *dir, mntpath[MAXPATHLEN];
97	int verbose;
98
99	mntflags = opts = verbose = 0;
100	memset(&args, 0, sizeof args);
101	args.ssector = -1;
102	args.cs_disk = NULL;
103	args.cs_local = NULL;
104	while ((ch = getopt(argc, argv, "begjo:rs:vC:")) != -1)
105		switch (ch) {
106		case 'b':
107			opts |= ISOFSMNT_BROKENJOLIET;
108			break;
109		case 'e':
110			opts |= ISOFSMNT_EXTATT;
111			break;
112		case 'g':
113			opts |= ISOFSMNT_GENS;
114			break;
115		case 'j':
116			opts |= ISOFSMNT_NOJOLIET;
117			break;
118		case 'o':
119			getmntopts(optarg, mopts, &mntflags, &opts);
120			break;
121		case 'r':
122			opts |= ISOFSMNT_NORRIP;
123			break;
124		case 's':
125			args.ssector = atoi(optarg);
126			break;
127		case 'v':
128			verbose++;
129			break;
130		case 'C':
131			if (set_charset(&args, optarg) == -1)
132				err(EX_OSERR, "cd9660_iconv");
133			opts |= ISOFSMNT_KICONV;
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#define DEFAULT_ROOTUID	-2
156	/*
157	 * ISO 9660 file systems are not writeable.
158	 */
159	mntflags |= MNT_RDONLY;
160	args.export.ex_flags = MNT_EXRDONLY;
161	args.fspec = dev;
162	args.export.ex_root = DEFAULT_ROOTUID;
163	args.flags = opts;
164
165	if (args.ssector == -1) {
166		/*
167		 * The start of the session has not been specified on
168		 * the command line.  If we can successfully read the
169		 * TOC of a CD-ROM, use the last data track we find.
170		 * Otherwise, just use 0, in order to mount the very
171		 * first session.  This is compatible with the
172		 * historic behaviour of mount_cd9660(8).  If the user
173		 * has specified -s <ssector> above, we don't get here
174		 * and leave the user's will.
175		 */
176		if ((args.ssector = get_ssector(dev)) == -1) {
177			if (verbose)
178				printf("could not determine starting sector, "
179				       "using very first session\n");
180			args.ssector = 0;
181		} else if (verbose)
182			printf("using starting sector %d\n", args.ssector);
183	}
184
185	if (mount("cd9660", mntpath, mntflags, &args) < 0)
186		err(1, "%s", args.fspec);
187	exit(0);
188}
189
190void
191usage(void)
192{
193	(void)fprintf(stderr,
194		"usage: mount_cd9660 [-egrv] [-o options] [-s startsector] [-C charset ] special node\n");
195	exit(EX_USAGE);
196}
197
198int
199get_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
240int
241set_charset(struct iso_args *args, const char *localcs)
242{
243	int error;
244
245	if (modfind("cd9660_iconv") < 0)
246		if (kldload("cd9660_iconv") < 0 || modfind("cd9660_iconv") < 0) {
247			warnx( "cannot find or load \"cd9660_iconv\" kernel module");
248			return (-1);
249		}
250
251	if ((args->cs_disk = malloc(ICONV_CSNMAXLEN)) == NULL)
252		return (-1);
253	if ((args->cs_local = malloc(ICONV_CSNMAXLEN)) == NULL)
254		return (-1);
255	strncpy(args->cs_disk, ENCODING_UNICODE, ICONV_CSNMAXLEN);
256	strncpy(args->cs_local, kiconv_quirkcs(localcs, KICONV_VENDOR_MICSFT),
257	    ICONV_CSNMAXLEN);
258	error = kiconv_add_xlat16_cspair(args->cs_local, args->cs_disk, 0);
259	if (error)
260		return (-1);
261	error = kiconv_add_xlat16_cspair(args->cs_disk, args->cs_local, 0);
262	if (error)
263		return (-1);
264
265	return (0);
266}
267