Blob


1 /*
2 * Copyright (c) 2021, 2022 Omar Polo <op@omarpolo.com>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include "compat.h"
19 #include <limits.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <unistd.h>
24 #include "fs.h"
25 #include "telescope.h"
26 #include "utils.h"
28 void
29 tofu_init(struct ohash *h, unsigned int sz, ptrdiff_t ko)
30 {
31 struct ohash_info info = {
32 .key_offset = ko,
33 .calloc = hash_calloc,
34 .free = hash_free,
35 .alloc = hash_alloc,
36 };
38 ohash_init(h, sz, &info);
39 }
41 struct tofu_entry *
42 tofu_lookup(struct ohash *h, const char *domain, const char *port)
43 {
44 char buf[GEMINI_URL_LEN];
45 unsigned int slot;
47 strlcpy(buf, domain, sizeof(buf));
48 if (port != NULL && *port != '\0' && strcmp(port, "1965")) {
49 strlcat(buf, ":", sizeof(buf));
50 strlcat(buf, port, sizeof(buf));
51 }
53 slot = ohash_qlookup(h, buf);
54 return ohash_find(h, slot);
55 }
57 void
58 tofu_add(struct ohash *h, struct tofu_entry *e)
59 {
60 unsigned int slot;
62 slot = ohash_qlookup(h, e->domain);
63 ohash_insert(h, slot, e);
64 }
66 int
67 tofu_save(struct ohash *h, struct tofu_entry *e)
68 {
69 FILE *fp;
71 tofu_add(h, e);
73 if ((fp = fopen(known_hosts_file, "a")) == NULL)
74 return -1;
75 fprintf(fp, "%s %s %d\n", e->domain, e->hash, e->verified);
76 fclose(fp);
77 return 0;
78 }
80 void
81 tofu_update(struct ohash *h, struct tofu_entry *e)
82 {
83 struct tofu_entry *t;
85 if ((t = tofu_lookup(h, e->domain, NULL)) == NULL)
86 tofu_add(h, e);
87 else {
88 strlcpy(t->hash, e->hash, sizeof(t->hash));
89 t->verified = e->verified;
90 free(e);
91 }
92 }
94 int
95 tofu_update_persist(struct ohash *h, struct tofu_entry *e)
96 {
97 FILE *tmp, *fp;
98 char sfn[PATH_MAX], *line = NULL;
99 size_t l, linesize = 0;
100 ssize_t linelen;
101 int fd, err;
103 tofu_update(h, e);
105 strlcpy(sfn, known_hosts_tmp, sizeof(sfn));
106 if ((fd = mkstemp(sfn)) == -1 ||
107 (tmp = fdopen(fd, "w")) == NULL) {
108 if (fd != -1) {
109 unlink(sfn);
110 close(fd);
112 return -1;
115 if ((fp = fopen(known_hosts_file, "r")) == NULL) {
116 unlink(sfn);
117 fclose(tmp);
118 return -1;
121 l = strlen(e->domain);
122 while ((linelen = getline(&line, &linesize, fp)) != -1) {
123 if (!strncmp(line, e->domain, l))
124 continue;
125 if (linesize > 0 && line[linesize-1] == '\n')
126 line[linesize-1] = '\0';
127 fprintf(tmp, "%s\n", line);
129 fprintf(tmp, "%s %s %d\n", e->domain, e->hash, e->verified);
131 free(line);
132 err = ferror(tmp);
133 fclose(tmp);
134 fclose(fp);
136 if (err) {
137 unlink(sfn);
138 return -1;
141 if (rename(sfn, known_hosts_file))
142 return -1;
143 return 0;
146 void
147 tofu_temp_trust(struct ohash *h, const char *host, const char *port,
148 const char *hash)
150 struct tofu_entry *e;
152 if ((e = calloc(1, sizeof(*e))) == NULL)
153 abort();
155 strlcpy(e->domain, host, sizeof(e->domain));
156 if (*port != '\0' && strcmp(port, "1965")) {
157 strlcat(e->domain, ":", sizeof(e->domain));
158 strlcat(e->domain, port, sizeof(e->domain));
160 strlcpy(e->hash, hash, sizeof(e->hash));
161 e->verified = -1;
163 tofu_update(h, e);