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"
35 static void die(void) __attribute__((__noreturn__));
36 static void serve_bookmarks(uint32_t);
37 static void send_page(struct imsg *, const char *);
38 static void handle_get(struct imsg*, size_t);
39 static void handle_quit(struct imsg*, size_t);
40 static void handle_bookmark_page(struct imsg*, size_t);
41 static void handle_save_cert(struct imsg*, size_t);
42 static void handle_update_cert(struct imsg*, size_t);
43 static void handle_file_open(struct imsg*, size_t);
44 static void handle_session_start(struct imsg*, size_t);
45 static void handle_session_tab(struct imsg*, size_t);
46 static void handle_session_end(struct imsg*, size_t);
47 static void handle_dispatch_imsg(int, short, void*);
48 static int fs_send_ui(int, uint32_t, int, const void *, uint16_t);
50 static struct imsgev *iev_ui;
53 static char bookmark_file[PATH_MAX];
54 static char known_hosts_file[PATH_MAX], known_hosts_tmp[PATH_MAX];
55 static char session_file[PATH_MAX];
57 static imsg_handlerfn *handlers[] = {
58 [IMSG_GET] = handle_get,
59 [IMSG_QUIT] = handle_quit,
60 [IMSG_BOOKMARK_PAGE] = handle_bookmark_page,
61 [IMSG_SAVE_CERT] = handle_save_cert,
62 [IMSG_UPDATE_CERT] = handle_update_cert,
63 [IMSG_FILE_OPEN] = handle_file_open,
64 [IMSG_SESSION_START] = handle_session_start,
65 [IMSG_SESSION_TAB] = handle_session_tab,
66 [IMSG_SESSION_END] = handle_session_end,
69 static void __attribute__((__noreturn__))
76 serve_bookmarks(uint32_t peerid)
83 if ((f = fopen(bookmark_file, "r")) == NULL) {
86 "Create ~/.telescope/bookmarks.gmi or use `bookmark-page'.\n";
87 fs_send_ui(IMSG_BUF, peerid, -1, t, strlen(t));
88 fs_send_ui(IMSG_EOF, peerid, -1, NULL, 0);
93 r = fread(buf, 1, sizeof(buf), f);
94 fs_send_ui(IMSG_BUF, peerid, -1, buf, r);
99 fs_send_ui(IMSG_EOF, peerid, -1, NULL, 0);
105 send_page(struct imsg *imsg, const char *page)
107 fs_send_ui(IMSG_BUF, imsg->hdr.peerid, -1, page, strlen(page));
108 fs_send_ui(IMSG_EOF, imsg->hdr.peerid, -1, NULL, 0);
112 handle_get(struct imsg *imsg, size_t datalen)
119 if (data[datalen-1] != '\0')
122 if (!strcmp(data, "about:about")) {
123 send_page(imsg, about_about);
124 } else if (!strcmp(data, "about:blank")) {
125 send_page(imsg, about_blank);
126 } else if (!strcmp(data, "about:bookmarks")) {
127 serve_bookmarks(imsg->hdr.peerid);
128 } else if (!strcmp(data, "about:help")) {
129 send_page(imsg, about_help);
130 } else if (!strcmp(data, "about:new")) {
131 send_page(imsg, about_new);
133 p = "# not found!\n";
134 fs_send_ui(IMSG_BUF, imsg->hdr.peerid, -1, p, strlen(p));
135 fs_send_ui(IMSG_EOF, imsg->hdr.peerid, -1, NULL, 0);
140 handle_quit(struct imsg *imsg, size_t datalen)
146 handle_bookmark_page(struct imsg *imsg, size_t datalen)
153 if (data[datalen-1] != '\0')
156 if ((f = fopen(bookmark_file, "a")) == NULL) {
160 fprintf(f, "=> %s\n", data);
165 fs_send_ui(IMSG_BOOKMARK_OK, 0, -1, &res, sizeof(res));
169 handle_save_cert(struct imsg *imsg, size_t datalen)
175 /* TODO: traverse the file to avoid duplications? */
177 if (datalen != sizeof(e))
179 memcpy(&e, imsg->data, datalen);
181 if ((f = fopen(known_hosts_file, "a")) == NULL) {
185 fprintf(f, "%s %s %d\n", e.domain, e.hash, e.verified);
190 fs_send_ui(IMSG_SAVE_CERT_OK, imsg->hdr.peerid, -1,
195 handle_update_cert(struct imsg *imsg, size_t datalen)
198 struct tofu_entry entry;
199 char sfn[PATH_MAX], *line = NULL, *t;
200 size_t l, linesize = 0;
204 if (datalen != sizeof(entry))
206 memcpy(&entry, imsg->data, datalen);
208 strlcpy(sfn, known_hosts_tmp, sizeof(sfn));
209 if ((fd = mkstemp(sfn)) == -1 ||
210 (tmp = fdopen(fd, "w")) == NULL) {
219 if ((f = fopen(known_hosts_file, "r")) == NULL) {
226 l = strlen(entry.domain);
227 while ((linelen = getline(&line, &linesize, f)) != -1) {
228 if ((t = strstr(line, entry.domain)) != NULL &&
229 (line[l] == ' ' || line[l] == '\t'))
231 /* line has a trailing \n */
232 fprintf(tmp, "%s", line);
234 fprintf(tmp, "%s %s %d\n", entry.domain, entry.hash, entry.verified);
248 res = rename(sfn, known_hosts_file) != -1;
251 fs_send_ui(IMSG_UPDATE_CERT_OK, imsg->hdr.peerid, -1,
256 handle_file_open(struct imsg *imsg, size_t datalen)
262 if (path[datalen-1] != '\0')
265 if ((fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, 0644)) == -1) {
267 fs_send_ui(IMSG_FILE_OPENED, imsg->hdr.peerid, -1,
270 fs_send_ui(IMSG_FILE_OPENED, imsg->hdr.peerid, fd,
275 handle_session_start(struct imsg *imsg, size_t datalen)
280 if ((session = fopen(session_file, "w")) == NULL)
285 handle_session_tab(struct imsg *imsg, size_t datalen)
293 /* skip the NUL-terminator */
294 fwrite(imsg->data, 1, datalen-1, session);
296 fprintf(session, "\n");
300 handle_session_end(struct imsg *imsg, size_t datalen)
309 handle_dispatch_imsg(int fd, short ev, void *d)
311 struct imsgev *iev = d;
312 dispatch_imsg(iev, ev, handlers, sizeof(handlers));
316 fs_send_ui(int type, uint32_t peerid, int fd, const void *data,
319 return imsg_compose_event(iev_ui, type, peerid, 0, fd,
328 strlcpy(dir, getenv("HOME"), sizeof(dir));
329 strlcat(dir, "/.telescope", sizeof(dir));
332 strlcpy(bookmark_file, getenv("HOME"), sizeof(bookmark_file));
333 strlcat(bookmark_file, "/.telescope/bookmarks.gmi", sizeof(bookmark_file));
335 strlcpy(known_hosts_file, getenv("HOME"), sizeof(known_hosts_file));
336 strlcat(known_hosts_file, "/.telescope/known_hosts", sizeof(known_hosts_file));
338 strlcpy(known_hosts_tmp, getenv("HOME"), sizeof(known_hosts_tmp));
339 strlcat(known_hosts_tmp, "/.telescope/known_hosts.tmp.XXXXXXXXXX",
340 sizeof(known_hosts_file));
342 strlcpy(session_file, getenv("HOME"), sizeof(session_file));
343 strlcat(session_file, "/.telescope/session", sizeof(session_file));
357 /* Setup pipe and event handler to the main process */
358 if ((iev_ui = malloc(sizeof(*iev_ui))) == NULL)
360 imsg_init(&iev_ui->ibuf, 3);
361 iev_ui->handler = handle_dispatch_imsg;
362 iev_ui->events = EV_READ;
363 event_set(&iev_ui->ev, iev_ui->ibuf.fd, iev_ui->events,
364 iev_ui->handler, iev_ui);
365 event_add(&iev_ui->ev, NULL);
367 sandbox_fs_process();
376 parse_khost_line(char *line, char *tmp[3])
380 for (ap = tmp; ap < &tmp[3] &&
381 (*ap = strsep(&line, " \t\n")) != NULL;) {
386 return ap == &tmp[3] && *line == '\0';
390 load_certs(struct ohash *h)
392 char *tmp[3], *line = NULL;
394 size_t lineno = 0, linesize = 0;
397 struct tofu_entry *e;
399 if ((f = fopen(known_hosts_file, "r")) == NULL)
402 while ((linelen = getline(&line, &linesize, f)) != -1) {
403 if ((e = calloc(1, sizeof(*e))) == NULL)
408 if (parse_khost_line(line, tmp)) {
409 strlcpy(e->domain, tmp[0], sizeof(e->domain));
410 strlcpy(e->hash, tmp[1], sizeof(e->hash));
412 e->verified = strtonum(tmp[2], 0, 1, &errstr);
414 errx(1, "%s:%zu verification for %s is %s: %s",
415 known_hosts_file, lineno,
416 e->domain, errstr, tmp[2]);
419 warnx("%s:%zu invalid entry",
420 known_hosts_file, lineno);
430 load_last_session(void (*cb)(const char*))
432 char *nl, *line = NULL;
438 if ((session = fopen(session_file, "r")) == NULL) {
444 while ((linelen = getline(&line, &linesize, session)) != -1) {
445 if ((nl = strchr(line, '\n')) != NULL)