archive.c 8.91 KB
Newer Older
1 2 3 4 5 6
/* archive.c - Alpine Package Keeper (APK)
 *
 * Copyright (C) 2005-2008 Natanael Copa <n@tanael.org>
 * Copyright (C) 2008 Timo Teräs <timo.teras@iki.fi>
 * All rights reserved.
 *
7
 * This program is free software; you can redistribute it and/or modify it
8 9 10 11 12 13 14 15 16
 * under the terms of the GNU General Public License version 2 as published
 * by the Free Software Foundation. See http://www.gnu.org/ for details.
 */

#include <stdio.h>

#include <err.h>
#include <errno.h>
#include <fcntl.h>
17
#include <utime.h>
18 19 20 21 22
#include <malloc.h>
#include <string.h>
#include <unistd.h>
#include <sysexits.h>
#include <sys/wait.h>
23
#include <limits.h>
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47

#include "apk_defines.h"
#include "apk_archive.h"

struct tar_header {
	/* ustar header, Posix 1003.1 */
	char name[100];     /*   0-99 */
	char mode[8];       /* 100-107 */
	char uid[8];        /* 108-115 */
	char gid[8];        /* 116-123 */
	char size[12];      /* 124-135 */
	char mtime[12];     /* 136-147 */
	char chksum[8];     /* 148-155 */
	char typeflag;      /* 156-156 */
	char linkname[100]; /* 157-256 */
	char magic[8];      /* 257-264 */
	char uname[32];     /* 265-296 */
	char gname[32];     /* 297-328 */
	char devmajor[8];   /* 329-336 */
	char devminor[8];   /* 337-344 */
	char prefix[155];   /* 345-499 */
	char padding[12];   /* 500-512 */
};

48 49 50 51 52 53 54
struct apk_tar_digest_info {
	char id[4];
	uint16_t nid;
	uint16_t size;
	char digest[];
};

Timo Teräs's avatar
Timo Teräs committed
55 56 57
#define GET_OCTAL(s)	get_octal(s, sizeof(s))
#define PUT_OCTAL(s,v)	put_octal(s, sizeof(s), v)

58 59 60
static int get_octal(char *s, size_t l)
{
	apk_blob_t b = APK_BLOB_PTR_LEN(s, l);
61
	return apk_blob_pull_uint(&b, 8);
62
}
63

Timo Teräs's avatar
Timo Teräs committed
64 65 66 67 68 69 70 71 72 73 74 75 76
static void put_octal(char *s, size_t l, size_t value)
{
	char *ptr = &s[l - 1];

	*(ptr--) = '\0';
	while (value != 0 && ptr >= s) {
		*(ptr--) = '0' + (value % 8);
		value /= 8;
	}
	while (ptr >= s)
		*(ptr--) = '0';
}

77 78 79 80
struct apk_tar_entry_istream {
	struct apk_istream is;
	struct apk_istream *tar_is;
	size_t bytes_left;
Timo Teräs's avatar
Timo Teräs committed
81 82
	EVP_MD_CTX mdctx;
	struct apk_checksum *csum;
83
};
84

85
static ssize_t tar_entry_read(void *stream, void *ptr, size_t size)
86
{
87 88
	struct apk_tar_entry_istream *teis =
		container_of(stream, struct apk_tar_entry_istream, is);
89
	ssize_t r;
90 91 92

	if (size > teis->bytes_left)
		size = teis->bytes_left;
93 94 95
        if (size == 0)
                return 0;

96 97 98
	r = teis->tar_is->read(teis->tar_is, ptr, size);
	if (r < 0)
		return r;
99

100
	teis->bytes_left -= r;
101
	if (teis->csum == NULL)
102
		return r;
103

104
	EVP_DigestUpdate(&teis->mdctx, ptr, r);
105 106 107
	if (teis->bytes_left == 0) {
		teis->csum->type = EVP_MD_CTX_size(&teis->mdctx);
		EVP_DigestFinal_ex(&teis->mdctx, teis->csum->data, NULL);
108
	}
109
	return r;
110 111
}

112 113 114 115
static void tar_entry_close(void *stream)
{
}

116
int apk_tar_parse(struct apk_istream *is, apk_archive_entry_parser parser,
117
		  void *ctx, int soft_checksums)
118
{
119
	struct apk_file_info entry;
120 121
	struct apk_tar_entry_istream teis = {
		.is.read = tar_entry_read,
122
		.is.close = tar_entry_close,
123 124
		.tar_is = is,
	};
125
	struct tar_header buf;
126
	struct apk_tar_digest_info *di;
127 128
	unsigned long offset = 0;
	int end = 0, r;
129
	size_t toskip;
130

131
	di = (struct apk_tar_digest_info *) &buf.linkname[3];
Timo Teräs's avatar
Timo Teräs committed
132
	EVP_MD_CTX_init(&teis.mdctx);
133
	memset(&entry, 0, sizeof(entry));
134
	while ((r = is->read(is, &buf, 512)) == 512) {
135 136
		offset += 512;
		if (buf.name[0] == '\0') {
137 138
			if (end)
				break;
139 140 141 142
			end++;
			continue;
		}

143
		entry = (struct apk_file_info){
144 145 146
			.size  = GET_OCTAL(buf.size),
			.uid   = GET_OCTAL(buf.uid),
			.gid   = GET_OCTAL(buf.gid),
147
			.mode  = GET_OCTAL(buf.mode) & 07777,
148 149 150 151 152 153 154
			.mtime = GET_OCTAL(buf.mtime),
			.name  = entry.name,
			.uname = buf.uname,
			.gname = buf.gname,
			.device = makedev(GET_OCTAL(buf.devmajor),
					  GET_OCTAL(buf.devminor)),
		};
155
		teis.csum = NULL;
156 157 158 159 160 161

		switch (buf.typeflag) {
		case 'L':
			if (entry.name != NULL)
				free(entry.name);
			entry.name = malloc(entry.size+1);
162
			is->read(is, entry.name, entry.size);
Timo Teräs's avatar
Timo Teräs committed
163
			entry.name[entry.size] = 0;
164 165 166 167 168 169
			offset += entry.size;
			entry.size = 0;
			break;
		case '0':
		case '7': /* regular file */
			entry.mode |= S_IFREG;
170 171 172 173
			if (memcmp(di->id, "APK2", 4) == 0 &&
			    di->size <= sizeof(entry.csum.data)) {
				entry.csum.type = di->size;
				memcpy(entry.csum.data, di->digest, di->size);
174
			} else if (soft_checksums) {
175 176
				teis.csum = &entry.csum;
			}
177 178 179 180 181 182 183 184 185 186 187 188
			break;
		case '1': /* hard link */
			entry.mode |= S_IFREG;
			entry.link_target = buf.linkname;
			break;
		case '2': /* symbolic link */
			entry.mode |= S_IFLNK;
			entry.link_target = buf.linkname;
			break;
		case '3': /* char device */
			entry.mode |= S_IFCHR;
			break;
Timo Teräs's avatar
Timo Teräs committed
189
		case '4': /* block device */
190 191 192 193 194 195 196 197 198
			entry.mode |= S_IFBLK;
			break;
		case '5': /* directory */
			entry.mode |= S_IFDIR;
			break;
		default:
			break;
		}

199
		teis.bytes_left = entry.size;
200 201 202 203 204
		if (entry.mode & S_IFMT) {
			if (entry.name == NULL)
				entry.name = strdup(buf.name);

			/* callback parser function */
205 206 207 208
			if (teis.csum != NULL)
				EVP_DigestInit_ex(&teis.mdctx,
						  apk_default_checksum(), NULL);

209
			r = parser(ctx, &entry, &teis.is);
Timo Teräs's avatar
Timo Teräs committed
210
			free(entry.name);
211
			if (r != 0)
Timo Teräs's avatar
Timo Teräs committed
212
				goto err;
213 214 215 216

			entry.name = NULL;
		}

217 218 219 220 221 222 223 224
		offset += entry.size - teis.bytes_left;
		toskip = teis.bytes_left;
		if ((offset + toskip) & 511)
			toskip += 512 - ((offset + toskip) & 511);
		offset += toskip;
		if (toskip != 0)
			is->read(is, NULL, toskip);
	}
Timo Teräs's avatar
Timo Teräs committed
225
	EVP_MD_CTX_cleanup(&teis.mdctx);
226

227 228 229
	/* Read remaining end-of-archive records, to ensure we read all of
	 * the file. The underlying istream is likely doing checksumming. */
	if (r == 512) {
Timo Teräs's avatar
Timo Teräs committed
230
		while ((r = is->read(is, &buf, 512)) == 512) {
231
			if (buf.name[0] != 0)
232
				return -EBADMSG;
Timo Teräs's avatar
Timo Teräs committed
233
		}
234 235
	}

236
	/* Check that there was no partial record */
237
	if (r > 0)
238
		r = -EBADMSG;
239

240
	return r;
Timo Teräs's avatar
Timo Teräs committed
241 242 243 244 245 246

err:
	EVP_MD_CTX_cleanup(&teis.mdctx);
	return r;
}

247
int apk_tar_write_entry(struct apk_ostream *os, const struct apk_file_info *ae, char *data)
Timo Teräs's avatar
Timo Teräs committed
248 249 250 251 252 253 254 255 256 257 258 259 260
{
	struct tar_header buf;

	memset(&buf, 0, sizeof(buf));
	if (ae != NULL) {
		const unsigned char *src;
	        int chksum, i;

		if (S_ISREG(ae->mode))
			buf.typeflag = '0';
		else
			return -1;

261 262 263 264 265
		if (ae->name != NULL)
			strncpy(buf.name, ae->name, sizeof(buf.name));

		strncpy(buf.uname, ae->uname ?: "root", sizeof(buf.uname));
		strncpy(buf.gname, ae->gname ?: "root", sizeof(buf.gname));
Timo Teräs's avatar
Timo Teräs committed
266 267 268 269 270

		PUT_OCTAL(buf.size, ae->size);
		PUT_OCTAL(buf.uid, ae->uid);
		PUT_OCTAL(buf.gid, ae->gid);
		PUT_OCTAL(buf.mode, ae->mode & 07777);
271
		PUT_OCTAL(buf.mtime, ae->mtime ?: time(NULL));
Timo Teräs's avatar
Timo Teräs committed
272 273 274 275 276 277 278 279 280 281 282 283 284

		/* Checksum */
		strcpy(buf.magic, "ustar  ");
		memset(buf.chksum, ' ', sizeof(buf.chksum));
		src = (const unsigned char *) &buf;
		for (i = chksum = 0; i < sizeof(buf); i++)
			chksum += src[i];
	        put_octal(buf.chksum, sizeof(buf.chksum)-1, chksum);
	}

	if (os->write(os, &buf, sizeof(buf)) != sizeof(buf))
		return -1;

285 286 287 288 289
	if (ae == NULL) {
		/* End-of-archive is two empty headers */
		if (os->write(os, &buf, sizeof(buf)) != sizeof(buf))
			return -1;
	} else if (data != NULL) {
Timo Teräs's avatar
Timo Teräs committed
290 291
		if (os->write(os, data, ae->size) != ae->size)
			return -1;
292
		if (apk_tar_write_padding(os, ae) != 0)
Timo Teräs's avatar
Timo Teräs committed
293 294 295
			return -1;
	}

296 297 298
	return 0;
}

299 300 301 302 303 304 305 306 307 308 309 310 311
int apk_tar_write_padding(struct apk_ostream *os, const struct apk_file_info *ae)
{
	static char padding[512];
	int pad;

	pad = 512 - (ae->size & 511);
	if (pad != 512 &&
	    os->write(os, padding, pad) != pad)
		return -1;

	return 0;
}

312
int apk_archive_entry_extract(const struct apk_file_info *ae,
313
			      struct apk_istream *is,
314 315
			      const char *fn, apk_progress_cb cb,
			      void *cb_ctx)
316
{
317
	struct utimbuf utb;
318
	int r = -1, fd;
319 320 321 322 323 324 325 326 327

	if (fn == NULL)
		fn = ae->name;

	/* BIG HONKING FIXME */
	unlink(fn);

	switch (ae->mode & S_IFMT) {
	case S_IFDIR:
328
		r = mkdir(fn, ae->mode & 07777);
329 330 331 332 333
		if (r < 0 && errno == EEXIST)
			r = 0;
		break;
	case S_IFREG:
		if (ae->link_target == NULL) {
334
			fd = open(fn, O_RDWR | O_CREAT, ae->mode & 07777);
335 336
			if (fd < 0) {
				r = -1;
337
				break;
338
			}
339 340
			if (apk_istream_splice(is, fd, ae->size, cb, cb_ctx)
			    == ae->size)
341 342
				r = 0;
			close(fd);
343
		} else {
344 345 346 347
			char link_target[PATH_MAX];
			snprintf(link_target, sizeof(link_target),
				 "%s.apk-new", ae->link_target);
			r = link(link_target, fn);
348 349 350 351 352 353 354 355 356
		}
		break;
	case S_IFLNK:
		r = symlink(ae->link_target, fn);
		break;
	case S_IFSOCK:
	case S_IFBLK:
	case S_IFCHR:
	case S_IFIFO:
357
		r = mknod(fn, ae->mode & 07777, ae->device);
358 359
		break;
	}
360 361 362 363 364
	if (r == 0) {
		if (!S_ISLNK(ae->mode))
			r = chown(fn, ae->uid, ae->gid);
		else
			r = lchown(fn, ae->uid, ae->gid);
365 366 367 368 369 370
		if (r < 0) {
			apk_error("Failed to set ownership on %s: %s",
				  fn, strerror(errno));
			return -errno;
		}

371
		/* chown resets suid bit so we need set it again */
372
		if (ae->mode & 07000) {
373
			r = chmod(fn, ae->mode & 07777);
374 375 376 377 378 379 380 381
			if (r < 0) {
				apk_error("Failed to set file permissions "
					  "on %s: %s",
					  fn, strerror(errno));
				return -errno;
			}
		}

382 383 384 385 386 387 388 389 390
		if (!S_ISLNK(ae->mode)) {
			/* preserve modification time */
			utb.actime = utb.modtime = ae->mtime;
			r = utime(fn, &utb);
			if (r < 0) {
				apk_error("Failed to preserve modification time on %s: %s",
					fn, strerror(errno));
				return -errno;
			}
391
		}
392
	} else {
393 394
		apk_error("Failed to extract %s: %s", ae->name, strerror(errno));
		return -errno;
395
	}
396
	return 0;
397
}