1 #include <sys/socket.h>
14 #include "telescope.h"
17 static struct imsgev *iev_fs, *iev_net;
19 struct tabshead tabshead = TAILQ_HEAD_INITIALIZER(tabshead);
20 struct proxylist proxies = TAILQ_HEAD_INITIALIZER(proxies);
22 enum telescope_process {
28 /* the first is also the fallback one */
29 static struct proto protos[] = {
30 { "gemini", load_gemini_url },
31 { "about", load_about_url },
35 static void die(void) __attribute__((__noreturn__));
36 static struct tab *tab_by_id(uint32_t);
37 static void handle_imsg_err(struct imsg*, size_t);
38 static void handle_imsg_check_cert(struct imsg*, size_t);
39 static void handle_check_cert_user_choice(int, struct tab *);
40 static void handle_maybe_save_new_cert(int, struct tab *);
41 static void handle_imsg_got_code(struct imsg*, size_t);
42 static void handle_imsg_got_meta(struct imsg*, size_t);
43 static void handle_maybe_save_page(int, struct tab *);
44 static void handle_save_page_path(const char *, struct tab *);
45 static void handle_imsg_file_opened(struct imsg*, size_t);
46 static void handle_imsg_buf(struct imsg*, size_t);
47 static void handle_imsg_eof(struct imsg*, size_t);
48 static void handle_imsg_bookmark_ok(struct imsg*, size_t);
49 static void handle_imsg_save_cert_ok(struct imsg*, size_t);
50 static void handle_imsg_update_cert_ok(struct imsg *, size_t);
51 static void handle_dispatch_imsg(int, short, void*);
52 static void load_page_from_str(struct tab*, const char*);
53 static void do_load_url(struct tab*, const char*);
54 static pid_t start_child(enum telescope_process, const char *, int);
55 static int ui_send_net(int, uint32_t, const void *, uint16_t);
56 static int ui_send_fs(int, uint32_t, const void *, uint16_t);
58 static imsg_handlerfn *handlers[] = {
59 [IMSG_ERR] = handle_imsg_err,
60 [IMSG_CHECK_CERT] = handle_imsg_check_cert,
61 [IMSG_GOT_CODE] = handle_imsg_got_code,
62 [IMSG_GOT_META] = handle_imsg_got_meta,
63 [IMSG_BUF] = handle_imsg_buf,
64 [IMSG_EOF] = handle_imsg_eof,
65 [IMSG_BOOKMARK_OK] = handle_imsg_bookmark_ok,
66 [IMSG_SAVE_CERT_OK] = handle_imsg_save_cert_ok,
67 [IMSG_UPDATE_CERT_OK] = handle_imsg_update_cert_ok,
68 [IMSG_FILE_OPENED] = handle_imsg_file_opened,
71 static struct ohash certs;
73 static void __attribute__((__noreturn__))
80 tab_by_id(uint32_t id)
84 TAILQ_FOREACH(t, &tabshead, tabs) {
93 handle_imsg_err(struct imsg *imsg, size_t datalen)
98 tab = tab_by_id(imsg->hdr.peerid);
101 page[datalen-1] = '\0';
103 if (asprintf(&page, "# Error loading %s\n\n> %s\n",
104 tab->hist_cur->h, page) == -1)
106 load_page_from_str(tab, page);
111 handle_imsg_check_cert(struct imsg *imsg, size_t datalen)
113 const char *hash, *host, *port;
115 struct tofu_entry *e;
119 if (hash[datalen-1] != '\0')
122 tab = tab_by_id(imsg->hdr.peerid);
124 if (tab->proxy != NULL) {
125 host = tab->proxy->host;
126 port = tab->proxy->port;
128 host = tab->uri.host;
129 port = tab->uri.port;
132 if ((e = tofu_lookup(&certs, host, port)) == NULL) {
133 /* TODO: an update in libressl/libretls changed
134 * significantly. Find a better approach at storing
136 if (datalen > sizeof(e->hash))
139 tofu_res = 1; /* trust on first use */
140 if ((e = calloc(1, sizeof(*e))) == NULL)
142 strlcpy(e->domain, host, sizeof(e->domain));
143 if (*port != '\0' && strcmp(port, "1965")) {
144 strlcat(e->domain, ":", sizeof(e->domain));
145 strlcat(e->domain, port, sizeof(e->domain));
147 strlcpy(e->hash, hash, sizeof(e->hash));
149 ui_send_fs(IMSG_SAVE_CERT, tab->id, e, sizeof(*e));
151 tofu_res = !strcmp(hash, e->hash);
154 if (e->verified == -1)
155 tab->trust = TS_TEMP_TRUSTED;
156 else if (e->verified == 1)
157 tab->trust = TS_VERIFIED;
159 tab->trust = TS_TRUSTED;
161 ui_send_net(IMSG_CERT_STATUS, imsg->hdr.peerid,
162 &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 ui_send_net(IMSG_CERT_STATUS, tab->id, &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 ui_send_fs(IMSG_UPDATE_CERT, 0, e, sizeof(*e));
226 tofu_update(&certs, e);
228 tab->trust = TS_TRUSTED;
236 normalize_code(int n)
239 if (n == 10 || n == 11)
245 if (n == 30 || n == 31)
253 if (n <= 53 || n == 59)
261 return MALFORMED_RESPONSE;
265 handle_imsg_got_code(struct imsg *imsg, size_t datalen)
269 tab = tab_by_id(imsg->hdr.peerid);
271 if (sizeof(tab->code) != datalen)
274 memcpy(&tab->code, imsg->data, sizeof(tab->code));
275 tab->code = normalize_code(tab->code);
276 if (tab->code != 30 && tab->code != 31)
277 tab->redirect_count = 0;
281 handle_imsg_got_meta(struct imsg *imsg, size_t datalen)
285 tab = tab_by_id(imsg->hdr.peerid);
287 if (sizeof(tab->meta) <= datalen)
290 memcpy(tab->meta, imsg->data, datalen);
292 if (tab->code < 10) { /* internal errors */
293 load_page_from_str(tab, err_pages[tab->code]);
294 } else if (tab->code < 20) { /* 1x */
295 load_page_from_str(tab, err_pages[tab->code]);
296 ui_require_input(tab, tab->code == 11);
297 } else if (tab->code == 20) {
298 if (setup_parser_for(tab)) {
299 ui_send_net(IMSG_PROCEED, tab->id, NULL, 0);
301 load_page_from_str(tab, err_pages[UNKNOWN_TYPE_OR_CSET]);
302 ui_yornp("Can't display page, wanna save?",
303 handle_maybe_save_page, tab);
305 } else if (tab->code < 40) { /* 3x */
306 tab->redirect_count++;
308 /* TODO: make customizable? */
309 if (tab->redirect_count > 5) {
310 load_page_from_str(tab,
311 err_pages[TOO_MUCH_REDIRECTS]);
313 do_load_url(tab, tab->meta);
314 } else { /* 4x, 5x & 6x */
315 load_page_from_str(tab, err_pages[tab->code]);
320 handle_maybe_save_page(int dosave, struct tab *tab)
323 ui_read("Save to path", handle_save_page_path, tab);
329 handle_save_page_path(const char *path, struct tab *tab)
336 tab->path = strdup(path);
338 ui_send_fs(IMSG_FILE_OPEN, tab->id, path, strlen(path)+1);
342 handle_imsg_file_opened(struct imsg *imsg, size_t datalen)
349 tab = tab_by_id(imsg->hdr.peerid);
351 if (imsg->fd == -1) {
355 if (e[datalen-1] != '\0')
357 l = asprintf(&page, "# Can't open file\n\n> %s: %s\n",
361 load_page_from_str(tab, page);
365 ui_send_net(IMSG_PROCEED, tab->id, NULL, 0);
370 handle_imsg_buf(struct imsg *imsg, size_t datalen)
374 char *page, buf[FMT_SCALED_STRSIZE] = {0};
376 tab = tab_by_id(imsg->hdr.peerid);
378 tab->bytes += datalen;
380 if (!tab->buffer.page.parse(&tab->buffer.page,
381 imsg->data, datalen))
384 write(tab->fd, imsg->data, datalen);
385 fmt_scaled(tab->bytes, buf);
386 l = asprintf(&page, "Saving to \"%s\"... (%s)\n",
391 load_page_from_str(tab, page);
395 ui_on_tab_refresh(tab);
399 handle_imsg_eof(struct imsg *imsg, size_t datalen)
403 char *page, buf[FMT_SCALED_STRSIZE] = {0};
405 tab = tab_by_id(imsg->hdr.peerid);
408 if (!tab->buffer.page.free(&tab->buffer.page))
411 fmt_scaled(tab->bytes, buf);
412 l = asprintf(&page, "Saved to \"%s\" (%s)\n",
417 load_page_from_str(tab, page);
426 ui_on_tab_refresh(tab);
427 ui_on_tab_loaded(tab);
431 handle_imsg_bookmark_ok(struct imsg *imsg, size_t datalen)
435 if (datalen != sizeof(res))
438 memcpy(&res, imsg->data, sizeof(res));
440 message("Added to bookmarks!");
442 message("Failed to add to bookmarks: %s",
447 handle_imsg_save_cert_ok(struct imsg *imsg, size_t datalen)
451 if (datalen != sizeof(res))
453 memcpy(&res, imsg->data, datalen);
455 message("Failed to save the cert for: %s",
460 handle_imsg_update_cert_ok(struct imsg *imsg, size_t datalen)
464 if (datalen != sizeof(res))
466 memcpy(&res, imsg->data, datalen);
468 message("Failed to update the certificate");
472 handle_dispatch_imsg(int fd, short ev, void *d)
474 struct imsgev *iev = d;
475 dispatch_imsg(iev, ev, handlers, sizeof(handlers));
479 load_page_from_str(struct tab *tab, const char *page)
481 erase_buffer(&tab->buffer);
482 gemtext_initparser(&tab->buffer.page);
483 if (!tab->buffer.page.parse(&tab->buffer.page, page, strlen(page)))
485 if (!tab->buffer.page.free(&tab->buffer.page))
487 ui_on_tab_refresh(tab);
488 ui_on_tab_loaded(tab);
492 load_about_url(struct tab *tab, const char *url)
494 tab->trust = TS_VERIFIED;
496 gemtext_initparser(&tab->buffer.page);
498 ui_send_fs(IMSG_GET, tab->id,
499 tab->hist_cur->h, strlen(tab->hist_cur->h)+1);
503 load_gemini_url(struct tab *tab, const char *url)
508 tab->id = tab_new_id();
510 memset(&req, 0, sizeof(req));
511 strlcpy(req.host, tab->uri.host, sizeof(req.host));
512 strlcpy(req.port, tab->uri.port, sizeof(req.host));
514 strlcpy(req.req, tab->hist_cur->h, sizeof(req.req));
515 strlcat(req.req, "\r\n", sizeof(req.req));
517 req.proto = PROTO_GEMINI;
519 ui_send_net(IMSG_GET_RAW, tab->id,
524 load_via_proxy(struct tab *tab, const char *url, struct proxy *p)
529 tab->id = tab_new_id();
532 memset(&req, 0, sizeof(req));
533 strlcpy(req.host, p->host, sizeof(req.host));
534 strlcpy(req.port, p->port, sizeof(req.host));
536 strlcpy(req.req, tab->hist_cur->h, sizeof(req.req));
537 strlcat(req.req, "\r\n", sizeof(req.req));
539 req.proto = p->proto;
541 ui_send_net(IMSG_GET_RAW, tab->id,
546 do_load_url(struct tab *tab, const char *url)
562 tab->trust = TS_UNKNOWN;
564 memcpy(&uri, &tab->uri, sizeof(tab->uri));
565 if (!phos_resolve_uri_from_str(&uri, url, &tab->uri)) {
566 if (asprintf(&t, "#error loading %s\n>%s\n",
567 url, "Can't parse the URI") == -1)
569 strlcpy(tab->hist_cur->h, url, sizeof(tab->hist_cur->h));
570 load_page_from_str(tab, t);
575 phos_serialize_uri(&tab->uri, tab->hist_cur->h,
576 sizeof(tab->hist_cur->h));
578 for (p = protos; p->schema != NULL; ++p) {
579 if (!strcmp(tab->uri.scheme, p->schema)) {
585 TAILQ_FOREACH(proxy, &proxies, proxies) {
586 if (!strcmp(tab->uri.scheme, proxy->match_proto)) {
587 load_via_proxy(tab, url, proxy);
592 protos[0].loadfn(tab, url);
596 load_url(struct tab *tab, const char *url)
598 if (tab->hist_cur != NULL)
599 hist_clear_forward(&tab->hist, TAILQ_NEXT(tab->hist_cur, entries));
601 if ((tab->hist_cur = calloc(1, sizeof(*tab->hist_cur))) == NULL) {
606 hist_push(&tab->hist, tab->hist_cur);
607 do_load_url(tab, url);
608 erase_buffer(&tab->buffer);
612 load_previous_page(struct tab *tab)
616 if ((h = TAILQ_PREV(tab->hist_cur, mhisthead, entries)) == NULL)
619 do_load_url(tab, h->h);
624 load_next_page(struct tab *tab)
628 if ((h = TAILQ_NEXT(tab->hist_cur, entries)) == NULL)
631 do_load_url(tab, h->h);
636 stop_tab(struct tab *tab)
638 ui_send_net(IMSG_STOP, tab->id, NULL, 0);
645 load_page_from_str(tab, "Stopped.\n");
650 add_to_bookmarks(const char *str)
652 ui_send_fs(IMSG_BOOKMARK_PAGE, 0,
661 ui_send_fs(IMSG_SESSION_START, 0, NULL, 0);
663 TAILQ_FOREACH(tab, &tabshead, tabs) {
664 ui_send_fs(IMSG_SESSION_TAB, 0,
665 tab->hist_cur->h, strlen(tab->hist_cur->h)+1);
668 ui_send_fs(IMSG_SESSION_END, 0, NULL, 0);
672 session_new_tab_cb(const char *url)
678 start_child(enum telescope_process p, const char *argv0, int fd)
684 switch (pid = fork()) {
694 if (dup2(fd, 3) == -1)
695 err(1, "cannot setup imsg fd");
697 argv[argc++] = argv0;
700 errx(1, "Can't start ui process");
702 argv[argc++] = "-Tf";
705 argv[argc++] = "-Tn";
710 execvp(argv0, (char *const *)argv);
711 err(1, "execvp(%s)", argv0);
715 ui_send_net(int type, uint32_t peerid, const void *data,
718 return imsg_compose_event(iev_net, type, peerid, 0, -1, data,
723 ui_send_fs(int type, uint32_t peerid, const void *data, uint16_t datalen)
725 return imsg_compose_event(iev_fs, type, peerid, 0, -1, data,
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 imsgev net_ibuf, fs_ibuf;
741 int pipe2net[2], pipe2fs[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, pipe2fs) == -1)
822 err(1, "socketpair");
823 start_child(PROC_FS, argv0, pipe2fs[1]);
824 imsg_init(&fs_ibuf.ibuf, pipe2fs[0]);
826 iev_fs->handler = handle_dispatch_imsg;
828 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe2net) == -1)
829 err(1, "socketpair");
830 start_child(PROC_NET, argv0, pipe2net[1]);
831 imsg_init(&net_ibuf.ibuf, pipe2net[0]);
833 iev_net->handler = handle_dispatch_imsg;
837 /* initialize tofu & load certificates */
839 tofu_init(&certs, 5, offsetof(struct tofu_entry, domain));
844 /* Setup event handlers for pipes to fs/net */
845 iev_fs->events = EV_READ;
846 event_set(&iev_fs->ev, iev_fs->ibuf.fd, iev_fs->events,
847 iev_fs->handler, iev_fs);
848 event_add(&iev_fs->ev, NULL);
850 iev_net->events = EV_READ;
851 event_set(&iev_net->ev, iev_net->ibuf.fd, iev_net->events,
852 iev_net->handler, iev_net);
853 event_add(&iev_net->ev, NULL);
856 load_last_session(session_new_tab_cb);
857 if (has_url || TAILQ_EMPTY(&tabshead))
860 sandbox_ui_process();
865 ui_send_fs(IMSG_QUIT, 0, NULL, 0);
866 ui_send_net(IMSG_QUIT, 0, NULL, 0);
867 imsg_flush(&iev_fs->ibuf);