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