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;
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 l = asprintf(&page, "Writing \"%s\"... (%zu bytes)\n",
357 tab->path,
358 tab->bytes);
359 if (l == -1)
360 die();
361 load_page_from_str(tab, page);
362 free(page);
365 ui_on_tab_refresh(tab);
368 static void
369 handle_imsg_eof(struct imsg *imsg, size_t datalen)
371 struct tab *tab;
372 int l;
373 char *page;
375 tab = tab_by_id(imsg->hdr.peerid);
377 if (tab->fd == -1) {
378 if (!tab->buffer.page.free(&tab->buffer.page))
379 die();
380 } else {
381 l = asprintf(&page, "Wrote %s (%zu bytes)\n",
382 tab->path,
383 tab->bytes);
384 if (l == -1)
385 die();
386 load_page_from_str(tab, page);
387 free(page);
389 close(tab->fd);
390 tab->fd = -1;
391 free(tab->path);
392 tab->path = NULL;
395 ui_on_tab_refresh(tab);
396 ui_on_tab_loaded(tab);
399 static void
400 handle_imsg_bookmark_ok(struct imsg *imsg, size_t datalen)
402 int res;
404 if (datalen != sizeof(res))
405 die();
407 memcpy(&res, imsg->data, sizeof(res));
408 if (res == 0)
409 ui_notify("Added to bookmarks!");
410 else
411 ui_notify("Failed to add to bookmarks: %s",
412 strerror(res));
415 static void
416 handle_imsg_save_cert_ok(struct imsg *imsg, size_t datalen)
418 int res;
420 if (datalen != sizeof(res))
421 die();
422 memcpy(&res, imsg->data, datalen);
423 if (res != 0)
424 ui_notify("Failed to save the cert for: %s",
425 strerror(res));
428 static void
429 handle_imsg_update_cert_ok(struct imsg *imsg, size_t datalen)
431 int res;
433 if (datalen != sizeof(res))
434 die();
435 memcpy(&res, imsg->data, datalen);
436 if (!res)
437 ui_notify("Failed to update the certificate");
440 static void
441 handle_dispatch_imsg(int fd, short ev, void *d)
443 struct imsgbuf *ibuf = d;
444 dispatch_imsg(ibuf, handlers, sizeof(handlers));
447 static void
448 load_page_from_str(struct tab *tab, const char *page)
450 gemtext_initparser(&tab->buffer.page);
451 if (!tab->buffer.page.parse(&tab->buffer.page, page, strlen(page)))
452 die();
453 if (!tab->buffer.page.free(&tab->buffer.page))
454 die();
455 ui_on_tab_refresh(tab);
456 ui_on_tab_loaded(tab);
459 void
460 load_about_url(struct tab *tab, const char *url)
462 tab->trust = TS_VERIFIED;
464 gemtext_initparser(&tab->buffer.page);
466 imsg_compose(fsibuf, IMSG_GET, tab->id, 0, -1,
467 tab->hist_cur->h, strlen(tab->hist_cur->h)+1);
468 imsg_flush(fsibuf);
471 void
472 load_gemini_url(struct tab *tab, const char *url)
474 size_t len;
476 stop_tab(tab);
477 tab->id = tab_new_id();
479 len = sizeof(tab->hist_cur->h);
480 imsg_compose(netibuf, IMSG_GET, tab->id, 0, -1,
481 tab->hist_cur->h, len);
482 imsg_flush(netibuf);
483 return;
486 static void
487 do_load_url(struct tab *tab, const char *url)
489 struct phos_uri uri;
490 struct proto *p;
491 char *t;
493 if (tab->fd != -1) {
494 close(tab->fd);
495 tab->fd = -1;
496 free(tab->path);
497 tab->path = NULL;
500 tab->trust = TS_UNKNOWN;
502 memcpy(&uri, &tab->uri, sizeof(tab->uri));
503 if (!phos_resolve_uri_from_str(&uri, url, &tab->uri)) {
504 if (asprintf(&t, "#error loading %s\n>%s\n",
505 url, "Can't parse the URI") == -1)
506 die();
507 strlcpy(tab->hist_cur->h, url, sizeof(tab->hist_cur->h));
508 load_page_from_str(tab, t);
509 free(t);
510 return;
513 phos_serialize_uri(&tab->uri, tab->hist_cur->h,
514 sizeof(tab->hist_cur->h));
516 for (p = protos; p->schema != NULL; ++p) {
517 if (!strcmp(tab->uri.scheme, p->schema)) {
518 p->loadfn(tab, url);
519 return;
523 protos[0].loadfn(tab, url);
526 void
527 load_url(struct tab *tab, const char *url)
529 if (tab->hist_cur != NULL)
530 hist_clear_forward(&tab->hist, TAILQ_NEXT(tab->hist_cur, entries));
532 if ((tab->hist_cur = calloc(1, sizeof(*tab->hist_cur))) == NULL) {
533 event_loopbreak();
534 return;
536 hist_push(&tab->hist, tab->hist_cur);
537 do_load_url(tab, url);
538 empty_vlist(&tab->buffer);
539 empty_linelist(&tab->buffer);
542 int
543 load_previous_page(struct tab *tab)
545 struct hist *h;
547 if ((h = TAILQ_PREV(tab->hist_cur, mhisthead, entries)) == NULL)
548 return 0;
549 tab->hist_cur = h;
550 do_load_url(tab, h->h);
551 return 1;
554 int
555 load_next_page(struct tab *tab)
557 struct hist *h;
559 if ((h = TAILQ_NEXT(tab->hist_cur, entries)) == NULL)
560 return 0;
561 tab->hist_cur = h;
562 do_load_url(tab, h->h);
563 return 1;
566 void
567 stop_tab(struct tab *tab)
569 imsg_compose(netibuf, IMSG_STOP, tab->id, 0, -1, NULL, 0);
570 imsg_flush(netibuf);
572 if (tab->fd != -1) {
573 close(tab->fd);
574 tab->fd = -1;
575 free(tab->path);
576 tab->path = NULL;
577 load_page_from_str(tab, "Stopped.\n");
581 void
582 add_to_bookmarks(const char *str)
584 imsg_compose(fsibuf, IMSG_BOOKMARK_PAGE, 0, 0, -1, str, strlen(str)+1);
585 imsg_flush(fsibuf);
588 void
589 save_session(void)
591 struct tab *tab;
593 imsg_compose(fsibuf, IMSG_SESSION_START, 0, 0, -1, NULL, 0);
594 imsg_flush(fsibuf);
596 TAILQ_FOREACH(tab, &tabshead, tabs) {
597 imsg_compose(fsibuf, IMSG_SESSION_TAB, 0, 0, -1,
598 tab->hist_cur->h, strlen(tab->hist_cur->h)+1);
599 imsg_flush(fsibuf);
602 imsg_compose(fsibuf, IMSG_SESSION_END, 0, 0, -1, NULL, 0);
603 imsg_flush(fsibuf);
606 int
607 main(int argc, char * const *argv)
609 struct imsgbuf network_ibuf, fs_ibuf;
610 int net_fds[2], fs_fds[2];
612 signal(SIGCHLD, SIG_IGN);
613 signal(SIGPIPE, SIG_IGN);
615 /* initialize part of the fs layer. Before starting the UI
616 * and dropping the priviledges we need to read some stuff. */
617 fs_init();
619 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, fs_fds) == -1)
620 err(1, "socketpair");
622 switch (fork()) {
623 case -1:
624 err(1, "fork");
625 case 0:
626 /* child */
627 setproctitle("fs");
628 close(fs_fds[0]);
629 imsg_init(&fs_ibuf, fs_fds[1]);
630 exit(fs_main(&fs_ibuf));
631 default:
632 close(fs_fds[1]);
633 imsg_init(&fs_ibuf, fs_fds[0]);
634 fsibuf = &fs_ibuf;
637 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, net_fds) == -1)
638 err(1, "socketpair");
640 switch (fork()) {
641 case -1:
642 err(1, "fork");
643 case 0:
644 /* child */
645 setproctitle("client");
646 close(net_fds[0]);
647 close(fs_fds[0]);
648 imsg_init(&network_ibuf, net_fds[1]);
649 exit(client_main(&network_ibuf));
650 default:
651 close(net_fds[1]);
652 imsg_init(&network_ibuf, net_fds[0]);
653 netibuf = &network_ibuf;
656 setproctitle("ui");
658 tofu_init(&certs, 5, offsetof(struct tofu_entry, domain));
659 load_certs(&certs);
661 TAILQ_INIT(&tabshead);
663 event_init();
665 event_set(&netev, netibuf->fd, EV_READ | EV_PERSIST,
666 handle_dispatch_imsg, netibuf);
667 event_add(&netev, NULL);
669 event_set(&fsev, fsibuf->fd, EV_READ | EV_PERSIST,
670 handle_dispatch_imsg, fsibuf);
671 event_add(&fsev, NULL);
673 if (ui_init(argc, argv)) {
674 sandbox_ui_process();
675 event_dispatch();
676 ui_end();
679 imsg_compose(netibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
680 imsg_flush(netibuf);
682 imsg_compose(fsibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
683 imsg_flush(fsibuf);
685 return 0;