Blob


1 #include "telescope.h"
3 #include <sys/socket.h>
5 #include <err.h>
6 #include <errno.h>
7 #include <signal.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <unistd.h>
13 struct event netev, fsev;
14 struct tabshead tabshead;
16 /* the first is also the fallback one */
17 static struct proto protos[] = {
18 { "gemini:", load_gemini_url },
19 { "about:", load_about_url },
20 { NULL, NULL },
21 };
23 static struct imsgbuf *netibuf, *fsibuf;
25 static void die(void) __attribute__((__noreturn__));
26 static struct tab *tab_by_id(uint32_t);
27 static void handle_imsg_err(struct imsg*, size_t);
28 static void handle_imsg_check_cert(struct imsg*, size_t);
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 else {
125 tab->trust = TS_UNTRUSTED;
126 load_page_from_str(tab, "# Certificate mismatch\n");
128 imsg_compose(netibuf, IMSG_CERT_STATUS, imsg->hdr.peerid, 0, -1,
129 &tofu_res, sizeof(tofu_res));
130 imsg_flush(netibuf);
133 static inline int
134 normalize_code(int n)
136 if (n < 20) {
137 if (n == 10 || n == 11)
138 return n;
139 return 10;
140 } else if (n < 30) {
141 return 20;
142 } else if (n < 40) {
143 if (n == 30 || n == 31)
144 return n;
145 return 30;
146 } else if (n < 50) {
147 if (n <= 44)
148 return n;
149 return 40;
150 } else if (n < 60) {
151 if (n <= 53 || n == 59)
152 return n;
153 return 50;
154 } else if (n < 70) {
155 if (n <= 62)
156 return n;
157 return 60;
158 } else
159 return MALFORMED_RESPONSE;
162 static void
163 handle_imsg_got_code(struct imsg *imsg, size_t datalen)
165 struct tab *tab;
167 tab = tab_by_id(imsg->hdr.peerid);
169 if (sizeof(tab->code) != datalen)
170 die();
172 memcpy(&tab->code, imsg->data, sizeof(tab->code));
173 tab->code = normalize_code(tab->code);
174 if (tab->code != 30 && tab->code != 31)
175 tab->redirect_count = 0;
178 static void
179 handle_imsg_got_meta(struct imsg *imsg, size_t datalen)
181 struct tab *tab;
183 tab = tab_by_id(imsg->hdr.peerid);
185 if (sizeof(tab->meta) <= datalen)
186 die();
188 memcpy(tab->meta, imsg->data, datalen);
190 if (tab->code < 10) { /* internal errors */
191 load_page_from_str(tab, err_pages[tab->code]);
192 } else if (tab->code < 20) { /* 1x */
193 load_page_from_str(tab, err_pages[tab->code]);
194 ui_require_input(tab, tab->code == 11);
195 } else if (tab->code == 20) {
196 if (setup_parser_for(tab)) {
197 imsg_compose(netibuf, IMSG_PROCEED, tab->id, 0, -1, NULL, 0);
198 imsg_flush(netibuf);
199 } else {
200 load_page_from_str(tab, err_pages[UNKNOWN_TYPE_OR_CSET]);
202 } else if (tab->code < 40) { /* 3x */
203 tab->redirect_count++;
205 /* TODO: make customizable? */
206 if (tab->redirect_count > 5) {
207 load_page_from_str(tab,
208 err_pages[TOO_MUCH_REDIRECTS]);
209 } else
210 do_load_url(tab, tab->meta);
211 } else { /* 4x, 5x & 6x */
212 load_page_from_str(tab, err_pages[tab->code]);
216 static void
217 handle_imsg_buf(struct imsg *imsg, size_t datalen)
219 struct tab *tab;
221 tab = tab_by_id(imsg->hdr.peerid);
223 if (!tab->page.parse(&tab->page, imsg->data, datalen))
224 die();
226 ui_on_tab_refresh(tab);
229 static void
230 handle_imsg_eof(struct imsg *imsg, size_t datalen)
232 struct tab *t;
234 t = tab_by_id(imsg->hdr.peerid);
235 if (!t->page.free(&t->page))
236 die();
238 ui_on_tab_refresh(t);
239 ui_on_tab_loaded(t);
242 static void
243 handle_imsg_bookmark_ok(struct imsg *imsg, size_t datalen)
245 int res;
247 if (datalen != sizeof(res))
248 die();
250 memcpy(&res, imsg->data, sizeof(res));
251 if (res == 0)
252 ui_notify("Added to bookmarks!");
253 else
254 ui_notify("Failed to add to bookmarks: %s",
255 strerror(res));
258 static void
259 handle_imsg_save_cert_ok(struct imsg *imsg, size_t datalen)
261 int res;
263 if (datalen != sizeof(res))
264 die();
265 memcpy(&res, imsg->data, datalen);
266 if (res != 0)
267 ui_notify("Failed to save the cert for: %s",
268 strerror(res));
271 static void
272 handle_dispatch_imsg(int fd, short ev, void *d)
274 struct imsgbuf *ibuf = d;
275 dispatch_imsg(ibuf, handlers, sizeof(handlers));
278 static void
279 load_page_from_str(struct tab *tab, const char *page)
281 gemtext_initparser(&tab->page);
282 if (!tab->page.parse(&tab->page, page, strlen(page)))
283 die();
284 if (!tab->page.free(&tab->page))
285 die();
286 ui_on_tab_refresh(tab);
287 ui_on_tab_loaded(tab);
290 void
291 load_about_url(struct tab *tab, const char *url)
293 char *m;
294 size_t len;
296 tab->trust = TS_VERIFIED;
298 memset(&tab->url, 0, sizeof(tab->url));
300 m = strchr(url, ':');
301 strlcpy(tab->url.scheme, "about", sizeof(tab->url.scheme));
302 strlcpy(tab->url.path, m+1, sizeof(tab->url.path));
304 len = sizeof(tab->hist_cur->h)-1;
305 strlcpy(tab->hist_cur->h, url, len);
307 gemtext_initparser(&tab->page);
309 imsg_compose(fsibuf, IMSG_GET, tab->id, 0, -1,
310 tab->hist_cur->h, len+1);
311 imsg_flush(fsibuf);
314 void
315 load_gemini_url(struct tab *tab, const char *url)
317 const char *err;
318 char *p;
319 size_t len;
321 len = sizeof(tab->hist_cur->h)-1;
323 if (has_prefix(url, "gemini:")) {
324 if (!url_parse(url, &tab->url, &err))
325 goto err;
326 } else {
327 if (!url_resolve_from(&tab->url, url, &err))
328 goto err;
331 url_unparse(&tab->url, tab->hist_cur->h, len);
332 imsg_compose(netibuf, IMSG_GET, tab->id, 0, -1,
333 tab->hist_cur->h, len+1);
334 imsg_flush(netibuf);
335 return;
337 err:
338 if (asprintf(&p, "#error loading %s\n>%s\n",
339 url, err) == -1)
340 die();
341 strlcpy(tab->hist_cur->h, url, len);
342 load_page_from_str(tab, p);
343 free(p);
346 static void
347 do_load_url(struct tab *tab, const char *url)
349 struct proto *p;
351 tab->trust = TS_UNKNOWN;
353 for (p = protos; p->schema != NULL; ++p) {
354 if (has_prefix(url, p->schema)) {
355 p->loadfn(tab, url);
356 return;
360 protos[0].loadfn(tab, url);
363 void
364 load_url(struct tab *tab, const char *url)
366 if (tab->hist_cur != NULL)
367 hist_clear_forward(&tab->hist, TAILQ_NEXT(tab->hist_cur, entries));
369 if ((tab->hist_cur = calloc(1, sizeof(*tab->hist_cur))) == NULL) {
370 event_loopbreak();
371 return;
373 hist_push(&tab->hist, tab->hist_cur);
374 do_load_url(tab, url);
377 int
378 load_previous_page(struct tab *tab)
380 struct hist *h;
382 if ((h = TAILQ_PREV(tab->hist_cur, mhisthead, entries)) == NULL)
383 return 0;
384 tab->hist_cur = h;
385 do_load_url(tab, h->h);
386 return 1;
389 int
390 load_next_page(struct tab *tab)
392 struct hist *h;
394 if ((h = TAILQ_NEXT(tab->hist_cur, entries)) == NULL)
395 return 0;
396 tab->hist_cur = h;
397 do_load_url(tab, h->h);
398 return 1;
401 void
402 stop_tab(struct tab *tab)
404 imsg_compose(netibuf, IMSG_STOP, tab->id, 0, -1, NULL, 0);
405 imsg_flush(netibuf);
408 void
409 add_to_bookmarks(const char *str)
411 imsg_compose(fsibuf, IMSG_BOOKMARK_PAGE, 0, 0, -1, str, strlen(str)+1);
412 imsg_flush(fsibuf);
415 int
416 main(void)
418 struct imsgbuf network_ibuf, fs_ibuf;
419 int net_fds[2], fs_fds[2];
420 pid_t pid;
422 pid = getpid();
424 signal(SIGCHLD, SIG_IGN);
426 /* initialize part of the fs layer. Before starting the UI
427 * and dropping the priviledges we need to read some stuff. */
428 fs_init();
430 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, fs_fds) == -1)
431 err(1, "socketpair");
433 switch (fork()) {
434 case -1:
435 err(1, "fork");
436 case 0:
437 /* child */
438 setproctitle("(%d) fs", pid);
439 close(fs_fds[0]);
440 imsg_init(&fs_ibuf, fs_fds[1]);
441 exit(fs_main(&fs_ibuf));
442 default:
443 close(fs_fds[1]);
444 imsg_init(&fs_ibuf, fs_fds[0]);
445 fsibuf = &fs_ibuf;
448 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, net_fds) == -1)
449 err(1, "socketpair");
451 switch (fork()) {
452 case -1:
453 err(1, "fork");
454 case 0:
455 /* child */
456 setproctitle("(%d) client", pid);
457 close(net_fds[0]);
458 close(fs_fds[0]);
459 imsg_init(&network_ibuf, net_fds[1]);
460 exit(client_main(&network_ibuf));
461 default:
462 close(net_fds[1]);
463 imsg_init(&network_ibuf, net_fds[0]);
464 netibuf = &network_ibuf;
467 setproctitle("(%d) ui", pid);
469 telescope_ohash_init(&certs, 5, offsetof(struct tofu_entry, domain));
470 load_certs(&certs);
472 TAILQ_INIT(&tabshead);
474 event_init();
476 event_set(&netev, netibuf->fd, EV_READ | EV_PERSIST, handle_dispatch_imsg, netibuf);
477 event_add(&netev, NULL);
479 event_set(&fsev, fsibuf->fd, EV_READ | EV_PERSIST, handle_dispatch_imsg, fsibuf);
480 event_add(&fsev, NULL);
482 ui_init();
484 sandbox_ui_process();
486 event_dispatch();
488 imsg_compose(netibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
489 imsg_flush(netibuf);
491 imsg_compose(fsibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
492 imsg_flush(fsibuf);
494 ui_end();
496 return 0;