Blob


1 /*
2 * Copyright (c) 2021, 2022, 2024 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 <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
25 #include "fs.h"
26 #include "tofu.h"
27 #include "utils.h"
28 #include "xwrapper.h"
30 void
31 tofu_init(struct ohash *h, unsigned int sz, ptrdiff_t ko)
32 {
33 struct ohash_info info = {
34 .key_offset = ko,
35 .calloc = hash_calloc,
36 .free = hash_free,
37 .alloc = hash_alloc,
38 };
40 ohash_init(h, sz, &info);
41 }
43 struct tofu_entry *
44 tofu_lookup(struct ohash *h, const char *domain, const char *port)
45 {
46 char buf[TOFU_URL_MAX_LEN];
47 unsigned int slot;
49 strlcpy(buf, domain, sizeof(buf));
50 if (port != NULL && *port != '\0' && strcmp(port, "1965")) {
51 strlcat(buf, ":", sizeof(buf));
52 strlcat(buf, port, sizeof(buf));
53 }
55 slot = ohash_qlookup(h, buf);
56 return ohash_find(h, slot);
57 }
59 void
60 tofu_add(struct ohash *h, struct tofu_entry *e)
61 {
62 unsigned int slot;
64 slot = ohash_qlookup(h, e->domain);
65 ohash_insert(h, slot, e);
66 }
68 int
69 tofu_save(struct ohash *h, struct tofu_entry *e)
70 {
71 FILE *fp;
73 tofu_add(h, e);
75 if ((fp = fopen(known_hosts_file, "a")) == NULL)
76 return -1;
77 fprintf(fp, "%s %s %d\n", e->domain, e->hash, e->verified);
78 fclose(fp);
79 return 0;
80 }
82 void
83 tofu_update(struct ohash *h, struct tofu_entry *e)
84 {
85 struct tofu_entry *t;
87 if ((t = tofu_lookup(h, e->domain, NULL)) == NULL)
88 tofu_add(h, e);
89 else {
90 strlcpy(t->hash, e->hash, sizeof(t->hash));
91 t->verified = e->verified;
92 free(e);
93 }
94 }
96 int
97 tofu_update_persist(struct ohash *h, struct tofu_entry *e)
98 {
99 FILE *tmp, *fp;
100 char sfn[PATH_MAX], *line = NULL;
101 size_t l, linesize = 0;
102 ssize_t linelen;
103 int fd, err;
105 tofu_update(h, e);
107 strlcpy(sfn, known_hosts_tmp, sizeof(sfn));
108 if ((fd = mkstemp(sfn)) == -1 ||
109 (tmp = fdopen(fd, "w")) == NULL) {
110 if (fd != -1) {
111 unlink(sfn);
112 close(fd);
114 return -1;
117 if ((fp = fopen(known_hosts_file, "r")) == NULL) {
118 unlink(sfn);
119 fclose(tmp);
120 return -1;
123 l = strlen(e->domain);
124 while ((linelen = getline(&line, &linesize, fp)) != -1) {
125 if (!strncmp(line, e->domain, l))
126 continue;
127 if (linesize > 0 && line[linesize-1] == '\n')
128 line[linesize-1] = '\0';
129 fprintf(tmp, "%s\n", line);
131 fprintf(tmp, "%s %s %d\n", e->domain, e->hash, e->verified);
133 free(line);
134 err = ferror(tmp);
135 fclose(tmp);
136 fclose(fp);
138 if (err) {
139 unlink(sfn);
140 return -1;
143 if (rename(sfn, known_hosts_file) == -1)
144 return -1;
145 return 0;
148 void
149 tofu_temp_trust(struct ohash *h, const char *host, const char *port,
150 const char *hash)
152 struct tofu_entry *e;
154 e = xcalloc(1, sizeof(*e));
156 strlcpy(e->domain, host, sizeof(e->domain));
157 if (*port != '\0' && strcmp(port, "1965")) {
158 strlcat(e->domain, ":", sizeof(e->domain));
159 strlcat(e->domain, port, sizeof(e->domain));
161 strlcpy(e->hash, hash, sizeof(e->hash));
162 e->verified = -1;
164 tofu_update(h, e);