xref: /freebsd/sbin/mount_cd9660/mount_cd9660.c (revision b52b9d56d4e96089873a75f9e29062eec19fabba)
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
42 static 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 /*
49 static char sccsid[] = "@(#)mount_cd9660.c	8.7 (Berkeley) 5/1/95";
50 */
51 static const char rcsid[] =
52   "$FreeBSD$";
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 
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 struct mntopt mopts[] = {
74 	MOPT_STDOPTS,
75 	MOPT_UPDATE,
76 	{ "extatt", 0, ISOFSMNT_EXTATT, 1 },
77 	{ "gens", 0, ISOFSMNT_GENS, 1 },
78 	{ "rrip", 1, ISOFSMNT_NORRIP, 1 },
79 	{ "joliet", 1, ISOFSMNT_NOJOLIET, 1 },
80 	{ "strictjoliet", 1, ISOFSMNT_BROKENJOLIET, 1 },
81 	{ NULL }
82 };
83 
84 int	get_ssector(const char *dev);
85 void	usage(void);
86 
87 int
88 main(int argc, char **argv)
89 {
90 	struct iso_args args;
91 	int ch, mntflags, opts;
92 	char *dev, *dir, mntpath[MAXPATHLEN];
93 	struct vfsconf vfc;
94 	int error, verbose;
95 
96 	mntflags = opts = verbose = 0;
97 	memset(&args, 0, sizeof args);
98 	args.ssector = -1;
99 	while ((ch = getopt(argc, argv, "begjo:rs:v")) != -1)
100 		switch (ch) {
101 		case 'b':
102 			opts |= ISOFSMNT_BROKENJOLIET;
103 			break;
104 		case 'e':
105 			opts |= ISOFSMNT_EXTATT;
106 			break;
107 		case 'g':
108 			opts |= ISOFSMNT_GENS;
109 			break;
110 		case 'j':
111 			opts |= ISOFSMNT_NOJOLIET;
112 			break;
113 		case 'o':
114 			getmntopts(optarg, mopts, &mntflags, &opts);
115 			break;
116 		case 'r':
117 			opts |= ISOFSMNT_NORRIP;
118 			break;
119 		case 's':
120 			args.ssector = atoi(optarg);
121 			break;
122 		case 'v':
123 			verbose++;
124 			break;
125 		case '?':
126 		default:
127 			usage();
128 		}
129 	argc -= optind;
130 	argv += optind;
131 
132 	if (argc != 2)
133 		usage();
134 
135 	dev = argv[0];
136 	dir = argv[1];
137 
138 	/*
139 	 * Resolve the mountpoint with realpath(3) and remove unnecessary
140 	 * slashes from the devicename if there are any.
141 	 */
142 	(void)checkpath(dir, mntpath);
143 	(void)rmslashes(dev, dev);
144 
145 #define DEFAULT_ROOTUID	-2
146 	/*
147 	 * ISO 9660 filesystems are not writeable.
148 	 */
149 	mntflags |= MNT_RDONLY;
150 	args.export.ex_flags = MNT_EXRDONLY;
151 	args.fspec = dev;
152 	args.export.ex_root = DEFAULT_ROOTUID;
153 	args.flags = opts;
154 
155 	if (args.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 ((args.ssector = get_ssector(dev)) == -1) {
167 			if (verbose)
168 				printf("could not determine starting sector, "
169 				       "using very first session\n");
170 			args.ssector = 0;
171 		} else if (verbose)
172 			printf("using starting sector %d\n", args.ssector);
173 	}
174 
175 	error = getvfsbyname("cd9660", &vfc);
176 	if (error && vfsisloadable("cd9660")) {
177 		if (vfsload("cd9660"))
178 			err(EX_OSERR, "vfsload(cd9660)");
179 		endvfsent();	/* flush cache */
180 		error = getvfsbyname("cd9660", &vfc);
181 	}
182 	if (error)
183 		errx(1, "cd9660 filesystem is not available");
184 
185 	if (mount(vfc.vfc_name, mntpath, mntflags, &args) < 0)
186 		err(1, "%s", args.fspec);
187 	exit(0);
188 }
189 
190 void
191 usage(void)
192 {
193 	(void)fprintf(stderr,
194 		"usage: mount_cd9660 [-egrv] [-o options] [-s startsector] special node\n");
195 	exit(EX_USAGE);
196 }
197 
198 int
199 get_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