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 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*);
49 static struct event imsgev;
50 static struct imsgbuf *ibuf;
54 static char bookmark_file[PATH_MAX];
55 static char known_hosts_file[PATH_MAX], known_hosts_tmp[PATH_MAX];
56 static char session_file[PATH_MAX];
58 static imsg_handlerfn *handlers[] = {
59 [IMSG_GET] = handle_get,
60 [IMSG_QUIT] = handle_quit,
61 [IMSG_BOOKMARK_PAGE] = handle_bookmark_page,
62 [IMSG_SAVE_CERT] = handle_save_cert,
63 [IMSG_UPDATE_CERT] = handle_update_cert,
64 [IMSG_FILE_OPEN] = handle_file_open,
65 [IMSG_SESSION_START] = handle_session_start,
66 [IMSG_SESSION_TAB] = handle_session_tab,
67 [IMSG_SESSION_END] = handle_session_end,
70 static void __attribute__((__noreturn__))
77 serve_bookmarks(uint32_t peerid)
84 if ((f = fopen(bookmark_file, "r")) == NULL) {
87 "Create ~/.telescope/bookmarks.gmi or use `bookmark-page'.\n";
88 imsg_compose(ibuf, IMSG_BUF, peerid, 0, -1, t, strlen(t));
89 imsg_compose(ibuf, IMSG_EOF, peerid, 0, -1, NULL, 0);
95 r = fread(buf, 1, sizeof(buf), f);
96 imsg_compose(ibuf, IMSG_BUF, peerid, 0, -1, buf, r);
102 imsg_compose(ibuf, IMSG_EOF, peerid, 0, -1, NULL, 0);
109 send_page(struct imsg *imsg, const char *page)
111 imsg_compose(ibuf, IMSG_BUF, imsg->hdr.peerid, 0, -1,
113 imsg_compose(ibuf, IMSG_EOF, imsg->hdr.peerid, 0, -1, NULL, 0);
118 handle_get(struct imsg *imsg, size_t datalen)
125 if (data[datalen-1] != '\0')
128 if (!strcmp(data, "about:about")) {
129 send_page(imsg, about_about);
130 } else if (!strcmp(data, "about:blank")) {
131 send_page(imsg, about_blank);
132 } else if (!strcmp(data, "about:bookmarks")) {
133 serve_bookmarks(imsg->hdr.peerid);
134 } else if (!strcmp(data, "about:help")) {
135 send_page(imsg, about_help);
136 } else if (!strcmp(data, "about:new")) {
137 send_page(imsg, about_new);
139 p = "# not found!\n";
140 imsg_compose(ibuf, IMSG_BUF, imsg->hdr.peerid, 0, -1, p, strlen(p));
141 imsg_compose(ibuf, IMSG_EOF, imsg->hdr.peerid, 0, -1, NULL, 0);
147 handle_quit(struct imsg *imsg, size_t datalen)
153 handle_bookmark_page(struct imsg *imsg, size_t datalen)
160 if (data[datalen-1] != '\0')
163 if ((f = fopen(bookmark_file, "a")) == NULL) {
167 fprintf(f, "=> %s\n", data);
172 imsg_compose(ibuf, IMSG_BOOKMARK_OK, 0, 0, -1, &res, sizeof(res));
177 handle_save_cert(struct imsg *imsg, size_t datalen)
183 /* TODO: traverse the file to avoid duplications? */
185 if (datalen != sizeof(e))
187 memcpy(&e, imsg->data, datalen);
189 if ((f = fopen(known_hosts_file, "a")) == NULL) {
193 fprintf(f, "%s %s %d\n", e.domain, e.hash, e.verified);
198 imsg_compose(ibuf, IMSG_SAVE_CERT_OK, imsg->hdr.peerid, 0, -1,
204 handle_update_cert(struct imsg *imsg, size_t datalen)
207 struct tofu_entry entry;
208 char sfn[PATH_MAX], *line = NULL, *t;
209 size_t l, linesize = 0;
213 if (datalen != sizeof(entry))
215 memcpy(&entry, imsg->data, datalen);
217 strlcpy(sfn, known_hosts_tmp, sizeof(sfn));
218 if ((fd = mkstemp(sfn)) == -1 ||
219 (tmp = fdopen(fd, "w")) == NULL) {
228 if ((f = fopen(known_hosts_file, "r")) == NULL) {
235 l = strlen(entry.domain);
236 while ((linelen = getline(&line, &linesize, f)) != -1) {
237 if ((t = strstr(line, entry.domain)) != NULL &&
238 (line[l] == ' ' || line[l] == '\t'))
240 /* line has a trailing \n */
241 fprintf(tmp, "%s", line);
243 fprintf(tmp, "%s %s %d\n", entry.domain, entry.hash, entry.verified);
257 res = rename(sfn, known_hosts_file) != -1;
260 imsg_compose(ibuf, IMSG_UPDATE_CERT_OK, imsg->hdr.peerid, 0, -1,
266 handle_file_open(struct imsg *imsg, size_t datalen)
272 if (path[datalen-1] != '\0')
275 if ((fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, 0644)) == -1) {
277 imsg_compose(ibuf, IMSG_FILE_OPENED, imsg->hdr.peerid, 0, -1,
280 imsg_compose(ibuf, IMSG_FILE_OPENED, imsg->hdr.peerid, 0, fd,
287 handle_session_start(struct imsg *imsg, size_t datalen)
292 if ((session = fopen(session_file, "w")) == NULL)
297 handle_session_tab(struct imsg *imsg, size_t datalen)
305 /* skip the NUL-terminator */
306 fwrite(imsg->data, 1, datalen-1, session);
308 fprintf(session, "\n");
312 handle_session_end(struct imsg *imsg, size_t datalen)
321 handle_dispatch_imsg(int fd, short ev, void *d)
323 struct imsgbuf *ibuf = d;
324 dispatch_imsg(ibuf, handlers, sizeof(handlers));
332 strlcpy(dir, getenv("HOME"), sizeof(dir));
333 strlcat(dir, "/.telescope", sizeof(dir));
336 strlcpy(bookmark_file, getenv("HOME"), sizeof(bookmark_file));
337 strlcat(bookmark_file, "/.telescope/bookmarks.gmi", sizeof(bookmark_file));
339 strlcpy(known_hosts_file, getenv("HOME"), sizeof(known_hosts_file));
340 strlcat(known_hosts_file, "/.telescope/known_hosts", sizeof(known_hosts_file));
342 strlcpy(known_hosts_tmp, getenv("HOME"), sizeof(known_hosts_tmp));
343 strlcat(known_hosts_tmp, "/.telescope/known_hosts.tmp.XXXXXXXXXX",
344 sizeof(known_hosts_file));
346 strlcpy(session_file, getenv("HOME"), sizeof(session_file));
347 strlcat(session_file, "/.telescope/session", sizeof(session_file));
353 fs_main(struct imsgbuf *b)
359 event_set(&imsgev, ibuf->fd, EV_READ | EV_PERSIST, handle_dispatch_imsg, ibuf);
360 event_add(&imsgev, NULL);
362 sandbox_fs_process();
371 parse_khost_line(char *line, char *tmp[3])
375 for (ap = tmp; ap < &tmp[3] &&
376 (*ap = strsep(&line, " \t\n")) != NULL;) {
381 return ap == &tmp[3] && *line == '\0';
385 load_certs(struct ohash *h)
387 char *tmp[3], *line = NULL;
389 size_t lineno = 0, linesize = 0;
392 struct tofu_entry *e;
394 if ((f = fopen(known_hosts_file, "r")) == NULL)
397 while ((linelen = getline(&line, &linesize, f)) != -1) {
398 if ((e = calloc(1, sizeof(*e))) == NULL)
403 if (parse_khost_line(line, tmp)) {
404 strlcpy(e->domain, tmp[0], sizeof(e->domain));
405 strlcpy(e->hash, tmp[1], sizeof(e->hash));
407 e->verified = strtonum(tmp[2], 0, 1, &errstr);
409 errx(1, "%s:%zu verification for %s is %s: %s",
410 known_hosts_file, lineno,
411 e->domain, errstr, tmp[2]);
414 warnx("%s:%zu invalid entry",
415 known_hosts_file, lineno);
425 load_last_session(void (*cb)(const char*))
427 char *nl, *line = NULL;
433 if ((session = fopen(session_file, "r")) == NULL) {
439 while ((linelen = getline(&line, &linesize, session)) != -1) {
440 if ((nl = strchr(line, '\n')) != NULL)