3 #include <sys/socket.h>
12 struct event netev, fsev;
13 struct tabshead tabshead;
14 struct proxylist proxies;
16 /* the first is also the fallback one */
17 static struct proto protos[] = {
18 { "gemini", load_gemini_url },
19 { "about", load_about_url },
23 static struct imsgbuf *netibuf, *fsibuf;
25 static void die(void) __attribute__((__noreturn__));
26 static struct tab *tab_by_id(uint32_t);
27 static void handle_imsg_err(struct imsg*, size_t);
28 static void handle_imsg_check_cert(struct imsg*, size_t);
29 static void handle_check_cert_user_choice(int, struct tab *);
30 static void handle_maybe_save_new_cert(int, struct tab *);
31 static void handle_imsg_got_code(struct imsg*, size_t);
32 static void handle_imsg_got_meta(struct imsg*, size_t);
33 static void handle_maybe_save_page(int, struct tab *);
34 static void handle_save_page_path(const char *, unsigned int);
35 static void handle_imsg_file_opened(struct imsg*, size_t);
36 static void handle_imsg_buf(struct imsg*, size_t);
37 static void handle_imsg_eof(struct imsg*, size_t);
38 static void handle_imsg_bookmark_ok(struct imsg*, size_t);
39 static void handle_imsg_save_cert_ok(struct imsg*, size_t);
40 static void handle_imsg_update_cert_ok(struct imsg *, size_t);
41 static void handle_dispatch_imsg(int, short, void*);
42 static void load_page_from_str(struct tab*, const char*);
43 static void do_load_url(struct tab*, const char*);
45 static imsg_handlerfn *handlers[] = {
46 [IMSG_ERR] = handle_imsg_err,
47 [IMSG_CHECK_CERT] = handle_imsg_check_cert,
48 [IMSG_GOT_CODE] = handle_imsg_got_code,
49 [IMSG_GOT_META] = handle_imsg_got_meta,
50 [IMSG_BUF] = handle_imsg_buf,
51 [IMSG_EOF] = handle_imsg_eof,
52 [IMSG_BOOKMARK_OK] = handle_imsg_bookmark_ok,
53 [IMSG_SAVE_CERT_OK] = handle_imsg_save_cert_ok,
54 [IMSG_UPDATE_CERT_OK] = handle_imsg_update_cert_ok,
55 [IMSG_FILE_OPENED] = handle_imsg_file_opened,
58 static struct ohash certs;
60 static void __attribute__((__noreturn__))
67 tab_by_id(uint32_t id)
71 TAILQ_FOREACH(t, &tabshead, tabs) {
80 handle_imsg_err(struct imsg *imsg, size_t datalen)
85 tab = tab_by_id(imsg->hdr.peerid);
88 page[datalen-1] = '\0';
90 if (asprintf(&page, "# Error loading %s\n\n> %s\n",
91 tab->hist_cur->h, page) == -1)
93 load_page_from_str(tab, page);
98 handle_imsg_check_cert(struct imsg *imsg, size_t datalen)
100 const char *hash, *host, *port;
102 struct tofu_entry *e;
106 if (hash[datalen-1] != '\0')
109 tab = tab_by_id(imsg->hdr.peerid);
111 if (tab->proxy != NULL) {
112 host = tab->proxy->host;
113 port = tab->proxy->port;
115 host = tab->uri.host;
116 port = tab->uri.port;
119 if ((e = tofu_lookup(&certs, host, port)) == NULL) {
120 /* TODO: an update in libressl/libretls changed
121 * significantly. Find a better approach at storing
123 if (datalen > sizeof(e->hash))
126 tofu_res = 1; /* trust on first use */
127 if ((e = calloc(1, sizeof(*e))) == NULL)
129 strlcpy(e->domain, host, sizeof(e->domain));
130 if (*port != '\0' && strcmp(port, "1965")) {
131 strlcat(e->domain, ":", sizeof(e->domain));
132 strlcat(e->domain, port, sizeof(e->domain));
134 strlcpy(e->hash, hash, sizeof(e->hash));
136 imsg_compose(fsibuf, IMSG_SAVE_CERT, tab->id, 0, -1,
140 tofu_res = !strcmp(hash, e->hash);
143 if (e->verified == -1)
144 tab->trust = TS_TEMP_TRUSTED;
145 else if (e->verified == 1)
146 tab->trust = TS_VERIFIED;
148 tab->trust = TS_TRUSTED;
150 imsg_compose(netibuf, IMSG_CERT_STATUS, imsg->hdr.peerid, 0, -1,
151 &tofu_res, sizeof(tofu_res));
154 tab->trust = TS_UNTRUSTED;
155 load_page_from_str(tab, "# Certificate mismatch\n");
156 if ((tab->cert = strdup(hash)) == NULL)
158 ui_yornp("Certificate mismatch. Proceed?",
159 handle_check_cert_user_choice, tab);
164 handle_check_cert_user_choice(int accept, struct tab *tab)
166 imsg_compose(netibuf, IMSG_CERT_STATUS, tab->id, 0, -1,
167 &accept, sizeof(accept));
172 * trust the certificate for this session only. If
173 * the page results in a redirect while we're asking
174 * the user to save, we'll end up with an invalid
175 * tabid (one request == one tab id) and crash. It
176 * also makes sense to save it for the current session
177 * if the user accepted it.
179 tofu_temp_trust(&certs, tab->uri.host, tab->uri.port, tab->cert);
181 ui_yornp("Save the new certificate?",
182 handle_maybe_save_new_cert, tab);
190 handle_maybe_save_new_cert(int accept, struct tab *tab)
192 struct tofu_entry *e;
193 const char *host, *port;
195 if (tab->proxy != NULL) {
196 host = tab->proxy->host;
197 port = tab->proxy->port;
199 host = tab->uri.host;
200 port = tab->uri.port;
206 if ((e = calloc(1, sizeof(*e))) == NULL)
209 strlcpy(e->domain, host, sizeof(e->domain));
210 if (*port != '\0' && strcmp(port, "1965")) {
211 strlcat(e->domain, ":", sizeof(e->domain));
212 strlcat(e->domain, port, sizeof(e->domain));
214 strlcpy(e->hash, tab->cert, sizeof(e->hash));
215 imsg_compose(fsibuf, IMSG_UPDATE_CERT, 0, 0, -1, e, sizeof(*e));
218 tofu_update(&certs, e);
220 tab->trust = TS_TRUSTED;
228 normalize_code(int n)
231 if (n == 10 || n == 11)
237 if (n == 30 || n == 31)
245 if (n <= 53 || n == 59)
253 return MALFORMED_RESPONSE;
257 handle_imsg_got_code(struct imsg *imsg, size_t datalen)
261 tab = tab_by_id(imsg->hdr.peerid);
263 if (sizeof(tab->code) != datalen)
266 memcpy(&tab->code, imsg->data, sizeof(tab->code));
267 tab->code = normalize_code(tab->code);
268 if (tab->code != 30 && tab->code != 31)
269 tab->redirect_count = 0;
273 handle_imsg_got_meta(struct imsg *imsg, size_t datalen)
277 tab = tab_by_id(imsg->hdr.peerid);
279 if (sizeof(tab->meta) <= datalen)
282 memcpy(tab->meta, imsg->data, datalen);
284 if (tab->code < 10) { /* internal errors */
285 load_page_from_str(tab, err_pages[tab->code]);
286 } else if (tab->code < 20) { /* 1x */
287 load_page_from_str(tab, err_pages[tab->code]);
288 ui_require_input(tab, tab->code == 11);
289 } else if (tab->code == 20) {
290 if (setup_parser_for(tab)) {
291 imsg_compose(netibuf, IMSG_PROCEED, tab->id, 0, -1, NULL, 0);
294 load_page_from_str(tab, err_pages[UNKNOWN_TYPE_OR_CSET]);
295 ui_yornp("Can't display page, wanna save?",
296 handle_maybe_save_page, tab);
298 } else if (tab->code < 40) { /* 3x */
299 tab->redirect_count++;
301 /* TODO: make customizable? */
302 if (tab->redirect_count > 5) {
303 load_page_from_str(tab,
304 err_pages[TOO_MUCH_REDIRECTS]);
306 do_load_url(tab, tab->meta);
307 } else { /* 4x, 5x & 6x */
308 load_page_from_str(tab, err_pages[tab->code]);
313 handle_maybe_save_page(int dosave, struct tab *tab)
316 ui_read("Save to path", handle_save_page_path, tab->id);
322 handle_save_page_path(const char *path, unsigned int tabid)
327 stop_tab(tab_by_id(tabid));
331 tab = tab_by_id(tabid);
332 tab->path = strdup(path);
334 imsg_compose(fsibuf, IMSG_FILE_OPEN, tabid, 0, -1, path, strlen(path)+1);
339 handle_imsg_file_opened(struct imsg *imsg, size_t datalen)
346 tab = tab_by_id(imsg->hdr.peerid);
348 if (imsg->fd == -1) {
352 if (e[datalen-1] != '\0')
354 l = asprintf(&page, "# Can't open file\n\n> %s: %s\n",
358 load_page_from_str(tab, page);
362 imsg_compose(netibuf, IMSG_PROCEED, tab->id, 0, -1, NULL, 0);
368 handle_imsg_buf(struct imsg *imsg, size_t datalen)
372 char *page, buf[FMT_SCALED_STRSIZE] = {0};
374 tab = tab_by_id(imsg->hdr.peerid);
376 tab->bytes += datalen;
378 if (!tab->buffer.page.parse(&tab->buffer.page,
379 imsg->data, datalen))
382 write(tab->fd, imsg->data, datalen);
383 fmt_scaled(tab->bytes, buf);
384 l = asprintf(&page, "Saving to \"%s\"... (%s)\n",
389 load_page_from_str(tab, page);
393 ui_on_tab_refresh(tab);
397 handle_imsg_eof(struct imsg *imsg, size_t datalen)
401 char *page, buf[FMT_SCALED_STRSIZE] = {0};
403 tab = tab_by_id(imsg->hdr.peerid);
406 if (!tab->buffer.page.free(&tab->buffer.page))
409 fmt_scaled(tab->bytes, buf);
410 l = asprintf(&page, "Saved to \"%s\" (%s)\n",
415 load_page_from_str(tab, page);
424 ui_on_tab_refresh(tab);
425 ui_on_tab_loaded(tab);
429 handle_imsg_bookmark_ok(struct imsg *imsg, size_t datalen)
433 if (datalen != sizeof(res))
436 memcpy(&res, imsg->data, sizeof(res));
438 message("Added to bookmarks!");
440 message("Failed to add to bookmarks: %s",
445 handle_imsg_save_cert_ok(struct imsg *imsg, size_t datalen)
449 if (datalen != sizeof(res))
451 memcpy(&res, imsg->data, datalen);
453 message("Failed to save the cert for: %s",
458 handle_imsg_update_cert_ok(struct imsg *imsg, size_t datalen)
462 if (datalen != sizeof(res))
464 memcpy(&res, imsg->data, datalen);
466 message("Failed to update the certificate");
470 handle_dispatch_imsg(int fd, short ev, void *d)
472 struct imsgbuf *ibuf = d;
473 dispatch_imsg(ibuf, handlers, sizeof(handlers));
477 load_page_from_str(struct tab *tab, const char *page)
479 erase_buffer(&tab->buffer);
480 gemtext_initparser(&tab->buffer.page);
481 if (!tab->buffer.page.parse(&tab->buffer.page, page, strlen(page)))
483 if (!tab->buffer.page.free(&tab->buffer.page))
485 ui_on_tab_refresh(tab);
486 ui_on_tab_loaded(tab);
490 load_about_url(struct tab *tab, const char *url)
492 tab->trust = TS_VERIFIED;
494 gemtext_initparser(&tab->buffer.page);
496 imsg_compose(fsibuf, IMSG_GET, tab->id, 0, -1,
497 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 imsg_compose(netibuf, IMSG_GET_RAW, tab->id, 0, -1,
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 imsg_compose(netibuf, IMSG_GET_RAW, tab->id, 0, -1,
547 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) {
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 imsg_compose(netibuf, IMSG_STOP, tab->id, 0, -1, NULL, 0);
647 load_page_from_str(tab, "Stopped.\n");
652 add_to_bookmarks(const char *str)
654 imsg_compose(fsibuf, IMSG_BOOKMARK_PAGE, 0, 0, -1, str, strlen(str)+1);
663 imsg_compose(fsibuf, IMSG_SESSION_START, 0, 0, -1, NULL, 0);
666 TAILQ_FOREACH(tab, &tabshead, tabs) {
667 imsg_compose(fsibuf, IMSG_SESSION_TAB, 0, 0, -1,
668 tab->hist_cur->h, strlen(tab->hist_cur->h)+1);
672 imsg_compose(fsibuf, IMSG_SESSION_END, 0, 0, -1, NULL, 0);
677 main(int argc, char * const *argv)
679 struct imsgbuf network_ibuf, fs_ibuf;
680 int net_fds[2], fs_fds[2];
682 signal(SIGCHLD, SIG_IGN);
683 signal(SIGPIPE, SIG_IGN);
685 /* initialize part of the fs layer. Before starting the UI
686 * and dropping the priviledges we need to read some stuff. */
689 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, fs_fds) == -1)
690 err(1, "socketpair");
699 imsg_init(&fs_ibuf, fs_fds[1]);
700 exit(fs_main(&fs_ibuf));
703 imsg_init(&fs_ibuf, fs_fds[0]);
707 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, net_fds) == -1)
708 err(1, "socketpair");
715 setproctitle("client");
718 imsg_init(&network_ibuf, net_fds[1]);
719 exit(client_main(&network_ibuf));
722 imsg_init(&network_ibuf, net_fds[0]);
723 netibuf = &network_ibuf;
728 tofu_init(&certs, 5, offsetof(struct tofu_entry, domain));
731 TAILQ_INIT(&tabshead);
732 TAILQ_INIT(&proxies);
736 event_set(&netev, netibuf->fd, EV_READ | EV_PERSIST,
737 handle_dispatch_imsg, netibuf);
738 event_add(&netev, NULL);
740 event_set(&fsev, fsibuf->fd, EV_READ | EV_PERSIST,
741 handle_dispatch_imsg, fsibuf);
742 event_add(&fsev, NULL);
744 if (ui_init(argc, argv)) {
745 sandbox_ui_process();
750 imsg_compose(netibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
753 imsg_compose(fsibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);