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 <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>
11 5e11c00c 2021-03-02 op
12 35e1f40a 2021-03-14 op struct event netev, fsev;
13 5e11c00c 2021-03-02 op struct tabshead tabshead;
14 5e11c00c 2021-03-02 op
15 c07ac996 2021-03-12 op /* the first is also the fallback one */
16 c07ac996 2021-03-12 op static struct proto protos[] = {
17 31f1a758 2021-04-22 op { "gemini", load_gemini_url },
18 31f1a758 2021-04-22 op { "about", load_about_url },
19 4d3785b1 2021-03-09 op { NULL, NULL },
20 4d3785b1 2021-03-09 op };
21 4d3785b1 2021-03-09 op
22 35e1f40a 2021-03-14 op static struct imsgbuf *netibuf, *fsibuf;
23 5e11c00c 2021-03-02 op
24 2051e653 2021-03-13 op static void die(void) __attribute__((__noreturn__));
25 2051e653 2021-03-13 op static struct tab *tab_by_id(uint32_t);
26 2051e653 2021-03-13 op static void handle_imsg_err(struct imsg*, size_t);
27 2051e653 2021-03-13 op static void handle_imsg_check_cert(struct imsg*, size_t);
28 b3575139 2021-04-01 op static void handle_check_cert_user_choice(int, unsigned int);
29 288fd238 2021-04-25 op static void handle_maybe_save_new_cert(int, unsigned int);
30 2051e653 2021-03-13 op static void handle_imsg_got_code(struct imsg*, size_t);
31 2051e653 2021-03-13 op static void handle_imsg_got_meta(struct imsg*, size_t);
32 de2a69bb 2021-05-17 op static void handle_maybe_save_page(int, unsigned int);
33 de2a69bb 2021-05-17 op static void handle_save_page_path(const char *, unsigned int);
34 de2a69bb 2021-05-17 op static void handle_imsg_file_opened(struct imsg*, size_t);
35 2051e653 2021-03-13 op static void handle_imsg_buf(struct imsg*, size_t);
36 2051e653 2021-03-13 op static void handle_imsg_eof(struct imsg*, size_t);
37 740f578b 2021-03-15 op static void handle_imsg_bookmark_ok(struct imsg*, size_t);
38 3a227e9a 2021-03-18 op static void handle_imsg_save_cert_ok(struct imsg*, size_t);
39 288fd238 2021-04-25 op static void handle_imsg_update_cert_ok(struct imsg *, size_t);
40 1304bbdd 2021-03-15 op static void handle_dispatch_imsg(int, short, void*);
41 2051e653 2021-03-13 op static void load_page_from_str(struct tab*, const char*);
42 2051e653 2021-03-13 op static void do_load_url(struct tab*, const char*);
43 5e11c00c 2021-03-02 op
44 5e11c00c 2021-03-02 op static imsg_handlerfn *handlers[] = {
45 5e11c00c 2021-03-02 op [IMSG_ERR] = handle_imsg_err,
46 5e11c00c 2021-03-02 op [IMSG_CHECK_CERT] = handle_imsg_check_cert,
47 5e11c00c 2021-03-02 op [IMSG_GOT_CODE] = handle_imsg_got_code,
48 5e11c00c 2021-03-02 op [IMSG_GOT_META] = handle_imsg_got_meta,
49 5e11c00c 2021-03-02 op [IMSG_BUF] = handle_imsg_buf,
50 5e11c00c 2021-03-02 op [IMSG_EOF] = handle_imsg_eof,
51 740f578b 2021-03-15 op [IMSG_BOOKMARK_OK] = handle_imsg_bookmark_ok,
52 3a227e9a 2021-03-18 op [IMSG_SAVE_CERT_OK] = handle_imsg_save_cert_ok,
53 288fd238 2021-04-25 op [IMSG_UPDATE_CERT_OK] = handle_imsg_update_cert_ok,
54 de2a69bb 2021-05-17 op [IMSG_FILE_OPENED] = handle_imsg_file_opened,
55 5e11c00c 2021-03-02 op };
56 5e11c00c 2021-03-02 op
57 cbcc75fb 2021-03-17 op static struct ohash certs;
58 cbcc75fb 2021-03-17 op
59 5e11c00c 2021-03-02 op static void __attribute__((__noreturn__))
60 5e11c00c 2021-03-02 op die(void)
61 5e11c00c 2021-03-02 op {
62 5e11c00c 2021-03-02 op abort(); /* TODO */
63 5e11c00c 2021-03-02 op }
64 5e11c00c 2021-03-02 op
65 5e11c00c 2021-03-02 op static struct tab *
66 5e11c00c 2021-03-02 op tab_by_id(uint32_t id)
67 5e11c00c 2021-03-02 op {
68 5e11c00c 2021-03-02 op struct tab *t;
69 5e11c00c 2021-03-02 op
70 5e11c00c 2021-03-02 op TAILQ_FOREACH(t, &tabshead, tabs) {
71 5e11c00c 2021-03-02 op if (t->id == id)
72 5e11c00c 2021-03-02 op return t;
73 5e11c00c 2021-03-02 op }
74 5e11c00c 2021-03-02 op
75 5e11c00c 2021-03-02 op die();
76 5e11c00c 2021-03-02 op }
77 5e11c00c 2021-03-02 op
78 5e11c00c 2021-03-02 op static void
79 5e11c00c 2021-03-02 op handle_imsg_err(struct imsg *imsg, size_t datalen)
80 5e11c00c 2021-03-02 op {
81 3a9b9365 2021-03-09 op struct tab *tab;
82 3a9b9365 2021-03-09 op char *page;
83 3a9b9365 2021-03-09 op
84 3a9b9365 2021-03-09 op tab = tab_by_id(imsg->hdr.peerid);
85 3a9b9365 2021-03-09 op
86 3a9b9365 2021-03-09 op page = imsg->data;
87 3a9b9365 2021-03-09 op page[datalen-1] = '\0';
88 3a9b9365 2021-03-09 op
89 3a9b9365 2021-03-09 op if (asprintf(&page, "# Error loading %s\n\n> %s\n",
90 2051e653 2021-03-13 op tab->hist_cur->h, page) == -1)
91 3a9b9365 2021-03-09 op die();
92 3a9b9365 2021-03-09 op load_page_from_str(tab, page);
93 3a9b9365 2021-03-09 op free(page);
94 5e11c00c 2021-03-02 op }
95 5e11c00c 2021-03-02 op
96 5e11c00c 2021-03-02 op static void
97 5e11c00c 2021-03-02 op handle_imsg_check_cert(struct imsg *imsg, size_t datalen)
98 5e11c00c 2021-03-02 op {
99 cbcc75fb 2021-03-17 op const char *hash;
100 cbcc75fb 2021-03-17 op int tofu_res;
101 cbcc75fb 2021-03-17 op struct tofu_entry *e;
102 cbcc75fb 2021-03-17 op struct tab *tab;
103 5e11c00c 2021-03-02 op
104 cbcc75fb 2021-03-17 op hash = imsg->data;
105 cbcc75fb 2021-03-17 op if (hash[datalen-1] != '\0')
106 cbcc75fb 2021-03-17 op abort();
107 cbcc75fb 2021-03-17 op
108 10346511 2021-03-17 op tab = tab_by_id(imsg->hdr.peerid);
109 cbcc75fb 2021-03-17 op
110 3768e50f 2021-04-25 op if ((e = tofu_lookup(&certs, tab->uri.host, tab->uri.port)) == NULL) {
111 cbcc75fb 2021-03-17 op /* TODO: an update in libressl/libretls changed
112 cbcc75fb 2021-03-17 op * significantly. Find a better approach at storing
113 cbcc75fb 2021-03-17 op * the certs! */
114 cbcc75fb 2021-03-17 op if (datalen > sizeof(e->hash))
115 cbcc75fb 2021-03-17 op abort();
116 cbcc75fb 2021-03-17 op
117 cbcc75fb 2021-03-17 op tofu_res = 1; /* trust on first use */
118 cbcc75fb 2021-03-17 op if ((e = calloc(1, sizeof(*e))) == NULL)
119 cbcc75fb 2021-03-17 op abort();
120 31f1a758 2021-04-22 op strlcpy(e->domain, tab->uri.host, sizeof(e->domain));
121 eb4388ee 2021-04-25 op if (*tab->uri.port != '\0' && strcmp(tab->uri.port, "1965")) {
122 eb4388ee 2021-04-25 op strlcat(e->domain, ":", sizeof(e->domain));
123 eb4388ee 2021-04-25 op strlcat(e->domain, tab->uri.port, sizeof(e->domain));
124 eb4388ee 2021-04-25 op }
125 cbcc75fb 2021-03-17 op strlcpy(e->hash, hash, sizeof(e->hash));
126 3768e50f 2021-04-25 op tofu_add(&certs, e);
127 3a227e9a 2021-03-18 op imsg_compose(fsibuf, IMSG_SAVE_CERT, tab->id, 0, -1,
128 3a227e9a 2021-03-18 op e, sizeof(*e));
129 3a227e9a 2021-03-18 op imsg_flush(fsibuf);
130 cbcc75fb 2021-03-17 op } else
131 cbcc75fb 2021-03-17 op tofu_res = !strcmp(hash, e->hash);
132 cbcc75fb 2021-03-17 op
133 5d1bac73 2021-03-25 op if (tofu_res) {
134 cbcc75fb 2021-03-17 op tab->trust = e->verified ? TS_VERIFIED : TS_TRUSTED;
135 5d1bac73 2021-03-25 op imsg_compose(netibuf, IMSG_CERT_STATUS, imsg->hdr.peerid, 0, -1,
136 5d1bac73 2021-03-25 op &tofu_res, sizeof(tofu_res));
137 5d1bac73 2021-03-25 op imsg_flush(netibuf);
138 5d1bac73 2021-03-25 op } else {
139 cbcc75fb 2021-03-17 op tab->trust = TS_UNTRUSTED;
140 cbcc75fb 2021-03-17 op load_page_from_str(tab, "# Certificate mismatch\n");
141 288fd238 2021-04-25 op if ((tab->cert = strdup(hash)) == NULL)
142 288fd238 2021-04-25 op die();
143 5d1bac73 2021-03-25 op ui_yornp("Certificate mismatch. Proceed?",
144 b3575139 2021-04-01 op handle_check_cert_user_choice, tab->id);
145 cbcc75fb 2021-03-17 op }
146 5d1bac73 2021-03-25 op }
147 5d1bac73 2021-03-25 op
148 5d1bac73 2021-03-25 op static void
149 b3575139 2021-04-01 op handle_check_cert_user_choice(int accept, unsigned int tabid)
150 5d1bac73 2021-03-25 op {
151 288fd238 2021-04-25 op struct tab *tab;
152 288fd238 2021-04-25 op
153 288fd238 2021-04-25 op tab = tab_by_id(tabid);
154 288fd238 2021-04-25 op
155 5d1bac73 2021-03-25 op imsg_compose(netibuf, IMSG_CERT_STATUS, tabid, 0, -1,
156 5d1bac73 2021-03-25 op &accept, sizeof(accept));
157 1b8a4bbf 2021-03-12 op imsg_flush(netibuf);
158 288fd238 2021-04-25 op
159 288fd238 2021-04-25 op if (accept)
160 288fd238 2021-04-25 op ui_yornp("Save the new certificate?",
161 288fd238 2021-04-25 op handle_maybe_save_new_cert, tabid);
162 288fd238 2021-04-25 op else {
163 288fd238 2021-04-25 op free(tab->cert);
164 288fd238 2021-04-25 op tab->cert = NULL;
165 288fd238 2021-04-25 op }
166 5e11c00c 2021-03-02 op }
167 5e11c00c 2021-03-02 op
168 288fd238 2021-04-25 op static void
169 288fd238 2021-04-25 op handle_maybe_save_new_cert(int accept, unsigned int tabid)
170 288fd238 2021-04-25 op {
171 288fd238 2021-04-25 op struct tab *tab;
172 288fd238 2021-04-25 op struct tofu_entry *e;
173 288fd238 2021-04-25 op
174 288fd238 2021-04-25 op tab = tab_by_id(tabid);
175 288fd238 2021-04-25 op
176 288fd238 2021-04-25 op if (!accept)
177 288fd238 2021-04-25 op goto end;
178 288fd238 2021-04-25 op
179 fe2262ad 2021-05-12 op if ((e = calloc(1, sizeof(*e))) == NULL)
180 288fd238 2021-04-25 op die();
181 288fd238 2021-04-25 op
182 288fd238 2021-04-25 op strlcpy(e->domain, tab->uri.host, sizeof(e->domain));
183 288fd238 2021-04-25 op if (*tab->uri.port != '\0' && strcmp(tab->uri.port, "1965")) {
184 288fd238 2021-04-25 op strlcat(e->domain, ":", sizeof(e->domain));
185 288fd238 2021-04-25 op strlcat(e->domain, tab->uri.port, sizeof(e->domain));
186 288fd238 2021-04-25 op }
187 288fd238 2021-04-25 op strlcpy(e->hash, tab->cert, sizeof(e->hash));
188 288fd238 2021-04-25 op imsg_compose(fsibuf, IMSG_UPDATE_CERT, 0, 0, -1, e, sizeof(*e));
189 288fd238 2021-04-25 op imsg_flush(fsibuf);
190 288fd238 2021-04-25 op
191 288fd238 2021-04-25 op tofu_update(&certs, e);
192 288fd238 2021-04-25 op
193 288fd238 2021-04-25 op tab->trust = TS_TRUSTED;
194 288fd238 2021-04-25 op
195 288fd238 2021-04-25 op end:
196 288fd238 2021-04-25 op free(tab->cert);
197 288fd238 2021-04-25 op tab->cert = NULL;
198 288fd238 2021-04-25 op }
199 288fd238 2021-04-25 op
200 5cd2ebb1 2021-03-11 op static inline int
201 5cd2ebb1 2021-03-11 op normalize_code(int n)
202 5cd2ebb1 2021-03-11 op {
203 5cd2ebb1 2021-03-11 op if (n < 20) {
204 5cd2ebb1 2021-03-11 op if (n == 10 || n == 11)
205 5cd2ebb1 2021-03-11 op return n;
206 5cd2ebb1 2021-03-11 op return 10;
207 5cd2ebb1 2021-03-11 op } else if (n < 30) {
208 5cd2ebb1 2021-03-11 op return 20;
209 5cd2ebb1 2021-03-11 op } else if (n < 40) {
210 5cd2ebb1 2021-03-11 op if (n == 30 || n == 31)
211 5cd2ebb1 2021-03-11 op return n;
212 5cd2ebb1 2021-03-11 op return 30;
213 5cd2ebb1 2021-03-11 op } else if (n < 50) {
214 5cd2ebb1 2021-03-11 op if (n <= 44)
215 5cd2ebb1 2021-03-11 op return n;
216 5cd2ebb1 2021-03-11 op return 40;
217 5cd2ebb1 2021-03-11 op } else if (n < 60) {
218 5cd2ebb1 2021-03-11 op if (n <= 53 || n == 59)
219 5cd2ebb1 2021-03-11 op return n;
220 5cd2ebb1 2021-03-11 op return 50;
221 5cd2ebb1 2021-03-11 op } else if (n < 70) {
222 5cd2ebb1 2021-03-11 op if (n <= 62)
223 5cd2ebb1 2021-03-11 op return n;
224 5cd2ebb1 2021-03-11 op return 60;
225 5cd2ebb1 2021-03-11 op } else
226 5cd2ebb1 2021-03-11 op return MALFORMED_RESPONSE;
227 5cd2ebb1 2021-03-11 op }
228 5cd2ebb1 2021-03-11 op
229 5e11c00c 2021-03-02 op static void
230 5e11c00c 2021-03-02 op handle_imsg_got_code(struct imsg *imsg, size_t datalen)
231 5e11c00c 2021-03-02 op {
232 0972d8b2 2021-03-02 op struct tab *tab;
233 5e11c00c 2021-03-02 op
234 0972d8b2 2021-03-02 op tab = tab_by_id(imsg->hdr.peerid);
235 0972d8b2 2021-03-02 op
236 0972d8b2 2021-03-02 op if (sizeof(tab->code) != datalen)
237 5e11c00c 2021-03-02 op die();
238 5e11c00c 2021-03-02 op
239 5cd2ebb1 2021-03-11 op memcpy(&tab->code, imsg->data, sizeof(tab->code));
240 5cd2ebb1 2021-03-11 op tab->code = normalize_code(tab->code);
241 5cd2ebb1 2021-03-11 op if (tab->code != 30 && tab->code != 31)
242 0972d8b2 2021-03-02 op tab->redirect_count = 0;
243 5e11c00c 2021-03-02 op }
244 5e11c00c 2021-03-02 op
245 5e11c00c 2021-03-02 op static void
246 5e11c00c 2021-03-02 op handle_imsg_got_meta(struct imsg *imsg, size_t datalen)
247 5e11c00c 2021-03-02 op {
248 0972d8b2 2021-03-02 op struct tab *tab;
249 0972d8b2 2021-03-02 op
250 0972d8b2 2021-03-02 op tab = tab_by_id(imsg->hdr.peerid);
251 0972d8b2 2021-03-02 op
252 0972d8b2 2021-03-02 op if (sizeof(tab->meta) <= datalen)
253 0972d8b2 2021-03-02 op die();
254 5cd2ebb1 2021-03-11 op
255 0972d8b2 2021-03-02 op memcpy(tab->meta, imsg->data, datalen);
256 0972d8b2 2021-03-02 op
257 5cd2ebb1 2021-03-11 op if (tab->code < 10) { /* internal errors */
258 5cd2ebb1 2021-03-11 op load_page_from_str(tab, err_pages[tab->code]);
259 5cd2ebb1 2021-03-11 op } else if (tab->code < 20) { /* 1x */
260 5cd2ebb1 2021-03-11 op load_page_from_str(tab, err_pages[tab->code]);
261 5cd2ebb1 2021-03-11 op ui_require_input(tab, tab->code == 11);
262 5cd2ebb1 2021-03-11 op } else if (tab->code == 20) {
263 c07ac996 2021-03-12 op if (setup_parser_for(tab)) {
264 1b8a4bbf 2021-03-12 op imsg_compose(netibuf, IMSG_PROCEED, tab->id, 0, -1, NULL, 0);
265 1b8a4bbf 2021-03-12 op imsg_flush(netibuf);
266 c07ac996 2021-03-12 op } else {
267 c07ac996 2021-03-12 op load_page_from_str(tab, err_pages[UNKNOWN_TYPE_OR_CSET]);
268 de2a69bb 2021-05-17 op ui_yornp("Can't display page, wanna save?",
269 de2a69bb 2021-05-17 op handle_maybe_save_page, tab->id);
270 c07ac996 2021-03-12 op }
271 5cd2ebb1 2021-03-11 op } else if (tab->code < 40) { /* 3x */
272 3a9b9365 2021-03-09 op tab->redirect_count++;
273 0972d8b2 2021-03-02 op
274 3a9b9365 2021-03-09 op /* TODO: make customizable? */
275 3a9b9365 2021-03-09 op if (tab->redirect_count > 5) {
276 3a9b9365 2021-03-09 op load_page_from_str(tab,
277 3a9b9365 2021-03-09 op err_pages[TOO_MUCH_REDIRECTS]);
278 5cd2ebb1 2021-03-11 op } else
279 2051e653 2021-03-13 op do_load_url(tab, tab->meta);
280 5cd2ebb1 2021-03-11 op } else { /* 4x, 5x & 6x */
281 5cd2ebb1 2021-03-11 op load_page_from_str(tab, err_pages[tab->code]);
282 3a9b9365 2021-03-09 op }
283 de2a69bb 2021-05-17 op }
284 de2a69bb 2021-05-17 op
285 de2a69bb 2021-05-17 op static void
286 de2a69bb 2021-05-17 op handle_maybe_save_page(int dosave, unsigned int tabid)
287 de2a69bb 2021-05-17 op {
288 de2a69bb 2021-05-17 op if (dosave)
289 de2a69bb 2021-05-17 op ui_read("Save to path", handle_save_page_path, tabid);
290 de2a69bb 2021-05-17 op else
291 de2a69bb 2021-05-17 op stop_tab(tab_by_id(tabid));
292 de2a69bb 2021-05-17 op }
293 de2a69bb 2021-05-17 op
294 de2a69bb 2021-05-17 op static void
295 de2a69bb 2021-05-17 op handle_save_page_path(const char *path, unsigned int tabid)
296 de2a69bb 2021-05-17 op {
297 de2a69bb 2021-05-17 op struct tab *tab;
298 de2a69bb 2021-05-17 op
299 de2a69bb 2021-05-17 op if (path == NULL) {
300 de2a69bb 2021-05-17 op stop_tab(tab_by_id(tabid));
301 de2a69bb 2021-05-17 op return;
302 de2a69bb 2021-05-17 op }
303 de2a69bb 2021-05-17 op
304 de2a69bb 2021-05-17 op tab = tab_by_id(tabid);
305 de2a69bb 2021-05-17 op tab->path = strdup(path);
306 de2a69bb 2021-05-17 op
307 de2a69bb 2021-05-17 op imsg_compose(fsibuf, IMSG_FILE_OPEN, tabid, 0, -1, path, strlen(path)+1);
308 de2a69bb 2021-05-17 op imsg_flush(fsibuf);
309 5e11c00c 2021-03-02 op }
310 5e11c00c 2021-03-02 op
311 5e11c00c 2021-03-02 op static void
312 de2a69bb 2021-05-17 op handle_imsg_file_opened(struct imsg *imsg, size_t datalen)
313 de2a69bb 2021-05-17 op {
314 de2a69bb 2021-05-17 op struct tab *tab;
315 de2a69bb 2021-05-17 op char *page;
316 de2a69bb 2021-05-17 op const char *e;
317 de2a69bb 2021-05-17 op int l;
318 de2a69bb 2021-05-17 op
319 de2a69bb 2021-05-17 op tab = tab_by_id(imsg->hdr.peerid);
320 de2a69bb 2021-05-17 op
321 de2a69bb 2021-05-17 op if (imsg->fd == -1) {
322 de2a69bb 2021-05-17 op stop_tab(tab);
323 de2a69bb 2021-05-17 op
324 de2a69bb 2021-05-17 op e = imsg->data;
325 de2a69bb 2021-05-17 op if (e[datalen-1] != '\0')
326 de2a69bb 2021-05-17 op die();
327 de2a69bb 2021-05-17 op l = asprintf(&page, "# Can't open file\n\n> %s: %s\n",
328 de2a69bb 2021-05-17 op tab->path, e);
329 de2a69bb 2021-05-17 op if (l == -1)
330 de2a69bb 2021-05-17 op die();
331 de2a69bb 2021-05-17 op load_page_from_str(tab, page);
332 de2a69bb 2021-05-17 op free(page);
333 de2a69bb 2021-05-17 op } else {
334 de2a69bb 2021-05-17 op tab->fd = imsg->fd;
335 de2a69bb 2021-05-17 op imsg_compose(netibuf, IMSG_PROCEED, tab->id, 0, -1, NULL, 0);
336 de2a69bb 2021-05-17 op imsg_flush(netibuf);
337 de2a69bb 2021-05-17 op }
338 de2a69bb 2021-05-17 op }
339 de2a69bb 2021-05-17 op
340 de2a69bb 2021-05-17 op static void
341 5e11c00c 2021-03-02 op handle_imsg_buf(struct imsg *imsg, size_t datalen)
342 5e11c00c 2021-03-02 op {
343 0972d8b2 2021-03-02 op struct tab *tab;
344 de2a69bb 2021-05-17 op int l;
345 de2a69bb 2021-05-17 op char *page;
346 5e11c00c 2021-03-02 op
347 0972d8b2 2021-03-02 op tab = tab_by_id(imsg->hdr.peerid);
348 5e11c00c 2021-03-02 op
349 de2a69bb 2021-05-17 op tab->bytes += datalen;
350 de2a69bb 2021-05-17 op if (tab->fd == -1) {
351 46f6e974 2021-05-17 op if (!tab->buffer.page.parse(&tab->buffer.page,
352 de2a69bb 2021-05-17 op imsg->data, datalen))
353 de2a69bb 2021-05-17 op die();
354 de2a69bb 2021-05-17 op } else {
355 de2a69bb 2021-05-17 op write(tab->fd, imsg->data, datalen);
356 de2a69bb 2021-05-17 op l = asprintf(&page, "Writing \"%s\"... (%zu bytes)\n",
357 de2a69bb 2021-05-17 op tab->path,
358 de2a69bb 2021-05-17 op tab->bytes);
359 de2a69bb 2021-05-17 op if (l == -1)
360 de2a69bb 2021-05-17 op die();
361 de2a69bb 2021-05-17 op load_page_from_str(tab, page);
362 de2a69bb 2021-05-17 op free(page);
363 de2a69bb 2021-05-17 op }
364 5e11c00c 2021-03-02 op
365 0972d8b2 2021-03-02 op ui_on_tab_refresh(tab);
366 5e11c00c 2021-03-02 op }
367 5e11c00c 2021-03-02 op
368 5e11c00c 2021-03-02 op static void
369 5e11c00c 2021-03-02 op handle_imsg_eof(struct imsg *imsg, size_t datalen)
370 5e11c00c 2021-03-02 op {
371 2ba66cea 2021-03-22 op struct tab *tab;
372 de2a69bb 2021-05-17 op int l;
373 de2a69bb 2021-05-17 op char *page;
374 a5c3e03d 2021-03-02 op
375 2ba66cea 2021-03-22 op tab = tab_by_id(imsg->hdr.peerid);
376 de2a69bb 2021-05-17 op
377 de2a69bb 2021-05-17 op if (tab->fd == -1) {
378 46f6e974 2021-05-17 op if (!tab->buffer.page.free(&tab->buffer.page))
379 de2a69bb 2021-05-17 op die();
380 de2a69bb 2021-05-17 op } else {
381 de2a69bb 2021-05-17 op l = asprintf(&page, "Wrote %s (%zu bytes)\n",
382 de2a69bb 2021-05-17 op tab->path,
383 de2a69bb 2021-05-17 op tab->bytes);
384 de2a69bb 2021-05-17 op if (l == -1)
385 de2a69bb 2021-05-17 op die();
386 de2a69bb 2021-05-17 op load_page_from_str(tab, page);
387 de2a69bb 2021-05-17 op free(page);
388 a5c3e03d 2021-03-02 op
389 de2a69bb 2021-05-17 op close(tab->fd);
390 de2a69bb 2021-05-17 op tab->fd = -1;
391 de2a69bb 2021-05-17 op free(tab->path);
392 de2a69bb 2021-05-17 op tab->path = NULL;
393 de2a69bb 2021-05-17 op }
394 de2a69bb 2021-05-17 op
395 2ba66cea 2021-03-22 op ui_on_tab_refresh(tab);
396 2ba66cea 2021-03-22 op ui_on_tab_loaded(tab);
397 5e11c00c 2021-03-02 op }
398 5e11c00c 2021-03-02 op
399 5e11c00c 2021-03-02 op static void
400 740f578b 2021-03-15 op handle_imsg_bookmark_ok(struct imsg *imsg, size_t datalen)
401 740f578b 2021-03-15 op {
402 740f578b 2021-03-15 op int res;
403 740f578b 2021-03-15 op
404 740f578b 2021-03-15 op if (datalen != sizeof(res))
405 740f578b 2021-03-15 op die();
406 740f578b 2021-03-15 op
407 740f578b 2021-03-15 op memcpy(&res, imsg->data, sizeof(res));
408 740f578b 2021-03-15 op if (res == 0)
409 740f578b 2021-03-15 op ui_notify("Added to bookmarks!");
410 740f578b 2021-03-15 op else
411 740f578b 2021-03-15 op ui_notify("Failed to add to bookmarks: %s",
412 740f578b 2021-03-15 op strerror(res));
413 740f578b 2021-03-15 op }
414 740f578b 2021-03-15 op
415 740f578b 2021-03-15 op static void
416 3a227e9a 2021-03-18 op handle_imsg_save_cert_ok(struct imsg *imsg, size_t datalen)
417 3a227e9a 2021-03-18 op {
418 3a227e9a 2021-03-18 op int res;
419 3a227e9a 2021-03-18 op
420 3a227e9a 2021-03-18 op if (datalen != sizeof(res))
421 3a227e9a 2021-03-18 op die();
422 3a227e9a 2021-03-18 op memcpy(&res, imsg->data, datalen);
423 3a227e9a 2021-03-18 op if (res != 0)
424 3a227e9a 2021-03-18 op ui_notify("Failed to save the cert for: %s",
425 3a227e9a 2021-03-18 op strerror(res));
426 288fd238 2021-04-25 op }
427 288fd238 2021-04-25 op
428 288fd238 2021-04-25 op static void
429 288fd238 2021-04-25 op handle_imsg_update_cert_ok(struct imsg *imsg, size_t datalen)
430 288fd238 2021-04-25 op {
431 288fd238 2021-04-25 op int res;
432 288fd238 2021-04-25 op
433 288fd238 2021-04-25 op if (datalen != sizeof(res))
434 288fd238 2021-04-25 op die();
435 288fd238 2021-04-25 op memcpy(&res, imsg->data, datalen);
436 288fd238 2021-04-25 op if (!res)
437 288fd238 2021-04-25 op ui_notify("Failed to update the certificate");
438 3a227e9a 2021-03-18 op }
439 3a227e9a 2021-03-18 op
440 3a227e9a 2021-03-18 op static void
441 1304bbdd 2021-03-15 op handle_dispatch_imsg(int fd, short ev, void *d)
442 5e11c00c 2021-03-02 op {
443 35e1f40a 2021-03-14 op struct imsgbuf *ibuf = d;
444 1304bbdd 2021-03-15 op dispatch_imsg(ibuf, handlers, sizeof(handlers));
445 5e11c00c 2021-03-02 op }
446 5e11c00c 2021-03-02 op
447 0972d8b2 2021-03-02 op static void
448 0972d8b2 2021-03-02 op load_page_from_str(struct tab *tab, const char *page)
449 0972d8b2 2021-03-02 op {
450 46f6e974 2021-05-17 op gemtext_initparser(&tab->buffer.page);
451 46f6e974 2021-05-17 op if (!tab->buffer.page.parse(&tab->buffer.page, page, strlen(page)))
452 0972d8b2 2021-03-02 op die();
453 46f6e974 2021-05-17 op if (!tab->buffer.page.free(&tab->buffer.page))
454 0972d8b2 2021-03-02 op die();
455 0972d8b2 2021-03-02 op ui_on_tab_refresh(tab);
456 8af5e5ed 2021-03-08 op ui_on_tab_loaded(tab);
457 0972d8b2 2021-03-02 op }
458 0972d8b2 2021-03-02 op
459 bcb0b073 2021-03-07 op void
460 4d3785b1 2021-03-09 op load_about_url(struct tab *tab, const char *url)
461 0972d8b2 2021-03-02 op {
462 10346511 2021-03-17 op tab->trust = TS_VERIFIED;
463 2051e653 2021-03-13 op
464 46f6e974 2021-05-17 op gemtext_initparser(&tab->buffer.page);
465 35e1f40a 2021-03-14 op
466 35e1f40a 2021-03-14 op imsg_compose(fsibuf, IMSG_GET, tab->id, 0, -1,
467 31f1a758 2021-04-22 op tab->hist_cur->h, strlen(tab->hist_cur->h)+1);
468 35e1f40a 2021-03-14 op imsg_flush(fsibuf);
469 4d3785b1 2021-03-09 op }
470 0972d8b2 2021-03-02 op
471 4d3785b1 2021-03-09 op void
472 4d3785b1 2021-03-09 op load_gemini_url(struct tab *tab, const char *url)
473 4d3785b1 2021-03-09 op {
474 2051e653 2021-03-13 op size_t len;
475 754622a2 2021-03-15 op
476 2eef3403 2021-04-22 op stop_tab(tab);
477 2eef3403 2021-04-22 op tab->id = tab_new_id();
478 2eef3403 2021-04-22 op
479 31f1a758 2021-04-22 op len = sizeof(tab->hist_cur->h);
480 1b8a4bbf 2021-03-12 op imsg_compose(netibuf, IMSG_GET, tab->id, 0, -1,
481 31f1a758 2021-04-22 op tab->hist_cur->h, len);
482 1b8a4bbf 2021-03-12 op imsg_flush(netibuf);
483 3a9b9365 2021-03-09 op return;
484 0972d8b2 2021-03-02 op }
485 0972d8b2 2021-03-02 op
486 2051e653 2021-03-13 op static void
487 2051e653 2021-03-13 op do_load_url(struct tab *tab, const char *url)
488 4d3785b1 2021-03-09 op {
489 31f1a758 2021-04-22 op struct phos_uri uri;
490 31f1a758 2021-04-22 op struct proto *p;
491 31f1a758 2021-04-22 op char *t;
492 de2a69bb 2021-05-17 op
493 de2a69bb 2021-05-17 op if (tab->fd != -1) {
494 de2a69bb 2021-05-17 op close(tab->fd);
495 de2a69bb 2021-05-17 op tab->fd = -1;
496 de2a69bb 2021-05-17 op free(tab->path);
497 de2a69bb 2021-05-17 op tab->path = NULL;
498 de2a69bb 2021-05-17 op }
499 4d3785b1 2021-03-09 op
500 10346511 2021-03-17 op tab->trust = TS_UNKNOWN;
501 10346511 2021-03-17 op
502 31f1a758 2021-04-22 op memcpy(&uri, &tab->uri, sizeof(tab->uri));
503 31f1a758 2021-04-22 op if (!phos_resolve_uri_from_str(&uri, url, &tab->uri)) {
504 31f1a758 2021-04-22 op if (asprintf(&t, "#error loading %s\n>%s\n",
505 31f1a758 2021-04-22 op url, "Can't parse the URI") == -1)
506 31f1a758 2021-04-22 op die();
507 31f1a758 2021-04-22 op strlcpy(tab->hist_cur->h, url, sizeof(tab->hist_cur->h));
508 31f1a758 2021-04-22 op load_page_from_str(tab, t);
509 31f1a758 2021-04-22 op free(t);
510 31f1a758 2021-04-22 op return;
511 31f1a758 2021-04-22 op }
512 31f1a758 2021-04-22 op
513 31f1a758 2021-04-22 op phos_serialize_uri(&tab->uri, tab->hist_cur->h,
514 31f1a758 2021-04-22 op sizeof(tab->hist_cur->h));
515 31f1a758 2021-04-22 op
516 4d3785b1 2021-03-09 op for (p = protos; p->schema != NULL; ++p) {
517 ed383cf4 2021-04-22 op if (!strcmp(tab->uri.scheme, p->schema)) {
518 4d3785b1 2021-03-09 op p->loadfn(tab, url);
519 31f1a758 2021-04-22 op return;
520 4d3785b1 2021-03-09 op }
521 4d3785b1 2021-03-09 op }
522 4d3785b1 2021-03-09 op
523 4d3785b1 2021-03-09 op protos[0].loadfn(tab, url);
524 4d3785b1 2021-03-09 op }
525 4d3785b1 2021-03-09 op
526 9ad4627d 2021-03-10 op void
527 2051e653 2021-03-13 op load_url(struct tab *tab, const char *url)
528 2051e653 2021-03-13 op {
529 2051e653 2021-03-13 op if (tab->hist_cur != NULL)
530 2051e653 2021-03-13 op hist_clear_forward(&tab->hist, TAILQ_NEXT(tab->hist_cur, entries));
531 2051e653 2021-03-13 op
532 2051e653 2021-03-13 op if ((tab->hist_cur = calloc(1, sizeof(*tab->hist_cur))) == NULL) {
533 2051e653 2021-03-13 op event_loopbreak();
534 2051e653 2021-03-13 op return;
535 2051e653 2021-03-13 op }
536 2051e653 2021-03-13 op hist_push(&tab->hist, tab->hist_cur);
537 2051e653 2021-03-13 op do_load_url(tab, url);
538 46f6e974 2021-05-17 op empty_vlist(&tab->buffer);
539 46f6e974 2021-05-17 op empty_linelist(&tab->buffer);
540 2051e653 2021-03-13 op }
541 2051e653 2021-03-13 op
542 2051e653 2021-03-13 op int
543 2051e653 2021-03-13 op load_previous_page(struct tab *tab)
544 2051e653 2021-03-13 op {
545 2051e653 2021-03-13 op struct hist *h;
546 2051e653 2021-03-13 op
547 2051e653 2021-03-13 op if ((h = TAILQ_PREV(tab->hist_cur, mhisthead, entries)) == NULL)
548 2051e653 2021-03-13 op return 0;
549 2051e653 2021-03-13 op tab->hist_cur = h;
550 2051e653 2021-03-13 op do_load_url(tab, h->h);
551 2051e653 2021-03-13 op return 1;
552 2051e653 2021-03-13 op }
553 2051e653 2021-03-13 op
554 2051e653 2021-03-13 op int
555 2051e653 2021-03-13 op load_next_page(struct tab *tab)
556 2051e653 2021-03-13 op {
557 2051e653 2021-03-13 op struct hist *h;
558 2051e653 2021-03-13 op
559 2051e653 2021-03-13 op if ((h = TAILQ_NEXT(tab->hist_cur, entries)) == NULL)
560 2051e653 2021-03-13 op return 0;
561 2051e653 2021-03-13 op tab->hist_cur = h;
562 2051e653 2021-03-13 op do_load_url(tab, h->h);
563 2051e653 2021-03-13 op return 1;
564 2051e653 2021-03-13 op }
565 2051e653 2021-03-13 op
566 2051e653 2021-03-13 op void
567 9ad4627d 2021-03-10 op stop_tab(struct tab *tab)
568 9ad4627d 2021-03-10 op {
569 1b8a4bbf 2021-03-12 op imsg_compose(netibuf, IMSG_STOP, tab->id, 0, -1, NULL, 0);
570 1b8a4bbf 2021-03-12 op imsg_flush(netibuf);
571 de2a69bb 2021-05-17 op
572 de2a69bb 2021-05-17 op if (tab->fd != -1) {
573 de2a69bb 2021-05-17 op close(tab->fd);
574 de2a69bb 2021-05-17 op tab->fd = -1;
575 de2a69bb 2021-05-17 op free(tab->path);
576 de2a69bb 2021-05-17 op tab->path = NULL;
577 de2a69bb 2021-05-17 op load_page_from_str(tab, "Stopped.\n");
578 de2a69bb 2021-05-17 op }
579 9ad4627d 2021-03-10 op }
580 9ad4627d 2021-03-10 op
581 740f578b 2021-03-15 op void
582 740f578b 2021-03-15 op add_to_bookmarks(const char *str)
583 740f578b 2021-03-15 op {
584 740f578b 2021-03-15 op imsg_compose(fsibuf, IMSG_BOOKMARK_PAGE, 0, 0, -1, str, strlen(str)+1);
585 c7107cec 2021-04-01 op imsg_flush(fsibuf);
586 c7107cec 2021-04-01 op }
587 c7107cec 2021-04-01 op
588 c7107cec 2021-04-01 op void
589 c7107cec 2021-04-01 op save_session(void)
590 c7107cec 2021-04-01 op {
591 c7107cec 2021-04-01 op struct tab *tab;
592 c7107cec 2021-04-01 op
593 c7107cec 2021-04-01 op imsg_compose(fsibuf, IMSG_SESSION_START, 0, 0, -1, NULL, 0);
594 c7107cec 2021-04-01 op imsg_flush(fsibuf);
595 c7107cec 2021-04-01 op
596 c7107cec 2021-04-01 op TAILQ_FOREACH(tab, &tabshead, tabs) {
597 c7107cec 2021-04-01 op imsg_compose(fsibuf, IMSG_SESSION_TAB, 0, 0, -1,
598 c7107cec 2021-04-01 op tab->hist_cur->h, strlen(tab->hist_cur->h)+1);
599 c7107cec 2021-04-01 op imsg_flush(fsibuf);
600 c7107cec 2021-04-01 op }
601 c7107cec 2021-04-01 op
602 c7107cec 2021-04-01 op imsg_compose(fsibuf, IMSG_SESSION_END, 0, 0, -1, NULL, 0);
603 740f578b 2021-03-15 op imsg_flush(fsibuf);
604 740f578b 2021-03-15 op }
605 740f578b 2021-03-15 op
606 5e11c00c 2021-03-02 op int
607 6cd6a9e1 2021-03-20 op main(int argc, char * const *argv)
608 5e11c00c 2021-03-02 op {
609 35e1f40a 2021-03-14 op struct imsgbuf network_ibuf, fs_ibuf;
610 35e1f40a 2021-03-14 op int net_fds[2], fs_fds[2];
611 5e11c00c 2021-03-02 op
612 5e11c00c 2021-03-02 op signal(SIGCHLD, SIG_IGN);
613 a0e91173 2021-06-13 op signal(SIGPIPE, SIG_IGN);
614 5e11c00c 2021-03-02 op
615 3a227e9a 2021-03-18 op /* initialize part of the fs layer. Before starting the UI
616 3a227e9a 2021-03-18 op * and dropping the priviledges we need to read some stuff. */
617 3a227e9a 2021-03-18 op fs_init();
618 3a227e9a 2021-03-18 op
619 35e1f40a 2021-03-14 op if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, fs_fds) == -1)
620 5e11c00c 2021-03-02 op err(1, "socketpair");
621 5e11c00c 2021-03-02 op
622 5e11c00c 2021-03-02 op switch (fork()) {
623 5e11c00c 2021-03-02 op case -1:
624 5e11c00c 2021-03-02 op err(1, "fork");
625 5e11c00c 2021-03-02 op case 0:
626 5e11c00c 2021-03-02 op /* child */
627 37d429b0 2021-04-01 op setproctitle("fs");
628 35e1f40a 2021-03-14 op close(fs_fds[0]);
629 35e1f40a 2021-03-14 op imsg_init(&fs_ibuf, fs_fds[1]);
630 35e1f40a 2021-03-14 op exit(fs_main(&fs_ibuf));
631 35e1f40a 2021-03-14 op default:
632 35e1f40a 2021-03-14 op close(fs_fds[1]);
633 35e1f40a 2021-03-14 op imsg_init(&fs_ibuf, fs_fds[0]);
634 35e1f40a 2021-03-14 op fsibuf = &fs_ibuf;
635 5e11c00c 2021-03-02 op }
636 b1d4d01b 2021-03-14 op
637 35e1f40a 2021-03-14 op if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, net_fds) == -1)
638 35e1f40a 2021-03-14 op err(1, "socketpair");
639 5e11c00c 2021-03-02 op
640 35e1f40a 2021-03-14 op switch (fork()) {
641 35e1f40a 2021-03-14 op case -1:
642 35e1f40a 2021-03-14 op err(1, "fork");
643 35e1f40a 2021-03-14 op case 0:
644 35e1f40a 2021-03-14 op /* child */
645 37d429b0 2021-04-01 op setproctitle("client");
646 35e1f40a 2021-03-14 op close(net_fds[0]);
647 35e1f40a 2021-03-14 op close(fs_fds[0]);
648 35e1f40a 2021-03-14 op imsg_init(&network_ibuf, net_fds[1]);
649 35e1f40a 2021-03-14 op exit(client_main(&network_ibuf));
650 35e1f40a 2021-03-14 op default:
651 35e1f40a 2021-03-14 op close(net_fds[1]);
652 35e1f40a 2021-03-14 op imsg_init(&network_ibuf, net_fds[0]);
653 35e1f40a 2021-03-14 op netibuf = &network_ibuf;
654 35e1f40a 2021-03-14 op }
655 5e11c00c 2021-03-02 op
656 37d429b0 2021-04-01 op setproctitle("ui");
657 35e1f40a 2021-03-14 op
658 3768e50f 2021-04-25 op tofu_init(&certs, 5, offsetof(struct tofu_entry, domain));
659 3a227e9a 2021-03-18 op load_certs(&certs);
660 cbcc75fb 2021-03-17 op
661 5e11c00c 2021-03-02 op TAILQ_INIT(&tabshead);
662 5e11c00c 2021-03-02 op
663 5e11c00c 2021-03-02 op event_init();
664 5e11c00c 2021-03-02 op
665 4ad01575 2021-06-13 op event_set(&netev, netibuf->fd, EV_READ | EV_PERSIST,
666 4ad01575 2021-06-13 op handle_dispatch_imsg, netibuf);
667 35e1f40a 2021-03-14 op event_add(&netev, NULL);
668 5e11c00c 2021-03-02 op
669 4ad01575 2021-06-13 op event_set(&fsev, fsibuf->fd, EV_READ | EV_PERSIST,
670 4ad01575 2021-06-13 op handle_dispatch_imsg, fsibuf);
671 35e1f40a 2021-03-14 op event_add(&fsev, NULL);
672 35e1f40a 2021-03-14 op
673 941b3761 2021-03-18 op if (ui_init(argc, argv)) {
674 941b3761 2021-03-18 op sandbox_ui_process();
675 941b3761 2021-03-18 op event_dispatch();
676 941b3761 2021-03-18 op ui_end();
677 941b3761 2021-03-18 op }
678 5e11c00c 2021-03-02 op
679 1b8a4bbf 2021-03-12 op imsg_compose(netibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
680 1b8a4bbf 2021-03-12 op imsg_flush(netibuf);
681 5e11c00c 2021-03-02 op
682 35e1f40a 2021-03-14 op imsg_compose(fsibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
683 35e1f40a 2021-03-14 op imsg_flush(fsibuf);
684 35e1f40a 2021-03-14 op
685 5e11c00c 2021-03-02 op return 0;
686 5e11c00c 2021-03-02 op }