]> git.cameronkatri.com Git - trustcache.git/blob - cache_from_tree.c
Add support for new version 2 trustcaches
[trustcache.git] / cache_from_tree.c
1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2022 Cameron Katri. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY CAMERON KATRI AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL CAMERON KATRI OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28 #define _XOPEN_SOURCE 500
29 #include <errno.h>
30 #include <ftw.h>
31 #include <stdio.h>
32 #include <string.h>
33
34 #include "trustcache.h"
35 #include "machoparse/cdhash.h"
36
37 static struct trust_cache cache = {};
38
39 static int
40 tccallback(const char *path, const struct stat *sb, __attribute__((unused)) int typeflag, __attribute__((unused)) struct FTW *ftw)
41 {
42 if (!S_ISREG(sb->st_mode))
43 return 0;
44
45 struct cdhashes c = {};
46 c.count = 0;
47 find_cdhash(path, sb, &c);
48
49 if (c.count == 0)
50 return 0;
51
52 for (int i = 0; i < c.count; i++) {
53 if (cache.version == 0) {
54 if ((cache.hashes = realloc(cache.hashes, sizeof(trust_cache_hash0) * (cache.num_entries + 1))) == NULL)
55 exit(1);
56 memcpy(cache.hashes[cache.num_entries], c.h[i].cdhash, CS_CDHASH_LEN);
57 } else if (cache.version == 1) {
58 if ((cache.entries = realloc(cache.entries, sizeof(struct trust_cache_entry1) * (cache.num_entries + 1))) == NULL)
59 exit(1);
60 cache.entries[cache.num_entries].hash_type = c.h[i].hash_type;
61 cache.entries[cache.num_entries].flags = 0;
62 memcpy(cache.entries[cache.num_entries].cdhash, c.h[i].cdhash, CS_CDHASH_LEN);
63 } else if (cache.version == 2) {
64 if ((cache.entries2 = realloc(cache.entries, sizeof(struct trust_cache_entry2) * (cache.num_entries + 1))) == NULL)
65 exit(1);
66 cache.entries2[cache.num_entries].hash_type = c.h[i].hash_type;
67 cache.entries2[cache.num_entries].category = 0;
68 memcpy(cache.entries2[cache.num_entries].cdhash, c.h[i].cdhash, CS_CDHASH_LEN);
69 }
70 cache.num_entries++;
71 }
72
73 free(c.h);
74
75 return 0;
76 }
77
78 struct trust_cache
79 cache_from_tree(const char *path, uint32_t version)
80 {
81 struct trust_cache ret = {};
82 cache.version = version;
83 cache.num_entries = 0;
84 ret.version = version;
85
86 if (nftw(path, tccallback, 20, 0) == -1) {
87 // on macOS, nftw(3) will fail if the path is not a directory, but we don't want that
88 if (errno == ENOTDIR) {
89 struct stat sb;
90 if (stat(path, &sb) == 0 && tccallback(path, &sb, 0, NULL) == 0)
91 goto done;
92 }
93 perror("nftw");
94 return ret;
95 }
96
97 done:
98 ret.num_entries = cache.num_entries;
99 ret.hashes = cache.hashes;
100 return ret;
101 }