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];
57 static char session_file[PATH_MAX];
59 static imsg_handlerfn *handlers[] = {
60 [IMSG_GET] = handle_get,
61 [IMSG_QUIT] = handle_quit,
62 [IMSG_BOOKMARK_PAGE] = handle_bookmark_page,
63 [IMSG_SAVE_CERT] = handle_save_cert,
64 [IMSG_UPDATE_CERT] = handle_update_cert,
65 [IMSG_FILE_OPEN] = handle_file_open,
66 [IMSG_SESSION_START] = handle_session_start,
67 [IMSG_SESSION_TAB] = handle_session_tab,
68 [IMSG_SESSION_END] = handle_session_end,
71 static void __attribute__((__noreturn__))
78 serve_bookmarks(uint32_t peerid)
85 if ((f = fopen(bookmark_file, "r")) == NULL) {
88 "Create ~/.telescope/bookmarks.gmi or use `bookmark-page'.\n";
89 fs_send_ui(IMSG_BUF, peerid, -1, t, strlen(t));
90 fs_send_ui(IMSG_EOF, peerid, -1, NULL, 0);
95 r = fread(buf, 1, sizeof(buf), f);
96 fs_send_ui(IMSG_BUF, peerid, -1, buf, r);
101 fs_send_ui(IMSG_EOF, peerid, -1, NULL, 0);
107 send_page(struct imsg *imsg, const char *page)
109 fs_send_ui(IMSG_BUF, imsg->hdr.peerid, -1, page, strlen(page));
110 fs_send_ui(IMSG_EOF, imsg->hdr.peerid, -1, NULL, 0);
114 handle_get(struct imsg *imsg, size_t datalen)
121 if (data[datalen-1] != '\0')
124 if (!strcmp(data, "about:about")) {
125 send_page(imsg, about_about);
126 } else if (!strcmp(data, "about:blank")) {
127 send_page(imsg, about_blank);
128 } else if (!strcmp(data, "about:bookmarks")) {
129 serve_bookmarks(imsg->hdr.peerid);
130 } else if (!strcmp(data, "about:help")) {
131 send_page(imsg, about_help);
132 } else if (!strcmp(data, "about:new")) {
133 send_page(imsg, about_new);
135 p = "# not found!\n";
136 fs_send_ui(IMSG_BUF, imsg->hdr.peerid, -1, p, strlen(p));
137 fs_send_ui(IMSG_EOF, imsg->hdr.peerid, -1, NULL, 0);
142 handle_quit(struct imsg *imsg, size_t datalen)
148 handle_bookmark_page(struct imsg *imsg, size_t datalen)
155 if (data[datalen-1] != '\0')
158 if ((f = fopen(bookmark_file, "a")) == NULL) {
162 fprintf(f, "=> %s\n", data);
167 fs_send_ui(IMSG_BOOKMARK_OK, 0, -1, &res, sizeof(res));
171 handle_save_cert(struct imsg *imsg, size_t datalen)
177 /* TODO: traverse the file to avoid duplications? */
179 if (datalen != sizeof(e))
181 memcpy(&e, imsg->data, datalen);
183 if ((f = fopen(known_hosts_file, "a")) == NULL) {
187 fprintf(f, "%s %s %d\n", e.domain, e.hash, e.verified);
192 fs_send_ui(IMSG_SAVE_CERT_OK, imsg->hdr.peerid, -1,
197 handle_update_cert(struct imsg *imsg, size_t datalen)
200 struct tofu_entry entry;
201 char sfn[PATH_MAX], *line = NULL, *t;
202 size_t l, linesize = 0;
206 if (datalen != sizeof(entry))
208 memcpy(&entry, imsg->data, datalen);
210 strlcpy(sfn, known_hosts_tmp, sizeof(sfn));
211 if ((fd = mkstemp(sfn)) == -1 ||
212 (tmp = fdopen(fd, "w")) == NULL) {
221 if ((f = fopen(known_hosts_file, "r")) == NULL) {
228 l = strlen(entry.domain);
229 while ((linelen = getline(&line, &linesize, f)) != -1) {
230 if ((t = strstr(line, entry.domain)) != NULL &&
231 (line[l] == ' ' || line[l] == '\t'))
233 /* line has a trailing \n */
234 fprintf(tmp, "%s", line);
236 fprintf(tmp, "%s %s %d\n", entry.domain, entry.hash, entry.verified);
250 res = rename(sfn, known_hosts_file) != -1;
253 fs_send_ui(IMSG_UPDATE_CERT_OK, imsg->hdr.peerid, -1,
258 handle_file_open(struct imsg *imsg, size_t datalen)
264 if (path[datalen-1] != '\0')
267 if ((fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, 0644)) == -1) {
269 fs_send_ui(IMSG_FILE_OPENED, imsg->hdr.peerid, -1,
272 fs_send_ui(IMSG_FILE_OPENED, imsg->hdr.peerid, fd,
277 handle_session_start(struct imsg *imsg, size_t datalen)
282 if ((session = fopen(session_file, "w")) == NULL)
287 handle_session_tab(struct imsg *imsg, size_t datalen)
295 /* skip the NUL-terminator */
296 fwrite(imsg->data, 1, datalen-1, session);
298 fprintf(session, "\n");
302 handle_session_end(struct imsg *imsg, size_t datalen)
311 handle_dispatch_imsg(int fd, short ev, void *d)
313 struct imsgev *iev = d;
314 dispatch_imsg(iev, ev, handlers, sizeof(handlers));
318 fs_send_ui(int type, uint32_t peerid, int fd, const void *data,
321 return imsg_compose_event(iev_ui, type, peerid, 0, fd,
330 strlcpy(dir, getenv("HOME"), sizeof(dir));
331 strlcat(dir, "/.telescope", sizeof(dir));
334 strlcpy(lockfile_path, getenv("HOME"), sizeof(lockfile_path));
335 strlcat(lockfile_path, "/.telescope/lock", sizeof(lockfile_path));
337 strlcpy(bookmark_file, getenv("HOME"), sizeof(bookmark_file));
338 strlcat(bookmark_file, "/.telescope/bookmarks.gmi", sizeof(bookmark_file));
340 strlcpy(known_hosts_file, getenv("HOME"), sizeof(known_hosts_file));
341 strlcat(known_hosts_file, "/.telescope/known_hosts", sizeof(known_hosts_file));
343 strlcpy(known_hosts_tmp, getenv("HOME"), sizeof(known_hosts_tmp));
344 strlcat(known_hosts_tmp, "/.telescope/known_hosts.tmp.XXXXXXXXXX",
345 sizeof(known_hosts_file));
347 strlcpy(session_file, getenv("HOME"), sizeof(session_file));
348 strlcat(session_file, "/.telescope/session", sizeof(session_file));
362 /* Setup pipe and event handler to the main process */
363 if ((iev_ui = malloc(sizeof(*iev_ui))) == NULL)
365 imsg_init(&iev_ui->ibuf, 3);
366 iev_ui->handler = handle_dispatch_imsg;
367 iev_ui->events = EV_READ;
368 event_set(&iev_ui->ev, iev_ui->ibuf.fd, iev_ui->events,
369 iev_ui->handler, iev_ui);
370 event_add(&iev_ui->ev, NULL);
372 sandbox_fs_process();
386 if ((fd = open(lockfile_path, O_WRONLY|O_CREAT, 0600)) == -1)
391 lock.l_type = F_WRLCK;
392 lock.l_whence = SEEK_SET;
394 if (fcntl(fd, F_SETLK, &lock) == -1) {
403 parse_khost_line(char *line, char *tmp[3])
407 for (ap = tmp; ap < &tmp[3] &&
408 (*ap = strsep(&line, " \t\n")) != NULL;) {
413 return ap == &tmp[3] && *line == '\0';
417 load_certs(struct ohash *h)
419 char *tmp[3], *line = NULL;
421 size_t lineno = 0, linesize = 0;
424 struct tofu_entry *e;
426 if ((f = fopen(known_hosts_file, "r")) == NULL)
429 while ((linelen = getline(&line, &linesize, f)) != -1) {
430 if ((e = calloc(1, sizeof(*e))) == NULL)
435 if (parse_khost_line(line, tmp)) {
436 strlcpy(e->domain, tmp[0], sizeof(e->domain));
437 strlcpy(e->hash, tmp[1], sizeof(e->hash));
439 e->verified = strtonum(tmp[2], 0, 1, &errstr);
441 errx(1, "%s:%zu verification for %s is %s: %s",
442 known_hosts_file, lineno,
443 e->domain, errstr, tmp[2]);
446 warnx("%s:%zu invalid entry",
447 known_hosts_file, lineno);
457 load_last_session(void (*cb)(const char*))
459 char *nl, *line = NULL;
465 if ((session = fopen(session_file, "r")) == NULL) {
471 while ((linelen = getline(&line, &linesize, session)) != -1) {
472 if ((nl = strchr(line, '\n')) != NULL)