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
|
/*-
* Copyright 2019 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 "rspamd.h"
static struct rspamd_http_context *default_ctx = NULL;
struct rspamd_http_keepalive_cbdata {
struct rspamd_http_connection *conn;
GQueue *queue;
GList *link;
struct event 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;
rspamd_http_connection_unref (cbd->conn);
event_del (&cbd->ev);
g_free (cbd);
cur = cur->next;
}
g_queue_clear (conns);
}
static void
rspamd_http_context_client_rotate_ev (gint fd, short what, void *arg)
{
struct timeval rot_tv;
struct rspamd_http_context *ctx = arg;
gpointer kp;
double_to_tv (ctx->config.client_key_rotate_time, &rot_tv);
rot_tv.tv_sec += ottery_rand_range (rot_tv.tv_sec);
event_del (&ctx->client_rotate_ev);
event_add (&ctx->client_rotate_ev, &rot_tv);
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 event_base *ev_base)
{
struct rspamd_http_context *ctx;
static const int default_kp_size = 1024;
static const gdouble default_rotate_time = 120;
static const gchar *default_user_agent = "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;
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->ev_base = ev_base;
ctx->keep_alive_hash = kh_init (rspamd_keep_alive_hash);
return ctx;
}
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_client > 0) {
ctx->client_kp_cache = rspamd_keypair_cache_new (ctx->config.kp_cache_size_client);
}
if (ctx->config.client_key_rotate_time > 0 && ctx->ev_base) {
struct timeval tv;
double jittered = rspamd_time_jitter (ctx->config.client_key_rotate_time,
0);
double_to_tv (jittered, &tv);
event_set (&ctx->client_rotate_ev, -1, EV_TIMEOUT,
rspamd_http_context_client_rotate_ev, ctx);
event_base_set (ctx->ev_base, &ctx->client_rotate_ev);
event_add (&ctx->client_rotate_ev, &tv);
}
default_ctx = ctx;
}
struct rspamd_http_context*
rspamd_http_context_create (struct rspamd_config *cfg,
struct event_base *ev_base)
{
struct rspamd_http_context *ctx;
const ucl_object_t *http_obj;
ctx = rspamd_http_context_new_default (cfg, ev_base);
http_obj = ucl_object_lookup (cfg->rcl_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;
}
}
}
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) {
/* Event is removed on base event loop termination */
/* event_del (&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, {
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);
g_free (ctx);
}
struct rspamd_http_context*
rspamd_http_context_create_config (struct rspamd_http_context_cfg *cfg,
struct event_base *ev_base)
{
struct rspamd_http_context *ctx;
ctx = rspamd_http_context_new_default (NULL, ev_base);
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)
{
gint32 h;
h = rspamd_inet_address_port_hash (k->addr);
if (k->host) {
h = rspamd_cryptobox_fast_hash (k->host, strlen (k->host), h);
}
return h;
}
bool
rspamd_keep_alive_key_equal (struct rspamd_keepalive_hash_key *k1,
struct rspamd_keepalive_hash_key *k2)
{
if (k1->host && k2->host) {
if (rspamd_inet_address_port_equal (k1->addr, k2->addr)) {
return strcmp (k1->host, k2->host);
}
}
else if (!k1->host && !k2->host) {
return rspamd_inet_address_port_equal (k1->addr, k2->addr);
}
/* 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)
{
struct rspamd_keepalive_hash_key hk, *phk;
khiter_t k;
hk.addr = (rspamd_inet_addr_t *)addr;
hk.host = (gchar *)host;
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;
cbd = g_queue_pop_head (conns);
event_del (&cbd->ev);
conn = cbd->conn;
g_free (cbd);
/* We transfer refcount here! */
return conn;
}
}
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)
{
struct rspamd_keepalive_hash_key hk, *phk;
khiter_t k;
hk.addr = (rspamd_inet_addr_t *)addr;
hk.host = (gchar *)host;
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);
}
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->addr = rspamd_inet_address_copy (addr);
kh_put (rspamd_keep_alive_hash, ctx->keep_alive_hash, phk, &r);
conn->keepalive_hash_key = phk;
}
}
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);
rspamd_http_connection_unref (cbdata->conn);
g_free (cbdata);
}
void
rspamd_http_context_push_keepalive (struct rspamd_http_context *ctx,
struct rspamd_http_connection *conn,
struct rspamd_http_message *msg,
struct event_base *ev_base)
{
struct rspamd_http_keepalive_cbdata *cbdata;
struct timeval tv;
gdouble timeout = ctx->config.keepalive_interval;
g_assert (conn->keepalive_hash_key != NULL);
/* Move connection to the keepalive pool */
cbdata = g_malloc0 (sizeof (*cbdata));
cbdata->conn = rspamd_http_connection_ref (conn);
g_queue_push_tail (&conn->keepalive_hash_key->conns, cbdata);
cbdata->link = conn->keepalive_hash_key->conns.tail;
cbdata->queue = &conn->keepalive_hash_key->conns;
conn->finished = FALSE;
event_set (&cbdata->ev, conn->fd, EV_READ|EV_TIMEOUT,
rspamd_http_keepalive_handler,
&cbdata);
if (msg) {
const rspamd_ftok_t *tok;
tok = rspamd_http_message_find_header (msg, "Keep-Alive");
if (tok) {
goffset pos = rspamd_substring_search_caseless (tok->begin,
tok->len, "timeout=", sizeof ("timeout=") - 1);
if (pos != -1) {
pos += sizeof ("timeout=");
gchar *end_pos = memchr (tok->begin + pos, ',', tok->len - pos);
glong real_timeout;
if (end_pos) {
if (rspamd_strtol (tok->begin + pos + 1,
(end_pos - tok->begin) - pos - 1, &real_timeout) &&
real_timeout > 0) {
timeout = real_timeout;
}
}
else {
if (rspamd_strtol (tok->begin + pos + 1,
tok->len - pos - 1, &real_timeout) &&
real_timeout > 0) {
timeout = real_timeout;
}
}
}
}
}
double_to_tv (timeout, &tv);
event_base_set (ev_base, &cbdata->ev);
event_add (&cbdata->ev, &tv);
}
|