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_imsg_got_code(struct imsg*, size_t);
29 static void handle_imsg_got_meta(struct imsg*, size_t);
30 static void handle_imsg_buf(struct imsg*, size_t);
31 static void handle_imsg_eof(struct imsg*, size_t);
32 static void handle_imsg_bookmark_ok(struct imsg*, size_t);
33 static void handle_imsg_save_cert_ok(struct imsg*, size_t);
34 static void handle_dispatch_imsg(int, short, void*);
35 static void load_page_from_str(struct tab*, const char*);
36 static void do_load_url(struct tab*, const char*);
38 static imsg_handlerfn *handlers[] = {
39 [IMSG_ERR] = handle_imsg_err,
40 [IMSG_CHECK_CERT] = handle_imsg_check_cert,
41 [IMSG_GOT_CODE] = handle_imsg_got_code,
42 [IMSG_GOT_META] = handle_imsg_got_meta,
43 [IMSG_BUF] = handle_imsg_buf,
44 [IMSG_EOF] = handle_imsg_eof,
45 [IMSG_BOOKMARK_OK] = handle_imsg_bookmark_ok,
46 [IMSG_SAVE_CERT_OK] = handle_imsg_save_cert_ok,
47 };
49 static struct ohash certs;
51 static void __attribute__((__noreturn__))
52 die(void)
53 {
54 abort(); /* TODO */
55 }
57 static struct tab *
58 tab_by_id(uint32_t id)
59 {
60 struct tab *t;
62 TAILQ_FOREACH(t, &tabshead, tabs) {
63 if (t->id == id)
64 return t;
65 }
67 die();
68 }
70 static void
71 handle_imsg_err(struct imsg *imsg, size_t datalen)
72 {
73 struct tab *tab;
74 char *page;
76 tab = tab_by_id(imsg->hdr.peerid);
78 page = imsg->data;
79 page[datalen-1] = '\0';
81 if (asprintf(&page, "# Error loading %s\n\n> %s\n",
82 tab->hist_cur->h, page) == -1)
83 die();
84 load_page_from_str(tab, page);
85 free(page);
86 }
88 static void
89 handle_imsg_check_cert(struct imsg *imsg, size_t datalen)
90 {
91 const char *hash;
92 int tofu_res;
93 struct tofu_entry *e;
94 struct tab *tab;
96 hash = imsg->data;
97 if (hash[datalen-1] != '\0')
98 abort();
100 tab = tab_by_id(imsg->hdr.peerid);
102 if ((e = telescope_lookup_tofu(&certs, tab->url.host)) == NULL) {
103 /* TODO: an update in libressl/libretls changed
104 * significantly. Find a better approach at storing
105 * the certs! */
106 if (datalen > sizeof(e->hash))
107 abort();
109 tofu_res = 1; /* trust on first use */
110 if ((e = calloc(1, sizeof(*e))) == NULL)
111 abort();
112 strlcpy(e->domain, tab->url.host, sizeof(e->domain));
113 strlcpy(e->hash, hash, sizeof(e->hash));
114 telescope_ohash_insert(&certs, e);
115 imsg_compose(fsibuf, IMSG_SAVE_CERT, tab->id, 0, -1,
116 e, sizeof(*e));
117 imsg_flush(fsibuf);
118 } else
119 tofu_res = !strcmp(hash, e->hash);
121 if (tofu_res)
122 tab->trust = e->verified ? TS_VERIFIED : TS_TRUSTED;
123 else {
124 tab->trust = TS_UNTRUSTED;
125 load_page_from_str(tab, "# Certificate mismatch\n");
127 imsg_compose(netibuf, IMSG_CERT_STATUS, imsg->hdr.peerid, 0, -1,
128 &tofu_res, sizeof(tofu_res));
129 imsg_flush(netibuf);
132 static inline int
133 normalize_code(int n)
135 if (n < 20) {
136 if (n == 10 || n == 11)
137 return n;
138 return 10;
139 } else if (n < 30) {
140 return 20;
141 } else if (n < 40) {
142 if (n == 30 || n == 31)
143 return n;
144 return 30;
145 } else if (n < 50) {
146 if (n <= 44)
147 return n;
148 return 40;
149 } else if (n < 60) {
150 if (n <= 53 || n == 59)
151 return n;
152 return 50;
153 } else if (n < 70) {
154 if (n <= 62)
155 return n;
156 return 60;
157 } else
158 return MALFORMED_RESPONSE;
161 static void
162 handle_imsg_got_code(struct imsg *imsg, size_t datalen)
164 struct tab *tab;
166 tab = tab_by_id(imsg->hdr.peerid);
168 if (sizeof(tab->code) != datalen)
169 die();
171 memcpy(&tab->code, imsg->data, sizeof(tab->code));
172 tab->code = normalize_code(tab->code);
173 if (tab->code != 30 && tab->code != 31)
174 tab->redirect_count = 0;
177 static void
178 handle_imsg_got_meta(struct imsg *imsg, size_t datalen)
180 struct tab *tab;
182 tab = tab_by_id(imsg->hdr.peerid);
184 if (sizeof(tab->meta) <= datalen)
185 die();
187 memcpy(tab->meta, imsg->data, datalen);
189 if (tab->code < 10) { /* internal errors */
190 load_page_from_str(tab, err_pages[tab->code]);
191 } else if (tab->code < 20) { /* 1x */
192 load_page_from_str(tab, err_pages[tab->code]);
193 ui_require_input(tab, tab->code == 11);
194 } else if (tab->code == 20) {
195 if (setup_parser_for(tab)) {
196 imsg_compose(netibuf, IMSG_PROCEED, tab->id, 0, -1, NULL, 0);
197 imsg_flush(netibuf);
198 } else {
199 load_page_from_str(tab, err_pages[UNKNOWN_TYPE_OR_CSET]);
201 } else if (tab->code < 40) { /* 3x */
202 tab->redirect_count++;
204 /* TODO: make customizable? */
205 if (tab->redirect_count > 5) {
206 load_page_from_str(tab,
207 err_pages[TOO_MUCH_REDIRECTS]);
208 } else
209 do_load_url(tab, tab->meta);
210 } else { /* 4x, 5x & 6x */
211 load_page_from_str(tab, err_pages[tab->code]);
215 static void
216 handle_imsg_buf(struct imsg *imsg, size_t datalen)
218 struct tab *tab;
220 tab = tab_by_id(imsg->hdr.peerid);
222 if (!tab->window.page.parse(&tab->window.page, imsg->data, datalen))
223 die();
225 ui_on_tab_refresh(tab);
228 static void
229 handle_imsg_eof(struct imsg *imsg, size_t datalen)
231 struct tab *tab;
233 tab = tab_by_id(imsg->hdr.peerid);
234 if (!tab->window.page.free(&tab->window.page))
235 die();
237 ui_on_tab_refresh(tab);
238 ui_on_tab_loaded(tab);
241 static void
242 handle_imsg_bookmark_ok(struct imsg *imsg, size_t datalen)
244 int res;
246 if (datalen != sizeof(res))
247 die();
249 memcpy(&res, imsg->data, sizeof(res));
250 if (res == 0)
251 ui_notify("Added to bookmarks!");
252 else
253 ui_notify("Failed to add to bookmarks: %s",
254 strerror(res));
257 static void
258 handle_imsg_save_cert_ok(struct imsg *imsg, size_t datalen)
260 int res;
262 if (datalen != sizeof(res))
263 die();
264 memcpy(&res, imsg->data, datalen);
265 if (res != 0)
266 ui_notify("Failed to save the cert for: %s",
267 strerror(res));
270 static void
271 handle_dispatch_imsg(int fd, short ev, void *d)
273 struct imsgbuf *ibuf = d;
274 dispatch_imsg(ibuf, handlers, sizeof(handlers));
277 static void
278 load_page_from_str(struct tab *tab, const char *page)
280 gemtext_initparser(&tab->window.page);
281 if (!tab->window.page.parse(&tab->window.page, page, strlen(page)))
282 die();
283 if (!tab->window.page.free(&tab->window.page))
284 die();
285 ui_on_tab_refresh(tab);
286 ui_on_tab_loaded(tab);
289 void
290 load_about_url(struct tab *tab, const char *url)
292 char *m;
293 size_t len;
295 tab->trust = TS_VERIFIED;
297 memset(&tab->url, 0, sizeof(tab->url));
299 m = strchr(url, ':');
300 strlcpy(tab->url.scheme, "about", sizeof(tab->url.scheme));
301 strlcpy(tab->url.path, m+1, sizeof(tab->url.path));
303 len = sizeof(tab->hist_cur->h)-1;
304 strlcpy(tab->hist_cur->h, url, len);
306 gemtext_initparser(&tab->window.page);
308 imsg_compose(fsibuf, IMSG_GET, tab->id, 0, -1,
309 tab->hist_cur->h, len+1);
310 imsg_flush(fsibuf);
313 void
314 load_gemini_url(struct tab *tab, const char *url)
316 const char *err;
317 char *p;
318 size_t len;
320 len = sizeof(tab->hist_cur->h)-1;
322 if (has_prefix(url, "gemini:")) {
323 if (!url_parse(url, &tab->url, &err))
324 goto err;
325 } else {
326 if (!url_resolve_from(&tab->url, url, &err))
327 goto err;
330 url_unparse(&tab->url, tab->hist_cur->h, len);
331 imsg_compose(netibuf, IMSG_GET, tab->id, 0, -1,
332 tab->hist_cur->h, len+1);
333 imsg_flush(netibuf);
334 return;
336 err:
337 if (asprintf(&p, "#error loading %s\n>%s\n",
338 url, err) == -1)
339 die();
340 strlcpy(tab->hist_cur->h, url, len);
341 load_page_from_str(tab, p);
342 free(p);
345 static void
346 do_load_url(struct tab *tab, const char *url)
348 struct proto *p;
350 tab->trust = TS_UNKNOWN;
352 for (p = protos; p->schema != NULL; ++p) {
353 if (has_prefix(url, p->schema)) {
354 p->loadfn(tab, url);
355 return;
359 protos[0].loadfn(tab, url);
362 void
363 load_url(struct tab *tab, const char *url)
365 if (tab->hist_cur != NULL)
366 hist_clear_forward(&tab->hist, TAILQ_NEXT(tab->hist_cur, entries));
368 if ((tab->hist_cur = calloc(1, sizeof(*tab->hist_cur))) == NULL) {
369 event_loopbreak();
370 return;
372 hist_push(&tab->hist, tab->hist_cur);
373 do_load_url(tab, url);
376 int
377 load_previous_page(struct tab *tab)
379 struct hist *h;
381 if ((h = TAILQ_PREV(tab->hist_cur, mhisthead, entries)) == NULL)
382 return 0;
383 tab->hist_cur = h;
384 do_load_url(tab, h->h);
385 return 1;
388 int
389 load_next_page(struct tab *tab)
391 struct hist *h;
393 if ((h = TAILQ_NEXT(tab->hist_cur, entries)) == NULL)
394 return 0;
395 tab->hist_cur = h;
396 do_load_url(tab, h->h);
397 return 1;
400 void
401 stop_tab(struct tab *tab)
403 imsg_compose(netibuf, IMSG_STOP, tab->id, 0, -1, NULL, 0);
404 imsg_flush(netibuf);
407 void
408 add_to_bookmarks(const char *str)
410 imsg_compose(fsibuf, IMSG_BOOKMARK_PAGE, 0, 0, -1, str, strlen(str)+1);
411 imsg_flush(fsibuf);
414 int
415 main(int argc, char * const *argv)
417 struct imsgbuf network_ibuf, fs_ibuf;
418 int net_fds[2], fs_fds[2];
419 pid_t pid;
421 pid = getpid();
423 signal(SIGCHLD, SIG_IGN);
425 /* initialize part of the fs layer. Before starting the UI
426 * and dropping the priviledges we need to read some stuff. */
427 fs_init();
429 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, fs_fds) == -1)
430 err(1, "socketpair");
432 switch (fork()) {
433 case -1:
434 err(1, "fork");
435 case 0:
436 /* child */
437 setproctitle("(%d) fs", pid);
438 close(fs_fds[0]);
439 imsg_init(&fs_ibuf, fs_fds[1]);
440 exit(fs_main(&fs_ibuf));
441 default:
442 close(fs_fds[1]);
443 imsg_init(&fs_ibuf, fs_fds[0]);
444 fsibuf = &fs_ibuf;
447 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, net_fds) == -1)
448 err(1, "socketpair");
450 switch (fork()) {
451 case -1:
452 err(1, "fork");
453 case 0:
454 /* child */
455 setproctitle("(%d) client", pid);
456 close(net_fds[0]);
457 close(fs_fds[0]);
458 imsg_init(&network_ibuf, net_fds[1]);
459 exit(client_main(&network_ibuf));
460 default:
461 close(net_fds[1]);
462 imsg_init(&network_ibuf, net_fds[0]);
463 netibuf = &network_ibuf;
466 setproctitle("(%d) ui", pid);
468 telescope_ohash_init(&certs, 5, offsetof(struct tofu_entry, domain));
469 load_certs(&certs);
471 TAILQ_INIT(&tabshead);
473 event_init();
475 event_set(&netev, netibuf->fd, EV_READ | EV_PERSIST, handle_dispatch_imsg, netibuf);
476 event_add(&netev, NULL);
478 event_set(&fsev, fsibuf->fd, EV_READ | EV_PERSIST, handle_dispatch_imsg, fsibuf);
479 event_add(&fsev, NULL);
481 if (ui_init(argc, argv)) {
482 sandbox_ui_process();
483 event_dispatch();
484 ui_end();
487 imsg_compose(netibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
488 imsg_flush(netibuf);
490 imsg_compose(fsibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
491 imsg_flush(fsibuf);
493 return 0;