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
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
|
/*-
* 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_message.h"
#include "http_connection.h"
#include "http_private.h"
#include "libutil/printf.h"
#include "libserver/logger.h"
#include "utlist.h"
#include "unix-std.h"
struct rspamd_http_message *
rspamd_http_new_message(enum rspamd_http_message_type type)
{
struct rspamd_http_message *new;
new = g_malloc0(sizeof(struct rspamd_http_message));
if (type == HTTP_REQUEST) {
new->url = rspamd_fstring_new();
}
else {
new->url = NULL;
new->code = 200;
}
new->port = 80;
new->type = type;
new->method = HTTP_INVALID;
new->headers = kh_init(rspamd_http_headers_hash);
REF_INIT_RETAIN(new, rspamd_http_message_free);
return new;
}
struct rspamd_http_message *
rspamd_http_message_from_url(const gchar *url)
{
struct http_parser_url pu;
struct rspamd_http_message *msg;
const gchar *host, *path;
size_t pathlen, urllen;
guint flags = 0;
if (url == NULL) {
return NULL;
}
urllen = strlen(url);
memset(&pu, 0, sizeof(pu));
if (http_parser_parse_url(url, urllen, FALSE, &pu) != 0) {
msg_warn("cannot parse URL: %s", url);
return NULL;
}
if ((pu.field_set & (1 << UF_HOST)) == 0) {
msg_warn("no host argument in URL: %s", url);
return NULL;
}
if ((pu.field_set & (1 << UF_SCHEMA))) {
if (pu.field_data[UF_SCHEMA].len == sizeof("https") - 1 &&
memcmp(url + pu.field_data[UF_SCHEMA].off, "https", 5) == 0) {
flags |= RSPAMD_HTTP_FLAG_WANT_SSL;
}
}
if ((pu.field_set & (1 << UF_PATH)) == 0) {
path = "/";
pathlen = 1;
}
else {
path = url + pu.field_data[UF_PATH].off;
pathlen = urllen - pu.field_data[UF_PATH].off;
}
msg = rspamd_http_new_message(HTTP_REQUEST);
host = url + pu.field_data[UF_HOST].off;
msg->flags = flags;
if ((pu.field_set & (1 << UF_PORT)) != 0) {
msg->port = pu.port;
}
else {
/* XXX: magic constant */
if (flags & RSPAMD_HTTP_FLAG_WANT_SSL) {
msg->port = 443;
}
else {
msg->port = 80;
}
}
msg->host = g_string_new_len(host, pu.field_data[UF_HOST].len);
msg->url = rspamd_fstring_append(msg->url, path, pathlen);
REF_INIT_RETAIN(msg, rspamd_http_message_free);
return msg;
}
const gchar *
rspamd_http_message_get_body(struct rspamd_http_message *msg,
gsize *blen)
{
const gchar *ret = NULL;
if (msg->body_buf.len > 0) {
ret = msg->body_buf.begin;
}
if (blen) {
*blen = msg->body_buf.len;
}
return ret;
}
static void
rspamd_http_shname_dtor(void *p)
{
struct rspamd_storage_shmem *n = p;
#ifdef HAVE_SANE_SHMEM
shm_unlink(n->shm_name);
#else
unlink(n->shm_name);
#endif
g_free(n->shm_name);
g_free(n);
}
struct rspamd_storage_shmem *
rspamd_http_message_shmem_ref(struct rspamd_http_message *msg)
{
if ((msg->flags & RSPAMD_HTTP_FLAG_SHMEM) && msg->body_buf.c.shared.name) {
REF_RETAIN(msg->body_buf.c.shared.name);
return msg->body_buf.c.shared.name;
}
return NULL;
}
guint rspamd_http_message_get_flags(struct rspamd_http_message *msg)
{
return msg->flags;
}
void rspamd_http_message_shmem_unref(struct rspamd_storage_shmem *p)
{
REF_RELEASE(p);
}
gboolean
rspamd_http_message_set_body(struct rspamd_http_message *msg,
const gchar *data, gsize len)
{
union _rspamd_storage_u *storage;
storage = &msg->body_buf.c;
rspamd_http_message_storage_cleanup(msg);
if (msg->flags & RSPAMD_HTTP_FLAG_SHMEM) {
storage->shared.name = g_malloc(sizeof(*storage->shared.name));
REF_INIT_RETAIN(storage->shared.name, rspamd_http_shname_dtor);
#ifdef HAVE_SANE_SHMEM
#if defined(__DragonFly__)
// DragonFly uses regular files for shm. User rspamd is not allowed to create
// files in the root.
storage->shared.name->shm_name = g_strdup("/tmp/rhm.XXXXXXXXXXXXXXXXXXXX");
#else
storage->shared.name->shm_name = g_strdup("/rhm.XXXXXXXXXXXXXXXXXXXX");
#endif
storage->shared.shm_fd = rspamd_shmem_mkstemp(storage->shared.name->shm_name);
#else
/* XXX: assume that tempdir is /tmp */
storage->shared.name->shm_name = g_strdup("/tmp/rhm.XXXXXXXXXXXXXXXXXXXX");
storage->shared.shm_fd = mkstemp(storage->shared.name->shm_name);
#endif
if (storage->shared.shm_fd == -1) {
return FALSE;
}
if (len != 0 && len != G_MAXSIZE) {
if (ftruncate(storage->shared.shm_fd, len) == -1) {
return FALSE;
}
msg->body_buf.str = mmap(NULL, len,
PROT_WRITE | PROT_READ, MAP_SHARED,
storage->shared.shm_fd, 0);
if (msg->body_buf.str == MAP_FAILED) {
return FALSE;
}
msg->body_buf.begin = msg->body_buf.str;
msg->body_buf.allocated_len = len;
if (data != NULL) {
memcpy(msg->body_buf.str, data, len);
msg->body_buf.len = len;
}
}
else {
msg->body_buf.len = 0;
msg->body_buf.begin = NULL;
msg->body_buf.str = NULL;
msg->body_buf.allocated_len = 0;
}
}
else {
if (len != 0 && len != G_MAXSIZE) {
if (data == NULL) {
storage->normal = rspamd_fstring_sized_new(len);
msg->body_buf.len = 0;
}
else {
storage->normal = rspamd_fstring_new_init(data, len);
msg->body_buf.len = len;
}
}
else {
storage->normal = rspamd_fstring_new();
}
msg->body_buf.begin = storage->normal->str;
msg->body_buf.str = storage->normal->str;
msg->body_buf.allocated_len = storage->normal->allocated;
}
msg->flags |= RSPAMD_HTTP_FLAG_HAS_BODY;
return TRUE;
}
void rspamd_http_message_set_method(struct rspamd_http_message *msg,
const gchar *method)
{
gint i;
/* Linear search: not very efficient method */
for (i = 0; i < HTTP_METHOD_MAX; i++) {
if (g_ascii_strcasecmp(method, http_method_str(i)) == 0) {
msg->method = i;
}
}
}
gboolean
rspamd_http_message_set_body_from_fd(struct rspamd_http_message *msg,
gint fd)
{
union _rspamd_storage_u *storage;
struct stat st;
rspamd_http_message_storage_cleanup(msg);
storage = &msg->body_buf.c;
msg->flags |= RSPAMD_HTTP_FLAG_SHMEM | RSPAMD_HTTP_FLAG_SHMEM_IMMUTABLE;
storage->shared.shm_fd = dup(fd);
msg->body_buf.str = MAP_FAILED;
if (storage->shared.shm_fd == -1) {
return FALSE;
}
if (fstat(storage->shared.shm_fd, &st) == -1) {
return FALSE;
}
msg->body_buf.str = mmap(NULL, st.st_size,
PROT_READ, MAP_SHARED,
storage->shared.shm_fd, 0);
if (msg->body_buf.str == MAP_FAILED) {
return FALSE;
}
msg->body_buf.begin = msg->body_buf.str;
msg->body_buf.len = st.st_size;
msg->body_buf.allocated_len = st.st_size;
return TRUE;
}
gboolean
rspamd_http_message_set_body_from_fstring_steal(struct rspamd_http_message *msg,
rspamd_fstring_t *fstr)
{
union _rspamd_storage_u *storage;
rspamd_http_message_storage_cleanup(msg);
storage = &msg->body_buf.c;
msg->flags &= ~(RSPAMD_HTTP_FLAG_SHMEM | RSPAMD_HTTP_FLAG_SHMEM_IMMUTABLE);
storage->normal = fstr;
msg->body_buf.str = fstr->str;
msg->body_buf.begin = msg->body_buf.str;
msg->body_buf.len = fstr->len;
msg->body_buf.allocated_len = fstr->allocated;
return TRUE;
}
gboolean
rspamd_http_message_set_body_from_fstring_copy(struct rspamd_http_message *msg,
const rspamd_fstring_t *fstr)
{
union _rspamd_storage_u *storage;
rspamd_http_message_storage_cleanup(msg);
storage = &msg->body_buf.c;
msg->flags &= ~(RSPAMD_HTTP_FLAG_SHMEM | RSPAMD_HTTP_FLAG_SHMEM_IMMUTABLE);
storage->normal = rspamd_fstring_new_init(fstr->str, fstr->len);
msg->body_buf.str = storage->normal->str;
msg->body_buf.begin = msg->body_buf.str;
msg->body_buf.len = storage->normal->len;
msg->body_buf.allocated_len = storage->normal->allocated;
return TRUE;
}
gboolean
rspamd_http_message_grow_body(struct rspamd_http_message *msg, gsize len)
{
struct stat st;
union _rspamd_storage_u *storage;
gsize newlen;
storage = &msg->body_buf.c;
if (msg->flags & RSPAMD_HTTP_FLAG_SHMEM) {
if (storage->shared.shm_fd == -1) {
return FALSE;
}
if (fstat(storage->shared.shm_fd, &st) == -1) {
return FALSE;
}
/* Check if we need to grow */
if ((gsize) st.st_size < msg->body_buf.len + len) {
/* Need to grow */
newlen = rspamd_fstring_suggest_size(msg->body_buf.len, st.st_size,
len);
/* Unmap as we need another size of segment */
if (msg->body_buf.str != MAP_FAILED) {
munmap(msg->body_buf.str, st.st_size);
}
if (ftruncate(storage->shared.shm_fd, newlen) == -1) {
return FALSE;
}
msg->body_buf.str = mmap(NULL, newlen,
PROT_WRITE | PROT_READ, MAP_SHARED,
storage->shared.shm_fd, 0);
if (msg->body_buf.str == MAP_FAILED) {
return FALSE;
}
msg->body_buf.begin = msg->body_buf.str;
msg->body_buf.allocated_len = newlen;
}
}
else {
storage->normal = rspamd_fstring_grow(storage->normal, len);
/* Append might cause realloc */
msg->body_buf.begin = storage->normal->str;
msg->body_buf.len = storage->normal->len;
msg->body_buf.str = storage->normal->str;
msg->body_buf.allocated_len = storage->normal->allocated;
}
return TRUE;
}
gboolean
rspamd_http_message_append_body(struct rspamd_http_message *msg,
const gchar *data, gsize len)
{
union _rspamd_storage_u *storage;
storage = &msg->body_buf.c;
if (msg->flags & RSPAMD_HTTP_FLAG_SHMEM) {
if (!rspamd_http_message_grow_body(msg, len)) {
return FALSE;
}
memcpy(msg->body_buf.str + msg->body_buf.len, data, len);
msg->body_buf.len += len;
}
else {
storage->normal = rspamd_fstring_append(storage->normal, data, len);
/* Append might cause realloc */
msg->body_buf.begin = storage->normal->str;
msg->body_buf.len = storage->normal->len;
msg->body_buf.str = storage->normal->str;
msg->body_buf.allocated_len = storage->normal->allocated;
}
return TRUE;
}
void rspamd_http_message_storage_cleanup(struct rspamd_http_message *msg)
{
union _rspamd_storage_u *storage;
struct stat st;
if (msg->flags & RSPAMD_HTTP_FLAG_SHMEM) {
storage = &msg->body_buf.c;
if (storage->shared.shm_fd > 0) {
g_assert(fstat(storage->shared.shm_fd, &st) != -1);
if (msg->body_buf.str != MAP_FAILED) {
munmap(msg->body_buf.str, st.st_size);
}
close(storage->shared.shm_fd);
}
if (storage->shared.name != NULL) {
REF_RELEASE(storage->shared.name);
}
storage->shared.shm_fd = -1;
msg->body_buf.str = MAP_FAILED;
}
else {
if (msg->body_buf.c.normal) {
rspamd_fstring_free(msg->body_buf.c.normal);
}
msg->body_buf.c.normal = NULL;
}
msg->body_buf.len = 0;
}
void rspamd_http_message_free(struct rspamd_http_message *msg)
{
struct rspamd_http_header *hdr, *hcur, *hcurtmp;
kh_foreach_value (msg->headers, hdr, {
DL_FOREACH_SAFE (hdr, hcur, hcurtmp) {
rspamd_fstring_free (hcur->combined);
g_free (hcur);
}
});
kh_destroy(rspamd_http_headers_hash, msg->headers);
rspamd_http_message_storage_cleanup(msg);
if (msg->url != NULL) {
rspamd_fstring_free(msg->url);
}
if (msg->status != NULL) {
rspamd_fstring_free(msg->status);
}
if (msg->host != NULL) {
g_string_free(msg->host, TRUE);
}
if (msg->peer_key != NULL) {
rspamd_pubkey_unref(msg->peer_key);
}
g_free(msg);
}
void rspamd_http_message_set_peer_key(struct rspamd_http_message *msg,
struct rspamd_cryptobox_pubkey *pk)
{
if (msg->peer_key != NULL) {
rspamd_pubkey_unref(msg->peer_key);
}
if (pk) {
msg->peer_key = rspamd_pubkey_ref(pk);
}
else {
msg->peer_key = NULL;
}
}
void rspamd_http_message_add_header_len(struct rspamd_http_message *msg,
const gchar *name,
const gchar *value,
gsize len)
{
struct rspamd_http_header *hdr, *found;
guint nlen, vlen;
khiter_t k;
gint r;
if (msg != NULL && name != NULL && value != NULL) {
hdr = g_malloc0(sizeof(struct rspamd_http_header));
nlen = strlen(name);
vlen = len;
if (g_ascii_strcasecmp(name, "host") == 0) {
msg->flags |= RSPAMD_HTTP_FLAG_HAS_HOST_HEADER;
}
hdr->combined = rspamd_fstring_sized_new(nlen + vlen + 4);
rspamd_printf_fstring(&hdr->combined, "%s: %*s\r\n", name, (gint) vlen,
value);
hdr->name.begin = hdr->combined->str;
hdr->name.len = nlen;
hdr->value.begin = hdr->combined->str + nlen + 2;
hdr->value.len = vlen;
k = kh_put(rspamd_http_headers_hash, msg->headers, &hdr->name,
&r);
if (r != 0) {
kh_value(msg->headers, k) = hdr;
found = NULL;
}
else {
found = kh_value(msg->headers, k);
}
DL_APPEND(found, hdr);
}
}
void rspamd_http_message_add_header(struct rspamd_http_message *msg,
const gchar *name,
const gchar *value)
{
if (value) {
rspamd_http_message_add_header_len(msg, name, value, strlen(value));
}
}
void rspamd_http_message_add_header_fstr(struct rspamd_http_message *msg,
const gchar *name,
rspamd_fstring_t *value)
{
struct rspamd_http_header *hdr, *found = NULL;
guint nlen, vlen;
khiter_t k;
gint r;
if (msg != NULL && name != NULL && value != NULL) {
hdr = g_malloc0(sizeof(struct rspamd_http_header));
nlen = strlen(name);
vlen = value->len;
hdr->combined = rspamd_fstring_sized_new(nlen + vlen + 4);
rspamd_printf_fstring(&hdr->combined, "%s: %V\r\n", name, value);
hdr->name.begin = hdr->combined->str;
hdr->name.len = nlen;
hdr->value.begin = hdr->combined->str + nlen + 2;
hdr->value.len = vlen;
k = kh_put(rspamd_http_headers_hash, msg->headers, &hdr->name,
&r);
if (r != 0) {
kh_value(msg->headers, k) = hdr;
found = NULL;
}
else {
found = kh_value(msg->headers, k);
}
DL_APPEND(found, hdr);
}
}
const rspamd_ftok_t *
rspamd_http_message_find_header(struct rspamd_http_message *msg,
const gchar *name)
{
const rspamd_ftok_t *res = NULL;
rspamd_ftok_t srch;
guint slen = strlen(name);
khiter_t k;
if (msg != NULL) {
srch.begin = name;
srch.len = slen;
k = kh_get(rspamd_http_headers_hash, msg->headers, &srch);
if (k != kh_end(msg->headers)) {
res = &(kh_value(msg->headers, k)->value);
}
}
return res;
}
GPtrArray *
rspamd_http_message_find_header_multiple(
struct rspamd_http_message *msg,
const gchar *name)
{
GPtrArray *res = NULL;
struct rspamd_http_header *hdr, *cur;
rspamd_ftok_t srch;
khiter_t k;
guint cnt = 0;
guint slen = strlen(name);
if (msg != NULL) {
srch.begin = name;
srch.len = slen;
k = kh_get(rspamd_http_headers_hash, msg->headers, &srch);
if (k != kh_end(msg->headers)) {
hdr = kh_value(msg->headers, k);
LL_COUNT(hdr, cur, cnt);
res = g_ptr_array_sized_new(cnt);
LL_FOREACH(hdr, cur)
{
g_ptr_array_add(res, &cur->value);
}
}
}
return res;
}
gboolean
rspamd_http_message_remove_header(struct rspamd_http_message *msg,
const gchar *name)
{
struct rspamd_http_header *hdr, *hcur, *hcurtmp;
gboolean res = FALSE;
guint slen = strlen(name);
rspamd_ftok_t srch;
khiter_t k;
if (msg != NULL) {
srch.begin = name;
srch.len = slen;
k = kh_get(rspamd_http_headers_hash, msg->headers, &srch);
if (k != kh_end(msg->headers)) {
hdr = kh_value(msg->headers, k);
kh_del(rspamd_http_headers_hash, msg->headers, k);
res = TRUE;
DL_FOREACH_SAFE(hdr, hcur, hcurtmp)
{
rspamd_fstring_free(hcur->combined);
g_free(hcur);
}
}
}
return res;
}
const gchar *
rspamd_http_message_get_http_host(struct rspamd_http_message *msg,
gsize *hostlen)
{
if (msg->flags & RSPAMD_HTTP_FLAG_HAS_HOST_HEADER) {
rspamd_ftok_t srch;
RSPAMD_FTOK_ASSIGN(&srch, "Host");
khiter_t k = kh_get(rspamd_http_headers_hash, msg->headers, &srch);
if (k != kh_end(msg->headers)) {
*hostlen = (kh_value(msg->headers, k)->value).len;
return (kh_value(msg->headers, k)->value).begin;
}
else if (msg->host) {
*hostlen = msg->host->len;
return msg->host->str;
}
}
else {
if (msg->host) {
*hostlen = msg->host->len;
return msg->host->str;
}
}
return NULL;
}
bool rspamd_http_message_is_standard_port(struct rspamd_http_message *msg)
{
if (msg->flags & RSPAMD_HTTP_FLAG_WANT_SSL) {
return msg->port == 443;
}
return msg->port == 80;
}
|