Blame


1 5e11c00c 2021-03-02 op #include "telescope.h"
2 5e11c00c 2021-03-02 op
3 5e11c00c 2021-03-02 op #include <sys/socket.h>
4 5e11c00c 2021-03-02 op
5 5e11c00c 2021-03-02 op #include <err.h>
6 5e11c00c 2021-03-02 op #include <errno.h>
7 5e11c00c 2021-03-02 op #include <signal.h>
8 5e11c00c 2021-03-02 op #include <stdio.h>
9 5e11c00c 2021-03-02 op #include <stdlib.h>
10 5e11c00c 2021-03-02 op #include <string.h>
11 5e11c00c 2021-03-02 op #include <unistd.h>
12 5e11c00c 2021-03-02 op
13 35e1f40a 2021-03-14 op struct event netev, fsev;
14 5e11c00c 2021-03-02 op struct tabshead tabshead;
15 5e11c00c 2021-03-02 op
16 c07ac996 2021-03-12 op /* the first is also the fallback one */
17 c07ac996 2021-03-12 op static struct proto protos[] = {
18 4d3785b1 2021-03-09 op { "gemini:", load_gemini_url },
19 4d3785b1 2021-03-09 op { "about:", load_about_url },
20 4d3785b1 2021-03-09 op { NULL, NULL },
21 4d3785b1 2021-03-09 op };
22 4d3785b1 2021-03-09 op
23 35e1f40a 2021-03-14 op static struct imsgbuf *netibuf, *fsibuf;
24 5e11c00c 2021-03-02 op
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 2051e653 2021-03-13 op static void handle_imsg_got_code(struct imsg*, size_t);
30 2051e653 2021-03-13 op static void handle_imsg_got_meta(struct imsg*, size_t);
31 2051e653 2021-03-13 op static void handle_imsg_buf(struct imsg*, size_t);
32 2051e653 2021-03-13 op static void handle_imsg_eof(struct imsg*, size_t);
33 740f578b 2021-03-15 op static void handle_imsg_bookmark_ok(struct imsg*, size_t);
34 3a227e9a 2021-03-18 op static void handle_imsg_save_cert_ok(struct imsg*, size_t);
35 1304bbdd 2021-03-15 op static void handle_dispatch_imsg(int, short, void*);
36 2051e653 2021-03-13 op static void load_page_from_str(struct tab*, const char*);
37 2051e653 2021-03-13 op static void do_load_url(struct tab*, const char*);
38 5e11c00c 2021-03-02 op
39 5e11c00c 2021-03-02 op static imsg_handlerfn *handlers[] = {
40 5e11c00c 2021-03-02 op [IMSG_ERR] = handle_imsg_err,
41 5e11c00c 2021-03-02 op [IMSG_CHECK_CERT] = handle_imsg_check_cert,
42 5e11c00c 2021-03-02 op [IMSG_GOT_CODE] = handle_imsg_got_code,
43 5e11c00c 2021-03-02 op [IMSG_GOT_META] = handle_imsg_got_meta,
44 5e11c00c 2021-03-02 op [IMSG_BUF] = handle_imsg_buf,
45 5e11c00c 2021-03-02 op [IMSG_EOF] = handle_imsg_eof,
46 740f578b 2021-03-15 op [IMSG_BOOKMARK_OK] = handle_imsg_bookmark_ok,
47 3a227e9a 2021-03-18 op [IMSG_SAVE_CERT_OK] = handle_imsg_save_cert_ok,
48 5e11c00c 2021-03-02 op };
49 5e11c00c 2021-03-02 op
50 cbcc75fb 2021-03-17 op static struct ohash certs;
51 cbcc75fb 2021-03-17 op
52 5e11c00c 2021-03-02 op static void __attribute__((__noreturn__))
53 5e11c00c 2021-03-02 op die(void)
54 5e11c00c 2021-03-02 op {
55 5e11c00c 2021-03-02 op abort(); /* TODO */
56 5e11c00c 2021-03-02 op }
57 5e11c00c 2021-03-02 op
58 5e11c00c 2021-03-02 op static struct tab *
59 5e11c00c 2021-03-02 op tab_by_id(uint32_t id)
60 5e11c00c 2021-03-02 op {
61 5e11c00c 2021-03-02 op struct tab *t;
62 5e11c00c 2021-03-02 op
63 5e11c00c 2021-03-02 op TAILQ_FOREACH(t, &tabshead, tabs) {
64 5e11c00c 2021-03-02 op if (t->id == id)
65 5e11c00c 2021-03-02 op return t;
66 5e11c00c 2021-03-02 op }
67 5e11c00c 2021-03-02 op
68 5e11c00c 2021-03-02 op die();
69 5e11c00c 2021-03-02 op }
70 5e11c00c 2021-03-02 op
71 5e11c00c 2021-03-02 op static void
72 5e11c00c 2021-03-02 op handle_imsg_err(struct imsg *imsg, size_t datalen)
73 5e11c00c 2021-03-02 op {
74 3a9b9365 2021-03-09 op struct tab *tab;
75 3a9b9365 2021-03-09 op char *page;
76 3a9b9365 2021-03-09 op
77 3a9b9365 2021-03-09 op tab = tab_by_id(imsg->hdr.peerid);
78 3a9b9365 2021-03-09 op
79 3a9b9365 2021-03-09 op page = imsg->data;
80 3a9b9365 2021-03-09 op page[datalen-1] = '\0';
81 3a9b9365 2021-03-09 op
82 3a9b9365 2021-03-09 op if (asprintf(&page, "# Error loading %s\n\n> %s\n",
83 2051e653 2021-03-13 op tab->hist_cur->h, page) == -1)
84 3a9b9365 2021-03-09 op die();
85 3a9b9365 2021-03-09 op load_page_from_str(tab, page);
86 3a9b9365 2021-03-09 op free(page);
87 5e11c00c 2021-03-02 op }
88 5e11c00c 2021-03-02 op
89 5e11c00c 2021-03-02 op static void
90 5e11c00c 2021-03-02 op handle_imsg_check_cert(struct imsg *imsg, size_t datalen)
91 5e11c00c 2021-03-02 op {
92 cbcc75fb 2021-03-17 op const char *hash;
93 cbcc75fb 2021-03-17 op int tofu_res;
94 cbcc75fb 2021-03-17 op struct tofu_entry *e;
95 cbcc75fb 2021-03-17 op struct tab *tab;
96 5e11c00c 2021-03-02 op
97 cbcc75fb 2021-03-17 op hash = imsg->data;
98 cbcc75fb 2021-03-17 op if (hash[datalen-1] != '\0')
99 cbcc75fb 2021-03-17 op abort();
100 cbcc75fb 2021-03-17 op
101 10346511 2021-03-17 op tab = tab_by_id(imsg->hdr.peerid);
102 cbcc75fb 2021-03-17 op
103 cbcc75fb 2021-03-17 op if ((e = telescope_lookup_tofu(&certs, tab->url.host)) == NULL) {
104 cbcc75fb 2021-03-17 op /* TODO: an update in libressl/libretls changed
105 cbcc75fb 2021-03-17 op * significantly. Find a better approach at storing
106 cbcc75fb 2021-03-17 op * the certs! */
107 cbcc75fb 2021-03-17 op if (datalen > sizeof(e->hash))
108 cbcc75fb 2021-03-17 op abort();
109 cbcc75fb 2021-03-17 op
110 cbcc75fb 2021-03-17 op tofu_res = 1; /* trust on first use */
111 cbcc75fb 2021-03-17 op if ((e = calloc(1, sizeof(*e))) == NULL)
112 cbcc75fb 2021-03-17 op abort();
113 cbcc75fb 2021-03-17 op strlcpy(e->domain, tab->url.host, sizeof(e->domain));
114 cbcc75fb 2021-03-17 op strlcpy(e->hash, hash, sizeof(e->hash));
115 cbcc75fb 2021-03-17 op telescope_ohash_insert(&certs, e);
116 3a227e9a 2021-03-18 op imsg_compose(fsibuf, IMSG_SAVE_CERT, tab->id, 0, -1,
117 3a227e9a 2021-03-18 op e, sizeof(*e));
118 3a227e9a 2021-03-18 op imsg_flush(fsibuf);
119 cbcc75fb 2021-03-17 op } else
120 cbcc75fb 2021-03-17 op tofu_res = !strcmp(hash, e->hash);
121 cbcc75fb 2021-03-17 op
122 cbcc75fb 2021-03-17 op if (tofu_res)
123 cbcc75fb 2021-03-17 op tab->trust = e->verified ? TS_VERIFIED : TS_TRUSTED;
124 cbcc75fb 2021-03-17 op else {
125 cbcc75fb 2021-03-17 op tab->trust = TS_UNTRUSTED;
126 cbcc75fb 2021-03-17 op load_page_from_str(tab, "# Certificate mismatch\n");
127 cbcc75fb 2021-03-17 op }
128 10346511 2021-03-17 op imsg_compose(netibuf, IMSG_CERT_STATUS, imsg->hdr.peerid, 0, -1,
129 10346511 2021-03-17 op &tofu_res, sizeof(tofu_res));
130 1b8a4bbf 2021-03-12 op imsg_flush(netibuf);
131 5e11c00c 2021-03-02 op }
132 5e11c00c 2021-03-02 op
133 5cd2ebb1 2021-03-11 op static inline int
134 5cd2ebb1 2021-03-11 op normalize_code(int n)
135 5cd2ebb1 2021-03-11 op {
136 5cd2ebb1 2021-03-11 op if (n < 20) {
137 5cd2ebb1 2021-03-11 op if (n == 10 || n == 11)
138 5cd2ebb1 2021-03-11 op return n;
139 5cd2ebb1 2021-03-11 op return 10;
140 5cd2ebb1 2021-03-11 op } else if (n < 30) {
141 5cd2ebb1 2021-03-11 op return 20;
142 5cd2ebb1 2021-03-11 op } else if (n < 40) {
143 5cd2ebb1 2021-03-11 op if (n == 30 || n == 31)
144 5cd2ebb1 2021-03-11 op return n;
145 5cd2ebb1 2021-03-11 op return 30;
146 5cd2ebb1 2021-03-11 op } else if (n < 50) {
147 5cd2ebb1 2021-03-11 op if (n <= 44)
148 5cd2ebb1 2021-03-11 op return n;
149 5cd2ebb1 2021-03-11 op return 40;
150 5cd2ebb1 2021-03-11 op } else if (n < 60) {
151 5cd2ebb1 2021-03-11 op if (n <= 53 || n == 59)
152 5cd2ebb1 2021-03-11 op return n;
153 5cd2ebb1 2021-03-11 op return 50;
154 5cd2ebb1 2021-03-11 op } else if (n < 70) {
155 5cd2ebb1 2021-03-11 op if (n <= 62)
156 5cd2ebb1 2021-03-11 op return n;
157 5cd2ebb1 2021-03-11 op return 60;
158 5cd2ebb1 2021-03-11 op } else
159 5cd2ebb1 2021-03-11 op return MALFORMED_RESPONSE;
160 5cd2ebb1 2021-03-11 op }
161 5cd2ebb1 2021-03-11 op
162 5e11c00c 2021-03-02 op static void
163 5e11c00c 2021-03-02 op handle_imsg_got_code(struct imsg *imsg, size_t datalen)
164 5e11c00c 2021-03-02 op {
165 0972d8b2 2021-03-02 op struct tab *tab;
166 5e11c00c 2021-03-02 op
167 0972d8b2 2021-03-02 op tab = tab_by_id(imsg->hdr.peerid);
168 0972d8b2 2021-03-02 op
169 0972d8b2 2021-03-02 op if (sizeof(tab->code) != datalen)
170 5e11c00c 2021-03-02 op die();
171 5e11c00c 2021-03-02 op
172 5cd2ebb1 2021-03-11 op memcpy(&tab->code, imsg->data, sizeof(tab->code));
173 5cd2ebb1 2021-03-11 op tab->code = normalize_code(tab->code);
174 5cd2ebb1 2021-03-11 op if (tab->code != 30 && tab->code != 31)
175 0972d8b2 2021-03-02 op tab->redirect_count = 0;
176 5e11c00c 2021-03-02 op }
177 5e11c00c 2021-03-02 op
178 5e11c00c 2021-03-02 op static void
179 5e11c00c 2021-03-02 op handle_imsg_got_meta(struct imsg *imsg, size_t datalen)
180 5e11c00c 2021-03-02 op {
181 0972d8b2 2021-03-02 op struct tab *tab;
182 0972d8b2 2021-03-02 op
183 0972d8b2 2021-03-02 op tab = tab_by_id(imsg->hdr.peerid);
184 0972d8b2 2021-03-02 op
185 0972d8b2 2021-03-02 op if (sizeof(tab->meta) <= datalen)
186 0972d8b2 2021-03-02 op die();
187 5cd2ebb1 2021-03-11 op
188 0972d8b2 2021-03-02 op memcpy(tab->meta, imsg->data, datalen);
189 0972d8b2 2021-03-02 op
190 5cd2ebb1 2021-03-11 op if (tab->code < 10) { /* internal errors */
191 5cd2ebb1 2021-03-11 op load_page_from_str(tab, err_pages[tab->code]);
192 5cd2ebb1 2021-03-11 op } else if (tab->code < 20) { /* 1x */
193 5cd2ebb1 2021-03-11 op load_page_from_str(tab, err_pages[tab->code]);
194 5cd2ebb1 2021-03-11 op ui_require_input(tab, tab->code == 11);
195 5cd2ebb1 2021-03-11 op } else if (tab->code == 20) {
196 c07ac996 2021-03-12 op if (setup_parser_for(tab)) {
197 1b8a4bbf 2021-03-12 op imsg_compose(netibuf, IMSG_PROCEED, tab->id, 0, -1, NULL, 0);
198 1b8a4bbf 2021-03-12 op imsg_flush(netibuf);
199 c07ac996 2021-03-12 op } else {
200 c07ac996 2021-03-12 op load_page_from_str(tab, err_pages[UNKNOWN_TYPE_OR_CSET]);
201 c07ac996 2021-03-12 op }
202 5cd2ebb1 2021-03-11 op } else if (tab->code < 40) { /* 3x */
203 3a9b9365 2021-03-09 op tab->redirect_count++;
204 0972d8b2 2021-03-02 op
205 3a9b9365 2021-03-09 op /* TODO: make customizable? */
206 3a9b9365 2021-03-09 op if (tab->redirect_count > 5) {
207 3a9b9365 2021-03-09 op load_page_from_str(tab,
208 3a9b9365 2021-03-09 op err_pages[TOO_MUCH_REDIRECTS]);
209 5cd2ebb1 2021-03-11 op } else
210 2051e653 2021-03-13 op do_load_url(tab, tab->meta);
211 5cd2ebb1 2021-03-11 op } else { /* 4x, 5x & 6x */
212 5cd2ebb1 2021-03-11 op load_page_from_str(tab, err_pages[tab->code]);
213 3a9b9365 2021-03-09 op }
214 5e11c00c 2021-03-02 op }
215 5e11c00c 2021-03-02 op
216 5e11c00c 2021-03-02 op static void
217 5e11c00c 2021-03-02 op handle_imsg_buf(struct imsg *imsg, size_t datalen)
218 5e11c00c 2021-03-02 op {
219 0972d8b2 2021-03-02 op struct tab *tab;
220 5e11c00c 2021-03-02 op
221 0972d8b2 2021-03-02 op tab = tab_by_id(imsg->hdr.peerid);
222 5e11c00c 2021-03-02 op
223 0972d8b2 2021-03-02 op if (!tab->page.parse(&tab->page, imsg->data, datalen))
224 5e11c00c 2021-03-02 op die();
225 5e11c00c 2021-03-02 op
226 0972d8b2 2021-03-02 op ui_on_tab_refresh(tab);
227 5e11c00c 2021-03-02 op }
228 5e11c00c 2021-03-02 op
229 5e11c00c 2021-03-02 op static void
230 5e11c00c 2021-03-02 op handle_imsg_eof(struct imsg *imsg, size_t datalen)
231 5e11c00c 2021-03-02 op {
232 a5c3e03d 2021-03-02 op struct tab *t;
233 a5c3e03d 2021-03-02 op
234 a5c3e03d 2021-03-02 op t = tab_by_id(imsg->hdr.peerid);
235 a5c3e03d 2021-03-02 op if (!t->page.free(&t->page))
236 a5c3e03d 2021-03-02 op die();
237 a5c3e03d 2021-03-02 op
238 a5c3e03d 2021-03-02 op ui_on_tab_refresh(t);
239 8af5e5ed 2021-03-08 op ui_on_tab_loaded(t);
240 5e11c00c 2021-03-02 op }
241 5e11c00c 2021-03-02 op
242 5e11c00c 2021-03-02 op static void
243 740f578b 2021-03-15 op handle_imsg_bookmark_ok(struct imsg *imsg, size_t datalen)
244 740f578b 2021-03-15 op {
245 740f578b 2021-03-15 op int res;
246 740f578b 2021-03-15 op
247 740f578b 2021-03-15 op if (datalen != sizeof(res))
248 740f578b 2021-03-15 op die();
249 740f578b 2021-03-15 op
250 740f578b 2021-03-15 op memcpy(&res, imsg->data, sizeof(res));
251 740f578b 2021-03-15 op if (res == 0)
252 740f578b 2021-03-15 op ui_notify("Added to bookmarks!");
253 740f578b 2021-03-15 op else
254 740f578b 2021-03-15 op ui_notify("Failed to add to bookmarks: %s",
255 740f578b 2021-03-15 op strerror(res));
256 740f578b 2021-03-15 op }
257 740f578b 2021-03-15 op
258 740f578b 2021-03-15 op static void
259 3a227e9a 2021-03-18 op handle_imsg_save_cert_ok(struct imsg *imsg, size_t datalen)
260 3a227e9a 2021-03-18 op {
261 3a227e9a 2021-03-18 op int res;
262 3a227e9a 2021-03-18 op
263 3a227e9a 2021-03-18 op if (datalen != sizeof(res))
264 3a227e9a 2021-03-18 op die();
265 3a227e9a 2021-03-18 op memcpy(&res, imsg->data, datalen);
266 3a227e9a 2021-03-18 op if (res != 0)
267 3a227e9a 2021-03-18 op ui_notify("Failed to save the cert for: %s",
268 3a227e9a 2021-03-18 op strerror(res));
269 3a227e9a 2021-03-18 op }
270 3a227e9a 2021-03-18 op
271 3a227e9a 2021-03-18 op static void
272 1304bbdd 2021-03-15 op handle_dispatch_imsg(int fd, short ev, void *d)
273 5e11c00c 2021-03-02 op {
274 35e1f40a 2021-03-14 op struct imsgbuf *ibuf = d;
275 1304bbdd 2021-03-15 op dispatch_imsg(ibuf, handlers, sizeof(handlers));
276 5e11c00c 2021-03-02 op }
277 5e11c00c 2021-03-02 op
278 0972d8b2 2021-03-02 op static void
279 0972d8b2 2021-03-02 op load_page_from_str(struct tab *tab, const char *page)
280 0972d8b2 2021-03-02 op {
281 0972d8b2 2021-03-02 op gemtext_initparser(&tab->page);
282 3a9b9365 2021-03-09 op if (!tab->page.parse(&tab->page, page, strlen(page)))
283 0972d8b2 2021-03-02 op die();
284 0972d8b2 2021-03-02 op if (!tab->page.free(&tab->page))
285 0972d8b2 2021-03-02 op die();
286 0972d8b2 2021-03-02 op ui_on_tab_refresh(tab);
287 8af5e5ed 2021-03-08 op ui_on_tab_loaded(tab);
288 0972d8b2 2021-03-02 op }
289 0972d8b2 2021-03-02 op
290 bcb0b073 2021-03-07 op void
291 4d3785b1 2021-03-09 op load_about_url(struct tab *tab, const char *url)
292 0972d8b2 2021-03-02 op {
293 2051e653 2021-03-13 op char *m;
294 2051e653 2021-03-13 op size_t len;
295 8af5e5ed 2021-03-08 op
296 10346511 2021-03-17 op tab->trust = TS_VERIFIED;
297 2051e653 2021-03-13 op
298 4d3785b1 2021-03-09 op memset(&tab->url, 0, sizeof(tab->url));
299 4d3785b1 2021-03-09 op
300 4d3785b1 2021-03-09 op m = strchr(url, ':');
301 4d3785b1 2021-03-09 op strlcpy(tab->url.scheme, "about", sizeof(tab->url.scheme));
302 4d3785b1 2021-03-09 op strlcpy(tab->url.path, m+1, sizeof(tab->url.path));
303 097ff997 2021-03-09 op
304 2051e653 2021-03-13 op len = sizeof(tab->hist_cur->h)-1;
305 2051e653 2021-03-13 op strlcpy(tab->hist_cur->h, url, len);
306 4d3785b1 2021-03-09 op
307 35e1f40a 2021-03-14 op gemtext_initparser(&tab->page);
308 35e1f40a 2021-03-14 op
309 35e1f40a 2021-03-14 op imsg_compose(fsibuf, IMSG_GET, tab->id, 0, -1,
310 35e1f40a 2021-03-14 op tab->hist_cur->h, len+1);
311 35e1f40a 2021-03-14 op imsg_flush(fsibuf);
312 4d3785b1 2021-03-09 op }
313 0972d8b2 2021-03-02 op
314 4d3785b1 2021-03-09 op void
315 4d3785b1 2021-03-09 op load_gemini_url(struct tab *tab, const char *url)
316 4d3785b1 2021-03-09 op {
317 4d3785b1 2021-03-09 op const char *err;
318 4d3785b1 2021-03-09 op char *p;
319 2051e653 2021-03-13 op size_t len;
320 754622a2 2021-03-15 op
321 754622a2 2021-03-15 op len = sizeof(tab->hist_cur->h)-1;
322 3a9b9365 2021-03-09 op
323 4d3785b1 2021-03-09 op if (has_prefix(url, "gemini:")) {
324 4d3785b1 2021-03-09 op if (!url_parse(url, &tab->url, &err))
325 3a9b9365 2021-03-09 op goto err;
326 4d3785b1 2021-03-09 op } else {
327 4d3785b1 2021-03-09 op if (!url_resolve_from(&tab->url, url, &err))
328 4d3785b1 2021-03-09 op goto err;
329 3a9b9365 2021-03-09 op }
330 3a9b9365 2021-03-09 op
331 2051e653 2021-03-13 op url_unparse(&tab->url, tab->hist_cur->h, len);
332 1b8a4bbf 2021-03-12 op imsg_compose(netibuf, IMSG_GET, tab->id, 0, -1,
333 2051e653 2021-03-13 op tab->hist_cur->h, len+1);
334 1b8a4bbf 2021-03-12 op imsg_flush(netibuf);
335 3a9b9365 2021-03-09 op return;
336 3a9b9365 2021-03-09 op
337 3a9b9365 2021-03-09 op err:
338 4d3785b1 2021-03-09 op if (asprintf(&p, "#error loading %s\n>%s\n",
339 4d3785b1 2021-03-09 op url, err) == -1)
340 3a9b9365 2021-03-09 op die();
341 2051e653 2021-03-13 op strlcpy(tab->hist_cur->h, url, len);
342 4d3785b1 2021-03-09 op load_page_from_str(tab, p);
343 4d3785b1 2021-03-09 op free(p);
344 0972d8b2 2021-03-02 op }
345 0972d8b2 2021-03-02 op
346 2051e653 2021-03-13 op static void
347 2051e653 2021-03-13 op do_load_url(struct tab *tab, const char *url)
348 4d3785b1 2021-03-09 op {
349 4d3785b1 2021-03-09 op struct proto *p;
350 4d3785b1 2021-03-09 op
351 10346511 2021-03-17 op tab->trust = TS_UNKNOWN;
352 10346511 2021-03-17 op
353 4d3785b1 2021-03-09 op for (p = protos; p->schema != NULL; ++p) {
354 4d3785b1 2021-03-09 op if (has_prefix(url, p->schema)) {
355 4d3785b1 2021-03-09 op p->loadfn(tab, url);
356 4d3785b1 2021-03-09 op return;
357 4d3785b1 2021-03-09 op }
358 4d3785b1 2021-03-09 op }
359 4d3785b1 2021-03-09 op
360 4d3785b1 2021-03-09 op protos[0].loadfn(tab, url);
361 4d3785b1 2021-03-09 op }
362 4d3785b1 2021-03-09 op
363 9ad4627d 2021-03-10 op void
364 2051e653 2021-03-13 op load_url(struct tab *tab, const char *url)
365 2051e653 2021-03-13 op {
366 2051e653 2021-03-13 op if (tab->hist_cur != NULL)
367 2051e653 2021-03-13 op hist_clear_forward(&tab->hist, TAILQ_NEXT(tab->hist_cur, entries));
368 2051e653 2021-03-13 op
369 2051e653 2021-03-13 op if ((tab->hist_cur = calloc(1, sizeof(*tab->hist_cur))) == NULL) {
370 2051e653 2021-03-13 op event_loopbreak();
371 2051e653 2021-03-13 op return;
372 2051e653 2021-03-13 op }
373 2051e653 2021-03-13 op hist_push(&tab->hist, tab->hist_cur);
374 2051e653 2021-03-13 op do_load_url(tab, url);
375 2051e653 2021-03-13 op }
376 2051e653 2021-03-13 op
377 2051e653 2021-03-13 op int
378 2051e653 2021-03-13 op load_previous_page(struct tab *tab)
379 2051e653 2021-03-13 op {
380 2051e653 2021-03-13 op struct hist *h;
381 2051e653 2021-03-13 op
382 2051e653 2021-03-13 op if ((h = TAILQ_PREV(tab->hist_cur, mhisthead, entries)) == NULL)
383 2051e653 2021-03-13 op return 0;
384 2051e653 2021-03-13 op tab->hist_cur = h;
385 2051e653 2021-03-13 op do_load_url(tab, h->h);
386 2051e653 2021-03-13 op return 1;
387 2051e653 2021-03-13 op }
388 2051e653 2021-03-13 op
389 2051e653 2021-03-13 op int
390 2051e653 2021-03-13 op load_next_page(struct tab *tab)
391 2051e653 2021-03-13 op {
392 2051e653 2021-03-13 op struct hist *h;
393 2051e653 2021-03-13 op
394 2051e653 2021-03-13 op if ((h = TAILQ_NEXT(tab->hist_cur, entries)) == NULL)
395 2051e653 2021-03-13 op return 0;
396 2051e653 2021-03-13 op tab->hist_cur = h;
397 2051e653 2021-03-13 op do_load_url(tab, h->h);
398 2051e653 2021-03-13 op return 1;
399 2051e653 2021-03-13 op }
400 2051e653 2021-03-13 op
401 2051e653 2021-03-13 op void
402 9ad4627d 2021-03-10 op stop_tab(struct tab *tab)
403 9ad4627d 2021-03-10 op {
404 1b8a4bbf 2021-03-12 op imsg_compose(netibuf, IMSG_STOP, tab->id, 0, -1, NULL, 0);
405 1b8a4bbf 2021-03-12 op imsg_flush(netibuf);
406 9ad4627d 2021-03-10 op }
407 9ad4627d 2021-03-10 op
408 740f578b 2021-03-15 op void
409 740f578b 2021-03-15 op add_to_bookmarks(const char *str)
410 740f578b 2021-03-15 op {
411 740f578b 2021-03-15 op imsg_compose(fsibuf, IMSG_BOOKMARK_PAGE, 0, 0, -1, str, strlen(str)+1);
412 740f578b 2021-03-15 op imsg_flush(fsibuf);
413 740f578b 2021-03-15 op }
414 740f578b 2021-03-15 op
415 5e11c00c 2021-03-02 op int
416 5e11c00c 2021-03-02 op main(void)
417 5e11c00c 2021-03-02 op {
418 35e1f40a 2021-03-14 op struct imsgbuf network_ibuf, fs_ibuf;
419 35e1f40a 2021-03-14 op int net_fds[2], fs_fds[2];
420 35e1f40a 2021-03-14 op pid_t pid;
421 5e11c00c 2021-03-02 op
422 35e1f40a 2021-03-14 op pid = getpid();
423 35e1f40a 2021-03-14 op
424 5e11c00c 2021-03-02 op signal(SIGCHLD, SIG_IGN);
425 5e11c00c 2021-03-02 op
426 3a227e9a 2021-03-18 op /* initialize part of the fs layer. Before starting the UI
427 3a227e9a 2021-03-18 op * and dropping the priviledges we need to read some stuff. */
428 3a227e9a 2021-03-18 op fs_init();
429 3a227e9a 2021-03-18 op
430 35e1f40a 2021-03-14 op if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, fs_fds) == -1)
431 5e11c00c 2021-03-02 op err(1, "socketpair");
432 5e11c00c 2021-03-02 op
433 5e11c00c 2021-03-02 op switch (fork()) {
434 5e11c00c 2021-03-02 op case -1:
435 5e11c00c 2021-03-02 op err(1, "fork");
436 5e11c00c 2021-03-02 op case 0:
437 5e11c00c 2021-03-02 op /* child */
438 35e1f40a 2021-03-14 op setproctitle("(%d) fs", pid);
439 35e1f40a 2021-03-14 op close(fs_fds[0]);
440 35e1f40a 2021-03-14 op imsg_init(&fs_ibuf, fs_fds[1]);
441 35e1f40a 2021-03-14 op exit(fs_main(&fs_ibuf));
442 35e1f40a 2021-03-14 op default:
443 35e1f40a 2021-03-14 op close(fs_fds[1]);
444 35e1f40a 2021-03-14 op imsg_init(&fs_ibuf, fs_fds[0]);
445 35e1f40a 2021-03-14 op fsibuf = &fs_ibuf;
446 5e11c00c 2021-03-02 op }
447 b1d4d01b 2021-03-14 op
448 35e1f40a 2021-03-14 op if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, net_fds) == -1)
449 35e1f40a 2021-03-14 op err(1, "socketpair");
450 5e11c00c 2021-03-02 op
451 35e1f40a 2021-03-14 op switch (fork()) {
452 35e1f40a 2021-03-14 op case -1:
453 35e1f40a 2021-03-14 op err(1, "fork");
454 35e1f40a 2021-03-14 op case 0:
455 35e1f40a 2021-03-14 op /* child */
456 35e1f40a 2021-03-14 op setproctitle("(%d) client", pid);
457 35e1f40a 2021-03-14 op close(net_fds[0]);
458 35e1f40a 2021-03-14 op close(fs_fds[0]);
459 35e1f40a 2021-03-14 op imsg_init(&network_ibuf, net_fds[1]);
460 35e1f40a 2021-03-14 op exit(client_main(&network_ibuf));
461 35e1f40a 2021-03-14 op default:
462 35e1f40a 2021-03-14 op close(net_fds[1]);
463 35e1f40a 2021-03-14 op imsg_init(&network_ibuf, net_fds[0]);
464 35e1f40a 2021-03-14 op netibuf = &network_ibuf;
465 35e1f40a 2021-03-14 op }
466 5e11c00c 2021-03-02 op
467 35e1f40a 2021-03-14 op setproctitle("(%d) ui", pid);
468 35e1f40a 2021-03-14 op
469 cbcc75fb 2021-03-17 op telescope_ohash_init(&certs, 5, offsetof(struct tofu_entry, domain));
470 3a227e9a 2021-03-18 op load_certs(&certs);
471 cbcc75fb 2021-03-17 op
472 5e11c00c 2021-03-02 op TAILQ_INIT(&tabshead);
473 5e11c00c 2021-03-02 op
474 5e11c00c 2021-03-02 op event_init();
475 5e11c00c 2021-03-02 op
476 1304bbdd 2021-03-15 op event_set(&netev, netibuf->fd, EV_READ | EV_PERSIST, handle_dispatch_imsg, netibuf);
477 35e1f40a 2021-03-14 op event_add(&netev, NULL);
478 5e11c00c 2021-03-02 op
479 1304bbdd 2021-03-15 op event_set(&fsev, fsibuf->fd, EV_READ | EV_PERSIST, handle_dispatch_imsg, fsibuf);
480 35e1f40a 2021-03-14 op event_add(&fsev, NULL);
481 35e1f40a 2021-03-14 op
482 5e11c00c 2021-03-02 op ui_init();
483 5e11c00c 2021-03-02 op
484 b1d4d01b 2021-03-14 op sandbox_ui_process();
485 b1d4d01b 2021-03-14 op
486 5e11c00c 2021-03-02 op event_dispatch();
487 5e11c00c 2021-03-02 op
488 1b8a4bbf 2021-03-12 op imsg_compose(netibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
489 1b8a4bbf 2021-03-12 op imsg_flush(netibuf);
490 5e11c00c 2021-03-02 op
491 35e1f40a 2021-03-14 op imsg_compose(fsibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
492 35e1f40a 2021-03-14 op imsg_flush(fsibuf);
493 35e1f40a 2021-03-14 op
494 5e11c00c 2021-03-02 op ui_end();
495 5e11c00c 2021-03-02 op
496 5e11c00c 2021-03-02 op return 0;
497 5e11c00c 2021-03-02 op }