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
|
/*
* Copyright (c) 2009-2012, 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 BY AUTHOR ''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.
*/
/*
* Read and write rspamd dynamic parameters from xml files
*/
#include "config.h"
#include "cfg_xml.h"
#include "main.h"
#include "logger.h"
#include "util.h"
#include "classifiers/classifiers.h"
#include "tokenizers/tokenizers.h"
#include "cfg_file.h"
#include "view.h"
#include "map.h"
#include "expressions.h"
#include "settings.h"
#include "lua/lua_common.h"
enum xml_read_state {
XML_READ_START,
XML_READ_PARAM,
XML_READ_MODULE,
XML_READ_MODULE_META,
XML_READ_MODULES,
XML_READ_CLASSIFIER,
XML_READ_STATFILE,
XML_READ_METRIC,
XML_READ_WORKER,
XML_READ_VIEW,
XML_READ_LOGGING,
XML_READ_OPTIONS,
XML_READ_VALUE,
XML_SKIP_ELEMENTS,
XML_ERROR,
XML_SUBPARSER,
XML_END
};
/* Maximum attributes for param */
#define MAX_PARAM 64
#define EOL "\n"
GQuark
xml_error_quark (void)
{
return g_quark_from_static_string ("xml-error-quark");
}
static inline const gchar *
xml_state_to_string (struct rspamd_xml_userdata *ud)
{
switch (ud->state) {
case XML_READ_START:
return "read start tag";
case XML_READ_PARAM:
return "read param";
case XML_READ_MODULE:
return "read module section";
case XML_READ_MODULE_META:
return "read module meta section";
case XML_READ_OPTIONS:
return "read options section";
case XML_READ_MODULES:
return "read modules section";
case XML_READ_CLASSIFIER:
return "read classifier section";
case XML_READ_STATFILE:
return "read statfile section";
case XML_READ_METRIC:
return "read metric section";
case XML_READ_WORKER:
return "read worker section";
case XML_READ_VIEW:
return "read view section";
case XML_READ_LOGGING:
return "read logging section";
case XML_READ_VALUE:
return "read value";
case XML_SKIP_ELEMENTS:
return "skip if block";
case XML_ERROR:
return "error occured";
case XML_END:
return "read final tag";
case XML_SUBPARSER:
return "subparser handle";
}
/* Unreached */
return "unknown state";
}
static inline gboolean
extract_attr (const gchar *attr, const gchar **attribute_names, const gchar **attribute_values, gchar **res)
{
const gchar **cur_attr, **cur_value;
cur_attr = attribute_names;
cur_value = attribute_values;
while (*cur_attr && *cur_value) {
if (g_ascii_strcasecmp (*cur_attr, attr) == 0) {
*res = (gchar *) *cur_value;
return TRUE;
}
cur_attr ++;
cur_value ++;
}
return FALSE;
}
/* Find among attributes required ones and form new array of pairs attribute-value */
static gboolean
process_attrs (const gchar **attribute_names, const gchar **attribute_values, ucl_object_t *top)
{
const gchar **attr, **value;
gboolean res = FALSE;
attr = attribute_names;
value = attribute_values;
while (*attr) {
/* Copy attributes to pool */
top = ucl_object_insert_key (top, ucl_object_fromstring_common (*value, 0, UCL_STRING_PARSE), *attr, 0, TRUE);
attr ++;
value ++;
res = TRUE;
}
return res;
}
/* Handlers */
/* XML callbacks */
void
rspamd_xml_start_element (GMarkupParseContext *context, const gchar *element_name, const gchar **attribute_names,
const gchar **attribute_values, gpointer user_data, GError **error)
{
struct rspamd_xml_userdata *ud = user_data;
gchar *res;
ucl_object_t *obj, *tobj;
switch (ud->state) {
case XML_READ_START:
if (g_ascii_strcasecmp (element_name, "rspamd") != 0) {
/* Invalid XML, it must contains root element <rspamd></rspamd> */
*error = g_error_new (xml_error_quark (), XML_START_MISSING, "start element is missing");
ud->state = XML_ERROR;
}
else {
ud->state = XML_READ_PARAM;
}
break;
case XML_READ_PARAM:
/* Read parameter name and try to find among list of known parameters */
/* Legacy XML support */
if (g_ascii_strcasecmp (element_name, "param") == 0) {
if (extract_attr ("value", attribute_names, attribute_values, &res)) {
element_name = res;
}
else if (extract_attr ("name", attribute_names, attribute_values, &res)) {
element_name = res;
}
else {
*error = g_error_new (xml_error_quark (), XML_PARAM_MISSING, "attribute 'value' or 'name' are required for tag 'param'");
ud->state = XML_ERROR;
}
}
rspamd_strlcpy (ud->section_name[ud->nested], element_name, MAX_NAME);
if (ud->nested == 0) {
/* Top object */
if (g_ascii_strcasecmp (element_name, "lua") == 0 &&
extract_attr ("src", attribute_names, attribute_values, &res)) {
/* Lua is 'special' tag */
obj = ucl_object_fromstring (res);
ud->cfg->rcl_obj = ucl_object_insert_key (ud->cfg->rcl_obj, obj, element_name, 0, true);
ud->parent_pointer[0] = obj;
ud->nested ++;
}
else if (g_ascii_strcasecmp (element_name, "composite") == 0) {
/* Composite is 'special' tag */
obj = ucl_object_new ();
obj->type = UCL_OBJECT;
ud->parent_pointer[0] = obj;
ud->cfg->rcl_obj = ucl_object_insert_key (ud->cfg->rcl_obj, obj, element_name, 0, true);
process_attrs (attribute_names, attribute_values, obj);
ud->nested ++;
rspamd_strlcpy (ud->section_name[ud->nested], "expression", MAX_NAME);
}
else if (g_ascii_strcasecmp (element_name, "module") == 0 &&
extract_attr ("name", attribute_names, attribute_values, &res)) {
obj = ucl_object_new ();
obj->type = UCL_OBJECT;
ud->parent_pointer[0] = obj;
ud->cfg->rcl_obj = ucl_object_insert_key (ud->cfg->rcl_obj, obj, res, 0, true);
ud->nested ++;
}
else {
obj = ucl_object_new ();
obj->type = UCL_OBJECT;
ud->parent_pointer[0] = obj;
ud->cfg->rcl_obj = ucl_object_insert_key (ud->cfg->rcl_obj, obj, element_name, 0, true);
process_attrs (attribute_names, attribute_values, obj);
ud->nested ++;
}
}
else {
tobj = ucl_object_new ();
if (g_ascii_strcasecmp (element_name, "symbol") == 0 &&
process_attrs (attribute_names, attribute_values, tobj)) {
ud->parent_pointer[ud->nested] = tobj;
tobj->type = UCL_OBJECT;
ud->parent_pointer[ud->nested - 1] =
ucl_object_insert_key (ud->parent_pointer[ud->nested - 1], tobj, element_name, 0, true);
ud->nested ++;
/* XXX: very ugly */
rspamd_strlcpy (ud->section_name[ud->nested], "name", MAX_NAME);
}
else if (g_ascii_strcasecmp (element_name, "statfile") == 0) {
/* XXX: ugly as well */
ud->parent_pointer[ud->nested] = tobj;
tobj->type = UCL_OBJECT;
ud->parent_pointer[ud->nested - 1] =
ucl_object_insert_key (ud->parent_pointer[ud->nested - 1], tobj, element_name, 0, true);
ud->nested ++;
}
else {
ucl_object_unref (tobj);
process_attrs (attribute_names, attribute_values, ud->parent_pointer[ud->nested - 1]);
}
}
break;
default:
if (*error == NULL) {
*error = g_error_new (xml_error_quark (), XML_EXTRA_ELEMENT, "element %s is unexpected in this state %s",
element_name, xml_state_to_string (ud));
}
break;
}
}
void
rspamd_xml_end_element (GMarkupParseContext *context, const gchar *element_name, gpointer user_data, GError **error)
{
struct rspamd_xml_userdata *ud = user_data;
if (ud->nested > 0) {
if (g_ascii_strcasecmp (ud->section_name[ud->nested - 1], element_name) == 0) {
ud->nested --;
}
else if (g_ascii_strcasecmp (element_name, "param") == 0) {
/* Another ugly hack */
ud->nested --;
}
else if (g_ascii_strcasecmp (ud->section_name[ud->nested], element_name) != 0) {
*error = g_error_new (xml_error_quark (), XML_EXTRA_ELEMENT, "element %s is unmatched", element_name);
ud->state = XML_ERROR;
}
}
else if (g_ascii_strcasecmp ("rspamd", element_name) != 0) {
*error = g_error_new (xml_error_quark (), XML_EXTRA_ELEMENT, "element %s is unmatched on the top level", element_name);
ud->state = XML_ERROR;
}
}
#undef CHECK_TAG
void
rspamd_xml_text (GMarkupParseContext *context, const gchar *text, gsize text_len, gpointer user_data, GError **error)
{
struct rspamd_xml_userdata *ud = user_data;
ucl_object_t *top;
while (text_len > 0 && g_ascii_isspace (*text)) {
text_len --;
text ++;
}
if (text_len == 0) {
return;
}
top = ud->parent_pointer[ud->nested - 1];
ud->parent_pointer[ud->nested - 1] =
ucl_object_insert_key (top, ucl_object_fromstring_common (text, text_len,
UCL_STRING_PARSE|UCL_STRING_PARSE_BYTES),
ud->section_name[ud->nested], 0, true);
}
void
rspamd_xml_error (GMarkupParseContext *context, GError *error, gpointer user_data)
{
struct rspamd_xml_userdata *ud = user_data;
msg_err ("xml parser error: %s, at state \"%s\"", error->message, xml_state_to_string (ud));
}
|