3 #include <sys/socket.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 },
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,
49 static struct ohash certs;
51 static void __attribute__((__noreturn__))
58 tab_by_id(uint32_t id)
62 TAILQ_FOREACH(t, &tabshead, tabs) {
71 handle_imsg_err(struct imsg *imsg, size_t datalen)
76 tab = tab_by_id(imsg->hdr.peerid);
79 page[datalen-1] = '\0';
81 if (asprintf(&page, "# Error loading %s\n\n> %s\n",
82 tab->hist_cur->h, page) == -1)
84 load_page_from_str(tab, page);
89 handle_imsg_check_cert(struct imsg *imsg, size_t datalen)
97 if (hash[datalen-1] != '\0')
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
106 if (datalen > sizeof(e->hash))
109 tofu_res = 1; /* trust on first use */
110 if ((e = calloc(1, sizeof(*e))) == NULL)
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,
119 tofu_res = !strcmp(hash, e->hash);
122 tab->trust = e->verified ? TS_VERIFIED : TS_TRUSTED;
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));
133 normalize_code(int n)
136 if (n == 10 || n == 11)
142 if (n == 30 || n == 31)
150 if (n <= 53 || n == 59)
158 return MALFORMED_RESPONSE;
162 handle_imsg_got_code(struct imsg *imsg, size_t datalen)
166 tab = tab_by_id(imsg->hdr.peerid);
168 if (sizeof(tab->code) != datalen)
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;
178 handle_imsg_got_meta(struct imsg *imsg, size_t datalen)
182 tab = tab_by_id(imsg->hdr.peerid);
184 if (sizeof(tab->meta) <= datalen)
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);
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]);
209 do_load_url(tab, tab->meta);
210 } else { /* 4x, 5x & 6x */
211 load_page_from_str(tab, err_pages[tab->code]);
216 handle_imsg_buf(struct imsg *imsg, size_t datalen)
220 tab = tab_by_id(imsg->hdr.peerid);
222 if (!tab->page.parse(&tab->page, imsg->data, datalen))
225 ui_on_tab_refresh(tab);
229 handle_imsg_eof(struct imsg *imsg, size_t datalen)
233 t = tab_by_id(imsg->hdr.peerid);
234 if (!t->page.free(&t->page))
237 ui_on_tab_refresh(t);
242 handle_imsg_bookmark_ok(struct imsg *imsg, size_t datalen)
246 if (datalen != sizeof(res))
249 memcpy(&res, imsg->data, sizeof(res));
251 ui_notify("Added to bookmarks!");
253 ui_notify("Failed to add to bookmarks: %s",
258 handle_imsg_save_cert_ok(struct imsg *imsg, size_t datalen)
262 if (datalen != sizeof(res))
264 memcpy(&res, imsg->data, datalen);
266 ui_notify("Failed to save the cert for: %s",
271 handle_dispatch_imsg(int fd, short ev, void *d)
273 struct imsgbuf *ibuf = d;
274 dispatch_imsg(ibuf, handlers, sizeof(handlers));
278 load_page_from_str(struct tab *tab, const char *page)
280 gemtext_initparser(&tab->page);
281 if (!tab->page.parse(&tab->page, page, strlen(page)))
283 if (!tab->page.free(&tab->page))
285 ui_on_tab_refresh(tab);
286 ui_on_tab_loaded(tab);
290 load_about_url(struct tab *tab, const char *url)
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->page);
308 imsg_compose(fsibuf, IMSG_GET, tab->id, 0, -1,
309 tab->hist_cur->h, len+1);
314 load_gemini_url(struct tab *tab, const char *url)
320 len = sizeof(tab->hist_cur->h)-1;
322 if (has_prefix(url, "gemini:")) {
323 if (!url_parse(url, &tab->url, &err))
326 if (!url_resolve_from(&tab->url, url, &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);
337 if (asprintf(&p, "#error loading %s\n>%s\n",
340 strlcpy(tab->hist_cur->h, url, len);
341 load_page_from_str(tab, p);
346 do_load_url(struct tab *tab, const char *url)
350 tab->trust = TS_UNKNOWN;
352 for (p = protos; p->schema != NULL; ++p) {
353 if (has_prefix(url, p->schema)) {
359 protos[0].loadfn(tab, url);
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) {
372 hist_push(&tab->hist, tab->hist_cur);
373 do_load_url(tab, url);
377 load_previous_page(struct tab *tab)
381 if ((h = TAILQ_PREV(tab->hist_cur, mhisthead, entries)) == NULL)
384 do_load_url(tab, h->h);
389 load_next_page(struct tab *tab)
393 if ((h = TAILQ_NEXT(tab->hist_cur, entries)) == NULL)
396 do_load_url(tab, h->h);
401 stop_tab(struct tab *tab)
403 imsg_compose(netibuf, IMSG_STOP, tab->id, 0, -1, NULL, 0);
408 add_to_bookmarks(const char *str)
410 imsg_compose(fsibuf, IMSG_BOOKMARK_PAGE, 0, 0, -1, str, strlen(str)+1);
417 struct imsgbuf network_ibuf, fs_ibuf;
418 int net_fds[2], fs_fds[2];
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. */
429 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, fs_fds) == -1)
430 err(1, "socketpair");
437 setproctitle("(%d) fs", pid);
439 imsg_init(&fs_ibuf, fs_fds[1]);
440 exit(fs_main(&fs_ibuf));
443 imsg_init(&fs_ibuf, fs_fds[0]);
447 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, net_fds) == -1)
448 err(1, "socketpair");
455 setproctitle("(%d) client", pid);
458 imsg_init(&network_ibuf, net_fds[1]);
459 exit(client_main(&network_ibuf));
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));
471 TAILQ_INIT(&tabshead);
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);
483 sandbox_ui_process();
487 imsg_compose(netibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
490 imsg_compose(fsibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);