2 a555e0d6 2022-07-04 op * Copyright (c) 2021, 2022 Omar Polo <op@omarpolo.com>
4 72b033ef 2021-12-29 op * Permission to use, copy, modify, and distribute this software for any
5 72b033ef 2021-12-29 op * purpose with or without fee is hereby granted, provided that the above
6 72b033ef 2021-12-29 op * copyright notice and this permission notice appear in all copies.
8 72b033ef 2021-12-29 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 72b033ef 2021-12-29 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 72b033ef 2021-12-29 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 72b033ef 2021-12-29 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 72b033ef 2021-12-29 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 72b033ef 2021-12-29 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 72b033ef 2021-12-29 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 72b033ef 2021-12-29 op #include "gmid.h"
19 72b033ef 2021-12-29 op #include <ctype.h>
20 72b033ef 2021-12-29 op #include <errno.h>
21 72b033ef 2021-12-29 op #include <string.h>
23 eae52ad4 2023-06-06 op #include "log.h"
25 72b033ef 2021-12-29 op #define MIN(a, b) ((a) < (b) ? (a) : (b))
27 e5d82d94 2022-03-19 op static const struct timeval handshake_timeout = { 5, 0 };
29 72b033ef 2021-12-29 op static void proxy_tls_readcb(int, short, void *);
30 72b033ef 2021-12-29 op static void proxy_tls_writecb(int, short, void *);
31 72b033ef 2021-12-29 op static void proxy_read(struct bufferevent *, void *);
32 72b033ef 2021-12-29 op static void proxy_write(struct bufferevent *, void *);
33 72b033ef 2021-12-29 op static void proxy_error(struct bufferevent *, short, void *);
36 72b033ef 2021-12-29 op proxy_tls_readcb(int fd, short event, void *d)
38 72b033ef 2021-12-29 op struct bufferevent *bufev = d;
39 72b033ef 2021-12-29 op struct client *c = bufev->cbarg;
40 72b033ef 2021-12-29 op char buf[IBUF_READ_SIZE];
41 72b033ef 2021-12-29 op int what = EVBUFFER_READ;
42 72b033ef 2021-12-29 op int howmuch = IBUF_READ_SIZE;
47 72b033ef 2021-12-29 op if (event == EV_TIMEOUT) {
48 72b033ef 2021-12-29 op what |= EVBUFFER_TIMEOUT;
52 72b033ef 2021-12-29 op if (bufev->wm_read.high != 0)
53 72b033ef 2021-12-29 op howmuch = MIN(sizeof(buf), bufev->wm_read.high);
55 72b033ef 2021-12-29 op switch (ret = tls_read(c->proxyctx, buf, howmuch)) {
56 72b033ef 2021-12-29 op case TLS_WANT_POLLIN:
57 72b033ef 2021-12-29 op case TLS_WANT_POLLOUT:
60 72b033ef 2021-12-29 op what |= EVBUFFER_ERROR;
65 72b033ef 2021-12-29 op if (len == 0) {
66 72b033ef 2021-12-29 op what |= EVBUFFER_EOF;
70 72b033ef 2021-12-29 op res = evbuffer_add(bufev->input, buf, len);
71 72b033ef 2021-12-29 op if (res == -1) {
72 72b033ef 2021-12-29 op what |= EVBUFFER_ERROR;
76 72b033ef 2021-12-29 op event_add(&bufev->ev_read, NULL);
78 72b033ef 2021-12-29 op len = EVBUFFER_LENGTH(bufev->input);
79 72b033ef 2021-12-29 op if (bufev->wm_read.low != 0 && len < bufev->wm_read.low)
82 72b033ef 2021-12-29 op if (bufev->readcb != NULL)
83 72b033ef 2021-12-29 op (*bufev->readcb)(bufev, bufev->cbarg);
87 72b033ef 2021-12-29 op event_add(&bufev->ev_read, NULL);
91 72b033ef 2021-12-29 op (*bufev->errorcb)(bufev, what, bufev->cbarg);
95 72b033ef 2021-12-29 op proxy_tls_writecb(int fd, short event, void *d)
97 72b033ef 2021-12-29 op struct bufferevent *bufev = d;
98 72b033ef 2021-12-29 op struct client *c = bufev->cbarg;
101 72b033ef 2021-12-29 op short what = EVBUFFER_WRITE;
103 72b033ef 2021-12-29 op if (event & EV_TIMEOUT) {
104 72b033ef 2021-12-29 op what |= EVBUFFER_TIMEOUT;
108 72b033ef 2021-12-29 op if (EVBUFFER_LENGTH(bufev->output) != 0) {
109 72b033ef 2021-12-29 op ret = tls_write(c->proxyctx, EVBUFFER_DATA(bufev->output),
110 72b033ef 2021-12-29 op EVBUFFER_LENGTH(bufev->output));
111 72b033ef 2021-12-29 op switch (ret) {
112 72b033ef 2021-12-29 op case TLS_WANT_POLLIN:
113 72b033ef 2021-12-29 op case TLS_WANT_POLLOUT:
116 72b033ef 2021-12-29 op what |= EVBUFFER_ERROR;
121 72b033ef 2021-12-29 op evbuffer_drain(bufev->output, len);
124 72b033ef 2021-12-29 op if (EVBUFFER_LENGTH(bufev->output) != 0)
125 72b033ef 2021-12-29 op event_add(&bufev->ev_write, NULL);
127 72b033ef 2021-12-29 op if (bufev->writecb != NULL &&
128 72b033ef 2021-12-29 op EVBUFFER_LENGTH(bufev->output) <= bufev->wm_write.low)
129 72b033ef 2021-12-29 op (*bufev->writecb)(bufev, bufev->cbarg);
133 72b033ef 2021-12-29 op event_add(&bufev->ev_write, NULL);
137 72b033ef 2021-12-29 op (*bufev->errorcb)(bufev, what, bufev->cbarg);
141 72b033ef 2021-12-29 op proxy_read(struct bufferevent *bev, void *d)
143 72b033ef 2021-12-29 op struct client *c = d;
144 72b033ef 2021-12-29 op struct evbuffer *src = EVBUFFER_INPUT(bev);
149 72b033ef 2021-12-29 op /* intercept the header */
150 72b033ef 2021-12-29 op if (c->code == 0) {
151 72b033ef 2021-12-29 op hdr = evbuffer_readln(src, &len, EVBUFFER_EOL_CRLF_STRICT);
152 72b033ef 2021-12-29 op if (hdr == NULL) {
153 72b033ef 2021-12-29 op /* max reply + \r\n */
154 72b033ef 2021-12-29 op if (EVBUFFER_LENGTH(src) > 1029) {
155 eae52ad4 2023-06-06 op log_warnx("upstream server is trying to "
156 72b033ef 2021-12-29 op "send a header that's too long.");
157 72b033ef 2021-12-29 op proxy_error(bev, EVBUFFER_READ, c);
160 72b033ef 2021-12-29 op /* wait a bit */
164 72b033ef 2021-12-29 op if (len < 3 || len > 1029 ||
165 6130e0ee 2022-11-17 op !isdigit((unsigned char)hdr[0]) ||
166 6130e0ee 2022-11-17 op !isdigit((unsigned char)hdr[1]) ||
167 6130e0ee 2022-11-17 op !isspace((unsigned char)hdr[2])) {
169 eae52ad4 2023-06-06 op log_warnx("upstream server is trying to send a "
170 72b033ef 2021-12-29 op "header that's too long.");
171 72b033ef 2021-12-29 op proxy_error(bev, EVBUFFER_READ, c);
175 72b033ef 2021-12-29 op c->header = hdr;
176 72b033ef 2021-12-29 op code = (hdr[0] - '0') * 10 + (hdr[1] - '0');
178 72b033ef 2021-12-29 op if (code < 10 || code >= 70) {
179 eae52ad4 2023-06-06 op log_warnx("upstream server is trying to send an "
180 72b033ef 2021-12-29 op "invalid reply code: %d", code);
181 72b033ef 2021-12-29 op proxy_error(bev, EVBUFFER_READ, c);
185 72b033ef 2021-12-29 op start_reply(c, code, hdr + 3);
187 72b033ef 2021-12-29 op if (c->code < 20 || c->code > 29) {
188 72b033ef 2021-12-29 op proxy_error(bev, EVBUFFER_EOF, c);
193 72b033ef 2021-12-29 op bufferevent_write_buffer(c->bev, src);
197 72b033ef 2021-12-29 op proxy_write(struct bufferevent *bev, void *d)
199 72b033ef 2021-12-29 op struct evbuffer *dst = EVBUFFER_OUTPUT(bev);
201 72b033ef 2021-12-29 op /* request successfully sent */
202 72b033ef 2021-12-29 op if (EVBUFFER_LENGTH(dst) == 0)
203 72b033ef 2021-12-29 op bufferevent_disable(bev, EV_WRITE);
207 72b033ef 2021-12-29 op proxy_error(struct bufferevent *bev, short error, void *d)
209 72b033ef 2021-12-29 op struct client *c = d;
212 72b033ef 2021-12-29 op * If we're here it means that some kind of non-recoverable
213 72b033ef 2021-12-29 op * error appened.
216 72b033ef 2021-12-29 op bufferevent_free(bev);
217 72b033ef 2021-12-29 op c->proxybev = NULL;
219 72b033ef 2021-12-29 op tls_free(c->proxyctx);
220 72b033ef 2021-12-29 op c->proxyctx = NULL;
222 72b033ef 2021-12-29 op close(c->pfd);
225 72b033ef 2021-12-29 op /* EOF and no header */
226 72b033ef 2021-12-29 op if (c->code == 0) {
227 72b033ef 2021-12-29 op start_reply(c, PROXY_ERROR, "protocol error");
231 72b033ef 2021-12-29 op c->type = REQUEST_DONE;
232 72b033ef 2021-12-29 op client_write(c->bev, c);
236 593e412b 2021-01-01 op proxy_enqueue_req(struct client *c)
238 b7967bc1 2021-01-02 op struct proxy *p = c->proxy;
239 72b033ef 2021-12-29 op struct evbuffer *evb;
240 72b033ef 2021-12-29 op char iribuf[GEMINI_URL_LEN];
242 593e412b 2021-01-01 op c->proxybev = bufferevent_new(c->pfd, proxy_read, proxy_write,
243 593e412b 2021-01-01 op proxy_error, c);
244 593e412b 2021-01-01 op if (c->proxybev == NULL)
245 df5058c9 2023-06-05 op fatal("can't allocate bufferevent");
247 593e412b 2021-01-01 op if (!p->notls) {
248 593e412b 2021-01-01 op event_set(&c->proxybev->ev_read, c->pfd, EV_READ,
249 593e412b 2021-01-01 op proxy_tls_readcb, c->proxybev);
250 593e412b 2021-01-01 op event_set(&c->proxybev->ev_write, c->pfd, EV_WRITE,
251 593e412b 2021-01-01 op proxy_tls_writecb, c->proxybev);
253 593e412b 2021-01-01 op #if HAVE_LIBEVENT2
254 593e412b 2021-01-01 op evbuffer_unfreeze(c->proxybev->input, 0);
255 593e412b 2021-01-01 op evbuffer_unfreeze(c->proxybev->output, 1);
259 593e412b 2021-01-01 op serialize_iri(&c->iri, iribuf, sizeof(iribuf));
261 593e412b 2021-01-01 op evb = EVBUFFER_OUTPUT(c->proxybev);
262 593e412b 2021-01-01 op evbuffer_add_printf(evb, "%s\r\n", iribuf);
264 593e412b 2021-01-01 op bufferevent_enable(c->proxybev, EV_READ|EV_WRITE);
268 593e412b 2021-01-01 op proxy_handshake(int fd, short event, void *d)
270 593e412b 2021-01-01 op struct client *c = d;
272 72b033ef 2021-12-29 op if (event == EV_TIMEOUT) {
273 72b033ef 2021-12-29 op start_reply(c, PROXY_ERROR, "timeout");
277 72b033ef 2021-12-29 op switch (tls_handshake(c->proxyctx)) {
278 72b033ef 2021-12-29 op case TLS_WANT_POLLIN:
279 72b033ef 2021-12-29 op event_set(&c->proxyev, fd, EV_READ, proxy_handshake, c);
280 72b033ef 2021-12-29 op event_add(&c->proxyev, &handshake_timeout);
282 72b033ef 2021-12-29 op case TLS_WANT_POLLOUT:
283 72b033ef 2021-12-29 op event_set(&c->proxyev, fd, EV_WRITE, proxy_handshake, c);
284 72b033ef 2021-12-29 op event_add(&c->proxyev, &handshake_timeout);
287 eae52ad4 2023-06-06 op log_warnx("handshake with proxy failed: %s",
288 72b033ef 2021-12-29 op tls_error(c->proxyctx));
289 72b033ef 2021-12-29 op start_reply(c, PROXY_ERROR, "handshake failed");
293 e0f6dc64 2022-01-27 op c->proxyevset = 0;
294 593e412b 2021-01-01 op proxy_enqueue_req(c);
298 593e412b 2021-01-01 op proxy_setup_tls(struct client *c)
300 b7967bc1 2021-01-02 op struct proxy *p = c->proxy;
301 72b033ef 2021-12-29 op struct tls_config *conf = NULL;
302 1cdea97b 2022-01-30 op const char *hn;
304 72b033ef 2021-12-29 op if ((conf = tls_config_new()) == NULL)
307 5128c0b0 2021-01-01 op if (p->noverifyname)
308 5128c0b0 2021-01-01 op tls_config_insecure_noverifyname(conf);
310 72b033ef 2021-12-29 op tls_config_insecure_noverifycert(conf);
311 c7c8ef44 2021-01-01 op tls_config_set_protocols(conf, p->protocols);
313 7bdcc91e 2021-01-01 op if (p->cert != NULL) {
316 7bdcc91e 2021-01-01 op r = tls_config_set_cert_mem(conf, p->cert, p->certlen);
320 7bdcc91e 2021-01-01 op r = tls_config_set_key_mem(conf, p->key, p->keylen);
325 72b033ef 2021-12-29 op if ((c->proxyctx = tls_client()) == NULL)
328 72b033ef 2021-12-29 op if (tls_configure(c->proxyctx, conf) == -1)
331 534afd0d 2022-10-05 op if (*(hn = p->sni) == '\0')
332 1cdea97b 2022-01-30 op hn = p->host;
333 1cdea97b 2022-01-30 op if (tls_connect_socket(c->proxyctx, c->pfd, hn) == -1)
336 e0f6dc64 2022-01-27 op c->proxyevset = 1;
337 72b033ef 2021-12-29 op event_set(&c->proxyev, c->pfd, EV_READ|EV_WRITE, proxy_handshake, c);
338 72b033ef 2021-12-29 op event_add(&c->proxyev, &handshake_timeout);
340 72b033ef 2021-12-29 op tls_config_free(conf);
344 72b033ef 2021-12-29 op tls_config_free(conf);
345 593e412b 2021-01-01 op if (c->proxyctx != NULL) {
346 72b033ef 2021-12-29 op tls_free(c->proxyctx);
347 593e412b 2021-01-01 op c->proxyctx = NULL;
353 593e412b 2021-01-01 op proxy_init(struct client *c)
355 b7967bc1 2021-01-02 op struct proxy *p = c->proxy;
357 89efa81b 2022-01-27 op if (!p->notls && proxy_setup_tls(c) == -1)
359 89efa81b 2022-01-27 op else if (p->notls)
360 89efa81b 2022-01-27 op proxy_enqueue_req(c);
362 593e412b 2021-01-01 op c->type = REQUEST_PROXY;