/
surbl.c
2286 lines (1994 loc) · 56.2 KB
/
surbl.c
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
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*-
* 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.
*/
/***MODULE:surbl
* rspamd module that implements SURBL url checking
*
* Allowed options:
* - weight (integer): weight of symbol
* Redirecotor options:
* - redirector (string): address of http redirector utility in format "host:port"
* - redirector_connect_timeout (seconds): redirector connect timeout (default: 1s)
* - redirector_read_timeout (seconds): timeout for reading data (default: 5s)
* - redirector_hosts_map (map string): map that contains domains to check with redirector
* Surbl options:
* - exceptions (map string): map of domains that should be checked via surbl using 3 (e.g. somehost.domain.com)
* components of domain name instead of normal 2 (e.g. domain.com)
* - whitelist (map string): map of domains that should be whitelisted for surbl checks
* - max_urls (integer): maximum allowed number of urls in message to be checked
* - suffix (string): surbl address (for example insecure-bl.rambler.ru), may contain %b if bits are used (read documentation about it)
* - bit (string): describes a prefix for a single bit
*/
#include "config.h"
#include "libmime/message.h"
#include "libutil/map.h"
#include "libutil/map_helpers.h"
#include "rspamd.h"
#include "utlist.h"
#include "multipattern.h"
#include "monitored.h"
#include "libserver/html.h"
#include "libutil/http_private.h"
#include "unix-std.h"
#include "lua/lua_common.h"
#define msg_err_surbl(...) rspamd_default_log_function (G_LOG_LEVEL_CRITICAL, \
"surbl", task->task_pool->tag.uid, \
G_STRFUNC, \
__VA_ARGS__)
#define msg_warn_surbl(...) rspamd_default_log_function (G_LOG_LEVEL_WARNING, \
"surbl", task->task_pool->tag.uid, \
G_STRFUNC, \
__VA_ARGS__)
#define msg_info_surbl(...) rspamd_default_log_function (G_LOG_LEVEL_INFO, \
"surbl", task->task_pool->tag.uid, \
G_STRFUNC, \
__VA_ARGS__)
#define msg_debug_surbl(...) rspamd_conditional_debug_fast (NULL, task->from_addr, \
rspamd_surbl_log_id, "surbl", task->task_pool->tag.uid, \
G_STRFUNC, \
__VA_ARGS__)
INIT_LOG_MODULE(surbl)
static const gchar *M = "surbl";
#define DEFAULT_SURBL_WEIGHT 10
#define DEFAULT_REDIRECTOR_READ_TIMEOUT 5.0
#define DEFAULT_SURBL_SYMBOL "SURBL_DNS"
#define SURBL_OPTION_NOIP (1u << 0u)
#define SURBL_OPTION_RESOLVEIP (1u << 1u)
#define SURBL_OPTION_CHECKIMAGES (1u << 2u)
#define SURBL_OPTION_CHECKDKIM (1u << 3u)
#define SURBL_OPTION_FULLDOMAIN (1u << 4u)
#define SURBL_OPTION_CHECKEMAILS (1u << 5u)
#define MAX_LEVELS 10
struct surbl_ctx {
struct module_ctx ctx;
guint16 weight;
gdouble read_timeout;
gboolean use_tags;
GList *suffixes;
const gchar *redirector_symbol;
GHashTable **exceptions;
struct rspamd_hash_map_helper *whitelist;
GHashTable *redirector_tlds;
guint use_redirector;
guint max_redirected_urls;
gint redirector_cbid;
struct upstream_list *redirectors;
};
struct suffix_item {
guint64 magic;
const gchar *monitored_domain;
const gchar *suffix;
const gchar *symbol;
GArray *bits;
GHashTable *ips;
struct rspamd_monitored *m;
guint32 options;
gboolean reported_offline;
gint callback_id;
gint url_process_cbref;
};
struct dns_param {
struct rspamd_url *url;
struct rspamd_task *task;
gchar *host_resolve;
gchar *host_orig; /* Name with no uribl suffix */
struct suffix_item *suffix;
struct rspamd_symcache_item *item;
struct surbl_module_ctx *ctx;
};
struct redirector_param {
struct rspamd_url *url;
struct rspamd_task *task;
struct upstream *redirector;
struct surbl_ctx *ctx;
struct rspamd_http_connection *conn;
GHashTable *tree;
struct suffix_item *suffix;
struct rspamd_symcache_item *item;
guint redirector_requests;
};
struct surbl_bit_item {
guint32 bit;
gchar *symbol;
};
#define SURBL_REDIRECTOR_CALLBACK "SURBL_REDIRECTOR_CALLBACK"
static const guint64 rspamd_surbl_cb_magic = 0xe09b8536f80de0d1ULL;
static const gchar *rspamd_surbl_default_monitored = "facebook.com";
static const guint default_max_redirected_urls = 10;
static void surbl_test_url (struct rspamd_task *task,
struct rspamd_symcache_item *item,
void *user_data);
static void surbl_test_redirector (struct rspamd_task *task,
struct rspamd_symcache_item *item,
void *user_data);
static void surbl_dns_callback (struct rdns_reply *reply, gpointer arg);
static void surbl_dns_ip_callback (struct rdns_reply *reply, gpointer arg);
static void process_dns_results (struct rspamd_task *task,
struct suffix_item *suffix, gchar *resolved_name,
guint32 addr, struct rspamd_url *url);
static gint surbl_register_redirect_handler (lua_State *L);
static gint surbl_continue_process_handler (lua_State *L);
static gint surbl_is_redirector_handler (lua_State *L);
#define NO_REGEXP (gpointer) - 1
#define SURBL_ERROR surbl_error_quark ()
#define WHITELIST_ERROR 0
#define CONVERSION_ERROR 1
#define DUPLICATE_ERROR 1
GQuark
surbl_error_quark (void)
{
return g_quark_from_static_string ("surbl-error-quark");
}
/* Initialization */
gint surbl_module_init (struct rspamd_config *cfg, struct module_ctx **ctx);
gint surbl_module_config (struct rspamd_config *cfg);
gint surbl_module_reconfig (struct rspamd_config *cfg);
module_t surbl_module = {
"surbl",
surbl_module_init,
surbl_module_config,
surbl_module_reconfig,
NULL,
RSPAMD_MODULE_VER,
(guint)-1,
};
static inline struct surbl_ctx *
surbl_get_context (struct rspamd_config *cfg)
{
return (struct surbl_ctx *)g_ptr_array_index (cfg->c_modules,
surbl_module.ctx_offset);
}
static void
exceptions_free_value (gpointer v)
{
rspamd_ftok_t *val = v;
g_free ((gpointer)val->begin);
g_free (val);
}
static void
exception_insert (gpointer st, gconstpointer key, gconstpointer value)
{
GHashTable **t = st;
gint level = 0;
const gchar *p = key;
rspamd_ftok_t *val;
while (*p) {
if (*p == '.') {
level++;
}
p++;
}
if (level >= MAX_LEVELS) {
msg_err ("invalid domain in exceptions list: %s, levels: %d",
(gchar *)key,
level);
return;
}
val = g_malloc (sizeof (rspamd_ftok_t));
val->begin = g_strdup (key);
val->len = strlen (key);
if (t[level] == NULL) {
t[level] = g_hash_table_new_full (rspamd_ftok_icase_hash,
rspamd_ftok_icase_equal,
exceptions_free_value,
g_free);
}
g_hash_table_replace (t[level], val, g_strdup (value));
}
static gchar *
read_exceptions_list (gchar * chunk,
gint len,
struct map_cb_data *data,
gboolean final)
{
GHashTable **t;
guint i;
if (data->cur_data == NULL) {
t = data->prev_data;
if (t) {
for (i = 0; i < MAX_LEVELS; i++) {
if (t[i] != NULL) {
g_hash_table_destroy (t[i]);
}
t[i] = NULL;
}
g_free (t);
}
data->prev_data = NULL;
data->cur_data = g_malloc0 (MAX_LEVELS * sizeof (GHashTable *));
}
return rspamd_parse_kv_list (
chunk,
len,
data,
exception_insert,
"",
final);
}
static void
fin_exceptions_list (struct map_cb_data *data, void **target)
{
GHashTable **t;
gint i;
if (target) {
*target = data->cur_data;
}
if (data->prev_data) {
t = data->prev_data;
for (i = 0; i < MAX_LEVELS; i++) {
if (t[i] != NULL) {
rspamd_default_log_function (G_LOG_LEVEL_DEBUG,
"surbl", "",
G_STRFUNC,
"exceptions level %d: %d elements",
i, g_hash_table_size (t[i]));
}
}
}
}
static void
dtor_exceptions_list (struct map_cb_data *data)
{
GHashTable **t;
gint i;
if (data->cur_data) {
t = data->cur_data;
for (i = 0; i < MAX_LEVELS; i++) {
if (t[i] != NULL) {
g_hash_table_destroy (t[i]);
}
t[i] = NULL;
}
g_free (t);
}
}
static void
redirector_insert (gpointer st, gconstpointer key, gconstpointer value)
{
GHashTable *tld_hash = st;
const gchar *p = key, *begin = key;
rspamd_fstring_t *pat;
rspamd_ftok_t *tok;
rspamd_regexp_t *re = NO_REGEXP;
GError *err = NULL;
while (*p && !g_ascii_isspace (*p)) {
p++;
}
pat = rspamd_fstring_new_init (begin, p - begin);
tok = g_malloc0 (sizeof (*tok));
tok->begin = pat->str;
tok->len = pat->len;
if (g_ascii_isspace (*p)) {
while (g_ascii_isspace (*p) && *p) {
p++;
}
if (*p) {
re = rspamd_regexp_new (p,
"ir",
&err);
if (re == NULL) {
msg_warn ("could not read regexp: %e while reading regexp %s",
err,
p);
g_error_free (err);
re = NO_REGEXP;
}
}
}
g_hash_table_replace (tld_hash, tok, re);
}
static void
redirector_item_free (gpointer p)
{
rspamd_regexp_t *re;
if (p != NULL && p != NO_REGEXP) {
re = (rspamd_regexp_t *)p;
rspamd_regexp_unref (re);
}
}
static gchar *
read_redirectors_list (gchar * chunk,
gint len,
struct map_cb_data *data,
gboolean final)
{
GHashTable *tld_hash;
if (data->cur_data == NULL) {
tld_hash = g_hash_table_new_full (rspamd_ftok_icase_hash,
rspamd_ftok_icase_equal,
rspamd_fstring_mapped_ftok_free,
redirector_item_free);
data->cur_data = tld_hash;
}
return rspamd_parse_kv_list (
chunk,
len,
data,
redirector_insert,
"",
final);
}
static void
fin_redirectors_list (struct map_cb_data *data, void **target)
{
GHashTable *tld_hash;
if (target) {
*target = data->cur_data;
}
if (data->prev_data) {
tld_hash = data->prev_data;
g_hash_table_unref (tld_hash);
}
}
static void
dtor_redirectors_list (struct map_cb_data *data)
{
GHashTable *tld_hash;
if (data->cur_data) {
tld_hash = data->cur_data;
g_hash_table_unref (tld_hash);
}
}
gint
surbl_module_init (struct rspamd_config *cfg, struct module_ctx **ctx)
{
struct surbl_ctx *surbl_module_ctx;
surbl_module_ctx = rspamd_mempool_alloc0 (cfg->cfg_pool,
sizeof (struct surbl_ctx));
surbl_module_ctx->use_redirector = 0;
surbl_module_ctx->suffixes = NULL;
surbl_module_ctx->redirectors = NULL;
surbl_module_ctx->whitelist = NULL;
surbl_module_ctx->exceptions = NULL;
surbl_module_ctx->redirector_cbid = -1;
*ctx = (struct module_ctx *)surbl_module_ctx;
rspamd_rcl_add_doc_by_path (cfg,
NULL,
"URL blacklist plugin",
"surbl",
UCL_OBJECT,
NULL,
0,
NULL,
0);
rspamd_rcl_add_doc_by_path (cfg,
"surbl",
"List of redirector servers",
"redirector",
UCL_STRING,
NULL,
0,
NULL,
0);
rspamd_rcl_add_doc_by_path (cfg,
"surbl",
"Map of domains that should be checked with redirector",
"redirector_hosts_map",
UCL_STRING,
NULL,
0,
NULL,
0);
rspamd_rcl_add_doc_by_path (cfg,
"surbl",
"Connect timeout for redirector",
"redirector_connect_timeout",
UCL_TIME,
NULL,
0,
NULL,
0);
rspamd_rcl_add_doc_by_path (cfg,
"surbl",
"Read timeout for redirector",
"redirector_read_timeout",
UCL_TIME,
NULL,
0,
NULL,
0);
rspamd_rcl_add_doc_by_path (cfg,
"surbl",
"Maximum number of URLs to process per message",
"max_urls",
UCL_INT,
NULL,
0,
NULL,
0);
rspamd_rcl_add_doc_by_path (cfg,
"surbl",
"Rules for TLD composition",
"exceptions",
UCL_STRING,
NULL,
0,
NULL,
0);
rspamd_rcl_add_doc_by_path (cfg,
"surbl",
"Map of whitelisted domains",
"whitelist",
UCL_STRING,
NULL,
0,
NULL,
0);
rspamd_rcl_add_doc_by_path (cfg,
"surbl",
"URL blacklist rule",
"rule",
UCL_OBJECT,
NULL,
0,
NULL,
0);
/* Rules doc strings */
rspamd_rcl_add_doc_by_path (cfg,
"surbl.rule",
"Name of DNS black list (e.g. `multi.surbl.com`)",
"suffix",
UCL_STRING,
NULL,
0,
NULL,
0);
rspamd_rcl_add_doc_by_path (cfg,
"surbl.rule",
"Symbol to insert (if no bits or suffixes are defined)",
"symbol",
UCL_STRING,
NULL,
0,
NULL,
0);
rspamd_rcl_add_doc_by_path (cfg,
"surbl.rule",
"Whether the defined rule should be used",
"enabled",
UCL_BOOLEAN,
NULL,
0,
NULL,
0);
rspamd_rcl_add_doc_by_path (cfg,
"surbl.rule",
"Do not try to check URLs with IP address instead of hostname",
"no_ip",
UCL_BOOLEAN,
NULL,
0,
NULL,
0);
rspamd_rcl_add_doc_by_path (cfg,
"surbl.rule",
"Resolve URL host and then check against the specified suffix with reversed IP octets",
"resolve_ip",
UCL_BOOLEAN,
NULL,
0,
NULL,
0);
rspamd_rcl_add_doc_by_path (cfg,
"surbl.rule",
"Check images URLs with this URL list",
"images",
UCL_BOOLEAN,
NULL,
0,
NULL,
0);
rspamd_rcl_add_doc_by_path (cfg,
"surbl.rule",
"Parse IP bits in DNS reply, the content is 'symbol = <bit>'",
"bits",
UCL_OBJECT,
NULL,
0,
NULL,
0);
rspamd_rcl_add_doc_by_path (cfg,
"surbl.rule",
"Parse IP addresses in DNS reply, the content is 'symbol = address'",
"ips",
UCL_OBJECT,
NULL,
0,
NULL,
0);
rspamd_rcl_add_doc_by_path (cfg,
"surbl.rule",
"Check domains in valid DKIM signatures",
"check_dkim",
UCL_BOOLEAN,
NULL,
0,
NULL,
0);
rspamd_rcl_add_doc_by_path (cfg,
"surbl.rule",
"Check full domain name instead of eSLD",
"full_domain",
UCL_BOOLEAN,
NULL,
0,
NULL,
0);
return 0;
}
/*
* Register virtual symbols for suffixes with bit wildcard
*/
static void
register_bit_symbols (struct rspamd_config *cfg, struct suffix_item *suffix,
gint parent_id)
{
guint i;
GHashTableIter it;
struct surbl_bit_item *bit;
gpointer k, v;
if (suffix->ips != NULL) {
g_hash_table_iter_init (&it, suffix->ips);
while (g_hash_table_iter_next (&it, &k, &v)) {
bit = v;
/*
* We can have multiple IPs mapped to a single symbol,
* so skip symbol's registration to avoid duplicates
*/
if (rspamd_symcache_find_symbol (cfg->cache, bit->symbol) == -1) {
rspamd_symcache_add_symbol (cfg->cache, bit->symbol,
0, NULL, NULL,
SYMBOL_TYPE_VIRTUAL, parent_id);
}
msg_debug_config ("bit: %d", bit->bit);
}
}
else if (suffix->bits != NULL) {
for (i = 0; i < suffix->bits->len; i++) {
bit = &g_array_index (suffix->bits, struct surbl_bit_item, i);
rspamd_symcache_add_symbol (cfg->cache, bit->symbol,
0, NULL, NULL,
SYMBOL_TYPE_VIRTUAL, parent_id);
}
}
else {
rspamd_symcache_add_symbol (cfg->cache, suffix->symbol,
0, NULL, NULL,
SYMBOL_TYPE_VIRTUAL, parent_id);
}
}
static void
surbl_module_add_ip (const ucl_object_t *ip, const gchar *symbol,
struct suffix_item* suffix,
struct rspamd_config* cfg)
{
gchar* p;
guint32 bit;
const gchar* ip_val;
struct surbl_bit_item* new_bit;
ip_val = ucl_obj_tostring (ip);
new_bit = rspamd_mempool_alloc (
cfg->cfg_pool,
sizeof(struct surbl_bit_item));
if (inet_pton (AF_INET, ip_val, &bit) != 1) {
msg_err_config ("cannot parse ip %s: %s", ip_val,
strerror (errno));
return;
}
new_bit->bit = bit;
new_bit->symbol = rspamd_mempool_strdup (
cfg->cfg_pool,
symbol);
/* Convert to uppercase */
p = new_bit->symbol;
while (*p) {
*p = g_ascii_toupper (*p);
p++;
}
msg_debug_config ("add new IP suffix: %d with symbol: %s",
(gint)new_bit->bit, new_bit->symbol);
g_hash_table_insert (suffix->ips, &new_bit->bit,
new_bit);
}
static gint
surbl_module_parse_rule (const ucl_object_t* value, struct rspamd_config* cfg)
{
const ucl_object_t* cur_rule;
const ucl_object_t* cur;
gint cb_id;
gint nrules = 0;
struct suffix_item* new_suffix;
const gchar *monitored_domain = NULL;
struct surbl_bit_item* new_bit;
ucl_object_t *ropts;
struct surbl_ctx *surbl_module_ctx = surbl_get_context (cfg);
LL_FOREACH(value, cur_rule) {
monitored_domain = NULL;
cur = ucl_object_lookup (cur_rule, "enabled");
if (cur != NULL && cur->type == UCL_BOOLEAN) {
if (!ucl_object_toboolean (cur)) {
continue;
}
}
cur = ucl_object_lookup (cur_rule, "suffix");
if (cur == NULL) {
msg_err_config("surbl rule must have explicit symbol "
"definition");
continue;
}
new_suffix = rspamd_mempool_alloc0 (cfg->cfg_pool,
sizeof (struct suffix_item));
new_suffix->magic = rspamd_surbl_cb_magic;
new_suffix->suffix = rspamd_mempool_strdup (
cfg->cfg_pool, ucl_obj_tostring (cur));
new_suffix->options = 0;
new_suffix->bits = g_array_new (FALSE, FALSE,
sizeof (struct surbl_bit_item));
rspamd_mempool_add_destructor (cfg->cfg_pool,
(rspamd_mempool_destruct_t )rspamd_array_free_hard,
new_suffix->bits);
cur = ucl_object_lookup (cur_rule, "symbol");
if (cur == NULL) {
if (ucl_object_key (value)) {
new_suffix->symbol = rspamd_mempool_strdup (
cfg->cfg_pool,
ucl_object_key (value));
}
else {
msg_warn_config(
"surbl rule for suffix %s lacks symbol, using %s as symbol",
new_suffix->suffix, DEFAULT_SURBL_SYMBOL);
new_suffix->symbol = rspamd_mempool_strdup (
cfg->cfg_pool, DEFAULT_SURBL_SYMBOL);
}
}
else {
new_suffix->symbol = rspamd_mempool_strdup (
cfg->cfg_pool, ucl_obj_tostring (cur));
}
cur = ucl_object_lookup (cur_rule, "options");
if (cur != NULL && cur->type == UCL_STRING) {
if (strstr(ucl_obj_tostring (cur), "noip") != NULL) {
new_suffix->options |= SURBL_OPTION_NOIP;
}
}
cur = ucl_object_lookup (cur_rule, "no_ip");
if (cur != NULL && cur->type == UCL_BOOLEAN) {
if (ucl_object_toboolean (cur)) {
new_suffix->options |= SURBL_OPTION_NOIP;
}
}
cur = ucl_object_lookup (cur_rule, "monitored_domain");
if (cur != NULL && cur->type == UCL_STRING) {
monitored_domain = ucl_object_tostring (cur);
}
cur = ucl_object_lookup (cur_rule, "resolve_ip");
if (cur != NULL && cur->type == UCL_BOOLEAN) {
if (ucl_object_toboolean (cur)) {
new_suffix->options |= SURBL_OPTION_RESOLVEIP;
if (!monitored_domain) {
monitored_domain = "1.0.0.127";
}
}
}
if (!monitored_domain) {
monitored_domain = rspamd_surbl_default_monitored;
}
ropts = ucl_object_typed_new (UCL_OBJECT);
ucl_object_insert_key (ropts,
ucl_object_fromstring (monitored_domain),
"prefix", 0, false);
ucl_object_insert_key (ropts,
ucl_object_fromstring ("nxdomain"),
"rcode", 0, false);
rspamd_mempool_add_destructor (cfg->cfg_pool,
(rspamd_mempool_destruct_t )ucl_object_unref,
ropts);
cur = ucl_object_lookup_any (cur_rule, "images", "check_images", NULL);
if (cur != NULL && cur->type == UCL_BOOLEAN) {
if (ucl_object_toboolean (cur)) {
new_suffix->options |= SURBL_OPTION_CHECKIMAGES;
}
}
cur = ucl_object_lookup_any (cur_rule, "emails", "check_emails", NULL);
if (cur != NULL && cur->type == UCL_BOOLEAN) {
if (ucl_object_toboolean (cur)) {
new_suffix->options |= SURBL_OPTION_CHECKEMAILS;
}
}
cur = ucl_object_lookup_any (cur_rule, "dkim", "check_dkim", NULL);
if (cur != NULL && cur->type == UCL_BOOLEAN) {
if (ucl_object_toboolean (cur)) {
new_suffix->options |= SURBL_OPTION_CHECKDKIM;
}
}
cur = ucl_object_lookup (cur_rule, "full_domain");
if (cur != NULL && cur->type == UCL_BOOLEAN) {
if (ucl_object_toboolean (cur)) {
new_suffix->options |= SURBL_OPTION_FULLDOMAIN;
}
}
if ((new_suffix->options & (SURBL_OPTION_RESOLVEIP | SURBL_OPTION_NOIP))
== (SURBL_OPTION_NOIP | SURBL_OPTION_RESOLVEIP)) {
/* Mutually exclusive options */
msg_err_config ("options noip and resolve_ip are "
"mutually exclusive for suffix %s", new_suffix->suffix);
continue;
}
GString *sym = g_string_sized_new (127);
gchar *p;
rspamd_printf_gstring (sym, "SURBL_%s",
new_suffix->suffix);
p = sym->str;
while (*p) {
if (*p == '.') {
*p = '_';
}
else {
*p = g_ascii_toupper (*p);
}
p ++;
}
cb_id = rspamd_symcache_add_symbol (cfg->cache, sym->str,
0, surbl_test_url, new_suffix, SYMBOL_TYPE_CALLBACK, -1);
rspamd_config_add_symbol (cfg,
sym->str,
0.0,
"SURBL rule check callback",
"surbl",
RSPAMD_SYMBOL_FLAG_IGNORE,
1,
1);
rspamd_symcache_add_dependency (cfg->cache, cb_id,
SURBL_REDIRECTOR_CALLBACK, -1);
/* Failure symbol */
g_string_append (sym, "_FAIL");
rspamd_symcache_add_symbol (cfg->cache, sym->str,
0, NULL, NULL, SYMBOL_TYPE_VIRTUAL|SYMBOL_TYPE_NOSTAT, cb_id);
rspamd_config_add_symbol (cfg, sym->str, 0.0, "SURBL failure symbol",
"surbl", 0, 0, 0);
g_string_free (sym, TRUE);
nrules++;
new_suffix->callback_id = cb_id;
cur = ucl_object_lookup (cur_rule, "bits");
if (cur != NULL && cur->type == UCL_OBJECT) {
ucl_object_iter_t it = NULL;
const ucl_object_t* cur_bit;
guint32 bit;
while ((cur_bit = ucl_object_iterate (cur, &it, true)) != NULL) {
if (ucl_object_key (cur_bit) != NULL
&& cur_bit->type == UCL_INT) {
gchar* p;
bit = ucl_obj_toint (cur_bit);
new_bit = rspamd_mempool_alloc (
cfg->cfg_pool,
sizeof(struct surbl_bit_item));
new_bit->bit = bit;
new_bit->symbol = rspamd_mempool_strdup (
cfg->cfg_pool,
ucl_object_key (cur_bit));
/* Convert to uppercase */
p = new_bit->symbol;
while (*p) {
*p = g_ascii_toupper (*p);
p++;
}
msg_debug_config("add new bit suffix: %d with symbol: %s",
(gint)new_bit->bit, new_bit->symbol);
g_array_append_val(new_suffix->bits, *new_bit);
}
}
}
cur = ucl_object_lookup(cur_rule, "ips");
if (cur != NULL && cur->type == UCL_OBJECT) {
ucl_object_iter_t it = NULL;
const ucl_object_t* cur_bit;
new_suffix->ips = g_hash_table_new (g_int_hash, g_int_equal);
rspamd_mempool_add_destructor (cfg->cfg_pool,
(rspamd_mempool_destruct_t )g_hash_table_unref,
new_suffix->ips);
while ((cur_bit = ucl_object_iterate (cur, &it, true)) != NULL) {
if (ucl_object_key (cur_bit) != NULL) {
if (ucl_object_type (cur_bit) == UCL_STRING) {
/* Single IP */
surbl_module_add_ip (cur_bit, ucl_object_key (cur_bit),
new_suffix, cfg);
}
else if (ucl_object_type (cur_bit) == UCL_ARRAY) {
ucl_object_iter_t ar_it = NULL;
const ucl_object_t* cur_ar;
/* Array of IPs */
while ((cur_ar = ucl_object_iterate (cur_bit, &ar_it,
true)) != NULL) {
if (ucl_object_type (cur_ar) == UCL_STRING) {
surbl_module_add_ip (cur_ar,
ucl_object_key (cur_bit),
new_suffix, cfg);
}
else {
msg_err_config ("garbadge in ips element");
}
}
}
}
}
}
cur = ucl_object_lookup (cur_rule, "process_script");
if (cur != NULL && cur->type == UCL_STRING) {
lua_State *L = cfg->lua_state;
GString *tb;
gint err_idx;
const gchar *input = ucl_object_tostring (cur);
gboolean loaded = FALSE;
lua_pushcfunction (L, &rspamd_lua_traceback);
err_idx = lua_gettop (L);
/* First try return + input */
tb = g_string_sized_new (strlen (input) + sizeof ("return "));
rspamd_printf_gstring (tb, "return %s", input);
if (luaL_loadstring (L, tb->str) != 0) {
/* Reset stack */
lua_settop (L, err_idx - 1);
lua_pushcfunction (L, &rspamd_lua_traceback);
err_idx = lua_gettop (L);
/* Try with no return */
if (luaL_loadstring (L, input) != 0) {
msg_err_config ("cannot load string %s\n",
input);
}
else {
loaded = TRUE;
}
}
else {
loaded = TRUE;
}
g_string_free (tb, TRUE);
if (loaded) {
if (lua_pcall (L, 0, 1, err_idx) != 0) {
msg_err_config ("call failed: %s\n", lua_tostring (L, -1));
}
else if (lua_isfunction (L, -1)) {
new_suffix->url_process_cbref = luaL_ref (L,
LUA_REGISTRYINDEX);
}
}
lua_settop (L, err_idx - 1);
}