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_imsg_buf(struct imsg*, size_t);
33 static void handle_imsg_eof(struct imsg*, size_t);
34 static void handle_imsg_bookmark_ok(struct imsg*, size_t);
35 static void handle_imsg_save_cert_ok(struct imsg*, size_t);
36 static void handle_imsg_update_cert_ok(struct imsg *, size_t);
37 static void handle_dispatch_imsg(int, short, void*);
38 static void load_page_from_str(struct tab*, const char*);
39 static void do_load_url(struct tab*, const char*);
41 static imsg_handlerfn *handlers[] = {
42 [IMSG_ERR] = handle_imsg_err,
43 [IMSG_CHECK_CERT] = handle_imsg_check_cert,
44 [IMSG_GOT_CODE] = handle_imsg_got_code,
45 [IMSG_GOT_META] = handle_imsg_got_meta,
46 [IMSG_BUF] = handle_imsg_buf,
47 [IMSG_EOF] = handle_imsg_eof,
48 [IMSG_BOOKMARK_OK] = handle_imsg_bookmark_ok,
49 [IMSG_SAVE_CERT_OK] = handle_imsg_save_cert_ok,
50 [IMSG_UPDATE_CERT_OK] = handle_imsg_update_cert_ok,
53 static struct ohash certs;
55 static void __attribute__((__noreturn__))
62 tab_by_id(uint32_t id)
66 TAILQ_FOREACH(t, &tabshead, tabs) {
75 handle_imsg_err(struct imsg *imsg, size_t datalen)
80 tab = tab_by_id(imsg->hdr.peerid);
83 page[datalen-1] = '\0';
85 if (asprintf(&page, "# Error loading %s\n\n> %s\n",
86 tab->hist_cur->h, page) == -1)
88 load_page_from_str(tab, page);
93 handle_imsg_check_cert(struct imsg *imsg, size_t datalen)
101 if (hash[datalen-1] != '\0')
104 tab = tab_by_id(imsg->hdr.peerid);
106 if ((e = tofu_lookup(&certs, tab->uri.host, tab->uri.port)) == NULL) {
107 /* TODO: an update in libressl/libretls changed
108 * significantly. Find a better approach at storing
110 if (datalen > sizeof(e->hash))
113 tofu_res = 1; /* trust on first use */
114 if ((e = calloc(1, sizeof(*e))) == NULL)
116 strlcpy(e->domain, tab->uri.host, sizeof(e->domain));
117 if (*tab->uri.port != '\0' && strcmp(tab->uri.port, "1965")) {
118 strlcat(e->domain, ":", sizeof(e->domain));
119 strlcat(e->domain, tab->uri.port, sizeof(e->domain));
121 strlcpy(e->hash, hash, sizeof(e->hash));
123 imsg_compose(fsibuf, IMSG_SAVE_CERT, tab->id, 0, -1,
127 tofu_res = !strcmp(hash, e->hash);
130 tab->trust = e->verified ? TS_VERIFIED : TS_TRUSTED;
131 imsg_compose(netibuf, IMSG_CERT_STATUS, imsg->hdr.peerid, 0, -1,
132 &tofu_res, sizeof(tofu_res));
135 tab->trust = TS_UNTRUSTED;
136 load_page_from_str(tab, "# Certificate mismatch\n");
137 if ((tab->cert = strdup(hash)) == NULL)
139 ui_yornp("Certificate mismatch. Proceed?",
140 handle_check_cert_user_choice, tab->id);
145 handle_check_cert_user_choice(int accept, unsigned int tabid)
149 tab = tab_by_id(tabid);
151 imsg_compose(netibuf, IMSG_CERT_STATUS, tabid, 0, -1,
152 &accept, sizeof(accept));
156 ui_yornp("Save the new certificate?",
157 handle_maybe_save_new_cert, tabid);
165 handle_maybe_save_new_cert(int accept, unsigned int tabid)
168 struct tofu_entry *e;
170 tab = tab_by_id(tabid);
175 if ((e = calloc(1, sizeof(e))) == NULL)
178 strlcpy(e->domain, tab->uri.host, sizeof(e->domain));
179 if (*tab->uri.port != '\0' && strcmp(tab->uri.port, "1965")) {
180 strlcat(e->domain, ":", sizeof(e->domain));
181 strlcat(e->domain, tab->uri.port, sizeof(e->domain));
183 strlcpy(e->hash, tab->cert, sizeof(e->hash));
184 imsg_compose(fsibuf, IMSG_UPDATE_CERT, 0, 0, -1, e, sizeof(*e));
187 tofu_update(&certs, e);
189 tab->trust = TS_TRUSTED;
197 normalize_code(int n)
200 if (n == 10 || n == 11)
206 if (n == 30 || n == 31)
214 if (n <= 53 || n == 59)
222 return MALFORMED_RESPONSE;
226 handle_imsg_got_code(struct imsg *imsg, size_t datalen)
230 tab = tab_by_id(imsg->hdr.peerid);
232 if (sizeof(tab->code) != datalen)
235 memcpy(&tab->code, imsg->data, sizeof(tab->code));
236 tab->code = normalize_code(tab->code);
237 if (tab->code != 30 && tab->code != 31)
238 tab->redirect_count = 0;
242 handle_imsg_got_meta(struct imsg *imsg, size_t datalen)
246 tab = tab_by_id(imsg->hdr.peerid);
248 if (sizeof(tab->meta) <= datalen)
251 memcpy(tab->meta, imsg->data, datalen);
253 if (tab->code < 10) { /* internal errors */
254 load_page_from_str(tab, err_pages[tab->code]);
255 } else if (tab->code < 20) { /* 1x */
256 load_page_from_str(tab, err_pages[tab->code]);
257 ui_require_input(tab, tab->code == 11);
258 } else if (tab->code == 20) {
259 if (setup_parser_for(tab)) {
260 imsg_compose(netibuf, IMSG_PROCEED, tab->id, 0, -1, NULL, 0);
263 load_page_from_str(tab, err_pages[UNKNOWN_TYPE_OR_CSET]);
265 } else if (tab->code < 40) { /* 3x */
266 tab->redirect_count++;
268 /* TODO: make customizable? */
269 if (tab->redirect_count > 5) {
270 load_page_from_str(tab,
271 err_pages[TOO_MUCH_REDIRECTS]);
273 do_load_url(tab, tab->meta);
274 } else { /* 4x, 5x & 6x */
275 load_page_from_str(tab, err_pages[tab->code]);
280 handle_imsg_buf(struct imsg *imsg, size_t datalen)
284 tab = tab_by_id(imsg->hdr.peerid);
286 if (!tab->window.page.parse(&tab->window.page, imsg->data, datalen))
289 ui_on_tab_refresh(tab);
293 handle_imsg_eof(struct imsg *imsg, size_t datalen)
297 tab = tab_by_id(imsg->hdr.peerid);
298 if (!tab->window.page.free(&tab->window.page))
301 ui_on_tab_refresh(tab);
302 ui_on_tab_loaded(tab);
306 handle_imsg_bookmark_ok(struct imsg *imsg, size_t datalen)
310 if (datalen != sizeof(res))
313 memcpy(&res, imsg->data, sizeof(res));
315 ui_notify("Added to bookmarks!");
317 ui_notify("Failed to add to bookmarks: %s",
322 handle_imsg_save_cert_ok(struct imsg *imsg, size_t datalen)
326 if (datalen != sizeof(res))
328 memcpy(&res, imsg->data, datalen);
330 ui_notify("Failed to save the cert for: %s",
335 handle_imsg_update_cert_ok(struct imsg *imsg, size_t datalen)
339 if (datalen != sizeof(res))
341 memcpy(&res, imsg->data, datalen);
343 ui_notify("Failed to update the certificate");
347 handle_dispatch_imsg(int fd, short ev, void *d)
349 struct imsgbuf *ibuf = d;
350 dispatch_imsg(ibuf, handlers, sizeof(handlers));
354 load_page_from_str(struct tab *tab, const char *page)
356 gemtext_initparser(&tab->window.page);
357 if (!tab->window.page.parse(&tab->window.page, page, strlen(page)))
359 if (!tab->window.page.free(&tab->window.page))
361 ui_on_tab_refresh(tab);
362 ui_on_tab_loaded(tab);
366 load_about_url(struct tab *tab, const char *url)
368 tab->trust = TS_VERIFIED;
370 gemtext_initparser(&tab->window.page);
372 imsg_compose(fsibuf, IMSG_GET, tab->id, 0, -1,
373 tab->hist_cur->h, strlen(tab->hist_cur->h)+1);
378 load_gemini_url(struct tab *tab, const char *url)
383 tab->id = tab_new_id();
385 len = sizeof(tab->hist_cur->h);
386 imsg_compose(netibuf, IMSG_GET, tab->id, 0, -1,
387 tab->hist_cur->h, len);
393 do_load_url(struct tab *tab, const char *url)
399 tab->trust = TS_UNKNOWN;
401 memcpy(&uri, &tab->uri, sizeof(tab->uri));
402 if (!phos_resolve_uri_from_str(&uri, url, &tab->uri)) {
403 if (asprintf(&t, "#error loading %s\n>%s\n",
404 url, "Can't parse the URI") == -1)
406 strlcpy(tab->hist_cur->h, url, sizeof(tab->hist_cur->h));
407 load_page_from_str(tab, t);
412 phos_serialize_uri(&tab->uri, tab->hist_cur->h,
413 sizeof(tab->hist_cur->h));
415 for (p = protos; p->schema != NULL; ++p) {
416 if (!strcmp(tab->uri.scheme, p->schema)) {
422 protos[0].loadfn(tab, url);
426 load_url(struct tab *tab, const char *url)
428 if (tab->hist_cur != NULL)
429 hist_clear_forward(&tab->hist, TAILQ_NEXT(tab->hist_cur, entries));
431 if ((tab->hist_cur = calloc(1, sizeof(*tab->hist_cur))) == NULL) {
435 hist_push(&tab->hist, tab->hist_cur);
436 do_load_url(tab, url);
437 empty_vlist(&tab->window);
438 empty_linelist(&tab->window);
442 load_previous_page(struct tab *tab)
446 if ((h = TAILQ_PREV(tab->hist_cur, mhisthead, entries)) == NULL)
449 do_load_url(tab, h->h);
454 load_next_page(struct tab *tab)
458 if ((h = TAILQ_NEXT(tab->hist_cur, entries)) == NULL)
461 do_load_url(tab, h->h);
466 stop_tab(struct tab *tab)
468 imsg_compose(netibuf, IMSG_STOP, tab->id, 0, -1, NULL, 0);
473 add_to_bookmarks(const char *str)
475 imsg_compose(fsibuf, IMSG_BOOKMARK_PAGE, 0, 0, -1, str, strlen(str)+1);
484 imsg_compose(fsibuf, IMSG_SESSION_START, 0, 0, -1, NULL, 0);
487 TAILQ_FOREACH(tab, &tabshead, tabs) {
488 imsg_compose(fsibuf, IMSG_SESSION_TAB, 0, 0, -1,
489 tab->hist_cur->h, strlen(tab->hist_cur->h)+1);
493 imsg_compose(fsibuf, IMSG_SESSION_END, 0, 0, -1, NULL, 0);
498 main(int argc, char * const *argv)
500 struct imsgbuf network_ibuf, fs_ibuf;
501 int net_fds[2], fs_fds[2];
503 signal(SIGCHLD, SIG_IGN);
505 /* initialize part of the fs layer. Before starting the UI
506 * and dropping the priviledges we need to read some stuff. */
509 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, fs_fds) == -1)
510 err(1, "socketpair");
519 imsg_init(&fs_ibuf, fs_fds[1]);
520 exit(fs_main(&fs_ibuf));
523 imsg_init(&fs_ibuf, fs_fds[0]);
527 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, net_fds) == -1)
528 err(1, "socketpair");
535 setproctitle("client");
538 imsg_init(&network_ibuf, net_fds[1]);
539 exit(client_main(&network_ibuf));
542 imsg_init(&network_ibuf, net_fds[0]);
543 netibuf = &network_ibuf;
548 tofu_init(&certs, 5, offsetof(struct tofu_entry, domain));
551 TAILQ_INIT(&tabshead);
555 event_set(&netev, netibuf->fd, EV_READ | EV_PERSIST, handle_dispatch_imsg, netibuf);
556 event_add(&netev, NULL);
558 event_set(&fsev, fsibuf->fd, EV_READ | EV_PERSIST, handle_dispatch_imsg, fsibuf);
559 event_add(&fsev, NULL);
561 if (ui_init(argc, argv)) {
562 sandbox_ui_process();
567 imsg_compose(netibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
570 imsg_compose(fsibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);