1 5e11c00c 2021-03-02 op #include <sys/socket.h>
3 5e11c00c 2021-03-02 op #include <errno.h>
4 d2544989 2021-07-08 op #include <limits.h>
5 5e11c00c 2021-03-02 op #include <signal.h>
6 5e11c00c 2021-03-02 op #include <stdio.h>
7 5e11c00c 2021-03-02 op #include <stdlib.h>
8 5e11c00c 2021-03-02 op #include <string.h>
9 5e11c00c 2021-03-02 op #include <unistd.h>
11 395b9f4e 2021-07-12 op #include "parser.h"
12 395b9f4e 2021-07-12 op #include "telescope.h"
13 d1a0f2a3 2021-07-12 op #include "ui.h"
15 35e1f40a 2021-03-14 op struct event netev, fsev;
16 f6a429eb 2021-07-10 op struct tabshead tabshead = TAILQ_HEAD_INITIALIZER(tabshead);
17 f6a429eb 2021-07-10 op struct proxylist proxies = TAILQ_HEAD_INITIALIZER(proxies);
19 6cc5fcfe 2021-07-08 op enum telescope_process {
25 c07ac996 2021-03-12 op /* the first is also the fallback one */
26 c07ac996 2021-03-12 op static struct proto protos[] = {
27 31f1a758 2021-04-22 op { "gemini", load_gemini_url },
28 31f1a758 2021-04-22 op { "about", load_about_url },
29 4d3785b1 2021-03-09 op { NULL, NULL },
32 35e1f40a 2021-03-14 op static struct imsgbuf *netibuf, *fsibuf;
34 2051e653 2021-03-13 op static void die(void) __attribute__((__noreturn__));
35 2051e653 2021-03-13 op static struct tab *tab_by_id(uint32_t);
36 2051e653 2021-03-13 op static void handle_imsg_err(struct imsg*, size_t);
37 2051e653 2021-03-13 op static void handle_imsg_check_cert(struct imsg*, size_t);
38 a2fd3805 2021-07-06 op static void handle_check_cert_user_choice(int, struct tab *);
39 a2fd3805 2021-07-06 op static void handle_maybe_save_new_cert(int, struct tab *);
40 2051e653 2021-03-13 op static void handle_imsg_got_code(struct imsg*, size_t);
41 2051e653 2021-03-13 op static void handle_imsg_got_meta(struct imsg*, size_t);
42 a2fd3805 2021-07-06 op static void handle_maybe_save_page(int, struct tab *);
43 de2a69bb 2021-05-17 op static void handle_save_page_path(const char *, unsigned int);
44 de2a69bb 2021-05-17 op static void handle_imsg_file_opened(struct imsg*, size_t);
45 2051e653 2021-03-13 op static void handle_imsg_buf(struct imsg*, size_t);
46 2051e653 2021-03-13 op static void handle_imsg_eof(struct imsg*, size_t);
47 740f578b 2021-03-15 op static void handle_imsg_bookmark_ok(struct imsg*, size_t);
48 3a227e9a 2021-03-18 op static void handle_imsg_save_cert_ok(struct imsg*, size_t);
49 288fd238 2021-04-25 op static void handle_imsg_update_cert_ok(struct imsg *, size_t);
50 1304bbdd 2021-03-15 op static void handle_dispatch_imsg(int, short, void*);
51 2051e653 2021-03-13 op static void load_page_from_str(struct tab*, const char*);
52 2051e653 2021-03-13 op static void do_load_url(struct tab*, const char*);
53 6cc5fcfe 2021-07-08 op static pid_t start_child(enum telescope_process, const char *, int);
55 5e11c00c 2021-03-02 op static imsg_handlerfn *handlers[] = {
56 5e11c00c 2021-03-02 op [IMSG_ERR] = handle_imsg_err,
57 5e11c00c 2021-03-02 op [IMSG_CHECK_CERT] = handle_imsg_check_cert,
58 5e11c00c 2021-03-02 op [IMSG_GOT_CODE] = handle_imsg_got_code,
59 5e11c00c 2021-03-02 op [IMSG_GOT_META] = handle_imsg_got_meta,
60 5e11c00c 2021-03-02 op [IMSG_BUF] = handle_imsg_buf,
61 5e11c00c 2021-03-02 op [IMSG_EOF] = handle_imsg_eof,
62 740f578b 2021-03-15 op [IMSG_BOOKMARK_OK] = handle_imsg_bookmark_ok,
63 3a227e9a 2021-03-18 op [IMSG_SAVE_CERT_OK] = handle_imsg_save_cert_ok,
64 288fd238 2021-04-25 op [IMSG_UPDATE_CERT_OK] = handle_imsg_update_cert_ok,
65 de2a69bb 2021-05-17 op [IMSG_FILE_OPENED] = handle_imsg_file_opened,
68 cbcc75fb 2021-03-17 op static struct ohash certs;
70 5e11c00c 2021-03-02 op static void __attribute__((__noreturn__))
73 5e11c00c 2021-03-02 op abort(); /* TODO */
76 5e11c00c 2021-03-02 op static struct tab *
77 5e11c00c 2021-03-02 op tab_by_id(uint32_t id)
79 5e11c00c 2021-03-02 op struct tab *t;
81 5e11c00c 2021-03-02 op TAILQ_FOREACH(t, &tabshead, tabs) {
82 5e11c00c 2021-03-02 op if (t->id == id)
90 5e11c00c 2021-03-02 op handle_imsg_err(struct imsg *imsg, size_t datalen)
92 3a9b9365 2021-03-09 op struct tab *tab;
95 3a9b9365 2021-03-09 op tab = tab_by_id(imsg->hdr.peerid);
97 3a9b9365 2021-03-09 op page = imsg->data;
98 3a9b9365 2021-03-09 op page[datalen-1] = '\0';
100 3a9b9365 2021-03-09 op if (asprintf(&page, "# Error loading %s\n\n> %s\n",
101 2051e653 2021-03-13 op tab->hist_cur->h, page) == -1)
103 3a9b9365 2021-03-09 op load_page_from_str(tab, page);
108 5e11c00c 2021-03-02 op handle_imsg_check_cert(struct imsg *imsg, size_t datalen)
110 984245ce 2021-06-23 op const char *hash, *host, *port;
111 cbcc75fb 2021-03-17 op int tofu_res;
112 cbcc75fb 2021-03-17 op struct tofu_entry *e;
113 cbcc75fb 2021-03-17 op struct tab *tab;
115 cbcc75fb 2021-03-17 op hash = imsg->data;
116 cbcc75fb 2021-03-17 op if (hash[datalen-1] != '\0')
119 10346511 2021-03-17 op tab = tab_by_id(imsg->hdr.peerid);
121 984245ce 2021-06-23 op if (tab->proxy != NULL) {
122 984245ce 2021-06-23 op host = tab->proxy->host;
123 984245ce 2021-06-23 op port = tab->proxy->port;
125 984245ce 2021-06-23 op host = tab->uri.host;
126 984245ce 2021-06-23 op port = tab->uri.port;
129 984245ce 2021-06-23 op if ((e = tofu_lookup(&certs, host, port)) == NULL) {
130 cbcc75fb 2021-03-17 op /* TODO: an update in libressl/libretls changed
131 cbcc75fb 2021-03-17 op * significantly. Find a better approach at storing
132 cbcc75fb 2021-03-17 op * the certs! */
133 cbcc75fb 2021-03-17 op if (datalen > sizeof(e->hash))
136 cbcc75fb 2021-03-17 op tofu_res = 1; /* trust on first use */
137 cbcc75fb 2021-03-17 op if ((e = calloc(1, sizeof(*e))) == NULL)
139 984245ce 2021-06-23 op strlcpy(e->domain, host, sizeof(e->domain));
140 984245ce 2021-06-23 op if (*port != '\0' && strcmp(port, "1965")) {
141 eb4388ee 2021-04-25 op strlcat(e->domain, ":", sizeof(e->domain));
142 984245ce 2021-06-23 op strlcat(e->domain, port, sizeof(e->domain));
144 cbcc75fb 2021-03-17 op strlcpy(e->hash, hash, sizeof(e->hash));
145 3768e50f 2021-04-25 op tofu_add(&certs, e);
146 3a227e9a 2021-03-18 op imsg_compose(fsibuf, IMSG_SAVE_CERT, tab->id, 0, -1,
147 3a227e9a 2021-03-18 op e, sizeof(*e));
148 3a227e9a 2021-03-18 op imsg_flush(fsibuf);
150 cbcc75fb 2021-03-17 op tofu_res = !strcmp(hash, e->hash);
152 5d1bac73 2021-03-25 op if (tofu_res) {
153 a2fd3805 2021-07-06 op if (e->verified == -1)
154 a2fd3805 2021-07-06 op tab->trust = TS_TEMP_TRUSTED;
155 a2fd3805 2021-07-06 op else if (e->verified == 1)
156 a2fd3805 2021-07-06 op tab->trust = TS_VERIFIED;
158 a2fd3805 2021-07-06 op tab->trust = TS_TRUSTED;
160 5d1bac73 2021-03-25 op imsg_compose(netibuf, IMSG_CERT_STATUS, imsg->hdr.peerid, 0, -1,
161 5d1bac73 2021-03-25 op &tofu_res, sizeof(tofu_res));
162 5d1bac73 2021-03-25 op imsg_flush(netibuf);
164 cbcc75fb 2021-03-17 op tab->trust = TS_UNTRUSTED;
165 cbcc75fb 2021-03-17 op load_page_from_str(tab, "# Certificate mismatch\n");
166 288fd238 2021-04-25 op if ((tab->cert = strdup(hash)) == NULL)
168 5d1bac73 2021-03-25 op ui_yornp("Certificate mismatch. Proceed?",
169 a2fd3805 2021-07-06 op handle_check_cert_user_choice, tab);
174 a2fd3805 2021-07-06 op handle_check_cert_user_choice(int accept, struct tab *tab)
176 a2fd3805 2021-07-06 op imsg_compose(netibuf, IMSG_CERT_STATUS, tab->id, 0, -1,
177 5d1bac73 2021-03-25 op &accept, sizeof(accept));
178 1b8a4bbf 2021-03-12 op imsg_flush(netibuf);
180 a2fd3805 2021-07-06 op if (accept) {
182 a2fd3805 2021-07-06 op * trust the certificate for this session only. If
183 a2fd3805 2021-07-06 op * the page results in a redirect while we're asking
184 a2fd3805 2021-07-06 op * the user to save, we'll end up with an invalid
185 a2fd3805 2021-07-06 op * tabid (one request == one tab id) and crash. It
186 a2fd3805 2021-07-06 op * also makes sense to save it for the current session
187 a2fd3805 2021-07-06 op * if the user accepted it.
189 a2fd3805 2021-07-06 op tofu_temp_trust(&certs, tab->uri.host, tab->uri.port, tab->cert);
191 288fd238 2021-04-25 op ui_yornp("Save the new certificate?",
192 a2fd3805 2021-07-06 op handle_maybe_save_new_cert, tab);
194 288fd238 2021-04-25 op free(tab->cert);
195 288fd238 2021-04-25 op tab->cert = NULL;
200 a2fd3805 2021-07-06 op handle_maybe_save_new_cert(int accept, struct tab *tab)
202 288fd238 2021-04-25 op struct tofu_entry *e;
203 984245ce 2021-06-23 op const char *host, *port;
205 984245ce 2021-06-23 op if (tab->proxy != NULL) {
206 984245ce 2021-06-23 op host = tab->proxy->host;
207 984245ce 2021-06-23 op port = tab->proxy->port;
209 984245ce 2021-06-23 op host = tab->uri.host;
210 984245ce 2021-06-23 op port = tab->uri.port;
216 fe2262ad 2021-05-12 op if ((e = calloc(1, sizeof(*e))) == NULL)
219 984245ce 2021-06-23 op strlcpy(e->domain, host, sizeof(e->domain));
220 984245ce 2021-06-23 op if (*port != '\0' && strcmp(port, "1965")) {
221 288fd238 2021-04-25 op strlcat(e->domain, ":", sizeof(e->domain));
222 984245ce 2021-06-23 op strlcat(e->domain, port, sizeof(e->domain));
224 288fd238 2021-04-25 op strlcpy(e->hash, tab->cert, sizeof(e->hash));
225 288fd238 2021-04-25 op imsg_compose(fsibuf, IMSG_UPDATE_CERT, 0, 0, -1, e, sizeof(*e));
226 288fd238 2021-04-25 op imsg_flush(fsibuf);
228 288fd238 2021-04-25 op tofu_update(&certs, e);
230 288fd238 2021-04-25 op tab->trust = TS_TRUSTED;
233 288fd238 2021-04-25 op free(tab->cert);
234 288fd238 2021-04-25 op tab->cert = NULL;
237 5cd2ebb1 2021-03-11 op static inline int
238 5cd2ebb1 2021-03-11 op normalize_code(int n)
240 5cd2ebb1 2021-03-11 op if (n < 20) {
241 5cd2ebb1 2021-03-11 op if (n == 10 || n == 11)
244 5cd2ebb1 2021-03-11 op } else if (n < 30) {
246 5cd2ebb1 2021-03-11 op } else if (n < 40) {
247 5cd2ebb1 2021-03-11 op if (n == 30 || n == 31)
250 5cd2ebb1 2021-03-11 op } else if (n < 50) {
254 5cd2ebb1 2021-03-11 op } else if (n < 60) {
255 5cd2ebb1 2021-03-11 op if (n <= 53 || n == 59)
258 5cd2ebb1 2021-03-11 op } else if (n < 70) {
263 5cd2ebb1 2021-03-11 op return MALFORMED_RESPONSE;
267 5e11c00c 2021-03-02 op handle_imsg_got_code(struct imsg *imsg, size_t datalen)
269 0972d8b2 2021-03-02 op struct tab *tab;
271 0972d8b2 2021-03-02 op tab = tab_by_id(imsg->hdr.peerid);
273 0972d8b2 2021-03-02 op if (sizeof(tab->code) != datalen)
276 5cd2ebb1 2021-03-11 op memcpy(&tab->code, imsg->data, sizeof(tab->code));
277 5cd2ebb1 2021-03-11 op tab->code = normalize_code(tab->code);
278 5cd2ebb1 2021-03-11 op if (tab->code != 30 && tab->code != 31)
279 0972d8b2 2021-03-02 op tab->redirect_count = 0;
283 5e11c00c 2021-03-02 op handle_imsg_got_meta(struct imsg *imsg, size_t datalen)
285 0972d8b2 2021-03-02 op struct tab *tab;
287 0972d8b2 2021-03-02 op tab = tab_by_id(imsg->hdr.peerid);
289 0972d8b2 2021-03-02 op if (sizeof(tab->meta) <= datalen)
292 0972d8b2 2021-03-02 op memcpy(tab->meta, imsg->data, datalen);
294 5cd2ebb1 2021-03-11 op if (tab->code < 10) { /* internal errors */
295 5cd2ebb1 2021-03-11 op load_page_from_str(tab, err_pages[tab->code]);
296 5cd2ebb1 2021-03-11 op } else if (tab->code < 20) { /* 1x */
297 5cd2ebb1 2021-03-11 op load_page_from_str(tab, err_pages[tab->code]);
298 5cd2ebb1 2021-03-11 op ui_require_input(tab, tab->code == 11);
299 5cd2ebb1 2021-03-11 op } else if (tab->code == 20) {
300 c07ac996 2021-03-12 op if (setup_parser_for(tab)) {
301 1b8a4bbf 2021-03-12 op imsg_compose(netibuf, IMSG_PROCEED, tab->id, 0, -1, NULL, 0);
302 1b8a4bbf 2021-03-12 op imsg_flush(netibuf);
304 c07ac996 2021-03-12 op load_page_from_str(tab, err_pages[UNKNOWN_TYPE_OR_CSET]);
305 de2a69bb 2021-05-17 op ui_yornp("Can't display page, wanna save?",
306 a2fd3805 2021-07-06 op handle_maybe_save_page, tab);
308 5cd2ebb1 2021-03-11 op } else if (tab->code < 40) { /* 3x */
309 3a9b9365 2021-03-09 op tab->redirect_count++;
311 3a9b9365 2021-03-09 op /* TODO: make customizable? */
312 3a9b9365 2021-03-09 op if (tab->redirect_count > 5) {
313 3a9b9365 2021-03-09 op load_page_from_str(tab,
314 3a9b9365 2021-03-09 op err_pages[TOO_MUCH_REDIRECTS]);
316 2051e653 2021-03-13 op do_load_url(tab, tab->meta);
317 5cd2ebb1 2021-03-11 op } else { /* 4x, 5x & 6x */
318 5cd2ebb1 2021-03-11 op load_page_from_str(tab, err_pages[tab->code]);
323 a2fd3805 2021-07-06 op handle_maybe_save_page(int dosave, struct tab *tab)
326 a2fd3805 2021-07-06 op ui_read("Save to path", handle_save_page_path, tab->id);
328 a2fd3805 2021-07-06 op stop_tab(tab);
332 de2a69bb 2021-05-17 op handle_save_page_path(const char *path, unsigned int tabid)
334 de2a69bb 2021-05-17 op struct tab *tab;
336 de2a69bb 2021-05-17 op if (path == NULL) {
337 de2a69bb 2021-05-17 op stop_tab(tab_by_id(tabid));
341 de2a69bb 2021-05-17 op tab = tab_by_id(tabid);
342 de2a69bb 2021-05-17 op tab->path = strdup(path);
344 de2a69bb 2021-05-17 op imsg_compose(fsibuf, IMSG_FILE_OPEN, tabid, 0, -1, path, strlen(path)+1);
345 de2a69bb 2021-05-17 op imsg_flush(fsibuf);
349 de2a69bb 2021-05-17 op handle_imsg_file_opened(struct imsg *imsg, size_t datalen)
351 de2a69bb 2021-05-17 op struct tab *tab;
353 de2a69bb 2021-05-17 op const char *e;
356 de2a69bb 2021-05-17 op tab = tab_by_id(imsg->hdr.peerid);
358 de2a69bb 2021-05-17 op if (imsg->fd == -1) {
359 de2a69bb 2021-05-17 op stop_tab(tab);
361 de2a69bb 2021-05-17 op e = imsg->data;
362 de2a69bb 2021-05-17 op if (e[datalen-1] != '\0')
364 de2a69bb 2021-05-17 op l = asprintf(&page, "# Can't open file\n\n> %s: %s\n",
365 de2a69bb 2021-05-17 op tab->path, e);
368 de2a69bb 2021-05-17 op load_page_from_str(tab, page);
371 de2a69bb 2021-05-17 op tab->fd = imsg->fd;
372 de2a69bb 2021-05-17 op imsg_compose(netibuf, IMSG_PROCEED, tab->id, 0, -1, NULL, 0);
373 de2a69bb 2021-05-17 op imsg_flush(netibuf);
378 5e11c00c 2021-03-02 op handle_imsg_buf(struct imsg *imsg, size_t datalen)
380 0972d8b2 2021-03-02 op struct tab *tab;
382 0e1eff5b 2021-06-23 op char *page, buf[FMT_SCALED_STRSIZE] = {0};
384 0972d8b2 2021-03-02 op tab = tab_by_id(imsg->hdr.peerid);
386 de2a69bb 2021-05-17 op tab->bytes += datalen;
387 de2a69bb 2021-05-17 op if (tab->fd == -1) {
388 46f6e974 2021-05-17 op if (!tab->buffer.page.parse(&tab->buffer.page,
389 de2a69bb 2021-05-17 op imsg->data, datalen))
392 de2a69bb 2021-05-17 op write(tab->fd, imsg->data, datalen);
393 0e1eff5b 2021-06-23 op fmt_scaled(tab->bytes, buf);
394 0e1eff5b 2021-06-23 op l = asprintf(&page, "Saving to \"%s\"... (%s)\n",
399 de2a69bb 2021-05-17 op load_page_from_str(tab, page);
403 0972d8b2 2021-03-02 op ui_on_tab_refresh(tab);
407 5e11c00c 2021-03-02 op handle_imsg_eof(struct imsg *imsg, size_t datalen)
409 2ba66cea 2021-03-22 op struct tab *tab;
411 0e1eff5b 2021-06-23 op char *page, buf[FMT_SCALED_STRSIZE] = {0};
413 2ba66cea 2021-03-22 op tab = tab_by_id(imsg->hdr.peerid);
415 de2a69bb 2021-05-17 op if (tab->fd == -1) {
416 46f6e974 2021-05-17 op if (!tab->buffer.page.free(&tab->buffer.page))
419 0e1eff5b 2021-06-23 op fmt_scaled(tab->bytes, buf);
420 0e1eff5b 2021-06-23 op l = asprintf(&page, "Saved to \"%s\" (%s)\n",
425 de2a69bb 2021-05-17 op load_page_from_str(tab, page);
428 de2a69bb 2021-05-17 op close(tab->fd);
429 de2a69bb 2021-05-17 op tab->fd = -1;
430 de2a69bb 2021-05-17 op free(tab->path);
431 de2a69bb 2021-05-17 op tab->path = NULL;
434 2ba66cea 2021-03-22 op ui_on_tab_refresh(tab);
435 2ba66cea 2021-03-22 op ui_on_tab_loaded(tab);
439 740f578b 2021-03-15 op handle_imsg_bookmark_ok(struct imsg *imsg, size_t datalen)
443 740f578b 2021-03-15 op if (datalen != sizeof(res))
446 740f578b 2021-03-15 op memcpy(&res, imsg->data, sizeof(res));
447 740f578b 2021-03-15 op if (res == 0)
448 7f963c41 2021-06-20 op message("Added to bookmarks!");
450 7f963c41 2021-06-20 op message("Failed to add to bookmarks: %s",
451 740f578b 2021-03-15 op strerror(res));
455 3a227e9a 2021-03-18 op handle_imsg_save_cert_ok(struct imsg *imsg, size_t datalen)
459 3a227e9a 2021-03-18 op if (datalen != sizeof(res))
461 3a227e9a 2021-03-18 op memcpy(&res, imsg->data, datalen);
462 3a227e9a 2021-03-18 op if (res != 0)
463 7f963c41 2021-06-20 op message("Failed to save the cert for: %s",
464 3a227e9a 2021-03-18 op strerror(res));
468 288fd238 2021-04-25 op handle_imsg_update_cert_ok(struct imsg *imsg, size_t datalen)
472 288fd238 2021-04-25 op if (datalen != sizeof(res))
474 288fd238 2021-04-25 op memcpy(&res, imsg->data, datalen);
476 7f963c41 2021-06-20 op message("Failed to update the certificate");
480 1304bbdd 2021-03-15 op handle_dispatch_imsg(int fd, short ev, void *d)
482 35e1f40a 2021-03-14 op struct imsgbuf *ibuf = d;
483 1304bbdd 2021-03-15 op dispatch_imsg(ibuf, handlers, sizeof(handlers));
487 0972d8b2 2021-03-02 op load_page_from_str(struct tab *tab, const char *page)
489 00ccb53d 2021-07-01 op erase_buffer(&tab->buffer);
490 46f6e974 2021-05-17 op gemtext_initparser(&tab->buffer.page);
491 46f6e974 2021-05-17 op if (!tab->buffer.page.parse(&tab->buffer.page, page, strlen(page)))
493 46f6e974 2021-05-17 op if (!tab->buffer.page.free(&tab->buffer.page))
495 0972d8b2 2021-03-02 op ui_on_tab_refresh(tab);
496 8af5e5ed 2021-03-08 op ui_on_tab_loaded(tab);
500 4d3785b1 2021-03-09 op load_about_url(struct tab *tab, const char *url)
502 10346511 2021-03-17 op tab->trust = TS_VERIFIED;
504 46f6e974 2021-05-17 op gemtext_initparser(&tab->buffer.page);
506 35e1f40a 2021-03-14 op imsg_compose(fsibuf, IMSG_GET, tab->id, 0, -1,
507 31f1a758 2021-04-22 op tab->hist_cur->h, strlen(tab->hist_cur->h)+1);
508 35e1f40a 2021-03-14 op imsg_flush(fsibuf);
512 4d3785b1 2021-03-09 op load_gemini_url(struct tab *tab, const char *url)
514 984245ce 2021-06-23 op struct get_req req;
516 2eef3403 2021-04-22 op stop_tab(tab);
517 2eef3403 2021-04-22 op tab->id = tab_new_id();
519 984245ce 2021-06-23 op memset(&req, 0, sizeof(req));
520 984245ce 2021-06-23 op strlcpy(req.host, tab->uri.host, sizeof(req.host));
521 984245ce 2021-06-23 op strlcpy(req.port, tab->uri.port, sizeof(req.host));
523 984245ce 2021-06-23 op strlcpy(req.req, tab->hist_cur->h, sizeof(req.req));
524 984245ce 2021-06-23 op strlcat(req.req, "\r\n", sizeof(req.req));
526 984245ce 2021-06-23 op req.proto = PROTO_GEMINI;
528 984245ce 2021-06-23 op imsg_compose(netibuf, IMSG_GET_RAW, tab->id, 0, -1,
529 984245ce 2021-06-23 op &req, sizeof(req));
530 1b8a4bbf 2021-03-12 op imsg_flush(netibuf);
534 984245ce 2021-06-23 op load_via_proxy(struct tab *tab, const char *url, struct proxy *p)
536 984245ce 2021-06-23 op struct get_req req;
538 984245ce 2021-06-23 op stop_tab(tab);
539 984245ce 2021-06-23 op tab->id = tab_new_id();
540 984245ce 2021-06-23 op tab->proxy = p;
542 984245ce 2021-06-23 op memset(&req, 0, sizeof(req));
543 984245ce 2021-06-23 op strlcpy(req.host, p->host, sizeof(req.host));
544 984245ce 2021-06-23 op strlcpy(req.port, p->port, sizeof(req.host));
546 984245ce 2021-06-23 op strlcpy(req.req, tab->hist_cur->h, sizeof(req.req));
547 984245ce 2021-06-23 op strlcat(req.req, "\r\n", sizeof(req.req));
549 984245ce 2021-06-23 op req.proto = p->proto;
551 984245ce 2021-06-23 op imsg_compose(netibuf, IMSG_GET_RAW, tab->id, 0, -1,
552 984245ce 2021-06-23 op &req, sizeof(req));
553 984245ce 2021-06-23 op imsg_flush(netibuf);
557 2051e653 2021-03-13 op do_load_url(struct tab *tab, const char *url)
559 31f1a758 2021-04-22 op struct phos_uri uri;
560 31f1a758 2021-04-22 op struct proto *p;
561 984245ce 2021-06-23 op struct proxy *proxy;
564 7943bb81 2021-07-10 op tab->proxy = NULL;
566 de2a69bb 2021-05-17 op if (tab->fd != -1) {
567 de2a69bb 2021-05-17 op close(tab->fd);
568 de2a69bb 2021-05-17 op tab->fd = -1;
569 de2a69bb 2021-05-17 op free(tab->path);
570 de2a69bb 2021-05-17 op tab->path = NULL;
573 10346511 2021-03-17 op tab->trust = TS_UNKNOWN;
575 31f1a758 2021-04-22 op memcpy(&uri, &tab->uri, sizeof(tab->uri));
576 31f1a758 2021-04-22 op if (!phos_resolve_uri_from_str(&uri, url, &tab->uri)) {
577 31f1a758 2021-04-22 op if (asprintf(&t, "#error loading %s\n>%s\n",
578 31f1a758 2021-04-22 op url, "Can't parse the URI") == -1)
580 31f1a758 2021-04-22 op strlcpy(tab->hist_cur->h, url, sizeof(tab->hist_cur->h));
581 31f1a758 2021-04-22 op load_page_from_str(tab, t);
586 31f1a758 2021-04-22 op phos_serialize_uri(&tab->uri, tab->hist_cur->h,
587 31f1a758 2021-04-22 op sizeof(tab->hist_cur->h));
589 4d3785b1 2021-03-09 op for (p = protos; p->schema != NULL; ++p) {
590 ed383cf4 2021-04-22 op if (!strcmp(tab->uri.scheme, p->schema)) {
591 4d3785b1 2021-03-09 op p->loadfn(tab, url);
596 984245ce 2021-06-23 op TAILQ_FOREACH(proxy, &proxies, proxies) {
597 984245ce 2021-06-23 op if (!strcmp(tab->uri.scheme, proxy->match_proto)) {
598 984245ce 2021-06-23 op load_via_proxy(tab, url, proxy);
603 4d3785b1 2021-03-09 op protos[0].loadfn(tab, url);
607 2051e653 2021-03-13 op load_url(struct tab *tab, const char *url)
609 2051e653 2021-03-13 op if (tab->hist_cur != NULL)
610 2051e653 2021-03-13 op hist_clear_forward(&tab->hist, TAILQ_NEXT(tab->hist_cur, entries));
612 2051e653 2021-03-13 op if ((tab->hist_cur = calloc(1, sizeof(*tab->hist_cur))) == NULL) {
613 2051e653 2021-03-13 op event_loopbreak();
617 2051e653 2021-03-13 op hist_push(&tab->hist, tab->hist_cur);
618 2051e653 2021-03-13 op do_load_url(tab, url);
619 bca92a4c 2021-07-01 op erase_buffer(&tab->buffer);
623 2051e653 2021-03-13 op load_previous_page(struct tab *tab)
625 2051e653 2021-03-13 op struct hist *h;
627 2051e653 2021-03-13 op if ((h = TAILQ_PREV(tab->hist_cur, mhisthead, entries)) == NULL)
629 2051e653 2021-03-13 op tab->hist_cur = h;
630 2051e653 2021-03-13 op do_load_url(tab, h->h);
635 2051e653 2021-03-13 op load_next_page(struct tab *tab)
637 2051e653 2021-03-13 op struct hist *h;
639 2051e653 2021-03-13 op if ((h = TAILQ_NEXT(tab->hist_cur, entries)) == NULL)
641 2051e653 2021-03-13 op tab->hist_cur = h;
642 2051e653 2021-03-13 op do_load_url(tab, h->h);
647 9ad4627d 2021-03-10 op stop_tab(struct tab *tab)
649 1b8a4bbf 2021-03-12 op imsg_compose(netibuf, IMSG_STOP, tab->id, 0, -1, NULL, 0);
650 1b8a4bbf 2021-03-12 op imsg_flush(netibuf);
652 de2a69bb 2021-05-17 op if (tab->fd != -1) {
653 de2a69bb 2021-05-17 op close(tab->fd);
654 de2a69bb 2021-05-17 op tab->fd = -1;
655 de2a69bb 2021-05-17 op free(tab->path);
656 de2a69bb 2021-05-17 op tab->path = NULL;
657 de2a69bb 2021-05-17 op load_page_from_str(tab, "Stopped.\n");
662 740f578b 2021-03-15 op add_to_bookmarks(const char *str)
664 740f578b 2021-03-15 op imsg_compose(fsibuf, IMSG_BOOKMARK_PAGE, 0, 0, -1, str, strlen(str)+1);
665 c7107cec 2021-04-01 op imsg_flush(fsibuf);
669 c7107cec 2021-04-01 op save_session(void)
671 c7107cec 2021-04-01 op struct tab *tab;
673 c7107cec 2021-04-01 op imsg_compose(fsibuf, IMSG_SESSION_START, 0, 0, -1, NULL, 0);
674 c7107cec 2021-04-01 op imsg_flush(fsibuf);
676 c7107cec 2021-04-01 op TAILQ_FOREACH(tab, &tabshead, tabs) {
677 c7107cec 2021-04-01 op imsg_compose(fsibuf, IMSG_SESSION_TAB, 0, 0, -1,
678 c7107cec 2021-04-01 op tab->hist_cur->h, strlen(tab->hist_cur->h)+1);
679 c7107cec 2021-04-01 op imsg_flush(fsibuf);
682 c7107cec 2021-04-01 op imsg_compose(fsibuf, IMSG_SESSION_END, 0, 0, -1, NULL, 0);
683 740f578b 2021-03-15 op imsg_flush(fsibuf);
687 d2544989 2021-07-08 op session_new_tab_cb(const char *url)
689 d2544989 2021-07-08 op new_tab(url);
693 6cc5fcfe 2021-07-08 op start_child(enum telescope_process p, const char *argv0, int fd)
695 6cc5fcfe 2021-07-08 op const char *argv[4];
696 6cc5fcfe 2021-07-08 op int argc = 0;
699 6cc5fcfe 2021-07-08 op switch (pid = fork()) {
709 6cc5fcfe 2021-07-08 op if (dup2(fd, 3) == -1)
710 6cc5fcfe 2021-07-08 op err(1, "cannot setup imsg fd");
712 6cc5fcfe 2021-07-08 op argv[argc++] = argv0;
714 6cc5fcfe 2021-07-08 op case PROC_UI:
715 6cc5fcfe 2021-07-08 op errx(1, "Can't start ui process");
716 6cc5fcfe 2021-07-08 op case PROC_FS:
717 6cc5fcfe 2021-07-08 op argv[argc++] = "-Tf";
719 6cc5fcfe 2021-07-08 op case PROC_NET:
720 6cc5fcfe 2021-07-08 op argv[argc++] = "-Tn";
724 6cc5fcfe 2021-07-08 op argv[argc++] = NULL;
725 6cc5fcfe 2021-07-08 op execvp(argv0, (char *const *)argv);
726 6cc5fcfe 2021-07-08 op err(1, "execvp(%s)", argv0);
729 6cc5fcfe 2021-07-08 op static void __attribute__((noreturn))
732 d2544989 2021-07-08 op fprintf(stderr, "USAGE: %s [-hn] [-c config] [url]\n", getprogname());
733 d2544989 2021-07-08 op fprintf(stderr, "version: " PACKAGE " " VERSION "\n");
738 6cd6a9e1 2021-03-20 op main(int argc, char * const *argv)
740 17c10c65 2021-07-12 op struct imsgbuf net_ibuf, fs_ibuf;
741 d2544989 2021-07-08 op int net_fds[2], fs_fds[2];
742 d2544989 2021-07-08 op int ch, configtest = 0, fail = 0;
743 d2544989 2021-07-08 op int has_url = 0;
744 6cc5fcfe 2021-07-08 op int proc = -1;
745 d2544989 2021-07-08 op char path[PATH_MAX];
746 6cc5fcfe 2021-07-08 op const char *url = NEW_TAB_URL;
747 6cc5fcfe 2021-07-08 op const char *argv0;
749 6cc5fcfe 2021-07-08 op argv0 = argv[0];
751 5e11c00c 2021-03-02 op signal(SIGCHLD, SIG_IGN);
752 a0e91173 2021-06-13 op signal(SIGPIPE, SIG_IGN);
754 d2544989 2021-07-08 op if (getenv("NO_COLOR") != NULL)
755 d2544989 2021-07-08 op enable_colors = 0;
757 d2544989 2021-07-08 op strlcpy(path, getenv("HOME"), sizeof(path));
758 d2544989 2021-07-08 op strlcat(path, "/.telescope/config", sizeof(path));
760 6cc5fcfe 2021-07-08 op while ((ch = getopt(argc, argv, "c:hnT:")) != -1) {
761 d2544989 2021-07-08 op switch (ch) {
764 d2544989 2021-07-08 op strlcpy(path, optarg, sizeof(path));
767 d2544989 2021-07-08 op configtest = 1;
772 6cc5fcfe 2021-07-08 op switch (*optarg) {
774 6cc5fcfe 2021-07-08 op proc = PROC_FS;
777 6cc5fcfe 2021-07-08 op proc = PROC_NET;
780 6cc5fcfe 2021-07-08 op errx(1, "invalid process spec %c",
789 d2544989 2021-07-08 op argc -= optind;
790 d2544989 2021-07-08 op argv += optind;
792 6cc5fcfe 2021-07-08 op if (proc != -1) {
793 6cc5fcfe 2021-07-08 op if (argc > 0)
795 6cc5fcfe 2021-07-08 op else if (proc == PROC_FS)
796 6cc5fcfe 2021-07-08 op return fs_main();
797 6cc5fcfe 2021-07-08 op else if (proc == PROC_NET)
798 6cc5fcfe 2021-07-08 op return client_main();
803 d2544989 2021-07-08 op if (argc != 0) {
805 d2544989 2021-07-08 op url = argv[0];
808 d5bdf203 2021-07-08 op /* setup keys before reading the config */
809 d5bdf203 2021-07-08 op TAILQ_INIT(&global_map.m);
810 d5bdf203 2021-07-08 op global_map.unhandled_input = global_key_unbound;
811 d5bdf203 2021-07-08 op TAILQ_INIT(&minibuffer_map.m);
813 d2544989 2021-07-08 op config_init();
814 d2544989 2021-07-08 op parseconfig(path, fail);
815 d2544989 2021-07-08 op if (configtest){
816 d2544989 2021-07-08 op puts("config OK");
820 6cc5fcfe 2021-07-08 op /* Start children. */
821 35e1f40a 2021-03-14 op if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, fs_fds) == -1)
822 5e11c00c 2021-03-02 op err(1, "socketpair");
823 6cc5fcfe 2021-07-08 op start_child(PROC_FS, argv0, fs_fds[1]);
824 6cc5fcfe 2021-07-08 op imsg_init(&fs_ibuf, fs_fds[0]);
825 6cc5fcfe 2021-07-08 op fsibuf = &fs_ibuf;
827 35e1f40a 2021-03-14 op if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, net_fds) == -1)
828 35e1f40a 2021-03-14 op err(1, "socketpair");
829 6cc5fcfe 2021-07-08 op start_child(PROC_NET, argv0, net_fds[1]);
830 17c10c65 2021-07-12 op imsg_init(&net_ibuf, net_fds[0]);
831 17c10c65 2021-07-12 op netibuf = &net_ibuf;
833 37d429b0 2021-04-01 op setproctitle("ui");
835 6cc5fcfe 2021-07-08 op /* initialize tofu & load certificates */
837 3768e50f 2021-04-25 op tofu_init(&certs, 5, offsetof(struct tofu_entry, domain));
838 3a227e9a 2021-03-18 op load_certs(&certs);
840 5e11c00c 2021-03-02 op event_init();
842 4ad01575 2021-06-13 op event_set(&netev, netibuf->fd, EV_READ | EV_PERSIST,
843 4ad01575 2021-06-13 op handle_dispatch_imsg, netibuf);
844 35e1f40a 2021-03-14 op event_add(&netev, NULL);
846 4ad01575 2021-06-13 op event_set(&fsev, fsibuf->fd, EV_READ | EV_PERSIST,
847 4ad01575 2021-06-13 op handle_dispatch_imsg, fsibuf);
848 35e1f40a 2021-03-14 op event_add(&fsev, NULL);
850 d2544989 2021-07-08 op if (ui_init()) {
851 d2544989 2021-07-08 op load_last_session(session_new_tab_cb);
852 d2544989 2021-07-08 op if (has_url || TAILQ_EMPTY(&tabshead))
853 d2544989 2021-07-08 op new_tab(url);
855 941b3761 2021-03-18 op sandbox_ui_process();
856 941b3761 2021-03-18 op event_dispatch();
860 1b8a4bbf 2021-03-12 op imsg_compose(netibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
861 1b8a4bbf 2021-03-12 op imsg_flush(netibuf);
863 35e1f40a 2021-03-14 op imsg_compose(fsibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
864 35e1f40a 2021-03-14 op imsg_flush(fsibuf);