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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
|
/* Copyright (c) 2013, Vsevolod Stakhov
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "cfg_rcl.h"
#include "main.h"
#include "uthash_strcase.h"
#include "utlist.h"
#include "cfg_file.h"
#include "lua/lua_common.h"
#include "expressions.h"
struct rspamd_rcl_default_handler_data {
struct rspamd_rcl_struct_parser pd;
const gchar *key;
rspamd_rcl_handler_t handler;
UT_hash_handle hh;
};
struct rspamd_rcl_section {
const gchar *name; /**< name of section */
rspamd_rcl_handler_t handler; /**< handler of section attributes */
enum ucl_type type; /**< type of attribute */
gboolean required; /**< whether this param is required */
gboolean strict_type; /**< whether we need strict type */
UT_hash_handle hh; /** hash handle */
struct rspamd_rcl_section *subsections; /**< hash table of subsections */
struct rspamd_rcl_default_handler_data *default_parser; /**< generic parsing fields */
rspamd_rcl_section_fin_t fin; /** called at the end of section parsing */
gpointer fin_ud;
};
struct rspamd_worker_param_parser {
rspamd_rcl_handler_t handler; /**< handler function */
struct rspamd_rcl_struct_parser parser; /**< parser attributes */
const gchar *name; /**< parameter's name */
UT_hash_handle hh; /**< hash by name */
};
struct rspamd_worker_cfg_parser {
struct rspamd_worker_param_parser *parsers; /**< parsers hash */
gint type; /**< workers quark */
gboolean (*def_obj_parser)(const ucl_object_t *obj, gpointer ud); /**< default object parser */
gpointer def_ud;
UT_hash_handle hh; /**< hash by type */
};
/*
* Common section handlers
*/
static gboolean
rspamd_rcl_logging_handler (struct rspamd_config *cfg, const ucl_object_t *obj,
gpointer ud, struct rspamd_rcl_section *section, GError **err)
{
const ucl_object_t *val;
const gchar *facility, *log_type, *log_level;
val = ucl_object_find_key (obj, "type");
if (val != NULL && ucl_object_tostring_safe (val, &log_type)) {
if (g_ascii_strcasecmp (log_type, "file") == 0) {
/* Need to get filename */
val = ucl_object_find_key (obj, "filename");
if (val == NULL || val->type != UCL_STRING) {
g_set_error (err,
CFG_RCL_ERROR,
ENOENT,
"filename attribute must be specified for file logging type");
return FALSE;
}
cfg->log_type = RSPAMD_LOG_FILE;
cfg->log_file = rspamd_mempool_strdup (cfg->cfg_pool,
ucl_object_tostring (val));
}
else if (g_ascii_strcasecmp (log_type, "syslog") == 0) {
/* Need to get facility */
cfg->log_facility = LOG_DAEMON;
cfg->log_type = RSPAMD_LOG_SYSLOG;
val = ucl_object_find_key (obj, "facility");
if (val != NULL && ucl_object_tostring_safe (val, &facility)) {
if (g_ascii_strcasecmp (facility, "LOG_AUTH") == 0 ||
g_ascii_strcasecmp (facility, "auth") == 0 ) {
cfg->log_facility = LOG_AUTH;
}
else if (g_ascii_strcasecmp (facility, "LOG_CRON") == 0 ||
g_ascii_strcasecmp (facility, "cron") == 0 ) {
cfg->log_facility = LOG_CRON;
}
else if (g_ascii_strcasecmp (facility, "LOG_DAEMON") == 0 ||
g_ascii_strcasecmp (facility, "daemon") == 0 ) {
cfg->log_facility = LOG_DAEMON;
}
else if (g_ascii_strcasecmp (facility, "LOG_MAIL") == 0 ||
g_ascii_strcasecmp (facility, "mail") == 0) {
cfg->log_facility = LOG_MAIL;
}
else if (g_ascii_strcasecmp (facility, "LOG_USER") == 0 ||
g_ascii_strcasecmp (facility, "user") == 0 ) {
cfg->log_facility = LOG_USER;
}
else if (g_ascii_strcasecmp (facility, "LOG_LOCAL0") == 0 ||
g_ascii_strcasecmp (facility, "local0") == 0) {
cfg->log_facility = LOG_LOCAL0;
}
else if (g_ascii_strcasecmp (facility, "LOG_LOCAL1") == 0 ||
g_ascii_strcasecmp (facility, "local1") == 0) {
cfg->log_facility = LOG_LOCAL1;
}
else if (g_ascii_strcasecmp (facility, "LOG_LOCAL2") == 0 ||
g_ascii_strcasecmp (facility, "local2") == 0) {
cfg->log_facility = LOG_LOCAL2;
}
else if (g_ascii_strcasecmp (facility, "LOG_LOCAL3") == 0 ||
g_ascii_strcasecmp (facility, "local3") == 0) {
cfg->log_facility = LOG_LOCAL3;
}
else if (g_ascii_strcasecmp (facility, "LOG_LOCAL4") == 0 ||
g_ascii_strcasecmp (facility, "local4") == 0) {
cfg->log_facility = LOG_LOCAL4;
}
else if (g_ascii_strcasecmp (facility, "LOG_LOCAL5") == 0 ||
g_ascii_strcasecmp (facility, "local5") == 0) {
cfg->log_facility = LOG_LOCAL5;
}
else if (g_ascii_strcasecmp (facility, "LOG_LOCAL6") == 0 ||
g_ascii_strcasecmp (facility, "local6") == 0) {
cfg->log_facility = LOG_LOCAL6;
}
else if (g_ascii_strcasecmp (facility, "LOG_LOCAL7") == 0 ||
g_ascii_strcasecmp (facility, "local7") == 0) {
cfg->log_facility = LOG_LOCAL7;
}
else {
g_set_error (err,
CFG_RCL_ERROR,
EINVAL,
"invalid log facility: %s",
facility);
return FALSE;
}
}
}
else if (g_ascii_strcasecmp (log_type,
"stderr") == 0 || g_ascii_strcasecmp (log_type, "console") == 0) {
cfg->log_type = RSPAMD_LOG_CONSOLE;
}
else {
g_set_error (err,
CFG_RCL_ERROR,
EINVAL,
"invalid log type: %s",
log_type);
return FALSE;
}
}
else {
/* No type specified */
msg_warn (
"logging type is not specified correctly, log output to the console");
}
/* Handle log level */
val = ucl_object_find_key (obj, "level");
if (val != NULL && ucl_object_tostring_safe (val, &log_level)) {
if (g_ascii_strcasecmp (log_level, "error") == 0) {
cfg->log_level = G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL;
}
else if (g_ascii_strcasecmp (log_level, "warning") == 0) {
cfg->log_level = G_LOG_LEVEL_WARNING;
}
else if (g_ascii_strcasecmp (log_level, "info") == 0) {
cfg->log_level = G_LOG_LEVEL_INFO | G_LOG_LEVEL_MESSAGE;
}
else if (g_ascii_strcasecmp (log_level, "debug") == 0) {
cfg->log_level = G_LOG_LEVEL_DEBUG;
}
else {
g_set_error (err,
CFG_RCL_ERROR,
EINVAL,
"invalid log level: %s",
log_level);
return FALSE;
}
}
return rspamd_rcl_section_parse_defaults (section, cfg, obj, cfg, err);
}
static gboolean
rspamd_rcl_options_handler (struct rspamd_config *cfg, const ucl_object_t *obj,
gpointer ud, struct rspamd_rcl_section *section, GError **err)
{
const ucl_object_t *dns, *upstream;
struct rspamd_rcl_section *dns_section, *upstream_section;
HASH_FIND_STR (section->subsections, "dns", dns_section);
dns = ucl_object_find_key (obj, "dns");
if (dns_section != NULL && dns != NULL) {
if (!rspamd_rcl_section_parse_defaults (dns_section, cfg, dns, cfg,
err)) {
return FALSE;
}
}
HASH_FIND_STR (section->subsections, "upstream", upstream_section);
upstream = ucl_object_find_key (obj, "upstream");
if (upstream_section != NULL && upstream != NULL) {
if (!rspamd_rcl_section_parse_defaults (upstream_section, cfg,
upstream, cfg, err)) {
return FALSE;
}
}
return rspamd_rcl_section_parse_defaults (section, cfg, obj, cfg, err);
}
static gint
rspamd_symbols_group_find_func (gconstpointer a, gconstpointer b)
{
const struct rspamd_symbols_group *gr = a;
const gchar *uv = b;
return g_ascii_strcasecmp (gr->name, uv);
}
/**
* Insert a symbol to the metric
* @param cfg
* @param metric
* @param obj symbol rcl object (either float value or an object)
* @param err
* @return
*/
static gboolean
rspamd_rcl_insert_symbol (struct rspamd_config *cfg, struct metric *metric,
const ucl_object_t *obj, gboolean is_legacy, GError **err)
{
const gchar *group = "ungrouped", *description = NULL, *sym_name;
gdouble symbol_score, *score_ptr;
const ucl_object_t *val;
struct rspamd_symbols_group *sym_group;
struct rspamd_symbol_def *sym_def;
GList *metric_list, *group_list;
gboolean one_shot = FALSE;
/*
* We allow two type of definitions:
* symbol = weight
* or
* symbol {
* weight = ...;
* description = ...;
* group = ...;
* one_shot = true/false;
* }
*/
if (is_legacy) {
val = ucl_object_find_key (obj, "name");
if (val == NULL) {
g_set_error (err, CFG_RCL_ERROR, EINVAL, "symbol name is missing");
return FALSE;
}
sym_name = ucl_object_tostring (val);
}
else {
sym_name = ucl_object_key (obj);
}
if (ucl_object_todouble_safe (obj, &symbol_score)) {
description = NULL;
}
else if (obj->type == UCL_OBJECT) {
val = ucl_object_find_key (obj, "weight");
if (val == NULL || !ucl_object_todouble_safe (val, &symbol_score)) {
g_set_error (err,
CFG_RCL_ERROR,
EINVAL,
"invalid symbol score: %s",
sym_name);
return FALSE;
}
val = ucl_object_find_key (obj, "description");
if (val != NULL) {
description = ucl_object_tostring (val);
}
val = ucl_object_find_key (obj, "group");
if (val != NULL) {
ucl_object_tostring_safe (val, &group);
}
val = ucl_object_find_key (obj, "one_shot");
if (val != NULL) {
one_shot = ucl_object_toboolean (val);
}
}
else {
g_set_error (err,
CFG_RCL_ERROR,
EINVAL,
"invalid symbol type: %s",
sym_name);
return FALSE;
}
sym_def =
rspamd_mempool_alloc (cfg->cfg_pool, sizeof (struct rspamd_symbol_def));
score_ptr = rspamd_mempool_alloc (cfg->cfg_pool, sizeof (gdouble));
*score_ptr = symbol_score;
sym_def->weight_ptr = score_ptr;
sym_def->name = rspamd_mempool_strdup (cfg->cfg_pool, sym_name);
sym_def->description = (gchar *)description;
sym_def->one_shot = one_shot;
g_hash_table_insert (metric->symbols, sym_def->name, sym_def);
if ((metric_list =
g_hash_table_lookup (cfg->metrics_symbols, sym_def->name)) == NULL) {
metric_list = g_list_prepend (NULL, metric);
rspamd_mempool_add_destructor (cfg->cfg_pool,
(rspamd_mempool_destruct_t)g_list_free,
metric_list);
g_hash_table_insert (cfg->metrics_symbols, sym_def->name, metric_list);
}
else {
/* Slow but keep start element of list in safe */
if (!g_list_find (metric_list, metric)) {
metric_list = g_list_append (metric_list, metric);
}
}
/* Search for symbol group */
group_list = g_list_find_custom (cfg->symbols_groups,
group,
rspamd_symbols_group_find_func);
if (group_list == NULL) {
/* Create new group */
sym_group =
rspamd_mempool_alloc (cfg->cfg_pool,
sizeof (struct rspamd_symbols_group));
sym_group->name = rspamd_mempool_strdup (cfg->cfg_pool, group);
sym_group->symbols = NULL;
cfg->symbols_groups = g_list_prepend (cfg->symbols_groups, sym_group);
}
else {
sym_group = group_list->data;
}
/* Insert symbol */
sym_group->symbols = g_list_prepend (sym_group->symbols, sym_def);
return TRUE;
}
static gboolean
rspamd_rcl_metric_handler (struct rspamd_config *cfg, const ucl_object_t *obj,
gpointer ud, struct rspamd_rcl_section *section, GError **err)
{
const ucl_object_t *val, *cur;
const gchar *metric_name, *subject_name, *semicolon, *act_str;
struct metric *metric;
struct metric_action *action;
gdouble action_score, grow_factor;
gint action_value;
gboolean new = TRUE, have_actions = FALSE;
gdouble unknown_weight;
ucl_object_iter_t it = NULL;
val = ucl_object_find_key (obj, "name");
if (val == NULL || !ucl_object_tostring_safe (val, &metric_name)) {
metric_name = DEFAULT_METRIC;
}
metric = g_hash_table_lookup (cfg->metrics, metric_name);
if (metric == NULL) {
metric = rspamd_config_new_metric (cfg, metric);
metric->name = metric_name;
}
else {
new = FALSE;
}
/* Handle actions */
val = ucl_object_find_key (obj, "actions");
if (val != NULL) {
if (val->type != UCL_OBJECT) {
g_set_error (err, CFG_RCL_ERROR, EINVAL,
"actions must be an object");
return FALSE;
}
while ((cur = ucl_iterate_object (val, &it, true)) != NULL) {
if (!rspamd_action_from_str (ucl_object_key (cur), &action_value) ||
!ucl_object_todouble_safe (cur, &action_score)) {
g_set_error (err,
CFG_RCL_ERROR,
EINVAL,
"invalid action definition: %s",
ucl_object_key (cur));
return FALSE;
}
action = &metric->actions[action_value];
action->action = action_value;
action->score = action_score;
}
}
else if (new) {
/* Switch to legacy mode */
val = ucl_object_find_key (obj, "required_score");
if (val != NULL && ucl_object_todouble_safe (val, &action_score)) {
action = &metric->actions[METRIC_ACTION_REJECT];
action->action = METRIC_ACTION_REJECT;
action->score = action_score;
have_actions = TRUE;
}
val = ucl_object_find_key (obj, "action");
LL_FOREACH (val, cur)
{
if (cur->type == UCL_STRING) {
act_str = ucl_object_tostring (cur);
semicolon = strchr (act_str, ':');
if (semicolon != NULL) {
if (rspamd_action_from_str (act_str, &action_value)) {
action_score = strtod (semicolon + 1, NULL);
action = &metric->actions[action_value];
action->action = action_value;
action->score = action_score;
have_actions = TRUE;
}
}
}
}
if (new && !have_actions) {
g_set_error (err,
CFG_RCL_ERROR,
EINVAL,
"metric %s has no actions",
metric_name);
return FALSE;
}
}
/* Handle symbols */
val = ucl_object_find_key (obj, "symbols");
if (val != NULL) {
if (val->type == UCL_ARRAY) {
val = val->value.ov;
}
if (val->type != UCL_OBJECT) {
g_set_error (err, CFG_RCL_ERROR, EINVAL,
"symbols must be an object");
return FALSE;
}
it = NULL;
while ((cur = ucl_iterate_object (val, &it, true)) != NULL) {
if (!rspamd_rcl_insert_symbol (cfg, metric, cur, FALSE, err)) {
return FALSE;
}
}
}
else {
/* Legacy variant */
val = ucl_object_find_key (obj, "symbol");
if (val != NULL) {
if (val->type == UCL_ARRAY) {
val = val->value.ov;
}
if (val->type != UCL_OBJECT) {
g_set_error (err,
CFG_RCL_ERROR,
EINVAL,
"symbols must be an object");
return FALSE;
}
LL_FOREACH (val, cur)
{
if (!rspamd_rcl_insert_symbol (cfg, metric, cur, TRUE, err)) {
return FALSE;
}
}
}
else if (new) {
g_set_error (err,
CFG_RCL_ERROR,
EINVAL,
"metric %s has no symbols",
metric_name);
return FALSE;
}
}
val = ucl_object_find_key (obj, "grow_factor");
if (val && ucl_object_todouble_safe (val, &grow_factor)) {
metric->grow_factor = grow_factor;
}
val = ucl_object_find_key (obj, "subject");
if (val && ucl_object_tostring_safe (val, &subject_name)) {
metric->subject = (gchar *)subject_name;
}
val = ucl_object_find_key (obj, "unknown_weight");
if (val && ucl_object_todouble_safe (val, &unknown_weight) &&
unknown_weight != 0.) {
metric->unknown_weight = unknown_weight;
metric->accept_unknown_symbols = TRUE;
}
/* Insert the resulting metric */
if (new) {
g_hash_table_insert (cfg->metrics, (void *)metric->name, metric);
cfg->metrics_list = g_list_prepend (cfg->metrics_list, metric);
if (strcmp (metric->name, DEFAULT_METRIC) == 0) {
cfg->default_metric = metric;
}
}
return TRUE;
}
static gboolean
rspamd_rcl_worker_handler (struct rspamd_config *cfg, const ucl_object_t *obj,
gpointer ud, struct rspamd_rcl_section *section, GError **err)
{
const ucl_object_t *val, *cur;
ucl_object_iter_t it = NULL;
const gchar *worker_type, *worker_bind;
GQuark qtype;
struct rspamd_worker_conf *wrk;
struct rspamd_worker_cfg_parser *wparser;
struct rspamd_worker_param_parser *whandler;
val = ucl_object_find_key (obj, "type");
if (val != NULL && ucl_object_tostring_safe (val, &worker_type)) {
qtype = g_quark_try_string (worker_type);
if (qtype != 0) {
wrk = rspamd_config_new_worker (cfg, NULL);
wrk->worker = rspamd_get_worker_by_type (qtype);
if (wrk->worker == NULL) {
g_set_error (err,
CFG_RCL_ERROR,
EINVAL,
"unknown worker type: %s",
worker_type);
return FALSE;
}
wrk->type = qtype;
if (wrk->worker->worker_init_func) {
wrk->ctx = wrk->worker->worker_init_func (cfg);
}
}
else {
g_set_error (err,
CFG_RCL_ERROR,
EINVAL,
"unknown worker type: %s",
worker_type);
return FALSE;
}
}
else {
g_set_error (err, CFG_RCL_ERROR, EINVAL, "undefined worker type");
return FALSE;
}
val = ucl_object_find_key (obj, "bind_socket");
if (val != NULL) {
it = ucl_object_iterate_new (val);
while ((cur = ucl_object_iterate_safe (it, true)) != NULL) {
if (!ucl_object_tostring_safe (cur, &worker_bind)) {
continue;
}
if (!rspamd_parse_bind_line (cfg, wrk, worker_bind)) {
g_set_error (err,
CFG_RCL_ERROR,
EINVAL,
"cannot parse bind line: %s",
worker_bind);
ucl_object_iterate_free (it);
return FALSE;
}
}
ucl_object_iterate_free (it);
}
wrk->options = (ucl_object_t *)obj;
if (!rspamd_rcl_section_parse_defaults (section, cfg, obj, wrk, err)) {
return FALSE;
}
/* Parse other attributes */
HASH_FIND_INT (cfg->wrk_parsers, (gint *)&qtype, wparser);
if (wparser != NULL && obj->type == UCL_OBJECT) {
it = NULL;
while ((cur = ucl_iterate_object (obj, &it, true)) != NULL) {
HASH_FIND_STR (wparser->parsers, ucl_object_key (cur), whandler);
if (whandler != NULL) {
if (!whandler->handler (cfg, cur, &whandler->parser, section,
err)) {
return FALSE;
}
}
}
if (wparser->def_obj_parser != NULL) {
if (!wparser->def_obj_parser (obj, wparser->def_ud)) {
return FALSE;
}
}
}
cfg->workers = g_list_prepend (cfg->workers, wrk);
return TRUE;
}
static void
rspamd_rcl_set_lua_globals (struct rspamd_config *cfg, lua_State *L)
{
struct rspamd_config **pcfg;
/* First check for global variable 'config' */
lua_getglobal (L, "config");
if (lua_isnil (L, -1)) {
/* Assign global table to set up attributes */
lua_newtable (L);
lua_setglobal (L, "config");
}
lua_getglobal (L, "metrics");
if (lua_isnil (L, -1)) {
lua_newtable (L);
lua_setglobal (L, "metrics");
}
lua_getglobal (L, "composites");
if (lua_isnil (L, -1)) {
lua_newtable (L);
lua_setglobal (L, "composites");
}
lua_getglobal (L, "classifiers");
if (lua_isnil (L, -1)) {
lua_newtable (L);
lua_setglobal (L, "classifiers");
}
pcfg = lua_newuserdata (L, sizeof (struct rspamd_config *));
rspamd_lua_setclass (L, "rspamd{config}", -1);
*pcfg = cfg;
lua_setglobal (L, "rspamd_config");
/* Clear stack from globals */
lua_pop (L, 4);
}
static gboolean
rspamd_rcl_lua_handler (struct rspamd_config *cfg, const ucl_object_t *obj,
gpointer ud, struct rspamd_rcl_section *section, GError **err)
{
const gchar *lua_src = rspamd_mempool_strdup (cfg->cfg_pool,
ucl_object_tostring (obj));
gchar *cur_dir, *lua_dir, *lua_file, *tmp1, *tmp2;
lua_State *L = cfg->lua_state;
tmp1 = g_strdup (lua_src);
tmp2 = g_strdup (lua_src);
lua_dir = dirname (tmp1);
lua_file = basename (tmp2);
if (lua_dir && lua_file) {
cur_dir = g_malloc (PATH_MAX);
if (getcwd (cur_dir, PATH_MAX) != NULL && chdir (lua_dir) != -1) {
/* Load file */
if (luaL_loadfile (L, lua_file) != 0) {
g_set_error (err,
CFG_RCL_ERROR,
EINVAL,
"cannot load lua file %s: %s",
lua_src,
lua_tostring (L, -1));
if (chdir (cur_dir) == -1) {
msg_err ("cannot chdir to %s: %s", cur_dir,
strerror (errno));;
}
g_free (cur_dir);
g_free (tmp1);
g_free (tmp2);
return FALSE;
}
rspamd_rcl_set_lua_globals (cfg, L);
/* Now do it */
if (lua_pcall (L, 0, LUA_MULTRET, 0) != 0) {
g_set_error (err,
CFG_RCL_ERROR,
EINVAL,
"cannot init lua file %s: %s",
lua_src,
lua_tostring (L, -1));
if (chdir (cur_dir) == -1) {
msg_err ("cannot chdir to %s: %s", cur_dir,
strerror (errno));;
}
g_free (cur_dir);
g_free (tmp1);
g_free (tmp2);
return FALSE;
}
}
else {
g_set_error (err, CFG_RCL_ERROR, ENOENT, "cannot chdir to %s: %s",
lua_src, strerror (errno));
if (chdir (cur_dir) == -1) {
msg_err ("cannot chdir to %s: %s", cur_dir, strerror (errno));;
}
g_free (cur_dir);
g_free (tmp1);
g_free (tmp2);
return FALSE;
}
if (chdir (cur_dir) == -1) {
msg_err ("cannot chdir to %s: %s", cur_dir, strerror (errno));;
}
g_free (cur_dir);
g_free (tmp1);
g_free (tmp2);
}
else {
g_set_error (err, CFG_RCL_ERROR, ENOENT, "cannot find to %s: %s",
lua_src, strerror (errno));
return FALSE;
}
return TRUE;
}
static gboolean
rspamd_rcl_add_module_path (struct rspamd_config *cfg,
const gchar *path,
GError **err)
{
struct stat st;
struct script_module *cur_mod;
glob_t globbuf;
gchar *pattern;
size_t len;
guint i;
if (stat (path, &st) == -1) {
g_set_error (err,
CFG_RCL_ERROR,
errno,
"cannot stat path %s, %s",
path,
strerror (errno));
return FALSE;
}
/* Handle directory */
if (S_ISDIR (st.st_mode)) {
globbuf.gl_offs = 0;
len = strlen (path) + sizeof ("*.lua");
pattern = g_malloc (len);
snprintf (pattern, len, "%s%s", path, "*.lua");
if (glob (pattern, GLOB_DOOFFS, NULL, &globbuf) == 0) {
for (i = 0; i < globbuf.gl_pathc; i++) {
cur_mod =
rspamd_mempool_alloc (cfg->cfg_pool,
sizeof (struct script_module));
cur_mod->path = rspamd_mempool_strdup (cfg->cfg_pool,
globbuf.gl_pathv[i]);
cfg->script_modules = g_list_prepend (cfg->script_modules,
cur_mod);
}
globfree (&globbuf);
g_free (pattern);
}
else {
g_set_error (err,
CFG_RCL_ERROR,
errno,
"glob failed for %s, %s",
pattern,
strerror (errno));
g_free (pattern);
return FALSE;
}
}
else {
/* Handle single file */
cur_mod =
rspamd_mempool_alloc (cfg->cfg_pool, sizeof (struct script_module));
cur_mod->path = rspamd_mempool_strdup (cfg->cfg_pool, path);
cfg->script_modules = g_list_prepend (cfg->script_modules, cur_mod);
}
return TRUE;
}
static gboolean
rspamd_rcl_modules_handler (struct rspamd_config *cfg, const ucl_object_t *obj,
gpointer ud, struct rspamd_rcl_section *section, GError **err)
{
const ucl_object_t *val, *cur;
const gchar *data;
if (obj->type == UCL_OBJECT) {
val = ucl_object_find_key (obj, "path");
LL_FOREACH (val, cur)
{
if (ucl_object_tostring_safe (cur, &data)) {
if (!rspamd_rcl_add_module_path (cfg,
rspamd_mempool_strdup (cfg->cfg_pool, data), err)) {
return FALSE;
}
}
}
}
else if (ucl_object_tostring_safe (obj, &data)) {
if (!rspamd_rcl_add_module_path (cfg,
rspamd_mempool_strdup (cfg->cfg_pool, data), err)) {
return FALSE;
}
}
else {
g_set_error (err,
CFG_RCL_ERROR,
EINVAL,
"module parameter has wrong type (must be an object or a string)");
return FALSE;
}
return TRUE;
}
static gboolean
rspamd_rcl_statfile_handler (struct rspamd_config *cfg, const ucl_object_t *obj,
gpointer ud, struct rspamd_rcl_section *section, GError **err)
{
struct rspamd_classifier_config *ccf = ud;
const ucl_object_t *val;
struct rspamd_statfile_config *st;
GList *labels;
st = rspamd_config_new_statfile (cfg, NULL);
if (rspamd_rcl_section_parse_defaults (section, cfg, obj, st, err)) {
ccf->statfiles = g_list_prepend (ccf->statfiles, st);
if (st->label != NULL) {
labels = g_hash_table_lookup (ccf->labels, st->label);
if (labels != NULL) {
labels = g_list_append (labels, st);
}
else {
g_hash_table_insert (ccf->labels, st->label,
g_list_prepend (NULL, st));
}
}
if (st->symbol != NULL) {
g_hash_table_insert (cfg->classifiers_symbols, st->symbol, st);
}
else {
g_set_error (err,
CFG_RCL_ERROR,
EINVAL,
"statfile must have a symbol defined");
return FALSE;
}
st->opts = (ucl_object_t *)obj;
val = ucl_object_find_key (obj, "spam");
if (val == NULL) {
msg_info (
"statfile %s has no explicit 'spam' setting, trying to guess by symbol",
st->symbol);
if (rspamd_strncasestr (st->symbol, "spam",
strlen (st->symbol)) != NULL) {
st->is_spam = TRUE;
}
else if (rspamd_strncasestr (st->symbol, "ham",
strlen (st->symbol)) != NULL) {
st->is_spam = FALSE;
}
else {
g_set_error (err,
CFG_RCL_ERROR,
EINVAL,
"cannot guess spam setting from %s",
st->symbol);
return FALSE;
}
msg_info ("guessed that statfile with symbol %s is %s",
st->symbol,
st->is_spam ?
"spam" : "ham");
}
return TRUE;
}
return FALSE;
}
static gboolean
rspamd_rcl_classifier_handler (struct rspamd_config *cfg,
const ucl_object_t *obj,
gpointer ud,
struct rspamd_rcl_section *section,
GError **err)
{
const ucl_object_t *val, *cur;
ucl_object_iter_t it = NULL;
const gchar *key;
struct rspamd_classifier_config *ccf;
gboolean res = TRUE;
struct rspamd_rcl_section *stat_section;
ccf = rspamd_config_new_classifier (cfg, NULL);
if (rspamd_rcl_section_parse_defaults (section, cfg, obj, ccf, err)) {
HASH_FIND_STR (section->subsections, "statfile", stat_section);
if (ccf->classifier == NULL) {
ccf->classifier = "bayes";
}
if (ccf->name == NULL) {
ccf->name = ccf->classifier;
}
while ((val = ucl_iterate_object (obj, &it, true)) != NULL && res) {
key = ucl_object_key (val);
if (key != NULL) {
if (g_ascii_strcasecmp (key, "statfile") == 0) {
LL_FOREACH (val, cur) {
res = rspamd_rcl_statfile_handler (cfg,
cur,
ccf,
stat_section,
err);
if (!res) {
return FALSE;
}
}
}
}
}
}
else {
msg_err ("fatal configuration error, cannot parse statfile definition");
}
cfg->classifiers = g_list_prepend (cfg->classifiers, ccf);
return res;
}
static gboolean
rspamd_rcl_composite_handler (struct rspamd_config *cfg,
const ucl_object_t *obj,
gpointer ud,
struct rspamd_rcl_section *section,
GError **err)
{
const ucl_object_t *val;
struct expression *expr;
struct rspamd_composite *composite;
const gchar *composite_name, *composite_expression;
gboolean new = TRUE;
val = ucl_object_find_key (obj, "name");
if (val == NULL || !ucl_object_tostring_safe (val, &composite_name)) {
g_set_error (err,
CFG_RCL_ERROR,
EINVAL,
"composite must have a name defined");
return FALSE;
}
if (g_hash_table_lookup (cfg->composite_symbols, composite_name) != NULL) {
msg_warn ("composite %s is redefined", composite_name);
new = FALSE;
}
val = ucl_object_find_key (obj, "expression");
if (val == NULL || !ucl_object_tostring_safe (val, &composite_expression)) {
g_set_error (err,
CFG_RCL_ERROR,
EINVAL,
"composite must have an expression defined");
return FALSE;
}
if ((expr =
parse_expression (cfg->cfg_pool,
(gchar *)composite_expression)) == NULL) {
g_set_error (err,
CFG_RCL_ERROR,
EINVAL,
"cannot parse composite expression: %s",
composite_expression);
return FALSE;
}
composite =
rspamd_mempool_alloc (cfg->cfg_pool, sizeof (struct rspamd_composite));
composite->expr = expr;
composite->id = g_hash_table_size (cfg->composite_symbols);
g_hash_table_insert (cfg->composite_symbols,
(gpointer)composite_name,
composite);
if (new) {
register_virtual_symbol (&cfg->cache, composite_name, 1);
}
return TRUE;
}
/**
* Add new section to the configuration
* @param top top section
* @param name the name of the section
* @param handler handler function for all attributes
* @param type type of object handled by a handler
* @param required whether at least one of these sections is required
* @param strict_type turn on strict check for types for this section
* @return newly created structure
*/
static inline struct rspamd_rcl_section *
rspamd_rcl_add_section (struct rspamd_rcl_section **top,
const gchar *name, rspamd_rcl_handler_t handler,
enum ucl_type type, gboolean required, gboolean strict_type)
{
struct rspamd_rcl_section *new;
new = g_slice_alloc0 (sizeof (struct rspamd_rcl_section));
new->name = name;
new->handler = handler;
new->type = type;
new->strict_type = strict_type;
HASH_ADD_KEYPTR (hh, *top, new->name, strlen (new->name), new);
return new;
}
/**
* Add a default handler for a section
* @param section section pointer
* @param name name of param
* @param handler handler of param
* @param offset offset in a structure
* @param flags flags for the parser
* @return newly created structure
*/
static inline struct rspamd_rcl_default_handler_data *
rspamd_rcl_add_default_handler (struct rspamd_rcl_section *section,
const gchar *name,
rspamd_rcl_handler_t handler,
gsize offset,
gint flags)
{
struct rspamd_rcl_default_handler_data *new;
new = g_slice_alloc0 (sizeof (struct rspamd_rcl_default_handler_data));
new->key = name;
new->handler = handler;
new->pd.offset = offset;
new->pd.flags = flags;
HASH_ADD_KEYPTR (hh, section->default_parser, new->key, strlen (
new->key), new);
return new;
}
struct rspamd_rcl_section *
rspamd_rcl_config_init (void)
{
struct rspamd_rcl_section *new = NULL, *sub, *ssub;
/*
* Important notice:
* the order of parsing is equal to order of this initialization, therefore
* it is possible to init some portions of config prior to others
*/
/**
* Logging section
*/
sub = rspamd_rcl_add_section (&new,
"logging",
rspamd_rcl_logging_handler,
UCL_OBJECT,
FALSE,
TRUE);
/* Default handlers */
rspamd_rcl_add_default_handler (sub,
"log_buffer",
rspamd_rcl_parse_struct_integer,
G_STRUCT_OFFSET (struct rspamd_config, log_buf_size),
0);
rspamd_rcl_add_default_handler (sub,
"log_urls",
rspamd_rcl_parse_struct_boolean,
G_STRUCT_OFFSET (struct rspamd_config, log_urls),
0);
rspamd_rcl_add_default_handler (sub,
"debug_ip",
rspamd_rcl_parse_struct_string,
G_STRUCT_OFFSET (struct rspamd_config, debug_ip_map),
0);
rspamd_rcl_add_default_handler (sub,
"debug_symbols",
rspamd_rcl_parse_struct_string_list,
G_STRUCT_OFFSET (struct rspamd_config, debug_symbols),
0);
rspamd_rcl_add_default_handler (sub,
"log_color",
rspamd_rcl_parse_struct_boolean,
G_STRUCT_OFFSET (struct rspamd_config, log_color),
0);
/**
* Options section
*/
sub = rspamd_rcl_add_section (&new,
"options",
rspamd_rcl_options_handler,
UCL_OBJECT,
FALSE,
TRUE);
rspamd_rcl_add_default_handler (sub,
"cache_file",
rspamd_rcl_parse_struct_string,
G_STRUCT_OFFSET (struct rspamd_config, cache_filename),
RSPAMD_CL_FLAG_STRING_PATH);
/* Old DNS configuration */
rspamd_rcl_add_default_handler (sub,
"dns_nameserver",
rspamd_rcl_parse_struct_string_list,
G_STRUCT_OFFSET (struct rspamd_config, nameservers),
0);
rspamd_rcl_add_default_handler (sub,
"dns_timeout",
rspamd_rcl_parse_struct_time,
G_STRUCT_OFFSET (struct rspamd_config, dns_timeout),
RSPAMD_CL_FLAG_TIME_FLOAT);
rspamd_rcl_add_default_handler (sub,
"dns_retransmits",
rspamd_rcl_parse_struct_integer,
G_STRUCT_OFFSET (struct rspamd_config, dns_retransmits),
RSPAMD_CL_FLAG_INT_32);
rspamd_rcl_add_default_handler (sub,
"dns_sockets",
rspamd_rcl_parse_struct_integer,
G_STRUCT_OFFSET (struct rspamd_config, dns_io_per_server),
RSPAMD_CL_FLAG_INT_32);
/* New DNS configiration */
ssub = rspamd_rcl_add_section (&sub->subsections, "dns", NULL,
UCL_OBJECT, FALSE, TRUE);
rspamd_rcl_add_default_handler (ssub,
"nameserver",
rspamd_rcl_parse_struct_string_list,
G_STRUCT_OFFSET (struct rspamd_config, nameservers),
0);
rspamd_rcl_add_default_handler (ssub,
"timeout",
rspamd_rcl_parse_struct_time,
G_STRUCT_OFFSET (struct rspamd_config, dns_timeout),
RSPAMD_CL_FLAG_TIME_FLOAT);
rspamd_rcl_add_default_handler (ssub,
"retransmits",
rspamd_rcl_parse_struct_integer,
G_STRUCT_OFFSET (struct rspamd_config, dns_retransmits),
RSPAMD_CL_FLAG_INT_32);
rspamd_rcl_add_default_handler (ssub,
"sockets",
rspamd_rcl_parse_struct_integer,
G_STRUCT_OFFSET (struct rspamd_config, dns_io_per_server),
RSPAMD_CL_FLAG_INT_32);
/* New upstreams configuration */
ssub = rspamd_rcl_add_section (&sub->subsections, "upstream", NULL,
UCL_OBJECT, FALSE, TRUE);
rspamd_rcl_add_default_handler (ssub,
"max_errors",
rspamd_rcl_parse_struct_integer,
G_STRUCT_OFFSET (struct rspamd_config, upstream_max_errors),
RSPAMD_CL_FLAG_UINT);
rspamd_rcl_add_default_handler (ssub,
"error_time",
rspamd_rcl_parse_struct_time,
G_STRUCT_OFFSET (struct rspamd_config, upstream_error_time),
RSPAMD_CL_FLAG_TIME_FLOAT);
rspamd_rcl_add_default_handler (ssub,
"revive_time",
rspamd_rcl_parse_struct_time,
G_STRUCT_OFFSET (struct rspamd_config, upstream_revive_time),
RSPAMD_CL_FLAG_TIME_FLOAT);
rspamd_rcl_add_default_handler (sub,
"raw_mode",
rspamd_rcl_parse_struct_boolean,
G_STRUCT_OFFSET (struct rspamd_config, raw_mode),
0);
rspamd_rcl_add_default_handler (sub,
"one_shot",
rspamd_rcl_parse_struct_boolean,
G_STRUCT_OFFSET (struct rspamd_config, one_shot_mode),
0);
rspamd_rcl_add_default_handler (sub,
"check_attachements",
rspamd_rcl_parse_struct_boolean,
G_STRUCT_OFFSET (struct rspamd_config, check_text_attachements),
0);
rspamd_rcl_add_default_handler (sub,
"tempdir",
rspamd_rcl_parse_struct_string,
G_STRUCT_OFFSET (struct rspamd_config, temp_dir),
RSPAMD_CL_FLAG_STRING_PATH);
rspamd_rcl_add_default_handler (sub,
"pidfile",
rspamd_rcl_parse_struct_string,
G_STRUCT_OFFSET (struct rspamd_config, pid_file),
RSPAMD_CL_FLAG_STRING_PATH);
rspamd_rcl_add_default_handler (sub,
"filters",
rspamd_rcl_parse_struct_string,
G_STRUCT_OFFSET (struct rspamd_config, filters_str),
0);
rspamd_rcl_add_default_handler (sub,
"max_diff",
rspamd_rcl_parse_struct_integer,
G_STRUCT_OFFSET (struct rspamd_config, max_diff),
RSPAMD_CL_FLAG_INT_SIZE);
rspamd_rcl_add_default_handler (sub,
"map_watch_interval",
rspamd_rcl_parse_struct_time,
G_STRUCT_OFFSET (struct rspamd_config, map_timeout),
RSPAMD_CL_FLAG_TIME_FLOAT);
rspamd_rcl_add_default_handler (sub,
"dynamic_conf",
rspamd_rcl_parse_struct_string,
G_STRUCT_OFFSET (struct rspamd_config, dynamic_conf),
0);
rspamd_rcl_add_default_handler (sub, "rrd", rspamd_rcl_parse_struct_string,
G_STRUCT_OFFSET (struct rspamd_config,
rrd_file), RSPAMD_CL_FLAG_STRING_PATH);
rspamd_rcl_add_default_handler (sub,
"history_file",
rspamd_rcl_parse_struct_string,
G_STRUCT_OFFSET (struct rspamd_config, history_file),
RSPAMD_CL_FLAG_STRING_PATH);
rspamd_rcl_add_default_handler (sub,
"use_mlock",
rspamd_rcl_parse_struct_boolean,
G_STRUCT_OFFSET (struct rspamd_config, mlock_statfile_pool),
0);
rspamd_rcl_add_default_handler (sub,
"strict_protocol_headers",
rspamd_rcl_parse_struct_boolean,
G_STRUCT_OFFSET (struct rspamd_config, strict_protocol_headers),
0);
rspamd_rcl_add_default_handler (sub,
"check_all_filters",
rspamd_rcl_parse_struct_boolean,
G_STRUCT_OFFSET (struct rspamd_config, check_all_filters),
0);
/**
* Metric section
*/
sub = rspamd_rcl_add_section (&new,
"metric",
rspamd_rcl_metric_handler,
UCL_OBJECT,
FALSE,
TRUE);
/**
* Worker section
*/
sub = rspamd_rcl_add_section (&new,
"worker",
rspamd_rcl_worker_handler,
UCL_OBJECT,
FALSE,
TRUE);
rspamd_rcl_add_default_handler (sub,
"count",
rspamd_rcl_parse_struct_integer,
G_STRUCT_OFFSET (struct rspamd_worker_conf, count),
RSPAMD_CL_FLAG_INT_16);
rspamd_rcl_add_default_handler (sub,
"max_files",
rspamd_rcl_parse_struct_integer,
G_STRUCT_OFFSET (struct rspamd_worker_conf, rlimit_nofile),
RSPAMD_CL_FLAG_INT_32);
rspamd_rcl_add_default_handler (sub,
"max_core",
rspamd_rcl_parse_struct_integer,
G_STRUCT_OFFSET (struct rspamd_worker_conf, rlimit_maxcore),
RSPAMD_CL_FLAG_INT_32);
/**
* Modules handler
*/
sub = rspamd_rcl_add_section (&new,
"modules",
rspamd_rcl_modules_handler,
UCL_OBJECT,
FALSE,
FALSE);
/**
* Classifiers handler
*/
sub = rspamd_rcl_add_section (&new,
"classifier",
rspamd_rcl_classifier_handler,
UCL_OBJECT,
FALSE,
TRUE);
rspamd_rcl_add_default_handler (sub,
"name",
rspamd_rcl_parse_struct_string,
G_STRUCT_OFFSET (struct rspamd_classifier_config, name),
0);
rspamd_rcl_add_default_handler (sub,
"classifier",
rspamd_rcl_parse_struct_string,
G_STRUCT_OFFSET (struct rspamd_classifier_config, classifier),
0);
/* type is an alias for classifier now */
rspamd_rcl_add_default_handler (sub,
"type",
rspamd_rcl_parse_struct_string,
G_STRUCT_OFFSET (struct rspamd_classifier_config, classifier),
0);
rspamd_rcl_add_default_handler (sub,
"tokenizer",
rspamd_rcl_parse_struct_string,
G_STRUCT_OFFSET (struct rspamd_classifier_config, tokenizer),
0);
rspamd_rcl_add_default_handler (sub,
"min_tokens",
rspamd_rcl_parse_struct_integer,
G_STRUCT_OFFSET (struct rspamd_classifier_config, min_tokens),
RSPAMD_CL_FLAG_INT_32);
rspamd_rcl_add_default_handler (sub,
"max_tokens",
rspamd_rcl_parse_struct_integer,
G_STRUCT_OFFSET (struct rspamd_classifier_config, max_tokens),
RSPAMD_CL_FLAG_INT_32);
/*
* Statfile defaults
*/
ssub = rspamd_rcl_add_section (&sub->subsections,
"statfile",
rspamd_rcl_statfile_handler,
UCL_OBJECT,
TRUE,
TRUE);
rspamd_rcl_add_default_handler (ssub,
"symbol",
rspamd_rcl_parse_struct_string,
G_STRUCT_OFFSET (struct rspamd_statfile_config, symbol),
0);
rspamd_rcl_add_default_handler (ssub,
"label",
rspamd_rcl_parse_struct_string,
G_STRUCT_OFFSET (struct rspamd_statfile_config, label),
0);
rspamd_rcl_add_default_handler (ssub,
"spam",
rspamd_rcl_parse_struct_boolean,
G_STRUCT_OFFSET (struct rspamd_statfile_config, is_spam),
0);
rspamd_rcl_add_default_handler (ssub,
"backend",
rspamd_rcl_parse_struct_string,
G_STRUCT_OFFSET (struct rspamd_statfile_config, backend),
0);
/**
* Composites handler
*/
sub = rspamd_rcl_add_section (&new,
"composite",
rspamd_rcl_composite_handler,
UCL_OBJECT,
FALSE,
TRUE);
/**
* Lua handler
*/
sub = rspamd_rcl_add_section (&new,
"lua",
rspamd_rcl_lua_handler,
UCL_STRING,
FALSE,
TRUE);
return new;
}
struct rspamd_rcl_section *
rspamd_rcl_config_get_section (struct rspamd_rcl_section *top,
const char *path)
{
struct rspamd_rcl_section *cur, *found;
char **path_components;
gint ncomponents, i;
if (path == NULL) {
return top;
}
path_components = g_strsplit_set (path, "/", -1);
ncomponents = g_strv_length (path_components);
cur = top;
for (i = 0; i < ncomponents; i++) {
if (cur == NULL) {
g_strfreev (path_components);
return NULL;
}
HASH_FIND_STR (cur, path_components[i], found);
if (found == NULL) {
g_strfreev (path_components);
return NULL;
}
cur = found;
}
g_strfreev (path_components);
return found;
}
gboolean
rspamd_read_rcl_config (struct rspamd_rcl_section *top,
struct rspamd_config *cfg, const ucl_object_t *obj, GError **err)
{
const ucl_object_t *found, *cur_obj;
struct rspamd_rcl_section *cur, *tmp;
if (obj->type != UCL_OBJECT) {
g_set_error (err,
CFG_RCL_ERROR,
EINVAL,
"top configuration must be an object");
return FALSE;
}
/* Iterate over known sections and ignore unknown ones */
HASH_ITER (hh, top, cur, tmp)
{
found = ucl_object_find_key (obj, cur->name);
if (found == NULL) {
if (cur->required) {
g_set_error (err, CFG_RCL_ERROR, ENOENT,
"required section %s is missing", cur->name);
return FALSE;
}
}
else {
/* Check type */
if (cur->strict_type) {
if (cur->type != found->type) {
g_set_error (err, CFG_RCL_ERROR, EINVAL,
"object in section %s has invalid type", cur->name);
return FALSE;
}
}
LL_FOREACH (found, cur_obj)
{
if (cur->handler != NULL) {
if (!cur->handler (cfg, cur_obj, NULL, cur, err)) {
return FALSE;
}
}
else {
rspamd_rcl_section_parse_defaults (cur,
cfg,
cur_obj,
cfg,
err);
}
}
}
if (cur->fin) {
cur->fin (cfg, cur->fin_ud);
}
}
cfg->rcl_obj = (ucl_object_t *)obj;
return TRUE;
}
gboolean
rspamd_rcl_section_parse_defaults (struct rspamd_rcl_section *section,
struct rspamd_config *cfg, const ucl_object_t *obj, gpointer ptr,
GError **err)
{
const ucl_object_t *found;
struct rspamd_rcl_default_handler_data *cur, *tmp;
if (obj->type != UCL_OBJECT) {
g_set_error (err,
CFG_RCL_ERROR,
EINVAL,
"default configuration must be an object");
return FALSE;
}
HASH_ITER (hh, section->default_parser, cur, tmp)
{
found = ucl_object_find_key (obj, cur->key);
if (found != NULL) {
cur->pd.user_struct = ptr;
if (!cur->handler (cfg, found, &cur->pd, section, err)) {
return FALSE;
}
}
}
return TRUE;
}
gboolean
rspamd_rcl_parse_struct_string (struct rspamd_config *cfg,
const ucl_object_t *obj,
gpointer ud,
struct rspamd_rcl_section *section,
GError **err)
{
struct rspamd_rcl_struct_parser *pd = ud;
gchar **target;
const gsize num_str_len = 32;
target = (gchar **)(((gchar *)pd->user_struct) + pd->offset);
switch (obj->type) {
case UCL_STRING:
*target =
rspamd_mempool_strdup (cfg->cfg_pool, ucl_copy_value_trash (obj));
break;
case UCL_INT:
*target = rspamd_mempool_alloc (cfg->cfg_pool, num_str_len);
rspamd_snprintf (*target, num_str_len, "%L", obj->value.iv);
break;
case UCL_FLOAT:
*target = rspamd_mempool_alloc (cfg->cfg_pool, num_str_len);
rspamd_snprintf (*target, num_str_len, "%f", obj->value.dv);
break;
case UCL_BOOLEAN:
*target = rspamd_mempool_alloc (cfg->cfg_pool, num_str_len);
rspamd_snprintf (*target, num_str_len, "%b", (gboolean)obj->value.iv);
break;
default:
g_set_error (err,
CFG_RCL_ERROR,
EINVAL,
"cannot convert object or array to string");
return FALSE;
}
return TRUE;
}
gboolean
rspamd_rcl_parse_struct_integer (struct rspamd_config *cfg,
const ucl_object_t *obj,
gpointer ud,
struct rspamd_rcl_section *section,
GError **err)
{
struct rspamd_rcl_struct_parser *pd = ud;
union {
gint *ip;
gint32 *i32p;
gint16 *i16p;
gint64 *i64p;
guint *up;
gsize *sp;
} target;
gint64 val;
if (pd->flags == RSPAMD_CL_FLAG_INT_32) {
target.i32p = (gint32 *)(((gchar *)pd->user_struct) + pd->offset);
if (!ucl_object_toint_safe (obj, &val)) {
g_set_error (err,
CFG_RCL_ERROR,
EINVAL,
"cannot convert param to integer");
return FALSE;
}
*target.i32p = val;
}
else if (pd->flags == RSPAMD_CL_FLAG_INT_64) {
target.i64p = (gint64 *)(((gchar *)pd->user_struct) + pd->offset);
if (!ucl_object_toint_safe (obj, &val)) {
g_set_error (err,
CFG_RCL_ERROR,
EINVAL,
"cannot convert param to integer");
return FALSE;
}
*target.i64p = val;
}
else if (pd->flags == RSPAMD_CL_FLAG_INT_SIZE) {
target.sp = (gsize *)(((gchar *)pd->user_struct) + pd->offset);
if (!ucl_object_toint_safe (obj, &val)) {
g_set_error (err,
CFG_RCL_ERROR,
EINVAL,
"cannot convert param to integer");
return FALSE;
}
*target.sp = val;
}
else if (pd->flags == RSPAMD_CL_FLAG_INT_16) {
target.i16p = (gint16 *)(((gchar *)pd->user_struct) + pd->offset);
if (!ucl_object_toint_safe (obj, &val)) {
g_set_error (err,
CFG_RCL_ERROR,
EINVAL,
"cannot convert param to integer");
return FALSE;
}
*target.i16p = val;
}
else if (pd->flags == RSPAMD_CL_FLAG_UINT) {
target.up = (guint *)(((gchar *)pd->user_struct) + pd->offset);
if (!ucl_object_toint_safe (obj, &val)) {
g_set_error (err,
CFG_RCL_ERROR,
EINVAL,
"cannot convert param to integer");
return FALSE;
}
*target.up = val;
}
else {
target.ip = (gint *)(((gchar *)pd->user_struct) + pd->offset);
if (!ucl_object_toint_safe (obj, &val)) {
g_set_error (err,
CFG_RCL_ERROR,
EINVAL,
"cannot convert param to integer");
return FALSE;
}
*target.ip = val;
}
return TRUE;
}
gboolean
rspamd_rcl_parse_struct_double (struct rspamd_config *cfg,
const ucl_object_t *obj,
gpointer ud,
struct rspamd_rcl_section *section,
GError **err)
{
struct rspamd_rcl_struct_parser *pd = ud;
gdouble *target;
target = (gdouble *)(((gchar *)pd->user_struct) + pd->offset);
if (!ucl_object_todouble_safe (obj, target)) {
g_set_error (err,
CFG_RCL_ERROR,
EINVAL,
"cannot convert param to double");
return FALSE;
}
return TRUE;
}
gboolean
rspamd_rcl_parse_struct_time (struct rspamd_config *cfg,
const ucl_object_t *obj,
gpointer ud,
struct rspamd_rcl_section *section,
GError **err)
{
struct rspamd_rcl_struct_parser *pd = ud;
union {
gint *psec;
guint32 *pu32;
gdouble *pdv;
struct timeval *ptv;
struct timespec *pts;
} target;
gdouble val;
if (!ucl_object_todouble_safe (obj, &val)) {
g_set_error (err,
CFG_RCL_ERROR,
EINVAL,
"cannot convert param to double");
return FALSE;
}
if (pd->flags == RSPAMD_CL_FLAG_TIME_TIMEVAL) {
target.ptv =
(struct timeval *)(((gchar *)pd->user_struct) + pd->offset);
target.ptv->tv_sec = (glong)val;
target.ptv->tv_usec = (val - (glong)val) * 1000000;
}
else if (pd->flags == RSPAMD_CL_FLAG_TIME_TIMESPEC) {
target.pts =
(struct timespec *)(((gchar *)pd->user_struct) + pd->offset);
target.pts->tv_sec = (glong)val;
target.pts->tv_nsec = (val - (glong)val) * 1000000000000LL;
}
else if (pd->flags == RSPAMD_CL_FLAG_TIME_FLOAT) {
target.pdv = (double *)(((gchar *)pd->user_struct) + pd->offset);
*target.pdv = val;
}
else if (pd->flags == RSPAMD_CL_FLAG_TIME_INTEGER) {
target.psec = (gint *)(((gchar *)pd->user_struct) + pd->offset);
*target.psec = val * 1000;
}
else if (pd->flags == RSPAMD_CL_FLAG_TIME_UINT_32) {
target.pu32 = (guint32 *)(((gchar *)pd->user_struct) + pd->offset);
*target.pu32 = val * 1000;
}
else {
g_set_error (err,
CFG_RCL_ERROR,
EINVAL,
"invalid flags to parse time value");
return FALSE;
}
return TRUE;
}
gboolean
rspamd_rcl_parse_struct_keypair (struct rspamd_config *cfg,
const ucl_object_t *obj,
gpointer ud,
struct rspamd_rcl_section *section,
GError **err)
{
struct rspamd_rcl_struct_parser *pd = ud;
gpointer *target;
gpointer key;
const gchar *val, *sem = NULL, *pk = NULL, *sk = NULL;
gchar keybuf[256];
const ucl_object_t *elt;
target = (gpointer *)(((gchar *)pd->user_struct) + pd->offset);
if (obj->type == UCL_STRING) {
/* Pk and Sk are just linked all together */
val = ucl_object_tostring (obj);
if ((sem = strchr (val, ':')) != NULL) {
sk = val;
pk = sem + 1;
}
else {
/* Try to parse the key as is */
key = rspamd_http_connection_make_key ((gchar *)val, strlen (val));
if (key != NULL) {
*target = key;
return TRUE;
}
return FALSE;
}
}
else if (obj->type == UCL_OBJECT) {
elt = ucl_object_find_key (obj, "pubkey");
if (elt == NULL || !ucl_object_tostring_safe (elt, &pk)) {
return FALSE;
}
elt = ucl_object_find_key (obj, "privkey");
if (elt == NULL || !ucl_object_tostring_safe (elt, &sk)) {
return FALSE;
}
}
if (sk == NULL || pk == NULL) {
return FALSE;
}
if (!sem) {
rspamd_snprintf (keybuf, sizeof (keybuf), "%s%s", sk, pk);
}
else {
rspamd_snprintf (keybuf, sizeof (keybuf), "%*s%s", sem - sk, sk, pk);
}
key = rspamd_http_connection_make_key (keybuf, strlen (val));
if (key != NULL) {
/* XXX: clean buffer after usage */
*target = key;
return TRUE;
}
return FALSE;
}
gboolean
rspamd_rcl_parse_struct_string_list (struct rspamd_config *cfg,
const ucl_object_t *obj,
gpointer ud,
struct rspamd_rcl_section *section,
GError **err)
{
struct rspamd_rcl_struct_parser *pd = ud;
GList **target;
gchar *val;
const ucl_object_t *cur;
const gsize num_str_len = 32;
ucl_object_iter_t iter = NULL;
target = (GList **)(((gchar *)pd->user_struct) + pd->offset);
if (obj->type != UCL_ARRAY) {
g_set_error (err,
CFG_RCL_ERROR,
EINVAL,
"an array of strings is expected");
return FALSE;
}
while ((cur = ucl_iterate_object (obj, &iter, true)) != NULL) {
switch (cur->type) {
case UCL_STRING:
val =
rspamd_mempool_strdup (cfg->cfg_pool,
ucl_copy_value_trash (cur));
break;
case UCL_INT:
val = rspamd_mempool_alloc (cfg->cfg_pool, num_str_len);
rspamd_snprintf (val, num_str_len, "%L", cur->value.iv);
break;
case UCL_FLOAT:
val = rspamd_mempool_alloc (cfg->cfg_pool, num_str_len);
rspamd_snprintf (val, num_str_len, "%f", cur->value.dv);
break;
case UCL_BOOLEAN:
val = rspamd_mempool_alloc (cfg->cfg_pool, num_str_len);
rspamd_snprintf (val, num_str_len, "%b", (gboolean)cur->value.iv);
break;
default:
g_set_error (err,
CFG_RCL_ERROR,
EINVAL,
"cannot convert an object or array to string");
return FALSE;
}
*target = g_list_prepend (*target, val);
}
/* Add a destructor */
rspamd_mempool_add_destructor (cfg->cfg_pool,
(rspamd_mempool_destruct_t)g_list_free,
*target);
return TRUE;
}
gboolean
rspamd_rcl_parse_struct_boolean (struct rspamd_config *cfg,
const ucl_object_t *obj,
gpointer ud,
struct rspamd_rcl_section *section,
GError **err)
{
struct rspamd_rcl_struct_parser *pd = ud;
gboolean *target;
target = (gboolean *)(((gchar *)pd->user_struct) + pd->offset);
if (obj->type == UCL_BOOLEAN) {
*target = obj->value.iv;
}
else if (obj->type == UCL_INT) {
*target = obj->value.iv;
}
else {
g_set_error (err,
CFG_RCL_ERROR,
EINVAL,
"cannot convert an object to boolean");
return FALSE;
}
return TRUE;
}
void
rspamd_rcl_register_worker_option (struct rspamd_config *cfg,
gint type,
const gchar *name,
rspamd_rcl_handler_t handler,
gpointer target,
gsize offset,
gint flags)
{
struct rspamd_worker_param_parser *nhandler;
struct rspamd_worker_cfg_parser *nparser;
HASH_FIND_INT (cfg->wrk_parsers, &type, nparser);
if (nparser == NULL) {
/* Allocate new parser for this worker */
nparser =
rspamd_mempool_alloc0 (cfg->cfg_pool,
sizeof (struct rspamd_worker_cfg_parser));
nparser->type = type;
HASH_ADD_INT (cfg->wrk_parsers, type, nparser);
}
HASH_FIND_STR (nparser->parsers, name, nhandler);
if (nhandler != NULL) {
msg_warn (
"handler for parameter %s is already registered for worker type %s",
name,
g_quark_to_string (type));
return;
}
nhandler =
rspamd_mempool_alloc0 (cfg->cfg_pool,
sizeof (struct rspamd_worker_param_parser));
nhandler->name = name;
nhandler->parser.flags = flags;
nhandler->parser.offset = offset;
nhandler->parser.user_struct = target;
nhandler->handler = handler;
HASH_ADD_KEYPTR (hh, nparser->parsers, name, strlen (name), nhandler);
}
void
rspamd_rcl_register_worker_parser (struct rspamd_config *cfg, gint type,
gboolean (*func)(ucl_object_t *, gpointer), gpointer ud)
{
struct rspamd_worker_cfg_parser *nparser;
HASH_FIND_INT (cfg->wrk_parsers, &type, nparser);
if (nparser == NULL) {
/* Allocate new parser for this worker */
nparser =
rspamd_mempool_alloc0 (cfg->cfg_pool,
sizeof (struct rspamd_worker_cfg_parser));
nparser->type = type;
HASH_ADD_INT (cfg->wrk_parsers, type, nparser);
}
nparser->def_obj_parser = func;
nparser->def_ud = ud;
}
gboolean
rspamd_config_read (struct rspamd_config *cfg, const gchar *filename,
const gchar *convert_to, rspamd_rcl_section_fin_t logger_fin,
gpointer logger_ud)
{
struct stat st;
gint fd;
gchar *data;
GError *err = NULL;
struct rspamd_rcl_section *top, *logger;
gboolean res;
struct ucl_parser *parser;
if (stat (filename, &st) == -1) {
msg_err ("cannot stat %s: %s", filename, strerror (errno));
return FALSE;
}
if ((fd = open (filename, O_RDONLY)) == -1) {
msg_err ("cannot open %s: %s", filename, strerror (errno));
return FALSE;
}
/* Now mmap this file to simplify reading process */
if ((data =
mmap (NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0)) == MAP_FAILED) {
msg_err ("cannot mmap %s: %s", filename, strerror (errno));
close (fd);
return FALSE;
}
close (fd);
parser = ucl_parser_new (0);
rspamd_ucl_add_conf_variables (parser);
rspamd_ucl_add_conf_macros (parser, cfg);
if (!ucl_parser_add_chunk (parser, data, st.st_size)) {
msg_err ("ucl parser error: %s", ucl_parser_get_error (parser));
ucl_parser_free (parser);
munmap (data, st.st_size);
return FALSE;
}
munmap (data, st.st_size);
cfg->rcl_obj = ucl_parser_get_object (parser);
ucl_parser_free (parser);
res = TRUE;
if (!res) {
return FALSE;
}
top = rspamd_rcl_config_init ();
err = NULL;
HASH_FIND_STR (top, "logging", logger);
if (logger != NULL) {
logger->fin = logger_fin;
logger->fin_ud = logger_ud;
}
if (!rspamd_read_rcl_config (top, cfg, cfg->rcl_obj, &err)) {
msg_err ("rcl parse error: %s", err->message);
return FALSE;
}
return TRUE;
}
|