1 5e11c00c 2021-03-02 op #include "telescope.h"
3 5e11c00c 2021-03-02 op #include <sys/socket.h>
5 5e11c00c 2021-03-02 op #include <errno.h>
6 5e11c00c 2021-03-02 op #include <signal.h>
7 5e11c00c 2021-03-02 op #include <stdio.h>
8 5e11c00c 2021-03-02 op #include <stdlib.h>
9 5e11c00c 2021-03-02 op #include <string.h>
10 5e11c00c 2021-03-02 op #include <unistd.h>
12 35e1f40a 2021-03-14 op struct event netev, fsev;
13 5e11c00c 2021-03-02 op struct tabshead tabshead;
14 984245ce 2021-06-23 op struct proxylist proxies;
16 c07ac996 2021-03-12 op /* the first is also the fallback one */
17 c07ac996 2021-03-12 op static struct proto protos[] = {
18 31f1a758 2021-04-22 op { "gemini", load_gemini_url },
19 31f1a758 2021-04-22 op { "about", load_about_url },
20 4d3785b1 2021-03-09 op { NULL, NULL },
23 35e1f40a 2021-03-14 op static struct imsgbuf *netibuf, *fsibuf;
25 2051e653 2021-03-13 op static void die(void) __attribute__((__noreturn__));
26 2051e653 2021-03-13 op static struct tab *tab_by_id(uint32_t);
27 2051e653 2021-03-13 op static void handle_imsg_err(struct imsg*, size_t);
28 2051e653 2021-03-13 op static void handle_imsg_check_cert(struct imsg*, size_t);
29 a2fd3805 2021-07-06 op static void handle_check_cert_user_choice(int, struct tab *);
30 a2fd3805 2021-07-06 op static void handle_maybe_save_new_cert(int, struct tab *);
31 2051e653 2021-03-13 op static void handle_imsg_got_code(struct imsg*, size_t);
32 2051e653 2021-03-13 op static void handle_imsg_got_meta(struct imsg*, size_t);
33 a2fd3805 2021-07-06 op static void handle_maybe_save_page(int, struct tab *);
34 de2a69bb 2021-05-17 op static void handle_save_page_path(const char *, unsigned int);
35 de2a69bb 2021-05-17 op static void handle_imsg_file_opened(struct imsg*, size_t);
36 2051e653 2021-03-13 op static void handle_imsg_buf(struct imsg*, size_t);
37 2051e653 2021-03-13 op static void handle_imsg_eof(struct imsg*, size_t);
38 740f578b 2021-03-15 op static void handle_imsg_bookmark_ok(struct imsg*, size_t);
39 3a227e9a 2021-03-18 op static void handle_imsg_save_cert_ok(struct imsg*, size_t);
40 288fd238 2021-04-25 op static void handle_imsg_update_cert_ok(struct imsg *, size_t);
41 1304bbdd 2021-03-15 op static void handle_dispatch_imsg(int, short, void*);
42 2051e653 2021-03-13 op static void load_page_from_str(struct tab*, const char*);
43 2051e653 2021-03-13 op static void do_load_url(struct tab*, const char*);
45 5e11c00c 2021-03-02 op static imsg_handlerfn *handlers[] = {
46 5e11c00c 2021-03-02 op [IMSG_ERR] = handle_imsg_err,
47 5e11c00c 2021-03-02 op [IMSG_CHECK_CERT] = handle_imsg_check_cert,
48 5e11c00c 2021-03-02 op [IMSG_GOT_CODE] = handle_imsg_got_code,
49 5e11c00c 2021-03-02 op [IMSG_GOT_META] = handle_imsg_got_meta,
50 5e11c00c 2021-03-02 op [IMSG_BUF] = handle_imsg_buf,
51 5e11c00c 2021-03-02 op [IMSG_EOF] = handle_imsg_eof,
52 740f578b 2021-03-15 op [IMSG_BOOKMARK_OK] = handle_imsg_bookmark_ok,
53 3a227e9a 2021-03-18 op [IMSG_SAVE_CERT_OK] = handle_imsg_save_cert_ok,
54 288fd238 2021-04-25 op [IMSG_UPDATE_CERT_OK] = handle_imsg_update_cert_ok,
55 de2a69bb 2021-05-17 op [IMSG_FILE_OPENED] = handle_imsg_file_opened,
58 cbcc75fb 2021-03-17 op static struct ohash certs;
60 5e11c00c 2021-03-02 op static void __attribute__((__noreturn__))
63 5e11c00c 2021-03-02 op abort(); /* TODO */
66 5e11c00c 2021-03-02 op static struct tab *
67 5e11c00c 2021-03-02 op tab_by_id(uint32_t id)
69 5e11c00c 2021-03-02 op struct tab *t;
71 5e11c00c 2021-03-02 op TAILQ_FOREACH(t, &tabshead, tabs) {
72 5e11c00c 2021-03-02 op if (t->id == id)
80 5e11c00c 2021-03-02 op handle_imsg_err(struct imsg *imsg, size_t datalen)
82 3a9b9365 2021-03-09 op struct tab *tab;
85 3a9b9365 2021-03-09 op tab = tab_by_id(imsg->hdr.peerid);
87 3a9b9365 2021-03-09 op page = imsg->data;
88 3a9b9365 2021-03-09 op page[datalen-1] = '\0';
90 3a9b9365 2021-03-09 op if (asprintf(&page, "# Error loading %s\n\n> %s\n",
91 2051e653 2021-03-13 op tab->hist_cur->h, page) == -1)
93 3a9b9365 2021-03-09 op load_page_from_str(tab, page);
98 5e11c00c 2021-03-02 op handle_imsg_check_cert(struct imsg *imsg, size_t datalen)
100 984245ce 2021-06-23 op const char *hash, *host, *port;
101 cbcc75fb 2021-03-17 op int tofu_res;
102 cbcc75fb 2021-03-17 op struct tofu_entry *e;
103 cbcc75fb 2021-03-17 op struct tab *tab;
105 cbcc75fb 2021-03-17 op hash = imsg->data;
106 cbcc75fb 2021-03-17 op if (hash[datalen-1] != '\0')
109 10346511 2021-03-17 op tab = tab_by_id(imsg->hdr.peerid);
111 984245ce 2021-06-23 op if (tab->proxy != NULL) {
112 984245ce 2021-06-23 op host = tab->proxy->host;
113 984245ce 2021-06-23 op port = tab->proxy->port;
115 984245ce 2021-06-23 op host = tab->uri.host;
116 984245ce 2021-06-23 op port = tab->uri.port;
119 984245ce 2021-06-23 op if ((e = tofu_lookup(&certs, host, port)) == NULL) {
120 cbcc75fb 2021-03-17 op /* TODO: an update in libressl/libretls changed
121 cbcc75fb 2021-03-17 op * significantly. Find a better approach at storing
122 cbcc75fb 2021-03-17 op * the certs! */
123 cbcc75fb 2021-03-17 op if (datalen > sizeof(e->hash))
126 cbcc75fb 2021-03-17 op tofu_res = 1; /* trust on first use */
127 cbcc75fb 2021-03-17 op if ((e = calloc(1, sizeof(*e))) == NULL)
129 984245ce 2021-06-23 op strlcpy(e->domain, host, sizeof(e->domain));
130 984245ce 2021-06-23 op if (*port != '\0' && strcmp(port, "1965")) {
131 eb4388ee 2021-04-25 op strlcat(e->domain, ":", sizeof(e->domain));
132 984245ce 2021-06-23 op strlcat(e->domain, port, sizeof(e->domain));
134 cbcc75fb 2021-03-17 op strlcpy(e->hash, hash, sizeof(e->hash));
135 3768e50f 2021-04-25 op tofu_add(&certs, e);
136 3a227e9a 2021-03-18 op imsg_compose(fsibuf, IMSG_SAVE_CERT, tab->id, 0, -1,
137 3a227e9a 2021-03-18 op e, sizeof(*e));
138 3a227e9a 2021-03-18 op imsg_flush(fsibuf);
140 cbcc75fb 2021-03-17 op tofu_res = !strcmp(hash, e->hash);
142 5d1bac73 2021-03-25 op if (tofu_res) {
143 a2fd3805 2021-07-06 op if (e->verified == -1)
144 a2fd3805 2021-07-06 op tab->trust = TS_TEMP_TRUSTED;
145 a2fd3805 2021-07-06 op else if (e->verified == 1)
146 a2fd3805 2021-07-06 op tab->trust = TS_VERIFIED;
148 a2fd3805 2021-07-06 op tab->trust = TS_TRUSTED;
150 5d1bac73 2021-03-25 op imsg_compose(netibuf, IMSG_CERT_STATUS, imsg->hdr.peerid, 0, -1,
151 5d1bac73 2021-03-25 op &tofu_res, sizeof(tofu_res));
152 5d1bac73 2021-03-25 op imsg_flush(netibuf);
154 cbcc75fb 2021-03-17 op tab->trust = TS_UNTRUSTED;
155 cbcc75fb 2021-03-17 op load_page_from_str(tab, "# Certificate mismatch\n");
156 288fd238 2021-04-25 op if ((tab->cert = strdup(hash)) == NULL)
158 5d1bac73 2021-03-25 op ui_yornp("Certificate mismatch. Proceed?",
159 a2fd3805 2021-07-06 op handle_check_cert_user_choice, tab);
164 a2fd3805 2021-07-06 op handle_check_cert_user_choice(int accept, struct tab *tab)
166 a2fd3805 2021-07-06 op imsg_compose(netibuf, IMSG_CERT_STATUS, tab->id, 0, -1,
167 5d1bac73 2021-03-25 op &accept, sizeof(accept));
168 1b8a4bbf 2021-03-12 op imsg_flush(netibuf);
170 a2fd3805 2021-07-06 op if (accept) {
172 a2fd3805 2021-07-06 op * trust the certificate for this session only. If
173 a2fd3805 2021-07-06 op * the page results in a redirect while we're asking
174 a2fd3805 2021-07-06 op * the user to save, we'll end up with an invalid
175 a2fd3805 2021-07-06 op * tabid (one request == one tab id) and crash. It
176 a2fd3805 2021-07-06 op * also makes sense to save it for the current session
177 a2fd3805 2021-07-06 op * if the user accepted it.
179 a2fd3805 2021-07-06 op tofu_temp_trust(&certs, tab->uri.host, tab->uri.port, tab->cert);
181 288fd238 2021-04-25 op ui_yornp("Save the new certificate?",
182 a2fd3805 2021-07-06 op handle_maybe_save_new_cert, tab);
184 288fd238 2021-04-25 op free(tab->cert);
185 288fd238 2021-04-25 op tab->cert = NULL;
190 a2fd3805 2021-07-06 op handle_maybe_save_new_cert(int accept, struct tab *tab)
192 288fd238 2021-04-25 op struct tofu_entry *e;
193 984245ce 2021-06-23 op const char *host, *port;
195 984245ce 2021-06-23 op if (tab->proxy != NULL) {
196 984245ce 2021-06-23 op host = tab->proxy->host;
197 984245ce 2021-06-23 op port = tab->proxy->port;
199 984245ce 2021-06-23 op host = tab->uri.host;
200 984245ce 2021-06-23 op port = tab->uri.port;
206 fe2262ad 2021-05-12 op if ((e = calloc(1, sizeof(*e))) == NULL)
209 984245ce 2021-06-23 op strlcpy(e->domain, host, sizeof(e->domain));
210 984245ce 2021-06-23 op if (*port != '\0' && strcmp(port, "1965")) {
211 288fd238 2021-04-25 op strlcat(e->domain, ":", sizeof(e->domain));
212 984245ce 2021-06-23 op strlcat(e->domain, port, sizeof(e->domain));
214 288fd238 2021-04-25 op strlcpy(e->hash, tab->cert, sizeof(e->hash));
215 288fd238 2021-04-25 op imsg_compose(fsibuf, IMSG_UPDATE_CERT, 0, 0, -1, e, sizeof(*e));
216 288fd238 2021-04-25 op imsg_flush(fsibuf);
218 288fd238 2021-04-25 op tofu_update(&certs, e);
220 288fd238 2021-04-25 op tab->trust = TS_TRUSTED;
223 288fd238 2021-04-25 op free(tab->cert);
224 288fd238 2021-04-25 op tab->cert = NULL;
227 5cd2ebb1 2021-03-11 op static inline int
228 5cd2ebb1 2021-03-11 op normalize_code(int n)
230 5cd2ebb1 2021-03-11 op if (n < 20) {
231 5cd2ebb1 2021-03-11 op if (n == 10 || n == 11)
234 5cd2ebb1 2021-03-11 op } else if (n < 30) {
236 5cd2ebb1 2021-03-11 op } else if (n < 40) {
237 5cd2ebb1 2021-03-11 op if (n == 30 || n == 31)
240 5cd2ebb1 2021-03-11 op } else if (n < 50) {
244 5cd2ebb1 2021-03-11 op } else if (n < 60) {
245 5cd2ebb1 2021-03-11 op if (n <= 53 || n == 59)
248 5cd2ebb1 2021-03-11 op } else if (n < 70) {
253 5cd2ebb1 2021-03-11 op return MALFORMED_RESPONSE;
257 5e11c00c 2021-03-02 op handle_imsg_got_code(struct imsg *imsg, size_t datalen)
259 0972d8b2 2021-03-02 op struct tab *tab;
261 0972d8b2 2021-03-02 op tab = tab_by_id(imsg->hdr.peerid);
263 0972d8b2 2021-03-02 op if (sizeof(tab->code) != datalen)
266 5cd2ebb1 2021-03-11 op memcpy(&tab->code, imsg->data, sizeof(tab->code));
267 5cd2ebb1 2021-03-11 op tab->code = normalize_code(tab->code);
268 5cd2ebb1 2021-03-11 op if (tab->code != 30 && tab->code != 31)
269 0972d8b2 2021-03-02 op tab->redirect_count = 0;
273 5e11c00c 2021-03-02 op handle_imsg_got_meta(struct imsg *imsg, size_t datalen)
275 0972d8b2 2021-03-02 op struct tab *tab;
277 0972d8b2 2021-03-02 op tab = tab_by_id(imsg->hdr.peerid);
279 0972d8b2 2021-03-02 op if (sizeof(tab->meta) <= datalen)
282 0972d8b2 2021-03-02 op memcpy(tab->meta, imsg->data, datalen);
284 5cd2ebb1 2021-03-11 op if (tab->code < 10) { /* internal errors */
285 5cd2ebb1 2021-03-11 op load_page_from_str(tab, err_pages[tab->code]);
286 5cd2ebb1 2021-03-11 op } else if (tab->code < 20) { /* 1x */
287 5cd2ebb1 2021-03-11 op load_page_from_str(tab, err_pages[tab->code]);
288 5cd2ebb1 2021-03-11 op ui_require_input(tab, tab->code == 11);
289 5cd2ebb1 2021-03-11 op } else if (tab->code == 20) {
290 c07ac996 2021-03-12 op if (setup_parser_for(tab)) {
291 1b8a4bbf 2021-03-12 op imsg_compose(netibuf, IMSG_PROCEED, tab->id, 0, -1, NULL, 0);
292 1b8a4bbf 2021-03-12 op imsg_flush(netibuf);
294 c07ac996 2021-03-12 op load_page_from_str(tab, err_pages[UNKNOWN_TYPE_OR_CSET]);
295 de2a69bb 2021-05-17 op ui_yornp("Can't display page, wanna save?",
296 a2fd3805 2021-07-06 op handle_maybe_save_page, tab);
298 5cd2ebb1 2021-03-11 op } else if (tab->code < 40) { /* 3x */
299 3a9b9365 2021-03-09 op tab->redirect_count++;
301 3a9b9365 2021-03-09 op /* TODO: make customizable? */
302 3a9b9365 2021-03-09 op if (tab->redirect_count > 5) {
303 3a9b9365 2021-03-09 op load_page_from_str(tab,
304 3a9b9365 2021-03-09 op err_pages[TOO_MUCH_REDIRECTS]);
306 2051e653 2021-03-13 op do_load_url(tab, tab->meta);
307 5cd2ebb1 2021-03-11 op } else { /* 4x, 5x & 6x */
308 5cd2ebb1 2021-03-11 op load_page_from_str(tab, err_pages[tab->code]);
313 a2fd3805 2021-07-06 op handle_maybe_save_page(int dosave, struct tab *tab)
316 a2fd3805 2021-07-06 op ui_read("Save to path", handle_save_page_path, tab->id);
318 a2fd3805 2021-07-06 op stop_tab(tab);
322 de2a69bb 2021-05-17 op handle_save_page_path(const char *path, unsigned int tabid)
324 de2a69bb 2021-05-17 op struct tab *tab;
326 de2a69bb 2021-05-17 op if (path == NULL) {
327 de2a69bb 2021-05-17 op stop_tab(tab_by_id(tabid));
331 de2a69bb 2021-05-17 op tab = tab_by_id(tabid);
332 de2a69bb 2021-05-17 op tab->path = strdup(path);
334 de2a69bb 2021-05-17 op imsg_compose(fsibuf, IMSG_FILE_OPEN, tabid, 0, -1, path, strlen(path)+1);
335 de2a69bb 2021-05-17 op imsg_flush(fsibuf);
339 de2a69bb 2021-05-17 op handle_imsg_file_opened(struct imsg *imsg, size_t datalen)
341 de2a69bb 2021-05-17 op struct tab *tab;
343 de2a69bb 2021-05-17 op const char *e;
346 de2a69bb 2021-05-17 op tab = tab_by_id(imsg->hdr.peerid);
348 de2a69bb 2021-05-17 op if (imsg->fd == -1) {
349 de2a69bb 2021-05-17 op stop_tab(tab);
351 de2a69bb 2021-05-17 op e = imsg->data;
352 de2a69bb 2021-05-17 op if (e[datalen-1] != '\0')
354 de2a69bb 2021-05-17 op l = asprintf(&page, "# Can't open file\n\n> %s: %s\n",
355 de2a69bb 2021-05-17 op tab->path, e);
358 de2a69bb 2021-05-17 op load_page_from_str(tab, page);
361 de2a69bb 2021-05-17 op tab->fd = imsg->fd;
362 de2a69bb 2021-05-17 op imsg_compose(netibuf, IMSG_PROCEED, tab->id, 0, -1, NULL, 0);
363 de2a69bb 2021-05-17 op imsg_flush(netibuf);
368 5e11c00c 2021-03-02 op handle_imsg_buf(struct imsg *imsg, size_t datalen)
370 0972d8b2 2021-03-02 op struct tab *tab;
372 0e1eff5b 2021-06-23 op char *page, buf[FMT_SCALED_STRSIZE] = {0};
374 0972d8b2 2021-03-02 op tab = tab_by_id(imsg->hdr.peerid);
376 de2a69bb 2021-05-17 op tab->bytes += datalen;
377 de2a69bb 2021-05-17 op if (tab->fd == -1) {
378 46f6e974 2021-05-17 op if (!tab->buffer.page.parse(&tab->buffer.page,
379 de2a69bb 2021-05-17 op imsg->data, datalen))
382 de2a69bb 2021-05-17 op write(tab->fd, imsg->data, datalen);
383 0e1eff5b 2021-06-23 op fmt_scaled(tab->bytes, buf);
384 0e1eff5b 2021-06-23 op l = asprintf(&page, "Saving to \"%s\"... (%s)\n",
389 de2a69bb 2021-05-17 op load_page_from_str(tab, page);
393 0972d8b2 2021-03-02 op ui_on_tab_refresh(tab);
397 5e11c00c 2021-03-02 op handle_imsg_eof(struct imsg *imsg, size_t datalen)
399 2ba66cea 2021-03-22 op struct tab *tab;
401 0e1eff5b 2021-06-23 op char *page, buf[FMT_SCALED_STRSIZE] = {0};
403 2ba66cea 2021-03-22 op tab = tab_by_id(imsg->hdr.peerid);
405 de2a69bb 2021-05-17 op if (tab->fd == -1) {
406 46f6e974 2021-05-17 op if (!tab->buffer.page.free(&tab->buffer.page))
409 0e1eff5b 2021-06-23 op fmt_scaled(tab->bytes, buf);
410 0e1eff5b 2021-06-23 op l = asprintf(&page, "Saved to \"%s\" (%s)\n",
415 de2a69bb 2021-05-17 op load_page_from_str(tab, page);
418 de2a69bb 2021-05-17 op close(tab->fd);
419 de2a69bb 2021-05-17 op tab->fd = -1;
420 de2a69bb 2021-05-17 op free(tab->path);
421 de2a69bb 2021-05-17 op tab->path = NULL;
424 2ba66cea 2021-03-22 op ui_on_tab_refresh(tab);
425 2ba66cea 2021-03-22 op ui_on_tab_loaded(tab);
429 740f578b 2021-03-15 op handle_imsg_bookmark_ok(struct imsg *imsg, size_t datalen)
433 740f578b 2021-03-15 op if (datalen != sizeof(res))
436 740f578b 2021-03-15 op memcpy(&res, imsg->data, sizeof(res));
437 740f578b 2021-03-15 op if (res == 0)
438 7f963c41 2021-06-20 op message("Added to bookmarks!");
440 7f963c41 2021-06-20 op message("Failed to add to bookmarks: %s",
441 740f578b 2021-03-15 op strerror(res));
445 3a227e9a 2021-03-18 op handle_imsg_save_cert_ok(struct imsg *imsg, size_t datalen)
449 3a227e9a 2021-03-18 op if (datalen != sizeof(res))
451 3a227e9a 2021-03-18 op memcpy(&res, imsg->data, datalen);
452 3a227e9a 2021-03-18 op if (res != 0)
453 7f963c41 2021-06-20 op message("Failed to save the cert for: %s",
454 3a227e9a 2021-03-18 op strerror(res));
458 288fd238 2021-04-25 op handle_imsg_update_cert_ok(struct imsg *imsg, size_t datalen)
462 288fd238 2021-04-25 op if (datalen != sizeof(res))
464 288fd238 2021-04-25 op memcpy(&res, imsg->data, datalen);
466 7f963c41 2021-06-20 op message("Failed to update the certificate");
470 1304bbdd 2021-03-15 op handle_dispatch_imsg(int fd, short ev, void *d)
472 35e1f40a 2021-03-14 op struct imsgbuf *ibuf = d;
473 1304bbdd 2021-03-15 op dispatch_imsg(ibuf, handlers, sizeof(handlers));
477 0972d8b2 2021-03-02 op load_page_from_str(struct tab *tab, const char *page)
479 00ccb53d 2021-07-01 op erase_buffer(&tab->buffer);
480 46f6e974 2021-05-17 op gemtext_initparser(&tab->buffer.page);
481 46f6e974 2021-05-17 op if (!tab->buffer.page.parse(&tab->buffer.page, page, strlen(page)))
483 46f6e974 2021-05-17 op if (!tab->buffer.page.free(&tab->buffer.page))
485 0972d8b2 2021-03-02 op ui_on_tab_refresh(tab);
486 8af5e5ed 2021-03-08 op ui_on_tab_loaded(tab);
490 4d3785b1 2021-03-09 op load_about_url(struct tab *tab, const char *url)
492 10346511 2021-03-17 op tab->trust = TS_VERIFIED;
494 46f6e974 2021-05-17 op gemtext_initparser(&tab->buffer.page);
496 35e1f40a 2021-03-14 op imsg_compose(fsibuf, IMSG_GET, tab->id, 0, -1,
497 31f1a758 2021-04-22 op tab->hist_cur->h, strlen(tab->hist_cur->h)+1);
498 35e1f40a 2021-03-14 op imsg_flush(fsibuf);
502 4d3785b1 2021-03-09 op load_gemini_url(struct tab *tab, const char *url)
504 984245ce 2021-06-23 op struct get_req req;
506 2eef3403 2021-04-22 op stop_tab(tab);
507 2eef3403 2021-04-22 op tab->id = tab_new_id();
509 984245ce 2021-06-23 op memset(&req, 0, sizeof(req));
510 984245ce 2021-06-23 op strlcpy(req.host, tab->uri.host, sizeof(req.host));
511 984245ce 2021-06-23 op strlcpy(req.port, tab->uri.port, sizeof(req.host));
513 984245ce 2021-06-23 op strlcpy(req.req, tab->hist_cur->h, sizeof(req.req));
514 984245ce 2021-06-23 op strlcat(req.req, "\r\n", sizeof(req.req));
516 984245ce 2021-06-23 op req.proto = PROTO_GEMINI;
518 984245ce 2021-06-23 op imsg_compose(netibuf, IMSG_GET_RAW, tab->id, 0, -1,
519 984245ce 2021-06-23 op &req, sizeof(req));
520 1b8a4bbf 2021-03-12 op imsg_flush(netibuf);
524 984245ce 2021-06-23 op load_via_proxy(struct tab *tab, const char *url, struct proxy *p)
526 984245ce 2021-06-23 op struct get_req req;
528 984245ce 2021-06-23 op stop_tab(tab);
529 984245ce 2021-06-23 op tab->id = tab_new_id();
530 984245ce 2021-06-23 op tab->proxy = p;
532 984245ce 2021-06-23 op memset(&req, 0, sizeof(req));
533 984245ce 2021-06-23 op strlcpy(req.host, p->host, sizeof(req.host));
534 984245ce 2021-06-23 op strlcpy(req.port, p->port, sizeof(req.host));
536 984245ce 2021-06-23 op strlcpy(req.req, tab->hist_cur->h, sizeof(req.req));
537 984245ce 2021-06-23 op strlcat(req.req, "\r\n", sizeof(req.req));
539 984245ce 2021-06-23 op req.proto = p->proto;
541 984245ce 2021-06-23 op imsg_compose(netibuf, IMSG_GET_RAW, tab->id, 0, -1,
542 984245ce 2021-06-23 op &req, sizeof(req));
543 984245ce 2021-06-23 op imsg_flush(netibuf);
547 2051e653 2021-03-13 op do_load_url(struct tab *tab, const char *url)
549 31f1a758 2021-04-22 op struct phos_uri uri;
550 31f1a758 2021-04-22 op struct proto *p;
551 984245ce 2021-06-23 op struct proxy *proxy;
554 de2a69bb 2021-05-17 op if (tab->fd != -1) {
555 de2a69bb 2021-05-17 op close(tab->fd);
556 de2a69bb 2021-05-17 op tab->fd = -1;
557 de2a69bb 2021-05-17 op free(tab->path);
558 de2a69bb 2021-05-17 op tab->path = NULL;
561 10346511 2021-03-17 op tab->trust = TS_UNKNOWN;
563 31f1a758 2021-04-22 op memcpy(&uri, &tab->uri, sizeof(tab->uri));
564 31f1a758 2021-04-22 op if (!phos_resolve_uri_from_str(&uri, url, &tab->uri)) {
565 31f1a758 2021-04-22 op if (asprintf(&t, "#error loading %s\n>%s\n",
566 31f1a758 2021-04-22 op url, "Can't parse the URI") == -1)
568 31f1a758 2021-04-22 op strlcpy(tab->hist_cur->h, url, sizeof(tab->hist_cur->h));
569 31f1a758 2021-04-22 op load_page_from_str(tab, t);
574 31f1a758 2021-04-22 op phos_serialize_uri(&tab->uri, tab->hist_cur->h,
575 31f1a758 2021-04-22 op sizeof(tab->hist_cur->h));
577 4d3785b1 2021-03-09 op for (p = protos; p->schema != NULL; ++p) {
578 ed383cf4 2021-04-22 op if (!strcmp(tab->uri.scheme, p->schema)) {
579 4d3785b1 2021-03-09 op p->loadfn(tab, url);
584 984245ce 2021-06-23 op TAILQ_FOREACH(proxy, &proxies, proxies) {
585 984245ce 2021-06-23 op if (!strcmp(tab->uri.scheme, proxy->match_proto)) {
586 984245ce 2021-06-23 op load_via_proxy(tab, url, proxy);
591 4d3785b1 2021-03-09 op protos[0].loadfn(tab, url);
595 2051e653 2021-03-13 op load_url(struct tab *tab, const char *url)
597 2051e653 2021-03-13 op if (tab->hist_cur != NULL)
598 2051e653 2021-03-13 op hist_clear_forward(&tab->hist, TAILQ_NEXT(tab->hist_cur, entries));
600 2051e653 2021-03-13 op if ((tab->hist_cur = calloc(1, sizeof(*tab->hist_cur))) == NULL) {
601 2051e653 2021-03-13 op event_loopbreak();
605 984245ce 2021-06-23 op tab->proxy = NULL;
607 2051e653 2021-03-13 op hist_push(&tab->hist, tab->hist_cur);
608 2051e653 2021-03-13 op do_load_url(tab, url);
609 bca92a4c 2021-07-01 op erase_buffer(&tab->buffer);
613 2051e653 2021-03-13 op load_previous_page(struct tab *tab)
615 2051e653 2021-03-13 op struct hist *h;
617 2051e653 2021-03-13 op if ((h = TAILQ_PREV(tab->hist_cur, mhisthead, entries)) == NULL)
619 2051e653 2021-03-13 op tab->hist_cur = h;
620 2051e653 2021-03-13 op do_load_url(tab, h->h);
625 2051e653 2021-03-13 op load_next_page(struct tab *tab)
627 2051e653 2021-03-13 op struct hist *h;
629 2051e653 2021-03-13 op if ((h = TAILQ_NEXT(tab->hist_cur, entries)) == NULL)
631 2051e653 2021-03-13 op tab->hist_cur = h;
632 2051e653 2021-03-13 op do_load_url(tab, h->h);
637 9ad4627d 2021-03-10 op stop_tab(struct tab *tab)
639 1b8a4bbf 2021-03-12 op imsg_compose(netibuf, IMSG_STOP, tab->id, 0, -1, NULL, 0);
640 1b8a4bbf 2021-03-12 op imsg_flush(netibuf);
642 de2a69bb 2021-05-17 op if (tab->fd != -1) {
643 de2a69bb 2021-05-17 op close(tab->fd);
644 de2a69bb 2021-05-17 op tab->fd = -1;
645 de2a69bb 2021-05-17 op free(tab->path);
646 de2a69bb 2021-05-17 op tab->path = NULL;
647 de2a69bb 2021-05-17 op load_page_from_str(tab, "Stopped.\n");
652 740f578b 2021-03-15 op add_to_bookmarks(const char *str)
654 740f578b 2021-03-15 op imsg_compose(fsibuf, IMSG_BOOKMARK_PAGE, 0, 0, -1, str, strlen(str)+1);
655 c7107cec 2021-04-01 op imsg_flush(fsibuf);
659 c7107cec 2021-04-01 op save_session(void)
661 c7107cec 2021-04-01 op struct tab *tab;
663 c7107cec 2021-04-01 op imsg_compose(fsibuf, IMSG_SESSION_START, 0, 0, -1, NULL, 0);
664 c7107cec 2021-04-01 op imsg_flush(fsibuf);
666 c7107cec 2021-04-01 op TAILQ_FOREACH(tab, &tabshead, tabs) {
667 c7107cec 2021-04-01 op imsg_compose(fsibuf, IMSG_SESSION_TAB, 0, 0, -1,
668 c7107cec 2021-04-01 op tab->hist_cur->h, strlen(tab->hist_cur->h)+1);
669 c7107cec 2021-04-01 op imsg_flush(fsibuf);
672 c7107cec 2021-04-01 op imsg_compose(fsibuf, IMSG_SESSION_END, 0, 0, -1, NULL, 0);
673 740f578b 2021-03-15 op imsg_flush(fsibuf);
677 6cd6a9e1 2021-03-20 op main(int argc, char * const *argv)
679 35e1f40a 2021-03-14 op struct imsgbuf network_ibuf, fs_ibuf;
680 35e1f40a 2021-03-14 op int net_fds[2], fs_fds[2];
682 5e11c00c 2021-03-02 op signal(SIGCHLD, SIG_IGN);
683 a0e91173 2021-06-13 op signal(SIGPIPE, SIG_IGN);
685 3a227e9a 2021-03-18 op /* initialize part of the fs layer. Before starting the UI
686 3a227e9a 2021-03-18 op * and dropping the priviledges we need to read some stuff. */
689 35e1f40a 2021-03-14 op if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, fs_fds) == -1)
690 5e11c00c 2021-03-02 op err(1, "socketpair");
692 5e11c00c 2021-03-02 op switch (fork()) {
694 5e11c00c 2021-03-02 op err(1, "fork");
697 37d429b0 2021-04-01 op setproctitle("fs");
698 35e1f40a 2021-03-14 op close(fs_fds[0]);
699 35e1f40a 2021-03-14 op imsg_init(&fs_ibuf, fs_fds[1]);
700 35e1f40a 2021-03-14 op exit(fs_main(&fs_ibuf));
702 35e1f40a 2021-03-14 op close(fs_fds[1]);
703 35e1f40a 2021-03-14 op imsg_init(&fs_ibuf, fs_fds[0]);
704 35e1f40a 2021-03-14 op fsibuf = &fs_ibuf;
707 35e1f40a 2021-03-14 op if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, net_fds) == -1)
708 35e1f40a 2021-03-14 op err(1, "socketpair");
710 35e1f40a 2021-03-14 op switch (fork()) {
712 35e1f40a 2021-03-14 op err(1, "fork");
715 37d429b0 2021-04-01 op setproctitle("client");
716 35e1f40a 2021-03-14 op close(net_fds[0]);
717 35e1f40a 2021-03-14 op close(fs_fds[0]);
718 35e1f40a 2021-03-14 op imsg_init(&network_ibuf, net_fds[1]);
719 35e1f40a 2021-03-14 op exit(client_main(&network_ibuf));
721 35e1f40a 2021-03-14 op close(net_fds[1]);
722 35e1f40a 2021-03-14 op imsg_init(&network_ibuf, net_fds[0]);
723 35e1f40a 2021-03-14 op netibuf = &network_ibuf;
726 37d429b0 2021-04-01 op setproctitle("ui");
728 3768e50f 2021-04-25 op tofu_init(&certs, 5, offsetof(struct tofu_entry, domain));
729 3a227e9a 2021-03-18 op load_certs(&certs);
731 5e11c00c 2021-03-02 op TAILQ_INIT(&tabshead);
732 984245ce 2021-06-23 op TAILQ_INIT(&proxies);
734 5e11c00c 2021-03-02 op event_init();
736 4ad01575 2021-06-13 op event_set(&netev, netibuf->fd, EV_READ | EV_PERSIST,
737 4ad01575 2021-06-13 op handle_dispatch_imsg, netibuf);
738 35e1f40a 2021-03-14 op event_add(&netev, NULL);
740 4ad01575 2021-06-13 op event_set(&fsev, fsibuf->fd, EV_READ | EV_PERSIST,
741 4ad01575 2021-06-13 op handle_dispatch_imsg, fsibuf);
742 35e1f40a 2021-03-14 op event_add(&fsev, NULL);
744 941b3761 2021-03-18 op if (ui_init(argc, argv)) {
745 941b3761 2021-03-18 op sandbox_ui_process();
746 941b3761 2021-03-18 op event_dispatch();
750 1b8a4bbf 2021-03-12 op imsg_compose(netibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
751 1b8a4bbf 2021-03-12 op imsg_flush(netibuf);
753 35e1f40a 2021-03-14 op imsg_compose(fsibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
754 35e1f40a 2021-03-14 op imsg_flush(fsibuf);