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"
34 static void die(void) __attribute__((__noreturn__));
35 static void serve_bookmarks(uint32_t);
36 static void handle_get(struct imsg*, size_t);
37 static void handle_quit(struct imsg*, size_t);
38 static void handle_bookmark_page(struct imsg*, size_t);
39 static void handle_save_cert(struct imsg*, size_t);
40 static void handle_update_cert(struct imsg*, size_t);
41 static void handle_session_start(struct imsg*, size_t);
42 static void handle_session_tab(struct imsg*, size_t);
43 static void handle_session_end(struct imsg*, size_t);
44 static void handle_dispatch_imsg(int, short, void*);
46 static struct event imsgev;
47 static struct imsgbuf *ibuf;
51 static char bookmark_file[PATH_MAX];
52 static char known_hosts_file[PATH_MAX], known_hosts_tmp[PATH_MAX];
53 static char session_file[PATH_MAX];
55 static imsg_handlerfn *handlers[] = {
56 [IMSG_GET] = handle_get,
57 [IMSG_QUIT] = handle_quit,
58 [IMSG_BOOKMARK_PAGE] = handle_bookmark_page,
59 [IMSG_SAVE_CERT] = handle_save_cert,
60 [IMSG_UPDATE_CERT] = handle_update_cert,
61 [IMSG_SESSION_START] = handle_session_start,
62 [IMSG_SESSION_TAB] = handle_session_tab,
63 [IMSG_SESSION_END] = handle_session_end,
66 static void __attribute__((__noreturn__))
73 serve_bookmarks(uint32_t peerid)
80 if ((f = fopen(bookmark_file, "r")) == NULL) {
83 "Create ~/.telescope/bookmarks.gmi or use `bookmark-page'.\n";
84 imsg_compose(ibuf, IMSG_BUF, peerid, 0, -1, t, strlen(t));
85 imsg_compose(ibuf, IMSG_EOF, peerid, 0, -1, NULL, 0);
91 r = fread(buf, 1, sizeof(buf), f);
92 imsg_compose(ibuf, IMSG_BUF, peerid, 0, -1, buf, r);
98 imsg_compose(ibuf, IMSG_EOF, peerid, 0, -1, NULL, 0);
105 handle_get(struct imsg *imsg, size_t datalen)
112 if (data[datalen-1] != '\0')
115 if (!strcmp(data, "about:new")) {
116 imsg_compose(ibuf, IMSG_BUF, imsg->hdr.peerid, 0, -1,
117 about_new, strlen(about_new));
118 imsg_compose(ibuf, IMSG_EOF, imsg->hdr.peerid, 0, -1, NULL, 0);
120 } else if (!strcmp(data, "about:bookmarks")) {
121 serve_bookmarks(imsg->hdr.peerid);
123 p = "# not found!\n";
124 imsg_compose(ibuf, IMSG_BUF, imsg->hdr.peerid, 0, -1, p, strlen(p));
125 imsg_compose(ibuf, IMSG_EOF, imsg->hdr.peerid, 0, -1, NULL, 0);
131 handle_quit(struct imsg *imsg, size_t datalen)
137 handle_bookmark_page(struct imsg *imsg, size_t datalen)
144 if (data[datalen-1] != '\0')
147 if ((f = fopen(bookmark_file, "a")) == NULL) {
151 fprintf(f, "=> %s\n", data);
156 imsg_compose(ibuf, IMSG_BOOKMARK_OK, 0, 0, -1, &res, sizeof(res));
161 handle_save_cert(struct imsg *imsg, size_t datalen)
167 /* TODO: traverse the file to avoid duplications? */
169 if (datalen != sizeof(e))
171 memcpy(&e, imsg->data, datalen);
173 if ((f = fopen(known_hosts_file, "a")) == NULL) {
177 fprintf(f, "%s %s %d\n", e.domain, e.hash, e.verified);
182 imsg_compose(ibuf, IMSG_SAVE_CERT_OK, imsg->hdr.peerid, 0, -1,
188 handle_update_cert(struct imsg *imsg, size_t datalen)
191 struct tofu_entry entry;
192 char sfn[PATH_MAX], *line = NULL, *t;
193 size_t l, linesize = 0;
197 if (datalen != sizeof(entry))
199 memcpy(&entry, imsg->data, datalen);
201 strlcpy(sfn, known_hosts_tmp, sizeof(sfn));
202 if ((fd = mkstemp(sfn)) == -1 ||
203 (tmp = fdopen(fd, "w")) == NULL) {
212 if ((f = fopen(known_hosts_file, "r")) == NULL) {
219 l = strlen(entry.domain);
220 while ((linelen = getline(&line, &linesize, f)) != -1) {
221 if ((t = strstr(line, entry.domain)) != NULL &&
222 (line[l] == ' ' || line[l] == '\t'))
224 /* line has a trailing \n */
225 fprintf(tmp, "%s", line);
227 fprintf(tmp, "%s %s %d\n", entry.domain, entry.hash, entry.verified);
241 res = rename(sfn, known_hosts_file) != -1;
244 imsg_compose(ibuf, IMSG_UPDATE_CERT_OK, imsg->hdr.peerid, 0, -1,
250 handle_session_start(struct imsg *imsg, size_t datalen)
255 if ((session = fopen(session_file, "w")) == NULL)
260 handle_session_tab(struct imsg *imsg, size_t datalen)
268 /* skip the NUL-terminator */
269 fwrite(imsg->data, 1, datalen-1, session);
271 fprintf(session, "\n");
275 handle_session_end(struct imsg *imsg, size_t datalen)
284 handle_dispatch_imsg(int fd, short ev, void *d)
286 struct imsgbuf *ibuf = d;
287 dispatch_imsg(ibuf, handlers, sizeof(handlers));
295 strlcpy(dir, getenv("HOME"), sizeof(dir));
296 strlcat(dir, "/.telescope", sizeof(dir));
299 strlcpy(bookmark_file, getenv("HOME"), sizeof(bookmark_file));
300 strlcat(bookmark_file, "/.telescope/bookmarks.gmi", sizeof(bookmark_file));
302 strlcpy(known_hosts_file, getenv("HOME"), sizeof(known_hosts_file));
303 strlcat(known_hosts_file, "/.telescope/known_hosts", sizeof(known_hosts_file));
305 strlcpy(known_hosts_tmp, getenv("HOME"), sizeof(known_hosts_tmp));
306 strlcat(known_hosts_tmp, "/.telescope/known_hosts.tmp.XXXXXXXXXX",
307 sizeof(known_hosts_file));
309 strlcpy(session_file, getenv("HOME"), sizeof(session_file));
310 strlcat(session_file, "/.telescope/session", sizeof(session_file));
316 fs_main(struct imsgbuf *b)
322 event_set(&imsgev, ibuf->fd, EV_READ | EV_PERSIST, handle_dispatch_imsg, ibuf);
323 event_add(&imsgev, NULL);
325 sandbox_fs_process();
334 parse_khost_line(char *line, char *tmp[3])
338 for (ap = tmp; ap < &tmp[3] &&
339 (*ap = strsep(&line, " \t\n")) != NULL;) {
344 return ap == &tmp[3] && *line == '\0';
348 load_certs(struct ohash *h)
350 char *tmp[3], *line = NULL;
352 size_t lineno = 0, linesize = 0;
355 struct tofu_entry *e;
357 if ((f = fopen(known_hosts_file, "r")) == NULL)
360 while ((linelen = getline(&line, &linesize, f)) != -1) {
361 if ((e = calloc(1, sizeof(*e))) == NULL)
366 if (parse_khost_line(line, tmp)) {
367 strlcpy(e->domain, tmp[0], sizeof(e->domain));
368 strlcpy(e->hash, tmp[1], sizeof(e->hash));
370 e->verified = strtonum(tmp[2], 0, 1, &errstr);
372 errx(1, "%s:%zu verification for %s is %s: %s",
373 known_hosts_file, lineno,
374 e->domain, errstr, tmp[2]);
377 warnx("%s:%zu invalid entry",
378 known_hosts_file, lineno);
388 load_last_session(void (*cb)(const char*))
390 char *nl, *line = NULL;
396 if ((session = fopen(session_file, "r")) == NULL)
399 while ((linelen = getline(&line, &linesize, session)) != -1) {
400 if ((nl = strchr(line, '\n')) != NULL)