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