1 #include <sys/socket.h>
12 #include "telescope.h"
15 static struct imsgev *iev_fs, *iev_net;
17 struct tabshead tabshead = TAILQ_HEAD_INITIALIZER(tabshead);
18 struct proxylist proxies = TAILQ_HEAD_INITIALIZER(proxies);
20 enum telescope_process {
26 /* the first is also the fallback one */
27 static struct proto protos[] = {
28 { "gemini", load_gemini_url },
29 { "about", load_about_url },
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);
53 static int ui_send_net(int, uint32_t, const void *, uint16_t);
54 static int ui_send_fs(int, uint32_t, const void *, uint16_t);
56 static imsg_handlerfn *handlers[] = {
57 [IMSG_ERR] = handle_imsg_err,
58 [IMSG_CHECK_CERT] = handle_imsg_check_cert,
59 [IMSG_GOT_CODE] = handle_imsg_got_code,
60 [IMSG_GOT_META] = handle_imsg_got_meta,
61 [IMSG_BUF] = handle_imsg_buf,
62 [IMSG_EOF] = handle_imsg_eof,
63 [IMSG_BOOKMARK_OK] = handle_imsg_bookmark_ok,
64 [IMSG_SAVE_CERT_OK] = handle_imsg_save_cert_ok,
65 [IMSG_UPDATE_CERT_OK] = handle_imsg_update_cert_ok,
66 [IMSG_FILE_OPENED] = handle_imsg_file_opened,
69 static struct ohash certs;
71 static void __attribute__((__noreturn__))
78 tab_by_id(uint32_t id)
82 TAILQ_FOREACH(t, &tabshead, tabs) {
91 handle_imsg_err(struct imsg *imsg, size_t datalen)
96 tab = tab_by_id(imsg->hdr.peerid);
99 page[datalen-1] = '\0';
101 if (asprintf(&page, "# Error loading %s\n\n> %s\n",
102 tab->hist_cur->h, page) == -1)
104 load_page_from_str(tab, page);
109 handle_imsg_check_cert(struct imsg *imsg, size_t datalen)
111 const char *hash, *host, *port;
113 struct tofu_entry *e;
117 if (hash[datalen-1] != '\0')
120 tab = tab_by_id(imsg->hdr.peerid);
122 if (tab->proxy != NULL) {
123 host = tab->proxy->host;
124 port = tab->proxy->port;
126 host = tab->uri.host;
127 port = tab->uri.port;
130 if ((e = tofu_lookup(&certs, host, port)) == NULL) {
131 /* TODO: an update in libressl/libretls changed
132 * significantly. Find a better approach at storing
134 if (datalen > sizeof(e->hash))
137 tofu_res = 1; /* trust on first use */
138 if ((e = calloc(1, sizeof(*e))) == NULL)
140 strlcpy(e->domain, host, sizeof(e->domain));
141 if (*port != '\0' && strcmp(port, "1965")) {
142 strlcat(e->domain, ":", sizeof(e->domain));
143 strlcat(e->domain, port, sizeof(e->domain));
145 strlcpy(e->hash, hash, sizeof(e->hash));
147 ui_send_fs(IMSG_SAVE_CERT, tab->id, e, sizeof(*e));
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 ui_send_net(IMSG_CERT_STATUS, imsg->hdr.peerid,
160 &tofu_res, sizeof(tofu_res));
162 tab->trust = TS_UNTRUSTED;
163 load_page_from_str(tab, "# Certificate mismatch\n");
164 if ((tab->cert = strdup(hash)) == NULL)
166 ui_yornp("Certificate mismatch. Proceed?",
167 handle_check_cert_user_choice, tab);
172 handle_check_cert_user_choice(int accept, struct tab *tab)
174 ui_send_net(IMSG_CERT_STATUS, tab->id, &accept,
179 * trust the certificate for this session only. If
180 * the page results in a redirect while we're asking
181 * the user to save, we'll end up with an invalid
182 * tabid (one request == one tab id) and crash. It
183 * also makes sense to save it for the current session
184 * if the user accepted it.
186 tofu_temp_trust(&certs, tab->uri.host, tab->uri.port, tab->cert);
188 ui_yornp("Save the new certificate?",
189 handle_maybe_save_new_cert, tab);
197 handle_maybe_save_new_cert(int accept, struct tab *tab)
199 struct tofu_entry *e;
200 const char *host, *port;
202 if (tab->proxy != NULL) {
203 host = tab->proxy->host;
204 port = tab->proxy->port;
206 host = tab->uri.host;
207 port = tab->uri.port;
213 if ((e = calloc(1, sizeof(*e))) == NULL)
216 strlcpy(e->domain, host, sizeof(e->domain));
217 if (*port != '\0' && strcmp(port, "1965")) {
218 strlcat(e->domain, ":", sizeof(e->domain));
219 strlcat(e->domain, port, sizeof(e->domain));
221 strlcpy(e->hash, tab->cert, sizeof(e->hash));
222 ui_send_fs(IMSG_UPDATE_CERT, 0, e, sizeof(*e));
224 tofu_update(&certs, e);
226 tab->trust = TS_TRUSTED;
234 normalize_code(int n)
237 if (n == 10 || n == 11)
243 if (n == 30 || n == 31)
251 if (n <= 53 || n == 59)
259 return MALFORMED_RESPONSE;
263 handle_imsg_got_code(struct imsg *imsg, size_t datalen)
267 tab = tab_by_id(imsg->hdr.peerid);
269 if (sizeof(tab->code) != datalen)
272 memcpy(&tab->code, imsg->data, sizeof(tab->code));
273 tab->code = normalize_code(tab->code);
274 if (tab->code != 30 && tab->code != 31)
275 tab->redirect_count = 0;
279 handle_imsg_got_meta(struct imsg *imsg, size_t datalen)
283 tab = tab_by_id(imsg->hdr.peerid);
285 if (sizeof(tab->meta) <= datalen)
288 memcpy(tab->meta, imsg->data, datalen);
290 if (tab->code < 10) { /* internal errors */
291 load_page_from_str(tab, err_pages[tab->code]);
292 } else if (tab->code < 20) { /* 1x */
293 load_page_from_str(tab, err_pages[tab->code]);
294 ui_require_input(tab, tab->code == 11);
295 } else if (tab->code == 20) {
296 if (setup_parser_for(tab)) {
297 ui_send_net(IMSG_PROCEED, tab->id, NULL, 0);
299 load_page_from_str(tab, err_pages[UNKNOWN_TYPE_OR_CSET]);
300 ui_yornp("Can't display page, wanna save?",
301 handle_maybe_save_page, tab);
303 } else if (tab->code < 40) { /* 3x */
304 tab->redirect_count++;
306 /* TODO: make customizable? */
307 if (tab->redirect_count > 5) {
308 load_page_from_str(tab,
309 err_pages[TOO_MUCH_REDIRECTS]);
311 do_load_url(tab, tab->meta);
312 } else { /* 4x, 5x & 6x */
313 load_page_from_str(tab, err_pages[tab->code]);
318 handle_maybe_save_page(int dosave, struct tab *tab)
321 ui_read("Save to path", handle_save_page_path, tab->id);
327 handle_save_page_path(const char *path, unsigned int tabid)
332 stop_tab(tab_by_id(tabid));
336 tab = tab_by_id(tabid);
337 tab->path = strdup(path);
339 ui_send_fs(IMSG_FILE_OPEN, tabid, path, strlen(path)+1);
343 handle_imsg_file_opened(struct imsg *imsg, size_t datalen)
350 tab = tab_by_id(imsg->hdr.peerid);
352 if (imsg->fd == -1) {
356 if (e[datalen-1] != '\0')
358 l = asprintf(&page, "# Can't open file\n\n> %s: %s\n",
362 load_page_from_str(tab, page);
366 ui_send_net(IMSG_PROCEED, tab->id, NULL, 0);
371 handle_imsg_buf(struct imsg *imsg, size_t datalen)
375 char *page, buf[FMT_SCALED_STRSIZE] = {0};
377 tab = tab_by_id(imsg->hdr.peerid);
379 tab->bytes += datalen;
381 if (!tab->buffer.page.parse(&tab->buffer.page,
382 imsg->data, datalen))
385 write(tab->fd, imsg->data, datalen);
386 fmt_scaled(tab->bytes, buf);
387 l = asprintf(&page, "Saving to \"%s\"... (%s)\n",
392 load_page_from_str(tab, page);
396 ui_on_tab_refresh(tab);
400 handle_imsg_eof(struct imsg *imsg, size_t datalen)
404 char *page, buf[FMT_SCALED_STRSIZE] = {0};
406 tab = tab_by_id(imsg->hdr.peerid);
409 if (!tab->buffer.page.free(&tab->buffer.page))
412 fmt_scaled(tab->bytes, buf);
413 l = asprintf(&page, "Saved to \"%s\" (%s)\n",
418 load_page_from_str(tab, page);
427 ui_on_tab_refresh(tab);
428 ui_on_tab_loaded(tab);
432 handle_imsg_bookmark_ok(struct imsg *imsg, size_t datalen)
436 if (datalen != sizeof(res))
439 memcpy(&res, imsg->data, sizeof(res));
441 message("Added to bookmarks!");
443 message("Failed to add to bookmarks: %s",
448 handle_imsg_save_cert_ok(struct imsg *imsg, size_t datalen)
452 if (datalen != sizeof(res))
454 memcpy(&res, imsg->data, datalen);
456 message("Failed to save the cert for: %s",
461 handle_imsg_update_cert_ok(struct imsg *imsg, size_t datalen)
465 if (datalen != sizeof(res))
467 memcpy(&res, imsg->data, datalen);
469 message("Failed to update the certificate");
473 handle_dispatch_imsg(int fd, short ev, void *d)
475 struct imsgev *iev = d;
476 dispatch_imsg(iev, ev, handlers, sizeof(handlers));
480 load_page_from_str(struct tab *tab, const char *page)
482 erase_buffer(&tab->buffer);
483 gemtext_initparser(&tab->buffer.page);
484 if (!tab->buffer.page.parse(&tab->buffer.page, page, strlen(page)))
486 if (!tab->buffer.page.free(&tab->buffer.page))
488 ui_on_tab_refresh(tab);
489 ui_on_tab_loaded(tab);
493 load_about_url(struct tab *tab, const char *url)
495 tab->trust = TS_VERIFIED;
497 gemtext_initparser(&tab->buffer.page);
499 ui_send_fs(IMSG_GET, tab->id,
500 tab->hist_cur->h, strlen(tab->hist_cur->h)+1);
504 load_gemini_url(struct tab *tab, const char *url)
509 tab->id = tab_new_id();
511 memset(&req, 0, sizeof(req));
512 strlcpy(req.host, tab->uri.host, sizeof(req.host));
513 strlcpy(req.port, tab->uri.port, sizeof(req.host));
515 strlcpy(req.req, tab->hist_cur->h, sizeof(req.req));
516 strlcat(req.req, "\r\n", sizeof(req.req));
518 req.proto = PROTO_GEMINI;
520 ui_send_net(IMSG_GET_RAW, tab->id,
525 load_via_proxy(struct tab *tab, const char *url, struct proxy *p)
530 tab->id = tab_new_id();
533 memset(&req, 0, sizeof(req));
534 strlcpy(req.host, p->host, sizeof(req.host));
535 strlcpy(req.port, p->port, sizeof(req.host));
537 strlcpy(req.req, tab->hist_cur->h, sizeof(req.req));
538 strlcat(req.req, "\r\n", sizeof(req.req));
540 req.proto = p->proto;
542 ui_send_net(IMSG_GET_RAW, tab->id,
547 do_load_url(struct tab *tab, const char *url)
563 tab->trust = TS_UNKNOWN;
565 memcpy(&uri, &tab->uri, sizeof(tab->uri));
566 if (!phos_resolve_uri_from_str(&uri, url, &tab->uri)) {
567 if (asprintf(&t, "#error loading %s\n>%s\n",
568 url, "Can't parse the URI") == -1)
570 strlcpy(tab->hist_cur->h, url, sizeof(tab->hist_cur->h));
571 load_page_from_str(tab, t);
576 phos_serialize_uri(&tab->uri, tab->hist_cur->h,
577 sizeof(tab->hist_cur->h));
579 for (p = protos; p->schema != NULL; ++p) {
580 if (!strcmp(tab->uri.scheme, p->schema)) {
586 TAILQ_FOREACH(proxy, &proxies, proxies) {
587 if (!strcmp(tab->uri.scheme, proxy->match_proto)) {
588 load_via_proxy(tab, url, proxy);
593 protos[0].loadfn(tab, url);
597 load_url(struct tab *tab, const char *url)
599 if (tab->hist_cur != NULL)
600 hist_clear_forward(&tab->hist, TAILQ_NEXT(tab->hist_cur, entries));
602 if ((tab->hist_cur = calloc(1, sizeof(*tab->hist_cur))) == NULL) {
607 hist_push(&tab->hist, tab->hist_cur);
608 do_load_url(tab, url);
609 erase_buffer(&tab->buffer);
613 load_previous_page(struct tab *tab)
617 if ((h = TAILQ_PREV(tab->hist_cur, mhisthead, entries)) == NULL)
620 do_load_url(tab, h->h);
625 load_next_page(struct tab *tab)
629 if ((h = TAILQ_NEXT(tab->hist_cur, entries)) == NULL)
632 do_load_url(tab, h->h);
637 stop_tab(struct tab *tab)
639 ui_send_net(IMSG_STOP, tab->id, NULL, 0);
646 load_page_from_str(tab, "Stopped.\n");
651 add_to_bookmarks(const char *str)
653 ui_send_fs(IMSG_BOOKMARK_PAGE, 0,
662 ui_send_fs(IMSG_SESSION_START, 0, NULL, 0);
664 TAILQ_FOREACH(tab, &tabshead, tabs) {
665 ui_send_fs(IMSG_SESSION_TAB, 0,
666 tab->hist_cur->h, strlen(tab->hist_cur->h)+1);
669 ui_send_fs(IMSG_SESSION_END, 0, NULL, 0);
673 session_new_tab_cb(const char *url)
679 start_child(enum telescope_process p, const char *argv0, int fd)
685 switch (pid = fork()) {
695 if (dup2(fd, 3) == -1)
696 err(1, "cannot setup imsg fd");
698 argv[argc++] = argv0;
701 errx(1, "Can't start ui process");
703 argv[argc++] = "-Tf";
706 argv[argc++] = "-Tn";
711 execvp(argv0, (char *const *)argv);
712 err(1, "execvp(%s)", argv0);
716 ui_send_net(int type, uint32_t peerid, const void *data,
719 return imsg_compose_event(iev_net, type, peerid, 0, -1, data,
724 ui_send_fs(int type, uint32_t peerid, const void *data, uint16_t datalen)
726 return imsg_compose_event(iev_fs, type, peerid, 0, -1, data,
730 static void __attribute__((noreturn))
733 fprintf(stderr, "USAGE: %s [-hn] [-c config] [url]\n", getprogname());
734 fprintf(stderr, "version: " PACKAGE " " VERSION "\n");
739 main(int argc, char * const *argv)
741 struct imsgev net_ibuf, fs_ibuf;
742 int pipe2net[2], pipe2fs[2];
743 int ch, configtest = 0, fail = 0;
747 const char *url = NEW_TAB_URL;
752 signal(SIGCHLD, SIG_IGN);
753 signal(SIGPIPE, SIG_IGN);
755 if (getenv("NO_COLOR") != NULL)
758 strlcpy(path, getenv("HOME"), sizeof(path));
759 strlcat(path, "/.telescope/config", sizeof(path));
761 while ((ch = getopt(argc, argv, "c:hnT:")) != -1) {
765 strlcpy(path, optarg, sizeof(path));
781 errx(1, "invalid process spec %c",
796 else if (proc == PROC_FS)
798 else if (proc == PROC_NET)
799 return client_main();
809 /* setup keys before reading the config */
810 TAILQ_INIT(&global_map.m);
811 global_map.unhandled_input = global_key_unbound;
812 TAILQ_INIT(&minibuffer_map.m);
815 parseconfig(path, fail);
821 /* Start children. */
822 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe2fs) == -1)
823 err(1, "socketpair");
824 start_child(PROC_FS, argv0, pipe2fs[1]);
825 imsg_init(&fs_ibuf.ibuf, pipe2fs[0]);
827 iev_fs->handler = handle_dispatch_imsg;
829 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe2net) == -1)
830 err(1, "socketpair");
831 start_child(PROC_NET, argv0, pipe2net[1]);
832 imsg_init(&net_ibuf.ibuf, pipe2net[0]);
834 iev_net->handler = handle_dispatch_imsg;
838 /* initialize tofu & load certificates */
840 tofu_init(&certs, 5, offsetof(struct tofu_entry, domain));
845 /* Setup event handlers for pipes to fs/net */
846 iev_fs->events = EV_READ;
847 event_set(&iev_fs->ev, iev_fs->ibuf.fd, iev_fs->events,
848 iev_fs->handler, iev_fs);
849 event_add(&iev_fs->ev, NULL);
851 iev_net->events = EV_READ;
852 event_set(&iev_net->ev, iev_net->ibuf.fd, iev_net->events,
853 iev_net->handler, iev_net);
854 event_add(&iev_net->ev, NULL);
857 load_last_session(session_new_tab_cb);
858 if (has_url || TAILQ_EMPTY(&tabshead))
861 sandbox_ui_process();
866 ui_send_fs(IMSG_QUIT, 0, NULL, 0);
867 ui_send_net(IMSG_QUIT, 0, NULL, 0);