Blame


1 c36d43ac 2021-04-25 op /*
2 4fa88f57 2022-04-24 op * Copyright (c) 2021, 2022 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 4fa88f57 2022-04-24 op #include <limits.h>
20 c36d43ac 2021-04-25 op #include <stdlib.h>
21 c36d43ac 2021-04-25 op #include <string.h>
22 3078e1bc 2022-04-24 op #include <unistd.h>
23 c36d43ac 2021-04-25 op
24 4fa88f57 2022-04-24 op #include "fs.h"
25 036c104e 2022-01-11 op #include "telescope.h"
26 9d65b1d9 2022-01-11 op #include "utils.h"
27 c36d43ac 2021-04-25 op
28 c36d43ac 2021-04-25 op void
29 c36d43ac 2021-04-25 op tofu_init(struct ohash *h, unsigned int sz, ptrdiff_t ko)
30 c36d43ac 2021-04-25 op {
31 c36d43ac 2021-04-25 op struct ohash_info info = {
32 c36d43ac 2021-04-25 op .key_offset = ko,
33 c36d43ac 2021-04-25 op .calloc = hash_calloc,
34 c36d43ac 2021-04-25 op .free = hash_free,
35 c36d43ac 2021-04-25 op .alloc = hash_alloc,
36 c36d43ac 2021-04-25 op };
37 c36d43ac 2021-04-25 op
38 c36d43ac 2021-04-25 op ohash_init(h, sz, &info);
39 c36d43ac 2021-04-25 op }
40 c36d43ac 2021-04-25 op
41 c36d43ac 2021-04-25 op struct tofu_entry *
42 c36d43ac 2021-04-25 op tofu_lookup(struct ohash *h, const char *domain, const char *port)
43 c36d43ac 2021-04-25 op {
44 c36d43ac 2021-04-25 op char buf[GEMINI_URL_LEN];
45 c36d43ac 2021-04-25 op unsigned int slot;
46 c36d43ac 2021-04-25 op
47 c36d43ac 2021-04-25 op strlcpy(buf, domain, sizeof(buf));
48 c36d43ac 2021-04-25 op if (port != NULL && *port != '\0' && strcmp(port, "1965")) {
49 c36d43ac 2021-04-25 op strlcat(buf, ":", sizeof(buf));
50 c36d43ac 2021-04-25 op strlcat(buf, port, sizeof(buf));
51 c36d43ac 2021-04-25 op }
52 c36d43ac 2021-04-25 op
53 c36d43ac 2021-04-25 op slot = ohash_qlookup(h, buf);
54 c36d43ac 2021-04-25 op return ohash_find(h, slot);
55 c36d43ac 2021-04-25 op }
56 c36d43ac 2021-04-25 op
57 c36d43ac 2021-04-25 op void
58 c36d43ac 2021-04-25 op tofu_add(struct ohash *h, struct tofu_entry *e)
59 c36d43ac 2021-04-25 op {
60 c36d43ac 2021-04-25 op unsigned int slot;
61 c36d43ac 2021-04-25 op
62 c36d43ac 2021-04-25 op slot = ohash_qlookup(h, e->domain);
63 c36d43ac 2021-04-25 op ohash_insert(h, slot, e);
64 c36d43ac 2021-04-25 op }
65 288fd238 2021-04-25 op
66 4fa88f57 2022-04-24 op int
67 4fa88f57 2022-04-24 op tofu_save(struct ohash *h, struct tofu_entry *e)
68 4fa88f57 2022-04-24 op {
69 4fa88f57 2022-04-24 op FILE *fp;
70 4fa88f57 2022-04-24 op
71 4fa88f57 2022-04-24 op tofu_add(h, e);
72 4fa88f57 2022-04-24 op
73 4fa88f57 2022-04-24 op if ((fp = fopen(known_hosts_file, "a")) == NULL)
74 4fa88f57 2022-04-24 op return -1;
75 4fa88f57 2022-04-24 op fprintf(fp, "%s %s %d\n", e->domain, e->hash, e->verified);
76 4fa88f57 2022-04-24 op fclose(fp);
77 4fa88f57 2022-04-24 op return 0;
78 4fa88f57 2022-04-24 op }
79 4fa88f57 2022-04-24 op
80 288fd238 2021-04-25 op void
81 288fd238 2021-04-25 op tofu_update(struct ohash *h, struct tofu_entry *e)
82 288fd238 2021-04-25 op {
83 288fd238 2021-04-25 op struct tofu_entry *t;
84 288fd238 2021-04-25 op
85 288fd238 2021-04-25 op if ((t = tofu_lookup(h, e->domain, NULL)) == NULL)
86 288fd238 2021-04-25 op tofu_add(h, e);
87 288fd238 2021-04-25 op else {
88 288fd238 2021-04-25 op strlcpy(t->hash, e->hash, sizeof(t->hash));
89 288fd238 2021-04-25 op t->verified = e->verified;
90 288fd238 2021-04-25 op free(e);
91 288fd238 2021-04-25 op }
92 288fd238 2021-04-25 op }
93 a2fd3805 2021-07-06 op
94 3078e1bc 2022-04-24 op int
95 3078e1bc 2022-04-24 op tofu_update_persist(struct ohash *h, struct tofu_entry *e)
96 3078e1bc 2022-04-24 op {
97 3078e1bc 2022-04-24 op FILE *tmp, *fp;
98 3078e1bc 2022-04-24 op char sfn[PATH_MAX], *line = NULL;
99 3078e1bc 2022-04-24 op size_t l, linesize = 0;
100 3078e1bc 2022-04-24 op ssize_t linelen;
101 3078e1bc 2022-04-24 op int fd, err;
102 3078e1bc 2022-04-24 op
103 3078e1bc 2022-04-24 op tofu_update(h, e);
104 3078e1bc 2022-04-24 op
105 3078e1bc 2022-04-24 op strlcpy(sfn, known_hosts_tmp, sizeof(sfn));
106 3078e1bc 2022-04-24 op if ((fd = mkstemp(sfn)) == -1 ||
107 3078e1bc 2022-04-24 op (tmp = fdopen(fd, "w")) == NULL) {
108 3078e1bc 2022-04-24 op if (fd != -1) {
109 3078e1bc 2022-04-24 op unlink(sfn);
110 3078e1bc 2022-04-24 op close(fd);
111 3078e1bc 2022-04-24 op }
112 3078e1bc 2022-04-24 op return -1;
113 3078e1bc 2022-04-24 op }
114 3078e1bc 2022-04-24 op
115 3078e1bc 2022-04-24 op if ((fp = fopen(known_hosts_file, "r")) == NULL) {
116 3078e1bc 2022-04-24 op unlink(sfn);
117 3078e1bc 2022-04-24 op fclose(tmp);
118 3078e1bc 2022-04-24 op return -1;
119 3078e1bc 2022-04-24 op }
120 3078e1bc 2022-04-24 op
121 3078e1bc 2022-04-24 op l = strlen(e->domain);
122 3078e1bc 2022-04-24 op while ((linelen = getline(&line, &linesize, fp)) != -1) {
123 3078e1bc 2022-04-24 op if (!strncmp(line, e->domain, l))
124 3078e1bc 2022-04-24 op continue;
125 3078e1bc 2022-04-24 op if (linesize > 0 && line[linesize-1] == '\n')
126 3078e1bc 2022-04-24 op line[linesize-1] = '\0';
127 3078e1bc 2022-04-24 op fprintf(tmp, "%s\n", line);
128 3078e1bc 2022-04-24 op }
129 3078e1bc 2022-04-24 op fprintf(tmp, "%s %s %d\n", e->domain, e->hash, e->verified);
130 3078e1bc 2022-04-24 op
131 3078e1bc 2022-04-24 op free(line);
132 3078e1bc 2022-04-24 op err = ferror(tmp);
133 3078e1bc 2022-04-24 op fclose(tmp);
134 3078e1bc 2022-04-24 op fclose(fp);
135 3078e1bc 2022-04-24 op
136 3078e1bc 2022-04-24 op if (err) {
137 3078e1bc 2022-04-24 op unlink(sfn);
138 3078e1bc 2022-04-24 op return -1;
139 3078e1bc 2022-04-24 op }
140 3078e1bc 2022-04-24 op
141 3078e1bc 2022-04-24 op if (rename(sfn, known_hosts_file))
142 3078e1bc 2022-04-24 op return -1;
143 3078e1bc 2022-04-24 op return 0;
144 3078e1bc 2022-04-24 op }
145 3078e1bc 2022-04-24 op
146 a2fd3805 2021-07-06 op void
147 a2fd3805 2021-07-06 op tofu_temp_trust(struct ohash *h, const char *host, const char *port,
148 a2fd3805 2021-07-06 op const char *hash)
149 a2fd3805 2021-07-06 op {
150 a2fd3805 2021-07-06 op struct tofu_entry *e;
151 a2fd3805 2021-07-06 op
152 a2fd3805 2021-07-06 op if ((e = calloc(1, sizeof(*e))) == NULL)
153 95a8c791 2021-08-26 op abort();
154 a2fd3805 2021-07-06 op
155 95a8c791 2021-08-26 op strlcpy(e->domain, host, sizeof(e->domain));
156 a2fd3805 2021-07-06 op if (*port != '\0' && strcmp(port, "1965")) {
157 a2fd3805 2021-07-06 op strlcat(e->domain, ":", sizeof(e->domain));
158 a2fd3805 2021-07-06 op strlcat(e->domain, port, sizeof(e->domain));
159 a2fd3805 2021-07-06 op }
160 a2fd3805 2021-07-06 op strlcpy(e->hash, hash, sizeof(e->hash));
161 a2fd3805 2021-07-06 op e->verified = -1;
162 a2fd3805 2021-07-06 op
163 a2fd3805 2021-07-06 op tofu_update(h, e);
164 a2fd3805 2021-07-06 op }