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
|
/*-
* 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 "lua/lua_common.h"
#include "contrib/uthash/utlist.h"
/* Common */
static gchar *config_file = NULL;
static gchar *symbol_ham = NULL;
static gchar *symbol_spam = NULL;
static gdouble expire = 0.0;
/* Inputs */
static gchar *spam_db = NULL;
static gchar *ham_db = NULL;
static gchar *cache_db = NULL;
/* Outputs */
static gchar *redis_host = NULL;
static gchar *redis_db = NULL;
static gchar *redis_username = NULL;
static gchar *redis_password = NULL;
static gboolean reset_previous = FALSE;
static void rspamadm_statconvert(gint argc, gchar **argv,
const struct rspamadm_command *cmd);
static const char *rspamadm_statconvert_help(gboolean full_help,
const struct rspamadm_command *cmd);
struct rspamadm_command statconvert_command = {
.name = "statconvert",
.flags = 0,
.help = rspamadm_statconvert_help,
.run = rspamadm_statconvert,
.lua_subrs = NULL,
};
static GOptionEntry entries[] = {
{"config", 'c', 0, G_OPTION_ARG_FILENAME, &config_file,
"Config file to read data from", NULL},
{"reset", 'r', 0, G_OPTION_ARG_NONE, &reset_previous,
"Reset previous data instead of appending values", NULL},
{"expire", 'e', 0, G_OPTION_ARG_DOUBLE, &expire,
"Set expiration in seconds (can be fractional)", NULL},
{"symbol-spam", 0, 0, G_OPTION_ARG_STRING, &symbol_spam,
"Symbol for spam (e.g. BAYES_SPAM)", NULL},
{"symbol-ham", 0, 0, G_OPTION_ARG_STRING, &symbol_ham,
"Symbol for ham (e.g. BAYES_HAM)", NULL},
{"spam-db", 0, 0, G_OPTION_ARG_STRING, &spam_db,
"Input spam file (sqlite3)", NULL},
{"ham-db", 0, 0, G_OPTION_ARG_STRING, &ham_db,
"Input ham file (sqlite3)", NULL},
{"cache", 0, 0, G_OPTION_ARG_FILENAME, &cache_db,
"Input learn cache", NULL},
{"redis-host", 'h', 0, G_OPTION_ARG_STRING, &redis_host,
"Output redis ip (in format ip:port)", NULL},
{"redis-username", 'u', 0, G_OPTION_ARG_STRING, &redis_username,
"Username to connect to redis", NULL},
{"redis-password", 'p', 0, G_OPTION_ARG_STRING, &redis_password,
"Password to connect to redis", NULL},
{"redis-db", 'd', 0, G_OPTION_ARG_STRING, &redis_db,
"Redis database (should be numeric)", NULL},
{NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL}};
static const char *
rspamadm_statconvert_help(gboolean full_help, const struct rspamadm_command *cmd)
{
const char *help_str;
if (full_help) {
help_str = "Convert statistics from sqlite3 to redis\n\n"
"Usage: rspamadm statconvert -c /etc/rspamd.conf [-r]\n"
"Where options are:\n\n"
"-c: config file to read data from\n"
"-r: reset previous data instead of increasing values\n"
"-e: set expire to that amount of seconds\n"
"** Or specify options directly **\n"
"--redis-host: output redis ip (in format ip:port)\n"
"--redis-db: output redis database\n"
"--redis-username: redis username\n"
"--redis-password: redis password\n"
"--cache: sqlite3 file for learn cache\n"
"--spam-db: sqlite3 input file for spam data\n"
"--ham-db: sqlite3 input file for ham data\n"
"--symbol-spam: symbol in redis for spam (e.g. BAYES_SPAM)\n"
"--symbol-ham: symbol in redis for ham (e.g. BAYES_HAM)\n";
}
else {
help_str = "Convert statistics from sqlite3 to redis";
}
return help_str;
}
static void
rspamadm_statconvert(gint argc, gchar **argv, const struct rspamadm_command *cmd)
{
GOptionContext *context;
GError *error = NULL;
ucl_object_t *obj;
context = g_option_context_new(
"statconvert - converts statistics from sqlite3 to redis");
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);
g_option_context_set_ignore_unknown_options(context, TRUE);
if (!g_option_context_parse(context, &argc, &argv, &error)) {
rspamd_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_file) {
/* Load config file, assuming that it has all information required */
struct ucl_parser *parser;
parser = ucl_parser_new(0);
rspamd_ucl_add_conf_variables(parser, ucl_vars);
if (!ucl_parser_add_file(parser, config_file)) {
msg_err("ucl parser error: %s", ucl_parser_get_error(parser));
ucl_parser_free(parser);
exit(EXIT_FAILURE);
}
obj = ucl_parser_get_object(parser);
ucl_parser_free(parser);
}
else {
/* We need to get all information from the command line */
ucl_object_t *classifier, *statfile_ham, *statfile_spam, *tmp, *redis;
/* Check arguments sanity */
if (spam_db == NULL) {
msg_err("No spam-db specified");
exit(EXIT_FAILURE);
}
if (ham_db == NULL) {
msg_err("No ham-db specified");
exit(EXIT_FAILURE);
}
if (redis_host == NULL) {
msg_err("No redis-host specified");
exit(EXIT_FAILURE);
}
if (symbol_ham == NULL) {
msg_err("No symbol-ham specified");
exit(EXIT_FAILURE);
}
if (symbol_spam == NULL) {
msg_err("No symbol-spam specified");
exit(EXIT_FAILURE);
}
obj = ucl_object_typed_new(UCL_OBJECT);
classifier = ucl_object_typed_new(UCL_OBJECT);
ucl_object_insert_key(obj, classifier, "classifier", 0, false);
/* Now we need to create "bayes" key in it */
tmp = ucl_object_typed_new(UCL_OBJECT);
ucl_object_insert_key(classifier, tmp, "bayes", 0, false);
classifier = tmp;
ucl_object_insert_key(classifier, ucl_object_fromstring("sqlite3"),
"backend", 0, false);
if (cache_db != NULL) {
ucl_object_t *cache;
cache = ucl_object_typed_new(UCL_OBJECT);
ucl_object_insert_key(cache, ucl_object_fromstring("sqlite3"),
"type", 0, false);
ucl_object_insert_key(cache, ucl_object_fromstring(cache_db),
"file", 0, false);
ucl_object_insert_key(classifier, cache, "cache", 0, false);
}
statfile_ham = ucl_object_typed_new(UCL_OBJECT);
ucl_object_insert_key(statfile_ham, ucl_object_fromstring(symbol_ham),
"symbol", 0, false);
ucl_object_insert_key(statfile_ham, ucl_object_frombool(false),
"spam", 0, false);
ucl_object_insert_key(statfile_ham, ucl_object_fromstring(ham_db),
"db", 0, false);
statfile_spam = ucl_object_typed_new(UCL_OBJECT);
ucl_object_insert_key(statfile_spam, ucl_object_fromstring(symbol_spam),
"symbol", 0, false);
ucl_object_insert_key(statfile_spam, ucl_object_frombool(true),
"spam", 0, false);
ucl_object_insert_key(statfile_spam, ucl_object_fromstring(spam_db),
"db", 0, false);
DL_APPEND(statfile_ham, statfile_spam);
ucl_object_insert_key(classifier, statfile_ham,
"statfile", 0, false);
/* Deal with redis */
redis = ucl_object_typed_new(UCL_OBJECT);
ucl_object_insert_key(obj, redis, "redis", 0, false);
ucl_object_insert_key(redis, ucl_object_fromstring(redis_host),
"servers", 0, false);
if (redis_db) {
ucl_object_insert_key(redis, ucl_object_fromstring(redis_db),
"dbname", 0, false);
}
if (redis_username) {
ucl_object_insert_key(redis, ucl_object_fromstring(redis_username),
"username", 0, false);
}
if (redis_password) {
ucl_object_insert_key(redis, ucl_object_fromstring(redis_password),
"password", 0, false);
}
}
ucl_object_insert_key(obj, ucl_object_frombool(reset_previous),
"reset_previous", 0, false);
if (expire != 0) {
ucl_object_insert_key(obj, ucl_object_fromdouble(expire),
"expire", 0, false);
}
rspamadm_execute_lua_ucl_subr(argc,
argv,
obj,
"stat_convert",
TRUE);
ucl_object_unref(obj);
}
|