1 #include <sys/socket.h>
12 #include "telescope.h"
14 struct event netev, fsev;
15 struct tabshead tabshead = TAILQ_HEAD_INITIALIZER(tabshead);
16 struct proxylist proxies = TAILQ_HEAD_INITIALIZER(proxies);
18 enum telescope_process {
24 /* the first is also the fallback one */
25 static struct proto protos[] = {
26 { "gemini", load_gemini_url },
27 { "about", load_about_url },
31 static struct imsgbuf *netibuf, *fsibuf;
33 static void die(void) __attribute__((__noreturn__));
34 static struct tab *tab_by_id(uint32_t);
35 static void handle_imsg_err(struct imsg*, size_t);
36 static void handle_imsg_check_cert(struct imsg*, size_t);
37 static void handle_check_cert_user_choice(int, struct tab *);
38 static void handle_maybe_save_new_cert(int, struct tab *);
39 static void handle_imsg_got_code(struct imsg*, size_t);
40 static void handle_imsg_got_meta(struct imsg*, size_t);
41 static void handle_maybe_save_page(int, struct tab *);
42 static void handle_save_page_path(const char *, unsigned int);
43 static void handle_imsg_file_opened(struct imsg*, size_t);
44 static void handle_imsg_buf(struct imsg*, size_t);
45 static void handle_imsg_eof(struct imsg*, size_t);
46 static void handle_imsg_bookmark_ok(struct imsg*, size_t);
47 static void handle_imsg_save_cert_ok(struct imsg*, size_t);
48 static void handle_imsg_update_cert_ok(struct imsg *, size_t);
49 static void handle_dispatch_imsg(int, short, void*);
50 static void load_page_from_str(struct tab*, const char*);
51 static void do_load_url(struct tab*, const char*);
52 static pid_t start_child(enum telescope_process, const char *, int);
54 static imsg_handlerfn *handlers[] = {
55 [IMSG_ERR] = handle_imsg_err,
56 [IMSG_CHECK_CERT] = handle_imsg_check_cert,
57 [IMSG_GOT_CODE] = handle_imsg_got_code,
58 [IMSG_GOT_META] = handle_imsg_got_meta,
59 [IMSG_BUF] = handle_imsg_buf,
60 [IMSG_EOF] = handle_imsg_eof,
61 [IMSG_BOOKMARK_OK] = handle_imsg_bookmark_ok,
62 [IMSG_SAVE_CERT_OK] = handle_imsg_save_cert_ok,
63 [IMSG_UPDATE_CERT_OK] = handle_imsg_update_cert_ok,
64 [IMSG_FILE_OPENED] = handle_imsg_file_opened,
67 static struct ohash certs;
69 static void __attribute__((__noreturn__))
76 tab_by_id(uint32_t id)
80 TAILQ_FOREACH(t, &tabshead, tabs) {
89 handle_imsg_err(struct imsg *imsg, size_t datalen)
94 tab = tab_by_id(imsg->hdr.peerid);
97 page[datalen-1] = '\0';
99 if (asprintf(&page, "# Error loading %s\n\n> %s\n",
100 tab->hist_cur->h, page) == -1)
102 load_page_from_str(tab, page);
107 handle_imsg_check_cert(struct imsg *imsg, size_t datalen)
109 const char *hash, *host, *port;
111 struct tofu_entry *e;
115 if (hash[datalen-1] != '\0')
118 tab = tab_by_id(imsg->hdr.peerid);
120 if (tab->proxy != NULL) {
121 host = tab->proxy->host;
122 port = tab->proxy->port;
124 host = tab->uri.host;
125 port = tab->uri.port;
128 if ((e = tofu_lookup(&certs, host, port)) == NULL) {
129 /* TODO: an update in libressl/libretls changed
130 * significantly. Find a better approach at storing
132 if (datalen > sizeof(e->hash))
135 tofu_res = 1; /* trust on first use */
136 if ((e = calloc(1, sizeof(*e))) == NULL)
138 strlcpy(e->domain, host, sizeof(e->domain));
139 if (*port != '\0' && strcmp(port, "1965")) {
140 strlcat(e->domain, ":", sizeof(e->domain));
141 strlcat(e->domain, port, sizeof(e->domain));
143 strlcpy(e->hash, hash, sizeof(e->hash));
145 imsg_compose(fsibuf, IMSG_SAVE_CERT, tab->id, 0, -1,
149 tofu_res = !strcmp(hash, e->hash);
152 if (e->verified == -1)
153 tab->trust = TS_TEMP_TRUSTED;
154 else if (e->verified == 1)
155 tab->trust = TS_VERIFIED;
157 tab->trust = TS_TRUSTED;
159 imsg_compose(netibuf, IMSG_CERT_STATUS, imsg->hdr.peerid, 0, -1,
160 &tofu_res, sizeof(tofu_res));
163 tab->trust = TS_UNTRUSTED;
164 load_page_from_str(tab, "# Certificate mismatch\n");
165 if ((tab->cert = strdup(hash)) == NULL)
167 ui_yornp("Certificate mismatch. Proceed?",
168 handle_check_cert_user_choice, tab);
173 handle_check_cert_user_choice(int accept, struct tab *tab)
175 imsg_compose(netibuf, IMSG_CERT_STATUS, tab->id, 0, -1,
176 &accept, sizeof(accept));
181 * trust the certificate for this session only. If
182 * the page results in a redirect while we're asking
183 * the user to save, we'll end up with an invalid
184 * tabid (one request == one tab id) and crash. It
185 * also makes sense to save it for the current session
186 * if the user accepted it.
188 tofu_temp_trust(&certs, tab->uri.host, tab->uri.port, tab->cert);
190 ui_yornp("Save the new certificate?",
191 handle_maybe_save_new_cert, tab);
199 handle_maybe_save_new_cert(int accept, struct tab *tab)
201 struct tofu_entry *e;
202 const char *host, *port;
204 if (tab->proxy != NULL) {
205 host = tab->proxy->host;
206 port = tab->proxy->port;
208 host = tab->uri.host;
209 port = tab->uri.port;
215 if ((e = calloc(1, sizeof(*e))) == NULL)
218 strlcpy(e->domain, host, sizeof(e->domain));
219 if (*port != '\0' && strcmp(port, "1965")) {
220 strlcat(e->domain, ":", sizeof(e->domain));
221 strlcat(e->domain, port, sizeof(e->domain));
223 strlcpy(e->hash, tab->cert, sizeof(e->hash));
224 imsg_compose(fsibuf, IMSG_UPDATE_CERT, 0, 0, -1, e, sizeof(*e));
227 tofu_update(&certs, e);
229 tab->trust = TS_TRUSTED;
237 normalize_code(int n)
240 if (n == 10 || n == 11)
246 if (n == 30 || n == 31)
254 if (n <= 53 || n == 59)
262 return MALFORMED_RESPONSE;
266 handle_imsg_got_code(struct imsg *imsg, size_t datalen)
270 tab = tab_by_id(imsg->hdr.peerid);
272 if (sizeof(tab->code) != datalen)
275 memcpy(&tab->code, imsg->data, sizeof(tab->code));
276 tab->code = normalize_code(tab->code);
277 if (tab->code != 30 && tab->code != 31)
278 tab->redirect_count = 0;
282 handle_imsg_got_meta(struct imsg *imsg, size_t datalen)
286 tab = tab_by_id(imsg->hdr.peerid);
288 if (sizeof(tab->meta) <= datalen)
291 memcpy(tab->meta, imsg->data, datalen);
293 if (tab->code < 10) { /* internal errors */
294 load_page_from_str(tab, err_pages[tab->code]);
295 } else if (tab->code < 20) { /* 1x */
296 load_page_from_str(tab, err_pages[tab->code]);
297 ui_require_input(tab, tab->code == 11);
298 } else if (tab->code == 20) {
299 if (setup_parser_for(tab)) {
300 imsg_compose(netibuf, IMSG_PROCEED, tab->id, 0, -1, NULL, 0);
303 load_page_from_str(tab, err_pages[UNKNOWN_TYPE_OR_CSET]);
304 ui_yornp("Can't display page, wanna save?",
305 handle_maybe_save_page, tab);
307 } else if (tab->code < 40) { /* 3x */
308 tab->redirect_count++;
310 /* TODO: make customizable? */
311 if (tab->redirect_count > 5) {
312 load_page_from_str(tab,
313 err_pages[TOO_MUCH_REDIRECTS]);
315 do_load_url(tab, tab->meta);
316 } else { /* 4x, 5x & 6x */
317 load_page_from_str(tab, err_pages[tab->code]);
322 handle_maybe_save_page(int dosave, struct tab *tab)
325 ui_read("Save to path", handle_save_page_path, tab->id);
331 handle_save_page_path(const char *path, unsigned int tabid)
336 stop_tab(tab_by_id(tabid));
340 tab = tab_by_id(tabid);
341 tab->path = strdup(path);
343 imsg_compose(fsibuf, IMSG_FILE_OPEN, tabid, 0, -1, path, strlen(path)+1);
348 handle_imsg_file_opened(struct imsg *imsg, size_t datalen)
355 tab = tab_by_id(imsg->hdr.peerid);
357 if (imsg->fd == -1) {
361 if (e[datalen-1] != '\0')
363 l = asprintf(&page, "# Can't open file\n\n> %s: %s\n",
367 load_page_from_str(tab, page);
371 imsg_compose(netibuf, IMSG_PROCEED, tab->id, 0, -1, NULL, 0);
377 handle_imsg_buf(struct imsg *imsg, size_t datalen)
381 char *page, buf[FMT_SCALED_STRSIZE] = {0};
383 tab = tab_by_id(imsg->hdr.peerid);
385 tab->bytes += datalen;
387 if (!tab->buffer.page.parse(&tab->buffer.page,
388 imsg->data, datalen))
391 write(tab->fd, imsg->data, datalen);
392 fmt_scaled(tab->bytes, buf);
393 l = asprintf(&page, "Saving to \"%s\"... (%s)\n",
398 load_page_from_str(tab, page);
402 ui_on_tab_refresh(tab);
406 handle_imsg_eof(struct imsg *imsg, size_t datalen)
410 char *page, buf[FMT_SCALED_STRSIZE] = {0};
412 tab = tab_by_id(imsg->hdr.peerid);
415 if (!tab->buffer.page.free(&tab->buffer.page))
418 fmt_scaled(tab->bytes, buf);
419 l = asprintf(&page, "Saved to \"%s\" (%s)\n",
424 load_page_from_str(tab, page);
433 ui_on_tab_refresh(tab);
434 ui_on_tab_loaded(tab);
438 handle_imsg_bookmark_ok(struct imsg *imsg, size_t datalen)
442 if (datalen != sizeof(res))
445 memcpy(&res, imsg->data, sizeof(res));
447 message("Added to bookmarks!");
449 message("Failed to add to bookmarks: %s",
454 handle_imsg_save_cert_ok(struct imsg *imsg, size_t datalen)
458 if (datalen != sizeof(res))
460 memcpy(&res, imsg->data, datalen);
462 message("Failed to save the cert for: %s",
467 handle_imsg_update_cert_ok(struct imsg *imsg, size_t datalen)
471 if (datalen != sizeof(res))
473 memcpy(&res, imsg->data, datalen);
475 message("Failed to update the certificate");
479 handle_dispatch_imsg(int fd, short ev, void *d)
481 struct imsgbuf *ibuf = d;
482 dispatch_imsg(ibuf, handlers, sizeof(handlers));
486 load_page_from_str(struct tab *tab, const char *page)
488 erase_buffer(&tab->buffer);
489 gemtext_initparser(&tab->buffer.page);
490 if (!tab->buffer.page.parse(&tab->buffer.page, page, strlen(page)))
492 if (!tab->buffer.page.free(&tab->buffer.page))
494 ui_on_tab_refresh(tab);
495 ui_on_tab_loaded(tab);
499 load_about_url(struct tab *tab, const char *url)
501 tab->trust = TS_VERIFIED;
503 gemtext_initparser(&tab->buffer.page);
505 imsg_compose(fsibuf, IMSG_GET, tab->id, 0, -1,
506 tab->hist_cur->h, strlen(tab->hist_cur->h)+1);
511 load_gemini_url(struct tab *tab, const char *url)
516 tab->id = tab_new_id();
518 memset(&req, 0, sizeof(req));
519 strlcpy(req.host, tab->uri.host, sizeof(req.host));
520 strlcpy(req.port, tab->uri.port, sizeof(req.host));
522 strlcpy(req.req, tab->hist_cur->h, sizeof(req.req));
523 strlcat(req.req, "\r\n", sizeof(req.req));
525 req.proto = PROTO_GEMINI;
527 imsg_compose(netibuf, IMSG_GET_RAW, tab->id, 0, -1,
533 load_via_proxy(struct tab *tab, const char *url, struct proxy *p)
538 tab->id = tab_new_id();
541 memset(&req, 0, sizeof(req));
542 strlcpy(req.host, p->host, sizeof(req.host));
543 strlcpy(req.port, p->port, sizeof(req.host));
545 strlcpy(req.req, tab->hist_cur->h, sizeof(req.req));
546 strlcat(req.req, "\r\n", sizeof(req.req));
548 req.proto = p->proto;
550 imsg_compose(netibuf, IMSG_GET_RAW, tab->id, 0, -1,
556 do_load_url(struct tab *tab, const char *url)
572 tab->trust = TS_UNKNOWN;
574 memcpy(&uri, &tab->uri, sizeof(tab->uri));
575 if (!phos_resolve_uri_from_str(&uri, url, &tab->uri)) {
576 if (asprintf(&t, "#error loading %s\n>%s\n",
577 url, "Can't parse the URI") == -1)
579 strlcpy(tab->hist_cur->h, url, sizeof(tab->hist_cur->h));
580 load_page_from_str(tab, t);
585 phos_serialize_uri(&tab->uri, tab->hist_cur->h,
586 sizeof(tab->hist_cur->h));
588 for (p = protos; p->schema != NULL; ++p) {
589 if (!strcmp(tab->uri.scheme, p->schema)) {
595 TAILQ_FOREACH(proxy, &proxies, proxies) {
596 if (!strcmp(tab->uri.scheme, proxy->match_proto)) {
597 load_via_proxy(tab, url, proxy);
602 protos[0].loadfn(tab, url);
606 load_url(struct tab *tab, const char *url)
608 if (tab->hist_cur != NULL)
609 hist_clear_forward(&tab->hist, TAILQ_NEXT(tab->hist_cur, entries));
611 if ((tab->hist_cur = calloc(1, sizeof(*tab->hist_cur))) == NULL) {
616 hist_push(&tab->hist, tab->hist_cur);
617 do_load_url(tab, url);
618 erase_buffer(&tab->buffer);
622 load_previous_page(struct tab *tab)
626 if ((h = TAILQ_PREV(tab->hist_cur, mhisthead, entries)) == NULL)
629 do_load_url(tab, h->h);
634 load_next_page(struct tab *tab)
638 if ((h = TAILQ_NEXT(tab->hist_cur, entries)) == NULL)
641 do_load_url(tab, h->h);
646 stop_tab(struct tab *tab)
648 imsg_compose(netibuf, IMSG_STOP, tab->id, 0, -1, NULL, 0);
656 load_page_from_str(tab, "Stopped.\n");
661 add_to_bookmarks(const char *str)
663 imsg_compose(fsibuf, IMSG_BOOKMARK_PAGE, 0, 0, -1, str, strlen(str)+1);
672 imsg_compose(fsibuf, IMSG_SESSION_START, 0, 0, -1, NULL, 0);
675 TAILQ_FOREACH(tab, &tabshead, tabs) {
676 imsg_compose(fsibuf, IMSG_SESSION_TAB, 0, 0, -1,
677 tab->hist_cur->h, strlen(tab->hist_cur->h)+1);
681 imsg_compose(fsibuf, IMSG_SESSION_END, 0, 0, -1, NULL, 0);
686 session_new_tab_cb(const char *url)
692 start_child(enum telescope_process p, const char *argv0, int fd)
698 switch (pid = fork()) {
708 if (dup2(fd, 3) == -1)
709 err(1, "cannot setup imsg fd");
711 argv[argc++] = argv0;
714 errx(1, "Can't start ui process");
716 argv[argc++] = "-Tf";
719 argv[argc++] = "-Tn";
724 execvp(argv0, (char *const *)argv);
725 err(1, "execvp(%s)", argv0);
728 static void __attribute__((noreturn))
731 fprintf(stderr, "USAGE: %s [-hn] [-c config] [url]\n", getprogname());
732 fprintf(stderr, "version: " PACKAGE " " VERSION "\n");
737 main(int argc, char * const *argv)
739 struct imsgbuf network_ibuf, fs_ibuf;
740 int net_fds[2], fs_fds[2];
741 int ch, configtest = 0, fail = 0;
745 const char *url = NEW_TAB_URL;
750 signal(SIGCHLD, SIG_IGN);
751 signal(SIGPIPE, SIG_IGN);
753 if (getenv("NO_COLOR") != NULL)
756 strlcpy(path, getenv("HOME"), sizeof(path));
757 strlcat(path, "/.telescope/config", sizeof(path));
759 while ((ch = getopt(argc, argv, "c:hnT:")) != -1) {
763 strlcpy(path, optarg, sizeof(path));
779 errx(1, "invalid process spec %c",
794 else if (proc == PROC_FS)
796 else if (proc == PROC_NET)
797 return client_main();
807 /* setup keys before reading the config */
808 TAILQ_INIT(&global_map.m);
809 global_map.unhandled_input = global_key_unbound;
810 TAILQ_INIT(&minibuffer_map.m);
813 parseconfig(path, fail);
819 /* Start children. */
820 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, fs_fds) == -1)
821 err(1, "socketpair");
822 start_child(PROC_FS, argv0, fs_fds[1]);
823 imsg_init(&fs_ibuf, fs_fds[0]);
826 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, net_fds) == -1)
827 err(1, "socketpair");
828 start_child(PROC_NET, argv0, net_fds[1]);
829 imsg_init(&network_ibuf, net_fds[0]);
830 netibuf = &network_ibuf;
834 /* initialize tofu & load certificates */
836 tofu_init(&certs, 5, offsetof(struct tofu_entry, domain));
841 event_set(&netev, netibuf->fd, EV_READ | EV_PERSIST,
842 handle_dispatch_imsg, netibuf);
843 event_add(&netev, NULL);
845 event_set(&fsev, fsibuf->fd, EV_READ | EV_PERSIST,
846 handle_dispatch_imsg, fsibuf);
847 event_add(&fsev, NULL);
850 load_last_session(session_new_tab_cb);
851 if (has_url || TAILQ_EMPTY(&tabshead))
854 sandbox_ui_process();
859 imsg_compose(netibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
862 imsg_compose(fsibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);