xref: /freebsd/sbin/mount_cd9660/mount_cd9660.c (revision 6af83ee0d2941d18880b6aaa2b4facd1d30c6106)
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/../isofs/cd9660/cd9660_mount.h>
56 #include <sys/module.h>
57 #include <sys/iconv.h>
58 #include <sys/linker.h>
59 
60 #include <arpa/inet.h>
61 
62 #include <err.h>
63 #include <errno.h>
64 #include <stdlib.h>
65 #include <stdio.h>
66 #include <string.h>
67 #include <sysexits.h>
68 #include <unistd.h>
69 
70 #include "mntopts.h"
71 
72 struct mntopt mopts[] = {
73 	MOPT_STDOPTS,
74 	MOPT_UPDATE,
75 	{ "extatt", 0, ISOFSMNT_EXTATT, 1 },
76 	{ "gens", 0, ISOFSMNT_GENS, 1 },
77 	{ "rrip", 1, ISOFSMNT_NORRIP, 1 },
78 	{ "joliet", 1, ISOFSMNT_NOJOLIET, 1 },
79 	{ "strictjoliet", 1, ISOFSMNT_BROKENJOLIET, 1 },
80 	{ NULL }
81 };
82 
83 int	get_ssector(const char *dev);
84 int	set_charset(struct iso_args *, const char *);
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 	int verbose;
94 
95 	mntflags = opts = verbose = 0;
96 	memset(&args, 0, sizeof args);
97 	args.ssector = -1;
98 	args.cs_disk = NULL;
99 	args.cs_local = NULL;
100 	while ((ch = getopt(argc, argv, "begjo:rs:vC:")) != -1)
101 		switch (ch) {
102 		case 'b':
103 			opts |= ISOFSMNT_BROKENJOLIET;
104 			break;
105 		case 'e':
106 			opts |= ISOFSMNT_EXTATT;
107 			break;
108 		case 'g':
109 			opts |= ISOFSMNT_GENS;
110 			break;
111 		case 'j':
112 			opts |= ISOFSMNT_NOJOLIET;
113 			break;
114 		case 'o':
115 			getmntopts(optarg, mopts, &mntflags, &opts);
116 			break;
117 		case 'r':
118 			opts |= ISOFSMNT_NORRIP;
119 			break;
120 		case 's':
121 			args.ssector = atoi(optarg);
122 			break;
123 		case 'v':
124 			verbose++;
125 			break;
126 		case 'C':
127 			if (set_charset(&args, optarg) == -1)
128 				err(EX_OSERR, "cd9660_iconv");
129 			opts |= ISOFSMNT_KICONV;
130 			break;
131 		case '?':
132 		default:
133 			usage();
134 		}
135 	argc -= optind;
136 	argv += optind;
137 
138 	if (argc != 2)
139 		usage();
140 
141 	dev = argv[0];
142 	dir = argv[1];
143 
144 	/*
145 	 * Resolve the mountpoint with realpath(3) and remove unnecessary
146 	 * slashes from the devicename if there are any.
147 	 */
148 	(void)checkpath(dir, mntpath);
149 	(void)rmslashes(dev, dev);
150 
151 #define DEFAULT_ROOTUID	-2
152 	/*
153 	 * ISO 9660 file systems are not writeable.
154 	 */
155 	mntflags |= MNT_RDONLY;
156 	args.export.ex_flags = MNT_EXRDONLY;
157 	args.fspec = dev;
158 	args.export.ex_root = DEFAULT_ROOTUID;
159 	args.flags = opts;
160 
161 	if (args.ssector == -1) {
162 		/*
163 		 * The start of the session has not been specified on
164 		 * the command line.  If we can successfully read the
165 		 * TOC of a CD-ROM, use the last data track we find.
166 		 * Otherwise, just use 0, in order to mount the very
167 		 * first session.  This is compatible with the
168 		 * historic behaviour of mount_cd9660(8).  If the user
169 		 * has specified -s <ssector> above, we don't get here
170 		 * and leave the user's will.
171 		 */
172 		if ((args.ssector = get_ssector(dev)) == -1) {
173 			if (verbose)
174 				printf("could not determine starting sector, "
175 				       "using very first session\n");
176 			args.ssector = 0;
177 		} else if (verbose)
178 			printf("using starting sector %d\n", args.ssector);
179 	}
180 
181 	if (mount("cd9660", mntpath, mntflags, &args) < 0)
182 		err(1, "%s", args.fspec);
183 	exit(0);
184 }
185 
186 void
187 usage(void)
188 {
189 	(void)fprintf(stderr,
190 "usage: mount_cd9660 [-begjrv] [-C charset] [-o options] [-s startsector]\n"
191 "                    special node\n");
192 	exit(EX_USAGE);
193 }
194 
195 int
196 get_ssector(const char *dev)
197 {
198 	struct ioc_toc_header h;
199 	struct ioc_read_toc_entry t;
200 	struct cd_toc_entry toc_buffer[100];
201 	int fd, ntocentries, i;
202 
203 	if ((fd = open(dev, O_RDONLY)) == -1)
204 		return -1;
205 	if (ioctl(fd, CDIOREADTOCHEADER, &h) == -1) {
206 		close(fd);
207 		return -1;
208 	}
209 
210 	ntocentries = h.ending_track - h.starting_track + 1;
211 	if (ntocentries > 100) {
212 		/* unreasonable, only 100 allowed */
213 		close(fd);
214 		return -1;
215 	}
216 	t.address_format = CD_LBA_FORMAT;
217 	t.starting_track = 0;
218 	t.data_len = ntocentries * sizeof(struct cd_toc_entry);
219 	t.data = toc_buffer;
220 
221 	if (ioctl(fd, CDIOREADTOCENTRYS, (char *) &t) == -1) {
222 		close(fd);
223 		return -1;
224 	}
225 	close(fd);
226 
227 	for (i = ntocentries - 1; i >= 0; i--)
228 		if ((toc_buffer[i].control & 4) != 0)
229 			/* found a data track */
230 			break;
231 	if (i < 0)
232 		return -1;
233 
234 	return ntohl(toc_buffer[i].addr.lba);
235 }
236 
237 int
238 set_charset(struct iso_args *args, const char *localcs)
239 {
240 	int error;
241 
242 	if (modfind("cd9660_iconv") < 0)
243 		if (kldload("cd9660_iconv") < 0 || modfind("cd9660_iconv") < 0) {
244 			warnx( "cannot find or load \"cd9660_iconv\" kernel module");
245 			return (-1);
246 		}
247 
248 	if ((args->cs_disk = malloc(ICONV_CSNMAXLEN)) == NULL)
249 		return (-1);
250 	if ((args->cs_local = malloc(ICONV_CSNMAXLEN)) == NULL)
251 		return (-1);
252 	strncpy(args->cs_disk, ENCODING_UNICODE, ICONV_CSNMAXLEN);
253 	strncpy(args->cs_local, kiconv_quirkcs(localcs, KICONV_VENDOR_MICSFT),
254 	    ICONV_CSNMAXLEN);
255 	error = kiconv_add_xlat16_cspairs(args->cs_disk, args->cs_local);
256 	if (error)
257 		return (-1);
258 
259 	return (0);
260 }
261