2 43b38b2d 2023-10-18 op * Copyright (c) 2021, 2022, 2023 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 390d312b 2023-08-09 op if (start_reply(c, code, hdr + 3) == -1 ||
186 390d312b 2023-08-09 op c->code < 20 || c->code > 29) {
187 72b033ef 2021-12-29 op proxy_error(bev, EVBUFFER_EOF, c);
192 72b033ef 2021-12-29 op bufferevent_write_buffer(c->bev, src);
196 72b033ef 2021-12-29 op proxy_write(struct bufferevent *bev, void *d)
198 72b033ef 2021-12-29 op struct evbuffer *dst = EVBUFFER_OUTPUT(bev);
200 72b033ef 2021-12-29 op /* request successfully sent */
201 72b033ef 2021-12-29 op if (EVBUFFER_LENGTH(dst) == 0)
202 72b033ef 2021-12-29 op bufferevent_disable(bev, EV_WRITE);
206 72b033ef 2021-12-29 op proxy_error(struct bufferevent *bev, short error, void *d)
208 72b033ef 2021-12-29 op struct client *c = d;
211 72b033ef 2021-12-29 op * If we're here it means that some kind of non-recoverable
212 1134ea14 2023-07-01 op * error happened.
215 72b033ef 2021-12-29 op bufferevent_free(bev);
216 72b033ef 2021-12-29 op c->proxybev = NULL;
218 72b033ef 2021-12-29 op tls_free(c->proxyctx);
219 72b033ef 2021-12-29 op c->proxyctx = NULL;
221 72b033ef 2021-12-29 op close(c->pfd);
224 72b033ef 2021-12-29 op /* EOF and no header */
225 72b033ef 2021-12-29 op if (c->code == 0) {
226 72b033ef 2021-12-29 op start_reply(c, PROXY_ERROR, "protocol error");
230 72b033ef 2021-12-29 op c->type = REQUEST_DONE;
231 72b033ef 2021-12-29 op client_write(c->bev, c);
235 593e412b 2021-01-01 op proxy_enqueue_req(struct client *c)
237 b7967bc1 2021-01-02 op struct proxy *p = c->proxy;
238 72b033ef 2021-12-29 op struct evbuffer *evb;
239 72b033ef 2021-12-29 op char iribuf[GEMINI_URL_LEN];
241 593e412b 2021-01-01 op c->proxybev = bufferevent_new(c->pfd, proxy_read, proxy_write,
242 593e412b 2021-01-01 op proxy_error, c);
243 593e412b 2021-01-01 op if (c->proxybev == NULL)
244 df5058c9 2023-06-05 op fatal("can't allocate bufferevent");
246 593e412b 2021-01-01 op if (!p->notls) {
247 593e412b 2021-01-01 op event_set(&c->proxybev->ev_read, c->pfd, EV_READ,
248 593e412b 2021-01-01 op proxy_tls_readcb, c->proxybev);
249 593e412b 2021-01-01 op event_set(&c->proxybev->ev_write, c->pfd, EV_WRITE,
250 593e412b 2021-01-01 op proxy_tls_writecb, c->proxybev);
252 593e412b 2021-01-01 op #if HAVE_LIBEVENT2
253 593e412b 2021-01-01 op evbuffer_unfreeze(c->proxybev->input, 0);
254 593e412b 2021-01-01 op evbuffer_unfreeze(c->proxybev->output, 1);
258 593e412b 2021-01-01 op serialize_iri(&c->iri, iribuf, sizeof(iribuf));
260 593e412b 2021-01-01 op evb = EVBUFFER_OUTPUT(c->proxybev);
261 593e412b 2021-01-01 op evbuffer_add_printf(evb, "%s\r\n", iribuf);
263 593e412b 2021-01-01 op bufferevent_enable(c->proxybev, EV_READ|EV_WRITE);
267 593e412b 2021-01-01 op proxy_handshake(int fd, short event, void *d)
269 593e412b 2021-01-01 op struct client *c = d;
271 72b033ef 2021-12-29 op if (event == EV_TIMEOUT) {
272 72b033ef 2021-12-29 op start_reply(c, PROXY_ERROR, "timeout");
276 72b033ef 2021-12-29 op switch (tls_handshake(c->proxyctx)) {
277 72b033ef 2021-12-29 op case TLS_WANT_POLLIN:
278 72b033ef 2021-12-29 op event_set(&c->proxyev, fd, EV_READ, proxy_handshake, c);
279 72b033ef 2021-12-29 op event_add(&c->proxyev, &handshake_timeout);
281 72b033ef 2021-12-29 op case TLS_WANT_POLLOUT:
282 72b033ef 2021-12-29 op event_set(&c->proxyev, fd, EV_WRITE, proxy_handshake, c);
283 72b033ef 2021-12-29 op event_add(&c->proxyev, &handshake_timeout);
286 eae52ad4 2023-06-06 op log_warnx("handshake with proxy failed: %s",
287 72b033ef 2021-12-29 op tls_error(c->proxyctx));
288 72b033ef 2021-12-29 op start_reply(c, PROXY_ERROR, "handshake failed");
292 e0f6dc64 2022-01-27 op c->proxyevset = 0;
293 593e412b 2021-01-01 op proxy_enqueue_req(c);
297 593e412b 2021-01-01 op proxy_setup_tls(struct client *c)
299 b7967bc1 2021-01-02 op struct proxy *p = c->proxy;
300 72b033ef 2021-12-29 op struct tls_config *conf = NULL;
301 1cdea97b 2022-01-30 op const char *hn;
303 72b033ef 2021-12-29 op if ((conf = tls_config_new()) == NULL)
306 5128c0b0 2021-01-01 op if (p->noverifyname)
307 5128c0b0 2021-01-01 op tls_config_insecure_noverifyname(conf);
309 72b033ef 2021-12-29 op tls_config_insecure_noverifycert(conf);
310 c7c8ef44 2021-01-01 op tls_config_set_protocols(conf, p->protocols);
312 7bdcc91e 2021-01-01 op if (p->cert != NULL) {
315 7bdcc91e 2021-01-01 op r = tls_config_set_cert_mem(conf, p->cert, p->certlen);
319 7bdcc91e 2021-01-01 op r = tls_config_set_key_mem(conf, p->key, p->keylen);
324 72b033ef 2021-12-29 op if ((c->proxyctx = tls_client()) == NULL)
327 72b033ef 2021-12-29 op if (tls_configure(c->proxyctx, conf) == -1)
330 534afd0d 2022-10-05 op if (*(hn = p->sni) == '\0')
331 1cdea97b 2022-01-30 op hn = p->host;
332 1cdea97b 2022-01-30 op if (tls_connect_socket(c->proxyctx, c->pfd, hn) == -1)
335 e0f6dc64 2022-01-27 op c->proxyevset = 1;
336 72b033ef 2021-12-29 op event_set(&c->proxyev, c->pfd, EV_READ|EV_WRITE, proxy_handshake, c);
337 72b033ef 2021-12-29 op event_add(&c->proxyev, &handshake_timeout);
339 72b033ef 2021-12-29 op tls_config_free(conf);
343 72b033ef 2021-12-29 op tls_config_free(conf);
344 593e412b 2021-01-01 op if (c->proxyctx != NULL) {
345 72b033ef 2021-12-29 op tls_free(c->proxyctx);
346 593e412b 2021-01-01 op c->proxyctx = NULL;
352 593e412b 2021-01-01 op proxy_init(struct client *c)
354 b7967bc1 2021-01-02 op struct proxy *p = c->proxy;
356 89efa81b 2022-01-27 op if (!p->notls && proxy_setup_tls(c) == -1)
358 89efa81b 2022-01-27 op else if (p->notls)
359 89efa81b 2022-01-27 op proxy_enqueue_req(c);
361 593e412b 2021-01-01 op c->type = REQUEST_PROXY;