]> git.saurik.com Git - apple/xnu.git/blame - bsd/isofs/cd9660/cd9660_util.c
xnu-1228.15.4.tar.gz
[apple/xnu.git] / bsd / isofs / cd9660 / cd9660_util.c
CommitLineData
1c79356b 1/*
2d21ac55 2 * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
1c79356b 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
2d21ac55
A
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
8f6c56a5 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
27 */
28/* $NetBSD: cd9660_util.c,v 1.8 1994/12/13 22:33:25 mycroft Exp $ */
29
30/*-
31 * Copyright (c) 1994
32 * The Regents of the University of California. All rights reserved.
33 *
34 * This code is derived from software contributed to Berkeley
35 * by Pace Willisson (pace@blitz.com). The Rock Ridge Extension
36 * Support code is derived from software contributed to Berkeley
37 * by Atsushi Murai (amurai@spec.co.jp).
38 *
39 * Redistribution and use in source and binary forms, with or without
40 * modification, are permitted provided that the following conditions
41 * are met:
42 * 1. Redistributions of source code must retain the above copyright
43 * notice, this list of conditions and the following disclaimer.
44 * 2. Redistributions in binary form must reproduce the above copyright
45 * notice, this list of conditions and the following disclaimer in the
46 * documentation and/or other materials provided with the distribution.
47 * 3. All advertising materials mentioning features or use of this software
48 * must display the following acknowledgement:
49 * This product includes software developed by the University of
50 * California, Berkeley and its contributors.
51 * 4. Neither the name of the University nor the names of its contributors
52 * may be used to endorse or promote products derived from this software
53 * without specific prior written permission.
54 *
55 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
56 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
58 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
59 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
60 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
61 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
62 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
63 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
64 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65 * SUCH DAMAGE.
66 *
67 * @(#)cd9660_util.c 8.3 (Berkeley) 12/5/94
68 *
69 * HISTORY
70 * 7-Dec-98 Add ATTR_VOL_MOUNTFLAGS attribute support - djb
71 * 18-Nov-98 Add support for volfs - djb
72 */
73
74#include <sys/param.h>
75#include <sys/systm.h>
76#include <sys/vnode.h>
77#include <sys/mount.h>
78#include <sys/namei.h>
79#include <sys/resourcevar.h>
80#include <sys/kernel.h>
81#include <sys/file.h>
82#include <sys/stat.h>
83#include <sys/buf.h>
84#include <sys/proc.h>
91447636 85#include <sys/kauth.h>
1c79356b
A
86#include <sys/conf.h>
87#include <sys/utfconv.h>
88#include <miscfs/specfs/specdev.h> /* XXX */
89#include <miscfs/fifofs/fifo.h> /* XXX */
90#include <sys/malloc.h>
91#include <sys/dir.h>
92#include <sys/attr.h>
93#include <kern/assert.h>
1c79356b
A
94
95#include <isofs/cd9660/iso.h>
96#include <isofs/cd9660/cd9660_node.h>
97#include <isofs/cd9660/iso_rrip.h>
98
2d21ac55
A
99#include <libkern/OSByteOrder.h>
100
1c79356b
A
101/*
102 * translate and compare a filename
103 * Note: Version number plus ';' may be omitted.
104 */
105int
91447636 106isofncmp(u_char *fn, int fnlen, u_char *isofn, int isolen)
1c79356b
A
107{
108 int i, j;
109 char c;
110
111 while (--fnlen >= 0) {
112 if (--isolen < 0)
113 return *fn;
114 if ((c = *isofn++) == ';') {
115 switch (*fn++) {
116 default:
117 return *--fn;
118 case 0:
119 return 0;
120 case ';':
121 break;
122 }
123 for (i = 0; --fnlen >= 0; i = i * 10 + *fn++ - '0') {
124 if (*fn < '0' || *fn > '9') {
125 return -1;
126 }
127 }
128 for (j = 0; --isolen >= 0; j = j * 10 + *isofn++ - '0');
129 return i - j;
130 }
131 /* if raw comparison fails, check if char was mapped */
132 if (c != *fn) {
133 if (c >= 'A' && c <= 'Z') {
134 if (c + ('a' - 'A') != *fn) {
135 if (*fn >= 'a' && *fn <= 'z')
136 return *fn - ('a' - 'A') - c;
137 else
138 return *fn - c;
139 }
140 } else if (c == '/') {
141 if (*fn != ':')
142 return *fn - c;
143 } else if (c > 0 || *fn != '_')
144 return *fn - c;
145 }
146 fn++;
147 }
148 if (isolen > 0) {
149 switch (*isofn) {
150 default:
151 return -1;
152 case '.':
153 if (isofn[1] != ';')
154 return -1;
155 case ';':
156 return 0;
157 }
158 }
159 return 0;
160}
161
162
163/*
164 * translate and compare a UCS-2 filename
165 * Note: Version number plus ';' may be omitted.
0c530ab8
A
166 *
167 * The name pointed to by "fn" is the search name, whose characters are
168 * in native endian order. The name "ucsfn" is the on-disk name, whose
169 * characters are in big endian order.
1c79356b
A
170 */
171
172int
91447636 173ucsfncmp(u_int16_t *fn, int fnlen, u_int16_t *ucsfn, int ucslen)
1c79356b
A
174{
175 int i, j;
176 u_int16_t c;
177
178 /* convert byte count to char count */
179 ucslen /= 2;
180 fnlen /= 2;
181
182 while (--fnlen >= 0) {
183 if (--ucslen < 0)
184 return *fn;
0c530ab8 185 if ((c = OSSwapBigToHostInt16(*ucsfn++)) == UCS_SEPARATOR2) {
1c79356b
A
186 switch (*fn++) {
187 default:
188 return *--fn;
189 case 0:
190 return 0;
191 case UCS_SEPARATOR2:
192 break;
193 }
194 for (i = 0; --fnlen >= 0; i = i * 10 + *fn++ - '0') {
195 if (*fn < '0' || *fn > '9') {
196 return -1;
197 }
198 }
0c530ab8 199 for (j = 0; --ucslen >= 0; j = j * 10 + OSSwapBigToHostInt16(*ucsfn++) - '0');
1c79356b
A
200 return i - j;
201 }
202 if (c != *fn)
203 return *fn - c;
204 fn++;
205 }
206 if (ucslen > 0) {
207 switch (*ucsfn) {
208 default:
209 return -1;
0c530ab8
A
210 case OSSwapHostToBigConstInt16(UCS_SEPARATOR1):
211 if (ucsfn[1] != OSSwapHostToBigConstInt16(UCS_SEPARATOR2))
1c79356b 212 return -1;
0c530ab8 213 case OSSwapHostToBigConstInt16(UCS_SEPARATOR2):
1c79356b
A
214 return 0;
215 }
216 }
217 return 0;
218}
219
220
221/*
222 * translate a filename
223 */
224void
91447636
A
225isofntrans(u_char *infn, int infnlen, u_char *outfn, u_short *outfnlen,
226 int original, int assoc)
1c79356b
A
227{
228 int fnidx = 0;
229
55e303ae
A
230 /*
231 * Add a "._" prefix for associated files
232 */
233 if (assoc) {
234 *outfn++ = ASSOCCHAR1;
235 *outfn++ = ASSOCCHAR2;
236 fnidx += 2;
237 infnlen +=2;
238 }
1c79356b
A
239 for (; fnidx < infnlen; fnidx++) {
240 char c = *infn++;
241
242 /*
243 * Some ISO 9600 CD names contain 8-bit chars.
244 * These chars are mapped to '_' because there
245 * is no context for mapping them to UTF-8.
246 * In addition '/' is mapped to ':'.
247 *
248 * isofncmp accounts for these mappings.
249 */
250 if (!original) {
251 if (c < 0)
252 c = '_';
253 else if (c == '/')
254 c = ':';
255 else if (c == '.' && *infn == ';')
256 break;
257 else if (c == ';')
258 break;
259 }
260 *outfn++ = c;
261 }
262 *outfnlen = fnidx;
263}
264
265
266
267/*
268 * translate a UCS-2 filename to UTF-8
269 */
270void
91447636
A
271ucsfntrans(u_int16_t *infn, int infnlen, u_char *outfn, u_short *outfnlen,
272 int dir, int assoc)
1c79356b
A
273{
274 if (infnlen == 1) {
275 strcpy(outfn, "..");
276
277 if (*(u_char*)infn == 0)
278 *outfnlen = 1;
279 else if (*(u_char*)infn == 1)
280 *outfnlen = 2;
281 } else {
282 int fnidx;
283 size_t outbytes;
284 int flags;
285
286 fnidx = infnlen/2;
287 flags = 0;
288
55e303ae
A
289 /*
290 * Add a "._" prefix for associated files
291 */
292 if (assoc) {
293 *outfn++ = ASSOCCHAR1;
294 *outfn++ = ASSOCCHAR2;
295 }
1c79356b
A
296 if (!dir) {
297 /* strip file version number */
298 for (fnidx--; fnidx > 0; fnidx--) {
299 /* stop when ';' is found */
0c530ab8 300 if (infn[fnidx] == OSSwapHostToBigConstInt16(UCS_SEPARATOR2)) {
1c79356b 301 /* drop dangling dot */
0c530ab8 302 if (fnidx > 0 && infn[fnidx-1] == OSSwapHostToBigConstInt16(UCS_SEPARATOR1))
1c79356b
A
303 fnidx--;
304 break;
305 }
306 }
307 if (fnidx <= 0)
308 fnidx = infnlen/2;
309 }
310
2d21ac55 311 flags = UTF_NO_NULL_TERM | UTF_DECOMPOSED | UTF_BIG_ENDIAN;
1c79356b
A
312
313 (void) utf8_encodestr(infn, fnidx * 2, outfn, &outbytes, ISO_JOLIET_NAMEMAX, 0, flags);
55e303ae 314 *outfnlen = assoc ? outbytes + 2 : outbytes;
1c79356b
A
315 }
316}
317
318
319/*
320 * count the number of children by enumerating the directory
321 */
322static int
91447636 323isochildcount(struct vnode *vdp, int *dircnt, int *filcnt)
1c79356b
A
324{
325 struct iso_node *dp;
326 struct buf *bp = NULL;
55e303ae 327 struct iso_mnt *imp;
1c79356b 328 struct iso_directory_record *ep;
91447636 329 uint32_t bmask;
1c79356b
A
330 int error = 0;
331 int reclen;
332 int dirs, files;
333 int blkoffset;
334 int logblksize;
91447636 335 int32_t diroffset;
1c79356b
A
336
337 dp = VTOI(vdp);
55e303ae
A
338 imp = dp->i_mnt;
339 bmask = imp->im_sector_size - 1;
340 logblksize = imp->im_sector_size;
1c79356b
A
341 blkoffset = diroffset = 0;
342 dirs = files = 0;
343
344 while (diroffset < dp->i_size) {
345 /*
346 * If offset is on a block boundary, read the next
347 * directory block. Release previous if it exists.
348 */
349 if ((diroffset & bmask) == 0) {
350 if (bp != NULL)
91447636
A
351 buf_brelse(bp);
352 if ( (error = cd9660_blkatoff(vdp, SECTOFF(imp, diroffset), NULL, &bp)) )
1c79356b
A
353 break;
354 blkoffset = 0;
355 }
356
357 ep = (struct iso_directory_record *)
91447636 358 (buf_dataptr(bp) + blkoffset);
1c79356b
A
359
360 reclen = isonum_711(ep->length);
361 if (reclen == 0) {
362 /* skip to next block, if any */
363 diroffset =
364 (diroffset & ~bmask) + logblksize;
365 continue;
366 }
367
368 if ((reclen < ISO_DIRECTORY_RECORD_SIZE) ||
369 (blkoffset + reclen > logblksize) ||
370 (reclen < ISO_DIRECTORY_RECORD_SIZE + isonum_711(ep->name_len))){
371 /* illegal, so give up */
372 break;
373 }
374
55e303ae
A
375 /*
376 * Some poorly mastered discs have an incorrect directory
377 * file size. If the '.' entry has a better size (bigger)
378 * then use that instead.
379 */
380 if ((diroffset == 0) && (isonum_733(ep->size) > dp->i_size)) {
381 dp->i_size = isonum_733(ep->size);
382 }
383
1c79356b
A
384 if ( isonum_711(ep->flags) & directoryBit )
385 dirs++;
386 else if ((isonum_711(ep->flags) & associatedBit) == 0)
387 files++;
388
389 diroffset += reclen;
390 blkoffset += reclen;
391 }
392
393 if (bp)
91447636 394 buf_brelse (bp);
1c79356b
A
395
396 *dircnt = dirs;
397 *filcnt = files;
398
399 return (error);
400}
401
402
91447636
A
403static uint32_t
404DerivePermissionSummary(uid_t owner, gid_t group, mode_t obj_mode, __unused struct iso_mnt *imp)
405{
406 kauth_cred_t cred = kauth_cred_get();
407 uint32_t permissions;
408 int is_member;
1c79356b
A
409
410 /* User id 0 (root) always gets access. */
91447636 411 if (!suser(cred, NULL)) {
1c79356b
A
412 permissions = R_OK | X_OK;
413 goto Exit;
414 };
415
416 /* Otherwise, check the owner. */
91447636
A
417 if (owner == kauth_cred_getuid(cred)) {
418 permissions = ((uint32_t)obj_mode & S_IRWXU) >> 6;
1c79356b
A
419 goto Exit;
420 }
421
422 /* Otherwise, check the groups. */
91447636
A
423 if (kauth_cred_ismember_gid(cred, group, &is_member) == 0 && is_member) {
424 permissions = ((uint32_t)obj_mode & S_IRWXG) >> 3;
1c79356b 425 goto Exit;
91447636 426 }
1c79356b
A
427
428 /* Otherwise, settle for 'others' access. */
91447636 429 permissions = (uint32_t)obj_mode & S_IRWXO;
1c79356b
A
430
431Exit:
432 return permissions & ~W_OK; /* Write access is always impossible */
433}