Blame


1 4540067e 2021-07-18 op /*
2 4540067e 2021-07-18 op * Copyright (c) 2021 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 4cf6ba13 2022-02-11 op #include <sys/un.h>
21 be97d6e6 2021-08-15 op #include <sys/wait.h>
22 5e11c00c 2021-03-02 op
23 be97d6e6 2021-08-15 op #include <errno.h>
24 dc761924 2021-07-15 op #include <getopt.h>
25 5e11c00c 2021-03-02 op #include <signal.h>
26 5e11c00c 2021-03-02 op #include <stdio.h>
27 5e11c00c 2021-03-02 op #include <stdlib.h>
28 5e11c00c 2021-03-02 op #include <string.h>
29 5e11c00c 2021-03-02 op #include <unistd.h>
30 5e11c00c 2021-03-02 op
31 4cf6ba13 2022-02-11 op #include "control.h"
32 e659558c 2021-07-13 op #include "defaults.h"
33 eb2ed626 2021-10-07 op #include "fs.h"
34 d5af38cc 2022-01-10 op #include "mcache.h"
35 4bc446b9 2021-07-21 op #include "minibuffer.h"
36 395b9f4e 2021-07-12 op #include "parser.h"
37 1fce2e75 2021-08-14 op #include "session.h"
38 395b9f4e 2021-07-12 op #include "telescope.h"
39 d1a0f2a3 2021-07-12 op #include "ui.h"
40 9d65b1d9 2022-01-11 op #include "utils.h"
41 dc761924 2021-07-15 op
42 dc761924 2021-07-15 op static struct option longopts[] = {
43 a599eaa6 2021-01-02 op {"colors", no_argument, NULL, 'C'},
44 a599eaa6 2021-01-02 op {"colours", no_argument, NULL, 'C'},
45 dc761924 2021-07-15 op {"help", no_argument, NULL, 'h'},
46 d0971653 2021-09-15 op {"safe", no_argument, NULL, 'S'},
47 dc761924 2021-07-15 op {"version", no_argument, NULL, 'v'},
48 dc761924 2021-07-15 op {NULL, 0, NULL, 0},
49 dc761924 2021-07-15 op };
50 dc761924 2021-07-15 op
51 d0971653 2021-09-15 op static const char *opts = "Cc:hnST:v";
52 a37d1a1c 2021-08-14 op
53 bb28f1c2 2021-12-30 op static int has_url;
54 bb28f1c2 2021-12-30 op static char url[GEMINI_URL_LEN];
55 bb28f1c2 2021-12-30 op
56 a37d1a1c 2021-08-14 op /*
57 a37d1a1c 2021-08-14 op * Used to know when we're finished loading.
58 a37d1a1c 2021-08-14 op */
59 1fce2e75 2021-08-14 op int operating;
60 395b9f4e 2021-07-12 op
61 d0971653 2021-09-15 op /*
62 d0971653 2021-09-15 op * "Safe" (or "sandobox") mode. If enabled, Telescope shouldn't write
63 d0971653 2021-09-15 op * anything to the filesystem or execute external programs.
64 d0971653 2021-09-15 op */
65 d0971653 2021-09-15 op int safe_mode;
66 d0971653 2021-09-15 op
67 bc10f6a5 2021-07-12 op static struct imsgev *iev_fs, *iev_net;
68 ecfbb1d4 2021-08-14 op
69 f6a429eb 2021-07-10 op struct tabshead tabshead = TAILQ_HEAD_INITIALIZER(tabshead);
70 6c74799d 2022-01-05 op struct tabshead ktabshead = TAILQ_HEAD_INITIALIZER(ktabshead);
71 f6a429eb 2021-07-10 op struct proxylist proxies = TAILQ_HEAD_INITIALIZER(proxies);
72 6cc5fcfe 2021-07-08 op
73 6cc5fcfe 2021-07-08 op enum telescope_process {
74 6cc5fcfe 2021-07-08 op PROC_UI,
75 6cc5fcfe 2021-07-08 op PROC_FS,
76 6cc5fcfe 2021-07-08 op PROC_NET,
77 6cc5fcfe 2021-07-08 op };
78 5e11c00c 2021-03-02 op
79 9a8a7402 2021-07-18 op #define CANNOT_FETCH 0
80 9a8a7402 2021-07-18 op #define TOO_MUCH_REDIRECTS 1
81 9a8a7402 2021-07-18 op #define MALFORMED_RESPONSE 2
82 9a8a7402 2021-07-18 op #define UNKNOWN_TYPE_OR_CSET 3
83 9a8a7402 2021-07-18 op #define UNKNOWN_PROTOCOL 4
84 9a8a7402 2021-07-18 op
85 a21e0756 2021-08-14 op /* XXX: keep in sync with normalize_code */
86 9a8a7402 2021-07-18 op const char *err_pages[70] = {
87 9a8a7402 2021-07-18 op [CANNOT_FETCH] = "# Couldn't load the page\n",
88 9a8a7402 2021-07-18 op [TOO_MUCH_REDIRECTS] = "# Too much redirects\n",
89 9a8a7402 2021-07-18 op [MALFORMED_RESPONSE] = "# Got a malformed response\n",
90 9a8a7402 2021-07-18 op [UNKNOWN_TYPE_OR_CSET] = "# Unsupported type or charset\n",
91 9a8a7402 2021-07-18 op [UNKNOWN_PROTOCOL] = "# Unknown protocol\n",
92 9a8a7402 2021-07-18 op
93 9a8a7402 2021-07-18 op [10] = "# Input required\n",
94 9a8a7402 2021-07-18 op [11] = "# Input required\n",
95 9a8a7402 2021-07-18 op [40] = "# Temporary failure\n",
96 9a8a7402 2021-07-18 op [41] = "# Server unavailable\n",
97 9a8a7402 2021-07-18 op [42] = "# CGI error\n",
98 9a8a7402 2021-07-18 op [43] = "# Proxy error\n",
99 9a8a7402 2021-07-18 op [44] = "# Slow down\n",
100 9a8a7402 2021-07-18 op [50] = "# Permanent failure\n",
101 9a8a7402 2021-07-18 op [51] = "# Not found\n",
102 9a8a7402 2021-07-18 op [52] = "# Gone\n",
103 9a8a7402 2021-07-18 op [53] = "# Proxy request refused\n",
104 9a8a7402 2021-07-18 op [59] = "# Bad request\n",
105 9a8a7402 2021-07-18 op [60] = "# Client certificate required\n",
106 9a8a7402 2021-07-18 op [61] = "# Certificate not authorised\n",
107 9a8a7402 2021-07-18 op [62] = "# Certificate not valid\n"
108 9a8a7402 2021-07-18 op };
109 9a8a7402 2021-07-18 op
110 2051e653 2021-03-13 op static void die(void) __attribute__((__noreturn__));
111 2051e653 2021-03-13 op static struct tab *tab_by_id(uint32_t);
112 69f32f56 2021-08-16 op static void handle_imsg_err(struct imsg *, size_t);
113 69f32f56 2021-08-16 op static void handle_imsg_check_cert(struct imsg *, size_t);
114 a2fd3805 2021-07-06 op static void handle_check_cert_user_choice(int, struct tab *);
115 a2fd3805 2021-07-06 op static void handle_maybe_save_new_cert(int, struct tab *);
116 69f32f56 2021-08-16 op static void handle_imsg_got_code(struct imsg *, size_t);
117 69f32f56 2021-08-16 op static void handle_imsg_got_meta(struct imsg *, size_t);
118 a2fd3805 2021-07-06 op static void handle_maybe_save_page(int, struct tab *);
119 d1353324 2021-07-13 op static void handle_save_page_path(const char *, struct tab *);
120 69f32f56 2021-08-16 op static void handle_imsg_file_opened(struct imsg *, size_t);
121 69f32f56 2021-08-16 op static void handle_imsg_buf(struct imsg *, size_t);
122 69f32f56 2021-08-16 op static void handle_imsg_eof(struct imsg *, size_t);
123 eb722b50 2022-01-03 op static void handle_imsg_tofu(struct imsg *, size_t);
124 69f32f56 2021-08-16 op static void handle_imsg_bookmark_ok(struct imsg *, size_t);
125 69f32f56 2021-08-16 op static void handle_imsg_save_cert_ok(struct imsg *, size_t);
126 288fd238 2021-04-25 op static void handle_imsg_update_cert_ok(struct imsg *, size_t);
127 bb28f1c2 2021-12-30 op static void handle_imsg_session(struct imsg *, size_t);
128 69f32f56 2021-08-16 op static void handle_dispatch_imsg(int, short, void *);
129 69f32f56 2021-08-16 op static int load_about_url(struct tab *, const char *);
130 0b1053d2 2021-08-13 op static int load_file_url(struct tab *, const char *);
131 1c690d0a 2021-08-03 op static int load_finger_url(struct tab *, const char *);
132 69f32f56 2021-08-16 op static int load_gemini_url(struct tab *, const char *);
133 1c690d0a 2021-08-03 op static int load_gopher_url(struct tab *, const char *);
134 1c690d0a 2021-08-03 op static int load_via_proxy(struct tab *, const char *,
135 dfb5eb39 2021-07-25 op struct proxy *);
136 1c690d0a 2021-08-03 op static int make_request(struct tab *, struct get_req *, int,
137 cf1195ce 2021-07-25 op const char *);
138 c7586aab 2021-08-13 op static int make_fs_request(struct tab *, int, const char *);
139 ed21a9a1 2022-01-11 op static int do_load_url(struct tab *, const char *, const char *, int);
140 6cc5fcfe 2021-07-08 op static pid_t start_child(enum telescope_process, const char *, int);
141 4cf6ba13 2022-02-11 op static void send_url(const char *);
142 dfb5eb39 2021-07-25 op
143 352e0355 2021-07-25 op static struct proto {
144 352e0355 2021-07-25 op const char *schema;
145 ba782cb8 2021-07-25 op const char *port;
146 69f32f56 2021-08-16 op int (*loadfn)(struct tab *, const char *);
147 352e0355 2021-07-25 op } protos[] = {
148 ba782cb8 2021-07-25 op {"about", NULL, load_about_url},
149 0b1053d2 2021-08-13 op {"file", NULL, load_file_url},
150 ba782cb8 2021-07-25 op {"finger", "79", load_finger_url},
151 ba782cb8 2021-07-25 op {"gemini", "1965", load_gemini_url},
152 1663bf1e 2021-07-25 op {"gopher", "70", load_gopher_url},
153 ba782cb8 2021-07-25 op {NULL, NULL, NULL},
154 dfb5eb39 2021-07-25 op };
155 5e11c00c 2021-03-02 op
156 5e11c00c 2021-03-02 op static imsg_handlerfn *handlers[] = {
157 5e11c00c 2021-03-02 op [IMSG_ERR] = handle_imsg_err,
158 5e11c00c 2021-03-02 op [IMSG_CHECK_CERT] = handle_imsg_check_cert,
159 5e11c00c 2021-03-02 op [IMSG_GOT_CODE] = handle_imsg_got_code,
160 5e11c00c 2021-03-02 op [IMSG_GOT_META] = handle_imsg_got_meta,
161 5e11c00c 2021-03-02 op [IMSG_BUF] = handle_imsg_buf,
162 5e11c00c 2021-03-02 op [IMSG_EOF] = handle_imsg_eof,
163 eb722b50 2022-01-03 op [IMSG_TOFU] = handle_imsg_tofu,
164 740f578b 2021-03-15 op [IMSG_BOOKMARK_OK] = handle_imsg_bookmark_ok,
165 3a227e9a 2021-03-18 op [IMSG_SAVE_CERT_OK] = handle_imsg_save_cert_ok,
166 288fd238 2021-04-25 op [IMSG_UPDATE_CERT_OK] = handle_imsg_update_cert_ok,
167 de2a69bb 2021-05-17 op [IMSG_FILE_OPENED] = handle_imsg_file_opened,
168 bb28f1c2 2021-12-30 op [IMSG_SESSION_TAB] = handle_imsg_session,
169 1040cc7f 2021-01-02 op [IMSG_SESSION_TAB_HIST] = handle_imsg_session,
170 bb28f1c2 2021-12-30 op [IMSG_SESSION_END] = handle_imsg_session,
171 5e11c00c 2021-03-02 op };
172 5e11c00c 2021-03-02 op
173 cbcc75fb 2021-03-17 op static struct ohash certs;
174 cbcc75fb 2021-03-17 op
175 5e11c00c 2021-03-02 op static void __attribute__((__noreturn__))
176 5e11c00c 2021-03-02 op die(void)
177 5e11c00c 2021-03-02 op {
178 5e11c00c 2021-03-02 op abort(); /* TODO */
179 5e11c00c 2021-03-02 op }
180 5e11c00c 2021-03-02 op
181 5e11c00c 2021-03-02 op static struct tab *
182 f6243012 2021-07-19 op tab_by_id(uint32_t id)
183 5e11c00c 2021-03-02 op {
184 5e11c00c 2021-03-02 op struct tab *t;
185 5e11c00c 2021-03-02 op
186 5e11c00c 2021-03-02 op TAILQ_FOREACH(t, &tabshead, tabs) {
187 5e11c00c 2021-03-02 op if (t->id == id)
188 5e11c00c 2021-03-02 op return t;
189 5e11c00c 2021-03-02 op }
190 5e11c00c 2021-03-02 op
191 5d6c2088 2021-07-19 op return NULL;
192 5e11c00c 2021-03-02 op }
193 5e11c00c 2021-03-02 op
194 5e11c00c 2021-03-02 op static void
195 5e11c00c 2021-03-02 op handle_imsg_err(struct imsg *imsg, size_t datalen)
196 5e11c00c 2021-03-02 op {
197 3a9b9365 2021-03-09 op struct tab *tab;
198 3a9b9365 2021-03-09 op char *page;
199 3a9b9365 2021-03-09 op
200 f6243012 2021-07-19 op if ((tab = tab_by_id(imsg->hdr.peerid)) == NULL)
201 f6243012 2021-07-19 op return;
202 3a9b9365 2021-03-09 op
203 3a9b9365 2021-03-09 op page = imsg->data;
204 3a9b9365 2021-03-09 op page[datalen-1] = '\0';
205 3a9b9365 2021-03-09 op
206 3a9b9365 2021-03-09 op if (asprintf(&page, "# Error loading %s\n\n> %s\n",
207 2051e653 2021-03-13 op tab->hist_cur->h, page) == -1)
208 3a9b9365 2021-03-09 op die();
209 3a9b9365 2021-03-09 op load_page_from_str(tab, page);
210 3a9b9365 2021-03-09 op free(page);
211 5e11c00c 2021-03-02 op }
212 5e11c00c 2021-03-02 op
213 5e11c00c 2021-03-02 op static void
214 5e11c00c 2021-03-02 op handle_imsg_check_cert(struct imsg *imsg, size_t datalen)
215 5e11c00c 2021-03-02 op {
216 984245ce 2021-06-23 op const char *hash, *host, *port;
217 cbcc75fb 2021-03-17 op int tofu_res;
218 cbcc75fb 2021-03-17 op struct tofu_entry *e;
219 cbcc75fb 2021-03-17 op struct tab *tab;
220 5e11c00c 2021-03-02 op
221 cbcc75fb 2021-03-17 op hash = imsg->data;
222 cbcc75fb 2021-03-17 op if (hash[datalen-1] != '\0')
223 cbcc75fb 2021-03-17 op abort();
224 cbcc75fb 2021-03-17 op
225 f6243012 2021-07-19 op if ((tab = tab_by_id(imsg->hdr.peerid)) == NULL)
226 f6243012 2021-07-19 op return;
227 cbcc75fb 2021-03-17 op
228 984245ce 2021-06-23 op if (tab->proxy != NULL) {
229 984245ce 2021-06-23 op host = tab->proxy->host;
230 984245ce 2021-06-23 op port = tab->proxy->port;
231 984245ce 2021-06-23 op } else {
232 984245ce 2021-06-23 op host = tab->uri.host;
233 984245ce 2021-06-23 op port = tab->uri.port;
234 984245ce 2021-06-23 op }
235 984245ce 2021-06-23 op
236 984245ce 2021-06-23 op if ((e = tofu_lookup(&certs, host, port)) == NULL) {
237 cbcc75fb 2021-03-17 op /* TODO: an update in libressl/libretls changed
238 cbcc75fb 2021-03-17 op * significantly. Find a better approach at storing
239 cbcc75fb 2021-03-17 op * the certs! */
240 cbcc75fb 2021-03-17 op if (datalen > sizeof(e->hash))
241 cbcc75fb 2021-03-17 op abort();
242 cbcc75fb 2021-03-17 op
243 cbcc75fb 2021-03-17 op tofu_res = 1; /* trust on first use */
244 cbcc75fb 2021-03-17 op if ((e = calloc(1, sizeof(*e))) == NULL)
245 cbcc75fb 2021-03-17 op abort();
246 984245ce 2021-06-23 op strlcpy(e->domain, host, sizeof(e->domain));
247 984245ce 2021-06-23 op if (*port != '\0' && strcmp(port, "1965")) {
248 eb4388ee 2021-04-25 op strlcat(e->domain, ":", sizeof(e->domain));
249 984245ce 2021-06-23 op strlcat(e->domain, port, sizeof(e->domain));
250 eb4388ee 2021-04-25 op }
251 cbcc75fb 2021-03-17 op strlcpy(e->hash, hash, sizeof(e->hash));
252 3768e50f 2021-04-25 op tofu_add(&certs, e);
253 bc10f6a5 2021-07-12 op ui_send_fs(IMSG_SAVE_CERT, tab->id, e, sizeof(*e));
254 cbcc75fb 2021-03-17 op } else
255 cbcc75fb 2021-03-17 op tofu_res = !strcmp(hash, e->hash);
256 cbcc75fb 2021-03-17 op
257 5d1bac73 2021-03-25 op if (tofu_res) {
258 a2fd3805 2021-07-06 op if (e->verified == -1)
259 a2fd3805 2021-07-06 op tab->trust = TS_TEMP_TRUSTED;
260 a2fd3805 2021-07-06 op else if (e->verified == 1)
261 a2fd3805 2021-07-06 op tab->trust = TS_VERIFIED;
262 a2fd3805 2021-07-06 op else
263 a2fd3805 2021-07-06 op tab->trust = TS_TRUSTED;
264 a2fd3805 2021-07-06 op
265 bc10f6a5 2021-07-12 op ui_send_net(IMSG_CERT_STATUS, imsg->hdr.peerid,
266 5d1bac73 2021-03-25 op &tofu_res, sizeof(tofu_res));
267 5d1bac73 2021-03-25 op } else {
268 cbcc75fb 2021-03-17 op tab->trust = TS_UNTRUSTED;
269 cbcc75fb 2021-03-17 op load_page_from_str(tab, "# Certificate mismatch\n");
270 288fd238 2021-04-25 op if ((tab->cert = strdup(hash)) == NULL)
271 288fd238 2021-04-25 op die();
272 5d1bac73 2021-03-25 op ui_yornp("Certificate mismatch. Proceed?",
273 a2fd3805 2021-07-06 op handle_check_cert_user_choice, tab);
274 cbcc75fb 2021-03-17 op }
275 5d1bac73 2021-03-25 op }
276 5d1bac73 2021-03-25 op
277 5d1bac73 2021-03-25 op static void
278 a2fd3805 2021-07-06 op handle_check_cert_user_choice(int accept, struct tab *tab)
279 5d1bac73 2021-03-25 op {
280 bc10f6a5 2021-07-12 op ui_send_net(IMSG_CERT_STATUS, tab->id, &accept,
281 bc10f6a5 2021-07-12 op sizeof(accept));
282 288fd238 2021-04-25 op
283 a2fd3805 2021-07-06 op if (accept) {
284 a2fd3805 2021-07-06 op /*
285 a2fd3805 2021-07-06 op * trust the certificate for this session only. If
286 a2fd3805 2021-07-06 op * the page results in a redirect while we're asking
287 a2fd3805 2021-07-06 op * the user to save, we'll end up with an invalid
288 7b4e4feb 2021-08-15 op * tabid (one request == one tab id). It also makes
289 7b4e4feb 2021-08-15 op * sense to save it for the current session if the
290 7b4e4feb 2021-08-15 op * user accepted it.
291 a2fd3805 2021-07-06 op */
292 69f32f56 2021-08-16 op tofu_temp_trust(&certs, tab->uri.host, tab->uri.port,
293 69f32f56 2021-08-16 op tab->cert);
294 a2fd3805 2021-07-06 op
295 d0971653 2021-09-15 op if (!safe_mode)
296 d0971653 2021-09-15 op ui_yornp("Save the new certificate?",
297 d0971653 2021-09-15 op handle_maybe_save_new_cert, tab);
298 d0971653 2021-09-15 op else
299 d0971653 2021-09-15 op message("Certificate temporarly trusted");
300 a2fd3805 2021-07-06 op } else {
301 288fd238 2021-04-25 op free(tab->cert);
302 288fd238 2021-04-25 op tab->cert = NULL;
303 288fd238 2021-04-25 op }
304 5e11c00c 2021-03-02 op }
305 5e11c00c 2021-03-02 op
306 288fd238 2021-04-25 op static void
307 a2fd3805 2021-07-06 op handle_maybe_save_new_cert(int accept, struct tab *tab)
308 288fd238 2021-04-25 op {
309 288fd238 2021-04-25 op struct tofu_entry *e;
310 984245ce 2021-06-23 op const char *host, *port;
311 288fd238 2021-04-25 op
312 984245ce 2021-06-23 op if (tab->proxy != NULL) {
313 984245ce 2021-06-23 op host = tab->proxy->host;
314 984245ce 2021-06-23 op port = tab->proxy->port;
315 984245ce 2021-06-23 op } else {
316 984245ce 2021-06-23 op host = tab->uri.host;
317 984245ce 2021-06-23 op port = tab->uri.port;
318 984245ce 2021-06-23 op }
319 984245ce 2021-06-23 op
320 288fd238 2021-04-25 op if (!accept)
321 288fd238 2021-04-25 op goto end;
322 288fd238 2021-04-25 op
323 fe2262ad 2021-05-12 op if ((e = calloc(1, sizeof(*e))) == NULL)
324 288fd238 2021-04-25 op die();
325 288fd238 2021-04-25 op
326 984245ce 2021-06-23 op strlcpy(e->domain, host, sizeof(e->domain));
327 984245ce 2021-06-23 op if (*port != '\0' && strcmp(port, "1965")) {
328 288fd238 2021-04-25 op strlcat(e->domain, ":", sizeof(e->domain));
329 984245ce 2021-06-23 op strlcat(e->domain, port, sizeof(e->domain));
330 288fd238 2021-04-25 op }
331 288fd238 2021-04-25 op strlcpy(e->hash, tab->cert, sizeof(e->hash));
332 bc10f6a5 2021-07-12 op ui_send_fs(IMSG_UPDATE_CERT, 0, e, sizeof(*e));
333 288fd238 2021-04-25 op
334 288fd238 2021-04-25 op tofu_update(&certs, e);
335 288fd238 2021-04-25 op
336 288fd238 2021-04-25 op tab->trust = TS_TRUSTED;
337 288fd238 2021-04-25 op
338 288fd238 2021-04-25 op end:
339 288fd238 2021-04-25 op free(tab->cert);
340 288fd238 2021-04-25 op tab->cert = NULL;
341 288fd238 2021-04-25 op }
342 288fd238 2021-04-25 op
343 5cd2ebb1 2021-03-11 op static inline int
344 5cd2ebb1 2021-03-11 op normalize_code(int n)
345 5cd2ebb1 2021-03-11 op {
346 5cd2ebb1 2021-03-11 op if (n < 20) {
347 5cd2ebb1 2021-03-11 op if (n == 10 || n == 11)
348 5cd2ebb1 2021-03-11 op return n;
349 5cd2ebb1 2021-03-11 op return 10;
350 5cd2ebb1 2021-03-11 op } else if (n < 30) {
351 5cd2ebb1 2021-03-11 op return 20;
352 5cd2ebb1 2021-03-11 op } else if (n < 40) {
353 5cd2ebb1 2021-03-11 op if (n == 30 || n == 31)
354 5cd2ebb1 2021-03-11 op return n;
355 5cd2ebb1 2021-03-11 op return 30;
356 5cd2ebb1 2021-03-11 op } else if (n < 50) {
357 5cd2ebb1 2021-03-11 op if (n <= 44)
358 5cd2ebb1 2021-03-11 op return n;
359 5cd2ebb1 2021-03-11 op return 40;
360 5cd2ebb1 2021-03-11 op } else if (n < 60) {
361 5cd2ebb1 2021-03-11 op if (n <= 53 || n == 59)
362 5cd2ebb1 2021-03-11 op return n;
363 5cd2ebb1 2021-03-11 op return 50;
364 5cd2ebb1 2021-03-11 op } else if (n < 70) {
365 5cd2ebb1 2021-03-11 op if (n <= 62)
366 5cd2ebb1 2021-03-11 op return n;
367 5cd2ebb1 2021-03-11 op return 60;
368 5cd2ebb1 2021-03-11 op } else
369 5cd2ebb1 2021-03-11 op return MALFORMED_RESPONSE;
370 5cd2ebb1 2021-03-11 op }
371 5cd2ebb1 2021-03-11 op
372 5e11c00c 2021-03-02 op static void
373 5e11c00c 2021-03-02 op handle_imsg_got_code(struct imsg *imsg, size_t datalen)
374 5e11c00c 2021-03-02 op {
375 0972d8b2 2021-03-02 op struct tab *tab;
376 5e11c00c 2021-03-02 op
377 f6243012 2021-07-19 op if ((tab = tab_by_id(imsg->hdr.peerid)) == NULL)
378 f6243012 2021-07-19 op return;
379 0972d8b2 2021-03-02 op
380 0972d8b2 2021-03-02 op if (sizeof(tab->code) != datalen)
381 5e11c00c 2021-03-02 op die();
382 5e11c00c 2021-03-02 op
383 5cd2ebb1 2021-03-11 op memcpy(&tab->code, imsg->data, sizeof(tab->code));
384 5cd2ebb1 2021-03-11 op tab->code = normalize_code(tab->code);
385 5cd2ebb1 2021-03-11 op if (tab->code != 30 && tab->code != 31)
386 0972d8b2 2021-03-02 op tab->redirect_count = 0;
387 5e11c00c 2021-03-02 op }
388 5e11c00c 2021-03-02 op
389 5e11c00c 2021-03-02 op static void
390 5e11c00c 2021-03-02 op handle_imsg_got_meta(struct imsg *imsg, size_t datalen)
391 5e11c00c 2021-03-02 op {
392 0972d8b2 2021-03-02 op struct tab *tab;
393 fcd99a0d 2021-11-05 op char buf[128];
394 0972d8b2 2021-03-02 op
395 f6243012 2021-07-19 op if ((tab = tab_by_id(imsg->hdr.peerid)) == NULL)
396 f6243012 2021-07-19 op return;
397 0972d8b2 2021-03-02 op
398 0972d8b2 2021-03-02 op if (sizeof(tab->meta) <= datalen)
399 0972d8b2 2021-03-02 op die();
400 5cd2ebb1 2021-03-11 op
401 0972d8b2 2021-03-02 op memcpy(tab->meta, imsg->data, datalen);
402 0972d8b2 2021-03-02 op
403 5cd2ebb1 2021-03-11 op if (tab->code < 10) { /* internal errors */
404 5cd2ebb1 2021-03-11 op load_page_from_str(tab, err_pages[tab->code]);
405 5cd2ebb1 2021-03-11 op } else if (tab->code < 20) { /* 1x */
406 ed504b9e 2022-02-07 op free(tab->last_input_url);
407 ed504b9e 2022-02-07 op tab->last_input_url = strdup(tab->hist_cur->h);
408 ed504b9e 2022-02-07 op if (tab->last_input_url == NULL)
409 ed504b9e 2022-02-07 op die();
410 ed504b9e 2022-02-07 op
411 5cd2ebb1 2021-03-11 op load_page_from_str(tab, err_pages[tab->code]);
412 693188cb 2022-02-07 op ui_require_input(tab, tab->code == 11, ir_select_gemini);
413 5cd2ebb1 2021-03-11 op } else if (tab->code == 20) {
414 c07ac996 2021-03-12 op if (setup_parser_for(tab)) {
415 bc10f6a5 2021-07-12 op ui_send_net(IMSG_PROCEED, tab->id, NULL, 0);
416 fcd99a0d 2021-11-05 op } else if (safe_mode) {
417 69f32f56 2021-08-16 op load_page_from_str(tab,
418 69f32f56 2021-08-16 op err_pages[UNKNOWN_TYPE_OR_CSET]);
419 fcd99a0d 2021-11-05 op } else {
420 fcd99a0d 2021-11-05 op struct hist *h;
421 fcd99a0d 2021-11-05 op
422 fcd99a0d 2021-11-05 op if ((h = hist_pop(&tab->hist)) != NULL)
423 fcd99a0d 2021-11-05 op tab->hist_cur = h;
424 fcd99a0d 2021-11-05 op snprintf(buf, sizeof(buf),
425 fcd99a0d 2021-11-05 op "Can't display \"%s\", save it?", tab->meta);
426 fcd99a0d 2021-11-05 op ui_yornp(buf, handle_maybe_save_page, tab);
427 c07ac996 2021-03-12 op }
428 5cd2ebb1 2021-03-11 op } else if (tab->code < 40) { /* 3x */
429 3a9b9365 2021-03-09 op tab->redirect_count++;
430 0972d8b2 2021-03-02 op
431 3a9b9365 2021-03-09 op /* TODO: make customizable? */
432 3a9b9365 2021-03-09 op if (tab->redirect_count > 5) {
433 3a9b9365 2021-03-09 op load_page_from_str(tab,
434 3a9b9365 2021-03-09 op err_pages[TOO_MUCH_REDIRECTS]);
435 5cd2ebb1 2021-03-11 op } else
436 ed21a9a1 2022-01-11 op do_load_url(tab, tab->meta, NULL, LU_MODE_NOCACHE);
437 5cd2ebb1 2021-03-11 op } else { /* 4x, 5x & 6x */
438 5cd2ebb1 2021-03-11 op load_page_from_str(tab, err_pages[tab->code]);
439 3a9b9365 2021-03-09 op }
440 de2a69bb 2021-05-17 op }
441 de2a69bb 2021-05-17 op
442 de2a69bb 2021-05-17 op static void
443 a2fd3805 2021-07-06 op handle_maybe_save_page(int dosave, struct tab *tab)
444 de2a69bb 2021-05-17 op {
445 bb9912eb 2021-08-29 op const char *f;
446 bb9912eb 2021-08-29 op char input[PATH_MAX];
447 bb9912eb 2021-08-29 op
448 fcd99a0d 2021-11-05 op /* XXX: this print a message that is confusing */
449 fcd99a0d 2021-11-05 op ui_on_tab_loaded(tab);
450 fcd99a0d 2021-11-05 op
451 bb9912eb 2021-08-29 op if (!dosave) {
452 a2fd3805 2021-07-06 op stop_tab(tab);
453 bb9912eb 2021-08-29 op return;
454 bb9912eb 2021-08-29 op }
455 bb9912eb 2021-08-29 op
456 bb9912eb 2021-08-29 op if ((f = strrchr(tab->uri.path, '/')) == NULL)
457 bb9912eb 2021-08-29 op f = "";
458 bb9912eb 2021-08-29 op else
459 bb9912eb 2021-08-29 op f++;
460 bb9912eb 2021-08-29 op
461 112b9046 2021-08-29 op strlcpy(input, download_path, sizeof(input));
462 bb9912eb 2021-08-29 op strlcat(input, f, sizeof(input));
463 bb9912eb 2021-08-29 op
464 bb9912eb 2021-08-29 op ui_read("Save to path", handle_save_page_path, tab, input);
465 de2a69bb 2021-05-17 op }
466 de2a69bb 2021-05-17 op
467 de2a69bb 2021-05-17 op static void
468 d1353324 2021-07-13 op handle_save_page_path(const char *path, struct tab *tab)
469 d1353324 2021-07-13 op {
470 de2a69bb 2021-05-17 op if (path == NULL) {
471 d1353324 2021-07-13 op stop_tab(tab);
472 de2a69bb 2021-05-17 op return;
473 de2a69bb 2021-05-17 op }
474 de2a69bb 2021-05-17 op
475 fcd99a0d 2021-11-05 op ui_show_downloads_pane();
476 de2a69bb 2021-05-17 op
477 fcd99a0d 2021-11-05 op enqueue_download(tab->id, path);
478 d1353324 2021-07-13 op ui_send_fs(IMSG_FILE_OPEN, tab->id, path, strlen(path)+1);
479 fcd99a0d 2021-11-05 op
480 fcd99a0d 2021-11-05 op /*
481 fcd99a0d 2021-11-05 op * Change this tab id, the old one is associated with the
482 fcd99a0d 2021-11-05 op * download now.
483 fcd99a0d 2021-11-05 op */
484 fcd99a0d 2021-11-05 op tab->id = tab_new_id();
485 5e11c00c 2021-03-02 op }
486 5e11c00c 2021-03-02 op
487 5e11c00c 2021-03-02 op static void
488 de2a69bb 2021-05-17 op handle_imsg_file_opened(struct imsg *imsg, size_t datalen)
489 de2a69bb 2021-05-17 op {
490 fcd99a0d 2021-11-05 op struct download *d;
491 de2a69bb 2021-05-17 op const char *e;
492 de2a69bb 2021-05-17 op
493 fcd99a0d 2021-11-05 op /*
494 fcd99a0d 2021-11-05 op * There are no reason we shouldn't be able to find the
495 fcd99a0d 2021-11-05 op * required download.
496 fcd99a0d 2021-11-05 op */
497 fcd99a0d 2021-11-05 op if ((d = download_by_id(imsg->hdr.peerid)) == NULL)
498 fcd99a0d 2021-11-05 op die();
499 de2a69bb 2021-05-17 op
500 de2a69bb 2021-05-17 op if (imsg->fd == -1) {
501 de2a69bb 2021-05-17 op e = imsg->data;
502 de2a69bb 2021-05-17 op if (e[datalen-1] != '\0')
503 de2a69bb 2021-05-17 op die();
504 fcd99a0d 2021-11-05 op message("Can't open file %s: %s", d->path, e);
505 de2a69bb 2021-05-17 op } else {
506 fcd99a0d 2021-11-05 op d->fd = imsg->fd;
507 fcd99a0d 2021-11-05 op ui_send_net(IMSG_PROCEED, d->id, NULL, 0);
508 bb28f1c2 2021-12-30 op }
509 bb28f1c2 2021-12-30 op }
510 bb28f1c2 2021-12-30 op
511 bb28f1c2 2021-12-30 op static void
512 bb28f1c2 2021-12-30 op handle_imsg_session(struct imsg *imsg, size_t datalen)
513 bb28f1c2 2021-12-30 op {
514 bb28f1c2 2021-12-30 op static struct tab *curr;
515 1040cc7f 2021-01-02 op static struct tab *tab;
516 bb28f1c2 2021-12-30 op struct session_tab st;
517 1040cc7f 2021-01-02 op struct session_tab_hist sth;
518 1040cc7f 2021-01-02 op struct hist *h;
519 bb28f1c2 2021-12-30 op int first_time;
520 bb28f1c2 2021-12-30 op
521 bb28f1c2 2021-12-30 op /*
522 bb28f1c2 2021-12-30 op * The fs process tried to send tabs after it has announced
523 bb28f1c2 2021-12-30 op * that he's done. Something fishy is going on, better die.
524 bb28f1c2 2021-12-30 op */
525 bb28f1c2 2021-12-30 op if (operating)
526 bb28f1c2 2021-12-30 op die();
527 bb28f1c2 2021-12-30 op
528 92ce02e2 2021-01-02 op switch (imsg->hdr.type) {
529 92ce02e2 2021-01-02 op case IMSG_SESSION_TAB:
530 92ce02e2 2021-01-02 op if (datalen != sizeof(st))
531 92ce02e2 2021-01-02 op die();
532 92ce02e2 2021-01-02 op
533 92ce02e2 2021-01-02 op memcpy(&st, imsg->data, sizeof(st));
534 92ce02e2 2021-01-02 op if ((tab = new_tab(st.uri, NULL, NULL)) == NULL)
535 92ce02e2 2021-01-02 op die();
536 e795e935 2022-01-18 op tab->hist_cur->line_off = st.top_line;
537 e795e935 2022-01-18 op tab->hist_cur->current_off = st.current_line;
538 92ce02e2 2021-01-02 op strlcpy(tab->buffer.page.title, st.title,
539 92ce02e2 2021-01-02 op sizeof(tab->buffer.page.title));
540 92ce02e2 2021-01-02 op if (st.flags & TAB_CURRENT)
541 92ce02e2 2021-01-02 op curr = tab;
542 6c74799d 2022-01-05 op if (st.flags & TAB_KILLED)
543 05de8ac3 2022-01-06 op kill_tab(tab, 1);
544 92ce02e2 2021-01-02 op break;
545 92ce02e2 2021-01-02 op
546 1040cc7f 2021-01-02 op case IMSG_SESSION_TAB_HIST:
547 1040cc7f 2021-01-02 op if (tab == NULL || datalen != sizeof(sth))
548 1040cc7f 2021-01-02 op die();
549 1040cc7f 2021-01-02 op
550 1040cc7f 2021-01-02 op memcpy(&sth, imsg->data, sizeof(sth));
551 1040cc7f 2021-01-02 op if (sth.uri[sizeof(sth.uri)-1] != '\0')
552 1040cc7f 2021-01-02 op die();
553 1040cc7f 2021-01-02 op
554 1040cc7f 2021-01-02 op if ((h = calloc(1, sizeof(*h))) == NULL)
555 1040cc7f 2021-01-02 op die();
556 1040cc7f 2021-01-02 op strlcpy(h->h, sth.uri, sizeof(h->h));
557 1040cc7f 2021-01-02 op
558 1040cc7f 2021-01-02 op if (sth.future)
559 1040cc7f 2021-01-02 op hist_push(&tab->hist, h);
560 1040cc7f 2021-01-02 op else
561 1040cc7f 2021-01-02 op hist_add_before(&tab->hist, tab->hist_cur, h);
562 1040cc7f 2021-01-02 op break;
563 1040cc7f 2021-01-02 op
564 92ce02e2 2021-01-02 op case IMSG_SESSION_END:
565 bb28f1c2 2021-12-30 op if (datalen != sizeof(first_time))
566 bb28f1c2 2021-12-30 op die();
567 bb28f1c2 2021-12-30 op memcpy(&first_time, imsg->data, sizeof(first_time));
568 bb28f1c2 2021-12-30 op if (first_time) {
569 bb28f1c2 2021-12-30 op new_tab("about:new", NULL, NULL);
570 bb28f1c2 2021-12-30 op curr = new_tab("about:help", NULL, NULL);
571 bb28f1c2 2021-12-30 op }
572 bb28f1c2 2021-12-30 op
573 bb28f1c2 2021-12-30 op operating = 1;
574 bb28f1c2 2021-12-30 op if (curr != NULL)
575 bb28f1c2 2021-12-30 op switch_to_tab(curr);
576 bb28f1c2 2021-12-30 op if (has_url || TAILQ_EMPTY(&tabshead))
577 bb28f1c2 2021-12-30 op new_tab(url, NULL, NULL);
578 bb28f1c2 2021-12-30 op ui_main_loop();
579 92ce02e2 2021-01-02 op break;
580 bb28f1c2 2021-12-30 op
581 92ce02e2 2021-01-02 op default:
582 bb28f1c2 2021-12-30 op die();
583 92ce02e2 2021-01-02 op }
584 de2a69bb 2021-05-17 op }
585 de2a69bb 2021-05-17 op
586 de2a69bb 2021-05-17 op static void
587 5e11c00c 2021-03-02 op handle_imsg_buf(struct imsg *imsg, size_t datalen)
588 5e11c00c 2021-03-02 op {
589 fcd99a0d 2021-11-05 op struct tab *tab = NULL;
590 fcd99a0d 2021-11-05 op struct download *d = NULL;
591 5e11c00c 2021-03-02 op
592 fcd99a0d 2021-11-05 op if ((tab = tab_by_id(imsg->hdr.peerid)) == NULL &&
593 fcd99a0d 2021-11-05 op (d = download_by_id(imsg->hdr.peerid)) == NULL)
594 5d6c2088 2021-07-19 op return;
595 5e11c00c 2021-03-02 op
596 fcd99a0d 2021-11-05 op if (tab != NULL) {
597 8c1b0837 2021-07-25 op if (!parser_parse(tab, imsg->data, datalen))
598 de2a69bb 2021-05-17 op die();
599 fcd99a0d 2021-11-05 op ui_on_tab_refresh(tab);
600 de2a69bb 2021-05-17 op } else {
601 fcd99a0d 2021-11-05 op d->bytes += datalen;
602 fcd99a0d 2021-11-05 op write(d->fd, imsg->data, datalen);
603 fcd99a0d 2021-11-05 op ui_on_download_refresh();
604 de2a69bb 2021-05-17 op }
605 5e11c00c 2021-03-02 op }
606 5e11c00c 2021-03-02 op
607 5e11c00c 2021-03-02 op static void
608 5e11c00c 2021-03-02 op handle_imsg_eof(struct imsg *imsg, size_t datalen)
609 5e11c00c 2021-03-02 op {
610 fcd99a0d 2021-11-05 op struct tab *tab = NULL;
611 fcd99a0d 2021-11-05 op struct download *d = NULL;
612 a5c3e03d 2021-03-02 op
613 fcd99a0d 2021-11-05 op if ((tab = tab_by_id(imsg->hdr.peerid)) == NULL &&
614 fcd99a0d 2021-11-05 op (d = download_by_id(imsg->hdr.peerid)) == NULL)
615 f6243012 2021-07-19 op return;
616 de2a69bb 2021-05-17 op
617 fcd99a0d 2021-11-05 op if (tab != NULL) {
618 8c1b0837 2021-07-25 op if (!parser_free(tab))
619 de2a69bb 2021-05-17 op die();
620 dc8e7bf6 2022-01-11 op if (has_prefix(tab->hist_cur->h, "gemini://") ||
621 dc8e7bf6 2022-01-11 op has_prefix(tab->hist_cur->h, "gopher://"))
622 8f3c9af8 2022-01-11 op mcache_tab(tab);
623 fcd99a0d 2021-11-05 op ui_on_tab_refresh(tab);
624 fcd99a0d 2021-11-05 op ui_on_tab_loaded(tab);
625 de2a69bb 2021-05-17 op } else {
626 fcd99a0d 2021-11-05 op close(d->fd);
627 fcd99a0d 2021-11-05 op d->fd = -1;
628 fcd99a0d 2021-11-05 op ui_on_download_refresh();
629 de2a69bb 2021-05-17 op }
630 5e11c00c 2021-03-02 op }
631 5e11c00c 2021-03-02 op
632 5e11c00c 2021-03-02 op static void
633 eb722b50 2022-01-03 op handle_imsg_tofu(struct imsg *imsg, size_t datalen)
634 eb722b50 2022-01-03 op {
635 eb722b50 2022-01-03 op struct tofu_entry *e;
636 eb722b50 2022-01-03 op
637 eb722b50 2022-01-03 op if (operating)
638 eb722b50 2022-01-03 op die();
639 eb722b50 2022-01-03 op
640 eb722b50 2022-01-03 op if ((e = calloc(1, sizeof(*e))) == NULL)
641 eb722b50 2022-01-03 op die();
642 eb722b50 2022-01-03 op
643 eb722b50 2022-01-03 op if (datalen != sizeof(*e))
644 eb722b50 2022-01-03 op die();
645 eb722b50 2022-01-03 op memcpy(e, imsg->data, sizeof(*e));
646 eb722b50 2022-01-03 op if (e->domain[sizeof(e->domain)-1] != '\0' ||
647 eb722b50 2022-01-03 op e->hash[sizeof(e->hash)-1] != '\0')
648 eb722b50 2022-01-03 op die();
649 eb722b50 2022-01-03 op tofu_add(&certs, e);
650 eb722b50 2022-01-03 op }
651 eb722b50 2022-01-03 op
652 eb722b50 2022-01-03 op static void
653 740f578b 2021-03-15 op handle_imsg_bookmark_ok(struct imsg *imsg, size_t datalen)
654 740f578b 2021-03-15 op {
655 740f578b 2021-03-15 op int res;
656 740f578b 2021-03-15 op
657 740f578b 2021-03-15 op if (datalen != sizeof(res))
658 740f578b 2021-03-15 op die();
659 740f578b 2021-03-15 op
660 740f578b 2021-03-15 op memcpy(&res, imsg->data, sizeof(res));
661 740f578b 2021-03-15 op if (res == 0)
662 7f963c41 2021-06-20 op message("Added to bookmarks!");
663 740f578b 2021-03-15 op else
664 7f963c41 2021-06-20 op message("Failed to add to bookmarks: %s",
665 740f578b 2021-03-15 op strerror(res));
666 740f578b 2021-03-15 op }
667 740f578b 2021-03-15 op
668 740f578b 2021-03-15 op static void
669 3a227e9a 2021-03-18 op handle_imsg_save_cert_ok(struct imsg *imsg, size_t datalen)
670 3a227e9a 2021-03-18 op {
671 3a227e9a 2021-03-18 op int res;
672 3a227e9a 2021-03-18 op
673 3a227e9a 2021-03-18 op if (datalen != sizeof(res))
674 3a227e9a 2021-03-18 op die();
675 3a227e9a 2021-03-18 op memcpy(&res, imsg->data, datalen);
676 3a227e9a 2021-03-18 op if (res != 0)
677 7f963c41 2021-06-20 op message("Failed to save the cert for: %s",
678 3a227e9a 2021-03-18 op strerror(res));
679 288fd238 2021-04-25 op }
680 288fd238 2021-04-25 op
681 288fd238 2021-04-25 op static void
682 288fd238 2021-04-25 op handle_imsg_update_cert_ok(struct imsg *imsg, size_t datalen)
683 288fd238 2021-04-25 op {
684 288fd238 2021-04-25 op int res;
685 288fd238 2021-04-25 op
686 288fd238 2021-04-25 op if (datalen != sizeof(res))
687 288fd238 2021-04-25 op die();
688 288fd238 2021-04-25 op memcpy(&res, imsg->data, datalen);
689 288fd238 2021-04-25 op if (!res)
690 7f963c41 2021-06-20 op message("Failed to update the certificate");
691 3a227e9a 2021-03-18 op }
692 3a227e9a 2021-03-18 op
693 3a227e9a 2021-03-18 op static void
694 1304bbdd 2021-03-15 op handle_dispatch_imsg(int fd, short ev, void *d)
695 5e11c00c 2021-03-02 op {
696 bc10f6a5 2021-07-12 op struct imsgev *iev = d;
697 ea080950 2021-07-20 op
698 ea080950 2021-07-20 op if (dispatch_imsg(iev, ev, handlers, sizeof(handlers)) == -1)
699 ea080950 2021-07-20 op err(1, "connection closed");
700 0972d8b2 2021-03-02 op }
701 0972d8b2 2021-03-02 op
702 1c690d0a 2021-08-03 op static int
703 4d3785b1 2021-03-09 op load_about_url(struct tab *tab, const char *url)
704 0972d8b2 2021-03-02 op {
705 7b2aac84 2021-08-13 op tab->trust = TS_UNKNOWN;
706 8c1b0837 2021-07-25 op parser_init(tab, gemtext_initparser);
707 c7586aab 2021-08-13 op return make_fs_request(tab, IMSG_GET, url);
708 4d3785b1 2021-03-09 op }
709 0972d8b2 2021-03-02 op
710 1c690d0a 2021-08-03 op static int
711 0b1053d2 2021-08-13 op load_file_url(struct tab *tab, const char *url)
712 0b1053d2 2021-08-13 op {
713 0b1053d2 2021-08-13 op tab->trust = TS_UNKNOWN;
714 0b1053d2 2021-08-13 op return make_fs_request(tab, IMSG_GET_FILE, tab->uri.path);
715 0b1053d2 2021-08-13 op }
716 0b1053d2 2021-08-13 op
717 0b1053d2 2021-08-13 op static int
718 5fb52fc0 2021-07-25 op load_finger_url(struct tab *tab, const char *url)
719 5fb52fc0 2021-07-25 op {
720 5fb52fc0 2021-07-25 op struct get_req req;
721 5fb52fc0 2021-07-25 op size_t len;
722 5fb52fc0 2021-07-25 op char *at;
723 5fb52fc0 2021-07-25 op
724 5fb52fc0 2021-07-25 op memset(&req, 0, sizeof(req));
725 ba782cb8 2021-07-25 op strlcpy(req.port, tab->uri.port, sizeof(req.port));
726 5fb52fc0 2021-07-25 op
727 5fb52fc0 2021-07-25 op /*
728 5fb52fc0 2021-07-25 op * Sometimes the finger url have the user as path component
729 5fb52fc0 2021-07-25 op * (e.g. finger://thelambdalab.xyz/plugd), sometimes as
730 5fb52fc0 2021-07-25 op * userinfo (e.g. finger://cobradile@finger.farm).
731 5fb52fc0 2021-07-25 op */
732 5fb52fc0 2021-07-25 op if ((at = strchr(tab->uri.host, '@')) != NULL) {
733 5fb52fc0 2021-07-25 op len = at - tab->uri.host;
734 5fb52fc0 2021-07-25 op memcpy(req.req, tab->uri.host, len);
735 5fb52fc0 2021-07-25 op
736 5fb52fc0 2021-07-25 op if (len >= sizeof(req.req))
737 5fb52fc0 2021-07-25 op die();
738 5fb52fc0 2021-07-25 op req.req[len] = '\0';
739 5fb52fc0 2021-07-25 op
740 5fb52fc0 2021-07-25 op strlcpy(req.host, at+1, sizeof(req.host));
741 5fb52fc0 2021-07-25 op } else {
742 5fb52fc0 2021-07-25 op strlcpy(req.host, tab->uri.host, sizeof(req.host));
743 5fb52fc0 2021-07-25 op
744 5fb52fc0 2021-07-25 op /* +1 to skip the initial `/' */
745 5fb52fc0 2021-07-25 op strlcpy(req.req, tab->uri.path+1, sizeof(req.req));
746 5fb52fc0 2021-07-25 op }
747 5fb52fc0 2021-07-25 op strlcat(req.req, "\r\n", sizeof(req.req));
748 5fb52fc0 2021-07-25 op
749 8c1b0837 2021-07-25 op parser_init(tab, textplain_initparser);
750 1c690d0a 2021-08-03 op return make_request(tab, &req, PROTO_FINGER, NULL);
751 5fb52fc0 2021-07-25 op }
752 5fb52fc0 2021-07-25 op
753 1c690d0a 2021-08-03 op static int
754 4d3785b1 2021-03-09 op load_gemini_url(struct tab *tab, const char *url)
755 4d3785b1 2021-03-09 op {
756 984245ce 2021-06-23 op struct get_req req;
757 2eef3403 2021-04-22 op
758 984245ce 2021-06-23 op memset(&req, 0, sizeof(req));
759 984245ce 2021-06-23 op strlcpy(req.host, tab->uri.host, sizeof(req.host));
760 f94c7ac1 2021-11-24 op strlcpy(req.port, tab->uri.port, sizeof(req.port));
761 984245ce 2021-06-23 op
762 1c690d0a 2021-08-03 op return make_request(tab, &req, PROTO_GEMINI, tab->hist_cur->h);
763 ca190c99 2021-08-14 op }
764 ca190c99 2021-08-14 op
765 ca190c99 2021-08-14 op static inline const char *
766 ca190c99 2021-08-14 op gopher_skip_selector(const char *path, int *ret_type)
767 ca190c99 2021-08-14 op {
768 ca190c99 2021-08-14 op *ret_type = 0;
769 ca190c99 2021-08-14 op
770 ca190c99 2021-08-14 op if (!strcmp(path, "/") || *path == '\0') {
771 ca190c99 2021-08-14 op *ret_type = '1';
772 ca190c99 2021-08-14 op return path;
773 ca190c99 2021-08-14 op }
774 ca190c99 2021-08-14 op
775 ca190c99 2021-08-14 op if (*path != '/')
776 ca190c99 2021-08-14 op return path;
777 ca190c99 2021-08-14 op path++;
778 ca190c99 2021-08-14 op
779 ca190c99 2021-08-14 op switch (*ret_type = *path) {
780 ca190c99 2021-08-14 op case '0':
781 ca190c99 2021-08-14 op case '1':
782 ca190c99 2021-08-14 op case '7':
783 ca190c99 2021-08-14 op break;
784 ca190c99 2021-08-14 op
785 ca190c99 2021-08-14 op default:
786 ca190c99 2021-08-14 op *ret_type = 0;
787 ca190c99 2021-08-14 op path -= 1;
788 ca190c99 2021-08-14 op return path;
789 ca190c99 2021-08-14 op }
790 ca190c99 2021-08-14 op
791 95bb01e2 2021-08-28 op return ++path;
792 1663bf1e 2021-07-25 op }
793 1663bf1e 2021-07-25 op
794 1c690d0a 2021-08-03 op static int
795 1663bf1e 2021-07-25 op load_gopher_url(struct tab *tab, const char *url)
796 1663bf1e 2021-07-25 op {
797 81839fee 2021-07-25 op struct get_req req;
798 ca190c99 2021-08-14 op int type;
799 81839fee 2021-07-25 op const char *path;
800 1663bf1e 2021-07-25 op
801 1663bf1e 2021-07-25 op memset(&req, 0, sizeof(req));
802 1663bf1e 2021-07-25 op strlcpy(req.host, tab->uri.host, sizeof(req.host));
803 f94c7ac1 2021-11-24 op strlcpy(req.port, tab->uri.port, sizeof(req.port));
804 1663bf1e 2021-07-25 op
805 ca190c99 2021-08-14 op path = gopher_skip_selector(tab->uri.path, &type);
806 ca190c99 2021-08-14 op switch (type) {
807 ca190c99 2021-08-14 op case '0':
808 8c1b0837 2021-07-25 op parser_init(tab, textplain_initparser);
809 ca190c99 2021-08-14 op break;
810 ca190c99 2021-08-14 op case '1':
811 ca190c99 2021-08-14 op parser_init(tab, gophermap_initparser);
812 ca190c99 2021-08-14 op break;
813 ca190c99 2021-08-14 op case '7':
814 acf9defe 2022-02-08 op free(tab->last_input_url);
815 acf9defe 2022-02-08 op tab->last_input_url = strdup(url);
816 acf9defe 2022-02-08 op if (tab->last_input_url == NULL)
817 acf9defe 2022-02-08 op die();
818 693188cb 2022-02-07 op ui_require_input(tab, 0, ir_select_gopher);
819 9a28e7e5 2021-08-03 op return load_page_from_str(tab, err_pages[10]);
820 ca190c99 2021-08-14 op default:
821 ca190c99 2021-08-14 op return load_page_from_str(tab, "Unknown gopher selector");
822 81839fee 2021-07-25 op }
823 81839fee 2021-07-25 op
824 9a28e7e5 2021-08-03 op strlcpy(req.req, path, sizeof(req.req));
825 9a28e7e5 2021-08-03 op if (*tab->uri.query != '\0') {
826 9a28e7e5 2021-08-03 op strlcat(req.req, "?", sizeof(req.req));
827 9a28e7e5 2021-08-03 op strlcat(req.req, tab->uri.query, sizeof(req.req));
828 9a28e7e5 2021-08-03 op }
829 767da05c 2021-08-13 op strlcat(req.req, "\r\n", sizeof(req.req));
830 9a28e7e5 2021-08-03 op
831 767da05c 2021-08-13 op return make_request(tab, &req, PROTO_GOPHER, NULL);
832 0972d8b2 2021-03-02 op }
833 0972d8b2 2021-03-02 op
834 1c690d0a 2021-08-03 op static int
835 984245ce 2021-06-23 op load_via_proxy(struct tab *tab, const char *url, struct proxy *p)
836 984245ce 2021-06-23 op {
837 984245ce 2021-06-23 op struct get_req req;
838 984245ce 2021-06-23 op
839 984245ce 2021-06-23 op memset(&req, 0, sizeof(req));
840 984245ce 2021-06-23 op strlcpy(req.host, p->host, sizeof(req.host));
841 f94c7ac1 2021-11-24 op strlcpy(req.port, p->port, sizeof(req.port));
842 984245ce 2021-06-23 op
843 cf1195ce 2021-07-25 op tab->proxy = p;
844 984245ce 2021-06-23 op
845 1c690d0a 2021-08-03 op return make_request(tab, &req, p->proto, tab->hist_cur->h);
846 cf1195ce 2021-07-25 op }
847 984245ce 2021-06-23 op
848 1c690d0a 2021-08-03 op static int
849 cf1195ce 2021-07-25 op make_request(struct tab *tab, struct get_req *req, int proto, const char *r)
850 cf1195ce 2021-07-25 op {
851 cf1195ce 2021-07-25 op stop_tab(tab);
852 cf1195ce 2021-07-25 op tab->id = tab_new_id();
853 cf1195ce 2021-07-25 op req->proto = proto;
854 cf1195ce 2021-07-25 op
855 cf1195ce 2021-07-25 op if (r != NULL) {
856 cf1195ce 2021-07-25 op strlcpy(req->req, r, sizeof(req->req));
857 cf1195ce 2021-07-25 op strlcat(req->req, "\r\n", sizeof(req->req));
858 cf1195ce 2021-07-25 op }
859 cf1195ce 2021-07-25 op
860 5df02e0d 2022-01-11 op start_loading_anim(tab);
861 cf1195ce 2021-07-25 op ui_send_net(IMSG_GET_RAW, tab->id, req, sizeof(*req));
862 1c690d0a 2021-08-03 op
863 1c690d0a 2021-08-03 op /*
864 1c690d0a 2021-08-03 op * So the various load_*_url can `return make_request` and
865 1c690d0a 2021-08-03 op * do_load_url is happy.
866 1c690d0a 2021-08-03 op */
867 1c690d0a 2021-08-03 op return 1;
868 984245ce 2021-06-23 op }
869 984245ce 2021-06-23 op
870 c7586aab 2021-08-13 op static int
871 c7586aab 2021-08-13 op make_fs_request(struct tab *tab, int type, const char *r)
872 c7586aab 2021-08-13 op {
873 c7586aab 2021-08-13 op stop_tab(tab);
874 c7586aab 2021-08-13 op tab->id = tab_new_id();
875 c7586aab 2021-08-13 op
876 c7586aab 2021-08-13 op ui_send_fs(type, tab->id, r, strlen(r)+1);
877 c7586aab 2021-08-13 op
878 c7586aab 2021-08-13 op /*
879 c7586aab 2021-08-13 op * So load_{about,file}_url can `return make_fs_request` and
880 c7586aab 2021-08-13 op * do_load_url is happy.
881 c7586aab 2021-08-13 op */
882 c7586aab 2021-08-13 op return 1;
883 c7586aab 2021-08-13 op }
884 c7586aab 2021-08-13 op
885 9a28e7e5 2021-08-03 op void
886 9a28e7e5 2021-08-03 op gopher_send_search_req(struct tab *tab, const char *text)
887 9a28e7e5 2021-08-03 op {
888 9a28e7e5 2021-08-03 op struct get_req req;
889 1b0e6efc 2021-08-08 op
890 9a28e7e5 2021-08-03 op memset(&req, 0, sizeof(req));
891 9a28e7e5 2021-08-03 op strlcpy(req.host, tab->uri.host, sizeof(req.host));
892 f94c7ac1 2021-11-24 op strlcpy(req.port, tab->uri.port, sizeof(req.port));
893 9a28e7e5 2021-08-03 op
894 9a28e7e5 2021-08-03 op /* +2 to skip /7 */
895 9a28e7e5 2021-08-03 op strlcpy(req.req, tab->uri.path+2, sizeof(req.req));
896 9a28e7e5 2021-08-03 op if (*tab->uri.query != '\0') {
897 9a28e7e5 2021-08-03 op strlcat(req.req, "?", sizeof(req.req));
898 9a28e7e5 2021-08-03 op strlcat(req.req, tab->uri.query, sizeof(req.req));
899 9a28e7e5 2021-08-03 op }
900 9a28e7e5 2021-08-03 op
901 9a28e7e5 2021-08-03 op strlcat(req.req, "\t", sizeof(req.req));
902 9a28e7e5 2021-08-03 op strlcat(req.req, text, sizeof(req.req));
903 9a28e7e5 2021-08-03 op strlcat(req.req, "\r\n", sizeof(req.req));
904 9a28e7e5 2021-08-03 op
905 9a28e7e5 2021-08-03 op erase_buffer(&tab->buffer);
906 9a28e7e5 2021-08-03 op parser_init(tab, gophermap_initparser);
907 9a28e7e5 2021-08-03 op
908 9a28e7e5 2021-08-03 op make_request(tab, &req, PROTO_GOPHER, NULL);
909 9a28e7e5 2021-08-03 op }
910 9a28e7e5 2021-08-03 op
911 1bcb8303 2022-01-19 op int
912 1bcb8303 2022-01-19 op load_page_from_str(struct tab *tab, const char *page)
913 1bcb8303 2022-01-19 op {
914 1bcb8303 2022-01-19 op parser_init(tab, gemtext_initparser);
915 1bcb8303 2022-01-19 op if (!tab->buffer.page.parse(&tab->buffer.page, page, strlen(page)))
916 1bcb8303 2022-01-19 op abort();
917 1bcb8303 2022-01-19 op if (!tab->buffer.page.free(&tab->buffer.page))
918 1bcb8303 2022-01-19 op abort();
919 1bcb8303 2022-01-19 op ui_on_tab_refresh(tab);
920 1bcb8303 2022-01-19 op ui_on_tab_loaded(tab);
921 1bcb8303 2022-01-19 op return 0;
922 1bcb8303 2022-01-19 op }
923 1bcb8303 2022-01-19 op
924 adea4eec 2021-07-16 op /*
925 adea4eec 2021-07-16 op * Effectively load the given url in the given tab. Return 1 when
926 adea4eec 2021-07-16 op * loading the page asynchronously, and thus when an erase_buffer can
927 adea4eec 2021-07-16 op * be done right after this function return, or 0 when loading the
928 9eca3d7c 2021-11-02 op * page synchronously.
929 adea4eec 2021-07-16 op */
930 adea4eec 2021-07-16 op static int
931 ed21a9a1 2022-01-11 op do_load_url(struct tab *tab, const char *url, const char *base, int mode)
932 4d3785b1 2021-03-09 op {
933 31f1a758 2021-04-22 op struct phos_uri uri;
934 31f1a758 2021-04-22 op struct proto *p;
935 984245ce 2021-06-23 op struct proxy *proxy;
936 8f3c9af8 2022-01-11 op int nocache = mode & LU_MODE_NOCACHE;
937 31f1a758 2021-04-22 op char *t;
938 7943bb81 2021-07-10 op
939 7943bb81 2021-07-10 op tab->proxy = NULL;
940 10346511 2021-03-17 op tab->trust = TS_UNKNOWN;
941 10346511 2021-03-17 op
942 bd3a3a95 2021-07-20 op if (base == NULL)
943 bd3a3a95 2021-07-20 op memcpy(&uri, &tab->uri, sizeof(tab->uri));
944 bd3a3a95 2021-07-20 op else
945 bd3a3a95 2021-07-20 op phos_parse_absolute_uri(base, &uri);
946 bd3a3a95 2021-07-20 op
947 31f1a758 2021-04-22 op if (!phos_resolve_uri_from_str(&uri, url, &tab->uri)) {
948 902fc2c2 2021-07-25 op if (asprintf(&t, "#error loading %s\n>%s\n",
949 31f1a758 2021-04-22 op url, "Can't parse the URI") == -1)
950 31f1a758 2021-04-22 op die();
951 31f1a758 2021-04-22 op strlcpy(tab->hist_cur->h, url, sizeof(tab->hist_cur->h));
952 31f1a758 2021-04-22 op load_page_from_str(tab, t);
953 31f1a758 2021-04-22 op free(t);
954 adea4eec 2021-07-16 op return 0;
955 31f1a758 2021-04-22 op }
956 31f1a758 2021-04-22 op
957 31f1a758 2021-04-22 op phos_serialize_uri(&tab->uri, tab->hist_cur->h,
958 31f1a758 2021-04-22 op sizeof(tab->hist_cur->h));
959 31f1a758 2021-04-22 op
960 8f3c9af8 2022-01-11 op if (!nocache && mcache_lookup(tab->hist_cur->h, tab)) {
961 8f3c9af8 2022-01-11 op ui_on_tab_refresh(tab);
962 8f3c9af8 2022-01-11 op ui_on_tab_loaded(tab);
963 d5af38cc 2022-01-10 op return 0;
964 8f3c9af8 2022-01-11 op }
965 d5af38cc 2022-01-10 op
966 4d3785b1 2021-03-09 op for (p = protos; p->schema != NULL; ++p) {
967 ed383cf4 2021-04-22 op if (!strcmp(tab->uri.scheme, p->schema)) {
968 ba782cb8 2021-07-25 op /* patch the port */
969 ba782cb8 2021-07-25 op if (*tab->uri.port == '\0' && p->port != NULL)
970 ba782cb8 2021-07-25 op strlcpy(tab->uri.port, p->port,
971 ba782cb8 2021-07-25 op sizeof(tab->uri.port));
972 ba782cb8 2021-07-25 op
973 52a99c56 2022-01-15 op return p->loadfn(tab, tab->hist_cur->h);
974 4d3785b1 2021-03-09 op }
975 4d3785b1 2021-03-09 op }
976 4d3785b1 2021-03-09 op
977 984245ce 2021-06-23 op TAILQ_FOREACH(proxy, &proxies, proxies) {
978 1c690d0a 2021-08-03 op if (!strcmp(tab->uri.scheme, proxy->match_proto))
979 1c690d0a 2021-08-03 op return load_via_proxy(tab, url, proxy);
980 984245ce 2021-06-23 op }
981 984245ce 2021-06-23 op
982 b38cd1f7 2021-08-03 op return load_page_from_str(tab, err_pages[UNKNOWN_PROTOCOL]);
983 4d3785b1 2021-03-09 op }
984 4d3785b1 2021-03-09 op
985 2ddbda6b 2021-07-17 op /*
986 ea639250 2021-01-02 op * Load url in tab and handle history. If a tab is marked as lazy, only
987 ea639250 2021-01-02 op * prepare the url but don't load it.
988 2ddbda6b 2021-07-17 op */
989 9ad4627d 2021-03-10 op void
990 ed21a9a1 2022-01-11 op load_url(struct tab *tab, const char *url, const char *base, int mode)
991 2051e653 2021-03-13 op {
992 ea639250 2021-01-02 op int lazy = tab->flags & TAB_LAZY;
993 ed21a9a1 2022-01-11 op int nohist = mode & LU_MODE_NOHIST;
994 bc55cdb9 2021-08-12 op
995 fb8dcd1c 2022-01-18 op if (operating && lazy) {
996 fb8dcd1c 2022-01-18 op tab->flags ^= TAB_LAZY;
997 fb8dcd1c 2022-01-18 op lazy = 0;
998 bf5b33f4 2022-01-18 op } else if (tab->hist_cur != NULL)
999 bf5b33f4 2022-01-18 op get_scroll_position(tab, &tab->hist_cur->line_off,
1000 bf5b33f4 2022-01-18 op &tab->hist_cur->current_off);
1001 fb8dcd1c 2022-01-18 op
1002 bc55cdb9 2021-08-12 op if (!nohist && (!lazy || tab->hist_cur == NULL)) {
1003 bf5b33f4 2022-01-18 op if (tab->hist_cur != NULL)
1004 2ddbda6b 2021-07-17 op hist_clear_forward(&tab->hist,
1005 2ddbda6b 2021-07-17 op TAILQ_NEXT(tab->hist_cur, entries));
1006 2ddbda6b 2021-07-17 op
1007 69f32f56 2021-08-16 op if ((tab->hist_cur = calloc(1, sizeof(*tab->hist_cur)))
1008 69f32f56 2021-08-16 op == NULL) {
1009 2ddbda6b 2021-07-17 op event_loopbreak();
1010 2ddbda6b 2021-07-17 op return;
1011 2ddbda6b 2021-07-17 op }
1012 2ddbda6b 2021-07-17 op
1013 2ddbda6b 2021-07-17 op strlcpy(tab->buffer.page.title, url,
1014 2ddbda6b 2021-07-17 op sizeof(tab->buffer.page.title));
1015 2ddbda6b 2021-07-17 op hist_push(&tab->hist, tab->hist_cur);
1016 2ddbda6b 2021-07-17 op
1017 2ddbda6b 2021-07-17 op if (lazy)
1018 2ddbda6b 2021-07-17 op strlcpy(tab->hist_cur->h, url,
1019 2ddbda6b 2021-07-17 op sizeof(tab->hist_cur->h));
1020 2ddbda6b 2021-07-17 op }
1021 2ddbda6b 2021-07-17 op
1022 9eca3d7c 2021-11-02 op if (!lazy)
1023 ed21a9a1 2022-01-11 op do_load_url(tab, url, base, mode);
1024 2051e653 2021-03-13 op }
1025 2051e653 2021-03-13 op
1026 a37d1a1c 2021-08-14 op void
1027 ed21a9a1 2022-01-11 op load_url_in_tab(struct tab *tab, const char *url, const char *base, int mode)
1028 a37d1a1c 2021-08-14 op {
1029 0259f38d 2022-01-11 op if (operating) {
1030 0259f38d 2022-01-11 op autosave_hook();
1031 0259f38d 2022-01-11 op message("Loading %s...", url);
1032 a37d1a1c 2021-08-14 op }
1033 a37d1a1c 2021-08-14 op
1034 ed21a9a1 2022-01-11 op load_url(tab, url, base, mode);
1035 a37d1a1c 2021-08-14 op }
1036 a37d1a1c 2021-08-14 op
1037 2051e653 2021-03-13 op int
1038 2051e653 2021-03-13 op load_previous_page(struct tab *tab)
1039 2051e653 2021-03-13 op {
1040 2051e653 2021-03-13 op struct hist *h;
1041 2051e653 2021-03-13 op
1042 2051e653 2021-03-13 op if ((h = TAILQ_PREV(tab->hist_cur, mhisthead, entries)) == NULL)
1043 2051e653 2021-03-13 op return 0;
1044 2051e653 2021-03-13 op tab->hist_cur = h;
1045 ed21a9a1 2022-01-11 op do_load_url(tab, h->h, NULL, LU_MODE_NONE);
1046 2051e653 2021-03-13 op return 1;
1047 2051e653 2021-03-13 op }
1048 2051e653 2021-03-13 op
1049 2051e653 2021-03-13 op int
1050 2051e653 2021-03-13 op load_next_page(struct tab *tab)
1051 2051e653 2021-03-13 op {
1052 2051e653 2021-03-13 op struct hist *h;
1053 2051e653 2021-03-13 op
1054 2051e653 2021-03-13 op if ((h = TAILQ_NEXT(tab->hist_cur, entries)) == NULL)
1055 2051e653 2021-03-13 op return 0;
1056 2051e653 2021-03-13 op tab->hist_cur = h;
1057 ed21a9a1 2022-01-11 op do_load_url(tab, h->h, NULL, LU_MODE_NONE);
1058 2051e653 2021-03-13 op return 1;
1059 a37d1a1c 2021-08-14 op }
1060 a37d1a1c 2021-08-14 op
1061 2051e653 2021-03-13 op void
1062 740f578b 2021-03-15 op add_to_bookmarks(const char *str)
1063 740f578b 2021-03-15 op {
1064 bc10f6a5 2021-07-12 op ui_send_fs(IMSG_BOOKMARK_PAGE, 0,
1065 bc10f6a5 2021-07-12 op str, strlen(str)+1);
1066 c7107cec 2021-04-01 op }
1067 c6efff96 2021-08-16 op
1068 c6efff96 2021-08-16 op /*
1069 f6d7d047 2021-12-30 op * Given a user-entered URL, apply some heuristics to use it:
1070 c6efff96 2021-08-16 op *
1071 f6d7d047 2021-12-30 op * - if it's a proper url use it
1072 f6d7d047 2021-12-30 op * - if it starts with a `./' or a `/' assume its a file:// url
1073 f6d7d047 2021-12-30 op * - assume it's a gemini:// url
1074 c6efff96 2021-08-16 op *
1075 f6d7d047 2021-12-30 op * `ret' (of which len is the size) will be filled with the resulting
1076 f6d7d047 2021-12-30 op * url.
1077 c6efff96 2021-08-16 op */
1078 c6efff96 2021-08-16 op void
1079 c6efff96 2021-08-16 op humanify_url(const char *raw, char *ret, size_t len)
1080 c6efff96 2021-08-16 op {
1081 c6efff96 2021-08-16 op struct phos_uri uri;
1082 c6efff96 2021-08-16 op char buf[PATH_MAX];
1083 c7107cec 2021-04-01 op
1084 c6efff96 2021-08-16 op if (phos_parse_absolute_uri(raw, &uri)) {
1085 c6efff96 2021-08-16 op strlcpy(ret, raw, len);
1086 c6efff96 2021-08-16 op return;
1087 c6efff96 2021-08-16 op }
1088 c6efff96 2021-08-16 op
1089 c6efff96 2021-08-16 op if (has_prefix(raw, "./")) {
1090 c6efff96 2021-08-16 op strlcpy(ret, "file://", len);
1091 c6efff96 2021-08-16 op getcwd(buf, sizeof(buf));
1092 c6efff96 2021-08-16 op strlcat(ret, buf, len);
1093 c6efff96 2021-08-16 op strlcat(ret, "/", len);
1094 c6efff96 2021-08-16 op strlcat(ret, raw+2, len);
1095 c6efff96 2021-08-16 op return;
1096 c6efff96 2021-08-16 op }
1097 c6efff96 2021-08-16 op
1098 c6efff96 2021-08-16 op if (*raw == '/') {
1099 c6efff96 2021-08-16 op strlcpy(ret, "file://", len);
1100 c6efff96 2021-08-16 op strlcat(ret, raw, len);
1101 c6efff96 2021-08-16 op return;
1102 c6efff96 2021-08-16 op }
1103 c6efff96 2021-08-16 op
1104 c6efff96 2021-08-16 op strlcpy(ret, "gemini://", len);
1105 c6efff96 2021-08-16 op strlcat(ret, raw, len);
1106 c6efff96 2021-08-16 op }
1107 c6efff96 2021-08-16 op
1108 6cc5fcfe 2021-07-08 op static pid_t
1109 6cc5fcfe 2021-07-08 op start_child(enum telescope_process p, const char *argv0, int fd)
1110 6cc5fcfe 2021-07-08 op {
1111 55ccd6d0 2021-09-15 op const char *argv[5];
1112 6cc5fcfe 2021-07-08 op int argc = 0;
1113 6cc5fcfe 2021-07-08 op pid_t pid;
1114 6cc5fcfe 2021-07-08 op
1115 6cc5fcfe 2021-07-08 op switch (pid = fork()) {
1116 6cc5fcfe 2021-07-08 op case -1:
1117 6cc5fcfe 2021-07-08 op die();
1118 6cc5fcfe 2021-07-08 op case 0:
1119 6cc5fcfe 2021-07-08 op break;
1120 6cc5fcfe 2021-07-08 op default:
1121 6cc5fcfe 2021-07-08 op close(fd);
1122 6cc5fcfe 2021-07-08 op return pid;
1123 6cc5fcfe 2021-07-08 op }
1124 6cc5fcfe 2021-07-08 op
1125 6cc5fcfe 2021-07-08 op if (dup2(fd, 3) == -1)
1126 6cc5fcfe 2021-07-08 op err(1, "cannot setup imsg fd");
1127 6cc5fcfe 2021-07-08 op
1128 6cc5fcfe 2021-07-08 op argv[argc++] = argv0;
1129 6cc5fcfe 2021-07-08 op switch (p) {
1130 6cc5fcfe 2021-07-08 op case PROC_UI:
1131 6cc5fcfe 2021-07-08 op errx(1, "Can't start ui process");
1132 6cc5fcfe 2021-07-08 op case PROC_FS:
1133 6cc5fcfe 2021-07-08 op argv[argc++] = "-Tf";
1134 6cc5fcfe 2021-07-08 op break;
1135 6cc5fcfe 2021-07-08 op case PROC_NET:
1136 6cc5fcfe 2021-07-08 op argv[argc++] = "-Tn";
1137 6cc5fcfe 2021-07-08 op break;
1138 6cc5fcfe 2021-07-08 op }
1139 6cc5fcfe 2021-07-08 op
1140 55ccd6d0 2021-09-15 op if (safe_mode)
1141 55ccd6d0 2021-09-15 op argv[argc++] = "-S";
1142 55ccd6d0 2021-09-15 op
1143 6cc5fcfe 2021-07-08 op argv[argc++] = NULL;
1144 6cc5fcfe 2021-07-08 op execvp(argv0, (char *const *)argv);
1145 6cc5fcfe 2021-07-08 op err(1, "execvp(%s)", argv0);
1146 4cf6ba13 2022-02-11 op }
1147 4cf6ba13 2022-02-11 op
1148 4cf6ba13 2022-02-11 op static void
1149 4cf6ba13 2022-02-11 op send_url(const char *url)
1150 4cf6ba13 2022-02-11 op {
1151 4cf6ba13 2022-02-11 op struct sockaddr_un sun;
1152 4cf6ba13 2022-02-11 op struct imsgbuf ibuf;
1153 4cf6ba13 2022-02-11 op int ctl_sock;
1154 4cf6ba13 2022-02-11 op
1155 4cf6ba13 2022-02-11 op if ((ctl_sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
1156 4cf6ba13 2022-02-11 op err(1, "socket");
1157 4cf6ba13 2022-02-11 op
1158 4cf6ba13 2022-02-11 op memset(&sun, 0, sizeof(sun));
1159 4cf6ba13 2022-02-11 op sun.sun_family = AF_UNIX;
1160 4cf6ba13 2022-02-11 op strlcpy(sun.sun_path, ctlsock_path, sizeof(sun.sun_path));
1161 4cf6ba13 2022-02-11 op
1162 4cf6ba13 2022-02-11 op if (connect(ctl_sock, (struct sockaddr *)&sun, sizeof(sun)) == -1)
1163 4cf6ba13 2022-02-11 op err(1, "connect: %s", ctlsock_path);
1164 4cf6ba13 2022-02-11 op
1165 4cf6ba13 2022-02-11 op imsg_init(&ibuf, ctl_sock);
1166 4cf6ba13 2022-02-11 op imsg_compose(&ibuf, IMSG_CTL_OPEN_URL, 0, 0, -1, url,
1167 4cf6ba13 2022-02-11 op strlen(url) + 1);
1168 4cf6ba13 2022-02-11 op imsg_flush(&ibuf);
1169 4cf6ba13 2022-02-11 op close(ctl_sock);
1170 6cc5fcfe 2021-07-08 op }
1171 6cc5fcfe 2021-07-08 op
1172 1fce2e75 2021-08-14 op int
1173 bc10f6a5 2021-07-12 op ui_send_net(int type, uint32_t peerid, const void *data,
1174 bc10f6a5 2021-07-12 op uint16_t datalen)
1175 bc10f6a5 2021-07-12 op {
1176 bc10f6a5 2021-07-12 op return imsg_compose_event(iev_net, type, peerid, 0, -1, data,
1177 bc10f6a5 2021-07-12 op datalen);
1178 bc10f6a5 2021-07-12 op }
1179 bc10f6a5 2021-07-12 op
1180 1fce2e75 2021-08-14 op int
1181 bc10f6a5 2021-07-12 op ui_send_fs(int type, uint32_t peerid, const void *data, uint16_t datalen)
1182 bc10f6a5 2021-07-12 op {
1183 bc10f6a5 2021-07-12 op return imsg_compose_event(iev_fs, type, peerid, 0, -1, data,
1184 bc10f6a5 2021-07-12 op datalen);
1185 bc10f6a5 2021-07-12 op }
1186 bc10f6a5 2021-07-12 op
1187 6cc5fcfe 2021-07-08 op static void __attribute__((noreturn))
1188 6cc5fcfe 2021-07-08 op usage(int r)
1189 d2544989 2021-07-08 op {
1190 335cfaec 2021-09-15 op fprintf(stderr, "USAGE: %s [-ChnSv] [-c config] [url]\n",
1191 dc761924 2021-07-15 op getprogname());
1192 d2544989 2021-07-08 op fprintf(stderr, "version: " PACKAGE " " VERSION "\n");
1193 6cc5fcfe 2021-07-08 op exit(r);
1194 740f578b 2021-03-15 op }
1195 740f578b 2021-03-15 op
1196 5e11c00c 2021-03-02 op int
1197 6cd6a9e1 2021-03-20 op main(int argc, char * const *argv)
1198 5e11c00c 2021-03-02 op {
1199 bc10f6a5 2021-07-12 op struct imsgev net_ibuf, fs_ibuf;
1200 be97d6e6 2021-08-15 op pid_t pid;
1201 4cf6ba13 2022-02-11 op int control_fd;
1202 bc10f6a5 2021-07-12 op int pipe2net[2], pipe2fs[2];
1203 d2544989 2021-07-08 op int ch, configtest = 0, fail = 0;
1204 6cc5fcfe 2021-07-08 op int proc = -1;
1205 5d7c642a 2021-09-15 op int sessionfd = -1;
1206 be97d6e6 2021-08-15 op int status;
1207 6cc5fcfe 2021-07-08 op const char *argv0;
1208 5e11c00c 2021-03-02 op
1209 6cc5fcfe 2021-07-08 op argv0 = argv[0];
1210 6cc5fcfe 2021-07-08 op
1211 5e11c00c 2021-03-02 op signal(SIGCHLD, SIG_IGN);
1212 a0e91173 2021-06-13 op signal(SIGPIPE, SIG_IGN);
1213 5e11c00c 2021-03-02 op
1214 d2544989 2021-07-08 op if (getenv("NO_COLOR") != NULL)
1215 d2544989 2021-07-08 op enable_colors = 0;
1216 d2544989 2021-07-08 op
1217 eb2ed626 2021-10-07 op fs_init();
1218 d2544989 2021-07-08 op
1219 dc761924 2021-07-15 op while ((ch = getopt_long(argc, argv, opts, longopts, NULL)) != -1) {
1220 d2544989 2021-07-08 op switch (ch) {
1221 1853ee50 2021-07-15 op case 'C':
1222 1853ee50 2021-07-15 op exit(ui_print_colors());
1223 d2544989 2021-07-08 op case 'c':
1224 d2544989 2021-07-08 op fail = 1;
1225 eb2ed626 2021-10-07 op strlcpy(config_path, optarg, sizeof(config_path));
1226 d2544989 2021-07-08 op break;
1227 d2544989 2021-07-08 op case 'n':
1228 d2544989 2021-07-08 op configtest = 1;
1229 d2544989 2021-07-08 op break;
1230 d2544989 2021-07-08 op case 'h':
1231 6cc5fcfe 2021-07-08 op usage(0);
1232 d0971653 2021-09-15 op case 'S':
1233 d0971653 2021-09-15 op safe_mode = 1;
1234 d0971653 2021-09-15 op break;
1235 6cc5fcfe 2021-07-08 op case 'T':
1236 6cc5fcfe 2021-07-08 op switch (*optarg) {
1237 6cc5fcfe 2021-07-08 op case 'f':
1238 6cc5fcfe 2021-07-08 op proc = PROC_FS;
1239 6cc5fcfe 2021-07-08 op break;
1240 6cc5fcfe 2021-07-08 op case 'n':
1241 6cc5fcfe 2021-07-08 op proc = PROC_NET;
1242 6cc5fcfe 2021-07-08 op break;
1243 6cc5fcfe 2021-07-08 op default:
1244 6cc5fcfe 2021-07-08 op errx(1, "invalid process spec %c",
1245 6cc5fcfe 2021-07-08 op *optarg);
1246 6cc5fcfe 2021-07-08 op }
1247 6cc5fcfe 2021-07-08 op break;
1248 dc761924 2021-07-15 op case 'v':
1249 dc761924 2021-07-15 op printf("%s %s\n", PACKAGE_NAME, PACKAGE_VERSION);
1250 dc761924 2021-07-15 op exit(0);
1251 dc761924 2021-07-15 op break;
1252 d2544989 2021-07-08 op default:
1253 6cc5fcfe 2021-07-08 op usage(1);
1254 d2544989 2021-07-08 op }
1255 d2544989 2021-07-08 op }
1256 d2544989 2021-07-08 op
1257 d2544989 2021-07-08 op argc -= optind;
1258 d2544989 2021-07-08 op argv += optind;
1259 d2544989 2021-07-08 op
1260 6cc5fcfe 2021-07-08 op if (proc != -1) {
1261 6cc5fcfe 2021-07-08 op if (argc > 0)
1262 6cc5fcfe 2021-07-08 op usage(1);
1263 6cc5fcfe 2021-07-08 op else if (proc == PROC_FS)
1264 6cc5fcfe 2021-07-08 op return fs_main();
1265 6cc5fcfe 2021-07-08 op else if (proc == PROC_NET)
1266 f45bd2e3 2021-07-24 op return net_main();
1267 6cc5fcfe 2021-07-08 op else
1268 6cc5fcfe 2021-07-08 op usage(1);
1269 6cc5fcfe 2021-07-08 op }
1270 6cc5fcfe 2021-07-08 op
1271 d2544989 2021-07-08 op if (argc != 0) {
1272 d2544989 2021-07-08 op has_url = 1;
1273 c6efff96 2021-08-16 op humanify_url(argv[0], url, sizeof(url));
1274 d2544989 2021-07-08 op }
1275 d2544989 2021-07-08 op
1276 d5bdf203 2021-07-08 op /* setup keys before reading the config */
1277 d5bdf203 2021-07-08 op TAILQ_INIT(&global_map.m);
1278 d5bdf203 2021-07-08 op global_map.unhandled_input = global_key_unbound;
1279 d5bdf203 2021-07-08 op TAILQ_INIT(&minibuffer_map.m);
1280 d5bdf203 2021-07-08 op
1281 d2544989 2021-07-08 op config_init();
1282 eb2ed626 2021-10-07 op parseconfig(config_path, fail);
1283 69f32f56 2021-08-16 op if (configtest) {
1284 d2544989 2021-07-08 op puts("config OK");
1285 d2544989 2021-07-08 op exit(0);
1286 d2544989 2021-07-08 op }
1287 d2544989 2021-07-08 op
1288 112b9046 2021-08-29 op if (download_path == NULL &&
1289 112b9046 2021-08-29 op (download_path = strdup("/tmp/")) == NULL)
1290 112b9046 2021-08-29 op errx(1, "strdup");
1291 112b9046 2021-08-29 op
1292 4cf6ba13 2022-02-11 op if (!safe_mode && (sessionfd = lock_session()) == -1) {
1293 4cf6ba13 2022-02-11 op if (has_url) {
1294 4cf6ba13 2022-02-11 op send_url(url);
1295 4cf6ba13 2022-02-11 op exit(0);
1296 4cf6ba13 2022-02-11 op }
1297 4cf6ba13 2022-02-11 op
1298 d0fd368a 2021-07-15 op errx(1, "can't lock session, is another instance of "
1299 d0fd368a 2021-07-15 op "telescope already running?");
1300 4cf6ba13 2022-02-11 op }
1301 d0fd368a 2021-07-15 op
1302 6cc5fcfe 2021-07-08 op /* Start children. */
1303 bc10f6a5 2021-07-12 op if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe2fs) == -1)
1304 5e11c00c 2021-03-02 op err(1, "socketpair");
1305 bc10f6a5 2021-07-12 op start_child(PROC_FS, argv0, pipe2fs[1]);
1306 bc10f6a5 2021-07-12 op imsg_init(&fs_ibuf.ibuf, pipe2fs[0]);
1307 bc10f6a5 2021-07-12 op iev_fs = &fs_ibuf;
1308 bc10f6a5 2021-07-12 op iev_fs->handler = handle_dispatch_imsg;
1309 5e11c00c 2021-03-02 op
1310 bc10f6a5 2021-07-12 op if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe2net) == -1)
1311 35e1f40a 2021-03-14 op err(1, "socketpair");
1312 bc10f6a5 2021-07-12 op start_child(PROC_NET, argv0, pipe2net[1]);
1313 bc10f6a5 2021-07-12 op imsg_init(&net_ibuf.ibuf, pipe2net[0]);
1314 bc10f6a5 2021-07-12 op iev_net = &net_ibuf;
1315 bc10f6a5 2021-07-12 op iev_net->handler = handle_dispatch_imsg;
1316 5e11c00c 2021-03-02 op
1317 37d429b0 2021-04-01 op setproctitle("ui");
1318 35e1f40a 2021-03-14 op
1319 eb722b50 2022-01-03 op /* initialize tofu store */
1320 3768e50f 2021-04-25 op tofu_init(&certs, 5, offsetof(struct tofu_entry, domain));
1321 cbcc75fb 2021-03-17 op
1322 5e11c00c 2021-03-02 op event_init();
1323 5e11c00c 2021-03-02 op
1324 4cf6ba13 2022-02-11 op if (!safe_mode) {
1325 4cf6ba13 2022-02-11 op if ((control_fd = control_init(ctlsock_path)) == -1)
1326 4cf6ba13 2022-02-11 op err(1, "control_init %s", ctlsock_path);
1327 4cf6ba13 2022-02-11 op control_listen(control_fd);
1328 4cf6ba13 2022-02-11 op }
1329 4cf6ba13 2022-02-11 op
1330 d5af38cc 2022-01-10 op /* initialize the in-memory cache store */
1331 d5af38cc 2022-01-10 op mcache_init();
1332 d5af38cc 2022-01-10 op
1333 ecfbb1d4 2021-08-14 op /* Setup event handler for the autosave */
1334 1fce2e75 2021-08-14 op autosave_init();
1335 ecfbb1d4 2021-08-14 op
1336 bc10f6a5 2021-07-12 op /* Setup event handlers for pipes to fs/net */
1337 bc10f6a5 2021-07-12 op iev_fs->events = EV_READ;
1338 bc10f6a5 2021-07-12 op event_set(&iev_fs->ev, iev_fs->ibuf.fd, iev_fs->events,
1339 bc10f6a5 2021-07-12 op iev_fs->handler, iev_fs);
1340 bc10f6a5 2021-07-12 op event_add(&iev_fs->ev, NULL);
1341 5e11c00c 2021-03-02 op
1342 bc10f6a5 2021-07-12 op iev_net->events = EV_READ;
1343 bc10f6a5 2021-07-12 op event_set(&iev_net->ev, iev_net->ibuf.fd, iev_net->events,
1344 bc10f6a5 2021-07-12 op iev_net->handler, iev_net);
1345 bc10f6a5 2021-07-12 op event_add(&iev_net->ev, NULL);
1346 d2544989 2021-07-08 op
1347 bb28f1c2 2021-12-30 op if (ui_init()) {
1348 941b3761 2021-03-18 op sandbox_ui_process();
1349 50f03682 2022-01-03 op ui_send_fs(IMSG_INIT, 0, NULL, 0);
1350 bb28f1c2 2021-12-30 op event_dispatch();
1351 941b3761 2021-03-18 op ui_end();
1352 941b3761 2021-03-18 op }
1353 5e11c00c 2021-03-02 op
1354 bc10f6a5 2021-07-12 op ui_send_fs(IMSG_QUIT, 0, NULL, 0);
1355 bc10f6a5 2021-07-12 op ui_send_net(IMSG_QUIT, 0, NULL, 0);
1356 3e5fd7be 2021-07-13 op imsg_flush(&iev_fs->ibuf);
1357 a9925134 2021-07-15 op imsg_flush(&iev_net->ibuf);
1358 be97d6e6 2021-08-15 op
1359 be97d6e6 2021-08-15 op /* wait for children to terminate */
1360 be97d6e6 2021-08-15 op do {
1361 be97d6e6 2021-08-15 op pid = wait(&status);
1362 be97d6e6 2021-08-15 op if (pid == -1) {
1363 be97d6e6 2021-08-15 op if (errno != EINTR && errno != ECHILD)
1364 be97d6e6 2021-08-15 op err(1, "wait");
1365 be97d6e6 2021-08-15 op } else if (WIFSIGNALED(status))
1366 be97d6e6 2021-08-15 op warnx("child terminated; signal %d", WTERMSIG(status));
1367 be97d6e6 2021-08-15 op } while (pid != -1 || (pid == -1 && errno == EINTR));
1368 5e11c00c 2021-03-02 op
1369 5d7c642a 2021-09-15 op if (!safe_mode && close(sessionfd) == -1)
1370 5d7c642a 2021-09-15 op err(1, "close(sessionfd = %d)", sessionfd);
1371 d0fd368a 2021-07-15 op
1372 5e11c00c 2021-03-02 op return 0;
1373 5e11c00c 2021-03-02 op }