xref: /freebsd-13-stable/sbin/mount_cd9660/mount_cd9660.c (revision 470de7a717fc1c5954f1196c1d10b96fd9f2d524)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1992, 1993, 1994
5  *      The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley
8  * by Pace Willisson (pace@blitz.com).  The Rock Ridge Extension
9  * Support code is derived from software contributed to Berkeley
10  * by Atsushi Murai (amurai@spec.co.jp).
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  *      @(#)mount_cd9660.c	8.7 (Berkeley) 5/1/95
37  */
38 
39 #ifndef lint
40 static const char copyright[] =
41 "@(#) Copyright (c) 1992, 1993, 1994\n\
42         The Regents of the University of California.  All rights reserved.\n";
43 #endif /* not lint */
44 
45 #ifndef lint
46 /*
47 static char sccsid[] = "@(#)mount_cd9660.c	8.7 (Berkeley) 5/1/95";
48 */
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 <ctype.h>
62 #include <err.h>
63 #include <errno.h>
64 #include <grp.h>
65 #include <pwd.h>
66 #include <stdlib.h>
67 #include <stdio.h>
68 #include <string.h>
69 #include <sysexits.h>
70 #include <unistd.h>
71 
72 #include "mntopts.h"
73 
74 static struct mntopt mopts[] = {
75 	MOPT_STDOPTS,
76 	MOPT_UPDATE,
77 	MOPT_END
78 };
79 
80 static gid_t	a_gid(const char *);
81 static uid_t	a_uid(const char *);
82 static mode_t	a_mask(const char *);
83 static int	get_ssector(const char *dev);
84 static int	set_charset(struct iovec **, int *iovlen, const char *);
85 void	usage(void);
86 
87 int
main(int argc,char ** argv)88 main(int argc, char **argv)
89 {
90 	struct iovec *iov;
91 	int iovlen;
92 	int ch, mntflags;
93 	char *dev, *dir, *p, *val, mntpath[MAXPATHLEN];
94 	int verbose;
95 	int ssector;		/* starting sector, 0 for 1st session */
96 	char fstype[] = "cd9660";
97 
98 	iov = NULL;
99 	iovlen = 0;
100 	mntflags = verbose = 0;
101 	ssector = -1;
102 
103 	while ((ch = getopt(argc, argv, "begG:jm:M:o:rs:U:vC:")) != -1)
104 		switch (ch) {
105 		case 'b':
106 			build_iovec(&iov, &iovlen, "brokenjoliet", NULL, (size_t)-1);
107 			break;
108 		case 'e':
109 			build_iovec(&iov, &iovlen, "extatt", NULL, (size_t)-1);
110 			break;
111 		case 'g':
112 			build_iovec(&iov, &iovlen, "gens", NULL, (size_t)-1);
113 			break;
114 		case 'G':
115 		        build_iovec_argf(&iov, &iovlen, "gid", "%d", a_gid(optarg));
116 			break;
117 		case 'm':
118 			build_iovec_argf(&iov, &iovlen, "mask", "%u", a_mask(optarg));
119 			break;
120 		case 'M':
121 			build_iovec_argf(&iov, &iovlen, "dirmask", "%u", a_mask(optarg));
122 			break;
123 		case 'j':
124 			build_iovec(&iov, &iovlen, "nojoliet", NULL, (size_t)-1);
125 			break;
126 		case 'o':
127 			getmntopts(optarg, mopts, &mntflags, NULL);
128 			p = strchr(optarg, '=');
129 			val = NULL;
130 			if (p != NULL) {
131 				*p = '\0';
132 				val = p + 1;
133 			}
134 			build_iovec(&iov, &iovlen, optarg, val, (size_t)-1);
135 			break;
136 		case 'r':
137 			build_iovec(&iov, &iovlen, "norrip", NULL, (size_t)-1);
138 			break;
139 		case 's':
140 			ssector = atoi(optarg);
141 			break;
142 		case 'U':
143 		        build_iovec_argf(&iov, &iovlen, "uid", "%d", a_uid(optarg));
144 			break;
145 		case 'v':
146 			verbose++;
147 			break;
148 		case 'C':
149 			if (set_charset(&iov, &iovlen, optarg) == -1)
150 				err(EX_OSERR, "cd9660_iconv");
151 			build_iovec(&iov, &iovlen, "kiconv", NULL, (size_t)-1);
152 			break;
153 		case '?':
154 		default:
155 			usage();
156 		}
157 	argc -= optind;
158 	argv += optind;
159 
160 	if (argc != 2)
161 		usage();
162 
163 	dev = argv[0];
164 	dir = argv[1];
165 
166 	/*
167 	 * Resolve the mountpoint with realpath(3) and remove unnecessary
168 	 * slashes from the devicename if there are any.
169 	 */
170 	if (checkpath(dir, mntpath) != 0)
171 		err(1, "%s", mntpath);
172 	(void)rmslashes(dev, dev);
173 
174 	if (ssector == -1) {
175 		/*
176 		 * The start of the session has not been specified on
177 		 * the command line.  If we can successfully read the
178 		 * TOC of a CD-ROM, use the last data track we find.
179 		 * Otherwise, just use 0, in order to mount the very
180 		 * first session.  This is compatible with the
181 		 * historic behaviour of mount_cd9660(8).  If the user
182 		 * has specified -s <ssector> above, we don't get here
183 		 * and leave the user's will.
184 		 */
185 		if ((ssector = get_ssector(dev)) == -1) {
186 			if (verbose)
187 				printf("could not determine starting sector, "
188 				       "using very first session\n");
189 			ssector = 0;
190 		} else if (verbose)
191 			printf("using starting sector %d\n", ssector);
192 	}
193 	mntflags |= MNT_RDONLY;
194 	build_iovec(&iov, &iovlen, "fstype", fstype, (size_t)-1);
195 	build_iovec(&iov, &iovlen, "fspath", mntpath, (size_t)-1);
196 	build_iovec(&iov, &iovlen, "from", dev, (size_t)-1);
197 	build_iovec_argf(&iov, &iovlen, "ssector", "%d", ssector);
198 
199 	if (nmount(iov, iovlen, mntflags) < 0)
200 		err(1, "%s", dev);
201 	exit(0);
202 }
203 
204 void
usage(void)205 usage(void)
206 {
207 	(void)fprintf(stderr,
208 "usage: mount_cd9660 [-begjrv] [-C charset] [-G gid] [-m mask] [-M mask]\n"
209 "                    [-o options] [-U uid] [-s startsector] special node\n");
210 	exit(EX_USAGE);
211 }
212 
213 static int
get_ssector(const char * dev)214 get_ssector(const char *dev)
215 {
216 	struct ioc_toc_header h;
217 	struct ioc_read_toc_entry t;
218 	struct cd_toc_entry toc_buffer[100];
219 	int fd, ntocentries, i;
220 
221 	if ((fd = open(dev, O_RDONLY)) == -1)
222 		return -1;
223 	if (ioctl(fd, CDIOREADTOCHEADER, &h) == -1) {
224 		close(fd);
225 		return -1;
226 	}
227 
228 	ntocentries = h.ending_track - h.starting_track + 1;
229 	if (ntocentries > 100) {
230 		/* unreasonable, only 100 allowed */
231 		close(fd);
232 		return -1;
233 	}
234 	t.address_format = CD_LBA_FORMAT;
235 	t.starting_track = 0;
236 	t.data_len = ntocentries * sizeof(struct cd_toc_entry);
237 	t.data = toc_buffer;
238 
239 	if (ioctl(fd, CDIOREADTOCENTRYS, (char *) &t) == -1) {
240 		close(fd);
241 		return -1;
242 	}
243 	close(fd);
244 
245 	for (i = ntocentries - 1; i >= 0; i--)
246 		if ((toc_buffer[i].control & 4) != 0)
247 			/* found a data track */
248 			break;
249 	if (i < 0)
250 		return -1;
251 
252 	return ntohl(toc_buffer[i].addr.lba);
253 }
254 
255 static int
set_charset(struct iovec ** iov,int * iovlen,const char * localcs)256 set_charset(struct iovec **iov, int *iovlen, const char *localcs)
257 {
258 	int error;
259 	char *cs_disk;	/* disk charset for Joliet cs conversion */
260 	char *cs_local;	/* local charset for Joliet cs conversion */
261 
262 	cs_disk = NULL;
263 	cs_local = NULL;
264 
265 	if (modfind("cd9660_iconv") < 0)
266 		if (kldload("cd9660_iconv") < 0 || modfind("cd9660_iconv") < 0) {
267 			warnx( "cannot find or load \"cd9660_iconv\" kernel module");
268 			return (-1);
269 		}
270 
271 	if ((cs_disk = malloc(ICONV_CSNMAXLEN)) == NULL)
272 		return (-1);
273 	if ((cs_local = malloc(ICONV_CSNMAXLEN)) == NULL) {
274 		free(cs_disk);
275 		return (-1);
276 	}
277 	strncpy(cs_disk, ENCODING_UNICODE, ICONV_CSNMAXLEN);
278 	strncpy(cs_local, kiconv_quirkcs(localcs, KICONV_VENDOR_MICSFT),
279 	    ICONV_CSNMAXLEN);
280 	error = kiconv_add_xlat16_cspairs(cs_disk, cs_local);
281 	if (error)
282 		return (-1);
283 
284 	build_iovec(iov, iovlen, "cs_disk", cs_disk, (size_t)-1);
285 	build_iovec(iov, iovlen, "cs_local", cs_local, (size_t)-1);
286 
287 	return (0);
288 }
289 
290 static gid_t
a_gid(const char * s)291 a_gid(const char *s)
292 {
293 	struct group *gr;
294 	const char *gname;
295 	gid_t gid;
296 
297 	if ((gr = getgrnam(s)) != NULL)
298 		gid = gr->gr_gid;
299 	else {
300 		for (gname = s; *s && isdigit(*s); ++s);
301 		if (!*s)
302 			gid = atoi(gname);
303 		else
304 			errx(EX_NOUSER, "unknown group id: %s", gname);
305 	}
306 	return (gid);
307 }
308 
309 static uid_t
a_uid(const char * s)310 a_uid(const char *s)
311 {
312 	struct passwd *pw;
313 	const char *uname;
314 	uid_t uid;
315 
316 	if ((pw = getpwnam(s)) != NULL)
317 		uid = pw->pw_uid;
318 	else {
319 		for (uname = s; *s && isdigit(*s); ++s);
320 		if (!*s)
321 			uid = atoi(uname);
322 		else
323 			errx(EX_NOUSER, "unknown user id: %s", uname);
324 	}
325 	return (uid);
326 }
327 
328 static mode_t
a_mask(const char * s)329 a_mask(const char *s)
330 {
331 	int done, rv;
332 	char *ep;
333 
334 	done = 0;
335 	rv = -1;
336 	if (*s >= '0' && *s <= '7') {
337 		done = 1;
338 		rv = strtol(optarg, &ep, 8);
339 	}
340 	if (!done || rv < 0 || *ep)
341 		errx(EX_USAGE, "invalid file mode: %s", s);
342 	return (rv);
343 }
344