Blame


1 c36d43ac 2021-04-25 op /*
2 c36d43ac 2021-04-25 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
3 c36d43ac 2021-04-25 op *
4 c36d43ac 2021-04-25 op * Permission to use, copy, modify, and distribute this software for any
5 c36d43ac 2021-04-25 op * purpose with or without fee is hereby granted, provided that the above
6 c36d43ac 2021-04-25 op * copyright notice and this permission notice appear in all copies.
7 c36d43ac 2021-04-25 op *
8 c36d43ac 2021-04-25 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 c36d43ac 2021-04-25 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 c36d43ac 2021-04-25 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 c36d43ac 2021-04-25 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 c36d43ac 2021-04-25 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 c36d43ac 2021-04-25 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 c36d43ac 2021-04-25 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 c36d43ac 2021-04-25 op */
16 c36d43ac 2021-04-25 op
17 036c104e 2022-01-11 op #include "compat.h"
18 c36d43ac 2021-04-25 op
19 c36d43ac 2021-04-25 op #include <stdlib.h>
20 c36d43ac 2021-04-25 op #include <string.h>
21 c36d43ac 2021-04-25 op
22 036c104e 2022-01-11 op #include "telescope.h"
23 9d65b1d9 2022-01-11 op #include "utils.h"
24 c36d43ac 2021-04-25 op
25 c36d43ac 2021-04-25 op void
26 c36d43ac 2021-04-25 op tofu_init(struct ohash *h, unsigned int sz, ptrdiff_t ko)
27 c36d43ac 2021-04-25 op {
28 c36d43ac 2021-04-25 op struct ohash_info info = {
29 c36d43ac 2021-04-25 op .key_offset = ko,
30 c36d43ac 2021-04-25 op .calloc = hash_calloc,
31 c36d43ac 2021-04-25 op .free = hash_free,
32 c36d43ac 2021-04-25 op .alloc = hash_alloc,
33 c36d43ac 2021-04-25 op };
34 c36d43ac 2021-04-25 op
35 c36d43ac 2021-04-25 op ohash_init(h, sz, &info);
36 c36d43ac 2021-04-25 op }
37 c36d43ac 2021-04-25 op
38 c36d43ac 2021-04-25 op struct tofu_entry *
39 c36d43ac 2021-04-25 op tofu_lookup(struct ohash *h, const char *domain, const char *port)
40 c36d43ac 2021-04-25 op {
41 c36d43ac 2021-04-25 op char buf[GEMINI_URL_LEN];
42 c36d43ac 2021-04-25 op unsigned int slot;
43 c36d43ac 2021-04-25 op
44 c36d43ac 2021-04-25 op strlcpy(buf, domain, sizeof(buf));
45 c36d43ac 2021-04-25 op if (port != NULL && *port != '\0' && strcmp(port, "1965")) {
46 c36d43ac 2021-04-25 op strlcat(buf, ":", sizeof(buf));
47 c36d43ac 2021-04-25 op strlcat(buf, port, sizeof(buf));
48 c36d43ac 2021-04-25 op }
49 c36d43ac 2021-04-25 op
50 c36d43ac 2021-04-25 op slot = ohash_qlookup(h, buf);
51 c36d43ac 2021-04-25 op return ohash_find(h, slot);
52 c36d43ac 2021-04-25 op }
53 c36d43ac 2021-04-25 op
54 c36d43ac 2021-04-25 op void
55 c36d43ac 2021-04-25 op tofu_add(struct ohash *h, struct tofu_entry *e)
56 c36d43ac 2021-04-25 op {
57 c36d43ac 2021-04-25 op unsigned int slot;
58 c36d43ac 2021-04-25 op
59 c36d43ac 2021-04-25 op slot = ohash_qlookup(h, e->domain);
60 c36d43ac 2021-04-25 op ohash_insert(h, slot, e);
61 c36d43ac 2021-04-25 op }
62 288fd238 2021-04-25 op
63 288fd238 2021-04-25 op void
64 288fd238 2021-04-25 op tofu_update(struct ohash *h, struct tofu_entry *e)
65 288fd238 2021-04-25 op {
66 288fd238 2021-04-25 op struct tofu_entry *t;
67 288fd238 2021-04-25 op
68 288fd238 2021-04-25 op if ((t = tofu_lookup(h, e->domain, NULL)) == NULL)
69 288fd238 2021-04-25 op tofu_add(h, e);
70 288fd238 2021-04-25 op else {
71 288fd238 2021-04-25 op strlcpy(t->hash, e->hash, sizeof(t->hash));
72 288fd238 2021-04-25 op t->verified = e->verified;
73 288fd238 2021-04-25 op free(e);
74 288fd238 2021-04-25 op }
75 288fd238 2021-04-25 op }
76 a2fd3805 2021-07-06 op
77 a2fd3805 2021-07-06 op void
78 a2fd3805 2021-07-06 op tofu_temp_trust(struct ohash *h, const char *host, const char *port,
79 a2fd3805 2021-07-06 op const char *hash)
80 a2fd3805 2021-07-06 op {
81 a2fd3805 2021-07-06 op struct tofu_entry *e;
82 a2fd3805 2021-07-06 op
83 a2fd3805 2021-07-06 op if ((e = calloc(1, sizeof(*e))) == NULL)
84 95a8c791 2021-08-26 op abort();
85 a2fd3805 2021-07-06 op
86 95a8c791 2021-08-26 op strlcpy(e->domain, host, sizeof(e->domain));
87 a2fd3805 2021-07-06 op if (*port != '\0' && strcmp(port, "1965")) {
88 a2fd3805 2021-07-06 op strlcat(e->domain, ":", sizeof(e->domain));
89 a2fd3805 2021-07-06 op strlcat(e->domain, port, sizeof(e->domain));
90 a2fd3805 2021-07-06 op }
91 a2fd3805 2021-07-06 op strlcpy(e->hash, hash, sizeof(e->hash));
92 a2fd3805 2021-07-06 op e->verified = -1;
93 a2fd3805 2021-07-06 op
94 a2fd3805 2021-07-06 op tofu_update(h, e);
95 a2fd3805 2021-07-06 op }