]> git.cameronkatri.com Git - trustcache.git/blob - remove.c
Add support for new version 2 trustcaches
[trustcache.git] / remove.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 #include <errno.h>
29 #include <getopt.h>
30 #include <stdbool.h>
31 #include <stdint.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35
36 #include "trustcache.h"
37 #include "uuid/uuid.h"
38
39 int
40 tcremove(int argc, char **argv)
41 {
42 bool keepuuid = false;
43 int numremoved = 0;
44
45 int ch;
46 while ((ch = getopt(argc, argv, "k")) != -1) {
47 switch (ch) {
48 case 'k':
49 keepuuid = true;
50 break;
51 }
52 }
53
54 argc -= optind;
55 argv += optind;
56
57 if (argc < 2)
58 return -1;
59
60 FILE *f = NULL;
61 struct trust_cache cache = opentrustcache(argv[0]);
62
63 if (!keepuuid)
64 uuid_generate(cache.uuid);
65
66 uint8_t hash[CS_CDHASH_LEN] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
67
68 for (int i = 1; i < argc; i++) {
69 if (strlen(argv[i]) != 40) {
70 fprintf(stderr, "%s is not a valid CDHash\n", argv[i]);
71 exit(1);
72 }
73 for (size_t j = 0; j < CS_CDHASH_LEN; j++)
74 sscanf(argv[i] + 2 * j, "%02hhx", &hash[j]);
75
76 uint32_t j = 0;
77 while (j < cache.num_entries) {
78 if (cache.version == 0) {
79 if (memcmp(cache.hashes[j], hash, CS_CDHASH_LEN) == 0) {
80 memmove(&cache.hashes[j], &cache.hashes[j + 1], (cache.num_entries - j - 1) * sizeof(trust_cache_hash0));
81 cache.num_entries--;
82 numremoved++;
83 continue;
84 }
85 } else if (cache.version == 1) {
86 if (memcmp(cache.entries[j].cdhash, hash, CS_CDHASH_LEN) == 0) {
87 memmove(&cache.entries[j], &cache.entries[j + 1], (cache.num_entries - j - 1) * sizeof(struct trust_cache_entry1));
88 cache.num_entries--;
89 numremoved++;
90 continue;
91 }
92 } else if (cache.version == 2) {
93 if (memcmp(cache.entries2[j].cdhash, hash, CS_CDHASH_LEN) == 0) {
94 memmove(&cache.entries2[j], &cache.entries2[j + 1], (cache.num_entries - j - 1) * sizeof(struct trust_cache_entry2));
95 cache.num_entries--;
96 numremoved++;
97 continue;
98 }
99 }
100 j++;
101 }
102 for (size_t j = 0; j < CS_CDHASH_LEN; j++)
103 hash[j] = 0;
104 }
105
106 if (writetrustcache(cache, argv[0]) == -1)
107 return 1;
108
109 free(cache.entries);
110
111 printf("Removed %i %s\n", numremoved, numremoved == 1 ? "entry" : "entries");
112
113 return 0;
114 }