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
|
/*-
* Copyright 2016 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 "rspamdclient.h"
#include "libutil/util.h"
#include "libutil/http_connection.h"
#include "libutil/http_private.h"
#include "unix-std.h"
#include "contrib/zstd/zstd.h"
#include "contrib/zstd/zdict.h"
#ifdef HAVE_FETCH_H
#include <fetch.h>
#elif defined(CURL_FOUND)
#include <curl/curl.h>
#endif
struct rspamd_client_request;
/*
* Since rspamd uses untagged HTTP we can pass a single message per socket
*/
struct rspamd_client_connection {
gint fd;
GString *server_name;
struct rspamd_cryptobox_pubkey *key;
struct rspamd_cryptobox_keypair *keypair;
struct ev_loop *ev_base;
struct timeval timeout;
struct rspamd_http_connection *http_conn;
gboolean req_sent;
gdouble start_time;
gdouble send_time;
struct rspamd_client_request *req;
struct rspamd_keypair_cache *keys_cache;
};
struct rspamd_client_request {
struct rspamd_client_connection *conn;
struct rspamd_http_message *msg;
GString *input;
rspamd_client_callback cb;
gpointer ud;
};
#define RCLIENT_ERROR rspamd_client_error_quark ()
GQuark
rspamd_client_error_quark (void)
{
return g_quark_from_static_string ("rspamd-client-error");
}
static void
rspamd_client_request_free (struct rspamd_client_request *req)
{
if (req != NULL) {
if (req->conn) {
req->conn->req = NULL;
}
if (req->input) {
g_string_free (req->input, TRUE);
}
g_free (req);
}
}
static gint
rspamd_client_body_handler (struct rspamd_http_connection *conn,
struct rspamd_http_message *msg,
const gchar *chunk, gsize len)
{
/* Do nothing here */
return 0;
}
static void
rspamd_client_error_handler (struct rspamd_http_connection *conn, GError *err)
{
struct rspamd_client_request *req =
(struct rspamd_client_request *)conn->ud;
struct rspamd_client_connection *c;
c = req->conn;
req->cb (c, NULL, c->server_name->str, NULL,
req->input, req->ud,
c->start_time, c->send_time, err);
}
static gint
rspamd_client_finish_handler (struct rspamd_http_connection *conn,
struct rspamd_http_message *msg)
{
struct rspamd_client_request *req =
(struct rspamd_client_request *)conn->ud;
struct rspamd_client_connection *c;
struct ucl_parser *parser;
GError *err;
const rspamd_ftok_t *tok;
c = req->conn;
if (!c->req_sent) {
c->req_sent = TRUE;
c->send_time = rspamd_get_ticks (FALSE);
rspamd_http_connection_reset (c->http_conn);
rspamd_http_connection_read_message (c->http_conn,
c->req,
&c->timeout);
return 0;
}
else {
if (rspamd_http_message_get_body (msg, NULL) == NULL || msg->code / 100 != 2) {
err = g_error_new (RCLIENT_ERROR, msg->code, "HTTP error: %d, %.*s",
msg->code,
(gint)msg->status->len, msg->status->str);
req->cb (c, msg, c->server_name->str, NULL, req->input, req->ud,
c->start_time, c->send_time, err);
g_error_free (err);
return 0;
}
tok = rspamd_http_message_find_header (msg, "compression");
if (tok) {
/* Need to uncompress */
rspamd_ftok_t t;
t.begin = "zstd";
t.len = 4;
if (rspamd_ftok_casecmp (tok, &t) == 0) {
ZSTD_DStream *zstream;
ZSTD_inBuffer zin;
ZSTD_outBuffer zout;
guchar *out;
gsize outlen, r;
zstream = ZSTD_createDStream ();
ZSTD_initDStream (zstream);
zin.pos = 0;
zin.src = msg->body_buf.begin;
zin.size = msg->body_buf.len;
if ((outlen = ZSTD_getDecompressedSize (zin.src, zin.size)) == 0) {
outlen = ZSTD_DStreamOutSize ();
}
out = g_malloc (outlen);
zout.dst = out;
zout.pos = 0;
zout.size = outlen;
while (zin.pos < zin.size) {
r = ZSTD_decompressStream (zstream, &zout, &zin);
if (ZSTD_isError (r)) {
err = g_error_new (RCLIENT_ERROR, 500,
"Decompression error: %s",
ZSTD_getErrorName (r));
req->cb (c, msg, c->server_name->str, NULL,
req->input, req->ud, c->start_time,
c->send_time, err);
g_error_free (err);
ZSTD_freeDStream (zstream);
g_free (out);
return 0;
}
if (zout.pos == zout.size) {
/* We need to extend output buffer */
zout.size = zout.size * 1.5 + 1.0;
zout.dst = g_realloc (zout.dst, zout.size);
}
}
ZSTD_freeDStream (zstream);
parser = ucl_parser_new (0);
if (!ucl_parser_add_chunk (parser, zout.dst, zout.pos)) {
err = g_error_new (RCLIENT_ERROR, msg->code, "Cannot parse UCL: %s",
ucl_parser_get_error (parser));
ucl_parser_free (parser);
req->cb (c, msg, c->server_name->str, NULL, req->input,
req->ud, c->start_time, c->send_time, err);
g_error_free (err);
g_free (zout.dst);
return 0;
}
g_free (zout.dst);
}
else {
err = g_error_new (RCLIENT_ERROR, 500,
"Invalid compression method");
req->cb (c, msg, c->server_name->str, NULL,
req->input, req->ud, c->start_time, c->send_time, err);
g_error_free (err);
return 0;
}
}
else {
parser = ucl_parser_new (0);
if (!ucl_parser_add_chunk (parser, msg->body_buf.begin, msg->body_buf.len)) {
err = g_error_new (RCLIENT_ERROR, msg->code, "Cannot parse UCL: %s",
ucl_parser_get_error (parser));
ucl_parser_free (parser);
req->cb (c, msg, c->server_name->str, NULL,
req->input, req->ud, c->start_time, c->send_time, err);
g_error_free (err);
return 0;
}
}
req->cb (c, msg, c->server_name->str, ucl_parser_get_object (
parser), req->input, req->ud, c->start_time, c->send_time, NULL);
ucl_parser_free (parser);
}
return 0;
}
struct rspamd_client_connection *
rspamd_client_init (struct rspamd_http_context *http_ctx,
struct ev_loop *ev_base, const gchar *name,
guint16 port, gdouble timeout, const gchar *key)
{
struct rspamd_client_connection *conn;
gint fd;
fd = rspamd_socket (name, port, SOCK_STREAM, TRUE, FALSE, TRUE);
if (fd == -1) {
return NULL;
}
conn = g_malloc0 (sizeof (struct rspamd_client_connection));
conn->ev_base = ev_base;
conn->fd = fd;
conn->req_sent = FALSE;
conn->http_conn = rspamd_http_connection_new_client_socket (http_ctx,
rspamd_client_body_handler,
rspamd_client_error_handler,
rspamd_client_finish_handler,
0,
fd);
conn->server_name = g_string_new (name);
if (port != 0) {
rspamd_printf_gstring (conn->server_name, ":%d", (int)port);
}
double_to_tv (timeout, &conn->timeout);
if (key) {
conn->key = rspamd_pubkey_from_base32 (key, 0, RSPAMD_KEYPAIR_KEX,
RSPAMD_CRYPTOBOX_MODE_25519);
if (conn->key) {
conn->keypair = rspamd_keypair_new (RSPAMD_KEYPAIR_KEX,
RSPAMD_CRYPTOBOX_MODE_25519);
rspamd_http_connection_set_key (conn->http_conn, conn->keypair);
}
else {
rspamd_client_destroy (conn);
return NULL;
}
}
return conn;
}
gboolean
rspamd_client_command (struct rspamd_client_connection *conn,
const gchar *command, GQueue *attrs,
FILE *in, rspamd_client_callback cb,
gpointer ud, gboolean compressed,
const gchar *comp_dictionary,
const gchar *filename,
GError **err)
{
struct rspamd_client_request *req;
struct rspamd_http_client_header *nh;
gchar *p;
gsize remain, old_len;
GList *cur;
GString *input = NULL;
rspamd_fstring_t *body;
guint dict_id = 0;
gsize dict_len = 0;
void *dict = NULL;
ZSTD_CCtx *zctx;
req = g_malloc0 (sizeof (struct rspamd_client_request));
req->conn = conn;
req->cb = cb;
req->ud = ud;
req->msg = rspamd_http_new_message (HTTP_REQUEST);
if (conn->key) {
req->msg->peer_key = rspamd_pubkey_ref (conn->key);
}
if (in != NULL) {
/* Read input stream */
input = g_string_sized_new (BUFSIZ);
while (!feof (in)) {
p = input->str + input->len;
remain = input->allocated_len - input->len - 1;
if (remain == 0) {
old_len = input->len;
g_string_set_size (input, old_len * 2);
input->len = old_len;
continue;
}
remain = fread (p, 1, remain, in);
if (remain > 0) {
input->len += remain;
input->str[input->len] = '\0';
}
}
if (ferror (in) != 0) {
g_set_error (err, RCLIENT_ERROR, ferror (
in), "input IO error: %s", strerror (ferror (in)));
g_free (req);
g_string_free (input, TRUE);
return FALSE;
}
if (!compressed) {
body = rspamd_fstring_new_init (input->str, input->len);
}
else {
if (comp_dictionary) {
dict = rspamd_file_xmap (comp_dictionary, PROT_READ, &dict_len,
TRUE);
if (dict == NULL) {
g_set_error (err, RCLIENT_ERROR, errno,
"cannot open dictionary %s: %s",
comp_dictionary,
strerror (errno));
g_free (req);
g_string_free (input, TRUE);
return FALSE;
}
dict_id = ZDICT_getDictID (comp_dictionary, dict_len);
if (dict_id == 0) {
g_set_error (err, RCLIENT_ERROR, errno,
"cannot open dictionary %s: %s",
comp_dictionary,
strerror (errno));
g_free (req);
g_string_free (input, TRUE);
munmap (dict, dict_len);
return FALSE;
}
}
body = rspamd_fstring_sized_new (ZSTD_compressBound (input->len));
zctx = ZSTD_createCCtx ();
body->len = ZSTD_compress_usingDict (zctx, body->str, body->allocated,
input->str, input->len,
dict, dict_len,
1);
munmap (dict, dict_len);
if (ZSTD_isError (body->len)) {
g_set_error (err, RCLIENT_ERROR, ferror (
in), "compression error");
g_free (req);
g_string_free (input, TRUE);
rspamd_fstring_free (body);
ZSTD_freeCCtx (zctx);
return FALSE;
}
ZSTD_freeCCtx (zctx);
}
rspamd_http_message_set_body_from_fstring_steal (req->msg, body);
req->input = input;
}
else {
req->input = NULL;
}
/* Convert headers */
cur = attrs->head;
while (cur != NULL) {
nh = cur->data;
rspamd_http_message_add_header (req->msg, nh->name, nh->value);
cur = g_list_next (cur);
}
if (compressed) {
rspamd_http_message_add_header (req->msg, "Compression", "zstd");
if (dict_id != 0) {
gchar dict_str[32];
rspamd_snprintf (dict_str, sizeof (dict_str), "%ud", dict_id);
rspamd_http_message_add_header (req->msg, "Dictionary", dict_str);
}
}
if (filename) {
rspamd_http_message_add_header (req->msg, "Filename", filename);
}
req->msg->url = rspamd_fstring_append (req->msg->url, "/", 1);
req->msg->url = rspamd_fstring_append (req->msg->url, command, strlen (command));
conn->req = req;
conn->start_time = rspamd_get_ticks (FALSE);
if (compressed) {
rspamd_http_connection_write_message (conn->http_conn, req->msg, NULL,
"application/x-compressed", req,
&conn->timeout);
}
else {
rspamd_http_connection_write_message (conn->http_conn, req->msg, NULL,
"text/plain", req, &conn->timeout);
}
return TRUE;
}
void
rspamd_client_destroy (struct rspamd_client_connection *conn)
{
if (conn != NULL) {
rspamd_http_connection_unref (conn->http_conn);
if (conn->req != NULL) {
rspamd_client_request_free (conn->req);
}
close (conn->fd);
if (conn->key) {
rspamd_pubkey_unref (conn->key);
}
if (conn->keypair) {
rspamd_keypair_unref (conn->keypair);
}
g_string_free (conn->server_name, TRUE);
g_free (conn);
}
}
|