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_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 };
48 static struct ohash certs;
50 static void __attribute__((__noreturn__))
51 die(void)
52 {
53 abort(); /* TODO */
54 }
56 static struct tab *
57 tab_by_id(uint32_t id)
58 {
59 struct tab *t;
61 TAILQ_FOREACH(t, &tabshead, tabs) {
62 if (t->id == id)
63 return t;
64 }
66 die();
67 }
69 static void
70 handle_imsg_err(struct imsg *imsg, size_t datalen)
71 {
72 struct tab *tab;
73 char *page;
75 tab = tab_by_id(imsg->hdr.peerid);
77 page = imsg->data;
78 page[datalen-1] = '\0';
80 if (asprintf(&page, "# Error loading %s\n\n> %s\n",
81 tab->hist_cur->h, page) == -1)
82 die();
83 load_page_from_str(tab, page);
84 free(page);
85 }
87 static void
88 handle_imsg_check_cert(struct imsg *imsg, size_t datalen)
89 {
90 const char *hash;
91 int tofu_res;
92 struct tofu_entry *e;
93 struct tab *tab;
95 hash = imsg->data;
96 if (hash[datalen-1] != '\0')
97 abort();
99 tab = tab_by_id(imsg->hdr.peerid);
101 if ((e = telescope_lookup_tofu(&certs, tab->url.host)) == NULL) {
102 /* TODO: an update in libressl/libretls changed
103 * significantly. Find a better approach at storing
104 * the certs! */
105 if (datalen > sizeof(e->hash))
106 abort();
108 tofu_res = 1; /* trust on first use */
109 if ((e = calloc(1, sizeof(*e))) == NULL)
110 abort();
111 strlcpy(e->domain, tab->url.host, sizeof(e->domain));
112 strlcpy(e->hash, hash, sizeof(e->hash));
113 telescope_ohash_insert(&certs, e);
114 } else
115 tofu_res = !strcmp(hash, e->hash);
117 if (tofu_res)
118 tab->trust = e->verified ? TS_VERIFIED : TS_TRUSTED;
119 else {
120 tab->trust = TS_UNTRUSTED;
121 load_page_from_str(tab, "# Certificate mismatch\n");
123 imsg_compose(netibuf, IMSG_CERT_STATUS, imsg->hdr.peerid, 0, -1,
124 &tofu_res, sizeof(tofu_res));
125 imsg_flush(netibuf);
128 static inline int
129 normalize_code(int n)
131 if (n < 20) {
132 if (n == 10 || n == 11)
133 return n;
134 return 10;
135 } else if (n < 30) {
136 return 20;
137 } else if (n < 40) {
138 if (n == 30 || n == 31)
139 return n;
140 return 30;
141 } else if (n < 50) {
142 if (n <= 44)
143 return n;
144 return 40;
145 } else if (n < 60) {
146 if (n <= 53 || n == 59)
147 return n;
148 return 50;
149 } else if (n < 70) {
150 if (n <= 62)
151 return n;
152 return 60;
153 } else
154 return MALFORMED_RESPONSE;
157 static void
158 handle_imsg_got_code(struct imsg *imsg, size_t datalen)
160 struct tab *tab;
162 tab = tab_by_id(imsg->hdr.peerid);
164 if (sizeof(tab->code) != datalen)
165 die();
167 memcpy(&tab->code, imsg->data, sizeof(tab->code));
168 tab->code = normalize_code(tab->code);
169 if (tab->code != 30 && tab->code != 31)
170 tab->redirect_count = 0;
173 static void
174 handle_imsg_got_meta(struct imsg *imsg, size_t datalen)
176 struct tab *tab;
178 tab = tab_by_id(imsg->hdr.peerid);
180 if (sizeof(tab->meta) <= datalen)
181 die();
183 memcpy(tab->meta, imsg->data, datalen);
185 if (tab->code < 10) { /* internal errors */
186 load_page_from_str(tab, err_pages[tab->code]);
187 } else if (tab->code < 20) { /* 1x */
188 load_page_from_str(tab, err_pages[tab->code]);
189 ui_require_input(tab, tab->code == 11);
190 } else if (tab->code == 20) {
191 if (setup_parser_for(tab)) {
192 imsg_compose(netibuf, IMSG_PROCEED, tab->id, 0, -1, NULL, 0);
193 imsg_flush(netibuf);
194 } else {
195 load_page_from_str(tab, err_pages[UNKNOWN_TYPE_OR_CSET]);
197 } else if (tab->code < 40) { /* 3x */
198 tab->redirect_count++;
200 /* TODO: make customizable? */
201 if (tab->redirect_count > 5) {
202 load_page_from_str(tab,
203 err_pages[TOO_MUCH_REDIRECTS]);
204 } else
205 do_load_url(tab, tab->meta);
206 } else { /* 4x, 5x & 6x */
207 load_page_from_str(tab, err_pages[tab->code]);
211 static void
212 handle_imsg_buf(struct imsg *imsg, size_t datalen)
214 struct tab *tab;
216 tab = tab_by_id(imsg->hdr.peerid);
218 if (!tab->page.parse(&tab->page, imsg->data, datalen))
219 die();
221 ui_on_tab_refresh(tab);
224 static void
225 handle_imsg_eof(struct imsg *imsg, size_t datalen)
227 struct tab *t;
229 t = tab_by_id(imsg->hdr.peerid);
230 if (!t->page.free(&t->page))
231 die();
233 ui_on_tab_refresh(t);
234 ui_on_tab_loaded(t);
237 static void
238 handle_imsg_bookmark_ok(struct imsg *imsg, size_t datalen)
240 int res;
242 if (datalen != sizeof(res))
243 die();
245 memcpy(&res, imsg->data, sizeof(res));
246 if (res == 0)
247 ui_notify("Added to bookmarks!");
248 else
249 ui_notify("Failed to add to bookmarks: %s",
250 strerror(res));
253 static void
254 handle_dispatch_imsg(int fd, short ev, void *d)
256 struct imsgbuf *ibuf = d;
257 dispatch_imsg(ibuf, handlers, sizeof(handlers));
260 static void
261 load_page_from_str(struct tab *tab, const char *page)
263 gemtext_initparser(&tab->page);
264 if (!tab->page.parse(&tab->page, page, strlen(page)))
265 die();
266 if (!tab->page.free(&tab->page))
267 die();
268 ui_on_tab_refresh(tab);
269 ui_on_tab_loaded(tab);
272 void
273 load_about_url(struct tab *tab, const char *url)
275 char *m;
276 size_t len;
278 tab->trust = TS_VERIFIED;
280 memset(&tab->url, 0, sizeof(tab->url));
282 m = strchr(url, ':');
283 strlcpy(tab->url.scheme, "about", sizeof(tab->url.scheme));
284 strlcpy(tab->url.path, m+1, sizeof(tab->url.path));
286 len = sizeof(tab->hist_cur->h)-1;
287 strlcpy(tab->hist_cur->h, url, len);
289 gemtext_initparser(&tab->page);
291 imsg_compose(fsibuf, IMSG_GET, tab->id, 0, -1,
292 tab->hist_cur->h, len+1);
293 imsg_flush(fsibuf);
296 void
297 load_gemini_url(struct tab *tab, const char *url)
299 const char *err;
300 char *p;
301 size_t len;
303 len = sizeof(tab->hist_cur->h)-1;
305 if (has_prefix(url, "gemini:")) {
306 if (!url_parse(url, &tab->url, &err))
307 goto err;
308 } else {
309 if (!url_resolve_from(&tab->url, url, &err))
310 goto err;
313 url_unparse(&tab->url, tab->hist_cur->h, len);
314 imsg_compose(netibuf, IMSG_GET, tab->id, 0, -1,
315 tab->hist_cur->h, len+1);
316 imsg_flush(netibuf);
317 return;
319 err:
320 if (asprintf(&p, "#error loading %s\n>%s\n",
321 url, err) == -1)
322 die();
323 strlcpy(tab->hist_cur->h, url, len);
324 load_page_from_str(tab, p);
325 free(p);
328 static void
329 do_load_url(struct tab *tab, const char *url)
331 struct proto *p;
333 tab->trust = TS_UNKNOWN;
335 for (p = protos; p->schema != NULL; ++p) {
336 if (has_prefix(url, p->schema)) {
337 p->loadfn(tab, url);
338 return;
342 protos[0].loadfn(tab, url);
345 void
346 load_url(struct tab *tab, const char *url)
348 if (tab->hist_cur != NULL)
349 hist_clear_forward(&tab->hist, TAILQ_NEXT(tab->hist_cur, entries));
351 if ((tab->hist_cur = calloc(1, sizeof(*tab->hist_cur))) == NULL) {
352 event_loopbreak();
353 return;
355 hist_push(&tab->hist, tab->hist_cur);
356 do_load_url(tab, url);
359 int
360 load_previous_page(struct tab *tab)
362 struct hist *h;
364 if ((h = TAILQ_PREV(tab->hist_cur, mhisthead, entries)) == NULL)
365 return 0;
366 tab->hist_cur = h;
367 do_load_url(tab, h->h);
368 return 1;
371 int
372 load_next_page(struct tab *tab)
374 struct hist *h;
376 if ((h = TAILQ_NEXT(tab->hist_cur, entries)) == NULL)
377 return 0;
378 tab->hist_cur = h;
379 do_load_url(tab, h->h);
380 return 1;
383 void
384 stop_tab(struct tab *tab)
386 imsg_compose(netibuf, IMSG_STOP, tab->id, 0, -1, NULL, 0);
387 imsg_flush(netibuf);
390 void
391 add_to_bookmarks(const char *str)
393 imsg_compose(fsibuf, IMSG_BOOKMARK_PAGE, 0, 0, -1, str, strlen(str)+1);
394 imsg_flush(fsibuf);
397 int
398 main(void)
400 struct imsgbuf network_ibuf, fs_ibuf;
401 int net_fds[2], fs_fds[2];
402 pid_t pid;
404 pid = getpid();
406 signal(SIGCHLD, SIG_IGN);
408 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, fs_fds) == -1)
409 err(1, "socketpair");
411 switch (fork()) {
412 case -1:
413 err(1, "fork");
414 case 0:
415 /* child */
416 setproctitle("(%d) fs", pid);
417 close(fs_fds[0]);
418 imsg_init(&fs_ibuf, fs_fds[1]);
419 exit(fs_main(&fs_ibuf));
420 default:
421 close(fs_fds[1]);
422 imsg_init(&fs_ibuf, fs_fds[0]);
423 fsibuf = &fs_ibuf;
426 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, net_fds) == -1)
427 err(1, "socketpair");
429 switch (fork()) {
430 case -1:
431 err(1, "fork");
432 case 0:
433 /* child */
434 setproctitle("(%d) client", pid);
435 close(net_fds[0]);
436 close(fs_fds[0]);
437 imsg_init(&network_ibuf, net_fds[1]);
438 exit(client_main(&network_ibuf));
439 default:
440 close(net_fds[1]);
441 imsg_init(&network_ibuf, net_fds[0]);
442 netibuf = &network_ibuf;
445 setproctitle("(%d) ui", pid);
447 telescope_ohash_init(&certs, 5, offsetof(struct tofu_entry, domain));
449 TAILQ_INIT(&tabshead);
451 event_init();
453 event_set(&netev, netibuf->fd, EV_READ | EV_PERSIST, handle_dispatch_imsg, netibuf);
454 event_add(&netev, NULL);
456 event_set(&fsev, fsibuf->fd, EV_READ | EV_PERSIST, handle_dispatch_imsg, fsibuf);
457 event_add(&fsev, NULL);
459 ui_init();
461 sandbox_ui_process();
463 event_dispatch();
465 imsg_compose(netibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
466 imsg_flush(netibuf);
468 imsg_compose(fsibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
469 imsg_flush(fsibuf);
471 ui_end();
473 return 0;