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 35e1f40a 2021-03-14 op static void handle_get(struct imsg*, size_t);
38 35e1f40a 2021-03-14 op static void handle_quit(struct imsg*, size_t);
39 740f578b 2021-03-15 op static void handle_bookmark_page(struct imsg*, size_t);
40 3a227e9a 2021-03-18 op static void handle_save_cert(struct imsg*, size_t);
41 288fd238 2021-04-25 op static void handle_update_cert(struct imsg*, size_t);
42 de2a69bb 2021-05-17 op static void handle_file_open(struct imsg*, size_t);
43 c7107cec 2021-04-01 op static void handle_session_start(struct imsg*, size_t);
44 c7107cec 2021-04-01 op static void handle_session_tab(struct imsg*, size_t);
45 c7107cec 2021-04-01 op static void handle_session_end(struct imsg*, size_t);
46 1304bbdd 2021-03-15 op static void handle_dispatch_imsg(int, short, void*);
48 35e1f40a 2021-03-14 op static struct event imsgev;
49 35e1f40a 2021-03-14 op static struct imsgbuf *ibuf;
51 c7107cec 2021-04-01 op static FILE *session;
53 740f578b 2021-03-15 op static char bookmark_file[PATH_MAX];
54 288fd238 2021-04-25 op static char known_hosts_file[PATH_MAX], known_hosts_tmp[PATH_MAX];
55 c7107cec 2021-04-01 op static char session_file[PATH_MAX];
57 35e1f40a 2021-03-14 op static imsg_handlerfn *handlers[] = {
58 35e1f40a 2021-03-14 op [IMSG_GET] = handle_get,
59 35e1f40a 2021-03-14 op [IMSG_QUIT] = handle_quit,
60 740f578b 2021-03-15 op [IMSG_BOOKMARK_PAGE] = handle_bookmark_page,
61 3a227e9a 2021-03-18 op [IMSG_SAVE_CERT] = handle_save_cert,
62 288fd238 2021-04-25 op [IMSG_UPDATE_CERT] = handle_update_cert,
63 de2a69bb 2021-05-17 op [IMSG_FILE_OPEN] = handle_file_open,
64 c7107cec 2021-04-01 op [IMSG_SESSION_START] = handle_session_start,
65 c7107cec 2021-04-01 op [IMSG_SESSION_TAB] = handle_session_tab,
66 c7107cec 2021-04-01 op [IMSG_SESSION_END] = handle_session_end,
69 35e1f40a 2021-03-14 op static void __attribute__((__noreturn__))
72 35e1f40a 2021-03-14 op abort(); /* TODO */
76 35e1f40a 2021-03-14 op serve_bookmarks(uint32_t peerid)
78 35e1f40a 2021-03-14 op const char *t;
79 740f578b 2021-03-15 op char buf[BUFSIZ];
83 740f578b 2021-03-15 op if ((f = fopen(bookmark_file, "r")) == NULL) {
84 503425db 2021-04-01 op t = "# Bookmarks\n\n"
85 503425db 2021-04-01 op "No bookmarks yet!\n"
86 503425db 2021-04-01 op "Create ~/.telescope/bookmarks.gmi or use `bookmark-page'.\n";
87 35e1f40a 2021-03-14 op imsg_compose(ibuf, IMSG_BUF, peerid, 0, -1, t, strlen(t));
88 35e1f40a 2021-03-14 op imsg_compose(ibuf, IMSG_EOF, peerid, 0, -1, NULL, 0);
89 35e1f40a 2021-03-14 op imsg_flush(ibuf);
94 35e1f40a 2021-03-14 op r = fread(buf, 1, sizeof(buf), f);
95 35e1f40a 2021-03-14 op imsg_compose(ibuf, IMSG_BUF, peerid, 0, -1, buf, r);
96 35e1f40a 2021-03-14 op imsg_flush(ibuf);
97 35e1f40a 2021-03-14 op if (r != sizeof(buf))
101 35e1f40a 2021-03-14 op imsg_compose(ibuf, IMSG_EOF, peerid, 0, -1, NULL, 0);
102 35e1f40a 2021-03-14 op imsg_flush(ibuf);
108 35e1f40a 2021-03-14 op handle_get(struct imsg *imsg, size_t datalen)
111 35e1f40a 2021-03-14 op const char *p;
113 35e1f40a 2021-03-14 op data = imsg->data;
115 35e1f40a 2021-03-14 op if (data[datalen-1] != '\0')
118 35e1f40a 2021-03-14 op if (!strcmp(data, "about:new")) {
119 35e1f40a 2021-03-14 op imsg_compose(ibuf, IMSG_BUF, imsg->hdr.peerid, 0, -1,
120 35e1f40a 2021-03-14 op about_new, strlen(about_new));
121 35e1f40a 2021-03-14 op imsg_compose(ibuf, IMSG_EOF, imsg->hdr.peerid, 0, -1, NULL, 0);
122 35e1f40a 2021-03-14 op imsg_flush(ibuf);
123 35e1f40a 2021-03-14 op } else if (!strcmp(data, "about:bookmarks")) {
124 35e1f40a 2021-03-14 op serve_bookmarks(imsg->hdr.peerid);
126 35e1f40a 2021-03-14 op p = "# not found!\n";
127 35e1f40a 2021-03-14 op imsg_compose(ibuf, IMSG_BUF, imsg->hdr.peerid, 0, -1, p, strlen(p));
128 35e1f40a 2021-03-14 op imsg_compose(ibuf, IMSG_EOF, imsg->hdr.peerid, 0, -1, NULL, 0);
129 35e1f40a 2021-03-14 op imsg_flush(ibuf);
134 35e1f40a 2021-03-14 op handle_quit(struct imsg *imsg, size_t datalen)
136 35e1f40a 2021-03-14 op event_loopbreak();
140 740f578b 2021-03-15 op handle_bookmark_page(struct imsg *imsg, size_t datalen)
146 740f578b 2021-03-15 op data = imsg->data;
147 740f578b 2021-03-15 op if (data[datalen-1] != '\0')
150 740f578b 2021-03-15 op if ((f = fopen(bookmark_file, "a")) == NULL) {
154 740f578b 2021-03-15 op fprintf(f, "=> %s\n", data);
159 740f578b 2021-03-15 op imsg_compose(ibuf, IMSG_BOOKMARK_OK, 0, 0, -1, &res, sizeof(res));
160 740f578b 2021-03-15 op imsg_flush(ibuf);
164 3a227e9a 2021-03-18 op handle_save_cert(struct imsg *imsg, size_t datalen)
166 3a227e9a 2021-03-18 op struct tofu_entry e;
170 3a227e9a 2021-03-18 op /* TODO: traverse the file to avoid duplications? */
172 3a227e9a 2021-03-18 op if (datalen != sizeof(e))
174 3a227e9a 2021-03-18 op memcpy(&e, imsg->data, datalen);
176 3a227e9a 2021-03-18 op if ((f = fopen(known_hosts_file, "a")) == NULL) {
180 3a227e9a 2021-03-18 op fprintf(f, "%s %s %d\n", e.domain, e.hash, e.verified);
185 3a227e9a 2021-03-18 op imsg_compose(ibuf, IMSG_SAVE_CERT_OK, imsg->hdr.peerid, 0, -1,
186 288fd238 2021-04-25 op &res, sizeof(res));
187 288fd238 2021-04-25 op imsg_flush(ibuf);
191 288fd238 2021-04-25 op handle_update_cert(struct imsg *imsg, size_t datalen)
193 288fd238 2021-04-25 op FILE *tmp, *f;
194 288fd238 2021-04-25 op struct tofu_entry entry;
195 288fd238 2021-04-25 op char sfn[PATH_MAX], *line = NULL, *t;
196 288fd238 2021-04-25 op size_t l, linesize = 0;
197 288fd238 2021-04-25 op ssize_t linelen;
198 288fd238 2021-04-25 op int fd, e, res = 0;
200 288fd238 2021-04-25 op if (datalen != sizeof(entry))
202 288fd238 2021-04-25 op memcpy(&entry, imsg->data, datalen);
204 288fd238 2021-04-25 op strlcpy(sfn, known_hosts_tmp, sizeof(sfn));
205 288fd238 2021-04-25 op if ((fd = mkstemp(sfn)) == -1 ||
206 288fd238 2021-04-25 op (tmp = fdopen(fd, "w")) == NULL) {
207 288fd238 2021-04-25 op if (fd != -1) {
215 288fd238 2021-04-25 op if ((f = fopen(known_hosts_file, "r")) == NULL) {
222 288fd238 2021-04-25 op l = strlen(entry.domain);
223 288fd238 2021-04-25 op while ((linelen = getline(&line, &linesize, f)) != -1) {
224 288fd238 2021-04-25 op if ((t = strstr(line, entry.domain)) != NULL &&
225 288fd238 2021-04-25 op (line[l] == ' ' || line[l] == '\t'))
227 288fd238 2021-04-25 op /* line has a trailing \n */
228 288fd238 2021-04-25 op fprintf(tmp, "%s", line);
230 288fd238 2021-04-25 op fprintf(tmp, "%s %s %d\n", entry.domain, entry.hash, entry.verified);
233 288fd238 2021-04-25 op e = ferror(tmp);
244 288fd238 2021-04-25 op res = rename(sfn, known_hosts_file) != -1;
247 288fd238 2021-04-25 op imsg_compose(ibuf, IMSG_UPDATE_CERT_OK, imsg->hdr.peerid, 0, -1,
248 3a227e9a 2021-03-18 op &res, sizeof(res));
249 de2a69bb 2021-05-17 op imsg_flush(ibuf);
253 de2a69bb 2021-05-17 op handle_file_open(struct imsg *imsg, size_t datalen)
255 de2a69bb 2021-05-17 op char *path, *e;
258 de2a69bb 2021-05-17 op path = imsg->data;
259 de2a69bb 2021-05-17 op if (path[datalen-1] != '\0')
262 de2a69bb 2021-05-17 op if ((fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, 0644)) == -1) {
263 de2a69bb 2021-05-17 op e = strerror(errno);
264 de2a69bb 2021-05-17 op imsg_compose(ibuf, IMSG_FILE_OPENED, imsg->hdr.peerid, 0, -1,
265 de2a69bb 2021-05-17 op e, strlen(e)+1);
267 de2a69bb 2021-05-17 op imsg_compose(ibuf, IMSG_FILE_OPENED, imsg->hdr.peerid, 0, fd,
270 3a227e9a 2021-03-18 op imsg_flush(ibuf);
274 c7107cec 2021-04-01 op handle_session_start(struct imsg *imsg, size_t datalen)
276 c7107cec 2021-04-01 op if (datalen != 0)
279 c7107cec 2021-04-01 op if ((session = fopen(session_file, "w")) == NULL)
284 c7107cec 2021-04-01 op handle_session_tab(struct imsg *imsg, size_t datalen)
286 c7107cec 2021-04-01 op if (session == NULL)
289 c7107cec 2021-04-01 op if (datalen == 0)
292 c7107cec 2021-04-01 op /* skip the NUL-terminator */
293 c7107cec 2021-04-01 op fwrite(imsg->data, 1, datalen-1, session);
295 c7107cec 2021-04-01 op fprintf(session, "\n");
299 c7107cec 2021-04-01 op handle_session_end(struct imsg *imsg, size_t datalen)
301 c7107cec 2021-04-01 op if (session == NULL)
303 c7107cec 2021-04-01 op fclose(session);
304 c7107cec 2021-04-01 op session = NULL;
308 1304bbdd 2021-03-15 op handle_dispatch_imsg(int fd, short ev, void *d)
310 35e1f40a 2021-03-14 op struct imsgbuf *ibuf = d;
311 1304bbdd 2021-03-15 op dispatch_imsg(ibuf, handlers, sizeof(handlers));
315 3a227e9a 2021-03-18 op fs_init(void)
317 63a715ea 2021-03-18 op char dir[PATH_MAX];
319 63a715ea 2021-03-18 op strlcpy(dir, getenv("HOME"), sizeof(dir));
320 63a715ea 2021-03-18 op strlcat(dir, "/.telescope", sizeof(dir));
321 63a715ea 2021-03-18 op mkdir(dir, 0700);
323 740f578b 2021-03-15 op strlcpy(bookmark_file, getenv("HOME"), sizeof(bookmark_file));
324 740f578b 2021-03-15 op strlcat(bookmark_file, "/.telescope/bookmarks.gmi", sizeof(bookmark_file));
326 3a227e9a 2021-03-18 op strlcpy(known_hosts_file, getenv("HOME"), sizeof(known_hosts_file));
327 3a227e9a 2021-03-18 op strlcat(known_hosts_file, "/.telescope/known_hosts", sizeof(known_hosts_file));
329 288fd238 2021-04-25 op strlcpy(known_hosts_tmp, getenv("HOME"), sizeof(known_hosts_tmp));
330 288fd238 2021-04-25 op strlcat(known_hosts_tmp, "/.telescope/known_hosts.tmp.XXXXXXXXXX",
331 288fd238 2021-04-25 op sizeof(known_hosts_file));
333 c7107cec 2021-04-01 op strlcpy(session_file, getenv("HOME"), sizeof(session_file));
334 c7107cec 2021-04-01 op strlcat(session_file, "/.telescope/session", sizeof(session_file));
340 3a227e9a 2021-03-18 op fs_main(struct imsgbuf *b)
344 35e1f40a 2021-03-14 op event_init();
346 1304bbdd 2021-03-15 op event_set(&imsgev, ibuf->fd, EV_READ | EV_PERSIST, handle_dispatch_imsg, ibuf);
347 35e1f40a 2021-03-14 op event_add(&imsgev, NULL);
349 35e1f40a 2021-03-14 op sandbox_fs_process();
351 35e1f40a 2021-03-14 op event_dispatch();
358 c6d03cf5 2021-04-25 op parse_khost_line(char *line, char *tmp[3])
362 c6d03cf5 2021-04-25 op for (ap = tmp; ap < &tmp[3] &&
363 c6d03cf5 2021-04-25 op (*ap = strsep(&line, " \t\n")) != NULL;) {
364 c6d03cf5 2021-04-25 op if (**ap != '\0')
368 c6d03cf5 2021-04-25 op return ap == &tmp[3] && *line == '\0';
372 3a227e9a 2021-03-18 op load_certs(struct ohash *h)
374 c6d03cf5 2021-04-25 op char *tmp[3], *line = NULL;
375 6cd6a9e1 2021-03-20 op const char *errstr;
376 ec1fa0b0 2021-04-25 op size_t lineno = 0, linesize = 0;
377 3a227e9a 2021-03-18 op ssize_t linelen;
379 3a227e9a 2021-03-18 op struct tofu_entry *e;
381 3a227e9a 2021-03-18 op if ((f = fopen(known_hosts_file, "r")) == NULL)
384 3a227e9a 2021-03-18 op while ((linelen = getline(&line, &linesize, f)) != -1) {
385 3a227e9a 2021-03-18 op if ((e = calloc(1, sizeof(*e))) == NULL)
390 c6d03cf5 2021-04-25 op if (parse_khost_line(line, tmp)) {
391 c6d03cf5 2021-04-25 op strlcpy(e->domain, tmp[0], sizeof(e->domain));
392 c6d03cf5 2021-04-25 op strlcpy(e->hash, tmp[1], sizeof(e->hash));
394 c6d03cf5 2021-04-25 op e->verified = strtonum(tmp[2], 0, 1, &errstr);
395 c6d03cf5 2021-04-25 op if (errstr != NULL)
396 c6d03cf5 2021-04-25 op errx(1, "%s:%zu verification for %s is %s: %s",
397 c6d03cf5 2021-04-25 op known_hosts_file, lineno,
398 c6d03cf5 2021-04-25 op e->domain, errstr, tmp[2]);
399 c6d03cf5 2021-04-25 op tofu_add(h, e);
401 ec1fa0b0 2021-04-25 op warnx("%s:%zu invalid entry",
402 ec1fa0b0 2021-04-25 op known_hosts_file, lineno);
408 3a227e9a 2021-03-18 op return ferror(f);
412 c7107cec 2021-04-01 op load_last_session(void (*cb)(const char*))
414 c7107cec 2021-04-01 op char *nl, *line = NULL;
416 c7107cec 2021-04-01 op size_t linesize = 0;
417 c7107cec 2021-04-01 op ssize_t linelen;
418 c7107cec 2021-04-01 op FILE *session;
420 c7107cec 2021-04-01 op if ((session = fopen(session_file, "r")) == NULL)
423 c7107cec 2021-04-01 op while ((linelen = getline(&line, &linesize, session)) != -1) {
424 c7107cec 2021-04-01 op if ((nl = strchr(line, '\n')) != NULL)
430 c7107cec 2021-04-01 op e = ferror(session);
431 c7107cec 2021-04-01 op fclose(session);