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_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,
50 static struct ohash certs;
52 static void __attribute__((__noreturn__))
59 tab_by_id(uint32_t id)
63 TAILQ_FOREACH(t, &tabshead, tabs) {
72 handle_imsg_err(struct imsg *imsg, size_t datalen)
77 tab = tab_by_id(imsg->hdr.peerid);
80 page[datalen-1] = '\0';
82 if (asprintf(&page, "# Error loading %s\n\n> %s\n",
83 tab->hist_cur->h, page) == -1)
85 load_page_from_str(tab, page);
90 handle_imsg_check_cert(struct imsg *imsg, size_t datalen)
98 if (hash[datalen-1] != '\0')
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
107 if (datalen > sizeof(e->hash))
110 tofu_res = 1; /* trust on first use */
111 if ((e = calloc(1, sizeof(*e))) == NULL)
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,
120 tofu_res = !strcmp(hash, e->hash);
123 tab->trust = e->verified ? TS_VERIFIED : TS_TRUSTED;
124 imsg_compose(netibuf, IMSG_CERT_STATUS, imsg->hdr.peerid, 0, -1,
125 &tofu_res, sizeof(tofu_res));
128 tab->trust = TS_UNTRUSTED;
129 load_page_from_str(tab, "# Certificate mismatch\n");
130 ui_yornp("Certificate mismatch. Proceed?",
131 handle_check_cert_user_choice);
136 handle_check_cert_user_choice(int accept, unsigned int tabid)
138 imsg_compose(netibuf, IMSG_CERT_STATUS, tabid, 0, -1,
139 &accept, sizeof(accept));
144 normalize_code(int n)
147 if (n == 10 || n == 11)
153 if (n == 30 || n == 31)
161 if (n <= 53 || n == 59)
169 return MALFORMED_RESPONSE;
173 handle_imsg_got_code(struct imsg *imsg, size_t datalen)
177 tab = tab_by_id(imsg->hdr.peerid);
179 if (sizeof(tab->code) != datalen)
182 memcpy(&tab->code, imsg->data, sizeof(tab->code));
183 tab->code = normalize_code(tab->code);
184 if (tab->code != 30 && tab->code != 31)
185 tab->redirect_count = 0;
189 handle_imsg_got_meta(struct imsg *imsg, size_t datalen)
193 tab = tab_by_id(imsg->hdr.peerid);
195 if (sizeof(tab->meta) <= datalen)
198 memcpy(tab->meta, imsg->data, datalen);
200 if (tab->code < 10) { /* internal errors */
201 load_page_from_str(tab, err_pages[tab->code]);
202 } else if (tab->code < 20) { /* 1x */
203 load_page_from_str(tab, err_pages[tab->code]);
204 ui_require_input(tab, tab->code == 11);
205 } else if (tab->code == 20) {
206 if (setup_parser_for(tab)) {
207 imsg_compose(netibuf, IMSG_PROCEED, tab->id, 0, -1, NULL, 0);
210 load_page_from_str(tab, err_pages[UNKNOWN_TYPE_OR_CSET]);
212 } else if (tab->code < 40) { /* 3x */
213 tab->redirect_count++;
215 /* TODO: make customizable? */
216 if (tab->redirect_count > 5) {
217 load_page_from_str(tab,
218 err_pages[TOO_MUCH_REDIRECTS]);
220 do_load_url(tab, tab->meta);
221 } else { /* 4x, 5x & 6x */
222 load_page_from_str(tab, err_pages[tab->code]);
227 handle_imsg_buf(struct imsg *imsg, size_t datalen)
231 tab = tab_by_id(imsg->hdr.peerid);
233 if (!tab->window.page.parse(&tab->window.page, imsg->data, datalen))
236 ui_on_tab_refresh(tab);
240 handle_imsg_eof(struct imsg *imsg, size_t datalen)
244 tab = tab_by_id(imsg->hdr.peerid);
245 if (!tab->window.page.free(&tab->window.page))
248 ui_on_tab_refresh(tab);
249 ui_on_tab_loaded(tab);
253 handle_imsg_bookmark_ok(struct imsg *imsg, size_t datalen)
257 if (datalen != sizeof(res))
260 memcpy(&res, imsg->data, sizeof(res));
262 ui_notify("Added to bookmarks!");
264 ui_notify("Failed to add to bookmarks: %s",
269 handle_imsg_save_cert_ok(struct imsg *imsg, size_t datalen)
273 if (datalen != sizeof(res))
275 memcpy(&res, imsg->data, datalen);
277 ui_notify("Failed to save the cert for: %s",
282 handle_dispatch_imsg(int fd, short ev, void *d)
284 struct imsgbuf *ibuf = d;
285 dispatch_imsg(ibuf, handlers, sizeof(handlers));
289 load_page_from_str(struct tab *tab, const char *page)
291 gemtext_initparser(&tab->window.page);
292 if (!tab->window.page.parse(&tab->window.page, page, strlen(page)))
294 if (!tab->window.page.free(&tab->window.page))
296 ui_on_tab_refresh(tab);
297 ui_on_tab_loaded(tab);
301 load_about_url(struct tab *tab, const char *url)
306 tab->trust = TS_VERIFIED;
308 memset(&tab->url, 0, sizeof(tab->url));
310 m = strchr(url, ':');
311 strlcpy(tab->url.scheme, "about", sizeof(tab->url.scheme));
312 strlcpy(tab->url.path, m+1, sizeof(tab->url.path));
314 len = sizeof(tab->hist_cur->h)-1;
315 strlcpy(tab->hist_cur->h, url, len);
317 gemtext_initparser(&tab->window.page);
319 imsg_compose(fsibuf, IMSG_GET, tab->id, 0, -1,
320 tab->hist_cur->h, len+1);
325 load_gemini_url(struct tab *tab, const char *url)
331 len = sizeof(tab->hist_cur->h)-1;
333 if (has_prefix(url, "gemini:")) {
334 if (!url_parse(url, &tab->url, &err))
337 if (!url_resolve_from(&tab->url, url, &err))
341 url_unparse(&tab->url, tab->hist_cur->h, len);
342 imsg_compose(netibuf, IMSG_GET, tab->id, 0, -1,
343 tab->hist_cur->h, len+1);
348 if (asprintf(&p, "#error loading %s\n>%s\n",
351 strlcpy(tab->hist_cur->h, url, len);
352 load_page_from_str(tab, p);
357 do_load_url(struct tab *tab, const char *url)
361 tab->trust = TS_UNKNOWN;
363 for (p = protos; p->schema != NULL; ++p) {
364 if (has_prefix(url, p->schema)) {
370 protos[0].loadfn(tab, url);
374 load_url(struct tab *tab, const char *url)
376 if (tab->hist_cur != NULL)
377 hist_clear_forward(&tab->hist, TAILQ_NEXT(tab->hist_cur, entries));
379 if ((tab->hist_cur = calloc(1, sizeof(*tab->hist_cur))) == NULL) {
383 hist_push(&tab->hist, tab->hist_cur);
384 do_load_url(tab, url);
388 load_previous_page(struct tab *tab)
392 if ((h = TAILQ_PREV(tab->hist_cur, mhisthead, entries)) == NULL)
395 do_load_url(tab, h->h);
400 load_next_page(struct tab *tab)
404 if ((h = TAILQ_NEXT(tab->hist_cur, entries)) == NULL)
407 do_load_url(tab, h->h);
412 stop_tab(struct tab *tab)
414 imsg_compose(netibuf, IMSG_STOP, tab->id, 0, -1, NULL, 0);
419 add_to_bookmarks(const char *str)
421 imsg_compose(fsibuf, IMSG_BOOKMARK_PAGE, 0, 0, -1, str, strlen(str)+1);
426 main(int argc, char * const *argv)
428 struct imsgbuf network_ibuf, fs_ibuf;
429 int net_fds[2], fs_fds[2];
434 signal(SIGCHLD, SIG_IGN);
436 /* initialize part of the fs layer. Before starting the UI
437 * and dropping the priviledges we need to read some stuff. */
440 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, fs_fds) == -1)
441 err(1, "socketpair");
448 setproctitle("(%d) fs", pid);
450 imsg_init(&fs_ibuf, fs_fds[1]);
451 exit(fs_main(&fs_ibuf));
454 imsg_init(&fs_ibuf, fs_fds[0]);
458 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, net_fds) == -1)
459 err(1, "socketpair");
466 setproctitle("(%d) client", pid);
469 imsg_init(&network_ibuf, net_fds[1]);
470 exit(client_main(&network_ibuf));
473 imsg_init(&network_ibuf, net_fds[0]);
474 netibuf = &network_ibuf;
477 setproctitle("(%d) ui", pid);
479 telescope_ohash_init(&certs, 5, offsetof(struct tofu_entry, domain));
482 TAILQ_INIT(&tabshead);
486 event_set(&netev, netibuf->fd, EV_READ | EV_PERSIST, handle_dispatch_imsg, netibuf);
487 event_add(&netev, NULL);
489 event_set(&fsev, fsibuf->fd, EV_READ | EV_PERSIST, handle_dispatch_imsg, fsibuf);
490 event_add(&fsev, NULL);
492 if (ui_init(argc, argv)) {
493 sandbox_ui_process();
498 imsg_compose(netibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
501 imsg_compose(fsibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);