2 b0a6bcf7 2022-09-13 op * Copyright (c) 2022 Omar Polo <op@omarpolo.com>
3 b0a6bcf7 2022-09-13 op * Copyright (c) 2006 - 2015 Reyk Floeter <reyk@openbsd.org>
5 b0a6bcf7 2022-09-13 op * Permission to use, copy, modify, and distribute this software for any
6 b0a6bcf7 2022-09-13 op * purpose with or without fee is hereby granted, provided that the above
7 b0a6bcf7 2022-09-13 op * copyright notice and this permission notice appear in all copies.
9 b0a6bcf7 2022-09-13 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 b0a6bcf7 2022-09-13 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 b0a6bcf7 2022-09-13 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 b0a6bcf7 2022-09-13 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 b0a6bcf7 2022-09-13 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 b0a6bcf7 2022-09-13 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 b0a6bcf7 2022-09-13 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 b0a6bcf7 2022-09-13 op #include <sys/types.h>
19 b0a6bcf7 2022-09-13 op #include <sys/queue.h>
20 b0a6bcf7 2022-09-13 op #include <sys/tree.h>
21 b0a6bcf7 2022-09-13 op #include <sys/socket.h>
22 b0a6bcf7 2022-09-13 op #include <sys/uio.h>
24 b0a6bcf7 2022-09-13 op #include <netinet/in.h>
25 b0a6bcf7 2022-09-13 op #include <arpa/inet.h>
26 b0a6bcf7 2022-09-13 op #include <netdb.h>
28 b0a6bcf7 2022-09-13 op #include <asr.h>
29 b0a6bcf7 2022-09-13 op #include <ctype.h>
30 b0a6bcf7 2022-09-13 op #include <errno.h>
31 b0a6bcf7 2022-09-13 op #include <event.h>
32 33f4bf26 2022-09-14 op #include <fnmatch.h>
33 b0a6bcf7 2022-09-13 op #include <limits.h>
34 b0a6bcf7 2022-09-13 op #include <stdlib.h>
35 b0a6bcf7 2022-09-13 op #include <stdint.h>
36 b0a6bcf7 2022-09-13 op #include <stdio.h>
37 b0a6bcf7 2022-09-13 op #include <string.h>
38 b0a6bcf7 2022-09-13 op #include <imsg.h>
39 b0a6bcf7 2022-09-13 op #include <tls.h>
40 b0a6bcf7 2022-09-13 op #include <unistd.h>
42 83f0f95a 2022-09-29 op #include "config.h"
43 b0a6bcf7 2022-09-13 op #include "log.h"
44 b0a6bcf7 2022-09-13 op #include "proc.h"
45 bff58270 2022-09-19 op #include "tmpl.h"
47 b0a6bcf7 2022-09-13 op #include "galileo.h"
49 b0a6bcf7 2022-09-13 op #define MINIMUM(a, b) ((a) < (b) ? (a) : (b))
51 83f0f95a 2022-09-29 op #if HAVE_LIBEVENT2
52 83f0f95a 2022-09-29 op # define G_TOUT(t) ((t).tv_sec)
54 83f0f95a 2022-09-29 op # define G_TOUT(t) (t)
57 b0a6bcf7 2022-09-13 op /* provided by OpenBSD' base libevent but not in any header? */
58 b0a6bcf7 2022-09-13 op extern void bufferevent_read_pressure_cb(struct evbuffer *, size_t,
59 b0a6bcf7 2022-09-13 op size_t, void *);
61 b0a6bcf7 2022-09-13 op void proxy_init(struct privsep *, struct privsep_proc *, void *);
62 b0a6bcf7 2022-09-13 op int proxy_launch(struct galileo *);
63 b0a6bcf7 2022-09-13 op void proxy_inflight_dec(const char *);
64 b0a6bcf7 2022-09-13 op int proxy_dispatch_parent(int, struct privsep_proc *, struct imsg *);
65 cb05cce7 2022-11-11 op int proxy_resurl(struct client *, const char *, char *, size_t);
66 c9dc4bc6 2022-09-13 op void proxy_translate_gemtext(struct client *);
67 b0a6bcf7 2022-09-13 op void proxy_resolved(struct asr_result *, void *);
68 b0a6bcf7 2022-09-13 op void proxy_connect(int, short, void *);
69 5163fbe9 2022-09-20 op int proxy_start_reply(struct client *, int, const char *);
70 b0a6bcf7 2022-09-13 op void proxy_read(struct bufferevent *, void *);
71 b0a6bcf7 2022-09-13 op void proxy_write(struct bufferevent *, void *);
72 b0a6bcf7 2022-09-13 op void proxy_error(struct bufferevent *, short, void *);
73 b0a6bcf7 2022-09-13 op int proxy_bufferevent_add(struct event *, int);
74 b0a6bcf7 2022-09-13 op void proxy_tls_writecb(int, short, void *);
75 b0a6bcf7 2022-09-13 op void proxy_tls_readcb(int, short, void *);
77 b0a6bcf7 2022-09-13 op static struct privsep_proc procs[] = {
78 b0a6bcf7 2022-09-13 op { "parent", PROC_PARENT, proxy_dispatch_parent },
81 b0a6bcf7 2022-09-13 op volatile int proxy_clients;
82 b0a6bcf7 2022-09-13 op volatile int proxy_inflight;
83 b0a6bcf7 2022-09-13 op uint32_t proxy_fcg_id;
86 b0a6bcf7 2022-09-13 op proxy(struct privsep *ps, struct privsep_proc *p)
88 b0a6bcf7 2022-09-13 op proc_run(ps, p, procs, nitems(procs), proxy_init, NULL);
92 b0a6bcf7 2022-09-13 op proxy_init(struct privsep *ps, struct privsep_proc *p, void *arg)
94 b0a6bcf7 2022-09-13 op if (config_init(ps->ps_env) == -1)
95 b0a6bcf7 2022-09-13 op fatal("failed to initialize configuration");
97 b0a6bcf7 2022-09-13 op /* We use a custom shutdown callback */
98 b0a6bcf7 2022-09-13 op /* p->p_shutdown = proxy_shutdown */
100 b0a6bcf7 2022-09-13 op if (pledge("stdio recvfd unix inet dns", NULL) == -1)
101 b0a6bcf7 2022-09-13 op fatal("pledge");
105 b0a6bcf7 2022-09-13 op proxy_launch(struct galileo *env)
107 b0a6bcf7 2022-09-13 op event_add(&env->sc_evsock, NULL);
112 40f762b2 2022-09-23 op proxy_purge(struct proxy *pr)
118 b0a6bcf7 2022-09-13 op proxy_inflight_dec(const char *why)
120 b0a6bcf7 2022-09-13 op proxy_inflight--;
121 b0a6bcf7 2022-09-13 op log_debug("%s: inflight decremented, now %d, %s",
122 b0a6bcf7 2022-09-13 op __func__, proxy_inflight, why);
126 b0a6bcf7 2022-09-13 op proxy_dispatch_parent(int fd, struct privsep_proc *p, struct imsg *imsg)
128 b0a6bcf7 2022-09-13 op struct privsep *ps = p->p_ps;
129 b0a6bcf7 2022-09-13 op struct galileo *env = ps->ps_env;
131 b0a6bcf7 2022-09-13 op switch (imsg->hdr.type) {
132 b0a6bcf7 2022-09-13 op case IMSG_CFG_SRV:
133 afb86b2c 2022-09-23 op if (config_getproxy(env, imsg) == -1)
134 b0a6bcf7 2022-09-13 op fatal("config_getproxy");
136 b0a6bcf7 2022-09-13 op case IMSG_CFG_SOCK:
137 b0a6bcf7 2022-09-13 op /* XXX: improve */
139 b0a6bcf7 2022-09-13 op env->sc_sock_fd = config_getsock(env, imsg);
140 b0a6bcf7 2022-09-13 op if (env->sc_sock_fd == -1)
141 b0a6bcf7 2022-09-13 op fatal("config_getsock");
143 b0a6bcf7 2022-09-13 op event_set(&env->sc_evsock, env->sc_sock_fd,
144 ea8874f1 2022-09-13 op EV_READ | EV_PERSIST, fcgi_accept, env);
145 ea8874f1 2022-09-13 op evtimer_set(&env->sc_evpause, fcgi_accept, env);
147 b0a6bcf7 2022-09-13 op case IMSG_CFG_DONE:
148 40f762b2 2022-09-23 op config_getcfg(env, imsg);
149 40f762b2 2022-09-23 op proxy_launch(env);
151 b0a6bcf7 2022-09-13 op case IMSG_CTL_START:
153 40f762b2 2022-09-23 op case IMSG_CTL_RESET:
154 40f762b2 2022-09-23 op config_getreset(env, imsg);
164 cb05cce7 2022-11-11 op portok(const char **url, struct client *clt)
166 cb05cce7 2022-11-11 op const char *u = *url;
167 cb05cce7 2022-11-11 op const char *port = clt->clt_pc->proxy_port;
170 cb05cce7 2022-11-11 op len = strlen(port);
172 cb05cce7 2022-11-11 op if (*u == '\0' || *u == '/' || *u == '?' || *u == '#')
174 cb05cce7 2022-11-11 op if (*u != ':')
177 cb05cce7 2022-11-11 op if (strncmp(u, port, len) != 0)
180 cb05cce7 2022-11-11 op if (*u != '\0' && *u != '/' && *u != '?' && *u != '#')
187 cb05cce7 2022-11-11 op proxy_resurl(struct client *clt, const char *url, char *buf, size_t len)
189 cb05cce7 2022-11-11 op const char *tmp;
192 a1cf3ab5 2022-11-11 op if (clt->clt_server_name == NULL)
195 cb05cce7 2022-11-11 op l = strlen(clt->clt_server_name);
197 cb05cce7 2022-11-11 op if (len == 0) {
198 cb05cce7 2022-11-11 op log_warn("%s: zero-sized buffer!", __func__);
202 cb05cce7 2022-11-11 op /* look if it's an absolute URI */
203 cb05cce7 2022-11-11 op if (strncmp(url, "//", 2) == 0) {
204 cb05cce7 2022-11-11 op tmp = url + 2;
205 cb05cce7 2022-11-11 op if (strncmp(tmp, clt->clt_server_name, l) != 0)
209 cb05cce7 2022-11-11 op if (!portok(&tmp, clt))
212 cb05cce7 2022-11-11 op } else if (strncmp(url, "gemini://", 9) == 0) {
213 cb05cce7 2022-11-11 op tmp = url + 9;
214 cb05cce7 2022-11-11 op if (strncmp(tmp, clt->clt_server_name, l) != 0)
218 cb05cce7 2022-11-11 op if (!portok(&tmp, clt))
223 cb05cce7 2022-11-11 op while (isalpha((unsigned char)*tmp) || *tmp == '+')
225 cb05cce7 2022-11-11 op if (strncmp(tmp, "://", 3) == 0)
229 cb05cce7 2022-11-11 op /* maybe it's an absolute path? */
230 cb05cce7 2022-11-11 op if (*url == '\0' || *url == '/') {
231 cb05cce7 2022-11-11 op if (strlcpy(buf, clt->clt_script_name, len) >= len)
233 cb05cce7 2022-11-11 op if (strlcat(buf, url + 1, len) >= len)
238 cb05cce7 2022-11-11 op /* otherwise, leave it as it is */
240 cb05cce7 2022-11-11 op if (strlcpy(buf, url, len) >= len)
245 da7b17a3 2023-03-03 op static inline int
246 da7b17a3 2023-03-03 op match_image_heur(const char *url)
248 da7b17a3 2023-03-03 op return (fnmatch("*.jpg", url, 0) == 0 ||
249 da7b17a3 2023-03-03 op fnmatch("*.jpeg", url, 0) == 0 ||
250 da7b17a3 2023-03-03 op fnmatch("*.gif", url, 0) == 0 ||
251 da7b17a3 2023-03-03 op fnmatch("*.png", url, 0) == 0 ||
252 da7b17a3 2023-03-03 op fnmatch("*.svg", url, 0) == 0 ||
253 da7b17a3 2023-03-03 op fnmatch("*.webp", url, 0) == 0);
257 c9dc4bc6 2022-09-13 op gemtext_translate_line(struct client *clt, char *line)
259 18906bcc 2023-09-13 op struct template *tp = clt->clt_tp;
260 cb05cce7 2022-11-11 op char buf[1025];
263 c9dc4bc6 2022-09-13 op /* preformatted line / closing */
264 b9de0a74 2022-09-14 op if (clt->clt_translate & TR_PRE) {
265 c9dc4bc6 2022-09-13 op if (!strncmp(line, "```", 3)) {
266 b9de0a74 2022-09-14 op clt->clt_translate &= ~TR_PRE;
267 ceb3f79c 2022-09-29 op return (tp_pre_close(clt->clt_tp));
270 df2db214 2022-09-19 op if (tp_htmlescape(clt->clt_tp, line) == -1)
272 18906bcc 2023-09-13 op return (tp_write(tp, "\n", 1));
276 b9de0a74 2022-09-14 op if (!strncmp(line, "* ", 2)) {
277 b9de0a74 2022-09-14 op if (clt->clt_translate & TR_NAV) {
278 18906bcc 2023-09-13 op if (tp_writes(tp, "</ul></nav>") == -1)
280 b9de0a74 2022-09-14 op clt->clt_translate &= ~TR_NAV;
283 b9de0a74 2022-09-14 op if (!(clt->clt_translate & TR_LIST)) {
284 18906bcc 2023-09-13 op if (tp_writes(tp, "<ul>") == -1)
286 b9de0a74 2022-09-14 op clt->clt_translate |= TR_LIST;
289 18906bcc 2023-09-13 op if (tp_writes(tp, "<li>") == -1 ||
290 df2db214 2022-09-19 op tp_htmlescape(clt->clt_tp, line + 2) == -1 ||
291 18906bcc 2023-09-13 op tp_writes(tp, "</li>") == -1)
296 b9de0a74 2022-09-14 op if (clt->clt_translate & TR_LIST) {
297 18906bcc 2023-09-13 op if (tp_writes(tp, "</ul>") == -1)
299 b9de0a74 2022-09-14 op clt->clt_translate &= ~TR_LIST;
303 b9de0a74 2022-09-14 op if (!strncmp(line, "=>", 2)) {
307 b9de0a74 2022-09-14 op line += strspn(line, " \t");
309 b9de0a74 2022-09-14 op label = line + strcspn(line, " \t");
310 b9de0a74 2022-09-14 op if (*label == '\0')
311 b9de0a74 2022-09-14 op label = line;
313 b9de0a74 2022-09-14 op *label++ = '\0';
314 cd396568 2022-11-11 op label += strspn(label, " \t");
315 cd396568 2022-11-11 op if (*label == '\0')
316 cd396568 2022-11-11 op label = line;
319 cb05cce7 2022-11-11 op if (proxy_resurl(clt, line, buf, sizeof(buf)) == 0)
322 cb05cce7 2022-11-11 op url = line; /* leave the URL as it is */
324 da7b17a3 2023-03-03 op if (!(clt->clt_pc->flags & PROXY_NO_IMGPRV) &&
325 da7b17a3 2023-03-03 op match_image_heur(url)) {
326 33f4bf26 2022-09-14 op if (clt->clt_translate & TR_NAV) {
327 18906bcc 2023-09-13 op if (tp_writes(tp, "</ul></nav>") == -1)
329 33f4bf26 2022-09-14 op clt->clt_translate &= ~TR_NAV;
332 18906bcc 2023-09-13 op if (tp_figure(tp, url, label) == -1)
338 33f4bf26 2022-09-14 op if (!(clt->clt_translate & TR_NAV)) {
339 18906bcc 2023-09-13 op if (tp_writes(tp, "<nav><ul>") == -1)
341 33f4bf26 2022-09-14 op clt->clt_translate |= TR_NAV;
344 18906bcc 2023-09-13 op if (tp_writes(tp, "<li><a href='") == -1)
347 cb05cce7 2022-11-11 op if (tp_urlescape(clt->clt_tp, url) == -1 ||
348 18906bcc 2023-09-13 op tp_writes(tp, "'>") == -1 ||
349 df2db214 2022-09-19 op tp_htmlescape(clt->clt_tp, label) == -1 ||
350 18906bcc 2023-09-13 op tp_writes(tp, "</a></li>") == -1)
356 b9de0a74 2022-09-14 op if (clt->clt_translate & TR_NAV) {
357 18906bcc 2023-09-13 op if (tp_writes(tp, "</ul></nav>") == -1)
359 b9de0a74 2022-09-14 op clt->clt_translate &= ~TR_NAV;
362 c9dc4bc6 2022-09-13 op /* pre opening */
363 c9dc4bc6 2022-09-13 op if (!strncmp(line, "```", 3)) {
365 ceb3f79c 2022-09-29 op line += strspn(line, " \t");
367 b9de0a74 2022-09-14 op clt->clt_translate |= TR_PRE;
368 18906bcc 2023-09-13 op return (tp_pre_open(tp, line));
371 c9dc4bc6 2022-09-13 op /* citation block */
372 c9dc4bc6 2022-09-13 op if (*line == '>') {
373 18906bcc 2023-09-13 op if (tp_writes(tp, "<blockquote>") == -1 ||
374 df2db214 2022-09-19 op tp_htmlescape(clt->clt_tp, line + 1) == -1 ||
375 18906bcc 2023-09-13 op tp_writes(tp, "</blockquote>") == -1)
380 c9dc4bc6 2022-09-13 op /* headings */
381 c9dc4bc6 2022-09-13 op if (!strncmp(line, "###", 3)) {
382 18906bcc 2023-09-13 op if (tp_writes(tp, "<h3>") == -1 ||
383 df2db214 2022-09-19 op tp_htmlescape(clt->clt_tp, line + 3) == -1 ||
384 18906bcc 2023-09-13 op tp_writes(tp, "</h3>") == -1)
388 c9dc4bc6 2022-09-13 op if (!strncmp(line, "##", 2)) {
389 18906bcc 2023-09-13 op if (tp_writes(tp, "<h2>") == -1 ||
390 df2db214 2022-09-19 op tp_htmlescape(clt->clt_tp, line + 2) == -1 ||
391 18906bcc 2023-09-13 op tp_writes(tp, "</h2>") == -1)
395 c9dc4bc6 2022-09-13 op if (!strncmp(line, "#", 1)) {
396 18906bcc 2023-09-13 op if (tp_writes(tp, "<h1>") == -1 ||
397 df2db214 2022-09-19 op tp_htmlescape(clt->clt_tp, line + 1) == -1 ||
398 18906bcc 2023-09-13 op tp_writes(tp, "</h1>") == -1)
403 df5279ca 2022-09-14 op /* Not following strictly the gemini specification... */
404 df5279ca 2022-09-14 op if (*line == '\0')
407 c9dc4bc6 2022-09-13 op /* paragraph */
408 18906bcc 2023-09-13 op if (tp_writes(tp, "<p>") == -1 ||
409 df2db214 2022-09-19 op tp_htmlescape(clt->clt_tp, line) == -1 ||
410 18906bcc 2023-09-13 op tp_writes(tp, "</p>") == -1)
417 c9dc4bc6 2022-09-13 op proxy_translate_gemtext(struct client *clt)
419 c9dc4bc6 2022-09-13 op struct bufferevent *bev = clt->clt_bev;
420 c9dc4bc6 2022-09-13 op struct evbuffer *src = EVBUFFER_INPUT(bev);
426 b9de0a74 2022-09-14 op line = evbuffer_readln(src, &len, EVBUFFER_EOL_CRLF);
427 c9dc4bc6 2022-09-13 op if (line == NULL)
430 c9dc4bc6 2022-09-13 op r = gemtext_translate_line(clt, line);
437 f774ad1e 2022-09-23 op struct proxy_config *
438 f774ad1e 2022-09-23 op proxy_match(struct galileo *env, const char *name)
440 afb86b2c 2022-09-23 op struct proxy *pr;
442 f774ad1e 2022-09-23 op if (name == NULL)
443 c9e6d547 2022-09-23 op return (NULL);
445 afb86b2c 2022-09-23 op TAILQ_FOREACH(pr, &env->sc_proxies, pr_entry) {
446 f774ad1e 2022-09-23 op if (!strcmp(name, pr->pr_conf.host))
447 c9e6d547 2022-09-23 op return (&pr->pr_conf);
450 c9e6d547 2022-09-23 op return (NULL);
454 b0a6bcf7 2022-09-13 op proxy_start_request(struct galileo *env, struct client *clt)
456 b0a6bcf7 2022-09-13 op struct addrinfo hints;
457 b0a6bcf7 2022-09-13 op struct asr_query *query;
461 f33634c5 2022-09-30 op if (clt->clt_path_info == NULL) {
462 f33634c5 2022-09-30 op log_warnx("PATH_INFO not defined!");
463 f33634c5 2022-09-30 op if (proxy_start_reply(clt, 501, "text/html") == -1)
465 056e82d3 2022-09-30 op if (tp_error(clt->clt_tp, -1, "internal server error") == -1)
467 f33634c5 2022-09-30 op return (fcgi_end_request(clt, 1));
470 f774ad1e 2022-09-23 op if ((clt->clt_pc = proxy_match(env, clt->clt_server_name)) == NULL) {
471 e3c1cbab 2022-09-20 op if (proxy_start_reply(clt, 501, "text/html") == -1)
473 abe17781 2022-09-20 op if (tp_error(clt->clt_tp, -1, "unknown server") == -1)
475 8857131d 2022-09-20 op return (fcgi_end_request(clt, 1));
478 8857131d 2022-09-20 op if (clt->clt_bodylen != 0 && clt->clt_body == NULL) {
479 8857131d 2022-09-20 op if (proxy_start_reply(clt, 400, "text/html") == -1)
481 8857131d 2022-09-20 op if (tp_error(clt->clt_tp, -1, "bad request") == -1)
483 8857131d 2022-09-20 op return (fcgi_end_request(clt, 1));
486 8857131d 2022-09-20 op if (clt->clt_body) {
487 8857131d 2022-09-20 op r = asprintf(&url, "%s%s?%s", clt->clt_script_name,
488 8857131d 2022-09-20 op clt->clt_path_info + 1, clt->clt_body);
490 8857131d 2022-09-20 op return (fcgi_end_request(clt, 1));
492 8857131d 2022-09-20 op if (proxy_start_reply(clt, 302, url) == -1 ||
493 8857131d 2022-09-20 op fcgi_end_request(clt, 1) == -1) {
501 b0a6bcf7 2022-09-13 op memset(&hints, 0, sizeof(hints));
502 b0a6bcf7 2022-09-13 op hints.ai_family = AF_UNSPEC;
503 b0a6bcf7 2022-09-13 op hints.ai_socktype = SOCK_STREAM;
505 26cd3e9d 2022-09-23 op query = getaddrinfo_async(clt->clt_pc->proxy_addr,
506 26cd3e9d 2022-09-23 op clt->clt_pc->proxy_port, &hints, NULL);
507 b0a6bcf7 2022-09-13 op if (query == NULL) {
508 b0a6bcf7 2022-09-13 op log_warn("getaddrinfo_async");
509 8857131d 2022-09-20 op return (fcgi_abort_request(clt));
512 b0a6bcf7 2022-09-13 op clt->clt_evasr = event_asr_run(query, proxy_resolved, clt);
513 b0a6bcf7 2022-09-13 op if (clt->clt_evasr == NULL) {
514 b0a6bcf7 2022-09-13 op log_warn("event_asr_run");
515 b0a6bcf7 2022-09-13 op asr_abort(query);
516 8857131d 2022-09-20 op return (fcgi_abort_request(clt));
523 b0a6bcf7 2022-09-13 op proxy_resolved(struct asr_result *res, void *d)
525 b0a6bcf7 2022-09-13 op struct client *clt = d;
526 b0a6bcf7 2022-09-13 op struct proxy_config *pc = clt->clt_pc;
528 b0a6bcf7 2022-09-13 op clt->clt_evasr = NULL;
530 b0a6bcf7 2022-09-13 op if (res->ar_gai_errno != 0) {
531 26cd3e9d 2022-09-23 op log_warnx("failed to resolve %s:%s: %s",
532 b0a6bcf7 2022-09-13 op pc->proxy_addr, pc->proxy_port,
533 b0a6bcf7 2022-09-13 op gai_strerror(res->ar_gai_errno));
534 e3c1cbab 2022-09-20 op if (proxy_start_reply(clt, 501, "text/html") == -1)
536 abe17781 2022-09-20 op if (tp_error(clt->clt_tp, -1, "Can't resolve host") == -1)
538 b0a6bcf7 2022-09-13 op fcgi_end_request(clt, 1);
542 b0a6bcf7 2022-09-13 op clt->clt_addrinfo = res->ar_addrinfo;
543 b0a6bcf7 2022-09-13 op clt->clt_p = clt->clt_addrinfo;
544 b0a6bcf7 2022-09-13 op proxy_connect(-1, 0, clt);
548 b0a6bcf7 2022-09-13 op proxy_connect(int fd, short ev, void *d)
550 b0a6bcf7 2022-09-13 op struct client *clt = d;
551 b0a6bcf7 2022-09-13 op struct evbuffer *out;
552 b0a6bcf7 2022-09-13 op struct addrinfo *p;
553 b0a6bcf7 2022-09-13 op struct tls_config *conf;
554 b0a6bcf7 2022-09-13 op struct timeval conntv = {5, 0};
556 b0a6bcf7 2022-09-13 op socklen_t len = sizeof(err);
559 b0a6bcf7 2022-09-13 op if (clt->clt_p == NULL)
562 b0a6bcf7 2022-09-13 op if (clt->clt_fd != -1) {
563 b0a6bcf7 2022-09-13 op if (getsockopt(clt->clt_fd, SOL_SOCKET, SO_ERROR, &err, &len)
566 b0a6bcf7 2022-09-13 op if (err != 0) {
573 b0a6bcf7 2022-09-13 op p = clt->clt_p;
574 b0a6bcf7 2022-09-13 op clt->clt_fd = socket(p->ai_family, p->ai_socktype | SOCK_NONBLOCK,
575 b0a6bcf7 2022-09-13 op p->ai_protocol);
576 b0a6bcf7 2022-09-13 op if (clt->clt_fd == -1) {
577 b0a6bcf7 2022-09-13 op clt->clt_p = clt->clt_p->ai_next;
581 b0a6bcf7 2022-09-13 op if (connect(clt->clt_fd, p->ai_addr, p->ai_addrlen) == 0)
584 b0a6bcf7 2022-09-13 op clt->clt_evconn_live = 1;
585 b0a6bcf7 2022-09-13 op event_set(&clt->clt_evconn, clt->clt_fd, EV_WRITE, proxy_connect, clt);
586 b0a6bcf7 2022-09-13 op event_add(&clt->clt_evconn, &conntv);
590 b0a6bcf7 2022-09-13 op clt->clt_evconn_live = 0;
591 b0a6bcf7 2022-09-13 op freeaddrinfo(clt->clt_addrinfo);
592 b0a6bcf7 2022-09-13 op clt->clt_addrinfo = clt->clt_p = NULL;
594 c743c9a1 2022-09-23 op clt->clt_bev = bufferevent_new(clt->clt_fd, proxy_read, proxy_write,
595 c743c9a1 2022-09-23 op proxy_error, clt);
596 c743c9a1 2022-09-23 op if (clt->clt_bev == NULL) {
597 c743c9a1 2022-09-23 op log_warn("bufferevent_new");
601 da7b17a3 2023-03-03 op if (!(clt->clt_pc->flags & PROXY_NO_TLS)) {
602 c743c9a1 2022-09-23 op /* initialize TLS for Gemini */
603 c743c9a1 2022-09-23 op if ((conf = tls_config_new()) == NULL) {
604 c743c9a1 2022-09-23 op log_warn("tls_config_new failed");
608 c743c9a1 2022-09-23 op tls_config_insecure_noverifycert(conf);
610 c743c9a1 2022-09-23 op if ((clt->clt_ctx = tls_client()) == NULL) {
611 c743c9a1 2022-09-23 op log_warnx("tls_client failed");
612 c743c9a1 2022-09-23 op tls_config_free(conf);
616 c743c9a1 2022-09-23 op if (tls_configure(clt->clt_ctx, conf) == -1) {
617 c743c9a1 2022-09-23 op log_warnx("tls_configure failed");
618 c743c9a1 2022-09-23 op tls_config_free(conf);
622 c743c9a1 2022-09-23 op tls_config_free(conf);
624 c743c9a1 2022-09-23 op if (tls_connect_socket(clt->clt_ctx, clt->clt_fd,
625 c743c9a1 2022-09-23 op clt->clt_pc->proxy_name) == -1) {
626 c743c9a1 2022-09-23 op log_warnx("tls_connect_socket failed");
630 c743c9a1 2022-09-23 op event_set(&clt->clt_bev->ev_read, clt->clt_fd, EV_READ,
631 c743c9a1 2022-09-23 op proxy_tls_readcb, clt->clt_bev);
632 c743c9a1 2022-09-23 op event_set(&clt->clt_bev->ev_write, clt->clt_fd, EV_WRITE,
633 c743c9a1 2022-09-23 op proxy_tls_writecb, clt->clt_bev);
635 83f0f95a 2022-09-29 op #if HAVE_LIBEVENT2
636 83f0f95a 2022-09-29 op evbuffer_unfreeze(clt->clt_bev->input, 0);
637 83f0f95a 2022-09-29 op evbuffer_unfreeze(clt->clt_bev->output, 1);
641 b0a6bcf7 2022-09-13 op /* bufferevent_settimeout(); */
642 b0a6bcf7 2022-09-13 op bufferevent_enable(clt->clt_bev, EV_READ|EV_WRITE);
644 b0a6bcf7 2022-09-13 op /* TODO: compute the URL */
645 c743c9a1 2022-09-23 op out = EVBUFFER_OUTPUT(clt->clt_bev);
646 28239bac 2022-09-20 op if (evbuffer_add_printf(out, "gemini://%s/%s",
647 ee80262d 2022-09-13 op clt->clt_pc->proxy_name, clt->clt_path_info) == -1) {
648 28239bac 2022-09-20 op log_warn("bufferevent_printf failed");
652 28239bac 2022-09-20 op if (clt->clt_query &&
653 28239bac 2022-09-20 op evbuffer_add_printf(out, "?%s", clt->clt_query) == -1) {
654 b0a6bcf7 2022-09-13 op log_warn("bufferevent_printf failed");
658 28239bac 2022-09-20 op if (evbuffer_add(out, "\r\n", 2) == -1) {
659 28239bac 2022-09-20 op log_warn("bufferevent_add failed");
666 26cd3e9d 2022-09-23 op log_warn("failed to connect to %s:%s",
667 b0a6bcf7 2022-09-13 op clt->clt_pc->proxy_addr, clt->clt_pc->proxy_port);
668 e3c1cbab 2022-09-20 op if (proxy_start_reply(clt, 501, "text/html") == -1)
670 abe17781 2022-09-20 op if (tp_error(clt->clt_tp, -1, "Can't connect") == -1)
672 b0a6bcf7 2022-09-13 op fcgi_end_request(clt, 1);
675 6edd9a00 2022-09-14 op static inline int
676 6edd9a00 2022-09-14 op parse_mime(struct client *clt, char *mime, char *lang, size_t len)
678 6edd9a00 2022-09-14 op char *t, *semi;
680 d7c5c52a 2023-04-13 op memset(lang, 0, len);
682 6edd9a00 2022-09-14 op if (strncmp(mime, "text/gemini", 11) != 0)
685 6edd9a00 2022-09-14 op clt->clt_translate = TR_ENABLED;
687 6edd9a00 2022-09-14 op if ((mime = strchr(mime, ';')) == NULL)
690 6edd9a00 2022-09-14 op *mime++ = '\0';
691 6edd9a00 2022-09-14 op while ((t = strsep(&mime, ";")) != NULL) {
692 6edd9a00 2022-09-14 op if (!strncmp(t, "charset=", 8)) {
694 6edd9a00 2022-09-14 op if (!strncasecmp(t, "utf8", 4) ||
695 8ab2d60e 2022-09-14 op !strncasecmp(t, "utf-8", 5) ||
696 6edd9a00 2022-09-14 op !strncasecmp(t, "ascii", 5)) {
697 6edd9a00 2022-09-14 op log_debug("unknown charset %s", t);
703 6edd9a00 2022-09-14 op if (!strncmp(t, "lang=", 5)) {
705 6edd9a00 2022-09-14 op if ((semi = strchr(t, ';')) != NULL)
706 6edd9a00 2022-09-14 op *semi = '\0';
708 6edd9a00 2022-09-14 op if (strlcpy(lang, t, len) >= len) {
709 6edd9a00 2022-09-14 op log_debug("lang too long: %s", t);
710 6edd9a00 2022-09-14 op *lang = '\0';
723 5163fbe9 2022-09-20 op proxy_start_reply(struct client *clt, int status, const char *ctype)
725 18906bcc 2023-09-13 op struct template *tp = clt->clt_tp;
726 5163fbe9 2022-09-20 op const char *csp;
728 5163fbe9 2022-09-20 op csp = "Content-Security-Policy: default-src 'self'; "
729 5163fbe9 2022-09-20 op "script-src 'none'; object-src 'none';\r\n";
731 5163fbe9 2022-09-20 op if (status != 200 &&
732 18906bcc 2023-09-13 op tp_writef(tp, "Status: %d\r\n", status) == -1)
735 18906bcc 2023-09-13 op if (tp_writes(tp, csp) == -1)
738 8857131d 2022-09-20 op if (status == 302) {
739 8857131d 2022-09-20 op /* use "ctype" as redirect target */
740 18906bcc 2023-09-13 op if (tp_writef(tp, "Location: %s\r\n", ctype) == -1)
742 18906bcc 2023-09-13 op if (tp_writes(tp, "\r\n") == -1)
747 8857131d 2022-09-20 op if (ctype != NULL) {
748 8857131d 2022-09-20 op if (!strcmp(ctype, "text/html"))
749 8857131d 2022-09-20 op ctype = "text/html;charset=utf-8";
750 18906bcc 2023-09-13 op if (tp_writef(tp, "Content-Type: %s\r\n", ctype)
755 18906bcc 2023-09-13 op if (tp_writes(tp, "\r\n") == -1)
762 b0a6bcf7 2022-09-13 op proxy_read(struct bufferevent *bev, void *d)
764 b0a6bcf7 2022-09-13 op struct client *clt = d;
765 b0a6bcf7 2022-09-13 op struct evbuffer *src = EVBUFFER_INPUT(bev);
766 c9dc4bc6 2022-09-13 op const char *ctype;
767 cb05cce7 2022-11-11 op char buf[1025];
768 6edd9a00 2022-09-14 op char lang[16];
769 6edd9a00 2022-09-14 op char *hdr, *mime;
773 b0a6bcf7 2022-09-13 op if (clt->clt_headersdone) {
774 c9dc4bc6 2022-09-13 op if (clt->clt_translate)
775 c9dc4bc6 2022-09-13 op proxy_translate_gemtext(clt);
777 c9dc4bc6 2022-09-13 op clt_write_bufferevent(clt, bev);
781 b0a6bcf7 2022-09-13 op hdr = evbuffer_readln(src, &len, EVBUFFER_EOL_CRLF_STRICT);
782 b0a6bcf7 2022-09-13 op if (hdr == NULL) {
783 b0a6bcf7 2022-09-13 op if (EVBUFFER_LENGTH(src) >= 1026)
784 b0a6bcf7 2022-09-13 op proxy_error(bev, EV_READ, clt);
788 b0a6bcf7 2022-09-13 op if (len < 4 ||
789 b0a6bcf7 2022-09-13 op !isdigit((unsigned char)hdr[0]) ||
790 b0a6bcf7 2022-09-13 op !isdigit((unsigned char)hdr[1]) ||
791 b0a6bcf7 2022-09-13 op hdr[2] != ' ') {
792 b0a6bcf7 2022-09-13 op log_warnx("invalid ");
793 b0a6bcf7 2022-09-13 op proxy_error(bev, EV_READ, clt);
797 abe17781 2022-09-20 op code = (hdr[0] - '0') * 10 + (hdr[1] - '0');
799 6e8f426e 2022-09-14 op switch (hdr[0]) {
801 8857131d 2022-09-20 op if (proxy_start_reply(clt, 200, "text/html") == -1)
803 8857131d 2022-09-20 op if (tp_inputpage(clt->clt_tp, &hdr[3]) == -1)
805 8857131d 2022-09-20 op fcgi_end_request(clt, 0);
808 6e8f426e 2022-09-14 op /* handled below */
811 cb05cce7 2022-11-11 op if (proxy_resurl(clt, &hdr[3], buf, sizeof(buf)) == 0) {
812 cb05cce7 2022-11-11 op if (proxy_start_reply(clt, 302, buf) == -1)
814 b4f7fe4f 2022-09-21 op fcgi_end_request(clt, 0);
817 02be3606 2022-09-29 op /* fallthrough */
819 e3c1cbab 2022-09-20 op if (proxy_start_reply(clt, 501, "text/html") == -1)
821 abe17781 2022-09-20 op if (tp_error(clt->clt_tp, code, &hdr[3]) == -1)
823 6e8f426e 2022-09-14 op fcgi_end_request(clt, 1);
827 6edd9a00 2022-09-14 op mime = hdr + 2 + strspn(hdr + 2, " \t");
828 6edd9a00 2022-09-14 op if (parse_mime(clt, mime, lang, sizeof(lang)) == -1) {
829 e3c1cbab 2022-09-20 op if (proxy_start_reply(clt, 501, "text/html") == -1)
831 abe17781 2022-09-20 op if (tp_error(clt->clt_tp, -1, "Bad response") == -1)
833 6edd9a00 2022-09-14 op fcgi_end_request(clt, 1);
837 232eee45 2022-09-20 op if (clt->clt_translate)
838 8ab2d60e 2022-09-14 op ctype = "text/html;charset=utf-8";
840 6edd9a00 2022-09-14 op ctype = mime;
842 18906bcc 2023-09-13 op if (tp_writef(clt->clt_tp, "Content-Type: %s\r\n\r\n", ctype) == -1)
845 b0a6bcf7 2022-09-13 op clt->clt_headersdone = 1;
847 bff58270 2022-09-19 op if (clt->clt_translate &&
848 e3c1cbab 2022-09-20 op tp_head(clt->clt_tp, lang, NULL) == -1)
852 1337dc2b 2022-09-14 op * Trigger the read again so we proceed with the response
853 1337dc2b 2022-09-14 op * body, if any.
856 1337dc2b 2022-09-14 op proxy_read(bev, d);
864 b0a6bcf7 2022-09-13 op proxy_write(struct bufferevent *bev, void *d)
870 b0a6bcf7 2022-09-13 op proxy_error(struct bufferevent *bev, short err, void *d)
872 b0a6bcf7 2022-09-13 op struct client *clt = d;
873 18906bcc 2023-09-13 op struct template *tp = clt->clt_tp;
874 b0a6bcf7 2022-09-13 op int status = !(err & EVBUFFER_EOF);
876 b0a6bcf7 2022-09-13 op log_debug("proxy error, shutting down the connection (err: %x)",
879 b0a6bcf7 2022-09-13 op if (!clt->clt_headersdone) {
880 e3c1cbab 2022-09-20 op if (proxy_start_reply(clt, 501, "text/html") == -1)
882 abe17781 2022-09-20 op if (tp_error(clt->clt_tp, -1, "Proxy error") == -1)
884 c9dc4bc6 2022-09-13 op } else if (status == 0) {
885 c42fce9b 2022-09-14 op if (clt->clt_translate & TR_PRE) {
886 ceb3f79c 2022-09-29 op if (tp_pre_close(clt->clt_tp))
888 c42fce9b 2022-09-14 op clt->clt_translate &= ~TR_PRE;
891 c42fce9b 2022-09-14 op if (clt->clt_translate & TR_LIST) {
892 18906bcc 2023-09-13 op if (tp_writes(tp, "</ul>") == -1)
894 c42fce9b 2022-09-14 op clt->clt_translate &= ~TR_LIST;
897 c42fce9b 2022-09-14 op if (clt->clt_translate & TR_NAV) {
898 18906bcc 2023-09-13 op if (tp_writes(tp, "</ul></nav>") == -1)
900 c42fce9b 2022-09-14 op clt->clt_translate &= ~TR_NAV;
903 54acddc9 2022-09-19 op if (clt->clt_translate &&
904 54acddc9 2022-09-19 op tp_foot(clt->clt_tp) == -1)
908 b0a6bcf7 2022-09-13 op fcgi_end_request(clt, status);
912 b0a6bcf7 2022-09-13 op proxy_tls_readcb(int fd, short event, void *arg)
914 b0a6bcf7 2022-09-13 op struct bufferevent *bufev = arg;
915 b0a6bcf7 2022-09-13 op struct client *clt = bufev->cbarg;
916 b0a6bcf7 2022-09-13 op char rbuf[IBUF_READ_SIZE];
917 b0a6bcf7 2022-09-13 op int what = EVBUFFER_READ;
918 b0a6bcf7 2022-09-13 op int howmuch = IBUF_READ_SIZE;
922 b0a6bcf7 2022-09-13 op if (event == EV_TIMEOUT) {
923 b0a6bcf7 2022-09-13 op what |= EVBUFFER_TIMEOUT;
927 b0a6bcf7 2022-09-13 op if (bufev->wm_read.high != 0)
928 b0a6bcf7 2022-09-13 op howmuch = MINIMUM(sizeof(rbuf), bufev->wm_read.high);
930 b0a6bcf7 2022-09-13 op ret = tls_read(clt->clt_ctx, rbuf, howmuch);
931 b0a6bcf7 2022-09-13 op if (ret == TLS_WANT_POLLIN || ret == TLS_WANT_POLLOUT) {
933 b0a6bcf7 2022-09-13 op } else if (ret == -1) {
934 b0a6bcf7 2022-09-13 op what |= EVBUFFER_ERROR;
939 b0a6bcf7 2022-09-13 op if (len == 0) {
940 b0a6bcf7 2022-09-13 op what |= EVBUFFER_EOF;
944 b0a6bcf7 2022-09-13 op if (evbuffer_add(bufev->input, rbuf, len) == -1) {
945 b0a6bcf7 2022-09-13 op what |= EVBUFFER_ERROR;
949 83f0f95a 2022-09-29 op proxy_bufferevent_add(&bufev->ev_read, G_TOUT(bufev->timeout_read));
951 b0a6bcf7 2022-09-13 op len = EVBUFFER_LENGTH(bufev->input);
952 b0a6bcf7 2022-09-13 op if (bufev->wm_read.low != 0 && len < bufev->wm_read.low)
954 b0a6bcf7 2022-09-13 op if (bufev->wm_read.high != 0 && len > bufev->wm_read.high) {
955 b0a6bcf7 2022-09-13 op struct evbuffer *buf = bufev->input;
956 b0a6bcf7 2022-09-13 op event_del(&bufev->ev_read);
957 b0a6bcf7 2022-09-13 op evbuffer_setcb(buf, bufferevent_read_pressure_cb, bufev);
961 b0a6bcf7 2022-09-13 op if (bufev->readcb != NULL)
962 b0a6bcf7 2022-09-13 op (*bufev->readcb)(bufev, bufev->cbarg);
966 83f0f95a 2022-09-29 op proxy_bufferevent_add(&bufev->ev_read, G_TOUT(bufev->timeout_read));
970 b0a6bcf7 2022-09-13 op (*bufev->errorcb)(bufev, what, bufev->cbarg);
974 b0a6bcf7 2022-09-13 op proxy_bufferevent_add(struct event *ev, int timeout)
976 b0a6bcf7 2022-09-13 op struct timeval tv, *ptv = NULL;
978 b0a6bcf7 2022-09-13 op if (timeout) {
979 b0a6bcf7 2022-09-13 op timerclear(&tv);
980 b0a6bcf7 2022-09-13 op tv.tv_sec = timeout;
984 b0a6bcf7 2022-09-13 op return (event_add(ev, ptv));
988 b0a6bcf7 2022-09-13 op proxy_tls_writecb(int fd, short event, void *arg)
990 b0a6bcf7 2022-09-13 op struct bufferevent *bufev = arg;
991 b0a6bcf7 2022-09-13 op struct client *clt = bufev->cbarg;
993 b0a6bcf7 2022-09-13 op short what = EVBUFFER_WRITE;
996 b0a6bcf7 2022-09-13 op if (event == EV_TIMEOUT) {
997 b0a6bcf7 2022-09-13 op what |= EVBUFFER_TIMEOUT;
1001 b0a6bcf7 2022-09-13 op if (EVBUFFER_LENGTH(bufev->output)) {
1002 b0a6bcf7 2022-09-13 op ret = tls_write(clt->clt_ctx,
1003 b0a6bcf7 2022-09-13 op EVBUFFER_DATA(bufev->output),
1004 b0a6bcf7 2022-09-13 op EVBUFFER_LENGTH(bufev->output));
1005 b0a6bcf7 2022-09-13 op if (ret == TLS_WANT_POLLIN || ret == TLS_WANT_POLLOUT) {
1007 b0a6bcf7 2022-09-13 op } else if (ret == -1) {
1008 b0a6bcf7 2022-09-13 op what |= EVBUFFER_ERROR;
1012 b0a6bcf7 2022-09-13 op evbuffer_drain(bufev->output, len);
1015 b0a6bcf7 2022-09-13 op if (EVBUFFER_LENGTH(bufev->output) != 0)
1016 83f0f95a 2022-09-29 op proxy_bufferevent_add(&bufev->ev_write,
1017 83f0f95a 2022-09-29 op G_TOUT(bufev->timeout_write));
1019 b0a6bcf7 2022-09-13 op if (bufev->writecb != NULL &&
1020 b0a6bcf7 2022-09-13 op EVBUFFER_LENGTH(bufev->output) <= bufev->wm_write.low)
1021 b0a6bcf7 2022-09-13 op (*bufev->writecb)(bufev, bufev->cbarg);
1025 83f0f95a 2022-09-29 op proxy_bufferevent_add(&bufev->ev_write, G_TOUT(bufev->timeout_write));
1029 b0a6bcf7 2022-09-13 op (*bufev->errorcb)(bufev, what, bufev->cbarg);
1033 b0a6bcf7 2022-09-13 op proxy_client_free(struct client *clt)
1035 b0a6bcf7 2022-09-13 op if (clt->clt_evasr)
1036 b0a6bcf7 2022-09-13 op event_asr_abort(clt->clt_evasr);
1038 b0a6bcf7 2022-09-13 op if (clt->clt_addrinfo)
1039 b0a6bcf7 2022-09-13 op freeaddrinfo(clt->clt_addrinfo);
1041 b0a6bcf7 2022-09-13 op if (clt->clt_evconn_live)
1042 b0a6bcf7 2022-09-13 op event_del(&clt->clt_evconn);
1044 b0a6bcf7 2022-09-13 op if (clt->clt_fd != -1)
1045 b0a6bcf7 2022-09-13 op close(clt->clt_fd);
1047 b0a6bcf7 2022-09-13 op if (clt->clt_ctx)
1048 b0a6bcf7 2022-09-13 op tls_free(clt->clt_ctx);
1050 b0a6bcf7 2022-09-13 op if (clt->clt_bev)
1051 b0a6bcf7 2022-09-13 op bufferevent_free(clt->clt_bev);
1053 a244322c 2022-09-29 op template_free(clt->clt_tp);
1055 8857131d 2022-09-20 op free(clt->clt_body);
1056 b0a6bcf7 2022-09-13 op free(clt->clt_server_name);
1057 b0a6bcf7 2022-09-13 op free(clt->clt_script_name);
1058 b0a6bcf7 2022-09-13 op free(clt->clt_path_info);
1059 28239bac 2022-09-20 op free(clt->clt_query);