1 5e11c00c 2021-03-02 op #include "telescope.h"
3 5e11c00c 2021-03-02 op #include <sys/socket.h>
5 5e11c00c 2021-03-02 op #include <errno.h>
6 5e11c00c 2021-03-02 op #include <signal.h>
7 5e11c00c 2021-03-02 op #include <stdio.h>
8 5e11c00c 2021-03-02 op #include <stdlib.h>
9 5e11c00c 2021-03-02 op #include <string.h>
10 5e11c00c 2021-03-02 op #include <unistd.h>
12 35e1f40a 2021-03-14 op struct event netev, fsev;
13 5e11c00c 2021-03-02 op struct tabshead tabshead;
15 c07ac996 2021-03-12 op /* the first is also the fallback one */
16 c07ac996 2021-03-12 op static struct proto protos[] = {
17 31f1a758 2021-04-22 op { "gemini", load_gemini_url },
18 31f1a758 2021-04-22 op { "about", load_about_url },
19 4d3785b1 2021-03-09 op { NULL, NULL },
22 35e1f40a 2021-03-14 op static struct imsgbuf *netibuf, *fsibuf;
24 2051e653 2021-03-13 op static void die(void) __attribute__((__noreturn__));
25 2051e653 2021-03-13 op static struct tab *tab_by_id(uint32_t);
26 2051e653 2021-03-13 op static void handle_imsg_err(struct imsg*, size_t);
27 2051e653 2021-03-13 op static void handle_imsg_check_cert(struct imsg*, size_t);
28 b3575139 2021-04-01 op static void handle_check_cert_user_choice(int, unsigned int);
29 2051e653 2021-03-13 op static void handle_imsg_got_code(struct imsg*, size_t);
30 2051e653 2021-03-13 op static void handle_imsg_got_meta(struct imsg*, size_t);
31 2051e653 2021-03-13 op static void handle_imsg_buf(struct imsg*, size_t);
32 2051e653 2021-03-13 op static void handle_imsg_eof(struct imsg*, size_t);
33 740f578b 2021-03-15 op static void handle_imsg_bookmark_ok(struct imsg*, size_t);
34 3a227e9a 2021-03-18 op static void handle_imsg_save_cert_ok(struct imsg*, size_t);
35 1304bbdd 2021-03-15 op static void handle_dispatch_imsg(int, short, void*);
36 2051e653 2021-03-13 op static void load_page_from_str(struct tab*, const char*);
37 2051e653 2021-03-13 op static void do_load_url(struct tab*, const char*);
39 5e11c00c 2021-03-02 op static imsg_handlerfn *handlers[] = {
40 5e11c00c 2021-03-02 op [IMSG_ERR] = handle_imsg_err,
41 5e11c00c 2021-03-02 op [IMSG_CHECK_CERT] = handle_imsg_check_cert,
42 5e11c00c 2021-03-02 op [IMSG_GOT_CODE] = handle_imsg_got_code,
43 5e11c00c 2021-03-02 op [IMSG_GOT_META] = handle_imsg_got_meta,
44 5e11c00c 2021-03-02 op [IMSG_BUF] = handle_imsg_buf,
45 5e11c00c 2021-03-02 op [IMSG_EOF] = handle_imsg_eof,
46 740f578b 2021-03-15 op [IMSG_BOOKMARK_OK] = handle_imsg_bookmark_ok,
47 3a227e9a 2021-03-18 op [IMSG_SAVE_CERT_OK] = handle_imsg_save_cert_ok,
50 cbcc75fb 2021-03-17 op static struct ohash certs;
52 5e11c00c 2021-03-02 op static void __attribute__((__noreturn__))
55 5e11c00c 2021-03-02 op abort(); /* TODO */
58 5e11c00c 2021-03-02 op static struct tab *
59 5e11c00c 2021-03-02 op tab_by_id(uint32_t id)
61 5e11c00c 2021-03-02 op struct tab *t;
63 5e11c00c 2021-03-02 op TAILQ_FOREACH(t, &tabshead, tabs) {
64 5e11c00c 2021-03-02 op if (t->id == id)
72 5e11c00c 2021-03-02 op handle_imsg_err(struct imsg *imsg, size_t datalen)
74 3a9b9365 2021-03-09 op struct tab *tab;
77 3a9b9365 2021-03-09 op tab = tab_by_id(imsg->hdr.peerid);
79 3a9b9365 2021-03-09 op page = imsg->data;
80 3a9b9365 2021-03-09 op page[datalen-1] = '\0';
82 3a9b9365 2021-03-09 op if (asprintf(&page, "# Error loading %s\n\n> %s\n",
83 2051e653 2021-03-13 op tab->hist_cur->h, page) == -1)
85 3a9b9365 2021-03-09 op load_page_from_str(tab, page);
90 5e11c00c 2021-03-02 op handle_imsg_check_cert(struct imsg *imsg, size_t datalen)
92 cbcc75fb 2021-03-17 op const char *hash;
94 cbcc75fb 2021-03-17 op struct tofu_entry *e;
95 cbcc75fb 2021-03-17 op struct tab *tab;
97 cbcc75fb 2021-03-17 op hash = imsg->data;
98 cbcc75fb 2021-03-17 op if (hash[datalen-1] != '\0')
101 10346511 2021-03-17 op tab = tab_by_id(imsg->hdr.peerid);
103 31f1a758 2021-04-22 op if ((e = telescope_lookup_tofu(&certs, tab->uri.host)) == NULL) {
104 cbcc75fb 2021-03-17 op /* TODO: an update in libressl/libretls changed
105 cbcc75fb 2021-03-17 op * significantly. Find a better approach at storing
106 cbcc75fb 2021-03-17 op * the certs! */
107 cbcc75fb 2021-03-17 op if (datalen > sizeof(e->hash))
110 cbcc75fb 2021-03-17 op tofu_res = 1; /* trust on first use */
111 cbcc75fb 2021-03-17 op if ((e = calloc(1, sizeof(*e))) == NULL)
113 31f1a758 2021-04-22 op strlcpy(e->domain, tab->uri.host, sizeof(e->domain));
114 cbcc75fb 2021-03-17 op strlcpy(e->hash, hash, sizeof(e->hash));
115 cbcc75fb 2021-03-17 op telescope_ohash_insert(&certs, e);
116 3a227e9a 2021-03-18 op imsg_compose(fsibuf, IMSG_SAVE_CERT, tab->id, 0, -1,
117 3a227e9a 2021-03-18 op e, sizeof(*e));
118 3a227e9a 2021-03-18 op imsg_flush(fsibuf);
120 cbcc75fb 2021-03-17 op tofu_res = !strcmp(hash, e->hash);
122 5d1bac73 2021-03-25 op if (tofu_res) {
123 cbcc75fb 2021-03-17 op tab->trust = e->verified ? TS_VERIFIED : TS_TRUSTED;
124 5d1bac73 2021-03-25 op imsg_compose(netibuf, IMSG_CERT_STATUS, imsg->hdr.peerid, 0, -1,
125 5d1bac73 2021-03-25 op &tofu_res, sizeof(tofu_res));
126 5d1bac73 2021-03-25 op imsg_flush(netibuf);
128 cbcc75fb 2021-03-17 op tab->trust = TS_UNTRUSTED;
129 cbcc75fb 2021-03-17 op load_page_from_str(tab, "# Certificate mismatch\n");
130 5d1bac73 2021-03-25 op ui_yornp("Certificate mismatch. Proceed?",
131 b3575139 2021-04-01 op handle_check_cert_user_choice, tab->id);
136 b3575139 2021-04-01 op handle_check_cert_user_choice(int accept, unsigned int tabid)
138 5d1bac73 2021-03-25 op imsg_compose(netibuf, IMSG_CERT_STATUS, tabid, 0, -1,
139 5d1bac73 2021-03-25 op &accept, sizeof(accept));
140 1b8a4bbf 2021-03-12 op imsg_flush(netibuf);
143 5cd2ebb1 2021-03-11 op static inline int
144 5cd2ebb1 2021-03-11 op normalize_code(int n)
146 5cd2ebb1 2021-03-11 op if (n < 20) {
147 5cd2ebb1 2021-03-11 op if (n == 10 || n == 11)
150 5cd2ebb1 2021-03-11 op } else if (n < 30) {
152 5cd2ebb1 2021-03-11 op } else if (n < 40) {
153 5cd2ebb1 2021-03-11 op if (n == 30 || n == 31)
156 5cd2ebb1 2021-03-11 op } else if (n < 50) {
160 5cd2ebb1 2021-03-11 op } else if (n < 60) {
161 5cd2ebb1 2021-03-11 op if (n <= 53 || n == 59)
164 5cd2ebb1 2021-03-11 op } else if (n < 70) {
169 5cd2ebb1 2021-03-11 op return MALFORMED_RESPONSE;
173 5e11c00c 2021-03-02 op handle_imsg_got_code(struct imsg *imsg, size_t datalen)
175 0972d8b2 2021-03-02 op struct tab *tab;
177 0972d8b2 2021-03-02 op tab = tab_by_id(imsg->hdr.peerid);
179 0972d8b2 2021-03-02 op if (sizeof(tab->code) != datalen)
182 5cd2ebb1 2021-03-11 op memcpy(&tab->code, imsg->data, sizeof(tab->code));
183 5cd2ebb1 2021-03-11 op tab->code = normalize_code(tab->code);
184 5cd2ebb1 2021-03-11 op if (tab->code != 30 && tab->code != 31)
185 0972d8b2 2021-03-02 op tab->redirect_count = 0;
189 5e11c00c 2021-03-02 op handle_imsg_got_meta(struct imsg *imsg, size_t datalen)
191 0972d8b2 2021-03-02 op struct tab *tab;
193 0972d8b2 2021-03-02 op tab = tab_by_id(imsg->hdr.peerid);
195 0972d8b2 2021-03-02 op if (sizeof(tab->meta) <= datalen)
198 0972d8b2 2021-03-02 op memcpy(tab->meta, imsg->data, datalen);
200 5cd2ebb1 2021-03-11 op if (tab->code < 10) { /* internal errors */
201 5cd2ebb1 2021-03-11 op load_page_from_str(tab, err_pages[tab->code]);
202 5cd2ebb1 2021-03-11 op } else if (tab->code < 20) { /* 1x */
203 5cd2ebb1 2021-03-11 op load_page_from_str(tab, err_pages[tab->code]);
204 5cd2ebb1 2021-03-11 op ui_require_input(tab, tab->code == 11);
205 5cd2ebb1 2021-03-11 op } else if (tab->code == 20) {
206 c07ac996 2021-03-12 op if (setup_parser_for(tab)) {
207 1b8a4bbf 2021-03-12 op imsg_compose(netibuf, IMSG_PROCEED, tab->id, 0, -1, NULL, 0);
208 1b8a4bbf 2021-03-12 op imsg_flush(netibuf);
210 c07ac996 2021-03-12 op load_page_from_str(tab, err_pages[UNKNOWN_TYPE_OR_CSET]);
212 5cd2ebb1 2021-03-11 op } else if (tab->code < 40) { /* 3x */
213 3a9b9365 2021-03-09 op tab->redirect_count++;
215 3a9b9365 2021-03-09 op /* TODO: make customizable? */
216 3a9b9365 2021-03-09 op if (tab->redirect_count > 5) {
217 3a9b9365 2021-03-09 op load_page_from_str(tab,
218 3a9b9365 2021-03-09 op err_pages[TOO_MUCH_REDIRECTS]);
220 2051e653 2021-03-13 op do_load_url(tab, tab->meta);
221 5cd2ebb1 2021-03-11 op } else { /* 4x, 5x & 6x */
222 5cd2ebb1 2021-03-11 op load_page_from_str(tab, err_pages[tab->code]);
227 5e11c00c 2021-03-02 op handle_imsg_buf(struct imsg *imsg, size_t datalen)
229 0972d8b2 2021-03-02 op struct tab *tab;
231 0972d8b2 2021-03-02 op tab = tab_by_id(imsg->hdr.peerid);
233 2ba66cea 2021-03-22 op if (!tab->window.page.parse(&tab->window.page, imsg->data, datalen))
236 0972d8b2 2021-03-02 op ui_on_tab_refresh(tab);
240 5e11c00c 2021-03-02 op handle_imsg_eof(struct imsg *imsg, size_t datalen)
242 2ba66cea 2021-03-22 op struct tab *tab;
244 2ba66cea 2021-03-22 op tab = tab_by_id(imsg->hdr.peerid);
245 2ba66cea 2021-03-22 op if (!tab->window.page.free(&tab->window.page))
248 2ba66cea 2021-03-22 op ui_on_tab_refresh(tab);
249 2ba66cea 2021-03-22 op ui_on_tab_loaded(tab);
253 740f578b 2021-03-15 op handle_imsg_bookmark_ok(struct imsg *imsg, size_t datalen)
257 740f578b 2021-03-15 op if (datalen != sizeof(res))
260 740f578b 2021-03-15 op memcpy(&res, imsg->data, sizeof(res));
261 740f578b 2021-03-15 op if (res == 0)
262 740f578b 2021-03-15 op ui_notify("Added to bookmarks!");
264 740f578b 2021-03-15 op ui_notify("Failed to add to bookmarks: %s",
265 740f578b 2021-03-15 op strerror(res));
269 3a227e9a 2021-03-18 op handle_imsg_save_cert_ok(struct imsg *imsg, size_t datalen)
273 3a227e9a 2021-03-18 op if (datalen != sizeof(res))
275 3a227e9a 2021-03-18 op memcpy(&res, imsg->data, datalen);
276 3a227e9a 2021-03-18 op if (res != 0)
277 3a227e9a 2021-03-18 op ui_notify("Failed to save the cert for: %s",
278 3a227e9a 2021-03-18 op strerror(res));
282 1304bbdd 2021-03-15 op handle_dispatch_imsg(int fd, short ev, void *d)
284 35e1f40a 2021-03-14 op struct imsgbuf *ibuf = d;
285 1304bbdd 2021-03-15 op dispatch_imsg(ibuf, handlers, sizeof(handlers));
289 0972d8b2 2021-03-02 op load_page_from_str(struct tab *tab, const char *page)
291 2ba66cea 2021-03-22 op gemtext_initparser(&tab->window.page);
292 2ba66cea 2021-03-22 op if (!tab->window.page.parse(&tab->window.page, page, strlen(page)))
294 2ba66cea 2021-03-22 op if (!tab->window.page.free(&tab->window.page))
296 0972d8b2 2021-03-02 op ui_on_tab_refresh(tab);
297 8af5e5ed 2021-03-08 op ui_on_tab_loaded(tab);
301 4d3785b1 2021-03-09 op load_about_url(struct tab *tab, const char *url)
303 10346511 2021-03-17 op tab->trust = TS_VERIFIED;
305 2ba66cea 2021-03-22 op gemtext_initparser(&tab->window.page);
307 35e1f40a 2021-03-14 op imsg_compose(fsibuf, IMSG_GET, tab->id, 0, -1,
308 31f1a758 2021-04-22 op tab->hist_cur->h, strlen(tab->hist_cur->h)+1);
309 35e1f40a 2021-03-14 op imsg_flush(fsibuf);
313 4d3785b1 2021-03-09 op load_gemini_url(struct tab *tab, const char *url)
317 2eef3403 2021-04-22 op stop_tab(tab);
318 2eef3403 2021-04-22 op tab->id = tab_new_id();
320 31f1a758 2021-04-22 op len = sizeof(tab->hist_cur->h);
321 1b8a4bbf 2021-03-12 op imsg_compose(netibuf, IMSG_GET, tab->id, 0, -1,
322 31f1a758 2021-04-22 op tab->hist_cur->h, len);
323 1b8a4bbf 2021-03-12 op imsg_flush(netibuf);
328 2051e653 2021-03-13 op do_load_url(struct tab *tab, const char *url)
330 31f1a758 2021-04-22 op struct phos_uri uri;
331 31f1a758 2021-04-22 op struct proto *p;
334 10346511 2021-03-17 op tab->trust = TS_UNKNOWN;
336 31f1a758 2021-04-22 op memcpy(&uri, &tab->uri, sizeof(tab->uri));
337 31f1a758 2021-04-22 op if (!phos_resolve_uri_from_str(&uri, url, &tab->uri)) {
338 31f1a758 2021-04-22 op if (asprintf(&t, "#error loading %s\n>%s\n",
339 31f1a758 2021-04-22 op url, "Can't parse the URI") == -1)
341 31f1a758 2021-04-22 op strlcpy(tab->hist_cur->h, url, sizeof(tab->hist_cur->h));
342 31f1a758 2021-04-22 op load_page_from_str(tab, t);
347 31f1a758 2021-04-22 op phos_serialize_uri(&tab->uri, tab->hist_cur->h,
348 31f1a758 2021-04-22 op sizeof(tab->hist_cur->h));
350 4d3785b1 2021-03-09 op for (p = protos; p->schema != NULL; ++p) {
351 ed383cf4 2021-04-22 op if (!strcmp(tab->uri.scheme, p->schema)) {
352 4d3785b1 2021-03-09 op p->loadfn(tab, url);
357 4d3785b1 2021-03-09 op protos[0].loadfn(tab, url);
361 2051e653 2021-03-13 op load_url(struct tab *tab, const char *url)
363 2051e653 2021-03-13 op if (tab->hist_cur != NULL)
364 2051e653 2021-03-13 op hist_clear_forward(&tab->hist, TAILQ_NEXT(tab->hist_cur, entries));
366 2051e653 2021-03-13 op if ((tab->hist_cur = calloc(1, sizeof(*tab->hist_cur))) == NULL) {
367 2051e653 2021-03-13 op event_loopbreak();
370 2051e653 2021-03-13 op hist_push(&tab->hist, tab->hist_cur);
371 2051e653 2021-03-13 op do_load_url(tab, url);
372 31f1a758 2021-04-22 op empty_vlist(&tab->window);
373 31f1a758 2021-04-22 op empty_linelist(&tab->window);
377 2051e653 2021-03-13 op load_previous_page(struct tab *tab)
379 2051e653 2021-03-13 op struct hist *h;
381 2051e653 2021-03-13 op if ((h = TAILQ_PREV(tab->hist_cur, mhisthead, entries)) == NULL)
383 2051e653 2021-03-13 op tab->hist_cur = h;
384 2051e653 2021-03-13 op do_load_url(tab, h->h);
389 2051e653 2021-03-13 op load_next_page(struct tab *tab)
391 2051e653 2021-03-13 op struct hist *h;
393 2051e653 2021-03-13 op if ((h = TAILQ_NEXT(tab->hist_cur, entries)) == NULL)
395 2051e653 2021-03-13 op tab->hist_cur = h;
396 2051e653 2021-03-13 op do_load_url(tab, h->h);
401 9ad4627d 2021-03-10 op stop_tab(struct tab *tab)
403 1b8a4bbf 2021-03-12 op imsg_compose(netibuf, IMSG_STOP, tab->id, 0, -1, NULL, 0);
404 1b8a4bbf 2021-03-12 op imsg_flush(netibuf);
408 740f578b 2021-03-15 op add_to_bookmarks(const char *str)
410 740f578b 2021-03-15 op imsg_compose(fsibuf, IMSG_BOOKMARK_PAGE, 0, 0, -1, str, strlen(str)+1);
411 c7107cec 2021-04-01 op imsg_flush(fsibuf);
415 c7107cec 2021-04-01 op save_session(void)
417 c7107cec 2021-04-01 op struct tab *tab;
419 c7107cec 2021-04-01 op imsg_compose(fsibuf, IMSG_SESSION_START, 0, 0, -1, NULL, 0);
420 c7107cec 2021-04-01 op imsg_flush(fsibuf);
422 c7107cec 2021-04-01 op TAILQ_FOREACH(tab, &tabshead, tabs) {
423 c7107cec 2021-04-01 op imsg_compose(fsibuf, IMSG_SESSION_TAB, 0, 0, -1,
424 c7107cec 2021-04-01 op tab->hist_cur->h, strlen(tab->hist_cur->h)+1);
425 c7107cec 2021-04-01 op imsg_flush(fsibuf);
428 c7107cec 2021-04-01 op imsg_compose(fsibuf, IMSG_SESSION_END, 0, 0, -1, NULL, 0);
429 740f578b 2021-03-15 op imsg_flush(fsibuf);
433 6cd6a9e1 2021-03-20 op main(int argc, char * const *argv)
435 35e1f40a 2021-03-14 op struct imsgbuf network_ibuf, fs_ibuf;
436 35e1f40a 2021-03-14 op int net_fds[2], fs_fds[2];
438 5e11c00c 2021-03-02 op signal(SIGCHLD, SIG_IGN);
440 3a227e9a 2021-03-18 op /* initialize part of the fs layer. Before starting the UI
441 3a227e9a 2021-03-18 op * and dropping the priviledges we need to read some stuff. */
444 35e1f40a 2021-03-14 op if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, fs_fds) == -1)
445 5e11c00c 2021-03-02 op err(1, "socketpair");
447 5e11c00c 2021-03-02 op switch (fork()) {
449 5e11c00c 2021-03-02 op err(1, "fork");
452 37d429b0 2021-04-01 op setproctitle("fs");
453 35e1f40a 2021-03-14 op close(fs_fds[0]);
454 35e1f40a 2021-03-14 op imsg_init(&fs_ibuf, fs_fds[1]);
455 35e1f40a 2021-03-14 op exit(fs_main(&fs_ibuf));
457 35e1f40a 2021-03-14 op close(fs_fds[1]);
458 35e1f40a 2021-03-14 op imsg_init(&fs_ibuf, fs_fds[0]);
459 35e1f40a 2021-03-14 op fsibuf = &fs_ibuf;
462 35e1f40a 2021-03-14 op if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, net_fds) == -1)
463 35e1f40a 2021-03-14 op err(1, "socketpair");
465 35e1f40a 2021-03-14 op switch (fork()) {
467 35e1f40a 2021-03-14 op err(1, "fork");
470 37d429b0 2021-04-01 op setproctitle("client");
471 35e1f40a 2021-03-14 op close(net_fds[0]);
472 35e1f40a 2021-03-14 op close(fs_fds[0]);
473 35e1f40a 2021-03-14 op imsg_init(&network_ibuf, net_fds[1]);
474 35e1f40a 2021-03-14 op exit(client_main(&network_ibuf));
476 35e1f40a 2021-03-14 op close(net_fds[1]);
477 35e1f40a 2021-03-14 op imsg_init(&network_ibuf, net_fds[0]);
478 35e1f40a 2021-03-14 op netibuf = &network_ibuf;
481 37d429b0 2021-04-01 op setproctitle("ui");
483 cbcc75fb 2021-03-17 op telescope_ohash_init(&certs, 5, offsetof(struct tofu_entry, domain));
484 3a227e9a 2021-03-18 op load_certs(&certs);
486 5e11c00c 2021-03-02 op TAILQ_INIT(&tabshead);
488 5e11c00c 2021-03-02 op event_init();
490 1304bbdd 2021-03-15 op event_set(&netev, netibuf->fd, EV_READ | EV_PERSIST, handle_dispatch_imsg, netibuf);
491 35e1f40a 2021-03-14 op event_add(&netev, NULL);
493 1304bbdd 2021-03-15 op event_set(&fsev, fsibuf->fd, EV_READ | EV_PERSIST, handle_dispatch_imsg, fsibuf);
494 35e1f40a 2021-03-14 op event_add(&fsev, NULL);
496 941b3761 2021-03-18 op if (ui_init(argc, argv)) {
497 941b3761 2021-03-18 op sandbox_ui_process();
498 941b3761 2021-03-18 op event_dispatch();
502 1b8a4bbf 2021-03-12 op imsg_compose(netibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
503 1b8a4bbf 2021-03-12 op imsg_flush(netibuf);
505 35e1f40a 2021-03-14 op imsg_compose(fsibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
506 35e1f40a 2021-03-14 op imsg_flush(fsibuf);