aboutsummaryrefslogtreecommitdiffstats
path: root/src/manifest.c
blob: 1106a74e9a9635139aae1f81db48e08008c80c88 (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
/* manifest.c - Alpine Package Keeper (APK)
 *
 * Copyright (C) 2005-2017 Natanael Copa <n@tanael.org>
 * Copyright (C) 2008-2017 Timo Teräs <timo.teras@iki.fi>
 * Copyright (C) 2017 William Pitcock <nenolod@dereferenced.org>
 * All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify it
 * 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 <sys/stat.h>

#include "apk_defines.h"
#include "apk_applet.h"
#include "apk_database.h"
#include "apk_version.h"
#include "apk_print.h"

/* TODO: support package files as well as generating manifest from the installed DB. */
static char *csum_types[APK_CHECKSUM_SHA1 + 1] = {
	/* Note: if adding new algorithms, update apk-manifest(8) */
	[APK_CHECKSUM_MD5] = "md5",
	[APK_CHECKSUM_SHA1] = "sha1",
};

struct manifest_file_ctx {
	const char *file;
	struct apk_sign_ctx *sctx;
};

static void process_package(struct apk_database *db, struct apk_package *pkg)
{
	struct apk_installed_package *ipkg = pkg->ipkg;
	struct apk_db_dir_instance *diri;
	struct apk_db_file *file;
	struct hlist_node *dc, *dn, *fc, *fn;
	char csum_buf[APK_BLOB_CHECKSUM_BUF];

	if (ipkg == NULL)
		return;

	hlist_for_each_entry_safe(diri, dc, dn, &ipkg->owned_dirs,
				  pkg_dirs_list) {
		hlist_for_each_entry_safe(file, fc, fn, &diri->owned_files,
					  diri_files_list) {
			apk_blob_t csum_blob = APK_BLOB_BUF(csum_buf);

			memset(csum_buf, '\0', sizeof(csum_buf));
			apk_blob_push_hexdump(&csum_blob, APK_BLOB_CSUM(file->csum));

			if (apk_verbosity > 1)
				printf("%s: ", pkg->name->name);

                        printf("%s:%s  " DIR_FILE_FMT "\n", csum_types[file->csum.type], csum_buf, DIR_FILE_PRINTF(diri->dir, file));
                }
	}
}

static int read_file_entry(void *ctx, const struct apk_file_info *ae,
                           struct apk_istream *is)
{
	struct manifest_file_ctx *mctx = ctx;
	char csum_buf[APK_BLOB_CHECKSUM_BUF];
	apk_blob_t csum_blob = APK_BLOB_BUF(csum_buf);
	int r;

	r = apk_sign_ctx_verify_tar(mctx->sctx, ae, is);
	if (r != 0)
		return r;

	if (!mctx->sctx->data_started)
		return 0;

	if ((ae->mode & S_IFMT) != S_IFREG)
		return 0;

	memset(csum_buf, '\0', sizeof(csum_buf));
	apk_blob_push_hexdump(&csum_blob, APK_BLOB_CSUM(ae->csum));

	if (apk_verbosity > 1)
		printf("%s: ", mctx->file);

	printf("%s:%s  %s\n", csum_types[ae->csum.type], csum_buf, ae->name);

	return 0;
}

static void process_file(struct apk_database *db, const char *match)
{
	struct apk_sign_ctx sctx;
	struct manifest_file_ctx ctx = {match, &sctx};
	int r;

	apk_sign_ctx_init(&sctx, APK_SIGN_VERIFY, NULL, db->keys_fd);
	r = apk_tar_parse(
		apk_istream_gunzip_mpart(apk_istream_from_file(AT_FDCWD, match), apk_sign_ctx_mpart_cb, &sctx),
		read_file_entry, &ctx, &db->id_cache);
	apk_sign_ctx_free(&sctx);
	if (r < 0) apk_error("%s: %s", match, apk_error_str(r));
}

static void process_match(struct apk_database *db, const char *match, struct apk_name *name, void *ctx)
{
	struct apk_provider *p;

	if (name == NULL)
	{
		process_file(db, match);
		return;
	}

	foreach_array_item(p, name->providers)
		process_package(db, p->pkg);
}

static int manifest_main(void *ctx, struct apk_database *db, struct apk_string_array *args)
{
	apk_name_foreach_matching(db, args, apk_foreach_genid(), process_match, NULL);
	return 0;
}

static struct apk_applet apk_manifest = {
	.name = "manifest",
	.arguments = "PACKAGE...",
	.open_flags = APK_OPENF_READ,
	.command_groups = APK_COMMAND_GROUP_REPO,
	.main = manifest_main,
};

APK_DEFINE_APPLET(apk_manifest);