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, unsigned int);
30 static void handle_maybe_save_new_cert(int, unsigned int);
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, unsigned int);
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 tab->trust = e->verified ? TS_VERIFIED : TS_TRUSTED;
144 imsg_compose(netibuf, IMSG_CERT_STATUS, imsg->hdr.peerid, 0, -1,
145 &tofu_res, sizeof(tofu_res));
146 imsg_flush(netibuf);
147 } else {
148 tab->trust = TS_UNTRUSTED;
149 load_page_from_str(tab, "# Certificate mismatch\n");
150 if ((tab->cert = strdup(hash)) == NULL)
151 die();
152 ui_yornp("Certificate mismatch. Proceed?",
153 handle_check_cert_user_choice, tab->id);
157 static void
158 handle_check_cert_user_choice(int accept, unsigned int tabid)
160 struct tab *tab;
162 tab = tab_by_id(tabid);
164 imsg_compose(netibuf, IMSG_CERT_STATUS, tabid, 0, -1,
165 &accept, sizeof(accept));
166 imsg_flush(netibuf);
168 if (accept)
169 ui_yornp("Save the new certificate?",
170 handle_maybe_save_new_cert, tabid);
171 else {
172 free(tab->cert);
173 tab->cert = NULL;
177 static void
178 handle_maybe_save_new_cert(int accept, unsigned int tabid)
180 struct tab *tab;
181 struct tofu_entry *e;
182 const char *host, *port;
184 tab = tab_by_id(tabid);
186 if (tab->proxy != NULL) {
187 host = tab->proxy->host;
188 port = tab->proxy->port;
189 } else {
190 host = tab->uri.host;
191 port = tab->uri.port;
194 if (!accept)
195 goto end;
197 if ((e = calloc(1, sizeof(*e))) == NULL)
198 die();
200 strlcpy(e->domain, host, sizeof(e->domain));
201 if (*port != '\0' && strcmp(port, "1965")) {
202 strlcat(e->domain, ":", sizeof(e->domain));
203 strlcat(e->domain, port, sizeof(e->domain));
205 strlcpy(e->hash, tab->cert, sizeof(e->hash));
206 imsg_compose(fsibuf, IMSG_UPDATE_CERT, 0, 0, -1, e, sizeof(*e));
207 imsg_flush(fsibuf);
209 tofu_update(&certs, e);
211 tab->trust = TS_TRUSTED;
213 end:
214 free(tab->cert);
215 tab->cert = NULL;
218 static inline int
219 normalize_code(int n)
221 if (n < 20) {
222 if (n == 10 || n == 11)
223 return n;
224 return 10;
225 } else if (n < 30) {
226 return 20;
227 } else if (n < 40) {
228 if (n == 30 || n == 31)
229 return n;
230 return 30;
231 } else if (n < 50) {
232 if (n <= 44)
233 return n;
234 return 40;
235 } else if (n < 60) {
236 if (n <= 53 || n == 59)
237 return n;
238 return 50;
239 } else if (n < 70) {
240 if (n <= 62)
241 return n;
242 return 60;
243 } else
244 return MALFORMED_RESPONSE;
247 static void
248 handle_imsg_got_code(struct imsg *imsg, size_t datalen)
250 struct tab *tab;
252 tab = tab_by_id(imsg->hdr.peerid);
254 if (sizeof(tab->code) != datalen)
255 die();
257 memcpy(&tab->code, imsg->data, sizeof(tab->code));
258 tab->code = normalize_code(tab->code);
259 if (tab->code != 30 && tab->code != 31)
260 tab->redirect_count = 0;
263 static void
264 handle_imsg_got_meta(struct imsg *imsg, size_t datalen)
266 struct tab *tab;
268 tab = tab_by_id(imsg->hdr.peerid);
270 if (sizeof(tab->meta) <= datalen)
271 die();
273 memcpy(tab->meta, imsg->data, datalen);
275 if (tab->code < 10) { /* internal errors */
276 load_page_from_str(tab, err_pages[tab->code]);
277 } else if (tab->code < 20) { /* 1x */
278 load_page_from_str(tab, err_pages[tab->code]);
279 ui_require_input(tab, tab->code == 11);
280 } else if (tab->code == 20) {
281 if (setup_parser_for(tab)) {
282 imsg_compose(netibuf, IMSG_PROCEED, tab->id, 0, -1, NULL, 0);
283 imsg_flush(netibuf);
284 } else {
285 load_page_from_str(tab, err_pages[UNKNOWN_TYPE_OR_CSET]);
286 ui_yornp("Can't display page, wanna save?",
287 handle_maybe_save_page, tab->id);
289 } else if (tab->code < 40) { /* 3x */
290 tab->redirect_count++;
292 /* TODO: make customizable? */
293 if (tab->redirect_count > 5) {
294 load_page_from_str(tab,
295 err_pages[TOO_MUCH_REDIRECTS]);
296 } else
297 do_load_url(tab, tab->meta);
298 } else { /* 4x, 5x & 6x */
299 load_page_from_str(tab, err_pages[tab->code]);
303 static void
304 handle_maybe_save_page(int dosave, unsigned int tabid)
306 if (dosave)
307 ui_read("Save to path", handle_save_page_path, tabid);
308 else
309 stop_tab(tab_by_id(tabid));
312 static void
313 handle_save_page_path(const char *path, unsigned int tabid)
315 struct tab *tab;
317 if (path == NULL) {
318 stop_tab(tab_by_id(tabid));
319 return;
322 tab = tab_by_id(tabid);
323 tab->path = strdup(path);
325 imsg_compose(fsibuf, IMSG_FILE_OPEN, tabid, 0, -1, path, strlen(path)+1);
326 imsg_flush(fsibuf);
329 static void
330 handle_imsg_file_opened(struct imsg *imsg, size_t datalen)
332 struct tab *tab;
333 char *page;
334 const char *e;
335 int l;
337 tab = tab_by_id(imsg->hdr.peerid);
339 if (imsg->fd == -1) {
340 stop_tab(tab);
342 e = imsg->data;
343 if (e[datalen-1] != '\0')
344 die();
345 l = asprintf(&page, "# Can't open file\n\n> %s: %s\n",
346 tab->path, e);
347 if (l == -1)
348 die();
349 load_page_from_str(tab, page);
350 free(page);
351 } else {
352 tab->fd = imsg->fd;
353 imsg_compose(netibuf, IMSG_PROCEED, tab->id, 0, -1, NULL, 0);
354 imsg_flush(netibuf);
358 static void
359 handle_imsg_buf(struct imsg *imsg, size_t datalen)
361 struct tab *tab;
362 int l;
363 char *page, buf[FMT_SCALED_STRSIZE] = {0};
365 tab = tab_by_id(imsg->hdr.peerid);
367 tab->bytes += datalen;
368 if (tab->fd == -1) {
369 if (!tab->buffer.page.parse(&tab->buffer.page,
370 imsg->data, datalen))
371 die();
372 } else {
373 write(tab->fd, imsg->data, datalen);
374 fmt_scaled(tab->bytes, buf);
375 l = asprintf(&page, "Saving to \"%s\"... (%s)\n",
376 tab->path,
377 buf);
378 if (l == -1)
379 die();
380 load_page_from_str(tab, page);
381 free(page);
384 ui_on_tab_refresh(tab);
387 static void
388 handle_imsg_eof(struct imsg *imsg, size_t datalen)
390 struct tab *tab;
391 int l;
392 char *page, buf[FMT_SCALED_STRSIZE] = {0};
394 tab = tab_by_id(imsg->hdr.peerid);
396 if (tab->fd == -1) {
397 if (!tab->buffer.page.free(&tab->buffer.page))
398 die();
399 } else {
400 fmt_scaled(tab->bytes, buf);
401 l = asprintf(&page, "Saved to \"%s\" (%s)\n",
402 tab->path,
403 buf);
404 if (l == -1)
405 die();
406 load_page_from_str(tab, page);
407 free(page);
409 close(tab->fd);
410 tab->fd = -1;
411 free(tab->path);
412 tab->path = NULL;
415 ui_on_tab_refresh(tab);
416 ui_on_tab_loaded(tab);
419 static void
420 handle_imsg_bookmark_ok(struct imsg *imsg, size_t datalen)
422 int res;
424 if (datalen != sizeof(res))
425 die();
427 memcpy(&res, imsg->data, sizeof(res));
428 if (res == 0)
429 message("Added to bookmarks!");
430 else
431 message("Failed to add to bookmarks: %s",
432 strerror(res));
435 static void
436 handle_imsg_save_cert_ok(struct imsg *imsg, size_t datalen)
438 int res;
440 if (datalen != sizeof(res))
441 die();
442 memcpy(&res, imsg->data, datalen);
443 if (res != 0)
444 message("Failed to save the cert for: %s",
445 strerror(res));
448 static void
449 handle_imsg_update_cert_ok(struct imsg *imsg, size_t datalen)
451 int res;
453 if (datalen != sizeof(res))
454 die();
455 memcpy(&res, imsg->data, datalen);
456 if (!res)
457 message("Failed to update the certificate");
460 static void
461 handle_dispatch_imsg(int fd, short ev, void *d)
463 struct imsgbuf *ibuf = d;
464 dispatch_imsg(ibuf, handlers, sizeof(handlers));
467 static void
468 load_page_from_str(struct tab *tab, const char *page)
470 gemtext_initparser(&tab->buffer.page);
471 if (!tab->buffer.page.parse(&tab->buffer.page, page, strlen(page)))
472 die();
473 if (!tab->buffer.page.free(&tab->buffer.page))
474 die();
475 ui_on_tab_refresh(tab);
476 ui_on_tab_loaded(tab);
479 void
480 load_about_url(struct tab *tab, const char *url)
482 tab->trust = TS_VERIFIED;
484 gemtext_initparser(&tab->buffer.page);
486 imsg_compose(fsibuf, IMSG_GET, tab->id, 0, -1,
487 tab->hist_cur->h, strlen(tab->hist_cur->h)+1);
488 imsg_flush(fsibuf);
491 void
492 load_gemini_url(struct tab *tab, const char *url)
494 struct get_req req;
496 stop_tab(tab);
497 tab->id = tab_new_id();
499 memset(&req, 0, sizeof(req));
500 strlcpy(req.host, tab->uri.host, sizeof(req.host));
501 strlcpy(req.port, tab->uri.port, sizeof(req.host));
503 strlcpy(req.req, tab->hist_cur->h, sizeof(req.req));
504 strlcat(req.req, "\r\n", sizeof(req.req));
506 req.proto = PROTO_GEMINI;
508 imsg_compose(netibuf, IMSG_GET_RAW, tab->id, 0, -1,
509 &req, sizeof(req));
510 imsg_flush(netibuf);
513 void
514 load_via_proxy(struct tab *tab, const char *url, struct proxy *p)
516 struct get_req req;
518 stop_tab(tab);
519 tab->id = tab_new_id();
520 tab->proxy = p;
522 memset(&req, 0, sizeof(req));
523 strlcpy(req.host, p->host, sizeof(req.host));
524 strlcpy(req.port, p->port, sizeof(req.host));
526 strlcpy(req.req, tab->hist_cur->h, sizeof(req.req));
527 strlcat(req.req, "\r\n", sizeof(req.req));
529 req.proto = p->proto;
531 imsg_compose(netibuf, IMSG_GET_RAW, tab->id, 0, -1,
532 &req, sizeof(req));
533 imsg_flush(netibuf);
536 static void
537 do_load_url(struct tab *tab, const char *url)
539 struct phos_uri uri;
540 struct proto *p;
541 struct proxy *proxy;
542 char *t;
544 if (tab->fd != -1) {
545 close(tab->fd);
546 tab->fd = -1;
547 free(tab->path);
548 tab->path = NULL;
551 tab->trust = TS_UNKNOWN;
553 memcpy(&uri, &tab->uri, sizeof(tab->uri));
554 if (!phos_resolve_uri_from_str(&uri, url, &tab->uri)) {
555 if (asprintf(&t, "#error loading %s\n>%s\n",
556 url, "Can't parse the URI") == -1)
557 die();
558 strlcpy(tab->hist_cur->h, url, sizeof(tab->hist_cur->h));
559 load_page_from_str(tab, t);
560 free(t);
561 return;
564 phos_serialize_uri(&tab->uri, tab->hist_cur->h,
565 sizeof(tab->hist_cur->h));
567 for (p = protos; p->schema != NULL; ++p) {
568 if (!strcmp(tab->uri.scheme, p->schema)) {
569 p->loadfn(tab, url);
570 return;
574 TAILQ_FOREACH(proxy, &proxies, proxies) {
575 if (!strcmp(tab->uri.scheme, proxy->match_proto)) {
576 load_via_proxy(tab, url, proxy);
577 return;
581 protos[0].loadfn(tab, url);
584 void
585 load_url(struct tab *tab, const char *url)
587 if (tab->hist_cur != NULL)
588 hist_clear_forward(&tab->hist, TAILQ_NEXT(tab->hist_cur, entries));
590 if ((tab->hist_cur = calloc(1, sizeof(*tab->hist_cur))) == NULL) {
591 event_loopbreak();
592 return;
595 tab->proxy = NULL;
597 hist_push(&tab->hist, tab->hist_cur);
598 do_load_url(tab, url);
599 empty_vlist(&tab->buffer);
600 empty_linelist(&tab->buffer);
603 int
604 load_previous_page(struct tab *tab)
606 struct hist *h;
608 if ((h = TAILQ_PREV(tab->hist_cur, mhisthead, entries)) == NULL)
609 return 0;
610 tab->hist_cur = h;
611 do_load_url(tab, h->h);
612 return 1;
615 int
616 load_next_page(struct tab *tab)
618 struct hist *h;
620 if ((h = TAILQ_NEXT(tab->hist_cur, entries)) == NULL)
621 return 0;
622 tab->hist_cur = h;
623 do_load_url(tab, h->h);
624 return 1;
627 void
628 stop_tab(struct tab *tab)
630 imsg_compose(netibuf, IMSG_STOP, tab->id, 0, -1, NULL, 0);
631 imsg_flush(netibuf);
633 if (tab->fd != -1) {
634 close(tab->fd);
635 tab->fd = -1;
636 free(tab->path);
637 tab->path = NULL;
638 load_page_from_str(tab, "Stopped.\n");
642 void
643 add_to_bookmarks(const char *str)
645 imsg_compose(fsibuf, IMSG_BOOKMARK_PAGE, 0, 0, -1, str, strlen(str)+1);
646 imsg_flush(fsibuf);
649 void
650 save_session(void)
652 struct tab *tab;
654 imsg_compose(fsibuf, IMSG_SESSION_START, 0, 0, -1, NULL, 0);
655 imsg_flush(fsibuf);
657 TAILQ_FOREACH(tab, &tabshead, tabs) {
658 imsg_compose(fsibuf, IMSG_SESSION_TAB, 0, 0, -1,
659 tab->hist_cur->h, strlen(tab->hist_cur->h)+1);
660 imsg_flush(fsibuf);
663 imsg_compose(fsibuf, IMSG_SESSION_END, 0, 0, -1, NULL, 0);
664 imsg_flush(fsibuf);
667 int
668 main(int argc, char * const *argv)
670 struct imsgbuf network_ibuf, fs_ibuf;
671 int net_fds[2], fs_fds[2];
673 signal(SIGCHLD, SIG_IGN);
674 signal(SIGPIPE, SIG_IGN);
676 /* initialize part of the fs layer. Before starting the UI
677 * and dropping the priviledges we need to read some stuff. */
678 fs_init();
680 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, fs_fds) == -1)
681 err(1, "socketpair");
683 switch (fork()) {
684 case -1:
685 err(1, "fork");
686 case 0:
687 /* child */
688 setproctitle("fs");
689 close(fs_fds[0]);
690 imsg_init(&fs_ibuf, fs_fds[1]);
691 exit(fs_main(&fs_ibuf));
692 default:
693 close(fs_fds[1]);
694 imsg_init(&fs_ibuf, fs_fds[0]);
695 fsibuf = &fs_ibuf;
698 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, net_fds) == -1)
699 err(1, "socketpair");
701 switch (fork()) {
702 case -1:
703 err(1, "fork");
704 case 0:
705 /* child */
706 setproctitle("client");
707 close(net_fds[0]);
708 close(fs_fds[0]);
709 imsg_init(&network_ibuf, net_fds[1]);
710 exit(client_main(&network_ibuf));
711 default:
712 close(net_fds[1]);
713 imsg_init(&network_ibuf, net_fds[0]);
714 netibuf = &network_ibuf;
717 setproctitle("ui");
719 tofu_init(&certs, 5, offsetof(struct tofu_entry, domain));
720 load_certs(&certs);
722 TAILQ_INIT(&tabshead);
723 TAILQ_INIT(&proxies);
725 event_init();
727 event_set(&netev, netibuf->fd, EV_READ | EV_PERSIST,
728 handle_dispatch_imsg, netibuf);
729 event_add(&netev, NULL);
731 event_set(&fsev, fsibuf->fd, EV_READ | EV_PERSIST,
732 handle_dispatch_imsg, fsibuf);
733 event_add(&fsev, NULL);
735 if (ui_init(argc, argv)) {
736 sandbox_ui_process();
737 event_dispatch();
738 ui_end();
741 imsg_compose(netibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
742 imsg_flush(netibuf);
744 imsg_compose(fsibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
745 imsg_flush(fsibuf);
747 return 0;