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_accept(int, short, void *);
55 void proxy_inflight_dec(const char *);
56 int proxy_dispatch_parent(int, struct privsep_proc *, struct imsg *);
57 void proxy_resolved(struct asr_result *, void *);
58 void proxy_connect(int, short, void *);
59 void proxy_read(struct bufferevent *, void *);
60 void proxy_write(struct bufferevent *, void *);
61 void proxy_error(struct bufferevent *, short, void *);
62 int proxy_bufferevent_add(struct event *, int);
63 void proxy_tls_writecb(int, short, void *);
64 void proxy_tls_readcb(int, short, void *);
66 static struct privsep_proc procs[] = {
67 { "parent", PROC_PARENT, proxy_dispatch_parent },
70 volatile int proxy_clients;
71 volatile int proxy_inflight;
72 uint32_t proxy_fcg_id;
75 proxy(struct privsep *ps, struct privsep_proc *p)
77 proc_run(ps, p, procs, nitems(procs), proxy_init, NULL);
81 proxy_init(struct privsep *ps, struct privsep_proc *p, void *arg)
83 if (config_init(ps->ps_env) == -1)
84 fatal("failed to initialize configuration");
86 /* We use a custom shutdown callback */
87 /* p->p_shutdown = proxy_shutdown */
89 if (pledge("stdio recvfd unix inet dns", NULL) == -1)
94 proxy_launch(struct galileo *env)
96 event_add(&env->sc_evsock, NULL);
101 proxy_purge(struct server *srv)
106 proxy_accept(int fd, short event, void *arg)
108 struct galileo *env = arg;
110 log_debug("%s", __func__);
115 proxy_inflight_dec(const char *why)
118 log_debug("%s: inflight decremented, now %d, %s",
119 __func__, proxy_inflight, why);
123 proxy_dispatch_parent(int fd, struct privsep_proc *p, struct imsg *imsg)
125 struct privsep *ps = p->p_ps;
126 struct galileo *env = ps->ps_env;
128 switch (imsg->hdr.type) {
130 if (config_getserver(env, imsg) == -1)
131 fatal("config_getproxy");
136 if (env->sc_sock_fd != -1) {
137 event_del(&env->sc_evsock);
138 close(env->sc_sock_fd);
141 env->sc_sock_fd = config_getsock(env, imsg);
142 if (env->sc_sock_fd == -1)
143 fatal("config_getsock");
145 event_set(&env->sc_evsock, env->sc_sock_fd,
146 EV_READ | EV_PERSIST, proxy_accept, env);
147 event_add(&env->sc_evsock, NULL);
148 /* evtimer_set(&env->sc_evpause, proxy_accept_paused, env); */
151 log_debug("config done!");
157 log_warnx("unknown message %d", imsg->hdr.type);
164 static struct proxy_config *
165 proxy_server_match(struct galileo *env, struct client *clt)
169 if (clt->clt_server_name == NULL)
172 TAILQ_FOREACH(srv, &env->sc_servers, srv_entry) {
173 if (!strcmp(clt->clt_server_name, srv->srv_conf.host))
174 return &srv->srv_conf;
181 proxy_start_request(struct galileo *env, struct client *clt)
183 struct addrinfo hints;
184 struct asr_query *query;
187 if ((clt->clt_pc = proxy_server_match(env, clt)) == NULL) {
188 if (clt_printf(clt, "Status: 501\r\n\r\n") == -1)
190 fcgi_end_request(clt, 1);
194 (void)snprintf(port, sizeof(port), "%d", clt->clt_pc->proxy_port);
196 memset(&hints, 0, sizeof(hints));
197 hints.ai_family = AF_UNSPEC;
198 hints.ai_socktype = SOCK_STREAM;
200 query = getaddrinfo_async(clt->clt_pc->proxy_addr, port, &hints, NULL);
202 log_warn("getaddrinfo_async");
203 fcgi_abort_request(clt);
207 clt->clt_evasr = event_asr_run(query, proxy_resolved, clt);
208 if (clt->clt_evasr == NULL) {
209 log_warn("event_asr_run");
211 fcgi_abort_request(clt);
217 proxy_resolved(struct asr_result *res, void *d)
219 struct client *clt = d;
220 struct proxy_config *pc = clt->clt_pc;
222 clt->clt_evasr = NULL;
224 if (res->ar_gai_errno != 0) {
225 log_warnx("failed to resolve %s:%d: %s",
226 pc->proxy_addr, pc->proxy_port,
227 gai_strerror(res->ar_gai_errno));
228 if (clt_printf(clt, "Status: 501\r\n") == -1)
230 if (clt_printf(clt, "Content-Type: text/plain\r\n") == -1)
232 if (clt_printf(clt, "Proxy error; connection failed") == -1)
234 fcgi_end_request(clt, 1);
238 clt->clt_addrinfo = res->ar_addrinfo;
239 clt->clt_p = clt->clt_addrinfo;
240 proxy_connect(-1, 0, clt);
244 proxy_connect(int fd, short ev, void *d)
246 struct client *clt = d;
247 struct evbuffer *out;
249 struct tls_config *conf;
250 struct timeval conntv = {5, 0};
252 socklen_t len = sizeof(err);
255 if (clt->clt_p == NULL)
258 if (clt->clt_fd != -1) {
259 if (getsockopt(clt->clt_fd, SOL_SOCKET, SO_ERROR, &err, &len)
270 clt->clt_fd = socket(p->ai_family, p->ai_socktype | SOCK_NONBLOCK,
272 if (clt->clt_fd == -1) {
273 clt->clt_p = clt->clt_p->ai_next;
277 if (connect(clt->clt_fd, p->ai_addr, p->ai_addrlen) == 0)
280 clt->clt_evconn_live = 1;
281 event_set(&clt->clt_evconn, clt->clt_fd, EV_WRITE, proxy_connect, clt);
282 event_add(&clt->clt_evconn, &conntv);
286 clt->clt_evconn_live = 0;
287 freeaddrinfo(clt->clt_addrinfo);
288 clt->clt_addrinfo = clt->clt_p = NULL;
290 /* initialize TLS for Gemini */
291 if ((conf = tls_config_new()) == NULL) {
292 log_warn("tls_config_new failed");
296 tls_config_insecure_noverifycert(conf);
298 if ((clt->clt_ctx = tls_client()) == NULL) {
299 log_warnx("tls_client failed");
300 tls_config_free(conf);
304 if (tls_configure(clt->clt_ctx, conf) == -1) {
305 log_warnx("tls_configure failed");
306 tls_config_free(conf);
310 tls_config_free(conf);
312 if (tls_connect_socket(clt->clt_ctx, clt->clt_fd,
313 clt->clt_pc->proxy_name) == -1) {
314 log_warnx("tls_connect_socket failed");
318 clt->clt_bev = bufferevent_new(clt->clt_fd, proxy_read, proxy_write,
320 if (clt->clt_bev == NULL) {
321 log_warn("bufferevent_new");
324 out = EVBUFFER_OUTPUT(clt->clt_bev);
326 event_set(&clt->clt_bev->ev_read, clt->clt_fd, EV_READ,
327 proxy_tls_readcb, clt->clt_bev);
328 event_set(&clt->clt_bev->ev_write, clt->clt_fd, EV_WRITE,
329 proxy_tls_writecb, clt->clt_bev);
331 /* bufferevent_settimeout(); */
332 bufferevent_enable(clt->clt_bev, EV_READ|EV_WRITE);
334 /* TODO: compute the URL */
335 if (evbuffer_add_printf(out, "gemini://localhost/\r\n") == -1) {
336 log_warn("bufferevent_printf failed");
343 log_warn("failed to connect to %s:%d",
344 clt->clt_pc->proxy_addr, clt->clt_pc->proxy_port);
345 if (clt_printf(clt, "Status: 501\r\n") == -1)
347 if (clt_printf(clt, "Content-Type: text/plain\r\n") == -1)
349 if (clt_printf(clt, "Proxy error; connection failed") == -1)
351 fcgi_end_request(clt, 1);
355 proxy_read(struct bufferevent *bev, void *d)
357 struct client *clt = d;
358 struct evbuffer *src = EVBUFFER_INPUT(bev);
363 if (clt->clt_headersdone) {
365 clt_write_bufferevent(clt, bev);
369 hdr = evbuffer_readln(src, &len, EVBUFFER_EOL_CRLF_STRICT);
371 if (EVBUFFER_LENGTH(src) >= 1026)
372 proxy_error(bev, EV_READ, clt);
377 !isdigit((unsigned char)hdr[0]) ||
378 !isdigit((unsigned char)hdr[1]) ||
380 log_warnx("invalid ");
382 proxy_error(bev, EV_READ, clt);
386 code = (hdr[0] - '0') * 10 + (hdr[1] - '0');
388 log_warnx("un-handled gemini reply status %d", code);
390 proxy_error(bev, EV_READ, clt);
394 if (clt_printf(clt, "Content-Type: %s\r\n", &hdr[4]) == -1)
396 if (clt_printf(clt, "\r\n") == -1)
399 clt->clt_headersdone = 1;
404 proxy_write(struct bufferevent *bev, void *d)
410 proxy_error(struct bufferevent *bev, short err, void *d)
412 struct client *clt = d;
413 int status = !(err & EVBUFFER_EOF);
415 log_debug("proxy error, shutting down the connection (err: %x)",
418 if (!clt->clt_headersdone) {
419 if (clt_printf(clt, "Status: 501\r\n") == -1)
421 if (clt_printf(clt, "Content-Type: text/plain\r\n") == -1)
423 if (clt_printf(clt, "Proxy error\n") == -1)
427 fcgi_end_request(clt, status);
431 proxy_tls_readcb(int fd, short event, void *arg)
433 struct bufferevent *bufev = arg;
434 struct client *clt = bufev->cbarg;
435 char rbuf[IBUF_READ_SIZE];
436 int what = EVBUFFER_READ;
437 int howmuch = IBUF_READ_SIZE;
441 if (event == EV_TIMEOUT) {
442 what |= EVBUFFER_TIMEOUT;
446 if (bufev->wm_read.high != 0)
447 howmuch = MINIMUM(sizeof(rbuf), bufev->wm_read.high);
449 ret = tls_read(clt->clt_ctx, rbuf, howmuch);
450 if (ret == TLS_WANT_POLLIN || ret == TLS_WANT_POLLOUT) {
452 } else if (ret == -1) {
453 what |= EVBUFFER_ERROR;
459 what |= EVBUFFER_EOF;
463 if (evbuffer_add(bufev->input, rbuf, len) == -1) {
464 what |= EVBUFFER_ERROR;
468 proxy_bufferevent_add(&bufev->ev_read, bufev->timeout_read);
470 len = EVBUFFER_LENGTH(bufev->input);
471 if (bufev->wm_read.low != 0 && len < bufev->wm_read.low)
473 if (bufev->wm_read.high != 0 && len > bufev->wm_read.high) {
474 struct evbuffer *buf = bufev->input;
475 event_del(&bufev->ev_read);
476 evbuffer_setcb(buf, bufferevent_read_pressure_cb, bufev);
480 if (bufev->readcb != NULL)
481 (*bufev->readcb)(bufev, bufev->cbarg);
485 proxy_bufferevent_add(&bufev->ev_read, bufev->timeout_read);
489 (*bufev->errorcb)(bufev, what, bufev->cbarg);
493 proxy_bufferevent_add(struct event *ev, int timeout)
495 struct timeval tv, *ptv = NULL;
503 return (event_add(ev, ptv));
507 proxy_tls_writecb(int fd, short event, void *arg)
509 struct bufferevent *bufev = arg;
510 struct client *clt = bufev->cbarg;
512 short what = EVBUFFER_WRITE;
515 if (event == EV_TIMEOUT) {
516 what |= EVBUFFER_TIMEOUT;
520 if (EVBUFFER_LENGTH(bufev->output)) {
521 ret = tls_write(clt->clt_ctx,
522 EVBUFFER_DATA(bufev->output),
523 EVBUFFER_LENGTH(bufev->output));
524 if (ret == TLS_WANT_POLLIN || ret == TLS_WANT_POLLOUT) {
526 } else if (ret == -1) {
527 what |= EVBUFFER_ERROR;
531 evbuffer_drain(bufev->output, len);
534 if (EVBUFFER_LENGTH(bufev->output) != 0)
535 proxy_bufferevent_add(&bufev->ev_write, bufev->timeout_write);
537 if (bufev->writecb != NULL &&
538 EVBUFFER_LENGTH(bufev->output) <= bufev->wm_write.low)
539 (*bufev->writecb)(bufev, bufev->cbarg);
543 proxy_bufferevent_add(&bufev->ev_write, bufev->timeout_write);
547 (*bufev->errorcb)(bufev, what, bufev->cbarg);
551 proxy_client_free(struct client *clt)
554 event_asr_abort(clt->clt_evasr);
556 if (clt->clt_addrinfo)
557 freeaddrinfo(clt->clt_addrinfo);
559 if (clt->clt_evconn_live)
560 event_del(&clt->clt_evconn);
562 if (clt->clt_fd != -1)
566 tls_free(clt->clt_ctx);
569 bufferevent_free(clt->clt_bev);
571 free(clt->clt_server_name);
572 free(clt->clt_script_name);
573 free(clt->clt_path_info);