Blame


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