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 lockfile_path[PATH_MAX];
55 static char bookmark_file[PATH_MAX];
56 static char known_hosts_file[PATH_MAX], known_hosts_tmp[PATH_MAX];
58 char session_file[PATH_MAX];
60 static imsg_handlerfn *handlers[] = {
61 [IMSG_GET] = handle_get,
62 [IMSG_QUIT] = handle_quit,
63 [IMSG_BOOKMARK_PAGE] = handle_bookmark_page,
64 [IMSG_SAVE_CERT] = handle_save_cert,
65 [IMSG_UPDATE_CERT] = handle_update_cert,
66 [IMSG_FILE_OPEN] = handle_file_open,
67 [IMSG_SESSION_START] = handle_session_start,
68 [IMSG_SESSION_TAB] = handle_session_tab,
69 [IMSG_SESSION_END] = handle_session_end,
72 static void __attribute__((__noreturn__))
79 serve_bookmarks(uint32_t peerid)
86 if ((f = fopen(bookmark_file, "r")) == NULL) {
89 "Create ~/.telescope/bookmarks.gmi or use `bookmark-page'.\n";
90 fs_send_ui(IMSG_BUF, peerid, -1, t, strlen(t));
91 fs_send_ui(IMSG_EOF, peerid, -1, NULL, 0);
96 r = fread(buf, 1, sizeof(buf), f);
97 fs_send_ui(IMSG_BUF, peerid, -1, buf, r);
102 fs_send_ui(IMSG_EOF, peerid, -1, NULL, 0);
108 send_page(struct imsg *imsg, const char *page)
110 fs_send_ui(IMSG_BUF, imsg->hdr.peerid, -1, page, strlen(page));
111 fs_send_ui(IMSG_EOF, imsg->hdr.peerid, -1, NULL, 0);
115 handle_get(struct imsg *imsg, size_t datalen)
122 if (data[datalen-1] != '\0')
125 if (!strcmp(data, "about:about")) {
126 send_page(imsg, about_about);
127 } else if (!strcmp(data, "about:blank")) {
128 send_page(imsg, about_blank);
129 } else if (!strcmp(data, "about:bookmarks")) {
130 serve_bookmarks(imsg->hdr.peerid);
131 } else if (!strcmp(data, "about:help")) {
132 send_page(imsg, about_help);
133 } else if (!strcmp(data, "about:new")) {
134 send_page(imsg, about_new);
136 p = "# not found!\n";
137 fs_send_ui(IMSG_BUF, imsg->hdr.peerid, -1, p, strlen(p));
138 fs_send_ui(IMSG_EOF, imsg->hdr.peerid, -1, NULL, 0);
143 handle_quit(struct imsg *imsg, size_t datalen)
149 handle_bookmark_page(struct imsg *imsg, size_t datalen)
156 if (data[datalen-1] != '\0')
159 if ((f = fopen(bookmark_file, "a")) == NULL) {
163 fprintf(f, "=> %s\n", data);
168 fs_send_ui(IMSG_BOOKMARK_OK, 0, -1, &res, sizeof(res));
172 handle_save_cert(struct imsg *imsg, size_t datalen)
178 /* TODO: traverse the file to avoid duplications? */
180 if (datalen != sizeof(e))
182 memcpy(&e, imsg->data, datalen);
184 if ((f = fopen(known_hosts_file, "a")) == NULL) {
188 fprintf(f, "%s %s %d\n", e.domain, e.hash, e.verified);
193 fs_send_ui(IMSG_SAVE_CERT_OK, imsg->hdr.peerid, -1,
198 handle_update_cert(struct imsg *imsg, size_t datalen)
201 struct tofu_entry entry;
202 char sfn[PATH_MAX], *line = NULL, *t;
203 size_t l, linesize = 0;
207 if (datalen != sizeof(entry))
209 memcpy(&entry, imsg->data, datalen);
211 strlcpy(sfn, known_hosts_tmp, sizeof(sfn));
212 if ((fd = mkstemp(sfn)) == -1 ||
213 (tmp = fdopen(fd, "w")) == NULL) {
222 if ((f = fopen(known_hosts_file, "r")) == NULL) {
229 l = strlen(entry.domain);
230 while ((linelen = getline(&line, &linesize, f)) != -1) {
231 if ((t = strstr(line, entry.domain)) != NULL &&
232 (line[l] == ' ' || line[l] == '\t'))
234 /* line has a trailing \n */
235 fprintf(tmp, "%s", line);
237 fprintf(tmp, "%s %s %d\n", entry.domain, entry.hash, entry.verified);
251 res = rename(sfn, known_hosts_file) != -1;
254 fs_send_ui(IMSG_UPDATE_CERT_OK, imsg->hdr.peerid, -1,
259 handle_file_open(struct imsg *imsg, size_t datalen)
265 if (path[datalen-1] != '\0')
268 if ((fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, 0644)) == -1) {
270 fs_send_ui(IMSG_FILE_OPENED, imsg->hdr.peerid, -1,
273 fs_send_ui(IMSG_FILE_OPENED, imsg->hdr.peerid, fd,
278 handle_session_start(struct imsg *imsg, size_t datalen)
283 if ((session = fopen(session_file, "w")) == NULL)
288 handle_session_tab(struct imsg *imsg, size_t datalen)
296 flags = imsg->hdr.peerid;
298 if (datalen == 0 || url[datalen-1] != '\0')
300 fprintf(session, "%s", url);
302 if (flags & TAB_CURRENT)
303 fprintf(session, " current");
305 fprintf(session, "\n");
309 handle_session_end(struct imsg *imsg, size_t datalen)
318 handle_dispatch_imsg(int fd, short ev, void *d)
320 struct imsgev *iev = d;
321 dispatch_imsg(iev, ev, handlers, sizeof(handlers));
325 fs_send_ui(int type, uint32_t peerid, int fd, const void *data,
328 return imsg_compose_event(iev_ui, type, peerid, 0, fd,
337 strlcpy(dir, getenv("HOME"), sizeof(dir));
338 strlcat(dir, "/.telescope", sizeof(dir));
341 strlcpy(lockfile_path, getenv("HOME"), sizeof(lockfile_path));
342 strlcat(lockfile_path, "/.telescope/lock", sizeof(lockfile_path));
344 strlcpy(bookmark_file, getenv("HOME"), sizeof(bookmark_file));
345 strlcat(bookmark_file, "/.telescope/bookmarks.gmi", sizeof(bookmark_file));
347 strlcpy(known_hosts_file, getenv("HOME"), sizeof(known_hosts_file));
348 strlcat(known_hosts_file, "/.telescope/known_hosts", sizeof(known_hosts_file));
350 strlcpy(known_hosts_tmp, getenv("HOME"), sizeof(known_hosts_tmp));
351 strlcat(known_hosts_tmp, "/.telescope/known_hosts.tmp.XXXXXXXXXX",
352 sizeof(known_hosts_file));
354 strlcpy(session_file, getenv("HOME"), sizeof(session_file));
355 strlcat(session_file, "/.telescope/session", sizeof(session_file));
369 /* Setup pipe and event handler to the main process */
370 if ((iev_ui = malloc(sizeof(*iev_ui))) == NULL)
372 imsg_init(&iev_ui->ibuf, 3);
373 iev_ui->handler = handle_dispatch_imsg;
374 iev_ui->events = EV_READ;
375 event_set(&iev_ui->ev, iev_ui->ibuf.fd, iev_ui->events,
376 iev_ui->handler, iev_ui);
377 event_add(&iev_ui->ev, NULL);
379 sandbox_fs_process();
393 if ((fd = open(lockfile_path, O_WRONLY|O_CREAT, 0600)) == -1)
398 lock.l_type = F_WRLCK;
399 lock.l_whence = SEEK_SET;
401 if (fcntl(fd, F_SETLK, &lock) == -1) {
410 parse_khost_line(char *line, char *tmp[3])
414 for (ap = tmp; ap < &tmp[3] &&
415 (*ap = strsep(&line, " \t\n")) != NULL;) {
420 return ap == &tmp[3] && *line == '\0';
424 load_certs(struct ohash *h)
426 char *tmp[3], *line = NULL;
428 size_t lineno = 0, linesize = 0;
431 struct tofu_entry *e;
433 if ((f = fopen(known_hosts_file, "r")) == NULL)
436 while ((linelen = getline(&line, &linesize, f)) != -1) {
437 if ((e = calloc(1, sizeof(*e))) == NULL)
442 if (parse_khost_line(line, tmp)) {
443 strlcpy(e->domain, tmp[0], sizeof(e->domain));
444 strlcpy(e->hash, tmp[1], sizeof(e->hash));
446 e->verified = strtonum(tmp[2], 0, 1, &errstr);
448 errx(1, "%s:%zu verification for %s is %s: %s",
449 known_hosts_file, lineno,
450 e->domain, errstr, tmp[2]);
453 warnx("%s:%zu invalid entry",
454 known_hosts_file, lineno);