Blob


1 #include "telescope.h"
3 #include <sys/socket.h>
5 #include <errno.h>
6 #include <signal.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <unistd.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 },
19 { NULL, NULL },
20 };
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,
51 };
53 static struct ohash certs;
55 static void __attribute__((__noreturn__))
56 die(void)
57 {
58 abort(); /* TODO */
59 }
61 static struct tab *
62 tab_by_id(uint32_t id)
63 {
64 struct tab *t;
66 TAILQ_FOREACH(t, &tabshead, tabs) {
67 if (t->id == id)
68 return t;
69 }
71 die();
72 }
74 static void
75 handle_imsg_err(struct imsg *imsg, size_t datalen)
76 {
77 struct tab *tab;
78 char *page;
80 tab = tab_by_id(imsg->hdr.peerid);
82 page = imsg->data;
83 page[datalen-1] = '\0';
85 if (asprintf(&page, "# Error loading %s\n\n> %s\n",
86 tab->hist_cur->h, page) == -1)
87 die();
88 load_page_from_str(tab, page);
89 free(page);
90 }
92 static void
93 handle_imsg_check_cert(struct imsg *imsg, size_t datalen)
94 {
95 const char *hash;
96 int tofu_res;
97 struct tofu_entry *e;
98 struct tab *tab;
100 hash = imsg->data;
101 if (hash[datalen-1] != '\0')
102 abort();
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
109 * the certs! */
110 if (datalen > sizeof(e->hash))
111 abort();
113 tofu_res = 1; /* trust on first use */
114 if ((e = calloc(1, sizeof(*e))) == NULL)
115 abort();
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));
122 tofu_add(&certs, e);
123 imsg_compose(fsibuf, IMSG_SAVE_CERT, tab->id, 0, -1,
124 e, sizeof(*e));
125 imsg_flush(fsibuf);
126 } else
127 tofu_res = !strcmp(hash, e->hash);
129 if (tofu_res) {
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));
133 imsg_flush(netibuf);
134 } else {
135 tab->trust = TS_UNTRUSTED;
136 load_page_from_str(tab, "# Certificate mismatch\n");
137 if ((tab->cert = strdup(hash)) == NULL)
138 die();
139 ui_yornp("Certificate mismatch. Proceed?",
140 handle_check_cert_user_choice, tab->id);
144 static void
145 handle_check_cert_user_choice(int accept, unsigned int tabid)
147 struct tab *tab;
149 tab = tab_by_id(tabid);
151 imsg_compose(netibuf, IMSG_CERT_STATUS, tabid, 0, -1,
152 &accept, sizeof(accept));
153 imsg_flush(netibuf);
155 if (accept)
156 ui_yornp("Save the new certificate?",
157 handle_maybe_save_new_cert, tabid);
158 else {
159 free(tab->cert);
160 tab->cert = NULL;
164 static void
165 handle_maybe_save_new_cert(int accept, unsigned int tabid)
167 struct tab *tab;
168 struct tofu_entry *e;
170 tab = tab_by_id(tabid);
172 if (!accept)
173 goto end;
175 if ((e = calloc(1, sizeof(e))) == NULL)
176 die();
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));
185 imsg_flush(fsibuf);
187 tofu_update(&certs, e);
189 tab->trust = TS_TRUSTED;
191 end:
192 free(tab->cert);
193 tab->cert = NULL;
196 static inline int
197 normalize_code(int n)
199 if (n < 20) {
200 if (n == 10 || n == 11)
201 return n;
202 return 10;
203 } else if (n < 30) {
204 return 20;
205 } else if (n < 40) {
206 if (n == 30 || n == 31)
207 return n;
208 return 30;
209 } else if (n < 50) {
210 if (n <= 44)
211 return n;
212 return 40;
213 } else if (n < 60) {
214 if (n <= 53 || n == 59)
215 return n;
216 return 50;
217 } else if (n < 70) {
218 if (n <= 62)
219 return n;
220 return 60;
221 } else
222 return MALFORMED_RESPONSE;
225 static void
226 handle_imsg_got_code(struct imsg *imsg, size_t datalen)
228 struct tab *tab;
230 tab = tab_by_id(imsg->hdr.peerid);
232 if (sizeof(tab->code) != datalen)
233 die();
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;
241 static void
242 handle_imsg_got_meta(struct imsg *imsg, size_t datalen)
244 struct tab *tab;
246 tab = tab_by_id(imsg->hdr.peerid);
248 if (sizeof(tab->meta) <= datalen)
249 die();
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);
261 imsg_flush(netibuf);
262 } else {
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]);
272 } else
273 do_load_url(tab, tab->meta);
274 } else { /* 4x, 5x & 6x */
275 load_page_from_str(tab, err_pages[tab->code]);
279 static void
280 handle_imsg_buf(struct imsg *imsg, size_t datalen)
282 struct tab *tab;
284 tab = tab_by_id(imsg->hdr.peerid);
286 if (!tab->window.page.parse(&tab->window.page, imsg->data, datalen))
287 die();
289 ui_on_tab_refresh(tab);
292 static void
293 handle_imsg_eof(struct imsg *imsg, size_t datalen)
295 struct tab *tab;
297 tab = tab_by_id(imsg->hdr.peerid);
298 if (!tab->window.page.free(&tab->window.page))
299 die();
301 ui_on_tab_refresh(tab);
302 ui_on_tab_loaded(tab);
305 static void
306 handle_imsg_bookmark_ok(struct imsg *imsg, size_t datalen)
308 int res;
310 if (datalen != sizeof(res))
311 die();
313 memcpy(&res, imsg->data, sizeof(res));
314 if (res == 0)
315 ui_notify("Added to bookmarks!");
316 else
317 ui_notify("Failed to add to bookmarks: %s",
318 strerror(res));
321 static void
322 handle_imsg_save_cert_ok(struct imsg *imsg, size_t datalen)
324 int res;
326 if (datalen != sizeof(res))
327 die();
328 memcpy(&res, imsg->data, datalen);
329 if (res != 0)
330 ui_notify("Failed to save the cert for: %s",
331 strerror(res));
334 static void
335 handle_imsg_update_cert_ok(struct imsg *imsg, size_t datalen)
337 int res;
339 if (datalen != sizeof(res))
340 die();
341 memcpy(&res, imsg->data, datalen);
342 if (!res)
343 ui_notify("Failed to update the certificate");
346 static void
347 handle_dispatch_imsg(int fd, short ev, void *d)
349 struct imsgbuf *ibuf = d;
350 dispatch_imsg(ibuf, handlers, sizeof(handlers));
353 static void
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)))
358 die();
359 if (!tab->window.page.free(&tab->window.page))
360 die();
361 ui_on_tab_refresh(tab);
362 ui_on_tab_loaded(tab);
365 void
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);
374 imsg_flush(fsibuf);
377 void
378 load_gemini_url(struct tab *tab, const char *url)
380 size_t len;
382 stop_tab(tab);
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);
388 imsg_flush(netibuf);
389 return;
392 static void
393 do_load_url(struct tab *tab, const char *url)
395 struct phos_uri uri;
396 struct proto *p;
397 char *t;
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)
405 die();
406 strlcpy(tab->hist_cur->h, url, sizeof(tab->hist_cur->h));
407 load_page_from_str(tab, t);
408 free(t);
409 return;
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)) {
417 p->loadfn(tab, url);
418 return;
422 protos[0].loadfn(tab, url);
425 void
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) {
432 event_loopbreak();
433 return;
435 hist_push(&tab->hist, tab->hist_cur);
436 do_load_url(tab, url);
437 empty_vlist(&tab->window);
438 empty_linelist(&tab->window);
441 int
442 load_previous_page(struct tab *tab)
444 struct hist *h;
446 if ((h = TAILQ_PREV(tab->hist_cur, mhisthead, entries)) == NULL)
447 return 0;
448 tab->hist_cur = h;
449 do_load_url(tab, h->h);
450 return 1;
453 int
454 load_next_page(struct tab *tab)
456 struct hist *h;
458 if ((h = TAILQ_NEXT(tab->hist_cur, entries)) == NULL)
459 return 0;
460 tab->hist_cur = h;
461 do_load_url(tab, h->h);
462 return 1;
465 void
466 stop_tab(struct tab *tab)
468 imsg_compose(netibuf, IMSG_STOP, tab->id, 0, -1, NULL, 0);
469 imsg_flush(netibuf);
472 void
473 add_to_bookmarks(const char *str)
475 imsg_compose(fsibuf, IMSG_BOOKMARK_PAGE, 0, 0, -1, str, strlen(str)+1);
476 imsg_flush(fsibuf);
479 void
480 save_session(void)
482 struct tab *tab;
484 imsg_compose(fsibuf, IMSG_SESSION_START, 0, 0, -1, NULL, 0);
485 imsg_flush(fsibuf);
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);
490 imsg_flush(fsibuf);
493 imsg_compose(fsibuf, IMSG_SESSION_END, 0, 0, -1, NULL, 0);
494 imsg_flush(fsibuf);
497 int
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. */
507 fs_init();
509 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, fs_fds) == -1)
510 err(1, "socketpair");
512 switch (fork()) {
513 case -1:
514 err(1, "fork");
515 case 0:
516 /* child */
517 setproctitle("fs");
518 close(fs_fds[0]);
519 imsg_init(&fs_ibuf, fs_fds[1]);
520 exit(fs_main(&fs_ibuf));
521 default:
522 close(fs_fds[1]);
523 imsg_init(&fs_ibuf, fs_fds[0]);
524 fsibuf = &fs_ibuf;
527 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, net_fds) == -1)
528 err(1, "socketpair");
530 switch (fork()) {
531 case -1:
532 err(1, "fork");
533 case 0:
534 /* child */
535 setproctitle("client");
536 close(net_fds[0]);
537 close(fs_fds[0]);
538 imsg_init(&network_ibuf, net_fds[1]);
539 exit(client_main(&network_ibuf));
540 default:
541 close(net_fds[1]);
542 imsg_init(&network_ibuf, net_fds[0]);
543 netibuf = &network_ibuf;
546 setproctitle("ui");
548 tofu_init(&certs, 5, offsetof(struct tofu_entry, domain));
549 load_certs(&certs);
551 TAILQ_INIT(&tabshead);
553 event_init();
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();
563 event_dispatch();
564 ui_end();
567 imsg_compose(netibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
568 imsg_flush(netibuf);
570 imsg_compose(fsibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
571 imsg_flush(fsibuf);
573 return 0;