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  * @(#) Copyright (c) 1992, 1993, 1994 The Regents of the University of California.  All rights reserved.
41  * @(#)mount_cd9660.c	8.7 (Berkeley) 5/1/95
42  * $FreeBSD: src/sbin/mount_cd9660/mount_cd9660.c,v 1.15.2.3 2001/03/14 12:05:01 bp Exp $
43  * $DragonFly: src/sbin/mount_cd9660/mount_cd9660.c,v 1.5 2005/04/02 21:43:15 dillon Exp $
44  */
45 
46 #include <sys/cdio.h>
47 #include <sys/file.h>
48 #include <sys/param.h>
49 #include <sys/mount.h>
50 #include <sys/../vfs/isofs/cd9660/cd9660_mount.h>
51 
52 #include <err.h>
53 #include <errno.h>
54 #include <stdlib.h>
55 #include <stdio.h>
56 #include <string.h>
57 #include <sysexits.h>
58 #include <unistd.h>
59 
60 #include "mntopts.h"
61 
62 struct mntopt mopts[] = {
63 	MOPT_STDOPTS,
64 	MOPT_UPDATE,
65 	{ "extatt", 0, ISOFSMNT_EXTATT, 1 },
66 	{ "gens", 0, ISOFSMNT_GENS, 1 },
67 	{ "rrip", 1, ISOFSMNT_NORRIP, 1 },
68 	{ "joliet", 1, ISOFSMNT_NOJOLIET, 1 },
69 	{ "strictjoliet", 1, ISOFSMNT_BROKENJOLIET, 1 },
70 	MOPT_NULL
71 };
72 
73 static int	get_ssector(const char *dev);
74 static void	usage(void);
75 
76 int
77 main(int argc, char **argv)
78 {
79 	struct iso_args args;
80 	int ch, mntflags, opts;
81 	char *dev, *dir, mntpath[MAXPATHLEN];
82 	struct vfsconf vfc;
83 	int error, verbose;
84 
85 	mntflags = opts = verbose = 0;
86 	memset(&args, 0, sizeof args);
87 	args.ssector = -1;
88 	while ((ch = getopt(argc, argv, "begjo:rs:v")) != -1)
89 		switch (ch) {
90 		case 'b':
91 			opts |= ISOFSMNT_BROKENJOLIET;
92 			break;
93 		case 'e':
94 			opts |= ISOFSMNT_EXTATT;
95 			break;
96 		case 'g':
97 			opts |= ISOFSMNT_GENS;
98 			break;
99 		case 'j':
100 			opts |= ISOFSMNT_NOJOLIET;
101 			break;
102 		case 'o':
103 			getmntopts(optarg, mopts, &mntflags, &opts);
104 			break;
105 		case 'r':
106 			opts |= ISOFSMNT_NORRIP;
107 			break;
108 		case 's':
109 			args.ssector = atoi(optarg);
110 			break;
111 		case 'v':
112 			verbose++;
113 			break;
114 		case '?':
115 		default:
116 			usage();
117 		}
118 	argc -= optind;
119 	argv += optind;
120 
121 	if (argc != 2)
122 		usage();
123 
124 	dev = argv[0];
125 	dir = argv[1];
126 
127 	/*
128 	 * Resolve the mountpoint with realpath(3) and remove unnecessary
129 	 * slashes from the devicename if there are any.
130 	 */
131 	checkpath(dir, mntpath);
132 	rmslashes(dev, dev);
133 
134 #define DEFAULT_ROOTUID	-2
135 	/*
136 	 * ISO 9660 filesystems are not writeable.
137 	 */
138 	mntflags |= MNT_RDONLY;
139 	args.export.ex_flags = MNT_EXRDONLY;
140 	args.fspec = dev;
141 	args.export.ex_root = DEFAULT_ROOTUID;
142 	args.flags = opts;
143 
144 	if (args.ssector == -1) {
145 		/*
146 		 * The start of the session has not been specified on
147 		 * the command line.  If we can successfully read the
148 		 * TOC of a CD-ROM, use the last data track we find.
149 		 * Otherwise, just use 0, in order to mount the very
150 		 * first session.  This is compatible with the
151 		 * historic behaviour of mount_cd9660(8).  If the user
152 		 * has specified -s <ssector> above, we don't get here
153 		 * and leave the user's will.
154 		 */
155 		if ((args.ssector = get_ssector(dev)) == -1) {
156 			if (verbose)
157 				printf("could not determine starting sector, "
158 				       "using very first session\n");
159 			args.ssector = 0;
160 		} else if (verbose)
161 			printf("using starting sector %d\n", args.ssector);
162 	}
163 
164 	error = getvfsbyname("cd9660", &vfc);
165 	if (error && vfsisloadable("cd9660")) {
166 		if (vfsload("cd9660"))
167 			err(EX_OSERR, "vfsload(cd9660)");
168 		endvfsent();	/* flush cache */
169 		error = getvfsbyname("cd9660", &vfc);
170 	}
171 	if (error)
172 		errx(1, "cd9660 filesystem is not available");
173 
174 	if (mount(vfc.vfc_name, mntpath, mntflags, &args) < 0)
175 		err(1, "%s", args.fspec);
176 	exit(0);
177 }
178 
179 static void
180 usage(void)
181 {
182 	fprintf(stderr,
183 	    "usage: mount_cd9660 [-egrv] [-o options] [-s startsector] special node\n");
184 	exit(EX_USAGE);
185 }
186 
187 static int
188 get_ssector(const char *dev)
189 {
190 	struct ioc_toc_header h;
191 	struct ioc_read_toc_entry t;
192 	struct cd_toc_entry toc_buffer[100];
193 	int fd, ntocentries, i;
194 
195 	if ((fd = open(dev, O_RDONLY)) == -1)
196 		return -1;
197 	if (ioctl(fd, CDIOREADTOCHEADER, &h) == -1) {
198 		close(fd);
199 		return -1;
200 	}
201 
202 	ntocentries = h.ending_track - h.starting_track + 1;
203 	if (ntocentries > 100) {
204 		/* unreasonable, only 100 allowed */
205 		close(fd);
206 		return -1;
207 	}
208 	t.address_format = CD_LBA_FORMAT;
209 	t.starting_track = 0;
210 	t.data_len = ntocentries * sizeof(struct cd_toc_entry);
211 	t.data = toc_buffer;
212 
213 	if (ioctl(fd, CDIOREADTOCENTRYS, (char *) &t) == -1) {
214 		close(fd);
215 		return -1;
216 	}
217 	close(fd);
218 
219 	for (i = ntocentries - 1; i >= 0; i--)
220 		if ((toc_buffer[i].control & 4) != 0)
221 			/* found a data track */
222 			break;
223 	if (i < 0)
224 		return -1;
225 
226 	return ntohl(toc_buffer[i].addr.lba);
227 }
228