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 4540067e 2021-07-18 op
17 5e11c00c 2021-03-02 op #include <sys/socket.h>
18 5e11c00c 2021-03-02 op
19 5e11c00c 2021-03-02 op #include <errno.h>
20 dc761924 2021-07-15 op #include <getopt.h>
21 d2544989 2021-07-08 op #include <limits.h>
22 5e11c00c 2021-03-02 op #include <signal.h>
23 5e11c00c 2021-03-02 op #include <stdio.h>
24 5e11c00c 2021-03-02 op #include <stdlib.h>
25 5e11c00c 2021-03-02 op #include <string.h>
26 5e11c00c 2021-03-02 op #include <unistd.h>
27 5e11c00c 2021-03-02 op
28 e659558c 2021-07-13 op #include "defaults.h"
29 395b9f4e 2021-07-12 op #include "parser.h"
30 395b9f4e 2021-07-12 op #include "telescope.h"
31 d1a0f2a3 2021-07-12 op #include "ui.h"
32 dc761924 2021-07-15 op
33 dc761924 2021-07-15 op static struct option longopts[] = {
34 1853ee50 2021-07-15 op {"colors", no_argument, NULL, 'c'},
35 dc761924 2021-07-15 op {"help", no_argument, NULL, 'h'},
36 dc761924 2021-07-15 op {"version", no_argument, NULL, 'v'},
37 dc761924 2021-07-15 op {NULL, 0, NULL, 0},
38 dc761924 2021-07-15 op };
39 dc761924 2021-07-15 op
40 1853ee50 2021-07-15 op static const char *opts = "Cc:hnT:v";
41 395b9f4e 2021-07-12 op
42 bc10f6a5 2021-07-12 op static struct imsgev *iev_fs, *iev_net;
43 bc10f6a5 2021-07-12 op
44 f6a429eb 2021-07-10 op struct tabshead tabshead = TAILQ_HEAD_INITIALIZER(tabshead);
45 f6a429eb 2021-07-10 op struct proxylist proxies = TAILQ_HEAD_INITIALIZER(proxies);
46 6cc5fcfe 2021-07-08 op
47 6cc5fcfe 2021-07-08 op enum telescope_process {
48 6cc5fcfe 2021-07-08 op PROC_UI,
49 6cc5fcfe 2021-07-08 op PROC_FS,
50 6cc5fcfe 2021-07-08 op PROC_NET,
51 6cc5fcfe 2021-07-08 op };
52 5e11c00c 2021-03-02 op
53 c07ac996 2021-03-12 op /* the first is also the fallback one */
54 c07ac996 2021-03-12 op static struct proto protos[] = {
55 31f1a758 2021-04-22 op { "gemini", load_gemini_url },
56 31f1a758 2021-04-22 op { "about", load_about_url },
57 4d3785b1 2021-03-09 op { NULL, NULL },
58 4d3785b1 2021-03-09 op };
59 4d3785b1 2021-03-09 op
60 9a8a7402 2021-07-18 op
61 9a8a7402 2021-07-18 op #define CANNOT_FETCH 0
62 9a8a7402 2021-07-18 op #define TOO_MUCH_REDIRECTS 1
63 9a8a7402 2021-07-18 op #define MALFORMED_RESPONSE 2
64 9a8a7402 2021-07-18 op #define UNKNOWN_TYPE_OR_CSET 3
65 9a8a7402 2021-07-18 op #define UNKNOWN_PROTOCOL 4
66 9a8a7402 2021-07-18 op
67 9a8a7402 2021-07-18 op /* XXX: keep in sync with telescope.c:/^normalize_code/ */
68 9a8a7402 2021-07-18 op const char *err_pages[70] = {
69 9a8a7402 2021-07-18 op [CANNOT_FETCH] = "# Couldn't load the page\n",
70 9a8a7402 2021-07-18 op [TOO_MUCH_REDIRECTS] = "# Too much redirects\n",
71 9a8a7402 2021-07-18 op [MALFORMED_RESPONSE] = "# Got a malformed response\n",
72 9a8a7402 2021-07-18 op [UNKNOWN_TYPE_OR_CSET] = "# Unsupported type or charset\n",
73 9a8a7402 2021-07-18 op [UNKNOWN_PROTOCOL] = "# Unknown protocol\n",
74 9a8a7402 2021-07-18 op
75 9a8a7402 2021-07-18 op [10] = "# Input required\n",
76 9a8a7402 2021-07-18 op [11] = "# Input required\n",
77 9a8a7402 2021-07-18 op [40] = "# Temporary failure\n",
78 9a8a7402 2021-07-18 op [41] = "# Server unavailable\n",
79 9a8a7402 2021-07-18 op [42] = "# CGI error\n",
80 9a8a7402 2021-07-18 op [43] = "# Proxy error\n",
81 9a8a7402 2021-07-18 op [44] = "# Slow down\n",
82 9a8a7402 2021-07-18 op [50] = "# Permanent failure\n",
83 9a8a7402 2021-07-18 op [51] = "# Not found\n",
84 9a8a7402 2021-07-18 op [52] = "# Gone\n",
85 9a8a7402 2021-07-18 op [53] = "# Proxy request refused\n",
86 9a8a7402 2021-07-18 op [59] = "# Bad request\n",
87 9a8a7402 2021-07-18 op [60] = "# Client certificate required\n",
88 9a8a7402 2021-07-18 op [61] = "# Certificate not authorised\n",
89 9a8a7402 2021-07-18 op [62] = "# Certificate not valid\n"
90 9a8a7402 2021-07-18 op };
91 9a8a7402 2021-07-18 op
92 2051e653 2021-03-13 op static void die(void) __attribute__((__noreturn__));
93 2051e653 2021-03-13 op static struct tab *tab_by_id(uint32_t);
94 2051e653 2021-03-13 op static void handle_imsg_err(struct imsg*, size_t);
95 2051e653 2021-03-13 op static void handle_imsg_check_cert(struct imsg*, size_t);
96 a2fd3805 2021-07-06 op static void handle_check_cert_user_choice(int, struct tab *);
97 a2fd3805 2021-07-06 op static void handle_maybe_save_new_cert(int, struct tab *);
98 2051e653 2021-03-13 op static void handle_imsg_got_code(struct imsg*, size_t);
99 2051e653 2021-03-13 op static void handle_imsg_got_meta(struct imsg*, size_t);
100 a2fd3805 2021-07-06 op static void handle_maybe_save_page(int, struct tab *);
101 d1353324 2021-07-13 op static void handle_save_page_path(const char *, struct tab *);
102 de2a69bb 2021-05-17 op static void handle_imsg_file_opened(struct imsg*, size_t);
103 2051e653 2021-03-13 op static void handle_imsg_buf(struct imsg*, size_t);
104 2051e653 2021-03-13 op static void handle_imsg_eof(struct imsg*, size_t);
105 740f578b 2021-03-15 op static void handle_imsg_bookmark_ok(struct imsg*, size_t);
106 3a227e9a 2021-03-18 op static void handle_imsg_save_cert_ok(struct imsg*, size_t);
107 288fd238 2021-04-25 op static void handle_imsg_update_cert_ok(struct imsg *, size_t);
108 1304bbdd 2021-03-15 op static void handle_dispatch_imsg(int, short, void*);
109 2051e653 2021-03-13 op static void load_page_from_str(struct tab*, const char*);
110 bd3a3a95 2021-07-20 op static int do_load_url(struct tab*, const char *, const char *);
111 2f524b17 2021-07-17 op static void parse_session_line(char *, const char **, uint32_t *);
112 87e3e801 2021-07-17 op static void load_last_session(void);
113 6cc5fcfe 2021-07-08 op static pid_t start_child(enum telescope_process, const char *, int);
114 bc10f6a5 2021-07-12 op static int ui_send_net(int, uint32_t, const void *, uint16_t);
115 bc10f6a5 2021-07-12 op static int ui_send_fs(int, uint32_t, const void *, uint16_t);
116 5e11c00c 2021-03-02 op
117 5e11c00c 2021-03-02 op static imsg_handlerfn *handlers[] = {
118 5e11c00c 2021-03-02 op [IMSG_ERR] = handle_imsg_err,
119 5e11c00c 2021-03-02 op [IMSG_CHECK_CERT] = handle_imsg_check_cert,
120 5e11c00c 2021-03-02 op [IMSG_GOT_CODE] = handle_imsg_got_code,
121 5e11c00c 2021-03-02 op [IMSG_GOT_META] = handle_imsg_got_meta,
122 5e11c00c 2021-03-02 op [IMSG_BUF] = handle_imsg_buf,
123 5e11c00c 2021-03-02 op [IMSG_EOF] = handle_imsg_eof,
124 740f578b 2021-03-15 op [IMSG_BOOKMARK_OK] = handle_imsg_bookmark_ok,
125 3a227e9a 2021-03-18 op [IMSG_SAVE_CERT_OK] = handle_imsg_save_cert_ok,
126 288fd238 2021-04-25 op [IMSG_UPDATE_CERT_OK] = handle_imsg_update_cert_ok,
127 de2a69bb 2021-05-17 op [IMSG_FILE_OPENED] = handle_imsg_file_opened,
128 5e11c00c 2021-03-02 op };
129 5e11c00c 2021-03-02 op
130 cbcc75fb 2021-03-17 op static struct ohash certs;
131 cbcc75fb 2021-03-17 op
132 5e11c00c 2021-03-02 op static void __attribute__((__noreturn__))
133 5e11c00c 2021-03-02 op die(void)
134 5e11c00c 2021-03-02 op {
135 5e11c00c 2021-03-02 op abort(); /* TODO */
136 5e11c00c 2021-03-02 op }
137 5e11c00c 2021-03-02 op
138 5e11c00c 2021-03-02 op static struct tab *
139 f6243012 2021-07-19 op tab_by_id(uint32_t id)
140 5e11c00c 2021-03-02 op {
141 5e11c00c 2021-03-02 op struct tab *t;
142 5e11c00c 2021-03-02 op
143 5e11c00c 2021-03-02 op TAILQ_FOREACH(t, &tabshead, tabs) {
144 5e11c00c 2021-03-02 op if (t->id == id)
145 5e11c00c 2021-03-02 op return t;
146 5e11c00c 2021-03-02 op }
147 5e11c00c 2021-03-02 op
148 5d6c2088 2021-07-19 op return NULL;
149 5e11c00c 2021-03-02 op }
150 5e11c00c 2021-03-02 op
151 5e11c00c 2021-03-02 op static void
152 5e11c00c 2021-03-02 op handle_imsg_err(struct imsg *imsg, size_t datalen)
153 5e11c00c 2021-03-02 op {
154 3a9b9365 2021-03-09 op struct tab *tab;
155 3a9b9365 2021-03-09 op char *page;
156 3a9b9365 2021-03-09 op
157 f6243012 2021-07-19 op if ((tab = tab_by_id(imsg->hdr.peerid)) == NULL)
158 f6243012 2021-07-19 op return;
159 3a9b9365 2021-03-09 op
160 3a9b9365 2021-03-09 op page = imsg->data;
161 3a9b9365 2021-03-09 op page[datalen-1] = '\0';
162 3a9b9365 2021-03-09 op
163 3a9b9365 2021-03-09 op if (asprintf(&page, "# Error loading %s\n\n> %s\n",
164 2051e653 2021-03-13 op tab->hist_cur->h, page) == -1)
165 3a9b9365 2021-03-09 op die();
166 3a9b9365 2021-03-09 op load_page_from_str(tab, page);
167 3a9b9365 2021-03-09 op free(page);
168 5e11c00c 2021-03-02 op }
169 5e11c00c 2021-03-02 op
170 5e11c00c 2021-03-02 op static void
171 5e11c00c 2021-03-02 op handle_imsg_check_cert(struct imsg *imsg, size_t datalen)
172 5e11c00c 2021-03-02 op {
173 984245ce 2021-06-23 op const char *hash, *host, *port;
174 cbcc75fb 2021-03-17 op int tofu_res;
175 cbcc75fb 2021-03-17 op struct tofu_entry *e;
176 cbcc75fb 2021-03-17 op struct tab *tab;
177 5e11c00c 2021-03-02 op
178 cbcc75fb 2021-03-17 op hash = imsg->data;
179 cbcc75fb 2021-03-17 op if (hash[datalen-1] != '\0')
180 cbcc75fb 2021-03-17 op abort();
181 cbcc75fb 2021-03-17 op
182 f6243012 2021-07-19 op if ((tab = tab_by_id(imsg->hdr.peerid)) == NULL)
183 f6243012 2021-07-19 op return;
184 cbcc75fb 2021-03-17 op
185 984245ce 2021-06-23 op if (tab->proxy != NULL) {
186 984245ce 2021-06-23 op host = tab->proxy->host;
187 984245ce 2021-06-23 op port = tab->proxy->port;
188 984245ce 2021-06-23 op } else {
189 984245ce 2021-06-23 op host = tab->uri.host;
190 984245ce 2021-06-23 op port = tab->uri.port;
191 984245ce 2021-06-23 op }
192 984245ce 2021-06-23 op
193 984245ce 2021-06-23 op if ((e = tofu_lookup(&certs, host, port)) == NULL) {
194 cbcc75fb 2021-03-17 op /* TODO: an update in libressl/libretls changed
195 cbcc75fb 2021-03-17 op * significantly. Find a better approach at storing
196 cbcc75fb 2021-03-17 op * the certs! */
197 cbcc75fb 2021-03-17 op if (datalen > sizeof(e->hash))
198 cbcc75fb 2021-03-17 op abort();
199 cbcc75fb 2021-03-17 op
200 cbcc75fb 2021-03-17 op tofu_res = 1; /* trust on first use */
201 cbcc75fb 2021-03-17 op if ((e = calloc(1, sizeof(*e))) == NULL)
202 cbcc75fb 2021-03-17 op abort();
203 984245ce 2021-06-23 op strlcpy(e->domain, host, sizeof(e->domain));
204 984245ce 2021-06-23 op if (*port != '\0' && strcmp(port, "1965")) {
205 eb4388ee 2021-04-25 op strlcat(e->domain, ":", sizeof(e->domain));
206 984245ce 2021-06-23 op strlcat(e->domain, port, sizeof(e->domain));
207 eb4388ee 2021-04-25 op }
208 cbcc75fb 2021-03-17 op strlcpy(e->hash, hash, sizeof(e->hash));
209 3768e50f 2021-04-25 op tofu_add(&certs, e);
210 bc10f6a5 2021-07-12 op ui_send_fs(IMSG_SAVE_CERT, tab->id, e, sizeof(*e));
211 cbcc75fb 2021-03-17 op } else
212 cbcc75fb 2021-03-17 op tofu_res = !strcmp(hash, e->hash);
213 cbcc75fb 2021-03-17 op
214 5d1bac73 2021-03-25 op if (tofu_res) {
215 a2fd3805 2021-07-06 op if (e->verified == -1)
216 a2fd3805 2021-07-06 op tab->trust = TS_TEMP_TRUSTED;
217 a2fd3805 2021-07-06 op else if (e->verified == 1)
218 a2fd3805 2021-07-06 op tab->trust = TS_VERIFIED;
219 a2fd3805 2021-07-06 op else
220 a2fd3805 2021-07-06 op tab->trust = TS_TRUSTED;
221 a2fd3805 2021-07-06 op
222 bc10f6a5 2021-07-12 op ui_send_net(IMSG_CERT_STATUS, imsg->hdr.peerid,
223 5d1bac73 2021-03-25 op &tofu_res, sizeof(tofu_res));
224 5d1bac73 2021-03-25 op } else {
225 cbcc75fb 2021-03-17 op tab->trust = TS_UNTRUSTED;
226 cbcc75fb 2021-03-17 op load_page_from_str(tab, "# Certificate mismatch\n");
227 288fd238 2021-04-25 op if ((tab->cert = strdup(hash)) == NULL)
228 288fd238 2021-04-25 op die();
229 5d1bac73 2021-03-25 op ui_yornp("Certificate mismatch. Proceed?",
230 a2fd3805 2021-07-06 op handle_check_cert_user_choice, tab);
231 cbcc75fb 2021-03-17 op }
232 5d1bac73 2021-03-25 op }
233 5d1bac73 2021-03-25 op
234 5d1bac73 2021-03-25 op static void
235 a2fd3805 2021-07-06 op handle_check_cert_user_choice(int accept, struct tab *tab)
236 5d1bac73 2021-03-25 op {
237 bc10f6a5 2021-07-12 op ui_send_net(IMSG_CERT_STATUS, tab->id, &accept,
238 bc10f6a5 2021-07-12 op sizeof(accept));
239 288fd238 2021-04-25 op
240 a2fd3805 2021-07-06 op if (accept) {
241 a2fd3805 2021-07-06 op /*
242 a2fd3805 2021-07-06 op * trust the certificate for this session only. If
243 a2fd3805 2021-07-06 op * the page results in a redirect while we're asking
244 a2fd3805 2021-07-06 op * the user to save, we'll end up with an invalid
245 a2fd3805 2021-07-06 op * tabid (one request == one tab id) and crash. It
246 a2fd3805 2021-07-06 op * also makes sense to save it for the current session
247 a2fd3805 2021-07-06 op * if the user accepted it.
248 a2fd3805 2021-07-06 op */
249 a2fd3805 2021-07-06 op tofu_temp_trust(&certs, tab->uri.host, tab->uri.port, tab->cert);
250 a2fd3805 2021-07-06 op
251 288fd238 2021-04-25 op ui_yornp("Save the new certificate?",
252 a2fd3805 2021-07-06 op handle_maybe_save_new_cert, tab);
253 a2fd3805 2021-07-06 op } else {
254 288fd238 2021-04-25 op free(tab->cert);
255 288fd238 2021-04-25 op tab->cert = NULL;
256 288fd238 2021-04-25 op }
257 5e11c00c 2021-03-02 op }
258 5e11c00c 2021-03-02 op
259 288fd238 2021-04-25 op static void
260 a2fd3805 2021-07-06 op handle_maybe_save_new_cert(int accept, struct tab *tab)
261 288fd238 2021-04-25 op {
262 288fd238 2021-04-25 op struct tofu_entry *e;
263 984245ce 2021-06-23 op const char *host, *port;
264 288fd238 2021-04-25 op
265 984245ce 2021-06-23 op if (tab->proxy != NULL) {
266 984245ce 2021-06-23 op host = tab->proxy->host;
267 984245ce 2021-06-23 op port = tab->proxy->port;
268 984245ce 2021-06-23 op } else {
269 984245ce 2021-06-23 op host = tab->uri.host;
270 984245ce 2021-06-23 op port = tab->uri.port;
271 984245ce 2021-06-23 op }
272 984245ce 2021-06-23 op
273 288fd238 2021-04-25 op if (!accept)
274 288fd238 2021-04-25 op goto end;
275 288fd238 2021-04-25 op
276 fe2262ad 2021-05-12 op if ((e = calloc(1, sizeof(*e))) == NULL)
277 288fd238 2021-04-25 op die();
278 288fd238 2021-04-25 op
279 984245ce 2021-06-23 op strlcpy(e->domain, host, sizeof(e->domain));
280 984245ce 2021-06-23 op if (*port != '\0' && strcmp(port, "1965")) {
281 288fd238 2021-04-25 op strlcat(e->domain, ":", sizeof(e->domain));
282 984245ce 2021-06-23 op strlcat(e->domain, port, sizeof(e->domain));
283 288fd238 2021-04-25 op }
284 288fd238 2021-04-25 op strlcpy(e->hash, tab->cert, sizeof(e->hash));
285 bc10f6a5 2021-07-12 op ui_send_fs(IMSG_UPDATE_CERT, 0, e, sizeof(*e));
286 288fd238 2021-04-25 op
287 288fd238 2021-04-25 op tofu_update(&certs, e);
288 288fd238 2021-04-25 op
289 288fd238 2021-04-25 op tab->trust = TS_TRUSTED;
290 288fd238 2021-04-25 op
291 288fd238 2021-04-25 op end:
292 288fd238 2021-04-25 op free(tab->cert);
293 288fd238 2021-04-25 op tab->cert = NULL;
294 288fd238 2021-04-25 op }
295 288fd238 2021-04-25 op
296 5cd2ebb1 2021-03-11 op static inline int
297 5cd2ebb1 2021-03-11 op normalize_code(int n)
298 5cd2ebb1 2021-03-11 op {
299 5cd2ebb1 2021-03-11 op if (n < 20) {
300 5cd2ebb1 2021-03-11 op if (n == 10 || n == 11)
301 5cd2ebb1 2021-03-11 op return n;
302 5cd2ebb1 2021-03-11 op return 10;
303 5cd2ebb1 2021-03-11 op } else if (n < 30) {
304 5cd2ebb1 2021-03-11 op return 20;
305 5cd2ebb1 2021-03-11 op } else if (n < 40) {
306 5cd2ebb1 2021-03-11 op if (n == 30 || n == 31)
307 5cd2ebb1 2021-03-11 op return n;
308 5cd2ebb1 2021-03-11 op return 30;
309 5cd2ebb1 2021-03-11 op } else if (n < 50) {
310 5cd2ebb1 2021-03-11 op if (n <= 44)
311 5cd2ebb1 2021-03-11 op return n;
312 5cd2ebb1 2021-03-11 op return 40;
313 5cd2ebb1 2021-03-11 op } else if (n < 60) {
314 5cd2ebb1 2021-03-11 op if (n <= 53 || n == 59)
315 5cd2ebb1 2021-03-11 op return n;
316 5cd2ebb1 2021-03-11 op return 50;
317 5cd2ebb1 2021-03-11 op } else if (n < 70) {
318 5cd2ebb1 2021-03-11 op if (n <= 62)
319 5cd2ebb1 2021-03-11 op return n;
320 5cd2ebb1 2021-03-11 op return 60;
321 5cd2ebb1 2021-03-11 op } else
322 5cd2ebb1 2021-03-11 op return MALFORMED_RESPONSE;
323 5cd2ebb1 2021-03-11 op }
324 5cd2ebb1 2021-03-11 op
325 5e11c00c 2021-03-02 op static void
326 5e11c00c 2021-03-02 op handle_imsg_got_code(struct imsg *imsg, size_t datalen)
327 5e11c00c 2021-03-02 op {
328 0972d8b2 2021-03-02 op struct tab *tab;
329 5e11c00c 2021-03-02 op
330 f6243012 2021-07-19 op if ((tab = tab_by_id(imsg->hdr.peerid)) == NULL)
331 f6243012 2021-07-19 op return;
332 0972d8b2 2021-03-02 op
333 0972d8b2 2021-03-02 op if (sizeof(tab->code) != datalen)
334 5e11c00c 2021-03-02 op die();
335 5e11c00c 2021-03-02 op
336 5cd2ebb1 2021-03-11 op memcpy(&tab->code, imsg->data, sizeof(tab->code));
337 5cd2ebb1 2021-03-11 op tab->code = normalize_code(tab->code);
338 5cd2ebb1 2021-03-11 op if (tab->code != 30 && tab->code != 31)
339 0972d8b2 2021-03-02 op tab->redirect_count = 0;
340 5e11c00c 2021-03-02 op }
341 5e11c00c 2021-03-02 op
342 5e11c00c 2021-03-02 op static void
343 5e11c00c 2021-03-02 op handle_imsg_got_meta(struct imsg *imsg, size_t datalen)
344 5e11c00c 2021-03-02 op {
345 0972d8b2 2021-03-02 op struct tab *tab;
346 0972d8b2 2021-03-02 op
347 f6243012 2021-07-19 op if ((tab = tab_by_id(imsg->hdr.peerid)) == NULL)
348 f6243012 2021-07-19 op return;
349 0972d8b2 2021-03-02 op
350 0972d8b2 2021-03-02 op if (sizeof(tab->meta) <= datalen)
351 0972d8b2 2021-03-02 op die();
352 5cd2ebb1 2021-03-11 op
353 0972d8b2 2021-03-02 op memcpy(tab->meta, imsg->data, datalen);
354 0972d8b2 2021-03-02 op
355 5cd2ebb1 2021-03-11 op if (tab->code < 10) { /* internal errors */
356 5cd2ebb1 2021-03-11 op load_page_from_str(tab, err_pages[tab->code]);
357 5cd2ebb1 2021-03-11 op } else if (tab->code < 20) { /* 1x */
358 5cd2ebb1 2021-03-11 op load_page_from_str(tab, err_pages[tab->code]);
359 5cd2ebb1 2021-03-11 op ui_require_input(tab, tab->code == 11);
360 5cd2ebb1 2021-03-11 op } else if (tab->code == 20) {
361 c07ac996 2021-03-12 op if (setup_parser_for(tab)) {
362 bc10f6a5 2021-07-12 op ui_send_net(IMSG_PROCEED, tab->id, NULL, 0);
363 c07ac996 2021-03-12 op } else {
364 c07ac996 2021-03-12 op load_page_from_str(tab, err_pages[UNKNOWN_TYPE_OR_CSET]);
365 de2a69bb 2021-05-17 op ui_yornp("Can't display page, wanna save?",
366 a2fd3805 2021-07-06 op handle_maybe_save_page, tab);
367 c07ac996 2021-03-12 op }
368 5cd2ebb1 2021-03-11 op } else if (tab->code < 40) { /* 3x */
369 3a9b9365 2021-03-09 op tab->redirect_count++;
370 0972d8b2 2021-03-02 op
371 3a9b9365 2021-03-09 op /* TODO: make customizable? */
372 3a9b9365 2021-03-09 op if (tab->redirect_count > 5) {
373 3a9b9365 2021-03-09 op load_page_from_str(tab,
374 3a9b9365 2021-03-09 op err_pages[TOO_MUCH_REDIRECTS]);
375 5cd2ebb1 2021-03-11 op } else
376 bd3a3a95 2021-07-20 op do_load_url(tab, tab->meta, NULL);
377 5cd2ebb1 2021-03-11 op } else { /* 4x, 5x & 6x */
378 5cd2ebb1 2021-03-11 op load_page_from_str(tab, err_pages[tab->code]);
379 3a9b9365 2021-03-09 op }
380 de2a69bb 2021-05-17 op }
381 de2a69bb 2021-05-17 op
382 de2a69bb 2021-05-17 op static void
383 a2fd3805 2021-07-06 op handle_maybe_save_page(int dosave, struct tab *tab)
384 de2a69bb 2021-05-17 op {
385 de2a69bb 2021-05-17 op if (dosave)
386 d1353324 2021-07-13 op ui_read("Save to path", handle_save_page_path, tab);
387 de2a69bb 2021-05-17 op else
388 a2fd3805 2021-07-06 op stop_tab(tab);
389 de2a69bb 2021-05-17 op }
390 de2a69bb 2021-05-17 op
391 de2a69bb 2021-05-17 op static void
392 d1353324 2021-07-13 op handle_save_page_path(const char *path, struct tab *tab)
393 d1353324 2021-07-13 op {
394 de2a69bb 2021-05-17 op if (path == NULL) {
395 d1353324 2021-07-13 op stop_tab(tab);
396 de2a69bb 2021-05-17 op return;
397 de2a69bb 2021-05-17 op }
398 de2a69bb 2021-05-17 op
399 de2a69bb 2021-05-17 op tab->path = strdup(path);
400 de2a69bb 2021-05-17 op
401 d1353324 2021-07-13 op ui_send_fs(IMSG_FILE_OPEN, tab->id, path, strlen(path)+1);
402 5e11c00c 2021-03-02 op }
403 5e11c00c 2021-03-02 op
404 5e11c00c 2021-03-02 op static void
405 de2a69bb 2021-05-17 op handle_imsg_file_opened(struct imsg *imsg, size_t datalen)
406 de2a69bb 2021-05-17 op {
407 de2a69bb 2021-05-17 op struct tab *tab;
408 de2a69bb 2021-05-17 op char *page;
409 de2a69bb 2021-05-17 op const char *e;
410 de2a69bb 2021-05-17 op int l;
411 de2a69bb 2021-05-17 op
412 f6243012 2021-07-19 op if ((tab = tab_by_id(imsg->hdr.peerid)) == NULL) {
413 f6243012 2021-07-19 op if (imsg->fd != -1)
414 f6243012 2021-07-19 op close(imsg->fd);
415 f6243012 2021-07-19 op return;
416 f6243012 2021-07-19 op }
417 de2a69bb 2021-05-17 op
418 de2a69bb 2021-05-17 op if (imsg->fd == -1) {
419 de2a69bb 2021-05-17 op stop_tab(tab);
420 de2a69bb 2021-05-17 op
421 de2a69bb 2021-05-17 op e = imsg->data;
422 de2a69bb 2021-05-17 op if (e[datalen-1] != '\0')
423 de2a69bb 2021-05-17 op die();
424 de2a69bb 2021-05-17 op l = asprintf(&page, "# Can't open file\n\n> %s: %s\n",
425 de2a69bb 2021-05-17 op tab->path, e);
426 de2a69bb 2021-05-17 op if (l == -1)
427 de2a69bb 2021-05-17 op die();
428 de2a69bb 2021-05-17 op load_page_from_str(tab, page);
429 de2a69bb 2021-05-17 op free(page);
430 de2a69bb 2021-05-17 op } else {
431 de2a69bb 2021-05-17 op tab->fd = imsg->fd;
432 bc10f6a5 2021-07-12 op ui_send_net(IMSG_PROCEED, tab->id, NULL, 0);
433 de2a69bb 2021-05-17 op }
434 de2a69bb 2021-05-17 op }
435 de2a69bb 2021-05-17 op
436 de2a69bb 2021-05-17 op static void
437 5e11c00c 2021-03-02 op handle_imsg_buf(struct imsg *imsg, size_t datalen)
438 5e11c00c 2021-03-02 op {
439 0972d8b2 2021-03-02 op struct tab *tab;
440 de2a69bb 2021-05-17 op int l;
441 0e1eff5b 2021-06-23 op char *page, buf[FMT_SCALED_STRSIZE] = {0};
442 5e11c00c 2021-03-02 op
443 f6243012 2021-07-19 op if ((tab = tab_by_id(imsg->hdr.peerid)) == NULL)
444 5d6c2088 2021-07-19 op return;
445 5e11c00c 2021-03-02 op
446 de2a69bb 2021-05-17 op tab->bytes += datalen;
447 de2a69bb 2021-05-17 op if (tab->fd == -1) {
448 46f6e974 2021-05-17 op if (!tab->buffer.page.parse(&tab->buffer.page,
449 de2a69bb 2021-05-17 op imsg->data, datalen))
450 de2a69bb 2021-05-17 op die();
451 de2a69bb 2021-05-17 op } else {
452 de2a69bb 2021-05-17 op write(tab->fd, imsg->data, datalen);
453 0e1eff5b 2021-06-23 op fmt_scaled(tab->bytes, buf);
454 0e1eff5b 2021-06-23 op l = asprintf(&page, "Saving to \"%s\"... (%s)\n",
455 de2a69bb 2021-05-17 op tab->path,
456 0e1eff5b 2021-06-23 op buf);
457 de2a69bb 2021-05-17 op if (l == -1)
458 de2a69bb 2021-05-17 op die();
459 de2a69bb 2021-05-17 op load_page_from_str(tab, page);
460 de2a69bb 2021-05-17 op free(page);
461 de2a69bb 2021-05-17 op }
462 5e11c00c 2021-03-02 op
463 0972d8b2 2021-03-02 op ui_on_tab_refresh(tab);
464 5e11c00c 2021-03-02 op }
465 5e11c00c 2021-03-02 op
466 5e11c00c 2021-03-02 op static void
467 5e11c00c 2021-03-02 op handle_imsg_eof(struct imsg *imsg, size_t datalen)
468 5e11c00c 2021-03-02 op {
469 2ba66cea 2021-03-22 op struct tab *tab;
470 de2a69bb 2021-05-17 op int l;
471 0e1eff5b 2021-06-23 op char *page, buf[FMT_SCALED_STRSIZE] = {0};
472 a5c3e03d 2021-03-02 op
473 f6243012 2021-07-19 op if ((tab = tab_by_id(imsg->hdr.peerid)) == NULL)
474 f6243012 2021-07-19 op return;
475 de2a69bb 2021-05-17 op
476 de2a69bb 2021-05-17 op if (tab->fd == -1) {
477 46f6e974 2021-05-17 op if (!tab->buffer.page.free(&tab->buffer.page))
478 de2a69bb 2021-05-17 op die();
479 de2a69bb 2021-05-17 op } else {
480 0e1eff5b 2021-06-23 op fmt_scaled(tab->bytes, buf);
481 0e1eff5b 2021-06-23 op l = asprintf(&page, "Saved to \"%s\" (%s)\n",
482 de2a69bb 2021-05-17 op tab->path,
483 0e1eff5b 2021-06-23 op buf);
484 de2a69bb 2021-05-17 op if (l == -1)
485 de2a69bb 2021-05-17 op die();
486 de2a69bb 2021-05-17 op load_page_from_str(tab, page);
487 de2a69bb 2021-05-17 op free(page);
488 a5c3e03d 2021-03-02 op
489 de2a69bb 2021-05-17 op close(tab->fd);
490 de2a69bb 2021-05-17 op tab->fd = -1;
491 de2a69bb 2021-05-17 op free(tab->path);
492 de2a69bb 2021-05-17 op tab->path = NULL;
493 de2a69bb 2021-05-17 op }
494 de2a69bb 2021-05-17 op
495 2ba66cea 2021-03-22 op ui_on_tab_refresh(tab);
496 2ba66cea 2021-03-22 op ui_on_tab_loaded(tab);
497 5e11c00c 2021-03-02 op }
498 5e11c00c 2021-03-02 op
499 5e11c00c 2021-03-02 op static void
500 740f578b 2021-03-15 op handle_imsg_bookmark_ok(struct imsg *imsg, size_t datalen)
501 740f578b 2021-03-15 op {
502 740f578b 2021-03-15 op int res;
503 740f578b 2021-03-15 op
504 740f578b 2021-03-15 op if (datalen != sizeof(res))
505 740f578b 2021-03-15 op die();
506 740f578b 2021-03-15 op
507 740f578b 2021-03-15 op memcpy(&res, imsg->data, sizeof(res));
508 740f578b 2021-03-15 op if (res == 0)
509 7f963c41 2021-06-20 op message("Added to bookmarks!");
510 740f578b 2021-03-15 op else
511 7f963c41 2021-06-20 op message("Failed to add to bookmarks: %s",
512 740f578b 2021-03-15 op strerror(res));
513 740f578b 2021-03-15 op }
514 740f578b 2021-03-15 op
515 740f578b 2021-03-15 op static void
516 3a227e9a 2021-03-18 op handle_imsg_save_cert_ok(struct imsg *imsg, size_t datalen)
517 3a227e9a 2021-03-18 op {
518 3a227e9a 2021-03-18 op int res;
519 3a227e9a 2021-03-18 op
520 3a227e9a 2021-03-18 op if (datalen != sizeof(res))
521 3a227e9a 2021-03-18 op die();
522 3a227e9a 2021-03-18 op memcpy(&res, imsg->data, datalen);
523 3a227e9a 2021-03-18 op if (res != 0)
524 7f963c41 2021-06-20 op message("Failed to save the cert for: %s",
525 3a227e9a 2021-03-18 op strerror(res));
526 288fd238 2021-04-25 op }
527 288fd238 2021-04-25 op
528 288fd238 2021-04-25 op static void
529 288fd238 2021-04-25 op handle_imsg_update_cert_ok(struct imsg *imsg, size_t datalen)
530 288fd238 2021-04-25 op {
531 288fd238 2021-04-25 op int res;
532 288fd238 2021-04-25 op
533 288fd238 2021-04-25 op if (datalen != sizeof(res))
534 288fd238 2021-04-25 op die();
535 288fd238 2021-04-25 op memcpy(&res, imsg->data, datalen);
536 288fd238 2021-04-25 op if (!res)
537 7f963c41 2021-06-20 op message("Failed to update the certificate");
538 3a227e9a 2021-03-18 op }
539 3a227e9a 2021-03-18 op
540 3a227e9a 2021-03-18 op static void
541 1304bbdd 2021-03-15 op handle_dispatch_imsg(int fd, short ev, void *d)
542 5e11c00c 2021-03-02 op {
543 bc10f6a5 2021-07-12 op struct imsgev *iev = d;
544 ea080950 2021-07-20 op
545 ea080950 2021-07-20 op if (dispatch_imsg(iev, ev, handlers, sizeof(handlers)) == -1)
546 ea080950 2021-07-20 op err(1, "connection closed");
547 5e11c00c 2021-03-02 op }
548 5e11c00c 2021-03-02 op
549 0972d8b2 2021-03-02 op static void
550 0972d8b2 2021-03-02 op load_page_from_str(struct tab *tab, const char *page)
551 0972d8b2 2021-03-02 op {
552 00ccb53d 2021-07-01 op erase_buffer(&tab->buffer);
553 46f6e974 2021-05-17 op gemtext_initparser(&tab->buffer.page);
554 46f6e974 2021-05-17 op if (!tab->buffer.page.parse(&tab->buffer.page, page, strlen(page)))
555 0972d8b2 2021-03-02 op die();
556 46f6e974 2021-05-17 op if (!tab->buffer.page.free(&tab->buffer.page))
557 0972d8b2 2021-03-02 op die();
558 0972d8b2 2021-03-02 op ui_on_tab_refresh(tab);
559 8af5e5ed 2021-03-08 op ui_on_tab_loaded(tab);
560 0972d8b2 2021-03-02 op }
561 0972d8b2 2021-03-02 op
562 bcb0b073 2021-03-07 op void
563 4d3785b1 2021-03-09 op load_about_url(struct tab *tab, const char *url)
564 0972d8b2 2021-03-02 op {
565 10346511 2021-03-17 op tab->trust = TS_VERIFIED;
566 2051e653 2021-03-13 op
567 46f6e974 2021-05-17 op gemtext_initparser(&tab->buffer.page);
568 35e1f40a 2021-03-14 op
569 bc10f6a5 2021-07-12 op ui_send_fs(IMSG_GET, tab->id,
570 31f1a758 2021-04-22 op tab->hist_cur->h, strlen(tab->hist_cur->h)+1);
571 4d3785b1 2021-03-09 op }
572 0972d8b2 2021-03-02 op
573 4d3785b1 2021-03-09 op void
574 4d3785b1 2021-03-09 op load_gemini_url(struct tab *tab, const char *url)
575 4d3785b1 2021-03-09 op {
576 984245ce 2021-06-23 op struct get_req req;
577 754622a2 2021-03-15 op
578 2eef3403 2021-04-22 op stop_tab(tab);
579 2eef3403 2021-04-22 op tab->id = tab_new_id();
580 2eef3403 2021-04-22 op
581 984245ce 2021-06-23 op memset(&req, 0, sizeof(req));
582 984245ce 2021-06-23 op strlcpy(req.host, tab->uri.host, sizeof(req.host));
583 984245ce 2021-06-23 op strlcpy(req.port, tab->uri.port, sizeof(req.host));
584 984245ce 2021-06-23 op
585 984245ce 2021-06-23 op strlcpy(req.req, tab->hist_cur->h, sizeof(req.req));
586 984245ce 2021-06-23 op strlcat(req.req, "\r\n", sizeof(req.req));
587 984245ce 2021-06-23 op
588 984245ce 2021-06-23 op req.proto = PROTO_GEMINI;
589 984245ce 2021-06-23 op
590 bc10f6a5 2021-07-12 op ui_send_net(IMSG_GET_RAW, tab->id,
591 984245ce 2021-06-23 op &req, sizeof(req));
592 0972d8b2 2021-03-02 op }
593 0972d8b2 2021-03-02 op
594 984245ce 2021-06-23 op void
595 984245ce 2021-06-23 op load_via_proxy(struct tab *tab, const char *url, struct proxy *p)
596 984245ce 2021-06-23 op {
597 984245ce 2021-06-23 op struct get_req req;
598 984245ce 2021-06-23 op
599 984245ce 2021-06-23 op stop_tab(tab);
600 984245ce 2021-06-23 op tab->id = tab_new_id();
601 984245ce 2021-06-23 op tab->proxy = p;
602 984245ce 2021-06-23 op
603 984245ce 2021-06-23 op memset(&req, 0, sizeof(req));
604 984245ce 2021-06-23 op strlcpy(req.host, p->host, sizeof(req.host));
605 984245ce 2021-06-23 op strlcpy(req.port, p->port, sizeof(req.host));
606 984245ce 2021-06-23 op
607 984245ce 2021-06-23 op strlcpy(req.req, tab->hist_cur->h, sizeof(req.req));
608 984245ce 2021-06-23 op strlcat(req.req, "\r\n", sizeof(req.req));
609 984245ce 2021-06-23 op
610 984245ce 2021-06-23 op req.proto = p->proto;
611 984245ce 2021-06-23 op
612 bc10f6a5 2021-07-12 op ui_send_net(IMSG_GET_RAW, tab->id,
613 984245ce 2021-06-23 op &req, sizeof(req));
614 984245ce 2021-06-23 op }
615 984245ce 2021-06-23 op
616 adea4eec 2021-07-16 op /*
617 adea4eec 2021-07-16 op * Effectively load the given url in the given tab. Return 1 when
618 adea4eec 2021-07-16 op * loading the page asynchronously, and thus when an erase_buffer can
619 adea4eec 2021-07-16 op * be done right after this function return, or 0 when loading the
620 adea4eec 2021-07-16 op * page synchronously. In this last case, erase_buffer *MUST* be
621 adea4eec 2021-07-16 op * called by the handling function (such as load_page_from_str).
622 adea4eec 2021-07-16 op */
623 adea4eec 2021-07-16 op static int
624 bd3a3a95 2021-07-20 op do_load_url(struct tab *tab, const char *url, const char *base)
625 4d3785b1 2021-03-09 op {
626 31f1a758 2021-04-22 op struct phos_uri uri;
627 31f1a758 2021-04-22 op struct proto *p;
628 984245ce 2021-06-23 op struct proxy *proxy;
629 31f1a758 2021-04-22 op char *t;
630 7943bb81 2021-07-10 op
631 7943bb81 2021-07-10 op tab->proxy = NULL;
632 de2a69bb 2021-05-17 op
633 de2a69bb 2021-05-17 op if (tab->fd != -1) {
634 de2a69bb 2021-05-17 op close(tab->fd);
635 de2a69bb 2021-05-17 op tab->fd = -1;
636 de2a69bb 2021-05-17 op free(tab->path);
637 de2a69bb 2021-05-17 op tab->path = NULL;
638 de2a69bb 2021-05-17 op }
639 4d3785b1 2021-03-09 op
640 10346511 2021-03-17 op tab->trust = TS_UNKNOWN;
641 10346511 2021-03-17 op
642 bd3a3a95 2021-07-20 op if (base == NULL)
643 bd3a3a95 2021-07-20 op memcpy(&uri, &tab->uri, sizeof(tab->uri));
644 bd3a3a95 2021-07-20 op else
645 bd3a3a95 2021-07-20 op phos_parse_absolute_uri(base, &uri);
646 bd3a3a95 2021-07-20 op
647 31f1a758 2021-04-22 op if (!phos_resolve_uri_from_str(&uri, url, &tab->uri)) {
648 31f1a758 2021-04-22 op if (asprintf(&t, "#error loading %s\n>%s\n",
649 31f1a758 2021-04-22 op url, "Can't parse the URI") == -1)
650 31f1a758 2021-04-22 op die();
651 31f1a758 2021-04-22 op strlcpy(tab->hist_cur->h, url, sizeof(tab->hist_cur->h));
652 31f1a758 2021-04-22 op load_page_from_str(tab, t);
653 31f1a758 2021-04-22 op free(t);
654 adea4eec 2021-07-16 op return 0;
655 31f1a758 2021-04-22 op }
656 31f1a758 2021-04-22 op
657 31f1a758 2021-04-22 op phos_serialize_uri(&tab->uri, tab->hist_cur->h,
658 31f1a758 2021-04-22 op sizeof(tab->hist_cur->h));
659 31f1a758 2021-04-22 op
660 4d3785b1 2021-03-09 op for (p = protos; p->schema != NULL; ++p) {
661 ed383cf4 2021-04-22 op if (!strcmp(tab->uri.scheme, p->schema)) {
662 4d3785b1 2021-03-09 op p->loadfn(tab, url);
663 adea4eec 2021-07-16 op return 1;
664 4d3785b1 2021-03-09 op }
665 4d3785b1 2021-03-09 op }
666 4d3785b1 2021-03-09 op
667 984245ce 2021-06-23 op TAILQ_FOREACH(proxy, &proxies, proxies) {
668 984245ce 2021-06-23 op if (!strcmp(tab->uri.scheme, proxy->match_proto)) {
669 984245ce 2021-06-23 op load_via_proxy(tab, url, proxy);
670 adea4eec 2021-07-16 op return 1;
671 984245ce 2021-06-23 op }
672 984245ce 2021-06-23 op }
673 984245ce 2021-06-23 op
674 adea4eec 2021-07-16 op load_page_from_str(tab, err_pages[UNKNOWN_PROTOCOL]);
675 adea4eec 2021-07-16 op return 0;
676 4d3785b1 2021-03-09 op }
677 4d3785b1 2021-03-09 op
678 2ddbda6b 2021-07-17 op /*
679 2ddbda6b 2021-07-17 op * Load url in tab. If tab is marked as lazy, only prepare the url
680 2ddbda6b 2021-07-17 op * but don't load it. If tab is lazy and a url was already prepared,
681 2ddbda6b 2021-07-17 op * do load it!
682 2ddbda6b 2021-07-17 op */
683 9ad4627d 2021-03-10 op void
684 bd3a3a95 2021-07-20 op load_url(struct tab *tab, const char *url, const char *base)
685 2051e653 2021-03-13 op {
686 2ddbda6b 2021-07-17 op int lazy;
687 2051e653 2021-03-13 op
688 2ddbda6b 2021-07-17 op lazy = tab->flags & TAB_LAZY;
689 2ddbda6b 2021-07-17 op
690 2ddbda6b 2021-07-17 op if (lazy && tab->hist_cur != NULL) {
691 2ddbda6b 2021-07-17 op lazy = 0;
692 2ddbda6b 2021-07-17 op tab->flags &= ~TAB_LAZY;
693 2051e653 2021-03-13 op }
694 984245ce 2021-06-23 op
695 2ddbda6b 2021-07-17 op if (!lazy || tab->hist_cur == NULL) {
696 2ddbda6b 2021-07-17 op if (tab->hist_cur != NULL)
697 2ddbda6b 2021-07-17 op hist_clear_forward(&tab->hist,
698 2ddbda6b 2021-07-17 op TAILQ_NEXT(tab->hist_cur, entries));
699 2ddbda6b 2021-07-17 op
700 2ddbda6b 2021-07-17 op if ((tab->hist_cur = calloc(1, sizeof(*tab->hist_cur))) == NULL) {
701 2ddbda6b 2021-07-17 op event_loopbreak();
702 2ddbda6b 2021-07-17 op return;
703 2ddbda6b 2021-07-17 op }
704 2ddbda6b 2021-07-17 op
705 2ddbda6b 2021-07-17 op strlcpy(tab->buffer.page.title, url,
706 2ddbda6b 2021-07-17 op sizeof(tab->buffer.page.title));
707 2ddbda6b 2021-07-17 op hist_push(&tab->hist, tab->hist_cur);
708 2ddbda6b 2021-07-17 op
709 2ddbda6b 2021-07-17 op if (lazy)
710 2ddbda6b 2021-07-17 op strlcpy(tab->hist_cur->h, url,
711 2ddbda6b 2021-07-17 op sizeof(tab->hist_cur->h));
712 2ddbda6b 2021-07-17 op }
713 2ddbda6b 2021-07-17 op
714 bd3a3a95 2021-07-20 op if (!lazy && do_load_url(tab, url, base))
715 adea4eec 2021-07-16 op erase_buffer(&tab->buffer);
716 2051e653 2021-03-13 op }
717 2051e653 2021-03-13 op
718 2051e653 2021-03-13 op int
719 2051e653 2021-03-13 op load_previous_page(struct tab *tab)
720 2051e653 2021-03-13 op {
721 2051e653 2021-03-13 op struct hist *h;
722 2051e653 2021-03-13 op
723 2051e653 2021-03-13 op if ((h = TAILQ_PREV(tab->hist_cur, mhisthead, entries)) == NULL)
724 2051e653 2021-03-13 op return 0;
725 2051e653 2021-03-13 op tab->hist_cur = h;
726 bd3a3a95 2021-07-20 op do_load_url(tab, h->h, NULL);
727 2051e653 2021-03-13 op return 1;
728 2051e653 2021-03-13 op }
729 2051e653 2021-03-13 op
730 2051e653 2021-03-13 op int
731 2051e653 2021-03-13 op load_next_page(struct tab *tab)
732 2051e653 2021-03-13 op {
733 2051e653 2021-03-13 op struct hist *h;
734 2051e653 2021-03-13 op
735 2051e653 2021-03-13 op if ((h = TAILQ_NEXT(tab->hist_cur, entries)) == NULL)
736 2051e653 2021-03-13 op return 0;
737 2051e653 2021-03-13 op tab->hist_cur = h;
738 bd3a3a95 2021-07-20 op do_load_url(tab, h->h, NULL);
739 2051e653 2021-03-13 op return 1;
740 2051e653 2021-03-13 op }
741 2051e653 2021-03-13 op
742 90730426 2021-07-21 op /*
743 90730426 2021-07-21 op * Free every resource linked to the tab, including the tab itself.
744 90730426 2021-07-21 op * Removes the tab from the tablist, but doesn't update the
745 90730426 2021-07-21 op * current_tab though.
746 90730426 2021-07-21 op */
747 2051e653 2021-03-13 op void
748 90730426 2021-07-21 op free_tab(struct tab *tab)
749 90730426 2021-07-21 op {
750 90730426 2021-07-21 op stop_tab(tab);
751 90730426 2021-07-21 op
752 90730426 2021-07-21 op if (evtimer_pending(&tab->loadingev, NULL))
753 90730426 2021-07-21 op evtimer_del(&tab->loadingev);
754 90730426 2021-07-21 op
755 90730426 2021-07-21 op TAILQ_REMOVE(&tabshead, tab, tabs);
756 90730426 2021-07-21 op free(tab);
757 90730426 2021-07-21 op }
758 90730426 2021-07-21 op
759 90730426 2021-07-21 op void
760 9ad4627d 2021-03-10 op stop_tab(struct tab *tab)
761 9ad4627d 2021-03-10 op {
762 bc10f6a5 2021-07-12 op ui_send_net(IMSG_STOP, tab->id, NULL, 0);
763 de2a69bb 2021-05-17 op
764 de2a69bb 2021-05-17 op if (tab->fd != -1) {
765 de2a69bb 2021-05-17 op close(tab->fd);
766 de2a69bb 2021-05-17 op tab->fd = -1;
767 de2a69bb 2021-05-17 op free(tab->path);
768 de2a69bb 2021-05-17 op tab->path = NULL;
769 de2a69bb 2021-05-17 op load_page_from_str(tab, "Stopped.\n");
770 de2a69bb 2021-05-17 op }
771 9ad4627d 2021-03-10 op }
772 9ad4627d 2021-03-10 op
773 740f578b 2021-03-15 op void
774 740f578b 2021-03-15 op add_to_bookmarks(const char *str)
775 740f578b 2021-03-15 op {
776 bc10f6a5 2021-07-12 op ui_send_fs(IMSG_BOOKMARK_PAGE, 0,
777 bc10f6a5 2021-07-12 op str, strlen(str)+1);
778 c7107cec 2021-04-01 op }
779 c7107cec 2021-04-01 op
780 c7107cec 2021-04-01 op void
781 c7107cec 2021-04-01 op save_session(void)
782 c7107cec 2021-04-01 op {
783 87e3e801 2021-07-17 op struct tab *tab;
784 2f524b17 2021-07-17 op char *t;
785 87e3e801 2021-07-17 op int flags;
786 c7107cec 2021-04-01 op
787 bc10f6a5 2021-07-12 op ui_send_fs(IMSG_SESSION_START, 0, NULL, 0);
788 c7107cec 2021-04-01 op
789 c7107cec 2021-04-01 op TAILQ_FOREACH(tab, &tabshead, tabs) {
790 87e3e801 2021-07-17 op flags = tab->flags;
791 87e3e801 2021-07-17 op if (tab == current_tab)
792 87e3e801 2021-07-17 op flags |= TAB_CURRENT;
793 2f524b17 2021-07-17 op
794 2f524b17 2021-07-17 op t = tab->hist_cur->h;
795 2f524b17 2021-07-17 op ui_send_fs(IMSG_SESSION_TAB, flags, t, strlen(t)+1);
796 2f524b17 2021-07-17 op
797 2f524b17 2021-07-17 op t = tab->buffer.page.title;
798 2f524b17 2021-07-17 op ui_send_fs(IMSG_SESSION_TAB_TITLE, 0, t, strlen(t)+1);
799 c7107cec 2021-04-01 op }
800 c7107cec 2021-04-01 op
801 bc10f6a5 2021-07-12 op ui_send_fs(IMSG_SESSION_END, 0, NULL, 0);
802 d2544989 2021-07-08 op }
803 d2544989 2021-07-08 op
804 87e3e801 2021-07-17 op /*
805 87e3e801 2021-07-17 op * Parse a line of the session file. The format is:
806 87e3e801 2021-07-17 op *
807 2f524b17 2021-07-17 op * URL [flags,...] [title]\n
808 87e3e801 2021-07-17 op */
809 d2544989 2021-07-08 op static void
810 2f524b17 2021-07-17 op parse_session_line(char *line, const char **title, uint32_t *flags)
811 d2544989 2021-07-08 op {
812 2f524b17 2021-07-17 op char *s, *t, *ap;
813 87e3e801 2021-07-17 op
814 2f524b17 2021-07-17 op *title = "";
815 87e3e801 2021-07-17 op *flags = 0;
816 87e3e801 2021-07-17 op if ((s = strchr(line, ' ')) == NULL)
817 87e3e801 2021-07-17 op return;
818 87e3e801 2021-07-17 op
819 87e3e801 2021-07-17 op *s++ = '\0';
820 2f524b17 2021-07-17 op
821 2f524b17 2021-07-17 op if ((t = strchr(s, ' ')) != NULL) {
822 2f524b17 2021-07-17 op *t++ = '\0';
823 2f524b17 2021-07-17 op *title = t;
824 2f524b17 2021-07-17 op }
825 2f524b17 2021-07-17 op
826 87e3e801 2021-07-17 op while ((ap = strsep(&s, ",")) != NULL) {
827 87e3e801 2021-07-17 op if (*ap == '\0')
828 87e3e801 2021-07-17 op ;
829 87e3e801 2021-07-17 op else if (!strcmp(ap, "current"))
830 87e3e801 2021-07-17 op *flags |= TAB_CURRENT;
831 87e3e801 2021-07-17 op else
832 87e3e801 2021-07-17 op message("unknown tab flag: %s", ap);
833 87e3e801 2021-07-17 op }
834 87e3e801 2021-07-17 op }
835 87e3e801 2021-07-17 op
836 87e3e801 2021-07-17 op static void
837 87e3e801 2021-07-17 op load_last_session(void)
838 87e3e801 2021-07-17 op {
839 2f524b17 2021-07-17 op const char *title;
840 87e3e801 2021-07-17 op char *nl, *line = NULL;
841 87e3e801 2021-07-17 op uint32_t flags;
842 87e3e801 2021-07-17 op size_t linesize = 0;
843 87e3e801 2021-07-17 op ssize_t linelen;
844 87e3e801 2021-07-17 op FILE *session;
845 259cf35a 2021-07-18 op struct tab *tab, *curr = NULL;
846 87e3e801 2021-07-17 op
847 87e3e801 2021-07-17 op if ((session = fopen(session_file, "r")) == NULL) {
848 87e3e801 2021-07-17 op /* first time? */
849 bd3a3a95 2021-07-20 op new_tab("about:new", NULL);
850 bd3a3a95 2021-07-20 op switch_to_tab(new_tab("about:help", NULL));
851 87e3e801 2021-07-17 op return;
852 87e3e801 2021-07-17 op }
853 87e3e801 2021-07-17 op
854 87e3e801 2021-07-17 op while ((linelen = getline(&line, &linesize, session)) != -1) {
855 87e3e801 2021-07-17 op if ((nl = strchr(line, '\n')) != NULL)
856 87e3e801 2021-07-17 op *nl = '\0';
857 2f524b17 2021-07-17 op parse_session_line(line, &title, &flags);
858 bd3a3a95 2021-07-20 op if ((tab = new_tab(line, NULL)) == NULL)
859 2f524b17 2021-07-17 op err(1, "new_tab");
860 2f524b17 2021-07-17 op strlcpy(tab->buffer.page.title, title,
861 2f524b17 2021-07-17 op sizeof(tab->buffer.page.title));
862 87e3e801 2021-07-17 op if (flags & TAB_CURRENT)
863 87e3e801 2021-07-17 op curr = tab;
864 87e3e801 2021-07-17 op }
865 87e3e801 2021-07-17 op
866 87e3e801 2021-07-17 op if (ferror(session))
867 87e3e801 2021-07-17 op message("error reading %s: %s",
868 87e3e801 2021-07-17 op session_file, strerror(errno));
869 87e3e801 2021-07-17 op fclose(session);
870 87e3e801 2021-07-17 op free(line);
871 87e3e801 2021-07-17 op
872 87e3e801 2021-07-17 op if (curr != NULL)
873 87e3e801 2021-07-17 op switch_to_tab(curr);
874 87e3e801 2021-07-17 op
875 b6171794 2021-07-20 op if (last_time_crashed())
876 bd3a3a95 2021-07-20 op switch_to_tab(new_tab("about:crash", NULL));
877 b6171794 2021-07-20 op
878 87e3e801 2021-07-17 op return;
879 d2544989 2021-07-08 op }
880 d2544989 2021-07-08 op
881 6cc5fcfe 2021-07-08 op static pid_t
882 6cc5fcfe 2021-07-08 op start_child(enum telescope_process p, const char *argv0, int fd)
883 6cc5fcfe 2021-07-08 op {
884 6cc5fcfe 2021-07-08 op const char *argv[4];
885 6cc5fcfe 2021-07-08 op int argc = 0;
886 6cc5fcfe 2021-07-08 op pid_t pid;
887 6cc5fcfe 2021-07-08 op
888 6cc5fcfe 2021-07-08 op switch (pid = fork()) {
889 6cc5fcfe 2021-07-08 op case -1:
890 6cc5fcfe 2021-07-08 op die();
891 6cc5fcfe 2021-07-08 op case 0:
892 6cc5fcfe 2021-07-08 op break;
893 6cc5fcfe 2021-07-08 op default:
894 6cc5fcfe 2021-07-08 op close(fd);
895 6cc5fcfe 2021-07-08 op return pid;
896 6cc5fcfe 2021-07-08 op }
897 6cc5fcfe 2021-07-08 op
898 6cc5fcfe 2021-07-08 op if (dup2(fd, 3) == -1)
899 6cc5fcfe 2021-07-08 op err(1, "cannot setup imsg fd");
900 6cc5fcfe 2021-07-08 op
901 6cc5fcfe 2021-07-08 op argv[argc++] = argv0;
902 6cc5fcfe 2021-07-08 op switch (p) {
903 6cc5fcfe 2021-07-08 op case PROC_UI:
904 6cc5fcfe 2021-07-08 op errx(1, "Can't start ui process");
905 6cc5fcfe 2021-07-08 op case PROC_FS:
906 6cc5fcfe 2021-07-08 op argv[argc++] = "-Tf";
907 6cc5fcfe 2021-07-08 op break;
908 6cc5fcfe 2021-07-08 op case PROC_NET:
909 6cc5fcfe 2021-07-08 op argv[argc++] = "-Tn";
910 6cc5fcfe 2021-07-08 op break;
911 6cc5fcfe 2021-07-08 op }
912 6cc5fcfe 2021-07-08 op
913 6cc5fcfe 2021-07-08 op argv[argc++] = NULL;
914 6cc5fcfe 2021-07-08 op execvp(argv0, (char *const *)argv);
915 6cc5fcfe 2021-07-08 op err(1, "execvp(%s)", argv0);
916 6cc5fcfe 2021-07-08 op }
917 6cc5fcfe 2021-07-08 op
918 bc10f6a5 2021-07-12 op static int
919 bc10f6a5 2021-07-12 op ui_send_net(int type, uint32_t peerid, const void *data,
920 bc10f6a5 2021-07-12 op uint16_t datalen)
921 bc10f6a5 2021-07-12 op {
922 bc10f6a5 2021-07-12 op return imsg_compose_event(iev_net, type, peerid, 0, -1, data,
923 bc10f6a5 2021-07-12 op datalen);
924 bc10f6a5 2021-07-12 op }
925 bc10f6a5 2021-07-12 op
926 bc10f6a5 2021-07-12 op static int
927 bc10f6a5 2021-07-12 op ui_send_fs(int type, uint32_t peerid, const void *data, uint16_t datalen)
928 bc10f6a5 2021-07-12 op {
929 bc10f6a5 2021-07-12 op return imsg_compose_event(iev_fs, type, peerid, 0, -1, data,
930 bc10f6a5 2021-07-12 op datalen);
931 bc10f6a5 2021-07-12 op }
932 bc10f6a5 2021-07-12 op
933 6cc5fcfe 2021-07-08 op static void __attribute__((noreturn))
934 6cc5fcfe 2021-07-08 op usage(int r)
935 d2544989 2021-07-08 op {
936 dc761924 2021-07-15 op fprintf(stderr, "USAGE: %s [-hnv] [-c config] [url]\n",
937 dc761924 2021-07-15 op getprogname());
938 d2544989 2021-07-08 op fprintf(stderr, "version: " PACKAGE " " VERSION "\n");
939 6cc5fcfe 2021-07-08 op exit(r);
940 740f578b 2021-03-15 op }
941 740f578b 2021-03-15 op
942 5e11c00c 2021-03-02 op int
943 6cd6a9e1 2021-03-20 op main(int argc, char * const *argv)
944 5e11c00c 2021-03-02 op {
945 bc10f6a5 2021-07-12 op struct imsgev net_ibuf, fs_ibuf;
946 bc10f6a5 2021-07-12 op int pipe2net[2], pipe2fs[2];
947 d2544989 2021-07-08 op int ch, configtest = 0, fail = 0;
948 d2544989 2021-07-08 op int has_url = 0;
949 6cc5fcfe 2021-07-08 op int proc = -1;
950 d0fd368a 2021-07-15 op int sessionfd;
951 d2544989 2021-07-08 op char path[PATH_MAX];
952 6cc5fcfe 2021-07-08 op const char *url = NEW_TAB_URL;
953 6cc5fcfe 2021-07-08 op const char *argv0;
954 5e11c00c 2021-03-02 op
955 6cc5fcfe 2021-07-08 op argv0 = argv[0];
956 6cc5fcfe 2021-07-08 op
957 5e11c00c 2021-03-02 op signal(SIGCHLD, SIG_IGN);
958 a0e91173 2021-06-13 op signal(SIGPIPE, SIG_IGN);
959 5e11c00c 2021-03-02 op
960 d2544989 2021-07-08 op if (getenv("NO_COLOR") != NULL)
961 d2544989 2021-07-08 op enable_colors = 0;
962 d2544989 2021-07-08 op
963 d2544989 2021-07-08 op strlcpy(path, getenv("HOME"), sizeof(path));
964 d2544989 2021-07-08 op strlcat(path, "/.telescope/config", sizeof(path));
965 d2544989 2021-07-08 op
966 dc761924 2021-07-15 op while ((ch = getopt_long(argc, argv, opts, longopts, NULL)) != -1) {
967 d2544989 2021-07-08 op switch (ch) {
968 1853ee50 2021-07-15 op case 'C':
969 1853ee50 2021-07-15 op exit(ui_print_colors());
970 d2544989 2021-07-08 op case 'c':
971 d2544989 2021-07-08 op fail = 1;
972 d2544989 2021-07-08 op strlcpy(path, optarg, sizeof(path));
973 d2544989 2021-07-08 op break;
974 d2544989 2021-07-08 op case 'n':
975 d2544989 2021-07-08 op configtest = 1;
976 d2544989 2021-07-08 op break;
977 d2544989 2021-07-08 op case 'h':
978 6cc5fcfe 2021-07-08 op usage(0);
979 6cc5fcfe 2021-07-08 op case 'T':
980 6cc5fcfe 2021-07-08 op switch (*optarg) {
981 6cc5fcfe 2021-07-08 op case 'f':
982 6cc5fcfe 2021-07-08 op proc = PROC_FS;
983 6cc5fcfe 2021-07-08 op break;
984 6cc5fcfe 2021-07-08 op case 'n':
985 6cc5fcfe 2021-07-08 op proc = PROC_NET;
986 6cc5fcfe 2021-07-08 op break;
987 6cc5fcfe 2021-07-08 op default:
988 6cc5fcfe 2021-07-08 op errx(1, "invalid process spec %c",
989 6cc5fcfe 2021-07-08 op *optarg);
990 6cc5fcfe 2021-07-08 op }
991 6cc5fcfe 2021-07-08 op break;
992 dc761924 2021-07-15 op case 'v':
993 dc761924 2021-07-15 op printf("%s %s\n", PACKAGE_NAME, PACKAGE_VERSION);
994 dc761924 2021-07-15 op exit(0);
995 dc761924 2021-07-15 op break;
996 d2544989 2021-07-08 op default:
997 6cc5fcfe 2021-07-08 op usage(1);
998 d2544989 2021-07-08 op }
999 d2544989 2021-07-08 op }
1000 d2544989 2021-07-08 op
1001 d2544989 2021-07-08 op argc -= optind;
1002 d2544989 2021-07-08 op argv += optind;
1003 d2544989 2021-07-08 op
1004 6cc5fcfe 2021-07-08 op if (proc != -1) {
1005 6cc5fcfe 2021-07-08 op if (argc > 0)
1006 6cc5fcfe 2021-07-08 op usage(1);
1007 6cc5fcfe 2021-07-08 op else if (proc == PROC_FS)
1008 6cc5fcfe 2021-07-08 op return fs_main();
1009 6cc5fcfe 2021-07-08 op else if (proc == PROC_NET)
1010 6cc5fcfe 2021-07-08 op return client_main();
1011 6cc5fcfe 2021-07-08 op else
1012 6cc5fcfe 2021-07-08 op usage(1);
1013 6cc5fcfe 2021-07-08 op }
1014 6cc5fcfe 2021-07-08 op
1015 d2544989 2021-07-08 op if (argc != 0) {
1016 d2544989 2021-07-08 op has_url = 1;
1017 d2544989 2021-07-08 op url = argv[0];
1018 d2544989 2021-07-08 op }
1019 d2544989 2021-07-08 op
1020 d5bdf203 2021-07-08 op /* setup keys before reading the config */
1021 d5bdf203 2021-07-08 op TAILQ_INIT(&global_map.m);
1022 d5bdf203 2021-07-08 op global_map.unhandled_input = global_key_unbound;
1023 d5bdf203 2021-07-08 op TAILQ_INIT(&minibuffer_map.m);
1024 d5bdf203 2021-07-08 op
1025 d2544989 2021-07-08 op config_init();
1026 d2544989 2021-07-08 op parseconfig(path, fail);
1027 d2544989 2021-07-08 op if (configtest){
1028 d2544989 2021-07-08 op puts("config OK");
1029 d2544989 2021-07-08 op exit(0);
1030 d2544989 2021-07-08 op }
1031 d2544989 2021-07-08 op
1032 d0fd368a 2021-07-15 op fs_init();
1033 d0fd368a 2021-07-15 op if ((sessionfd = lock_session()) == -1)
1034 d0fd368a 2021-07-15 op errx(1, "can't lock session, is another instance of "
1035 d0fd368a 2021-07-15 op "telescope already running?");
1036 d0fd368a 2021-07-15 op
1037 6cc5fcfe 2021-07-08 op /* Start children. */
1038 bc10f6a5 2021-07-12 op if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe2fs) == -1)
1039 5e11c00c 2021-03-02 op err(1, "socketpair");
1040 bc10f6a5 2021-07-12 op start_child(PROC_FS, argv0, pipe2fs[1]);
1041 bc10f6a5 2021-07-12 op imsg_init(&fs_ibuf.ibuf, pipe2fs[0]);
1042 bc10f6a5 2021-07-12 op iev_fs = &fs_ibuf;
1043 bc10f6a5 2021-07-12 op iev_fs->handler = handle_dispatch_imsg;
1044 5e11c00c 2021-03-02 op
1045 bc10f6a5 2021-07-12 op if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe2net) == -1)
1046 35e1f40a 2021-03-14 op err(1, "socketpair");
1047 bc10f6a5 2021-07-12 op start_child(PROC_NET, argv0, pipe2net[1]);
1048 bc10f6a5 2021-07-12 op imsg_init(&net_ibuf.ibuf, pipe2net[0]);
1049 bc10f6a5 2021-07-12 op iev_net = &net_ibuf;
1050 bc10f6a5 2021-07-12 op iev_net->handler = handle_dispatch_imsg;
1051 5e11c00c 2021-03-02 op
1052 37d429b0 2021-04-01 op setproctitle("ui");
1053 35e1f40a 2021-03-14 op
1054 6cc5fcfe 2021-07-08 op /* initialize tofu & load certificates */
1055 3768e50f 2021-04-25 op tofu_init(&certs, 5, offsetof(struct tofu_entry, domain));
1056 3a227e9a 2021-03-18 op load_certs(&certs);
1057 cbcc75fb 2021-03-17 op
1058 5e11c00c 2021-03-02 op event_init();
1059 5e11c00c 2021-03-02 op
1060 bc10f6a5 2021-07-12 op /* Setup event handlers for pipes to fs/net */
1061 bc10f6a5 2021-07-12 op iev_fs->events = EV_READ;
1062 bc10f6a5 2021-07-12 op event_set(&iev_fs->ev, iev_fs->ibuf.fd, iev_fs->events,
1063 bc10f6a5 2021-07-12 op iev_fs->handler, iev_fs);
1064 bc10f6a5 2021-07-12 op event_add(&iev_fs->ev, NULL);
1065 5e11c00c 2021-03-02 op
1066 bc10f6a5 2021-07-12 op iev_net->events = EV_READ;
1067 bc10f6a5 2021-07-12 op event_set(&iev_net->ev, iev_net->ibuf.fd, iev_net->events,
1068 bc10f6a5 2021-07-12 op iev_net->handler, iev_net);
1069 bc10f6a5 2021-07-12 op event_add(&iev_net->ev, NULL);
1070 35e1f40a 2021-03-14 op
1071 d2544989 2021-07-08 op if (ui_init()) {
1072 87e3e801 2021-07-17 op load_last_session();
1073 d2544989 2021-07-08 op if (has_url || TAILQ_EMPTY(&tabshead))
1074 bd3a3a95 2021-07-20 op new_tab(url, NULL);
1075 d2544989 2021-07-08 op
1076 941b3761 2021-03-18 op sandbox_ui_process();
1077 2ddbda6b 2021-07-17 op ui_main_loop();
1078 941b3761 2021-03-18 op ui_end();
1079 941b3761 2021-03-18 op }
1080 5e11c00c 2021-03-02 op
1081 bc10f6a5 2021-07-12 op ui_send_fs(IMSG_QUIT, 0, NULL, 0);
1082 bc10f6a5 2021-07-12 op ui_send_net(IMSG_QUIT, 0, NULL, 0);
1083 3e5fd7be 2021-07-13 op imsg_flush(&iev_fs->ibuf);
1084 a9925134 2021-07-15 op imsg_flush(&iev_net->ibuf);
1085 5e11c00c 2021-03-02 op
1086 d0fd368a 2021-07-15 op close(sessionfd);
1087 d0fd368a 2021-07-15 op
1088 5e11c00c 2021-03-02 op return 0;
1089 5e11c00c 2021-03-02 op }