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 = tofu_lookup(&certs, tab->uri.host, tab->uri.port)) == 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 if (*tab->uri.port != '\0' && strcmp(tab->uri.port, "1965")) {
115 strlcat(e->domain, ":", sizeof(e->domain));
116 strlcat(e->domain, tab->uri.port, sizeof(e->domain));
118 strlcpy(e->hash, hash, sizeof(e->hash));
119 tofu_add(&certs, e);
120 imsg_compose(fsibuf, IMSG_SAVE_CERT, tab->id, 0, -1,
121 e, sizeof(*e));
122 imsg_flush(fsibuf);
123 } else
124 tofu_res = !strcmp(hash, e->hash);
126 if (tofu_res) {
127 tab->trust = e->verified ? TS_VERIFIED : TS_TRUSTED;
128 imsg_compose(netibuf, IMSG_CERT_STATUS, imsg->hdr.peerid, 0, -1,
129 &tofu_res, sizeof(tofu_res));
130 imsg_flush(netibuf);
131 } else {
132 tab->trust = TS_UNTRUSTED;
133 load_page_from_str(tab, "# Certificate mismatch\n");
134 ui_yornp("Certificate mismatch. Proceed?",
135 handle_check_cert_user_choice, tab->id);
139 static void
140 handle_check_cert_user_choice(int accept, unsigned int tabid)
142 imsg_compose(netibuf, IMSG_CERT_STATUS, tabid, 0, -1,
143 &accept, sizeof(accept));
144 imsg_flush(netibuf);
147 static inline int
148 normalize_code(int n)
150 if (n < 20) {
151 if (n == 10 || n == 11)
152 return n;
153 return 10;
154 } else if (n < 30) {
155 return 20;
156 } else if (n < 40) {
157 if (n == 30 || n == 31)
158 return n;
159 return 30;
160 } else if (n < 50) {
161 if (n <= 44)
162 return n;
163 return 40;
164 } else if (n < 60) {
165 if (n <= 53 || n == 59)
166 return n;
167 return 50;
168 } else if (n < 70) {
169 if (n <= 62)
170 return n;
171 return 60;
172 } else
173 return MALFORMED_RESPONSE;
176 static void
177 handle_imsg_got_code(struct imsg *imsg, size_t datalen)
179 struct tab *tab;
181 tab = tab_by_id(imsg->hdr.peerid);
183 if (sizeof(tab->code) != datalen)
184 die();
186 memcpy(&tab->code, imsg->data, sizeof(tab->code));
187 tab->code = normalize_code(tab->code);
188 if (tab->code != 30 && tab->code != 31)
189 tab->redirect_count = 0;
192 static void
193 handle_imsg_got_meta(struct imsg *imsg, size_t datalen)
195 struct tab *tab;
197 tab = tab_by_id(imsg->hdr.peerid);
199 if (sizeof(tab->meta) <= datalen)
200 die();
202 memcpy(tab->meta, imsg->data, datalen);
204 if (tab->code < 10) { /* internal errors */
205 load_page_from_str(tab, err_pages[tab->code]);
206 } else if (tab->code < 20) { /* 1x */
207 load_page_from_str(tab, err_pages[tab->code]);
208 ui_require_input(tab, tab->code == 11);
209 } else if (tab->code == 20) {
210 if (setup_parser_for(tab)) {
211 imsg_compose(netibuf, IMSG_PROCEED, tab->id, 0, -1, NULL, 0);
212 imsg_flush(netibuf);
213 } else {
214 load_page_from_str(tab, err_pages[UNKNOWN_TYPE_OR_CSET]);
216 } else if (tab->code < 40) { /* 3x */
217 tab->redirect_count++;
219 /* TODO: make customizable? */
220 if (tab->redirect_count > 5) {
221 load_page_from_str(tab,
222 err_pages[TOO_MUCH_REDIRECTS]);
223 } else
224 do_load_url(tab, tab->meta);
225 } else { /* 4x, 5x & 6x */
226 load_page_from_str(tab, err_pages[tab->code]);
230 static void
231 handle_imsg_buf(struct imsg *imsg, size_t datalen)
233 struct tab *tab;
235 tab = tab_by_id(imsg->hdr.peerid);
237 if (!tab->window.page.parse(&tab->window.page, imsg->data, datalen))
238 die();
240 ui_on_tab_refresh(tab);
243 static void
244 handle_imsg_eof(struct imsg *imsg, size_t datalen)
246 struct tab *tab;
248 tab = tab_by_id(imsg->hdr.peerid);
249 if (!tab->window.page.free(&tab->window.page))
250 die();
252 ui_on_tab_refresh(tab);
253 ui_on_tab_loaded(tab);
256 static void
257 handle_imsg_bookmark_ok(struct imsg *imsg, size_t datalen)
259 int res;
261 if (datalen != sizeof(res))
262 die();
264 memcpy(&res, imsg->data, sizeof(res));
265 if (res == 0)
266 ui_notify("Added to bookmarks!");
267 else
268 ui_notify("Failed to add to bookmarks: %s",
269 strerror(res));
272 static void
273 handle_imsg_save_cert_ok(struct imsg *imsg, size_t datalen)
275 int res;
277 if (datalen != sizeof(res))
278 die();
279 memcpy(&res, imsg->data, datalen);
280 if (res != 0)
281 ui_notify("Failed to save the cert for: %s",
282 strerror(res));
285 static void
286 handle_dispatch_imsg(int fd, short ev, void *d)
288 struct imsgbuf *ibuf = d;
289 dispatch_imsg(ibuf, handlers, sizeof(handlers));
292 static void
293 load_page_from_str(struct tab *tab, const char *page)
295 gemtext_initparser(&tab->window.page);
296 if (!tab->window.page.parse(&tab->window.page, page, strlen(page)))
297 die();
298 if (!tab->window.page.free(&tab->window.page))
299 die();
300 ui_on_tab_refresh(tab);
301 ui_on_tab_loaded(tab);
304 void
305 load_about_url(struct tab *tab, const char *url)
307 tab->trust = TS_VERIFIED;
309 gemtext_initparser(&tab->window.page);
311 imsg_compose(fsibuf, IMSG_GET, tab->id, 0, -1,
312 tab->hist_cur->h, strlen(tab->hist_cur->h)+1);
313 imsg_flush(fsibuf);
316 void
317 load_gemini_url(struct tab *tab, const char *url)
319 size_t len;
321 stop_tab(tab);
322 tab->id = tab_new_id();
324 len = sizeof(tab->hist_cur->h);
325 imsg_compose(netibuf, IMSG_GET, tab->id, 0, -1,
326 tab->hist_cur->h, len);
327 imsg_flush(netibuf);
328 return;
331 static void
332 do_load_url(struct tab *tab, const char *url)
334 struct phos_uri uri;
335 struct proto *p;
336 char *t;
338 tab->trust = TS_UNKNOWN;
340 memcpy(&uri, &tab->uri, sizeof(tab->uri));
341 if (!phos_resolve_uri_from_str(&uri, url, &tab->uri)) {
342 if (asprintf(&t, "#error loading %s\n>%s\n",
343 url, "Can't parse the URI") == -1)
344 die();
345 strlcpy(tab->hist_cur->h, url, sizeof(tab->hist_cur->h));
346 load_page_from_str(tab, t);
347 free(t);
348 return;
351 phos_serialize_uri(&tab->uri, tab->hist_cur->h,
352 sizeof(tab->hist_cur->h));
354 for (p = protos; p->schema != NULL; ++p) {
355 if (!strcmp(tab->uri.scheme, p->schema)) {
356 p->loadfn(tab, url);
357 return;
361 protos[0].loadfn(tab, url);
364 void
365 load_url(struct tab *tab, const char *url)
367 if (tab->hist_cur != NULL)
368 hist_clear_forward(&tab->hist, TAILQ_NEXT(tab->hist_cur, entries));
370 if ((tab->hist_cur = calloc(1, sizeof(*tab->hist_cur))) == NULL) {
371 event_loopbreak();
372 return;
374 hist_push(&tab->hist, tab->hist_cur);
375 do_load_url(tab, url);
376 empty_vlist(&tab->window);
377 empty_linelist(&tab->window);
380 int
381 load_previous_page(struct tab *tab)
383 struct hist *h;
385 if ((h = TAILQ_PREV(tab->hist_cur, mhisthead, entries)) == NULL)
386 return 0;
387 tab->hist_cur = h;
388 do_load_url(tab, h->h);
389 return 1;
392 int
393 load_next_page(struct tab *tab)
395 struct hist *h;
397 if ((h = TAILQ_NEXT(tab->hist_cur, entries)) == NULL)
398 return 0;
399 tab->hist_cur = h;
400 do_load_url(tab, h->h);
401 return 1;
404 void
405 stop_tab(struct tab *tab)
407 imsg_compose(netibuf, IMSG_STOP, tab->id, 0, -1, NULL, 0);
408 imsg_flush(netibuf);
411 void
412 add_to_bookmarks(const char *str)
414 imsg_compose(fsibuf, IMSG_BOOKMARK_PAGE, 0, 0, -1, str, strlen(str)+1);
415 imsg_flush(fsibuf);
418 void
419 save_session(void)
421 struct tab *tab;
423 imsg_compose(fsibuf, IMSG_SESSION_START, 0, 0, -1, NULL, 0);
424 imsg_flush(fsibuf);
426 TAILQ_FOREACH(tab, &tabshead, tabs) {
427 imsg_compose(fsibuf, IMSG_SESSION_TAB, 0, 0, -1,
428 tab->hist_cur->h, strlen(tab->hist_cur->h)+1);
429 imsg_flush(fsibuf);
432 imsg_compose(fsibuf, IMSG_SESSION_END, 0, 0, -1, NULL, 0);
433 imsg_flush(fsibuf);
436 int
437 main(int argc, char * const *argv)
439 struct imsgbuf network_ibuf, fs_ibuf;
440 int net_fds[2], fs_fds[2];
442 signal(SIGCHLD, SIG_IGN);
444 /* initialize part of the fs layer. Before starting the UI
445 * and dropping the priviledges we need to read some stuff. */
446 fs_init();
448 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, fs_fds) == -1)
449 err(1, "socketpair");
451 switch (fork()) {
452 case -1:
453 err(1, "fork");
454 case 0:
455 /* child */
456 setproctitle("fs");
457 close(fs_fds[0]);
458 imsg_init(&fs_ibuf, fs_fds[1]);
459 exit(fs_main(&fs_ibuf));
460 default:
461 close(fs_fds[1]);
462 imsg_init(&fs_ibuf, fs_fds[0]);
463 fsibuf = &fs_ibuf;
466 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, net_fds) == -1)
467 err(1, "socketpair");
469 switch (fork()) {
470 case -1:
471 err(1, "fork");
472 case 0:
473 /* child */
474 setproctitle("client");
475 close(net_fds[0]);
476 close(fs_fds[0]);
477 imsg_init(&network_ibuf, net_fds[1]);
478 exit(client_main(&network_ibuf));
479 default:
480 close(net_fds[1]);
481 imsg_init(&network_ibuf, net_fds[0]);
482 netibuf = &network_ibuf;
485 setproctitle("ui");
487 tofu_init(&certs, 5, offsetof(struct tofu_entry, domain));
488 load_certs(&certs);
490 TAILQ_INIT(&tabshead);
492 event_init();
494 event_set(&netev, netibuf->fd, EV_READ | EV_PERSIST, handle_dispatch_imsg, netibuf);
495 event_add(&netev, NULL);
497 event_set(&fsev, fsibuf->fd, EV_READ | EV_PERSIST, handle_dispatch_imsg, fsibuf);
498 event_add(&fsev, NULL);
500 if (ui_init(argc, argv)) {
501 sandbox_ui_process();
502 event_dispatch();
503 ui_end();
506 imsg_compose(netibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
507 imsg_flush(netibuf);
509 imsg_compose(fsibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
510 imsg_flush(fsibuf);
512 return 0;