002
2022-07-04
op
* Copyright (c) 2021, 2022 Omar Polo <op@omarpolo.com>
004
2021-12-29
op
* Permission to use, copy, modify, and distribute this software for any
005
2021-12-29
op
* purpose with or without fee is hereby granted, provided that the above
006
2021-12-29
op
* copyright notice and this permission notice appear in all copies.
008
2021-12-29
op
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
009
2021-12-29
op
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
010
2021-12-29
op
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
011
2021-12-29
op
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
012
2021-12-29
op
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
013
2021-12-29
op
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
014
2021-12-29
op
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
017
2021-12-29
op
#include "gmid.h"
019
2021-12-29
op
#include <ctype.h>
020
2021-12-29
op
#include <errno.h>
021
2021-12-29
op
#include <string.h>
023
2021-12-29
op
#define MIN(a, b) ((a) < (b) ? (a) : (b))
025
2022-03-19
op
static const struct timeval handshake_timeout = { 5, 0 };
027
2021-12-29
op
static void proxy_tls_readcb(int, short, void *);
028
2021-12-29
op
static void proxy_tls_writecb(int, short, void *);
029
2021-12-29
op
static void proxy_read(struct bufferevent *, void *);
030
2021-12-29
op
static void proxy_write(struct bufferevent *, void *);
031
2021-12-29
op
static void proxy_error(struct bufferevent *, short, void *);
033
2021-12-29
op
static void
034
2021-12-29
op
proxy_tls_readcb(int fd, short event, void *d)
036
2021-12-29
op
struct bufferevent *bufev = d;
037
2021-12-29
op
struct client *c = bufev->cbarg;
038
2021-12-29
op
char buf[IBUF_READ_SIZE];
039
2021-12-29
op
int what = EVBUFFER_READ;
040
2021-12-29
op
int howmuch = IBUF_READ_SIZE;
042
2021-12-29
op
ssize_t ret;
043
2021-12-29
op
size_t len;
045
2021-12-29
op
if (event == EV_TIMEOUT) {
046
2021-12-29
op
what |= EVBUFFER_TIMEOUT;
047
2021-12-29
op
goto err;
050
2021-12-29
op
if (bufev->wm_read.high != 0)
051
2021-12-29
op
howmuch = MIN(sizeof(buf), bufev->wm_read.high);
053
2021-12-29
op
switch (ret = tls_read(c->proxyctx, buf, howmuch)) {
054
2021-12-29
op
case TLS_WANT_POLLIN:
055
2021-12-29
op
case TLS_WANT_POLLOUT:
056
2021-12-29
op
goto retry;
058
2021-12-29
op
what |= EVBUFFER_ERROR;
059
2021-12-29
op
goto err;
061
2021-12-29
op
len = ret;
063
2021-12-29
op
if (len == 0) {
064
2021-12-29
op
what |= EVBUFFER_EOF;
065
2021-12-29
op
goto err;
068
2021-12-29
op
res = evbuffer_add(bufev->input, buf, len);
069
2021-12-29
op
if (res == -1) {
070
2021-12-29
op
what |= EVBUFFER_ERROR;
071
2021-12-29
op
goto err;
074
2021-12-29
op
event_add(&bufev->ev_read, NULL);
076
2021-12-29
op
len = EVBUFFER_LENGTH(bufev->input);
077
2021-12-29
op
if (bufev->wm_read.low != 0 && len < bufev->wm_read.low)
080
2021-12-29
op
if (bufev->readcb != NULL)
081
2021-12-29
op
(*bufev->readcb)(bufev, bufev->cbarg);
085
2021-12-29
op
event_add(&bufev->ev_read, NULL);
089
2021-12-29
op
(*bufev->errorcb)(bufev, what, bufev->cbarg);
092
2021-12-29
op
static void
093
2021-12-29
op
proxy_tls_writecb(int fd, short event, void *d)
095
2021-12-29
op
struct bufferevent *bufev = d;
096
2021-12-29
op
struct client *c = bufev->cbarg;
097
2021-12-29
op
ssize_t ret;
098
2021-12-29
op
size_t len;
099
2021-12-29
op
short what = EVBUFFER_WRITE;
101
2021-12-29
op
if (event & EV_TIMEOUT) {
102
2021-12-29
op
what |= EVBUFFER_TIMEOUT;
103
2021-12-29
op
goto err;
106
2021-12-29
op
if (EVBUFFER_LENGTH(bufev->output) != 0) {
107
2021-12-29
op
ret = tls_write(c->proxyctx, EVBUFFER_DATA(bufev->output),
108
2021-12-29
op
EVBUFFER_LENGTH(bufev->output));
109
2021-12-29
op
switch (ret) {
110
2021-12-29
op
case TLS_WANT_POLLIN:
111
2021-12-29
op
case TLS_WANT_POLLOUT:
112
2021-12-29
op
goto retry;
114
2021-12-29
op
what |= EVBUFFER_ERROR;
115
2021-12-29
op
goto err;
117
2021-12-29
op
len = ret;
119
2021-12-29
op
evbuffer_drain(bufev->output, len);
122
2021-12-29
op
if (EVBUFFER_LENGTH(bufev->output) != 0)
123
2021-12-29
op
event_add(&bufev->ev_write, NULL);
125
2021-12-29
op
if (bufev->writecb != NULL &&
126
2021-12-29
op
EVBUFFER_LENGTH(bufev->output) <= bufev->wm_write.low)
127
2021-12-29
op
(*bufev->writecb)(bufev, bufev->cbarg);
131
2021-12-29
op
event_add(&bufev->ev_write, NULL);
135
2021-12-29
op
(*bufev->errorcb)(bufev, what, bufev->cbarg);
138
2021-12-29
op
static void
139
2021-12-29
op
proxy_read(struct bufferevent *bev, void *d)
141
2021-12-29
op
struct client *c = d;
142
2021-12-29
op
struct evbuffer *src = EVBUFFER_INPUT(bev);
143
2021-12-29
op
char *hdr;
144
2021-12-29
op
size_t len;
145
2021-12-29
op
int code;
147
2021-12-29
op
/* intercept the header */
148
2021-12-29
op
if (c->code == 0) {
149
2021-12-29
op
hdr = evbuffer_readln(src, &len, EVBUFFER_EOL_CRLF_STRICT);
150
2021-12-29
op
if (hdr == NULL) {
151
2021-12-29
op
/* max reply + \r\n */
152
2021-12-29
op
if (EVBUFFER_LENGTH(src) > 1029) {
153
2021-12-29
op
log_warn(c, "upstream server is trying to "
154
2021-12-29
op
"send a header that's too long.");
155
2021-12-29
op
proxy_error(bev, EVBUFFER_READ, c);
158
2021-12-29
op
/* wait a bit */
162
2021-12-29
op
if (len < 3 || len > 1029 ||
163
2021-12-29
op
!isdigit(hdr[0]) ||
164
2021-12-29
op
!isdigit(hdr[1]) ||
165
2021-12-29
op
!isspace(hdr[2])) {
166
2021-12-29
op
free(hdr);
167
2021-12-29
op
log_warn(c, "upstream server is trying to send a "
168
2021-12-29
op
"header that's too long.");
169
2021-12-29
op
proxy_error(bev, EVBUFFER_READ, c);
173
2021-12-29
op
c->header = hdr;
174
2021-12-29
op
code = (hdr[0] - '0') * 10 + (hdr[1] - '0');
176
2021-12-29
op
if (code < 10 || code >= 70) {
177
2021-12-29
op
log_warn(c, "upstream server is trying to send an "
178
2021-12-29
op
"invalid reply code: %d", code);
179
2021-12-29
op
proxy_error(bev, EVBUFFER_READ, c);
183
2021-12-29
op
start_reply(c, code, hdr + 3);
185
2021-12-29
op
if (c->code < 20 || c->code > 29) {
186
2021-12-29
op
proxy_error(bev, EVBUFFER_EOF, c);
191
2021-12-29
op
bufferevent_write_buffer(c->bev, src);
194
2021-12-29
op
static void
195
2021-12-29
op
proxy_write(struct bufferevent *bev, void *d)
197
2021-12-29
op
struct evbuffer *dst = EVBUFFER_OUTPUT(bev);
199
2021-12-29
op
/* request successfully sent */
200
2021-12-29
op
if (EVBUFFER_LENGTH(dst) == 0)
201
2021-12-29
op
bufferevent_disable(bev, EV_WRITE);
204
2021-12-29
op
static void
205
2021-12-29
op
proxy_error(struct bufferevent *bev, short error, void *d)
207
2021-12-29
op
struct client *c = d;
210
2021-12-29
op
* If we're here it means that some kind of non-recoverable
211
2021-12-29
op
* error appened.
214
2021-12-29
op
bufferevent_free(bev);
215
2021-12-29
op
c->proxybev = NULL;
217
2021-12-29
op
tls_free(c->proxyctx);
218
2021-12-29
op
c->proxyctx = NULL;
220
2021-12-29
op
close(c->pfd);
221
2021-12-29
op
c->pfd = -1;
223
2021-12-29
op
/* EOF and no header */
224
2021-12-29
op
if (c->code == 0) {
225
2021-12-29
op
start_reply(c, PROXY_ERROR, "protocol error");
229
2021-12-29
op
c->type = REQUEST_DONE;
230
2021-12-29
op
client_write(c->bev, c);
233
2021-12-29
op
static void
234
2021-01-01
op
proxy_enqueue_req(struct client *c)
236
2021-01-02
op
struct proxy *p = c->proxy;
237
2021-12-29
op
struct evbuffer *evb;
238
2021-12-29
op
char iribuf[GEMINI_URL_LEN];
240
2021-01-01
op
c->proxybev = bufferevent_new(c->pfd, proxy_read, proxy_write,
241
2021-01-01
op
proxy_error, c);
242
2021-01-01
op
if (c->proxybev == NULL)
243
2021-01-01
op
fatal("can't allocate bufferevent: %s", strerror(errno));
245
2021-01-01
op
if (!p->notls) {
246
2021-01-01
op
event_set(&c->proxybev->ev_read, c->pfd, EV_READ,
247
2021-01-01
op
proxy_tls_readcb, c->proxybev);
248
2021-01-01
op
event_set(&c->proxybev->ev_write, c->pfd, EV_WRITE,
249
2021-01-01
op
proxy_tls_writecb, c->proxybev);
251
2021-01-01
op
#if HAVE_LIBEVENT2
252
2021-01-01
op
evbuffer_unfreeze(c->proxybev->input, 0);
253
2021-01-01
op
evbuffer_unfreeze(c->proxybev->output, 1);
257
2021-01-01
op
serialize_iri(&c->iri, iribuf, sizeof(iribuf));
259
2021-01-01
op
evb = EVBUFFER_OUTPUT(c->proxybev);
260
2021-01-01
op
evbuffer_add_printf(evb, "%s\r\n", iribuf);
262
2021-01-01
op
bufferevent_enable(c->proxybev, EV_READ|EV_WRITE);
265
2021-01-01
op
static void
266
2021-01-01
op
proxy_handshake(int fd, short event, void *d)
268
2021-01-01
op
struct client *c = d;
270
2021-12-29
op
if (event == EV_TIMEOUT) {
271
2021-12-29
op
start_reply(c, PROXY_ERROR, "timeout");
275
2021-12-29
op
switch (tls_handshake(c->proxyctx)) {
276
2021-12-29
op
case TLS_WANT_POLLIN:
277
2021-12-29
op
event_set(&c->proxyev, fd, EV_READ, proxy_handshake, c);
278
2021-12-29
op
event_add(&c->proxyev, &handshake_timeout);
280
2021-12-29
op
case TLS_WANT_POLLOUT:
281
2021-12-29
op
event_set(&c->proxyev, fd, EV_WRITE, proxy_handshake, c);
282
2021-12-29
op
event_add(&c->proxyev, &handshake_timeout);
285
2021-12-29
op
log_warn(c, "handshake with proxy failed: %s",
286
2021-12-29
op
tls_error(c->proxyctx));
287
2021-12-29
op
start_reply(c, PROXY_ERROR, "handshake failed");
291
2022-01-27
op
c->proxyevset = 0;
292
2021-01-01
op
proxy_enqueue_req(c);
295
2021-01-01
op
static int
296
2021-01-01
op
proxy_setup_tls(struct client *c)
298
2021-01-02
op
struct proxy *p = c->proxy;
299
2021-12-29
op
struct tls_config *conf = NULL;
300
2022-01-30
op
const char *hn;
302
2021-12-29
op
if ((conf = tls_config_new()) == NULL)
303
2021-12-29
op
return -1;
305
2021-01-01
op
if (p->noverifyname)
306
2021-01-01
op
tls_config_insecure_noverifyname(conf);
308
2021-12-29
op
tls_config_insecure_noverifycert(conf);
309
2021-01-01
op
tls_config_set_protocols(conf, p->protocols);
311
2021-01-01
op
if (p->cert != NULL) {
314
2021-01-01
op
r = tls_config_set_cert_mem(conf, p->cert, p->certlen);
315
2021-01-01
op
if (r == -1)
316
2021-01-01
op
goto err;
318
2021-01-01
op
r = tls_config_set_key_mem(conf, p->key, p->keylen);
319
2021-01-01
op
if (r == -1)
320
2021-01-01
op
goto err;
323
2021-12-29
op
if ((c->proxyctx = tls_client()) == NULL)
324
2021-12-29
op
goto err;
326
2021-12-29
op
if (tls_configure(c->proxyctx, conf) == -1)
327
2021-12-29
op
goto err;
329
2022-01-30
op
if ((hn = p->sni) == NULL)
330
2022-01-30
op
hn = p->host;
331
2022-01-30
op
if (tls_connect_socket(c->proxyctx, c->pfd, hn) == -1)
332
2021-12-29
op
goto err;
334
2022-01-27
op
c->proxyevset = 1;
335
2021-12-29
op
event_set(&c->proxyev, c->pfd, EV_READ|EV_WRITE, proxy_handshake, c);
336
2021-12-29
op
event_add(&c->proxyev, &handshake_timeout);
338
2021-12-29
op
tls_config_free(conf);
339
2021-12-29
op
return 0;
342
2021-12-29
op
tls_config_free(conf);
343
2021-01-01
op
if (c->proxyctx != NULL) {
344
2021-12-29
op
tls_free(c->proxyctx);
345
2021-01-01
op
c->proxyctx = NULL;
347
2021-12-29
op
return -1;
351
2021-01-01
op
proxy_init(struct client *c)
353
2021-01-02
op
struct proxy *p = c->proxy;
355
2022-01-27
op
if (!p->notls && proxy_setup_tls(c) == -1)
356
2022-01-27
op
return -1;
357
2022-01-27
op
else if (p->notls)
358
2022-01-27
op
proxy_enqueue_req(c);
360
2021-01-01
op
c->type = REQUEST_PROXY;
362
2022-01-27
op
return 0;