3 #include <sys/socket.h>
13 struct event netev, fsev;
14 struct tabshead tabshead;
15 struct proxylist proxies;
17 /* the first is also the fallback one */
18 static struct proto protos[] = {
19 { "gemini", load_gemini_url },
20 { "about", load_about_url },
24 static struct imsgbuf *netibuf, *fsibuf;
26 static void die(void) __attribute__((__noreturn__));
27 static struct tab *tab_by_id(uint32_t);
28 static void handle_imsg_err(struct imsg*, size_t);
29 static void handle_imsg_check_cert(struct imsg*, size_t);
30 static void handle_check_cert_user_choice(int, struct tab *);
31 static void handle_maybe_save_new_cert(int, struct tab *);
32 static void handle_imsg_got_code(struct imsg*, size_t);
33 static void handle_imsg_got_meta(struct imsg*, size_t);
34 static void handle_maybe_save_page(int, struct tab *);
35 static void handle_save_page_path(const char *, unsigned int);
36 static void handle_imsg_file_opened(struct imsg*, size_t);
37 static void handle_imsg_buf(struct imsg*, size_t);
38 static void handle_imsg_eof(struct imsg*, size_t);
39 static void handle_imsg_bookmark_ok(struct imsg*, size_t);
40 static void handle_imsg_save_cert_ok(struct imsg*, size_t);
41 static void handle_imsg_update_cert_ok(struct imsg *, size_t);
42 static void handle_dispatch_imsg(int, short, void*);
43 static void load_page_from_str(struct tab*, const char*);
44 static void do_load_url(struct tab*, const char*);
46 static imsg_handlerfn *handlers[] = {
47 [IMSG_ERR] = handle_imsg_err,
48 [IMSG_CHECK_CERT] = handle_imsg_check_cert,
49 [IMSG_GOT_CODE] = handle_imsg_got_code,
50 [IMSG_GOT_META] = handle_imsg_got_meta,
51 [IMSG_BUF] = handle_imsg_buf,
52 [IMSG_EOF] = handle_imsg_eof,
53 [IMSG_BOOKMARK_OK] = handle_imsg_bookmark_ok,
54 [IMSG_SAVE_CERT_OK] = handle_imsg_save_cert_ok,
55 [IMSG_UPDATE_CERT_OK] = handle_imsg_update_cert_ok,
56 [IMSG_FILE_OPENED] = handle_imsg_file_opened,
59 static struct ohash certs;
61 static void __attribute__((__noreturn__))
68 tab_by_id(uint32_t id)
72 TAILQ_FOREACH(t, &tabshead, tabs) {
81 handle_imsg_err(struct imsg *imsg, size_t datalen)
86 tab = tab_by_id(imsg->hdr.peerid);
89 page[datalen-1] = '\0';
91 if (asprintf(&page, "# Error loading %s\n\n> %s\n",
92 tab->hist_cur->h, page) == -1)
94 load_page_from_str(tab, page);
99 handle_imsg_check_cert(struct imsg *imsg, size_t datalen)
101 const char *hash, *host, *port;
103 struct tofu_entry *e;
107 if (hash[datalen-1] != '\0')
110 tab = tab_by_id(imsg->hdr.peerid);
112 if (tab->proxy != NULL) {
113 host = tab->proxy->host;
114 port = tab->proxy->port;
116 host = tab->uri.host;
117 port = tab->uri.port;
120 if ((e = tofu_lookup(&certs, host, port)) == NULL) {
121 /* TODO: an update in libressl/libretls changed
122 * significantly. Find a better approach at storing
124 if (datalen > sizeof(e->hash))
127 tofu_res = 1; /* trust on first use */
128 if ((e = calloc(1, sizeof(*e))) == NULL)
130 strlcpy(e->domain, host, sizeof(e->domain));
131 if (*port != '\0' && strcmp(port, "1965")) {
132 strlcat(e->domain, ":", sizeof(e->domain));
133 strlcat(e->domain, port, sizeof(e->domain));
135 strlcpy(e->hash, hash, sizeof(e->hash));
137 imsg_compose(fsibuf, IMSG_SAVE_CERT, tab->id, 0, -1,
141 tofu_res = !strcmp(hash, e->hash);
144 if (e->verified == -1)
145 tab->trust = TS_TEMP_TRUSTED;
146 else if (e->verified == 1)
147 tab->trust = TS_VERIFIED;
149 tab->trust = TS_TRUSTED;
151 imsg_compose(netibuf, IMSG_CERT_STATUS, imsg->hdr.peerid, 0, -1,
152 &tofu_res, sizeof(tofu_res));
155 tab->trust = TS_UNTRUSTED;
156 load_page_from_str(tab, "# Certificate mismatch\n");
157 if ((tab->cert = strdup(hash)) == NULL)
159 ui_yornp("Certificate mismatch. Proceed?",
160 handle_check_cert_user_choice, tab);
165 handle_check_cert_user_choice(int accept, struct tab *tab)
167 imsg_compose(netibuf, IMSG_CERT_STATUS, tab->id, 0, -1,
168 &accept, sizeof(accept));
173 * trust the certificate for this session only. If
174 * the page results in a redirect while we're asking
175 * the user to save, we'll end up with an invalid
176 * tabid (one request == one tab id) and crash. It
177 * also makes sense to save it for the current session
178 * if the user accepted it.
180 tofu_temp_trust(&certs, tab->uri.host, tab->uri.port, tab->cert);
182 ui_yornp("Save the new certificate?",
183 handle_maybe_save_new_cert, tab);
191 handle_maybe_save_new_cert(int accept, struct tab *tab)
193 struct tofu_entry *e;
194 const char *host, *port;
196 if (tab->proxy != NULL) {
197 host = tab->proxy->host;
198 port = tab->proxy->port;
200 host = tab->uri.host;
201 port = tab->uri.port;
207 if ((e = calloc(1, sizeof(*e))) == NULL)
210 strlcpy(e->domain, host, sizeof(e->domain));
211 if (*port != '\0' && strcmp(port, "1965")) {
212 strlcat(e->domain, ":", sizeof(e->domain));
213 strlcat(e->domain, port, sizeof(e->domain));
215 strlcpy(e->hash, tab->cert, sizeof(e->hash));
216 imsg_compose(fsibuf, IMSG_UPDATE_CERT, 0, 0, -1, e, sizeof(*e));
219 tofu_update(&certs, e);
221 tab->trust = TS_TRUSTED;
229 normalize_code(int n)
232 if (n == 10 || n == 11)
238 if (n == 30 || n == 31)
246 if (n <= 53 || n == 59)
254 return MALFORMED_RESPONSE;
258 handle_imsg_got_code(struct imsg *imsg, size_t datalen)
262 tab = tab_by_id(imsg->hdr.peerid);
264 if (sizeof(tab->code) != datalen)
267 memcpy(&tab->code, imsg->data, sizeof(tab->code));
268 tab->code = normalize_code(tab->code);
269 if (tab->code != 30 && tab->code != 31)
270 tab->redirect_count = 0;
274 handle_imsg_got_meta(struct imsg *imsg, size_t datalen)
278 tab = tab_by_id(imsg->hdr.peerid);
280 if (sizeof(tab->meta) <= datalen)
283 memcpy(tab->meta, imsg->data, datalen);
285 if (tab->code < 10) { /* internal errors */
286 load_page_from_str(tab, err_pages[tab->code]);
287 } else if (tab->code < 20) { /* 1x */
288 load_page_from_str(tab, err_pages[tab->code]);
289 ui_require_input(tab, tab->code == 11);
290 } else if (tab->code == 20) {
291 if (setup_parser_for(tab)) {
292 imsg_compose(netibuf, IMSG_PROCEED, tab->id, 0, -1, NULL, 0);
295 load_page_from_str(tab, err_pages[UNKNOWN_TYPE_OR_CSET]);
296 ui_yornp("Can't display page, wanna save?",
297 handle_maybe_save_page, tab);
299 } else if (tab->code < 40) { /* 3x */
300 tab->redirect_count++;
302 /* TODO: make customizable? */
303 if (tab->redirect_count > 5) {
304 load_page_from_str(tab,
305 err_pages[TOO_MUCH_REDIRECTS]);
307 do_load_url(tab, tab->meta);
308 } else { /* 4x, 5x & 6x */
309 load_page_from_str(tab, err_pages[tab->code]);
314 handle_maybe_save_page(int dosave, struct tab *tab)
317 ui_read("Save to path", handle_save_page_path, tab->id);
323 handle_save_page_path(const char *path, unsigned int tabid)
328 stop_tab(tab_by_id(tabid));
332 tab = tab_by_id(tabid);
333 tab->path = strdup(path);
335 imsg_compose(fsibuf, IMSG_FILE_OPEN, tabid, 0, -1, path, strlen(path)+1);
340 handle_imsg_file_opened(struct imsg *imsg, size_t datalen)
347 tab = tab_by_id(imsg->hdr.peerid);
349 if (imsg->fd == -1) {
353 if (e[datalen-1] != '\0')
355 l = asprintf(&page, "# Can't open file\n\n> %s: %s\n",
359 load_page_from_str(tab, page);
363 imsg_compose(netibuf, IMSG_PROCEED, tab->id, 0, -1, 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 imsgbuf *ibuf = d;
474 dispatch_imsg(ibuf, 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 imsg_compose(fsibuf, IMSG_GET, tab->id, 0, -1,
498 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 imsg_compose(netibuf, IMSG_GET_RAW, tab->id, 0, -1,
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 imsg_compose(netibuf, IMSG_GET_RAW, tab->id, 0, -1,
548 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) {
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 imsg_compose(netibuf, IMSG_STOP, tab->id, 0, -1, NULL, 0);
648 load_page_from_str(tab, "Stopped.\n");
653 add_to_bookmarks(const char *str)
655 imsg_compose(fsibuf, IMSG_BOOKMARK_PAGE, 0, 0, -1, str, strlen(str)+1);
664 imsg_compose(fsibuf, IMSG_SESSION_START, 0, 0, -1, NULL, 0);
667 TAILQ_FOREACH(tab, &tabshead, tabs) {
668 imsg_compose(fsibuf, IMSG_SESSION_TAB, 0, 0, -1,
669 tab->hist_cur->h, strlen(tab->hist_cur->h)+1);
673 imsg_compose(fsibuf, IMSG_SESSION_END, 0, 0, -1, NULL, 0);
678 session_new_tab_cb(const char *url)
686 fprintf(stderr, "USAGE: %s [-hn] [-c config] [url]\n", getprogname());
687 fprintf(stderr, "version: " PACKAGE " " VERSION "\n");
691 main(int argc, char * const *argv)
693 struct imsgbuf network_ibuf, fs_ibuf;
694 int net_fds[2], fs_fds[2];
695 int ch, configtest = 0, fail = 0;
698 char *url = NEW_TAB_URL;
700 signal(SIGCHLD, SIG_IGN);
701 signal(SIGPIPE, SIG_IGN);
703 if (getenv("NO_COLOR") != NULL)
706 strlcpy(path, getenv("HOME"), sizeof(path));
707 strlcat(path, "/.telescope/config", sizeof(path));
709 while ((ch = getopt(argc, argv, "c:hn")) != -1) {
713 strlcpy(path, optarg, sizeof(path));
735 /* initialize part of the fs layer. Before starting the UI
736 * and dropping the priviledges we need to read some stuff. */
739 /* setup keys before reading the config */
740 TAILQ_INIT(&global_map.m);
741 global_map.unhandled_input = global_key_unbound;
742 TAILQ_INIT(&minibuffer_map.m);
745 parseconfig(path, fail);
751 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, fs_fds) == -1)
752 err(1, "socketpair");
761 imsg_init(&fs_ibuf, fs_fds[1]);
762 exit(fs_main(&fs_ibuf));
765 imsg_init(&fs_ibuf, fs_fds[0]);
769 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, net_fds) == -1)
770 err(1, "socketpair");
777 setproctitle("client");
780 imsg_init(&network_ibuf, net_fds[1]);
781 exit(client_main(&network_ibuf));
784 imsg_init(&network_ibuf, net_fds[0]);
785 netibuf = &network_ibuf;
790 tofu_init(&certs, 5, offsetof(struct tofu_entry, domain));
793 TAILQ_INIT(&tabshead);
794 TAILQ_INIT(&proxies);
798 event_set(&netev, netibuf->fd, EV_READ | EV_PERSIST,
799 handle_dispatch_imsg, netibuf);
800 event_add(&netev, NULL);
802 event_set(&fsev, fsibuf->fd, EV_READ | EV_PERSIST,
803 handle_dispatch_imsg, fsibuf);
804 event_add(&fsev, NULL);
807 load_last_session(session_new_tab_cb);
808 if (has_url || TAILQ_EMPTY(&tabshead))
811 sandbox_ui_process();
816 imsg_compose(netibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
819 imsg_compose(fsibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);