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 0e1eff5b 2021-06-23 op char *page, buf[FMT_SCALED_STRSIZE] = {0};
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 0e1eff5b 2021-06-23 op fmt_scaled(tab->bytes, buf);
357 0e1eff5b 2021-06-23 op l = asprintf(&page, "Saving to \"%s\"... (%s)\n",
358 de2a69bb 2021-05-17 op tab->path,
359 0e1eff5b 2021-06-23 op buf);
360 de2a69bb 2021-05-17 op if (l == -1)
361 de2a69bb 2021-05-17 op die();
362 de2a69bb 2021-05-17 op load_page_from_str(tab, page);
363 de2a69bb 2021-05-17 op free(page);
364 de2a69bb 2021-05-17 op }
365 5e11c00c 2021-03-02 op
366 0972d8b2 2021-03-02 op ui_on_tab_refresh(tab);
367 5e11c00c 2021-03-02 op }
368 5e11c00c 2021-03-02 op
369 5e11c00c 2021-03-02 op static void
370 5e11c00c 2021-03-02 op handle_imsg_eof(struct imsg *imsg, size_t datalen)
371 5e11c00c 2021-03-02 op {
372 2ba66cea 2021-03-22 op struct tab *tab;
373 de2a69bb 2021-05-17 op int l;
374 0e1eff5b 2021-06-23 op char *page, buf[FMT_SCALED_STRSIZE] = {0};
375 a5c3e03d 2021-03-02 op
376 2ba66cea 2021-03-22 op tab = tab_by_id(imsg->hdr.peerid);
377 de2a69bb 2021-05-17 op
378 de2a69bb 2021-05-17 op if (tab->fd == -1) {
379 46f6e974 2021-05-17 op if (!tab->buffer.page.free(&tab->buffer.page))
380 de2a69bb 2021-05-17 op die();
381 de2a69bb 2021-05-17 op } else {
382 0e1eff5b 2021-06-23 op fmt_scaled(tab->bytes, buf);
383 0e1eff5b 2021-06-23 op l = asprintf(&page, "Saved to \"%s\" (%s)\n",
384 de2a69bb 2021-05-17 op tab->path,
385 0e1eff5b 2021-06-23 op buf);
386 de2a69bb 2021-05-17 op if (l == -1)
387 de2a69bb 2021-05-17 op die();
388 de2a69bb 2021-05-17 op load_page_from_str(tab, page);
389 de2a69bb 2021-05-17 op free(page);
390 a5c3e03d 2021-03-02 op
391 de2a69bb 2021-05-17 op close(tab->fd);
392 de2a69bb 2021-05-17 op tab->fd = -1;
393 de2a69bb 2021-05-17 op free(tab->path);
394 de2a69bb 2021-05-17 op tab->path = NULL;
395 de2a69bb 2021-05-17 op }
396 de2a69bb 2021-05-17 op
397 2ba66cea 2021-03-22 op ui_on_tab_refresh(tab);
398 2ba66cea 2021-03-22 op ui_on_tab_loaded(tab);
399 5e11c00c 2021-03-02 op }
400 5e11c00c 2021-03-02 op
401 5e11c00c 2021-03-02 op static void
402 740f578b 2021-03-15 op handle_imsg_bookmark_ok(struct imsg *imsg, size_t datalen)
403 740f578b 2021-03-15 op {
404 740f578b 2021-03-15 op int res;
405 740f578b 2021-03-15 op
406 740f578b 2021-03-15 op if (datalen != sizeof(res))
407 740f578b 2021-03-15 op die();
408 740f578b 2021-03-15 op
409 740f578b 2021-03-15 op memcpy(&res, imsg->data, sizeof(res));
410 740f578b 2021-03-15 op if (res == 0)
411 7f963c41 2021-06-20 op message("Added to bookmarks!");
412 740f578b 2021-03-15 op else
413 7f963c41 2021-06-20 op message("Failed to add to bookmarks: %s",
414 740f578b 2021-03-15 op strerror(res));
415 740f578b 2021-03-15 op }
416 740f578b 2021-03-15 op
417 740f578b 2021-03-15 op static void
418 3a227e9a 2021-03-18 op handle_imsg_save_cert_ok(struct imsg *imsg, size_t datalen)
419 3a227e9a 2021-03-18 op {
420 3a227e9a 2021-03-18 op int res;
421 3a227e9a 2021-03-18 op
422 3a227e9a 2021-03-18 op if (datalen != sizeof(res))
423 3a227e9a 2021-03-18 op die();
424 3a227e9a 2021-03-18 op memcpy(&res, imsg->data, datalen);
425 3a227e9a 2021-03-18 op if (res != 0)
426 7f963c41 2021-06-20 op message("Failed to save the cert for: %s",
427 3a227e9a 2021-03-18 op strerror(res));
428 288fd238 2021-04-25 op }
429 288fd238 2021-04-25 op
430 288fd238 2021-04-25 op static void
431 288fd238 2021-04-25 op handle_imsg_update_cert_ok(struct imsg *imsg, size_t datalen)
432 288fd238 2021-04-25 op {
433 288fd238 2021-04-25 op int res;
434 288fd238 2021-04-25 op
435 288fd238 2021-04-25 op if (datalen != sizeof(res))
436 288fd238 2021-04-25 op die();
437 288fd238 2021-04-25 op memcpy(&res, imsg->data, datalen);
438 288fd238 2021-04-25 op if (!res)
439 7f963c41 2021-06-20 op message("Failed to update the certificate");
440 3a227e9a 2021-03-18 op }
441 3a227e9a 2021-03-18 op
442 3a227e9a 2021-03-18 op static void
443 1304bbdd 2021-03-15 op handle_dispatch_imsg(int fd, short ev, void *d)
444 5e11c00c 2021-03-02 op {
445 35e1f40a 2021-03-14 op struct imsgbuf *ibuf = d;
446 1304bbdd 2021-03-15 op dispatch_imsg(ibuf, handlers, sizeof(handlers));
447 5e11c00c 2021-03-02 op }
448 5e11c00c 2021-03-02 op
449 0972d8b2 2021-03-02 op static void
450 0972d8b2 2021-03-02 op load_page_from_str(struct tab *tab, const char *page)
451 0972d8b2 2021-03-02 op {
452 46f6e974 2021-05-17 op gemtext_initparser(&tab->buffer.page);
453 46f6e974 2021-05-17 op if (!tab->buffer.page.parse(&tab->buffer.page, page, strlen(page)))
454 0972d8b2 2021-03-02 op die();
455 46f6e974 2021-05-17 op if (!tab->buffer.page.free(&tab->buffer.page))
456 0972d8b2 2021-03-02 op die();
457 0972d8b2 2021-03-02 op ui_on_tab_refresh(tab);
458 8af5e5ed 2021-03-08 op ui_on_tab_loaded(tab);
459 0972d8b2 2021-03-02 op }
460 0972d8b2 2021-03-02 op
461 bcb0b073 2021-03-07 op void
462 4d3785b1 2021-03-09 op load_about_url(struct tab *tab, const char *url)
463 0972d8b2 2021-03-02 op {
464 10346511 2021-03-17 op tab->trust = TS_VERIFIED;
465 2051e653 2021-03-13 op
466 46f6e974 2021-05-17 op gemtext_initparser(&tab->buffer.page);
467 35e1f40a 2021-03-14 op
468 35e1f40a 2021-03-14 op imsg_compose(fsibuf, IMSG_GET, tab->id, 0, -1,
469 31f1a758 2021-04-22 op tab->hist_cur->h, strlen(tab->hist_cur->h)+1);
470 35e1f40a 2021-03-14 op imsg_flush(fsibuf);
471 4d3785b1 2021-03-09 op }
472 0972d8b2 2021-03-02 op
473 4d3785b1 2021-03-09 op void
474 4d3785b1 2021-03-09 op load_gemini_url(struct tab *tab, const char *url)
475 4d3785b1 2021-03-09 op {
476 2051e653 2021-03-13 op size_t len;
477 754622a2 2021-03-15 op
478 2eef3403 2021-04-22 op stop_tab(tab);
479 2eef3403 2021-04-22 op tab->id = tab_new_id();
480 2eef3403 2021-04-22 op
481 31f1a758 2021-04-22 op len = sizeof(tab->hist_cur->h);
482 1b8a4bbf 2021-03-12 op imsg_compose(netibuf, IMSG_GET, tab->id, 0, -1,
483 31f1a758 2021-04-22 op tab->hist_cur->h, len);
484 1b8a4bbf 2021-03-12 op imsg_flush(netibuf);
485 3a9b9365 2021-03-09 op return;
486 0972d8b2 2021-03-02 op }
487 0972d8b2 2021-03-02 op
488 2051e653 2021-03-13 op static void
489 2051e653 2021-03-13 op do_load_url(struct tab *tab, const char *url)
490 4d3785b1 2021-03-09 op {
491 31f1a758 2021-04-22 op struct phos_uri uri;
492 31f1a758 2021-04-22 op struct proto *p;
493 31f1a758 2021-04-22 op char *t;
494 de2a69bb 2021-05-17 op
495 de2a69bb 2021-05-17 op if (tab->fd != -1) {
496 de2a69bb 2021-05-17 op close(tab->fd);
497 de2a69bb 2021-05-17 op tab->fd = -1;
498 de2a69bb 2021-05-17 op free(tab->path);
499 de2a69bb 2021-05-17 op tab->path = NULL;
500 de2a69bb 2021-05-17 op }
501 4d3785b1 2021-03-09 op
502 10346511 2021-03-17 op tab->trust = TS_UNKNOWN;
503 10346511 2021-03-17 op
504 31f1a758 2021-04-22 op memcpy(&uri, &tab->uri, sizeof(tab->uri));
505 31f1a758 2021-04-22 op if (!phos_resolve_uri_from_str(&uri, url, &tab->uri)) {
506 31f1a758 2021-04-22 op if (asprintf(&t, "#error loading %s\n>%s\n",
507 31f1a758 2021-04-22 op url, "Can't parse the URI") == -1)
508 31f1a758 2021-04-22 op die();
509 31f1a758 2021-04-22 op strlcpy(tab->hist_cur->h, url, sizeof(tab->hist_cur->h));
510 31f1a758 2021-04-22 op load_page_from_str(tab, t);
511 31f1a758 2021-04-22 op free(t);
512 31f1a758 2021-04-22 op return;
513 31f1a758 2021-04-22 op }
514 31f1a758 2021-04-22 op
515 31f1a758 2021-04-22 op phos_serialize_uri(&tab->uri, tab->hist_cur->h,
516 31f1a758 2021-04-22 op sizeof(tab->hist_cur->h));
517 31f1a758 2021-04-22 op
518 4d3785b1 2021-03-09 op for (p = protos; p->schema != NULL; ++p) {
519 ed383cf4 2021-04-22 op if (!strcmp(tab->uri.scheme, p->schema)) {
520 4d3785b1 2021-03-09 op p->loadfn(tab, url);
521 31f1a758 2021-04-22 op return;
522 4d3785b1 2021-03-09 op }
523 4d3785b1 2021-03-09 op }
524 4d3785b1 2021-03-09 op
525 4d3785b1 2021-03-09 op protos[0].loadfn(tab, url);
526 4d3785b1 2021-03-09 op }
527 4d3785b1 2021-03-09 op
528 9ad4627d 2021-03-10 op void
529 2051e653 2021-03-13 op load_url(struct tab *tab, const char *url)
530 2051e653 2021-03-13 op {
531 2051e653 2021-03-13 op if (tab->hist_cur != NULL)
532 2051e653 2021-03-13 op hist_clear_forward(&tab->hist, TAILQ_NEXT(tab->hist_cur, entries));
533 2051e653 2021-03-13 op
534 2051e653 2021-03-13 op if ((tab->hist_cur = calloc(1, sizeof(*tab->hist_cur))) == NULL) {
535 2051e653 2021-03-13 op event_loopbreak();
536 2051e653 2021-03-13 op return;
537 2051e653 2021-03-13 op }
538 2051e653 2021-03-13 op hist_push(&tab->hist, tab->hist_cur);
539 2051e653 2021-03-13 op do_load_url(tab, url);
540 46f6e974 2021-05-17 op empty_vlist(&tab->buffer);
541 46f6e974 2021-05-17 op empty_linelist(&tab->buffer);
542 2051e653 2021-03-13 op }
543 2051e653 2021-03-13 op
544 2051e653 2021-03-13 op int
545 2051e653 2021-03-13 op load_previous_page(struct tab *tab)
546 2051e653 2021-03-13 op {
547 2051e653 2021-03-13 op struct hist *h;
548 2051e653 2021-03-13 op
549 2051e653 2021-03-13 op if ((h = TAILQ_PREV(tab->hist_cur, mhisthead, entries)) == NULL)
550 2051e653 2021-03-13 op return 0;
551 2051e653 2021-03-13 op tab->hist_cur = h;
552 2051e653 2021-03-13 op do_load_url(tab, h->h);
553 2051e653 2021-03-13 op return 1;
554 2051e653 2021-03-13 op }
555 2051e653 2021-03-13 op
556 2051e653 2021-03-13 op int
557 2051e653 2021-03-13 op load_next_page(struct tab *tab)
558 2051e653 2021-03-13 op {
559 2051e653 2021-03-13 op struct hist *h;
560 2051e653 2021-03-13 op
561 2051e653 2021-03-13 op if ((h = TAILQ_NEXT(tab->hist_cur, entries)) == NULL)
562 2051e653 2021-03-13 op return 0;
563 2051e653 2021-03-13 op tab->hist_cur = h;
564 2051e653 2021-03-13 op do_load_url(tab, h->h);
565 2051e653 2021-03-13 op return 1;
566 2051e653 2021-03-13 op }
567 2051e653 2021-03-13 op
568 2051e653 2021-03-13 op void
569 9ad4627d 2021-03-10 op stop_tab(struct tab *tab)
570 9ad4627d 2021-03-10 op {
571 1b8a4bbf 2021-03-12 op imsg_compose(netibuf, IMSG_STOP, tab->id, 0, -1, NULL, 0);
572 1b8a4bbf 2021-03-12 op imsg_flush(netibuf);
573 de2a69bb 2021-05-17 op
574 de2a69bb 2021-05-17 op if (tab->fd != -1) {
575 de2a69bb 2021-05-17 op close(tab->fd);
576 de2a69bb 2021-05-17 op tab->fd = -1;
577 de2a69bb 2021-05-17 op free(tab->path);
578 de2a69bb 2021-05-17 op tab->path = NULL;
579 de2a69bb 2021-05-17 op load_page_from_str(tab, "Stopped.\n");
580 de2a69bb 2021-05-17 op }
581 9ad4627d 2021-03-10 op }
582 9ad4627d 2021-03-10 op
583 740f578b 2021-03-15 op void
584 740f578b 2021-03-15 op add_to_bookmarks(const char *str)
585 740f578b 2021-03-15 op {
586 740f578b 2021-03-15 op imsg_compose(fsibuf, IMSG_BOOKMARK_PAGE, 0, 0, -1, str, strlen(str)+1);
587 c7107cec 2021-04-01 op imsg_flush(fsibuf);
588 c7107cec 2021-04-01 op }
589 c7107cec 2021-04-01 op
590 c7107cec 2021-04-01 op void
591 c7107cec 2021-04-01 op save_session(void)
592 c7107cec 2021-04-01 op {
593 c7107cec 2021-04-01 op struct tab *tab;
594 c7107cec 2021-04-01 op
595 c7107cec 2021-04-01 op imsg_compose(fsibuf, IMSG_SESSION_START, 0, 0, -1, NULL, 0);
596 c7107cec 2021-04-01 op imsg_flush(fsibuf);
597 c7107cec 2021-04-01 op
598 c7107cec 2021-04-01 op TAILQ_FOREACH(tab, &tabshead, tabs) {
599 c7107cec 2021-04-01 op imsg_compose(fsibuf, IMSG_SESSION_TAB, 0, 0, -1,
600 c7107cec 2021-04-01 op tab->hist_cur->h, strlen(tab->hist_cur->h)+1);
601 c7107cec 2021-04-01 op imsg_flush(fsibuf);
602 c7107cec 2021-04-01 op }
603 c7107cec 2021-04-01 op
604 c7107cec 2021-04-01 op imsg_compose(fsibuf, IMSG_SESSION_END, 0, 0, -1, NULL, 0);
605 740f578b 2021-03-15 op imsg_flush(fsibuf);
606 740f578b 2021-03-15 op }
607 740f578b 2021-03-15 op
608 5e11c00c 2021-03-02 op int
609 6cd6a9e1 2021-03-20 op main(int argc, char * const *argv)
610 5e11c00c 2021-03-02 op {
611 35e1f40a 2021-03-14 op struct imsgbuf network_ibuf, fs_ibuf;
612 35e1f40a 2021-03-14 op int net_fds[2], fs_fds[2];
613 5e11c00c 2021-03-02 op
614 5e11c00c 2021-03-02 op signal(SIGCHLD, SIG_IGN);
615 a0e91173 2021-06-13 op signal(SIGPIPE, SIG_IGN);
616 5e11c00c 2021-03-02 op
617 3a227e9a 2021-03-18 op /* initialize part of the fs layer. Before starting the UI
618 3a227e9a 2021-03-18 op * and dropping the priviledges we need to read some stuff. */
619 3a227e9a 2021-03-18 op fs_init();
620 3a227e9a 2021-03-18 op
621 35e1f40a 2021-03-14 op if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, fs_fds) == -1)
622 5e11c00c 2021-03-02 op err(1, "socketpair");
623 5e11c00c 2021-03-02 op
624 5e11c00c 2021-03-02 op switch (fork()) {
625 5e11c00c 2021-03-02 op case -1:
626 5e11c00c 2021-03-02 op err(1, "fork");
627 5e11c00c 2021-03-02 op case 0:
628 5e11c00c 2021-03-02 op /* child */
629 37d429b0 2021-04-01 op setproctitle("fs");
630 35e1f40a 2021-03-14 op close(fs_fds[0]);
631 35e1f40a 2021-03-14 op imsg_init(&fs_ibuf, fs_fds[1]);
632 35e1f40a 2021-03-14 op exit(fs_main(&fs_ibuf));
633 35e1f40a 2021-03-14 op default:
634 35e1f40a 2021-03-14 op close(fs_fds[1]);
635 35e1f40a 2021-03-14 op imsg_init(&fs_ibuf, fs_fds[0]);
636 35e1f40a 2021-03-14 op fsibuf = &fs_ibuf;
637 5e11c00c 2021-03-02 op }
638 b1d4d01b 2021-03-14 op
639 35e1f40a 2021-03-14 op if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, net_fds) == -1)
640 35e1f40a 2021-03-14 op err(1, "socketpair");
641 5e11c00c 2021-03-02 op
642 35e1f40a 2021-03-14 op switch (fork()) {
643 35e1f40a 2021-03-14 op case -1:
644 35e1f40a 2021-03-14 op err(1, "fork");
645 35e1f40a 2021-03-14 op case 0:
646 35e1f40a 2021-03-14 op /* child */
647 37d429b0 2021-04-01 op setproctitle("client");
648 35e1f40a 2021-03-14 op close(net_fds[0]);
649 35e1f40a 2021-03-14 op close(fs_fds[0]);
650 35e1f40a 2021-03-14 op imsg_init(&network_ibuf, net_fds[1]);
651 35e1f40a 2021-03-14 op exit(client_main(&network_ibuf));
652 35e1f40a 2021-03-14 op default:
653 35e1f40a 2021-03-14 op close(net_fds[1]);
654 35e1f40a 2021-03-14 op imsg_init(&network_ibuf, net_fds[0]);
655 35e1f40a 2021-03-14 op netibuf = &network_ibuf;
656 35e1f40a 2021-03-14 op }
657 5e11c00c 2021-03-02 op
658 37d429b0 2021-04-01 op setproctitle("ui");
659 35e1f40a 2021-03-14 op
660 3768e50f 2021-04-25 op tofu_init(&certs, 5, offsetof(struct tofu_entry, domain));
661 3a227e9a 2021-03-18 op load_certs(&certs);
662 cbcc75fb 2021-03-17 op
663 5e11c00c 2021-03-02 op TAILQ_INIT(&tabshead);
664 5e11c00c 2021-03-02 op
665 5e11c00c 2021-03-02 op event_init();
666 5e11c00c 2021-03-02 op
667 4ad01575 2021-06-13 op event_set(&netev, netibuf->fd, EV_READ | EV_PERSIST,
668 4ad01575 2021-06-13 op handle_dispatch_imsg, netibuf);
669 35e1f40a 2021-03-14 op event_add(&netev, NULL);
670 5e11c00c 2021-03-02 op
671 4ad01575 2021-06-13 op event_set(&fsev, fsibuf->fd, EV_READ | EV_PERSIST,
672 4ad01575 2021-06-13 op handle_dispatch_imsg, fsibuf);
673 35e1f40a 2021-03-14 op event_add(&fsev, NULL);
674 35e1f40a 2021-03-14 op
675 941b3761 2021-03-18 op if (ui_init(argc, argv)) {
676 941b3761 2021-03-18 op sandbox_ui_process();
677 941b3761 2021-03-18 op event_dispatch();
678 941b3761 2021-03-18 op ui_end();
679 941b3761 2021-03-18 op }
680 5e11c00c 2021-03-02 op
681 1b8a4bbf 2021-03-12 op imsg_compose(netibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
682 1b8a4bbf 2021-03-12 op imsg_flush(netibuf);
683 5e11c00c 2021-03-02 op
684 35e1f40a 2021-03-14 op imsg_compose(fsibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
685 35e1f40a 2021-03-14 op imsg_flush(fsibuf);
686 35e1f40a 2021-03-14 op
687 5e11c00c 2021-03-02 op return 0;
688 5e11c00c 2021-03-02 op }