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 de2a69bb 2021-05-17 op #include <fcntl.h>
29 35e1f40a 2021-03-14 op #include <limits.h>
30 35e1f40a 2021-03-14 op #include <stdio.h>
31 35e1f40a 2021-03-14 op #include <stdlib.h>
32 35e1f40a 2021-03-14 op #include <string.h>
33 35e1f40a 2021-03-14 op #include <unistd.h>
35 35e1f40a 2021-03-14 op static void die(void) __attribute__((__noreturn__));
36 35e1f40a 2021-03-14 op static void serve_bookmarks(uint32_t);
37 1b412079 2021-06-19 op static void send_page(struct imsg *, const char *);
38 35e1f40a 2021-03-14 op static void handle_get(struct imsg*, size_t);
39 35e1f40a 2021-03-14 op static void handle_quit(struct imsg*, size_t);
40 740f578b 2021-03-15 op static void handle_bookmark_page(struct imsg*, size_t);
41 3a227e9a 2021-03-18 op static void handle_save_cert(struct imsg*, size_t);
42 288fd238 2021-04-25 op static void handle_update_cert(struct imsg*, size_t);
43 de2a69bb 2021-05-17 op static void handle_file_open(struct imsg*, size_t);
44 c7107cec 2021-04-01 op static void handle_session_start(struct imsg*, size_t);
45 c7107cec 2021-04-01 op static void handle_session_tab(struct imsg*, size_t);
46 c7107cec 2021-04-01 op static void handle_session_end(struct imsg*, size_t);
47 1304bbdd 2021-03-15 op static void handle_dispatch_imsg(int, short, void*);
49 35e1f40a 2021-03-14 op static struct event imsgev;
50 35e1f40a 2021-03-14 op static struct imsgbuf *ibuf;
52 c7107cec 2021-04-01 op static FILE *session;
54 740f578b 2021-03-15 op static char bookmark_file[PATH_MAX];
55 288fd238 2021-04-25 op static char known_hosts_file[PATH_MAX], known_hosts_tmp[PATH_MAX];
56 c7107cec 2021-04-01 op static char session_file[PATH_MAX];
58 35e1f40a 2021-03-14 op static imsg_handlerfn *handlers[] = {
59 35e1f40a 2021-03-14 op [IMSG_GET] = handle_get,
60 35e1f40a 2021-03-14 op [IMSG_QUIT] = handle_quit,
61 740f578b 2021-03-15 op [IMSG_BOOKMARK_PAGE] = handle_bookmark_page,
62 3a227e9a 2021-03-18 op [IMSG_SAVE_CERT] = handle_save_cert,
63 288fd238 2021-04-25 op [IMSG_UPDATE_CERT] = handle_update_cert,
64 de2a69bb 2021-05-17 op [IMSG_FILE_OPEN] = handle_file_open,
65 c7107cec 2021-04-01 op [IMSG_SESSION_START] = handle_session_start,
66 c7107cec 2021-04-01 op [IMSG_SESSION_TAB] = handle_session_tab,
67 c7107cec 2021-04-01 op [IMSG_SESSION_END] = handle_session_end,
70 35e1f40a 2021-03-14 op static void __attribute__((__noreturn__))
73 35e1f40a 2021-03-14 op abort(); /* TODO */
77 35e1f40a 2021-03-14 op serve_bookmarks(uint32_t peerid)
79 35e1f40a 2021-03-14 op const char *t;
80 740f578b 2021-03-15 op char buf[BUFSIZ];
84 740f578b 2021-03-15 op if ((f = fopen(bookmark_file, "r")) == NULL) {
85 503425db 2021-04-01 op t = "# Bookmarks\n\n"
86 503425db 2021-04-01 op "No bookmarks yet!\n"
87 503425db 2021-04-01 op "Create ~/.telescope/bookmarks.gmi or use `bookmark-page'.\n";
88 35e1f40a 2021-03-14 op imsg_compose(ibuf, IMSG_BUF, peerid, 0, -1, t, strlen(t));
89 35e1f40a 2021-03-14 op imsg_compose(ibuf, IMSG_EOF, peerid, 0, -1, NULL, 0);
90 35e1f40a 2021-03-14 op imsg_flush(ibuf);
95 35e1f40a 2021-03-14 op r = fread(buf, 1, sizeof(buf), f);
96 35e1f40a 2021-03-14 op imsg_compose(ibuf, IMSG_BUF, peerid, 0, -1, buf, r);
97 35e1f40a 2021-03-14 op imsg_flush(ibuf);
98 35e1f40a 2021-03-14 op if (r != sizeof(buf))
102 35e1f40a 2021-03-14 op imsg_compose(ibuf, IMSG_EOF, peerid, 0, -1, NULL, 0);
103 35e1f40a 2021-03-14 op imsg_flush(ibuf);
109 1b412079 2021-06-19 op send_page(struct imsg *imsg, const char *page)
111 1b412079 2021-06-19 op imsg_compose(ibuf, IMSG_BUF, imsg->hdr.peerid, 0, -1,
112 1b412079 2021-06-19 op page, strlen(page));
113 1b412079 2021-06-19 op imsg_compose(ibuf, IMSG_EOF, imsg->hdr.peerid, 0, -1, NULL, 0);
114 1b412079 2021-06-19 op imsg_flush(ibuf);
118 35e1f40a 2021-03-14 op handle_get(struct imsg *imsg, size_t datalen)
121 35e1f40a 2021-03-14 op const char *p;
123 35e1f40a 2021-03-14 op data = imsg->data;
125 35e1f40a 2021-03-14 op if (data[datalen-1] != '\0')
128 1b412079 2021-06-19 op if (!strcmp(data, "about:about")) {
129 1b412079 2021-06-19 op send_page(imsg, about_about);
130 1b412079 2021-06-19 op } else if (!strcmp(data, "about:blank")) {
131 1b412079 2021-06-19 op send_page(imsg, about_blank);
132 35e1f40a 2021-03-14 op } else if (!strcmp(data, "about:bookmarks")) {
133 35e1f40a 2021-03-14 op serve_bookmarks(imsg->hdr.peerid);
134 1b412079 2021-06-19 op } else if (!strcmp(data, "about:help")) {
135 1b412079 2021-06-19 op send_page(imsg, about_help);
136 1b412079 2021-06-19 op } else if (!strcmp(data, "about:new")) {
137 1b412079 2021-06-19 op send_page(imsg, about_new);
139 35e1f40a 2021-03-14 op p = "# not found!\n";
140 35e1f40a 2021-03-14 op imsg_compose(ibuf, IMSG_BUF, imsg->hdr.peerid, 0, -1, p, strlen(p));
141 35e1f40a 2021-03-14 op imsg_compose(ibuf, IMSG_EOF, imsg->hdr.peerid, 0, -1, NULL, 0);
142 35e1f40a 2021-03-14 op imsg_flush(ibuf);
147 35e1f40a 2021-03-14 op handle_quit(struct imsg *imsg, size_t datalen)
149 35e1f40a 2021-03-14 op event_loopbreak();
153 740f578b 2021-03-15 op handle_bookmark_page(struct imsg *imsg, size_t datalen)
159 740f578b 2021-03-15 op data = imsg->data;
160 740f578b 2021-03-15 op if (data[datalen-1] != '\0')
163 740f578b 2021-03-15 op if ((f = fopen(bookmark_file, "a")) == NULL) {
167 740f578b 2021-03-15 op fprintf(f, "=> %s\n", data);
172 740f578b 2021-03-15 op imsg_compose(ibuf, IMSG_BOOKMARK_OK, 0, 0, -1, &res, sizeof(res));
173 740f578b 2021-03-15 op imsg_flush(ibuf);
177 3a227e9a 2021-03-18 op handle_save_cert(struct imsg *imsg, size_t datalen)
179 3a227e9a 2021-03-18 op struct tofu_entry e;
183 3a227e9a 2021-03-18 op /* TODO: traverse the file to avoid duplications? */
185 3a227e9a 2021-03-18 op if (datalen != sizeof(e))
187 3a227e9a 2021-03-18 op memcpy(&e, imsg->data, datalen);
189 3a227e9a 2021-03-18 op if ((f = fopen(known_hosts_file, "a")) == NULL) {
193 3a227e9a 2021-03-18 op fprintf(f, "%s %s %d\n", e.domain, e.hash, e.verified);
198 3a227e9a 2021-03-18 op imsg_compose(ibuf, IMSG_SAVE_CERT_OK, imsg->hdr.peerid, 0, -1,
199 288fd238 2021-04-25 op &res, sizeof(res));
200 288fd238 2021-04-25 op imsg_flush(ibuf);
204 288fd238 2021-04-25 op handle_update_cert(struct imsg *imsg, size_t datalen)
206 288fd238 2021-04-25 op FILE *tmp, *f;
207 288fd238 2021-04-25 op struct tofu_entry entry;
208 288fd238 2021-04-25 op char sfn[PATH_MAX], *line = NULL, *t;
209 288fd238 2021-04-25 op size_t l, linesize = 0;
210 288fd238 2021-04-25 op ssize_t linelen;
211 288fd238 2021-04-25 op int fd, e, res = 0;
213 288fd238 2021-04-25 op if (datalen != sizeof(entry))
215 288fd238 2021-04-25 op memcpy(&entry, imsg->data, datalen);
217 288fd238 2021-04-25 op strlcpy(sfn, known_hosts_tmp, sizeof(sfn));
218 288fd238 2021-04-25 op if ((fd = mkstemp(sfn)) == -1 ||
219 288fd238 2021-04-25 op (tmp = fdopen(fd, "w")) == NULL) {
220 288fd238 2021-04-25 op if (fd != -1) {
228 288fd238 2021-04-25 op if ((f = fopen(known_hosts_file, "r")) == NULL) {
235 288fd238 2021-04-25 op l = strlen(entry.domain);
236 288fd238 2021-04-25 op while ((linelen = getline(&line, &linesize, f)) != -1) {
237 288fd238 2021-04-25 op if ((t = strstr(line, entry.domain)) != NULL &&
238 288fd238 2021-04-25 op (line[l] == ' ' || line[l] == '\t'))
240 288fd238 2021-04-25 op /* line has a trailing \n */
241 288fd238 2021-04-25 op fprintf(tmp, "%s", line);
243 288fd238 2021-04-25 op fprintf(tmp, "%s %s %d\n", entry.domain, entry.hash, entry.verified);
246 288fd238 2021-04-25 op e = ferror(tmp);
257 288fd238 2021-04-25 op res = rename(sfn, known_hosts_file) != -1;
260 288fd238 2021-04-25 op imsg_compose(ibuf, IMSG_UPDATE_CERT_OK, imsg->hdr.peerid, 0, -1,
261 3a227e9a 2021-03-18 op &res, sizeof(res));
262 de2a69bb 2021-05-17 op imsg_flush(ibuf);
266 de2a69bb 2021-05-17 op handle_file_open(struct imsg *imsg, size_t datalen)
268 de2a69bb 2021-05-17 op char *path, *e;
271 de2a69bb 2021-05-17 op path = imsg->data;
272 de2a69bb 2021-05-17 op if (path[datalen-1] != '\0')
275 de2a69bb 2021-05-17 op if ((fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, 0644)) == -1) {
276 de2a69bb 2021-05-17 op e = strerror(errno);
277 de2a69bb 2021-05-17 op imsg_compose(ibuf, IMSG_FILE_OPENED, imsg->hdr.peerid, 0, -1,
278 de2a69bb 2021-05-17 op e, strlen(e)+1);
280 de2a69bb 2021-05-17 op imsg_compose(ibuf, IMSG_FILE_OPENED, imsg->hdr.peerid, 0, fd,
283 3a227e9a 2021-03-18 op imsg_flush(ibuf);
287 c7107cec 2021-04-01 op handle_session_start(struct imsg *imsg, size_t datalen)
289 c7107cec 2021-04-01 op if (datalen != 0)
292 c7107cec 2021-04-01 op if ((session = fopen(session_file, "w")) == NULL)
297 c7107cec 2021-04-01 op handle_session_tab(struct imsg *imsg, size_t datalen)
299 c7107cec 2021-04-01 op if (session == NULL)
302 c7107cec 2021-04-01 op if (datalen == 0)
305 c7107cec 2021-04-01 op /* skip the NUL-terminator */
306 c7107cec 2021-04-01 op fwrite(imsg->data, 1, datalen-1, session);
308 c7107cec 2021-04-01 op fprintf(session, "\n");
312 c7107cec 2021-04-01 op handle_session_end(struct imsg *imsg, size_t datalen)
314 c7107cec 2021-04-01 op if (session == NULL)
316 c7107cec 2021-04-01 op fclose(session);
317 c7107cec 2021-04-01 op session = NULL;
321 1304bbdd 2021-03-15 op handle_dispatch_imsg(int fd, short ev, void *d)
323 35e1f40a 2021-03-14 op struct imsgbuf *ibuf = d;
324 1304bbdd 2021-03-15 op dispatch_imsg(ibuf, handlers, sizeof(handlers));
328 3a227e9a 2021-03-18 op fs_init(void)
330 63a715ea 2021-03-18 op char dir[PATH_MAX];
332 63a715ea 2021-03-18 op strlcpy(dir, getenv("HOME"), sizeof(dir));
333 63a715ea 2021-03-18 op strlcat(dir, "/.telescope", sizeof(dir));
334 63a715ea 2021-03-18 op mkdir(dir, 0700);
336 740f578b 2021-03-15 op strlcpy(bookmark_file, getenv("HOME"), sizeof(bookmark_file));
337 740f578b 2021-03-15 op strlcat(bookmark_file, "/.telescope/bookmarks.gmi", sizeof(bookmark_file));
339 3a227e9a 2021-03-18 op strlcpy(known_hosts_file, getenv("HOME"), sizeof(known_hosts_file));
340 3a227e9a 2021-03-18 op strlcat(known_hosts_file, "/.telescope/known_hosts", sizeof(known_hosts_file));
342 288fd238 2021-04-25 op strlcpy(known_hosts_tmp, getenv("HOME"), sizeof(known_hosts_tmp));
343 288fd238 2021-04-25 op strlcat(known_hosts_tmp, "/.telescope/known_hosts.tmp.XXXXXXXXXX",
344 288fd238 2021-04-25 op sizeof(known_hosts_file));
346 c7107cec 2021-04-01 op strlcpy(session_file, getenv("HOME"), sizeof(session_file));
347 c7107cec 2021-04-01 op strlcat(session_file, "/.telescope/session", sizeof(session_file));
353 6cc5fcfe 2021-07-08 op fs_main(void)
355 6cc5fcfe 2021-07-08 op setproctitle("fs");
359 35e1f40a 2021-03-14 op event_init();
361 6cc5fcfe 2021-07-08 op if ((ibuf = calloc(1, sizeof(*ibuf))) == NULL)
363 6cc5fcfe 2021-07-08 op imsg_init(ibuf, 3);
364 1304bbdd 2021-03-15 op event_set(&imsgev, ibuf->fd, EV_READ | EV_PERSIST, handle_dispatch_imsg, ibuf);
365 35e1f40a 2021-03-14 op event_add(&imsgev, NULL);
367 35e1f40a 2021-03-14 op sandbox_fs_process();
369 35e1f40a 2021-03-14 op event_dispatch();
376 c6d03cf5 2021-04-25 op parse_khost_line(char *line, char *tmp[3])
380 c6d03cf5 2021-04-25 op for (ap = tmp; ap < &tmp[3] &&
381 c6d03cf5 2021-04-25 op (*ap = strsep(&line, " \t\n")) != NULL;) {
382 c6d03cf5 2021-04-25 op if (**ap != '\0')
386 c6d03cf5 2021-04-25 op return ap == &tmp[3] && *line == '\0';
390 3a227e9a 2021-03-18 op load_certs(struct ohash *h)
392 c6d03cf5 2021-04-25 op char *tmp[3], *line = NULL;
393 6cd6a9e1 2021-03-20 op const char *errstr;
394 ec1fa0b0 2021-04-25 op size_t lineno = 0, linesize = 0;
395 3a227e9a 2021-03-18 op ssize_t linelen;
397 3a227e9a 2021-03-18 op struct tofu_entry *e;
399 3a227e9a 2021-03-18 op if ((f = fopen(known_hosts_file, "r")) == NULL)
402 3a227e9a 2021-03-18 op while ((linelen = getline(&line, &linesize, f)) != -1) {
403 3a227e9a 2021-03-18 op if ((e = calloc(1, sizeof(*e))) == NULL)
408 c6d03cf5 2021-04-25 op if (parse_khost_line(line, tmp)) {
409 c6d03cf5 2021-04-25 op strlcpy(e->domain, tmp[0], sizeof(e->domain));
410 c6d03cf5 2021-04-25 op strlcpy(e->hash, tmp[1], sizeof(e->hash));
412 c6d03cf5 2021-04-25 op e->verified = strtonum(tmp[2], 0, 1, &errstr);
413 c6d03cf5 2021-04-25 op if (errstr != NULL)
414 c6d03cf5 2021-04-25 op errx(1, "%s:%zu verification for %s is %s: %s",
415 c6d03cf5 2021-04-25 op known_hosts_file, lineno,
416 c6d03cf5 2021-04-25 op e->domain, errstr, tmp[2]);
417 c6d03cf5 2021-04-25 op tofu_add(h, e);
419 ec1fa0b0 2021-04-25 op warnx("%s:%zu invalid entry",
420 ec1fa0b0 2021-04-25 op known_hosts_file, lineno);
426 3a227e9a 2021-03-18 op return ferror(f);
430 c7107cec 2021-04-01 op load_last_session(void (*cb)(const char*))
432 c7107cec 2021-04-01 op char *nl, *line = NULL;
434 c7107cec 2021-04-01 op size_t linesize = 0;
435 c7107cec 2021-04-01 op ssize_t linelen;
436 c7107cec 2021-04-01 op FILE *session;
438 51dc2fa4 2021-06-19 op if ((session = fopen(session_file, "r")) == NULL) {
439 51dc2fa4 2021-06-19 op /* first time? */
440 51dc2fa4 2021-06-19 op cb("about:help");
444 c7107cec 2021-04-01 op while ((linelen = getline(&line, &linesize, session)) != -1) {
445 c7107cec 2021-04-01 op if ((nl = strchr(line, '\n')) != NULL)
451 c7107cec 2021-04-01 op e = ferror(session);
452 c7107cec 2021-04-01 op fclose(session);