summaryrefslogtreecommitdiffstats
path: root/lib/libarchive/archive_read_support_format_mtree.c
blob: e6ac99f6bfb1091037c8870c50b88b7452fe6dea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
/*-
 * Copyright (c) 2003-2007 Tim Kientzle
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "archive_platform.h"
__FBSDID("$FreeBSD$");

#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif
#include <stddef.h>
/* #include <stdint.h> */ /* See archive_platform.h */
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif

#include "archive.h"
#include "archive_entry.h"
#include "archive_private.h"
#include "archive_read_private.h"
#include "archive_string.h"

struct mtree_entry {
	struct mtree_entry *next;
	char *name;
	char *option_start;
	char *option_end;
	char full;
	char used;
};

struct mtree {
	struct archive_string	 line;
	size_t			 buffsize;
	char			*buff;
	off_t			 offset;
	int			 fd;
	int			 filetype;
	int			 archive_format;
	const char		*archive_format_name;
	struct mtree_entry	*entries;
	struct mtree_entry	*this_entry;
	struct archive_string	 current_dir;
	struct archive_string	 contents_name;
};

static int	cleanup(struct archive_read *);
static int	mtree_bid(struct archive_read *);
static void	parse_escapes(char *, struct mtree_entry *);
static int	parse_setting(struct archive_read *, struct mtree *,
		    struct archive_entry *, char *, char *);
static int	read_data(struct archive_read *a,
		    const void **buff, size_t *size, off_t *offset);
static ssize_t	readline(struct archive_read *, struct mtree *, char **, ssize_t);
static int	skip(struct archive_read *a);
static int	read_header(struct archive_read *,
		    struct archive_entry *);
static int64_t	mtree_atol10(char **);
static int64_t	mtree_atol8(char **);

int
archive_read_support_format_mtree(struct archive *_a)
{
	struct archive_read *a = (struct archive_read *)_a;
	struct mtree *mtree;
	int r;

	mtree = (struct mtree *)malloc(sizeof(*mtree));
	if (mtree == NULL) {
		archive_set_error(&a->archive, ENOMEM,
		    "Can't allocate mtree data");
		return (ARCHIVE_FATAL);
	}
	memset(mtree, 0, sizeof(*mtree));
	mtree->fd = -1;

	r = __archive_read_register_format(a, mtree,
	    mtree_bid, read_header, read_data, skip, cleanup);

	if (r != ARCHIVE_OK)
		free(mtree);
	return (ARCHIVE_OK);
}

static int
cleanup(struct archive_read *a)
{
	struct mtree *mtree;
	struct mtree_entry *p, *q;

	mtree = (struct mtree *)(a->format->data);
	p = mtree->entries;
	while (p != NULL) {
		q = p->next;
		free(p->name);
		/*
		 * Note: option_start, option_end are pointers into
		 * the block that p->name points to.  So we should
		 * not try to free them!
		 */
		free(p);
		p = q;
	}
	archive_string_free(&mtree->line);
	archive_string_free(&mtree->current_dir);
	archive_string_free(&mtree->contents_name);
	free(mtree->buff);
	free(mtree);
	(a->format->data) = NULL;
	return (ARCHIVE_OK);
}


static int
mtree_bid(struct archive_read *a)
{
	struct mtree *mtree;
	ssize_t bytes_read;
	const void *h;
	const char *signature = "#mtree";
	const char *p;
	int bid;

	mtree = (struct mtree *)(a->format->data);

	/* Now let's look at the actual header and see if it matches. */
	bytes_read = (a->decompressor->read_ahead)(a, &h, strlen(signature));

	if (bytes_read <= 0)
		return (bytes_read);

	p = h;
	bid = 0;
	while (bytes_read > 0 && *signature != '\0') {
		if (*p != *signature)
			return (bid = 0);
		bid += 8;
		p++;
		signature++;
		bytes_read--;
	}
	return (bid);
}

/*
 * The extended mtree format permits multiple lines specifying
 * attributes for each file.  Practically speaking, that means we have
 * to read the entire mtree file into memory up front.
 */
static int
read_mtree(struct archive_read *a, struct mtree *mtree)
{
	ssize_t len;
	char *p;
	struct mtree_entry *mentry;
	struct mtree_entry *last_mentry = NULL;

	mtree->archive_format = ARCHIVE_FORMAT_MTREE_V1;
	mtree->archive_format_name = "mtree";

	for (;;) {
		len = readline(a, mtree, &p, 256);
		if (len == 0) {
			mtree->this_entry = mtree->entries;
			return (ARCHIVE_OK);
		}
		if (len < 0)
			return (len);
		/* Leading whitespace is never significant, ignore it. */
		while (*p == ' ' || *p == '\t') {
			++p;
			--len;
		}
		/* Skip content lines and blank lines. */
		if (*p == '#')
			continue;
		if (*p == '\r' || *p == '\n' || *p == '\0')
			continue;
		mentry = malloc(sizeof(*mentry));
		if (mentry == NULL) {
			archive_set_error(&a->archive, ENOMEM,
			    "Can't allocate memory");
			return (ARCHIVE_FATAL);
		}
		memset(mentry, 0, sizeof(*mentry));
		/* Add this entry to list. */
		if (last_mentry == NULL) {
			last_mentry = mtree->entries = mentry;
		} else {
			last_mentry->next = mentry;
		}
		last_mentry = mentry;

		/* Copy line over onto heap. */
		mentry->name = malloc(len + 1);
		if (mentry->name == NULL) {
			free(mentry);
			archive_set_error(&a->archive, ENOMEM,
			    "Can't allocate memory");
			return (ARCHIVE_FATAL);
		}
		strcpy(mentry->name, p);
		mentry->option_end = mentry->name + len;
		/* Find end of name. */
		p = mentry->name;
		while (*p != ' ' && *p != '\n' && *p != '\0')
			++p;
		*p++ = '\0';
		parse_escapes(mentry->name, mentry);
		/* Find start of options and record it. */
		while (p < mentry->option_end && (*p == ' ' || *p == '\t'))
			++p;
		mentry->option_start = p;
		/* Null terminate each separate option. */
		while (++p < mentry->option_end)
			if (*p == ' ' || *p == '\t' || *p == '\n')
				*p = '\0';
	}
}

static int
read_header(struct archive_read *a, struct archive_entry *entry)
{
	struct stat st;
	struct mtree *mtree;
	struct mtree_entry *mentry, *mentry2;
	char *p, *q;
	int r = ARCHIVE_OK, r1;

	mtree = (struct mtree *)(a->format->data);

	if (mtree->fd >= 0) {
		close(mtree->fd);
		mtree->fd = -1;
	}

	if (mtree->entries == NULL) {
		r = read_mtree(a, mtree);
		if (r != ARCHIVE_OK)
			return (r);
	}

	a->archive.archive_format = mtree->archive_format;
	a->archive.archive_format_name = mtree->archive_format_name;

	for (;;) {
		mentry = mtree->this_entry;
		if (mentry == NULL) {
			mtree->this_entry = NULL;
			return (ARCHIVE_EOF);
		}
		mtree->this_entry = mentry->next;
		if (mentry->used)
			continue;
		mentry->used = 1;
		if (strcmp(mentry->name, "..") == 0) {
			if (archive_strlen(&mtree->current_dir) > 0) {
				/* Roll back current path. */
				p = mtree->current_dir.s
				    + mtree->current_dir.length - 1;
				while (p >= mtree->current_dir.s && *p != '/')
					--p;
				if (p >= mtree->current_dir.s)
					--p;
				mtree->current_dir.length
				    = p - mtree->current_dir.s + 1;
			}
			continue;
		}

		mtree->filetype = AE_IFREG;

		/* Parse options. */
		p = mentry->option_start;
		while (p < mentry->option_end) {
			q = p + strlen(p);
			r1 = parse_setting(a, mtree, entry, p, q);
			if (r1 != ARCHIVE_OK)
				r = r1;
			p = q + 1;
		}

		if (mentry->full) {
			archive_entry_copy_pathname(entry, mentry->name);
			/*
			 * "Full" entries are allowed to have multiple
			 * lines and those lines aren't required to be
			 * adjacent.  We don't support multiple lines
			 * for "relative" entries nor do we make any
			 * attempt to merge data from separate
			 * "relative" and "full" entries.  (Merging
			 * "relative" and "full" entries would require
			 * dealing with pathname canonicalization,
			 * which is a very tricky subject.)
			 */
			mentry2 = mentry->next;
			while (mentry2 != NULL) {
				if (mentry2->full
				    && !mentry2->used
				    && strcmp(mentry->name, mentry2->name) == 0) {
					/*
					 * Add those options as well;
					 * later lines override
					 * earlier ones.
					 */
					p = mentry2->option_start;
					while (p < mentry2->option_end) {
						q = p + strlen(p);
						r1 = parse_setting(a, mtree, entry, p, q);
						if (r1 != ARCHIVE_OK)
							r = r1;
						p = q + 1;
					}
					mentry2->used = 1;
				}
				mentry2 = mentry2->next;
			}
		} else {
			/*
			 * Relative entries require us to construct
			 * the full path and possibly update the
			 * current directory.
			 */
			size_t n = archive_strlen(&mtree->current_dir);
			if (n > 0)
				archive_strcat(&mtree->current_dir, "/");
			archive_strcat(&mtree->current_dir, mentry->name);
			archive_entry_copy_pathname(entry, mtree->current_dir.s);
			if (archive_entry_filetype(entry) != AE_IFDIR)
				mtree->current_dir.length = n;
		}

		/*
		 * Try to open and stat the file to get the real size.
		 * It would be nice to avoid this here so that getting
		 * a listing of an mtree wouldn't require opening
		 * every referenced contents file.  But then we
		 * wouldn't know the actual contents size, so I don't
		 * see a really viable way around this.  (Also, we may
		 * want to someday pull other unspecified info from
		 * the contents file on disk.)
		 */
		if (archive_strlen(&mtree->contents_name) > 0) {
			mtree->fd = open(mtree->contents_name.s, O_RDONLY);
			if (mtree->fd < 0) {
				archive_set_error(&a->archive, errno,
				    "Can't open content=\"%s\"",
				    mtree->contents_name.s);
				r = ARCHIVE_WARN;
			}
		} else {
			/* If the specified path opens, use it. */
			mtree->fd = open(mtree->current_dir.s, O_RDONLY);
			/* But don't fail if it's not there. */
		}

		/*
		 * If there is a contents file on disk, use that size;
		 * otherwise leave it as-is (it might have been set from
		 * the mtree size= keyword).
		 */
		if (mtree->fd >= 0) {
			fstat(mtree->fd, &st);
			archive_entry_set_size(entry, st.st_size);
		}

		return r;
	}
}

static int
parse_setting(struct archive_read *a, struct mtree *mtree, struct archive_entry *entry, char *key, char *end)
{
	char *val;


	if (end == key)
		return (ARCHIVE_OK);
	if (*key == '\0')
		return (ARCHIVE_OK);

	val = strchr(key, '=');
	if (val == NULL) {
		archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
		    "Malformed attribute \"%s\" (%d)", key, key[0]);
		return (ARCHIVE_WARN);
	}

	*val = '\0';
	++val;

	switch (key[0]) {
	case 'c':
		if (strcmp(key, "content") == 0) {
			parse_escapes(val, NULL);
			archive_strcpy(&mtree->contents_name, val);
			break;
		}
	case 'g':
		if (strcmp(key, "gid") == 0) {
			archive_entry_set_gid(entry, mtree_atol10(&val));
			break;
		}
		if (strcmp(key, "gname") == 0) {
			archive_entry_copy_gname(entry, val);
			break;
		}
	case 'm':
		if (strcmp(key, "mode") == 0) {
			if (val[0] == '0') {
				archive_entry_set_perm(entry,
				    mtree_atol8(&val));
			} else
				archive_set_error(&a->archive,
				    ARCHIVE_ERRNO_FILE_FORMAT,
				    "Symbolic mode \"%s\" unsupported", val);
			break;
		}
	case 't':
		if (strcmp(key, "type") == 0) {
			switch (val[0]) {
			case 'b':
				if (strcmp(val, "block") == 0) {
					mtree->filetype = AE_IFBLK;
					break;
				}
			case 'c':
				if (strcmp(val, "char") == 0) {
					mtree->filetype = AE_IFCHR;
					break;
				}
			case 'd':
				if (strcmp(val, "dir") == 0) {
					mtree->filetype = AE_IFDIR;
					break;
				}
			case 'f':
				if (strcmp(val, "fifo") == 0) {
					mtree->filetype = AE_IFIFO;
					break;
				}
				if (strcmp(val, "file") == 0) {
					mtree->filetype = AE_IFREG;
					break;
				}
			case 'l':
				if (strcmp(val, "link") == 0) {
					mtree->filetype = AE_IFLNK;
					break;
				}
			default:
				archive_set_error(&a->archive,
				    ARCHIVE_ERRNO_FILE_FORMAT,
				    "Unrecognized file type \"%s\"", val);
				return (ARCHIVE_WARN);
			}
			archive_entry_set_filetype(entry, mtree->filetype);
			break;
		}
		if (strcmp(key, "time") == 0) {
			archive_entry_set_mtime(entry, mtree_atol10(&val), 0);
			break;
		}
	case 'u':
		if (strcmp(key, "uid") == 0) {
			archive_entry_set_uid(entry, mtree_atol10(&val));
			break;
		}
		if (strcmp(key, "uname") == 0) {
			archive_entry_copy_uname(entry, val);
			break;
		}
	default:
		archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
		    "Unrecognized key %s=%s", key, val);
		return (ARCHIVE_WARN);
	}
	return (ARCHIVE_OK);
}

static int
read_data(struct archive_read *a, const void **buff, size_t *size, off_t *offset)
{
	ssize_t bytes_read;
	struct mtree *mtree;

	mtree = (struct mtree *)(a->format->data);
	if (mtree->fd < 0) {
		*buff = NULL;
		*offset = 0;
		*size = 0;
		return (ARCHIVE_EOF);
	}
	if (mtree->buff == NULL) {
		mtree->buffsize = 64 * 1024;
		mtree->buff = malloc(mtree->buffsize);
		if (mtree->buff == NULL) {
			archive_set_error(&a->archive, ENOMEM,
			    "Can't allocate memory");
		}
	}

	*buff = mtree->buff;
	*offset = mtree->offset;
	bytes_read = read(mtree->fd, mtree->buff, mtree->buffsize);
	if (bytes_read < 0) {
		archive_set_error(&a->archive, errno, "Can't read");
		return (ARCHIVE_WARN);
	}
	if (bytes_read == 0) {
		*size = 0;
		return (ARCHIVE_EOF);
	}
	mtree->offset += bytes_read;
	*size = (size_t)bytes_read;
	return (ARCHIVE_OK);
}

/* Skip does nothing except possibly close the contents file. */
static int
skip(struct archive_read *a)
{
	struct mtree *mtree;

	mtree = (struct mtree *)(a->format->data);
	if (mtree->fd >= 0) {
		close(mtree->fd);
		mtree->fd = -1;
	}
	return (ARCHIVE_OK);
}

/*
 * Since parsing octal escapes always makes strings shorter,
 * we can always do this conversion in-place.
 */
static void
parse_escapes(char *src, struct mtree_entry *mentry)
{
	char *dest = src;
	char c;

	while (*src != '\0') {
		c = *src++;
		if (c == '/' && mentry != NULL)
			mentry->full = 1;
		if (c == '\\') {
			if (src[0] >= '0' && src[0] <= '3'
			    && src[1] >= '0' && src[1] <= '7'
			    && src[2] >= '0' && src[2] <= '7') {
				c = (src[0] - '0') << 6;
				c |= (src[1] - '0') << 3;
				c |= (src[2] - '0');
				src += 3;
			}
		}
		*dest++ = c;
	}
	*dest = '\0';
}

/*
 * Note that this implementation does not (and should not!) obey
 * locale settings; you cannot simply substitute strtol here, since
 * it does obey locale.
 */
static int64_t
mtree_atol8(char **p)
{
	int64_t	l, limit, last_digit_limit;
	int digit, base;

	base = 8;
	limit = INT64_MAX / base;
	last_digit_limit = INT64_MAX % base;

	l = 0;
	digit = **p - '0';
	while (digit >= 0 && digit < base) {
		if (l>limit || (l == limit && digit > last_digit_limit)) {
			l = INT64_MAX; /* Truncate on overflow. */
			break;
		}
		l = (l * base) + digit;
		digit = *++(*p) - '0';
	}
	return (l);
}

/*
 * Note that this implementation does not (and should not!) obey
 * locale settings; you cannot simply substitute strtol here, since
 * it does obey locale.
 */
static int64_t
mtree_atol10(char **p)
{
	int64_t l, limit, last_digit_limit;
	int base, digit, sign;

	base = 10;
	limit = INT64_MAX / base;
	last_digit_limit = INT64_MAX % base;

	if (**p == '-') {
		sign = -1;
		++(*p);
	} else
		sign = 1;

	l = 0;
	digit = **p - '0';
	while (digit >= 0 && digit < base) {
		if (l > limit || (l == limit && digit > last_digit_limit)) {
			l = UINT64_MAX; /* Truncate on overflow. */
			break;
		}
		l = (l * base) + digit;
		digit = *++(*p) - '0';
	}
	return (sign < 0) ? -l : l;
}

/*
 * Returns length of line (including trailing newline)
 * or negative on error.  'start' argument is updated to
 * point to first character of line.
 */
static ssize_t
readline(struct archive_read *a, struct mtree *mtree, char **start, ssize_t limit)
{
	ssize_t bytes_read;
	ssize_t total_size = 0;
	const void *t;
	const char *s;
	void *p;

	/* Accumulate line in a line buffer. */
	for (;;) {
		/* Read some more. */
		bytes_read = (a->decompressor->read_ahead)(a, &t, 1);
		if (bytes_read == 0)
			return (0);
		if (bytes_read < 0)
			return (ARCHIVE_FATAL);
		s = t;  /* Start of line? */
		p = memchr(t, '\n', bytes_read);
		/* If we found '\n', trim the read. */
		if (p != NULL) {
			bytes_read = 1 + ((const char *)p) - s;
		}
		if (total_size + bytes_read + 1 > limit) {
			archive_set_error(&a->archive,
			    ARCHIVE_ERRNO_FILE_FORMAT,
			    "Line too long");
			return (ARCHIVE_FATAL);
		}
		if (archive_string_ensure(&mtree->line,
			total_size + bytes_read + 1) == NULL) {
			archive_set_error(&a->archive, ENOMEM,
			    "Can't allocate working buffer");
			return (ARCHIVE_FATAL);
		}
		memcpy(mtree->line.s + total_size, t, bytes_read);
		(a->decompressor->consume)(a, bytes_read);
		total_size += bytes_read;
		/* Null terminate. */
		mtree->line.s[total_size] = '\0';
		/* If we found '\n', clean up and return. */
		if (p != NULL) {
			*start = mtree->line.s;
			return (total_size);
		}
	}
}
OpenPOWER on IntegriCloud