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
|
/*-
* 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 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);
static const char *rspamadm_configdump_help (gboolean full_help);
struct rspamadm_command configdump_command = {
.name = "configdump",
.flags = 0,
.help = rspamadm_configdump_help,
.run = rspamadm_configdump
};
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 },
{NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL}
};
static const char *
rspamadm_configdump_help (gboolean full_help)
{
const char *help_str;
if (full_help) {
help_str = "Perform configuration file dump\n\n"
"Usage: rspamadm configdump [-c <config_name> -j --compact [<path1> [<path2> ...]]]\n"
"Where options are:\n\n"
"-j: output plain json\n"
"--compact: output compacted json\n"
"-c: config file to test\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)
{
struct rspamd_main *rm = ud;
rm->cfg->log_type = RSPAMD_LOG_CONSOLE;
rm->cfg->log_level = G_LOG_LEVEL_CRITICAL;
rspamd_set_logger (rm->cfg, g_quark_try_string ("main"), rm);
if (rspamd_log_open_priv (rm->logger, rm->workers_uid, rm->workers_gid) ==
-1) {
fprintf (stderr, "Fatal error, cannot open logfile, exiting\n");
exit (EXIT_FAILURE);
}
}
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 *))) {
/* 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: %B",
ucl_object_toboolean (elt));
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);
}
}
static void
rspamadm_configdump (gint argc, gchar **argv)
{
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 (
"keypair - create encryption keys");
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);
exit (1);
}
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->cache = rspamd_symbols_cache_new (cfg);
cfg->compiled_modules = modules;
cfg->compiled_workers = workers;
cfg->cfg_name = config;
if (!rspamd_config_read (cfg, cfg->cfg_name, NULL,
config_logger, rspamd_main, ucl_vars)) {
ret = FALSE;
}
else {
/* Do post-load actions */
rspamd_lua_post_load_config (cfg);
if (!rspamd_init_filters (rspamd_main->cfg, FALSE)) {
ret = FALSE;
}
if (ret) {
ret = rspamd_config_post_load (cfg, FALSE);
}
}
if (ret) {
/* 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 ("*** End of section %s ***\n", argv[i]);
}
else {
rspamd_printf ("\n");
}
}
}
}
}
}
if (!ret) {
exit (EXIT_FAILURE);
}
exit (EXIT_SUCCESS);
}
|