2 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 * Handles the data in ~/.telescope
20 * TODO: add some form of locking on the files
33 #include "telescope.h"
36 static void die(void) __attribute__((__noreturn__));
37 static void serve_bookmarks(uint32_t);
38 static void send_page(struct imsg *, const char *);
39 static void handle_get(struct imsg*, size_t);
40 static void handle_quit(struct imsg*, size_t);
41 static void handle_bookmark_page(struct imsg*, size_t);
42 static void handle_save_cert(struct imsg*, size_t);
43 static void handle_update_cert(struct imsg*, size_t);
44 static void handle_file_open(struct imsg*, size_t);
45 static void handle_session_start(struct imsg*, size_t);
46 static void handle_session_tab(struct imsg*, size_t);
47 static void handle_session_end(struct imsg*, size_t);
48 static void handle_dispatch_imsg(int, short, void*);
49 static int fs_send_ui(int, uint32_t, int, const void *, uint16_t);
51 static struct imsgev *iev_ui;
54 static char bookmark_file[PATH_MAX];
55 static char known_hosts_file[PATH_MAX], known_hosts_tmp[PATH_MAX];
56 static char session_file[PATH_MAX];
58 static imsg_handlerfn *handlers[] = {
59 [IMSG_GET] = handle_get,
60 [IMSG_QUIT] = handle_quit,
61 [IMSG_BOOKMARK_PAGE] = handle_bookmark_page,
62 [IMSG_SAVE_CERT] = handle_save_cert,
63 [IMSG_UPDATE_CERT] = handle_update_cert,
64 [IMSG_FILE_OPEN] = handle_file_open,
65 [IMSG_SESSION_START] = handle_session_start,
66 [IMSG_SESSION_TAB] = handle_session_tab,
67 [IMSG_SESSION_END] = handle_session_end,
70 static void __attribute__((__noreturn__))
77 serve_bookmarks(uint32_t peerid)
84 if ((f = fopen(bookmark_file, "r")) == NULL) {
87 "Create ~/.telescope/bookmarks.gmi or use `bookmark-page'.\n";
88 fs_send_ui(IMSG_BUF, peerid, -1, t, strlen(t));
89 fs_send_ui(IMSG_EOF, peerid, -1, NULL, 0);
94 r = fread(buf, 1, sizeof(buf), f);
95 fs_send_ui(IMSG_BUF, peerid, -1, buf, r);
100 fs_send_ui(IMSG_EOF, peerid, -1, NULL, 0);
106 send_page(struct imsg *imsg, const char *page)
108 fs_send_ui(IMSG_BUF, imsg->hdr.peerid, -1, page, strlen(page));
109 fs_send_ui(IMSG_EOF, imsg->hdr.peerid, -1, NULL, 0);
113 handle_get(struct imsg *imsg, size_t datalen)
120 if (data[datalen-1] != '\0')
123 if (!strcmp(data, "about:about")) {
124 send_page(imsg, about_about);
125 } else if (!strcmp(data, "about:blank")) {
126 send_page(imsg, about_blank);
127 } else if (!strcmp(data, "about:bookmarks")) {
128 serve_bookmarks(imsg->hdr.peerid);
129 } else if (!strcmp(data, "about:help")) {
130 send_page(imsg, about_help);
131 } else if (!strcmp(data, "about:new")) {
132 send_page(imsg, about_new);
134 p = "# not found!\n";
135 fs_send_ui(IMSG_BUF, imsg->hdr.peerid, -1, p, strlen(p));
136 fs_send_ui(IMSG_EOF, imsg->hdr.peerid, -1, NULL, 0);
141 handle_quit(struct imsg *imsg, size_t datalen)
147 handle_bookmark_page(struct imsg *imsg, size_t datalen)
154 if (data[datalen-1] != '\0')
157 if ((f = fopen(bookmark_file, "a")) == NULL) {
161 fprintf(f, "=> %s\n", data);
166 fs_send_ui(IMSG_BOOKMARK_OK, 0, -1, &res, sizeof(res));
170 handle_save_cert(struct imsg *imsg, size_t datalen)
176 /* TODO: traverse the file to avoid duplications? */
178 if (datalen != sizeof(e))
180 memcpy(&e, imsg->data, datalen);
182 if ((f = fopen(known_hosts_file, "a")) == NULL) {
186 fprintf(f, "%s %s %d\n", e.domain, e.hash, e.verified);
191 fs_send_ui(IMSG_SAVE_CERT_OK, imsg->hdr.peerid, -1,
196 handle_update_cert(struct imsg *imsg, size_t datalen)
199 struct tofu_entry entry;
200 char sfn[PATH_MAX], *line = NULL, *t;
201 size_t l, linesize = 0;
205 if (datalen != sizeof(entry))
207 memcpy(&entry, imsg->data, datalen);
209 strlcpy(sfn, known_hosts_tmp, sizeof(sfn));
210 if ((fd = mkstemp(sfn)) == -1 ||
211 (tmp = fdopen(fd, "w")) == NULL) {
220 if ((f = fopen(known_hosts_file, "r")) == NULL) {
227 l = strlen(entry.domain);
228 while ((linelen = getline(&line, &linesize, f)) != -1) {
229 if ((t = strstr(line, entry.domain)) != NULL &&
230 (line[l] == ' ' || line[l] == '\t'))
232 /* line has a trailing \n */
233 fprintf(tmp, "%s", line);
235 fprintf(tmp, "%s %s %d\n", entry.domain, entry.hash, entry.verified);
249 res = rename(sfn, known_hosts_file) != -1;
252 fs_send_ui(IMSG_UPDATE_CERT_OK, imsg->hdr.peerid, -1,
257 handle_file_open(struct imsg *imsg, size_t datalen)
263 if (path[datalen-1] != '\0')
266 if ((fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, 0644)) == -1) {
268 fs_send_ui(IMSG_FILE_OPENED, imsg->hdr.peerid, -1,
271 fs_send_ui(IMSG_FILE_OPENED, imsg->hdr.peerid, fd,
276 handle_session_start(struct imsg *imsg, size_t datalen)
281 if ((session = fopen(session_file, "w")) == NULL)
286 handle_session_tab(struct imsg *imsg, size_t datalen)
294 /* skip the NUL-terminator */
295 fwrite(imsg->data, 1, datalen-1, session);
297 fprintf(session, "\n");
301 handle_session_end(struct imsg *imsg, size_t datalen)
310 handle_dispatch_imsg(int fd, short ev, void *d)
312 struct imsgev *iev = d;
313 dispatch_imsg(iev, ev, handlers, sizeof(handlers));
317 fs_send_ui(int type, uint32_t peerid, int fd, const void *data,
320 return imsg_compose_event(iev_ui, type, peerid, 0, fd,
329 strlcpy(dir, getenv("HOME"), sizeof(dir));
330 strlcat(dir, "/.telescope", sizeof(dir));
333 strlcpy(bookmark_file, getenv("HOME"), sizeof(bookmark_file));
334 strlcat(bookmark_file, "/.telescope/bookmarks.gmi", sizeof(bookmark_file));
336 strlcpy(known_hosts_file, getenv("HOME"), sizeof(known_hosts_file));
337 strlcat(known_hosts_file, "/.telescope/known_hosts", sizeof(known_hosts_file));
339 strlcpy(known_hosts_tmp, getenv("HOME"), sizeof(known_hosts_tmp));
340 strlcat(known_hosts_tmp, "/.telescope/known_hosts.tmp.XXXXXXXXXX",
341 sizeof(known_hosts_file));
343 strlcpy(session_file, getenv("HOME"), sizeof(session_file));
344 strlcat(session_file, "/.telescope/session", sizeof(session_file));
358 /* Setup pipe and event handler to the main process */
359 if ((iev_ui = malloc(sizeof(*iev_ui))) == NULL)
361 imsg_init(&iev_ui->ibuf, 3);
362 iev_ui->handler = handle_dispatch_imsg;
363 iev_ui->events = EV_READ;
364 event_set(&iev_ui->ev, iev_ui->ibuf.fd, iev_ui->events,
365 iev_ui->handler, iev_ui);
366 event_add(&iev_ui->ev, NULL);
368 sandbox_fs_process();
377 parse_khost_line(char *line, char *tmp[3])
381 for (ap = tmp; ap < &tmp[3] &&
382 (*ap = strsep(&line, " \t\n")) != NULL;) {
387 return ap == &tmp[3] && *line == '\0';
391 load_certs(struct ohash *h)
393 char *tmp[3], *line = NULL;
395 size_t lineno = 0, linesize = 0;
398 struct tofu_entry *e;
400 if ((f = fopen(known_hosts_file, "r")) == NULL)
403 while ((linelen = getline(&line, &linesize, f)) != -1) {
404 if ((e = calloc(1, sizeof(*e))) == NULL)
409 if (parse_khost_line(line, tmp)) {
410 strlcpy(e->domain, tmp[0], sizeof(e->domain));
411 strlcpy(e->hash, tmp[1], sizeof(e->hash));
413 e->verified = strtonum(tmp[2], 0, 1, &errstr);
415 errx(1, "%s:%zu verification for %s is %s: %s",
416 known_hosts_file, lineno,
417 e->domain, errstr, tmp[2]);
420 warnx("%s:%zu invalid entry",
421 known_hosts_file, lineno);
431 load_last_session(void (*cb)(const char*))
433 char *nl, *line = NULL;
439 if ((session = fopen(session_file, "r")) == NULL) {
445 while ((linelen = getline(&line, &linesize, session)) != -1) {
446 if ((nl = strchr(line, '\n')) != NULL)