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 *, 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);
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->id);
328 handle_save_page_path(const char *path, unsigned int tabid)
333 stop_tab(tab_by_id(tabid));
337 tab = tab_by_id(tabid);
338 tab->path = strdup(path);
340 ui_send_fs(IMSG_FILE_OPEN, tabid, path, strlen(path)+1);
344 handle_imsg_file_opened(struct imsg *imsg, size_t datalen)
351 tab = tab_by_id(imsg->hdr.peerid);
353 if (imsg->fd == -1) {
357 if (e[datalen-1] != '\0')
359 l = asprintf(&page, "# Can't open file\n\n> %s: %s\n",
363 load_page_from_str(tab, page);
367 ui_send_net(IMSG_PROCEED, tab->id, NULL, 0);
372 handle_imsg_buf(struct imsg *imsg, size_t datalen)
376 char *page, buf[FMT_SCALED_STRSIZE] = {0};
378 tab = tab_by_id(imsg->hdr.peerid);
380 tab->bytes += datalen;
382 if (!tab->buffer.page.parse(&tab->buffer.page,
383 imsg->data, datalen))
386 write(tab->fd, imsg->data, datalen);
387 fmt_scaled(tab->bytes, buf);
388 l = asprintf(&page, "Saving to \"%s\"... (%s)\n",
393 load_page_from_str(tab, page);
397 ui_on_tab_refresh(tab);
401 handle_imsg_eof(struct imsg *imsg, size_t datalen)
405 char *page, buf[FMT_SCALED_STRSIZE] = {0};
407 tab = tab_by_id(imsg->hdr.peerid);
410 if (!tab->buffer.page.free(&tab->buffer.page))
413 fmt_scaled(tab->bytes, buf);
414 l = asprintf(&page, "Saved to \"%s\" (%s)\n",
419 load_page_from_str(tab, page);
428 ui_on_tab_refresh(tab);
429 ui_on_tab_loaded(tab);
433 handle_imsg_bookmark_ok(struct imsg *imsg, size_t datalen)
437 if (datalen != sizeof(res))
440 memcpy(&res, imsg->data, sizeof(res));
442 message("Added to bookmarks!");
444 message("Failed to add to bookmarks: %s",
449 handle_imsg_save_cert_ok(struct imsg *imsg, size_t datalen)
453 if (datalen != sizeof(res))
455 memcpy(&res, imsg->data, datalen);
457 message("Failed to save the cert for: %s",
462 handle_imsg_update_cert_ok(struct imsg *imsg, size_t datalen)
466 if (datalen != sizeof(res))
468 memcpy(&res, imsg->data, datalen);
470 message("Failed to update the certificate");
474 handle_dispatch_imsg(int fd, short ev, void *d)
476 struct imsgev *iev = d;
477 dispatch_imsg(iev, ev, handlers, sizeof(handlers));
481 load_page_from_str(struct tab *tab, const char *page)
483 erase_buffer(&tab->buffer);
484 gemtext_initparser(&tab->buffer.page);
485 if (!tab->buffer.page.parse(&tab->buffer.page, page, strlen(page)))
487 if (!tab->buffer.page.free(&tab->buffer.page))
489 ui_on_tab_refresh(tab);
490 ui_on_tab_loaded(tab);
494 load_about_url(struct tab *tab, const char *url)
496 tab->trust = TS_VERIFIED;
498 gemtext_initparser(&tab->buffer.page);
500 ui_send_fs(IMSG_GET, tab->id,
501 tab->hist_cur->h, strlen(tab->hist_cur->h)+1);
505 load_gemini_url(struct tab *tab, const char *url)
510 tab->id = tab_new_id();
512 memset(&req, 0, sizeof(req));
513 strlcpy(req.host, tab->uri.host, sizeof(req.host));
514 strlcpy(req.port, tab->uri.port, sizeof(req.host));
516 strlcpy(req.req, tab->hist_cur->h, sizeof(req.req));
517 strlcat(req.req, "\r\n", sizeof(req.req));
519 req.proto = PROTO_GEMINI;
521 ui_send_net(IMSG_GET_RAW, tab->id,
526 load_via_proxy(struct tab *tab, const char *url, struct proxy *p)
531 tab->id = tab_new_id();
534 memset(&req, 0, sizeof(req));
535 strlcpy(req.host, p->host, sizeof(req.host));
536 strlcpy(req.port, p->port, sizeof(req.host));
538 strlcpy(req.req, tab->hist_cur->h, sizeof(req.req));
539 strlcat(req.req, "\r\n", sizeof(req.req));
541 req.proto = p->proto;
543 ui_send_net(IMSG_GET_RAW, tab->id,
548 do_load_url(struct tab *tab, const char *url)
564 tab->trust = TS_UNKNOWN;
566 memcpy(&uri, &tab->uri, sizeof(tab->uri));
567 if (!phos_resolve_uri_from_str(&uri, url, &tab->uri)) {
568 if (asprintf(&t, "#error loading %s\n>%s\n",
569 url, "Can't parse the URI") == -1)
571 strlcpy(tab->hist_cur->h, url, sizeof(tab->hist_cur->h));
572 load_page_from_str(tab, t);
577 phos_serialize_uri(&tab->uri, tab->hist_cur->h,
578 sizeof(tab->hist_cur->h));
580 for (p = protos; p->schema != NULL; ++p) {
581 if (!strcmp(tab->uri.scheme, p->schema)) {
587 TAILQ_FOREACH(proxy, &proxies, proxies) {
588 if (!strcmp(tab->uri.scheme, proxy->match_proto)) {
589 load_via_proxy(tab, url, proxy);
594 protos[0].loadfn(tab, url);
598 load_url(struct tab *tab, const char *url)
600 if (tab->hist_cur != NULL)
601 hist_clear_forward(&tab->hist, TAILQ_NEXT(tab->hist_cur, entries));
603 if ((tab->hist_cur = calloc(1, sizeof(*tab->hist_cur))) == NULL) {
608 hist_push(&tab->hist, tab->hist_cur);
609 do_load_url(tab, url);
610 erase_buffer(&tab->buffer);
614 load_previous_page(struct tab *tab)
618 if ((h = TAILQ_PREV(tab->hist_cur, mhisthead, entries)) == NULL)
621 do_load_url(tab, h->h);
626 load_next_page(struct tab *tab)
630 if ((h = TAILQ_NEXT(tab->hist_cur, entries)) == NULL)
633 do_load_url(tab, h->h);
638 stop_tab(struct tab *tab)
640 ui_send_net(IMSG_STOP, tab->id, NULL, 0);
647 load_page_from_str(tab, "Stopped.\n");
652 add_to_bookmarks(const char *str)
654 ui_send_fs(IMSG_BOOKMARK_PAGE, 0,
663 ui_send_fs(IMSG_SESSION_START, 0, NULL, 0);
665 TAILQ_FOREACH(tab, &tabshead, tabs) {
666 ui_send_fs(IMSG_SESSION_TAB, 0,
667 tab->hist_cur->h, strlen(tab->hist_cur->h)+1);
670 ui_send_fs(IMSG_SESSION_END, 0, NULL, 0);
674 session_new_tab_cb(const char *url)
680 start_child(enum telescope_process p, const char *argv0, int fd)
686 switch (pid = fork()) {
696 if (dup2(fd, 3) == -1)
697 err(1, "cannot setup imsg fd");
699 argv[argc++] = argv0;
702 errx(1, "Can't start ui process");
704 argv[argc++] = "-Tf";
707 argv[argc++] = "-Tn";
712 execvp(argv0, (char *const *)argv);
713 err(1, "execvp(%s)", argv0);
717 ui_send_net(int type, uint32_t peerid, const void *data,
720 return imsg_compose_event(iev_net, type, peerid, 0, -1, data,
725 ui_send_fs(int type, uint32_t peerid, const void *data, uint16_t datalen)
727 return imsg_compose_event(iev_fs, type, peerid, 0, -1, data,
731 static void __attribute__((noreturn))
734 fprintf(stderr, "USAGE: %s [-hn] [-c config] [url]\n", getprogname());
735 fprintf(stderr, "version: " PACKAGE " " VERSION "\n");
740 main(int argc, char * const *argv)
742 struct imsgev net_ibuf, fs_ibuf;
743 int pipe2net[2], pipe2fs[2];
744 int ch, configtest = 0, fail = 0;
748 const char *url = NEW_TAB_URL;
753 signal(SIGCHLD, SIG_IGN);
754 signal(SIGPIPE, SIG_IGN);
756 if (getenv("NO_COLOR") != NULL)
759 strlcpy(path, getenv("HOME"), sizeof(path));
760 strlcat(path, "/.telescope/config", sizeof(path));
762 while ((ch = getopt(argc, argv, "c:hnT:")) != -1) {
766 strlcpy(path, optarg, sizeof(path));
782 errx(1, "invalid process spec %c",
797 else if (proc == PROC_FS)
799 else if (proc == PROC_NET)
800 return client_main();
810 /* setup keys before reading the config */
811 TAILQ_INIT(&global_map.m);
812 global_map.unhandled_input = global_key_unbound;
813 TAILQ_INIT(&minibuffer_map.m);
816 parseconfig(path, fail);
822 /* Start children. */
823 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe2fs) == -1)
824 err(1, "socketpair");
825 start_child(PROC_FS, argv0, pipe2fs[1]);
826 imsg_init(&fs_ibuf.ibuf, pipe2fs[0]);
828 iev_fs->handler = handle_dispatch_imsg;
830 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe2net) == -1)
831 err(1, "socketpair");
832 start_child(PROC_NET, argv0, pipe2net[1]);
833 imsg_init(&net_ibuf.ibuf, pipe2net[0]);
835 iev_net->handler = handle_dispatch_imsg;
839 /* initialize tofu & load certificates */
841 tofu_init(&certs, 5, offsetof(struct tofu_entry, domain));
846 /* Setup event handlers for pipes to fs/net */
847 iev_fs->events = EV_READ;
848 event_set(&iev_fs->ev, iev_fs->ibuf.fd, iev_fs->events,
849 iev_fs->handler, iev_fs);
850 event_add(&iev_fs->ev, NULL);
852 iev_net->events = EV_READ;
853 event_set(&iev_net->ev, iev_net->ibuf.fd, iev_net->events,
854 iev_net->handler, iev_net);
855 event_add(&iev_net->ev, NULL);
858 load_last_session(session_new_tab_cb);
859 if (has_url || TAILQ_EMPTY(&tabshead))
862 sandbox_ui_process();
867 ui_send_fs(IMSG_QUIT, 0, NULL, 0);
868 ui_send_net(IMSG_QUIT, 0, NULL, 0);
869 imsg_flush(&iev_fs->ibuf);