]> git.cameronkatri.com Git - trustcache.git/blob - tc.c
Add actions and artifacts
[trustcache.git] / tc.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 <limits.h>
31 #include <stdbool.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <sysexits.h>
36
37 #include "trustcache.h"
38
39 int
40 main(int argc, char **argv)
41 {
42 if (argc < 2) {
43 help:
44 fprintf(stderr, "Usage: tc append [-f flags] [-u uuid | 0] infile file ...\n"
45 " tc create [-u uuid] [-v version] outfile file ...\n"
46 " tc info [-c] [-h] [-e entrynum] file\n"
47 " tc remove [-k] file hash ...\n\n"
48 "See tc(1) for more information\n");
49 exit(1);
50 }
51
52 int ret = 1;
53
54 if (strcmp(argv[1], "info") == 0)
55 ret = tcinfo(argc - 1, argv + 1);
56 else if (strcmp(argv[1], "create") == 0)
57 ret = tccreate(argc - 1, argv + 1);
58 else if (strcmp(argv[1], "append") == 0)
59 ret = tcappend(argc - 1, argv + 1);
60 else if (strcmp(argv[1], "remove") == 0)
61 ret = tcremove(argc - 1, argv + 1);
62 else
63 fprintf(stderr, "Unknown subcommand %s\n", argv[1]);
64
65 if (ret == -1)
66 goto help;
67
68 return ret;
69 }
70
71 struct trust_cache
72 opentrustcache(const char *path)
73 {
74 FILE *f;
75 struct trust_cache cache;
76
77 if ((f = fopen(path, "r")) == NULL) {
78 fprintf(stderr, "%s: %s\n", path, strerror(errno));
79 exit(1);
80 }
81
82 fread(&cache, sizeof(struct trust_cache) - sizeof(struct trust_cache_entry1*), 1, f);
83 cache.version = le32toh(cache.version);
84 cache.num_entries = le32toh(cache.num_entries);
85
86 if (cache.version == 0) {
87 if ((cache.hashes = calloc(cache.num_entries, sizeof(trust_cache_hash0))) == NULL)
88 exit(EX_OSERR);
89 fread(cache.hashes, sizeof(trust_cache_hash0), cache.num_entries, f);
90 } else if (cache.version == 1) {
91 if ((cache.entries = calloc(cache.num_entries, sizeof(struct trust_cache_entry1))) == NULL)
92 exit(EX_OSERR);
93 fread(cache.entries, sizeof(struct trust_cache_entry1), cache.num_entries, f);
94 } else {
95 fprintf(stderr, "%s: Unsupported version %i\n", path, cache.version);
96 exit(1);
97 }
98
99 fclose(f);
100 return cache;
101 }