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
|
/*-
* Copyright 2016 Vsevolod Stakhov
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "config.h"
#include "rspamadm.h"
#include "cfg_file.h"
#include "cfg_rcl.h"
#include "utlist.h"
#include "rspamd.h"
#include "lua/lua_common.h"
#include "utlist.h"
static gboolean json = FALSE;
static gboolean compact = FALSE;
static gboolean show_help = FALSE;
static gboolean show_comments = FALSE;
static gboolean modules_state = FALSE;
static gboolean symbol_groups_only = FALSE;
static gboolean skip_template = FALSE;
static gchar *config = NULL;
extern struct rspamd_main *rspamd_main;
/* Defined in modules.c */
extern module_t *modules[];
extern worker_t *workers[];
static void rspamadm_configdump (gint argc, gchar **argv, const struct rspamadm_command *);
static const char *rspamadm_configdump_help (gboolean full_help, const struct rspamadm_command *);
struct rspamadm_command configdump_command = {
.name = "configdump",
.flags = 0,
.help = rspamadm_configdump_help,
.run = rspamadm_configdump,
.lua_subrs = NULL,
};
static GOptionEntry entries[] = {
{"json", 'j', 0, G_OPTION_ARG_NONE, &json,
"Json output (pretty formatted)", NULL},
{"compact", 'C', 0, G_OPTION_ARG_NONE, &compact,
"Compacted json output", NULL},
{"config", 'c', 0, G_OPTION_ARG_STRING, &config,
"Config file to test", NULL},
{"show-help", 'h', 0, G_OPTION_ARG_NONE, &show_help,
"Show help as comments for each option", NULL },
{"show-comments", 's', 0, G_OPTION_ARG_NONE, &show_comments,
"Show saved comments from the configuration file", NULL },
{"modules-state", 'm', 0, G_OPTION_ARG_NONE, &modules_state,
"Show modules state only", NULL},
{"groups", 'g', 0, G_OPTION_ARG_NONE, &symbol_groups_only,
"Show symbols groups only", NULL},
{"skip-template", 'T', 0, G_OPTION_ARG_NONE, &skip_template,
"Do not apply Jinja templates", NULL},
{NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL}
};
static const char *
rspamadm_configdump_help (gboolean full_help, const struct rspamadm_command *cmd)
{
const char *help_str;
if (full_help) {
help_str = "Perform configuration file dump\n\n"
"Usage: rspamadm configdump [-c <config_name> [-j --compact -m] [<path1> [<path2> ...]]]\n"
"Where options are:\n\n"
"-j: output plain json\n"
"--compact: output compacted json\n"
"-c: config file to test\n"
"-m: show state of modules only\n"
"-h: show help for dumped options\n"
"--help: shows available options and commands";
}
else {
help_str = "Perform configuration file dump";
}
return help_str;
}
static void
config_logger (rspamd_mempool_t *pool, gpointer ud)
{
}
static void
rspamadm_add_doc_elt (const ucl_object_t *obj, const ucl_object_t *doc_obj,
ucl_object_t *comment_obj)
{
rspamd_fstring_t *comment = rspamd_fstring_new ();
const ucl_object_t *elt;
ucl_object_t *nobj, *cur_comment;
if (ucl_object_lookup_len (comment_obj, (const char *)&obj,
sizeof (void *))) {
rspamd_fstring_free (comment);
/* Do not rewrite the existing comment */
return;
}
if (doc_obj != NULL) {
/* Create doc comment */
nobj = ucl_object_fromstring_common ("/*", 0, 0);
}
else {
rspamd_fstring_free (comment);
return;
}
/* We create comments as a list of parts */
elt = ucl_object_lookup (doc_obj, "data");
if (elt) {
rspamd_printf_fstring (&comment, " * %s", ucl_object_tostring (elt));
cur_comment = ucl_object_fromstring_common (comment->str, comment->len, 0);
rspamd_fstring_erase (comment, 0, comment->len);
DL_APPEND (nobj, cur_comment);
}
elt = ucl_object_lookup (doc_obj, "type");
if (elt) {
rspamd_printf_fstring (&comment, " * Type: %s", ucl_object_tostring (elt));
cur_comment = ucl_object_fromstring_common (comment->str, comment->len, 0);
rspamd_fstring_erase (comment, 0, comment->len);
DL_APPEND (nobj, cur_comment);
}
elt = ucl_object_lookup (doc_obj, "required");
if (elt) {
rspamd_printf_fstring (&comment, " * Required: %s",
ucl_object_toboolean (elt) ? "true" : "false");
cur_comment = ucl_object_fromstring_common (comment->str, comment->len, 0);
rspamd_fstring_erase (comment, 0, comment->len);
DL_APPEND (nobj, cur_comment);
}
cur_comment = ucl_object_fromstring (" */");
DL_APPEND (nobj, cur_comment);
rspamd_fstring_free (comment);
ucl_object_insert_key (comment_obj, ucl_object_ref (nobj),
(const char *)&obj,
sizeof (void *), true);
ucl_object_unref (nobj);
}
static void
rspamadm_gen_comments (const ucl_object_t *obj, const ucl_object_t *doc_obj,
ucl_object_t *comments)
{
const ucl_object_t *cur_obj, *cur_doc, *cur_elt;
ucl_object_iter_t it = NULL;
if (obj == NULL || doc_obj == NULL) {
return;
}
if (obj->keylen > 0) {
rspamadm_add_doc_elt (obj, doc_obj, comments);
}
if (ucl_object_type (obj) == UCL_OBJECT) {
while ((cur_obj = ucl_object_iterate (obj, &it, true))) {
cur_doc = ucl_object_lookup_len (doc_obj, cur_obj->key,
cur_obj->keylen);
if (cur_doc != NULL) {
LL_FOREACH (cur_obj, cur_elt) {
if (ucl_object_lookup_len (comments, (const char *)&cur_elt,
sizeof (void *)) == NULL) {
rspamadm_gen_comments (cur_elt, cur_doc, comments);
}
}
}
}
}
}
static void
rspamadm_dump_section_obj (struct rspamd_config *cfg,
const ucl_object_t *obj, const ucl_object_t *doc_obj)
{
rspamd_fstring_t *output;
ucl_object_t *comments = NULL;
output = rspamd_fstring_new ();
if (show_help) {
if (show_comments) {
comments = cfg->config_comments;
}
else {
comments = ucl_object_typed_new (UCL_OBJECT);
}
rspamadm_gen_comments (obj, doc_obj, comments);
}
else if (show_comments) {
comments = cfg->config_comments;
}
if (json) {
rspamd_ucl_emit_fstring_comments (obj, UCL_EMIT_JSON, &output, comments);
}
else if (compact) {
rspamd_ucl_emit_fstring_comments (obj, UCL_EMIT_JSON_COMPACT, &output,
comments);
}
else {
rspamd_ucl_emit_fstring_comments (obj, UCL_EMIT_CONFIG, &output,
comments);
}
rspamd_printf ("%V", output);
rspamd_fstring_free (output);
if (comments != NULL) {
ucl_object_unref (comments);
}
}
__attribute__((noreturn))
static void
rspamadm_configdump (gint argc, gchar **argv, const struct rspamadm_command *cmd)
{
GOptionContext *context;
GError *error = NULL;
const gchar *confdir;
const ucl_object_t *obj, *cur, *doc_obj;
struct rspamd_config *cfg = rspamd_main->cfg;
gboolean ret = TRUE;
worker_t **pworker;
gint i;
context = g_option_context_new (
"configdump - dumps Rspamd configuration");
g_option_context_set_summary (context,
"Summary:\n Rspamd administration utility version "
RVERSION
"\n Release id: "
RID);
g_option_context_add_main_entries (context, entries, NULL);
if (!g_option_context_parse (context, &argc, &argv, &error)) {
fprintf (stderr, "option parsing failed: %s\n", error->message);
g_error_free (error);
g_option_context_free (context);
exit (EXIT_FAILURE);
}
g_option_context_free (context);
if (config == NULL) {
if ((confdir = g_hash_table_lookup (ucl_vars, "CONFDIR")) == NULL) {
confdir = RSPAMD_CONFDIR;
}
config = g_strdup_printf ("%s%c%s", confdir, G_DIR_SEPARATOR,
"rspamd.conf");
}
pworker = &workers[0];
while (*pworker) {
/* Init string quarks */
(void) g_quark_from_static_string ((*pworker)->name);
pworker++;
}
cfg->compiled_modules = modules;
cfg->compiled_workers = workers;
cfg->cfg_name = config;
if (!rspamd_config_read (cfg, cfg->cfg_name, config_logger, rspamd_main,
ucl_vars, skip_template, lua_env)) {
ret = FALSE;
}
else {
/* Do post-load actions */
rspamd_lua_post_load_config (cfg);
(void)rspamd_init_filters (rspamd_main->cfg, false, false);
rspamd_config_post_load (cfg, RSPAMD_CONFIG_INIT_SYMCACHE);
}
if (ret) {
if (modules_state) {
rspamadm_execute_lua_ucl_subr (argc,
argv,
cfg->rcl_obj,
"plugins_stats",
FALSE);
exit (EXIT_SUCCESS);
}
if (symbol_groups_only) {
/*
* Create object from symbols groups and output it using the
* specified format
*/
ucl_object_t *out = ucl_object_typed_new (UCL_OBJECT);
GHashTableIter it;
gpointer k, v;
g_hash_table_iter_init (&it, cfg->groups);
while (g_hash_table_iter_next (&it, &k, &v)) {
const gchar *gr_name = (const gchar *)k;
struct rspamd_symbols_group *gr = (struct rspamd_symbols_group *)v;
ucl_object_t *gr_ucl = ucl_object_typed_new (UCL_OBJECT);
ucl_object_insert_key (gr_ucl,
ucl_object_frombool (!!(gr->flags & RSPAMD_SYMBOL_GROUP_PUBLIC)),
"public", strlen ("public"), false);
ucl_object_insert_key (gr_ucl,
ucl_object_frombool (!!(gr->flags & RSPAMD_SYMBOL_GROUP_DISABLED)),
"disabled", strlen ("disabled"), false);
ucl_object_insert_key (gr_ucl,
ucl_object_frombool (!!(gr->flags & RSPAMD_SYMBOL_GROUP_ONE_SHOT)),
"one_shot", strlen ("one_shot"), false);
ucl_object_insert_key (gr_ucl,
ucl_object_fromdouble (gr->max_score),
"max_score", strlen ("max_score"), false);
ucl_object_insert_key (gr_ucl,
ucl_object_fromstring (gr->description),
"description", strlen ("description"), false);
if (gr->symbols) {
GHashTableIter sit;
gpointer sk, sv;
g_hash_table_iter_init (&sit, gr->symbols);
ucl_object_t *sym_ucl = ucl_object_typed_new (UCL_OBJECT);
while (g_hash_table_iter_next (&sit, &sk, &sv)) {
const gchar *sym_name = (const gchar *) sk;
struct rspamd_symbol *s = (struct rspamd_symbol *)sv;
ucl_object_t *spec_sym = ucl_object_typed_new (UCL_OBJECT);
ucl_object_insert_key (spec_sym,
ucl_object_fromdouble (s->score),
"score", strlen ("score"),
false);
ucl_object_insert_key (spec_sym,
ucl_object_fromstring (s->description),
"description", strlen ("description"), false);
ucl_object_insert_key (spec_sym,
ucl_object_frombool (!!(s->flags & RSPAMD_SYMBOL_FLAG_DISABLED)),
"disabled", strlen ("disabled"),
false);
if (s->nshots == 1) {
ucl_object_insert_key (spec_sym,
ucl_object_frombool (true),
"one_shot", strlen ("one_shot"),
false);
}
else {
ucl_object_insert_key (spec_sym,
ucl_object_frombool (false),
"one_shot", strlen ("one_shot"),
false);
}
ucl_object_t *add_groups = ucl_object_typed_new (UCL_ARRAY);
guint j;
struct rspamd_symbols_group *add_gr;
PTR_ARRAY_FOREACH (s->groups, j, add_gr) {
if (add_gr->name && strcmp (add_gr->name, gr_name) != 0) {
ucl_array_append (add_groups,
ucl_object_fromstring (add_gr->name));
}
}
ucl_object_insert_key (spec_sym,
add_groups,
"extra_groups", strlen ("extra_groups"),
false);
ucl_object_insert_key (sym_ucl, spec_sym, sym_name,
strlen (sym_name), true);
}
ucl_object_insert_key (gr_ucl, sym_ucl, "symbols",
strlen ("symbols"), false);
}
ucl_object_insert_key (out, gr_ucl, gr_name, strlen (gr_name),
true);
}
rspamadm_dump_section_obj (cfg, out, NULL);
exit (EXIT_SUCCESS);
}
/* Output configuration */
if (argc == 1) {
rspamadm_dump_section_obj (cfg, cfg->rcl_obj, cfg->doc_strings);
}
else {
for (i = 1; i < argc; i ++) {
obj = ucl_object_lookup_path (cfg->rcl_obj, argv[i]);
doc_obj = ucl_object_lookup_path (cfg->doc_strings, argv[i]);
if (!obj) {
rspamd_printf ("Section %s NOT FOUND\n", argv[i]);
}
else {
LL_FOREACH (obj, cur) {
if (!json && !compact) {
rspamd_printf ("*** Section %s ***\n", argv[i]);
}
rspamadm_dump_section_obj (cfg, cur, doc_obj);
if (!json && !compact) {
rspamd_printf ("\n*** End of section %s ***\n", argv[i]);
}
else {
rspamd_printf ("\n");
}
}
}
}
}
}
exit (ret ? EXIT_SUCCESS : EXIT_FAILURE);
}
|