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, unsigned int);
30 static void handle_maybe_save_new_cert(int, unsigned int);
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, unsigned int);
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 tab->trust = e->verified ? TS_VERIFIED : TS_TRUSTED;
144 imsg_compose(netibuf, IMSG_CERT_STATUS, imsg->hdr.peerid, 0, -1,
145 &tofu_res, sizeof(tofu_res));
148 tab->trust = TS_UNTRUSTED;
149 load_page_from_str(tab, "# Certificate mismatch\n");
150 if ((tab->cert = strdup(hash)) == NULL)
152 ui_yornp("Certificate mismatch. Proceed?",
153 handle_check_cert_user_choice, tab->id);
158 handle_check_cert_user_choice(int accept, unsigned int tabid)
162 tab = tab_by_id(tabid);
164 imsg_compose(netibuf, IMSG_CERT_STATUS, tabid, 0, -1,
165 &accept, sizeof(accept));
169 ui_yornp("Save the new certificate?",
170 handle_maybe_save_new_cert, tabid);
178 handle_maybe_save_new_cert(int accept, unsigned int tabid)
181 struct tofu_entry *e;
182 const char *host, *port;
184 tab = tab_by_id(tabid);
186 if (tab->proxy != NULL) {
187 host = tab->proxy->host;
188 port = tab->proxy->port;
190 host = tab->uri.host;
191 port = tab->uri.port;
197 if ((e = calloc(1, sizeof(*e))) == NULL)
200 strlcpy(e->domain, host, sizeof(e->domain));
201 if (*port != '\0' && strcmp(port, "1965")) {
202 strlcat(e->domain, ":", sizeof(e->domain));
203 strlcat(e->domain, port, sizeof(e->domain));
205 strlcpy(e->hash, tab->cert, sizeof(e->hash));
206 imsg_compose(fsibuf, IMSG_UPDATE_CERT, 0, 0, -1, e, sizeof(*e));
209 tofu_update(&certs, e);
211 tab->trust = TS_TRUSTED;
219 normalize_code(int n)
222 if (n == 10 || n == 11)
228 if (n == 30 || n == 31)
236 if (n <= 53 || n == 59)
244 return MALFORMED_RESPONSE;
248 handle_imsg_got_code(struct imsg *imsg, size_t datalen)
252 tab = tab_by_id(imsg->hdr.peerid);
254 if (sizeof(tab->code) != datalen)
257 memcpy(&tab->code, imsg->data, sizeof(tab->code));
258 tab->code = normalize_code(tab->code);
259 if (tab->code != 30 && tab->code != 31)
260 tab->redirect_count = 0;
264 handle_imsg_got_meta(struct imsg *imsg, size_t datalen)
268 tab = tab_by_id(imsg->hdr.peerid);
270 if (sizeof(tab->meta) <= datalen)
273 memcpy(tab->meta, imsg->data, datalen);
275 if (tab->code < 10) { /* internal errors */
276 load_page_from_str(tab, err_pages[tab->code]);
277 } else if (tab->code < 20) { /* 1x */
278 load_page_from_str(tab, err_pages[tab->code]);
279 ui_require_input(tab, tab->code == 11);
280 } else if (tab->code == 20) {
281 if (setup_parser_for(tab)) {
282 imsg_compose(netibuf, IMSG_PROCEED, tab->id, 0, -1, NULL, 0);
285 load_page_from_str(tab, err_pages[UNKNOWN_TYPE_OR_CSET]);
286 ui_yornp("Can't display page, wanna save?",
287 handle_maybe_save_page, tab->id);
289 } else if (tab->code < 40) { /* 3x */
290 tab->redirect_count++;
292 /* TODO: make customizable? */
293 if (tab->redirect_count > 5) {
294 load_page_from_str(tab,
295 err_pages[TOO_MUCH_REDIRECTS]);
297 do_load_url(tab, tab->meta);
298 } else { /* 4x, 5x & 6x */
299 load_page_from_str(tab, err_pages[tab->code]);
304 handle_maybe_save_page(int dosave, unsigned int tabid)
307 ui_read("Save to path", handle_save_page_path, tabid);
309 stop_tab(tab_by_id(tabid));
313 handle_save_page_path(const char *path, unsigned int tabid)
318 stop_tab(tab_by_id(tabid));
322 tab = tab_by_id(tabid);
323 tab->path = strdup(path);
325 imsg_compose(fsibuf, IMSG_FILE_OPEN, tabid, 0, -1, path, strlen(path)+1);
330 handle_imsg_file_opened(struct imsg *imsg, size_t datalen)
337 tab = tab_by_id(imsg->hdr.peerid);
339 if (imsg->fd == -1) {
343 if (e[datalen-1] != '\0')
345 l = asprintf(&page, "# Can't open file\n\n> %s: %s\n",
349 load_page_from_str(tab, page);
353 imsg_compose(netibuf, IMSG_PROCEED, tab->id, 0, -1, NULL, 0);
359 handle_imsg_buf(struct imsg *imsg, size_t datalen)
363 char *page, buf[FMT_SCALED_STRSIZE] = {0};
365 tab = tab_by_id(imsg->hdr.peerid);
367 tab->bytes += datalen;
369 if (!tab->buffer.page.parse(&tab->buffer.page,
370 imsg->data, datalen))
373 write(tab->fd, imsg->data, datalen);
374 fmt_scaled(tab->bytes, buf);
375 l = asprintf(&page, "Saving to \"%s\"... (%s)\n",
380 load_page_from_str(tab, page);
384 ui_on_tab_refresh(tab);
388 handle_imsg_eof(struct imsg *imsg, size_t datalen)
392 char *page, buf[FMT_SCALED_STRSIZE] = {0};
394 tab = tab_by_id(imsg->hdr.peerid);
397 if (!tab->buffer.page.free(&tab->buffer.page))
400 fmt_scaled(tab->bytes, buf);
401 l = asprintf(&page, "Saved to \"%s\" (%s)\n",
406 load_page_from_str(tab, page);
415 ui_on_tab_refresh(tab);
416 ui_on_tab_loaded(tab);
420 handle_imsg_bookmark_ok(struct imsg *imsg, size_t datalen)
424 if (datalen != sizeof(res))
427 memcpy(&res, imsg->data, sizeof(res));
429 message("Added to bookmarks!");
431 message("Failed to add to bookmarks: %s",
436 handle_imsg_save_cert_ok(struct imsg *imsg, size_t datalen)
440 if (datalen != sizeof(res))
442 memcpy(&res, imsg->data, datalen);
444 message("Failed to save the cert for: %s",
449 handle_imsg_update_cert_ok(struct imsg *imsg, size_t datalen)
453 if (datalen != sizeof(res))
455 memcpy(&res, imsg->data, datalen);
457 message("Failed to update the certificate");
461 handle_dispatch_imsg(int fd, short ev, void *d)
463 struct imsgbuf *ibuf = d;
464 dispatch_imsg(ibuf, handlers, sizeof(handlers));
468 load_page_from_str(struct tab *tab, const char *page)
470 gemtext_initparser(&tab->buffer.page);
471 if (!tab->buffer.page.parse(&tab->buffer.page, page, strlen(page)))
473 if (!tab->buffer.page.free(&tab->buffer.page))
475 ui_on_tab_refresh(tab);
476 ui_on_tab_loaded(tab);
480 load_about_url(struct tab *tab, const char *url)
482 tab->trust = TS_VERIFIED;
484 gemtext_initparser(&tab->buffer.page);
486 imsg_compose(fsibuf, IMSG_GET, tab->id, 0, -1,
487 tab->hist_cur->h, strlen(tab->hist_cur->h)+1);
492 load_gemini_url(struct tab *tab, const char *url)
497 tab->id = tab_new_id();
499 memset(&req, 0, sizeof(req));
500 strlcpy(req.host, tab->uri.host, sizeof(req.host));
501 strlcpy(req.port, tab->uri.port, sizeof(req.host));
503 strlcpy(req.req, tab->hist_cur->h, sizeof(req.req));
504 strlcat(req.req, "\r\n", sizeof(req.req));
506 req.proto = PROTO_GEMINI;
508 imsg_compose(netibuf, IMSG_GET_RAW, tab->id, 0, -1,
514 load_via_proxy(struct tab *tab, const char *url, struct proxy *p)
519 tab->id = tab_new_id();
522 memset(&req, 0, sizeof(req));
523 strlcpy(req.host, p->host, sizeof(req.host));
524 strlcpy(req.port, p->port, sizeof(req.host));
526 strlcpy(req.req, tab->hist_cur->h, sizeof(req.req));
527 strlcat(req.req, "\r\n", sizeof(req.req));
529 req.proto = p->proto;
531 imsg_compose(netibuf, IMSG_GET_RAW, tab->id, 0, -1,
537 do_load_url(struct tab *tab, const char *url)
551 tab->trust = TS_UNKNOWN;
553 memcpy(&uri, &tab->uri, sizeof(tab->uri));
554 if (!phos_resolve_uri_from_str(&uri, url, &tab->uri)) {
555 if (asprintf(&t, "#error loading %s\n>%s\n",
556 url, "Can't parse the URI") == -1)
558 strlcpy(tab->hist_cur->h, url, sizeof(tab->hist_cur->h));
559 load_page_from_str(tab, t);
564 phos_serialize_uri(&tab->uri, tab->hist_cur->h,
565 sizeof(tab->hist_cur->h));
567 for (p = protos; p->schema != NULL; ++p) {
568 if (!strcmp(tab->uri.scheme, p->schema)) {
574 TAILQ_FOREACH(proxy, &proxies, proxies) {
575 if (!strcmp(tab->uri.scheme, proxy->match_proto)) {
576 load_via_proxy(tab, url, proxy);
581 protos[0].loadfn(tab, url);
585 load_url(struct tab *tab, const char *url)
587 if (tab->hist_cur != NULL)
588 hist_clear_forward(&tab->hist, TAILQ_NEXT(tab->hist_cur, entries));
590 if ((tab->hist_cur = calloc(1, sizeof(*tab->hist_cur))) == NULL) {
597 hist_push(&tab->hist, tab->hist_cur);
598 do_load_url(tab, url);
599 empty_vlist(&tab->buffer);
600 empty_linelist(&tab->buffer);
604 load_previous_page(struct tab *tab)
608 if ((h = TAILQ_PREV(tab->hist_cur, mhisthead, entries)) == NULL)
611 do_load_url(tab, h->h);
616 load_next_page(struct tab *tab)
620 if ((h = TAILQ_NEXT(tab->hist_cur, entries)) == NULL)
623 do_load_url(tab, h->h);
628 stop_tab(struct tab *tab)
630 imsg_compose(netibuf, IMSG_STOP, tab->id, 0, -1, NULL, 0);
638 load_page_from_str(tab, "Stopped.\n");
643 add_to_bookmarks(const char *str)
645 imsg_compose(fsibuf, IMSG_BOOKMARK_PAGE, 0, 0, -1, str, strlen(str)+1);
654 imsg_compose(fsibuf, IMSG_SESSION_START, 0, 0, -1, NULL, 0);
657 TAILQ_FOREACH(tab, &tabshead, tabs) {
658 imsg_compose(fsibuf, IMSG_SESSION_TAB, 0, 0, -1,
659 tab->hist_cur->h, strlen(tab->hist_cur->h)+1);
663 imsg_compose(fsibuf, IMSG_SESSION_END, 0, 0, -1, NULL, 0);
668 main(int argc, char * const *argv)
670 struct imsgbuf network_ibuf, fs_ibuf;
671 int net_fds[2], fs_fds[2];
673 signal(SIGCHLD, SIG_IGN);
674 signal(SIGPIPE, SIG_IGN);
676 /* initialize part of the fs layer. Before starting the UI
677 * and dropping the priviledges we need to read some stuff. */
680 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, fs_fds) == -1)
681 err(1, "socketpair");
690 imsg_init(&fs_ibuf, fs_fds[1]);
691 exit(fs_main(&fs_ibuf));
694 imsg_init(&fs_ibuf, fs_fds[0]);
698 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, net_fds) == -1)
699 err(1, "socketpair");
706 setproctitle("client");
709 imsg_init(&network_ibuf, net_fds[1]);
710 exit(client_main(&network_ibuf));
713 imsg_init(&network_ibuf, net_fds[0]);
714 netibuf = &network_ibuf;
719 tofu_init(&certs, 5, offsetof(struct tofu_entry, domain));
722 TAILQ_INIT(&tabshead);
723 TAILQ_INIT(&proxies);
727 event_set(&netev, netibuf->fd, EV_READ | EV_PERSIST,
728 handle_dispatch_imsg, netibuf);
729 event_add(&netev, NULL);
731 event_set(&fsev, fsibuf->fd, EV_READ | EV_PERSIST,
732 handle_dispatch_imsg, fsibuf);
733 event_add(&fsev, NULL);
735 if (ui_init(argc, argv)) {
736 sandbox_ui_process();
741 imsg_compose(netibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
744 imsg_compose(fsibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);