Blob


1 #include "telescope.h"
3 #include <sys/socket.h>
5 #include <errno.h>
6 #include <limits.h>
7 #include <signal.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <unistd.h>
13 struct event netev, fsev;
14 struct tabshead tabshead;
15 struct proxylist proxies;
17 /* the first is also the fallback one */
18 static struct proto protos[] = {
19 { "gemini", load_gemini_url },
20 { "about", load_about_url },
21 { NULL, NULL },
22 };
24 static struct imsgbuf *netibuf, *fsibuf;
26 static void die(void) __attribute__((__noreturn__));
27 static struct tab *tab_by_id(uint32_t);
28 static void handle_imsg_err(struct imsg*, size_t);
29 static void handle_imsg_check_cert(struct imsg*, size_t);
30 static void handle_check_cert_user_choice(int, struct tab *);
31 static void handle_maybe_save_new_cert(int, struct tab *);
32 static void handle_imsg_got_code(struct imsg*, size_t);
33 static void handle_imsg_got_meta(struct imsg*, size_t);
34 static void handle_maybe_save_page(int, struct tab *);
35 static void handle_save_page_path(const char *, unsigned int);
36 static void handle_imsg_file_opened(struct imsg*, size_t);
37 static void handle_imsg_buf(struct imsg*, size_t);
38 static void handle_imsg_eof(struct imsg*, size_t);
39 static void handle_imsg_bookmark_ok(struct imsg*, size_t);
40 static void handle_imsg_save_cert_ok(struct imsg*, size_t);
41 static void handle_imsg_update_cert_ok(struct imsg *, size_t);
42 static void handle_dispatch_imsg(int, short, void*);
43 static void load_page_from_str(struct tab*, const char*);
44 static void do_load_url(struct tab*, const char*);
46 static imsg_handlerfn *handlers[] = {
47 [IMSG_ERR] = handle_imsg_err,
48 [IMSG_CHECK_CERT] = handle_imsg_check_cert,
49 [IMSG_GOT_CODE] = handle_imsg_got_code,
50 [IMSG_GOT_META] = handle_imsg_got_meta,
51 [IMSG_BUF] = handle_imsg_buf,
52 [IMSG_EOF] = handle_imsg_eof,
53 [IMSG_BOOKMARK_OK] = handle_imsg_bookmark_ok,
54 [IMSG_SAVE_CERT_OK] = handle_imsg_save_cert_ok,
55 [IMSG_UPDATE_CERT_OK] = handle_imsg_update_cert_ok,
56 [IMSG_FILE_OPENED] = handle_imsg_file_opened,
57 };
59 static struct ohash certs;
61 static void __attribute__((__noreturn__))
62 die(void)
63 {
64 abort(); /* TODO */
65 }
67 static struct tab *
68 tab_by_id(uint32_t id)
69 {
70 struct tab *t;
72 TAILQ_FOREACH(t, &tabshead, tabs) {
73 if (t->id == id)
74 return t;
75 }
77 die();
78 }
80 static void
81 handle_imsg_err(struct imsg *imsg, size_t datalen)
82 {
83 struct tab *tab;
84 char *page;
86 tab = tab_by_id(imsg->hdr.peerid);
88 page = imsg->data;
89 page[datalen-1] = '\0';
91 if (asprintf(&page, "# Error loading %s\n\n> %s\n",
92 tab->hist_cur->h, page) == -1)
93 die();
94 load_page_from_str(tab, page);
95 free(page);
96 }
98 static void
99 handle_imsg_check_cert(struct imsg *imsg, size_t datalen)
101 const char *hash, *host, *port;
102 int tofu_res;
103 struct tofu_entry *e;
104 struct tab *tab;
106 hash = imsg->data;
107 if (hash[datalen-1] != '\0')
108 abort();
110 tab = tab_by_id(imsg->hdr.peerid);
112 if (tab->proxy != NULL) {
113 host = tab->proxy->host;
114 port = tab->proxy->port;
115 } else {
116 host = tab->uri.host;
117 port = tab->uri.port;
120 if ((e = tofu_lookup(&certs, host, port)) == NULL) {
121 /* TODO: an update in libressl/libretls changed
122 * significantly. Find a better approach at storing
123 * the certs! */
124 if (datalen > sizeof(e->hash))
125 abort();
127 tofu_res = 1; /* trust on first use */
128 if ((e = calloc(1, sizeof(*e))) == NULL)
129 abort();
130 strlcpy(e->domain, host, sizeof(e->domain));
131 if (*port != '\0' && strcmp(port, "1965")) {
132 strlcat(e->domain, ":", sizeof(e->domain));
133 strlcat(e->domain, port, sizeof(e->domain));
135 strlcpy(e->hash, hash, sizeof(e->hash));
136 tofu_add(&certs, e);
137 imsg_compose(fsibuf, IMSG_SAVE_CERT, tab->id, 0, -1,
138 e, sizeof(*e));
139 imsg_flush(fsibuf);
140 } else
141 tofu_res = !strcmp(hash, e->hash);
143 if (tofu_res) {
144 if (e->verified == -1)
145 tab->trust = TS_TEMP_TRUSTED;
146 else if (e->verified == 1)
147 tab->trust = TS_VERIFIED;
148 else
149 tab->trust = TS_TRUSTED;
151 imsg_compose(netibuf, IMSG_CERT_STATUS, imsg->hdr.peerid, 0, -1,
152 &tofu_res, sizeof(tofu_res));
153 imsg_flush(netibuf);
154 } else {
155 tab->trust = TS_UNTRUSTED;
156 load_page_from_str(tab, "# Certificate mismatch\n");
157 if ((tab->cert = strdup(hash)) == NULL)
158 die();
159 ui_yornp("Certificate mismatch. Proceed?",
160 handle_check_cert_user_choice, tab);
164 static void
165 handle_check_cert_user_choice(int accept, struct tab *tab)
167 imsg_compose(netibuf, IMSG_CERT_STATUS, tab->id, 0, -1,
168 &accept, sizeof(accept));
169 imsg_flush(netibuf);
171 if (accept) {
172 /*
173 * trust the certificate for this session only. If
174 * the page results in a redirect while we're asking
175 * the user to save, we'll end up with an invalid
176 * tabid (one request == one tab id) and crash. It
177 * also makes sense to save it for the current session
178 * if the user accepted it.
179 */
180 tofu_temp_trust(&certs, tab->uri.host, tab->uri.port, tab->cert);
182 ui_yornp("Save the new certificate?",
183 handle_maybe_save_new_cert, tab);
184 } else {
185 free(tab->cert);
186 tab->cert = NULL;
190 static void
191 handle_maybe_save_new_cert(int accept, struct tab *tab)
193 struct tofu_entry *e;
194 const char *host, *port;
196 if (tab->proxy != NULL) {
197 host = tab->proxy->host;
198 port = tab->proxy->port;
199 } else {
200 host = tab->uri.host;
201 port = tab->uri.port;
204 if (!accept)
205 goto end;
207 if ((e = calloc(1, sizeof(*e))) == NULL)
208 die();
210 strlcpy(e->domain, host, sizeof(e->domain));
211 if (*port != '\0' && strcmp(port, "1965")) {
212 strlcat(e->domain, ":", sizeof(e->domain));
213 strlcat(e->domain, port, sizeof(e->domain));
215 strlcpy(e->hash, tab->cert, sizeof(e->hash));
216 imsg_compose(fsibuf, IMSG_UPDATE_CERT, 0, 0, -1, e, sizeof(*e));
217 imsg_flush(fsibuf);
219 tofu_update(&certs, e);
221 tab->trust = TS_TRUSTED;
223 end:
224 free(tab->cert);
225 tab->cert = NULL;
228 static inline int
229 normalize_code(int n)
231 if (n < 20) {
232 if (n == 10 || n == 11)
233 return n;
234 return 10;
235 } else if (n < 30) {
236 return 20;
237 } else if (n < 40) {
238 if (n == 30 || n == 31)
239 return n;
240 return 30;
241 } else if (n < 50) {
242 if (n <= 44)
243 return n;
244 return 40;
245 } else if (n < 60) {
246 if (n <= 53 || n == 59)
247 return n;
248 return 50;
249 } else if (n < 70) {
250 if (n <= 62)
251 return n;
252 return 60;
253 } else
254 return MALFORMED_RESPONSE;
257 static void
258 handle_imsg_got_code(struct imsg *imsg, size_t datalen)
260 struct tab *tab;
262 tab = tab_by_id(imsg->hdr.peerid);
264 if (sizeof(tab->code) != datalen)
265 die();
267 memcpy(&tab->code, imsg->data, sizeof(tab->code));
268 tab->code = normalize_code(tab->code);
269 if (tab->code != 30 && tab->code != 31)
270 tab->redirect_count = 0;
273 static void
274 handle_imsg_got_meta(struct imsg *imsg, size_t datalen)
276 struct tab *tab;
278 tab = tab_by_id(imsg->hdr.peerid);
280 if (sizeof(tab->meta) <= datalen)
281 die();
283 memcpy(tab->meta, imsg->data, datalen);
285 if (tab->code < 10) { /* internal errors */
286 load_page_from_str(tab, err_pages[tab->code]);
287 } else if (tab->code < 20) { /* 1x */
288 load_page_from_str(tab, err_pages[tab->code]);
289 ui_require_input(tab, tab->code == 11);
290 } else if (tab->code == 20) {
291 if (setup_parser_for(tab)) {
292 imsg_compose(netibuf, IMSG_PROCEED, tab->id, 0, -1, NULL, 0);
293 imsg_flush(netibuf);
294 } else {
295 load_page_from_str(tab, err_pages[UNKNOWN_TYPE_OR_CSET]);
296 ui_yornp("Can't display page, wanna save?",
297 handle_maybe_save_page, tab);
299 } else if (tab->code < 40) { /* 3x */
300 tab->redirect_count++;
302 /* TODO: make customizable? */
303 if (tab->redirect_count > 5) {
304 load_page_from_str(tab,
305 err_pages[TOO_MUCH_REDIRECTS]);
306 } else
307 do_load_url(tab, tab->meta);
308 } else { /* 4x, 5x & 6x */
309 load_page_from_str(tab, err_pages[tab->code]);
313 static void
314 handle_maybe_save_page(int dosave, struct tab *tab)
316 if (dosave)
317 ui_read("Save to path", handle_save_page_path, tab->id);
318 else
319 stop_tab(tab);
322 static void
323 handle_save_page_path(const char *path, unsigned int tabid)
325 struct tab *tab;
327 if (path == NULL) {
328 stop_tab(tab_by_id(tabid));
329 return;
332 tab = tab_by_id(tabid);
333 tab->path = strdup(path);
335 imsg_compose(fsibuf, IMSG_FILE_OPEN, tabid, 0, -1, path, strlen(path)+1);
336 imsg_flush(fsibuf);
339 static void
340 handle_imsg_file_opened(struct imsg *imsg, size_t datalen)
342 struct tab *tab;
343 char *page;
344 const char *e;
345 int l;
347 tab = tab_by_id(imsg->hdr.peerid);
349 if (imsg->fd == -1) {
350 stop_tab(tab);
352 e = imsg->data;
353 if (e[datalen-1] != '\0')
354 die();
355 l = asprintf(&page, "# Can't open file\n\n> %s: %s\n",
356 tab->path, e);
357 if (l == -1)
358 die();
359 load_page_from_str(tab, page);
360 free(page);
361 } else {
362 tab->fd = imsg->fd;
363 imsg_compose(netibuf, IMSG_PROCEED, tab->id, 0, -1, NULL, 0);
364 imsg_flush(netibuf);
368 static void
369 handle_imsg_buf(struct imsg *imsg, size_t datalen)
371 struct tab *tab;
372 int l;
373 char *page, buf[FMT_SCALED_STRSIZE] = {0};
375 tab = tab_by_id(imsg->hdr.peerid);
377 tab->bytes += datalen;
378 if (tab->fd == -1) {
379 if (!tab->buffer.page.parse(&tab->buffer.page,
380 imsg->data, datalen))
381 die();
382 } else {
383 write(tab->fd, imsg->data, datalen);
384 fmt_scaled(tab->bytes, buf);
385 l = asprintf(&page, "Saving to \"%s\"... (%s)\n",
386 tab->path,
387 buf);
388 if (l == -1)
389 die();
390 load_page_from_str(tab, page);
391 free(page);
394 ui_on_tab_refresh(tab);
397 static void
398 handle_imsg_eof(struct imsg *imsg, size_t datalen)
400 struct tab *tab;
401 int l;
402 char *page, buf[FMT_SCALED_STRSIZE] = {0};
404 tab = tab_by_id(imsg->hdr.peerid);
406 if (tab->fd == -1) {
407 if (!tab->buffer.page.free(&tab->buffer.page))
408 die();
409 } else {
410 fmt_scaled(tab->bytes, buf);
411 l = asprintf(&page, "Saved to \"%s\" (%s)\n",
412 tab->path,
413 buf);
414 if (l == -1)
415 die();
416 load_page_from_str(tab, page);
417 free(page);
419 close(tab->fd);
420 tab->fd = -1;
421 free(tab->path);
422 tab->path = NULL;
425 ui_on_tab_refresh(tab);
426 ui_on_tab_loaded(tab);
429 static void
430 handle_imsg_bookmark_ok(struct imsg *imsg, size_t datalen)
432 int res;
434 if (datalen != sizeof(res))
435 die();
437 memcpy(&res, imsg->data, sizeof(res));
438 if (res == 0)
439 message("Added to bookmarks!");
440 else
441 message("Failed to add to bookmarks: %s",
442 strerror(res));
445 static void
446 handle_imsg_save_cert_ok(struct imsg *imsg, size_t datalen)
448 int res;
450 if (datalen != sizeof(res))
451 die();
452 memcpy(&res, imsg->data, datalen);
453 if (res != 0)
454 message("Failed to save the cert for: %s",
455 strerror(res));
458 static void
459 handle_imsg_update_cert_ok(struct imsg *imsg, size_t datalen)
461 int res;
463 if (datalen != sizeof(res))
464 die();
465 memcpy(&res, imsg->data, datalen);
466 if (!res)
467 message("Failed to update the certificate");
470 static void
471 handle_dispatch_imsg(int fd, short ev, void *d)
473 struct imsgbuf *ibuf = d;
474 dispatch_imsg(ibuf, handlers, sizeof(handlers));
477 static void
478 load_page_from_str(struct tab *tab, const char *page)
480 erase_buffer(&tab->buffer);
481 gemtext_initparser(&tab->buffer.page);
482 if (!tab->buffer.page.parse(&tab->buffer.page, page, strlen(page)))
483 die();
484 if (!tab->buffer.page.free(&tab->buffer.page))
485 die();
486 ui_on_tab_refresh(tab);
487 ui_on_tab_loaded(tab);
490 void
491 load_about_url(struct tab *tab, const char *url)
493 tab->trust = TS_VERIFIED;
495 gemtext_initparser(&tab->buffer.page);
497 imsg_compose(fsibuf, IMSG_GET, tab->id, 0, -1,
498 tab->hist_cur->h, strlen(tab->hist_cur->h)+1);
499 imsg_flush(fsibuf);
502 void
503 load_gemini_url(struct tab *tab, const char *url)
505 struct get_req req;
507 stop_tab(tab);
508 tab->id = tab_new_id();
510 memset(&req, 0, sizeof(req));
511 strlcpy(req.host, tab->uri.host, sizeof(req.host));
512 strlcpy(req.port, tab->uri.port, sizeof(req.host));
514 strlcpy(req.req, tab->hist_cur->h, sizeof(req.req));
515 strlcat(req.req, "\r\n", sizeof(req.req));
517 req.proto = PROTO_GEMINI;
519 imsg_compose(netibuf, IMSG_GET_RAW, tab->id, 0, -1,
520 &req, sizeof(req));
521 imsg_flush(netibuf);
524 void
525 load_via_proxy(struct tab *tab, const char *url, struct proxy *p)
527 struct get_req req;
529 stop_tab(tab);
530 tab->id = tab_new_id();
531 tab->proxy = p;
533 memset(&req, 0, sizeof(req));
534 strlcpy(req.host, p->host, sizeof(req.host));
535 strlcpy(req.port, p->port, sizeof(req.host));
537 strlcpy(req.req, tab->hist_cur->h, sizeof(req.req));
538 strlcat(req.req, "\r\n", sizeof(req.req));
540 req.proto = p->proto;
542 imsg_compose(netibuf, IMSG_GET_RAW, tab->id, 0, -1,
543 &req, sizeof(req));
544 imsg_flush(netibuf);
547 static void
548 do_load_url(struct tab *tab, const char *url)
550 struct phos_uri uri;
551 struct proto *p;
552 struct proxy *proxy;
553 char *t;
555 if (tab->fd != -1) {
556 close(tab->fd);
557 tab->fd = -1;
558 free(tab->path);
559 tab->path = NULL;
562 tab->trust = TS_UNKNOWN;
564 memcpy(&uri, &tab->uri, sizeof(tab->uri));
565 if (!phos_resolve_uri_from_str(&uri, url, &tab->uri)) {
566 if (asprintf(&t, "#error loading %s\n>%s\n",
567 url, "Can't parse the URI") == -1)
568 die();
569 strlcpy(tab->hist_cur->h, url, sizeof(tab->hist_cur->h));
570 load_page_from_str(tab, t);
571 free(t);
572 return;
575 phos_serialize_uri(&tab->uri, tab->hist_cur->h,
576 sizeof(tab->hist_cur->h));
578 for (p = protos; p->schema != NULL; ++p) {
579 if (!strcmp(tab->uri.scheme, p->schema)) {
580 p->loadfn(tab, url);
581 return;
585 TAILQ_FOREACH(proxy, &proxies, proxies) {
586 if (!strcmp(tab->uri.scheme, proxy->match_proto)) {
587 load_via_proxy(tab, url, proxy);
588 return;
592 protos[0].loadfn(tab, url);
595 void
596 load_url(struct tab *tab, const char *url)
598 if (tab->hist_cur != NULL)
599 hist_clear_forward(&tab->hist, TAILQ_NEXT(tab->hist_cur, entries));
601 if ((tab->hist_cur = calloc(1, sizeof(*tab->hist_cur))) == NULL) {
602 event_loopbreak();
603 return;
606 tab->proxy = NULL;
608 hist_push(&tab->hist, tab->hist_cur);
609 do_load_url(tab, url);
610 erase_buffer(&tab->buffer);
613 int
614 load_previous_page(struct tab *tab)
616 struct hist *h;
618 if ((h = TAILQ_PREV(tab->hist_cur, mhisthead, entries)) == NULL)
619 return 0;
620 tab->hist_cur = h;
621 do_load_url(tab, h->h);
622 return 1;
625 int
626 load_next_page(struct tab *tab)
628 struct hist *h;
630 if ((h = TAILQ_NEXT(tab->hist_cur, entries)) == NULL)
631 return 0;
632 tab->hist_cur = h;
633 do_load_url(tab, h->h);
634 return 1;
637 void
638 stop_tab(struct tab *tab)
640 imsg_compose(netibuf, IMSG_STOP, tab->id, 0, -1, NULL, 0);
641 imsg_flush(netibuf);
643 if (tab->fd != -1) {
644 close(tab->fd);
645 tab->fd = -1;
646 free(tab->path);
647 tab->path = NULL;
648 load_page_from_str(tab, "Stopped.\n");
652 void
653 add_to_bookmarks(const char *str)
655 imsg_compose(fsibuf, IMSG_BOOKMARK_PAGE, 0, 0, -1, str, strlen(str)+1);
656 imsg_flush(fsibuf);
659 void
660 save_session(void)
662 struct tab *tab;
664 imsg_compose(fsibuf, IMSG_SESSION_START, 0, 0, -1, NULL, 0);
665 imsg_flush(fsibuf);
667 TAILQ_FOREACH(tab, &tabshead, tabs) {
668 imsg_compose(fsibuf, IMSG_SESSION_TAB, 0, 0, -1,
669 tab->hist_cur->h, strlen(tab->hist_cur->h)+1);
670 imsg_flush(fsibuf);
673 imsg_compose(fsibuf, IMSG_SESSION_END, 0, 0, -1, NULL, 0);
674 imsg_flush(fsibuf);
677 static void
678 session_new_tab_cb(const char *url)
680 new_tab(url);
683 static void
684 usage(void)
686 fprintf(stderr, "USAGE: %s [-hn] [-c config] [url]\n", getprogname());
687 fprintf(stderr, "version: " PACKAGE " " VERSION "\n");
690 int
691 main(int argc, char * const *argv)
693 struct imsgbuf network_ibuf, fs_ibuf;
694 int net_fds[2], fs_fds[2];
695 int ch, configtest = 0, fail = 0;
696 int has_url = 0;
697 char path[PATH_MAX];
698 char *url = NEW_TAB_URL;
700 signal(SIGCHLD, SIG_IGN);
701 signal(SIGPIPE, SIG_IGN);
703 if (getenv("NO_COLOR") != NULL)
704 enable_colors = 0;
706 strlcpy(path, getenv("HOME"), sizeof(path));
707 strlcat(path, "/.telescope/config", sizeof(path));
709 while ((ch = getopt(argc, argv, "c:hn")) != -1) {
710 switch (ch) {
711 case 'c':
712 fail = 1;
713 strlcpy(path, optarg, sizeof(path));
714 break;
715 case 'n':
716 configtest = 1;
717 break;
718 case 'h':
719 usage();
720 return 0;
721 default:
722 usage();
723 return 1;
727 argc -= optind;
728 argv += optind;
730 if (argc != 0) {
731 has_url = 1;
732 url = argv[0];
735 /* initialize part of the fs layer. Before starting the UI
736 * and dropping the priviledges we need to read some stuff. */
737 fs_init();
739 /* setup keys before reading the config */
740 TAILQ_INIT(&global_map.m);
741 global_map.unhandled_input = global_key_unbound;
742 TAILQ_INIT(&minibuffer_map.m);
744 config_init();
745 parseconfig(path, fail);
746 if (configtest){
747 puts("config OK");
748 exit(0);
751 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, fs_fds) == -1)
752 err(1, "socketpair");
754 switch (fork()) {
755 case -1:
756 err(1, "fork");
757 case 0:
758 /* child */
759 setproctitle("fs");
760 close(fs_fds[0]);
761 imsg_init(&fs_ibuf, fs_fds[1]);
762 exit(fs_main(&fs_ibuf));
763 default:
764 close(fs_fds[1]);
765 imsg_init(&fs_ibuf, fs_fds[0]);
766 fsibuf = &fs_ibuf;
769 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, net_fds) == -1)
770 err(1, "socketpair");
772 switch (fork()) {
773 case -1:
774 err(1, "fork");
775 case 0:
776 /* child */
777 setproctitle("client");
778 close(net_fds[0]);
779 close(fs_fds[0]);
780 imsg_init(&network_ibuf, net_fds[1]);
781 exit(client_main(&network_ibuf));
782 default:
783 close(net_fds[1]);
784 imsg_init(&network_ibuf, net_fds[0]);
785 netibuf = &network_ibuf;
788 setproctitle("ui");
790 tofu_init(&certs, 5, offsetof(struct tofu_entry, domain));
791 load_certs(&certs);
793 TAILQ_INIT(&tabshead);
794 TAILQ_INIT(&proxies);
796 event_init();
798 event_set(&netev, netibuf->fd, EV_READ | EV_PERSIST,
799 handle_dispatch_imsg, netibuf);
800 event_add(&netev, NULL);
802 event_set(&fsev, fsibuf->fd, EV_READ | EV_PERSIST,
803 handle_dispatch_imsg, fsibuf);
804 event_add(&fsev, NULL);
806 if (ui_init()) {
807 load_last_session(session_new_tab_cb);
808 if (has_url || TAILQ_EMPTY(&tabshead))
809 new_tab(url);
811 sandbox_ui_process();
812 event_dispatch();
813 ui_end();
816 imsg_compose(netibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
817 imsg_flush(netibuf);
819 imsg_compose(fsibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
820 imsg_flush(fsibuf);
822 return 0;