2 35e1f40a 2021-03-14 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
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.
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.
18 740f578b 2021-03-15 op * Handles the data in ~/.telescope
20 3a227e9a 2021-03-18 op * TODO: add some form of locking on the files
23 35e1f40a 2021-03-14 op #include "telescope.h"
25 6cd6a9e1 2021-03-20 op #include <sys/stat.h>
27 35e1f40a 2021-03-14 op #include <errno.h>
28 35e1f40a 2021-03-14 op #include <limits.h>
29 35e1f40a 2021-03-14 op #include <stdio.h>
30 35e1f40a 2021-03-14 op #include <stdlib.h>
31 35e1f40a 2021-03-14 op #include <string.h>
32 35e1f40a 2021-03-14 op #include <unistd.h>
34 35e1f40a 2021-03-14 op static void die(void) __attribute__((__noreturn__));
35 35e1f40a 2021-03-14 op static void serve_bookmarks(uint32_t);
36 35e1f40a 2021-03-14 op static void handle_get(struct imsg*, size_t);
37 35e1f40a 2021-03-14 op static void handle_quit(struct imsg*, size_t);
38 740f578b 2021-03-15 op static void handle_bookmark_page(struct imsg*, size_t);
39 3a227e9a 2021-03-18 op static void handle_save_cert(struct imsg*, size_t);
40 288fd238 2021-04-25 op static void handle_update_cert(struct imsg*, size_t);
41 c7107cec 2021-04-01 op static void handle_session_start(struct imsg*, size_t);
42 c7107cec 2021-04-01 op static void handle_session_tab(struct imsg*, size_t);
43 c7107cec 2021-04-01 op static void handle_session_end(struct imsg*, size_t);
44 1304bbdd 2021-03-15 op static void handle_dispatch_imsg(int, short, void*);
46 35e1f40a 2021-03-14 op static struct event imsgev;
47 35e1f40a 2021-03-14 op static struct imsgbuf *ibuf;
49 c7107cec 2021-04-01 op static FILE *session;
51 740f578b 2021-03-15 op static char bookmark_file[PATH_MAX];
52 288fd238 2021-04-25 op static char known_hosts_file[PATH_MAX], known_hosts_tmp[PATH_MAX];
53 c7107cec 2021-04-01 op static char session_file[PATH_MAX];
55 35e1f40a 2021-03-14 op static imsg_handlerfn *handlers[] = {
56 35e1f40a 2021-03-14 op [IMSG_GET] = handle_get,
57 35e1f40a 2021-03-14 op [IMSG_QUIT] = handle_quit,
58 740f578b 2021-03-15 op [IMSG_BOOKMARK_PAGE] = handle_bookmark_page,
59 3a227e9a 2021-03-18 op [IMSG_SAVE_CERT] = handle_save_cert,
60 288fd238 2021-04-25 op [IMSG_UPDATE_CERT] = handle_update_cert,
61 c7107cec 2021-04-01 op [IMSG_SESSION_START] = handle_session_start,
62 c7107cec 2021-04-01 op [IMSG_SESSION_TAB] = handle_session_tab,
63 c7107cec 2021-04-01 op [IMSG_SESSION_END] = handle_session_end,
66 35e1f40a 2021-03-14 op static void __attribute__((__noreturn__))
69 35e1f40a 2021-03-14 op abort(); /* TODO */
73 35e1f40a 2021-03-14 op serve_bookmarks(uint32_t peerid)
75 35e1f40a 2021-03-14 op const char *t;
76 740f578b 2021-03-15 op char buf[BUFSIZ];
80 740f578b 2021-03-15 op if ((f = fopen(bookmark_file, "r")) == NULL) {
81 503425db 2021-04-01 op t = "# Bookmarks\n\n"
82 503425db 2021-04-01 op "No bookmarks yet!\n"
83 503425db 2021-04-01 op "Create ~/.telescope/bookmarks.gmi or use `bookmark-page'.\n";
84 35e1f40a 2021-03-14 op imsg_compose(ibuf, IMSG_BUF, peerid, 0, -1, t, strlen(t));
85 35e1f40a 2021-03-14 op imsg_compose(ibuf, IMSG_EOF, peerid, 0, -1, NULL, 0);
86 35e1f40a 2021-03-14 op imsg_flush(ibuf);
91 35e1f40a 2021-03-14 op r = fread(buf, 1, sizeof(buf), f);
92 35e1f40a 2021-03-14 op imsg_compose(ibuf, IMSG_BUF, peerid, 0, -1, buf, r);
93 35e1f40a 2021-03-14 op imsg_flush(ibuf);
94 35e1f40a 2021-03-14 op if (r != sizeof(buf))
98 35e1f40a 2021-03-14 op imsg_compose(ibuf, IMSG_EOF, peerid, 0, -1, NULL, 0);
99 35e1f40a 2021-03-14 op imsg_flush(ibuf);
105 35e1f40a 2021-03-14 op handle_get(struct imsg *imsg, size_t datalen)
108 35e1f40a 2021-03-14 op const char *p;
110 35e1f40a 2021-03-14 op data = imsg->data;
112 35e1f40a 2021-03-14 op if (data[datalen-1] != '\0')
115 35e1f40a 2021-03-14 op if (!strcmp(data, "about:new")) {
116 35e1f40a 2021-03-14 op imsg_compose(ibuf, IMSG_BUF, imsg->hdr.peerid, 0, -1,
117 35e1f40a 2021-03-14 op about_new, strlen(about_new));
118 35e1f40a 2021-03-14 op imsg_compose(ibuf, IMSG_EOF, imsg->hdr.peerid, 0, -1, NULL, 0);
119 35e1f40a 2021-03-14 op imsg_flush(ibuf);
120 35e1f40a 2021-03-14 op } else if (!strcmp(data, "about:bookmarks")) {
121 35e1f40a 2021-03-14 op serve_bookmarks(imsg->hdr.peerid);
123 35e1f40a 2021-03-14 op p = "# not found!\n";
124 35e1f40a 2021-03-14 op imsg_compose(ibuf, IMSG_BUF, imsg->hdr.peerid, 0, -1, p, strlen(p));
125 35e1f40a 2021-03-14 op imsg_compose(ibuf, IMSG_EOF, imsg->hdr.peerid, 0, -1, NULL, 0);
126 35e1f40a 2021-03-14 op imsg_flush(ibuf);
131 35e1f40a 2021-03-14 op handle_quit(struct imsg *imsg, size_t datalen)
133 35e1f40a 2021-03-14 op event_loopbreak();
137 740f578b 2021-03-15 op handle_bookmark_page(struct imsg *imsg, size_t datalen)
143 740f578b 2021-03-15 op data = imsg->data;
144 740f578b 2021-03-15 op if (data[datalen-1] != '\0')
147 740f578b 2021-03-15 op if ((f = fopen(bookmark_file, "a")) == NULL) {
151 740f578b 2021-03-15 op fprintf(f, "=> %s\n", data);
156 740f578b 2021-03-15 op imsg_compose(ibuf, IMSG_BOOKMARK_OK, 0, 0, -1, &res, sizeof(res));
157 740f578b 2021-03-15 op imsg_flush(ibuf);
161 3a227e9a 2021-03-18 op handle_save_cert(struct imsg *imsg, size_t datalen)
163 3a227e9a 2021-03-18 op struct tofu_entry e;
167 3a227e9a 2021-03-18 op /* TODO: traverse the file to avoid duplications? */
169 3a227e9a 2021-03-18 op if (datalen != sizeof(e))
171 3a227e9a 2021-03-18 op memcpy(&e, imsg->data, datalen);
173 3a227e9a 2021-03-18 op if ((f = fopen(known_hosts_file, "a")) == NULL) {
177 3a227e9a 2021-03-18 op fprintf(f, "%s %s %d\n", e.domain, e.hash, e.verified);
182 3a227e9a 2021-03-18 op imsg_compose(ibuf, IMSG_SAVE_CERT_OK, imsg->hdr.peerid, 0, -1,
183 288fd238 2021-04-25 op &res, sizeof(res));
184 288fd238 2021-04-25 op imsg_flush(ibuf);
188 288fd238 2021-04-25 op handle_update_cert(struct imsg *imsg, size_t datalen)
190 288fd238 2021-04-25 op FILE *tmp, *f;
191 288fd238 2021-04-25 op struct tofu_entry entry;
192 288fd238 2021-04-25 op char sfn[PATH_MAX], *line = NULL, *t;
193 288fd238 2021-04-25 op size_t l, linesize = 0;
194 288fd238 2021-04-25 op ssize_t linelen;
195 288fd238 2021-04-25 op int fd, e, res = 0;
197 288fd238 2021-04-25 op if (datalen != sizeof(entry))
199 288fd238 2021-04-25 op memcpy(&entry, imsg->data, datalen);
201 288fd238 2021-04-25 op strlcpy(sfn, known_hosts_tmp, sizeof(sfn));
202 288fd238 2021-04-25 op if ((fd = mkstemp(sfn)) == -1 ||
203 288fd238 2021-04-25 op (tmp = fdopen(fd, "w")) == NULL) {
204 288fd238 2021-04-25 op if (fd != -1) {
212 288fd238 2021-04-25 op if ((f = fopen(known_hosts_file, "r")) == NULL) {
219 288fd238 2021-04-25 op l = strlen(entry.domain);
220 288fd238 2021-04-25 op while ((linelen = getline(&line, &linesize, f)) != -1) {
221 288fd238 2021-04-25 op if ((t = strstr(line, entry.domain)) != NULL &&
222 288fd238 2021-04-25 op (line[l] == ' ' || line[l] == '\t'))
224 288fd238 2021-04-25 op /* line has a trailing \n */
225 288fd238 2021-04-25 op fprintf(tmp, "%s", line);
227 288fd238 2021-04-25 op fprintf(tmp, "%s %s %d\n", entry.domain, entry.hash, entry.verified);
230 288fd238 2021-04-25 op e = ferror(tmp);
241 288fd238 2021-04-25 op res = rename(sfn, known_hosts_file) != -1;
244 288fd238 2021-04-25 op imsg_compose(ibuf, IMSG_UPDATE_CERT_OK, imsg->hdr.peerid, 0, -1,
245 3a227e9a 2021-03-18 op &res, sizeof(res));
246 3a227e9a 2021-03-18 op imsg_flush(ibuf);
250 c7107cec 2021-04-01 op handle_session_start(struct imsg *imsg, size_t datalen)
252 c7107cec 2021-04-01 op if (datalen != 0)
255 c7107cec 2021-04-01 op if ((session = fopen(session_file, "w")) == NULL)
260 c7107cec 2021-04-01 op handle_session_tab(struct imsg *imsg, size_t datalen)
262 c7107cec 2021-04-01 op if (session == NULL)
265 c7107cec 2021-04-01 op if (datalen == 0)
268 c7107cec 2021-04-01 op /* skip the NUL-terminator */
269 c7107cec 2021-04-01 op fwrite(imsg->data, 1, datalen-1, session);
271 c7107cec 2021-04-01 op fprintf(session, "\n");
275 c7107cec 2021-04-01 op handle_session_end(struct imsg *imsg, size_t datalen)
277 c7107cec 2021-04-01 op if (session == NULL)
279 c7107cec 2021-04-01 op fclose(session);
280 c7107cec 2021-04-01 op session = NULL;
284 1304bbdd 2021-03-15 op handle_dispatch_imsg(int fd, short ev, void *d)
286 35e1f40a 2021-03-14 op struct imsgbuf *ibuf = d;
287 1304bbdd 2021-03-15 op dispatch_imsg(ibuf, handlers, sizeof(handlers));
291 3a227e9a 2021-03-18 op fs_init(void)
293 63a715ea 2021-03-18 op char dir[PATH_MAX];
295 63a715ea 2021-03-18 op strlcpy(dir, getenv("HOME"), sizeof(dir));
296 63a715ea 2021-03-18 op strlcat(dir, "/.telescope", sizeof(dir));
297 63a715ea 2021-03-18 op mkdir(dir, 0700);
299 740f578b 2021-03-15 op strlcpy(bookmark_file, getenv("HOME"), sizeof(bookmark_file));
300 740f578b 2021-03-15 op strlcat(bookmark_file, "/.telescope/bookmarks.gmi", sizeof(bookmark_file));
302 3a227e9a 2021-03-18 op strlcpy(known_hosts_file, getenv("HOME"), sizeof(known_hosts_file));
303 3a227e9a 2021-03-18 op strlcat(known_hosts_file, "/.telescope/known_hosts", sizeof(known_hosts_file));
305 288fd238 2021-04-25 op strlcpy(known_hosts_tmp, getenv("HOME"), sizeof(known_hosts_tmp));
306 288fd238 2021-04-25 op strlcat(known_hosts_tmp, "/.telescope/known_hosts.tmp.XXXXXXXXXX",
307 288fd238 2021-04-25 op sizeof(known_hosts_file));
309 c7107cec 2021-04-01 op strlcpy(session_file, getenv("HOME"), sizeof(session_file));
310 c7107cec 2021-04-01 op strlcat(session_file, "/.telescope/session", sizeof(session_file));
316 3a227e9a 2021-03-18 op fs_main(struct imsgbuf *b)
320 35e1f40a 2021-03-14 op event_init();
322 1304bbdd 2021-03-15 op event_set(&imsgev, ibuf->fd, EV_READ | EV_PERSIST, handle_dispatch_imsg, ibuf);
323 35e1f40a 2021-03-14 op event_add(&imsgev, NULL);
325 35e1f40a 2021-03-14 op sandbox_fs_process();
327 35e1f40a 2021-03-14 op event_dispatch();
334 c6d03cf5 2021-04-25 op parse_khost_line(char *line, char *tmp[3])
338 c6d03cf5 2021-04-25 op for (ap = tmp; ap < &tmp[3] &&
339 c6d03cf5 2021-04-25 op (*ap = strsep(&line, " \t\n")) != NULL;) {
340 c6d03cf5 2021-04-25 op if (**ap != '\0')
344 c6d03cf5 2021-04-25 op return ap == &tmp[3] && *line == '\0';
348 3a227e9a 2021-03-18 op load_certs(struct ohash *h)
350 c6d03cf5 2021-04-25 op char *tmp[3], *line = NULL;
351 6cd6a9e1 2021-03-20 op const char *errstr;
352 ec1fa0b0 2021-04-25 op size_t lineno = 0, linesize = 0;
353 3a227e9a 2021-03-18 op ssize_t linelen;
355 3a227e9a 2021-03-18 op struct tofu_entry *e;
357 3a227e9a 2021-03-18 op if ((f = fopen(known_hosts_file, "r")) == NULL)
360 3a227e9a 2021-03-18 op while ((linelen = getline(&line, &linesize, f)) != -1) {
361 3a227e9a 2021-03-18 op if ((e = calloc(1, sizeof(*e))) == NULL)
366 c6d03cf5 2021-04-25 op if (parse_khost_line(line, tmp)) {
367 c6d03cf5 2021-04-25 op strlcpy(e->domain, tmp[0], sizeof(e->domain));
368 c6d03cf5 2021-04-25 op strlcpy(e->hash, tmp[1], sizeof(e->hash));
370 c6d03cf5 2021-04-25 op e->verified = strtonum(tmp[2], 0, 1, &errstr);
371 c6d03cf5 2021-04-25 op if (errstr != NULL)
372 c6d03cf5 2021-04-25 op errx(1, "%s:%zu verification for %s is %s: %s",
373 c6d03cf5 2021-04-25 op known_hosts_file, lineno,
374 c6d03cf5 2021-04-25 op e->domain, errstr, tmp[2]);
375 c6d03cf5 2021-04-25 op tofu_add(h, e);
377 ec1fa0b0 2021-04-25 op warnx("%s:%zu invalid entry",
378 ec1fa0b0 2021-04-25 op known_hosts_file, lineno);
384 3a227e9a 2021-03-18 op return ferror(f);
388 c7107cec 2021-04-01 op load_last_session(void (*cb)(const char*))
390 c7107cec 2021-04-01 op char *nl, *line = NULL;
392 c7107cec 2021-04-01 op size_t linesize = 0;
393 c7107cec 2021-04-01 op ssize_t linelen;
394 c7107cec 2021-04-01 op FILE *session;
396 c7107cec 2021-04-01 op if ((session = fopen(session_file, "r")) == NULL)
399 c7107cec 2021-04-01 op while ((linelen = getline(&line, &linesize, session)) != -1) {
400 c7107cec 2021-04-01 op if ((nl = strchr(line, '\n')) != NULL)
406 c7107cec 2021-04-01 op e = ferror(session);
407 c7107cec 2021-04-01 op fclose(session);