3 #include <sys/socket.h>
12 struct event netev, fsev;
13 struct tabshead tabshead;
15 /* the first is also the fallback one */
16 static struct proto protos[] = {
17 { "gemini", load_gemini_url },
18 { "about", load_about_url },
22 static struct imsgbuf *netibuf, *fsibuf;
24 static void die(void) __attribute__((__noreturn__));
25 static struct tab *tab_by_id(uint32_t);
26 static void handle_imsg_err(struct imsg*, size_t);
27 static void handle_imsg_check_cert(struct imsg*, size_t);
28 static void handle_check_cert_user_choice(int, unsigned int);
29 static void handle_maybe_save_new_cert(int, unsigned int);
30 static void handle_imsg_got_code(struct imsg*, size_t);
31 static void handle_imsg_got_meta(struct imsg*, size_t);
32 static void handle_maybe_save_page(int, unsigned int);
33 static void handle_save_page_path(const char *, unsigned int);
34 static void handle_imsg_file_opened(struct imsg*, size_t);
35 static void handle_imsg_buf(struct imsg*, size_t);
36 static void handle_imsg_eof(struct imsg*, size_t);
37 static void handle_imsg_bookmark_ok(struct imsg*, size_t);
38 static void handle_imsg_save_cert_ok(struct imsg*, size_t);
39 static void handle_imsg_update_cert_ok(struct imsg *, size_t);
40 static void handle_dispatch_imsg(int, short, void*);
41 static void load_page_from_str(struct tab*, const char*);
42 static void do_load_url(struct tab*, const char*);
44 static imsg_handlerfn *handlers[] = {
45 [IMSG_ERR] = handle_imsg_err,
46 [IMSG_CHECK_CERT] = handle_imsg_check_cert,
47 [IMSG_GOT_CODE] = handle_imsg_got_code,
48 [IMSG_GOT_META] = handle_imsg_got_meta,
49 [IMSG_BUF] = handle_imsg_buf,
50 [IMSG_EOF] = handle_imsg_eof,
51 [IMSG_BOOKMARK_OK] = handle_imsg_bookmark_ok,
52 [IMSG_SAVE_CERT_OK] = handle_imsg_save_cert_ok,
53 [IMSG_UPDATE_CERT_OK] = handle_imsg_update_cert_ok,
54 [IMSG_FILE_OPENED] = handle_imsg_file_opened,
57 static struct ohash certs;
59 static void __attribute__((__noreturn__))
66 tab_by_id(uint32_t id)
70 TAILQ_FOREACH(t, &tabshead, tabs) {
79 handle_imsg_err(struct imsg *imsg, size_t datalen)
84 tab = tab_by_id(imsg->hdr.peerid);
87 page[datalen-1] = '\0';
89 if (asprintf(&page, "# Error loading %s\n\n> %s\n",
90 tab->hist_cur->h, page) == -1)
92 load_page_from_str(tab, page);
97 handle_imsg_check_cert(struct imsg *imsg, size_t datalen)
101 struct tofu_entry *e;
105 if (hash[datalen-1] != '\0')
108 tab = tab_by_id(imsg->hdr.peerid);
110 if ((e = tofu_lookup(&certs, tab->uri.host, tab->uri.port)) == NULL) {
111 /* TODO: an update in libressl/libretls changed
112 * significantly. Find a better approach at storing
114 if (datalen > sizeof(e->hash))
117 tofu_res = 1; /* trust on first use */
118 if ((e = calloc(1, sizeof(*e))) == NULL)
120 strlcpy(e->domain, tab->uri.host, sizeof(e->domain));
121 if (*tab->uri.port != '\0' && strcmp(tab->uri.port, "1965")) {
122 strlcat(e->domain, ":", sizeof(e->domain));
123 strlcat(e->domain, tab->uri.port, sizeof(e->domain));
125 strlcpy(e->hash, hash, sizeof(e->hash));
127 imsg_compose(fsibuf, IMSG_SAVE_CERT, tab->id, 0, -1,
131 tofu_res = !strcmp(hash, e->hash);
134 tab->trust = e->verified ? TS_VERIFIED : TS_TRUSTED;
135 imsg_compose(netibuf, IMSG_CERT_STATUS, imsg->hdr.peerid, 0, -1,
136 &tofu_res, sizeof(tofu_res));
139 tab->trust = TS_UNTRUSTED;
140 load_page_from_str(tab, "# Certificate mismatch\n");
141 if ((tab->cert = strdup(hash)) == NULL)
143 ui_yornp("Certificate mismatch. Proceed?",
144 handle_check_cert_user_choice, tab->id);
149 handle_check_cert_user_choice(int accept, unsigned int tabid)
153 tab = tab_by_id(tabid);
155 imsg_compose(netibuf, IMSG_CERT_STATUS, tabid, 0, -1,
156 &accept, sizeof(accept));
160 ui_yornp("Save the new certificate?",
161 handle_maybe_save_new_cert, tabid);
169 handle_maybe_save_new_cert(int accept, unsigned int tabid)
172 struct tofu_entry *e;
174 tab = tab_by_id(tabid);
179 if ((e = calloc(1, sizeof(*e))) == NULL)
182 strlcpy(e->domain, tab->uri.host, sizeof(e->domain));
183 if (*tab->uri.port != '\0' && strcmp(tab->uri.port, "1965")) {
184 strlcat(e->domain, ":", sizeof(e->domain));
185 strlcat(e->domain, tab->uri.port, sizeof(e->domain));
187 strlcpy(e->hash, tab->cert, sizeof(e->hash));
188 imsg_compose(fsibuf, IMSG_UPDATE_CERT, 0, 0, -1, e, sizeof(*e));
191 tofu_update(&certs, e);
193 tab->trust = TS_TRUSTED;
201 normalize_code(int n)
204 if (n == 10 || n == 11)
210 if (n == 30 || n == 31)
218 if (n <= 53 || n == 59)
226 return MALFORMED_RESPONSE;
230 handle_imsg_got_code(struct imsg *imsg, size_t datalen)
234 tab = tab_by_id(imsg->hdr.peerid);
236 if (sizeof(tab->code) != datalen)
239 memcpy(&tab->code, imsg->data, sizeof(tab->code));
240 tab->code = normalize_code(tab->code);
241 if (tab->code != 30 && tab->code != 31)
242 tab->redirect_count = 0;
246 handle_imsg_got_meta(struct imsg *imsg, size_t datalen)
250 tab = tab_by_id(imsg->hdr.peerid);
252 if (sizeof(tab->meta) <= datalen)
255 memcpy(tab->meta, imsg->data, datalen);
257 if (tab->code < 10) { /* internal errors */
258 load_page_from_str(tab, err_pages[tab->code]);
259 } else if (tab->code < 20) { /* 1x */
260 load_page_from_str(tab, err_pages[tab->code]);
261 ui_require_input(tab, tab->code == 11);
262 } else if (tab->code == 20) {
263 if (setup_parser_for(tab)) {
264 imsg_compose(netibuf, IMSG_PROCEED, tab->id, 0, -1, NULL, 0);
267 load_page_from_str(tab, err_pages[UNKNOWN_TYPE_OR_CSET]);
268 ui_yornp("Can't display page, wanna save?",
269 handle_maybe_save_page, tab->id);
271 } else if (tab->code < 40) { /* 3x */
272 tab->redirect_count++;
274 /* TODO: make customizable? */
275 if (tab->redirect_count > 5) {
276 load_page_from_str(tab,
277 err_pages[TOO_MUCH_REDIRECTS]);
279 do_load_url(tab, tab->meta);
280 } else { /* 4x, 5x & 6x */
281 load_page_from_str(tab, err_pages[tab->code]);
286 handle_maybe_save_page(int dosave, unsigned int tabid)
289 ui_read("Save to path", handle_save_page_path, tabid);
291 stop_tab(tab_by_id(tabid));
295 handle_save_page_path(const char *path, unsigned int tabid)
300 stop_tab(tab_by_id(tabid));
304 tab = tab_by_id(tabid);
305 tab->path = strdup(path);
307 imsg_compose(fsibuf, IMSG_FILE_OPEN, tabid, 0, -1, path, strlen(path)+1);
312 handle_imsg_file_opened(struct imsg *imsg, size_t datalen)
319 tab = tab_by_id(imsg->hdr.peerid);
321 if (imsg->fd == -1) {
325 if (e[datalen-1] != '\0')
327 l = asprintf(&page, "# Can't open file\n\n> %s: %s\n",
331 load_page_from_str(tab, page);
335 imsg_compose(netibuf, IMSG_PROCEED, tab->id, 0, -1, NULL, 0);
341 handle_imsg_buf(struct imsg *imsg, size_t datalen)
345 char *page, buf[FMT_SCALED_STRSIZE] = {0};
347 tab = tab_by_id(imsg->hdr.peerid);
349 tab->bytes += datalen;
351 if (!tab->buffer.page.parse(&tab->buffer.page,
352 imsg->data, datalen))
355 write(tab->fd, imsg->data, datalen);
356 fmt_scaled(tab->bytes, buf);
357 l = asprintf(&page, "Saving to \"%s\"... (%s)\n",
362 load_page_from_str(tab, page);
366 ui_on_tab_refresh(tab);
370 handle_imsg_eof(struct imsg *imsg, size_t datalen)
374 char *page, buf[FMT_SCALED_STRSIZE] = {0};
376 tab = tab_by_id(imsg->hdr.peerid);
379 if (!tab->buffer.page.free(&tab->buffer.page))
382 fmt_scaled(tab->bytes, buf);
383 l = asprintf(&page, "Saved to \"%s\" (%s)\n",
388 load_page_from_str(tab, page);
397 ui_on_tab_refresh(tab);
398 ui_on_tab_loaded(tab);
402 handle_imsg_bookmark_ok(struct imsg *imsg, size_t datalen)
406 if (datalen != sizeof(res))
409 memcpy(&res, imsg->data, sizeof(res));
411 message("Added to bookmarks!");
413 message("Failed to add to bookmarks: %s",
418 handle_imsg_save_cert_ok(struct imsg *imsg, size_t datalen)
422 if (datalen != sizeof(res))
424 memcpy(&res, imsg->data, datalen);
426 message("Failed to save the cert for: %s",
431 handle_imsg_update_cert_ok(struct imsg *imsg, size_t datalen)
435 if (datalen != sizeof(res))
437 memcpy(&res, imsg->data, datalen);
439 message("Failed to update the certificate");
443 handle_dispatch_imsg(int fd, short ev, void *d)
445 struct imsgbuf *ibuf = d;
446 dispatch_imsg(ibuf, handlers, sizeof(handlers));
450 load_page_from_str(struct tab *tab, const char *page)
452 gemtext_initparser(&tab->buffer.page);
453 if (!tab->buffer.page.parse(&tab->buffer.page, page, strlen(page)))
455 if (!tab->buffer.page.free(&tab->buffer.page))
457 ui_on_tab_refresh(tab);
458 ui_on_tab_loaded(tab);
462 load_about_url(struct tab *tab, const char *url)
464 tab->trust = TS_VERIFIED;
466 gemtext_initparser(&tab->buffer.page);
468 imsg_compose(fsibuf, IMSG_GET, tab->id, 0, -1,
469 tab->hist_cur->h, strlen(tab->hist_cur->h)+1);
474 load_gemini_url(struct tab *tab, const char *url)
479 tab->id = tab_new_id();
481 len = sizeof(tab->hist_cur->h);
482 imsg_compose(netibuf, IMSG_GET, tab->id, 0, -1,
483 tab->hist_cur->h, len);
489 do_load_url(struct tab *tab, const char *url)
502 tab->trust = TS_UNKNOWN;
504 memcpy(&uri, &tab->uri, sizeof(tab->uri));
505 if (!phos_resolve_uri_from_str(&uri, url, &tab->uri)) {
506 if (asprintf(&t, "#error loading %s\n>%s\n",
507 url, "Can't parse the URI") == -1)
509 strlcpy(tab->hist_cur->h, url, sizeof(tab->hist_cur->h));
510 load_page_from_str(tab, t);
515 phos_serialize_uri(&tab->uri, tab->hist_cur->h,
516 sizeof(tab->hist_cur->h));
518 for (p = protos; p->schema != NULL; ++p) {
519 if (!strcmp(tab->uri.scheme, p->schema)) {
525 protos[0].loadfn(tab, url);
529 load_url(struct tab *tab, const char *url)
531 if (tab->hist_cur != NULL)
532 hist_clear_forward(&tab->hist, TAILQ_NEXT(tab->hist_cur, entries));
534 if ((tab->hist_cur = calloc(1, sizeof(*tab->hist_cur))) == NULL) {
538 hist_push(&tab->hist, tab->hist_cur);
539 do_load_url(tab, url);
540 empty_vlist(&tab->buffer);
541 empty_linelist(&tab->buffer);
545 load_previous_page(struct tab *tab)
549 if ((h = TAILQ_PREV(tab->hist_cur, mhisthead, entries)) == NULL)
552 do_load_url(tab, h->h);
557 load_next_page(struct tab *tab)
561 if ((h = TAILQ_NEXT(tab->hist_cur, entries)) == NULL)
564 do_load_url(tab, h->h);
569 stop_tab(struct tab *tab)
571 imsg_compose(netibuf, IMSG_STOP, tab->id, 0, -1, NULL, 0);
579 load_page_from_str(tab, "Stopped.\n");
584 add_to_bookmarks(const char *str)
586 imsg_compose(fsibuf, IMSG_BOOKMARK_PAGE, 0, 0, -1, str, strlen(str)+1);
595 imsg_compose(fsibuf, IMSG_SESSION_START, 0, 0, -1, NULL, 0);
598 TAILQ_FOREACH(tab, &tabshead, tabs) {
599 imsg_compose(fsibuf, IMSG_SESSION_TAB, 0, 0, -1,
600 tab->hist_cur->h, strlen(tab->hist_cur->h)+1);
604 imsg_compose(fsibuf, IMSG_SESSION_END, 0, 0, -1, NULL, 0);
609 main(int argc, char * const *argv)
611 struct imsgbuf network_ibuf, fs_ibuf;
612 int net_fds[2], fs_fds[2];
614 signal(SIGCHLD, SIG_IGN);
615 signal(SIGPIPE, SIG_IGN);
617 /* initialize part of the fs layer. Before starting the UI
618 * and dropping the priviledges we need to read some stuff. */
621 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, fs_fds) == -1)
622 err(1, "socketpair");
631 imsg_init(&fs_ibuf, fs_fds[1]);
632 exit(fs_main(&fs_ibuf));
635 imsg_init(&fs_ibuf, fs_fds[0]);
639 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, net_fds) == -1)
640 err(1, "socketpair");
647 setproctitle("client");
650 imsg_init(&network_ibuf, net_fds[1]);
651 exit(client_main(&network_ibuf));
654 imsg_init(&network_ibuf, net_fds[0]);
655 netibuf = &network_ibuf;
660 tofu_init(&certs, 5, offsetof(struct tofu_entry, domain));
663 TAILQ_INIT(&tabshead);
667 event_set(&netev, netibuf->fd, EV_READ | EV_PERSIST,
668 handle_dispatch_imsg, netibuf);
669 event_add(&netev, NULL);
671 event_set(&fsev, fsibuf->fd, EV_READ | EV_PERSIST,
672 handle_dispatch_imsg, fsibuf);
673 event_add(&fsev, NULL);
675 if (ui_init(argc, argv)) {
676 sandbox_ui_process();
681 imsg_compose(netibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
684 imsg_compose(fsibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);