1 #include <sys/socket.h>
13 #include "telescope.h"
16 static struct imsgev *iev_fs, *iev_net;
18 struct tabshead tabshead = TAILQ_HEAD_INITIALIZER(tabshead);
19 struct proxylist proxies = TAILQ_HEAD_INITIALIZER(proxies);
21 enum telescope_process {
27 /* the first is also the fallback one */
28 static struct proto protos[] = {
29 { "gemini", load_gemini_url },
30 { "about", load_about_url },
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 *, struct tab *);
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);
54 static int ui_send_net(int, uint32_t, const void *, uint16_t);
55 static int ui_send_fs(int, uint32_t, const void *, uint16_t);
57 static imsg_handlerfn *handlers[] = {
58 [IMSG_ERR] = handle_imsg_err,
59 [IMSG_CHECK_CERT] = handle_imsg_check_cert,
60 [IMSG_GOT_CODE] = handle_imsg_got_code,
61 [IMSG_GOT_META] = handle_imsg_got_meta,
62 [IMSG_BUF] = handle_imsg_buf,
63 [IMSG_EOF] = handle_imsg_eof,
64 [IMSG_BOOKMARK_OK] = handle_imsg_bookmark_ok,
65 [IMSG_SAVE_CERT_OK] = handle_imsg_save_cert_ok,
66 [IMSG_UPDATE_CERT_OK] = handle_imsg_update_cert_ok,
67 [IMSG_FILE_OPENED] = handle_imsg_file_opened,
70 static struct ohash certs;
72 static void __attribute__((__noreturn__))
79 tab_by_id(uint32_t id)
83 TAILQ_FOREACH(t, &tabshead, tabs) {
92 handle_imsg_err(struct imsg *imsg, size_t datalen)
97 tab = tab_by_id(imsg->hdr.peerid);
100 page[datalen-1] = '\0';
102 if (asprintf(&page, "# Error loading %s\n\n> %s\n",
103 tab->hist_cur->h, page) == -1)
105 load_page_from_str(tab, page);
110 handle_imsg_check_cert(struct imsg *imsg, size_t datalen)
112 const char *hash, *host, *port;
114 struct tofu_entry *e;
118 if (hash[datalen-1] != '\0')
121 tab = tab_by_id(imsg->hdr.peerid);
123 if (tab->proxy != NULL) {
124 host = tab->proxy->host;
125 port = tab->proxy->port;
127 host = tab->uri.host;
128 port = tab->uri.port;
131 if ((e = tofu_lookup(&certs, host, port)) == NULL) {
132 /* TODO: an update in libressl/libretls changed
133 * significantly. Find a better approach at storing
135 if (datalen > sizeof(e->hash))
138 tofu_res = 1; /* trust on first use */
139 if ((e = calloc(1, sizeof(*e))) == NULL)
141 strlcpy(e->domain, host, sizeof(e->domain));
142 if (*port != '\0' && strcmp(port, "1965")) {
143 strlcat(e->domain, ":", sizeof(e->domain));
144 strlcat(e->domain, port, sizeof(e->domain));
146 strlcpy(e->hash, hash, sizeof(e->hash));
148 ui_send_fs(IMSG_SAVE_CERT, tab->id, e, sizeof(*e));
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 ui_send_net(IMSG_CERT_STATUS, imsg->hdr.peerid,
161 &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 ui_send_net(IMSG_CERT_STATUS, tab->id, &accept,
180 * trust the certificate for this session only. If
181 * the page results in a redirect while we're asking
182 * the user to save, we'll end up with an invalid
183 * tabid (one request == one tab id) and crash. It
184 * also makes sense to save it for the current session
185 * if the user accepted it.
187 tofu_temp_trust(&certs, tab->uri.host, tab->uri.port, tab->cert);
189 ui_yornp("Save the new certificate?",
190 handle_maybe_save_new_cert, tab);
198 handle_maybe_save_new_cert(int accept, struct tab *tab)
200 struct tofu_entry *e;
201 const char *host, *port;
203 if (tab->proxy != NULL) {
204 host = tab->proxy->host;
205 port = tab->proxy->port;
207 host = tab->uri.host;
208 port = tab->uri.port;
214 if ((e = calloc(1, sizeof(*e))) == NULL)
217 strlcpy(e->domain, host, sizeof(e->domain));
218 if (*port != '\0' && strcmp(port, "1965")) {
219 strlcat(e->domain, ":", sizeof(e->domain));
220 strlcat(e->domain, port, sizeof(e->domain));
222 strlcpy(e->hash, tab->cert, sizeof(e->hash));
223 ui_send_fs(IMSG_UPDATE_CERT, 0, e, sizeof(*e));
225 tofu_update(&certs, e);
227 tab->trust = TS_TRUSTED;
235 normalize_code(int n)
238 if (n == 10 || n == 11)
244 if (n == 30 || n == 31)
252 if (n <= 53 || n == 59)
260 return MALFORMED_RESPONSE;
264 handle_imsg_got_code(struct imsg *imsg, size_t datalen)
268 tab = tab_by_id(imsg->hdr.peerid);
270 if (sizeof(tab->code) != datalen)
273 memcpy(&tab->code, imsg->data, sizeof(tab->code));
274 tab->code = normalize_code(tab->code);
275 if (tab->code != 30 && tab->code != 31)
276 tab->redirect_count = 0;
280 handle_imsg_got_meta(struct imsg *imsg, size_t datalen)
284 tab = tab_by_id(imsg->hdr.peerid);
286 if (sizeof(tab->meta) <= datalen)
289 memcpy(tab->meta, imsg->data, datalen);
291 if (tab->code < 10) { /* internal errors */
292 load_page_from_str(tab, err_pages[tab->code]);
293 } else if (tab->code < 20) { /* 1x */
294 load_page_from_str(tab, err_pages[tab->code]);
295 ui_require_input(tab, tab->code == 11);
296 } else if (tab->code == 20) {
297 if (setup_parser_for(tab)) {
298 ui_send_net(IMSG_PROCEED, tab->id, NULL, 0);
300 load_page_from_str(tab, err_pages[UNKNOWN_TYPE_OR_CSET]);
301 ui_yornp("Can't display page, wanna save?",
302 handle_maybe_save_page, tab);
304 } else if (tab->code < 40) { /* 3x */
305 tab->redirect_count++;
307 /* TODO: make customizable? */
308 if (tab->redirect_count > 5) {
309 load_page_from_str(tab,
310 err_pages[TOO_MUCH_REDIRECTS]);
312 do_load_url(tab, tab->meta);
313 } else { /* 4x, 5x & 6x */
314 load_page_from_str(tab, err_pages[tab->code]);
319 handle_maybe_save_page(int dosave, struct tab *tab)
322 ui_read("Save to path", handle_save_page_path, tab);
328 handle_save_page_path(const char *path, struct tab *tab)
335 tab->path = strdup(path);
337 ui_send_fs(IMSG_FILE_OPEN, tab->id, path, strlen(path)+1);
341 handle_imsg_file_opened(struct imsg *imsg, size_t datalen)
348 tab = tab_by_id(imsg->hdr.peerid);
350 if (imsg->fd == -1) {
354 if (e[datalen-1] != '\0')
356 l = asprintf(&page, "# Can't open file\n\n> %s: %s\n",
360 load_page_from_str(tab, page);
364 ui_send_net(IMSG_PROCEED, tab->id, NULL, 0);
369 handle_imsg_buf(struct imsg *imsg, size_t datalen)
373 char *page, buf[FMT_SCALED_STRSIZE] = {0};
375 tab = tab_by_id(imsg->hdr.peerid);
377 tab->bytes += datalen;
379 if (!tab->buffer.page.parse(&tab->buffer.page,
380 imsg->data, datalen))
383 write(tab->fd, imsg->data, datalen);
384 fmt_scaled(tab->bytes, buf);
385 l = asprintf(&page, "Saving to \"%s\"... (%s)\n",
390 load_page_from_str(tab, page);
394 ui_on_tab_refresh(tab);
398 handle_imsg_eof(struct imsg *imsg, size_t datalen)
402 char *page, buf[FMT_SCALED_STRSIZE] = {0};
404 tab = tab_by_id(imsg->hdr.peerid);
407 if (!tab->buffer.page.free(&tab->buffer.page))
410 fmt_scaled(tab->bytes, buf);
411 l = asprintf(&page, "Saved to \"%s\" (%s)\n",
416 load_page_from_str(tab, page);
425 ui_on_tab_refresh(tab);
426 ui_on_tab_loaded(tab);
430 handle_imsg_bookmark_ok(struct imsg *imsg, size_t datalen)
434 if (datalen != sizeof(res))
437 memcpy(&res, imsg->data, sizeof(res));
439 message("Added to bookmarks!");
441 message("Failed to add to bookmarks: %s",
446 handle_imsg_save_cert_ok(struct imsg *imsg, size_t datalen)
450 if (datalen != sizeof(res))
452 memcpy(&res, imsg->data, datalen);
454 message("Failed to save the cert for: %s",
459 handle_imsg_update_cert_ok(struct imsg *imsg, size_t datalen)
463 if (datalen != sizeof(res))
465 memcpy(&res, imsg->data, datalen);
467 message("Failed to update the certificate");
471 handle_dispatch_imsg(int fd, short ev, void *d)
473 struct imsgev *iev = d;
474 dispatch_imsg(iev, ev, handlers, sizeof(handlers));
478 load_page_from_str(struct tab *tab, const char *page)
480 erase_buffer(&tab->buffer);
481 gemtext_initparser(&tab->buffer.page);
482 if (!tab->buffer.page.parse(&tab->buffer.page, page, strlen(page)))
484 if (!tab->buffer.page.free(&tab->buffer.page))
486 ui_on_tab_refresh(tab);
487 ui_on_tab_loaded(tab);
491 load_about_url(struct tab *tab, const char *url)
493 tab->trust = TS_VERIFIED;
495 gemtext_initparser(&tab->buffer.page);
497 ui_send_fs(IMSG_GET, tab->id,
498 tab->hist_cur->h, strlen(tab->hist_cur->h)+1);
502 load_gemini_url(struct tab *tab, const char *url)
507 tab->id = tab_new_id();
509 memset(&req, 0, sizeof(req));
510 strlcpy(req.host, tab->uri.host, sizeof(req.host));
511 strlcpy(req.port, tab->uri.port, sizeof(req.host));
513 strlcpy(req.req, tab->hist_cur->h, sizeof(req.req));
514 strlcat(req.req, "\r\n", sizeof(req.req));
516 req.proto = PROTO_GEMINI;
518 ui_send_net(IMSG_GET_RAW, tab->id,
523 load_via_proxy(struct tab *tab, const char *url, struct proxy *p)
528 tab->id = tab_new_id();
531 memset(&req, 0, sizeof(req));
532 strlcpy(req.host, p->host, sizeof(req.host));
533 strlcpy(req.port, p->port, sizeof(req.host));
535 strlcpy(req.req, tab->hist_cur->h, sizeof(req.req));
536 strlcat(req.req, "\r\n", sizeof(req.req));
538 req.proto = p->proto;
540 ui_send_net(IMSG_GET_RAW, tab->id,
545 do_load_url(struct tab *tab, const char *url)
561 tab->trust = TS_UNKNOWN;
563 memcpy(&uri, &tab->uri, sizeof(tab->uri));
564 if (!phos_resolve_uri_from_str(&uri, url, &tab->uri)) {
565 if (asprintf(&t, "#error loading %s\n>%s\n",
566 url, "Can't parse the URI") == -1)
568 strlcpy(tab->hist_cur->h, url, sizeof(tab->hist_cur->h));
569 load_page_from_str(tab, t);
574 phos_serialize_uri(&tab->uri, tab->hist_cur->h,
575 sizeof(tab->hist_cur->h));
577 for (p = protos; p->schema != NULL; ++p) {
578 if (!strcmp(tab->uri.scheme, p->schema)) {
584 TAILQ_FOREACH(proxy, &proxies, proxies) {
585 if (!strcmp(tab->uri.scheme, proxy->match_proto)) {
586 load_via_proxy(tab, url, proxy);
591 protos[0].loadfn(tab, url);
595 load_url(struct tab *tab, const char *url)
597 if (tab->hist_cur != NULL)
598 hist_clear_forward(&tab->hist, TAILQ_NEXT(tab->hist_cur, entries));
600 if ((tab->hist_cur = calloc(1, sizeof(*tab->hist_cur))) == NULL) {
605 hist_push(&tab->hist, tab->hist_cur);
606 do_load_url(tab, url);
607 erase_buffer(&tab->buffer);
611 load_previous_page(struct tab *tab)
615 if ((h = TAILQ_PREV(tab->hist_cur, mhisthead, entries)) == NULL)
618 do_load_url(tab, h->h);
623 load_next_page(struct tab *tab)
627 if ((h = TAILQ_NEXT(tab->hist_cur, entries)) == NULL)
630 do_load_url(tab, h->h);
635 stop_tab(struct tab *tab)
637 ui_send_net(IMSG_STOP, tab->id, NULL, 0);
644 load_page_from_str(tab, "Stopped.\n");
649 add_to_bookmarks(const char *str)
651 ui_send_fs(IMSG_BOOKMARK_PAGE, 0,
660 ui_send_fs(IMSG_SESSION_START, 0, NULL, 0);
662 TAILQ_FOREACH(tab, &tabshead, tabs) {
663 ui_send_fs(IMSG_SESSION_TAB, 0,
664 tab->hist_cur->h, strlen(tab->hist_cur->h)+1);
667 ui_send_fs(IMSG_SESSION_END, 0, NULL, 0);
671 session_new_tab_cb(const char *url)
677 start_child(enum telescope_process p, const char *argv0, int fd)
683 switch (pid = fork()) {
693 if (dup2(fd, 3) == -1)
694 err(1, "cannot setup imsg fd");
696 argv[argc++] = argv0;
699 errx(1, "Can't start ui process");
701 argv[argc++] = "-Tf";
704 argv[argc++] = "-Tn";
709 execvp(argv0, (char *const *)argv);
710 err(1, "execvp(%s)", argv0);
714 ui_send_net(int type, uint32_t peerid, const void *data,
717 return imsg_compose_event(iev_net, type, peerid, 0, -1, data,
722 ui_send_fs(int type, uint32_t peerid, const void *data, uint16_t datalen)
724 return imsg_compose_event(iev_fs, type, peerid, 0, -1, data,
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 imsgev net_ibuf, fs_ibuf;
740 int pipe2net[2], pipe2fs[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, pipe2fs) == -1)
821 err(1, "socketpair");
822 start_child(PROC_FS, argv0, pipe2fs[1]);
823 imsg_init(&fs_ibuf.ibuf, pipe2fs[0]);
825 iev_fs->handler = handle_dispatch_imsg;
827 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe2net) == -1)
828 err(1, "socketpair");
829 start_child(PROC_NET, argv0, pipe2net[1]);
830 imsg_init(&net_ibuf.ibuf, pipe2net[0]);
832 iev_net->handler = handle_dispatch_imsg;
836 /* initialize tofu & load certificates */
838 tofu_init(&certs, 5, offsetof(struct tofu_entry, domain));
843 /* Setup event handlers for pipes to fs/net */
844 iev_fs->events = EV_READ;
845 event_set(&iev_fs->ev, iev_fs->ibuf.fd, iev_fs->events,
846 iev_fs->handler, iev_fs);
847 event_add(&iev_fs->ev, NULL);
849 iev_net->events = EV_READ;
850 event_set(&iev_net->ev, iev_net->ibuf.fd, iev_net->events,
851 iev_net->handler, iev_net);
852 event_add(&iev_net->ev, NULL);
855 load_last_session(session_new_tab_cb);
856 if (has_url || TAILQ_EMPTY(&tabshead))
859 sandbox_ui_process();
864 ui_send_fs(IMSG_QUIT, 0, NULL, 0);
865 ui_send_net(IMSG_QUIT, 0, NULL, 0);
866 imsg_flush(&iev_fs->ibuf);