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_maybe_save_page(int, unsigned int);
33 static void handle_save_page_path(const char *, unsigned int);
34 static void handle_imsg_file_opened(struct imsg*, size_t);
35 static void handle_imsg_buf(struct imsg*, size_t);
36 static void handle_imsg_eof(struct imsg*, size_t);
37 static void handle_imsg_bookmark_ok(struct imsg*, size_t);
38 static void handle_imsg_save_cert_ok(struct imsg*, size_t);
39 static void handle_imsg_update_cert_ok(struct imsg *, size_t);
40 static void handle_dispatch_imsg(int, short, void*);
41 static void load_page_from_str(struct tab*, const char*);
42 static void do_load_url(struct tab*, const char*);
44 static imsg_handlerfn *handlers[] = {
45 [IMSG_ERR] = handle_imsg_err,
46 [IMSG_CHECK_CERT] = handle_imsg_check_cert,
47 [IMSG_GOT_CODE] = handle_imsg_got_code,
48 [IMSG_GOT_META] = handle_imsg_got_meta,
49 [IMSG_BUF] = handle_imsg_buf,
50 [IMSG_EOF] = handle_imsg_eof,
51 [IMSG_BOOKMARK_OK] = handle_imsg_bookmark_ok,
52 [IMSG_SAVE_CERT_OK] = handle_imsg_save_cert_ok,
53 [IMSG_UPDATE_CERT_OK] = handle_imsg_update_cert_ok,
54 [IMSG_FILE_OPENED] = handle_imsg_file_opened,
55 };
57 static struct ohash certs;
59 static void __attribute__((__noreturn__))
60 die(void)
61 {
62 abort(); /* TODO */
63 }
65 static struct tab *
66 tab_by_id(uint32_t id)
67 {
68 struct tab *t;
70 TAILQ_FOREACH(t, &tabshead, tabs) {
71 if (t->id == id)
72 return t;
73 }
75 die();
76 }
78 static void
79 handle_imsg_err(struct imsg *imsg, size_t datalen)
80 {
81 struct tab *tab;
82 char *page;
84 tab = tab_by_id(imsg->hdr.peerid);
86 page = imsg->data;
87 page[datalen-1] = '\0';
89 if (asprintf(&page, "# Error loading %s\n\n> %s\n",
90 tab->hist_cur->h, page) == -1)
91 die();
92 load_page_from_str(tab, page);
93 free(page);
94 }
96 static void
97 handle_imsg_check_cert(struct imsg *imsg, size_t datalen)
98 {
99 const char *hash;
100 int tofu_res;
101 struct tofu_entry *e;
102 struct tab *tab;
104 hash = imsg->data;
105 if (hash[datalen-1] != '\0')
106 abort();
108 tab = tab_by_id(imsg->hdr.peerid);
110 if ((e = tofu_lookup(&certs, tab->uri.host, tab->uri.port)) == NULL) {
111 /* TODO: an update in libressl/libretls changed
112 * significantly. Find a better approach at storing
113 * the certs! */
114 if (datalen > sizeof(e->hash))
115 abort();
117 tofu_res = 1; /* trust on first use */
118 if ((e = calloc(1, sizeof(*e))) == NULL)
119 abort();
120 strlcpy(e->domain, tab->uri.host, sizeof(e->domain));
121 if (*tab->uri.port != '\0' && strcmp(tab->uri.port, "1965")) {
122 strlcat(e->domain, ":", sizeof(e->domain));
123 strlcat(e->domain, tab->uri.port, sizeof(e->domain));
125 strlcpy(e->hash, hash, sizeof(e->hash));
126 tofu_add(&certs, e);
127 imsg_compose(fsibuf, IMSG_SAVE_CERT, tab->id, 0, -1,
128 e, sizeof(*e));
129 imsg_flush(fsibuf);
130 } else
131 tofu_res = !strcmp(hash, e->hash);
133 if (tofu_res) {
134 tab->trust = e->verified ? TS_VERIFIED : TS_TRUSTED;
135 imsg_compose(netibuf, IMSG_CERT_STATUS, imsg->hdr.peerid, 0, -1,
136 &tofu_res, sizeof(tofu_res));
137 imsg_flush(netibuf);
138 } else {
139 tab->trust = TS_UNTRUSTED;
140 load_page_from_str(tab, "# Certificate mismatch\n");
141 if ((tab->cert = strdup(hash)) == NULL)
142 die();
143 ui_yornp("Certificate mismatch. Proceed?",
144 handle_check_cert_user_choice, tab->id);
148 static void
149 handle_check_cert_user_choice(int accept, unsigned int tabid)
151 struct tab *tab;
153 tab = tab_by_id(tabid);
155 imsg_compose(netibuf, IMSG_CERT_STATUS, tabid, 0, -1,
156 &accept, sizeof(accept));
157 imsg_flush(netibuf);
159 if (accept)
160 ui_yornp("Save the new certificate?",
161 handle_maybe_save_new_cert, tabid);
162 else {
163 free(tab->cert);
164 tab->cert = NULL;
168 static void
169 handle_maybe_save_new_cert(int accept, unsigned int tabid)
171 struct tab *tab;
172 struct tofu_entry *e;
174 tab = tab_by_id(tabid);
176 if (!accept)
177 goto end;
179 if ((e = calloc(1, sizeof(*e))) == NULL)
180 die();
182 strlcpy(e->domain, tab->uri.host, sizeof(e->domain));
183 if (*tab->uri.port != '\0' && strcmp(tab->uri.port, "1965")) {
184 strlcat(e->domain, ":", sizeof(e->domain));
185 strlcat(e->domain, tab->uri.port, sizeof(e->domain));
187 strlcpy(e->hash, tab->cert, sizeof(e->hash));
188 imsg_compose(fsibuf, IMSG_UPDATE_CERT, 0, 0, -1, e, sizeof(*e));
189 imsg_flush(fsibuf);
191 tofu_update(&certs, e);
193 tab->trust = TS_TRUSTED;
195 end:
196 free(tab->cert);
197 tab->cert = NULL;
200 static inline int
201 normalize_code(int n)
203 if (n < 20) {
204 if (n == 10 || n == 11)
205 return n;
206 return 10;
207 } else if (n < 30) {
208 return 20;
209 } else if (n < 40) {
210 if (n == 30 || n == 31)
211 return n;
212 return 30;
213 } else if (n < 50) {
214 if (n <= 44)
215 return n;
216 return 40;
217 } else if (n < 60) {
218 if (n <= 53 || n == 59)
219 return n;
220 return 50;
221 } else if (n < 70) {
222 if (n <= 62)
223 return n;
224 return 60;
225 } else
226 return MALFORMED_RESPONSE;
229 static void
230 handle_imsg_got_code(struct imsg *imsg, size_t datalen)
232 struct tab *tab;
234 tab = tab_by_id(imsg->hdr.peerid);
236 if (sizeof(tab->code) != datalen)
237 die();
239 memcpy(&tab->code, imsg->data, sizeof(tab->code));
240 tab->code = normalize_code(tab->code);
241 if (tab->code != 30 && tab->code != 31)
242 tab->redirect_count = 0;
245 static void
246 handle_imsg_got_meta(struct imsg *imsg, size_t datalen)
248 struct tab *tab;
250 tab = tab_by_id(imsg->hdr.peerid);
252 if (sizeof(tab->meta) <= datalen)
253 die();
255 memcpy(tab->meta, imsg->data, datalen);
257 if (tab->code < 10) { /* internal errors */
258 load_page_from_str(tab, err_pages[tab->code]);
259 } else if (tab->code < 20) { /* 1x */
260 load_page_from_str(tab, err_pages[tab->code]);
261 ui_require_input(tab, tab->code == 11);
262 } else if (tab->code == 20) {
263 if (setup_parser_for(tab)) {
264 imsg_compose(netibuf, IMSG_PROCEED, tab->id, 0, -1, NULL, 0);
265 imsg_flush(netibuf);
266 } else {
267 load_page_from_str(tab, err_pages[UNKNOWN_TYPE_OR_CSET]);
268 ui_yornp("Can't display page, wanna save?",
269 handle_maybe_save_page, tab->id);
271 } else if (tab->code < 40) { /* 3x */
272 tab->redirect_count++;
274 /* TODO: make customizable? */
275 if (tab->redirect_count > 5) {
276 load_page_from_str(tab,
277 err_pages[TOO_MUCH_REDIRECTS]);
278 } else
279 do_load_url(tab, tab->meta);
280 } else { /* 4x, 5x & 6x */
281 load_page_from_str(tab, err_pages[tab->code]);
285 static void
286 handle_maybe_save_page(int dosave, unsigned int tabid)
288 if (dosave)
289 ui_read("Save to path", handle_save_page_path, tabid);
290 else
291 stop_tab(tab_by_id(tabid));
294 static void
295 handle_save_page_path(const char *path, unsigned int tabid)
297 struct tab *tab;
299 if (path == NULL) {
300 stop_tab(tab_by_id(tabid));
301 return;
304 tab = tab_by_id(tabid);
305 tab->path = strdup(path);
307 imsg_compose(fsibuf, IMSG_FILE_OPEN, tabid, 0, -1, path, strlen(path)+1);
308 imsg_flush(fsibuf);
311 static void
312 handle_imsg_file_opened(struct imsg *imsg, size_t datalen)
314 struct tab *tab;
315 char *page;
316 const char *e;
317 int l;
319 tab = tab_by_id(imsg->hdr.peerid);
321 if (imsg->fd == -1) {
322 stop_tab(tab);
324 e = imsg->data;
325 if (e[datalen-1] != '\0')
326 die();
327 l = asprintf(&page, "# Can't open file\n\n> %s: %s\n",
328 tab->path, e);
329 if (l == -1)
330 die();
331 load_page_from_str(tab, page);
332 free(page);
333 } else {
334 tab->fd = imsg->fd;
335 imsg_compose(netibuf, IMSG_PROCEED, tab->id, 0, -1, NULL, 0);
336 imsg_flush(netibuf);
340 static void
341 handle_imsg_buf(struct imsg *imsg, size_t datalen)
343 struct tab *tab;
344 int l;
345 char *page, buf[FMT_SCALED_STRSIZE] = {0};
347 tab = tab_by_id(imsg->hdr.peerid);
349 tab->bytes += datalen;
350 if (tab->fd == -1) {
351 if (!tab->buffer.page.parse(&tab->buffer.page,
352 imsg->data, datalen))
353 die();
354 } else {
355 write(tab->fd, imsg->data, datalen);
356 fmt_scaled(tab->bytes, buf);
357 l = asprintf(&page, "Saving to \"%s\"... (%s)\n",
358 tab->path,
359 buf);
360 if (l == -1)
361 die();
362 load_page_from_str(tab, page);
363 free(page);
366 ui_on_tab_refresh(tab);
369 static void
370 handle_imsg_eof(struct imsg *imsg, size_t datalen)
372 struct tab *tab;
373 int l;
374 char *page, buf[FMT_SCALED_STRSIZE] = {0};
376 tab = tab_by_id(imsg->hdr.peerid);
378 if (tab->fd == -1) {
379 if (!tab->buffer.page.free(&tab->buffer.page))
380 die();
381 } else {
382 fmt_scaled(tab->bytes, buf);
383 l = asprintf(&page, "Saved to \"%s\" (%s)\n",
384 tab->path,
385 buf);
386 if (l == -1)
387 die();
388 load_page_from_str(tab, page);
389 free(page);
391 close(tab->fd);
392 tab->fd = -1;
393 free(tab->path);
394 tab->path = NULL;
397 ui_on_tab_refresh(tab);
398 ui_on_tab_loaded(tab);
401 static void
402 handle_imsg_bookmark_ok(struct imsg *imsg, size_t datalen)
404 int res;
406 if (datalen != sizeof(res))
407 die();
409 memcpy(&res, imsg->data, sizeof(res));
410 if (res == 0)
411 message("Added to bookmarks!");
412 else
413 message("Failed to add to bookmarks: %s",
414 strerror(res));
417 static void
418 handle_imsg_save_cert_ok(struct imsg *imsg, size_t datalen)
420 int res;
422 if (datalen != sizeof(res))
423 die();
424 memcpy(&res, imsg->data, datalen);
425 if (res != 0)
426 message("Failed to save the cert for: %s",
427 strerror(res));
430 static void
431 handle_imsg_update_cert_ok(struct imsg *imsg, size_t datalen)
433 int res;
435 if (datalen != sizeof(res))
436 die();
437 memcpy(&res, imsg->data, datalen);
438 if (!res)
439 message("Failed to update the certificate");
442 static void
443 handle_dispatch_imsg(int fd, short ev, void *d)
445 struct imsgbuf *ibuf = d;
446 dispatch_imsg(ibuf, handlers, sizeof(handlers));
449 static void
450 load_page_from_str(struct tab *tab, const char *page)
452 gemtext_initparser(&tab->buffer.page);
453 if (!tab->buffer.page.parse(&tab->buffer.page, page, strlen(page)))
454 die();
455 if (!tab->buffer.page.free(&tab->buffer.page))
456 die();
457 ui_on_tab_refresh(tab);
458 ui_on_tab_loaded(tab);
461 void
462 load_about_url(struct tab *tab, const char *url)
464 tab->trust = TS_VERIFIED;
466 gemtext_initparser(&tab->buffer.page);
468 imsg_compose(fsibuf, IMSG_GET, tab->id, 0, -1,
469 tab->hist_cur->h, strlen(tab->hist_cur->h)+1);
470 imsg_flush(fsibuf);
473 void
474 load_gemini_url(struct tab *tab, const char *url)
476 size_t len;
478 stop_tab(tab);
479 tab->id = tab_new_id();
481 len = sizeof(tab->hist_cur->h);
482 imsg_compose(netibuf, IMSG_GET, tab->id, 0, -1,
483 tab->hist_cur->h, len);
484 imsg_flush(netibuf);
485 return;
488 static void
489 do_load_url(struct tab *tab, const char *url)
491 struct phos_uri uri;
492 struct proto *p;
493 char *t;
495 if (tab->fd != -1) {
496 close(tab->fd);
497 tab->fd = -1;
498 free(tab->path);
499 tab->path = NULL;
502 tab->trust = TS_UNKNOWN;
504 memcpy(&uri, &tab->uri, sizeof(tab->uri));
505 if (!phos_resolve_uri_from_str(&uri, url, &tab->uri)) {
506 if (asprintf(&t, "#error loading %s\n>%s\n",
507 url, "Can't parse the URI") == -1)
508 die();
509 strlcpy(tab->hist_cur->h, url, sizeof(tab->hist_cur->h));
510 load_page_from_str(tab, t);
511 free(t);
512 return;
515 phos_serialize_uri(&tab->uri, tab->hist_cur->h,
516 sizeof(tab->hist_cur->h));
518 for (p = protos; p->schema != NULL; ++p) {
519 if (!strcmp(tab->uri.scheme, p->schema)) {
520 p->loadfn(tab, url);
521 return;
525 protos[0].loadfn(tab, url);
528 void
529 load_url(struct tab *tab, const char *url)
531 if (tab->hist_cur != NULL)
532 hist_clear_forward(&tab->hist, TAILQ_NEXT(tab->hist_cur, entries));
534 if ((tab->hist_cur = calloc(1, sizeof(*tab->hist_cur))) == NULL) {
535 event_loopbreak();
536 return;
538 hist_push(&tab->hist, tab->hist_cur);
539 do_load_url(tab, url);
540 empty_vlist(&tab->buffer);
541 empty_linelist(&tab->buffer);
544 int
545 load_previous_page(struct tab *tab)
547 struct hist *h;
549 if ((h = TAILQ_PREV(tab->hist_cur, mhisthead, entries)) == NULL)
550 return 0;
551 tab->hist_cur = h;
552 do_load_url(tab, h->h);
553 return 1;
556 int
557 load_next_page(struct tab *tab)
559 struct hist *h;
561 if ((h = TAILQ_NEXT(tab->hist_cur, entries)) == NULL)
562 return 0;
563 tab->hist_cur = h;
564 do_load_url(tab, h->h);
565 return 1;
568 void
569 stop_tab(struct tab *tab)
571 imsg_compose(netibuf, IMSG_STOP, tab->id, 0, -1, NULL, 0);
572 imsg_flush(netibuf);
574 if (tab->fd != -1) {
575 close(tab->fd);
576 tab->fd = -1;
577 free(tab->path);
578 tab->path = NULL;
579 load_page_from_str(tab, "Stopped.\n");
583 void
584 add_to_bookmarks(const char *str)
586 imsg_compose(fsibuf, IMSG_BOOKMARK_PAGE, 0, 0, -1, str, strlen(str)+1);
587 imsg_flush(fsibuf);
590 void
591 save_session(void)
593 struct tab *tab;
595 imsg_compose(fsibuf, IMSG_SESSION_START, 0, 0, -1, NULL, 0);
596 imsg_flush(fsibuf);
598 TAILQ_FOREACH(tab, &tabshead, tabs) {
599 imsg_compose(fsibuf, IMSG_SESSION_TAB, 0, 0, -1,
600 tab->hist_cur->h, strlen(tab->hist_cur->h)+1);
601 imsg_flush(fsibuf);
604 imsg_compose(fsibuf, IMSG_SESSION_END, 0, 0, -1, NULL, 0);
605 imsg_flush(fsibuf);
608 int
609 main(int argc, char * const *argv)
611 struct imsgbuf network_ibuf, fs_ibuf;
612 int net_fds[2], fs_fds[2];
614 signal(SIGCHLD, SIG_IGN);
615 signal(SIGPIPE, SIG_IGN);
617 /* initialize part of the fs layer. Before starting the UI
618 * and dropping the priviledges we need to read some stuff. */
619 fs_init();
621 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, fs_fds) == -1)
622 err(1, "socketpair");
624 switch (fork()) {
625 case -1:
626 err(1, "fork");
627 case 0:
628 /* child */
629 setproctitle("fs");
630 close(fs_fds[0]);
631 imsg_init(&fs_ibuf, fs_fds[1]);
632 exit(fs_main(&fs_ibuf));
633 default:
634 close(fs_fds[1]);
635 imsg_init(&fs_ibuf, fs_fds[0]);
636 fsibuf = &fs_ibuf;
639 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, net_fds) == -1)
640 err(1, "socketpair");
642 switch (fork()) {
643 case -1:
644 err(1, "fork");
645 case 0:
646 /* child */
647 setproctitle("client");
648 close(net_fds[0]);
649 close(fs_fds[0]);
650 imsg_init(&network_ibuf, net_fds[1]);
651 exit(client_main(&network_ibuf));
652 default:
653 close(net_fds[1]);
654 imsg_init(&network_ibuf, net_fds[0]);
655 netibuf = &network_ibuf;
658 setproctitle("ui");
660 tofu_init(&certs, 5, offsetof(struct tofu_entry, domain));
661 load_certs(&certs);
663 TAILQ_INIT(&tabshead);
665 event_init();
667 event_set(&netev, netibuf->fd, EV_READ | EV_PERSIST,
668 handle_dispatch_imsg, netibuf);
669 event_add(&netev, NULL);
671 event_set(&fsev, fsibuf->fd, EV_READ | EV_PERSIST,
672 handle_dispatch_imsg, fsibuf);
673 event_add(&fsev, NULL);
675 if (ui_init(argc, argv)) {
676 sandbox_ui_process();
677 event_dispatch();
678 ui_end();
681 imsg_compose(netibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
682 imsg_flush(netibuf);
684 imsg_compose(fsibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
685 imsg_flush(fsibuf);
687 return 0;