1 #include <sys/socket.h>
12 #include "telescope.h"
15 struct event netev, fsev;
16 struct tabshead tabshead = TAILQ_HEAD_INITIALIZER(tabshead);
17 struct proxylist proxies = TAILQ_HEAD_INITIALIZER(proxies);
19 enum telescope_process {
25 /* the first is also the fallback one */
26 static struct proto protos[] = {
27 { "gemini", load_gemini_url },
28 { "about", load_about_url },
32 static struct imsgbuf *netibuf, *fsibuf;
34 static void die(void) __attribute__((__noreturn__));
35 static struct tab *tab_by_id(uint32_t);
36 static void handle_imsg_err(struct imsg*, size_t);
37 static void handle_imsg_check_cert(struct imsg*, size_t);
38 static void handle_check_cert_user_choice(int, struct tab *);
39 static void handle_maybe_save_new_cert(int, struct tab *);
40 static void handle_imsg_got_code(struct imsg*, size_t);
41 static void handle_imsg_got_meta(struct imsg*, size_t);
42 static void handle_maybe_save_page(int, struct tab *);
43 static void handle_save_page_path(const char *, unsigned int);
44 static void handle_imsg_file_opened(struct imsg*, size_t);
45 static void handle_imsg_buf(struct imsg*, size_t);
46 static void handle_imsg_eof(struct imsg*, size_t);
47 static void handle_imsg_bookmark_ok(struct imsg*, size_t);
48 static void handle_imsg_save_cert_ok(struct imsg*, size_t);
49 static void handle_imsg_update_cert_ok(struct imsg *, size_t);
50 static void handle_dispatch_imsg(int, short, void*);
51 static void load_page_from_str(struct tab*, const char*);
52 static void do_load_url(struct tab*, const char*);
53 static pid_t start_child(enum telescope_process, const char *, int);
55 static imsg_handlerfn *handlers[] = {
56 [IMSG_ERR] = handle_imsg_err,
57 [IMSG_CHECK_CERT] = handle_imsg_check_cert,
58 [IMSG_GOT_CODE] = handle_imsg_got_code,
59 [IMSG_GOT_META] = handle_imsg_got_meta,
60 [IMSG_BUF] = handle_imsg_buf,
61 [IMSG_EOF] = handle_imsg_eof,
62 [IMSG_BOOKMARK_OK] = handle_imsg_bookmark_ok,
63 [IMSG_SAVE_CERT_OK] = handle_imsg_save_cert_ok,
64 [IMSG_UPDATE_CERT_OK] = handle_imsg_update_cert_ok,
65 [IMSG_FILE_OPENED] = handle_imsg_file_opened,
68 static struct ohash certs;
70 static void __attribute__((__noreturn__))
77 tab_by_id(uint32_t id)
81 TAILQ_FOREACH(t, &tabshead, tabs) {
90 handle_imsg_err(struct imsg *imsg, size_t datalen)
95 tab = tab_by_id(imsg->hdr.peerid);
98 page[datalen-1] = '\0';
100 if (asprintf(&page, "# Error loading %s\n\n> %s\n",
101 tab->hist_cur->h, page) == -1)
103 load_page_from_str(tab, page);
108 handle_imsg_check_cert(struct imsg *imsg, size_t datalen)
110 const char *hash, *host, *port;
112 struct tofu_entry *e;
116 if (hash[datalen-1] != '\0')
119 tab = tab_by_id(imsg->hdr.peerid);
121 if (tab->proxy != NULL) {
122 host = tab->proxy->host;
123 port = tab->proxy->port;
125 host = tab->uri.host;
126 port = tab->uri.port;
129 if ((e = tofu_lookup(&certs, host, port)) == NULL) {
130 /* TODO: an update in libressl/libretls changed
131 * significantly. Find a better approach at storing
133 if (datalen > sizeof(e->hash))
136 tofu_res = 1; /* trust on first use */
137 if ((e = calloc(1, sizeof(*e))) == NULL)
139 strlcpy(e->domain, host, sizeof(e->domain));
140 if (*port != '\0' && strcmp(port, "1965")) {
141 strlcat(e->domain, ":", sizeof(e->domain));
142 strlcat(e->domain, port, sizeof(e->domain));
144 strlcpy(e->hash, hash, sizeof(e->hash));
146 imsg_compose(fsibuf, IMSG_SAVE_CERT, tab->id, 0, -1,
150 tofu_res = !strcmp(hash, e->hash);
153 if (e->verified == -1)
154 tab->trust = TS_TEMP_TRUSTED;
155 else if (e->verified == 1)
156 tab->trust = TS_VERIFIED;
158 tab->trust = TS_TRUSTED;
160 imsg_compose(netibuf, IMSG_CERT_STATUS, imsg->hdr.peerid, 0, -1,
161 &tofu_res, sizeof(tofu_res));
164 tab->trust = TS_UNTRUSTED;
165 load_page_from_str(tab, "# Certificate mismatch\n");
166 if ((tab->cert = strdup(hash)) == NULL)
168 ui_yornp("Certificate mismatch. Proceed?",
169 handle_check_cert_user_choice, tab);
174 handle_check_cert_user_choice(int accept, struct tab *tab)
176 imsg_compose(netibuf, IMSG_CERT_STATUS, tab->id, 0, -1,
177 &accept, sizeof(accept));
182 * trust the certificate for this session only. If
183 * the page results in a redirect while we're asking
184 * the user to save, we'll end up with an invalid
185 * tabid (one request == one tab id) and crash. It
186 * also makes sense to save it for the current session
187 * if the user accepted it.
189 tofu_temp_trust(&certs, tab->uri.host, tab->uri.port, tab->cert);
191 ui_yornp("Save the new certificate?",
192 handle_maybe_save_new_cert, tab);
200 handle_maybe_save_new_cert(int accept, struct tab *tab)
202 struct tofu_entry *e;
203 const char *host, *port;
205 if (tab->proxy != NULL) {
206 host = tab->proxy->host;
207 port = tab->proxy->port;
209 host = tab->uri.host;
210 port = tab->uri.port;
216 if ((e = calloc(1, sizeof(*e))) == NULL)
219 strlcpy(e->domain, host, sizeof(e->domain));
220 if (*port != '\0' && strcmp(port, "1965")) {
221 strlcat(e->domain, ":", sizeof(e->domain));
222 strlcat(e->domain, port, sizeof(e->domain));
224 strlcpy(e->hash, tab->cert, sizeof(e->hash));
225 imsg_compose(fsibuf, IMSG_UPDATE_CERT, 0, 0, -1, e, sizeof(*e));
228 tofu_update(&certs, e);
230 tab->trust = TS_TRUSTED;
238 normalize_code(int n)
241 if (n == 10 || n == 11)
247 if (n == 30 || n == 31)
255 if (n <= 53 || n == 59)
263 return MALFORMED_RESPONSE;
267 handle_imsg_got_code(struct imsg *imsg, size_t datalen)
271 tab = tab_by_id(imsg->hdr.peerid);
273 if (sizeof(tab->code) != datalen)
276 memcpy(&tab->code, imsg->data, sizeof(tab->code));
277 tab->code = normalize_code(tab->code);
278 if (tab->code != 30 && tab->code != 31)
279 tab->redirect_count = 0;
283 handle_imsg_got_meta(struct imsg *imsg, size_t datalen)
287 tab = tab_by_id(imsg->hdr.peerid);
289 if (sizeof(tab->meta) <= datalen)
292 memcpy(tab->meta, imsg->data, datalen);
294 if (tab->code < 10) { /* internal errors */
295 load_page_from_str(tab, err_pages[tab->code]);
296 } else if (tab->code < 20) { /* 1x */
297 load_page_from_str(tab, err_pages[tab->code]);
298 ui_require_input(tab, tab->code == 11);
299 } else if (tab->code == 20) {
300 if (setup_parser_for(tab)) {
301 imsg_compose(netibuf, IMSG_PROCEED, tab->id, 0, -1, NULL, 0);
304 load_page_from_str(tab, err_pages[UNKNOWN_TYPE_OR_CSET]);
305 ui_yornp("Can't display page, wanna save?",
306 handle_maybe_save_page, tab);
308 } else if (tab->code < 40) { /* 3x */
309 tab->redirect_count++;
311 /* TODO: make customizable? */
312 if (tab->redirect_count > 5) {
313 load_page_from_str(tab,
314 err_pages[TOO_MUCH_REDIRECTS]);
316 do_load_url(tab, tab->meta);
317 } else { /* 4x, 5x & 6x */
318 load_page_from_str(tab, err_pages[tab->code]);
323 handle_maybe_save_page(int dosave, struct tab *tab)
326 ui_read("Save to path", handle_save_page_path, tab->id);
332 handle_save_page_path(const char *path, unsigned int tabid)
337 stop_tab(tab_by_id(tabid));
341 tab = tab_by_id(tabid);
342 tab->path = strdup(path);
344 imsg_compose(fsibuf, IMSG_FILE_OPEN, tabid, 0, -1, path, strlen(path)+1);
349 handle_imsg_file_opened(struct imsg *imsg, size_t datalen)
356 tab = tab_by_id(imsg->hdr.peerid);
358 if (imsg->fd == -1) {
362 if (e[datalen-1] != '\0')
364 l = asprintf(&page, "# Can't open file\n\n> %s: %s\n",
368 load_page_from_str(tab, page);
372 imsg_compose(netibuf, IMSG_PROCEED, tab->id, 0, -1, NULL, 0);
378 handle_imsg_buf(struct imsg *imsg, size_t datalen)
382 char *page, buf[FMT_SCALED_STRSIZE] = {0};
384 tab = tab_by_id(imsg->hdr.peerid);
386 tab->bytes += datalen;
388 if (!tab->buffer.page.parse(&tab->buffer.page,
389 imsg->data, datalen))
392 write(tab->fd, imsg->data, datalen);
393 fmt_scaled(tab->bytes, buf);
394 l = asprintf(&page, "Saving to \"%s\"... (%s)\n",
399 load_page_from_str(tab, page);
403 ui_on_tab_refresh(tab);
407 handle_imsg_eof(struct imsg *imsg, size_t datalen)
411 char *page, buf[FMT_SCALED_STRSIZE] = {0};
413 tab = tab_by_id(imsg->hdr.peerid);
416 if (!tab->buffer.page.free(&tab->buffer.page))
419 fmt_scaled(tab->bytes, buf);
420 l = asprintf(&page, "Saved to \"%s\" (%s)\n",
425 load_page_from_str(tab, page);
434 ui_on_tab_refresh(tab);
435 ui_on_tab_loaded(tab);
439 handle_imsg_bookmark_ok(struct imsg *imsg, size_t datalen)
443 if (datalen != sizeof(res))
446 memcpy(&res, imsg->data, sizeof(res));
448 message("Added to bookmarks!");
450 message("Failed to add to bookmarks: %s",
455 handle_imsg_save_cert_ok(struct imsg *imsg, size_t datalen)
459 if (datalen != sizeof(res))
461 memcpy(&res, imsg->data, datalen);
463 message("Failed to save the cert for: %s",
468 handle_imsg_update_cert_ok(struct imsg *imsg, size_t datalen)
472 if (datalen != sizeof(res))
474 memcpy(&res, imsg->data, datalen);
476 message("Failed to update the certificate");
480 handle_dispatch_imsg(int fd, short ev, void *d)
482 struct imsgbuf *ibuf = d;
483 dispatch_imsg(ibuf, handlers, sizeof(handlers));
487 load_page_from_str(struct tab *tab, const char *page)
489 erase_buffer(&tab->buffer);
490 gemtext_initparser(&tab->buffer.page);
491 if (!tab->buffer.page.parse(&tab->buffer.page, page, strlen(page)))
493 if (!tab->buffer.page.free(&tab->buffer.page))
495 ui_on_tab_refresh(tab);
496 ui_on_tab_loaded(tab);
500 load_about_url(struct tab *tab, const char *url)
502 tab->trust = TS_VERIFIED;
504 gemtext_initparser(&tab->buffer.page);
506 imsg_compose(fsibuf, IMSG_GET, tab->id, 0, -1,
507 tab->hist_cur->h, strlen(tab->hist_cur->h)+1);
512 load_gemini_url(struct tab *tab, const char *url)
517 tab->id = tab_new_id();
519 memset(&req, 0, sizeof(req));
520 strlcpy(req.host, tab->uri.host, sizeof(req.host));
521 strlcpy(req.port, tab->uri.port, sizeof(req.host));
523 strlcpy(req.req, tab->hist_cur->h, sizeof(req.req));
524 strlcat(req.req, "\r\n", sizeof(req.req));
526 req.proto = PROTO_GEMINI;
528 imsg_compose(netibuf, IMSG_GET_RAW, tab->id, 0, -1,
534 load_via_proxy(struct tab *tab, const char *url, struct proxy *p)
539 tab->id = tab_new_id();
542 memset(&req, 0, sizeof(req));
543 strlcpy(req.host, p->host, sizeof(req.host));
544 strlcpy(req.port, p->port, sizeof(req.host));
546 strlcpy(req.req, tab->hist_cur->h, sizeof(req.req));
547 strlcat(req.req, "\r\n", sizeof(req.req));
549 req.proto = p->proto;
551 imsg_compose(netibuf, IMSG_GET_RAW, tab->id, 0, -1,
557 do_load_url(struct tab *tab, const char *url)
573 tab->trust = TS_UNKNOWN;
575 memcpy(&uri, &tab->uri, sizeof(tab->uri));
576 if (!phos_resolve_uri_from_str(&uri, url, &tab->uri)) {
577 if (asprintf(&t, "#error loading %s\n>%s\n",
578 url, "Can't parse the URI") == -1)
580 strlcpy(tab->hist_cur->h, url, sizeof(tab->hist_cur->h));
581 load_page_from_str(tab, t);
586 phos_serialize_uri(&tab->uri, tab->hist_cur->h,
587 sizeof(tab->hist_cur->h));
589 for (p = protos; p->schema != NULL; ++p) {
590 if (!strcmp(tab->uri.scheme, p->schema)) {
596 TAILQ_FOREACH(proxy, &proxies, proxies) {
597 if (!strcmp(tab->uri.scheme, proxy->match_proto)) {
598 load_via_proxy(tab, url, proxy);
603 protos[0].loadfn(tab, url);
607 load_url(struct tab *tab, const char *url)
609 if (tab->hist_cur != NULL)
610 hist_clear_forward(&tab->hist, TAILQ_NEXT(tab->hist_cur, entries));
612 if ((tab->hist_cur = calloc(1, sizeof(*tab->hist_cur))) == NULL) {
617 hist_push(&tab->hist, tab->hist_cur);
618 do_load_url(tab, url);
619 erase_buffer(&tab->buffer);
623 load_previous_page(struct tab *tab)
627 if ((h = TAILQ_PREV(tab->hist_cur, mhisthead, entries)) == NULL)
630 do_load_url(tab, h->h);
635 load_next_page(struct tab *tab)
639 if ((h = TAILQ_NEXT(tab->hist_cur, entries)) == NULL)
642 do_load_url(tab, h->h);
647 stop_tab(struct tab *tab)
649 imsg_compose(netibuf, IMSG_STOP, tab->id, 0, -1, NULL, 0);
657 load_page_from_str(tab, "Stopped.\n");
662 add_to_bookmarks(const char *str)
664 imsg_compose(fsibuf, IMSG_BOOKMARK_PAGE, 0, 0, -1, str, strlen(str)+1);
673 imsg_compose(fsibuf, IMSG_SESSION_START, 0, 0, -1, NULL, 0);
676 TAILQ_FOREACH(tab, &tabshead, tabs) {
677 imsg_compose(fsibuf, IMSG_SESSION_TAB, 0, 0, -1,
678 tab->hist_cur->h, strlen(tab->hist_cur->h)+1);
682 imsg_compose(fsibuf, IMSG_SESSION_END, 0, 0, -1, NULL, 0);
687 session_new_tab_cb(const char *url)
693 start_child(enum telescope_process p, const char *argv0, int fd)
699 switch (pid = fork()) {
709 if (dup2(fd, 3) == -1)
710 err(1, "cannot setup imsg fd");
712 argv[argc++] = argv0;
715 errx(1, "Can't start ui process");
717 argv[argc++] = "-Tf";
720 argv[argc++] = "-Tn";
725 execvp(argv0, (char *const *)argv);
726 err(1, "execvp(%s)", argv0);
729 static void __attribute__((noreturn))
732 fprintf(stderr, "USAGE: %s [-hn] [-c config] [url]\n", getprogname());
733 fprintf(stderr, "version: " PACKAGE " " VERSION "\n");
738 main(int argc, char * const *argv)
740 struct imsgbuf net_ibuf, fs_ibuf;
741 int net_fds[2], fs_fds[2];
742 int ch, configtest = 0, fail = 0;
746 const char *url = NEW_TAB_URL;
751 signal(SIGCHLD, SIG_IGN);
752 signal(SIGPIPE, SIG_IGN);
754 if (getenv("NO_COLOR") != NULL)
757 strlcpy(path, getenv("HOME"), sizeof(path));
758 strlcat(path, "/.telescope/config", sizeof(path));
760 while ((ch = getopt(argc, argv, "c:hnT:")) != -1) {
764 strlcpy(path, optarg, sizeof(path));
780 errx(1, "invalid process spec %c",
795 else if (proc == PROC_FS)
797 else if (proc == PROC_NET)
798 return client_main();
808 /* setup keys before reading the config */
809 TAILQ_INIT(&global_map.m);
810 global_map.unhandled_input = global_key_unbound;
811 TAILQ_INIT(&minibuffer_map.m);
814 parseconfig(path, fail);
820 /* Start children. */
821 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, fs_fds) == -1)
822 err(1, "socketpair");
823 start_child(PROC_FS, argv0, fs_fds[1]);
824 imsg_init(&fs_ibuf, fs_fds[0]);
827 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, net_fds) == -1)
828 err(1, "socketpair");
829 start_child(PROC_NET, argv0, net_fds[1]);
830 imsg_init(&net_ibuf, net_fds[0]);
835 /* initialize tofu & load certificates */
837 tofu_init(&certs, 5, offsetof(struct tofu_entry, domain));
842 event_set(&netev, netibuf->fd, EV_READ | EV_PERSIST,
843 handle_dispatch_imsg, netibuf);
844 event_add(&netev, NULL);
846 event_set(&fsev, fsibuf->fd, EV_READ | EV_PERSIST,
847 handle_dispatch_imsg, fsibuf);
848 event_add(&fsev, NULL);
851 load_last_session(session_new_tab_cb);
852 if (has_url || TAILQ_EMPTY(&tabshead))
855 sandbox_ui_process();
860 imsg_compose(netibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
863 imsg_compose(fsibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);