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 6cd6a9e1 2021-03-20 op #include <sys/stat.h>
25 35e1f40a 2021-03-14 op #include <errno.h>
26 de2a69bb 2021-05-17 op #include <fcntl.h>
27 35e1f40a 2021-03-14 op #include <limits.h>
28 35e1f40a 2021-03-14 op #include <stdio.h>
29 35e1f40a 2021-03-14 op #include <stdlib.h>
30 35e1f40a 2021-03-14 op #include <string.h>
31 35e1f40a 2021-03-14 op #include <unistd.h>
33 4913b479 2021-07-12 op #include "telescope.h"
34 5a824be4 2021-07-13 op #include "pages.h"
36 35e1f40a 2021-03-14 op static void die(void) __attribute__((__noreturn__));
37 35e1f40a 2021-03-14 op static void serve_bookmarks(uint32_t);
38 a2728733 2021-07-18 op static void send_page(struct imsg *, const uint8_t *, size_t);
39 35e1f40a 2021-03-14 op static void handle_get(struct imsg*, size_t);
40 35e1f40a 2021-03-14 op static void handle_quit(struct imsg*, size_t);
41 740f578b 2021-03-15 op static void handle_bookmark_page(struct imsg*, size_t);
42 3a227e9a 2021-03-18 op static void handle_save_cert(struct imsg*, size_t);
43 288fd238 2021-04-25 op static void handle_update_cert(struct imsg*, size_t);
44 de2a69bb 2021-05-17 op static void handle_file_open(struct imsg*, size_t);
45 c7107cec 2021-04-01 op static void handle_session_start(struct imsg*, size_t);
46 c7107cec 2021-04-01 op static void handle_session_tab(struct imsg*, size_t);
47 2f524b17 2021-07-17 op static void handle_session_tab_title(struct imsg*, size_t);
48 c7107cec 2021-04-01 op static void handle_session_end(struct imsg*, size_t);
49 1304bbdd 2021-03-15 op static void handle_dispatch_imsg(int, short, void*);
50 bc10f6a5 2021-07-12 op static int fs_send_ui(int, uint32_t, int, const void *, uint16_t);
52 bc10f6a5 2021-07-12 op static struct imsgev *iev_ui;
53 c7107cec 2021-04-01 op static FILE *session;
55 d0fd368a 2021-07-15 op static char lockfile_path[PATH_MAX];
56 740f578b 2021-03-15 op static char bookmark_file[PATH_MAX];
57 288fd238 2021-04-25 op static char known_hosts_file[PATH_MAX], known_hosts_tmp[PATH_MAX];
58 ea080950 2021-07-20 op static char crashed_file[PATH_MAX];
60 87e3e801 2021-07-17 op char session_file[PATH_MAX];
62 35e1f40a 2021-03-14 op static imsg_handlerfn *handlers[] = {
63 35e1f40a 2021-03-14 op [IMSG_GET] = handle_get,
64 35e1f40a 2021-03-14 op [IMSG_QUIT] = handle_quit,
65 740f578b 2021-03-15 op [IMSG_BOOKMARK_PAGE] = handle_bookmark_page,
66 3a227e9a 2021-03-18 op [IMSG_SAVE_CERT] = handle_save_cert,
67 288fd238 2021-04-25 op [IMSG_UPDATE_CERT] = handle_update_cert,
68 de2a69bb 2021-05-17 op [IMSG_FILE_OPEN] = handle_file_open,
69 c7107cec 2021-04-01 op [IMSG_SESSION_START] = handle_session_start,
70 c7107cec 2021-04-01 op [IMSG_SESSION_TAB] = handle_session_tab,
71 2f524b17 2021-07-17 op [IMSG_SESSION_TAB_TITLE] = handle_session_tab_title,
72 c7107cec 2021-04-01 op [IMSG_SESSION_END] = handle_session_end,
75 35e1f40a 2021-03-14 op static void __attribute__((__noreturn__))
78 35e1f40a 2021-03-14 op abort(); /* TODO */
82 35e1f40a 2021-03-14 op serve_bookmarks(uint32_t peerid)
84 35e1f40a 2021-03-14 op const char *t;
85 740f578b 2021-03-15 op char buf[BUFSIZ];
89 740f578b 2021-03-15 op if ((f = fopen(bookmark_file, "r")) == NULL) {
90 503425db 2021-04-01 op t = "# Bookmarks\n\n"
91 503425db 2021-04-01 op "No bookmarks yet!\n"
92 503425db 2021-04-01 op "Create ~/.telescope/bookmarks.gmi or use `bookmark-page'.\n";
93 bc10f6a5 2021-07-12 op fs_send_ui(IMSG_BUF, peerid, -1, t, strlen(t));
94 bc10f6a5 2021-07-12 op fs_send_ui(IMSG_EOF, peerid, -1, NULL, 0);
99 35e1f40a 2021-03-14 op r = fread(buf, 1, sizeof(buf), f);
100 bc10f6a5 2021-07-12 op fs_send_ui(IMSG_BUF, peerid, -1, buf, r);
101 35e1f40a 2021-03-14 op if (r != sizeof(buf))
105 bc10f6a5 2021-07-12 op fs_send_ui(IMSG_EOF, peerid, -1, NULL, 0);
111 a2728733 2021-07-18 op send_page(struct imsg *imsg, const uint8_t *page, size_t len)
113 a2728733 2021-07-18 op fs_send_ui(IMSG_BUF, imsg->hdr.peerid, -1, page, len);
114 bc10f6a5 2021-07-12 op fs_send_ui(IMSG_EOF, imsg->hdr.peerid, -1, NULL, 0);
118 35e1f40a 2021-03-14 op handle_get(struct imsg *imsg, size_t datalen)
120 a2728733 2021-07-18 op const char *data, *p;
122 a2728733 2021-07-18 op struct page {
123 a2728733 2021-07-18 op const char *name;
124 a2728733 2021-07-18 op void (*handle)(uint32_t);
125 a2728733 2021-07-18 op const uint8_t *data;
127 a2728733 2021-07-18 op } pages[] = {
128 a2728733 2021-07-18 op {"about:about", NULL, about_about, about_about_len},
129 a2728733 2021-07-18 op {"about:blank", NULL, about_blank, about_blank_len},
130 a2728733 2021-07-18 op {"about:bookmarks", serve_bookmarks, 0, 0},
131 8064b849 2021-07-20 op {"about:crash", NULL, about_crash, about_crash_len},
132 a2728733 2021-07-18 op {"about:help", NULL, about_help, about_help_len},
133 4745c100 2021-07-18 op {"about:license", NULL, about_license, about_license_len},
134 a2728733 2021-07-18 op {"about:new", NULL, about_new, about_new_len},
137 35e1f40a 2021-03-14 op data = imsg->data;
139 35e1f40a 2021-03-14 op if (data[datalen-1] != '\0')
142 a2728733 2021-07-18 op for (i = 0; i < sizeof(pages)/sizeof(pages[0]); ++i) {
143 a2728733 2021-07-18 op if (strcmp(data, pages[i].name) != 0)
146 a2728733 2021-07-18 op if (pages[i].handle != NULL)
147 a2728733 2021-07-18 op pages[i].handle(imsg->hdr.peerid);
149 a2728733 2021-07-18 op send_page(imsg, pages[i].data, pages[i].len);
153 a2728733 2021-07-18 op p = "# not found!\n";
154 a2728733 2021-07-18 op fs_send_ui(IMSG_BUF, imsg->hdr.peerid, -1, p, strlen(p));
155 a2728733 2021-07-18 op fs_send_ui(IMSG_EOF, imsg->hdr.peerid, -1, NULL, 0);
159 35e1f40a 2021-03-14 op handle_quit(struct imsg *imsg, size_t datalen)
161 35e1f40a 2021-03-14 op event_loopbreak();
165 740f578b 2021-03-15 op handle_bookmark_page(struct imsg *imsg, size_t datalen)
171 740f578b 2021-03-15 op data = imsg->data;
172 740f578b 2021-03-15 op if (data[datalen-1] != '\0')
175 740f578b 2021-03-15 op if ((f = fopen(bookmark_file, "a")) == NULL) {
179 740f578b 2021-03-15 op fprintf(f, "=> %s\n", data);
184 bc10f6a5 2021-07-12 op fs_send_ui(IMSG_BOOKMARK_OK, 0, -1, &res, sizeof(res));
188 3a227e9a 2021-03-18 op handle_save_cert(struct imsg *imsg, size_t datalen)
190 3a227e9a 2021-03-18 op struct tofu_entry e;
194 3a227e9a 2021-03-18 op /* TODO: traverse the file to avoid duplications? */
196 3a227e9a 2021-03-18 op if (datalen != sizeof(e))
198 3a227e9a 2021-03-18 op memcpy(&e, imsg->data, datalen);
200 3a227e9a 2021-03-18 op if ((f = fopen(known_hosts_file, "a")) == NULL) {
204 3a227e9a 2021-03-18 op fprintf(f, "%s %s %d\n", e.domain, e.hash, e.verified);
209 bc10f6a5 2021-07-12 op fs_send_ui(IMSG_SAVE_CERT_OK, imsg->hdr.peerid, -1,
210 288fd238 2021-04-25 op &res, sizeof(res));
214 288fd238 2021-04-25 op handle_update_cert(struct imsg *imsg, size_t datalen)
216 288fd238 2021-04-25 op FILE *tmp, *f;
217 288fd238 2021-04-25 op struct tofu_entry entry;
218 288fd238 2021-04-25 op char sfn[PATH_MAX], *line = NULL, *t;
219 288fd238 2021-04-25 op size_t l, linesize = 0;
220 288fd238 2021-04-25 op ssize_t linelen;
221 288fd238 2021-04-25 op int fd, e, res = 0;
223 288fd238 2021-04-25 op if (datalen != sizeof(entry))
225 288fd238 2021-04-25 op memcpy(&entry, imsg->data, datalen);
227 288fd238 2021-04-25 op strlcpy(sfn, known_hosts_tmp, sizeof(sfn));
228 288fd238 2021-04-25 op if ((fd = mkstemp(sfn)) == -1 ||
229 288fd238 2021-04-25 op (tmp = fdopen(fd, "w")) == NULL) {
230 288fd238 2021-04-25 op if (fd != -1) {
238 288fd238 2021-04-25 op if ((f = fopen(known_hosts_file, "r")) == NULL) {
245 288fd238 2021-04-25 op l = strlen(entry.domain);
246 288fd238 2021-04-25 op while ((linelen = getline(&line, &linesize, f)) != -1) {
247 288fd238 2021-04-25 op if ((t = strstr(line, entry.domain)) != NULL &&
248 288fd238 2021-04-25 op (line[l] == ' ' || line[l] == '\t'))
250 288fd238 2021-04-25 op /* line has a trailing \n */
251 288fd238 2021-04-25 op fprintf(tmp, "%s", line);
253 288fd238 2021-04-25 op fprintf(tmp, "%s %s %d\n", entry.domain, entry.hash, entry.verified);
256 288fd238 2021-04-25 op e = ferror(tmp);
267 288fd238 2021-04-25 op res = rename(sfn, known_hosts_file) != -1;
270 bc10f6a5 2021-07-12 op fs_send_ui(IMSG_UPDATE_CERT_OK, imsg->hdr.peerid, -1,
271 3a227e9a 2021-03-18 op &res, sizeof(res));
275 de2a69bb 2021-05-17 op handle_file_open(struct imsg *imsg, size_t datalen)
277 de2a69bb 2021-05-17 op char *path, *e;
280 de2a69bb 2021-05-17 op path = imsg->data;
281 de2a69bb 2021-05-17 op if (path[datalen-1] != '\0')
284 de2a69bb 2021-05-17 op if ((fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, 0644)) == -1) {
285 de2a69bb 2021-05-17 op e = strerror(errno);
286 bc10f6a5 2021-07-12 op fs_send_ui(IMSG_FILE_OPENED, imsg->hdr.peerid, -1,
287 de2a69bb 2021-05-17 op e, strlen(e)+1);
289 bc10f6a5 2021-07-12 op fs_send_ui(IMSG_FILE_OPENED, imsg->hdr.peerid, fd,
294 c7107cec 2021-04-01 op handle_session_start(struct imsg *imsg, size_t datalen)
296 c7107cec 2021-04-01 op if (datalen != 0)
299 c7107cec 2021-04-01 op if ((session = fopen(session_file, "w")) == NULL)
304 c7107cec 2021-04-01 op handle_session_tab(struct imsg *imsg, size_t datalen)
307 87e3e801 2021-07-17 op uint32_t flags;
309 c7107cec 2021-04-01 op if (session == NULL)
312 87e3e801 2021-07-17 op flags = imsg->hdr.peerid;
313 58df4f47 2021-07-17 op url = imsg->data;
314 58df4f47 2021-07-17 op if (datalen == 0 || url[datalen-1] != '\0')
316 58df4f47 2021-07-17 op fprintf(session, "%s", url);
318 87e3e801 2021-07-17 op if (flags & TAB_CURRENT)
319 2f524b17 2021-07-17 op fprintf(session, " current ");
321 2f524b17 2021-07-17 op fprintf(session, " - ");
325 2f524b17 2021-07-17 op handle_session_tab_title(struct imsg *imsg, size_t datalen)
327 2f524b17 2021-07-17 op const char *title;
329 2f524b17 2021-07-17 op title = imsg->data;
330 2f524b17 2021-07-17 op if (title == NULL) {
335 2f524b17 2021-07-17 op if (title[datalen-1] != '\0')
338 2f524b17 2021-07-17 op fprintf(session, "%s\n", title);
342 c7107cec 2021-04-01 op handle_session_end(struct imsg *imsg, size_t datalen)
344 c7107cec 2021-04-01 op if (session == NULL)
346 c7107cec 2021-04-01 op fclose(session);
347 c7107cec 2021-04-01 op session = NULL;
351 1304bbdd 2021-03-15 op handle_dispatch_imsg(int fd, short ev, void *d)
353 bc10f6a5 2021-07-12 op struct imsgev *iev = d;
356 ea080950 2021-07-20 op if (dispatch_imsg(iev, ev, handlers, sizeof(handlers)) == -1) {
358 ea080950 2021-07-20 op * This should leave a ~/.telescope/crashed file to
359 ea080950 2021-07-20 op * trigger about:crash on next run. Unfortunately, if
360 ea080950 2021-07-20 op * the main process dies the fs sticks around and
361 ea080950 2021-07-20 op * doesn't notice that the fd was closed. Why EV_READ
362 ea080950 2021-07-20 op * is not triggered when a fd is closed on the other end?
365 ea080950 2021-07-20 op if ((fd = open(crashed_file, O_CREAT|O_TRUNC|O_WRONLY, 0600))
367 ea080950 2021-07-20 op err(1, "open");
369 ea080950 2021-07-20 op errx(1, "connection closed: %s", strerror(e));
374 bc10f6a5 2021-07-12 op fs_send_ui(int type, uint32_t peerid, int fd, const void *data,
375 bc10f6a5 2021-07-12 op uint16_t datalen)
377 bc10f6a5 2021-07-12 op return imsg_compose_event(iev_ui, type, peerid, 0, fd,
378 bc10f6a5 2021-07-12 op data, datalen);
382 3a227e9a 2021-03-18 op fs_init(void)
384 63a715ea 2021-03-18 op char dir[PATH_MAX];
386 63a715ea 2021-03-18 op strlcpy(dir, getenv("HOME"), sizeof(dir));
387 63a715ea 2021-03-18 op strlcat(dir, "/.telescope", sizeof(dir));
388 63a715ea 2021-03-18 op mkdir(dir, 0700);
390 d0fd368a 2021-07-15 op strlcpy(lockfile_path, getenv("HOME"), sizeof(lockfile_path));
391 d0fd368a 2021-07-15 op strlcat(lockfile_path, "/.telescope/lock", sizeof(lockfile_path));
393 740f578b 2021-03-15 op strlcpy(bookmark_file, getenv("HOME"), sizeof(bookmark_file));
394 740f578b 2021-03-15 op strlcat(bookmark_file, "/.telescope/bookmarks.gmi", sizeof(bookmark_file));
396 3a227e9a 2021-03-18 op strlcpy(known_hosts_file, getenv("HOME"), sizeof(known_hosts_file));
397 3a227e9a 2021-03-18 op strlcat(known_hosts_file, "/.telescope/known_hosts", sizeof(known_hosts_file));
399 288fd238 2021-04-25 op strlcpy(known_hosts_tmp, getenv("HOME"), sizeof(known_hosts_tmp));
400 288fd238 2021-04-25 op strlcat(known_hosts_tmp, "/.telescope/known_hosts.tmp.XXXXXXXXXX",
401 288fd238 2021-04-25 op sizeof(known_hosts_file));
403 c7107cec 2021-04-01 op strlcpy(session_file, getenv("HOME"), sizeof(session_file));
404 c7107cec 2021-04-01 op strlcat(session_file, "/.telescope/session", sizeof(session_file));
406 ea080950 2021-07-20 op strlcpy(crashed_file, getenv("HOME"), sizeof(crashed_file));
407 ea080950 2021-07-20 op strlcat(crashed_file, "/.telescope/crashed", sizeof(crashed_file));
413 6cc5fcfe 2021-07-08 op fs_main(void)
415 6cc5fcfe 2021-07-08 op setproctitle("fs");
419 35e1f40a 2021-03-14 op event_init();
421 bc10f6a5 2021-07-12 op /* Setup pipe and event handler to the main process */
422 bc10f6a5 2021-07-12 op if ((iev_ui = malloc(sizeof(*iev_ui))) == NULL)
424 bc10f6a5 2021-07-12 op imsg_init(&iev_ui->ibuf, 3);
425 bc10f6a5 2021-07-12 op iev_ui->handler = handle_dispatch_imsg;
426 bc10f6a5 2021-07-12 op iev_ui->events = EV_READ;
427 bc10f6a5 2021-07-12 op event_set(&iev_ui->ev, iev_ui->ibuf.fd, iev_ui->events,
428 bc10f6a5 2021-07-12 op iev_ui->handler, iev_ui);
429 bc10f6a5 2021-07-12 op event_add(&iev_ui->ev, NULL);
431 35e1f40a 2021-03-14 op sandbox_fs_process();
433 35e1f40a 2021-03-14 op event_dispatch();
440 b6171794 2021-07-20 op last_time_crashed(void)
444 b6171794 2021-07-20 op if ((fd = open(crashed_file, O_RDONLY)) == -1)
448 b6171794 2021-07-20 op unlink(crashed_file);
453 d0fd368a 2021-07-15 op lock_session(void)
455 d0fd368a 2021-07-15 op struct flock lock;
458 d0fd368a 2021-07-15 op if ((fd = open(lockfile_path, O_WRONLY|O_CREAT, 0600)) == -1)
461 d0fd368a 2021-07-15 op lock.l_start = 0;
462 d0fd368a 2021-07-15 op lock.l_len = 0;
463 d0fd368a 2021-07-15 op lock.l_type = F_WRLCK;
464 d0fd368a 2021-07-15 op lock.l_whence = SEEK_SET;
466 d0fd368a 2021-07-15 op if (fcntl(fd, F_SETLK, &lock) == -1) {
475 c6d03cf5 2021-04-25 op parse_khost_line(char *line, char *tmp[3])
479 c6d03cf5 2021-04-25 op for (ap = tmp; ap < &tmp[3] &&
480 c6d03cf5 2021-04-25 op (*ap = strsep(&line, " \t\n")) != NULL;) {
481 c6d03cf5 2021-04-25 op if (**ap != '\0')
485 c6d03cf5 2021-04-25 op return ap == &tmp[3] && *line == '\0';
489 3a227e9a 2021-03-18 op load_certs(struct ohash *h)
491 c6d03cf5 2021-04-25 op char *tmp[3], *line = NULL;
492 6cd6a9e1 2021-03-20 op const char *errstr;
493 ec1fa0b0 2021-04-25 op size_t lineno = 0, linesize = 0;
494 3a227e9a 2021-03-18 op ssize_t linelen;
496 3a227e9a 2021-03-18 op struct tofu_entry *e;
498 3a227e9a 2021-03-18 op if ((f = fopen(known_hosts_file, "r")) == NULL)
501 3a227e9a 2021-03-18 op while ((linelen = getline(&line, &linesize, f)) != -1) {
502 3a227e9a 2021-03-18 op if ((e = calloc(1, sizeof(*e))) == NULL)
507 c6d03cf5 2021-04-25 op if (parse_khost_line(line, tmp)) {
508 c6d03cf5 2021-04-25 op strlcpy(e->domain, tmp[0], sizeof(e->domain));
509 c6d03cf5 2021-04-25 op strlcpy(e->hash, tmp[1], sizeof(e->hash));
511 c6d03cf5 2021-04-25 op e->verified = strtonum(tmp[2], 0, 1, &errstr);
512 c6d03cf5 2021-04-25 op if (errstr != NULL)
513 c6d03cf5 2021-04-25 op errx(1, "%s:%zu verification for %s is %s: %s",
514 c6d03cf5 2021-04-25 op known_hosts_file, lineno,
515 c6d03cf5 2021-04-25 op e->domain, errstr, tmp[2]);
516 c6d03cf5 2021-04-25 op tofu_add(h, e);
518 ec1fa0b0 2021-04-25 op warnx("%s:%zu invalid entry",
519 ec1fa0b0 2021-04-25 op known_hosts_file, lineno);
525 3a227e9a 2021-03-18 op return ferror(f);