2 72b033ef 2021-12-29 op * Copyright (c) 2021 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 72b033ef 2021-12-29 op #define MIN(a, b) ((a) < (b) ? (a) : (b))
25 72b033ef 2021-12-29 op static struct timeval handshake_timeout = { 5, 0 };
27 72b033ef 2021-12-29 op static void proxy_tls_readcb(int, short, void *);
28 72b033ef 2021-12-29 op static void proxy_tls_writecb(int, short, void *);
29 72b033ef 2021-12-29 op static void proxy_read(struct bufferevent *, void *);
30 72b033ef 2021-12-29 op static void proxy_write(struct bufferevent *, void *);
31 72b033ef 2021-12-29 op static void proxy_error(struct bufferevent *, short, void *);
34 72b033ef 2021-12-29 op proxy_tls_readcb(int fd, short event, void *d)
36 72b033ef 2021-12-29 op struct bufferevent *bufev = d;
37 72b033ef 2021-12-29 op struct client *c = bufev->cbarg;
38 72b033ef 2021-12-29 op char buf[IBUF_READ_SIZE];
39 72b033ef 2021-12-29 op int what = EVBUFFER_READ;
40 72b033ef 2021-12-29 op int howmuch = IBUF_READ_SIZE;
45 72b033ef 2021-12-29 op if (event == EV_TIMEOUT) {
46 72b033ef 2021-12-29 op what |= EVBUFFER_TIMEOUT;
50 72b033ef 2021-12-29 op if (bufev->wm_read.high != 0)
51 72b033ef 2021-12-29 op howmuch = MIN(sizeof(buf), bufev->wm_read.high);
53 72b033ef 2021-12-29 op switch (ret = tls_read(c->proxyctx, buf, howmuch)) {
54 72b033ef 2021-12-29 op case TLS_WANT_POLLIN:
55 72b033ef 2021-12-29 op case TLS_WANT_POLLOUT:
58 72b033ef 2021-12-29 op what |= EVBUFFER_ERROR;
63 72b033ef 2021-12-29 op if (len == 0) {
64 72b033ef 2021-12-29 op what |= EVBUFFER_EOF;
68 72b033ef 2021-12-29 op res = evbuffer_add(bufev->input, buf, len);
69 72b033ef 2021-12-29 op if (res == -1) {
70 72b033ef 2021-12-29 op what |= EVBUFFER_ERROR;
74 72b033ef 2021-12-29 op event_add(&bufev->ev_read, NULL);
76 72b033ef 2021-12-29 op len = EVBUFFER_LENGTH(bufev->input);
77 72b033ef 2021-12-29 op if (bufev->wm_read.low != 0 && len < bufev->wm_read.low)
80 72b033ef 2021-12-29 op if (bufev->readcb != NULL)
81 72b033ef 2021-12-29 op (*bufev->readcb)(bufev, bufev->cbarg);
85 72b033ef 2021-12-29 op event_add(&bufev->ev_read, NULL);
89 72b033ef 2021-12-29 op (*bufev->errorcb)(bufev, what, bufev->cbarg);
93 72b033ef 2021-12-29 op proxy_tls_writecb(int fd, short event, void *d)
95 72b033ef 2021-12-29 op struct bufferevent *bufev = d;
96 72b033ef 2021-12-29 op struct client *c = bufev->cbarg;
99 72b033ef 2021-12-29 op short what = EVBUFFER_WRITE;
101 72b033ef 2021-12-29 op if (event & EV_TIMEOUT) {
102 72b033ef 2021-12-29 op what |= EVBUFFER_TIMEOUT;
106 72b033ef 2021-12-29 op if (EVBUFFER_LENGTH(bufev->output) != 0) {
107 72b033ef 2021-12-29 op ret = tls_write(c->proxyctx, EVBUFFER_DATA(bufev->output),
108 72b033ef 2021-12-29 op EVBUFFER_LENGTH(bufev->output));
109 72b033ef 2021-12-29 op switch (ret) {
110 72b033ef 2021-12-29 op case TLS_WANT_POLLIN:
111 72b033ef 2021-12-29 op case TLS_WANT_POLLOUT:
114 72b033ef 2021-12-29 op what |= EVBUFFER_ERROR;
119 72b033ef 2021-12-29 op evbuffer_drain(bufev->output, len);
122 72b033ef 2021-12-29 op if (EVBUFFER_LENGTH(bufev->output) != 0)
123 72b033ef 2021-12-29 op event_add(&bufev->ev_write, NULL);
125 72b033ef 2021-12-29 op if (bufev->writecb != NULL &&
126 72b033ef 2021-12-29 op EVBUFFER_LENGTH(bufev->output) <= bufev->wm_write.low)
127 72b033ef 2021-12-29 op (*bufev->writecb)(bufev, bufev->cbarg);
131 72b033ef 2021-12-29 op event_add(&bufev->ev_write, NULL);
135 72b033ef 2021-12-29 op (*bufev->errorcb)(bufev, what, bufev->cbarg);
139 72b033ef 2021-12-29 op proxy_read(struct bufferevent *bev, void *d)
141 72b033ef 2021-12-29 op struct client *c = d;
142 72b033ef 2021-12-29 op struct evbuffer *src = EVBUFFER_INPUT(bev);
147 72b033ef 2021-12-29 op /* intercept the header */
148 72b033ef 2021-12-29 op if (c->code == 0) {
149 72b033ef 2021-12-29 op hdr = evbuffer_readln(src, &len, EVBUFFER_EOL_CRLF_STRICT);
150 72b033ef 2021-12-29 op if (hdr == NULL) {
151 72b033ef 2021-12-29 op /* max reply + \r\n */
152 72b033ef 2021-12-29 op if (EVBUFFER_LENGTH(src) > 1029) {
153 72b033ef 2021-12-29 op log_warn(c, "upstream server is trying to "
154 72b033ef 2021-12-29 op "send a header that's too long.");
155 72b033ef 2021-12-29 op proxy_error(bev, EVBUFFER_READ, c);
158 72b033ef 2021-12-29 op /* wait a bit */
162 72b033ef 2021-12-29 op if (len < 3 || len > 1029 ||
163 72b033ef 2021-12-29 op !isdigit(hdr[0]) ||
164 72b033ef 2021-12-29 op !isdigit(hdr[1]) ||
165 72b033ef 2021-12-29 op !isspace(hdr[2])) {
167 72b033ef 2021-12-29 op log_warn(c, "upstream server is trying to send a "
168 72b033ef 2021-12-29 op "header that's too long.");
169 72b033ef 2021-12-29 op proxy_error(bev, EVBUFFER_READ, c);
173 72b033ef 2021-12-29 op c->header = hdr;
174 72b033ef 2021-12-29 op code = (hdr[0] - '0') * 10 + (hdr[1] - '0');
176 72b033ef 2021-12-29 op if (code < 10 || code >= 70) {
177 72b033ef 2021-12-29 op log_warn(c, "upstream server is trying to send an "
178 72b033ef 2021-12-29 op "invalid reply code: %d", code);
179 72b033ef 2021-12-29 op proxy_error(bev, EVBUFFER_READ, c);
183 72b033ef 2021-12-29 op start_reply(c, code, hdr + 3);
185 72b033ef 2021-12-29 op if (c->code < 20 || c->code > 29) {
186 72b033ef 2021-12-29 op proxy_error(bev, EVBUFFER_EOF, c);
191 72b033ef 2021-12-29 op bufferevent_write_buffer(c->bev, src);
195 72b033ef 2021-12-29 op proxy_write(struct bufferevent *bev, void *d)
197 72b033ef 2021-12-29 op struct evbuffer *dst = EVBUFFER_OUTPUT(bev);
199 72b033ef 2021-12-29 op /* request successfully sent */
200 72b033ef 2021-12-29 op if (EVBUFFER_LENGTH(dst) == 0)
201 72b033ef 2021-12-29 op bufferevent_disable(bev, EV_WRITE);
205 72b033ef 2021-12-29 op proxy_error(struct bufferevent *bev, short error, void *d)
207 72b033ef 2021-12-29 op struct client *c = d;
210 72b033ef 2021-12-29 op * If we're here it means that some kind of non-recoverable
211 72b033ef 2021-12-29 op * error appened.
214 72b033ef 2021-12-29 op bufferevent_free(bev);
215 72b033ef 2021-12-29 op c->proxybev = NULL;
217 72b033ef 2021-12-29 op tls_free(c->proxyctx);
218 72b033ef 2021-12-29 op c->proxyctx = NULL;
220 72b033ef 2021-12-29 op close(c->pfd);
223 72b033ef 2021-12-29 op /* EOF and no header */
224 72b033ef 2021-12-29 op if (c->code == 0) {
225 72b033ef 2021-12-29 op start_reply(c, PROXY_ERROR, "protocol error");
229 72b033ef 2021-12-29 op c->type = REQUEST_DONE;
230 72b033ef 2021-12-29 op client_write(c->bev, c);
234 593e412b 2021-01-01 op proxy_enqueue_req(struct client *c)
236 b7967bc1 2021-01-02 op struct proxy *p = c->proxy;
237 72b033ef 2021-12-29 op struct evbuffer *evb;
238 72b033ef 2021-12-29 op char iribuf[GEMINI_URL_LEN];
240 593e412b 2021-01-01 op c->proxybev = bufferevent_new(c->pfd, proxy_read, proxy_write,
241 593e412b 2021-01-01 op proxy_error, c);
242 593e412b 2021-01-01 op if (c->proxybev == NULL)
243 593e412b 2021-01-01 op fatal("can't allocate bufferevent: %s", strerror(errno));
245 593e412b 2021-01-01 op if (!p->notls) {
246 593e412b 2021-01-01 op event_set(&c->proxybev->ev_read, c->pfd, EV_READ,
247 593e412b 2021-01-01 op proxy_tls_readcb, c->proxybev);
248 593e412b 2021-01-01 op event_set(&c->proxybev->ev_write, c->pfd, EV_WRITE,
249 593e412b 2021-01-01 op proxy_tls_writecb, c->proxybev);
251 593e412b 2021-01-01 op #if HAVE_LIBEVENT2
252 593e412b 2021-01-01 op evbuffer_unfreeze(c->proxybev->input, 0);
253 593e412b 2021-01-01 op evbuffer_unfreeze(c->proxybev->output, 1);
257 593e412b 2021-01-01 op serialize_iri(&c->iri, iribuf, sizeof(iribuf));
259 593e412b 2021-01-01 op evb = EVBUFFER_OUTPUT(c->proxybev);
260 593e412b 2021-01-01 op evbuffer_add_printf(evb, "%s\r\n", iribuf);
262 593e412b 2021-01-01 op bufferevent_enable(c->proxybev, EV_READ|EV_WRITE);
266 593e412b 2021-01-01 op proxy_handshake(int fd, short event, void *d)
268 593e412b 2021-01-01 op struct client *c = d;
270 72b033ef 2021-12-29 op if (event == EV_TIMEOUT) {
271 72b033ef 2021-12-29 op start_reply(c, PROXY_ERROR, "timeout");
275 72b033ef 2021-12-29 op switch (tls_handshake(c->proxyctx)) {
276 72b033ef 2021-12-29 op case TLS_WANT_POLLIN:
277 72b033ef 2021-12-29 op event_set(&c->proxyev, fd, EV_READ, proxy_handshake, c);
278 72b033ef 2021-12-29 op event_add(&c->proxyev, &handshake_timeout);
280 72b033ef 2021-12-29 op case TLS_WANT_POLLOUT:
281 72b033ef 2021-12-29 op event_set(&c->proxyev, fd, EV_WRITE, proxy_handshake, c);
282 72b033ef 2021-12-29 op event_add(&c->proxyev, &handshake_timeout);
285 72b033ef 2021-12-29 op log_warn(c, "handshake with proxy failed: %s",
286 72b033ef 2021-12-29 op tls_error(c->proxyctx));
287 72b033ef 2021-12-29 op start_reply(c, PROXY_ERROR, "handshake failed");
291 e0f6dc64 2022-01-27 op c->proxyevset = 0;
292 593e412b 2021-01-01 op proxy_enqueue_req(c);
296 593e412b 2021-01-01 op proxy_setup_tls(struct client *c)
298 b7967bc1 2021-01-02 op struct proxy *p = c->proxy;
299 72b033ef 2021-12-29 op struct tls_config *conf = NULL;
300 1cdea97b 2022-01-30 op const char *hn;
302 72b033ef 2021-12-29 op if ((conf = tls_config_new()) == NULL)
305 5128c0b0 2021-01-01 op if (p->noverifyname)
306 5128c0b0 2021-01-01 op tls_config_insecure_noverifyname(conf);
308 72b033ef 2021-12-29 op tls_config_insecure_noverifycert(conf);
309 c7c8ef44 2021-01-01 op tls_config_set_protocols(conf, p->protocols);
311 7bdcc91e 2021-01-01 op if (p->cert != NULL) {
314 7bdcc91e 2021-01-01 op r = tls_config_set_cert_mem(conf, p->cert, p->certlen);
318 7bdcc91e 2021-01-01 op r = tls_config_set_key_mem(conf, p->key, p->keylen);
323 72b033ef 2021-12-29 op if ((c->proxyctx = tls_client()) == NULL)
326 72b033ef 2021-12-29 op if (tls_configure(c->proxyctx, conf) == -1)
329 1cdea97b 2022-01-30 op if ((hn = p->sni) == NULL)
330 1cdea97b 2022-01-30 op hn = p->host;
331 1cdea97b 2022-01-30 op if (tls_connect_socket(c->proxyctx, c->pfd, hn) == -1)
334 e0f6dc64 2022-01-27 op c->proxyevset = 1;
335 72b033ef 2021-12-29 op event_set(&c->proxyev, c->pfd, EV_READ|EV_WRITE, proxy_handshake, c);
336 72b033ef 2021-12-29 op event_add(&c->proxyev, &handshake_timeout);
338 72b033ef 2021-12-29 op tls_config_free(conf);
342 72b033ef 2021-12-29 op tls_config_free(conf);
343 593e412b 2021-01-01 op if (c->proxyctx != NULL) {
344 72b033ef 2021-12-29 op tls_free(c->proxyctx);
345 593e412b 2021-01-01 op c->proxyctx = NULL;
351 593e412b 2021-01-01 op proxy_init(struct client *c)
353 b7967bc1 2021-01-02 op struct proxy *p = c->proxy;
355 89efa81b 2022-01-27 op if (!p->notls && proxy_setup_tls(c) == -1)
357 89efa81b 2022-01-27 op else if (p->notls)
358 89efa81b 2022-01-27 op proxy_enqueue_req(c);
360 593e412b 2021-01-01 op c->type = REQUEST_PROXY;