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
23 #include "telescope.h"
35 static void die(void) __attribute__((__noreturn__));
36 static void serve_bookmarks(uint32_t);
37 static void handle_get(struct imsg*, size_t);
38 static void handle_quit(struct imsg*, size_t);
39 static void handle_bookmark_page(struct imsg*, size_t);
40 static void handle_save_cert(struct imsg*, size_t);
41 static void handle_update_cert(struct imsg*, size_t);
42 static void handle_file_open(struct imsg*, size_t);
43 static void handle_session_start(struct imsg*, size_t);
44 static void handle_session_tab(struct imsg*, size_t);
45 static void handle_session_end(struct imsg*, size_t);
46 static void handle_dispatch_imsg(int, short, void*);
48 static struct event imsgev;
49 static struct imsgbuf *ibuf;
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 imsg_compose(ibuf, IMSG_BUF, peerid, 0, -1, t, strlen(t));
88 imsg_compose(ibuf, IMSG_EOF, peerid, 0, -1, NULL, 0);
94 r = fread(buf, 1, sizeof(buf), f);
95 imsg_compose(ibuf, IMSG_BUF, peerid, 0, -1, buf, r);
101 imsg_compose(ibuf, IMSG_EOF, peerid, 0, -1, NULL, 0);
108 handle_get(struct imsg *imsg, size_t datalen)
115 if (data[datalen-1] != '\0')
118 if (!strcmp(data, "about:new")) {
119 imsg_compose(ibuf, IMSG_BUF, imsg->hdr.peerid, 0, -1,
120 about_new, strlen(about_new));
121 imsg_compose(ibuf, IMSG_EOF, imsg->hdr.peerid, 0, -1, NULL, 0);
123 } else if (!strcmp(data, "about:bookmarks")) {
124 serve_bookmarks(imsg->hdr.peerid);
126 p = "# not found!\n";
127 imsg_compose(ibuf, IMSG_BUF, imsg->hdr.peerid, 0, -1, p, strlen(p));
128 imsg_compose(ibuf, IMSG_EOF, imsg->hdr.peerid, 0, -1, NULL, 0);
134 handle_quit(struct imsg *imsg, size_t datalen)
140 handle_bookmark_page(struct imsg *imsg, size_t datalen)
147 if (data[datalen-1] != '\0')
150 if ((f = fopen(bookmark_file, "a")) == NULL) {
154 fprintf(f, "=> %s\n", data);
159 imsg_compose(ibuf, IMSG_BOOKMARK_OK, 0, 0, -1, &res, sizeof(res));
164 handle_save_cert(struct imsg *imsg, size_t datalen)
170 /* TODO: traverse the file to avoid duplications? */
172 if (datalen != sizeof(e))
174 memcpy(&e, imsg->data, datalen);
176 if ((f = fopen(known_hosts_file, "a")) == NULL) {
180 fprintf(f, "%s %s %d\n", e.domain, e.hash, e.verified);
185 imsg_compose(ibuf, IMSG_SAVE_CERT_OK, imsg->hdr.peerid, 0, -1,
191 handle_update_cert(struct imsg *imsg, size_t datalen)
194 struct tofu_entry entry;
195 char sfn[PATH_MAX], *line = NULL, *t;
196 size_t l, linesize = 0;
200 if (datalen != sizeof(entry))
202 memcpy(&entry, imsg->data, datalen);
204 strlcpy(sfn, known_hosts_tmp, sizeof(sfn));
205 if ((fd = mkstemp(sfn)) == -1 ||
206 (tmp = fdopen(fd, "w")) == NULL) {
215 if ((f = fopen(known_hosts_file, "r")) == NULL) {
222 l = strlen(entry.domain);
223 while ((linelen = getline(&line, &linesize, f)) != -1) {
224 if ((t = strstr(line, entry.domain)) != NULL &&
225 (line[l] == ' ' || line[l] == '\t'))
227 /* line has a trailing \n */
228 fprintf(tmp, "%s", line);
230 fprintf(tmp, "%s %s %d\n", entry.domain, entry.hash, entry.verified);
244 res = rename(sfn, known_hosts_file) != -1;
247 imsg_compose(ibuf, IMSG_UPDATE_CERT_OK, imsg->hdr.peerid, 0, -1,
253 handle_file_open(struct imsg *imsg, size_t datalen)
259 if (path[datalen-1] != '\0')
262 if ((fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, 0644)) == -1) {
264 imsg_compose(ibuf, IMSG_FILE_OPENED, imsg->hdr.peerid, 0, -1,
267 imsg_compose(ibuf, IMSG_FILE_OPENED, imsg->hdr.peerid, 0, fd,
274 handle_session_start(struct imsg *imsg, size_t datalen)
279 if ((session = fopen(session_file, "w")) == NULL)
284 handle_session_tab(struct imsg *imsg, size_t datalen)
292 /* skip the NUL-terminator */
293 fwrite(imsg->data, 1, datalen-1, session);
295 fprintf(session, "\n");
299 handle_session_end(struct imsg *imsg, size_t datalen)
308 handle_dispatch_imsg(int fd, short ev, void *d)
310 struct imsgbuf *ibuf = d;
311 dispatch_imsg(ibuf, handlers, sizeof(handlers));
319 strlcpy(dir, getenv("HOME"), sizeof(dir));
320 strlcat(dir, "/.telescope", sizeof(dir));
323 strlcpy(bookmark_file, getenv("HOME"), sizeof(bookmark_file));
324 strlcat(bookmark_file, "/.telescope/bookmarks.gmi", sizeof(bookmark_file));
326 strlcpy(known_hosts_file, getenv("HOME"), sizeof(known_hosts_file));
327 strlcat(known_hosts_file, "/.telescope/known_hosts", sizeof(known_hosts_file));
329 strlcpy(known_hosts_tmp, getenv("HOME"), sizeof(known_hosts_tmp));
330 strlcat(known_hosts_tmp, "/.telescope/known_hosts.tmp.XXXXXXXXXX",
331 sizeof(known_hosts_file));
333 strlcpy(session_file, getenv("HOME"), sizeof(session_file));
334 strlcat(session_file, "/.telescope/session", sizeof(session_file));
340 fs_main(struct imsgbuf *b)
346 event_set(&imsgev, ibuf->fd, EV_READ | EV_PERSIST, handle_dispatch_imsg, ibuf);
347 event_add(&imsgev, NULL);
349 sandbox_fs_process();
358 parse_khost_line(char *line, char *tmp[3])
362 for (ap = tmp; ap < &tmp[3] &&
363 (*ap = strsep(&line, " \t\n")) != NULL;) {
368 return ap == &tmp[3] && *line == '\0';
372 load_certs(struct ohash *h)
374 char *tmp[3], *line = NULL;
376 size_t lineno = 0, linesize = 0;
379 struct tofu_entry *e;
381 if ((f = fopen(known_hosts_file, "r")) == NULL)
384 while ((linelen = getline(&line, &linesize, f)) != -1) {
385 if ((e = calloc(1, sizeof(*e))) == NULL)
390 if (parse_khost_line(line, tmp)) {
391 strlcpy(e->domain, tmp[0], sizeof(e->domain));
392 strlcpy(e->hash, tmp[1], sizeof(e->hash));
394 e->verified = strtonum(tmp[2], 0, 1, &errstr);
396 errx(1, "%s:%zu verification for %s is %s: %s",
397 known_hosts_file, lineno,
398 e->domain, errstr, tmp[2]);
401 warnx("%s:%zu invalid entry",
402 known_hosts_file, lineno);
412 load_last_session(void (*cb)(const char*))
414 char *nl, *line = NULL;
420 if ((session = fopen(session_file, "r")) == NULL)
423 while ((linelen = getline(&line, &linesize, session)) != -1) {
424 if ((nl = strchr(line, '\n')) != NULL)