]> git.cameronkatri.com Git - trustcache.git/blob - info.c
Add DESTDIR support
[trustcache.git] / info.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 <getopt.h>
29 #include <stdbool.h>
30 #include <stdint.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33
34 #include "trustcache.h"
35
36 #include "compat.h"
37
38 int
39 tcinfo(int argc, char **argv)
40 {
41 struct trust_cache cache;
42 bool headeronly = false, onlyhash = false;
43 uint32_t entrynum = 0;
44 const char *errstr = NULL;
45
46 int ch;
47 while ((ch = getopt(argc, argv, "che:")) != -1) {
48 switch (ch) {
49 case 'h':
50 headeronly = true;
51 break;
52 case 'e':
53 entrynum = strtonum(optarg, 1, UINT32_MAX, &errstr);
54 if (errstr != NULL) {
55 fprintf(stderr, "entry number is %s: %s\n", errstr, optarg);
56 exit(1);
57 }
58 break;
59 case 'c':
60 onlyhash = true;
61 break;
62 }
63 }
64
65 argc -= optind;
66 argv += optind;
67
68 if (argc == 0)
69 return -1;
70
71 cache = opentrustcache(argv[0]);
72
73 if (entrynum == 0 && !onlyhash)
74 print_header(cache);
75 if (!headeronly) {
76 if (onlyhash) {
77 for (uint32_t i = 0; i < cache.num_entries; i++) {
78 if (cache.version == 0)
79 print_hash(cache.hashes[i], true);
80 else
81 print_hash(cache.entries[i].cdhash, true);
82 }
83 goto done;
84 }
85 if (entrynum != 0) {
86 if (entrynum > cache.num_entries) {
87 fprintf(stderr, "no entry %i\n", entrynum);
88 exit(1);
89 }
90 if (cache.version == 1) {
91 print_entry(cache.entries[entrynum - 1]);
92 } else if (cache.version == 0) {
93 print_hash(cache.hashes[entrynum - 1], true);
94 }
95 } else {
96 print_entries(cache);
97 }
98 }
99
100 done:
101 free(cache.entries);
102
103 return 0;
104 }
105
106 void
107 print_header(struct trust_cache cache)
108 {
109 printf("version = %i\n", cache.version);
110 char out[37];
111 uuid_unparse(cache.uuid, out);
112 printf("uuid = %s\n", out);
113 printf("entry count = %i\n", cache.num_entries);
114 }
115
116 void
117 print_entries(struct trust_cache cache)
118 {
119 for (uint32_t i = 0; i < cache.num_entries; i++) {
120 if (cache.version == 0)
121 print_hash(cache.hashes[i], true);
122 else if (cache.version == 1)
123 print_entry(cache.entries[i]);
124 }
125 }
126
127 void
128 print_entry(struct trust_cache_entry1 entry)
129 {
130 print_hash(entry.cdhash, false);
131
132 switch (entry.flags) {
133 case CS_TRUST_CACHE_AMFID:
134 printf(" CS_TRUST_CACHE_AMFID ");
135 break;
136 case CS_TRUST_CACHE_ANE:
137 printf(" CS_TRUST_CACHE_ANE ");
138 break;
139 case CS_TRUST_CACHE_AMFID|CS_TRUST_CACHE_ANE:
140 printf(" CS_TRUST_CACHE_AMFID|CS_TRUST_CACHE_ANE ");
141 break;
142 case 0:
143 printf(" [none] ");
144 break;
145 default:
146 printf(" [%i] ", entry.flags);
147 break;
148 }
149
150 printf("[%i]\n", entry.hash_type);
151 }
152
153 void
154 print_hash(uint8_t cdhash[CS_CDHASH_LEN], bool newline)
155 {
156 for (size_t j = 0; j < CS_CDHASH_LEN; j++) {
157 printf("%02x", cdhash[j]);
158 }
159 if (newline)
160 printf("\n");
161 }