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