Blame


1 4540067e 2021-07-18 op /*
2 44008d59 2024-01-16 op * Copyright (c) 2021, 2024 Omar Polo <op@omarpolo.com>
3 4540067e 2021-07-18 op *
4 4540067e 2021-07-18 op * Permission to use, copy, modify, and distribute this software for any
5 4540067e 2021-07-18 op * purpose with or without fee is hereby granted, provided that the above
6 4540067e 2021-07-18 op * copyright notice and this permission notice appear in all copies.
7 4540067e 2021-07-18 op *
8 4540067e 2021-07-18 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 4540067e 2021-07-18 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 4540067e 2021-07-18 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 4540067e 2021-07-18 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 4540067e 2021-07-18 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 4540067e 2021-07-18 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 4540067e 2021-07-18 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 4540067e 2021-07-18 op */
16 1f496e7b 2021-07-21 op
17 1f496e7b 2021-07-21 op #include "compat.h"
18 4540067e 2021-07-18 op
19 5e11c00c 2021-03-02 op #include <sys/socket.h>
20 98d3e6c1 2024-02-18 op #include <sys/time.h>
21 4cf6ba13 2022-02-11 op #include <sys/un.h>
22 be97d6e6 2021-08-15 op #include <sys/wait.h>
23 5e11c00c 2021-03-02 op
24 be97d6e6 2021-08-15 op #include <errno.h>
25 f63b8f73 2022-04-24 op #include <fcntl.h>
26 dc761924 2021-07-15 op #include <getopt.h>
27 f63b8f73 2022-04-24 op #include <limits.h>
28 5e11c00c 2021-03-02 op #include <signal.h>
29 5e11c00c 2021-03-02 op #include <stdio.h>
30 5e11c00c 2021-03-02 op #include <stdlib.h>
31 5e11c00c 2021-03-02 op #include <string.h>
32 5e11c00c 2021-03-02 op #include <unistd.h>
33 5e11c00c 2021-03-02 op
34 d35e18b3 2024-02-04 op #include "certs.h"
35 d252d87c 2024-02-05 op #include "cmd.h"
36 4cf6ba13 2022-02-11 op #include "control.h"
37 e659558c 2021-07-13 op #include "defaults.h"
38 98d3e6c1 2024-02-18 op #include "ev.h"
39 eb2ed626 2021-10-07 op #include "fs.h"
40 65c49665 2024-01-23 op #include "hist.h"
41 87d297d1 2024-02-22 op #include "imsgev.h"
42 44008d59 2024-01-16 op #include "iri.h"
43 f7db6d13 2024-02-06 op #include "keymap.h"
44 d5af38cc 2022-01-10 op #include "mcache.h"
45 4bc446b9 2021-07-21 op #include "minibuffer.h"
46 395b9f4e 2021-07-12 op #include "parser.h"
47 1fce2e75 2021-08-14 op #include "session.h"
48 395b9f4e 2021-07-12 op #include "telescope.h"
49 d163c210 2024-02-06 op #include "tofu.h"
50 d1a0f2a3 2021-07-12 op #include "ui.h"
51 9d65b1d9 2022-01-11 op #include "utils.h"
52 dc761924 2021-07-15 op
53 0d877486 2022-02-24 op static const struct option longopts[] = {
54 dc761924 2021-07-15 op {"help", no_argument, NULL, 'h'},
55 d0971653 2021-09-15 op {"safe", no_argument, NULL, 'S'},
56 dc761924 2021-07-15 op {"version", no_argument, NULL, 'v'},
57 dc761924 2021-07-15 op {NULL, 0, NULL, 0},
58 dc761924 2021-07-15 op };
59 dc761924 2021-07-15 op
60 d08fccd2 2024-01-23 op static const char *opts = "c:hnST:v";
61 a37d1a1c 2021-08-14 op
62 bb28f1c2 2021-12-30 op static int has_url;
63 bb28f1c2 2021-12-30 op static char url[GEMINI_URL_LEN];
64 bb28f1c2 2021-12-30 op
65 a37d1a1c 2021-08-14 op /*
66 a37d1a1c 2021-08-14 op * Used to know when we're finished loading.
67 a37d1a1c 2021-08-14 op */
68 1fce2e75 2021-08-14 op int operating;
69 395b9f4e 2021-07-12 op
70 d0971653 2021-09-15 op /*
71 d0971653 2021-09-15 op * "Safe" (or "sandobox") mode. If enabled, Telescope shouldn't write
72 d0971653 2021-09-15 op * anything to the filesystem or execute external programs.
73 d0971653 2021-09-15 op */
74 d0971653 2021-09-15 op int safe_mode;
75 d0971653 2021-09-15 op
76 f63b8f73 2022-04-24 op static struct imsgev *iev_net;
77 ecfbb1d4 2021-08-14 op
78 f6a429eb 2021-07-10 op struct tabshead tabshead = TAILQ_HEAD_INITIALIZER(tabshead);
79 6c74799d 2022-01-05 op struct tabshead ktabshead = TAILQ_HEAD_INITIALIZER(ktabshead);
80 f6a429eb 2021-07-10 op struct proxylist proxies = TAILQ_HEAD_INITIALIZER(proxies);
81 6cc5fcfe 2021-07-08 op
82 6cc5fcfe 2021-07-08 op enum telescope_process {
83 6cc5fcfe 2021-07-08 op PROC_UI,
84 6cc5fcfe 2021-07-08 op PROC_NET,
85 6cc5fcfe 2021-07-08 op };
86 5e11c00c 2021-03-02 op
87 9a8a7402 2021-07-18 op #define CANNOT_FETCH 0
88 ef88dc16 2024-02-02 op #define TOO_MANY_REDIRECTS 1
89 9a8a7402 2021-07-18 op #define MALFORMED_RESPONSE 2
90 9a8a7402 2021-07-18 op #define UNKNOWN_TYPE_OR_CSET 3
91 9a8a7402 2021-07-18 op #define UNKNOWN_PROTOCOL 4
92 9a8a7402 2021-07-18 op
93 a21e0756 2021-08-14 op /* XXX: keep in sync with normalize_code */
94 9a8a7402 2021-07-18 op const char *err_pages[70] = {
95 9a8a7402 2021-07-18 op [CANNOT_FETCH] = "# Couldn't load the page\n",
96 ef88dc16 2024-02-02 op [TOO_MANY_REDIRECTS] = "# Too many redirects\n",
97 9a8a7402 2021-07-18 op [MALFORMED_RESPONSE] = "# Got a malformed response\n",
98 5014b6a9 2024-02-02 op [UNKNOWN_TYPE_OR_CSET] = "# Unsupported document format or charset\n",
99 9a8a7402 2021-07-18 op [UNKNOWN_PROTOCOL] = "# Unknown protocol\n",
100 9a8a7402 2021-07-18 op
101 9a8a7402 2021-07-18 op [10] = "# Input required\n",
102 9a8a7402 2021-07-18 op [11] = "# Input required\n",
103 9a8a7402 2021-07-18 op [40] = "# Temporary failure\n",
104 9a8a7402 2021-07-18 op [41] = "# Server unavailable\n",
105 9a8a7402 2021-07-18 op [42] = "# CGI error\n",
106 9a8a7402 2021-07-18 op [43] = "# Proxy error\n",
107 9a8a7402 2021-07-18 op [44] = "# Slow down\n",
108 9a8a7402 2021-07-18 op [50] = "# Permanent failure\n",
109 9a8a7402 2021-07-18 op [51] = "# Not found\n",
110 9a8a7402 2021-07-18 op [52] = "# Gone\n",
111 9a8a7402 2021-07-18 op [53] = "# Proxy request refused\n",
112 9a8a7402 2021-07-18 op [59] = "# Bad request\n",
113 9a8a7402 2021-07-18 op [60] = "# Client certificate required\n",
114 9a8a7402 2021-07-18 op [61] = "# Certificate not authorised\n",
115 9a8a7402 2021-07-18 op [62] = "# Certificate not valid\n"
116 9a8a7402 2021-07-18 op };
117 9a8a7402 2021-07-18 op
118 2051e653 2021-03-13 op static void die(void) __attribute__((__noreturn__));
119 2051e653 2021-03-13 op static struct tab *tab_by_id(uint32_t);
120 b148be71 2024-01-31 op static void handle_imsg_check_cert(struct imsg *);
121 a2fd3805 2021-07-06 op static void handle_check_cert_user_choice(int, struct tab *);
122 a2fd3805 2021-07-06 op static void handle_maybe_save_new_cert(int, struct tab *);
123 a2fd3805 2021-07-06 op static void handle_maybe_save_page(int, struct tab *);
124 d1353324 2021-07-13 op static void handle_save_page_path(const char *, struct tab *);
125 98d3e6c1 2024-02-18 op static void handle_dispatch_imsg(int, int, void *);
126 cd1d043d 2024-01-13 op static void load_about_url(struct tab *, const char *);
127 cd1d043d 2024-01-13 op static void load_file_url(struct tab *, const char *);
128 cd1d043d 2024-01-13 op static void load_finger_url(struct tab *, const char *);
129 cd1d043d 2024-01-13 op static void load_gemini_url(struct tab *, const char *);
130 cd1d043d 2024-01-13 op static void load_gopher_url(struct tab *, const char *);
131 cd1d043d 2024-01-13 op static void load_via_proxy(struct tab *, const char *,
132 cd1d043d 2024-01-13 op struct proxy *);
133 cd1d043d 2024-01-13 op static void make_request(struct tab *, struct get_req *, int,
134 71bc1636 2024-02-06 op const char *);
135 cd1d043d 2024-01-13 op static void do_load_url(struct tab *, const char *, const char *, int);
136 6cc5fcfe 2021-07-08 op static pid_t start_child(enum telescope_process, const char *, int);
137 4cf6ba13 2022-02-11 op static void send_url(const char *);
138 dfb5eb39 2021-07-25 op
139 0d877486 2022-02-24 op static const struct proto {
140 352e0355 2021-07-25 op const char *schema;
141 ba782cb8 2021-07-25 op const char *port;
142 cd1d043d 2024-01-13 op void (*loadfn)(struct tab *, const char *);
143 352e0355 2021-07-25 op } protos[] = {
144 ba782cb8 2021-07-25 op {"about", NULL, load_about_url},
145 0b1053d2 2021-08-13 op {"file", NULL, load_file_url},
146 ba782cb8 2021-07-25 op {"finger", "79", load_finger_url},
147 ba782cb8 2021-07-25 op {"gemini", "1965", load_gemini_url},
148 1663bf1e 2021-07-25 op {"gopher", "70", load_gopher_url},
149 ba782cb8 2021-07-25 op {NULL, NULL, NULL},
150 dfb5eb39 2021-07-25 op };
151 5e11c00c 2021-03-02 op
152 cbcc75fb 2021-03-17 op static struct ohash certs;
153 cbcc75fb 2021-03-17 op
154 5e11c00c 2021-03-02 op static void __attribute__((__noreturn__))
155 5e11c00c 2021-03-02 op die(void)
156 5e11c00c 2021-03-02 op {
157 5e11c00c 2021-03-02 op abort(); /* TODO */
158 5e11c00c 2021-03-02 op }
159 5e11c00c 2021-03-02 op
160 5e11c00c 2021-03-02 op static struct tab *
161 f6243012 2021-07-19 op tab_by_id(uint32_t id)
162 5e11c00c 2021-03-02 op {
163 5e11c00c 2021-03-02 op struct tab *t;
164 5e11c00c 2021-03-02 op
165 5e11c00c 2021-03-02 op TAILQ_FOREACH(t, &tabshead, tabs) {
166 5e11c00c 2021-03-02 op if (t->id == id)
167 5e11c00c 2021-03-02 op return t;
168 5e11c00c 2021-03-02 op }
169 5e11c00c 2021-03-02 op
170 5d6c2088 2021-07-19 op return NULL;
171 5e11c00c 2021-03-02 op }
172 5e11c00c 2021-03-02 op
173 5e11c00c 2021-03-02 op static void
174 b148be71 2024-01-31 op handle_imsg_check_cert(struct imsg *imsg)
175 5e11c00c 2021-03-02 op {
176 62cbcdae 2024-02-02 op char *hash;
177 62cbcdae 2024-02-02 op const char *host, *port;
178 cbcc75fb 2021-03-17 op int tofu_res;
179 62cbcdae 2024-02-02 op struct ibuf ibuf;
180 cbcc75fb 2021-03-17 op struct tofu_entry *e;
181 cbcc75fb 2021-03-17 op struct tab *tab;
182 b148be71 2024-01-31 op size_t datalen;
183 5e11c00c 2021-03-02 op
184 62cbcdae 2024-02-02 op if (imsg_get_ibuf(imsg, &ibuf) == -1 ||
185 62cbcdae 2024-02-02 op ibuf_borrow_str(&ibuf, &hash) == -1)
186 cbcc75fb 2021-03-17 op abort();
187 b148be71 2024-01-31 op datalen = strlen(hash);
188 cbcc75fb 2021-03-17 op
189 b148be71 2024-01-31 op if ((tab = tab_by_id(imsg_get_id(imsg))) == NULL)
190 f6243012 2021-07-19 op return;
191 cbcc75fb 2021-03-17 op
192 984245ce 2021-06-23 op if (tab->proxy != NULL) {
193 984245ce 2021-06-23 op host = tab->proxy->host;
194 984245ce 2021-06-23 op port = tab->proxy->port;
195 984245ce 2021-06-23 op } else {
196 25905047 2024-01-16 op host = tab->iri.iri_host;
197 25905047 2024-01-16 op port = tab->iri.iri_portstr;
198 984245ce 2021-06-23 op }
199 984245ce 2021-06-23 op
200 984245ce 2021-06-23 op if ((e = tofu_lookup(&certs, host, port)) == NULL) {
201 cbcc75fb 2021-03-17 op /* TODO: an update in libressl/libretls changed
202 cbcc75fb 2021-03-17 op * significantly. Find a better approach at storing
203 cbcc75fb 2021-03-17 op * the certs! */
204 cbcc75fb 2021-03-17 op if (datalen > sizeof(e->hash))
205 cbcc75fb 2021-03-17 op abort();
206 cbcc75fb 2021-03-17 op
207 cbcc75fb 2021-03-17 op tofu_res = 1; /* trust on first use */
208 cbcc75fb 2021-03-17 op if ((e = calloc(1, sizeof(*e))) == NULL)
209 cbcc75fb 2021-03-17 op abort();
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 eb4388ee 2021-04-25 op strlcat(e->domain, ":", sizeof(e->domain));
213 984245ce 2021-06-23 op strlcat(e->domain, port, sizeof(e->domain));
214 eb4388ee 2021-04-25 op }
215 cbcc75fb 2021-03-17 op strlcpy(e->hash, hash, sizeof(e->hash));
216 4fa88f57 2022-04-24 op tofu_save(&certs, e);
217 cbcc75fb 2021-03-17 op } else
218 cbcc75fb 2021-03-17 op tofu_res = !strcmp(hash, e->hash);
219 cbcc75fb 2021-03-17 op
220 5d1bac73 2021-03-25 op if (tofu_res) {
221 a2fd3805 2021-07-06 op if (e->verified == -1)
222 a2fd3805 2021-07-06 op tab->trust = TS_TEMP_TRUSTED;
223 a2fd3805 2021-07-06 op else if (e->verified == 1)
224 a2fd3805 2021-07-06 op tab->trust = TS_VERIFIED;
225 a2fd3805 2021-07-06 op else
226 a2fd3805 2021-07-06 op tab->trust = TS_TRUSTED;
227 a2fd3805 2021-07-06 op
228 d35e18b3 2024-02-04 op ui_send_net(IMSG_CERT_STATUS, imsg->hdr.peerid, -1,
229 5d1bac73 2021-03-25 op &tofu_res, sizeof(tofu_res));
230 5d1bac73 2021-03-25 op } else {
231 cbcc75fb 2021-03-17 op tab->trust = TS_UNTRUSTED;
232 cbcc75fb 2021-03-17 op load_page_from_str(tab, "# Certificate mismatch\n");
233 288fd238 2021-04-25 op if ((tab->cert = strdup(hash)) == NULL)
234 288fd238 2021-04-25 op die();
235 5d1bac73 2021-03-25 op ui_yornp("Certificate mismatch. Proceed?",
236 a2fd3805 2021-07-06 op handle_check_cert_user_choice, tab);
237 cbcc75fb 2021-03-17 op }
238 5d1bac73 2021-03-25 op }
239 5d1bac73 2021-03-25 op
240 5d1bac73 2021-03-25 op static void
241 a2fd3805 2021-07-06 op handle_check_cert_user_choice(int accept, struct tab *tab)
242 5d1bac73 2021-03-25 op {
243 d35e18b3 2024-02-04 op ui_send_net(IMSG_CERT_STATUS, tab->id, -1, &accept,
244 bc10f6a5 2021-07-12 op sizeof(accept));
245 288fd238 2021-04-25 op
246 a2fd3805 2021-07-06 op if (accept) {
247 25905047 2024-01-16 op const char *host, *port;
248 25905047 2024-01-16 op
249 25905047 2024-01-16 op host = tab->iri.iri_host;
250 25905047 2024-01-16 op port = tab->iri.iri_portstr;
251 25905047 2024-01-16 op
252 a2fd3805 2021-07-06 op /*
253 a2fd3805 2021-07-06 op * trust the certificate for this session only. If
254 a2fd3805 2021-07-06 op * the page results in a redirect while we're asking
255 a2fd3805 2021-07-06 op * the user to save, we'll end up with an invalid
256 7b4e4feb 2021-08-15 op * tabid (one request == one tab id). It also makes
257 7b4e4feb 2021-08-15 op * sense to save it for the current session if the
258 7b4e4feb 2021-08-15 op * user accepted it.
259 a2fd3805 2021-07-06 op */
260 25905047 2024-01-16 op tofu_temp_trust(&certs, host, port, tab->cert);
261 a2fd3805 2021-07-06 op
262 d0971653 2021-09-15 op if (!safe_mode)
263 d0971653 2021-09-15 op ui_yornp("Save the new certificate?",
264 d0971653 2021-09-15 op handle_maybe_save_new_cert, tab);
265 d0971653 2021-09-15 op else
266 d0971653 2021-09-15 op message("Certificate temporarly trusted");
267 a2fd3805 2021-07-06 op } else {
268 288fd238 2021-04-25 op free(tab->cert);
269 288fd238 2021-04-25 op tab->cert = NULL;
270 288fd238 2021-04-25 op }
271 5e11c00c 2021-03-02 op }
272 5e11c00c 2021-03-02 op
273 288fd238 2021-04-25 op static void
274 a2fd3805 2021-07-06 op handle_maybe_save_new_cert(int accept, struct tab *tab)
275 288fd238 2021-04-25 op {
276 288fd238 2021-04-25 op struct tofu_entry *e;
277 984245ce 2021-06-23 op const char *host, *port;
278 288fd238 2021-04-25 op
279 984245ce 2021-06-23 op if (tab->proxy != NULL) {
280 984245ce 2021-06-23 op host = tab->proxy->host;
281 984245ce 2021-06-23 op port = tab->proxy->port;
282 984245ce 2021-06-23 op } else {
283 25905047 2024-01-16 op host = tab->iri.iri_host;
284 25905047 2024-01-16 op port = tab->iri.iri_portstr;
285 984245ce 2021-06-23 op }
286 984245ce 2021-06-23 op
287 288fd238 2021-04-25 op if (!accept)
288 288fd238 2021-04-25 op goto end;
289 288fd238 2021-04-25 op
290 fe2262ad 2021-05-12 op if ((e = calloc(1, sizeof(*e))) == NULL)
291 288fd238 2021-04-25 op die();
292 288fd238 2021-04-25 op
293 984245ce 2021-06-23 op strlcpy(e->domain, host, sizeof(e->domain));
294 984245ce 2021-06-23 op if (*port != '\0' && strcmp(port, "1965")) {
295 288fd238 2021-04-25 op strlcat(e->domain, ":", sizeof(e->domain));
296 984245ce 2021-06-23 op strlcat(e->domain, port, sizeof(e->domain));
297 288fd238 2021-04-25 op }
298 288fd238 2021-04-25 op strlcpy(e->hash, tab->cert, sizeof(e->hash));
299 288fd238 2021-04-25 op
300 3078e1bc 2022-04-24 op tofu_update_persist(&certs, e);
301 288fd238 2021-04-25 op
302 288fd238 2021-04-25 op tab->trust = TS_TRUSTED;
303 288fd238 2021-04-25 op
304 288fd238 2021-04-25 op end:
305 288fd238 2021-04-25 op free(tab->cert);
306 288fd238 2021-04-25 op tab->cert = NULL;
307 288fd238 2021-04-25 op }
308 288fd238 2021-04-25 op
309 5cd2ebb1 2021-03-11 op static inline int
310 5cd2ebb1 2021-03-11 op normalize_code(int n)
311 5cd2ebb1 2021-03-11 op {
312 5cd2ebb1 2021-03-11 op if (n < 20) {
313 5cd2ebb1 2021-03-11 op if (n == 10 || n == 11)
314 5cd2ebb1 2021-03-11 op return n;
315 5cd2ebb1 2021-03-11 op return 10;
316 5cd2ebb1 2021-03-11 op } else if (n < 30) {
317 5cd2ebb1 2021-03-11 op return 20;
318 5cd2ebb1 2021-03-11 op } else if (n < 40) {
319 5cd2ebb1 2021-03-11 op if (n == 30 || n == 31)
320 5cd2ebb1 2021-03-11 op return n;
321 5cd2ebb1 2021-03-11 op return 30;
322 5cd2ebb1 2021-03-11 op } else if (n < 50) {
323 5cd2ebb1 2021-03-11 op if (n <= 44)
324 5cd2ebb1 2021-03-11 op return n;
325 5cd2ebb1 2021-03-11 op return 40;
326 5cd2ebb1 2021-03-11 op } else if (n < 60) {
327 5cd2ebb1 2021-03-11 op if (n <= 53 || n == 59)
328 5cd2ebb1 2021-03-11 op return n;
329 5cd2ebb1 2021-03-11 op return 50;
330 5cd2ebb1 2021-03-11 op } else if (n < 70) {
331 5cd2ebb1 2021-03-11 op if (n <= 62)
332 5cd2ebb1 2021-03-11 op return n;
333 5cd2ebb1 2021-03-11 op return 60;
334 5cd2ebb1 2021-03-11 op } else
335 5cd2ebb1 2021-03-11 op return MALFORMED_RESPONSE;
336 5cd2ebb1 2021-03-11 op }
337 5cd2ebb1 2021-03-11 op
338 5e11c00c 2021-03-02 op static void
339 b148be71 2024-01-31 op handle_request_response(struct tab *tab)
340 5e11c00c 2021-03-02 op {
341 fcd99a0d 2021-11-05 op char buf[128];
342 54764e41 2024-02-02 op
343 54764e41 2024-02-02 op if (tab->code != 30 && tab->code != 31)
344 54764e41 2024-02-02 op tab->redirect_count = 0;
345 0972d8b2 2021-03-02 op
346 5cd2ebb1 2021-03-11 op if (tab->code < 10) { /* internal errors */
347 5cd2ebb1 2021-03-11 op load_page_from_str(tab, err_pages[tab->code]);
348 5cd2ebb1 2021-03-11 op } else if (tab->code < 20) { /* 1x */
349 ed504b9e 2022-02-07 op free(tab->last_input_url);
350 65c49665 2024-01-23 op tab->last_input_url = strdup(hist_cur(tab->hist));
351 ed504b9e 2022-02-07 op if (tab->last_input_url == NULL)
352 ed504b9e 2022-02-07 op die();
353 ed504b9e 2022-02-07 op
354 5cd2ebb1 2021-03-11 op load_page_from_str(tab, err_pages[tab->code]);
355 693188cb 2022-02-07 op ui_require_input(tab, tab->code == 11, ir_select_gemini);
356 5cd2ebb1 2021-03-11 op } else if (tab->code == 20) {
357 65c49665 2024-01-23 op history_add(hist_cur(tab->hist));
358 c07ac996 2021-03-12 op if (setup_parser_for(tab)) {
359 d35e18b3 2024-02-04 op ui_send_net(IMSG_PROCEED, tab->id, -1, NULL, 0);
360 fcd99a0d 2021-11-05 op } else if (safe_mode) {
361 69f32f56 2021-08-16 op load_page_from_str(tab,
362 69f32f56 2021-08-16 op err_pages[UNKNOWN_TYPE_OR_CSET]);
363 fcd99a0d 2021-11-05 op } else {
364 65c49665 2024-01-23 op hist_prev(tab->hist);
365 fcd99a0d 2021-11-05 op snprintf(buf, sizeof(buf),
366 fcd99a0d 2021-11-05 op "Can't display \"%s\", save it?", tab->meta);
367 fcd99a0d 2021-11-05 op ui_yornp(buf, handle_maybe_save_page, tab);
368 c07ac996 2021-03-12 op }
369 5cd2ebb1 2021-03-11 op } else if (tab->code < 40) { /* 3x */
370 3a9b9365 2021-03-09 op tab->redirect_count++;
371 0972d8b2 2021-03-02 op
372 3a9b9365 2021-03-09 op /* TODO: make customizable? */
373 3a9b9365 2021-03-09 op if (tab->redirect_count > 5) {
374 3a9b9365 2021-03-09 op load_page_from_str(tab,
375 ef88dc16 2024-02-02 op err_pages[TOO_MANY_REDIRECTS]);
376 5cd2ebb1 2021-03-11 op } else
377 65c49665 2024-01-23 op do_load_url(tab, tab->meta, hist_cur(tab->hist),
378 33133ab1 2024-01-16 op LU_MODE_NOCACHE);
379 5cd2ebb1 2021-03-11 op } else { /* 4x, 5x & 6x */
380 5cd2ebb1 2021-03-11 op load_page_from_str(tab, err_pages[tab->code]);
381 5a39f593 2024-02-05 op if (tab->code >= 60)
382 5a39f593 2024-02-05 op cmd_use_certificate(&tab->buffer);
383 3a9b9365 2021-03-09 op }
384 de2a69bb 2021-05-17 op }
385 de2a69bb 2021-05-17 op
386 de2a69bb 2021-05-17 op static void
387 a2fd3805 2021-07-06 op handle_maybe_save_page(int dosave, struct tab *tab)
388 de2a69bb 2021-05-17 op {
389 bb9912eb 2021-08-29 op const char *f;
390 bb9912eb 2021-08-29 op char input[PATH_MAX];
391 bb9912eb 2021-08-29 op
392 fcd99a0d 2021-11-05 op /* XXX: this print a message that is confusing */
393 fcd99a0d 2021-11-05 op ui_on_tab_loaded(tab);
394 fcd99a0d 2021-11-05 op
395 bb9912eb 2021-08-29 op if (!dosave) {
396 a2fd3805 2021-07-06 op stop_tab(tab);
397 bb9912eb 2021-08-29 op return;
398 bb9912eb 2021-08-29 op }
399 bb9912eb 2021-08-29 op
400 25905047 2024-01-16 op if ((f = strrchr(tab->iri.iri_path, '/')) == NULL)
401 bb9912eb 2021-08-29 op f = "";
402 bb9912eb 2021-08-29 op else
403 bb9912eb 2021-08-29 op f++;
404 bb9912eb 2021-08-29 op
405 112b9046 2021-08-29 op strlcpy(input, download_path, sizeof(input));
406 bb9912eb 2021-08-29 op strlcat(input, f, sizeof(input));
407 bb9912eb 2021-08-29 op
408 bb9912eb 2021-08-29 op ui_read("Save to path", handle_save_page_path, tab, input);
409 de2a69bb 2021-05-17 op }
410 de2a69bb 2021-05-17 op
411 de2a69bb 2021-05-17 op static void
412 d1353324 2021-07-13 op handle_save_page_path(const char *path, struct tab *tab)
413 d1353324 2021-07-13 op {
414 f63b8f73 2022-04-24 op struct download *d;
415 f63b8f73 2022-04-24 op int fd;
416 f63b8f73 2022-04-24 op
417 de2a69bb 2021-05-17 op if (path == NULL) {
418 d1353324 2021-07-13 op stop_tab(tab);
419 de2a69bb 2021-05-17 op return;
420 de2a69bb 2021-05-17 op }
421 de2a69bb 2021-05-17 op
422 ff48dbfd 2022-05-05 op if ((fd = open(path, O_WRONLY|O_TRUNC|O_CREAT, 0644)) == -1) {
423 ff48dbfd 2022-05-05 op message("Can't open file %s: %s", path, strerror(errno));
424 ff48dbfd 2022-05-05 op stop_tab(tab);
425 ff48dbfd 2022-05-05 op return;
426 ff48dbfd 2022-05-05 op }
427 868b3a8f 2022-04-13 op
428 ff48dbfd 2022-05-05 op ui_show_downloads_pane();
429 fd984e76 2022-05-05 op d = enqueue_download(tab->id, path);
430 ff48dbfd 2022-05-05 op d->fd = fd;
431 d35e18b3 2024-02-04 op ui_send_net(IMSG_PROCEED, d->id, -1, NULL, 0);
432 89e46508 2022-05-05 op
433 89e46508 2022-05-05 op /*
434 89e46508 2022-05-05 op * Change this tab id, the old one is associated with the
435 89e46508 2022-05-05 op * download now.
436 89e46508 2022-05-05 op */
437 89e46508 2022-05-05 op tab->id = tab_new_id();
438 de2a69bb 2021-05-17 op }
439 de2a69bb 2021-05-17 op
440 de2a69bb 2021-05-17 op static void
441 98d3e6c1 2024-02-18 op handle_dispatch_imsg(int fd, int event, void *data)
442 5e11c00c 2021-03-02 op {
443 b148be71 2024-01-31 op struct imsgev *iev = data;
444 b148be71 2024-01-31 op struct imsgbuf *imsgbuf = &iev->ibuf;
445 b148be71 2024-01-31 op struct imsg imsg;
446 54764e41 2024-02-02 op struct ibuf ibuf;
447 b148be71 2024-01-31 op struct tab *tab;
448 b148be71 2024-01-31 op struct download *d;
449 54764e41 2024-02-02 op const char *h;
450 54764e41 2024-02-02 op char *str, *page;
451 b148be71 2024-01-31 op ssize_t n;
452 54764e41 2024-02-02 op int code;
453 5e11c00c 2021-03-02 op
454 b148be71 2024-01-31 op if (event & EV_READ) {
455 b148be71 2024-01-31 op if ((n = imsg_read(imsgbuf)) == -1 && errno != EAGAIN)
456 b148be71 2024-01-31 op err(1, "imsg_read");
457 b148be71 2024-01-31 op if (n == 0)
458 b148be71 2024-01-31 op err(1, "connection closed");
459 de2a69bb 2021-05-17 op }
460 b148be71 2024-01-31 op if (event & EV_WRITE) {
461 b148be71 2024-01-31 op if ((n = msgbuf_write(&imsgbuf->w)) == -1 && errno != EAGAIN)
462 b148be71 2024-01-31 op err(1, "msgbuf_write");
463 b148be71 2024-01-31 op if (n == 0)
464 b148be71 2024-01-31 op err(1, "connection closed");
465 b148be71 2024-01-31 op }
466 5e11c00c 2021-03-02 op
467 b148be71 2024-01-31 op for (;;) {
468 b148be71 2024-01-31 op if ((n = imsg_get(imsgbuf, &imsg)) == -1)
469 b148be71 2024-01-31 op err(1, "imsg_get");
470 b148be71 2024-01-31 op if (n == 0)
471 b148be71 2024-01-31 op break;
472 a5c3e03d 2021-03-02 op
473 b148be71 2024-01-31 op switch (imsg_get_type(&imsg)) {
474 b148be71 2024-01-31 op case IMSG_ERR:
475 b148be71 2024-01-31 op if ((tab = tab_by_id(imsg_get_id(&imsg))) == NULL)
476 b148be71 2024-01-31 op break;
477 62cbcdae 2024-02-02 op if (imsg_get_ibuf(&imsg, &ibuf) == -1 ||
478 62cbcdae 2024-02-02 op ibuf_borrow_str(&ibuf, &str) == -1)
479 b148be71 2024-01-31 op die();
480 b148be71 2024-01-31 op if (asprintf(&page, "# Error loading %s\n\n> %s\n",
481 b148be71 2024-01-31 op hist_cur(tab->hist), str) == -1)
482 b148be71 2024-01-31 op die();
483 b148be71 2024-01-31 op load_page_from_str(tab, page);
484 b148be71 2024-01-31 op free(page);
485 b148be71 2024-01-31 op break;
486 b148be71 2024-01-31 op case IMSG_CHECK_CERT:
487 b148be71 2024-01-31 op handle_imsg_check_cert(&imsg);
488 b148be71 2024-01-31 op break;
489 54764e41 2024-02-02 op case IMSG_REPLY:
490 b148be71 2024-01-31 op if ((tab = tab_by_id(imsg_get_id(&imsg))) == NULL)
491 b148be71 2024-01-31 op break;
492 54764e41 2024-02-02 op if (imsg_get_ibuf(&imsg, &ibuf) == -1 ||
493 54764e41 2024-02-02 op ibuf_get(&ibuf, &code, sizeof(code)) == -1 ||
494 54764e41 2024-02-02 op ibuf_borrow_str(&ibuf, &str) == -1)
495 b148be71 2024-01-31 op die();
496 b148be71 2024-01-31 op if (strlcpy(tab->meta, str, sizeof(tab->meta)) >=
497 b148be71 2024-01-31 op sizeof(tab->meta))
498 b148be71 2024-01-31 op die();
499 54764e41 2024-02-02 op tab->code = normalize_code(code);
500 b148be71 2024-01-31 op handle_request_response(tab);
501 b148be71 2024-01-31 op break;
502 b148be71 2024-01-31 op case IMSG_BUF:
503 b148be71 2024-01-31 op if ((tab = tab_by_id(imsg_get_id(&imsg))) == NULL &&
504 b148be71 2024-01-31 op ((d = download_by_id(imsg_get_id(&imsg)))) == NULL)
505 b148be71 2024-01-31 op return;
506 de2a69bb 2021-05-17 op
507 b148be71 2024-01-31 op if (tab) {
508 b148be71 2024-01-31 op if (!parser_parse(tab, imsg.data,
509 b148be71 2024-01-31 op imsg_get_len(&imsg)))
510 b148be71 2024-01-31 op die();
511 b148be71 2024-01-31 op ui_on_tab_refresh(tab);
512 b148be71 2024-01-31 op } else {
513 b148be71 2024-01-31 op size_t datalen = imsg_get_len(&imsg);
514 b10ba331 2024-01-23 op
515 b148be71 2024-01-31 op d->bytes += datalen;
516 b148be71 2024-01-31 op write(d->fd, imsg.data, datalen);
517 b148be71 2024-01-31 op ui_on_download_refresh();
518 b148be71 2024-01-31 op }
519 b148be71 2024-01-31 op break;
520 b148be71 2024-01-31 op case IMSG_EOF:
521 b148be71 2024-01-31 op if ((tab = tab_by_id(imsg_get_id(&imsg))) == NULL &&
522 b148be71 2024-01-31 op ((d = download_by_id(imsg_get_id(&imsg)))) == NULL)
523 b148be71 2024-01-31 op return;
524 b10ba331 2024-01-23 op
525 b148be71 2024-01-31 op if (tab != NULL) {
526 b148be71 2024-01-31 op if (!parser_free(tab))
527 b148be71 2024-01-31 op die();
528 b148be71 2024-01-31 op h = hist_cur(tab->hist);
529 b148be71 2024-01-31 op if (!strncmp(h, "gemini://", 9) ||
530 b148be71 2024-01-31 op !strncmp(h, "gopher://", 9) ||
531 b148be71 2024-01-31 op !strncmp(h, "finger://", 9))
532 b148be71 2024-01-31 op mcache_tab(tab);
533 ea080950 2021-07-20 op
534 b148be71 2024-01-31 op /*
535 b148be71 2024-01-31 op * Gemini is handled as soon as a 2x
536 b148be71 2024-01-31 op * reply is got.
537 b148be71 2024-01-31 op */
538 b148be71 2024-01-31 op if (!strncmp(h, "finger://", 9) ||
539 b148be71 2024-01-31 op !strncmp(h, "gopher://", 9))
540 b148be71 2024-01-31 op history_add(h);
541 b148be71 2024-01-31 op
542 b148be71 2024-01-31 op ui_on_tab_refresh(tab);
543 b148be71 2024-01-31 op ui_on_tab_loaded(tab);
544 b148be71 2024-01-31 op } else {
545 b148be71 2024-01-31 op close(d->fd);
546 b148be71 2024-01-31 op d->fd = -1;
547 b148be71 2024-01-31 op ui_on_download_refresh();
548 b148be71 2024-01-31 op }
549 b148be71 2024-01-31 op break;
550 b148be71 2024-01-31 op default:
551 b148be71 2024-01-31 op errx(1, "got unknown imsg %d", imsg_get_type(&imsg));
552 b148be71 2024-01-31 op }
553 b148be71 2024-01-31 op
554 b148be71 2024-01-31 op imsg_free(&imsg);
555 b148be71 2024-01-31 op }
556 b148be71 2024-01-31 op
557 b148be71 2024-01-31 op imsg_event_add(iev);
558 0972d8b2 2021-03-02 op }
559 0972d8b2 2021-03-02 op
560 cd1d043d 2024-01-13 op static void
561 4d3785b1 2021-03-09 op load_about_url(struct tab *tab, const char *url)
562 0972d8b2 2021-03-02 op {
563 f63b8f73 2022-04-24 op tab->trust = TS_TRUSTED;
564 f63b8f73 2022-04-24 op fs_load_url(tab, url);
565 f63b8f73 2022-04-24 op ui_on_tab_refresh(tab);
566 f63b8f73 2022-04-24 op ui_on_tab_loaded(tab);
567 4d3785b1 2021-03-09 op }
568 0972d8b2 2021-03-02 op
569 cd1d043d 2024-01-13 op static void
570 0b1053d2 2021-08-13 op load_file_url(struct tab *tab, const char *url)
571 0b1053d2 2021-08-13 op {
572 f63b8f73 2022-04-24 op tab->trust = TS_TRUSTED;
573 f63b8f73 2022-04-24 op fs_load_url(tab, url);
574 f63b8f73 2022-04-24 op ui_on_tab_refresh(tab);
575 f63b8f73 2022-04-24 op ui_on_tab_loaded(tab);
576 0b1053d2 2021-08-13 op }
577 0b1053d2 2021-08-13 op
578 cd1d043d 2024-01-13 op static void
579 5fb52fc0 2021-07-25 op load_finger_url(struct tab *tab, const char *url)
580 5fb52fc0 2021-07-25 op {
581 5fb52fc0 2021-07-25 op struct get_req req;
582 25905047 2024-01-16 op const char *path;
583 5fb52fc0 2021-07-25 op
584 5fb52fc0 2021-07-25 op memset(&req, 0, sizeof(req));
585 25905047 2024-01-16 op strlcpy(req.host, tab->iri.iri_host, sizeof(req.host));
586 25905047 2024-01-16 op strlcpy(req.port, tab->iri.iri_portstr, sizeof(req.port));
587 5fb52fc0 2021-07-25 op
588 5fb52fc0 2021-07-25 op /*
589 5fb52fc0 2021-07-25 op * Sometimes the finger url have the user as path component
590 5fb52fc0 2021-07-25 op * (e.g. finger://thelambdalab.xyz/plugd), sometimes as
591 5fb52fc0 2021-07-25 op * userinfo (e.g. finger://cobradile@finger.farm).
592 5fb52fc0 2021-07-25 op */
593 25905047 2024-01-16 op if (tab->iri.iri_flags & IH_UINFO) {
594 25905047 2024-01-16 op strlcpy(req.req, tab->iri.iri_uinfo, sizeof(req.req));
595 5fb52fc0 2021-07-25 op } else {
596 25905047 2024-01-16 op path = tab->iri.iri_path;
597 25905047 2024-01-16 op while (*path == '/')
598 25905047 2024-01-16 op ++path;
599 25905047 2024-01-16 op strlcpy(req.req, path, sizeof(req.req));
600 5fb52fc0 2021-07-25 op }
601 5fb52fc0 2021-07-25 op strlcat(req.req, "\r\n", sizeof(req.req));
602 5fb52fc0 2021-07-25 op
603 8c1b0837 2021-07-25 op parser_init(tab, textplain_initparser);
604 71bc1636 2024-02-06 op make_request(tab, &req, PROTO_FINGER, NULL);
605 5fb52fc0 2021-07-25 op }
606 5fb52fc0 2021-07-25 op
607 cd1d043d 2024-01-13 op static void
608 4d3785b1 2021-03-09 op load_gemini_url(struct tab *tab, const char *url)
609 4d3785b1 2021-03-09 op {
610 984245ce 2021-06-23 op struct get_req req;
611 d35e18b3 2024-02-04 op
612 984245ce 2021-06-23 op memset(&req, 0, sizeof(req));
613 25905047 2024-01-16 op strlcpy(req.host, tab->iri.iri_host, sizeof(req.host));
614 25905047 2024-01-16 op strlcpy(req.port, tab->iri.iri_portstr, sizeof(req.port));
615 984245ce 2021-06-23 op
616 71bc1636 2024-02-06 op make_request(tab, &req, PROTO_GEMINI, hist_cur(tab->hist));
617 ca190c99 2021-08-14 op }
618 ca190c99 2021-08-14 op
619 ca190c99 2021-08-14 op static inline const char *
620 ca190c99 2021-08-14 op gopher_skip_selector(const char *path, int *ret_type)
621 ca190c99 2021-08-14 op {
622 ca190c99 2021-08-14 op *ret_type = 0;
623 ca190c99 2021-08-14 op
624 ca190c99 2021-08-14 op if (!strcmp(path, "/") || *path == '\0') {
625 ca190c99 2021-08-14 op *ret_type = '1';
626 ca190c99 2021-08-14 op return path;
627 ca190c99 2021-08-14 op }
628 ca190c99 2021-08-14 op
629 ca190c99 2021-08-14 op if (*path != '/')
630 ca190c99 2021-08-14 op return path;
631 ca190c99 2021-08-14 op path++;
632 ca190c99 2021-08-14 op
633 ca190c99 2021-08-14 op switch (*ret_type = *path) {
634 ca190c99 2021-08-14 op case '0':
635 ca190c99 2021-08-14 op case '1':
636 ca190c99 2021-08-14 op case '7':
637 ca190c99 2021-08-14 op break;
638 ca190c99 2021-08-14 op
639 ca190c99 2021-08-14 op default:
640 ca190c99 2021-08-14 op *ret_type = 0;
641 ca190c99 2021-08-14 op path -= 1;
642 ca190c99 2021-08-14 op return path;
643 ca190c99 2021-08-14 op }
644 ca190c99 2021-08-14 op
645 95bb01e2 2021-08-28 op return ++path;
646 1663bf1e 2021-07-25 op }
647 1663bf1e 2021-07-25 op
648 cd1d043d 2024-01-13 op static void
649 1663bf1e 2021-07-25 op load_gopher_url(struct tab *tab, const char *url)
650 1663bf1e 2021-07-25 op {
651 81839fee 2021-07-25 op struct get_req req;
652 ca190c99 2021-08-14 op int type;
653 81839fee 2021-07-25 op const char *path;
654 1663bf1e 2021-07-25 op
655 1663bf1e 2021-07-25 op memset(&req, 0, sizeof(req));
656 25905047 2024-01-16 op strlcpy(req.host, tab->iri.iri_host, sizeof(req.host));
657 25905047 2024-01-16 op strlcpy(req.port, tab->iri.iri_portstr, sizeof(req.port));
658 1663bf1e 2021-07-25 op
659 25905047 2024-01-16 op path = gopher_skip_selector(tab->iri.iri_path, &type);
660 ca190c99 2021-08-14 op switch (type) {
661 ca190c99 2021-08-14 op case '0':
662 8c1b0837 2021-07-25 op parser_init(tab, textplain_initparser);
663 ca190c99 2021-08-14 op break;
664 ca190c99 2021-08-14 op case '1':
665 ca190c99 2021-08-14 op parser_init(tab, gophermap_initparser);
666 ca190c99 2021-08-14 op break;
667 ca190c99 2021-08-14 op case '7':
668 acf9defe 2022-02-08 op free(tab->last_input_url);
669 acf9defe 2022-02-08 op tab->last_input_url = strdup(url);
670 acf9defe 2022-02-08 op if (tab->last_input_url == NULL)
671 acf9defe 2022-02-08 op die();
672 693188cb 2022-02-07 op ui_require_input(tab, 0, ir_select_gopher);
673 cd1d043d 2024-01-13 op load_page_from_str(tab, err_pages[10]);
674 cd1d043d 2024-01-13 op return;
675 ca190c99 2021-08-14 op default:
676 cd1d043d 2024-01-13 op load_page_from_str(tab, "Unknown gopher selector");
677 cd1d043d 2024-01-13 op return;
678 81839fee 2021-07-25 op }
679 81839fee 2021-07-25 op
680 62c0f697 2024-02-21 op if (iri_urlunescape(path, req.req, sizeof(req.req)) == -1)
681 62c0f697 2024-02-21 op strlcpy(req.req, path, sizeof(req.req));
682 25905047 2024-01-16 op if (tab->iri.iri_flags & IH_QUERY) {
683 9a28e7e5 2021-08-03 op strlcat(req.req, "?", sizeof(req.req));
684 25905047 2024-01-16 op strlcat(req.req, tab->iri.iri_query, sizeof(req.req));
685 9a28e7e5 2021-08-03 op }
686 767da05c 2021-08-13 op strlcat(req.req, "\r\n", sizeof(req.req));
687 9a28e7e5 2021-08-03 op
688 71bc1636 2024-02-06 op make_request(tab, &req, PROTO_GOPHER, NULL);
689 0972d8b2 2021-03-02 op }
690 0972d8b2 2021-03-02 op
691 cd1d043d 2024-01-13 op static void
692 984245ce 2021-06-23 op load_via_proxy(struct tab *tab, const char *url, struct proxy *p)
693 984245ce 2021-06-23 op {
694 984245ce 2021-06-23 op struct get_req req;
695 984245ce 2021-06-23 op
696 984245ce 2021-06-23 op memset(&req, 0, sizeof(req));
697 984245ce 2021-06-23 op strlcpy(req.host, p->host, sizeof(req.host));
698 f94c7ac1 2021-11-24 op strlcpy(req.port, p->port, sizeof(req.port));
699 984245ce 2021-06-23 op
700 cf1195ce 2021-07-25 op tab->proxy = p;
701 984245ce 2021-06-23 op
702 71bc1636 2024-02-06 op make_request(tab, &req, p->proto, hist_cur(tab->hist));
703 cf1195ce 2021-07-25 op }
704 984245ce 2021-06-23 op
705 cd1d043d 2024-01-13 op static void
706 71bc1636 2024-02-06 op make_request(struct tab *tab, struct get_req *req, int proto, const char *r)
707 cf1195ce 2021-07-25 op {
708 71bc1636 2024-02-06 op int use_cert = 0, fd = -1;
709 71bc1636 2024-02-06 op
710 71bc1636 2024-02-06 op if (proto == PROTO_GEMINI) {
711 71bc1636 2024-02-06 op tab->client_cert = cert_for(&tab->iri, &tab->client_cert_temp);
712 71bc1636 2024-02-06 op use_cert = (tab->client_cert != NULL);
713 71bc1636 2024-02-06 op }
714 d35e18b3 2024-02-04 op
715 cf1195ce 2021-07-25 op stop_tab(tab);
716 cf1195ce 2021-07-25 op tab->id = tab_new_id();
717 cf1195ce 2021-07-25 op req->proto = proto;
718 cf1195ce 2021-07-25 op
719 cf1195ce 2021-07-25 op if (r != NULL) {
720 cf1195ce 2021-07-25 op strlcpy(req->req, r, sizeof(req->req));
721 cf1195ce 2021-07-25 op strlcat(req->req, "\r\n", sizeof(req->req));
722 cf1195ce 2021-07-25 op }
723 cf1195ce 2021-07-25 op
724 5df02e0d 2022-01-11 op start_loading_anim(tab);
725 d35e18b3 2024-02-04 op
726 d35e18b3 2024-02-04 op if (!use_cert)
727 d35e18b3 2024-02-04 op tab->client_cert = NULL;
728 d35e18b3 2024-02-04 op if (use_cert && (fd = cert_open(tab->client_cert)) == -1) {
729 d35e18b3 2024-02-04 op tab->client_cert = NULL;
730 d35e18b3 2024-02-04 op message("failed to open certificate: %s", strerror(errno));
731 d35e18b3 2024-02-04 op }
732 d35e18b3 2024-02-04 op
733 d35e18b3 2024-02-04 op ui_send_net(IMSG_GET, tab->id, fd, req, sizeof(*req));
734 984245ce 2021-06-23 op }
735 984245ce 2021-06-23 op
736 9a28e7e5 2021-08-03 op void
737 9a28e7e5 2021-08-03 op gopher_send_search_req(struct tab *tab, const char *text)
738 9a28e7e5 2021-08-03 op {
739 9a28e7e5 2021-08-03 op struct get_req req;
740 1b0e6efc 2021-08-08 op
741 9a28e7e5 2021-08-03 op memset(&req, 0, sizeof(req));
742 25905047 2024-01-16 op strlcpy(req.host, tab->iri.iri_host, sizeof(req.host));
743 25905047 2024-01-16 op strlcpy(req.port, tab->iri.iri_portstr, sizeof(req.port));
744 9a28e7e5 2021-08-03 op
745 9a28e7e5 2021-08-03 op /* +2 to skip /7 */
746 25905047 2024-01-16 op strlcpy(req.req, tab->iri.iri_path+2, sizeof(req.req));
747 25905047 2024-01-16 op if (tab->iri.iri_flags & IH_QUERY) {
748 9a28e7e5 2021-08-03 op strlcat(req.req, "?", sizeof(req.req));
749 25905047 2024-01-16 op strlcat(req.req, tab->iri.iri_query, sizeof(req.req));
750 9a28e7e5 2021-08-03 op }
751 9a28e7e5 2021-08-03 op
752 9a28e7e5 2021-08-03 op strlcat(req.req, "\t", sizeof(req.req));
753 9a28e7e5 2021-08-03 op strlcat(req.req, text, sizeof(req.req));
754 9a28e7e5 2021-08-03 op strlcat(req.req, "\r\n", sizeof(req.req));
755 9a28e7e5 2021-08-03 op
756 9a28e7e5 2021-08-03 op erase_buffer(&tab->buffer);
757 9a28e7e5 2021-08-03 op parser_init(tab, gophermap_initparser);
758 9a28e7e5 2021-08-03 op
759 71bc1636 2024-02-06 op make_request(tab, &req, PROTO_GOPHER, NULL);
760 9a28e7e5 2021-08-03 op }
761 9a28e7e5 2021-08-03 op
762 cd1d043d 2024-01-13 op void
763 1bcb8303 2022-01-19 op load_page_from_str(struct tab *tab, const char *page)
764 1bcb8303 2022-01-19 op {
765 1bcb8303 2022-01-19 op parser_init(tab, gemtext_initparser);
766 1bcb8303 2022-01-19 op if (!tab->buffer.page.parse(&tab->buffer.page, page, strlen(page)))
767 1bcb8303 2022-01-19 op abort();
768 1bcb8303 2022-01-19 op if (!tab->buffer.page.free(&tab->buffer.page))
769 1bcb8303 2022-01-19 op abort();
770 1bcb8303 2022-01-19 op ui_on_tab_refresh(tab);
771 1bcb8303 2022-01-19 op ui_on_tab_loaded(tab);
772 1bcb8303 2022-01-19 op }
773 1bcb8303 2022-01-19 op
774 adea4eec 2021-07-16 op /*
775 cd1d043d 2024-01-13 op * Effectively load the given url in the given tab.
776 adea4eec 2021-07-16 op */
777 cd1d043d 2024-01-13 op static void
778 ed21a9a1 2022-01-11 op do_load_url(struct tab *tab, const char *url, const char *base, int mode)
779 4d3785b1 2021-03-09 op {
780 0d877486 2022-02-24 op const struct proto *p;
781 984245ce 2021-06-23 op struct proxy *proxy;
782 8f3c9af8 2022-01-11 op int nocache = mode & LU_MODE_NOCACHE;
783 31f1a758 2021-04-22 op char *t;
784 65c49665 2024-01-23 op char buf[1025];
785 7943bb81 2021-07-10 op
786 7943bb81 2021-07-10 op tab->proxy = NULL;
787 10346511 2021-03-17 op tab->trust = TS_UNKNOWN;
788 10346511 2021-03-17 op
789 25905047 2024-01-16 op if (iri_parse(base, url, &tab->iri) == -1) {
790 25905047 2024-01-16 op if (asprintf(&t, "# error loading %s\n>%s\n",
791 25905047 2024-01-16 op url, "Can't parse the IRI") == -1)
792 31f1a758 2021-04-22 op die();
793 65c49665 2024-01-23 op hist_set_cur(tab->hist, url);
794 31f1a758 2021-04-22 op load_page_from_str(tab, t);
795 31f1a758 2021-04-22 op free(t);
796 cd1d043d 2024-01-13 op return;
797 31f1a758 2021-04-22 op }
798 31f1a758 2021-04-22 op
799 65c49665 2024-01-23 op iri_unparse(&tab->iri, buf, sizeof(buf));
800 65c49665 2024-01-23 op hist_set_cur(tab->hist, buf);
801 31f1a758 2021-04-22 op
802 65c49665 2024-01-23 op if (!nocache && mcache_lookup(buf, tab)) {
803 8f3c9af8 2022-01-11 op ui_on_tab_refresh(tab);
804 8f3c9af8 2022-01-11 op ui_on_tab_loaded(tab);
805 cd1d043d 2024-01-13 op return;
806 8f3c9af8 2022-01-11 op }
807 d5af38cc 2022-01-10 op
808 4d3785b1 2021-03-09 op for (p = protos; p->schema != NULL; ++p) {
809 25905047 2024-01-16 op if (!strcmp(tab->iri.iri_scheme, p->schema)) {
810 3b726df7 2024-01-16 op /* patch the port */
811 25905047 2024-01-16 op if (*tab->iri.iri_portstr == '\0' &&
812 25905047 2024-01-16 op p->port != NULL)
813 3b726df7 2024-01-16 op iri_setport(&tab->iri, p->port);
814 ba782cb8 2021-07-25 op
815 65c49665 2024-01-23 op p->loadfn(tab, buf);
816 cd1d043d 2024-01-13 op return;
817 4d3785b1 2021-03-09 op }
818 4d3785b1 2021-03-09 op }
819 4d3785b1 2021-03-09 op
820 984245ce 2021-06-23 op TAILQ_FOREACH(proxy, &proxies, proxies) {
821 25905047 2024-01-16 op if (!strcmp(tab->iri.iri_scheme, proxy->match_proto)) {
822 cd1d043d 2024-01-13 op load_via_proxy(tab, url, proxy);
823 cd1d043d 2024-01-13 op return;
824 cd1d043d 2024-01-13 op }
825 984245ce 2021-06-23 op }
826 984245ce 2021-06-23 op
827 cd1d043d 2024-01-13 op load_page_from_str(tab, err_pages[UNKNOWN_PROTOCOL]);
828 4d3785b1 2021-03-09 op }
829 4d3785b1 2021-03-09 op
830 2ddbda6b 2021-07-17 op /*
831 ea639250 2021-01-02 op * Load url in tab and handle history. If a tab is marked as lazy, only
832 ea639250 2021-01-02 op * prepare the url but don't load it.
833 2ddbda6b 2021-07-17 op */
834 9ad4627d 2021-03-10 op void
835 ed21a9a1 2022-01-11 op load_url(struct tab *tab, const char *url, const char *base, int mode)
836 2051e653 2021-03-13 op {
837 65c49665 2024-01-23 op size_t line_off, curr_off;
838 ea639250 2021-01-02 op int lazy = tab->flags & TAB_LAZY;
839 65c49665 2024-01-23 op int dohist = !(mode & LU_MODE_NOHIST);
840 bc55cdb9 2021-08-12 op
841 fb8dcd1c 2022-01-18 op if (operating && lazy) {
842 fb8dcd1c 2022-01-18 op tab->flags ^= TAB_LAZY;
843 fb8dcd1c 2022-01-18 op lazy = 0;
844 65c49665 2024-01-23 op } else if (hist_size(tab->hist) != 0) {
845 65c49665 2024-01-23 op get_scroll_position(tab, &line_off, &curr_off);
846 65c49665 2024-01-23 op hist_set_offs(tab->hist, line_off, curr_off);
847 65c49665 2024-01-23 op }
848 fb8dcd1c 2022-01-18 op
849 65c49665 2024-01-23 op if (dohist) {
850 65c49665 2024-01-23 op if (hist_push(tab->hist, url) == -1) {
851 98d3e6c1 2024-02-18 op ev_break();
852 2ddbda6b 2021-07-17 op return;
853 2ddbda6b 2021-07-17 op }
854 2ddbda6b 2021-07-17 op
855 2ddbda6b 2021-07-17 op strlcpy(tab->buffer.page.title, url,
856 2ddbda6b 2021-07-17 op sizeof(tab->buffer.page.title));
857 2ddbda6b 2021-07-17 op }
858 2ddbda6b 2021-07-17 op
859 9eca3d7c 2021-11-02 op if (!lazy)
860 ed21a9a1 2022-01-11 op do_load_url(tab, url, base, mode);
861 2051e653 2021-03-13 op }
862 2051e653 2021-03-13 op
863 a37d1a1c 2021-08-14 op void
864 ed21a9a1 2022-01-11 op load_url_in_tab(struct tab *tab, const char *url, const char *base, int mode)
865 a37d1a1c 2021-08-14 op {
866 0259f38d 2022-01-11 op if (operating) {
867 0259f38d 2022-01-11 op autosave_hook();
868 0259f38d 2022-01-11 op message("Loading %s...", url);
869 a37d1a1c 2021-08-14 op }
870 a37d1a1c 2021-08-14 op
871 0f7d8271 2024-01-22 op if (base == NULL)
872 65c49665 2024-01-23 op base = hist_cur(tab->hist);
873 0f7d8271 2024-01-22 op
874 ed21a9a1 2022-01-11 op load_url(tab, url, base, mode);
875 a37d1a1c 2021-08-14 op }
876 a37d1a1c 2021-08-14 op
877 2051e653 2021-03-13 op int
878 2051e653 2021-03-13 op load_previous_page(struct tab *tab)
879 2051e653 2021-03-13 op {
880 65c49665 2024-01-23 op const char *h;
881 2051e653 2021-03-13 op
882 65c49665 2024-01-23 op if ((h = hist_prev(tab->hist)) == NULL)
883 2051e653 2021-03-13 op return 0;
884 65c49665 2024-01-23 op do_load_url(tab, h, NULL, LU_MODE_NONE);
885 2051e653 2021-03-13 op return 1;
886 2051e653 2021-03-13 op }
887 2051e653 2021-03-13 op
888 2051e653 2021-03-13 op int
889 2051e653 2021-03-13 op load_next_page(struct tab *tab)
890 2051e653 2021-03-13 op {
891 65c49665 2024-01-23 op const char *h;
892 2051e653 2021-03-13 op
893 65c49665 2024-01-23 op if ((h = hist_next(tab->hist)) == NULL)
894 2051e653 2021-03-13 op return 0;
895 65c49665 2024-01-23 op do_load_url(tab, h, NULL, LU_MODE_NONE);
896 2051e653 2021-03-13 op return 1;
897 a37d1a1c 2021-08-14 op }
898 a37d1a1c 2021-08-14 op
899 2051e653 2021-03-13 op void
900 868b3a8f 2022-04-13 op write_buffer(const char *path, struct tab *tab)
901 868b3a8f 2022-04-13 op {
902 f63b8f73 2022-04-24 op FILE *fp;
903 f63b8f73 2022-04-24 op
904 868b3a8f 2022-04-13 op if (path == NULL)
905 868b3a8f 2022-04-13 op return;
906 868b3a8f 2022-04-13 op
907 f63b8f73 2022-04-24 op if ((fp = fopen(path, "w")) == NULL)
908 f63b8f73 2022-04-24 op return;
909 f63b8f73 2022-04-24 op if (!parser_serialize(tab, fp))
910 f63b8f73 2022-04-24 op message("Failed to save the page.");
911 f63b8f73 2022-04-24 op fclose(fp);
912 868b3a8f 2022-04-13 op }
913 868b3a8f 2022-04-13 op
914 c6efff96 2021-08-16 op /*
915 449ea6fe 2024-01-16 op * Given a user-entered URL, apply some heuristics to use it if
916 449ea6fe 2024-01-16 op * load-url-use-heuristic allows it.
917 c6efff96 2021-08-16 op *
918 f6d7d047 2021-12-30 op * - if it's a proper url use it
919 f6d7d047 2021-12-30 op * - if it starts with a `./' or a `/' assume its a file:// url
920 cfcbc23c 2024-01-16 op * - assume it's a default-protocol:// url
921 c6efff96 2021-08-16 op *
922 f6d7d047 2021-12-30 op * `ret' (of which len is the size) will be filled with the resulting
923 f6d7d047 2021-12-30 op * url.
924 c6efff96 2021-08-16 op */
925 c6efff96 2021-08-16 op void
926 449ea6fe 2024-01-16 op humanify_url(const char *raw, const char *base, char *ret, size_t len)
927 c6efff96 2021-08-16 op {
928 44008d59 2024-01-16 op static struct iri iri;
929 c7107cec 2021-04-01 op
930 449ea6fe 2024-01-16 op if (load_url_use_heuristic)
931 449ea6fe 2024-01-16 op base = NULL;
932 449ea6fe 2024-01-16 op
933 449ea6fe 2024-01-16 op if (iri_parse(base, raw, &iri) == 0) {
934 44008d59 2024-01-16 op iri_unparse(&iri, ret, len);
935 c6efff96 2021-08-16 op return;
936 c6efff96 2021-08-16 op }
937 c6efff96 2021-08-16 op
938 d89eb764 2022-04-24 op if (!strncmp(raw, "./", 2)) {
939 c6efff96 2021-08-16 op strlcpy(ret, "file://", len);
940 449ea6fe 2024-01-16 op strlcat(ret, cwd, len);
941 c6efff96 2021-08-16 op strlcat(ret, "/", len);
942 c6efff96 2021-08-16 op strlcat(ret, raw+2, len);
943 c6efff96 2021-08-16 op return;
944 c6efff96 2021-08-16 op }
945 c6efff96 2021-08-16 op
946 c6efff96 2021-08-16 op if (*raw == '/') {
947 c6efff96 2021-08-16 op strlcpy(ret, "file://", len);
948 c6efff96 2021-08-16 op strlcat(ret, raw, len);
949 c6efff96 2021-08-16 op return;
950 c6efff96 2021-08-16 op }
951 c6efff96 2021-08-16 op
952 cfcbc23c 2024-01-16 op strlcpy(ret, default_protocol, len);
953 cfcbc23c 2024-01-16 op strlcat(ret, "://", len);
954 c6efff96 2021-08-16 op strlcat(ret, raw, len);
955 486bde7d 2022-04-24 op }
956 486bde7d 2022-04-24 op
957 486bde7d 2022-04-24 op int
958 486bde7d 2022-04-24 op bookmark_page(const char *url)
959 486bde7d 2022-04-24 op {
960 486bde7d 2022-04-24 op FILE *fp;
961 486bde7d 2022-04-24 op
962 486bde7d 2022-04-24 op if ((fp = fopen(bookmark_file, "a")) == NULL)
963 486bde7d 2022-04-24 op return -1;
964 486bde7d 2022-04-24 op fprintf(fp, "=> %s\n", url);
965 486bde7d 2022-04-24 op fclose(fp);
966 486bde7d 2022-04-24 op return 0;
967 c6efff96 2021-08-16 op }
968 c6efff96 2021-08-16 op
969 6cc5fcfe 2021-07-08 op static pid_t
970 6cc5fcfe 2021-07-08 op start_child(enum telescope_process p, const char *argv0, int fd)
971 6cc5fcfe 2021-07-08 op {
972 55ccd6d0 2021-09-15 op const char *argv[5];
973 6cc5fcfe 2021-07-08 op int argc = 0;
974 6cc5fcfe 2021-07-08 op pid_t pid;
975 6cc5fcfe 2021-07-08 op
976 6cc5fcfe 2021-07-08 op switch (pid = fork()) {
977 6cc5fcfe 2021-07-08 op case -1:
978 6cc5fcfe 2021-07-08 op die();
979 6cc5fcfe 2021-07-08 op case 0:
980 6cc5fcfe 2021-07-08 op break;
981 6cc5fcfe 2021-07-08 op default:
982 6cc5fcfe 2021-07-08 op close(fd);
983 6cc5fcfe 2021-07-08 op return pid;
984 6cc5fcfe 2021-07-08 op }
985 6cc5fcfe 2021-07-08 op
986 6cc5fcfe 2021-07-08 op if (dup2(fd, 3) == -1)
987 6cc5fcfe 2021-07-08 op err(1, "cannot setup imsg fd");
988 6cc5fcfe 2021-07-08 op
989 6cc5fcfe 2021-07-08 op argv[argc++] = argv0;
990 6cc5fcfe 2021-07-08 op switch (p) {
991 6cc5fcfe 2021-07-08 op case PROC_UI:
992 6cc5fcfe 2021-07-08 op errx(1, "Can't start ui process");
993 6cc5fcfe 2021-07-08 op case PROC_NET:
994 6cc5fcfe 2021-07-08 op argv[argc++] = "-Tn";
995 6cc5fcfe 2021-07-08 op break;
996 6cc5fcfe 2021-07-08 op }
997 6cc5fcfe 2021-07-08 op
998 55ccd6d0 2021-09-15 op if (safe_mode)
999 55ccd6d0 2021-09-15 op argv[argc++] = "-S";
1000 55ccd6d0 2021-09-15 op
1001 6cc5fcfe 2021-07-08 op argv[argc++] = NULL;
1002 6cc5fcfe 2021-07-08 op execvp(argv0, (char *const *)argv);
1003 6cc5fcfe 2021-07-08 op err(1, "execvp(%s)", argv0);
1004 4cf6ba13 2022-02-11 op }
1005 4cf6ba13 2022-02-11 op
1006 4cf6ba13 2022-02-11 op static void
1007 4cf6ba13 2022-02-11 op send_url(const char *url)
1008 4cf6ba13 2022-02-11 op {
1009 4cf6ba13 2022-02-11 op struct sockaddr_un sun;
1010 4cf6ba13 2022-02-11 op struct imsgbuf ibuf;
1011 4cf6ba13 2022-02-11 op int ctl_sock;
1012 4cf6ba13 2022-02-11 op
1013 4cf6ba13 2022-02-11 op if ((ctl_sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
1014 4cf6ba13 2022-02-11 op err(1, "socket");
1015 4cf6ba13 2022-02-11 op
1016 4cf6ba13 2022-02-11 op memset(&sun, 0, sizeof(sun));
1017 4cf6ba13 2022-02-11 op sun.sun_family = AF_UNIX;
1018 4cf6ba13 2022-02-11 op strlcpy(sun.sun_path, ctlsock_path, sizeof(sun.sun_path));
1019 4cf6ba13 2022-02-11 op
1020 4cf6ba13 2022-02-11 op if (connect(ctl_sock, (struct sockaddr *)&sun, sizeof(sun)) == -1)
1021 4cf6ba13 2022-02-11 op err(1, "connect: %s", ctlsock_path);
1022 4cf6ba13 2022-02-11 op
1023 4cf6ba13 2022-02-11 op imsg_init(&ibuf, ctl_sock);
1024 4cf6ba13 2022-02-11 op imsg_compose(&ibuf, IMSG_CTL_OPEN_URL, 0, 0, -1, url,
1025 4cf6ba13 2022-02-11 op strlen(url) + 1);
1026 4cf6ba13 2022-02-11 op imsg_flush(&ibuf);
1027 4cf6ba13 2022-02-11 op close(ctl_sock);
1028 6cc5fcfe 2021-07-08 op }
1029 6cc5fcfe 2021-07-08 op
1030 1fce2e75 2021-08-14 op int
1031 d35e18b3 2024-02-04 op ui_send_net(int type, uint32_t peerid, int fd, const void *data,
1032 bc10f6a5 2021-07-12 op uint16_t datalen)
1033 bc10f6a5 2021-07-12 op {
1034 d35e18b3 2024-02-04 op return imsg_compose_event(iev_net, type, peerid, 0, fd, data,
1035 bc10f6a5 2021-07-12 op datalen);
1036 bc10f6a5 2021-07-12 op }
1037 bc10f6a5 2021-07-12 op
1038 6cc5fcfe 2021-07-08 op static void __attribute__((noreturn))
1039 6cc5fcfe 2021-07-08 op usage(int r)
1040 d2544989 2021-07-08 op {
1041 335cfaec 2021-09-15 op fprintf(stderr, "USAGE: %s [-ChnSv] [-c config] [url]\n",
1042 dc761924 2021-07-15 op getprogname());
1043 d2544989 2021-07-08 op fprintf(stderr, "version: " PACKAGE " " VERSION "\n");
1044 6cc5fcfe 2021-07-08 op exit(r);
1045 740f578b 2021-03-15 op }
1046 740f578b 2021-03-15 op
1047 5e11c00c 2021-03-02 op int
1048 6cd6a9e1 2021-03-20 op main(int argc, char * const *argv)
1049 5e11c00c 2021-03-02 op {
1050 f63b8f73 2022-04-24 op struct imsgev net_ibuf;
1051 be97d6e6 2021-08-15 op pid_t pid;
1052 4cf6ba13 2022-02-11 op int control_fd;
1053 f63b8f73 2022-04-24 op int pipe2net[2];
1054 d2544989 2021-07-08 op int ch, configtest = 0, fail = 0;
1055 6cc5fcfe 2021-07-08 op int proc = -1;
1056 5d7c642a 2021-09-15 op int sessionfd = -1;
1057 be97d6e6 2021-08-15 op int status;
1058 6cc5fcfe 2021-07-08 op const char *argv0;
1059 5e11c00c 2021-03-02 op
1060 6cc5fcfe 2021-07-08 op argv0 = argv[0];
1061 6cc5fcfe 2021-07-08 op
1062 5e11c00c 2021-03-02 op signal(SIGCHLD, SIG_IGN);
1063 a0e91173 2021-06-13 op signal(SIGPIPE, SIG_IGN);
1064 5e11c00c 2021-03-02 op
1065 d2544989 2021-07-08 op if (getenv("NO_COLOR") != NULL)
1066 d2544989 2021-07-08 op enable_colors = 0;
1067 449ea6fe 2024-01-16 op
1068 dc761924 2021-07-15 op while ((ch = getopt_long(argc, argv, opts, longopts, NULL)) != -1) {
1069 d2544989 2021-07-08 op switch (ch) {
1070 d2544989 2021-07-08 op case 'c':
1071 d2544989 2021-07-08 op fail = 1;
1072 eb2ed626 2021-10-07 op strlcpy(config_path, optarg, sizeof(config_path));
1073 d2544989 2021-07-08 op break;
1074 d2544989 2021-07-08 op case 'n':
1075 d2544989 2021-07-08 op configtest = 1;
1076 d2544989 2021-07-08 op break;
1077 d2544989 2021-07-08 op case 'h':
1078 6cc5fcfe 2021-07-08 op usage(0);
1079 d0971653 2021-09-15 op case 'S':
1080 d0971653 2021-09-15 op safe_mode = 1;
1081 d0971653 2021-09-15 op break;
1082 6cc5fcfe 2021-07-08 op case 'T':
1083 6cc5fcfe 2021-07-08 op switch (*optarg) {
1084 6cc5fcfe 2021-07-08 op case 'n':
1085 6cc5fcfe 2021-07-08 op proc = PROC_NET;
1086 6cc5fcfe 2021-07-08 op break;
1087 6cc5fcfe 2021-07-08 op default:
1088 6cc5fcfe 2021-07-08 op errx(1, "invalid process spec %c",
1089 6cc5fcfe 2021-07-08 op *optarg);
1090 6cc5fcfe 2021-07-08 op }
1091 6cc5fcfe 2021-07-08 op break;
1092 dc761924 2021-07-15 op case 'v':
1093 dc761924 2021-07-15 op printf("%s %s\n", PACKAGE_NAME, PACKAGE_VERSION);
1094 dc761924 2021-07-15 op exit(0);
1095 dc761924 2021-07-15 op break;
1096 d2544989 2021-07-08 op default:
1097 6cc5fcfe 2021-07-08 op usage(1);
1098 d2544989 2021-07-08 op }
1099 d2544989 2021-07-08 op }
1100 d2544989 2021-07-08 op
1101 d2544989 2021-07-08 op argc -= optind;
1102 d2544989 2021-07-08 op argv += optind;
1103 d2544989 2021-07-08 op
1104 6cc5fcfe 2021-07-08 op if (proc != -1) {
1105 6cc5fcfe 2021-07-08 op if (argc > 0)
1106 6cc5fcfe 2021-07-08 op usage(1);
1107 6cc5fcfe 2021-07-08 op else if (proc == PROC_NET)
1108 f45bd2e3 2021-07-24 op return net_main();
1109 6cc5fcfe 2021-07-08 op else
1110 6cc5fcfe 2021-07-08 op usage(1);
1111 6cc5fcfe 2021-07-08 op }
1112 f63b8f73 2022-04-24 op
1113 d5bdf203 2021-07-08 op /* setup keys before reading the config */
1114 d5bdf203 2021-07-08 op TAILQ_INIT(&global_map.m);
1115 d5bdf203 2021-07-08 op global_map.unhandled_input = global_key_unbound;
1116 d5bdf203 2021-07-08 op TAILQ_INIT(&minibuffer_map.m);
1117 d5bdf203 2021-07-08 op
1118 b8ec3578 2024-02-14 op if (fs_init() == -1)
1119 b8ec3578 2024-02-14 op err(1, "fs_init failed");
1120 b8ec3578 2024-02-14 op if (certs_init(certs_file) == -1)
1121 b8ec3578 2024-02-14 op err(1, "certs_init failed");
1122 d2544989 2021-07-08 op config_init();
1123 eb2ed626 2021-10-07 op parseconfig(config_path, fail);
1124 69f32f56 2021-08-16 op if (configtest) {
1125 d2544989 2021-07-08 op puts("config OK");
1126 d2544989 2021-07-08 op exit(0);
1127 d2544989 2021-07-08 op }
1128 d2544989 2021-07-08 op
1129 cfcbc23c 2024-01-16 op if (default_protocol == NULL &&
1130 cfcbc23c 2024-01-16 op (default_protocol = strdup("gemini")) == NULL)
1131 cfcbc23c 2024-01-16 op err(1, "strdup");
1132 cfcbc23c 2024-01-16 op
1133 112b9046 2021-08-29 op if (download_path == NULL &&
1134 112b9046 2021-08-29 op (download_path = strdup("/tmp/")) == NULL)
1135 112b9046 2021-08-29 op errx(1, "strdup");
1136 112b9046 2021-08-29 op
1137 449ea6fe 2024-01-16 op if (argc != 0) {
1138 449ea6fe 2024-01-16 op char *base;
1139 449ea6fe 2024-01-16 op
1140 449ea6fe 2024-01-16 op if (asprintf(&base, "file://%s/", cwd) == -1)
1141 449ea6fe 2024-01-16 op err(1, "asprintf");
1142 449ea6fe 2024-01-16 op
1143 449ea6fe 2024-01-16 op has_url = 1;
1144 449ea6fe 2024-01-16 op humanify_url(argv[0], base, url, sizeof(url));
1145 449ea6fe 2024-01-16 op
1146 449ea6fe 2024-01-16 op free(base);
1147 449ea6fe 2024-01-16 op }
1148 449ea6fe 2024-01-16 op
1149 4cf6ba13 2022-02-11 op if (!safe_mode && (sessionfd = lock_session()) == -1) {
1150 4cf6ba13 2022-02-11 op if (has_url) {
1151 4cf6ba13 2022-02-11 op send_url(url);
1152 4cf6ba13 2022-02-11 op exit(0);
1153 4cf6ba13 2022-02-11 op }
1154 4cf6ba13 2022-02-11 op
1155 d0fd368a 2021-07-15 op errx(1, "can't lock session, is another instance of "
1156 d0fd368a 2021-07-15 op "telescope already running?");
1157 4cf6ba13 2022-02-11 op }
1158 d0fd368a 2021-07-15 op
1159 6cc5fcfe 2021-07-08 op /* Start children. */
1160 bc10f6a5 2021-07-12 op if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe2net) == -1)
1161 35e1f40a 2021-03-14 op err(1, "socketpair");
1162 bc10f6a5 2021-07-12 op start_child(PROC_NET, argv0, pipe2net[1]);
1163 bc10f6a5 2021-07-12 op imsg_init(&net_ibuf.ibuf, pipe2net[0]);
1164 bc10f6a5 2021-07-12 op iev_net = &net_ibuf;
1165 bc10f6a5 2021-07-12 op iev_net->handler = handle_dispatch_imsg;
1166 5e11c00c 2021-03-02 op
1167 37d429b0 2021-04-01 op setproctitle("ui");
1168 35e1f40a 2021-03-14 op
1169 eb722b50 2022-01-03 op /* initialize tofu store */
1170 3768e50f 2021-04-25 op tofu_init(&certs, 5, offsetof(struct tofu_entry, domain));
1171 cbcc75fb 2021-03-17 op
1172 98d3e6c1 2024-02-18 op if (ev_init() == -1)
1173 98d3e6c1 2024-02-18 op err(1, "ev_init");
1174 5e11c00c 2021-03-02 op
1175 4cf6ba13 2022-02-11 op if (!safe_mode) {
1176 4cf6ba13 2022-02-11 op if ((control_fd = control_init(ctlsock_path)) == -1)
1177 4cf6ba13 2022-02-11 op err(1, "control_init %s", ctlsock_path);
1178 4cf6ba13 2022-02-11 op control_listen(control_fd);
1179 4cf6ba13 2022-02-11 op }
1180 4cf6ba13 2022-02-11 op
1181 d5af38cc 2022-01-10 op /* initialize the in-memory cache store */
1182 d5af38cc 2022-01-10 op mcache_init();
1183 d5af38cc 2022-01-10 op
1184 ecfbb1d4 2021-08-14 op /* Setup event handler for the autosave */
1185 1fce2e75 2021-08-14 op autosave_init();
1186 ecfbb1d4 2021-08-14 op
1187 f63b8f73 2022-04-24 op /* Setup event handlers for pipes to net */
1188 bc10f6a5 2021-07-12 op iev_net->events = EV_READ;
1189 98d3e6c1 2024-02-18 op ev_add(iev_net->ibuf.fd, iev_net->events, iev_net->handler, iev_net);
1190 d2544989 2021-07-08 op
1191 bb28f1c2 2021-12-30 op if (ui_init()) {
1192 941b3761 2021-03-18 op sandbox_ui_process();
1193 64f4f8e2 2022-04-24 op load_session(&certs);
1194 d521a6a4 2022-05-11 op if (has_url)
1195 d521a6a4 2022-05-11 op new_tab(url, NULL, NULL);
1196 f63b8f73 2022-04-24 op operating = 1;
1197 f63b8f73 2022-04-24 op switch_to_tab(current_tab);
1198 f63b8f73 2022-04-24 op ui_main_loop();
1199 941b3761 2021-03-18 op ui_end();
1200 941b3761 2021-03-18 op }
1201 5e11c00c 2021-03-02 op
1202 d35e18b3 2024-02-04 op ui_send_net(IMSG_QUIT, 0, -1, NULL, 0);
1203 a9925134 2021-07-15 op imsg_flush(&iev_net->ibuf);
1204 be97d6e6 2021-08-15 op
1205 be97d6e6 2021-08-15 op /* wait for children to terminate */
1206 be97d6e6 2021-08-15 op do {
1207 be97d6e6 2021-08-15 op pid = wait(&status);
1208 be97d6e6 2021-08-15 op if (pid == -1) {
1209 be97d6e6 2021-08-15 op if (errno != EINTR && errno != ECHILD)
1210 be97d6e6 2021-08-15 op err(1, "wait");
1211 be97d6e6 2021-08-15 op } else if (WIFSIGNALED(status))
1212 be97d6e6 2021-08-15 op warnx("child terminated; signal %d", WTERMSIG(status));
1213 be97d6e6 2021-08-15 op } while (pid != -1 || (pid == -1 && errno == EINTR));
1214 5e11c00c 2021-03-02 op
1215 f63b8f73 2022-04-24 op if (!safe_mode)
1216 f63b8f73 2022-04-24 op unlink(crashed_file);
1217 f63b8f73 2022-04-24 op
1218 5d7c642a 2021-09-15 op if (!safe_mode && close(sessionfd) == -1)
1219 5d7c642a 2021-09-15 op err(1, "close(sessionfd = %d)", sessionfd);
1220 d0fd368a 2021-07-15 op
1221 5e11c00c 2021-03-02 op return 0;
1222 5e11c00c 2021-03-02 op }