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)
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 l = asprintf(&page, "Writing \"%s\"... (%zu bytes)\n",
361 load_page_from_str(tab, page);
365 ui_on_tab_refresh(tab);
369 handle_imsg_eof(struct imsg *imsg, size_t datalen)
375 tab = tab_by_id(imsg->hdr.peerid);
378 if (!tab->buffer.page.free(&tab->buffer.page))
381 l = asprintf(&page, "Wrote %s (%zu bytes)\n",
386 load_page_from_str(tab, page);
395 ui_on_tab_refresh(tab);
396 ui_on_tab_loaded(tab);
400 handle_imsg_bookmark_ok(struct imsg *imsg, size_t datalen)
404 if (datalen != sizeof(res))
407 memcpy(&res, imsg->data, sizeof(res));
409 message("Added to bookmarks!");
411 message("Failed to add to bookmarks: %s",
416 handle_imsg_save_cert_ok(struct imsg *imsg, size_t datalen)
420 if (datalen != sizeof(res))
422 memcpy(&res, imsg->data, datalen);
424 message("Failed to save the cert for: %s",
429 handle_imsg_update_cert_ok(struct imsg *imsg, size_t datalen)
433 if (datalen != sizeof(res))
435 memcpy(&res, imsg->data, datalen);
437 message("Failed to update the certificate");
441 handle_dispatch_imsg(int fd, short ev, void *d)
443 struct imsgbuf *ibuf = d;
444 dispatch_imsg(ibuf, handlers, sizeof(handlers));
448 load_page_from_str(struct tab *tab, const char *page)
450 gemtext_initparser(&tab->buffer.page);
451 if (!tab->buffer.page.parse(&tab->buffer.page, page, strlen(page)))
453 if (!tab->buffer.page.free(&tab->buffer.page))
455 ui_on_tab_refresh(tab);
456 ui_on_tab_loaded(tab);
460 load_about_url(struct tab *tab, const char *url)
462 tab->trust = TS_VERIFIED;
464 gemtext_initparser(&tab->buffer.page);
466 imsg_compose(fsibuf, IMSG_GET, tab->id, 0, -1,
467 tab->hist_cur->h, strlen(tab->hist_cur->h)+1);
472 load_gemini_url(struct tab *tab, const char *url)
477 tab->id = tab_new_id();
479 len = sizeof(tab->hist_cur->h);
480 imsg_compose(netibuf, IMSG_GET, tab->id, 0, -1,
481 tab->hist_cur->h, len);
487 do_load_url(struct tab *tab, const char *url)
500 tab->trust = TS_UNKNOWN;
502 memcpy(&uri, &tab->uri, sizeof(tab->uri));
503 if (!phos_resolve_uri_from_str(&uri, url, &tab->uri)) {
504 if (asprintf(&t, "#error loading %s\n>%s\n",
505 url, "Can't parse the URI") == -1)
507 strlcpy(tab->hist_cur->h, url, sizeof(tab->hist_cur->h));
508 load_page_from_str(tab, t);
513 phos_serialize_uri(&tab->uri, tab->hist_cur->h,
514 sizeof(tab->hist_cur->h));
516 for (p = protos; p->schema != NULL; ++p) {
517 if (!strcmp(tab->uri.scheme, p->schema)) {
523 protos[0].loadfn(tab, url);
527 load_url(struct tab *tab, const char *url)
529 if (tab->hist_cur != NULL)
530 hist_clear_forward(&tab->hist, TAILQ_NEXT(tab->hist_cur, entries));
532 if ((tab->hist_cur = calloc(1, sizeof(*tab->hist_cur))) == NULL) {
536 hist_push(&tab->hist, tab->hist_cur);
537 do_load_url(tab, url);
538 empty_vlist(&tab->buffer);
539 empty_linelist(&tab->buffer);
543 load_previous_page(struct tab *tab)
547 if ((h = TAILQ_PREV(tab->hist_cur, mhisthead, entries)) == NULL)
550 do_load_url(tab, h->h);
555 load_next_page(struct tab *tab)
559 if ((h = TAILQ_NEXT(tab->hist_cur, entries)) == NULL)
562 do_load_url(tab, h->h);
567 stop_tab(struct tab *tab)
569 imsg_compose(netibuf, IMSG_STOP, tab->id, 0, -1, NULL, 0);
577 load_page_from_str(tab, "Stopped.\n");
582 add_to_bookmarks(const char *str)
584 imsg_compose(fsibuf, IMSG_BOOKMARK_PAGE, 0, 0, -1, str, strlen(str)+1);
593 imsg_compose(fsibuf, IMSG_SESSION_START, 0, 0, -1, NULL, 0);
596 TAILQ_FOREACH(tab, &tabshead, tabs) {
597 imsg_compose(fsibuf, IMSG_SESSION_TAB, 0, 0, -1,
598 tab->hist_cur->h, strlen(tab->hist_cur->h)+1);
602 imsg_compose(fsibuf, IMSG_SESSION_END, 0, 0, -1, NULL, 0);
607 main(int argc, char * const *argv)
609 struct imsgbuf network_ibuf, fs_ibuf;
610 int net_fds[2], fs_fds[2];
612 signal(SIGCHLD, SIG_IGN);
613 signal(SIGPIPE, SIG_IGN);
615 /* initialize part of the fs layer. Before starting the UI
616 * and dropping the priviledges we need to read some stuff. */
619 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, fs_fds) == -1)
620 err(1, "socketpair");
629 imsg_init(&fs_ibuf, fs_fds[1]);
630 exit(fs_main(&fs_ibuf));
633 imsg_init(&fs_ibuf, fs_fds[0]);
637 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, net_fds) == -1)
638 err(1, "socketpair");
645 setproctitle("client");
648 imsg_init(&network_ibuf, net_fds[1]);
649 exit(client_main(&network_ibuf));
652 imsg_init(&network_ibuf, net_fds[0]);
653 netibuf = &network_ibuf;
658 tofu_init(&certs, 5, offsetof(struct tofu_entry, domain));
661 TAILQ_INIT(&tabshead);
665 event_set(&netev, netibuf->fd, EV_READ | EV_PERSIST,
666 handle_dispatch_imsg, netibuf);
667 event_add(&netev, NULL);
669 event_set(&fsev, fsibuf->fd, EV_READ | EV_PERSIST,
670 handle_dispatch_imsg, fsibuf);
671 event_add(&fsev, NULL);
673 if (ui_init(argc, argv)) {
674 sandbox_ui_process();
679 imsg_compose(netibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
682 imsg_compose(fsibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);