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, void*);
29 static void handle_imsg_got_code(struct imsg*, size_t);
30 static void handle_imsg_got_meta(struct imsg*, size_t);
31 static void handle_imsg_buf(struct imsg*, size_t);
32 static void handle_imsg_eof(struct imsg*, size_t);
33 static void handle_imsg_bookmark_ok(struct imsg*, size_t);
34 static void handle_imsg_save_cert_ok(struct imsg*, size_t);
35 static void handle_dispatch_imsg(int, short, void*);
36 static void load_page_from_str(struct tab*, const char*);
37 static void do_load_url(struct tab*, const char*);
39 static imsg_handlerfn *handlers[] = {
40 [IMSG_ERR] = handle_imsg_err,
41 [IMSG_CHECK_CERT] = handle_imsg_check_cert,
42 [IMSG_GOT_CODE] = handle_imsg_got_code,
43 [IMSG_GOT_META] = handle_imsg_got_meta,
44 [IMSG_BUF] = handle_imsg_buf,
45 [IMSG_EOF] = handle_imsg_eof,
46 [IMSG_BOOKMARK_OK] = handle_imsg_bookmark_ok,
47 [IMSG_SAVE_CERT_OK] = handle_imsg_save_cert_ok,
48 };
50 static struct ohash certs;
52 static void __attribute__((__noreturn__))
53 die(void)
54 {
55 abort(); /* TODO */
56 }
58 static struct tab *
59 tab_by_id(uint32_t id)
60 {
61 struct tab *t;
63 TAILQ_FOREACH(t, &tabshead, tabs) {
64 if (t->id == id)
65 return t;
66 }
68 die();
69 }
71 static void
72 handle_imsg_err(struct imsg *imsg, size_t datalen)
73 {
74 struct tab *tab;
75 char *page;
77 tab = tab_by_id(imsg->hdr.peerid);
79 page = imsg->data;
80 page[datalen-1] = '\0';
82 if (asprintf(&page, "# Error loading %s\n\n> %s\n",
83 tab->hist_cur->h, page) == -1)
84 die();
85 load_page_from_str(tab, page);
86 free(page);
87 }
89 static void
90 handle_imsg_check_cert(struct imsg *imsg, size_t datalen)
91 {
92 const char *hash;
93 int tofu_res;
94 struct tofu_entry *e;
95 struct tab *tab;
97 hash = imsg->data;
98 if (hash[datalen-1] != '\0')
99 abort();
101 tab = tab_by_id(imsg->hdr.peerid);
103 if ((e = telescope_lookup_tofu(&certs, tab->url.host)) == NULL) {
104 /* TODO: an update in libressl/libretls changed
105 * significantly. Find a better approach at storing
106 * the certs! */
107 if (datalen > sizeof(e->hash))
108 abort();
110 tofu_res = 1; /* trust on first use */
111 if ((e = calloc(1, sizeof(*e))) == NULL)
112 abort();
113 strlcpy(e->domain, tab->url.host, sizeof(e->domain));
114 strlcpy(e->hash, hash, sizeof(e->hash));
115 telescope_ohash_insert(&certs, e);
116 imsg_compose(fsibuf, IMSG_SAVE_CERT, tab->id, 0, -1,
117 e, sizeof(*e));
118 imsg_flush(fsibuf);
119 } else
120 tofu_res = !strcmp(hash, e->hash);
122 if (tofu_res) {
123 tab->trust = e->verified ? TS_VERIFIED : TS_TRUSTED;
124 imsg_compose(netibuf, IMSG_CERT_STATUS, imsg->hdr.peerid, 0, -1,
125 &tofu_res, sizeof(tofu_res));
126 imsg_flush(netibuf);
127 } else {
128 tab->trust = TS_UNTRUSTED;
129 load_page_from_str(tab, "# Certificate mismatch\n");
130 ui_yornp("Certificate mismatch. Proceed?",
131 handle_check_cert_user_choice, &tab->id);
135 static void
136 handle_check_cert_user_choice(int accept, void *d)
138 unsigned int tabid = *(unsigned int*)d;
140 imsg_compose(netibuf, IMSG_CERT_STATUS, tabid, 0, -1,
141 &accept, sizeof(accept));
142 imsg_flush(netibuf);
145 static inline int
146 normalize_code(int n)
148 if (n < 20) {
149 if (n == 10 || n == 11)
150 return n;
151 return 10;
152 } else if (n < 30) {
153 return 20;
154 } else if (n < 40) {
155 if (n == 30 || n == 31)
156 return n;
157 return 30;
158 } else if (n < 50) {
159 if (n <= 44)
160 return n;
161 return 40;
162 } else if (n < 60) {
163 if (n <= 53 || n == 59)
164 return n;
165 return 50;
166 } else if (n < 70) {
167 if (n <= 62)
168 return n;
169 return 60;
170 } else
171 return MALFORMED_RESPONSE;
174 static void
175 handle_imsg_got_code(struct imsg *imsg, size_t datalen)
177 struct tab *tab;
179 tab = tab_by_id(imsg->hdr.peerid);
181 if (sizeof(tab->code) != datalen)
182 die();
184 memcpy(&tab->code, imsg->data, sizeof(tab->code));
185 tab->code = normalize_code(tab->code);
186 if (tab->code != 30 && tab->code != 31)
187 tab->redirect_count = 0;
190 static void
191 handle_imsg_got_meta(struct imsg *imsg, size_t datalen)
193 struct tab *tab;
195 tab = tab_by_id(imsg->hdr.peerid);
197 if (sizeof(tab->meta) <= datalen)
198 die();
200 memcpy(tab->meta, imsg->data, datalen);
202 if (tab->code < 10) { /* internal errors */
203 load_page_from_str(tab, err_pages[tab->code]);
204 } else if (tab->code < 20) { /* 1x */
205 load_page_from_str(tab, err_pages[tab->code]);
206 ui_require_input(tab, tab->code == 11);
207 } else if (tab->code == 20) {
208 if (setup_parser_for(tab)) {
209 imsg_compose(netibuf, IMSG_PROCEED, tab->id, 0, -1, NULL, 0);
210 imsg_flush(netibuf);
211 } else {
212 load_page_from_str(tab, err_pages[UNKNOWN_TYPE_OR_CSET]);
214 } else if (tab->code < 40) { /* 3x */
215 tab->redirect_count++;
217 /* TODO: make customizable? */
218 if (tab->redirect_count > 5) {
219 load_page_from_str(tab,
220 err_pages[TOO_MUCH_REDIRECTS]);
221 } else
222 do_load_url(tab, tab->meta);
223 } else { /* 4x, 5x & 6x */
224 load_page_from_str(tab, err_pages[tab->code]);
228 static void
229 handle_imsg_buf(struct imsg *imsg, size_t datalen)
231 struct tab *tab;
233 tab = tab_by_id(imsg->hdr.peerid);
235 if (!tab->window.page.parse(&tab->window.page, imsg->data, datalen))
236 die();
238 ui_on_tab_refresh(tab);
241 static void
242 handle_imsg_eof(struct imsg *imsg, size_t datalen)
244 struct tab *tab;
246 tab = tab_by_id(imsg->hdr.peerid);
247 if (!tab->window.page.free(&tab->window.page))
248 die();
250 ui_on_tab_refresh(tab);
251 ui_on_tab_loaded(tab);
254 static void
255 handle_imsg_bookmark_ok(struct imsg *imsg, size_t datalen)
257 int res;
259 if (datalen != sizeof(res))
260 die();
262 memcpy(&res, imsg->data, sizeof(res));
263 if (res == 0)
264 ui_notify("Added to bookmarks!");
265 else
266 ui_notify("Failed to add to bookmarks: %s",
267 strerror(res));
270 static void
271 handle_imsg_save_cert_ok(struct imsg *imsg, size_t datalen)
273 int res;
275 if (datalen != sizeof(res))
276 die();
277 memcpy(&res, imsg->data, datalen);
278 if (res != 0)
279 ui_notify("Failed to save the cert for: %s",
280 strerror(res));
283 static void
284 handle_dispatch_imsg(int fd, short ev, void *d)
286 struct imsgbuf *ibuf = d;
287 dispatch_imsg(ibuf, handlers, sizeof(handlers));
290 static void
291 load_page_from_str(struct tab *tab, const char *page)
293 gemtext_initparser(&tab->window.page);
294 if (!tab->window.page.parse(&tab->window.page, page, strlen(page)))
295 die();
296 if (!tab->window.page.free(&tab->window.page))
297 die();
298 ui_on_tab_refresh(tab);
299 ui_on_tab_loaded(tab);
302 void
303 load_about_url(struct tab *tab, const char *url)
305 char *m;
306 size_t len;
308 tab->trust = TS_VERIFIED;
310 memset(&tab->url, 0, sizeof(tab->url));
312 m = strchr(url, ':');
313 strlcpy(tab->url.scheme, "about", sizeof(tab->url.scheme));
314 strlcpy(tab->url.path, m+1, sizeof(tab->url.path));
316 len = sizeof(tab->hist_cur->h)-1;
317 strlcpy(tab->hist_cur->h, url, len);
319 gemtext_initparser(&tab->window.page);
321 imsg_compose(fsibuf, IMSG_GET, tab->id, 0, -1,
322 tab->hist_cur->h, len+1);
323 imsg_flush(fsibuf);
326 void
327 load_gemini_url(struct tab *tab, const char *url)
329 const char *err;
330 char *p;
331 size_t len;
333 len = sizeof(tab->hist_cur->h)-1;
335 if (has_prefix(url, "gemini:")) {
336 if (!url_parse(url, &tab->url, &err))
337 goto err;
338 } else {
339 if (!url_resolve_from(&tab->url, url, &err))
340 goto err;
343 url_unparse(&tab->url, tab->hist_cur->h, len);
344 imsg_compose(netibuf, IMSG_GET, tab->id, 0, -1,
345 tab->hist_cur->h, len+1);
346 imsg_flush(netibuf);
347 return;
349 err:
350 if (asprintf(&p, "#error loading %s\n>%s\n",
351 url, err) == -1)
352 die();
353 strlcpy(tab->hist_cur->h, url, len);
354 load_page_from_str(tab, p);
355 free(p);
358 static void
359 do_load_url(struct tab *tab, const char *url)
361 struct proto *p;
363 tab->trust = TS_UNKNOWN;
365 for (p = protos; p->schema != NULL; ++p) {
366 if (has_prefix(url, p->schema)) {
367 p->loadfn(tab, url);
368 return;
372 protos[0].loadfn(tab, url);
374 empty_vlist(&tab->window);
375 empty_linelist(&tab->window);
378 void
379 load_url(struct tab *tab, const char *url)
381 if (tab->hist_cur != NULL)
382 hist_clear_forward(&tab->hist, TAILQ_NEXT(tab->hist_cur, entries));
384 if ((tab->hist_cur = calloc(1, sizeof(*tab->hist_cur))) == NULL) {
385 event_loopbreak();
386 return;
388 hist_push(&tab->hist, tab->hist_cur);
389 do_load_url(tab, url);
392 int
393 load_previous_page(struct tab *tab)
395 struct hist *h;
397 if ((h = TAILQ_PREV(tab->hist_cur, mhisthead, entries)) == NULL)
398 return 0;
399 tab->hist_cur = h;
400 do_load_url(tab, h->h);
401 return 1;
404 int
405 load_next_page(struct tab *tab)
407 struct hist *h;
409 if ((h = TAILQ_NEXT(tab->hist_cur, entries)) == NULL)
410 return 0;
411 tab->hist_cur = h;
412 do_load_url(tab, h->h);
413 return 1;
416 void
417 stop_tab(struct tab *tab)
419 imsg_compose(netibuf, IMSG_STOP, tab->id, 0, -1, NULL, 0);
420 imsg_flush(netibuf);
423 void
424 add_to_bookmarks(const char *str)
426 imsg_compose(fsibuf, IMSG_BOOKMARK_PAGE, 0, 0, -1, str, strlen(str)+1);
427 imsg_flush(fsibuf);
430 void
431 save_session(void)
433 struct tab *tab;
435 imsg_compose(fsibuf, IMSG_SESSION_START, 0, 0, -1, NULL, 0);
436 imsg_flush(fsibuf);
438 TAILQ_FOREACH(tab, &tabshead, tabs) {
439 imsg_compose(fsibuf, IMSG_SESSION_TAB, 0, 0, -1,
440 tab->hist_cur->h, strlen(tab->hist_cur->h)+1);
441 imsg_flush(fsibuf);
444 imsg_compose(fsibuf, IMSG_SESSION_END, 0, 0, -1, NULL, 0);
445 imsg_flush(fsibuf);
448 int
449 main(int argc, char * const *argv)
451 struct imsgbuf network_ibuf, fs_ibuf;
452 int net_fds[2], fs_fds[2];
453 pid_t pid;
455 pid = getpid();
457 signal(SIGCHLD, SIG_IGN);
459 /* initialize part of the fs layer. Before starting the UI
460 * and dropping the priviledges we need to read some stuff. */
461 fs_init();
463 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, fs_fds) == -1)
464 err(1, "socketpair");
466 switch (fork()) {
467 case -1:
468 err(1, "fork");
469 case 0:
470 /* child */
471 setproctitle("(%d) fs", pid);
472 close(fs_fds[0]);
473 imsg_init(&fs_ibuf, fs_fds[1]);
474 exit(fs_main(&fs_ibuf));
475 default:
476 close(fs_fds[1]);
477 imsg_init(&fs_ibuf, fs_fds[0]);
478 fsibuf = &fs_ibuf;
481 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, net_fds) == -1)
482 err(1, "socketpair");
484 switch (fork()) {
485 case -1:
486 err(1, "fork");
487 case 0:
488 /* child */
489 setproctitle("(%d) client", pid);
490 close(net_fds[0]);
491 close(fs_fds[0]);
492 imsg_init(&network_ibuf, net_fds[1]);
493 exit(client_main(&network_ibuf));
494 default:
495 close(net_fds[1]);
496 imsg_init(&network_ibuf, net_fds[0]);
497 netibuf = &network_ibuf;
500 setproctitle("(%d) ui", pid);
502 telescope_ohash_init(&certs, 5, offsetof(struct tofu_entry, domain));
503 load_certs(&certs);
505 TAILQ_INIT(&tabshead);
507 event_init();
509 event_set(&netev, netibuf->fd, EV_READ | EV_PERSIST, handle_dispatch_imsg, netibuf);
510 event_add(&netev, NULL);
512 event_set(&fsev, fsibuf->fd, EV_READ | EV_PERSIST, handle_dispatch_imsg, fsibuf);
513 event_add(&fsev, NULL);
515 if (ui_init(argc, argv)) {
516 sandbox_ui_process();
517 event_dispatch();
518 ui_end();
521 imsg_compose(netibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
522 imsg_flush(netibuf);
524 imsg_compose(fsibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
525 imsg_flush(fsibuf);
527 return 0;