3 #include <sys/socket.h>
13 struct event netev, fsev;
14 struct tabshead tabshead = TAILQ_HEAD_INITIALIZER(tabshead);
15 struct proxylist proxies = TAILQ_HEAD_INITIALIZER(proxies);
17 enum telescope_process {
23 /* the first is also the fallback one */
24 static struct proto protos[] = {
25 { "gemini", load_gemini_url },
26 { "about", load_about_url },
30 static struct imsgbuf *netibuf, *fsibuf;
32 static void die(void) __attribute__((__noreturn__));
33 static struct tab *tab_by_id(uint32_t);
34 static void handle_imsg_err(struct imsg*, size_t);
35 static void handle_imsg_check_cert(struct imsg*, size_t);
36 static void handle_check_cert_user_choice(int, struct tab *);
37 static void handle_maybe_save_new_cert(int, struct tab *);
38 static void handle_imsg_got_code(struct imsg*, size_t);
39 static void handle_imsg_got_meta(struct imsg*, size_t);
40 static void handle_maybe_save_page(int, struct tab *);
41 static void handle_save_page_path(const char *, unsigned int);
42 static void handle_imsg_file_opened(struct imsg*, size_t);
43 static void handle_imsg_buf(struct imsg*, size_t);
44 static void handle_imsg_eof(struct imsg*, size_t);
45 static void handle_imsg_bookmark_ok(struct imsg*, size_t);
46 static void handle_imsg_save_cert_ok(struct imsg*, size_t);
47 static void handle_imsg_update_cert_ok(struct imsg *, size_t);
48 static void handle_dispatch_imsg(int, short, void*);
49 static void load_page_from_str(struct tab*, const char*);
50 static void do_load_url(struct tab*, const char*);
51 static pid_t start_child(enum telescope_process, const char *, int);
53 static imsg_handlerfn *handlers[] = {
54 [IMSG_ERR] = handle_imsg_err,
55 [IMSG_CHECK_CERT] = handle_imsg_check_cert,
56 [IMSG_GOT_CODE] = handle_imsg_got_code,
57 [IMSG_GOT_META] = handle_imsg_got_meta,
58 [IMSG_BUF] = handle_imsg_buf,
59 [IMSG_EOF] = handle_imsg_eof,
60 [IMSG_BOOKMARK_OK] = handle_imsg_bookmark_ok,
61 [IMSG_SAVE_CERT_OK] = handle_imsg_save_cert_ok,
62 [IMSG_UPDATE_CERT_OK] = handle_imsg_update_cert_ok,
63 [IMSG_FILE_OPENED] = handle_imsg_file_opened,
66 static struct ohash certs;
68 static void __attribute__((__noreturn__))
75 tab_by_id(uint32_t id)
79 TAILQ_FOREACH(t, &tabshead, tabs) {
88 handle_imsg_err(struct imsg *imsg, size_t datalen)
93 tab = tab_by_id(imsg->hdr.peerid);
96 page[datalen-1] = '\0';
98 if (asprintf(&page, "# Error loading %s\n\n> %s\n",
99 tab->hist_cur->h, page) == -1)
101 load_page_from_str(tab, page);
106 handle_imsg_check_cert(struct imsg *imsg, size_t datalen)
108 const char *hash, *host, *port;
110 struct tofu_entry *e;
114 if (hash[datalen-1] != '\0')
117 tab = tab_by_id(imsg->hdr.peerid);
119 if (tab->proxy != NULL) {
120 host = tab->proxy->host;
121 port = tab->proxy->port;
123 host = tab->uri.host;
124 port = tab->uri.port;
127 if ((e = tofu_lookup(&certs, host, port)) == NULL) {
128 /* TODO: an update in libressl/libretls changed
129 * significantly. Find a better approach at storing
131 if (datalen > sizeof(e->hash))
134 tofu_res = 1; /* trust on first use */
135 if ((e = calloc(1, sizeof(*e))) == NULL)
137 strlcpy(e->domain, host, sizeof(e->domain));
138 if (*port != '\0' && strcmp(port, "1965")) {
139 strlcat(e->domain, ":", sizeof(e->domain));
140 strlcat(e->domain, port, sizeof(e->domain));
142 strlcpy(e->hash, hash, sizeof(e->hash));
144 imsg_compose(fsibuf, IMSG_SAVE_CERT, tab->id, 0, -1,
148 tofu_res = !strcmp(hash, e->hash);
151 if (e->verified == -1)
152 tab->trust = TS_TEMP_TRUSTED;
153 else if (e->verified == 1)
154 tab->trust = TS_VERIFIED;
156 tab->trust = TS_TRUSTED;
158 imsg_compose(netibuf, IMSG_CERT_STATUS, imsg->hdr.peerid, 0, -1,
159 &tofu_res, sizeof(tofu_res));
162 tab->trust = TS_UNTRUSTED;
163 load_page_from_str(tab, "# Certificate mismatch\n");
164 if ((tab->cert = strdup(hash)) == NULL)
166 ui_yornp("Certificate mismatch. Proceed?",
167 handle_check_cert_user_choice, tab);
172 handle_check_cert_user_choice(int accept, struct tab *tab)
174 imsg_compose(netibuf, IMSG_CERT_STATUS, tab->id, 0, -1,
175 &accept, sizeof(accept));
180 * trust the certificate for this session only. If
181 * the page results in a redirect while we're asking
182 * the user to save, we'll end up with an invalid
183 * tabid (one request == one tab id) and crash. It
184 * also makes sense to save it for the current session
185 * if the user accepted it.
187 tofu_temp_trust(&certs, tab->uri.host, tab->uri.port, tab->cert);
189 ui_yornp("Save the new certificate?",
190 handle_maybe_save_new_cert, tab);
198 handle_maybe_save_new_cert(int accept, struct tab *tab)
200 struct tofu_entry *e;
201 const char *host, *port;
203 if (tab->proxy != NULL) {
204 host = tab->proxy->host;
205 port = tab->proxy->port;
207 host = tab->uri.host;
208 port = tab->uri.port;
214 if ((e = calloc(1, sizeof(*e))) == NULL)
217 strlcpy(e->domain, host, sizeof(e->domain));
218 if (*port != '\0' && strcmp(port, "1965")) {
219 strlcat(e->domain, ":", sizeof(e->domain));
220 strlcat(e->domain, port, sizeof(e->domain));
222 strlcpy(e->hash, tab->cert, sizeof(e->hash));
223 imsg_compose(fsibuf, IMSG_UPDATE_CERT, 0, 0, -1, e, sizeof(*e));
226 tofu_update(&certs, e);
228 tab->trust = TS_TRUSTED;
236 normalize_code(int n)
239 if (n == 10 || n == 11)
245 if (n == 30 || n == 31)
253 if (n <= 53 || n == 59)
261 return MALFORMED_RESPONSE;
265 handle_imsg_got_code(struct imsg *imsg, size_t datalen)
269 tab = tab_by_id(imsg->hdr.peerid);
271 if (sizeof(tab->code) != datalen)
274 memcpy(&tab->code, imsg->data, sizeof(tab->code));
275 tab->code = normalize_code(tab->code);
276 if (tab->code != 30 && tab->code != 31)
277 tab->redirect_count = 0;
281 handle_imsg_got_meta(struct imsg *imsg, size_t datalen)
285 tab = tab_by_id(imsg->hdr.peerid);
287 if (sizeof(tab->meta) <= datalen)
290 memcpy(tab->meta, imsg->data, datalen);
292 if (tab->code < 10) { /* internal errors */
293 load_page_from_str(tab, err_pages[tab->code]);
294 } else if (tab->code < 20) { /* 1x */
295 load_page_from_str(tab, err_pages[tab->code]);
296 ui_require_input(tab, tab->code == 11);
297 } else if (tab->code == 20) {
298 if (setup_parser_for(tab)) {
299 imsg_compose(netibuf, IMSG_PROCEED, tab->id, 0, -1, NULL, 0);
302 load_page_from_str(tab, err_pages[UNKNOWN_TYPE_OR_CSET]);
303 ui_yornp("Can't display page, wanna save?",
304 handle_maybe_save_page, tab);
306 } else if (tab->code < 40) { /* 3x */
307 tab->redirect_count++;
309 /* TODO: make customizable? */
310 if (tab->redirect_count > 5) {
311 load_page_from_str(tab,
312 err_pages[TOO_MUCH_REDIRECTS]);
314 do_load_url(tab, tab->meta);
315 } else { /* 4x, 5x & 6x */
316 load_page_from_str(tab, err_pages[tab->code]);
321 handle_maybe_save_page(int dosave, struct tab *tab)
324 ui_read("Save to path", handle_save_page_path, tab->id);
330 handle_save_page_path(const char *path, unsigned int tabid)
335 stop_tab(tab_by_id(tabid));
339 tab = tab_by_id(tabid);
340 tab->path = strdup(path);
342 imsg_compose(fsibuf, IMSG_FILE_OPEN, tabid, 0, -1, path, strlen(path)+1);
347 handle_imsg_file_opened(struct imsg *imsg, size_t datalen)
354 tab = tab_by_id(imsg->hdr.peerid);
356 if (imsg->fd == -1) {
360 if (e[datalen-1] != '\0')
362 l = asprintf(&page, "# Can't open file\n\n> %s: %s\n",
366 load_page_from_str(tab, page);
370 imsg_compose(netibuf, IMSG_PROCEED, tab->id, 0, -1, NULL, 0);
376 handle_imsg_buf(struct imsg *imsg, size_t datalen)
380 char *page, buf[FMT_SCALED_STRSIZE] = {0};
382 tab = tab_by_id(imsg->hdr.peerid);
384 tab->bytes += datalen;
386 if (!tab->buffer.page.parse(&tab->buffer.page,
387 imsg->data, datalen))
390 write(tab->fd, imsg->data, datalen);
391 fmt_scaled(tab->bytes, buf);
392 l = asprintf(&page, "Saving to \"%s\"... (%s)\n",
397 load_page_from_str(tab, page);
401 ui_on_tab_refresh(tab);
405 handle_imsg_eof(struct imsg *imsg, size_t datalen)
409 char *page, buf[FMT_SCALED_STRSIZE] = {0};
411 tab = tab_by_id(imsg->hdr.peerid);
414 if (!tab->buffer.page.free(&tab->buffer.page))
417 fmt_scaled(tab->bytes, buf);
418 l = asprintf(&page, "Saved to \"%s\" (%s)\n",
423 load_page_from_str(tab, page);
432 ui_on_tab_refresh(tab);
433 ui_on_tab_loaded(tab);
437 handle_imsg_bookmark_ok(struct imsg *imsg, size_t datalen)
441 if (datalen != sizeof(res))
444 memcpy(&res, imsg->data, sizeof(res));
446 message("Added to bookmarks!");
448 message("Failed to add to bookmarks: %s",
453 handle_imsg_save_cert_ok(struct imsg *imsg, size_t datalen)
457 if (datalen != sizeof(res))
459 memcpy(&res, imsg->data, datalen);
461 message("Failed to save the cert for: %s",
466 handle_imsg_update_cert_ok(struct imsg *imsg, size_t datalen)
470 if (datalen != sizeof(res))
472 memcpy(&res, imsg->data, datalen);
474 message("Failed to update the certificate");
478 handle_dispatch_imsg(int fd, short ev, void *d)
480 struct imsgbuf *ibuf = d;
481 dispatch_imsg(ibuf, handlers, sizeof(handlers));
485 load_page_from_str(struct tab *tab, const char *page)
487 erase_buffer(&tab->buffer);
488 gemtext_initparser(&tab->buffer.page);
489 if (!tab->buffer.page.parse(&tab->buffer.page, page, strlen(page)))
491 if (!tab->buffer.page.free(&tab->buffer.page))
493 ui_on_tab_refresh(tab);
494 ui_on_tab_loaded(tab);
498 load_about_url(struct tab *tab, const char *url)
500 tab->trust = TS_VERIFIED;
502 gemtext_initparser(&tab->buffer.page);
504 imsg_compose(fsibuf, IMSG_GET, tab->id, 0, -1,
505 tab->hist_cur->h, strlen(tab->hist_cur->h)+1);
510 load_gemini_url(struct tab *tab, const char *url)
515 tab->id = tab_new_id();
517 memset(&req, 0, sizeof(req));
518 strlcpy(req.host, tab->uri.host, sizeof(req.host));
519 strlcpy(req.port, tab->uri.port, sizeof(req.host));
521 strlcpy(req.req, tab->hist_cur->h, sizeof(req.req));
522 strlcat(req.req, "\r\n", sizeof(req.req));
524 req.proto = PROTO_GEMINI;
526 imsg_compose(netibuf, IMSG_GET_RAW, tab->id, 0, -1,
532 load_via_proxy(struct tab *tab, const char *url, struct proxy *p)
537 tab->id = tab_new_id();
540 memset(&req, 0, sizeof(req));
541 strlcpy(req.host, p->host, sizeof(req.host));
542 strlcpy(req.port, p->port, sizeof(req.host));
544 strlcpy(req.req, tab->hist_cur->h, sizeof(req.req));
545 strlcat(req.req, "\r\n", sizeof(req.req));
547 req.proto = p->proto;
549 imsg_compose(netibuf, IMSG_GET_RAW, tab->id, 0, -1,
555 do_load_url(struct tab *tab, const char *url)
571 tab->trust = TS_UNKNOWN;
573 memcpy(&uri, &tab->uri, sizeof(tab->uri));
574 if (!phos_resolve_uri_from_str(&uri, url, &tab->uri)) {
575 if (asprintf(&t, "#error loading %s\n>%s\n",
576 url, "Can't parse the URI") == -1)
578 strlcpy(tab->hist_cur->h, url, sizeof(tab->hist_cur->h));
579 load_page_from_str(tab, t);
584 phos_serialize_uri(&tab->uri, tab->hist_cur->h,
585 sizeof(tab->hist_cur->h));
587 for (p = protos; p->schema != NULL; ++p) {
588 if (!strcmp(tab->uri.scheme, p->schema)) {
594 TAILQ_FOREACH(proxy, &proxies, proxies) {
595 if (!strcmp(tab->uri.scheme, proxy->match_proto)) {
596 load_via_proxy(tab, url, proxy);
601 protos[0].loadfn(tab, url);
605 load_url(struct tab *tab, const char *url)
607 if (tab->hist_cur != NULL)
608 hist_clear_forward(&tab->hist, TAILQ_NEXT(tab->hist_cur, entries));
610 if ((tab->hist_cur = calloc(1, sizeof(*tab->hist_cur))) == NULL) {
615 hist_push(&tab->hist, tab->hist_cur);
616 do_load_url(tab, url);
617 erase_buffer(&tab->buffer);
621 load_previous_page(struct tab *tab)
625 if ((h = TAILQ_PREV(tab->hist_cur, mhisthead, entries)) == NULL)
628 do_load_url(tab, h->h);
633 load_next_page(struct tab *tab)
637 if ((h = TAILQ_NEXT(tab->hist_cur, entries)) == NULL)
640 do_load_url(tab, h->h);
645 stop_tab(struct tab *tab)
647 imsg_compose(netibuf, IMSG_STOP, tab->id, 0, -1, NULL, 0);
655 load_page_from_str(tab, "Stopped.\n");
660 add_to_bookmarks(const char *str)
662 imsg_compose(fsibuf, IMSG_BOOKMARK_PAGE, 0, 0, -1, str, strlen(str)+1);
671 imsg_compose(fsibuf, IMSG_SESSION_START, 0, 0, -1, NULL, 0);
674 TAILQ_FOREACH(tab, &tabshead, tabs) {
675 imsg_compose(fsibuf, IMSG_SESSION_TAB, 0, 0, -1,
676 tab->hist_cur->h, strlen(tab->hist_cur->h)+1);
680 imsg_compose(fsibuf, IMSG_SESSION_END, 0, 0, -1, NULL, 0);
685 session_new_tab_cb(const char *url)
691 start_child(enum telescope_process p, const char *argv0, int fd)
697 switch (pid = fork()) {
707 if (dup2(fd, 3) == -1)
708 err(1, "cannot setup imsg fd");
710 argv[argc++] = argv0;
713 errx(1, "Can't start ui process");
715 argv[argc++] = "-Tf";
718 argv[argc++] = "-Tn";
723 execvp(argv0, (char *const *)argv);
724 err(1, "execvp(%s)", argv0);
727 static void __attribute__((noreturn))
730 fprintf(stderr, "USAGE: %s [-hn] [-c config] [url]\n", getprogname());
731 fprintf(stderr, "version: " PACKAGE " " VERSION "\n");
736 main(int argc, char * const *argv)
738 struct imsgbuf network_ibuf, fs_ibuf;
739 int net_fds[2], fs_fds[2];
740 int ch, configtest = 0, fail = 0;
744 const char *url = NEW_TAB_URL;
749 signal(SIGCHLD, SIG_IGN);
750 signal(SIGPIPE, SIG_IGN);
752 if (getenv("NO_COLOR") != NULL)
755 strlcpy(path, getenv("HOME"), sizeof(path));
756 strlcat(path, "/.telescope/config", sizeof(path));
758 while ((ch = getopt(argc, argv, "c:hnT:")) != -1) {
762 strlcpy(path, optarg, sizeof(path));
778 errx(1, "invalid process spec %c",
793 else if (proc == PROC_FS)
795 else if (proc == PROC_NET)
796 return client_main();
806 /* setup keys before reading the config */
807 TAILQ_INIT(&global_map.m);
808 global_map.unhandled_input = global_key_unbound;
809 TAILQ_INIT(&minibuffer_map.m);
812 parseconfig(path, fail);
818 /* Start children. */
819 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, fs_fds) == -1)
820 err(1, "socketpair");
821 start_child(PROC_FS, argv0, fs_fds[1]);
822 imsg_init(&fs_ibuf, fs_fds[0]);
825 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, net_fds) == -1)
826 err(1, "socketpair");
827 start_child(PROC_NET, argv0, net_fds[1]);
828 imsg_init(&network_ibuf, net_fds[0]);
829 netibuf = &network_ibuf;
833 /* initialize tofu & load certificates */
835 tofu_init(&certs, 5, offsetof(struct tofu_entry, domain));
840 event_set(&netev, netibuf->fd, EV_READ | EV_PERSIST,
841 handle_dispatch_imsg, netibuf);
842 event_add(&netev, NULL);
844 event_set(&fsev, fsibuf->fd, EV_READ | EV_PERSIST,
845 handle_dispatch_imsg, fsibuf);
846 event_add(&fsev, NULL);
849 load_last_session(session_new_tab_cb);
850 if (has_url || TAILQ_EMPTY(&tabshead))
853 sandbox_ui_process();
858 imsg_compose(netibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
861 imsg_compose(fsibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);