Blame


1 35e1f40a 2021-03-14 op /*
2 35e1f40a 2021-03-14 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
3 35e1f40a 2021-03-14 op *
4 35e1f40a 2021-03-14 op * Permission to use, copy, modify, and distribute this software for any
5 35e1f40a 2021-03-14 op * purpose with or without fee is hereby granted, provided that the above
6 35e1f40a 2021-03-14 op * copyright notice and this permission notice appear in all copies.
7 35e1f40a 2021-03-14 op *
8 35e1f40a 2021-03-14 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 35e1f40a 2021-03-14 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 35e1f40a 2021-03-14 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 35e1f40a 2021-03-14 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 35e1f40a 2021-03-14 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 35e1f40a 2021-03-14 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 35e1f40a 2021-03-14 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 35e1f40a 2021-03-14 op */
16 35e1f40a 2021-03-14 op
17 740f578b 2021-03-15 op /*
18 eb2ed626 2021-10-07 op * Handles config and runtime files
19 740f578b 2021-03-15 op */
20 786e6deb 2021-07-21 op
21 786e6deb 2021-07-21 op #include "compat.h"
22 35e1f40a 2021-03-14 op
23 6cd6a9e1 2021-03-20 op #include <sys/stat.h>
24 65124267 2021-08-13 op #include <sys/types.h>
25 6cd6a9e1 2021-03-20 op
26 65124267 2021-08-13 op #include <dirent.h>
27 35e1f40a 2021-03-14 op #include <errno.h>
28 de2a69bb 2021-05-17 op #include <fcntl.h>
29 35e1f40a 2021-03-14 op #include <limits.h>
30 eb2ed626 2021-10-07 op #include <libgen.h>
31 35e1f40a 2021-03-14 op #include <stdio.h>
32 35e1f40a 2021-03-14 op #include <stdlib.h>
33 35e1f40a 2021-03-14 op #include <string.h>
34 35e1f40a 2021-03-14 op #include <unistd.h>
35 4913b479 2021-07-12 op
36 eb2ed626 2021-10-07 op #include "fs.h"
37 5a824be4 2021-07-13 op #include "pages.h"
38 328e7915 2021-09-19 op #include "telescope.h"
39 35e1f40a 2021-03-14 op
40 35e1f40a 2021-03-14 op static void die(void) __attribute__((__noreturn__));
41 fb4dc49f 2021-08-13 op static void send_file(uint32_t, FILE *);
42 35e1f40a 2021-03-14 op static void handle_get(struct imsg*, size_t);
43 5cbe1763 2021-08-13 op static int select_non_dot(const struct dirent *);
44 5cbe1763 2021-08-13 op static int select_non_dotdot(const struct dirent *);
45 fb4dc49f 2021-08-13 op static void handle_get_file(struct imsg*, size_t);
46 35e1f40a 2021-03-14 op static void handle_quit(struct imsg*, size_t);
47 740f578b 2021-03-15 op static void handle_bookmark_page(struct imsg*, size_t);
48 3a227e9a 2021-03-18 op static void handle_save_cert(struct imsg*, size_t);
49 288fd238 2021-04-25 op static void handle_update_cert(struct imsg*, size_t);
50 de2a69bb 2021-05-17 op static void handle_file_open(struct imsg*, size_t);
51 c7107cec 2021-04-01 op static void handle_session_start(struct imsg*, size_t);
52 c7107cec 2021-04-01 op static void handle_session_tab(struct imsg*, size_t);
53 2f524b17 2021-07-17 op static void handle_session_tab_title(struct imsg*, size_t);
54 c7107cec 2021-04-01 op static void handle_session_end(struct imsg*, size_t);
55 1304bbdd 2021-03-15 op static void handle_dispatch_imsg(int, short, void*);
56 bc10f6a5 2021-07-12 op static int fs_send_ui(int, uint32_t, int, const void *, uint16_t);
57 eb2ed626 2021-10-07 op static size_t join_path(char*, const char*, const char*, size_t);
58 eb2ed626 2021-10-07 op static void getenv_default(char*, const char*, const char*, size_t);
59 eb2ed626 2021-10-07 op static void mkdirs(const char*, mode_t);
60 eb2ed626 2021-10-07 op static void xdg_init(void);
61 35e1f40a 2021-03-14 op
62 bc10f6a5 2021-07-12 op static struct imsgev *iev_ui;
63 c7107cec 2021-04-01 op static FILE *session;
64 c7107cec 2021-04-01 op
65 eb2ed626 2021-10-07 op /* WARNING: xdg_*_base variables are not initialized if ~/.telescope exists */
66 eb2ed626 2021-10-07 op static char xdg_config_base[PATH_MAX];
67 eb2ed626 2021-10-07 op static char xdg_data_base[PATH_MAX];
68 eb2ed626 2021-10-07 op static char xdg_cache_base[PATH_MAX];
69 eb2ed626 2021-10-07 op
70 eb2ed626 2021-10-07 op /* *_path_base variables are all equal to $HOME/.telescope if it exists */
71 fb3d08c1 2021-10-07 op char config_path_base[PATH_MAX];
72 fb3d08c1 2021-10-07 op char data_path_base[PATH_MAX];
73 fb3d08c1 2021-10-07 op char cache_path_base[PATH_MAX];
74 eb2ed626 2021-10-07 op
75 eb2ed626 2021-10-07 op char config_path[PATH_MAX];
76 fb3d08c1 2021-10-07 op char lockfile_path[PATH_MAX];
77 fb3d08c1 2021-10-07 op char bookmark_file[PATH_MAX];
78 fb3d08c1 2021-10-07 op char known_hosts_file[PATH_MAX], known_hosts_tmp[PATH_MAX];
79 fb3d08c1 2021-10-07 op char crashed_file[PATH_MAX];
80 fb3d08c1 2021-10-07 op char session_file[PATH_MAX];
81 740f578b 2021-03-15 op
82 35e1f40a 2021-03-14 op static imsg_handlerfn *handlers[] = {
83 35e1f40a 2021-03-14 op [IMSG_GET] = handle_get,
84 fb4dc49f 2021-08-13 op [IMSG_GET_FILE] = handle_get_file,
85 35e1f40a 2021-03-14 op [IMSG_QUIT] = handle_quit,
86 740f578b 2021-03-15 op [IMSG_BOOKMARK_PAGE] = handle_bookmark_page,
87 3a227e9a 2021-03-18 op [IMSG_SAVE_CERT] = handle_save_cert,
88 288fd238 2021-04-25 op [IMSG_UPDATE_CERT] = handle_update_cert,
89 de2a69bb 2021-05-17 op [IMSG_FILE_OPEN] = handle_file_open,
90 c7107cec 2021-04-01 op [IMSG_SESSION_START] = handle_session_start,
91 c7107cec 2021-04-01 op [IMSG_SESSION_TAB] = handle_session_tab,
92 2f524b17 2021-07-17 op [IMSG_SESSION_TAB_TITLE] = handle_session_tab_title,
93 c7107cec 2021-04-01 op [IMSG_SESSION_END] = handle_session_end,
94 35e1f40a 2021-03-14 op };
95 35e1f40a 2021-03-14 op
96 35e1f40a 2021-03-14 op static void __attribute__((__noreturn__))
97 35e1f40a 2021-03-14 op die(void)
98 35e1f40a 2021-03-14 op {
99 35e1f40a 2021-03-14 op abort(); /* TODO */
100 35e1f40a 2021-03-14 op }
101 35e1f40a 2021-03-14 op
102 35e1f40a 2021-03-14 op static void
103 fb4dc49f 2021-08-13 op send_file(uint32_t peerid, FILE *f)
104 fb4dc49f 2021-08-13 op {
105 fb4dc49f 2021-08-13 op ssize_t r;
106 fb4dc49f 2021-08-13 op char buf[BUFSIZ];
107 fb4dc49f 2021-08-13 op
108 fb4dc49f 2021-08-13 op for (;;) {
109 fb4dc49f 2021-08-13 op r = fread(buf, 1, sizeof(buf), f);
110 fb4dc49f 2021-08-13 op if (r != 0)
111 fb4dc49f 2021-08-13 op fs_send_ui(IMSG_BUF, peerid, -1, buf, r);
112 fb4dc49f 2021-08-13 op if (r != sizeof(buf))
113 fb4dc49f 2021-08-13 op break;
114 fb4dc49f 2021-08-13 op }
115 fb4dc49f 2021-08-13 op fs_send_ui(IMSG_EOF, peerid, -1, NULL, 0);
116 fb4dc49f 2021-08-13 op fclose(f);
117 fb4dc49f 2021-08-13 op }
118 fb4dc49f 2021-08-13 op
119 fb4dc49f 2021-08-13 op static void
120 35e1f40a 2021-03-14 op handle_get(struct imsg *imsg, size_t datalen)
121 35e1f40a 2021-03-14 op {
122 56e7efb4 2021-07-21 op const char *bpath = "bookmarks.gmi";
123 fb4dc49f 2021-08-13 op char path[PATH_MAX];
124 56e7efb4 2021-07-21 op FILE *f;
125 a2728733 2021-07-18 op const char *data, *p;
126 a2728733 2021-07-18 op size_t i;
127 a2728733 2021-07-18 op struct page {
128 a2728733 2021-07-18 op const char *name;
129 56e7efb4 2021-07-21 op const char *path;
130 a2728733 2021-07-18 op const uint8_t *data;
131 a2728733 2021-07-18 op size_t len;
132 a2728733 2021-07-18 op } pages[] = {
133 56e7efb4 2021-07-21 op {"about", NULL, about_about, about_about_len},
134 56e7efb4 2021-07-21 op {"blank", NULL, about_blank, about_blank_len},
135 56e7efb4 2021-07-21 op {"bookmarks", bpath, bookmarks, bookmarks_len},
136 56e7efb4 2021-07-21 op {"crash", NULL, about_crash, about_crash_len},
137 56e7efb4 2021-07-21 op {"help", NULL, about_help, about_help_len},
138 56e7efb4 2021-07-21 op {"license", NULL, about_license, about_license_len},
139 56e7efb4 2021-07-21 op {"new", NULL, about_new, about_new_len},
140 56e7efb4 2021-07-21 op }, *page = NULL;
141 35e1f40a 2021-03-14 op
142 35e1f40a 2021-03-14 op data = imsg->data;
143 56e7efb4 2021-07-21 op if (data[datalen-1] != '\0') /* make sure it's NUL-terminated */
144 35e1f40a 2021-03-14 op die();
145 56e7efb4 2021-07-21 op if ((data = strchr(data, ':')) == NULL)
146 56e7efb4 2021-07-21 op goto notfound;
147 56e7efb4 2021-07-21 op data++;
148 35e1f40a 2021-03-14 op
149 56e7efb4 2021-07-21 op for (i = 0; i < sizeof(pages)/sizeof(pages[0]); ++i)
150 56e7efb4 2021-07-21 op if (!strcmp(data, pages[i].name)) {
151 56e7efb4 2021-07-21 op page = &pages[i];
152 56e7efb4 2021-07-21 op break;
153 56e7efb4 2021-07-21 op }
154 56e7efb4 2021-07-21 op
155 56e7efb4 2021-07-21 op if (page == NULL)
156 56e7efb4 2021-07-21 op goto notfound;
157 56e7efb4 2021-07-21 op
158 de04b178 2021-11-26 op strlcpy(path, data_path_base, sizeof(path));
159 56e7efb4 2021-07-21 op strlcat(path, "/", sizeof(path));
160 56e7efb4 2021-07-21 op if (page->path != NULL)
161 56e7efb4 2021-07-21 op strlcat(path, page->path, sizeof(path));
162 56e7efb4 2021-07-21 op else {
163 56e7efb4 2021-07-21 op strlcat(path, "pages/about_", sizeof(path));
164 56e7efb4 2021-07-21 op strlcat(path, page->name, sizeof(path));
165 56e7efb4 2021-07-21 op strlcat(path, ".gmi", sizeof(path));
166 56e7efb4 2021-07-21 op }
167 56e7efb4 2021-07-21 op
168 56e7efb4 2021-07-21 op if ((f = fopen(path, "r")) == NULL) {
169 56e7efb4 2021-07-21 op fs_send_ui(IMSG_BUF, imsg->hdr.peerid, -1,
170 56e7efb4 2021-07-21 op page->data, page->len);
171 56e7efb4 2021-07-21 op fs_send_ui(IMSG_EOF, imsg->hdr.peerid, -1,
172 56e7efb4 2021-07-21 op NULL, 0);
173 a2728733 2021-07-18 op return;
174 35e1f40a 2021-03-14 op }
175 a2728733 2021-07-18 op
176 fb4dc49f 2021-08-13 op send_file(imsg->hdr.peerid, f);
177 56e7efb4 2021-07-21 op return;
178 56e7efb4 2021-07-21 op
179 56e7efb4 2021-07-21 op notfound:
180 a2728733 2021-07-18 op p = "# not found!\n";
181 a2728733 2021-07-18 op fs_send_ui(IMSG_BUF, imsg->hdr.peerid, -1, p, strlen(p));
182 a2728733 2021-07-18 op fs_send_ui(IMSG_EOF, imsg->hdr.peerid, -1, NULL, 0);
183 fb4dc49f 2021-08-13 op }
184 fb4dc49f 2021-08-13 op
185 fb4dc49f 2021-08-13 op static inline void
186 fb4dc49f 2021-08-13 op send_hdr(uint32_t peerid, int code, const char *meta)
187 fb4dc49f 2021-08-13 op {
188 fb4dc49f 2021-08-13 op fs_send_ui(IMSG_GOT_CODE, peerid, -1, &code, sizeof(code));
189 fb4dc49f 2021-08-13 op fs_send_ui(IMSG_GOT_META, peerid, -1, meta, strlen(meta)+1);
190 fb4dc49f 2021-08-13 op }
191 fb4dc49f 2021-08-13 op
192 24a68158 2021-08-13 op static inline void
193 24a68158 2021-08-13 op send_errno(uint32_t peerid, int code, const char *str, int no)
194 24a68158 2021-08-13 op {
195 24a68158 2021-08-13 op char *s;
196 24a68158 2021-08-13 op
197 24a68158 2021-08-13 op if (asprintf(&s, "%s: %s", str, strerror(no)) == -1)
198 24a68158 2021-08-13 op s = NULL;
199 24a68158 2021-08-13 op
200 24a68158 2021-08-13 op send_hdr(peerid, code, s == NULL ? str : s);
201 24a68158 2021-08-13 op free(s);
202 24a68158 2021-08-13 op }
203 24a68158 2021-08-13 op
204 65124267 2021-08-13 op static inline const char *
205 65124267 2021-08-13 op file_type(const char *path)
206 fb4dc49f 2021-08-13 op {
207 fb4dc49f 2021-08-13 op struct mapping {
208 fb4dc49f 2021-08-13 op const char *ext;
209 fb4dc49f 2021-08-13 op const char *mime;
210 fb4dc49f 2021-08-13 op } ms[] = {
211 fb4dc49f 2021-08-13 op {"diff", "text/x-patch"},
212 fb4dc49f 2021-08-13 op {"gemini", "text/gemini"},
213 fb4dc49f 2021-08-13 op {"gmi", "text/gemini"},
214 fb4dc49f 2021-08-13 op {"markdown", "text/plain"},
215 fb4dc49f 2021-08-13 op {"md", "text/plain"},
216 fb4dc49f 2021-08-13 op {"patch", "text/x-patch"},
217 fb4dc49f 2021-08-13 op {"txt", "text/plain"},
218 fb4dc49f 2021-08-13 op {NULL, NULL},
219 65124267 2021-08-13 op }, *m;
220 65124267 2021-08-13 op char *dot;
221 65124267 2021-08-13 op
222 65124267 2021-08-13 op if ((dot = strrchr(path, '.')) == NULL)
223 65124267 2021-08-13 op return NULL;
224 65124267 2021-08-13 op
225 65124267 2021-08-13 op dot++;
226 65124267 2021-08-13 op
227 65124267 2021-08-13 op for (m = ms; m->ext != NULL; ++m)
228 65124267 2021-08-13 op if (!strcmp(m->ext, dot))
229 65124267 2021-08-13 op return m->mime;
230 65124267 2021-08-13 op
231 65124267 2021-08-13 op return NULL;
232 5cbe1763 2021-08-13 op }
233 5cbe1763 2021-08-13 op
234 5cbe1763 2021-08-13 op static int
235 5cbe1763 2021-08-13 op select_non_dot(const struct dirent *d)
236 5cbe1763 2021-08-13 op {
237 5cbe1763 2021-08-13 op return strcmp(d->d_name, ".");
238 5cbe1763 2021-08-13 op }
239 5cbe1763 2021-08-13 op
240 5cbe1763 2021-08-13 op static int
241 5cbe1763 2021-08-13 op select_non_dotdot(const struct dirent *d)
242 5cbe1763 2021-08-13 op {
243 5cbe1763 2021-08-13 op return strcmp(d->d_name, ".") && strcmp(d->d_name, "..");
244 65124267 2021-08-13 op }
245 65124267 2021-08-13 op
246 65124267 2021-08-13 op static inline void
247 65124267 2021-08-13 op send_dir(uint32_t peerid, const char *path)
248 65124267 2021-08-13 op {
249 65124267 2021-08-13 op struct dirent **names;
250 65124267 2021-08-13 op struct evbuffer *ev;
251 5d43215b 2021-08-13 op char *s;
252 5cbe1763 2021-08-13 op int (*selector)(const struct dirent *) = select_non_dot;
253 24a68158 2021-08-13 op int i, len, no;
254 65124267 2021-08-13 op
255 5d43215b 2021-08-13 op if (!has_suffix(path, "/")) {
256 5d43215b 2021-08-13 op if (asprintf(&s, "%s/", path) == -1)
257 5d43215b 2021-08-13 op die();
258 5d43215b 2021-08-13 op send_hdr(peerid, 30, s);
259 5d43215b 2021-08-13 op free(s);
260 5d43215b 2021-08-13 op return;
261 5d43215b 2021-08-13 op }
262 5d43215b 2021-08-13 op
263 5cbe1763 2021-08-13 op if (!strcmp(path, "/"))
264 5cbe1763 2021-08-13 op selector = select_non_dotdot;
265 5cbe1763 2021-08-13 op
266 65124267 2021-08-13 op if ((ev = evbuffer_new()) == NULL ||
267 5cbe1763 2021-08-13 op (len = scandir(path, &names, selector, alphasort)) == -1) {
268 24a68158 2021-08-13 op no = errno;
269 65124267 2021-08-13 op evbuffer_free(ev);
270 24a68158 2021-08-13 op send_errno(peerid, 40, "failure reading the directory", no);
271 65124267 2021-08-13 op return;
272 65124267 2021-08-13 op }
273 65124267 2021-08-13 op
274 65124267 2021-08-13 op evbuffer_add_printf(ev, "# Index of %s\n\n", path);
275 65124267 2021-08-13 op for (i = 0; i < len; ++i) {
276 65124267 2021-08-13 op evbuffer_add_printf(ev, "=> %s", names[i]->d_name);
277 65124267 2021-08-13 op if (names[i]->d_type == DT_DIR)
278 65124267 2021-08-13 op evbuffer_add(ev, "/", 1);
279 65124267 2021-08-13 op evbuffer_add(ev, "\n", 1);
280 65124267 2021-08-13 op }
281 65124267 2021-08-13 op
282 65124267 2021-08-13 op send_hdr(peerid, 20, "text/gemini");
283 65124267 2021-08-13 op fs_send_ui(IMSG_BUF, peerid, -1,
284 65124267 2021-08-13 op EVBUFFER_DATA(ev), EVBUFFER_LENGTH(ev));
285 65124267 2021-08-13 op fs_send_ui(IMSG_EOF, peerid, -1, NULL, 0);
286 65124267 2021-08-13 op
287 65124267 2021-08-13 op evbuffer_free(ev);
288 65124267 2021-08-13 op free(names);
289 65124267 2021-08-13 op }
290 65124267 2021-08-13 op
291 65124267 2021-08-13 op static void
292 65124267 2021-08-13 op handle_get_file(struct imsg *imsg, size_t datalen)
293 65124267 2021-08-13 op {
294 65124267 2021-08-13 op struct stat sb;
295 fb4dc49f 2021-08-13 op FILE *f;
296 65124267 2021-08-13 op char *data;
297 fb4dc49f 2021-08-13 op const char *meta = NULL;
298 fb4dc49f 2021-08-13 op
299 fb4dc49f 2021-08-13 op data = imsg->data;
300 fb4dc49f 2021-08-13 op data[datalen-1] = '\0';
301 fb4dc49f 2021-08-13 op
302 65124267 2021-08-13 op if ((f = fopen(data, "r")) == NULL) {
303 24a68158 2021-08-13 op send_errno(imsg->hdr.peerid, 51, "can't open", errno);
304 fb4dc49f 2021-08-13 op return;
305 fb4dc49f 2021-08-13 op }
306 fb4dc49f 2021-08-13 op
307 65124267 2021-08-13 op if (fstat(fileno(f), &sb) == -1) {
308 24a68158 2021-08-13 op send_errno(imsg->hdr.peerid, 40, "fstat", errno);
309 65124267 2021-08-13 op return;
310 fb4dc49f 2021-08-13 op }
311 fb4dc49f 2021-08-13 op
312 65124267 2021-08-13 op if (S_ISDIR(sb.st_mode)) {
313 65124267 2021-08-13 op fclose(f);
314 65124267 2021-08-13 op send_dir(imsg->hdr.peerid, data);
315 fb4dc49f 2021-08-13 op return;
316 fb4dc49f 2021-08-13 op }
317 fb4dc49f 2021-08-13 op
318 65124267 2021-08-13 op if ((meta = file_type(data)) == NULL) {
319 65124267 2021-08-13 op fclose(f);
320 65124267 2021-08-13 op send_hdr(imsg->hdr.peerid, 51,
321 65124267 2021-08-13 op "don't know how to visualize this file");
322 fb4dc49f 2021-08-13 op return;
323 fb4dc49f 2021-08-13 op }
324 fb4dc49f 2021-08-13 op
325 fb4dc49f 2021-08-13 op send_hdr(imsg->hdr.peerid, 20, meta);
326 fb4dc49f 2021-08-13 op send_file(imsg->hdr.peerid, f);
327 35e1f40a 2021-03-14 op }
328 35e1f40a 2021-03-14 op
329 35e1f40a 2021-03-14 op static void
330 35e1f40a 2021-03-14 op handle_quit(struct imsg *imsg, size_t datalen)
331 35e1f40a 2021-03-14 op {
332 2b409042 2021-09-15 op if (!safe_mode)
333 2b409042 2021-09-15 op unlink(crashed_file);
334 be97d6e6 2021-08-15 op
335 35e1f40a 2021-03-14 op event_loopbreak();
336 35e1f40a 2021-03-14 op }
337 35e1f40a 2021-03-14 op
338 35e1f40a 2021-03-14 op static void
339 740f578b 2021-03-15 op handle_bookmark_page(struct imsg *imsg, size_t datalen)
340 740f578b 2021-03-15 op {
341 740f578b 2021-03-15 op char *data;
342 740f578b 2021-03-15 op int res;
343 740f578b 2021-03-15 op FILE *f;
344 740f578b 2021-03-15 op
345 740f578b 2021-03-15 op data = imsg->data;
346 740f578b 2021-03-15 op if (data[datalen-1] != '\0')
347 740f578b 2021-03-15 op die();
348 740f578b 2021-03-15 op
349 740f578b 2021-03-15 op if ((f = fopen(bookmark_file, "a")) == NULL) {
350 740f578b 2021-03-15 op res = errno;
351 740f578b 2021-03-15 op goto end;
352 740f578b 2021-03-15 op }
353 740f578b 2021-03-15 op fprintf(f, "=> %s\n", data);
354 740f578b 2021-03-15 op fclose(f);
355 740f578b 2021-03-15 op
356 740f578b 2021-03-15 op res = 0;
357 740f578b 2021-03-15 op end:
358 bc10f6a5 2021-07-12 op fs_send_ui(IMSG_BOOKMARK_OK, 0, -1, &res, sizeof(res));
359 740f578b 2021-03-15 op }
360 740f578b 2021-03-15 op
361 740f578b 2021-03-15 op static void
362 3a227e9a 2021-03-18 op handle_save_cert(struct imsg *imsg, size_t datalen)
363 3a227e9a 2021-03-18 op {
364 3a227e9a 2021-03-18 op struct tofu_entry e;
365 3a227e9a 2021-03-18 op FILE *f;
366 3a227e9a 2021-03-18 op int res;
367 3a227e9a 2021-03-18 op
368 3a227e9a 2021-03-18 op /* TODO: traverse the file to avoid duplications? */
369 3a227e9a 2021-03-18 op
370 3a227e9a 2021-03-18 op if (datalen != sizeof(e))
371 3a227e9a 2021-03-18 op die();
372 3a227e9a 2021-03-18 op memcpy(&e, imsg->data, datalen);
373 3a227e9a 2021-03-18 op
374 3a227e9a 2021-03-18 op if ((f = fopen(known_hosts_file, "a")) == NULL) {
375 3a227e9a 2021-03-18 op res = errno;
376 3a227e9a 2021-03-18 op goto end;
377 3a227e9a 2021-03-18 op }
378 3a227e9a 2021-03-18 op fprintf(f, "%s %s %d\n", e.domain, e.hash, e.verified);
379 3a227e9a 2021-03-18 op fclose(f);
380 3a227e9a 2021-03-18 op
381 3a227e9a 2021-03-18 op res = 0;
382 3a227e9a 2021-03-18 op end:
383 bc10f6a5 2021-07-12 op fs_send_ui(IMSG_SAVE_CERT_OK, imsg->hdr.peerid, -1,
384 288fd238 2021-04-25 op &res, sizeof(res));
385 288fd238 2021-04-25 op }
386 288fd238 2021-04-25 op
387 288fd238 2021-04-25 op static void
388 288fd238 2021-04-25 op handle_update_cert(struct imsg *imsg, size_t datalen)
389 288fd238 2021-04-25 op {
390 288fd238 2021-04-25 op FILE *tmp, *f;
391 288fd238 2021-04-25 op struct tofu_entry entry;
392 288fd238 2021-04-25 op char sfn[PATH_MAX], *line = NULL, *t;
393 288fd238 2021-04-25 op size_t l, linesize = 0;
394 288fd238 2021-04-25 op ssize_t linelen;
395 288fd238 2021-04-25 op int fd, e, res = 0;
396 288fd238 2021-04-25 op
397 288fd238 2021-04-25 op if (datalen != sizeof(entry))
398 288fd238 2021-04-25 op die();
399 288fd238 2021-04-25 op memcpy(&entry, imsg->data, datalen);
400 288fd238 2021-04-25 op
401 288fd238 2021-04-25 op strlcpy(sfn, known_hosts_tmp, sizeof(sfn));
402 288fd238 2021-04-25 op if ((fd = mkstemp(sfn)) == -1 ||
403 288fd238 2021-04-25 op (tmp = fdopen(fd, "w")) == NULL) {
404 288fd238 2021-04-25 op if (fd != -1) {
405 288fd238 2021-04-25 op unlink(sfn);
406 288fd238 2021-04-25 op close(fd);
407 288fd238 2021-04-25 op }
408 288fd238 2021-04-25 op res = 0;
409 288fd238 2021-04-25 op goto end;
410 288fd238 2021-04-25 op }
411 288fd238 2021-04-25 op
412 288fd238 2021-04-25 op if ((f = fopen(known_hosts_file, "r")) == NULL) {
413 288fd238 2021-04-25 op unlink(sfn);
414 288fd238 2021-04-25 op fclose(tmp);
415 288fd238 2021-04-25 op res = 0;
416 288fd238 2021-04-25 op goto end;
417 288fd238 2021-04-25 op }
418 288fd238 2021-04-25 op
419 288fd238 2021-04-25 op l = strlen(entry.domain);
420 288fd238 2021-04-25 op while ((linelen = getline(&line, &linesize, f)) != -1) {
421 288fd238 2021-04-25 op if ((t = strstr(line, entry.domain)) != NULL &&
422 288fd238 2021-04-25 op (line[l] == ' ' || line[l] == '\t'))
423 288fd238 2021-04-25 op continue;
424 288fd238 2021-04-25 op /* line has a trailing \n */
425 288fd238 2021-04-25 op fprintf(tmp, "%s", line);
426 288fd238 2021-04-25 op }
427 288fd238 2021-04-25 op fprintf(tmp, "%s %s %d\n", entry.domain, entry.hash, entry.verified);
428 288fd238 2021-04-25 op
429 288fd238 2021-04-25 op free(line);
430 288fd238 2021-04-25 op e = ferror(tmp);
431 288fd238 2021-04-25 op
432 288fd238 2021-04-25 op fclose(tmp);
433 288fd238 2021-04-25 op fclose(f);
434 288fd238 2021-04-25 op
435 288fd238 2021-04-25 op if (e) {
436 288fd238 2021-04-25 op unlink(sfn);
437 288fd238 2021-04-25 op res = 0;
438 288fd238 2021-04-25 op goto end;
439 288fd238 2021-04-25 op }
440 288fd238 2021-04-25 op
441 288fd238 2021-04-25 op res = rename(sfn, known_hosts_file) != -1;
442 288fd238 2021-04-25 op
443 288fd238 2021-04-25 op end:
444 bc10f6a5 2021-07-12 op fs_send_ui(IMSG_UPDATE_CERT_OK, imsg->hdr.peerid, -1,
445 3a227e9a 2021-03-18 op &res, sizeof(res));
446 de2a69bb 2021-05-17 op }
447 de2a69bb 2021-05-17 op
448 de2a69bb 2021-05-17 op static void
449 de2a69bb 2021-05-17 op handle_file_open(struct imsg *imsg, size_t datalen)
450 de2a69bb 2021-05-17 op {
451 de2a69bb 2021-05-17 op char *path, *e;
452 de2a69bb 2021-05-17 op int fd;
453 de2a69bb 2021-05-17 op
454 de2a69bb 2021-05-17 op path = imsg->data;
455 de2a69bb 2021-05-17 op if (path[datalen-1] != '\0')
456 de2a69bb 2021-05-17 op die();
457 de2a69bb 2021-05-17 op
458 de2a69bb 2021-05-17 op if ((fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, 0644)) == -1) {
459 de2a69bb 2021-05-17 op e = strerror(errno);
460 bc10f6a5 2021-07-12 op fs_send_ui(IMSG_FILE_OPENED, imsg->hdr.peerid, -1,
461 de2a69bb 2021-05-17 op e, strlen(e)+1);
462 de2a69bb 2021-05-17 op } else
463 bc10f6a5 2021-07-12 op fs_send_ui(IMSG_FILE_OPENED, imsg->hdr.peerid, fd,
464 de2a69bb 2021-05-17 op NULL, 0);
465 3a227e9a 2021-03-18 op }
466 3a227e9a 2021-03-18 op
467 3a227e9a 2021-03-18 op static void
468 c7107cec 2021-04-01 op handle_session_start(struct imsg *imsg, size_t datalen)
469 c7107cec 2021-04-01 op {
470 c7107cec 2021-04-01 op if (datalen != 0)
471 c7107cec 2021-04-01 op die();
472 c7107cec 2021-04-01 op
473 c7107cec 2021-04-01 op if ((session = fopen(session_file, "w")) == NULL)
474 c7107cec 2021-04-01 op die();
475 c7107cec 2021-04-01 op }
476 c7107cec 2021-04-01 op
477 c7107cec 2021-04-01 op static void
478 c7107cec 2021-04-01 op handle_session_tab(struct imsg *imsg, size_t datalen)
479 c7107cec 2021-04-01 op {
480 87e3e801 2021-07-17 op char *url;
481 87e3e801 2021-07-17 op uint32_t flags;
482 58df4f47 2021-07-17 op
483 c7107cec 2021-04-01 op if (session == NULL)
484 c7107cec 2021-04-01 op die();
485 c7107cec 2021-04-01 op
486 87e3e801 2021-07-17 op flags = imsg->hdr.peerid;
487 58df4f47 2021-07-17 op url = imsg->data;
488 58df4f47 2021-07-17 op if (datalen == 0 || url[datalen-1] != '\0')
489 c7107cec 2021-04-01 op die();
490 58df4f47 2021-07-17 op fprintf(session, "%s", url);
491 87e3e801 2021-07-17 op
492 87e3e801 2021-07-17 op if (flags & TAB_CURRENT)
493 2f524b17 2021-07-17 op fprintf(session, " current ");
494 2f524b17 2021-07-17 op else
495 2f524b17 2021-07-17 op fprintf(session, " - ");
496 2f524b17 2021-07-17 op }
497 87e3e801 2021-07-17 op
498 2f524b17 2021-07-17 op static void
499 2f524b17 2021-07-17 op handle_session_tab_title(struct imsg *imsg, size_t datalen)
500 2f524b17 2021-07-17 op {
501 2f524b17 2021-07-17 op const char *title;
502 2f524b17 2021-07-17 op
503 2f524b17 2021-07-17 op title = imsg->data;
504 2f524b17 2021-07-17 op if (title == NULL) {
505 2f524b17 2021-07-17 op datalen = 1;
506 2f524b17 2021-07-17 op title = "";
507 2f524b17 2021-07-17 op }
508 2f524b17 2021-07-17 op
509 2f524b17 2021-07-17 op if (title[datalen-1] != '\0')
510 2f524b17 2021-07-17 op die();
511 2f524b17 2021-07-17 op
512 2f524b17 2021-07-17 op fprintf(session, "%s\n", title);
513 c7107cec 2021-04-01 op }
514 c7107cec 2021-04-01 op
515 c7107cec 2021-04-01 op static void
516 c7107cec 2021-04-01 op handle_session_end(struct imsg *imsg, size_t datalen)
517 c7107cec 2021-04-01 op {
518 c7107cec 2021-04-01 op if (session == NULL)
519 c7107cec 2021-04-01 op die();
520 c7107cec 2021-04-01 op fclose(session);
521 c7107cec 2021-04-01 op session = NULL;
522 c7107cec 2021-04-01 op }
523 c7107cec 2021-04-01 op
524 c7107cec 2021-04-01 op static void
525 1304bbdd 2021-03-15 op handle_dispatch_imsg(int fd, short ev, void *d)
526 35e1f40a 2021-03-14 op {
527 bc10f6a5 2021-07-12 op struct imsgev *iev = d;
528 ea080950 2021-07-20 op int e;
529 ea080950 2021-07-20 op
530 ea080950 2021-07-20 op if (dispatch_imsg(iev, ev, handlers, sizeof(handlers)) == -1) {
531 ea080950 2021-07-20 op /*
532 eb2ed626 2021-10-07 op * This should leave a ~/.cache/telescope/crashed file to
533 ea080950 2021-07-20 op * trigger about:crash on next run. Unfortunately, if
534 ea080950 2021-07-20 op * the main process dies the fs sticks around and
535 ea080950 2021-07-20 op * doesn't notice that the fd was closed. Why EV_READ
536 ea080950 2021-07-20 op * is not triggered when a fd is closed on the other end?
537 ea080950 2021-07-20 op */
538 ea080950 2021-07-20 op e = errno;
539 ea080950 2021-07-20 op if ((fd = open(crashed_file, O_CREAT|O_TRUNC|O_WRONLY, 0600))
540 ea080950 2021-07-20 op == -1)
541 ea080950 2021-07-20 op err(1, "open");
542 ea080950 2021-07-20 op close(fd);
543 ea080950 2021-07-20 op errx(1, "connection closed: %s", strerror(e));
544 ea080950 2021-07-20 op }
545 35e1f40a 2021-03-14 op }
546 35e1f40a 2021-03-14 op
547 bc10f6a5 2021-07-12 op static int
548 bc10f6a5 2021-07-12 op fs_send_ui(int type, uint32_t peerid, int fd, const void *data,
549 bc10f6a5 2021-07-12 op uint16_t datalen)
550 bc10f6a5 2021-07-12 op {
551 bc10f6a5 2021-07-12 op return imsg_compose_event(iev_ui, type, peerid, 0, fd,
552 bc10f6a5 2021-07-12 op data, datalen);
553 bc10f6a5 2021-07-12 op }
554 bc10f6a5 2021-07-12 op
555 eb2ed626 2021-10-07 op static size_t
556 eb2ed626 2021-10-07 op join_path(char *buf, const char *lhs, const char *rhs, size_t buflen)
557 35e1f40a 2021-03-14 op {
558 eb2ed626 2021-10-07 op strlcpy(buf, lhs, buflen);
559 eb2ed626 2021-10-07 op return strlcat(buf, rhs, buflen);
560 eb2ed626 2021-10-07 op }
561 d0fd368a 2021-07-15 op
562 eb2ed626 2021-10-07 op static void
563 eb2ed626 2021-10-07 op getenv_default(char *buf, const char *name, const char *def, size_t buflen)
564 eb2ed626 2021-10-07 op {
565 eb2ed626 2021-10-07 op size_t ret;
566 eb2ed626 2021-10-07 op char *home, *env;
567 740f578b 2021-03-15 op
568 eb2ed626 2021-10-07 op if ((home = getenv("HOME")) == NULL)
569 eb2ed626 2021-10-07 op errx(1, "HOME is not defined");
570 3a227e9a 2021-03-18 op
571 eb2ed626 2021-10-07 op if ((env = getenv(name)) != NULL)
572 eb2ed626 2021-10-07 op ret = strlcpy(buf, env, buflen);
573 eb2ed626 2021-10-07 op else
574 eb2ed626 2021-10-07 op ret = join_path(buf, home, def, buflen);
575 288fd238 2021-04-25 op
576 eb2ed626 2021-10-07 op if (ret >= buflen)
577 eb2ed626 2021-10-07 op errx(1, "buffer too small for %s", name);
578 eb2ed626 2021-10-07 op }
579 c7107cec 2021-04-01 op
580 eb2ed626 2021-10-07 op static void
581 eb2ed626 2021-10-07 op mkdirs(const char *path, mode_t mode)
582 eb2ed626 2021-10-07 op {
583 444dad86 2021-10-07 op char copy[PATH_MAX+1], orig[PATH_MAX+1], *parent;
584 ea080950 2021-07-20 op
585 eb2ed626 2021-10-07 op strlcpy(copy, path, sizeof(copy));
586 444dad86 2021-10-07 op strlcpy(orig, path, sizeof(orig));
587 eb2ed626 2021-10-07 op parent = dirname(copy);
588 eb2ed626 2021-10-07 op if (!strcmp(parent, "/"))
589 eb2ed626 2021-10-07 op return;
590 eb2ed626 2021-10-07 op mkdirs(parent, mode);
591 eb2ed626 2021-10-07 op
592 444dad86 2021-10-07 op if (mkdir(orig, mode) != 0) {
593 eb2ed626 2021-10-07 op if (errno == EEXIST)
594 eb2ed626 2021-10-07 op return;
595 444dad86 2021-10-07 op err(1, "can't mkdir %s", orig);
596 eb2ed626 2021-10-07 op }
597 eb2ed626 2021-10-07 op }
598 eb2ed626 2021-10-07 op
599 eb2ed626 2021-10-07 op static void
600 eb2ed626 2021-10-07 op xdg_init(void)
601 eb2ed626 2021-10-07 op {
602 eb2ed626 2021-10-07 op char *home, old_path[PATH_MAX];
603 eb2ed626 2021-10-07 op struct stat info;
604 eb2ed626 2021-10-07 op
605 eb2ed626 2021-10-07 op /* old path */
606 eb2ed626 2021-10-07 op if ((home = getenv("HOME")) == NULL)
607 eb2ed626 2021-10-07 op errx(1, "HOME is not defined");
608 eb2ed626 2021-10-07 op join_path(old_path, home, "/.telescope", sizeof(old_path));
609 eb2ed626 2021-10-07 op
610 eb2ed626 2021-10-07 op /* if ~/.telescope exists, use that instead of xdg dirs */
611 eb2ed626 2021-10-07 op if (stat(old_path, &info) == 0 && S_ISDIR(info.st_mode)) {
612 eb2ed626 2021-10-07 op join_path(config_path_base, home, "/.telescope",
613 eb2ed626 2021-10-07 op sizeof(config_path_base));
614 eb2ed626 2021-10-07 op join_path(data_path_base, home, "/.telescope",
615 eb2ed626 2021-10-07 op sizeof(data_path_base));
616 eb2ed626 2021-10-07 op join_path(cache_path_base, home, "/.telescope",
617 eb2ed626 2021-10-07 op sizeof(cache_path_base));
618 eb2ed626 2021-10-07 op return;
619 eb2ed626 2021-10-07 op }
620 eb2ed626 2021-10-07 op
621 eb2ed626 2021-10-07 op /* xdg paths */
622 eb2ed626 2021-10-07 op getenv_default(xdg_config_base, "XDG_CONFIG_HOME", "/.config",
623 eb2ed626 2021-10-07 op sizeof(xdg_config_base));
624 eb2ed626 2021-10-07 op getenv_default(xdg_data_base, "XDG_DATA_HOME", "/.local/share",
625 eb2ed626 2021-10-07 op sizeof(xdg_data_base));
626 eb2ed626 2021-10-07 op getenv_default(xdg_cache_base, "XDG_CACHE_HOME", "/.cache",
627 eb2ed626 2021-10-07 op sizeof(xdg_cache_base));
628 eb2ed626 2021-10-07 op
629 eb2ed626 2021-10-07 op join_path(config_path_base, xdg_config_base, "/telescope",
630 eb2ed626 2021-10-07 op sizeof(config_path_base));
631 eb2ed626 2021-10-07 op join_path(data_path_base, xdg_data_base, "/telescope",
632 eb2ed626 2021-10-07 op sizeof(data_path_base));
633 eb2ed626 2021-10-07 op join_path(cache_path_base, xdg_cache_base, "/telescope",
634 eb2ed626 2021-10-07 op sizeof(cache_path_base));
635 eb2ed626 2021-10-07 op
636 eb2ed626 2021-10-07 op mkdirs(xdg_config_base, S_IRWXU);
637 eb2ed626 2021-10-07 op mkdirs(xdg_data_base, S_IRWXU);
638 eb2ed626 2021-10-07 op mkdirs(xdg_cache_base, S_IRWXU);
639 eb2ed626 2021-10-07 op
640 eb2ed626 2021-10-07 op mkdirs(config_path_base, S_IRWXU);
641 eb2ed626 2021-10-07 op mkdirs(data_path_base, S_IRWXU);
642 eb2ed626 2021-10-07 op mkdirs(cache_path_base, S_IRWXU);
643 eb2ed626 2021-10-07 op }
644 eb2ed626 2021-10-07 op
645 eb2ed626 2021-10-07 op int
646 eb2ed626 2021-10-07 op fs_init(void)
647 eb2ed626 2021-10-07 op {
648 eb2ed626 2021-10-07 op xdg_init();
649 eb2ed626 2021-10-07 op
650 eb2ed626 2021-10-07 op join_path(config_path, config_path_base, "/config",
651 eb2ed626 2021-10-07 op sizeof(config_path));
652 eb2ed626 2021-10-07 op join_path(lockfile_path, cache_path_base, "/lock",
653 eb2ed626 2021-10-07 op sizeof(lockfile_path));
654 eb2ed626 2021-10-07 op join_path(bookmark_file, data_path_base, "/bookmarks.gmi",
655 eb2ed626 2021-10-07 op sizeof(bookmark_file));
656 eb2ed626 2021-10-07 op join_path(known_hosts_file, data_path_base, "/known_hosts",
657 eb2ed626 2021-10-07 op sizeof(known_hosts_file));
658 eb2ed626 2021-10-07 op join_path(known_hosts_tmp, cache_path_base,
659 eb2ed626 2021-10-07 op "/known_hosts.tmp.XXXXXXXXXX", sizeof(known_hosts_tmp));
660 eb2ed626 2021-10-07 op join_path(session_file, cache_path_base, "/session",
661 eb2ed626 2021-10-07 op sizeof(session_file));
662 eb2ed626 2021-10-07 op join_path(crashed_file, cache_path_base, "/crashed",
663 eb2ed626 2021-10-07 op sizeof(crashed_file));
664 eb2ed626 2021-10-07 op
665 3a227e9a 2021-03-18 op return 1;
666 3a227e9a 2021-03-18 op }
667 3a227e9a 2021-03-18 op
668 3a227e9a 2021-03-18 op int
669 6cc5fcfe 2021-07-08 op fs_main(void)
670 3a227e9a 2021-03-18 op {
671 6cc5fcfe 2021-07-08 op setproctitle("fs");
672 3a227e9a 2021-03-18 op
673 6cc5fcfe 2021-07-08 op fs_init();
674 6cc5fcfe 2021-07-08 op
675 35e1f40a 2021-03-14 op event_init();
676 35e1f40a 2021-03-14 op
677 bc10f6a5 2021-07-12 op /* Setup pipe and event handler to the main process */
678 bc10f6a5 2021-07-12 op if ((iev_ui = malloc(sizeof(*iev_ui))) == NULL)
679 6cc5fcfe 2021-07-08 op die();
680 bc10f6a5 2021-07-12 op imsg_init(&iev_ui->ibuf, 3);
681 bc10f6a5 2021-07-12 op iev_ui->handler = handle_dispatch_imsg;
682 bc10f6a5 2021-07-12 op iev_ui->events = EV_READ;
683 bc10f6a5 2021-07-12 op event_set(&iev_ui->ev, iev_ui->ibuf.fd, iev_ui->events,
684 bc10f6a5 2021-07-12 op iev_ui->handler, iev_ui);
685 bc10f6a5 2021-07-12 op event_add(&iev_ui->ev, NULL);
686 35e1f40a 2021-03-14 op
687 35e1f40a 2021-03-14 op sandbox_fs_process();
688 35e1f40a 2021-03-14 op
689 35e1f40a 2021-03-14 op event_dispatch();
690 35e1f40a 2021-03-14 op return 0;
691 35e1f40a 2021-03-14 op }
692 3a227e9a 2021-03-18 op
693 3a227e9a 2021-03-18 op
694 d0fd368a 2021-07-15 op
695 be97d6e6 2021-08-15 op /*
696 be97d6e6 2021-08-15 op * Check if the last time telescope crashed. The check is done by
697 be97d6e6 2021-08-15 op * looking at `crashed_file': if it exists then last time we crashed.
698 be97d6e6 2021-08-15 op * Then, while here, touch the file too. During IMSG_QUIT we'll
699 be97d6e6 2021-08-15 op * remove it.
700 be97d6e6 2021-08-15 op */
701 d0fd368a 2021-07-15 op int
702 b6171794 2021-07-20 op last_time_crashed(void)
703 b6171794 2021-07-20 op {
704 be97d6e6 2021-08-15 op int fd, crashed = 1;
705 b6171794 2021-07-20 op
706 2b409042 2021-09-15 op if (safe_mode)
707 2b409042 2021-09-15 op return 0;
708 2b409042 2021-09-15 op
709 be97d6e6 2021-08-15 op if (unlink(crashed_file) == -1 && errno == ENOENT)
710 be97d6e6 2021-08-15 op crashed = 0;
711 be97d6e6 2021-08-15 op
712 be97d6e6 2021-08-15 op if ((fd = open(crashed_file, O_CREAT|O_WRONLY, 0600)) == -1)
713 be97d6e6 2021-08-15 op return crashed;
714 b6171794 2021-07-20 op close(fd);
715 be97d6e6 2021-08-15 op
716 be97d6e6 2021-08-15 op return crashed;
717 b6171794 2021-07-20 op }
718 b6171794 2021-07-20 op
719 b6171794 2021-07-20 op int
720 d0fd368a 2021-07-15 op lock_session(void)
721 d0fd368a 2021-07-15 op {
722 d0fd368a 2021-07-15 op struct flock lock;
723 d0fd368a 2021-07-15 op int fd;
724 d0fd368a 2021-07-15 op
725 d0fd368a 2021-07-15 op if ((fd = open(lockfile_path, O_WRONLY|O_CREAT, 0600)) == -1)
726 d0fd368a 2021-07-15 op return -1;
727 d0fd368a 2021-07-15 op
728 d0fd368a 2021-07-15 op lock.l_start = 0;
729 d0fd368a 2021-07-15 op lock.l_len = 0;
730 d0fd368a 2021-07-15 op lock.l_type = F_WRLCK;
731 d0fd368a 2021-07-15 op lock.l_whence = SEEK_SET;
732 c6d03cf5 2021-04-25 op
733 d0fd368a 2021-07-15 op if (fcntl(fd, F_SETLK, &lock) == -1) {
734 d0fd368a 2021-07-15 op close(fd);
735 d0fd368a 2021-07-15 op return -1;
736 d0fd368a 2021-07-15 op }
737 d0fd368a 2021-07-15 op
738 d0fd368a 2021-07-15 op return fd;
739 d0fd368a 2021-07-15 op }
740 d0fd368a 2021-07-15 op
741 c6d03cf5 2021-04-25 op static int
742 c6d03cf5 2021-04-25 op parse_khost_line(char *line, char *tmp[3])
743 c6d03cf5 2021-04-25 op {
744 c6d03cf5 2021-04-25 op char **ap;
745 c6d03cf5 2021-04-25 op
746 c6d03cf5 2021-04-25 op for (ap = tmp; ap < &tmp[3] &&
747 c6d03cf5 2021-04-25 op (*ap = strsep(&line, " \t\n")) != NULL;) {
748 c6d03cf5 2021-04-25 op if (**ap != '\0')
749 c6d03cf5 2021-04-25 op ap++;
750 c6d03cf5 2021-04-25 op }
751 3a227e9a 2021-03-18 op
752 c6d03cf5 2021-04-25 op return ap == &tmp[3] && *line == '\0';
753 c6d03cf5 2021-04-25 op }
754 c6d03cf5 2021-04-25 op
755 3a227e9a 2021-03-18 op int
756 3a227e9a 2021-03-18 op load_certs(struct ohash *h)
757 3a227e9a 2021-03-18 op {
758 c6d03cf5 2021-04-25 op char *tmp[3], *line = NULL;
759 6cd6a9e1 2021-03-20 op const char *errstr;
760 ec1fa0b0 2021-04-25 op size_t lineno = 0, linesize = 0;
761 3a227e9a 2021-03-18 op ssize_t linelen;
762 3a227e9a 2021-03-18 op FILE *f;
763 3a227e9a 2021-03-18 op struct tofu_entry *e;
764 3a227e9a 2021-03-18 op
765 3a227e9a 2021-03-18 op if ((f = fopen(known_hosts_file, "r")) == NULL)
766 3a227e9a 2021-03-18 op return 0;
767 3a227e9a 2021-03-18 op
768 3a227e9a 2021-03-18 op while ((linelen = getline(&line, &linesize, f)) != -1) {
769 3a227e9a 2021-03-18 op if ((e = calloc(1, sizeof(*e))) == NULL)
770 3a227e9a 2021-03-18 op abort();
771 3a227e9a 2021-03-18 op
772 ec1fa0b0 2021-04-25 op lineno++;
773 3a227e9a 2021-03-18 op
774 c6d03cf5 2021-04-25 op if (parse_khost_line(line, tmp)) {
775 c6d03cf5 2021-04-25 op strlcpy(e->domain, tmp[0], sizeof(e->domain));
776 c6d03cf5 2021-04-25 op strlcpy(e->hash, tmp[1], sizeof(e->hash));
777 3a227e9a 2021-03-18 op
778 c6d03cf5 2021-04-25 op e->verified = strtonum(tmp[2], 0, 1, &errstr);
779 c6d03cf5 2021-04-25 op if (errstr != NULL)
780 c6d03cf5 2021-04-25 op errx(1, "%s:%zu verification for %s is %s: %s",
781 c6d03cf5 2021-04-25 op known_hosts_file, lineno,
782 c6d03cf5 2021-04-25 op e->domain, errstr, tmp[2]);
783 c6d03cf5 2021-04-25 op tofu_add(h, e);
784 c6d03cf5 2021-04-25 op } else {
785 ec1fa0b0 2021-04-25 op warnx("%s:%zu invalid entry",
786 ec1fa0b0 2021-04-25 op known_hosts_file, lineno);
787 ec1fa0b0 2021-04-25 op free(e);
788 c6d03cf5 2021-04-25 op }
789 3a227e9a 2021-03-18 op }
790 3a227e9a 2021-03-18 op
791 3a227e9a 2021-03-18 op free(line);
792 3a227e9a 2021-03-18 op return ferror(f);
793 3a227e9a 2021-03-18 op }
794 c7107cec 2021-04-01 op