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;
14 struct proxylist proxies;
16 /* the first is also the fallback one */
17 static struct proto protos[] = {
18 { "gemini", load_gemini_url },
19 { "about", load_about_url },
20 { NULL, NULL },
21 };
23 static struct imsgbuf *netibuf, *fsibuf;
25 static void die(void) __attribute__((__noreturn__));
26 static struct tab *tab_by_id(uint32_t);
27 static void handle_imsg_err(struct imsg*, size_t);
28 static void handle_imsg_check_cert(struct imsg*, size_t);
29 static void handle_check_cert_user_choice(int, struct tab *);
30 static void handle_maybe_save_new_cert(int, struct tab *);
31 static void handle_imsg_got_code(struct imsg*, size_t);
32 static void handle_imsg_got_meta(struct imsg*, size_t);
33 static void handle_maybe_save_page(int, struct tab *);
34 static void handle_save_page_path(const char *, unsigned int);
35 static void handle_imsg_file_opened(struct imsg*, size_t);
36 static void handle_imsg_buf(struct imsg*, size_t);
37 static void handle_imsg_eof(struct imsg*, size_t);
38 static void handle_imsg_bookmark_ok(struct imsg*, size_t);
39 static void handle_imsg_save_cert_ok(struct imsg*, size_t);
40 static void handle_imsg_update_cert_ok(struct imsg *, size_t);
41 static void handle_dispatch_imsg(int, short, void*);
42 static void load_page_from_str(struct tab*, const char*);
43 static void do_load_url(struct tab*, const char*);
45 static imsg_handlerfn *handlers[] = {
46 [IMSG_ERR] = handle_imsg_err,
47 [IMSG_CHECK_CERT] = handle_imsg_check_cert,
48 [IMSG_GOT_CODE] = handle_imsg_got_code,
49 [IMSG_GOT_META] = handle_imsg_got_meta,
50 [IMSG_BUF] = handle_imsg_buf,
51 [IMSG_EOF] = handle_imsg_eof,
52 [IMSG_BOOKMARK_OK] = handle_imsg_bookmark_ok,
53 [IMSG_SAVE_CERT_OK] = handle_imsg_save_cert_ok,
54 [IMSG_UPDATE_CERT_OK] = handle_imsg_update_cert_ok,
55 [IMSG_FILE_OPENED] = handle_imsg_file_opened,
56 };
58 static struct ohash certs;
60 static void __attribute__((__noreturn__))
61 die(void)
62 {
63 abort(); /* TODO */
64 }
66 static struct tab *
67 tab_by_id(uint32_t id)
68 {
69 struct tab *t;
71 TAILQ_FOREACH(t, &tabshead, tabs) {
72 if (t->id == id)
73 return t;
74 }
76 die();
77 }
79 static void
80 handle_imsg_err(struct imsg *imsg, size_t datalen)
81 {
82 struct tab *tab;
83 char *page;
85 tab = tab_by_id(imsg->hdr.peerid);
87 page = imsg->data;
88 page[datalen-1] = '\0';
90 if (asprintf(&page, "# Error loading %s\n\n> %s\n",
91 tab->hist_cur->h, page) == -1)
92 die();
93 load_page_from_str(tab, page);
94 free(page);
95 }
97 static void
98 handle_imsg_check_cert(struct imsg *imsg, size_t datalen)
99 {
100 const char *hash, *host, *port;
101 int tofu_res;
102 struct tofu_entry *e;
103 struct tab *tab;
105 hash = imsg->data;
106 if (hash[datalen-1] != '\0')
107 abort();
109 tab = tab_by_id(imsg->hdr.peerid);
111 if (tab->proxy != NULL) {
112 host = tab->proxy->host;
113 port = tab->proxy->port;
114 } else {
115 host = tab->uri.host;
116 port = tab->uri.port;
119 if ((e = tofu_lookup(&certs, host, port)) == NULL) {
120 /* TODO: an update in libressl/libretls changed
121 * significantly. Find a better approach at storing
122 * the certs! */
123 if (datalen > sizeof(e->hash))
124 abort();
126 tofu_res = 1; /* trust on first use */
127 if ((e = calloc(1, sizeof(*e))) == NULL)
128 abort();
129 strlcpy(e->domain, host, sizeof(e->domain));
130 if (*port != '\0' && strcmp(port, "1965")) {
131 strlcat(e->domain, ":", sizeof(e->domain));
132 strlcat(e->domain, port, sizeof(e->domain));
134 strlcpy(e->hash, hash, sizeof(e->hash));
135 tofu_add(&certs, e);
136 imsg_compose(fsibuf, IMSG_SAVE_CERT, tab->id, 0, -1,
137 e, sizeof(*e));
138 imsg_flush(fsibuf);
139 } else
140 tofu_res = !strcmp(hash, e->hash);
142 if (tofu_res) {
143 if (e->verified == -1)
144 tab->trust = TS_TEMP_TRUSTED;
145 else if (e->verified == 1)
146 tab->trust = TS_VERIFIED;
147 else
148 tab->trust = TS_TRUSTED;
150 imsg_compose(netibuf, IMSG_CERT_STATUS, imsg->hdr.peerid, 0, -1,
151 &tofu_res, sizeof(tofu_res));
152 imsg_flush(netibuf);
153 } else {
154 tab->trust = TS_UNTRUSTED;
155 load_page_from_str(tab, "# Certificate mismatch\n");
156 if ((tab->cert = strdup(hash)) == NULL)
157 die();
158 ui_yornp("Certificate mismatch. Proceed?",
159 handle_check_cert_user_choice, tab);
163 static void
164 handle_check_cert_user_choice(int accept, struct tab *tab)
166 imsg_compose(netibuf, IMSG_CERT_STATUS, tab->id, 0, -1,
167 &accept, sizeof(accept));
168 imsg_flush(netibuf);
170 if (accept) {
171 /*
172 * trust the certificate for this session only. If
173 * the page results in a redirect while we're asking
174 * the user to save, we'll end up with an invalid
175 * tabid (one request == one tab id) and crash. It
176 * also makes sense to save it for the current session
177 * if the user accepted it.
178 */
179 tofu_temp_trust(&certs, tab->uri.host, tab->uri.port, tab->cert);
181 ui_yornp("Save the new certificate?",
182 handle_maybe_save_new_cert, tab);
183 } else {
184 free(tab->cert);
185 tab->cert = NULL;
189 static void
190 handle_maybe_save_new_cert(int accept, struct tab *tab)
192 struct tofu_entry *e;
193 const char *host, *port;
195 if (tab->proxy != NULL) {
196 host = tab->proxy->host;
197 port = tab->proxy->port;
198 } else {
199 host = tab->uri.host;
200 port = tab->uri.port;
203 if (!accept)
204 goto end;
206 if ((e = calloc(1, sizeof(*e))) == NULL)
207 die();
209 strlcpy(e->domain, host, sizeof(e->domain));
210 if (*port != '\0' && strcmp(port, "1965")) {
211 strlcat(e->domain, ":", sizeof(e->domain));
212 strlcat(e->domain, port, sizeof(e->domain));
214 strlcpy(e->hash, tab->cert, sizeof(e->hash));
215 imsg_compose(fsibuf, IMSG_UPDATE_CERT, 0, 0, -1, e, sizeof(*e));
216 imsg_flush(fsibuf);
218 tofu_update(&certs, e);
220 tab->trust = TS_TRUSTED;
222 end:
223 free(tab->cert);
224 tab->cert = NULL;
227 static inline int
228 normalize_code(int n)
230 if (n < 20) {
231 if (n == 10 || n == 11)
232 return n;
233 return 10;
234 } else if (n < 30) {
235 return 20;
236 } else if (n < 40) {
237 if (n == 30 || n == 31)
238 return n;
239 return 30;
240 } else if (n < 50) {
241 if (n <= 44)
242 return n;
243 return 40;
244 } else if (n < 60) {
245 if (n <= 53 || n == 59)
246 return n;
247 return 50;
248 } else if (n < 70) {
249 if (n <= 62)
250 return n;
251 return 60;
252 } else
253 return MALFORMED_RESPONSE;
256 static void
257 handle_imsg_got_code(struct imsg *imsg, size_t datalen)
259 struct tab *tab;
261 tab = tab_by_id(imsg->hdr.peerid);
263 if (sizeof(tab->code) != datalen)
264 die();
266 memcpy(&tab->code, imsg->data, sizeof(tab->code));
267 tab->code = normalize_code(tab->code);
268 if (tab->code != 30 && tab->code != 31)
269 tab->redirect_count = 0;
272 static void
273 handle_imsg_got_meta(struct imsg *imsg, size_t datalen)
275 struct tab *tab;
277 tab = tab_by_id(imsg->hdr.peerid);
279 if (sizeof(tab->meta) <= datalen)
280 die();
282 memcpy(tab->meta, imsg->data, datalen);
284 if (tab->code < 10) { /* internal errors */
285 load_page_from_str(tab, err_pages[tab->code]);
286 } else if (tab->code < 20) { /* 1x */
287 load_page_from_str(tab, err_pages[tab->code]);
288 ui_require_input(tab, tab->code == 11);
289 } else if (tab->code == 20) {
290 if (setup_parser_for(tab)) {
291 imsg_compose(netibuf, IMSG_PROCEED, tab->id, 0, -1, NULL, 0);
292 imsg_flush(netibuf);
293 } else {
294 load_page_from_str(tab, err_pages[UNKNOWN_TYPE_OR_CSET]);
295 ui_yornp("Can't display page, wanna save?",
296 handle_maybe_save_page, tab);
298 } else if (tab->code < 40) { /* 3x */
299 tab->redirect_count++;
301 /* TODO: make customizable? */
302 if (tab->redirect_count > 5) {
303 load_page_from_str(tab,
304 err_pages[TOO_MUCH_REDIRECTS]);
305 } else
306 do_load_url(tab, tab->meta);
307 } else { /* 4x, 5x & 6x */
308 load_page_from_str(tab, err_pages[tab->code]);
312 static void
313 handle_maybe_save_page(int dosave, struct tab *tab)
315 if (dosave)
316 ui_read("Save to path", handle_save_page_path, tab->id);
317 else
318 stop_tab(tab);
321 static void
322 handle_save_page_path(const char *path, unsigned int tabid)
324 struct tab *tab;
326 if (path == NULL) {
327 stop_tab(tab_by_id(tabid));
328 return;
331 tab = tab_by_id(tabid);
332 tab->path = strdup(path);
334 imsg_compose(fsibuf, IMSG_FILE_OPEN, tabid, 0, -1, path, strlen(path)+1);
335 imsg_flush(fsibuf);
338 static void
339 handle_imsg_file_opened(struct imsg *imsg, size_t datalen)
341 struct tab *tab;
342 char *page;
343 const char *e;
344 int l;
346 tab = tab_by_id(imsg->hdr.peerid);
348 if (imsg->fd == -1) {
349 stop_tab(tab);
351 e = imsg->data;
352 if (e[datalen-1] != '\0')
353 die();
354 l = asprintf(&page, "# Can't open file\n\n> %s: %s\n",
355 tab->path, e);
356 if (l == -1)
357 die();
358 load_page_from_str(tab, page);
359 free(page);
360 } else {
361 tab->fd = imsg->fd;
362 imsg_compose(netibuf, IMSG_PROCEED, tab->id, 0, -1, NULL, 0);
363 imsg_flush(netibuf);
367 static void
368 handle_imsg_buf(struct imsg *imsg, size_t datalen)
370 struct tab *tab;
371 int l;
372 char *page, buf[FMT_SCALED_STRSIZE] = {0};
374 tab = tab_by_id(imsg->hdr.peerid);
376 tab->bytes += datalen;
377 if (tab->fd == -1) {
378 if (!tab->buffer.page.parse(&tab->buffer.page,
379 imsg->data, datalen))
380 die();
381 } else {
382 write(tab->fd, imsg->data, datalen);
383 fmt_scaled(tab->bytes, buf);
384 l = asprintf(&page, "Saving to \"%s\"... (%s)\n",
385 tab->path,
386 buf);
387 if (l == -1)
388 die();
389 load_page_from_str(tab, page);
390 free(page);
393 ui_on_tab_refresh(tab);
396 static void
397 handle_imsg_eof(struct imsg *imsg, size_t datalen)
399 struct tab *tab;
400 int l;
401 char *page, buf[FMT_SCALED_STRSIZE] = {0};
403 tab = tab_by_id(imsg->hdr.peerid);
405 if (tab->fd == -1) {
406 if (!tab->buffer.page.free(&tab->buffer.page))
407 die();
408 } else {
409 fmt_scaled(tab->bytes, buf);
410 l = asprintf(&page, "Saved to \"%s\" (%s)\n",
411 tab->path,
412 buf);
413 if (l == -1)
414 die();
415 load_page_from_str(tab, page);
416 free(page);
418 close(tab->fd);
419 tab->fd = -1;
420 free(tab->path);
421 tab->path = NULL;
424 ui_on_tab_refresh(tab);
425 ui_on_tab_loaded(tab);
428 static void
429 handle_imsg_bookmark_ok(struct imsg *imsg, size_t datalen)
431 int res;
433 if (datalen != sizeof(res))
434 die();
436 memcpy(&res, imsg->data, sizeof(res));
437 if (res == 0)
438 message("Added to bookmarks!");
439 else
440 message("Failed to add to bookmarks: %s",
441 strerror(res));
444 static void
445 handle_imsg_save_cert_ok(struct imsg *imsg, size_t datalen)
447 int res;
449 if (datalen != sizeof(res))
450 die();
451 memcpy(&res, imsg->data, datalen);
452 if (res != 0)
453 message("Failed to save the cert for: %s",
454 strerror(res));
457 static void
458 handle_imsg_update_cert_ok(struct imsg *imsg, size_t datalen)
460 int res;
462 if (datalen != sizeof(res))
463 die();
464 memcpy(&res, imsg->data, datalen);
465 if (!res)
466 message("Failed to update the certificate");
469 static void
470 handle_dispatch_imsg(int fd, short ev, void *d)
472 struct imsgbuf *ibuf = d;
473 dispatch_imsg(ibuf, handlers, sizeof(handlers));
476 static void
477 load_page_from_str(struct tab *tab, const char *page)
479 erase_buffer(&tab->buffer);
480 gemtext_initparser(&tab->buffer.page);
481 if (!tab->buffer.page.parse(&tab->buffer.page, page, strlen(page)))
482 die();
483 if (!tab->buffer.page.free(&tab->buffer.page))
484 die();
485 ui_on_tab_refresh(tab);
486 ui_on_tab_loaded(tab);
489 void
490 load_about_url(struct tab *tab, const char *url)
492 tab->trust = TS_VERIFIED;
494 gemtext_initparser(&tab->buffer.page);
496 imsg_compose(fsibuf, IMSG_GET, tab->id, 0, -1,
497 tab->hist_cur->h, strlen(tab->hist_cur->h)+1);
498 imsg_flush(fsibuf);
501 void
502 load_gemini_url(struct tab *tab, const char *url)
504 struct get_req req;
506 stop_tab(tab);
507 tab->id = tab_new_id();
509 memset(&req, 0, sizeof(req));
510 strlcpy(req.host, tab->uri.host, sizeof(req.host));
511 strlcpy(req.port, tab->uri.port, sizeof(req.host));
513 strlcpy(req.req, tab->hist_cur->h, sizeof(req.req));
514 strlcat(req.req, "\r\n", sizeof(req.req));
516 req.proto = PROTO_GEMINI;
518 imsg_compose(netibuf, IMSG_GET_RAW, tab->id, 0, -1,
519 &req, sizeof(req));
520 imsg_flush(netibuf);
523 void
524 load_via_proxy(struct tab *tab, const char *url, struct proxy *p)
526 struct get_req req;
528 stop_tab(tab);
529 tab->id = tab_new_id();
530 tab->proxy = p;
532 memset(&req, 0, sizeof(req));
533 strlcpy(req.host, p->host, sizeof(req.host));
534 strlcpy(req.port, p->port, sizeof(req.host));
536 strlcpy(req.req, tab->hist_cur->h, sizeof(req.req));
537 strlcat(req.req, "\r\n", sizeof(req.req));
539 req.proto = p->proto;
541 imsg_compose(netibuf, IMSG_GET_RAW, tab->id, 0, -1,
542 &req, sizeof(req));
543 imsg_flush(netibuf);
546 static void
547 do_load_url(struct tab *tab, const char *url)
549 struct phos_uri uri;
550 struct proto *p;
551 struct proxy *proxy;
552 char *t;
554 if (tab->fd != -1) {
555 close(tab->fd);
556 tab->fd = -1;
557 free(tab->path);
558 tab->path = NULL;
561 tab->trust = TS_UNKNOWN;
563 memcpy(&uri, &tab->uri, sizeof(tab->uri));
564 if (!phos_resolve_uri_from_str(&uri, url, &tab->uri)) {
565 if (asprintf(&t, "#error loading %s\n>%s\n",
566 url, "Can't parse the URI") == -1)
567 die();
568 strlcpy(tab->hist_cur->h, url, sizeof(tab->hist_cur->h));
569 load_page_from_str(tab, t);
570 free(t);
571 return;
574 phos_serialize_uri(&tab->uri, tab->hist_cur->h,
575 sizeof(tab->hist_cur->h));
577 for (p = protos; p->schema != NULL; ++p) {
578 if (!strcmp(tab->uri.scheme, p->schema)) {
579 p->loadfn(tab, url);
580 return;
584 TAILQ_FOREACH(proxy, &proxies, proxies) {
585 if (!strcmp(tab->uri.scheme, proxy->match_proto)) {
586 load_via_proxy(tab, url, proxy);
587 return;
591 protos[0].loadfn(tab, url);
594 void
595 load_url(struct tab *tab, const char *url)
597 if (tab->hist_cur != NULL)
598 hist_clear_forward(&tab->hist, TAILQ_NEXT(tab->hist_cur, entries));
600 if ((tab->hist_cur = calloc(1, sizeof(*tab->hist_cur))) == NULL) {
601 event_loopbreak();
602 return;
605 tab->proxy = NULL;
607 hist_push(&tab->hist, tab->hist_cur);
608 do_load_url(tab, url);
609 erase_buffer(&tab->buffer);
612 int
613 load_previous_page(struct tab *tab)
615 struct hist *h;
617 if ((h = TAILQ_PREV(tab->hist_cur, mhisthead, entries)) == NULL)
618 return 0;
619 tab->hist_cur = h;
620 do_load_url(tab, h->h);
621 return 1;
624 int
625 load_next_page(struct tab *tab)
627 struct hist *h;
629 if ((h = TAILQ_NEXT(tab->hist_cur, entries)) == NULL)
630 return 0;
631 tab->hist_cur = h;
632 do_load_url(tab, h->h);
633 return 1;
636 void
637 stop_tab(struct tab *tab)
639 imsg_compose(netibuf, IMSG_STOP, tab->id, 0, -1, NULL, 0);
640 imsg_flush(netibuf);
642 if (tab->fd != -1) {
643 close(tab->fd);
644 tab->fd = -1;
645 free(tab->path);
646 tab->path = NULL;
647 load_page_from_str(tab, "Stopped.\n");
651 void
652 add_to_bookmarks(const char *str)
654 imsg_compose(fsibuf, IMSG_BOOKMARK_PAGE, 0, 0, -1, str, strlen(str)+1);
655 imsg_flush(fsibuf);
658 void
659 save_session(void)
661 struct tab *tab;
663 imsg_compose(fsibuf, IMSG_SESSION_START, 0, 0, -1, NULL, 0);
664 imsg_flush(fsibuf);
666 TAILQ_FOREACH(tab, &tabshead, tabs) {
667 imsg_compose(fsibuf, IMSG_SESSION_TAB, 0, 0, -1,
668 tab->hist_cur->h, strlen(tab->hist_cur->h)+1);
669 imsg_flush(fsibuf);
672 imsg_compose(fsibuf, IMSG_SESSION_END, 0, 0, -1, NULL, 0);
673 imsg_flush(fsibuf);
676 int
677 main(int argc, char * const *argv)
679 struct imsgbuf network_ibuf, fs_ibuf;
680 int net_fds[2], fs_fds[2];
682 signal(SIGCHLD, SIG_IGN);
683 signal(SIGPIPE, SIG_IGN);
685 /* initialize part of the fs layer. Before starting the UI
686 * and dropping the priviledges we need to read some stuff. */
687 fs_init();
689 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, fs_fds) == -1)
690 err(1, "socketpair");
692 switch (fork()) {
693 case -1:
694 err(1, "fork");
695 case 0:
696 /* child */
697 setproctitle("fs");
698 close(fs_fds[0]);
699 imsg_init(&fs_ibuf, fs_fds[1]);
700 exit(fs_main(&fs_ibuf));
701 default:
702 close(fs_fds[1]);
703 imsg_init(&fs_ibuf, fs_fds[0]);
704 fsibuf = &fs_ibuf;
707 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, net_fds) == -1)
708 err(1, "socketpair");
710 switch (fork()) {
711 case -1:
712 err(1, "fork");
713 case 0:
714 /* child */
715 setproctitle("client");
716 close(net_fds[0]);
717 close(fs_fds[0]);
718 imsg_init(&network_ibuf, net_fds[1]);
719 exit(client_main(&network_ibuf));
720 default:
721 close(net_fds[1]);
722 imsg_init(&network_ibuf, net_fds[0]);
723 netibuf = &network_ibuf;
726 setproctitle("ui");
728 tofu_init(&certs, 5, offsetof(struct tofu_entry, domain));
729 load_certs(&certs);
731 TAILQ_INIT(&tabshead);
732 TAILQ_INIT(&proxies);
734 event_init();
736 event_set(&netev, netibuf->fd, EV_READ | EV_PERSIST,
737 handle_dispatch_imsg, netibuf);
738 event_add(&netev, NULL);
740 event_set(&fsev, fsibuf->fd, EV_READ | EV_PERSIST,
741 handle_dispatch_imsg, fsibuf);
742 event_add(&fsev, NULL);
744 if (ui_init(argc, argv)) {
745 sandbox_ui_process();
746 event_dispatch();
747 ui_end();
750 imsg_compose(netibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
751 imsg_flush(netibuf);
753 imsg_compose(fsibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
754 imsg_flush(fsibuf);
756 return 0;