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 fcd99a0d 2021-11-05 op enqueue_download(tab->id, path);
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 de2a69bb 2021-05-17 op } else {
510 fcd99a0d 2021-11-05 op d->fd = imsg->fd;
511 fcd99a0d 2021-11-05 op ui_send_net(IMSG_PROCEED, d->id, NULL, 0);
512 bb28f1c2 2021-12-30 op }
513 bb28f1c2 2021-12-30 op }
514 bb28f1c2 2021-12-30 op
515 bb28f1c2 2021-12-30 op static void
516 bb28f1c2 2021-12-30 op handle_imsg_session(struct imsg *imsg, size_t datalen)
517 bb28f1c2 2021-12-30 op {
518 bb28f1c2 2021-12-30 op static struct tab *curr;
519 1040cc7f 2021-01-02 op static struct tab *tab;
520 bb28f1c2 2021-12-30 op struct session_tab st;
521 1040cc7f 2021-01-02 op struct session_tab_hist sth;
522 1040cc7f 2021-01-02 op struct hist *h;
523 bb28f1c2 2021-12-30 op int first_time;
524 bb28f1c2 2021-12-30 op
525 bb28f1c2 2021-12-30 op /*
526 bb28f1c2 2021-12-30 op * The fs process tried to send tabs after it has announced
527 bb28f1c2 2021-12-30 op * that he's done. Something fishy is going on, better die.
528 bb28f1c2 2021-12-30 op */
529 bb28f1c2 2021-12-30 op if (operating)
530 bb28f1c2 2021-12-30 op die();
531 bb28f1c2 2021-12-30 op
532 92ce02e2 2021-01-02 op switch (imsg->hdr.type) {
533 92ce02e2 2021-01-02 op case IMSG_SESSION_TAB:
534 92ce02e2 2021-01-02 op if (datalen != sizeof(st))
535 92ce02e2 2021-01-02 op die();
536 92ce02e2 2021-01-02 op
537 92ce02e2 2021-01-02 op memcpy(&st, imsg->data, sizeof(st));
538 92ce02e2 2021-01-02 op if ((tab = new_tab(st.uri, NULL, NULL)) == NULL)
539 92ce02e2 2021-01-02 op die();
540 e795e935 2022-01-18 op tab->hist_cur->line_off = st.top_line;
541 e795e935 2022-01-18 op tab->hist_cur->current_off = st.current_line;
542 92ce02e2 2021-01-02 op strlcpy(tab->buffer.page.title, st.title,
543 92ce02e2 2021-01-02 op sizeof(tab->buffer.page.title));
544 92ce02e2 2021-01-02 op if (st.flags & TAB_CURRENT)
545 92ce02e2 2021-01-02 op curr = tab;
546 6c74799d 2022-01-05 op if (st.flags & TAB_KILLED)
547 05de8ac3 2022-01-06 op kill_tab(tab, 1);
548 92ce02e2 2021-01-02 op break;
549 92ce02e2 2021-01-02 op
550 1040cc7f 2021-01-02 op case IMSG_SESSION_TAB_HIST:
551 1040cc7f 2021-01-02 op if (tab == NULL || datalen != sizeof(sth))
552 1040cc7f 2021-01-02 op die();
553 1040cc7f 2021-01-02 op
554 1040cc7f 2021-01-02 op memcpy(&sth, imsg->data, sizeof(sth));
555 1040cc7f 2021-01-02 op if (sth.uri[sizeof(sth.uri)-1] != '\0')
556 1040cc7f 2021-01-02 op die();
557 1040cc7f 2021-01-02 op
558 1040cc7f 2021-01-02 op if ((h = calloc(1, sizeof(*h))) == NULL)
559 1040cc7f 2021-01-02 op die();
560 1040cc7f 2021-01-02 op strlcpy(h->h, sth.uri, sizeof(h->h));
561 1040cc7f 2021-01-02 op
562 1040cc7f 2021-01-02 op if (sth.future)
563 1040cc7f 2021-01-02 op hist_push(&tab->hist, h);
564 1040cc7f 2021-01-02 op else
565 1040cc7f 2021-01-02 op hist_add_before(&tab->hist, tab->hist_cur, h);
566 1040cc7f 2021-01-02 op break;
567 1040cc7f 2021-01-02 op
568 92ce02e2 2021-01-02 op case IMSG_SESSION_END:
569 bb28f1c2 2021-12-30 op if (datalen != sizeof(first_time))
570 bb28f1c2 2021-12-30 op die();
571 bb28f1c2 2021-12-30 op memcpy(&first_time, imsg->data, sizeof(first_time));
572 bb28f1c2 2021-12-30 op if (first_time) {
573 bb28f1c2 2021-12-30 op new_tab("about:new", NULL, NULL);
574 bb28f1c2 2021-12-30 op curr = new_tab("about:help", NULL, NULL);
575 bb28f1c2 2021-12-30 op }
576 bb28f1c2 2021-12-30 op
577 bb28f1c2 2021-12-30 op operating = 1;
578 bb28f1c2 2021-12-30 op if (curr != NULL)
579 bb28f1c2 2021-12-30 op switch_to_tab(curr);
580 bb28f1c2 2021-12-30 op if (has_url || TAILQ_EMPTY(&tabshead))
581 bb28f1c2 2021-12-30 op new_tab(url, NULL, NULL);
582 bb28f1c2 2021-12-30 op ui_main_loop();
583 92ce02e2 2021-01-02 op break;
584 bb28f1c2 2021-12-30 op
585 92ce02e2 2021-01-02 op default:
586 bb28f1c2 2021-12-30 op die();
587 92ce02e2 2021-01-02 op }
588 de2a69bb 2021-05-17 op }
589 de2a69bb 2021-05-17 op
590 de2a69bb 2021-05-17 op static void
591 9e97090d 2022-02-26 op handle_imsg_history(struct imsg *imsg, size_t datalen)
592 9e97090d 2022-02-26 op {
593 9e97090d 2022-02-26 op struct histitem hi;
594 9e97090d 2022-02-26 op
595 9e97090d 2022-02-26 op /*
596 9e97090d 2022-02-26 op * The fs process tried to send history item after it
597 9e97090d 2022-02-26 op * has announced that it's done. Something fishy is
598 9e97090d 2022-02-26 op * going on, better die
599 9e97090d 2022-02-26 op */
600 9e97090d 2022-02-26 op if (operating)
601 9e97090d 2022-02-26 op die();
602 9e97090d 2022-02-26 op
603 9e97090d 2022-02-26 op switch (imsg->hdr.type) {
604 9e97090d 2022-02-26 op case IMSG_HIST_ITEM:
605 9e97090d 2022-02-26 op if (datalen != sizeof(hi))
606 9e97090d 2022-02-26 op die();
607 9e97090d 2022-02-26 op
608 9e97090d 2022-02-26 op memcpy(&hi, imsg->data, sizeof(hi));
609 9e97090d 2022-02-26 op history_push(&hi);
610 9e97090d 2022-02-26 op break;
611 9e97090d 2022-02-26 op
612 9e97090d 2022-02-26 op case IMSG_HIST_END:
613 9e97090d 2022-02-26 op history_sort();
614 9e97090d 2022-02-26 op break;
615 9e97090d 2022-02-26 op
616 9e97090d 2022-02-26 op default:
617 9e97090d 2022-02-26 op die();
618 9e97090d 2022-02-26 op }
619 9e97090d 2022-02-26 op }
620 9e97090d 2022-02-26 op
621 9e97090d 2022-02-26 op static void
622 5e11c00c 2021-03-02 op handle_imsg_buf(struct imsg *imsg, size_t datalen)
623 5e11c00c 2021-03-02 op {
624 fcd99a0d 2021-11-05 op struct tab *tab = NULL;
625 fcd99a0d 2021-11-05 op struct download *d = NULL;
626 5e11c00c 2021-03-02 op
627 fcd99a0d 2021-11-05 op if ((tab = tab_by_id(imsg->hdr.peerid)) == NULL &&
628 fcd99a0d 2021-11-05 op (d = download_by_id(imsg->hdr.peerid)) == NULL)
629 5d6c2088 2021-07-19 op return;
630 5e11c00c 2021-03-02 op
631 fcd99a0d 2021-11-05 op if (tab != NULL) {
632 8c1b0837 2021-07-25 op if (!parser_parse(tab, imsg->data, datalen))
633 de2a69bb 2021-05-17 op die();
634 fcd99a0d 2021-11-05 op ui_on_tab_refresh(tab);
635 de2a69bb 2021-05-17 op } else {
636 fcd99a0d 2021-11-05 op d->bytes += datalen;
637 fcd99a0d 2021-11-05 op write(d->fd, imsg->data, datalen);
638 fcd99a0d 2021-11-05 op ui_on_download_refresh();
639 de2a69bb 2021-05-17 op }
640 5e11c00c 2021-03-02 op }
641 5e11c00c 2021-03-02 op
642 5e11c00c 2021-03-02 op static void
643 5e11c00c 2021-03-02 op handle_imsg_eof(struct imsg *imsg, size_t datalen)
644 5e11c00c 2021-03-02 op {
645 fcd99a0d 2021-11-05 op struct tab *tab = NULL;
646 fcd99a0d 2021-11-05 op struct download *d = NULL;
647 a5c3e03d 2021-03-02 op
648 fcd99a0d 2021-11-05 op if ((tab = tab_by_id(imsg->hdr.peerid)) == NULL &&
649 fcd99a0d 2021-11-05 op (d = download_by_id(imsg->hdr.peerid)) == NULL)
650 f6243012 2021-07-19 op return;
651 de2a69bb 2021-05-17 op
652 fcd99a0d 2021-11-05 op if (tab != NULL) {
653 8c1b0837 2021-07-25 op if (!parser_free(tab))
654 de2a69bb 2021-05-17 op die();
655 dc8e7bf6 2022-01-11 op if (has_prefix(tab->hist_cur->h, "gemini://") ||
656 dc8e7bf6 2022-01-11 op has_prefix(tab->hist_cur->h, "gopher://"))
657 8f3c9af8 2022-01-11 op mcache_tab(tab);
658 fcd99a0d 2021-11-05 op ui_on_tab_refresh(tab);
659 fcd99a0d 2021-11-05 op ui_on_tab_loaded(tab);
660 de2a69bb 2021-05-17 op } else {
661 fcd99a0d 2021-11-05 op close(d->fd);
662 fcd99a0d 2021-11-05 op d->fd = -1;
663 fcd99a0d 2021-11-05 op ui_on_download_refresh();
664 de2a69bb 2021-05-17 op }
665 5e11c00c 2021-03-02 op }
666 5e11c00c 2021-03-02 op
667 5e11c00c 2021-03-02 op static void
668 eb722b50 2022-01-03 op handle_imsg_tofu(struct imsg *imsg, size_t datalen)
669 eb722b50 2022-01-03 op {
670 eb722b50 2022-01-03 op struct tofu_entry *e;
671 eb722b50 2022-01-03 op
672 eb722b50 2022-01-03 op if (operating)
673 eb722b50 2022-01-03 op die();
674 eb722b50 2022-01-03 op
675 eb722b50 2022-01-03 op if ((e = calloc(1, sizeof(*e))) == NULL)
676 eb722b50 2022-01-03 op die();
677 eb722b50 2022-01-03 op
678 eb722b50 2022-01-03 op if (datalen != sizeof(*e))
679 eb722b50 2022-01-03 op die();
680 eb722b50 2022-01-03 op memcpy(e, imsg->data, sizeof(*e));
681 eb722b50 2022-01-03 op if (e->domain[sizeof(e->domain)-1] != '\0' ||
682 eb722b50 2022-01-03 op e->hash[sizeof(e->hash)-1] != '\0')
683 eb722b50 2022-01-03 op die();
684 eb722b50 2022-01-03 op tofu_add(&certs, e);
685 eb722b50 2022-01-03 op }
686 eb722b50 2022-01-03 op
687 eb722b50 2022-01-03 op static void
688 740f578b 2021-03-15 op handle_imsg_bookmark_ok(struct imsg *imsg, size_t datalen)
689 740f578b 2021-03-15 op {
690 740f578b 2021-03-15 op int res;
691 740f578b 2021-03-15 op
692 740f578b 2021-03-15 op if (datalen != sizeof(res))
693 740f578b 2021-03-15 op die();
694 740f578b 2021-03-15 op
695 740f578b 2021-03-15 op memcpy(&res, imsg->data, sizeof(res));
696 740f578b 2021-03-15 op if (res == 0)
697 7f963c41 2021-06-20 op message("Added to bookmarks!");
698 740f578b 2021-03-15 op else
699 7f963c41 2021-06-20 op message("Failed to add to bookmarks: %s",
700 740f578b 2021-03-15 op strerror(res));
701 740f578b 2021-03-15 op }
702 740f578b 2021-03-15 op
703 740f578b 2021-03-15 op static void
704 3a227e9a 2021-03-18 op handle_imsg_save_cert_ok(struct imsg *imsg, size_t datalen)
705 3a227e9a 2021-03-18 op {
706 3a227e9a 2021-03-18 op int res;
707 3a227e9a 2021-03-18 op
708 3a227e9a 2021-03-18 op if (datalen != sizeof(res))
709 3a227e9a 2021-03-18 op die();
710 3a227e9a 2021-03-18 op memcpy(&res, imsg->data, datalen);
711 3a227e9a 2021-03-18 op if (res != 0)
712 7f963c41 2021-06-20 op message("Failed to save the cert for: %s",
713 3a227e9a 2021-03-18 op strerror(res));
714 288fd238 2021-04-25 op }
715 288fd238 2021-04-25 op
716 288fd238 2021-04-25 op static void
717 288fd238 2021-04-25 op handle_imsg_update_cert_ok(struct imsg *imsg, size_t datalen)
718 288fd238 2021-04-25 op {
719 288fd238 2021-04-25 op int res;
720 288fd238 2021-04-25 op
721 288fd238 2021-04-25 op if (datalen != sizeof(res))
722 288fd238 2021-04-25 op die();
723 288fd238 2021-04-25 op memcpy(&res, imsg->data, datalen);
724 288fd238 2021-04-25 op if (!res)
725 7f963c41 2021-06-20 op message("Failed to update the certificate");
726 3a227e9a 2021-03-18 op }
727 3a227e9a 2021-03-18 op
728 3a227e9a 2021-03-18 op static void
729 1304bbdd 2021-03-15 op handle_dispatch_imsg(int fd, short ev, void *d)
730 5e11c00c 2021-03-02 op {
731 bc10f6a5 2021-07-12 op struct imsgev *iev = d;
732 ea080950 2021-07-20 op
733 ea080950 2021-07-20 op if (dispatch_imsg(iev, ev, handlers, sizeof(handlers)) == -1)
734 ea080950 2021-07-20 op err(1, "connection closed");
735 0972d8b2 2021-03-02 op }
736 0972d8b2 2021-03-02 op
737 1c690d0a 2021-08-03 op static int
738 4d3785b1 2021-03-09 op load_about_url(struct tab *tab, const char *url)
739 0972d8b2 2021-03-02 op {
740 7b2aac84 2021-08-13 op tab->trust = TS_UNKNOWN;
741 8c1b0837 2021-07-25 op parser_init(tab, gemtext_initparser);
742 c7586aab 2021-08-13 op return make_fs_request(tab, IMSG_GET, url);
743 4d3785b1 2021-03-09 op }
744 0972d8b2 2021-03-02 op
745 1c690d0a 2021-08-03 op static int
746 0b1053d2 2021-08-13 op load_file_url(struct tab *tab, const char *url)
747 0b1053d2 2021-08-13 op {
748 0b1053d2 2021-08-13 op tab->trust = TS_UNKNOWN;
749 0b1053d2 2021-08-13 op return make_fs_request(tab, IMSG_GET_FILE, tab->uri.path);
750 0b1053d2 2021-08-13 op }
751 0b1053d2 2021-08-13 op
752 0b1053d2 2021-08-13 op static int
753 5fb52fc0 2021-07-25 op load_finger_url(struct tab *tab, const char *url)
754 5fb52fc0 2021-07-25 op {
755 5fb52fc0 2021-07-25 op struct get_req req;
756 5fb52fc0 2021-07-25 op size_t len;
757 5fb52fc0 2021-07-25 op char *at;
758 5fb52fc0 2021-07-25 op
759 5fb52fc0 2021-07-25 op memset(&req, 0, sizeof(req));
760 ba782cb8 2021-07-25 op strlcpy(req.port, tab->uri.port, sizeof(req.port));
761 5fb52fc0 2021-07-25 op
762 5fb52fc0 2021-07-25 op /*
763 5fb52fc0 2021-07-25 op * Sometimes the finger url have the user as path component
764 5fb52fc0 2021-07-25 op * (e.g. finger://thelambdalab.xyz/plugd), sometimes as
765 5fb52fc0 2021-07-25 op * userinfo (e.g. finger://cobradile@finger.farm).
766 5fb52fc0 2021-07-25 op */
767 5fb52fc0 2021-07-25 op if ((at = strchr(tab->uri.host, '@')) != NULL) {
768 5fb52fc0 2021-07-25 op len = at - tab->uri.host;
769 5fb52fc0 2021-07-25 op memcpy(req.req, tab->uri.host, len);
770 5fb52fc0 2021-07-25 op
771 5fb52fc0 2021-07-25 op if (len >= sizeof(req.req))
772 5fb52fc0 2021-07-25 op die();
773 5fb52fc0 2021-07-25 op req.req[len] = '\0';
774 5fb52fc0 2021-07-25 op
775 5fb52fc0 2021-07-25 op strlcpy(req.host, at+1, sizeof(req.host));
776 5fb52fc0 2021-07-25 op } else {
777 5fb52fc0 2021-07-25 op strlcpy(req.host, tab->uri.host, sizeof(req.host));
778 5fb52fc0 2021-07-25 op
779 5fb52fc0 2021-07-25 op /* +1 to skip the initial `/' */
780 5fb52fc0 2021-07-25 op strlcpy(req.req, tab->uri.path+1, sizeof(req.req));
781 5fb52fc0 2021-07-25 op }
782 5fb52fc0 2021-07-25 op strlcat(req.req, "\r\n", sizeof(req.req));
783 5fb52fc0 2021-07-25 op
784 8c1b0837 2021-07-25 op parser_init(tab, textplain_initparser);
785 1c690d0a 2021-08-03 op return make_request(tab, &req, PROTO_FINGER, NULL);
786 5fb52fc0 2021-07-25 op }
787 5fb52fc0 2021-07-25 op
788 1c690d0a 2021-08-03 op static int
789 4d3785b1 2021-03-09 op load_gemini_url(struct tab *tab, const char *url)
790 4d3785b1 2021-03-09 op {
791 984245ce 2021-06-23 op struct get_req req;
792 2eef3403 2021-04-22 op
793 984245ce 2021-06-23 op memset(&req, 0, sizeof(req));
794 984245ce 2021-06-23 op strlcpy(req.host, tab->uri.host, sizeof(req.host));
795 f94c7ac1 2021-11-24 op strlcpy(req.port, tab->uri.port, sizeof(req.port));
796 984245ce 2021-06-23 op
797 1c690d0a 2021-08-03 op return make_request(tab, &req, PROTO_GEMINI, tab->hist_cur->h);
798 ca190c99 2021-08-14 op }
799 ca190c99 2021-08-14 op
800 ca190c99 2021-08-14 op static inline const char *
801 ca190c99 2021-08-14 op gopher_skip_selector(const char *path, int *ret_type)
802 ca190c99 2021-08-14 op {
803 ca190c99 2021-08-14 op *ret_type = 0;
804 ca190c99 2021-08-14 op
805 ca190c99 2021-08-14 op if (!strcmp(path, "/") || *path == '\0') {
806 ca190c99 2021-08-14 op *ret_type = '1';
807 ca190c99 2021-08-14 op return path;
808 ca190c99 2021-08-14 op }
809 ca190c99 2021-08-14 op
810 ca190c99 2021-08-14 op if (*path != '/')
811 ca190c99 2021-08-14 op return path;
812 ca190c99 2021-08-14 op path++;
813 ca190c99 2021-08-14 op
814 ca190c99 2021-08-14 op switch (*ret_type = *path) {
815 ca190c99 2021-08-14 op case '0':
816 ca190c99 2021-08-14 op case '1':
817 ca190c99 2021-08-14 op case '7':
818 ca190c99 2021-08-14 op break;
819 ca190c99 2021-08-14 op
820 ca190c99 2021-08-14 op default:
821 ca190c99 2021-08-14 op *ret_type = 0;
822 ca190c99 2021-08-14 op path -= 1;
823 ca190c99 2021-08-14 op return path;
824 ca190c99 2021-08-14 op }
825 ca190c99 2021-08-14 op
826 95bb01e2 2021-08-28 op return ++path;
827 1663bf1e 2021-07-25 op }
828 1663bf1e 2021-07-25 op
829 1c690d0a 2021-08-03 op static int
830 1663bf1e 2021-07-25 op load_gopher_url(struct tab *tab, const char *url)
831 1663bf1e 2021-07-25 op {
832 81839fee 2021-07-25 op struct get_req req;
833 ca190c99 2021-08-14 op int type;
834 81839fee 2021-07-25 op const char *path;
835 1663bf1e 2021-07-25 op
836 1663bf1e 2021-07-25 op memset(&req, 0, sizeof(req));
837 1663bf1e 2021-07-25 op strlcpy(req.host, tab->uri.host, sizeof(req.host));
838 f94c7ac1 2021-11-24 op strlcpy(req.port, tab->uri.port, sizeof(req.port));
839 1663bf1e 2021-07-25 op
840 ca190c99 2021-08-14 op path = gopher_skip_selector(tab->uri.path, &type);
841 ca190c99 2021-08-14 op switch (type) {
842 ca190c99 2021-08-14 op case '0':
843 8c1b0837 2021-07-25 op parser_init(tab, textplain_initparser);
844 ca190c99 2021-08-14 op break;
845 ca190c99 2021-08-14 op case '1':
846 ca190c99 2021-08-14 op parser_init(tab, gophermap_initparser);
847 ca190c99 2021-08-14 op break;
848 ca190c99 2021-08-14 op case '7':
849 acf9defe 2022-02-08 op free(tab->last_input_url);
850 acf9defe 2022-02-08 op tab->last_input_url = strdup(url);
851 acf9defe 2022-02-08 op if (tab->last_input_url == NULL)
852 acf9defe 2022-02-08 op die();
853 693188cb 2022-02-07 op ui_require_input(tab, 0, ir_select_gopher);
854 9a28e7e5 2021-08-03 op return load_page_from_str(tab, err_pages[10]);
855 ca190c99 2021-08-14 op default:
856 ca190c99 2021-08-14 op return load_page_from_str(tab, "Unknown gopher selector");
857 81839fee 2021-07-25 op }
858 81839fee 2021-07-25 op
859 9a28e7e5 2021-08-03 op strlcpy(req.req, path, sizeof(req.req));
860 9a28e7e5 2021-08-03 op if (*tab->uri.query != '\0') {
861 9a28e7e5 2021-08-03 op strlcat(req.req, "?", sizeof(req.req));
862 9a28e7e5 2021-08-03 op strlcat(req.req, tab->uri.query, sizeof(req.req));
863 9a28e7e5 2021-08-03 op }
864 767da05c 2021-08-13 op strlcat(req.req, "\r\n", sizeof(req.req));
865 9a28e7e5 2021-08-03 op
866 767da05c 2021-08-13 op return make_request(tab, &req, PROTO_GOPHER, NULL);
867 0972d8b2 2021-03-02 op }
868 0972d8b2 2021-03-02 op
869 1c690d0a 2021-08-03 op static int
870 984245ce 2021-06-23 op load_via_proxy(struct tab *tab, const char *url, struct proxy *p)
871 984245ce 2021-06-23 op {
872 984245ce 2021-06-23 op struct get_req req;
873 984245ce 2021-06-23 op
874 984245ce 2021-06-23 op memset(&req, 0, sizeof(req));
875 984245ce 2021-06-23 op strlcpy(req.host, p->host, sizeof(req.host));
876 f94c7ac1 2021-11-24 op strlcpy(req.port, p->port, sizeof(req.port));
877 984245ce 2021-06-23 op
878 cf1195ce 2021-07-25 op tab->proxy = p;
879 984245ce 2021-06-23 op
880 1c690d0a 2021-08-03 op return make_request(tab, &req, p->proto, tab->hist_cur->h);
881 cf1195ce 2021-07-25 op }
882 984245ce 2021-06-23 op
883 1c690d0a 2021-08-03 op static int
884 cf1195ce 2021-07-25 op make_request(struct tab *tab, struct get_req *req, int proto, const char *r)
885 cf1195ce 2021-07-25 op {
886 cf1195ce 2021-07-25 op stop_tab(tab);
887 cf1195ce 2021-07-25 op tab->id = tab_new_id();
888 cf1195ce 2021-07-25 op req->proto = proto;
889 cf1195ce 2021-07-25 op
890 cf1195ce 2021-07-25 op if (r != NULL) {
891 cf1195ce 2021-07-25 op strlcpy(req->req, r, sizeof(req->req));
892 cf1195ce 2021-07-25 op strlcat(req->req, "\r\n", sizeof(req->req));
893 cf1195ce 2021-07-25 op }
894 cf1195ce 2021-07-25 op
895 5df02e0d 2022-01-11 op start_loading_anim(tab);
896 cf1195ce 2021-07-25 op ui_send_net(IMSG_GET_RAW, tab->id, req, sizeof(*req));
897 1c690d0a 2021-08-03 op
898 1c690d0a 2021-08-03 op /*
899 1c690d0a 2021-08-03 op * So the various load_*_url can `return make_request` and
900 1c690d0a 2021-08-03 op * do_load_url is happy.
901 1c690d0a 2021-08-03 op */
902 1c690d0a 2021-08-03 op return 1;
903 984245ce 2021-06-23 op }
904 984245ce 2021-06-23 op
905 c7586aab 2021-08-13 op static int
906 c7586aab 2021-08-13 op make_fs_request(struct tab *tab, int type, const char *r)
907 c7586aab 2021-08-13 op {
908 c7586aab 2021-08-13 op stop_tab(tab);
909 c7586aab 2021-08-13 op tab->id = tab_new_id();
910 c7586aab 2021-08-13 op
911 c7586aab 2021-08-13 op ui_send_fs(type, tab->id, r, strlen(r)+1);
912 c7586aab 2021-08-13 op
913 c7586aab 2021-08-13 op /*
914 c7586aab 2021-08-13 op * So load_{about,file}_url can `return make_fs_request` and
915 c7586aab 2021-08-13 op * do_load_url is happy.
916 c7586aab 2021-08-13 op */
917 c7586aab 2021-08-13 op return 1;
918 c7586aab 2021-08-13 op }
919 c7586aab 2021-08-13 op
920 9a28e7e5 2021-08-03 op void
921 9a28e7e5 2021-08-03 op gopher_send_search_req(struct tab *tab, const char *text)
922 9a28e7e5 2021-08-03 op {
923 9a28e7e5 2021-08-03 op struct get_req req;
924 1b0e6efc 2021-08-08 op
925 9a28e7e5 2021-08-03 op memset(&req, 0, sizeof(req));
926 9a28e7e5 2021-08-03 op strlcpy(req.host, tab->uri.host, sizeof(req.host));
927 f94c7ac1 2021-11-24 op strlcpy(req.port, tab->uri.port, sizeof(req.port));
928 9a28e7e5 2021-08-03 op
929 9a28e7e5 2021-08-03 op /* +2 to skip /7 */
930 9a28e7e5 2021-08-03 op strlcpy(req.req, tab->uri.path+2, sizeof(req.req));
931 9a28e7e5 2021-08-03 op if (*tab->uri.query != '\0') {
932 9a28e7e5 2021-08-03 op strlcat(req.req, "?", sizeof(req.req));
933 9a28e7e5 2021-08-03 op strlcat(req.req, tab->uri.query, sizeof(req.req));
934 9a28e7e5 2021-08-03 op }
935 9a28e7e5 2021-08-03 op
936 9a28e7e5 2021-08-03 op strlcat(req.req, "\t", sizeof(req.req));
937 9a28e7e5 2021-08-03 op strlcat(req.req, text, sizeof(req.req));
938 9a28e7e5 2021-08-03 op strlcat(req.req, "\r\n", sizeof(req.req));
939 9a28e7e5 2021-08-03 op
940 9a28e7e5 2021-08-03 op erase_buffer(&tab->buffer);
941 9a28e7e5 2021-08-03 op parser_init(tab, gophermap_initparser);
942 9a28e7e5 2021-08-03 op
943 9a28e7e5 2021-08-03 op make_request(tab, &req, PROTO_GOPHER, NULL);
944 9a28e7e5 2021-08-03 op }
945 9a28e7e5 2021-08-03 op
946 1bcb8303 2022-01-19 op int
947 1bcb8303 2022-01-19 op load_page_from_str(struct tab *tab, const char *page)
948 1bcb8303 2022-01-19 op {
949 1bcb8303 2022-01-19 op parser_init(tab, gemtext_initparser);
950 1bcb8303 2022-01-19 op if (!tab->buffer.page.parse(&tab->buffer.page, page, strlen(page)))
951 1bcb8303 2022-01-19 op abort();
952 1bcb8303 2022-01-19 op if (!tab->buffer.page.free(&tab->buffer.page))
953 1bcb8303 2022-01-19 op abort();
954 1bcb8303 2022-01-19 op ui_on_tab_refresh(tab);
955 1bcb8303 2022-01-19 op ui_on_tab_loaded(tab);
956 1bcb8303 2022-01-19 op return 0;
957 1bcb8303 2022-01-19 op }
958 1bcb8303 2022-01-19 op
959 adea4eec 2021-07-16 op /*
960 adea4eec 2021-07-16 op * Effectively load the given url in the given tab. Return 1 when
961 adea4eec 2021-07-16 op * loading the page asynchronously, and thus when an erase_buffer can
962 adea4eec 2021-07-16 op * be done right after this function return, or 0 when loading the
963 9eca3d7c 2021-11-02 op * page synchronously.
964 adea4eec 2021-07-16 op */
965 adea4eec 2021-07-16 op static int
966 ed21a9a1 2022-01-11 op do_load_url(struct tab *tab, const char *url, const char *base, int mode)
967 4d3785b1 2021-03-09 op {
968 0d877486 2022-02-24 op const struct proto *p;
969 31f1a758 2021-04-22 op struct phos_uri uri;
970 984245ce 2021-06-23 op struct proxy *proxy;
971 8f3c9af8 2022-01-11 op int nocache = mode & LU_MODE_NOCACHE;
972 31f1a758 2021-04-22 op char *t;
973 7943bb81 2021-07-10 op
974 7943bb81 2021-07-10 op tab->proxy = NULL;
975 10346511 2021-03-17 op tab->trust = TS_UNKNOWN;
976 10346511 2021-03-17 op
977 bd3a3a95 2021-07-20 op if (base == NULL)
978 bd3a3a95 2021-07-20 op memcpy(&uri, &tab->uri, sizeof(tab->uri));
979 bd3a3a95 2021-07-20 op else
980 bd3a3a95 2021-07-20 op phos_parse_absolute_uri(base, &uri);
981 bd3a3a95 2021-07-20 op
982 31f1a758 2021-04-22 op if (!phos_resolve_uri_from_str(&uri, url, &tab->uri)) {
983 902fc2c2 2021-07-25 op if (asprintf(&t, "#error loading %s\n>%s\n",
984 31f1a758 2021-04-22 op url, "Can't parse the URI") == -1)
985 31f1a758 2021-04-22 op die();
986 31f1a758 2021-04-22 op strlcpy(tab->hist_cur->h, url, sizeof(tab->hist_cur->h));
987 31f1a758 2021-04-22 op load_page_from_str(tab, t);
988 31f1a758 2021-04-22 op free(t);
989 adea4eec 2021-07-16 op return 0;
990 31f1a758 2021-04-22 op }
991 31f1a758 2021-04-22 op
992 31f1a758 2021-04-22 op phos_serialize_uri(&tab->uri, tab->hist_cur->h,
993 31f1a758 2021-04-22 op sizeof(tab->hist_cur->h));
994 31f1a758 2021-04-22 op
995 8f3c9af8 2022-01-11 op if (!nocache && mcache_lookup(tab->hist_cur->h, tab)) {
996 8f3c9af8 2022-01-11 op ui_on_tab_refresh(tab);
997 8f3c9af8 2022-01-11 op ui_on_tab_loaded(tab);
998 d5af38cc 2022-01-10 op return 0;
999 8f3c9af8 2022-01-11 op }
1000 d5af38cc 2022-01-10 op
1001 4d3785b1 2021-03-09 op for (p = protos; p->schema != NULL; ++p) {
1002 ed383cf4 2021-04-22 op if (!strcmp(tab->uri.scheme, p->schema)) {
1003 ba782cb8 2021-07-25 op /* patch the port */
1004 ba782cb8 2021-07-25 op if (*tab->uri.port == '\0' && p->port != NULL)
1005 ba782cb8 2021-07-25 op strlcpy(tab->uri.port, p->port,
1006 ba782cb8 2021-07-25 op sizeof(tab->uri.port));
1007 ba782cb8 2021-07-25 op
1008 52a99c56 2022-01-15 op return p->loadfn(tab, tab->hist_cur->h);
1009 4d3785b1 2021-03-09 op }
1010 4d3785b1 2021-03-09 op }
1011 4d3785b1 2021-03-09 op
1012 984245ce 2021-06-23 op TAILQ_FOREACH(proxy, &proxies, proxies) {
1013 1c690d0a 2021-08-03 op if (!strcmp(tab->uri.scheme, proxy->match_proto))
1014 1c690d0a 2021-08-03 op return load_via_proxy(tab, url, proxy);
1015 984245ce 2021-06-23 op }
1016 984245ce 2021-06-23 op
1017 b38cd1f7 2021-08-03 op return load_page_from_str(tab, err_pages[UNKNOWN_PROTOCOL]);
1018 4d3785b1 2021-03-09 op }
1019 4d3785b1 2021-03-09 op
1020 2ddbda6b 2021-07-17 op /*
1021 ea639250 2021-01-02 op * Load url in tab and handle history. If a tab is marked as lazy, only
1022 ea639250 2021-01-02 op * prepare the url but don't load it.
1023 2ddbda6b 2021-07-17 op */
1024 9ad4627d 2021-03-10 op void
1025 ed21a9a1 2022-01-11 op load_url(struct tab *tab, const char *url, const char *base, int mode)
1026 2051e653 2021-03-13 op {
1027 ea639250 2021-01-02 op int lazy = tab->flags & TAB_LAZY;
1028 ed21a9a1 2022-01-11 op int nohist = mode & LU_MODE_NOHIST;
1029 bc55cdb9 2021-08-12 op
1030 fb8dcd1c 2022-01-18 op if (operating && lazy) {
1031 fb8dcd1c 2022-01-18 op tab->flags ^= TAB_LAZY;
1032 fb8dcd1c 2022-01-18 op lazy = 0;
1033 bf5b33f4 2022-01-18 op } else if (tab->hist_cur != NULL)
1034 bf5b33f4 2022-01-18 op get_scroll_position(tab, &tab->hist_cur->line_off,
1035 bf5b33f4 2022-01-18 op &tab->hist_cur->current_off);
1036 fb8dcd1c 2022-01-18 op
1037 bc55cdb9 2021-08-12 op if (!nohist && (!lazy || tab->hist_cur == NULL)) {
1038 bf5b33f4 2022-01-18 op if (tab->hist_cur != NULL)
1039 2ddbda6b 2021-07-17 op hist_clear_forward(&tab->hist,
1040 2ddbda6b 2021-07-17 op TAILQ_NEXT(tab->hist_cur, entries));
1041 2ddbda6b 2021-07-17 op
1042 69f32f56 2021-08-16 op if ((tab->hist_cur = calloc(1, sizeof(*tab->hist_cur)))
1043 69f32f56 2021-08-16 op == NULL) {
1044 2ddbda6b 2021-07-17 op event_loopbreak();
1045 2ddbda6b 2021-07-17 op return;
1046 2ddbda6b 2021-07-17 op }
1047 2ddbda6b 2021-07-17 op
1048 2ddbda6b 2021-07-17 op strlcpy(tab->buffer.page.title, url,
1049 2ddbda6b 2021-07-17 op sizeof(tab->buffer.page.title));
1050 2ddbda6b 2021-07-17 op hist_push(&tab->hist, tab->hist_cur);
1051 2ddbda6b 2021-07-17 op
1052 2ddbda6b 2021-07-17 op if (lazy)
1053 2ddbda6b 2021-07-17 op strlcpy(tab->hist_cur->h, url,
1054 2ddbda6b 2021-07-17 op sizeof(tab->hist_cur->h));
1055 2ddbda6b 2021-07-17 op }
1056 2ddbda6b 2021-07-17 op
1057 9eca3d7c 2021-11-02 op if (!lazy)
1058 ed21a9a1 2022-01-11 op do_load_url(tab, url, base, mode);
1059 2051e653 2021-03-13 op }
1060 2051e653 2021-03-13 op
1061 a37d1a1c 2021-08-14 op void
1062 ed21a9a1 2022-01-11 op load_url_in_tab(struct tab *tab, const char *url, const char *base, int mode)
1063 a37d1a1c 2021-08-14 op {
1064 0259f38d 2022-01-11 op if (operating) {
1065 0259f38d 2022-01-11 op autosave_hook();
1066 0259f38d 2022-01-11 op message("Loading %s...", url);
1067 a37d1a1c 2021-08-14 op }
1068 a37d1a1c 2021-08-14 op
1069 ed21a9a1 2022-01-11 op load_url(tab, url, base, mode);
1070 a37d1a1c 2021-08-14 op }
1071 a37d1a1c 2021-08-14 op
1072 2051e653 2021-03-13 op int
1073 2051e653 2021-03-13 op load_previous_page(struct tab *tab)
1074 2051e653 2021-03-13 op {
1075 2051e653 2021-03-13 op struct hist *h;
1076 2051e653 2021-03-13 op
1077 2051e653 2021-03-13 op if ((h = TAILQ_PREV(tab->hist_cur, mhisthead, entries)) == NULL)
1078 2051e653 2021-03-13 op return 0;
1079 2051e653 2021-03-13 op tab->hist_cur = h;
1080 ed21a9a1 2022-01-11 op do_load_url(tab, h->h, NULL, LU_MODE_NONE);
1081 2051e653 2021-03-13 op return 1;
1082 2051e653 2021-03-13 op }
1083 2051e653 2021-03-13 op
1084 2051e653 2021-03-13 op int
1085 2051e653 2021-03-13 op load_next_page(struct tab *tab)
1086 2051e653 2021-03-13 op {
1087 2051e653 2021-03-13 op struct hist *h;
1088 2051e653 2021-03-13 op
1089 2051e653 2021-03-13 op if ((h = TAILQ_NEXT(tab->hist_cur, entries)) == NULL)
1090 2051e653 2021-03-13 op return 0;
1091 2051e653 2021-03-13 op tab->hist_cur = h;
1092 ed21a9a1 2022-01-11 op do_load_url(tab, h->h, NULL, LU_MODE_NONE);
1093 2051e653 2021-03-13 op return 1;
1094 a37d1a1c 2021-08-14 op }
1095 a37d1a1c 2021-08-14 op
1096 2051e653 2021-03-13 op void
1097 740f578b 2021-03-15 op add_to_bookmarks(const char *str)
1098 740f578b 2021-03-15 op {
1099 bc10f6a5 2021-07-12 op ui_send_fs(IMSG_BOOKMARK_PAGE, 0,
1100 bc10f6a5 2021-07-12 op str, strlen(str)+1);
1101 c7107cec 2021-04-01 op }
1102 c6efff96 2021-08-16 op
1103 c6efff96 2021-08-16 op /*
1104 f6d7d047 2021-12-30 op * Given a user-entered URL, apply some heuristics to use it:
1105 c6efff96 2021-08-16 op *
1106 f6d7d047 2021-12-30 op * - if it's a proper url use it
1107 f6d7d047 2021-12-30 op * - if it starts with a `./' or a `/' assume its a file:// url
1108 f6d7d047 2021-12-30 op * - assume it's a gemini:// url
1109 c6efff96 2021-08-16 op *
1110 f6d7d047 2021-12-30 op * `ret' (of which len is the size) will be filled with the resulting
1111 f6d7d047 2021-12-30 op * url.
1112 c6efff96 2021-08-16 op */
1113 c6efff96 2021-08-16 op void
1114 c6efff96 2021-08-16 op humanify_url(const char *raw, char *ret, size_t len)
1115 c6efff96 2021-08-16 op {
1116 c6efff96 2021-08-16 op struct phos_uri uri;
1117 c6efff96 2021-08-16 op char buf[PATH_MAX];
1118 c7107cec 2021-04-01 op
1119 c6efff96 2021-08-16 op if (phos_parse_absolute_uri(raw, &uri)) {
1120 c6efff96 2021-08-16 op strlcpy(ret, raw, len);
1121 c6efff96 2021-08-16 op return;
1122 c6efff96 2021-08-16 op }
1123 c6efff96 2021-08-16 op
1124 c6efff96 2021-08-16 op if (has_prefix(raw, "./")) {
1125 c6efff96 2021-08-16 op strlcpy(ret, "file://", len);
1126 c6efff96 2021-08-16 op getcwd(buf, sizeof(buf));
1127 c6efff96 2021-08-16 op strlcat(ret, buf, len);
1128 c6efff96 2021-08-16 op strlcat(ret, "/", len);
1129 c6efff96 2021-08-16 op strlcat(ret, raw+2, len);
1130 c6efff96 2021-08-16 op return;
1131 c6efff96 2021-08-16 op }
1132 c6efff96 2021-08-16 op
1133 c6efff96 2021-08-16 op if (*raw == '/') {
1134 c6efff96 2021-08-16 op strlcpy(ret, "file://", len);
1135 c6efff96 2021-08-16 op strlcat(ret, raw, len);
1136 c6efff96 2021-08-16 op return;
1137 c6efff96 2021-08-16 op }
1138 c6efff96 2021-08-16 op
1139 c6efff96 2021-08-16 op strlcpy(ret, "gemini://", len);
1140 c6efff96 2021-08-16 op strlcat(ret, raw, len);
1141 c6efff96 2021-08-16 op }
1142 c6efff96 2021-08-16 op
1143 6cc5fcfe 2021-07-08 op static pid_t
1144 6cc5fcfe 2021-07-08 op start_child(enum telescope_process p, const char *argv0, int fd)
1145 6cc5fcfe 2021-07-08 op {
1146 55ccd6d0 2021-09-15 op const char *argv[5];
1147 6cc5fcfe 2021-07-08 op int argc = 0;
1148 6cc5fcfe 2021-07-08 op pid_t pid;
1149 6cc5fcfe 2021-07-08 op
1150 6cc5fcfe 2021-07-08 op switch (pid = fork()) {
1151 6cc5fcfe 2021-07-08 op case -1:
1152 6cc5fcfe 2021-07-08 op die();
1153 6cc5fcfe 2021-07-08 op case 0:
1154 6cc5fcfe 2021-07-08 op break;
1155 6cc5fcfe 2021-07-08 op default:
1156 6cc5fcfe 2021-07-08 op close(fd);
1157 6cc5fcfe 2021-07-08 op return pid;
1158 6cc5fcfe 2021-07-08 op }
1159 6cc5fcfe 2021-07-08 op
1160 6cc5fcfe 2021-07-08 op if (dup2(fd, 3) == -1)
1161 6cc5fcfe 2021-07-08 op err(1, "cannot setup imsg fd");
1162 6cc5fcfe 2021-07-08 op
1163 6cc5fcfe 2021-07-08 op argv[argc++] = argv0;
1164 6cc5fcfe 2021-07-08 op switch (p) {
1165 6cc5fcfe 2021-07-08 op case PROC_UI:
1166 6cc5fcfe 2021-07-08 op errx(1, "Can't start ui process");
1167 6cc5fcfe 2021-07-08 op case PROC_FS:
1168 6cc5fcfe 2021-07-08 op argv[argc++] = "-Tf";
1169 6cc5fcfe 2021-07-08 op break;
1170 6cc5fcfe 2021-07-08 op case PROC_NET:
1171 6cc5fcfe 2021-07-08 op argv[argc++] = "-Tn";
1172 6cc5fcfe 2021-07-08 op break;
1173 6cc5fcfe 2021-07-08 op }
1174 6cc5fcfe 2021-07-08 op
1175 55ccd6d0 2021-09-15 op if (safe_mode)
1176 55ccd6d0 2021-09-15 op argv[argc++] = "-S";
1177 55ccd6d0 2021-09-15 op
1178 6cc5fcfe 2021-07-08 op argv[argc++] = NULL;
1179 6cc5fcfe 2021-07-08 op execvp(argv0, (char *const *)argv);
1180 6cc5fcfe 2021-07-08 op err(1, "execvp(%s)", argv0);
1181 4cf6ba13 2022-02-11 op }
1182 4cf6ba13 2022-02-11 op
1183 4cf6ba13 2022-02-11 op static void
1184 4cf6ba13 2022-02-11 op send_url(const char *url)
1185 4cf6ba13 2022-02-11 op {
1186 4cf6ba13 2022-02-11 op struct sockaddr_un sun;
1187 4cf6ba13 2022-02-11 op struct imsgbuf ibuf;
1188 4cf6ba13 2022-02-11 op int ctl_sock;
1189 4cf6ba13 2022-02-11 op
1190 4cf6ba13 2022-02-11 op if ((ctl_sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
1191 4cf6ba13 2022-02-11 op err(1, "socket");
1192 4cf6ba13 2022-02-11 op
1193 4cf6ba13 2022-02-11 op memset(&sun, 0, sizeof(sun));
1194 4cf6ba13 2022-02-11 op sun.sun_family = AF_UNIX;
1195 4cf6ba13 2022-02-11 op strlcpy(sun.sun_path, ctlsock_path, sizeof(sun.sun_path));
1196 4cf6ba13 2022-02-11 op
1197 4cf6ba13 2022-02-11 op if (connect(ctl_sock, (struct sockaddr *)&sun, sizeof(sun)) == -1)
1198 4cf6ba13 2022-02-11 op err(1, "connect: %s", ctlsock_path);
1199 4cf6ba13 2022-02-11 op
1200 4cf6ba13 2022-02-11 op imsg_init(&ibuf, ctl_sock);
1201 4cf6ba13 2022-02-11 op imsg_compose(&ibuf, IMSG_CTL_OPEN_URL, 0, 0, -1, url,
1202 4cf6ba13 2022-02-11 op strlen(url) + 1);
1203 4cf6ba13 2022-02-11 op imsg_flush(&ibuf);
1204 4cf6ba13 2022-02-11 op close(ctl_sock);
1205 6cc5fcfe 2021-07-08 op }
1206 6cc5fcfe 2021-07-08 op
1207 1fce2e75 2021-08-14 op int
1208 bc10f6a5 2021-07-12 op ui_send_net(int type, uint32_t peerid, const void *data,
1209 bc10f6a5 2021-07-12 op uint16_t datalen)
1210 bc10f6a5 2021-07-12 op {
1211 bc10f6a5 2021-07-12 op return imsg_compose_event(iev_net, type, peerid, 0, -1, data,
1212 bc10f6a5 2021-07-12 op datalen);
1213 bc10f6a5 2021-07-12 op }
1214 bc10f6a5 2021-07-12 op
1215 1fce2e75 2021-08-14 op int
1216 bc10f6a5 2021-07-12 op ui_send_fs(int type, uint32_t peerid, const void *data, uint16_t datalen)
1217 bc10f6a5 2021-07-12 op {
1218 bc10f6a5 2021-07-12 op return imsg_compose_event(iev_fs, type, peerid, 0, -1, data,
1219 bc10f6a5 2021-07-12 op datalen);
1220 bc10f6a5 2021-07-12 op }
1221 bc10f6a5 2021-07-12 op
1222 6cc5fcfe 2021-07-08 op static void __attribute__((noreturn))
1223 6cc5fcfe 2021-07-08 op usage(int r)
1224 d2544989 2021-07-08 op {
1225 335cfaec 2021-09-15 op fprintf(stderr, "USAGE: %s [-ChnSv] [-c config] [url]\n",
1226 dc761924 2021-07-15 op getprogname());
1227 d2544989 2021-07-08 op fprintf(stderr, "version: " PACKAGE " " VERSION "\n");
1228 6cc5fcfe 2021-07-08 op exit(r);
1229 740f578b 2021-03-15 op }
1230 740f578b 2021-03-15 op
1231 5e11c00c 2021-03-02 op int
1232 6cd6a9e1 2021-03-20 op main(int argc, char * const *argv)
1233 5e11c00c 2021-03-02 op {
1234 bc10f6a5 2021-07-12 op struct imsgev net_ibuf, fs_ibuf;
1235 be97d6e6 2021-08-15 op pid_t pid;
1236 4cf6ba13 2022-02-11 op int control_fd;
1237 bc10f6a5 2021-07-12 op int pipe2net[2], pipe2fs[2];
1238 d2544989 2021-07-08 op int ch, configtest = 0, fail = 0;
1239 6cc5fcfe 2021-07-08 op int proc = -1;
1240 5d7c642a 2021-09-15 op int sessionfd = -1;
1241 be97d6e6 2021-08-15 op int status;
1242 6cc5fcfe 2021-07-08 op const char *argv0;
1243 5e11c00c 2021-03-02 op
1244 6cc5fcfe 2021-07-08 op argv0 = argv[0];
1245 6cc5fcfe 2021-07-08 op
1246 5e11c00c 2021-03-02 op signal(SIGCHLD, SIG_IGN);
1247 a0e91173 2021-06-13 op signal(SIGPIPE, SIG_IGN);
1248 5e11c00c 2021-03-02 op
1249 d2544989 2021-07-08 op if (getenv("NO_COLOR") != NULL)
1250 d2544989 2021-07-08 op enable_colors = 0;
1251 d2544989 2021-07-08 op
1252 eb2ed626 2021-10-07 op fs_init();
1253 d2544989 2021-07-08 op
1254 dc761924 2021-07-15 op while ((ch = getopt_long(argc, argv, opts, longopts, NULL)) != -1) {
1255 d2544989 2021-07-08 op switch (ch) {
1256 1853ee50 2021-07-15 op case 'C':
1257 1853ee50 2021-07-15 op exit(ui_print_colors());
1258 d2544989 2021-07-08 op case 'c':
1259 d2544989 2021-07-08 op fail = 1;
1260 eb2ed626 2021-10-07 op strlcpy(config_path, optarg, sizeof(config_path));
1261 d2544989 2021-07-08 op break;
1262 d2544989 2021-07-08 op case 'n':
1263 d2544989 2021-07-08 op configtest = 1;
1264 d2544989 2021-07-08 op break;
1265 d2544989 2021-07-08 op case 'h':
1266 6cc5fcfe 2021-07-08 op usage(0);
1267 d0971653 2021-09-15 op case 'S':
1268 d0971653 2021-09-15 op safe_mode = 1;
1269 d0971653 2021-09-15 op break;
1270 6cc5fcfe 2021-07-08 op case 'T':
1271 6cc5fcfe 2021-07-08 op switch (*optarg) {
1272 6cc5fcfe 2021-07-08 op case 'f':
1273 6cc5fcfe 2021-07-08 op proc = PROC_FS;
1274 6cc5fcfe 2021-07-08 op break;
1275 6cc5fcfe 2021-07-08 op case 'n':
1276 6cc5fcfe 2021-07-08 op proc = PROC_NET;
1277 6cc5fcfe 2021-07-08 op break;
1278 6cc5fcfe 2021-07-08 op default:
1279 6cc5fcfe 2021-07-08 op errx(1, "invalid process spec %c",
1280 6cc5fcfe 2021-07-08 op *optarg);
1281 6cc5fcfe 2021-07-08 op }
1282 6cc5fcfe 2021-07-08 op break;
1283 dc761924 2021-07-15 op case 'v':
1284 dc761924 2021-07-15 op printf("%s %s\n", PACKAGE_NAME, PACKAGE_VERSION);
1285 dc761924 2021-07-15 op exit(0);
1286 dc761924 2021-07-15 op break;
1287 d2544989 2021-07-08 op default:
1288 6cc5fcfe 2021-07-08 op usage(1);
1289 d2544989 2021-07-08 op }
1290 d2544989 2021-07-08 op }
1291 d2544989 2021-07-08 op
1292 d2544989 2021-07-08 op argc -= optind;
1293 d2544989 2021-07-08 op argv += optind;
1294 d2544989 2021-07-08 op
1295 6cc5fcfe 2021-07-08 op if (proc != -1) {
1296 6cc5fcfe 2021-07-08 op if (argc > 0)
1297 6cc5fcfe 2021-07-08 op usage(1);
1298 6cc5fcfe 2021-07-08 op else if (proc == PROC_FS)
1299 6cc5fcfe 2021-07-08 op return fs_main();
1300 6cc5fcfe 2021-07-08 op else if (proc == PROC_NET)
1301 f45bd2e3 2021-07-24 op return net_main();
1302 6cc5fcfe 2021-07-08 op else
1303 6cc5fcfe 2021-07-08 op usage(1);
1304 6cc5fcfe 2021-07-08 op }
1305 6cc5fcfe 2021-07-08 op
1306 d2544989 2021-07-08 op if (argc != 0) {
1307 d2544989 2021-07-08 op has_url = 1;
1308 c6efff96 2021-08-16 op humanify_url(argv[0], url, sizeof(url));
1309 d2544989 2021-07-08 op }
1310 d2544989 2021-07-08 op
1311 d5bdf203 2021-07-08 op /* setup keys before reading the config */
1312 d5bdf203 2021-07-08 op TAILQ_INIT(&global_map.m);
1313 d5bdf203 2021-07-08 op global_map.unhandled_input = global_key_unbound;
1314 d5bdf203 2021-07-08 op TAILQ_INIT(&minibuffer_map.m);
1315 d5bdf203 2021-07-08 op
1316 d2544989 2021-07-08 op config_init();
1317 eb2ed626 2021-10-07 op parseconfig(config_path, fail);
1318 69f32f56 2021-08-16 op if (configtest) {
1319 d2544989 2021-07-08 op puts("config OK");
1320 d2544989 2021-07-08 op exit(0);
1321 d2544989 2021-07-08 op }
1322 d2544989 2021-07-08 op
1323 112b9046 2021-08-29 op if (download_path == NULL &&
1324 112b9046 2021-08-29 op (download_path = strdup("/tmp/")) == NULL)
1325 112b9046 2021-08-29 op errx(1, "strdup");
1326 112b9046 2021-08-29 op
1327 4cf6ba13 2022-02-11 op if (!safe_mode && (sessionfd = lock_session()) == -1) {
1328 4cf6ba13 2022-02-11 op if (has_url) {
1329 4cf6ba13 2022-02-11 op send_url(url);
1330 4cf6ba13 2022-02-11 op exit(0);
1331 4cf6ba13 2022-02-11 op }
1332 4cf6ba13 2022-02-11 op
1333 d0fd368a 2021-07-15 op errx(1, "can't lock session, is another instance of "
1334 d0fd368a 2021-07-15 op "telescope already running?");
1335 4cf6ba13 2022-02-11 op }
1336 d0fd368a 2021-07-15 op
1337 6cc5fcfe 2021-07-08 op /* Start children. */
1338 bc10f6a5 2021-07-12 op if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe2fs) == -1)
1339 5e11c00c 2021-03-02 op err(1, "socketpair");
1340 bc10f6a5 2021-07-12 op start_child(PROC_FS, argv0, pipe2fs[1]);
1341 bc10f6a5 2021-07-12 op imsg_init(&fs_ibuf.ibuf, pipe2fs[0]);
1342 bc10f6a5 2021-07-12 op iev_fs = &fs_ibuf;
1343 bc10f6a5 2021-07-12 op iev_fs->handler = handle_dispatch_imsg;
1344 5e11c00c 2021-03-02 op
1345 bc10f6a5 2021-07-12 op if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe2net) == -1)
1346 35e1f40a 2021-03-14 op err(1, "socketpair");
1347 bc10f6a5 2021-07-12 op start_child(PROC_NET, argv0, pipe2net[1]);
1348 bc10f6a5 2021-07-12 op imsg_init(&net_ibuf.ibuf, pipe2net[0]);
1349 bc10f6a5 2021-07-12 op iev_net = &net_ibuf;
1350 bc10f6a5 2021-07-12 op iev_net->handler = handle_dispatch_imsg;
1351 5e11c00c 2021-03-02 op
1352 37d429b0 2021-04-01 op setproctitle("ui");
1353 35e1f40a 2021-03-14 op
1354 eb722b50 2022-01-03 op /* initialize tofu store */
1355 3768e50f 2021-04-25 op tofu_init(&certs, 5, offsetof(struct tofu_entry, domain));
1356 cbcc75fb 2021-03-17 op
1357 5e11c00c 2021-03-02 op event_init();
1358 5e11c00c 2021-03-02 op
1359 4cf6ba13 2022-02-11 op if (!safe_mode) {
1360 4cf6ba13 2022-02-11 op if ((control_fd = control_init(ctlsock_path)) == -1)
1361 4cf6ba13 2022-02-11 op err(1, "control_init %s", ctlsock_path);
1362 4cf6ba13 2022-02-11 op control_listen(control_fd);
1363 4cf6ba13 2022-02-11 op }
1364 4cf6ba13 2022-02-11 op
1365 d5af38cc 2022-01-10 op /* initialize the in-memory cache store */
1366 d5af38cc 2022-01-10 op mcache_init();
1367 d5af38cc 2022-01-10 op
1368 ecfbb1d4 2021-08-14 op /* Setup event handler for the autosave */
1369 1fce2e75 2021-08-14 op autosave_init();
1370 ecfbb1d4 2021-08-14 op
1371 bc10f6a5 2021-07-12 op /* Setup event handlers for pipes to fs/net */
1372 bc10f6a5 2021-07-12 op iev_fs->events = EV_READ;
1373 bc10f6a5 2021-07-12 op event_set(&iev_fs->ev, iev_fs->ibuf.fd, iev_fs->events,
1374 bc10f6a5 2021-07-12 op iev_fs->handler, iev_fs);
1375 bc10f6a5 2021-07-12 op event_add(&iev_fs->ev, NULL);
1376 5e11c00c 2021-03-02 op
1377 bc10f6a5 2021-07-12 op iev_net->events = EV_READ;
1378 bc10f6a5 2021-07-12 op event_set(&iev_net->ev, iev_net->ibuf.fd, iev_net->events,
1379 bc10f6a5 2021-07-12 op iev_net->handler, iev_net);
1380 bc10f6a5 2021-07-12 op event_add(&iev_net->ev, NULL);
1381 d2544989 2021-07-08 op
1382 bb28f1c2 2021-12-30 op if (ui_init()) {
1383 941b3761 2021-03-18 op sandbox_ui_process();
1384 50f03682 2022-01-03 op ui_send_fs(IMSG_INIT, 0, NULL, 0);
1385 bb28f1c2 2021-12-30 op event_dispatch();
1386 941b3761 2021-03-18 op ui_end();
1387 941b3761 2021-03-18 op }
1388 5e11c00c 2021-03-02 op
1389 bc10f6a5 2021-07-12 op ui_send_fs(IMSG_QUIT, 0, NULL, 0);
1390 bc10f6a5 2021-07-12 op ui_send_net(IMSG_QUIT, 0, NULL, 0);
1391 3e5fd7be 2021-07-13 op imsg_flush(&iev_fs->ibuf);
1392 a9925134 2021-07-15 op imsg_flush(&iev_net->ibuf);
1393 be97d6e6 2021-08-15 op
1394 be97d6e6 2021-08-15 op /* wait for children to terminate */
1395 be97d6e6 2021-08-15 op do {
1396 be97d6e6 2021-08-15 op pid = wait(&status);
1397 be97d6e6 2021-08-15 op if (pid == -1) {
1398 be97d6e6 2021-08-15 op if (errno != EINTR && errno != ECHILD)
1399 be97d6e6 2021-08-15 op err(1, "wait");
1400 be97d6e6 2021-08-15 op } else if (WIFSIGNALED(status))
1401 be97d6e6 2021-08-15 op warnx("child terminated; signal %d", WTERMSIG(status));
1402 be97d6e6 2021-08-15 op } while (pid != -1 || (pid == -1 && errno == EINTR));
1403 5e11c00c 2021-03-02 op
1404 5d7c642a 2021-09-15 op if (!safe_mode && close(sessionfd) == -1)
1405 5d7c642a 2021-09-15 op err(1, "close(sessionfd = %d)", sessionfd);
1406 d0fd368a 2021-07-15 op
1407 5e11c00c 2021-03-02 op return 0;
1408 5e11c00c 2021-03-02 op }