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