1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
|
/*
* Copyright 2023 Vsevolod Stakhov
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "http_context.h"
#include "http_private.h"
#include "keypair.h"
#include "keypairs_cache.h"
#include "cfg_file.h"
#include "contrib/libottery/ottery.h"
#include "contrib/http-parser/http_parser.h"
#include "ssl_util.h"
#include "rspamd.h"
#include "libev_helper.h"
INIT_LOG_MODULE(http_context)
#define msg_debug_http_context(...) rspamd_conditional_debug_fast(NULL, NULL, \
rspamd_http_context_log_id, "http_context", NULL, \
G_STRFUNC, \
__VA_ARGS__)
static struct rspamd_http_context *default_ctx = NULL;
struct rspamd_http_keepalive_cbdata {
struct rspamd_http_connection *conn;
struct rspamd_http_context *ctx;
GQueue *queue;
GList *link;
struct rspamd_io_ev ev;
};
static void
rspamd_http_keepalive_queue_cleanup(GQueue *conns)
{
GList *cur;
cur = conns->head;
while (cur) {
struct rspamd_http_keepalive_cbdata *cbd;
cbd = (struct rspamd_http_keepalive_cbdata *) cur->data;
/* unref call closes fd, so we need to remove ev watcher first! */
rspamd_ev_watcher_stop(cbd->ctx->event_loop, &cbd->ev);
rspamd_http_connection_unref(cbd->conn);
g_free(cbd);
cur = cur->next;
}
g_queue_clear(conns);
}
static void
rspamd_http_context_client_rotate_ev(struct ev_loop *loop, ev_timer *w, int revents)
{
struct rspamd_http_context *ctx = (struct rspamd_http_context *) w->data;
gpointer kp;
w->repeat = rspamd_time_jitter(ctx->config.client_key_rotate_time, 0);
msg_debug_http_context("rotate local keypair, next rotate in %.0f seconds",
w->repeat);
ev_timer_again(loop, w);
kp = ctx->client_kp;
ctx->client_kp = rspamd_keypair_new(RSPAMD_KEYPAIR_KEX,
RSPAMD_CRYPTOBOX_MODE_25519);
rspamd_keypair_unref(kp);
}
static struct rspamd_http_context *
rspamd_http_context_new_default(struct rspamd_config *cfg,
struct ev_loop *ev_base,
struct upstream_ctx *ups_ctx)
{
struct rspamd_http_context *ctx;
static const int default_kp_size = 1024;
static const gdouble default_rotate_time = 120;
static const gdouble default_keepalive_interval = 65;
static const gchar *default_user_agent = "rspamd-" RSPAMD_VERSION_FULL;
static const gchar *default_server_hdr = "rspamd/" RSPAMD_VERSION_FULL;
ctx = g_malloc0(sizeof(*ctx));
ctx->config.kp_cache_size_client = default_kp_size;
ctx->config.kp_cache_size_server = default_kp_size;
ctx->config.client_key_rotate_time = default_rotate_time;
ctx->config.user_agent = default_user_agent;
ctx->config.keepalive_interval = default_keepalive_interval;
ctx->config.server_hdr = default_server_hdr;
ctx->ups_ctx = ups_ctx;
if (cfg) {
ctx->ssl_ctx = cfg->libs_ctx->ssl_ctx;
ctx->ssl_ctx_noverify = cfg->libs_ctx->ssl_ctx_noverify;
}
else {
ctx->ssl_ctx = rspamd_init_ssl_ctx();
ctx->ssl_ctx_noverify = rspamd_init_ssl_ctx_noverify();
}
ctx->event_loop = ev_base;
ctx->keep_alive_hash = kh_init(rspamd_keep_alive_hash);
return ctx;
}
static void
rspamd_http_context_parse_proxy(struct rspamd_http_context *ctx,
const gchar *name,
struct upstream_list **pls)
{
struct http_parser_url u;
struct upstream_list *uls;
if (!ctx->ups_ctx) {
msg_err("cannot parse http_proxy %s - upstreams context is undefined", name);
return;
}
memset(&u, 0, sizeof(u));
if (http_parser_parse_url(name, strlen(name), 1, &u) == 0) {
if (!(u.field_set & (1u << UF_HOST)) || u.port == 0) {
msg_err("cannot parse http(s) proxy %s - invalid host or port", name);
return;
}
uls = rspamd_upstreams_create(ctx->ups_ctx);
if (!rspamd_upstreams_parse_line_len(uls,
name + u.field_data[UF_HOST].off,
u.field_data[UF_HOST].len, u.port, NULL)) {
msg_err("cannot parse http(s) proxy %s - invalid data", name);
rspamd_upstreams_destroy(uls);
}
else {
*pls = uls;
msg_info("set http(s) proxy to %s", name);
}
}
else {
uls = rspamd_upstreams_create(ctx->ups_ctx);
if (!rspamd_upstreams_parse_line(uls,
name, 3128, NULL)) {
msg_err("cannot parse http(s) proxy %s - invalid data", name);
rspamd_upstreams_destroy(uls);
}
else {
*pls = uls;
msg_info("set http(s) proxy to %s", name);
}
}
}
static void
rspamd_http_context_init(struct rspamd_http_context *ctx)
{
if (ctx->config.kp_cache_size_client > 0) {
ctx->client_kp_cache = rspamd_keypair_cache_new(ctx->config.kp_cache_size_client);
}
if (ctx->config.kp_cache_size_server > 0) {
ctx->server_kp_cache = rspamd_keypair_cache_new(ctx->config.kp_cache_size_server);
}
if (ctx->config.client_key_rotate_time > 0 && ctx->event_loop) {
double jittered = rspamd_time_jitter(ctx->config.client_key_rotate_time,
0);
ev_timer_init(&ctx->client_rotate_ev,
rspamd_http_context_client_rotate_ev, jittered, 0);
ev_timer_start(ctx->event_loop, &ctx->client_rotate_ev);
ctx->client_rotate_ev.data = ctx;
}
if (ctx->config.http_proxy) {
rspamd_http_context_parse_proxy(ctx, ctx->config.http_proxy,
&ctx->http_proxies);
}
default_ctx = ctx;
}
struct rspamd_http_context *
rspamd_http_context_create(struct rspamd_config *cfg,
struct ev_loop *ev_base,
struct upstream_ctx *ups_ctx)
{
struct rspamd_http_context *ctx;
const ucl_object_t *http_obj;
ctx = rspamd_http_context_new_default(cfg, ev_base, ups_ctx);
http_obj = ucl_object_lookup(cfg->cfg_ucl_obj, "http");
if (http_obj) {
const ucl_object_t *server_obj, *client_obj;
client_obj = ucl_object_lookup(http_obj, "client");
if (client_obj) {
const ucl_object_t *kp_size;
kp_size = ucl_object_lookup(client_obj, "cache_size");
if (kp_size) {
ctx->config.kp_cache_size_client = ucl_object_toint(kp_size);
}
const ucl_object_t *rotate_time;
rotate_time = ucl_object_lookup(client_obj, "rotate_time");
if (rotate_time) {
ctx->config.client_key_rotate_time = ucl_object_todouble(rotate_time);
}
const ucl_object_t *user_agent;
user_agent = ucl_object_lookup(client_obj, "user_agent");
if (user_agent) {
ctx->config.user_agent = ucl_object_tostring(user_agent);
if (ctx->config.user_agent && strlen(ctx->config.user_agent) == 0) {
ctx->config.user_agent = NULL;
}
}
const ucl_object_t *server_hdr;
server_hdr = ucl_object_lookup(client_obj, "server_hdr");
if (server_hdr) {
ctx->config.server_hdr = ucl_object_tostring(server_hdr);
if (ctx->config.server_hdr && strlen(ctx->config.server_hdr) == 0) {
ctx->config.server_hdr = "";
}
}
const ucl_object_t *keepalive_interval;
keepalive_interval = ucl_object_lookup(client_obj, "keepalive_interval");
if (keepalive_interval) {
ctx->config.keepalive_interval = ucl_object_todouble(keepalive_interval);
}
const ucl_object_t *http_proxy;
http_proxy = ucl_object_lookup(client_obj, "http_proxy");
if (http_proxy) {
ctx->config.http_proxy = ucl_object_tostring(http_proxy);
}
}
server_obj = ucl_object_lookup(http_obj, "server");
if (server_obj) {
const ucl_object_t *kp_size;
kp_size = ucl_object_lookup(server_obj, "cache_size");
if (kp_size) {
ctx->config.kp_cache_size_server = ucl_object_toint(kp_size);
}
}
}
rspamd_http_context_init(ctx);
return ctx;
}
void rspamd_http_context_free(struct rspamd_http_context *ctx)
{
if (ctx == default_ctx) {
default_ctx = NULL;
}
if (ctx->client_kp_cache) {
rspamd_keypair_cache_destroy(ctx->client_kp_cache);
}
if (ctx->server_kp_cache) {
rspamd_keypair_cache_destroy(ctx->server_kp_cache);
}
if (ctx->config.client_key_rotate_time > 0) {
ev_timer_stop(ctx->event_loop, &ctx->client_rotate_ev);
if (ctx->client_kp) {
rspamd_keypair_unref(ctx->client_kp);
}
}
struct rspamd_keepalive_hash_key *hk;
kh_foreach_key(ctx->keep_alive_hash, hk, {
msg_debug_http_context("cleanup keepalive elt %s (%s)",
rspamd_inet_address_to_string_pretty(hk->addr),
hk->host);
if (hk->host) {
g_free(hk->host);
}
rspamd_inet_address_free(hk->addr);
rspamd_http_keepalive_queue_cleanup(&hk->conns);
g_free(hk);
});
kh_destroy(rspamd_keep_alive_hash, ctx->keep_alive_hash);
if (ctx->http_proxies) {
rspamd_upstreams_destroy(ctx->http_proxies);
}
g_free(ctx);
}
struct rspamd_http_context *
rspamd_http_context_create_config(struct rspamd_http_context_cfg *cfg,
struct ev_loop *ev_base,
struct upstream_ctx *ups_ctx)
{
struct rspamd_http_context *ctx;
ctx = rspamd_http_context_new_default(NULL, ev_base, ups_ctx);
memcpy(&ctx->config, cfg, sizeof(*cfg));
rspamd_http_context_init(ctx);
return ctx;
}
struct rspamd_http_context *
rspamd_http_context_default(void)
{
g_assert(default_ctx != NULL);
return default_ctx;
}
gint32
rspamd_keep_alive_key_hash(struct rspamd_keepalive_hash_key *k)
{
rspamd_cryptobox_fast_hash_state_t hst;
rspamd_cryptobox_fast_hash_init(&hst, 0);
if (k->host) {
rspamd_cryptobox_fast_hash_update(&hst, k->host, strlen(k->host));
}
rspamd_cryptobox_fast_hash_update(&hst, &k->port, sizeof(k->port));
rspamd_cryptobox_fast_hash_update(&hst, &k->is_ssl, sizeof(k->is_ssl));
return rspamd_cryptobox_fast_hash_final(&hst);
}
bool rspamd_keep_alive_key_equal(struct rspamd_keepalive_hash_key *k1,
struct rspamd_keepalive_hash_key *k2)
{
if (k1->is_ssl != k2->is_ssl) {
return false;
}
if (k1->host && k2->host) {
if (k1->port == k2->port) {
return strcmp(k1->host, k2->host) == 0;
}
}
else if (!k1->host && !k2->host) {
return (k1->port == k2->port);
}
/* One has host and another has no host */
return false;
}
struct rspamd_http_connection *
rspamd_http_context_check_keepalive(struct rspamd_http_context *ctx,
const rspamd_inet_addr_t *addr,
const gchar *host,
bool is_ssl)
{
struct rspamd_keepalive_hash_key hk, *phk;
khiter_t k;
if (ctx == NULL) {
ctx = rspamd_http_context_default();
}
hk.addr = (rspamd_inet_addr_t *) addr;
hk.host = (gchar *) host;
hk.port = rspamd_inet_address_get_port(addr);
hk.is_ssl = is_ssl;
k = kh_get(rspamd_keep_alive_hash, ctx->keep_alive_hash, &hk);
if (k != kh_end(ctx->keep_alive_hash)) {
phk = kh_key(ctx->keep_alive_hash, k);
GQueue *conns = &phk->conns;
/* Use stack based approach */
if (g_queue_get_length(conns) > 0) {
struct rspamd_http_keepalive_cbdata *cbd;
struct rspamd_http_connection *conn;
gint err;
socklen_t len = sizeof(gint);
cbd = g_queue_pop_head(conns);
rspamd_ev_watcher_stop(ctx->event_loop, &cbd->ev);
conn = cbd->conn;
g_free(cbd);
if (getsockopt(conn->fd, SOL_SOCKET, SO_ERROR, (void *) &err, &len) == -1) {
err = errno;
}
if (err != 0) {
rspamd_http_connection_unref(conn);
msg_debug_http_context("invalid reused keepalive element %s (%s, ssl=%d); "
"%s error; "
"%d connections queued",
rspamd_inet_address_to_string_pretty(phk->addr),
phk->host,
(int) phk->is_ssl,
g_strerror(err),
conns->length);
return NULL;
}
msg_debug_http_context("reused keepalive element %s (%s, ssl=%d), %d connections queued",
rspamd_inet_address_to_string_pretty(phk->addr),
phk->host,
(int) phk->is_ssl,
conns->length);
/* We transfer refcount here! */
return conn;
}
else {
msg_debug_http_context("found empty keepalive element %s (%s), cannot reuse",
rspamd_inet_address_to_string_pretty(phk->addr),
phk->host);
}
}
return NULL;
}
const rspamd_inet_addr_t *
rspamd_http_context_has_keepalive(struct rspamd_http_context *ctx,
const gchar *host,
unsigned port,
bool is_ssl)
{
struct rspamd_keepalive_hash_key hk, *phk;
khiter_t k;
if (ctx == NULL) {
ctx = rspamd_http_context_default();
}
hk.host = (gchar *) host;
hk.port = port;
hk.is_ssl = is_ssl;
k = kh_get(rspamd_keep_alive_hash, ctx->keep_alive_hash, &hk);
if (k != kh_end(ctx->keep_alive_hash)) {
phk = kh_key(ctx->keep_alive_hash, k);
GQueue *conns = &phk->conns;
if (g_queue_get_length(conns) > 0) {
return phk->addr;
}
}
return NULL;
}
void rspamd_http_context_prepare_keepalive(struct rspamd_http_context *ctx,
struct rspamd_http_connection *conn,
const rspamd_inet_addr_t *addr,
const gchar *host,
bool is_ssl)
{
struct rspamd_keepalive_hash_key hk, *phk;
khiter_t k;
hk.addr = (rspamd_inet_addr_t *) addr;
hk.host = (gchar *) host;
hk.is_ssl = is_ssl;
hk.port = rspamd_inet_address_get_port(addr);
k = kh_get(rspamd_keep_alive_hash, ctx->keep_alive_hash, &hk);
if (k != kh_end(ctx->keep_alive_hash)) {
/* Reuse existing */
conn->keepalive_hash_key = kh_key(ctx->keep_alive_hash, k);
msg_debug_http_context("use existing keepalive element %s (%s)",
rspamd_inet_address_to_string_pretty(conn->keepalive_hash_key->addr),
conn->keepalive_hash_key->host);
}
else {
/* Create new one */
GQueue empty_init = G_QUEUE_INIT;
gint r;
phk = g_malloc(sizeof(*phk));
phk->conns = empty_init;
phk->host = g_strdup(host);
phk->is_ssl = is_ssl;
phk->addr = rspamd_inet_address_copy(addr, NULL);
phk->port = hk.port;
kh_put(rspamd_keep_alive_hash, ctx->keep_alive_hash, phk, &r);
conn->keepalive_hash_key = phk;
msg_debug_http_context("create new keepalive element %s (%s)",
rspamd_inet_address_to_string_pretty(conn->keepalive_hash_key->addr),
conn->keepalive_hash_key->host);
}
}
static void
rspamd_http_keepalive_handler(gint fd, short what, gpointer ud)
{
struct rspamd_http_keepalive_cbdata *cbdata =
(struct rspamd_http_keepalive_cbdata *) ud; /*
* We can get here if a remote side reported something or it has
* timed out. In both cases we just terminate keepalive connection.
*/
g_queue_delete_link(cbdata->queue, cbdata->link);
msg_debug_http_context("remove keepalive element %s (%s), %d connections left",
rspamd_inet_address_to_string_pretty(cbdata->conn->keepalive_hash_key->addr),
cbdata->conn->keepalive_hash_key->host,
cbdata->queue->length);
/* unref call closes fd, so we need to remove ev watcher first! */
rspamd_ev_watcher_stop(cbdata->ctx->event_loop, &cbdata->ev);
rspamd_http_connection_unref(cbdata->conn);
g_free(cbdata);
}
/* Non-static for unit testing */
long rspamd_http_parse_keepalive_timeout(const rspamd_ftok_t *tok)
{
long timeout = -1;
goffset pos = rspamd_substring_search(tok->begin,
tok->len, "timeout", sizeof("timeout") - 1);
if (pos != -1) {
pos += sizeof("timeout") - 1;
/* Skip spaces and equal sign */
while (pos < tok->len) {
if (tok->begin[pos] != '=' && !g_ascii_isspace(tok->begin[pos])) {
break;
}
pos++;
}
gsize ndigits = rspamd_memspn(tok->begin + pos, "0123456789", tok->len - pos);
glong real_timeout;
if (ndigits > 0) {
if (rspamd_strtoul(tok->begin + pos, ndigits, &real_timeout)) {
timeout = real_timeout;
msg_debug_http_context("got timeout attr %l", timeout);
}
}
}
return timeout;
}
void rspamd_http_context_push_keepalive(struct rspamd_http_context *ctx,
struct rspamd_http_connection *conn,
struct rspamd_http_message *msg,
struct ev_loop *event_loop)
{
struct rspamd_http_keepalive_cbdata *cbdata;
gdouble timeout = ctx->config.keepalive_interval;
g_assert(conn->keepalive_hash_key != NULL);
if (msg) {
const rspamd_ftok_t *tok;
rspamd_ftok_t cmp;
tok = rspamd_http_message_find_header(msg, "Connection");
if (!tok) {
/* Server has not stated that it can do keep alive */
conn->finished = TRUE;
msg_debug_http_context("no Connection header");
return;
}
RSPAMD_FTOK_ASSIGN(&cmp, "keep-alive");
if (rspamd_ftok_casecmp(&cmp, tok) != 0) {
conn->finished = TRUE;
msg_debug_http_context("connection header is not `keep-alive`");
return;
}
/* We can proceed, check timeout */
tok = rspamd_http_message_find_header(msg, "Keep-Alive");
if (tok) {
long maybe_timeout = rspamd_http_parse_keepalive_timeout(tok);
if (maybe_timeout > 0) {
timeout = maybe_timeout;
}
}
}
/* Move connection to the keepalive pool */
cbdata = g_malloc0(sizeof(*cbdata));
cbdata->conn = rspamd_http_connection_ref(conn);
/* Use stack like approach to that would easy reading */
g_queue_push_head(&conn->keepalive_hash_key->conns, cbdata);
cbdata->link = conn->keepalive_hash_key->conns.head;
cbdata->queue = &conn->keepalive_hash_key->conns;
cbdata->ctx = ctx;
conn->finished = FALSE;
rspamd_ev_watcher_init(&cbdata->ev, conn->fd, EV_READ,
rspamd_http_keepalive_handler,
cbdata);
rspamd_ev_watcher_start(event_loop, &cbdata->ev, timeout);
msg_debug_http_context("push keepalive element %s (%s), %d connections queued, %.1f timeout",
rspamd_inet_address_to_string_pretty(cbdata->conn->keepalive_hash_key->addr),
cbdata->conn->keepalive_hash_key->host,
cbdata->queue->length,
timeout);
}
|