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_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->uri.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->uri.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, unsigned int tabid)
138 imsg_compose(netibuf, IMSG_CERT_STATUS, tabid, 0, -1,
139 &accept, sizeof(accept));
140 imsg_flush(netibuf);
143 static inline int
144 normalize_code(int n)
146 if (n < 20) {
147 if (n == 10 || n == 11)
148 return n;
149 return 10;
150 } else if (n < 30) {
151 return 20;
152 } else if (n < 40) {
153 if (n == 30 || n == 31)
154 return n;
155 return 30;
156 } else if (n < 50) {
157 if (n <= 44)
158 return n;
159 return 40;
160 } else if (n < 60) {
161 if (n <= 53 || n == 59)
162 return n;
163 return 50;
164 } else if (n < 70) {
165 if (n <= 62)
166 return n;
167 return 60;
168 } else
169 return MALFORMED_RESPONSE;
172 static void
173 handle_imsg_got_code(struct imsg *imsg, size_t datalen)
175 struct tab *tab;
177 tab = tab_by_id(imsg->hdr.peerid);
179 if (sizeof(tab->code) != datalen)
180 die();
182 memcpy(&tab->code, imsg->data, sizeof(tab->code));
183 tab->code = normalize_code(tab->code);
184 if (tab->code != 30 && tab->code != 31)
185 tab->redirect_count = 0;
188 static void
189 handle_imsg_got_meta(struct imsg *imsg, size_t datalen)
191 struct tab *tab;
193 tab = tab_by_id(imsg->hdr.peerid);
195 if (sizeof(tab->meta) <= datalen)
196 die();
198 memcpy(tab->meta, imsg->data, datalen);
200 if (tab->code < 10) { /* internal errors */
201 load_page_from_str(tab, err_pages[tab->code]);
202 } else if (tab->code < 20) { /* 1x */
203 load_page_from_str(tab, err_pages[tab->code]);
204 ui_require_input(tab, tab->code == 11);
205 } else if (tab->code == 20) {
206 if (setup_parser_for(tab)) {
207 imsg_compose(netibuf, IMSG_PROCEED, tab->id, 0, -1, NULL, 0);
208 imsg_flush(netibuf);
209 } else {
210 load_page_from_str(tab, err_pages[UNKNOWN_TYPE_OR_CSET]);
212 } else if (tab->code < 40) { /* 3x */
213 tab->redirect_count++;
215 /* TODO: make customizable? */
216 if (tab->redirect_count > 5) {
217 load_page_from_str(tab,
218 err_pages[TOO_MUCH_REDIRECTS]);
219 } else
220 do_load_url(tab, tab->meta);
221 } else { /* 4x, 5x & 6x */
222 load_page_from_str(tab, err_pages[tab->code]);
226 static void
227 handle_imsg_buf(struct imsg *imsg, size_t datalen)
229 struct tab *tab;
231 tab = tab_by_id(imsg->hdr.peerid);
233 if (!tab->window.page.parse(&tab->window.page, imsg->data, datalen))
234 die();
236 ui_on_tab_refresh(tab);
239 static void
240 handle_imsg_eof(struct imsg *imsg, size_t datalen)
242 struct tab *tab;
244 tab = tab_by_id(imsg->hdr.peerid);
245 if (!tab->window.page.free(&tab->window.page))
246 die();
248 ui_on_tab_refresh(tab);
249 ui_on_tab_loaded(tab);
252 static void
253 handle_imsg_bookmark_ok(struct imsg *imsg, size_t datalen)
255 int res;
257 if (datalen != sizeof(res))
258 die();
260 memcpy(&res, imsg->data, sizeof(res));
261 if (res == 0)
262 ui_notify("Added to bookmarks!");
263 else
264 ui_notify("Failed to add to bookmarks: %s",
265 strerror(res));
268 static void
269 handle_imsg_save_cert_ok(struct imsg *imsg, size_t datalen)
271 int res;
273 if (datalen != sizeof(res))
274 die();
275 memcpy(&res, imsg->data, datalen);
276 if (res != 0)
277 ui_notify("Failed to save the cert for: %s",
278 strerror(res));
281 static void
282 handle_dispatch_imsg(int fd, short ev, void *d)
284 struct imsgbuf *ibuf = d;
285 dispatch_imsg(ibuf, handlers, sizeof(handlers));
288 static void
289 load_page_from_str(struct tab *tab, const char *page)
291 gemtext_initparser(&tab->window.page);
292 if (!tab->window.page.parse(&tab->window.page, page, strlen(page)))
293 die();
294 if (!tab->window.page.free(&tab->window.page))
295 die();
296 ui_on_tab_refresh(tab);
297 ui_on_tab_loaded(tab);
300 void
301 load_about_url(struct tab *tab, const char *url)
303 tab->trust = TS_VERIFIED;
305 gemtext_initparser(&tab->window.page);
307 imsg_compose(fsibuf, IMSG_GET, tab->id, 0, -1,
308 tab->hist_cur->h, strlen(tab->hist_cur->h)+1);
309 imsg_flush(fsibuf);
312 void
313 load_gemini_url(struct tab *tab, const char *url)
315 size_t len;
317 len = sizeof(tab->hist_cur->h);
318 imsg_compose(netibuf, IMSG_GET, tab->id, 0, -1,
319 tab->hist_cur->h, len);
320 imsg_flush(netibuf);
321 return;
324 static void
325 do_load_url(struct tab *tab, const char *url)
327 struct phos_uri uri;
328 struct proto *p;
329 char *t;
331 tab->trust = TS_UNKNOWN;
333 memcpy(&uri, &tab->uri, sizeof(tab->uri));
334 if (!phos_resolve_uri_from_str(&uri, url, &tab->uri)) {
335 if (asprintf(&t, "#error loading %s\n>%s\n",
336 url, "Can't parse the URI") == -1)
337 die();
338 strlcpy(tab->hist_cur->h, url, sizeof(tab->hist_cur->h));
339 load_page_from_str(tab, t);
340 free(t);
341 return;
344 phos_serialize_uri(&tab->uri, tab->hist_cur->h,
345 sizeof(tab->hist_cur->h));
347 for (p = protos; p->schema != NULL; ++p) {
348 if (!strcmp(uri.scheme, p->schema)) {
349 p->loadfn(tab, url);
350 return;
354 protos[0].loadfn(tab, url);
357 void
358 load_url(struct tab *tab, const char *url)
360 if (tab->hist_cur != NULL)
361 hist_clear_forward(&tab->hist, TAILQ_NEXT(tab->hist_cur, entries));
363 if ((tab->hist_cur = calloc(1, sizeof(*tab->hist_cur))) == NULL) {
364 event_loopbreak();
365 return;
367 hist_push(&tab->hist, tab->hist_cur);
368 do_load_url(tab, url);
369 empty_vlist(&tab->window);
370 empty_linelist(&tab->window);
373 int
374 load_previous_page(struct tab *tab)
376 struct hist *h;
378 if ((h = TAILQ_PREV(tab->hist_cur, mhisthead, entries)) == NULL)
379 return 0;
380 tab->hist_cur = h;
381 do_load_url(tab, h->h);
382 return 1;
385 int
386 load_next_page(struct tab *tab)
388 struct hist *h;
390 if ((h = TAILQ_NEXT(tab->hist_cur, entries)) == NULL)
391 return 0;
392 tab->hist_cur = h;
393 do_load_url(tab, h->h);
394 return 1;
397 void
398 stop_tab(struct tab *tab)
400 imsg_compose(netibuf, IMSG_STOP, tab->id, 0, -1, NULL, 0);
401 imsg_flush(netibuf);
404 void
405 add_to_bookmarks(const char *str)
407 imsg_compose(fsibuf, IMSG_BOOKMARK_PAGE, 0, 0, -1, str, strlen(str)+1);
408 imsg_flush(fsibuf);
411 void
412 save_session(void)
414 struct tab *tab;
416 imsg_compose(fsibuf, IMSG_SESSION_START, 0, 0, -1, NULL, 0);
417 imsg_flush(fsibuf);
419 TAILQ_FOREACH(tab, &tabshead, tabs) {
420 imsg_compose(fsibuf, IMSG_SESSION_TAB, 0, 0, -1,
421 tab->hist_cur->h, strlen(tab->hist_cur->h)+1);
422 imsg_flush(fsibuf);
425 imsg_compose(fsibuf, IMSG_SESSION_END, 0, 0, -1, NULL, 0);
426 imsg_flush(fsibuf);
429 int
430 main(int argc, char * const *argv)
432 struct imsgbuf network_ibuf, fs_ibuf;
433 int net_fds[2], fs_fds[2];
435 signal(SIGCHLD, SIG_IGN);
437 /* initialize part of the fs layer. Before starting the UI
438 * and dropping the priviledges we need to read some stuff. */
439 fs_init();
441 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, fs_fds) == -1)
442 err(1, "socketpair");
444 switch (fork()) {
445 case -1:
446 err(1, "fork");
447 case 0:
448 /* child */
449 setproctitle("fs");
450 close(fs_fds[0]);
451 imsg_init(&fs_ibuf, fs_fds[1]);
452 exit(fs_main(&fs_ibuf));
453 default:
454 close(fs_fds[1]);
455 imsg_init(&fs_ibuf, fs_fds[0]);
456 fsibuf = &fs_ibuf;
459 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, net_fds) == -1)
460 err(1, "socketpair");
462 switch (fork()) {
463 case -1:
464 err(1, "fork");
465 case 0:
466 /* child */
467 setproctitle("client");
468 close(net_fds[0]);
469 close(fs_fds[0]);
470 imsg_init(&network_ibuf, net_fds[1]);
471 exit(client_main(&network_ibuf));
472 default:
473 close(net_fds[1]);
474 imsg_init(&network_ibuf, net_fds[0]);
475 netibuf = &network_ibuf;
478 setproctitle("ui");
480 telescope_ohash_init(&certs, 5, offsetof(struct tofu_entry, domain));
481 load_certs(&certs);
483 TAILQ_INIT(&tabshead);
485 event_init();
487 event_set(&netev, netibuf->fd, EV_READ | EV_PERSIST, handle_dispatch_imsg, netibuf);
488 event_add(&netev, NULL);
490 event_set(&fsev, fsibuf->fd, EV_READ | EV_PERSIST, handle_dispatch_imsg, fsibuf);
491 event_add(&fsev, NULL);
493 if (ui_init(argc, argv)) {
494 sandbox_ui_process();
495 event_dispatch();
496 ui_end();
499 imsg_compose(netibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
500 imsg_flush(netibuf);
502 imsg_compose(fsibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
503 imsg_flush(fsibuf);
505 return 0;