1 #include <sys/socket.h>
15 #include "telescope.h"
18 static struct option longopts[] = {
19 {"colors", no_argument, NULL, 'c'},
20 {"help", no_argument, NULL, 'h'},
21 {"version", no_argument, NULL, 'v'},
25 static const char *opts = "Cc:hnT:v";
27 static struct imsgev *iev_fs, *iev_net;
29 struct tabshead tabshead = TAILQ_HEAD_INITIALIZER(tabshead);
30 struct proxylist proxies = TAILQ_HEAD_INITIALIZER(proxies);
32 enum telescope_process {
38 /* the first is also the fallback one */
39 static struct proto protos[] = {
40 { "gemini", load_gemini_url },
41 { "about", load_about_url },
45 static void die(void) __attribute__((__noreturn__));
46 static struct tab *tab_by_id(uint32_t);
47 static void handle_imsg_err(struct imsg*, size_t);
48 static void handle_imsg_check_cert(struct imsg*, size_t);
49 static void handle_check_cert_user_choice(int, struct tab *);
50 static void handle_maybe_save_new_cert(int, struct tab *);
51 static void handle_imsg_got_code(struct imsg*, size_t);
52 static void handle_imsg_got_meta(struct imsg*, size_t);
53 static void handle_maybe_save_page(int, struct tab *);
54 static void handle_save_page_path(const char *, struct tab *);
55 static void handle_imsg_file_opened(struct imsg*, size_t);
56 static void handle_imsg_buf(struct imsg*, size_t);
57 static void handle_imsg_eof(struct imsg*, size_t);
58 static void handle_imsg_bookmark_ok(struct imsg*, size_t);
59 static void handle_imsg_save_cert_ok(struct imsg*, size_t);
60 static void handle_imsg_update_cert_ok(struct imsg *, size_t);
61 static void handle_dispatch_imsg(int, short, void*);
62 static void load_page_from_str(struct tab*, const char*);
63 static void do_load_url(struct tab*, const char*);
64 static pid_t start_child(enum telescope_process, const char *, int);
65 static int ui_send_net(int, uint32_t, const void *, uint16_t);
66 static int ui_send_fs(int, uint32_t, const void *, uint16_t);
68 static imsg_handlerfn *handlers[] = {
69 [IMSG_ERR] = handle_imsg_err,
70 [IMSG_CHECK_CERT] = handle_imsg_check_cert,
71 [IMSG_GOT_CODE] = handle_imsg_got_code,
72 [IMSG_GOT_META] = handle_imsg_got_meta,
73 [IMSG_BUF] = handle_imsg_buf,
74 [IMSG_EOF] = handle_imsg_eof,
75 [IMSG_BOOKMARK_OK] = handle_imsg_bookmark_ok,
76 [IMSG_SAVE_CERT_OK] = handle_imsg_save_cert_ok,
77 [IMSG_UPDATE_CERT_OK] = handle_imsg_update_cert_ok,
78 [IMSG_FILE_OPENED] = handle_imsg_file_opened,
81 static struct ohash certs;
83 static void __attribute__((__noreturn__))
90 tab_by_id(uint32_t id)
94 TAILQ_FOREACH(t, &tabshead, tabs) {
103 handle_imsg_err(struct imsg *imsg, size_t datalen)
108 tab = tab_by_id(imsg->hdr.peerid);
111 page[datalen-1] = '\0';
113 if (asprintf(&page, "# Error loading %s\n\n> %s\n",
114 tab->hist_cur->h, page) == -1)
116 load_page_from_str(tab, page);
121 handle_imsg_check_cert(struct imsg *imsg, size_t datalen)
123 const char *hash, *host, *port;
125 struct tofu_entry *e;
129 if (hash[datalen-1] != '\0')
132 tab = tab_by_id(imsg->hdr.peerid);
134 if (tab->proxy != NULL) {
135 host = tab->proxy->host;
136 port = tab->proxy->port;
138 host = tab->uri.host;
139 port = tab->uri.port;
142 if ((e = tofu_lookup(&certs, host, port)) == NULL) {
143 /* TODO: an update in libressl/libretls changed
144 * significantly. Find a better approach at storing
146 if (datalen > sizeof(e->hash))
149 tofu_res = 1; /* trust on first use */
150 if ((e = calloc(1, sizeof(*e))) == NULL)
152 strlcpy(e->domain, host, sizeof(e->domain));
153 if (*port != '\0' && strcmp(port, "1965")) {
154 strlcat(e->domain, ":", sizeof(e->domain));
155 strlcat(e->domain, port, sizeof(e->domain));
157 strlcpy(e->hash, hash, sizeof(e->hash));
159 ui_send_fs(IMSG_SAVE_CERT, tab->id, e, sizeof(*e));
161 tofu_res = !strcmp(hash, e->hash);
164 if (e->verified == -1)
165 tab->trust = TS_TEMP_TRUSTED;
166 else if (e->verified == 1)
167 tab->trust = TS_VERIFIED;
169 tab->trust = TS_TRUSTED;
171 ui_send_net(IMSG_CERT_STATUS, imsg->hdr.peerid,
172 &tofu_res, sizeof(tofu_res));
174 tab->trust = TS_UNTRUSTED;
175 load_page_from_str(tab, "# Certificate mismatch\n");
176 if ((tab->cert = strdup(hash)) == NULL)
178 ui_yornp("Certificate mismatch. Proceed?",
179 handle_check_cert_user_choice, tab);
184 handle_check_cert_user_choice(int accept, struct tab *tab)
186 ui_send_net(IMSG_CERT_STATUS, tab->id, &accept,
191 * trust the certificate for this session only. If
192 * the page results in a redirect while we're asking
193 * the user to save, we'll end up with an invalid
194 * tabid (one request == one tab id) and crash. It
195 * also makes sense to save it for the current session
196 * if the user accepted it.
198 tofu_temp_trust(&certs, tab->uri.host, tab->uri.port, tab->cert);
200 ui_yornp("Save the new certificate?",
201 handle_maybe_save_new_cert, tab);
209 handle_maybe_save_new_cert(int accept, struct tab *tab)
211 struct tofu_entry *e;
212 const char *host, *port;
214 if (tab->proxy != NULL) {
215 host = tab->proxy->host;
216 port = tab->proxy->port;
218 host = tab->uri.host;
219 port = tab->uri.port;
225 if ((e = calloc(1, sizeof(*e))) == NULL)
228 strlcpy(e->domain, host, sizeof(e->domain));
229 if (*port != '\0' && strcmp(port, "1965")) {
230 strlcat(e->domain, ":", sizeof(e->domain));
231 strlcat(e->domain, port, sizeof(e->domain));
233 strlcpy(e->hash, tab->cert, sizeof(e->hash));
234 ui_send_fs(IMSG_UPDATE_CERT, 0, e, sizeof(*e));
236 tofu_update(&certs, e);
238 tab->trust = TS_TRUSTED;
246 normalize_code(int n)
249 if (n == 10 || n == 11)
255 if (n == 30 || n == 31)
263 if (n <= 53 || n == 59)
271 return MALFORMED_RESPONSE;
275 handle_imsg_got_code(struct imsg *imsg, size_t datalen)
279 tab = tab_by_id(imsg->hdr.peerid);
281 if (sizeof(tab->code) != datalen)
284 memcpy(&tab->code, imsg->data, sizeof(tab->code));
285 tab->code = normalize_code(tab->code);
286 if (tab->code != 30 && tab->code != 31)
287 tab->redirect_count = 0;
291 handle_imsg_got_meta(struct imsg *imsg, size_t datalen)
295 tab = tab_by_id(imsg->hdr.peerid);
297 if (sizeof(tab->meta) <= datalen)
300 memcpy(tab->meta, imsg->data, datalen);
302 if (tab->code < 10) { /* internal errors */
303 load_page_from_str(tab, err_pages[tab->code]);
304 } else if (tab->code < 20) { /* 1x */
305 load_page_from_str(tab, err_pages[tab->code]);
306 ui_require_input(tab, tab->code == 11);
307 } else if (tab->code == 20) {
308 if (setup_parser_for(tab)) {
309 ui_send_net(IMSG_PROCEED, tab->id, NULL, 0);
311 load_page_from_str(tab, err_pages[UNKNOWN_TYPE_OR_CSET]);
312 ui_yornp("Can't display page, wanna save?",
313 handle_maybe_save_page, tab);
315 } else if (tab->code < 40) { /* 3x */
316 tab->redirect_count++;
318 /* TODO: make customizable? */
319 if (tab->redirect_count > 5) {
320 load_page_from_str(tab,
321 err_pages[TOO_MUCH_REDIRECTS]);
323 do_load_url(tab, tab->meta);
324 } else { /* 4x, 5x & 6x */
325 load_page_from_str(tab, err_pages[tab->code]);
330 handle_maybe_save_page(int dosave, struct tab *tab)
333 ui_read("Save to path", handle_save_page_path, tab);
339 handle_save_page_path(const char *path, struct tab *tab)
346 tab->path = strdup(path);
348 ui_send_fs(IMSG_FILE_OPEN, tab->id, path, strlen(path)+1);
352 handle_imsg_file_opened(struct imsg *imsg, size_t datalen)
359 tab = tab_by_id(imsg->hdr.peerid);
361 if (imsg->fd == -1) {
365 if (e[datalen-1] != '\0')
367 l = asprintf(&page, "# Can't open file\n\n> %s: %s\n",
371 load_page_from_str(tab, page);
375 ui_send_net(IMSG_PROCEED, tab->id, NULL, 0);
380 handle_imsg_buf(struct imsg *imsg, size_t datalen)
384 char *page, buf[FMT_SCALED_STRSIZE] = {0};
386 tab = tab_by_id(imsg->hdr.peerid);
388 tab->bytes += datalen;
390 if (!tab->buffer.page.parse(&tab->buffer.page,
391 imsg->data, datalen))
394 write(tab->fd, imsg->data, datalen);
395 fmt_scaled(tab->bytes, buf);
396 l = asprintf(&page, "Saving to \"%s\"... (%s)\n",
401 load_page_from_str(tab, page);
405 ui_on_tab_refresh(tab);
409 handle_imsg_eof(struct imsg *imsg, size_t datalen)
413 char *page, buf[FMT_SCALED_STRSIZE] = {0};
415 tab = tab_by_id(imsg->hdr.peerid);
418 if (!tab->buffer.page.free(&tab->buffer.page))
421 fmt_scaled(tab->bytes, buf);
422 l = asprintf(&page, "Saved to \"%s\" (%s)\n",
427 load_page_from_str(tab, page);
436 ui_on_tab_refresh(tab);
437 ui_on_tab_loaded(tab);
441 handle_imsg_bookmark_ok(struct imsg *imsg, size_t datalen)
445 if (datalen != sizeof(res))
448 memcpy(&res, imsg->data, sizeof(res));
450 message("Added to bookmarks!");
452 message("Failed to add to bookmarks: %s",
457 handle_imsg_save_cert_ok(struct imsg *imsg, size_t datalen)
461 if (datalen != sizeof(res))
463 memcpy(&res, imsg->data, datalen);
465 message("Failed to save the cert for: %s",
470 handle_imsg_update_cert_ok(struct imsg *imsg, size_t datalen)
474 if (datalen != sizeof(res))
476 memcpy(&res, imsg->data, datalen);
478 message("Failed to update the certificate");
482 handle_dispatch_imsg(int fd, short ev, void *d)
484 struct imsgev *iev = d;
485 dispatch_imsg(iev, ev, handlers, sizeof(handlers));
489 load_page_from_str(struct tab *tab, const char *page)
491 erase_buffer(&tab->buffer);
492 gemtext_initparser(&tab->buffer.page);
493 if (!tab->buffer.page.parse(&tab->buffer.page, page, strlen(page)))
495 if (!tab->buffer.page.free(&tab->buffer.page))
497 ui_on_tab_refresh(tab);
498 ui_on_tab_loaded(tab);
502 load_about_url(struct tab *tab, const char *url)
504 tab->trust = TS_VERIFIED;
506 gemtext_initparser(&tab->buffer.page);
508 ui_send_fs(IMSG_GET, tab->id,
509 tab->hist_cur->h, strlen(tab->hist_cur->h)+1);
513 load_gemini_url(struct tab *tab, const char *url)
518 tab->id = tab_new_id();
520 memset(&req, 0, sizeof(req));
521 strlcpy(req.host, tab->uri.host, sizeof(req.host));
522 strlcpy(req.port, tab->uri.port, sizeof(req.host));
524 strlcpy(req.req, tab->hist_cur->h, sizeof(req.req));
525 strlcat(req.req, "\r\n", sizeof(req.req));
527 req.proto = PROTO_GEMINI;
529 ui_send_net(IMSG_GET_RAW, tab->id,
534 load_via_proxy(struct tab *tab, const char *url, struct proxy *p)
539 tab->id = tab_new_id();
542 memset(&req, 0, sizeof(req));
543 strlcpy(req.host, p->host, sizeof(req.host));
544 strlcpy(req.port, p->port, sizeof(req.host));
546 strlcpy(req.req, tab->hist_cur->h, sizeof(req.req));
547 strlcat(req.req, "\r\n", sizeof(req.req));
549 req.proto = p->proto;
551 ui_send_net(IMSG_GET_RAW, tab->id,
556 do_load_url(struct tab *tab, const char *url)
572 tab->trust = TS_UNKNOWN;
574 memcpy(&uri, &tab->uri, sizeof(tab->uri));
575 if (!phos_resolve_uri_from_str(&uri, url, &tab->uri)) {
576 if (asprintf(&t, "#error loading %s\n>%s\n",
577 url, "Can't parse the URI") == -1)
579 strlcpy(tab->hist_cur->h, url, sizeof(tab->hist_cur->h));
580 load_page_from_str(tab, t);
585 phos_serialize_uri(&tab->uri, tab->hist_cur->h,
586 sizeof(tab->hist_cur->h));
588 for (p = protos; p->schema != NULL; ++p) {
589 if (!strcmp(tab->uri.scheme, p->schema)) {
595 TAILQ_FOREACH(proxy, &proxies, proxies) {
596 if (!strcmp(tab->uri.scheme, proxy->match_proto)) {
597 load_via_proxy(tab, url, proxy);
602 protos[0].loadfn(tab, url);
606 load_url(struct tab *tab, const char *url)
608 if (tab->hist_cur != NULL)
609 hist_clear_forward(&tab->hist, TAILQ_NEXT(tab->hist_cur, entries));
611 if ((tab->hist_cur = calloc(1, sizeof(*tab->hist_cur))) == NULL) {
616 hist_push(&tab->hist, tab->hist_cur);
617 do_load_url(tab, url);
618 erase_buffer(&tab->buffer);
622 load_previous_page(struct tab *tab)
626 if ((h = TAILQ_PREV(tab->hist_cur, mhisthead, entries)) == NULL)
629 do_load_url(tab, h->h);
634 load_next_page(struct tab *tab)
638 if ((h = TAILQ_NEXT(tab->hist_cur, entries)) == NULL)
641 do_load_url(tab, h->h);
646 stop_tab(struct tab *tab)
648 ui_send_net(IMSG_STOP, tab->id, NULL, 0);
655 load_page_from_str(tab, "Stopped.\n");
660 add_to_bookmarks(const char *str)
662 ui_send_fs(IMSG_BOOKMARK_PAGE, 0,
671 ui_send_fs(IMSG_SESSION_START, 0, NULL, 0);
673 TAILQ_FOREACH(tab, &tabshead, tabs) {
674 ui_send_fs(IMSG_SESSION_TAB, 0,
675 tab->hist_cur->h, strlen(tab->hist_cur->h)+1);
678 ui_send_fs(IMSG_SESSION_END, 0, NULL, 0);
682 session_new_tab_cb(const char *url)
688 start_child(enum telescope_process p, const char *argv0, int fd)
694 switch (pid = fork()) {
704 if (dup2(fd, 3) == -1)
705 err(1, "cannot setup imsg fd");
707 argv[argc++] = argv0;
710 errx(1, "Can't start ui process");
712 argv[argc++] = "-Tf";
715 argv[argc++] = "-Tn";
720 execvp(argv0, (char *const *)argv);
721 err(1, "execvp(%s)", argv0);
725 ui_send_net(int type, uint32_t peerid, const void *data,
728 return imsg_compose_event(iev_net, type, peerid, 0, -1, data,
733 ui_send_fs(int type, uint32_t peerid, const void *data, uint16_t datalen)
735 return imsg_compose_event(iev_fs, type, peerid, 0, -1, data,
739 static void __attribute__((noreturn))
742 fprintf(stderr, "USAGE: %s [-hnv] [-c config] [url]\n",
744 fprintf(stderr, "version: " PACKAGE " " VERSION "\n");
749 main(int argc, char * const *argv)
751 struct imsgev net_ibuf, fs_ibuf;
752 int pipe2net[2], pipe2fs[2];
753 int ch, configtest = 0, fail = 0;
758 const char *url = NEW_TAB_URL;
763 signal(SIGCHLD, SIG_IGN);
764 signal(SIGPIPE, SIG_IGN);
766 if (getenv("NO_COLOR") != NULL)
769 strlcpy(path, getenv("HOME"), sizeof(path));
770 strlcat(path, "/.telescope/config", sizeof(path));
772 while ((ch = getopt_long(argc, argv, opts, longopts, NULL)) != -1) {
775 exit(ui_print_colors());
778 strlcpy(path, optarg, sizeof(path));
794 errx(1, "invalid process spec %c",
799 printf("%s %s\n", PACKAGE_NAME, PACKAGE_VERSION);
813 else if (proc == PROC_FS)
815 else if (proc == PROC_NET)
816 return client_main();
826 /* setup keys before reading the config */
827 TAILQ_INIT(&global_map.m);
828 global_map.unhandled_input = global_key_unbound;
829 TAILQ_INIT(&minibuffer_map.m);
832 parseconfig(path, fail);
839 if ((sessionfd = lock_session()) == -1)
840 errx(1, "can't lock session, is another instance of "
841 "telescope already running?");
843 /* Start children. */
844 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe2fs) == -1)
845 err(1, "socketpair");
846 start_child(PROC_FS, argv0, pipe2fs[1]);
847 imsg_init(&fs_ibuf.ibuf, pipe2fs[0]);
849 iev_fs->handler = handle_dispatch_imsg;
851 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe2net) == -1)
852 err(1, "socketpair");
853 start_child(PROC_NET, argv0, pipe2net[1]);
854 imsg_init(&net_ibuf.ibuf, pipe2net[0]);
856 iev_net->handler = handle_dispatch_imsg;
860 /* initialize tofu & load certificates */
861 tofu_init(&certs, 5, offsetof(struct tofu_entry, domain));
866 /* Setup event handlers for pipes to fs/net */
867 iev_fs->events = EV_READ;
868 event_set(&iev_fs->ev, iev_fs->ibuf.fd, iev_fs->events,
869 iev_fs->handler, iev_fs);
870 event_add(&iev_fs->ev, NULL);
872 iev_net->events = EV_READ;
873 event_set(&iev_net->ev, iev_net->ibuf.fd, iev_net->events,
874 iev_net->handler, iev_net);
875 event_add(&iev_net->ev, NULL);
878 load_last_session(session_new_tab_cb);
879 if (has_url || TAILQ_EMPTY(&tabshead))
882 sandbox_ui_process();
887 ui_send_fs(IMSG_QUIT, 0, NULL, 0);
888 ui_send_net(IMSG_QUIT, 0, NULL, 0);
889 imsg_flush(&iev_fs->ibuf);
890 imsg_flush(&iev_net->ibuf);