2 12472189 2024-02-05 op * Copyright (c) 2021, 2022, 2024 Omar Polo <op@omarpolo.com>
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.
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.
17 036c104e 2022-01-11 op #include "compat.h"
19 4fa88f57 2022-04-24 op #include <limits.h>
20 d163c210 2024-02-06 op #include <stdio.h>
21 c36d43ac 2021-04-25 op #include <stdlib.h>
22 c36d43ac 2021-04-25 op #include <string.h>
23 3078e1bc 2022-04-24 op #include <unistd.h>
25 4fa88f57 2022-04-24 op #include "fs.h"
26 d163c210 2024-02-06 op #include "tofu.h"
27 9d65b1d9 2022-01-11 op #include "utils.h"
28 3d89457c 2024-06-18 thomas.ad #include "xwrapper.h"
31 c36d43ac 2021-04-25 op tofu_init(struct ohash *h, unsigned int sz, ptrdiff_t ko)
33 c36d43ac 2021-04-25 op struct ohash_info info = {
34 c36d43ac 2021-04-25 op .key_offset = ko,
35 c36d43ac 2021-04-25 op .calloc = hash_calloc,
36 c36d43ac 2021-04-25 op .free = hash_free,
37 c36d43ac 2021-04-25 op .alloc = hash_alloc,
40 c36d43ac 2021-04-25 op ohash_init(h, sz, &info);
43 c36d43ac 2021-04-25 op struct tofu_entry *
44 c36d43ac 2021-04-25 op tofu_lookup(struct ohash *h, const char *domain, const char *port)
46 d163c210 2024-02-06 op char buf[TOFU_URL_MAX_LEN];
47 c36d43ac 2021-04-25 op unsigned int slot;
49 c36d43ac 2021-04-25 op strlcpy(buf, domain, sizeof(buf));
50 c36d43ac 2021-04-25 op if (port != NULL && *port != '\0' && strcmp(port, "1965")) {
51 c36d43ac 2021-04-25 op strlcat(buf, ":", sizeof(buf));
52 c36d43ac 2021-04-25 op strlcat(buf, port, sizeof(buf));
55 c36d43ac 2021-04-25 op slot = ohash_qlookup(h, buf);
56 c36d43ac 2021-04-25 op return ohash_find(h, slot);
60 c36d43ac 2021-04-25 op tofu_add(struct ohash *h, struct tofu_entry *e)
62 c36d43ac 2021-04-25 op unsigned int slot;
64 c36d43ac 2021-04-25 op slot = ohash_qlookup(h, e->domain);
65 c36d43ac 2021-04-25 op ohash_insert(h, slot, e);
69 4fa88f57 2022-04-24 op tofu_save(struct ohash *h, struct tofu_entry *e)
73 4fa88f57 2022-04-24 op tofu_add(h, e);
75 4fa88f57 2022-04-24 op if ((fp = fopen(known_hosts_file, "a")) == NULL)
77 4fa88f57 2022-04-24 op fprintf(fp, "%s %s %d\n", e->domain, e->hash, e->verified);
83 288fd238 2021-04-25 op tofu_update(struct ohash *h, struct tofu_entry *e)
85 288fd238 2021-04-25 op struct tofu_entry *t;
87 288fd238 2021-04-25 op if ((t = tofu_lookup(h, e->domain, NULL)) == NULL)
88 288fd238 2021-04-25 op tofu_add(h, e);
90 288fd238 2021-04-25 op strlcpy(t->hash, e->hash, sizeof(t->hash));
91 288fd238 2021-04-25 op t->verified = e->verified;
97 3078e1bc 2022-04-24 op tofu_update_persist(struct ohash *h, struct tofu_entry *e)
99 3078e1bc 2022-04-24 op FILE *tmp, *fp;
100 3078e1bc 2022-04-24 op char sfn[PATH_MAX], *line = NULL;
101 3078e1bc 2022-04-24 op size_t l, linesize = 0;
102 3078e1bc 2022-04-24 op ssize_t linelen;
105 3078e1bc 2022-04-24 op tofu_update(h, e);
107 3078e1bc 2022-04-24 op strlcpy(sfn, known_hosts_tmp, sizeof(sfn));
108 3078e1bc 2022-04-24 op if ((fd = mkstemp(sfn)) == -1 ||
109 3078e1bc 2022-04-24 op (tmp = fdopen(fd, "w")) == NULL) {
110 3078e1bc 2022-04-24 op if (fd != -1) {
117 3078e1bc 2022-04-24 op if ((fp = fopen(known_hosts_file, "r")) == NULL) {
123 3078e1bc 2022-04-24 op l = strlen(e->domain);
124 3078e1bc 2022-04-24 op while ((linelen = getline(&line, &linesize, fp)) != -1) {
125 3078e1bc 2022-04-24 op if (!strncmp(line, e->domain, l))
127 3078e1bc 2022-04-24 op if (linesize > 0 && line[linesize-1] == '\n')
128 3078e1bc 2022-04-24 op line[linesize-1] = '\0';
129 3078e1bc 2022-04-24 op fprintf(tmp, "%s\n", line);
131 3078e1bc 2022-04-24 op fprintf(tmp, "%s %s %d\n", e->domain, e->hash, e->verified);
134 3078e1bc 2022-04-24 op err = ferror(tmp);
143 12472189 2024-02-05 op if (rename(sfn, known_hosts_file) == -1)
149 a2fd3805 2021-07-06 op tofu_temp_trust(struct ohash *h, const char *host, const char *port,
150 a2fd3805 2021-07-06 op const char *hash)
152 a2fd3805 2021-07-06 op struct tofu_entry *e;
154 3d89457c 2024-06-18 thomas.ad e = xcalloc(1, sizeof(*e));
156 95a8c791 2021-08-26 op strlcpy(e->domain, host, sizeof(e->domain));
157 a2fd3805 2021-07-06 op if (*port != '\0' && strcmp(port, "1965")) {
158 a2fd3805 2021-07-06 op strlcat(e->domain, ":", sizeof(e->domain));
159 a2fd3805 2021-07-06 op strlcat(e->domain, port, sizeof(e->domain));
161 a2fd3805 2021-07-06 op strlcpy(e->hash, hash, sizeof(e->hash));
162 a2fd3805 2021-07-06 op e->verified = -1;
164 a2fd3805 2021-07-06 op tofu_update(h, e);