2 * Copyright (c) 2022 Omar Polo <op@omarpolo.com>
3 * Copyright (c) 2006 - 2015 Reyk Floeter <reyk@openbsd.org>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <sys/types.h>
19 #include <sys/queue.h>
21 #include <sys/socket.h>
24 #include <netinet/in.h>
25 #include <arpa/inet.h>
46 #define MINIMUM(a, b) ((a) < (b) ? (a) : (b))
48 /* provided by OpenBSD' base libevent but not in any header? */
49 extern void bufferevent_read_pressure_cb(struct evbuffer *, size_t,
52 void proxy_init(struct privsep *, struct privsep_proc *, void *);
53 int proxy_launch(struct galileo *);
54 void proxy_inflight_dec(const char *);
55 int proxy_dispatch_parent(int, struct privsep_proc *, struct imsg *);
56 void proxy_resolved(struct asr_result *, void *);
57 void proxy_connect(int, short, void *);
58 void proxy_read(struct bufferevent *, void *);
59 void proxy_write(struct bufferevent *, void *);
60 void proxy_error(struct bufferevent *, short, void *);
61 int proxy_bufferevent_add(struct event *, int);
62 void proxy_tls_writecb(int, short, void *);
63 void proxy_tls_readcb(int, short, void *);
65 static struct privsep_proc procs[] = {
66 { "parent", PROC_PARENT, proxy_dispatch_parent },
69 volatile int proxy_clients;
70 volatile int proxy_inflight;
71 uint32_t proxy_fcg_id;
74 proxy(struct privsep *ps, struct privsep_proc *p)
76 proc_run(ps, p, procs, nitems(procs), proxy_init, NULL);
80 proxy_init(struct privsep *ps, struct privsep_proc *p, void *arg)
82 if (config_init(ps->ps_env) == -1)
83 fatal("failed to initialize configuration");
85 /* We use a custom shutdown callback */
86 /* p->p_shutdown = proxy_shutdown */
88 if (pledge("stdio recvfd unix inet dns", NULL) == -1)
93 proxy_launch(struct galileo *env)
95 event_add(&env->sc_evsock, NULL);
100 proxy_purge(struct server *srv)
105 proxy_inflight_dec(const char *why)
108 log_debug("%s: inflight decremented, now %d, %s",
109 __func__, proxy_inflight, why);
113 proxy_dispatch_parent(int fd, struct privsep_proc *p, struct imsg *imsg)
115 struct privsep *ps = p->p_ps;
116 struct galileo *env = ps->ps_env;
118 switch (imsg->hdr.type) {
120 if (config_getserver(env, imsg) == -1)
121 fatal("config_getproxy");
126 if (env->sc_sock_fd != -1) {
127 event_del(&env->sc_evsock);
128 close(env->sc_sock_fd);
131 env->sc_sock_fd = config_getsock(env, imsg);
132 if (env->sc_sock_fd == -1)
133 fatal("config_getsock");
135 event_set(&env->sc_evsock, env->sc_sock_fd,
136 EV_READ | EV_PERSIST, fcgi_accept, env);
137 event_add(&env->sc_evsock, NULL);
138 evtimer_set(&env->sc_evpause, fcgi_accept, env);
141 log_debug("config done!");
147 log_warnx("unknown message %d", imsg->hdr.type);
154 static struct proxy_config *
155 proxy_server_match(struct galileo *env, struct client *clt)
159 if (clt->clt_server_name == NULL)
162 TAILQ_FOREACH(srv, &env->sc_servers, srv_entry) {
163 if (!strcmp(clt->clt_server_name, srv->srv_conf.host))
164 return &srv->srv_conf;
171 proxy_start_request(struct galileo *env, struct client *clt)
173 struct addrinfo hints;
174 struct asr_query *query;
177 if ((clt->clt_pc = proxy_server_match(env, clt)) == NULL) {
178 if (clt_printf(clt, "Status: 501\r\n\r\n") == -1)
180 fcgi_end_request(clt, 1);
184 (void)snprintf(port, sizeof(port), "%d", clt->clt_pc->proxy_port);
186 memset(&hints, 0, sizeof(hints));
187 hints.ai_family = AF_UNSPEC;
188 hints.ai_socktype = SOCK_STREAM;
190 query = getaddrinfo_async(clt->clt_pc->proxy_addr, port, &hints, NULL);
192 log_warn("getaddrinfo_async");
193 fcgi_abort_request(clt);
197 clt->clt_evasr = event_asr_run(query, proxy_resolved, clt);
198 if (clt->clt_evasr == NULL) {
199 log_warn("event_asr_run");
201 fcgi_abort_request(clt);
207 proxy_resolved(struct asr_result *res, void *d)
209 struct client *clt = d;
210 struct proxy_config *pc = clt->clt_pc;
212 clt->clt_evasr = NULL;
214 if (res->ar_gai_errno != 0) {
215 log_warnx("failed to resolve %s:%d: %s",
216 pc->proxy_addr, pc->proxy_port,
217 gai_strerror(res->ar_gai_errno));
218 if (clt_printf(clt, "Status: 501\r\n") == -1)
220 if (clt_printf(clt, "Content-Type: text/plain\r\n") == -1)
222 if (clt_printf(clt, "Proxy error; connection failed") == -1)
224 fcgi_end_request(clt, 1);
228 clt->clt_addrinfo = res->ar_addrinfo;
229 clt->clt_p = clt->clt_addrinfo;
230 proxy_connect(-1, 0, clt);
234 proxy_connect(int fd, short ev, void *d)
236 struct client *clt = d;
237 struct evbuffer *out;
239 struct tls_config *conf;
240 struct timeval conntv = {5, 0};
242 socklen_t len = sizeof(err);
245 if (clt->clt_p == NULL)
248 if (clt->clt_fd != -1) {
249 if (getsockopt(clt->clt_fd, SOL_SOCKET, SO_ERROR, &err, &len)
260 clt->clt_fd = socket(p->ai_family, p->ai_socktype | SOCK_NONBLOCK,
262 if (clt->clt_fd == -1) {
263 clt->clt_p = clt->clt_p->ai_next;
267 if (connect(clt->clt_fd, p->ai_addr, p->ai_addrlen) == 0)
270 clt->clt_evconn_live = 1;
271 event_set(&clt->clt_evconn, clt->clt_fd, EV_WRITE, proxy_connect, clt);
272 event_add(&clt->clt_evconn, &conntv);
276 clt->clt_evconn_live = 0;
277 freeaddrinfo(clt->clt_addrinfo);
278 clt->clt_addrinfo = clt->clt_p = NULL;
280 /* initialize TLS for Gemini */
281 if ((conf = tls_config_new()) == NULL) {
282 log_warn("tls_config_new failed");
286 tls_config_insecure_noverifycert(conf);
288 if ((clt->clt_ctx = tls_client()) == NULL) {
289 log_warnx("tls_client failed");
290 tls_config_free(conf);
294 if (tls_configure(clt->clt_ctx, conf) == -1) {
295 log_warnx("tls_configure failed");
296 tls_config_free(conf);
300 tls_config_free(conf);
302 if (tls_connect_socket(clt->clt_ctx, clt->clt_fd,
303 clt->clt_pc->proxy_name) == -1) {
304 log_warnx("tls_connect_socket failed");
308 clt->clt_bev = bufferevent_new(clt->clt_fd, proxy_read, proxy_write,
310 if (clt->clt_bev == NULL) {
311 log_warn("bufferevent_new");
314 out = EVBUFFER_OUTPUT(clt->clt_bev);
316 event_set(&clt->clt_bev->ev_read, clt->clt_fd, EV_READ,
317 proxy_tls_readcb, clt->clt_bev);
318 event_set(&clt->clt_bev->ev_write, clt->clt_fd, EV_WRITE,
319 proxy_tls_writecb, clt->clt_bev);
321 /* bufferevent_settimeout(); */
322 bufferevent_enable(clt->clt_bev, EV_READ|EV_WRITE);
324 /* TODO: compute the URL */
325 if (evbuffer_add_printf(out, "gemini://localhost/\r\n") == -1) {
326 log_warn("bufferevent_printf failed");
333 log_warn("failed to connect to %s:%d",
334 clt->clt_pc->proxy_addr, clt->clt_pc->proxy_port);
335 if (clt_printf(clt, "Status: 501\r\n") == -1)
337 if (clt_printf(clt, "Content-Type: text/plain\r\n") == -1)
339 if (clt_printf(clt, "Proxy error; connection failed") == -1)
341 fcgi_end_request(clt, 1);
345 proxy_read(struct bufferevent *bev, void *d)
347 struct client *clt = d;
348 struct evbuffer *src = EVBUFFER_INPUT(bev);
353 if (clt->clt_headersdone) {
355 clt_write_bufferevent(clt, bev);
359 hdr = evbuffer_readln(src, &len, EVBUFFER_EOL_CRLF_STRICT);
361 if (EVBUFFER_LENGTH(src) >= 1026)
362 proxy_error(bev, EV_READ, clt);
367 !isdigit((unsigned char)hdr[0]) ||
368 !isdigit((unsigned char)hdr[1]) ||
370 log_warnx("invalid ");
372 proxy_error(bev, EV_READ, clt);
376 code = (hdr[0] - '0') * 10 + (hdr[1] - '0');
378 log_warnx("un-handled gemini reply status %d", code);
380 proxy_error(bev, EV_READ, clt);
384 if (clt_printf(clt, "Content-Type: %s\r\n", &hdr[4]) == -1)
386 if (clt_printf(clt, "\r\n") == -1)
389 clt->clt_headersdone = 1;
394 proxy_write(struct bufferevent *bev, void *d)
400 proxy_error(struct bufferevent *bev, short err, void *d)
402 struct client *clt = d;
403 int status = !(err & EVBUFFER_EOF);
405 log_debug("proxy error, shutting down the connection (err: %x)",
408 if (!clt->clt_headersdone) {
409 if (clt_printf(clt, "Status: 501\r\n") == -1)
411 if (clt_printf(clt, "Content-Type: text/plain\r\n") == -1)
413 if (clt_printf(clt, "Proxy error\n") == -1)
417 fcgi_end_request(clt, status);
421 proxy_tls_readcb(int fd, short event, void *arg)
423 struct bufferevent *bufev = arg;
424 struct client *clt = bufev->cbarg;
425 char rbuf[IBUF_READ_SIZE];
426 int what = EVBUFFER_READ;
427 int howmuch = IBUF_READ_SIZE;
431 if (event == EV_TIMEOUT) {
432 what |= EVBUFFER_TIMEOUT;
436 if (bufev->wm_read.high != 0)
437 howmuch = MINIMUM(sizeof(rbuf), bufev->wm_read.high);
439 ret = tls_read(clt->clt_ctx, rbuf, howmuch);
440 if (ret == TLS_WANT_POLLIN || ret == TLS_WANT_POLLOUT) {
442 } else if (ret == -1) {
443 what |= EVBUFFER_ERROR;
449 what |= EVBUFFER_EOF;
453 if (evbuffer_add(bufev->input, rbuf, len) == -1) {
454 what |= EVBUFFER_ERROR;
458 proxy_bufferevent_add(&bufev->ev_read, bufev->timeout_read);
460 len = EVBUFFER_LENGTH(bufev->input);
461 if (bufev->wm_read.low != 0 && len < bufev->wm_read.low)
463 if (bufev->wm_read.high != 0 && len > bufev->wm_read.high) {
464 struct evbuffer *buf = bufev->input;
465 event_del(&bufev->ev_read);
466 evbuffer_setcb(buf, bufferevent_read_pressure_cb, bufev);
470 if (bufev->readcb != NULL)
471 (*bufev->readcb)(bufev, bufev->cbarg);
475 proxy_bufferevent_add(&bufev->ev_read, bufev->timeout_read);
479 (*bufev->errorcb)(bufev, what, bufev->cbarg);
483 proxy_bufferevent_add(struct event *ev, int timeout)
485 struct timeval tv, *ptv = NULL;
493 return (event_add(ev, ptv));
497 proxy_tls_writecb(int fd, short event, void *arg)
499 struct bufferevent *bufev = arg;
500 struct client *clt = bufev->cbarg;
502 short what = EVBUFFER_WRITE;
505 if (event == EV_TIMEOUT) {
506 what |= EVBUFFER_TIMEOUT;
510 if (EVBUFFER_LENGTH(bufev->output)) {
511 ret = tls_write(clt->clt_ctx,
512 EVBUFFER_DATA(bufev->output),
513 EVBUFFER_LENGTH(bufev->output));
514 if (ret == TLS_WANT_POLLIN || ret == TLS_WANT_POLLOUT) {
516 } else if (ret == -1) {
517 what |= EVBUFFER_ERROR;
521 evbuffer_drain(bufev->output, len);
524 if (EVBUFFER_LENGTH(bufev->output) != 0)
525 proxy_bufferevent_add(&bufev->ev_write, bufev->timeout_write);
527 if (bufev->writecb != NULL &&
528 EVBUFFER_LENGTH(bufev->output) <= bufev->wm_write.low)
529 (*bufev->writecb)(bufev, bufev->cbarg);
533 proxy_bufferevent_add(&bufev->ev_write, bufev->timeout_write);
537 (*bufev->errorcb)(bufev, what, bufev->cbarg);
541 proxy_client_free(struct client *clt)
544 event_asr_abort(clt->clt_evasr);
546 if (clt->clt_addrinfo)
547 freeaddrinfo(clt->clt_addrinfo);
549 if (clt->clt_evconn_live)
550 event_del(&clt->clt_evconn);
552 if (clt->clt_fd != -1)
556 tls_free(clt->clt_ctx);
559 bufferevent_free(clt->clt_bev);
561 free(clt->clt_server_name);
562 free(clt->clt_script_name);
563 free(clt->clt_path_info);