2016-02-04 10:37:21 +01:00
|
|
|
/*-
|
|
|
|
* Copyright 2016 Vsevolod Stakhov
|
2009-02-16 11:41:40 +01:00
|
|
|
*
|
2016-02-04 10:37:21 +01:00
|
|
|
* 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
|
2009-02-16 11:41:40 +01:00
|
|
|
*
|
2016-02-04 10:37:21 +01:00
|
|
|
* 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.
|
2009-02-16 11:41:40 +01:00
|
|
|
*/
|
2009-02-16 17:50:18 +01:00
|
|
|
#include "config.h"
|
2014-09-28 22:44:56 +02:00
|
|
|
#include "libserver/dynamic_cfg.h"
|
2019-01-14 17:48:52 +01:00
|
|
|
#include "libserver/cfg_file_private.h"
|
2014-09-28 22:44:56 +02:00
|
|
|
#include "libutil/rrd.h"
|
|
|
|
#include "libutil/map.h"
|
2018-04-23 18:03:16 +02:00
|
|
|
#include "libutil/map_helpers.h"
|
2016-02-15 15:37:34 +01:00
|
|
|
#include "libutil/map_private.h"
|
2016-06-10 17:38:29 +02:00
|
|
|
#include "libutil/http_private.h"
|
2019-02-15 19:23:40 +01:00
|
|
|
#include "libutil/http_router.h"
|
2015-02-01 23:33:59 +01:00
|
|
|
#include "libstat/stat_api.h"
|
2015-09-22 19:17:24 +02:00
|
|
|
#include "rspamd.h"
|
2015-08-06 16:30:10 +02:00
|
|
|
#include "libserver/worker_util.h"
|
2018-12-06 16:17:29 +01:00
|
|
|
#include "worker_private.h"
|
2016-09-24 21:42:12 +02:00
|
|
|
#include "lua/lua_common.h"
|
2015-04-24 01:12:11 +02:00
|
|
|
#include "cryptobox.h"
|
|
|
|
#include "ottery.h"
|
2016-09-01 16:58:05 +02:00
|
|
|
#include "fuzzy_wire.h"
|
2015-10-08 14:15:15 +02:00
|
|
|
#include "unix-std.h"
|
2016-05-30 13:01:33 +02:00
|
|
|
#include "utlist.h"
|
2018-01-01 19:58:01 +01:00
|
|
|
#include "libmime/lang_detection.h"
|
2015-10-08 14:15:15 +02:00
|
|
|
#include <math.h>
|
2015-04-24 01:12:11 +02:00
|
|
|
|
2014-04-30 18:23:37 +02:00
|
|
|
/* 60 seconds for worker's IO */
|
|
|
|
#define DEFAULT_WORKER_IO_TIMEOUT 60000
|
|
|
|
|
2015-09-15 18:34:36 +02:00
|
|
|
#define DEFAULT_STATS_PATH RSPAMD_DBDIR "/stats.ucl"
|
|
|
|
|
2014-04-30 18:23:37 +02:00
|
|
|
/* HTTP paths */
|
|
|
|
#define PATH_AUTH "/auth"
|
|
|
|
#define PATH_SYMBOLS "/symbols"
|
|
|
|
#define PATH_ACTIONS "/actions"
|
|
|
|
#define PATH_MAPS "/maps"
|
|
|
|
#define PATH_GET_MAP "/getmap"
|
|
|
|
#define PATH_GRAPH "/graph"
|
|
|
|
#define PATH_PIE_CHART "/pie"
|
|
|
|
#define PATH_HISTORY "/history"
|
2015-10-03 15:20:55 +02:00
|
|
|
#define PATH_HISTORY_RESET "/historyreset"
|
2014-04-30 18:23:37 +02:00
|
|
|
#define PATH_LEARN_SPAM "/learnspam"
|
|
|
|
#define PATH_LEARN_HAM "/learnham"
|
|
|
|
#define PATH_SAVE_ACTIONS "/saveactions"
|
|
|
|
#define PATH_SAVE_SYMBOLS "/savesymbols"
|
|
|
|
#define PATH_SAVE_MAP "/savemap"
|
|
|
|
#define PATH_SCAN "/scan"
|
2014-04-30 22:28:27 +02:00
|
|
|
#define PATH_CHECK "/check"
|
2017-05-24 13:15:48 +02:00
|
|
|
#define PATH_CHECKV2 "/checkv2"
|
2014-04-30 18:23:37 +02:00
|
|
|
#define PATH_STAT "/stat"
|
|
|
|
#define PATH_STAT_RESET "/statreset"
|
|
|
|
#define PATH_COUNTERS "/counters"
|
2016-11-14 16:57:32 +01:00
|
|
|
#define PATH_ERRORS "/errors"
|
2017-01-05 18:20:13 +01:00
|
|
|
#define PATH_NEIGHBOURS "/neighbours"
|
2017-01-09 17:08:00 +01:00
|
|
|
#define PATH_PLUGINS "/plugins"
|
2017-07-05 09:22:57 +02:00
|
|
|
#define PATH_PING "/ping"
|
2015-09-21 20:14:24 +02:00
|
|
|
|
2015-08-30 13:35:18 +02:00
|
|
|
#define msg_err_session(...) rspamd_default_log_function(G_LOG_LEVEL_CRITICAL, \
|
|
|
|
session->pool->tag.tagname, session->pool->tag.uid, \
|
|
|
|
G_STRFUNC, \
|
|
|
|
__VA_ARGS__)
|
|
|
|
#define msg_warn_session(...) rspamd_default_log_function (G_LOG_LEVEL_WARNING, \
|
|
|
|
session->pool->tag.tagname, session->pool->tag.uid, \
|
|
|
|
G_STRFUNC, \
|
|
|
|
__VA_ARGS__)
|
|
|
|
#define msg_info_session(...) rspamd_default_log_function (G_LOG_LEVEL_INFO, \
|
|
|
|
session->pool->tag.tagname, session->pool->tag.uid, \
|
|
|
|
G_STRFUNC, \
|
|
|
|
__VA_ARGS__)
|
|
|
|
#define msg_err_ctx(...) rspamd_default_log_function(G_LOG_LEVEL_CRITICAL, \
|
|
|
|
"controller", ctx->cfg->cfg_pool->tag.uid, \
|
|
|
|
G_STRFUNC, \
|
|
|
|
__VA_ARGS__)
|
|
|
|
#define msg_warn_ctx(...) rspamd_default_log_function (G_LOG_LEVEL_WARNING, \
|
|
|
|
"controller", ctx->cfg->cfg_pool->tag.uid, \
|
|
|
|
G_STRFUNC, \
|
|
|
|
__VA_ARGS__)
|
|
|
|
#define msg_info_ctx(...) rspamd_default_log_function (G_LOG_LEVEL_INFO, \
|
|
|
|
"controller", ctx->cfg->cfg_pool->tag.uid, \
|
|
|
|
G_STRFUNC, \
|
|
|
|
__VA_ARGS__)
|
2018-01-20 15:10:08 +01:00
|
|
|
|
|
|
|
#define msg_debug_session(...) rspamd_conditional_debug_fast (NULL, session->from_addr, \
|
|
|
|
rspamd_controller_log_id, "controller", session->pool->tag.uid, \
|
2015-08-30 13:35:18 +02:00
|
|
|
G_STRFUNC, \
|
|
|
|
__VA_ARGS__)
|
|
|
|
|
2018-01-20 15:10:08 +01:00
|
|
|
INIT_LOG_MODULE(controller)
|
|
|
|
|
2014-04-30 18:23:37 +02:00
|
|
|
/* Graph colors */
|
|
|
|
#define COLOR_CLEAN "#58A458"
|
|
|
|
#define COLOR_PROBABLE_SPAM "#D67E7E"
|
|
|
|
#define COLOR_GREYLIST "#A0A0A0"
|
|
|
|
#define COLOR_REJECT "#CB4B4B"
|
|
|
|
#define COLOR_TOTAL "#9440ED"
|
|
|
|
|
2019-06-19 15:13:39 +02:00
|
|
|
const static ev_tstamp rrd_update_time = 1.0;
|
|
|
|
const static guint64 rspamd_controller_ctx_magic = 0xf72697805e6941faULL;
|
2016-04-04 17:07:14 +02:00
|
|
|
|
2016-06-27 19:16:11 +02:00
|
|
|
extern void fuzzy_stat_command (struct rspamd_task *task);
|
|
|
|
|
2014-04-30 18:23:58 +02:00
|
|
|
gpointer init_controller_worker (struct rspamd_config *cfg);
|
|
|
|
void start_controller_worker (struct rspamd_worker *worker);
|
2014-04-30 18:23:37 +02:00
|
|
|
|
2014-04-30 18:23:58 +02:00
|
|
|
worker_t controller_worker = {
|
2014-07-23 13:57:31 +02:00
|
|
|
"controller", /* Name */
|
|
|
|
init_controller_worker, /* Init function */
|
|
|
|
start_controller_worker, /* Start function */
|
2018-02-26 16:45:33 +01:00
|
|
|
RSPAMD_WORKER_HAS_SOCKET | RSPAMD_WORKER_KILLABLE |
|
|
|
|
RSPAMD_WORKER_SCANNER | RSPAMD_WORKER_CONTROLLER,
|
2016-05-14 13:39:56 +02:00
|
|
|
RSPAMD_WORKER_SOCKET_TCP, /* TCP socket */
|
2016-02-12 14:34:17 +01:00
|
|
|
RSPAMD_WORKER_VER /* Version info */
|
2012-01-16 17:59:37 +01:00
|
|
|
};
|
2014-04-30 18:23:37 +02:00
|
|
|
/*
|
|
|
|
* Worker's context
|
|
|
|
*/
|
2014-04-30 18:23:58 +02:00
|
|
|
struct rspamd_controller_worker_ctx {
|
2016-04-04 17:07:14 +02:00
|
|
|
guint64 magic;
|
2014-04-30 18:23:37 +02:00
|
|
|
/* Events base */
|
2019-06-19 15:13:39 +02:00
|
|
|
struct ev_loop *event_loop;
|
2017-06-14 11:27:13 +02:00
|
|
|
/* DNS resolver */
|
|
|
|
struct rspamd_dns_resolver *resolver;
|
|
|
|
/* Config */
|
|
|
|
struct rspamd_config *cfg;
|
|
|
|
/* END OF COMMON PART */
|
2019-06-19 18:07:56 +02:00
|
|
|
ev_tstamp timeout;
|
2014-04-30 18:23:37 +02:00
|
|
|
/* Whether we use ssl for this server */
|
|
|
|
gboolean use_ssl;
|
|
|
|
/* Webui password */
|
|
|
|
gchar *password;
|
|
|
|
/* Privilleged password */
|
|
|
|
gchar *enable_password;
|
2015-09-22 10:25:14 +02:00
|
|
|
/* Cached versions of the passwords */
|
2015-10-06 17:45:47 +02:00
|
|
|
rspamd_ftok_t cached_password;
|
|
|
|
rspamd_ftok_t cached_enable_password;
|
2014-04-30 18:23:37 +02:00
|
|
|
/* HTTP server */
|
2019-02-19 18:57:13 +01:00
|
|
|
struct rspamd_http_context *http_ctx;
|
2014-04-30 18:23:37 +02:00
|
|
|
struct rspamd_http_connection_router *http;
|
|
|
|
/* Server's start time */
|
2019-06-19 15:13:39 +02:00
|
|
|
ev_tstamp start_time;
|
2014-04-30 18:23:37 +02:00
|
|
|
/* Main server */
|
|
|
|
struct rspamd_main *srv;
|
|
|
|
/* SSL cert */
|
|
|
|
gchar *ssl_cert;
|
|
|
|
/* SSL private key */
|
|
|
|
gchar *ssl_key;
|
|
|
|
/* A map of secure IP */
|
2016-05-10 16:29:05 +02:00
|
|
|
const ucl_object_t *secure_ip;
|
2018-04-23 18:03:16 +02:00
|
|
|
struct rspamd_radix_map_helper *secure_map;
|
2014-04-30 18:23:37 +02:00
|
|
|
|
|
|
|
/* Static files dir */
|
|
|
|
gchar *static_files_dir;
|
|
|
|
|
2015-09-15 18:34:36 +02:00
|
|
|
/* Saved statistics path */
|
|
|
|
gchar *saved_stats_path;
|
|
|
|
|
2014-04-30 18:23:37 +02:00
|
|
|
/* Custom commands registered by plugins */
|
|
|
|
GHashTable *custom_commands;
|
|
|
|
|
2017-01-07 19:58:14 +01:00
|
|
|
/* Plugins registered from lua */
|
|
|
|
GHashTable *plugins;
|
|
|
|
|
2014-04-30 18:23:37 +02:00
|
|
|
/* Worker */
|
|
|
|
struct rspamd_worker *worker;
|
2015-02-01 23:44:20 +01:00
|
|
|
|
|
|
|
/* Local keypair */
|
|
|
|
gpointer key;
|
2015-09-18 18:15:18 +02:00
|
|
|
|
2019-06-19 15:13:39 +02:00
|
|
|
ev_timer rrd_event;
|
2015-09-18 18:15:18 +02:00
|
|
|
struct rspamd_rrd_file *rrd;
|
2019-06-19 15:13:39 +02:00
|
|
|
ev_timer save_stats_event;
|
2018-01-01 19:58:01 +01:00
|
|
|
struct rspamd_lang_detector *lang_det;
|
2018-12-06 16:17:29 +01:00
|
|
|
gdouble task_timeout;
|
2017-01-07 19:58:14 +01:00
|
|
|
};
|
2015-09-18 18:15:18 +02:00
|
|
|
|
2017-01-07 19:58:14 +01:00
|
|
|
struct rspamd_controller_plugin_cbdata {
|
|
|
|
lua_State *L;
|
|
|
|
struct rspamd_controller_worker_ctx *ctx;
|
|
|
|
gchar *plugin;
|
|
|
|
struct ucl_lua_funcdata *handler;
|
|
|
|
ucl_object_t *obj;
|
|
|
|
gboolean is_enable;
|
|
|
|
gboolean need_task;
|
2017-01-09 17:08:00 +01:00
|
|
|
guint version;
|
2008-10-23 17:10:40 +02:00
|
|
|
};
|
|
|
|
|
2015-04-24 01:12:11 +02:00
|
|
|
static gboolean
|
2015-04-24 16:18:57 +02:00
|
|
|
rspamd_is_encrypted_password (const gchar *password,
|
|
|
|
struct rspamd_controller_pbkdf const **pbkdf)
|
2015-04-24 01:12:11 +02:00
|
|
|
{
|
2015-04-24 16:18:57 +02:00
|
|
|
const gchar *start, *end;
|
|
|
|
gint64 id;
|
2016-04-21 13:10:59 +02:00
|
|
|
gsize size, i;
|
2015-04-24 16:18:57 +02:00
|
|
|
gboolean ret = FALSE;
|
2016-04-21 13:10:59 +02:00
|
|
|
const struct rspamd_controller_pbkdf *p;
|
2015-04-24 16:18:57 +02:00
|
|
|
|
|
|
|
if (password[0] == '$') {
|
|
|
|
/* Parse id */
|
|
|
|
start = password + 1;
|
|
|
|
end = start;
|
|
|
|
size = 0;
|
|
|
|
|
|
|
|
while (*end != '\0' && g_ascii_isdigit (*end)) {
|
|
|
|
size++;
|
|
|
|
end++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (size > 0) {
|
|
|
|
gchar *endptr;
|
|
|
|
id = strtoul (start, &endptr, 10);
|
|
|
|
|
2016-04-21 13:10:59 +02:00
|
|
|
if ((endptr == NULL || *endptr == *end)) {
|
|
|
|
for (i = 0; i < RSPAMD_PBKDF_ID_MAX - 1; i ++) {
|
|
|
|
p = &pbkdf_list[i];
|
|
|
|
|
|
|
|
if (p->id == id) {
|
|
|
|
ret = TRUE;
|
|
|
|
if (pbkdf != NULL) {
|
2016-07-28 19:34:41 +02:00
|
|
|
*pbkdf = &pbkdf_list[i];
|
2016-04-21 13:10:59 +02:00
|
|
|
}
|
2015-04-24 16:58:12 +02:00
|
|
|
|
2016-04-21 13:10:59 +02:00
|
|
|
break;
|
|
|
|
}
|
2015-04-24 16:58:12 +02:00
|
|
|
}
|
2015-04-24 16:18:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2015-04-24 01:12:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static const gchar *
|
2015-04-24 16:18:57 +02:00
|
|
|
rspamd_encrypted_password_get_str (const gchar * password, gsize skip,
|
|
|
|
gsize * length)
|
2015-04-24 01:12:11 +02:00
|
|
|
{
|
2015-04-24 16:18:57 +02:00
|
|
|
const gchar *str, *start, *end;
|
|
|
|
gsize size;
|
|
|
|
|
|
|
|
start = password + skip;
|
|
|
|
end = start;
|
|
|
|
size = 0;
|
|
|
|
|
|
|
|
while (*end != '\0' && g_ascii_isalnum (*end)) {
|
|
|
|
size++;
|
|
|
|
end++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (size) {
|
|
|
|
str = start;
|
|
|
|
*length = size;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
str = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return str;
|
2015-04-24 01:12:11 +02:00
|
|
|
}
|
|
|
|
|
2015-08-30 13:35:18 +02:00
|
|
|
static gboolean
|
|
|
|
rspamd_check_encrypted_password (struct rspamd_controller_worker_ctx *ctx,
|
2015-10-07 16:58:57 +02:00
|
|
|
const rspamd_ftok_t * password, const gchar * check,
|
2015-09-22 10:25:14 +02:00
|
|
|
const struct rspamd_controller_pbkdf *pbkdf,
|
|
|
|
gboolean is_enable)
|
2015-04-24 01:12:11 +02:00
|
|
|
{
|
2015-04-24 16:18:57 +02:00
|
|
|
const gchar *salt, *hash;
|
|
|
|
gchar *salt_decoded, *key_decoded;
|
2016-01-30 11:32:07 +01:00
|
|
|
gsize salt_len = 0, key_len = 0;
|
2015-04-24 16:18:57 +02:00
|
|
|
gboolean ret = TRUE;
|
|
|
|
guchar *local_key;
|
2015-10-06 17:45:47 +02:00
|
|
|
rspamd_ftok_t *cache;
|
|
|
|
gpointer m;
|
2015-09-22 10:25:14 +02:00
|
|
|
|
|
|
|
/* First of all check cached versions to save resources */
|
|
|
|
if (is_enable && ctx->cached_enable_password.len != 0) {
|
|
|
|
if (password->len != ctx->cached_enable_password.len ||
|
2015-10-07 16:58:57 +02:00
|
|
|
!rspamd_constant_memcmp (password->begin,
|
2015-09-22 10:25:14 +02:00
|
|
|
ctx->cached_enable_password.begin, password->len)) {
|
|
|
|
msg_info_ctx ("incorrect or absent enable password has been specified");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
else if (!is_enable && ctx->cached_password.len != 0) {
|
|
|
|
if (password->len != ctx->cached_password.len ||
|
2015-10-07 16:58:57 +02:00
|
|
|
!rspamd_constant_memcmp (password->begin,
|
2015-09-22 10:25:14 +02:00
|
|
|
ctx->cached_password.begin, password->len)) {
|
2017-06-12 12:38:35 +02:00
|
|
|
/* We still need to check enable password here */
|
|
|
|
if (ctx->cached_enable_password.len != 0) {
|
|
|
|
if (password->len != ctx->cached_enable_password.len ||
|
|
|
|
!rspamd_constant_memcmp (password->begin,
|
|
|
|
ctx->cached_enable_password.begin,
|
|
|
|
password->len)) {
|
|
|
|
msg_info_ctx (
|
|
|
|
"incorrect or absent password has been specified");
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* Cached matched */
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* We might want to check uncached version */
|
|
|
|
goto check_uncached;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* Cached matched */
|
|
|
|
return TRUE;
|
2015-09-22 10:25:14 +02:00
|
|
|
}
|
|
|
|
}
|
2015-04-24 16:18:57 +02:00
|
|
|
|
2017-06-12 12:38:35 +02:00
|
|
|
check_uncached:
|
2015-04-24 16:18:57 +02:00
|
|
|
g_assert (pbkdf != NULL);
|
|
|
|
/* get salt */
|
|
|
|
salt = rspamd_encrypted_password_get_str (check, 3, &salt_len);
|
|
|
|
/* get hash */
|
|
|
|
hash = rspamd_encrypted_password_get_str (check, 3 + salt_len + 1,
|
|
|
|
&key_len);
|
|
|
|
if (salt != NULL && hash != NULL) {
|
|
|
|
|
|
|
|
/* decode salt */
|
|
|
|
salt_decoded = rspamd_decode_base32 (salt, salt_len, &salt_len);
|
|
|
|
|
|
|
|
if (salt_decoded == NULL || salt_len != pbkdf->salt_len) {
|
|
|
|
/* We have some unknown salt here */
|
2015-08-30 13:35:18 +02:00
|
|
|
msg_info_ctx ("incorrect salt: %z, while %z expected",
|
2015-04-24 16:18:57 +02:00
|
|
|
salt_len, pbkdf->salt_len);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2015-04-24 16:58:12 +02:00
|
|
|
key_decoded = rspamd_decode_base32 (hash, key_len, &key_len);
|
2015-04-24 16:18:57 +02:00
|
|
|
|
|
|
|
if (key_decoded == NULL || key_len != pbkdf->key_len) {
|
|
|
|
/* We have some unknown salt here */
|
2015-08-30 13:35:18 +02:00
|
|
|
msg_info_ctx ("incorrect key: %z, while %z expected",
|
2015-04-24 16:18:57 +02:00
|
|
|
key_len, pbkdf->key_len);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
local_key = g_alloca (pbkdf->key_len);
|
2015-10-07 16:58:57 +02:00
|
|
|
rspamd_cryptobox_pbkdf (password->begin, password->len,
|
2015-04-24 16:58:12 +02:00
|
|
|
salt_decoded, salt_len,
|
2016-04-21 13:10:59 +02:00
|
|
|
local_key, pbkdf->key_len, pbkdf->complexity,
|
|
|
|
pbkdf->type);
|
2015-04-24 16:18:57 +02:00
|
|
|
|
|
|
|
if (!rspamd_constant_memcmp (key_decoded, local_key, pbkdf->key_len)) {
|
2015-08-30 13:35:18 +02:00
|
|
|
msg_info_ctx ("incorrect or absent password has been specified");
|
2015-04-24 16:18:57 +02:00
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_free (salt_decoded);
|
|
|
|
g_free (key_decoded);
|
|
|
|
}
|
|
|
|
|
2015-09-22 10:25:14 +02:00
|
|
|
if (ret) {
|
|
|
|
/* Save cached version */
|
|
|
|
cache = is_enable ? &ctx->cached_enable_password : &ctx->cached_password;
|
|
|
|
|
|
|
|
if (cache->len == 0) {
|
|
|
|
/* Mmap region */
|
2017-01-15 18:38:25 +01:00
|
|
|
#ifdef MAP_NOCORE
|
|
|
|
m = mmap (NULL, password->len, PROT_WRITE,
|
|
|
|
MAP_PRIVATE | MAP_ANON | MAP_NOCORE, -1, 0);
|
|
|
|
#else
|
2015-10-06 17:45:47 +02:00
|
|
|
m = mmap (NULL, password->len, PROT_WRITE,
|
2015-09-22 10:25:14 +02:00
|
|
|
MAP_PRIVATE | MAP_ANON, -1, 0);
|
2017-01-15 18:38:25 +01:00
|
|
|
#endif
|
|
|
|
if (m != MAP_FAILED) {
|
|
|
|
memcpy (m, password->begin, password->len);
|
|
|
|
(void)mprotect (m, password->len, PROT_READ);
|
|
|
|
(void)mlock (m, password->len);
|
|
|
|
cache->begin = m;
|
|
|
|
cache->len = password->len;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
msg_err_ctx ("cannot store cached password, mmap failed: %s",
|
|
|
|
strerror (errno));
|
|
|
|
}
|
2015-09-22 10:25:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-24 16:18:57 +02:00
|
|
|
return ret;
|
2015-04-24 01:12:11 +02:00
|
|
|
}
|
|
|
|
|
2016-01-20 01:53:02 +01:00
|
|
|
/**
|
|
|
|
* Checks for X-Forwarded-For header and update client's address if needed
|
|
|
|
*
|
|
|
|
* This function is intended to be called for a trusted client to ensure that
|
|
|
|
* a request is not proxied through it
|
|
|
|
* @return 0 if no forwarded found, 1 if forwarded found and it is yet trusted
|
|
|
|
* and -1 if forwarded is denied
|
|
|
|
*/
|
|
|
|
static gint
|
|
|
|
rspamd_controller_check_forwarded (struct rspamd_controller_session *session,
|
|
|
|
struct rspamd_http_message *msg,
|
|
|
|
struct rspamd_controller_worker_ctx *ctx)
|
|
|
|
{
|
|
|
|
const rspamd_ftok_t *hdr;
|
|
|
|
const gchar *comma;
|
2016-01-20 15:37:47 +01:00
|
|
|
const char *hdr_name = "X-Forwarded-For", *alt_hdr_name = "X-Real-IP";
|
2016-01-20 01:53:02 +01:00
|
|
|
char ip_buf[INET6_ADDRSTRLEN + 1];
|
|
|
|
rspamd_inet_addr_t *addr = NULL;
|
|
|
|
gint ret = 0;
|
|
|
|
|
|
|
|
hdr = rspamd_http_message_find_header (msg, hdr_name);
|
|
|
|
|
|
|
|
if (hdr) {
|
|
|
|
/*
|
|
|
|
* We need to parse and update the header
|
|
|
|
* X-Forwarded-For: client, proxy1, proxy2
|
|
|
|
*/
|
2016-08-23 13:24:08 +02:00
|
|
|
comma = rspamd_memrchr (hdr->begin, ',', hdr->len);
|
2016-01-20 01:53:02 +01:00
|
|
|
if (comma != NULL) {
|
2016-08-23 13:57:37 +02:00
|
|
|
while (comma < hdr->begin + hdr->len &&
|
|
|
|
(*comma == ',' || g_ascii_isspace (*comma))) {
|
2016-08-23 13:24:08 +02:00
|
|
|
comma ++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
comma = hdr->begin;
|
|
|
|
}
|
2016-08-23 13:36:21 +02:00
|
|
|
if (rspamd_parse_inet_address (&addr, comma,
|
|
|
|
(hdr->begin + hdr->len) - comma)) {
|
2016-08-23 13:24:08 +02:00
|
|
|
/* We have addr now, so check if it is still trusted */
|
|
|
|
if (ctx->secure_map &&
|
2018-04-23 18:03:16 +02:00
|
|
|
rspamd_match_radix_map_addr (ctx->secure_map, addr) != NULL) {
|
2016-08-23 13:24:08 +02:00
|
|
|
/* rspamd_inet_address_to_string is not reentrant */
|
|
|
|
rspamd_strlcpy (ip_buf, rspamd_inet_address_to_string (addr),
|
|
|
|
sizeof (ip_buf));
|
|
|
|
msg_info_session ("allow unauthorized proxied connection "
|
|
|
|
"from a trusted IP %s via %s",
|
|
|
|
ip_buf,
|
|
|
|
rspamd_inet_address_to_string (session->from_addr));
|
|
|
|
ret = 1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ret = -1;
|
2016-01-20 01:53:02 +01:00
|
|
|
}
|
2016-08-23 13:24:08 +02:00
|
|
|
|
2017-04-29 14:15:21 +02:00
|
|
|
rspamd_inet_address_free (addr);
|
2016-08-23 13:24:08 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
msg_warn_session ("cannot parse forwarded IP: %T", hdr);
|
|
|
|
ret = -1;
|
2016-01-20 01:53:02 +01:00
|
|
|
}
|
|
|
|
}
|
2016-01-20 15:37:47 +01:00
|
|
|
else {
|
|
|
|
/* Try also X-Real-IP */
|
|
|
|
hdr = rspamd_http_message_find_header (msg, alt_hdr_name);
|
|
|
|
|
|
|
|
if (hdr) {
|
|
|
|
if (rspamd_parse_inet_address (&addr, hdr->begin, hdr->len)) {
|
|
|
|
/* We have addr now, so check if it is still trusted */
|
|
|
|
if (ctx->secure_map &&
|
2018-04-23 18:03:16 +02:00
|
|
|
rspamd_match_radix_map_addr (ctx->secure_map, addr) != NULL) {
|
2016-01-20 15:37:47 +01:00
|
|
|
/* rspamd_inet_address_to_string is not reentrant */
|
|
|
|
rspamd_strlcpy (ip_buf, rspamd_inet_address_to_string (addr),
|
|
|
|
sizeof (ip_buf));
|
|
|
|
msg_info_session ("allow unauthorized proxied connection "
|
|
|
|
"from a trusted IP %s via %s",
|
|
|
|
ip_buf,
|
|
|
|
rspamd_inet_address_to_string (session->from_addr));
|
|
|
|
ret = 1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ret = -1;
|
|
|
|
}
|
|
|
|
|
2017-04-29 14:15:21 +02:00
|
|
|
rspamd_inet_address_free (addr);
|
2016-01-20 15:37:47 +01:00
|
|
|
}
|
2016-08-23 13:24:08 +02:00
|
|
|
else {
|
|
|
|
msg_warn_session ("cannot parse real IP: %T", hdr);
|
|
|
|
ret = -1;
|
|
|
|
}
|
2016-01-20 15:37:47 +01:00
|
|
|
}
|
|
|
|
}
|
2016-01-20 01:53:02 +01:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-04-30 18:23:37 +02:00
|
|
|
/* Check for password if it is required by configuration */
|
2016-11-14 13:51:19 +01:00
|
|
|
static gboolean
|
|
|
|
rspamd_controller_check_password (struct rspamd_http_connection_entry *entry,
|
2015-04-24 16:18:57 +02:00
|
|
|
struct rspamd_controller_session *session,
|
|
|
|
struct rspamd_http_message *msg, gboolean is_enable)
|
2014-04-30 18:23:37 +02:00
|
|
|
{
|
2015-05-12 16:11:42 +02:00
|
|
|
const gchar *check;
|
2015-10-07 16:58:57 +02:00
|
|
|
const rspamd_ftok_t *password;
|
|
|
|
rspamd_ftok_t lookup;
|
2015-08-11 16:58:32 +02:00
|
|
|
GHashTable *query_args = NULL;
|
2014-07-23 13:57:31 +02:00
|
|
|
struct rspamd_controller_worker_ctx *ctx = session->ctx;
|
2015-09-22 10:25:14 +02:00
|
|
|
gboolean check_normal = TRUE, check_enable = TRUE, ret = TRUE,
|
|
|
|
use_enable = FALSE;
|
2015-04-24 16:18:57 +02:00
|
|
|
const struct rspamd_controller_pbkdf *pbkdf = NULL;
|
|
|
|
|
2014-04-30 18:23:37 +02:00
|
|
|
/* Access list logic */
|
2015-07-08 15:17:49 +02:00
|
|
|
if (rspamd_inet_address_get_af (session->from_addr) == AF_UNIX) {
|
2016-01-20 01:53:02 +01:00
|
|
|
ret = rspamd_controller_check_forwarded (session, msg, ctx);
|
|
|
|
|
|
|
|
if (ret == 1) {
|
2016-11-19 17:33:29 +01:00
|
|
|
session->is_enable = TRUE;
|
|
|
|
|
2016-01-20 01:53:02 +01:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
else if (ret == 0) {
|
|
|
|
/* No forwarded found */
|
|
|
|
msg_info_session ("allow unauthorized connection from a unix socket");
|
2016-11-19 17:33:29 +01:00
|
|
|
session->is_enable = TRUE;
|
|
|
|
|
2016-01-20 09:38:35 +01:00
|
|
|
return TRUE;
|
2016-01-20 01:53:02 +01:00
|
|
|
}
|
2014-04-30 18:23:37 +02:00
|
|
|
}
|
2015-04-24 16:18:57 +02:00
|
|
|
else if (ctx->secure_map
|
2018-04-23 18:03:16 +02:00
|
|
|
&& rspamd_match_radix_map_addr (ctx->secure_map, session->from_addr)
|
|
|
|
!= NULL) {
|
2016-01-20 01:53:02 +01:00
|
|
|
ret = rspamd_controller_check_forwarded (session, msg, ctx);
|
|
|
|
|
|
|
|
if (ret == 1) {
|
2016-11-19 17:33:29 +01:00
|
|
|
session->is_enable = TRUE;
|
|
|
|
|
2016-01-20 01:53:02 +01:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
else if (ret == 0) {
|
|
|
|
/* No forwarded found */
|
|
|
|
msg_info_session ("allow unauthorized connection from a trusted IP %s",
|
|
|
|
rspamd_inet_address_to_string (session->from_addr));
|
2016-11-19 17:33:29 +01:00
|
|
|
session->is_enable = TRUE;
|
|
|
|
|
2016-01-20 09:38:35 +01:00
|
|
|
return TRUE;
|
2016-01-20 01:53:02 +01:00
|
|
|
}
|
2014-04-30 18:23:37 +02:00
|
|
|
}
|
|
|
|
|
2015-04-24 16:18:57 +02:00
|
|
|
/* Password logic */
|
2015-04-24 01:12:11 +02:00
|
|
|
password = rspamd_http_message_find_header (msg, "Password");
|
2015-04-24 16:18:57 +02:00
|
|
|
|
2015-04-24 01:12:11 +02:00
|
|
|
if (password == NULL) {
|
2015-08-11 16:58:32 +02:00
|
|
|
/* Try to get password from query args */
|
|
|
|
query_args = rspamd_http_message_parse_query (msg);
|
|
|
|
|
2015-10-07 16:58:57 +02:00
|
|
|
lookup.begin = (gchar *)"password";
|
2015-08-11 16:58:32 +02:00
|
|
|
lookup.len = sizeof ("password") - 1;
|
|
|
|
|
|
|
|
password = g_hash_table_lookup (query_args, &lookup);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (password == NULL) {
|
2015-05-05 16:28:36 +02:00
|
|
|
if (ctx->secure_map == NULL) {
|
|
|
|
if (ctx->password == NULL && !is_enable) {
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
else if (is_enable && (ctx->password == NULL &&
|
|
|
|
ctx->enable_password == NULL)) {
|
2016-10-26 11:41:36 +02:00
|
|
|
session->is_enable = TRUE;
|
2015-05-05 16:28:36 +02:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
2016-11-19 17:33:29 +01:00
|
|
|
|
2015-08-30 13:35:18 +02:00
|
|
|
msg_info_session ("absent password has been specified");
|
2015-04-24 01:12:11 +02:00
|
|
|
ret = FALSE;
|
2015-04-24 16:18:57 +02:00
|
|
|
}
|
|
|
|
else {
|
2016-10-17 11:25:37 +02:00
|
|
|
if (rspamd_ftok_cstr_equal (password, "q1", FALSE) ||
|
|
|
|
rspamd_ftok_cstr_equal (password, "q2", FALSE)) {
|
|
|
|
msg_info_session ("deny default password for remote access");
|
|
|
|
ret = FALSE;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
2015-04-24 16:18:57 +02:00
|
|
|
if (is_enable) {
|
|
|
|
/* For privileged commands we strictly require enable password */
|
|
|
|
if (ctx->enable_password != NULL) {
|
|
|
|
check = ctx->enable_password;
|
2015-09-22 10:25:14 +02:00
|
|
|
use_enable = TRUE;
|
2015-04-24 16:18:57 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* Use just a password (legacy mode) */
|
|
|
|
msg_info(
|
|
|
|
"using password as enable_password for a privileged command");
|
|
|
|
check = ctx->password;
|
|
|
|
}
|
2016-06-01 22:25:25 +02:00
|
|
|
|
2015-04-24 16:18:57 +02:00
|
|
|
if (check != NULL) {
|
|
|
|
if (!rspamd_is_encrypted_password (check, &pbkdf)) {
|
2016-06-01 22:25:25 +02:00
|
|
|
ret = FALSE;
|
|
|
|
|
|
|
|
if (strlen (check) == password->len) {
|
|
|
|
ret = rspamd_constant_memcmp (password->begin, check,
|
|
|
|
password->len);
|
|
|
|
}
|
2015-04-24 16:18:57 +02:00
|
|
|
}
|
|
|
|
else {
|
2015-08-30 13:35:18 +02:00
|
|
|
ret = rspamd_check_encrypted_password (ctx, password, check,
|
2015-09-22 10:25:14 +02:00
|
|
|
pbkdf, use_enable);
|
2015-04-24 16:18:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2015-08-30 13:35:18 +02:00
|
|
|
msg_warn_session (
|
2015-04-24 16:18:57 +02:00
|
|
|
"no password to check while executing a privileged command");
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
2016-10-26 11:41:36 +02:00
|
|
|
|
|
|
|
if (ret) {
|
|
|
|
session->is_enable = TRUE;
|
|
|
|
}
|
2015-04-24 16:18:57 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* Accept both normal and enable passwords */
|
|
|
|
if (ctx->password != NULL) {
|
|
|
|
check = ctx->password;
|
2016-06-01 22:25:25 +02:00
|
|
|
|
2015-04-24 16:18:57 +02:00
|
|
|
if (!rspamd_is_encrypted_password (check, &pbkdf)) {
|
2016-06-01 22:25:25 +02:00
|
|
|
check_normal = FALSE;
|
|
|
|
|
|
|
|
if (strlen (check) == password->len) {
|
|
|
|
check_normal = rspamd_constant_memcmp (password->begin,
|
|
|
|
check,
|
|
|
|
password->len);
|
|
|
|
}
|
2015-04-24 16:18:57 +02:00
|
|
|
}
|
|
|
|
else {
|
2015-08-30 13:35:18 +02:00
|
|
|
check_normal = rspamd_check_encrypted_password (ctx,
|
|
|
|
password,
|
2015-09-22 10:25:14 +02:00
|
|
|
check, pbkdf, FALSE);
|
2015-04-24 16:18:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
check_normal = FALSE;
|
|
|
|
}
|
2016-06-01 22:25:25 +02:00
|
|
|
|
2015-04-24 16:18:57 +02:00
|
|
|
if (ctx->enable_password != NULL) {
|
|
|
|
check = ctx->enable_password;
|
2016-06-01 22:25:25 +02:00
|
|
|
|
2015-04-24 16:18:57 +02:00
|
|
|
if (!rspamd_is_encrypted_password (check, &pbkdf)) {
|
2016-06-01 22:25:25 +02:00
|
|
|
check_enable = FALSE;
|
|
|
|
|
|
|
|
if (strlen (check) == password->len) {
|
|
|
|
check_enable = rspamd_constant_memcmp (password->begin,
|
|
|
|
check,
|
|
|
|
password->len);
|
|
|
|
}
|
2015-04-24 16:18:57 +02:00
|
|
|
}
|
|
|
|
else {
|
2015-08-30 13:35:18 +02:00
|
|
|
check_enable = rspamd_check_encrypted_password (ctx,
|
|
|
|
password,
|
2015-09-22 10:25:14 +02:00
|
|
|
check, pbkdf, TRUE);
|
2015-04-24 16:18:57 +02:00
|
|
|
}
|
2016-10-26 11:41:36 +02:00
|
|
|
|
|
|
|
if (check_enable) {
|
|
|
|
session->is_enable = TRUE;
|
|
|
|
}
|
2015-04-24 16:18:57 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
check_enable = FALSE;
|
2018-10-17 18:44:52 +02:00
|
|
|
|
|
|
|
if (check_normal) {
|
|
|
|
/*
|
|
|
|
* If no enable password is specified use normal password as
|
|
|
|
* enable password
|
|
|
|
*/
|
|
|
|
session->is_enable = TRUE;
|
|
|
|
}
|
2015-04-24 16:18:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-24 01:12:11 +02:00
|
|
|
if (check_normal == FALSE && check_enable == FALSE) {
|
2016-06-01 22:25:25 +02:00
|
|
|
msg_info ("absent or incorrect password has been specified");
|
2015-04-24 16:18:57 +02:00
|
|
|
ret = FALSE;
|
2015-04-24 01:12:11 +02:00
|
|
|
}
|
2015-04-24 16:18:57 +02:00
|
|
|
|
2016-10-17 11:25:37 +02:00
|
|
|
end:
|
|
|
|
if (query_args != NULL) {
|
|
|
|
g_hash_table_unref (query_args);
|
|
|
|
}
|
|
|
|
|
2014-04-30 18:23:37 +02:00
|
|
|
if (!ret) {
|
|
|
|
rspamd_controller_send_error (entry, 403, "Unauthorized");
|
|
|
|
}
|
2008-10-23 17:10:40 +02:00
|
|
|
|
2014-04-30 18:23:37 +02:00
|
|
|
return ret;
|
|
|
|
}
|
2009-01-11 15:31:26 +01:00
|
|
|
|
2014-04-30 18:23:37 +02:00
|
|
|
/* Command handlers */
|
2009-09-22 18:22:31 +02:00
|
|
|
|
2014-04-30 18:23:37 +02:00
|
|
|
/*
|
|
|
|
* Auth command handler:
|
|
|
|
* request: /auth
|
|
|
|
* headers: Password
|
|
|
|
* reply: json {"auth": "ok", "version": "0.5.2", "uptime": "some uptime", "error": "none"}
|
|
|
|
*/
|
|
|
|
static int
|
2014-04-30 18:23:58 +02:00
|
|
|
rspamd_controller_handle_auth (struct rspamd_http_connection_entry *conn_ent,
|
2014-07-23 13:57:31 +02:00
|
|
|
struct rspamd_http_message *msg)
|
2008-10-23 17:10:40 +02:00
|
|
|
{
|
2014-07-23 13:57:31 +02:00
|
|
|
struct rspamd_controller_session *session = conn_ent->ud;
|
|
|
|
struct rspamd_stat *st;
|
|
|
|
int64_t uptime;
|
2017-01-13 15:47:46 +01:00
|
|
|
gulong data[5];
|
2014-07-23 13:57:31 +02:00
|
|
|
ucl_object_t *obj;
|
2009-10-02 15:09:38 +02:00
|
|
|
|
2014-04-30 18:23:58 +02:00
|
|
|
if (!rspamd_controller_check_password (conn_ent, session, msg, FALSE)) {
|
2014-04-30 18:23:37 +02:00
|
|
|
return 0;
|
2009-01-21 15:25:06 +01:00
|
|
|
}
|
|
|
|
|
2014-04-30 18:23:37 +02:00
|
|
|
obj = ucl_object_typed_new (UCL_OBJECT);
|
|
|
|
st = session->ctx->srv->stat;
|
|
|
|
data[0] = st->actions_stat[METRIC_ACTION_NOACTION];
|
2014-07-23 13:57:31 +02:00
|
|
|
data[1] = st->actions_stat[METRIC_ACTION_ADD_HEADER] +
|
|
|
|
st->actions_stat[METRIC_ACTION_REWRITE_SUBJECT];
|
2014-04-30 18:23:37 +02:00
|
|
|
data[2] = st->actions_stat[METRIC_ACTION_GREYLIST];
|
|
|
|
data[3] = st->actions_stat[METRIC_ACTION_REJECT];
|
2017-01-13 15:47:46 +01:00
|
|
|
data[4] = st->actions_stat[METRIC_ACTION_SOFT_REJECT];
|
2014-04-30 18:23:37 +02:00
|
|
|
|
|
|
|
/* Get uptime */
|
2019-06-19 18:07:56 +02:00
|
|
|
uptime = ev_time () - session->ctx->start_time;
|
2014-04-30 18:23:37 +02:00
|
|
|
|
2014-07-23 13:57:31 +02:00
|
|
|
ucl_object_insert_key (obj, ucl_object_fromstring (
|
|
|
|
RVERSION), "version", 0, false);
|
|
|
|
ucl_object_insert_key (obj, ucl_object_fromstring (
|
|
|
|
"ok"), "auth", 0, false);
|
|
|
|
ucl_object_insert_key (obj, ucl_object_fromint (
|
|
|
|
uptime), "uptime", 0, false);
|
|
|
|
ucl_object_insert_key (obj, ucl_object_fromint (
|
|
|
|
data[0]), "clean", 0, false);
|
|
|
|
ucl_object_insert_key (obj, ucl_object_fromint (
|
|
|
|
data[1]), "probable", 0, false);
|
|
|
|
ucl_object_insert_key (obj, ucl_object_fromint (
|
|
|
|
data[2]), "greylist", 0, false);
|
|
|
|
ucl_object_insert_key (obj, ucl_object_fromint (
|
|
|
|
data[3]), "reject", 0, false);
|
2017-01-13 15:47:46 +01:00
|
|
|
ucl_object_insert_key (obj, ucl_object_fromint (
|
|
|
|
data[4]), "soft_reject", 0, false);
|
2014-07-23 13:57:31 +02:00
|
|
|
ucl_object_insert_key (obj, ucl_object_fromint (
|
|
|
|
st->messages_scanned), "scanned", 0, false);
|
|
|
|
ucl_object_insert_key (obj, ucl_object_fromint (
|
|
|
|
st->messages_learned), "learned", 0, false);
|
2016-11-14 13:51:19 +01:00
|
|
|
ucl_object_insert_key (obj, ucl_object_frombool (!session->is_enable),
|
|
|
|
"read_only", 0, false);
|
2017-01-06 13:41:35 +01:00
|
|
|
ucl_object_insert_key (obj, ucl_object_fromstring (session->ctx->cfg->checksum),
|
|
|
|
"config_id", 0, false);
|
2012-09-14 18:59:23 +02:00
|
|
|
|
2014-04-30 18:23:37 +02:00
|
|
|
rspamd_controller_send_ucl (conn_ent, obj);
|
|
|
|
ucl_object_unref (obj);
|
2009-08-19 15:56:47 +02:00
|
|
|
|
2014-04-30 18:23:37 +02:00
|
|
|
return 0;
|
2008-10-23 17:10:40 +02:00
|
|
|
}
|
|
|
|
|
2014-04-30 18:23:37 +02:00
|
|
|
/*
|
|
|
|
* Symbols command handler:
|
|
|
|
* request: /symbols
|
|
|
|
* reply: json [{
|
2014-07-23 13:57:31 +02:00
|
|
|
* "name": "group_name",
|
|
|
|
* "symbols": [
|
|
|
|
* {
|
|
|
|
* "name": "name",
|
|
|
|
* "weight": 0.1,
|
|
|
|
* "description": "description of symbol"
|
|
|
|
* },
|
|
|
|
* {...}
|
2014-04-30 18:23:37 +02:00
|
|
|
* },
|
|
|
|
* {...}]
|
|
|
|
*/
|
|
|
|
static int
|
2014-04-30 18:23:58 +02:00
|
|
|
rspamd_controller_handle_symbols (struct rspamd_http_connection_entry *conn_ent,
|
2014-07-23 13:57:31 +02:00
|
|
|
struct rspamd_http_message *msg)
|
2012-09-16 18:11:19 +02:00
|
|
|
{
|
2014-07-23 13:57:31 +02:00
|
|
|
struct rspamd_controller_session *session = conn_ent->ud;
|
2015-08-13 16:28:07 +02:00
|
|
|
GHashTableIter it, sit;
|
2014-07-23 13:57:31 +02:00
|
|
|
struct rspamd_symbols_group *gr;
|
2016-12-05 14:27:46 +01:00
|
|
|
struct rspamd_symbol *sym;
|
2015-05-15 15:56:31 +02:00
|
|
|
ucl_object_t *obj, *top, *sym_obj, *group_symbols;
|
2015-04-14 18:02:15 +02:00
|
|
|
gpointer k, v;
|
2012-09-16 18:11:19 +02:00
|
|
|
|
2014-04-30 18:23:58 +02:00
|
|
|
if (!rspamd_controller_check_password (conn_ent, session, msg, FALSE)) {
|
2014-04-30 18:23:37 +02:00
|
|
|
return 0;
|
2012-09-16 18:11:19 +02:00
|
|
|
}
|
|
|
|
|
2014-04-30 18:23:37 +02:00
|
|
|
top = ucl_object_typed_new (UCL_ARRAY);
|
|
|
|
|
2015-08-13 16:28:07 +02:00
|
|
|
/* Go through all symbols groups in the default metric */
|
2017-11-04 16:42:15 +01:00
|
|
|
g_hash_table_iter_init (&it, session->cfg->groups);
|
2015-08-13 16:28:07 +02:00
|
|
|
|
2015-04-14 18:02:15 +02:00
|
|
|
while (g_hash_table_iter_next (&it, &k, &v)) {
|
|
|
|
gr = v;
|
2014-04-30 18:23:37 +02:00
|
|
|
obj = ucl_object_typed_new (UCL_OBJECT);
|
2014-07-23 13:57:31 +02:00
|
|
|
ucl_object_insert_key (obj, ucl_object_fromstring (
|
|
|
|
gr->name), "group", 0, false);
|
2014-04-30 18:23:37 +02:00
|
|
|
/* Iterate through all symbols */
|
2015-04-14 18:02:15 +02:00
|
|
|
|
2015-08-13 16:28:07 +02:00
|
|
|
g_hash_table_iter_init (&sit, gr->symbols);
|
2015-05-15 15:56:31 +02:00
|
|
|
group_symbols = ucl_object_typed_new (UCL_ARRAY);
|
|
|
|
|
2015-08-13 16:28:07 +02:00
|
|
|
while (g_hash_table_iter_next (&sit, &k, &v)) {
|
2017-01-28 13:27:52 +01:00
|
|
|
gdouble tm = 0.0, freq = 0, freq_dev = 0;
|
2016-11-01 13:26:43 +01:00
|
|
|
|
2015-08-13 16:28:07 +02:00
|
|
|
sym = v;
|
2014-04-30 18:23:37 +02:00
|
|
|
sym_obj = ucl_object_typed_new (UCL_OBJECT);
|
|
|
|
|
2015-08-13 16:28:07 +02:00
|
|
|
ucl_object_insert_key (sym_obj, ucl_object_fromstring (sym->name),
|
2014-07-23 13:57:31 +02:00
|
|
|
"symbol", 0, false);
|
|
|
|
ucl_object_insert_key (sym_obj,
|
2015-08-13 16:28:07 +02:00
|
|
|
ucl_object_fromdouble (*sym->weight_ptr),
|
2014-07-23 13:57:31 +02:00
|
|
|
"weight", 0, false);
|
2015-08-13 16:28:07 +02:00
|
|
|
if (sym->description) {
|
2014-07-23 13:57:31 +02:00
|
|
|
ucl_object_insert_key (sym_obj,
|
2015-08-13 16:28:07 +02:00
|
|
|
ucl_object_fromstring (sym->description),
|
2014-04-30 18:23:37 +02:00
|
|
|
"description", 0, false);
|
|
|
|
}
|
|
|
|
|
2018-10-29 13:22:46 +01:00
|
|
|
if (rspamd_symcache_stat_symbol (session->ctx->cfg->cache,
|
2017-06-20 09:47:45 +02:00
|
|
|
sym->name, &freq, &freq_dev, &tm, NULL)) {
|
2016-11-01 13:26:43 +01:00
|
|
|
ucl_object_insert_key (sym_obj,
|
2017-01-24 16:47:51 +01:00
|
|
|
ucl_object_fromdouble (freq),
|
2016-11-01 13:26:43 +01:00
|
|
|
"frequency", 0, false);
|
2017-01-28 13:27:52 +01:00
|
|
|
ucl_object_insert_key (sym_obj,
|
|
|
|
ucl_object_fromdouble (freq_dev),
|
|
|
|
"frequency_stddev", 0, false);
|
2016-11-01 13:26:43 +01:00
|
|
|
ucl_object_insert_key (sym_obj,
|
|
|
|
ucl_object_fromdouble (tm),
|
|
|
|
"time", 0, false);
|
|
|
|
}
|
|
|
|
|
2015-05-15 15:56:31 +02:00
|
|
|
ucl_array_append (group_symbols, sym_obj);
|
2012-09-16 18:11:19 +02:00
|
|
|
}
|
2015-04-14 18:02:15 +02:00
|
|
|
|
2015-05-15 15:56:31 +02:00
|
|
|
ucl_object_insert_key (obj, group_symbols, "rules", 0, false);
|
2014-04-30 18:23:37 +02:00
|
|
|
ucl_array_append (top, obj);
|
2012-09-16 18:11:19 +02:00
|
|
|
}
|
|
|
|
|
2014-04-30 18:23:37 +02:00
|
|
|
rspamd_controller_send_ucl (conn_ent, top);
|
|
|
|
ucl_object_unref (top);
|
|
|
|
|
|
|
|
return 0;
|
2012-09-16 18:11:19 +02:00
|
|
|
}
|
|
|
|
|
2014-04-30 18:23:37 +02:00
|
|
|
/*
|
|
|
|
* Actions command handler:
|
|
|
|
* request: /actions
|
|
|
|
* reply: json [{
|
2014-07-23 13:57:31 +02:00
|
|
|
* "action": "no action",
|
|
|
|
* "value": 1.1
|
2014-04-30 18:23:37 +02:00
|
|
|
* },
|
|
|
|
* {...}]
|
|
|
|
*/
|
|
|
|
static int
|
2014-04-30 18:23:58 +02:00
|
|
|
rspamd_controller_handle_actions (struct rspamd_http_connection_entry *conn_ent,
|
2014-07-23 13:57:31 +02:00
|
|
|
struct rspamd_http_message *msg)
|
2013-12-23 16:09:20 +01:00
|
|
|
{
|
2014-07-23 13:57:31 +02:00
|
|
|
struct rspamd_controller_session *session = conn_ent->ud;
|
2019-01-14 17:48:52 +01:00
|
|
|
struct rspamd_action *act, *tmp;
|
2014-07-23 13:57:31 +02:00
|
|
|
ucl_object_t *obj, *top;
|
2013-12-23 16:09:20 +01:00
|
|
|
|
2014-04-30 18:23:58 +02:00
|
|
|
if (!rspamd_controller_check_password (conn_ent, session, msg, FALSE)) {
|
2014-04-30 18:23:37 +02:00
|
|
|
return 0;
|
2013-12-23 16:09:20 +01:00
|
|
|
}
|
|
|
|
|
2014-04-30 18:23:37 +02:00
|
|
|
top = ucl_object_typed_new (UCL_ARRAY);
|
|
|
|
|
2019-01-14 17:48:52 +01:00
|
|
|
HASH_ITER (hh, session->cfg->actions, act, tmp) {
|
2018-03-19 17:48:04 +01:00
|
|
|
obj = ucl_object_typed_new (UCL_OBJECT);
|
|
|
|
ucl_object_insert_key (obj,
|
2019-01-14 17:48:52 +01:00
|
|
|
ucl_object_fromstring (act->name),
|
|
|
|
"action", 0, false);
|
|
|
|
ucl_object_insert_key (obj,
|
|
|
|
ucl_object_fromdouble (act->threshold),
|
|
|
|
"value", 0, false);
|
2018-03-19 17:48:04 +01:00
|
|
|
ucl_array_append (top, obj);
|
2013-12-23 16:09:20 +01:00
|
|
|
}
|
|
|
|
|
2014-04-30 18:23:37 +02:00
|
|
|
rspamd_controller_send_ucl (conn_ent, top);
|
|
|
|
ucl_object_unref (top);
|
2013-12-23 16:09:20 +01:00
|
|
|
|
2014-04-30 18:23:37 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2017-01-09 18:15:36 +01:00
|
|
|
|
|
|
|
static gboolean
|
|
|
|
rspamd_controller_can_edit_map (struct rspamd_map_backend *bk)
|
|
|
|
{
|
|
|
|
gchar *fpath;
|
|
|
|
|
|
|
|
if (access (bk->uri, W_OK) == 0) {
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
else if (access (bk->uri, R_OK) == -1 && errno == ENOENT) {
|
|
|
|
fpath = g_path_get_dirname (bk->uri);
|
|
|
|
|
|
|
|
if (fpath) {
|
|
|
|
if (access (fpath, W_OK) == 0) {
|
|
|
|
g_free (fpath);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_free (fpath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2014-04-30 18:23:37 +02:00
|
|
|
/*
|
|
|
|
* Maps command handler:
|
|
|
|
* request: /maps
|
|
|
|
* headers: Password
|
|
|
|
* reply: json [
|
2014-07-23 13:57:31 +02:00
|
|
|
* {
|
|
|
|
* "map": "name",
|
|
|
|
* "description": "description",
|
|
|
|
* "editable": true
|
|
|
|
* },
|
|
|
|
* {...}
|
2014-04-30 18:23:37 +02:00
|
|
|
* ]
|
|
|
|
*/
|
|
|
|
static int
|
2014-04-30 18:23:58 +02:00
|
|
|
rspamd_controller_handle_maps (struct rspamd_http_connection_entry *conn_ent,
|
2014-07-23 13:57:31 +02:00
|
|
|
struct rspamd_http_message *msg)
|
2008-10-23 17:10:40 +02:00
|
|
|
{
|
2014-07-23 13:57:31 +02:00
|
|
|
struct rspamd_controller_session *session = conn_ent->ud;
|
2017-01-09 18:15:36 +01:00
|
|
|
GList *cur;
|
2014-07-23 13:57:31 +02:00
|
|
|
struct rspamd_map *map;
|
2016-05-09 19:59:46 +02:00
|
|
|
struct rspamd_map_backend *bk;
|
2017-01-09 18:15:36 +01:00
|
|
|
guint i;
|
2014-07-23 13:57:31 +02:00
|
|
|
gboolean editable;
|
|
|
|
ucl_object_t *obj, *top;
|
2008-10-23 17:10:40 +02:00
|
|
|
|
2014-04-30 18:23:58 +02:00
|
|
|
if (!rspamd_controller_check_password (conn_ent, session, msg, FALSE)) {
|
2014-04-30 18:23:37 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
top = ucl_object_typed_new (UCL_ARRAY);
|
2017-01-09 18:15:36 +01:00
|
|
|
|
2014-04-30 18:23:37 +02:00
|
|
|
/* Iterate over all maps */
|
|
|
|
cur = session->ctx->cfg->maps;
|
|
|
|
while (cur) {
|
|
|
|
map = cur->data;
|
2016-05-09 19:59:46 +02:00
|
|
|
|
2017-01-09 18:15:36 +01:00
|
|
|
PTR_ARRAY_FOREACH (map->backends, i, bk) {
|
2014-04-30 18:23:37 +02:00
|
|
|
|
2017-01-09 18:15:36 +01:00
|
|
|
if (bk->protocol == MAP_PROTO_FILE) {
|
|
|
|
editable = rspamd_controller_can_edit_map (bk);
|
2014-04-30 18:23:37 +02:00
|
|
|
|
2017-01-09 18:15:36 +01:00
|
|
|
if (!editable && access (bk->uri, R_OK) == -1) {
|
|
|
|
/* Skip unreadable and non-existing maps */
|
|
|
|
continue;
|
|
|
|
}
|
2014-04-30 18:23:37 +02:00
|
|
|
|
2017-01-09 18:15:36 +01:00
|
|
|
obj = ucl_object_typed_new (UCL_OBJECT);
|
|
|
|
ucl_object_insert_key (obj, ucl_object_fromint (bk->id),
|
|
|
|
"map", 0, false);
|
|
|
|
if (map->description) {
|
|
|
|
ucl_object_insert_key (obj, ucl_object_fromstring (map->description),
|
|
|
|
"description", 0, false);
|
|
|
|
}
|
|
|
|
ucl_object_insert_key (obj, ucl_object_fromstring (bk->uri),
|
|
|
|
"uri", 0, false);
|
|
|
|
ucl_object_insert_key (obj, ucl_object_frombool (editable),
|
|
|
|
"editable", 0, false);
|
|
|
|
ucl_array_append (top, obj);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cur = g_list_next (cur);
|
2008-10-23 17:10:40 +02:00
|
|
|
}
|
|
|
|
|
2014-04-30 18:23:37 +02:00
|
|
|
rspamd_controller_send_ucl (conn_ent, top);
|
|
|
|
ucl_object_unref (top);
|
|
|
|
|
|
|
|
return 0;
|
2008-10-23 17:10:40 +02:00
|
|
|
}
|
|
|
|
|
2014-04-30 18:23:37 +02:00
|
|
|
/*
|
|
|
|
* Get map command handler:
|
|
|
|
* request: /getmap
|
|
|
|
* headers: Password, Map
|
|
|
|
* reply: plain-text
|
|
|
|
*/
|
|
|
|
static int
|
2014-04-30 18:23:58 +02:00
|
|
|
rspamd_controller_handle_get_map (struct rspamd_http_connection_entry *conn_ent,
|
2014-07-23 13:57:31 +02:00
|
|
|
struct rspamd_http_message *msg)
|
2009-11-27 17:15:48 +01:00
|
|
|
{
|
2014-07-23 13:57:31 +02:00
|
|
|
struct rspamd_controller_session *session = conn_ent->ud;
|
|
|
|
GList *cur;
|
|
|
|
struct rspamd_map *map;
|
2019-06-19 18:07:56 +02:00
|
|
|
struct rspamd_map_backend *bk = NULL;
|
2015-10-07 16:58:57 +02:00
|
|
|
const rspamd_ftok_t *idstr;
|
2014-07-23 13:57:31 +02:00
|
|
|
struct stat st;
|
|
|
|
gint fd;
|
2017-01-09 18:15:36 +01:00
|
|
|
gulong id, i;
|
2014-07-23 13:57:31 +02:00
|
|
|
gboolean found = FALSE;
|
|
|
|
struct rspamd_http_message *reply;
|
2014-04-30 18:23:37 +02:00
|
|
|
|
2014-04-30 18:23:58 +02:00
|
|
|
if (!rspamd_controller_check_password (conn_ent, session, msg, FALSE)) {
|
2014-04-30 18:23:37 +02:00
|
|
|
return 0;
|
2009-11-27 17:15:48 +01:00
|
|
|
}
|
2009-12-01 22:45:37 +01:00
|
|
|
|
2014-04-30 18:23:37 +02:00
|
|
|
idstr = rspamd_http_message_find_header (msg, "Map");
|
|
|
|
|
|
|
|
if (idstr == NULL) {
|
2015-08-30 13:35:18 +02:00
|
|
|
msg_info_session ("absent map id");
|
2016-09-21 00:05:07 +02:00
|
|
|
rspamd_controller_send_error (conn_ent, 400, "Id header missing");
|
2014-04-30 18:23:37 +02:00
|
|
|
return 0;
|
2010-06-24 16:25:16 +02:00
|
|
|
}
|
2009-12-01 22:45:37 +01:00
|
|
|
|
2015-10-07 16:58:57 +02:00
|
|
|
if (!rspamd_strtoul (idstr->begin, idstr->len, &id)) {
|
2015-08-30 13:35:18 +02:00
|
|
|
msg_info_session ("invalid map id");
|
2016-09-21 00:05:07 +02:00
|
|
|
rspamd_controller_send_error (conn_ent, 400, "Invalid map id");
|
2014-04-30 18:23:37 +02:00
|
|
|
return 0;
|
2009-11-27 17:15:48 +01:00
|
|
|
}
|
|
|
|
|
2014-04-30 18:23:37 +02:00
|
|
|
/* Now let's be sure that we have map defined in configuration */
|
|
|
|
cur = session->ctx->cfg->maps;
|
2017-01-09 18:15:36 +01:00
|
|
|
while (cur && !found) {
|
2014-04-30 18:23:37 +02:00
|
|
|
map = cur->data;
|
2017-01-09 18:15:36 +01:00
|
|
|
|
|
|
|
PTR_ARRAY_FOREACH (map->backends, i, bk) {
|
|
|
|
if (bk->id == id && bk->protocol == MAP_PROTO_FILE) {
|
|
|
|
found = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
2009-11-27 17:15:48 +01:00
|
|
|
}
|
2017-01-09 18:15:36 +01:00
|
|
|
|
2009-11-27 17:15:48 +01:00
|
|
|
cur = g_list_next (cur);
|
|
|
|
}
|
2014-04-30 18:23:37 +02:00
|
|
|
|
2019-06-19 18:07:56 +02:00
|
|
|
if (!found || bk == NULL) {
|
2015-08-30 13:35:18 +02:00
|
|
|
msg_info_session ("map not found");
|
2016-09-21 00:05:07 +02:00
|
|
|
rspamd_controller_send_error (conn_ent, 404, "Map not found");
|
2014-04-30 18:23:37 +02:00
|
|
|
return 0;
|
2009-11-27 17:15:48 +01:00
|
|
|
}
|
|
|
|
|
2016-05-09 19:59:46 +02:00
|
|
|
if (stat (bk->uri, &st) == -1 || (fd = open (bk->uri, O_RDONLY)) == -1) {
|
2017-01-09 18:15:36 +01:00
|
|
|
reply = rspamd_http_new_message (HTTP_RESPONSE);
|
|
|
|
reply->date = time (NULL);
|
|
|
|
reply->code = 200;
|
2009-11-27 17:15:48 +01:00
|
|
|
}
|
2017-01-09 18:15:36 +01:00
|
|
|
else {
|
2009-11-27 17:15:48 +01:00
|
|
|
|
2017-01-09 18:15:36 +01:00
|
|
|
reply = rspamd_http_new_message (HTTP_RESPONSE);
|
|
|
|
reply->date = time (NULL);
|
|
|
|
reply->code = 200;
|
|
|
|
|
2018-10-13 15:23:00 +02:00
|
|
|
if (st.st_size > 0) {
|
|
|
|
if (!rspamd_http_message_set_body_from_fd (reply, fd)) {
|
|
|
|
close (fd);
|
|
|
|
rspamd_http_message_unref (reply);
|
|
|
|
msg_err_session ("cannot read map %s: %s", bk->uri, strerror (errno));
|
|
|
|
rspamd_controller_send_error (conn_ent, 500, "Map read error");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rspamd_fstring_t *empty_body = rspamd_fstring_new_init ("", 0);
|
|
|
|
rspamd_http_message_set_body_from_fstring_steal (reply, empty_body);
|
2017-01-09 18:15:36 +01:00
|
|
|
}
|
2014-04-30 18:23:37 +02:00
|
|
|
|
2015-02-05 19:20:22 +01:00
|
|
|
close (fd);
|
2009-11-27 17:15:48 +01:00
|
|
|
}
|
|
|
|
|
2014-04-30 18:23:37 +02:00
|
|
|
rspamd_http_connection_reset (conn_ent->conn);
|
2018-08-12 18:05:54 +02:00
|
|
|
rspamd_http_router_insert_headers (conn_ent->rt, reply);
|
2014-04-30 18:23:37 +02:00
|
|
|
rspamd_http_connection_write_message (conn_ent->conn, reply, NULL,
|
2019-02-25 16:23:22 +01:00
|
|
|
"text/plain", conn_ent,
|
2019-06-19 18:07:56 +02:00
|
|
|
conn_ent->rt->timeout);
|
2014-04-30 18:23:37 +02:00
|
|
|
conn_ent->is_reply = TRUE;
|
|
|
|
|
|
|
|
return 0;
|
2009-11-27 17:15:48 +01:00
|
|
|
}
|
|
|
|
|
2014-09-09 17:56:24 +02:00
|
|
|
static ucl_object_t *
|
2017-11-04 16:42:15 +01:00
|
|
|
rspamd_controller_pie_element (enum rspamd_action_type action,
|
2014-11-04 14:52:53 +01:00
|
|
|
const char *label, gdouble data)
|
2013-06-07 15:52:25 +02:00
|
|
|
{
|
2014-09-09 17:56:24 +02:00
|
|
|
ucl_object_t *res = ucl_object_typed_new (UCL_OBJECT);
|
2014-11-04 14:52:53 +01:00
|
|
|
const char *colors[METRIC_ACTION_MAX] = {
|
2015-09-23 15:15:49 +02:00
|
|
|
[METRIC_ACTION_REJECT] = "#FF0000",
|
2014-11-08 19:31:18 +01:00
|
|
|
[METRIC_ACTION_SOFT_REJECT] = "#cc9966",
|
|
|
|
[METRIC_ACTION_REWRITE_SUBJECT] = "#ff6600",
|
2015-09-23 15:15:49 +02:00
|
|
|
[METRIC_ACTION_ADD_HEADER] = "#FFD700",
|
|
|
|
[METRIC_ACTION_GREYLIST] = "#436EEE",
|
2014-11-08 19:31:18 +01:00
|
|
|
[METRIC_ACTION_NOACTION] = "#66cc00"
|
2014-11-04 14:52:53 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
ucl_object_insert_key (res, ucl_object_fromstring (colors[action]),
|
|
|
|
"color", 0, false);
|
2014-09-09 17:56:24 +02:00
|
|
|
ucl_object_insert_key (res, ucl_object_fromstring (label), "label", 0, false);
|
|
|
|
ucl_object_insert_key (res, ucl_object_fromdouble (data), "data", 0, false);
|
2015-09-23 14:35:42 +02:00
|
|
|
ucl_object_insert_key (res, ucl_object_fromdouble (data), "value", 0, false);
|
2013-06-07 15:52:25 +02:00
|
|
|
|
2014-09-09 17:56:24 +02:00
|
|
|
return res;
|
2014-04-30 18:23:37 +02:00
|
|
|
}
|
2014-09-09 17:56:24 +02:00
|
|
|
|
2014-04-30 18:23:37 +02:00
|
|
|
/*
|
|
|
|
* Pie chart command handler:
|
|
|
|
* request: /pie
|
|
|
|
* headers: Password
|
|
|
|
* reply: json [
|
2014-07-23 13:57:31 +02:00
|
|
|
* { label: "Foo", data: 11 },
|
|
|
|
* { label: "Bar", data: 20 },
|
|
|
|
* {...}
|
2014-04-30 18:23:37 +02:00
|
|
|
* ]
|
|
|
|
*/
|
|
|
|
static int
|
2014-07-23 13:57:31 +02:00
|
|
|
rspamd_controller_handle_pie_chart (
|
|
|
|
struct rspamd_http_connection_entry *conn_ent,
|
|
|
|
struct rspamd_http_message *msg)
|
2012-10-02 17:44:49 +02:00
|
|
|
{
|
2014-07-23 13:57:31 +02:00
|
|
|
struct rspamd_controller_session *session = conn_ent->ud;
|
|
|
|
struct rspamd_controller_worker_ctx *ctx;
|
2014-11-04 14:52:53 +01:00
|
|
|
gdouble data[5], total;
|
2014-09-09 17:56:24 +02:00
|
|
|
ucl_object_t *top;
|
2014-04-30 18:23:37 +02:00
|
|
|
|
|
|
|
ctx = session->ctx;
|
|
|
|
|
2014-04-30 18:23:58 +02:00
|
|
|
if (!rspamd_controller_check_password (conn_ent, session, msg, FALSE)) {
|
2014-04-30 18:23:37 +02:00
|
|
|
return 0;
|
2012-10-02 17:44:49 +02:00
|
|
|
}
|
|
|
|
|
2014-04-30 18:23:37 +02:00
|
|
|
top = ucl_object_typed_new (UCL_ARRAY);
|
|
|
|
total = ctx->srv->stat->messages_scanned;
|
|
|
|
if (total != 0) {
|
|
|
|
|
2015-09-23 20:31:11 +02:00
|
|
|
data[0] = ctx->srv->stat->actions_stat[METRIC_ACTION_NOACTION];
|
|
|
|
data[1] = ctx->srv->stat->actions_stat[METRIC_ACTION_SOFT_REJECT];
|
2014-11-04 14:52:53 +01:00
|
|
|
data[2] = (ctx->srv->stat->actions_stat[METRIC_ACTION_ADD_HEADER] +
|
2015-09-23 14:35:42 +02:00
|
|
|
ctx->srv->stat->actions_stat[METRIC_ACTION_REWRITE_SUBJECT]);
|
|
|
|
data[3] = ctx->srv->stat->actions_stat[METRIC_ACTION_GREYLIST];
|
|
|
|
data[4] = ctx->srv->stat->actions_stat[METRIC_ACTION_REJECT];
|
2012-10-02 17:44:49 +02:00
|
|
|
}
|
|
|
|
else {
|
2014-09-09 17:56:24 +02:00
|
|
|
memset (data, 0, sizeof (data));
|
2012-10-02 17:44:49 +02:00
|
|
|
}
|
2014-11-04 14:52:53 +01:00
|
|
|
ucl_array_append (top, rspamd_controller_pie_element (
|
|
|
|
METRIC_ACTION_NOACTION, "Clean", data[0]));
|
|
|
|
ucl_array_append (top, rspamd_controller_pie_element (
|
2017-03-27 09:46:56 +02:00
|
|
|
METRIC_ACTION_SOFT_REJECT, "Temporarily rejected", data[1]));
|
2014-11-04 14:52:53 +01:00
|
|
|
ucl_array_append (top, rspamd_controller_pie_element (
|
|
|
|
METRIC_ACTION_ADD_HEADER, "Probable spam", data[2]));
|
|
|
|
ucl_array_append (top, rspamd_controller_pie_element (
|
|
|
|
METRIC_ACTION_GREYLIST, "Greylisted", data[3]));
|
|
|
|
ucl_array_append (top, rspamd_controller_pie_element (
|
|
|
|
METRIC_ACTION_REJECT, "Rejected", data[4]));
|
2014-04-30 18:23:37 +02:00
|
|
|
|
|
|
|
rspamd_controller_send_ucl (conn_ent, top);
|
|
|
|
ucl_object_unref (top);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-07-02 14:08:42 +02:00
|
|
|
void
|
|
|
|
rspamd_controller_graph_point (gulong t, gulong step,
|
|
|
|
struct rspamd_rrd_query_result* rrd_result,
|
|
|
|
gdouble *acc,
|
|
|
|
ucl_object_t **elt)
|
|
|
|
{
|
|
|
|
guint nan_cnt;
|
|
|
|
gdouble sum = 0.0, yval;
|
|
|
|
ucl_object_t* data_elt;
|
|
|
|
guint i, j;
|
|
|
|
|
|
|
|
for (i = 0; i < rrd_result->ds_count; i++) {
|
|
|
|
sum = 0.0;
|
|
|
|
nan_cnt = 0;
|
|
|
|
data_elt = ucl_object_typed_new (UCL_OBJECT);
|
|
|
|
ucl_object_insert_key (data_elt, ucl_object_fromint (t), "x", 1, false);
|
|
|
|
|
|
|
|
for (j = 0; j < step; j++) {
|
|
|
|
yval = acc[i + j * rrd_result->ds_count];
|
2017-02-10 11:27:31 +01:00
|
|
|
if (!isfinite (yval)) {
|
2016-07-02 14:08:42 +02:00
|
|
|
nan_cnt++;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
sum += yval;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (nan_cnt == step) {
|
|
|
|
ucl_object_insert_key (data_elt, ucl_object_typed_new (UCL_NULL),
|
|
|
|
"y", 1, false);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ucl_object_insert_key (data_elt,
|
|
|
|
ucl_object_fromdouble (sum / (gdouble) step), "y", 1,
|
|
|
|
false);
|
|
|
|
}
|
|
|
|
ucl_array_append (elt[i], data_elt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-21 20:14:24 +02:00
|
|
|
/*
|
|
|
|
* Graph command handler:
|
|
|
|
* request: /graph?type=<hourly|daily|weekly|monthly>
|
|
|
|
* headers: Password
|
|
|
|
* reply: json [
|
|
|
|
* { label: "Foo", data: 11 },
|
|
|
|
* { label: "Bar", data: 20 },
|
|
|
|
* {...}
|
|
|
|
* ]
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
rspamd_controller_handle_graph (
|
|
|
|
struct rspamd_http_connection_entry *conn_ent,
|
|
|
|
struct rspamd_http_message *msg)
|
|
|
|
{
|
|
|
|
GHashTable *query;
|
|
|
|
struct rspamd_controller_session *session = conn_ent->ud;
|
|
|
|
struct rspamd_controller_worker_ctx *ctx;
|
2015-10-07 16:58:57 +02:00
|
|
|
rspamd_ftok_t srch, *value;
|
2015-09-21 20:14:24 +02:00
|
|
|
struct rspamd_rrd_query_result *rrd_result;
|
2016-07-20 15:53:19 +02:00
|
|
|
gulong i, k, start_row, cnt, t, ts, step;
|
2016-07-02 14:08:42 +02:00
|
|
|
gdouble *acc;
|
2017-01-03 17:44:38 +01:00
|
|
|
ucl_object_t *res, *elt[METRIC_ACTION_MAX];
|
2015-09-21 20:14:24 +02:00
|
|
|
enum {
|
|
|
|
rra_hourly = 0,
|
|
|
|
rra_daily,
|
|
|
|
rra_weekly,
|
|
|
|
rra_monthly,
|
|
|
|
rra_invalid
|
|
|
|
} rra_num = rra_invalid;
|
2016-07-02 14:08:42 +02:00
|
|
|
/* How many points are we going to send to display */
|
|
|
|
static const guint desired_points = 500;
|
2015-09-21 20:14:24 +02:00
|
|
|
|
|
|
|
ctx = session->ctx;
|
|
|
|
|
|
|
|
if (!rspamd_controller_check_password (conn_ent, session, msg, FALSE)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ctx->rrd == NULL) {
|
|
|
|
msg_err_session ("no rrd configured");
|
2016-09-21 00:05:07 +02:00
|
|
|
rspamd_controller_send_error (conn_ent, 404, "No rrd configured for graphs");
|
2015-09-21 20:14:24 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
query = rspamd_http_message_parse_query (msg);
|
2015-10-07 16:58:57 +02:00
|
|
|
srch.begin = (gchar *)"type";
|
2015-09-21 20:14:24 +02:00
|
|
|
srch.len = 4;
|
|
|
|
|
|
|
|
if (query == NULL || (value = g_hash_table_lookup (query, &srch)) == NULL) {
|
|
|
|
msg_err_session ("absent graph type query");
|
2016-09-21 00:05:07 +02:00
|
|
|
rspamd_controller_send_error (conn_ent, 400, "Absent graph type");
|
2015-09-21 20:14:24 +02:00
|
|
|
|
|
|
|
if (query) {
|
|
|
|
g_hash_table_unref (query);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-07-01 18:36:55 +02:00
|
|
|
if (value->len == 6 && rspamd_lc_cmp (value->begin, "hourly", value->len) == 0) {
|
2015-09-21 20:14:24 +02:00
|
|
|
rra_num = rra_hourly;
|
|
|
|
}
|
2016-07-01 18:36:55 +02:00
|
|
|
else if (value->len == 5 && rspamd_lc_cmp (value->begin, "daily", value->len) == 0) {
|
|
|
|
rra_num = rra_daily;
|
2015-09-21 20:14:24 +02:00
|
|
|
}
|
2016-07-01 18:36:55 +02:00
|
|
|
else if (value->len == 6 && rspamd_lc_cmp (value->begin, "weekly", value->len) == 0) {
|
|
|
|
rra_num = rra_weekly;
|
2015-09-21 20:14:24 +02:00
|
|
|
}
|
2016-07-01 18:36:55 +02:00
|
|
|
else if (value->len == 7 && rspamd_lc_cmp (value->begin, "monthly", value->len) == 0) {
|
2015-09-21 20:14:24 +02:00
|
|
|
rra_num = rra_monthly;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_hash_table_unref (query);
|
|
|
|
|
|
|
|
if (rra_num == rra_invalid) {
|
|
|
|
msg_err_session ("invalid graph type query");
|
2016-09-21 00:05:07 +02:00
|
|
|
rspamd_controller_send_error (conn_ent, 400, "Invalid graph type");
|
2015-09-21 20:14:24 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
rrd_result = rspamd_rrd_query (ctx->rrd, rra_num);
|
|
|
|
|
|
|
|
if (rrd_result == NULL) {
|
|
|
|
msg_err_session ("cannot query rrd");
|
2016-09-21 00:05:07 +02:00
|
|
|
rspamd_controller_send_error (conn_ent, 500, "Cannot query rrd");
|
2015-09-21 20:14:24 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_assert (rrd_result->ds_count == G_N_ELEMENTS (elt));
|
|
|
|
|
|
|
|
res = ucl_object_typed_new (UCL_ARRAY);
|
|
|
|
/* How much full updates happened since the last update */
|
2015-09-22 12:58:15 +02:00
|
|
|
ts = rrd_result->last_update / rrd_result->pdp_per_cdp - rrd_result->rra_rows;
|
2015-09-21 20:14:24 +02:00
|
|
|
|
|
|
|
for (i = 0; i < rrd_result->ds_count; i ++) {
|
|
|
|
elt[i] = ucl_object_typed_new (UCL_ARRAY);
|
|
|
|
}
|
|
|
|
|
|
|
|
start_row = rrd_result->cur_row == rrd_result->rra_rows - 1 ?
|
|
|
|
0 : rrd_result->cur_row;
|
2016-07-05 13:57:58 +02:00
|
|
|
t = ts * rrd_result->pdp_per_cdp;
|
|
|
|
k = 0;
|
2015-09-21 20:14:24 +02:00
|
|
|
|
2016-07-02 14:08:42 +02:00
|
|
|
/* Create window */
|
2017-01-15 21:23:35 +01:00
|
|
|
step = ceil (((gdouble)rrd_result->rra_rows) / desired_points);
|
2016-07-20 15:53:19 +02:00
|
|
|
g_assert (step >= 1);
|
2016-07-02 14:08:42 +02:00
|
|
|
acc = g_malloc0 (sizeof (double) * rrd_result->ds_count * step);
|
|
|
|
|
2016-07-02 14:18:18 +02:00
|
|
|
for (i = start_row, cnt = 0; cnt < rrd_result->rra_rows;
|
|
|
|
cnt ++) {
|
2016-07-02 14:08:42 +02:00
|
|
|
|
2016-07-20 15:53:19 +02:00
|
|
|
memcpy (&acc[k * rrd_result->ds_count],
|
|
|
|
&rrd_result->data[i * rrd_result->ds_count],
|
|
|
|
sizeof (gdouble) * rrd_result->ds_count);
|
|
|
|
|
|
|
|
if (k < step - 1) {
|
|
|
|
k ++;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
t = ts * rrd_result->pdp_per_cdp;
|
|
|
|
|
|
|
|
/* Need a fresh point */
|
|
|
|
rspamd_controller_graph_point (t, step, rrd_result, acc, elt);
|
|
|
|
k = 0;
|
2015-09-21 20:14:24 +02:00
|
|
|
}
|
|
|
|
|
2016-07-02 14:18:18 +02:00
|
|
|
if (i == rrd_result->rra_rows - 1) {
|
|
|
|
i = 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
i ++;
|
|
|
|
}
|
|
|
|
|
2015-09-22 12:58:15 +02:00
|
|
|
ts ++;
|
2015-09-21 20:14:24 +02:00
|
|
|
}
|
|
|
|
|
2016-07-02 14:08:42 +02:00
|
|
|
if (k > 0) {
|
|
|
|
rspamd_controller_graph_point (t, k, rrd_result, acc, elt);
|
|
|
|
}
|
|
|
|
|
2015-09-21 20:14:24 +02:00
|
|
|
for (i = 0; i < rrd_result->ds_count; i++) {
|
|
|
|
ucl_array_append (res, elt[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
rspamd_controller_send_ucl (conn_ent, res);
|
|
|
|
ucl_object_unref (res);
|
2016-07-02 14:08:42 +02:00
|
|
|
g_free (acc);
|
2015-09-21 20:14:24 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-03-14 14:29:18 +01:00
|
|
|
static void
|
|
|
|
rspamd_controller_handle_legacy_history (
|
|
|
|
struct rspamd_controller_session *session,
|
|
|
|
struct rspamd_controller_worker_ctx *ctx,
|
|
|
|
struct rspamd_http_connection_entry *conn_ent,
|
|
|
|
struct rspamd_http_message *msg)
|
2014-04-30 18:23:37 +02:00
|
|
|
{
|
2015-10-24 23:30:56 +02:00
|
|
|
struct roll_history_row *row, *copied_rows;
|
|
|
|
guint i, rows_proc, row_num;
|
2018-02-18 14:35:59 +01:00
|
|
|
struct tm tm;
|
2018-07-18 13:40:43 +02:00
|
|
|
gchar timebuf[32], **syms;
|
2014-07-23 13:57:31 +02:00
|
|
|
ucl_object_t *top, *obj;
|
2014-04-30 18:23:37 +02:00
|
|
|
|
|
|
|
top = ucl_object_typed_new (UCL_ARRAY);
|
2012-10-02 17:44:49 +02:00
|
|
|
|
2014-04-30 18:23:37 +02:00
|
|
|
/* Set lock on history */
|
2017-10-27 12:21:40 +02:00
|
|
|
copied_rows = g_malloc (sizeof (*copied_rows) * ctx->srv->history->nrows);
|
2015-10-24 23:30:56 +02:00
|
|
|
memcpy (copied_rows, ctx->srv->history->rows,
|
|
|
|
sizeof (*copied_rows) * ctx->srv->history->nrows);
|
2014-04-30 18:23:37 +02:00
|
|
|
|
|
|
|
/* Go through all rows */
|
2015-10-24 23:30:56 +02:00
|
|
|
row_num = ctx->srv->history->cur_row;
|
|
|
|
|
|
|
|
for (i = 0, rows_proc = 0; i < ctx->srv->history->nrows; i++, row_num++) {
|
|
|
|
if (row_num == ctx->srv->history->nrows) {
|
2014-04-30 18:23:37 +02:00
|
|
|
row_num = 0;
|
2012-10-02 17:44:49 +02:00
|
|
|
}
|
2015-10-24 23:30:56 +02:00
|
|
|
row = &copied_rows[row_num];
|
2014-04-30 18:23:37 +02:00
|
|
|
/* Get only completed rows */
|
|
|
|
if (row->completed) {
|
2019-06-19 18:07:56 +02:00
|
|
|
rspamd_localtime (row->timestamp, &tm);
|
2018-02-18 14:35:59 +01:00
|
|
|
strftime (timebuf, sizeof (timebuf) - 1, "%Y-%m-%d %H:%M:%S", &tm);
|
2014-04-30 18:23:37 +02:00
|
|
|
obj = ucl_object_typed_new (UCL_OBJECT);
|
2014-07-23 13:57:31 +02:00
|
|
|
ucl_object_insert_key (obj, ucl_object_fromstring (
|
|
|
|
timebuf), "time", 0, false);
|
2015-10-02 15:42:40 +02:00
|
|
|
ucl_object_insert_key (obj, ucl_object_fromint (
|
2019-06-19 18:07:56 +02:00
|
|
|
row->timestamp), "unix_time", 0, false);
|
2014-07-23 13:57:31 +02:00
|
|
|
ucl_object_insert_key (obj, ucl_object_fromstring (
|
|
|
|
row->message_id), "id", 0, false);
|
2015-03-12 15:18:57 +01:00
|
|
|
ucl_object_insert_key (obj, ucl_object_fromstring (row->from_addr),
|
2014-10-31 12:46:48 +01:00
|
|
|
"ip", 0, false);
|
2014-07-23 13:57:31 +02:00
|
|
|
ucl_object_insert_key (obj,
|
2017-03-14 14:29:18 +01:00
|
|
|
ucl_object_fromstring (rspamd_action_to_str (
|
|
|
|
row->action)), "action", 0, false);
|
2016-07-29 18:10:44 +02:00
|
|
|
|
|
|
|
if (!isnan (row->score)) {
|
|
|
|
ucl_object_insert_key (obj, ucl_object_fromdouble (
|
|
|
|
row->score), "score", 0, false);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ucl_object_insert_key (obj,
|
|
|
|
ucl_object_fromdouble (0.0), "score", 0, false);
|
|
|
|
}
|
2016-07-29 17:30:31 +02:00
|
|
|
|
|
|
|
if (!isnan (row->required_score)) {
|
|
|
|
ucl_object_insert_key (obj,
|
|
|
|
ucl_object_fromdouble (
|
|
|
|
row->required_score), "required_score", 0, false);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ucl_object_insert_key (obj,
|
|
|
|
ucl_object_fromdouble (0.0), "required_score", 0, false);
|
|
|
|
}
|
|
|
|
|
2018-07-18 15:16:42 +02:00
|
|
|
syms = g_strsplit_set (row->symbols, ", ", -1);
|
2018-07-18 13:40:43 +02:00
|
|
|
|
|
|
|
if (syms) {
|
|
|
|
guint nelts = g_strv_length (syms);
|
|
|
|
ucl_object_t *syms_obj = ucl_object_typed_new (UCL_OBJECT);
|
|
|
|
ucl_object_reserve (syms_obj, nelts);
|
|
|
|
|
|
|
|
for (guint j = 0; j < nelts; j++) {
|
2018-07-18 15:16:42 +02:00
|
|
|
g_strstrip (syms[j]);
|
|
|
|
|
|
|
|
if (strlen (syms[j]) == 0) {
|
|
|
|
/* Empty garbadge */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-07-18 13:40:43 +02:00
|
|
|
ucl_object_t *cur = ucl_object_typed_new (UCL_OBJECT);
|
|
|
|
|
|
|
|
ucl_object_insert_key (cur, ucl_object_fromdouble (0.0),
|
|
|
|
"score", 0, false);
|
|
|
|
ucl_object_insert_key (syms_obj, cur, syms[j], 0, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
ucl_object_insert_key (obj, syms_obj, "symbols", 0, false);
|
|
|
|
g_strfreev (syms);
|
|
|
|
}
|
|
|
|
|
|
|
|
ucl_object_insert_key (obj, ucl_object_fromint (row->len),
|
|
|
|
"size", 0, false);
|
|
|
|
ucl_object_insert_key (obj,
|
|
|
|
ucl_object_fromdouble (row->scan_time),
|
|
|
|
"scan_time", 0, false);
|
|
|
|
|
2014-04-30 18:23:37 +02:00
|
|
|
if (row->user[0] != '\0') {
|
2018-07-18 13:40:43 +02:00
|
|
|
ucl_object_insert_key (obj, ucl_object_fromstring (row->user),
|
|
|
|
"user", 0, false);
|
2014-04-30 18:23:37 +02:00
|
|
|
}
|
2015-10-24 23:30:56 +02:00
|
|
|
if (row->from_addr[0] != '\0') {
|
|
|
|
ucl_object_insert_key (obj, ucl_object_fromstring (
|
|
|
|
row->from_addr), "from", 0, false);
|
|
|
|
}
|
2014-04-30 18:23:37 +02:00
|
|
|
ucl_array_append (top, obj);
|
2014-07-23 13:57:31 +02:00
|
|
|
rows_proc++;
|
2012-10-02 17:44:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-30 18:23:37 +02:00
|
|
|
rspamd_controller_send_ucl (conn_ent, top);
|
|
|
|
ucl_object_unref (top);
|
2017-10-27 12:21:40 +02:00
|
|
|
g_free (copied_rows);
|
2017-03-14 14:29:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
rspamd_controller_history_lua_fin_task (void *ud)
|
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
rspamd_controller_handle_lua_history (lua_State *L,
|
|
|
|
struct rspamd_controller_session *session,
|
|
|
|
struct rspamd_controller_worker_ctx *ctx,
|
|
|
|
struct rspamd_http_connection_entry *conn_ent,
|
|
|
|
struct rspamd_http_message *msg,
|
|
|
|
gboolean reset)
|
|
|
|
{
|
|
|
|
struct rspamd_task *task, **ptask;
|
|
|
|
struct rspamd_http_connection_entry **pconn_ent;
|
|
|
|
GHashTable *params;
|
|
|
|
rspamd_ftok_t srch, *found;
|
|
|
|
glong from = 0, to = -1;
|
|
|
|
|
|
|
|
params = rspamd_http_message_parse_query (msg);
|
|
|
|
|
|
|
|
if (params) {
|
|
|
|
/* Check from and to */
|
|
|
|
RSPAMD_FTOK_ASSIGN (&srch, "from");
|
|
|
|
found = g_hash_table_lookup (params, &srch);
|
|
|
|
|
|
|
|
if (found) {
|
|
|
|
rspamd_strtol (found->begin, found->len, &from);
|
|
|
|
}
|
|
|
|
RSPAMD_FTOK_ASSIGN (&srch, "to");
|
|
|
|
found = g_hash_table_lookup (params, &srch);
|
|
|
|
|
|
|
|
if (found) {
|
|
|
|
rspamd_strtol (found->begin, found->len, &to);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_hash_table_unref (params);
|
|
|
|
}
|
|
|
|
|
|
|
|
lua_getglobal (L, "rspamd_plugins");
|
|
|
|
|
|
|
|
if (lua_istable (L, -1)) {
|
|
|
|
lua_pushstring (L, "history");
|
|
|
|
lua_gettable (L, -2);
|
|
|
|
|
|
|
|
if (lua_istable (L, -1)) {
|
|
|
|
lua_pushstring (L, "handler");
|
|
|
|
lua_gettable (L, -2);
|
|
|
|
|
|
|
|
if (lua_isfunction (L, -1)) {
|
2017-06-08 20:16:56 +02:00
|
|
|
task = rspamd_task_new (session->ctx->worker, session->cfg,
|
2019-06-19 15:13:39 +02:00
|
|
|
session->pool, ctx->lang_det, ctx->event_loop);
|
2017-03-14 14:29:18 +01:00
|
|
|
|
|
|
|
task->resolver = ctx->resolver;
|
|
|
|
task->s = rspamd_session_create (session->pool,
|
|
|
|
rspamd_controller_history_lua_fin_task,
|
|
|
|
NULL,
|
|
|
|
(event_finalizer_t )rspamd_task_free,
|
|
|
|
task);
|
|
|
|
task->fin_arg = conn_ent;
|
|
|
|
|
|
|
|
ptask = lua_newuserdata (L, sizeof (*ptask));
|
|
|
|
*ptask = task;
|
|
|
|
rspamd_lua_setclass (L, "rspamd{task}", -1);
|
|
|
|
pconn_ent = lua_newuserdata (L, sizeof (*pconn_ent));
|
|
|
|
*pconn_ent = conn_ent;
|
|
|
|
rspamd_lua_setclass (L, "rspamd{csession}", -1);
|
2018-08-15 17:20:41 +02:00
|
|
|
lua_pushinteger (L, from);
|
|
|
|
lua_pushinteger (L, to);
|
2017-03-14 14:29:18 +01:00
|
|
|
lua_pushboolean (L, reset);
|
|
|
|
|
|
|
|
if (lua_pcall (L, 5, 0, 0) != 0) {
|
|
|
|
msg_err_session ("call to history function failed: %s",
|
|
|
|
lua_tostring (L, -1));
|
|
|
|
lua_settop (L, 0);
|
|
|
|
rspamd_task_free (task);
|
|
|
|
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
task->http_conn = rspamd_http_connection_ref (conn_ent->conn);;
|
|
|
|
task->sock = -1;
|
|
|
|
session->task = task;
|
|
|
|
|
|
|
|
rspamd_session_pending (task->s);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
msg_err_session ("rspamd_plugins.history.handler is not a function");
|
2017-03-15 16:54:33 +01:00
|
|
|
lua_settop (L, 0);
|
2017-03-14 14:29:18 +01:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
msg_err_session ("rspamd_plugins.history is not a table");
|
2017-03-15 16:54:33 +01:00
|
|
|
lua_settop (L, 0);
|
2017-03-14 14:29:18 +01:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
msg_err_session ("rspamd_plugins is absent or has incorrect type");
|
2017-03-15 16:54:33 +01:00
|
|
|
lua_settop (L, 0);
|
2017-03-14 14:29:18 +01:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2017-03-15 16:54:33 +01:00
|
|
|
lua_settop (L, 0);
|
2017-03-14 14:29:18 +01:00
|
|
|
|
|
|
|
return;
|
|
|
|
err:
|
|
|
|
rspamd_controller_send_error (conn_ent, 500, "Internal error");
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* History command handler:
|
|
|
|
* request: /history
|
|
|
|
* headers: Password
|
|
|
|
* reply: json [
|
|
|
|
* { label: "Foo", data: 11 },
|
|
|
|
* { label: "Bar", data: 20 },
|
|
|
|
* {...}
|
|
|
|
* ]
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
rspamd_controller_handle_history (struct rspamd_http_connection_entry *conn_ent,
|
|
|
|
struct rspamd_http_message *msg)
|
|
|
|
{
|
|
|
|
struct rspamd_controller_session *session = conn_ent->ud;
|
|
|
|
struct rspamd_controller_worker_ctx *ctx;
|
|
|
|
lua_State *L;
|
|
|
|
ctx = session->ctx;
|
|
|
|
|
|
|
|
if (!rspamd_controller_check_password (conn_ent, session, msg, FALSE)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
L = ctx->cfg->lua_state;
|
|
|
|
|
|
|
|
if (!ctx->srv->history->disabled) {
|
|
|
|
rspamd_controller_handle_legacy_history (session, ctx, conn_ent, msg);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rspamd_controller_handle_lua_history (L, session, ctx, conn_ent, msg,
|
|
|
|
FALSE);
|
|
|
|
}
|
2014-04-30 18:23:37 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-11-14 16:57:32 +01:00
|
|
|
/*
|
|
|
|
* Errors command handler:
|
|
|
|
* request: /errors
|
|
|
|
* headers: Password
|
|
|
|
* reply: json [
|
|
|
|
* { ts: 100500, type: normal, pid: 100, module: lua, message: bad things },
|
|
|
|
* {...}
|
|
|
|
* ]
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
rspamd_controller_handle_errors (struct rspamd_http_connection_entry *conn_ent,
|
|
|
|
struct rspamd_http_message *msg)
|
|
|
|
{
|
|
|
|
struct rspamd_controller_session *session = conn_ent->ud;
|
|
|
|
struct rspamd_controller_worker_ctx *ctx;
|
|
|
|
ucl_object_t *top;
|
|
|
|
|
|
|
|
ctx = session->ctx;
|
|
|
|
|
|
|
|
if (!rspamd_controller_check_password (conn_ent, session, msg, TRUE)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
top = rspamd_log_errorbuf_export (ctx->worker->srv->logger);
|
|
|
|
rspamd_controller_send_ucl (conn_ent, top);
|
|
|
|
ucl_object_unref (top);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-01-05 18:20:13 +01:00
|
|
|
/*
|
|
|
|
* Neighbours command handler:
|
|
|
|
* request: /neighbours
|
|
|
|
* headers: Password
|
|
|
|
* reply: json {name: {url: "http://...", host: "host"}}
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
rspamd_controller_handle_neighbours (struct rspamd_http_connection_entry *conn_ent,
|
|
|
|
struct rspamd_http_message *msg)
|
|
|
|
{
|
|
|
|
struct rspamd_controller_session *session = conn_ent->ud;
|
|
|
|
struct rspamd_controller_worker_ctx *ctx;
|
|
|
|
|
|
|
|
ctx = session->ctx;
|
|
|
|
|
|
|
|
if (!rspamd_controller_check_password (conn_ent, session, msg, FALSE)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
rspamd_controller_send_ucl (conn_ent, ctx->cfg->neighbours);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-10-03 15:20:55 +02:00
|
|
|
static int
|
|
|
|
rspamd_controller_handle_history_reset (struct rspamd_http_connection_entry *conn_ent,
|
|
|
|
struct rspamd_http_message *msg)
|
|
|
|
{
|
|
|
|
struct rspamd_controller_session *session = conn_ent->ud;
|
|
|
|
struct rspamd_controller_worker_ctx *ctx;
|
2015-10-24 23:30:56 +02:00
|
|
|
struct roll_history_row *row;
|
2019-05-23 16:58:56 +02:00
|
|
|
guint completed_rows, i, t;
|
2017-03-14 14:29:18 +01:00
|
|
|
lua_State *L;
|
2015-10-03 15:20:55 +02:00
|
|
|
|
|
|
|
ctx = session->ctx;
|
2017-03-14 14:29:18 +01:00
|
|
|
L = ctx->cfg->lua_state;
|
2015-10-03 15:20:55 +02:00
|
|
|
|
|
|
|
if (!rspamd_controller_check_password (conn_ent, session, msg, TRUE)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-03-14 14:29:18 +01:00
|
|
|
if (!ctx->srv->history->disabled) {
|
|
|
|
/* Clean from start to the current row */
|
2019-05-23 16:58:56 +02:00
|
|
|
completed_rows = g_atomic_int_get (&ctx->srv->history->cur_row);
|
2015-10-24 23:30:56 +02:00
|
|
|
|
2019-05-23 16:58:56 +02:00
|
|
|
completed_rows = MIN (completed_rows, ctx->srv->history->nrows - 1);
|
|
|
|
|
|
|
|
for (i = 0; i <= completed_rows; i ++) {
|
2017-03-14 14:29:18 +01:00
|
|
|
t = g_atomic_int_get (&ctx->srv->history->cur_row);
|
2015-10-24 23:30:56 +02:00
|
|
|
|
2017-03-14 14:29:18 +01:00
|
|
|
/* We somehow come to the race condition */
|
2019-05-23 16:58:56 +02:00
|
|
|
if (i > t) {
|
2017-03-14 14:29:18 +01:00
|
|
|
break;
|
|
|
|
}
|
2015-10-24 23:30:56 +02:00
|
|
|
|
2017-03-14 14:29:18 +01:00
|
|
|
row = &ctx->srv->history->rows[i];
|
|
|
|
memset (row, 0, sizeof (*row));
|
|
|
|
}
|
2015-10-24 23:30:56 +02:00
|
|
|
|
2019-05-23 16:58:56 +02:00
|
|
|
msg_info_session ("<%s> cleared %d entries from history",
|
|
|
|
rspamd_inet_address_to_string (session->from_addr),
|
|
|
|
completed_rows);
|
2017-03-14 14:29:18 +01:00
|
|
|
rspamd_controller_send_string (conn_ent, "{\"success\":true}");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rspamd_controller_handle_lua_history (L, session, ctx, conn_ent, msg,
|
|
|
|
TRUE);
|
|
|
|
}
|
2015-10-03 15:20:55 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-09-24 21:42:12 +02:00
|
|
|
static gboolean
|
|
|
|
rspamd_controller_lua_fin_task (void *ud)
|
|
|
|
{
|
|
|
|
struct rspamd_task *task = ud;
|
|
|
|
struct rspamd_http_connection_entry *conn_ent;
|
|
|
|
|
|
|
|
conn_ent = task->fin_arg;
|
|
|
|
|
|
|
|
if (task->err != NULL) {
|
|
|
|
rspamd_controller_send_error (conn_ent, task->err->code, "%s",
|
|
|
|
task->err->message);
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
rspamd_controller_handle_lua (struct rspamd_http_connection_entry *conn_ent,
|
|
|
|
struct rspamd_http_message *msg)
|
|
|
|
{
|
|
|
|
struct rspamd_controller_session *session = conn_ent->ud;
|
|
|
|
struct rspamd_task *task, **ptask;
|
|
|
|
struct rspamd_http_connection_entry **pconn;
|
|
|
|
struct rspamd_controller_worker_ctx *ctx;
|
|
|
|
gchar filebuf[PATH_MAX], realbuf[PATH_MAX];
|
|
|
|
struct http_parser_url u;
|
|
|
|
rspamd_ftok_t lookup;
|
|
|
|
struct stat st;
|
|
|
|
lua_State *L;
|
|
|
|
|
|
|
|
if (!rspamd_controller_check_password (conn_ent, session, msg, TRUE)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx = session->ctx;
|
|
|
|
L = ctx->cfg->lua_state;
|
|
|
|
|
|
|
|
/* Find lua script */
|
|
|
|
if (msg->url != NULL && msg->url->len != 0) {
|
|
|
|
|
2018-03-31 18:41:19 +02:00
|
|
|
http_parser_parse_url (RSPAMD_FSTRING_DATA (msg->url),
|
|
|
|
RSPAMD_FSTRING_LEN (msg->url), TRUE, &u);
|
2016-09-24 21:42:12 +02:00
|
|
|
|
|
|
|
if (u.field_set & (1 << UF_PATH)) {
|
2018-03-31 18:41:19 +02:00
|
|
|
lookup.begin = RSPAMD_FSTRING_DATA (msg->url) +
|
|
|
|
u.field_data[UF_PATH].off;
|
2016-09-24 21:42:12 +02:00
|
|
|
lookup.len = u.field_data[UF_PATH].len;
|
|
|
|
}
|
|
|
|
else {
|
2018-03-31 18:41:19 +02:00
|
|
|
lookup.begin = RSPAMD_FSTRING_DATA (msg->url);
|
|
|
|
lookup.len = RSPAMD_FSTRING_LEN (msg->url);
|
2016-09-24 21:42:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
rspamd_snprintf (filebuf, sizeof (filebuf), "%s%c%T",
|
2016-11-22 18:39:46 +01:00
|
|
|
ctx->static_files_dir, G_DIR_SEPARATOR, &lookup);
|
2016-09-24 21:42:12 +02:00
|
|
|
|
|
|
|
if (realpath (filebuf, realbuf) == NULL ||
|
|
|
|
lstat (realbuf, &st) == -1) {
|
|
|
|
rspamd_controller_send_error (conn_ent, 404, "Cannot find path: %s",
|
|
|
|
strerror (errno));
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* TODO: add caching here, should be trivial */
|
|
|
|
/* Now we load and execute the code fragment, which should return a function */
|
|
|
|
if (luaL_loadfile (L, realbuf) != 0) {
|
|
|
|
rspamd_controller_send_error (conn_ent, 500, "Cannot load path: %s",
|
|
|
|
lua_tostring (L, -1));
|
|
|
|
lua_settop (L, 0);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lua_pcall (L, 0, 1, 0) != 0) {
|
|
|
|
rspamd_controller_send_error (conn_ent, 501, "Cannot run path: %s",
|
|
|
|
lua_tostring (L, -1));
|
|
|
|
lua_settop (L, 0);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lua_type (L, -1) != LUA_TFUNCTION) {
|
|
|
|
rspamd_controller_send_error (conn_ent, 502, "Bad return type: %s",
|
|
|
|
lua_typename (L, lua_type (L, -1)));
|
|
|
|
lua_settop (L, 0);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rspamd_controller_send_error (conn_ent, 404, "Empty path is not permitted");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-01-01 19:58:01 +01:00
|
|
|
task = rspamd_task_new (session->ctx->worker, session->cfg, session->pool,
|
2019-06-19 15:13:39 +02:00
|
|
|
ctx->lang_det, ctx->event_loop);
|
2016-09-24 21:42:12 +02:00
|
|
|
|
|
|
|
task->resolver = ctx->resolver;
|
|
|
|
task->s = rspamd_session_create (session->pool,
|
|
|
|
rspamd_controller_lua_fin_task,
|
|
|
|
NULL,
|
|
|
|
(event_finalizer_t )rspamd_task_free,
|
|
|
|
task);
|
|
|
|
task->fin_arg = conn_ent;
|
|
|
|
task->http_conn = rspamd_http_connection_ref (conn_ent->conn);;
|
|
|
|
task->sock = -1;
|
|
|
|
session->task = task;
|
|
|
|
|
|
|
|
if (msg->body_buf.len > 0) {
|
|
|
|
if (!rspamd_task_load_message (task, msg, msg->body_buf.begin, msg->body_buf.len)) {
|
|
|
|
rspamd_controller_send_error (conn_ent, task->err->code, "%s",
|
|
|
|
task->err->message);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ptask = lua_newuserdata (L, sizeof (*ptask));
|
|
|
|
rspamd_lua_setclass (L, "rspamd{task}", -1);
|
|
|
|
*ptask = task;
|
|
|
|
|
|
|
|
pconn = lua_newuserdata (L, sizeof (*pconn));
|
|
|
|
rspamd_lua_setclass (L, "rspamd{csession}", -1);
|
|
|
|
*pconn = conn_ent;
|
|
|
|
|
|
|
|
if (lua_pcall (L, 2, 0, 0) != 0) {
|
|
|
|
rspamd_controller_send_error (conn_ent, 503, "Cannot run callback: %s",
|
|
|
|
lua_tostring (L, -1));
|
|
|
|
lua_settop (L, 0);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
rspamd_session_pending (task->s);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-04-30 18:23:37 +02:00
|
|
|
static gboolean
|
2014-04-30 18:23:58 +02:00
|
|
|
rspamd_controller_learn_fin_task (void *ud)
|
2014-04-30 18:23:37 +02:00
|
|
|
{
|
2014-07-23 13:57:31 +02:00
|
|
|
struct rspamd_task *task = ud;
|
|
|
|
struct rspamd_controller_session *session;
|
|
|
|
struct rspamd_http_connection_entry *conn_ent;
|
2014-04-30 18:23:37 +02:00
|
|
|
|
|
|
|
conn_ent = task->fin_arg;
|
|
|
|
session = conn_ent->ud;
|
|
|
|
|
2016-01-06 16:08:48 +01:00
|
|
|
if (task->err != NULL) {
|
2019-07-12 16:11:45 +02:00
|
|
|
msg_info_session ("cannot learn <%s>: %e",
|
|
|
|
MESSAGE_FIELD (task, message_id), task->err);
|
2016-09-21 00:05:07 +02:00
|
|
|
rspamd_controller_send_error (conn_ent, task->err->code, "%s",
|
2016-01-06 16:08:48 +01:00
|
|
|
task->err->message);
|
2015-07-06 21:07:42 +02:00
|
|
|
|
2014-04-30 18:23:37 +02:00
|
|
|
return TRUE;
|
2012-10-02 17:44:49 +02:00
|
|
|
}
|
2015-10-03 15:20:55 +02:00
|
|
|
|
2016-01-06 16:08:48 +01:00
|
|
|
if (RSPAMD_TASK_IS_PROCESSED (task)) {
|
|
|
|
/* Successful learn */
|
2016-11-14 18:12:49 +01:00
|
|
|
msg_info_task ("<%s> learned message as %s: %s",
|
2016-01-06 16:08:48 +01:00
|
|
|
rspamd_inet_address_to_string (session->from_addr),
|
|
|
|
session->is_spam ? "spam" : "ham",
|
2019-07-12 16:11:45 +02:00
|
|
|
MESSAGE_FIELD (task, message_id));
|
2016-01-06 16:08:48 +01:00
|
|
|
rspamd_controller_send_string (conn_ent, "{\"success\":true}");
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!rspamd_task_process (task, RSPAMD_TASK_PROCESS_LEARN)) {
|
2019-07-12 16:11:45 +02:00
|
|
|
msg_info_task ("cannot learn <%s>: %e",
|
|
|
|
MESSAGE_FIELD (task, message_id), task->err);
|
2016-01-06 16:08:48 +01:00
|
|
|
|
|
|
|
if (task->err) {
|
2016-09-21 00:05:07 +02:00
|
|
|
rspamd_controller_send_error (conn_ent, task->err->code, "%s",
|
2016-01-06 16:08:48 +01:00
|
|
|
task->err->message);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rspamd_controller_send_error (conn_ent, 500,
|
|
|
|
"Internal error");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (RSPAMD_TASK_IS_PROCESSED (task)) {
|
2016-01-09 22:57:36 +01:00
|
|
|
if (task->err) {
|
2016-09-21 00:05:07 +02:00
|
|
|
rspamd_controller_send_error (conn_ent, task->err->code, "%s",
|
2016-01-09 22:57:36 +01:00
|
|
|
task->err->message);
|
|
|
|
}
|
|
|
|
else {
|
2016-11-14 18:12:49 +01:00
|
|
|
msg_info_task ("<%s> learned message as %s: %s",
|
2016-01-09 22:57:36 +01:00
|
|
|
rspamd_inet_address_to_string (session->from_addr),
|
|
|
|
session->is_spam ? "spam" : "ham",
|
2019-07-12 16:11:45 +02:00
|
|
|
MESSAGE_FIELD (task, message_id));
|
2016-01-09 22:57:36 +01:00
|
|
|
rspamd_controller_send_string (conn_ent, "{\"success\":true}");
|
|
|
|
}
|
|
|
|
|
2016-01-06 16:08:48 +01:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* One more iteration */
|
|
|
|
return FALSE;
|
2012-10-02 17:44:49 +02:00
|
|
|
}
|
|
|
|
|
2015-09-15 10:09:07 +02:00
|
|
|
static void
|
|
|
|
rspamd_controller_scan_reply (struct rspamd_task *task)
|
|
|
|
{
|
|
|
|
struct rspamd_http_message *msg;
|
2017-06-08 12:09:58 +02:00
|
|
|
struct rspamd_http_connection_entry *conn_ent;
|
2015-09-15 10:09:07 +02:00
|
|
|
|
|
|
|
conn_ent = task->fin_arg;
|
|
|
|
msg = rspamd_http_new_message (HTTP_RESPONSE);
|
|
|
|
msg->date = time (NULL);
|
|
|
|
msg->code = 200;
|
2017-05-02 18:27:51 +02:00
|
|
|
rspamd_protocol_http_reply (msg, task, NULL);
|
2015-09-15 10:09:07 +02:00
|
|
|
rspamd_http_connection_reset (conn_ent->conn);
|
2018-08-12 17:55:01 +02:00
|
|
|
rspamd_http_router_insert_headers (conn_ent->rt, msg);
|
2015-09-15 10:09:07 +02:00
|
|
|
rspamd_http_connection_write_message (conn_ent->conn, msg, NULL,
|
2019-06-19 18:07:56 +02:00
|
|
|
"application/json", conn_ent, conn_ent->rt->timeout);
|
2015-09-15 10:09:07 +02:00
|
|
|
conn_ent->is_reply = TRUE;
|
|
|
|
}
|
|
|
|
|
2010-06-24 16:25:16 +02:00
|
|
|
static gboolean
|
2014-04-30 18:23:58 +02:00
|
|
|
rspamd_controller_check_fin_task (void *ud)
|
2008-10-23 17:10:40 +02:00
|
|
|
{
|
2014-07-23 13:57:31 +02:00
|
|
|
struct rspamd_task *task = ud;
|
2017-06-08 12:09:58 +02:00
|
|
|
struct rspamd_http_connection_entry *conn_ent;
|
2011-07-12 18:46:55 +02:00
|
|
|
|
2015-09-15 10:09:07 +02:00
|
|
|
msg_debug_task ("finish task");
|
2017-06-08 12:09:58 +02:00
|
|
|
conn_ent = task->fin_arg;
|
|
|
|
|
|
|
|
if (task->err) {
|
2019-07-12 16:11:45 +02:00
|
|
|
msg_info_task ("cannot check <%s>: %e",
|
|
|
|
MESSAGE_FIELD (task, message_id), task->err);
|
2017-06-08 12:09:58 +02:00
|
|
|
rspamd_controller_send_error (conn_ent, task->err->code, "%s",
|
|
|
|
task->err->message);
|
|
|
|
return TRUE;
|
|
|
|
}
|
2015-09-15 10:09:07 +02:00
|
|
|
|
|
|
|
if (RSPAMD_TASK_IS_PROCESSED (task)) {
|
|
|
|
rspamd_controller_scan_reply (task);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!rspamd_task_process (task, RSPAMD_TASK_PROCESS_ALL)) {
|
|
|
|
rspamd_controller_scan_reply (task);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (RSPAMD_TASK_IS_PROCESSED (task)) {
|
|
|
|
rspamd_controller_scan_reply (task);
|
2015-06-02 16:34:43 +02:00
|
|
|
return TRUE;
|
|
|
|
}
|
2011-07-12 18:46:55 +02:00
|
|
|
|
2015-06-02 16:34:43 +02:00
|
|
|
/* One more iteration */
|
|
|
|
return FALSE;
|
2014-04-30 18:23:37 +02:00
|
|
|
}
|
2011-07-12 18:46:55 +02:00
|
|
|
|
2014-04-30 18:23:37 +02:00
|
|
|
static int
|
2014-07-23 13:57:31 +02:00
|
|
|
rspamd_controller_handle_learn_common (
|
|
|
|
struct rspamd_http_connection_entry *conn_ent,
|
|
|
|
struct rspamd_http_message *msg,
|
|
|
|
gboolean is_spam)
|
2014-04-30 18:23:37 +02:00
|
|
|
{
|
2014-07-23 13:57:31 +02:00
|
|
|
struct rspamd_controller_session *session = conn_ent->ud;
|
|
|
|
struct rspamd_controller_worker_ctx *ctx;
|
|
|
|
struct rspamd_task *task;
|
2015-11-23 19:36:41 +01:00
|
|
|
const rspamd_ftok_t *cl_header;
|
2011-07-12 18:46:55 +02:00
|
|
|
|
2014-04-30 18:23:37 +02:00
|
|
|
ctx = session->ctx;
|
2009-10-02 15:09:38 +02:00
|
|
|
|
2014-04-30 18:23:58 +02:00
|
|
|
if (!rspamd_controller_check_password (conn_ent, session, msg, TRUE)) {
|
2014-04-30 18:23:37 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2010-03-01 16:37:06 +01:00
|
|
|
|
2016-06-10 17:38:29 +02:00
|
|
|
if (rspamd_http_message_get_body (msg, NULL) == NULL) {
|
2015-08-30 13:35:18 +02:00
|
|
|
msg_err_session ("got zero length body, cannot continue");
|
2014-07-23 13:57:31 +02:00
|
|
|
rspamd_controller_send_error (conn_ent,
|
|
|
|
400,
|
|
|
|
"Empty body is not permitted");
|
2014-04-30 18:23:37 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2010-03-01 16:37:06 +01:00
|
|
|
|
2018-01-01 19:58:01 +01:00
|
|
|
task = rspamd_task_new (session->ctx->worker, session->cfg, session->pool,
|
2019-06-19 15:13:39 +02:00
|
|
|
session->ctx->lang_det, ctx->event_loop);
|
2014-04-30 18:23:37 +02:00
|
|
|
|
|
|
|
task->resolver = ctx->resolver;
|
2015-05-26 15:00:03 +02:00
|
|
|
task->s = rspamd_session_create (session->pool,
|
2014-07-23 13:57:31 +02:00
|
|
|
rspamd_controller_learn_fin_task,
|
|
|
|
NULL,
|
2015-11-18 17:01:05 +01:00
|
|
|
(event_finalizer_t )rspamd_task_free,
|
2014-07-23 13:57:31 +02:00
|
|
|
task);
|
2014-04-30 18:23:37 +02:00
|
|
|
task->fin_arg = conn_ent;
|
2014-05-01 00:09:26 +02:00
|
|
|
task->http_conn = rspamd_http_connection_ref (conn_ent->conn);;
|
2015-09-22 10:52:24 +02:00
|
|
|
task->sock = -1;
|
|
|
|
session->task = task;
|
2014-05-01 00:09:26 +02:00
|
|
|
|
2015-11-23 19:36:41 +01:00
|
|
|
cl_header = rspamd_http_message_find_header (msg, "classifier");
|
|
|
|
if (cl_header) {
|
|
|
|
session->classifier = rspamd_mempool_ftokdup (session->pool, cl_header);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
session->classifier = NULL;
|
|
|
|
}
|
2014-04-30 18:23:37 +02:00
|
|
|
|
2015-10-07 18:00:06 +02:00
|
|
|
if (!rspamd_task_load_message (task, msg, msg->body_buf.begin, msg->body_buf.len)) {
|
2017-06-08 12:09:58 +02:00
|
|
|
goto end;
|
2015-06-02 16:34:43 +02:00
|
|
|
}
|
|
|
|
|
2016-01-06 16:08:48 +01:00
|
|
|
rspamd_learn_task_spam (task, is_spam, session->classifier, NULL);
|
|
|
|
|
2015-06-02 16:34:43 +02:00
|
|
|
if (!rspamd_task_process (task, RSPAMD_TASK_PROCESS_LEARN)) {
|
2019-07-12 16:11:45 +02:00
|
|
|
msg_warn_session ("<%s> message cannot be processed",
|
|
|
|
MESSAGE_FIELD (task, message_id));
|
2017-06-08 12:09:58 +02:00
|
|
|
goto end;
|
2014-04-30 18:23:37 +02:00
|
|
|
}
|
|
|
|
|
2017-06-08 12:09:58 +02:00
|
|
|
end:
|
2014-04-30 18:23:37 +02:00
|
|
|
session->is_spam = is_spam;
|
2015-05-26 15:00:03 +02:00
|
|
|
rspamd_session_pending (task->s);
|
2014-04-30 18:23:37 +02:00
|
|
|
|
|
|
|
return 0;
|
2008-10-23 17:10:40 +02:00
|
|
|
}
|
|
|
|
|
2014-04-30 18:23:37 +02:00
|
|
|
/*
|
|
|
|
* Learn spam command handler:
|
|
|
|
* request: /learnspam
|
|
|
|
* headers: Password
|
|
|
|
* input: plaintext data
|
|
|
|
* reply: json {"success":true} or {"error":"error message"}
|
|
|
|
*/
|
|
|
|
static int
|
2014-07-23 13:57:31 +02:00
|
|
|
rspamd_controller_handle_learnspam (
|
|
|
|
struct rspamd_http_connection_entry *conn_ent,
|
|
|
|
struct rspamd_http_message *msg)
|
2009-07-29 18:17:29 +02:00
|
|
|
{
|
2014-04-30 18:23:58 +02:00
|
|
|
return rspamd_controller_handle_learn_common (conn_ent, msg, TRUE);
|
2014-04-30 18:23:37 +02:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Learn ham command handler:
|
|
|
|
* request: /learnham
|
|
|
|
* headers: Password
|
|
|
|
* input: plaintext data
|
|
|
|
* reply: json {"success":true} or {"error":"error message"}
|
|
|
|
*/
|
|
|
|
static int
|
2014-07-23 13:57:31 +02:00
|
|
|
rspamd_controller_handle_learnham (
|
|
|
|
struct rspamd_http_connection_entry *conn_ent,
|
|
|
|
struct rspamd_http_message *msg)
|
2014-04-30 18:23:37 +02:00
|
|
|
{
|
2014-09-02 16:04:14 +02:00
|
|
|
return rspamd_controller_handle_learn_common (conn_ent, msg, FALSE);
|
2014-04-30 18:23:37 +02:00
|
|
|
}
|
2009-07-29 18:17:29 +02:00
|
|
|
|
2014-04-30 18:23:37 +02:00
|
|
|
/*
|
|
|
|
* Scan command handler:
|
|
|
|
* request: /scan
|
|
|
|
* headers: Password
|
|
|
|
* input: plaintext data
|
|
|
|
* reply: json {scan data} or {"error":"error message"}
|
|
|
|
*/
|
|
|
|
static int
|
2014-04-30 18:23:58 +02:00
|
|
|
rspamd_controller_handle_scan (struct rspamd_http_connection_entry *conn_ent,
|
2014-07-23 13:57:31 +02:00
|
|
|
struct rspamd_http_message *msg)
|
2014-04-30 18:23:37 +02:00
|
|
|
{
|
2014-07-23 13:57:31 +02:00
|
|
|
struct rspamd_controller_session *session = conn_ent->ud;
|
|
|
|
struct rspamd_controller_worker_ctx *ctx;
|
|
|
|
struct rspamd_task *task;
|
2014-04-30 18:23:37 +02:00
|
|
|
|
|
|
|
ctx = session->ctx;
|
|
|
|
|
2014-04-30 18:23:58 +02:00
|
|
|
if (!rspamd_controller_check_password (conn_ent, session, msg, FALSE)) {
|
2014-04-30 18:23:37 +02:00
|
|
|
return 0;
|
2012-09-14 18:59:23 +02:00
|
|
|
}
|
2014-04-30 18:23:37 +02:00
|
|
|
|
2016-06-10 17:38:29 +02:00
|
|
|
if (rspamd_http_message_get_body (msg, NULL) == NULL) {
|
2015-08-30 13:35:18 +02:00
|
|
|
msg_err_session ("got zero length body, cannot continue");
|
2014-07-23 13:57:31 +02:00
|
|
|
rspamd_controller_send_error (conn_ent,
|
|
|
|
400,
|
|
|
|
"Empty body is not permitted");
|
2014-04-30 18:23:37 +02:00
|
|
|
return 0;
|
2009-07-29 18:17:29 +02:00
|
|
|
}
|
|
|
|
|
2018-01-01 19:58:01 +01:00
|
|
|
task = rspamd_task_new (session->ctx->worker, session->cfg, session->pool,
|
2019-06-19 15:13:39 +02:00
|
|
|
ctx->lang_det, ctx->event_loop);
|
2014-04-30 18:23:37 +02:00
|
|
|
|
|
|
|
task->resolver = ctx->resolver;
|
2015-05-26 15:00:03 +02:00
|
|
|
task->s = rspamd_session_create (session->pool,
|
2014-07-23 13:57:31 +02:00
|
|
|
rspamd_controller_check_fin_task,
|
|
|
|
NULL,
|
2015-11-18 17:01:05 +01:00
|
|
|
(event_finalizer_t )rspamd_task_free,
|
2014-07-23 13:57:31 +02:00
|
|
|
task);
|
2014-04-30 18:23:37 +02:00
|
|
|
task->fin_arg = conn_ent;
|
2014-05-01 00:09:26 +02:00
|
|
|
task->http_conn = rspamd_http_connection_ref (conn_ent->conn);
|
|
|
|
task->sock = conn_ent->conn->fd;
|
2015-09-15 10:09:07 +02:00
|
|
|
task->flags |= RSPAMD_TASK_FLAG_MIME;
|
|
|
|
task->resolver = ctx->resolver;
|
2014-04-30 18:23:37 +02:00
|
|
|
|
2017-05-24 13:15:48 +02:00
|
|
|
if (!rspamd_protocol_handle_request (task, msg)) {
|
2017-06-08 12:09:58 +02:00
|
|
|
goto end;
|
2017-05-24 13:15:48 +02:00
|
|
|
}
|
|
|
|
|
2015-10-07 18:00:06 +02:00
|
|
|
if (!rspamd_task_load_message (task, msg, msg->body_buf.begin, msg->body_buf.len)) {
|
2017-06-08 12:09:58 +02:00
|
|
|
goto end;
|
2015-06-02 16:34:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!rspamd_task_process (task, RSPAMD_TASK_PROCESS_ALL)) {
|
2017-06-08 12:09:58 +02:00
|
|
|
goto end;
|
2014-04-30 18:23:37 +02:00
|
|
|
}
|
|
|
|
|
2018-12-06 16:17:29 +01:00
|
|
|
if (ctx->task_timeout > 0.0) {
|
2019-06-19 18:07:56 +02:00
|
|
|
task->timeout_ev.data = task;
|
|
|
|
ev_timer_init (&task->timeout_ev, rspamd_task_timeout,
|
|
|
|
ctx->task_timeout, 0.0);
|
|
|
|
ev_timer_start (task->event_loop, &task->timeout_ev);
|
2018-12-06 16:17:29 +01:00
|
|
|
}
|
|
|
|
|
2017-06-08 12:09:58 +02:00
|
|
|
end:
|
2014-04-30 18:23:37 +02:00
|
|
|
session->task = task;
|
2015-05-26 15:00:03 +02:00
|
|
|
rspamd_session_pending (task->s);
|
2014-04-30 18:23:37 +02:00
|
|
|
|
|
|
|
return 0;
|
2009-07-29 18:17:29 +02:00
|
|
|
}
|
|
|
|
|
2014-04-30 18:23:37 +02:00
|
|
|
/*
|
|
|
|
* Save actions command handler:
|
|
|
|
* request: /saveactions
|
|
|
|
* headers: Password
|
|
|
|
* input: json array [<spam>,<probable spam>,<greylist>]
|
|
|
|
* reply: json {"success":true} or {"error":"error message"}
|
|
|
|
*/
|
|
|
|
static int
|
2014-07-23 13:57:31 +02:00
|
|
|
rspamd_controller_handle_saveactions (
|
|
|
|
struct rspamd_http_connection_entry *conn_ent,
|
|
|
|
struct rspamd_http_message *msg)
|
2011-07-14 09:31:09 +02:00
|
|
|
{
|
2014-07-23 13:57:31 +02:00
|
|
|
struct rspamd_controller_session *session = conn_ent->ud;
|
|
|
|
struct ucl_parser *parser;
|
2014-12-17 13:11:55 +01:00
|
|
|
ucl_object_t *obj;
|
|
|
|
const ucl_object_t *cur;
|
2014-07-23 13:57:31 +02:00
|
|
|
struct rspamd_controller_worker_ctx *ctx;
|
|
|
|
const gchar *error;
|
|
|
|
gdouble score;
|
2014-09-02 14:20:47 +02:00
|
|
|
gint i, added = 0;
|
2017-11-04 16:42:15 +01:00
|
|
|
enum rspamd_action_type act;
|
2014-12-17 13:11:55 +01:00
|
|
|
ucl_object_iter_t it = NULL;
|
2014-04-30 18:23:37 +02:00
|
|
|
|
|
|
|
ctx = session->ctx;
|
|
|
|
|
2014-04-30 18:23:58 +02:00
|
|
|
if (!rspamd_controller_check_password (conn_ent, session, msg, TRUE)) {
|
2014-04-30 18:23:37 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2011-07-14 09:31:09 +02:00
|
|
|
|
2016-06-10 17:38:29 +02:00
|
|
|
if (rspamd_http_message_get_body (msg, NULL) == NULL) {
|
2015-08-30 13:35:18 +02:00
|
|
|
msg_err_session ("got zero length body, cannot continue");
|
2014-07-23 13:57:31 +02:00
|
|
|
rspamd_controller_send_error (conn_ent,
|
|
|
|
400,
|
|
|
|
"Empty body is not permitted");
|
2014-04-30 18:23:37 +02:00
|
|
|
return 0;
|
2012-09-14 18:59:23 +02:00
|
|
|
}
|
2014-04-30 18:23:37 +02:00
|
|
|
|
|
|
|
parser = ucl_parser_new (0);
|
2017-01-15 21:23:35 +01:00
|
|
|
if (!ucl_parser_add_chunk (parser, msg->body_buf.begin, msg->body_buf.len)) {
|
|
|
|
if ((error = ucl_parser_get_error (parser)) != NULL) {
|
|
|
|
msg_err_session ("cannot parse input: %s", error);
|
|
|
|
rspamd_controller_send_error (conn_ent, 400, "Cannot parse input");
|
|
|
|
ucl_parser_free (parser);
|
|
|
|
return 0;
|
|
|
|
}
|
2014-04-30 18:23:37 +02:00
|
|
|
|
2017-01-15 21:23:35 +01:00
|
|
|
msg_err_session ("cannot parse input: unknown error");
|
2014-04-30 18:23:37 +02:00
|
|
|
rspamd_controller_send_error (conn_ent, 400, "Cannot parse input");
|
|
|
|
ucl_parser_free (parser);
|
|
|
|
return 0;
|
2012-09-14 18:59:23 +02:00
|
|
|
}
|
2014-04-30 18:23:37 +02:00
|
|
|
|
|
|
|
obj = ucl_parser_get_object (parser);
|
|
|
|
ucl_parser_free (parser);
|
|
|
|
|
2018-03-19 17:48:04 +01:00
|
|
|
if (obj->type != UCL_ARRAY || obj->len != 4) {
|
|
|
|
msg_err_session ("input is not an array of 4 elements");
|
2014-04-30 18:23:37 +02:00
|
|
|
rspamd_controller_send_error (conn_ent, 400, "Cannot parse input");
|
|
|
|
ucl_object_unref (obj);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-01-10 13:36:12 +01:00
|
|
|
it = ucl_object_iterate_new (obj);
|
|
|
|
|
2018-03-19 17:48:04 +01:00
|
|
|
for (i = 0; i < 4; i++) {
|
2017-01-10 13:36:12 +01:00
|
|
|
cur = ucl_object_iterate_safe (it, TRUE);
|
2018-03-19 17:48:04 +01:00
|
|
|
|
2014-07-23 13:57:31 +02:00
|
|
|
switch (i) {
|
2014-04-30 18:23:37 +02:00
|
|
|
case 0:
|
2019-06-19 18:07:56 +02:00
|
|
|
default:
|
2014-04-30 18:23:37 +02:00
|
|
|
act = METRIC_ACTION_REJECT;
|
|
|
|
break;
|
|
|
|
case 1:
|
2018-03-19 17:48:04 +01:00
|
|
|
act = METRIC_ACTION_REWRITE_SUBJECT;
|
2014-04-30 18:23:37 +02:00
|
|
|
break;
|
|
|
|
case 2:
|
2018-03-19 17:48:04 +01:00
|
|
|
act = METRIC_ACTION_ADD_HEADER;
|
|
|
|
break;
|
|
|
|
case 3:
|
2014-04-30 18:23:37 +02:00
|
|
|
act = METRIC_ACTION_GREYLIST;
|
|
|
|
break;
|
|
|
|
}
|
2018-03-19 14:25:44 +01:00
|
|
|
|
|
|
|
if (ucl_object_type (cur) == UCL_NULL) {
|
|
|
|
/* Assume NaN */
|
|
|
|
score = NAN;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
score = ucl_object_todouble (cur);
|
|
|
|
}
|
|
|
|
|
2019-01-13 15:22:04 +01:00
|
|
|
if ((isnan (session->cfg->actions[act].threshold) != isnan (score)) ||
|
|
|
|
(session->cfg->actions[act].threshold != score)) {
|
2014-04-30 18:23:37 +02:00
|
|
|
add_dynamic_action (ctx->cfg, DEFAULT_METRIC, act, score);
|
2014-09-02 14:20:47 +02:00
|
|
|
added ++;
|
2012-09-14 18:59:23 +02:00
|
|
|
}
|
2016-03-18 11:54:51 +01:00
|
|
|
else {
|
|
|
|
if (remove_dynamic_action (ctx->cfg, DEFAULT_METRIC, act)) {
|
|
|
|
added ++;
|
|
|
|
}
|
|
|
|
}
|
2012-09-14 18:59:23 +02:00
|
|
|
}
|
|
|
|
|
2017-01-10 13:36:12 +01:00
|
|
|
ucl_object_iterate_free (it);
|
|
|
|
|
2015-06-23 13:46:18 +02:00
|
|
|
if (dump_dynamic_config (ctx->cfg)) {
|
2015-08-30 13:35:18 +02:00
|
|
|
msg_info_session ("<%s> modified %d actions",
|
2015-06-23 13:46:18 +02:00
|
|
|
rspamd_inet_address_to_string (session->from_addr),
|
|
|
|
added);
|
2012-09-14 18:59:23 +02:00
|
|
|
|
2015-06-23 13:46:18 +02:00
|
|
|
rspamd_controller_send_string (conn_ent, "{\"success\":true}");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rspamd_controller_send_error (conn_ent, 500, "Save error");
|
|
|
|
}
|
|
|
|
|
|
|
|
ucl_object_unref (obj);
|
2014-04-30 18:23:37 +02:00
|
|
|
|
|
|
|
return 0;
|
2012-09-14 18:59:23 +02:00
|
|
|
}
|
|
|
|
|
2011-12-16 15:38:52 +01:00
|
|
|
/*
|
2014-04-30 18:23:37 +02:00
|
|
|
* Save symbols command handler:
|
|
|
|
* request: /savesymbols
|
|
|
|
* headers: Password
|
|
|
|
* input: json data
|
|
|
|
* reply: json {"success":true} or {"error":"error message"}
|
2011-12-16 15:38:52 +01:00
|
|
|
*/
|
2014-04-30 18:23:37 +02:00
|
|
|
static int
|
2014-07-23 13:57:31 +02:00
|
|
|
rspamd_controller_handle_savesymbols (
|
|
|
|
struct rspamd_http_connection_entry *conn_ent,
|
|
|
|
struct rspamd_http_message *msg)
|
2011-12-14 17:05:56 +01:00
|
|
|
{
|
2014-07-23 13:57:31 +02:00
|
|
|
struct rspamd_controller_session *session = conn_ent->ud;
|
|
|
|
struct ucl_parser *parser;
|
|
|
|
ucl_object_t *obj;
|
|
|
|
const ucl_object_t *cur, *jname, *jvalue;
|
|
|
|
ucl_object_iter_t iter = NULL;
|
|
|
|
struct rspamd_controller_worker_ctx *ctx;
|
|
|
|
const gchar *error;
|
|
|
|
gdouble val;
|
2016-12-05 14:27:46 +01:00
|
|
|
struct rspamd_symbol *sym;
|
2014-09-02 14:20:47 +02:00
|
|
|
int added = 0;
|
2014-04-30 18:23:37 +02:00
|
|
|
|
|
|
|
ctx = session->ctx;
|
|
|
|
|
2014-04-30 18:23:58 +02:00
|
|
|
if (!rspamd_controller_check_password (conn_ent, session, msg, TRUE)) {
|
2014-04-30 18:23:37 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2011-12-16 15:38:52 +01:00
|
|
|
|
2016-06-10 17:38:29 +02:00
|
|
|
if (rspamd_http_message_get_body (msg, NULL) == NULL) {
|
2015-08-30 13:35:18 +02:00
|
|
|
msg_err_session ("got zero length body, cannot continue");
|
2014-07-23 13:57:31 +02:00
|
|
|
rspamd_controller_send_error (conn_ent,
|
|
|
|
400,
|
|
|
|
"Empty body is not permitted");
|
2014-04-30 18:23:37 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2012-02-13 18:51:10 +01:00
|
|
|
|
2014-04-30 18:23:37 +02:00
|
|
|
parser = ucl_parser_new (0);
|
2017-01-15 21:23:35 +01:00
|
|
|
if (!ucl_parser_add_chunk (parser, msg->body_buf.begin, msg->body_buf.len)) {
|
|
|
|
if ((error = ucl_parser_get_error (parser)) != NULL) {
|
|
|
|
msg_err_session ("cannot parse input: %s", error);
|
|
|
|
rspamd_controller_send_error (conn_ent, 400, "Cannot parse input");
|
|
|
|
ucl_parser_free (parser);
|
|
|
|
return 0;
|
|
|
|
}
|
2014-04-30 18:23:37 +02:00
|
|
|
|
2017-01-15 21:23:35 +01:00
|
|
|
msg_err_session ("cannot parse input: unknown error");
|
2014-04-30 18:23:37 +02:00
|
|
|
rspamd_controller_send_error (conn_ent, 400, "Cannot parse input");
|
|
|
|
ucl_parser_free (parser);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
obj = ucl_parser_get_object (parser);
|
|
|
|
ucl_parser_free (parser);
|
|
|
|
|
2014-09-01 15:17:25 +02:00
|
|
|
if (obj->type != UCL_ARRAY) {
|
2015-08-30 13:35:18 +02:00
|
|
|
msg_err_session ("input is not an array");
|
2014-04-30 18:23:37 +02:00
|
|
|
rspamd_controller_send_error (conn_ent, 400, "Cannot parse input");
|
|
|
|
ucl_object_unref (obj);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-01-10 13:36:12 +01:00
|
|
|
iter = ucl_object_iterate_new (obj);
|
|
|
|
|
|
|
|
while ((cur = ucl_object_iterate_safe (iter, true))) {
|
2014-04-30 18:23:37 +02:00
|
|
|
if (cur->type != UCL_OBJECT) {
|
2015-08-30 13:35:18 +02:00
|
|
|
msg_err_session ("json array data error");
|
2014-04-30 18:23:37 +02:00
|
|
|
rspamd_controller_send_error (conn_ent, 400, "Cannot parse input");
|
|
|
|
ucl_object_unref (obj);
|
2017-01-10 13:36:12 +01:00
|
|
|
ucl_object_iterate_free (iter);
|
|
|
|
|
2014-04-30 18:23:37 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2016-11-01 12:59:31 +01:00
|
|
|
|
2016-02-16 10:59:52 +01:00
|
|
|
jname = ucl_object_lookup (cur, "name");
|
|
|
|
jvalue = ucl_object_lookup (cur, "value");
|
2014-04-30 18:23:37 +02:00
|
|
|
val = ucl_object_todouble (jvalue);
|
2017-11-04 16:42:15 +01:00
|
|
|
sym = g_hash_table_lookup (session->cfg->symbols, ucl_object_tostring (jname));
|
2016-11-01 12:59:31 +01:00
|
|
|
|
2014-10-09 02:08:58 +02:00
|
|
|
if (sym && fabs (*sym->weight_ptr - val) > 0.01) {
|
2014-04-30 18:23:37 +02:00
|
|
|
if (!add_dynamic_symbol (ctx->cfg, DEFAULT_METRIC,
|
2014-07-23 13:57:31 +02:00
|
|
|
ucl_object_tostring (jname), val)) {
|
2015-08-30 13:35:18 +02:00
|
|
|
msg_err_session ("add symbol failed for %s",
|
2014-07-23 13:57:31 +02:00
|
|
|
ucl_object_tostring (jname));
|
|
|
|
rspamd_controller_send_error (conn_ent, 506,
|
|
|
|
"Add symbol failed");
|
2014-04-30 18:23:37 +02:00
|
|
|
ucl_object_unref (obj);
|
2017-01-10 13:36:12 +01:00
|
|
|
ucl_object_iterate_free (iter);
|
|
|
|
|
2014-04-30 18:23:37 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2014-09-02 14:20:47 +02:00
|
|
|
added ++;
|
2014-04-30 18:23:37 +02:00
|
|
|
}
|
2016-11-01 12:59:31 +01:00
|
|
|
else if (sym && ctx->cfg->dynamic_conf) {
|
2016-03-18 11:54:51 +01:00
|
|
|
if (remove_dynamic_symbol (ctx->cfg, DEFAULT_METRIC,
|
|
|
|
ucl_object_tostring (jname))) {
|
|
|
|
added ++;
|
|
|
|
}
|
|
|
|
}
|
2014-04-30 18:23:37 +02:00
|
|
|
}
|
|
|
|
|
2017-01-10 13:36:12 +01:00
|
|
|
ucl_object_iterate_free (iter);
|
|
|
|
|
2015-06-23 13:46:18 +02:00
|
|
|
if (added > 0) {
|
2016-11-01 12:59:31 +01:00
|
|
|
if (ctx->cfg->dynamic_conf) {
|
|
|
|
if (dump_dynamic_config (ctx->cfg)) {
|
|
|
|
msg_info_session ("<%s> modified %d symbols",
|
|
|
|
rspamd_inet_address_to_string (session->from_addr),
|
|
|
|
added);
|
2014-04-30 18:23:37 +02:00
|
|
|
|
2016-11-01 12:59:31 +01:00
|
|
|
rspamd_controller_send_string (conn_ent, "{\"success\":true}");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rspamd_controller_send_error (conn_ent, 500, "Save error");
|
|
|
|
}
|
2015-06-23 13:46:18 +02:00
|
|
|
}
|
|
|
|
else {
|
2016-11-01 12:59:31 +01:00
|
|
|
rspamd_controller_send_string (conn_ent, "{\"success\":true}");
|
2015-06-23 13:46:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2015-08-30 13:35:18 +02:00
|
|
|
msg_err_session ("no symbols to save");
|
2015-06-23 13:46:18 +02:00
|
|
|
rspamd_controller_send_error (conn_ent, 404, "No symbols to save");
|
|
|
|
}
|
|
|
|
|
|
|
|
ucl_object_unref (obj);
|
2014-04-30 18:23:37 +02:00
|
|
|
|
|
|
|
return 0;
|
2011-12-14 17:05:56 +01:00
|
|
|
}
|
|
|
|
|
2011-12-16 15:38:52 +01:00
|
|
|
/*
|
2014-04-30 18:23:37 +02:00
|
|
|
* Save map command handler:
|
|
|
|
* request: /savemap
|
|
|
|
* headers: Password, Map
|
|
|
|
* input: plaintext data
|
|
|
|
* reply: json {"success":true} or {"error":"error message"}
|
2011-12-16 15:38:52 +01:00
|
|
|
*/
|
2014-04-30 18:23:37 +02:00
|
|
|
static int
|
2014-04-30 18:23:58 +02:00
|
|
|
rspamd_controller_handle_savemap (struct rspamd_http_connection_entry *conn_ent,
|
2014-07-23 13:57:31 +02:00
|
|
|
struct rspamd_http_message *msg)
|
2011-12-16 15:38:52 +01:00
|
|
|
{
|
2014-07-23 13:57:31 +02:00
|
|
|
struct rspamd_controller_session *session = conn_ent->ud;
|
|
|
|
GList *cur;
|
2019-06-19 18:07:56 +02:00
|
|
|
struct rspamd_map *map = NULL;
|
2016-05-09 19:59:46 +02:00
|
|
|
struct rspamd_map_backend *bk;
|
2014-07-23 13:57:31 +02:00
|
|
|
struct rspamd_controller_worker_ctx *ctx;
|
2015-10-07 16:58:57 +02:00
|
|
|
const rspamd_ftok_t *idstr;
|
2017-01-09 18:15:36 +01:00
|
|
|
gulong id, i, ntries = 0;
|
2014-07-23 13:57:31 +02:00
|
|
|
gboolean found = FALSE;
|
|
|
|
gint fd;
|
2014-04-30 18:23:37 +02:00
|
|
|
|
|
|
|
ctx = session->ctx;
|
|
|
|
|
2014-04-30 18:23:58 +02:00
|
|
|
if (!rspamd_controller_check_password (conn_ent, session, msg, TRUE)) {
|
2014-04-30 18:23:37 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2011-12-16 15:38:52 +01:00
|
|
|
|
2016-06-10 17:38:29 +02:00
|
|
|
if (rspamd_http_message_get_body (msg, NULL) == NULL) {
|
2015-08-30 13:35:18 +02:00
|
|
|
msg_err_session ("got zero length body, cannot continue");
|
2014-07-23 13:57:31 +02:00
|
|
|
rspamd_controller_send_error (conn_ent,
|
|
|
|
400,
|
|
|
|
"Empty body is not permitted");
|
2014-04-30 18:23:37 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2011-12-16 15:38:52 +01:00
|
|
|
|
2014-04-30 18:23:37 +02:00
|
|
|
idstr = rspamd_http_message_find_header (msg, "Map");
|
2009-02-19 19:16:30 +01:00
|
|
|
|
2014-04-30 18:23:37 +02:00
|
|
|
if (idstr == NULL) {
|
2015-08-30 13:35:18 +02:00
|
|
|
msg_info_session ("absent map id");
|
2014-04-30 18:23:37 +02:00
|
|
|
rspamd_controller_send_error (conn_ent, 400, "Map id not specified");
|
|
|
|
return 0;
|
|
|
|
}
|
2010-12-24 16:57:55 +01:00
|
|
|
|
2015-10-07 16:58:57 +02:00
|
|
|
if (!rspamd_strtoul (idstr->begin, idstr->len, &id)) {
|
|
|
|
msg_info_session ("invalid map id: %T", idstr);
|
2014-04-30 18:23:37 +02:00
|
|
|
rspamd_controller_send_error (conn_ent, 400, "Map id is invalid");
|
|
|
|
return 0;
|
|
|
|
}
|
2012-09-18 18:42:09 +02:00
|
|
|
|
2014-04-30 18:23:37 +02:00
|
|
|
/* Now let's be sure that we have map defined in configuration */
|
|
|
|
cur = ctx->cfg->maps;
|
2017-01-09 18:15:36 +01:00
|
|
|
while (cur && !found) {
|
2014-04-30 18:23:37 +02:00
|
|
|
map = cur->data;
|
2017-01-09 18:15:36 +01:00
|
|
|
|
|
|
|
PTR_ARRAY_FOREACH (map->backends, i, bk) {
|
|
|
|
if (bk->id == id && bk->protocol == MAP_PROTO_FILE) {
|
|
|
|
found = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
2009-11-12 18:38:20 +01:00
|
|
|
}
|
2014-04-30 18:23:37 +02:00
|
|
|
cur = g_list_next (cur);
|
|
|
|
}
|
2009-02-19 19:16:30 +01:00
|
|
|
|
2014-04-30 18:23:37 +02:00
|
|
|
if (!found) {
|
2015-11-11 19:00:43 +01:00
|
|
|
msg_info_session ("map not found: %L", id);
|
2014-04-30 18:23:37 +02:00
|
|
|
rspamd_controller_send_error (conn_ent, 404, "Map id not found");
|
|
|
|
return 0;
|
|
|
|
}
|
2010-03-01 16:37:06 +01:00
|
|
|
|
2017-01-09 18:15:36 +01:00
|
|
|
while (g_atomic_int_compare_and_exchange (map->locked, 0, 1)) {
|
|
|
|
struct timespec sleep_ts = {
|
|
|
|
.tv_sec = 0,
|
|
|
|
.tv_nsec = 100000000ULL,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (ntries > 5) {
|
|
|
|
msg_info_session ("map locked: %s", bk->uri);
|
|
|
|
rspamd_controller_send_error (conn_ent, 404, "Map is locked");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ntries ++;
|
|
|
|
nanosleep (&sleep_ts, NULL);
|
2014-04-30 18:23:37 +02:00
|
|
|
}
|
2010-03-01 16:37:06 +01:00
|
|
|
|
2014-04-30 18:23:37 +02:00
|
|
|
/* Set lock */
|
2017-01-09 18:15:36 +01:00
|
|
|
fd = open (bk->uri, O_WRONLY | O_TRUNC | O_CREAT, 00644);
|
|
|
|
|
2014-04-30 18:23:37 +02:00
|
|
|
if (fd == -1) {
|
|
|
|
g_atomic_int_set (map->locked, 0);
|
2016-05-09 19:59:46 +02:00
|
|
|
msg_info_session ("map %s open error: %s", bk->uri, strerror (errno));
|
2017-01-09 18:15:36 +01:00
|
|
|
rspamd_controller_send_error (conn_ent, 404, "Cannot open map: %s",
|
|
|
|
strerror (errno));
|
2014-04-30 18:23:37 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2010-03-01 16:37:06 +01:00
|
|
|
|
2015-10-07 18:00:06 +02:00
|
|
|
if (write (fd, msg->body_buf.begin, msg->body_buf.len) == -1) {
|
2016-05-09 19:59:46 +02:00
|
|
|
msg_info_session ("map %s write error: %s", bk->uri, strerror (errno));
|
2014-04-30 18:23:37 +02:00
|
|
|
close (fd);
|
|
|
|
g_atomic_int_set (map->locked, 0);
|
2017-01-09 18:15:36 +01:00
|
|
|
rspamd_controller_send_error (conn_ent, 500, "Map write error: %s",
|
|
|
|
strerror (errno));
|
2014-04-30 18:23:37 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2010-03-01 16:37:06 +01:00
|
|
|
|
2015-08-30 13:35:18 +02:00
|
|
|
msg_info_session ("<%s>, map %s saved",
|
2015-03-12 15:18:57 +01:00
|
|
|
rspamd_inet_address_to_string (session->from_addr),
|
2016-05-09 19:59:46 +02:00
|
|
|
bk->uri);
|
2014-04-30 18:23:37 +02:00
|
|
|
/* Close and unlock */
|
|
|
|
close (fd);
|
|
|
|
g_atomic_int_set (map->locked, 0);
|
2009-09-22 18:22:31 +02:00
|
|
|
|
2014-04-30 18:23:37 +02:00
|
|
|
rspamd_controller_send_string (conn_ent, "{\"success\":true}");
|
|
|
|
|
|
|
|
return 0;
|
2008-10-23 17:10:40 +02:00
|
|
|
}
|
|
|
|
|
2015-08-03 16:00:01 +02:00
|
|
|
struct rspamd_stat_cbdata {
|
|
|
|
struct rspamd_http_connection_entry *conn_ent;
|
|
|
|
ucl_object_t *top;
|
|
|
|
ucl_object_t *stat;
|
|
|
|
struct rspamd_task *task;
|
|
|
|
guint64 learned;
|
|
|
|
};
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
rspamd_controller_stat_fin_task (void *ud)
|
|
|
|
{
|
|
|
|
struct rspamd_stat_cbdata *cbdata = ud;
|
|
|
|
struct rspamd_http_connection_entry *conn_ent;
|
2016-06-27 19:16:11 +02:00
|
|
|
ucl_object_t *top, *ar;
|
|
|
|
GList *fuzzy_elts, *cur;
|
|
|
|
struct rspamd_fuzzy_stat_entry *entry;
|
2015-08-03 16:00:01 +02:00
|
|
|
|
|
|
|
conn_ent = cbdata->conn_ent;
|
|
|
|
top = cbdata->top;
|
|
|
|
|
|
|
|
ucl_object_insert_key (top,
|
|
|
|
ucl_object_fromint (cbdata->learned), "total_learns", 0, false);
|
|
|
|
|
|
|
|
if (cbdata->stat) {
|
|
|
|
ucl_object_insert_key (top, cbdata->stat, "statfiles", 0, false);
|
|
|
|
}
|
|
|
|
|
2016-06-27 19:16:11 +02:00
|
|
|
fuzzy_elts = rspamd_mempool_get_variable (cbdata->task->task_pool, "fuzzy_stat");
|
|
|
|
|
|
|
|
if (fuzzy_elts) {
|
|
|
|
ar = ucl_object_typed_new (UCL_OBJECT);
|
|
|
|
|
|
|
|
for (cur = fuzzy_elts; cur != NULL; cur = g_list_next (cur)) {
|
|
|
|
entry = cur->data;
|
|
|
|
|
|
|
|
if (entry->name) {
|
|
|
|
ucl_object_insert_key (ar, ucl_object_fromint (entry->fuzzy_cnt),
|
|
|
|
entry->name, 0, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ucl_object_insert_key (top, ar, "fuzzy_hashes", 0, false);
|
|
|
|
}
|
|
|
|
|
2015-08-03 16:00:01 +02:00
|
|
|
rspamd_controller_send_ucl (conn_ent, top);
|
|
|
|
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
rspamd_controller_stat_cleanup_task (void *ud)
|
|
|
|
{
|
|
|
|
struct rspamd_stat_cbdata *cbdata = ud;
|
|
|
|
|
2015-11-18 17:01:05 +01:00
|
|
|
rspamd_task_free (cbdata->task);
|
2015-08-03 16:00:01 +02:00
|
|
|
ucl_object_unref (cbdata->top);
|
|
|
|
}
|
|
|
|
|
2014-04-30 18:23:37 +02:00
|
|
|
/*
|
|
|
|
* Stat command handler:
|
|
|
|
* request: /stat (/resetstat)
|
|
|
|
* headers: Password
|
|
|
|
* reply: json data
|
|
|
|
*/
|
|
|
|
static int
|
2014-07-23 13:57:31 +02:00
|
|
|
rspamd_controller_handle_stat_common (
|
|
|
|
struct rspamd_http_connection_entry *conn_ent,
|
|
|
|
struct rspamd_http_message *msg,
|
|
|
|
gboolean do_reset)
|
2008-10-23 17:10:40 +02:00
|
|
|
{
|
2014-04-30 18:23:58 +02:00
|
|
|
struct rspamd_controller_session *session = conn_ent->ud;
|
2014-04-30 18:23:37 +02:00
|
|
|
ucl_object_t *top, *sub;
|
|
|
|
gint i;
|
2015-08-03 16:00:01 +02:00
|
|
|
guint64 spam = 0, ham = 0;
|
2014-04-30 18:23:37 +02:00
|
|
|
rspamd_mempool_stat_t mem_st;
|
|
|
|
struct rspamd_stat *stat, stat_copy;
|
2015-08-03 16:00:01 +02:00
|
|
|
struct rspamd_controller_worker_ctx *ctx;
|
|
|
|
struct rspamd_task *task;
|
|
|
|
struct rspamd_stat_cbdata *cbdata;
|
2014-04-30 18:23:37 +02:00
|
|
|
|
2016-07-14 17:52:33 +02:00
|
|
|
memset (&mem_st, 0, sizeof (mem_st));
|
2014-04-30 18:23:37 +02:00
|
|
|
rspamd_mempool_stat (&mem_st);
|
|
|
|
memcpy (&stat_copy, session->ctx->worker->srv->stat, sizeof (stat_copy));
|
|
|
|
stat = &stat_copy;
|
2015-08-03 16:00:01 +02:00
|
|
|
ctx = session->ctx;
|
2018-01-01 19:58:01 +01:00
|
|
|
|
|
|
|
task = rspamd_task_new (session->ctx->worker, session->cfg, session->pool,
|
2019-06-19 15:13:39 +02:00
|
|
|
ctx->lang_det, ctx->event_loop);
|
2015-08-03 16:00:01 +02:00
|
|
|
task->resolver = ctx->resolver;
|
|
|
|
cbdata = rspamd_mempool_alloc0 (session->pool, sizeof (*cbdata));
|
|
|
|
cbdata->conn_ent = conn_ent;
|
|
|
|
cbdata->task = task;
|
2014-04-30 18:23:37 +02:00
|
|
|
top = ucl_object_typed_new (UCL_OBJECT);
|
2015-08-03 16:00:01 +02:00
|
|
|
cbdata->top = top;
|
|
|
|
|
|
|
|
task->s = rspamd_session_create (session->pool,
|
|
|
|
rspamd_controller_stat_fin_task,
|
|
|
|
NULL,
|
|
|
|
rspamd_controller_stat_cleanup_task,
|
|
|
|
cbdata);
|
|
|
|
task->fin_arg = cbdata;
|
|
|
|
task->http_conn = rspamd_http_connection_ref (conn_ent->conn);;
|
|
|
|
task->sock = conn_ent->conn->fd;
|
2014-04-30 18:23:37 +02:00
|
|
|
|
2016-10-26 11:41:36 +02:00
|
|
|
ucl_object_insert_key (top, ucl_object_frombool (!session->is_enable),
|
|
|
|
"read_only", 0, false);
|
2014-07-23 13:57:31 +02:00
|
|
|
ucl_object_insert_key (top, ucl_object_fromint (
|
|
|
|
stat->messages_scanned), "scanned", 0, false);
|
2015-09-25 14:47:56 +02:00
|
|
|
ucl_object_insert_key (top, ucl_object_fromint (
|
|
|
|
stat->messages_learned), "learned", 0, false);
|
|
|
|
|
2014-04-30 18:23:37 +02:00
|
|
|
if (stat->messages_scanned > 0) {
|
|
|
|
sub = ucl_object_typed_new (UCL_OBJECT);
|
2014-07-23 13:57:31 +02:00
|
|
|
for (i = METRIC_ACTION_REJECT; i <= METRIC_ACTION_NOACTION; i++) {
|
2014-05-02 16:27:18 +02:00
|
|
|
ucl_object_insert_key (sub,
|
2014-07-23 13:57:31 +02:00
|
|
|
ucl_object_fromint (stat->actions_stat[i]),
|
2014-09-23 13:15:28 +02:00
|
|
|
rspamd_action_to_str (i), 0, false);
|
2014-04-30 18:23:37 +02:00
|
|
|
if (i < METRIC_ACTION_GREYLIST) {
|
|
|
|
spam += stat->actions_stat[i];
|
2012-09-18 18:42:09 +02:00
|
|
|
}
|
|
|
|
else {
|
2014-04-30 18:23:37 +02:00
|
|
|
ham += stat->actions_stat[i];
|
2012-09-18 18:42:09 +02:00
|
|
|
}
|
2014-04-30 18:23:37 +02:00
|
|
|
if (do_reset) {
|
2015-11-26 19:50:03 +01:00
|
|
|
#ifndef HAVE_ATOMIC_BUILTINS
|
2014-04-30 18:23:37 +02:00
|
|
|
session->ctx->worker->srv->stat->actions_stat[i] = 0;
|
2015-11-26 19:50:03 +01:00
|
|
|
#else
|
|
|
|
__atomic_store_n(&session->ctx->worker->srv->stat->actions_stat[i],
|
|
|
|
0, __ATOMIC_RELEASE);
|
|
|
|
#endif
|
2014-04-30 18:23:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ucl_object_insert_key (top, sub, "actions", 0, false);
|
|
|
|
}
|
|
|
|
|
2014-07-23 13:57:31 +02:00
|
|
|
ucl_object_insert_key (top, ucl_object_fromint (
|
|
|
|
spam), "spam_count", 0, false);
|
|
|
|
ucl_object_insert_key (top, ucl_object_fromint (
|
|
|
|
ham), "ham_count", 0, false);
|
2014-04-30 18:23:37 +02:00
|
|
|
ucl_object_insert_key (top,
|
2014-07-23 13:57:31 +02:00
|
|
|
ucl_object_fromint (stat->connections_count), "connections", 0, false);
|
2014-04-30 18:23:37 +02:00
|
|
|
ucl_object_insert_key (top,
|
2014-07-23 13:57:31 +02:00
|
|
|
ucl_object_fromint (stat->control_connections_count),
|
|
|
|
"control_connections", 0, false);
|
2014-04-30 18:23:37 +02:00
|
|
|
|
|
|
|
ucl_object_insert_key (top,
|
2014-07-23 13:57:31 +02:00
|
|
|
ucl_object_fromint (mem_st.pools_allocated), "pools_allocated", 0,
|
|
|
|
false);
|
2014-04-30 18:23:37 +02:00
|
|
|
ucl_object_insert_key (top,
|
2014-07-23 13:57:31 +02:00
|
|
|
ucl_object_fromint (mem_st.pools_freed), "pools_freed", 0, false);
|
2014-04-30 18:23:37 +02:00
|
|
|
ucl_object_insert_key (top,
|
2014-07-23 13:57:31 +02:00
|
|
|
ucl_object_fromint (mem_st.bytes_allocated), "bytes_allocated", 0,
|
|
|
|
false);
|
2014-04-30 18:23:37 +02:00
|
|
|
ucl_object_insert_key (top,
|
2014-07-23 13:57:31 +02:00
|
|
|
ucl_object_fromint (
|
|
|
|
mem_st.chunks_allocated), "chunks_allocated", 0, false);
|
2014-04-30 18:23:37 +02:00
|
|
|
ucl_object_insert_key (top,
|
2014-07-23 13:57:31 +02:00
|
|
|
ucl_object_fromint (mem_st.shared_chunks_allocated),
|
|
|
|
"shared_chunks_allocated", 0, false);
|
2014-04-30 18:23:37 +02:00
|
|
|
ucl_object_insert_key (top,
|
2014-07-23 13:57:31 +02:00
|
|
|
ucl_object_fromint (mem_st.chunks_freed), "chunks_freed", 0, false);
|
2014-04-30 18:23:37 +02:00
|
|
|
ucl_object_insert_key (top,
|
2014-07-23 13:57:31 +02:00
|
|
|
ucl_object_fromint (
|
|
|
|
mem_st.oversized_chunks), "chunks_oversized", 0, false);
|
2017-08-20 23:13:25 +02:00
|
|
|
ucl_object_insert_key (top,
|
|
|
|
ucl_object_fromint (mem_st.fragmented_size), "fragmented", 0, false);
|
2015-04-21 19:31:43 +02:00
|
|
|
|
2014-04-30 18:23:37 +02:00
|
|
|
if (do_reset) {
|
|
|
|
session->ctx->srv->stat->messages_scanned = 0;
|
|
|
|
session->ctx->srv->stat->messages_learned = 0;
|
|
|
|
session->ctx->srv->stat->connections_count = 0;
|
|
|
|
session->ctx->srv->stat->control_connections_count = 0;
|
|
|
|
rspamd_mempool_stat_reset ();
|
2009-01-11 15:31:26 +01:00
|
|
|
}
|
2014-04-30 18:23:37 +02:00
|
|
|
|
2016-06-27 19:16:11 +02:00
|
|
|
fuzzy_stat_command (task);
|
|
|
|
|
2015-08-03 16:00:01 +02:00
|
|
|
/* Now write statistics for each statfile */
|
|
|
|
rspamd_stat_statistics (task, session->ctx->cfg, &cbdata->learned,
|
|
|
|
&cbdata->stat);
|
|
|
|
session->task = task;
|
|
|
|
rspamd_session_pending (task->s);
|
2014-04-30 18:23:37 +02:00
|
|
|
|
|
|
|
return 0;
|
2008-10-23 17:10:40 +02:00
|
|
|
}
|
|
|
|
|
2014-04-30 18:23:37 +02:00
|
|
|
static int
|
2014-04-30 18:23:58 +02:00
|
|
|
rspamd_controller_handle_stat (struct rspamd_http_connection_entry *conn_ent,
|
2014-07-23 13:57:31 +02:00
|
|
|
struct rspamd_http_message *msg)
|
2008-10-23 17:10:40 +02:00
|
|
|
{
|
2014-07-23 13:57:31 +02:00
|
|
|
struct rspamd_controller_session *session = conn_ent->ud;
|
2009-02-20 16:49:04 +01:00
|
|
|
|
2014-04-30 18:23:58 +02:00
|
|
|
if (!rspamd_controller_check_password (conn_ent, session, msg, FALSE)) {
|
2014-04-30 18:23:37 +02:00
|
|
|
return 0;
|
2009-02-20 16:49:04 +01:00
|
|
|
}
|
2014-04-30 18:23:37 +02:00
|
|
|
|
2014-04-30 18:23:58 +02:00
|
|
|
return rspamd_controller_handle_stat_common (conn_ent, msg, FALSE);
|
2008-10-23 17:10:40 +02:00
|
|
|
}
|
|
|
|
|
2014-04-30 18:23:37 +02:00
|
|
|
static int
|
2014-07-23 13:57:31 +02:00
|
|
|
rspamd_controller_handle_statreset (
|
|
|
|
struct rspamd_http_connection_entry *conn_ent,
|
|
|
|
struct rspamd_http_message *msg)
|
2008-10-23 17:10:40 +02:00
|
|
|
{
|
2014-07-23 13:57:31 +02:00
|
|
|
struct rspamd_controller_session *session = conn_ent->ud;
|
2010-12-03 19:57:38 +01:00
|
|
|
|
2014-04-30 18:23:58 +02:00
|
|
|
if (!rspamd_controller_check_password (conn_ent, session, msg, TRUE)) {
|
2014-04-30 18:23:37 +02:00
|
|
|
return 0;
|
2008-10-23 17:10:40 +02:00
|
|
|
}
|
2009-03-03 12:31:51 +01:00
|
|
|
|
2015-08-30 13:35:18 +02:00
|
|
|
msg_info_session ("<%s> reset stat",
|
2015-03-12 15:18:57 +01:00
|
|
|
rspamd_inet_address_to_string (session->from_addr));
|
2014-04-30 18:23:58 +02:00
|
|
|
return rspamd_controller_handle_stat_common (conn_ent, msg, TRUE);
|
2014-04-30 18:23:37 +02:00
|
|
|
}
|
|
|
|
|
2012-04-23 15:57:02 +02:00
|
|
|
|
2014-04-30 18:23:37 +02:00
|
|
|
/*
|
|
|
|
* Counters command handler:
|
|
|
|
* request: /counters
|
|
|
|
* headers: Password
|
|
|
|
* reply: json array of all counters
|
|
|
|
*/
|
|
|
|
static int
|
2014-07-23 13:57:31 +02:00
|
|
|
rspamd_controller_handle_counters (
|
|
|
|
struct rspamd_http_connection_entry *conn_ent,
|
|
|
|
struct rspamd_http_message *msg)
|
2014-04-30 18:23:37 +02:00
|
|
|
{
|
2014-07-23 13:57:31 +02:00
|
|
|
struct rspamd_controller_session *session = conn_ent->ud;
|
|
|
|
ucl_object_t *top;
|
2018-10-29 13:22:46 +01:00
|
|
|
struct rspamd_symcache *cache;
|
2014-04-30 18:23:37 +02:00
|
|
|
|
2014-04-30 18:23:58 +02:00
|
|
|
if (!rspamd_controller_check_password (conn_ent, session, msg, FALSE)) {
|
2014-04-30 18:23:37 +02:00
|
|
|
return 0;
|
2012-04-23 15:57:02 +02:00
|
|
|
}
|
2008-10-23 17:10:40 +02:00
|
|
|
|
2014-04-30 18:23:37 +02:00
|
|
|
cache = session->ctx->cfg->cache;
|
2015-05-27 18:30:06 +02:00
|
|
|
|
2014-04-30 18:23:37 +02:00
|
|
|
if (cache != NULL) {
|
2018-10-29 13:22:46 +01:00
|
|
|
top = rspamd_symcache_counters (cache);
|
2015-05-27 18:30:06 +02:00
|
|
|
rspamd_controller_send_ucl (conn_ent, top);
|
|
|
|
ucl_object_unref (top);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rspamd_controller_send_error (conn_ent, 500, "Invalid cache");
|
2014-04-30 18:23:37 +02:00
|
|
|
}
|
2009-08-24 16:20:39 +02:00
|
|
|
|
2014-04-30 18:23:37 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2009-09-22 18:22:31 +02:00
|
|
|
|
2014-04-30 18:23:37 +02:00
|
|
|
static int
|
2014-04-30 18:23:58 +02:00
|
|
|
rspamd_controller_handle_custom (struct rspamd_http_connection_entry *conn_ent,
|
2014-07-23 13:57:31 +02:00
|
|
|
struct rspamd_http_message *msg)
|
2014-04-30 18:23:37 +02:00
|
|
|
{
|
2014-07-23 13:57:31 +02:00
|
|
|
struct rspamd_controller_session *session = conn_ent->ud;
|
|
|
|
struct rspamd_custom_controller_command *cmd;
|
2015-10-13 18:03:33 +02:00
|
|
|
gchar *url_str;
|
2018-11-09 09:31:46 +01:00
|
|
|
struct http_parser_url u;
|
|
|
|
rspamd_ftok_t lookup;
|
|
|
|
|
|
|
|
http_parser_parse_url (msg->url->str, msg->url->len, TRUE, &u);
|
2015-10-13 18:03:33 +02:00
|
|
|
|
2018-11-09 09:31:46 +01:00
|
|
|
if (u.field_set & (1 << UF_PATH)) {
|
|
|
|
guint unnorm_len;
|
|
|
|
lookup.begin = msg->url->str + u.field_data[UF_PATH].off;
|
|
|
|
lookup.len = u.field_data[UF_PATH].len;
|
|
|
|
|
|
|
|
rspamd_http_normalize_path_inplace ((gchar *)lookup.begin,
|
|
|
|
lookup.len,
|
|
|
|
&unnorm_len);
|
|
|
|
lookup.len = unnorm_len;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
lookup.begin = msg->url->str;
|
|
|
|
lookup.len = msg->url->len;
|
|
|
|
}
|
|
|
|
|
|
|
|
url_str = rspamd_ftok_cstr (&lookup);
|
2015-10-13 18:03:33 +02:00
|
|
|
cmd = g_hash_table_lookup (session->ctx->custom_commands, url_str);
|
|
|
|
g_free (url_str);
|
2011-10-26 19:13:19 +02:00
|
|
|
|
2014-04-30 18:23:37 +02:00
|
|
|
if (cmd == NULL || cmd->handler == NULL) {
|
2018-11-09 09:31:46 +01:00
|
|
|
msg_err_session ("custom command %T has not been found", &lookup);
|
2014-04-30 18:23:37 +02:00
|
|
|
rspamd_controller_send_error (conn_ent, 404, "No command associated");
|
|
|
|
return 0;
|
2011-08-01 13:32:45 +02:00
|
|
|
}
|
2014-04-30 18:23:37 +02:00
|
|
|
|
2014-07-23 13:57:31 +02:00
|
|
|
if (!rspamd_controller_check_password (conn_ent, session, msg,
|
|
|
|
cmd->privilleged)) {
|
2014-04-30 18:23:37 +02:00
|
|
|
return 0;
|
2011-08-01 13:32:45 +02:00
|
|
|
}
|
2016-06-10 17:38:29 +02:00
|
|
|
if (cmd->require_message && (rspamd_http_message_get_body (msg, NULL) == NULL)) {
|
2015-08-30 13:35:18 +02:00
|
|
|
msg_err_session ("got zero length body, cannot continue");
|
2014-07-23 13:57:31 +02:00
|
|
|
rspamd_controller_send_error (conn_ent,
|
|
|
|
400,
|
|
|
|
"Empty body is not permitted");
|
2014-04-30 18:23:37 +02:00
|
|
|
return 0;
|
2010-06-24 16:25:16 +02:00
|
|
|
}
|
2014-04-30 18:23:37 +02:00
|
|
|
|
2018-11-09 09:31:46 +01:00
|
|
|
/* Transfer query arguments to headers */
|
|
|
|
if (u.field_set & (1u << UF_QUERY)) {
|
|
|
|
GHashTable *query_args;
|
|
|
|
GHashTableIter it;
|
|
|
|
gpointer k, v;
|
|
|
|
rspamd_ftok_t *key, *value;
|
|
|
|
|
|
|
|
/* In case if we have a query, we need to store it somewhere */
|
|
|
|
query_args = rspamd_http_message_parse_query (msg);
|
|
|
|
|
|
|
|
/* Insert the rest of query params as HTTP headers */
|
|
|
|
g_hash_table_iter_init (&it, query_args);
|
|
|
|
|
|
|
|
while (g_hash_table_iter_next (&it, &k, &v)) {
|
|
|
|
key = k;
|
|
|
|
value = v;
|
|
|
|
/* Steal strings */
|
|
|
|
g_hash_table_iter_steal (&it);
|
|
|
|
url_str = rspamd_ftok_cstr (key);
|
|
|
|
rspamd_http_message_add_header_len (msg, url_str,
|
|
|
|
value->begin, value->len);
|
|
|
|
g_free (url_str);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_hash_table_unref (query_args);
|
|
|
|
}
|
|
|
|
|
2014-04-30 18:23:37 +02:00
|
|
|
return cmd->handler (conn_ent, msg, cmd->ctx);
|
2008-10-23 17:10:40 +02:00
|
|
|
}
|
|
|
|
|
2017-01-09 17:08:00 +01:00
|
|
|
static int
|
|
|
|
rspamd_controller_handle_plugins (struct rspamd_http_connection_entry *conn_ent,
|
|
|
|
struct rspamd_http_message *msg)
|
|
|
|
{
|
|
|
|
struct rspamd_controller_session *session = conn_ent->ud;
|
|
|
|
struct rspamd_controller_plugin_cbdata *cbd;
|
|
|
|
GHashTableIter it;
|
|
|
|
gpointer k, v;
|
|
|
|
ucl_object_t *plugins;
|
|
|
|
|
|
|
|
if (!rspamd_controller_check_password (conn_ent, session, msg,
|
|
|
|
FALSE)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
plugins = ucl_object_typed_new (UCL_OBJECT);
|
|
|
|
g_hash_table_iter_init (&it, session->ctx->plugins);
|
|
|
|
|
|
|
|
while (g_hash_table_iter_next (&it, &k, &v)) {
|
|
|
|
ucl_object_t *elt, *npath;
|
|
|
|
|
|
|
|
cbd = v;
|
|
|
|
elt = (ucl_object_t *)ucl_object_lookup (plugins, cbd->plugin);
|
|
|
|
|
|
|
|
if (elt == NULL) {
|
|
|
|
elt = ucl_object_typed_new (UCL_OBJECT);
|
|
|
|
ucl_object_insert_key (elt, ucl_object_fromint (cbd->version),
|
|
|
|
"version", 0, false);
|
|
|
|
npath = ucl_object_typed_new (UCL_ARRAY);
|
|
|
|
ucl_object_insert_key (elt, npath, "paths", 0, false);
|
|
|
|
ucl_object_insert_key (plugins, elt, cbd->plugin, 0, false);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
npath = (ucl_object_t *)ucl_object_lookup (elt, "paths");
|
|
|
|
}
|
|
|
|
|
|
|
|
g_assert (npath != NULL);
|
|
|
|
ucl_array_append (npath, ucl_object_fromstring (k));
|
|
|
|
}
|
|
|
|
|
|
|
|
rspamd_controller_send_ucl (conn_ent, plugins);
|
|
|
|
ucl_object_unref (plugins);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-07-05 09:22:57 +02:00
|
|
|
static int
|
|
|
|
rspamd_controller_handle_ping (struct rspamd_http_connection_entry *conn_ent,
|
|
|
|
struct rspamd_http_message *msg)
|
|
|
|
{
|
|
|
|
struct rspamd_http_message *rep_msg;
|
|
|
|
rspamd_fstring_t *reply;
|
|
|
|
|
|
|
|
rep_msg = rspamd_http_new_message (HTTP_RESPONSE);
|
|
|
|
rep_msg->date = time (NULL);
|
|
|
|
rep_msg->code = 200;
|
|
|
|
rep_msg->status = rspamd_fstring_new_init ("OK", 2);
|
|
|
|
reply = rspamd_fstring_new_init ("pong" CRLF, strlen ("pong" CRLF));
|
|
|
|
rspamd_http_message_set_body_from_fstring_steal (rep_msg, reply);
|
|
|
|
rspamd_http_connection_reset (conn_ent->conn);
|
|
|
|
rspamd_http_router_insert_headers (conn_ent->rt, rep_msg);
|
|
|
|
rspamd_http_connection_write_message (conn_ent->conn,
|
|
|
|
rep_msg,
|
|
|
|
NULL,
|
|
|
|
"text/plain",
|
|
|
|
conn_ent,
|
2019-06-19 18:07:56 +02:00
|
|
|
conn_ent->rt->timeout);
|
2017-07-05 09:22:57 +02:00
|
|
|
conn_ent->is_reply = TRUE;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-01-11 14:50:17 +01:00
|
|
|
/*
|
|
|
|
* Called on unknown methods and is used to deal with CORS as per
|
|
|
|
* https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
rspamd_controller_handle_unknown (struct rspamd_http_connection_entry *conn_ent,
|
|
|
|
struct rspamd_http_message *msg)
|
|
|
|
{
|
|
|
|
struct rspamd_http_message *rep;
|
|
|
|
|
|
|
|
if (msg->method == HTTP_OPTIONS) {
|
|
|
|
/* Assume CORS request */
|
|
|
|
|
|
|
|
rep = rspamd_http_new_message (HTTP_RESPONSE);
|
|
|
|
rep->date = time (NULL);
|
|
|
|
rep->code = 200;
|
|
|
|
rep->status = rspamd_fstring_new_init ("OK", 2);
|
|
|
|
rspamd_http_message_add_header (rep, "Access-Control-Allow-Methods",
|
|
|
|
"POST, GET, OPTIONS");
|
|
|
|
rspamd_http_message_add_header (rep, "Access-Control-Allow-Headers",
|
2018-08-16 14:40:44 +02:00
|
|
|
"Content-Type,Password,Map,Weight,Flag");
|
2017-01-11 14:50:17 +01:00
|
|
|
rspamd_http_connection_reset (conn_ent->conn);
|
|
|
|
rspamd_http_router_insert_headers (conn_ent->rt, rep);
|
|
|
|
rspamd_http_connection_write_message (conn_ent->conn,
|
|
|
|
rep,
|
|
|
|
NULL,
|
|
|
|
"text/plain",
|
|
|
|
conn_ent,
|
2019-06-19 18:07:56 +02:00
|
|
|
conn_ent->rt->timeout);
|
2017-01-11 14:50:17 +01:00
|
|
|
conn_ent->is_reply = TRUE;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rep = rspamd_http_new_message (HTTP_RESPONSE);
|
|
|
|
rep->date = time (NULL);
|
|
|
|
rep->code = 500;
|
|
|
|
rep->status = rspamd_fstring_new_init ("Invalid method",
|
|
|
|
strlen ("Invalid method"));
|
|
|
|
rspamd_http_connection_reset (conn_ent->conn);
|
|
|
|
rspamd_http_router_insert_headers (conn_ent->rt, rep);
|
|
|
|
rspamd_http_connection_write_message (conn_ent->conn,
|
|
|
|
rep,
|
|
|
|
NULL,
|
|
|
|
"text/plain",
|
|
|
|
conn_ent,
|
2019-06-19 18:07:56 +02:00
|
|
|
conn_ent->rt->timeout);
|
2017-01-11 14:50:17 +01:00
|
|
|
conn_ent->is_reply = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-01-07 19:58:14 +01:00
|
|
|
static int
|
|
|
|
rspamd_controller_handle_lua_plugin (struct rspamd_http_connection_entry *conn_ent,
|
|
|
|
struct rspamd_http_message *msg)
|
|
|
|
{
|
|
|
|
struct rspamd_controller_session *session = conn_ent->ud;
|
|
|
|
struct rspamd_controller_plugin_cbdata *cbd;
|
|
|
|
struct rspamd_task *task, **ptask;
|
|
|
|
struct rspamd_http_connection_entry **pconn;
|
|
|
|
struct rspamd_controller_worker_ctx *ctx;
|
|
|
|
lua_State *L;
|
|
|
|
gchar *url_str;
|
|
|
|
|
|
|
|
url_str = rspamd_fstring_cstr (msg->url);
|
|
|
|
cbd = g_hash_table_lookup (session->ctx->plugins, url_str);
|
|
|
|
g_free (url_str);
|
|
|
|
|
|
|
|
if (cbd == NULL || cbd->handler == NULL) {
|
|
|
|
msg_err_session ("plugin handler %V has not been found", msg->url);
|
|
|
|
rspamd_controller_send_error (conn_ent, 404, "No command associated");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
L = cbd->L;
|
|
|
|
ctx = cbd->ctx;
|
|
|
|
|
|
|
|
if (!rspamd_controller_check_password (conn_ent, session, msg,
|
|
|
|
cbd->is_enable)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (cbd->need_task && (rspamd_http_message_get_body (msg, NULL) == NULL)) {
|
|
|
|
msg_err_session ("got zero length body, cannot continue");
|
|
|
|
rspamd_controller_send_error (conn_ent,
|
|
|
|
400,
|
|
|
|
"Empty body is not permitted");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-01-01 19:58:01 +01:00
|
|
|
task = rspamd_task_new (session->ctx->worker, session->cfg, session->pool,
|
2019-06-19 15:13:39 +02:00
|
|
|
ctx->lang_det, ctx->event_loop);
|
2017-01-07 19:58:14 +01:00
|
|
|
|
|
|
|
task->resolver = ctx->resolver;
|
|
|
|
task->s = rspamd_session_create (session->pool,
|
|
|
|
rspamd_controller_lua_fin_task,
|
|
|
|
NULL,
|
|
|
|
(event_finalizer_t )rspamd_task_free,
|
|
|
|
task);
|
|
|
|
task->fin_arg = conn_ent;
|
|
|
|
task->http_conn = rspamd_http_connection_ref (conn_ent->conn);;
|
|
|
|
task->sock = -1;
|
|
|
|
session->task = task;
|
|
|
|
|
|
|
|
if (msg->body_buf.len > 0) {
|
|
|
|
if (!rspamd_task_load_message (task, msg, msg->body_buf.begin, msg->body_buf.len)) {
|
|
|
|
rspamd_controller_send_error (conn_ent, task->err->code, "%s",
|
|
|
|
task->err->message);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-09 17:15:38 +01:00
|
|
|
/* Callback */
|
|
|
|
lua_rawgeti (L, LUA_REGISTRYINDEX, cbd->handler->idx);
|
|
|
|
|
2017-01-07 19:58:14 +01:00
|
|
|
ptask = lua_newuserdata (L, sizeof (*ptask));
|
|
|
|
rspamd_lua_setclass (L, "rspamd{task}", -1);
|
|
|
|
*ptask = task;
|
|
|
|
|
|
|
|
pconn = lua_newuserdata (L, sizeof (*pconn));
|
|
|
|
rspamd_lua_setclass (L, "rspamd{csession}", -1);
|
|
|
|
*pconn = conn_ent;
|
|
|
|
|
|
|
|
if (lua_pcall (L, 2, 0, 0) != 0) {
|
|
|
|
rspamd_controller_send_error (conn_ent, 503, "Cannot run callback: %s",
|
|
|
|
lua_tostring (L, -1));
|
|
|
|
lua_settop (L, 0);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
rspamd_session_pending (task->s);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-30 18:23:37 +02:00
|
|
|
static void
|
2014-07-23 13:57:31 +02:00
|
|
|
rspamd_controller_error_handler (struct rspamd_http_connection_entry *conn_ent,
|
|
|
|
GError *err)
|
2014-04-30 18:23:37 +02:00
|
|
|
{
|
2015-08-30 13:35:18 +02:00
|
|
|
struct rspamd_controller_session *session = conn_ent->ud;
|
|
|
|
|
|
|
|
msg_err_session ("http error occurred: %s", err->message);
|
2014-04-30 18:23:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2014-04-30 18:23:58 +02:00
|
|
|
rspamd_controller_finish_handler (struct rspamd_http_connection_entry *conn_ent)
|
2013-01-05 17:29:26 +01:00
|
|
|
{
|
2014-07-23 13:57:31 +02:00
|
|
|
struct rspamd_controller_session *session = conn_ent->ud;
|
2014-04-30 18:23:37 +02:00
|
|
|
|
2014-10-04 21:44:55 +02:00
|
|
|
session->ctx->worker->srv->stat->control_connections_count++;
|
2015-10-26 23:22:09 +01:00
|
|
|
|
2014-04-30 18:23:37 +02:00
|
|
|
if (session->task != NULL) {
|
2015-05-26 15:00:03 +02:00
|
|
|
rspamd_session_destroy (session->task->s);
|
2013-01-05 17:29:26 +01:00
|
|
|
}
|
2015-10-26 23:22:09 +01:00
|
|
|
|
2017-01-21 14:16:47 +01:00
|
|
|
session->wrk->nconns --;
|
2017-04-29 14:15:21 +02:00
|
|
|
rspamd_inet_address_free (session->from_addr);
|
2017-01-21 14:16:47 +01:00
|
|
|
REF_RELEASE (session->cfg);
|
|
|
|
|
2014-04-30 19:28:42 +02:00
|
|
|
if (session->pool) {
|
2016-10-26 09:19:05 +02:00
|
|
|
msg_debug_session ("destroy session %p", session);
|
2017-01-21 14:16:47 +01:00
|
|
|
rspamd_mempool_delete (session->pool);
|
2014-04-30 19:28:42 +02:00
|
|
|
}
|
2013-01-05 17:29:26 +01:00
|
|
|
|
2017-10-27 12:21:40 +02:00
|
|
|
g_free (session);
|
2013-01-05 17:29:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2019-06-19 18:07:56 +02:00
|
|
|
rspamd_controller_accept_socket (EV_P_ ev_io *w, int revents)
|
2013-01-05 17:29:26 +01:00
|
|
|
{
|
2019-06-19 18:07:56 +02:00
|
|
|
struct rspamd_worker *worker = (struct rspamd_worker *)w->data;
|
2014-07-23 13:57:31 +02:00
|
|
|
struct rspamd_controller_worker_ctx *ctx;
|
2015-08-30 13:35:18 +02:00
|
|
|
struct rspamd_controller_session *session;
|
2015-03-12 15:18:57 +01:00
|
|
|
rspamd_inet_addr_t *addr;
|
2014-07-23 13:57:31 +02:00
|
|
|
gint nfd;
|
2014-04-30 18:23:37 +02:00
|
|
|
|
|
|
|
ctx = worker->ctx;
|
|
|
|
|
|
|
|
if ((nfd =
|
2019-06-19 18:07:56 +02:00
|
|
|
rspamd_accept_from_socket (w->fd, &addr,
|
|
|
|
rspamd_worker_throttle_accept_events, worker->accept_events)) == -1) {
|
2015-08-30 13:35:18 +02:00
|
|
|
msg_warn_ctx ("accept failed: %s", strerror (errno));
|
2014-04-30 18:23:37 +02:00
|
|
|
return;
|
2013-01-05 17:29:26 +01:00
|
|
|
}
|
2014-04-30 18:23:37 +02:00
|
|
|
/* Check for EAGAIN */
|
|
|
|
if (nfd == 0) {
|
|
|
|
return;
|
2013-01-05 17:29:26 +01:00
|
|
|
}
|
2014-04-30 18:23:37 +02:00
|
|
|
|
2017-10-27 12:21:40 +02:00
|
|
|
session = g_malloc0 (sizeof (struct rspamd_controller_session));
|
2015-08-30 13:35:18 +02:00
|
|
|
session->pool = rspamd_mempool_new (rspamd_mempool_suggest_size (),
|
|
|
|
"csession");
|
|
|
|
session->ctx = ctx;
|
2015-11-18 17:01:05 +01:00
|
|
|
session->cfg = ctx->cfg;
|
2018-01-19 08:43:21 +01:00
|
|
|
session->lang_det = ctx->lang_det;
|
2015-11-18 17:01:05 +01:00
|
|
|
REF_RETAIN (session->cfg);
|
2014-04-30 18:23:37 +02:00
|
|
|
|
2015-08-30 13:35:18 +02:00
|
|
|
session->from_addr = addr;
|
2015-10-12 13:19:14 +02:00
|
|
|
session->wrk = worker;
|
|
|
|
worker->nconns ++;
|
2014-04-30 18:23:37 +02:00
|
|
|
|
2015-08-30 13:35:18 +02:00
|
|
|
rspamd_http_router_handle_socket (ctx->http, nfd, session);
|
2013-01-05 17:29:26 +01:00
|
|
|
}
|
|
|
|
|
2015-09-18 18:15:18 +02:00
|
|
|
static void
|
2019-06-19 18:07:56 +02:00
|
|
|
rspamd_controller_rrd_update (EV_P_ ev_timer *w, int revents)
|
2015-09-18 18:15:18 +02:00
|
|
|
{
|
2019-06-19 18:07:56 +02:00
|
|
|
struct rspamd_controller_worker_ctx *ctx =
|
|
|
|
(struct rspamd_controller_worker_ctx *)w->data;
|
2015-09-18 18:15:18 +02:00
|
|
|
struct rspamd_stat *stat;
|
|
|
|
GArray ar;
|
2017-01-03 14:32:14 +01:00
|
|
|
gdouble points[METRIC_ACTION_MAX];
|
2015-09-18 18:15:18 +02:00
|
|
|
GError *err = NULL;
|
2017-01-03 14:32:14 +01:00
|
|
|
guint i;
|
2015-09-18 18:15:18 +02:00
|
|
|
|
|
|
|
g_assert (ctx->rrd != NULL);
|
|
|
|
stat = ctx->srv->stat;
|
|
|
|
|
2017-01-03 14:32:14 +01:00
|
|
|
for (i = METRIC_ACTION_REJECT; i < METRIC_ACTION_MAX; i ++) {
|
|
|
|
points[i] = stat->actions_stat[i];
|
2015-09-18 18:15:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ar.data = (gchar *)points;
|
|
|
|
ar.len = sizeof (points);
|
|
|
|
|
|
|
|
if (!rspamd_rrd_add_record (ctx->rrd, &ar, rspamd_get_calendar_ticks (),
|
|
|
|
&err)) {
|
|
|
|
msg_err_ctx ("cannot update rrd file: %e", err);
|
|
|
|
g_error_free (err);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Plan new event */
|
2019-06-19 18:07:56 +02:00
|
|
|
ev_timer_again (ctx->event_loop, &ctx->rrd_event);
|
2015-09-18 18:15:18 +02:00
|
|
|
}
|
|
|
|
|
2015-09-15 18:34:36 +02:00
|
|
|
static void
|
|
|
|
rspamd_controller_load_saved_stats (struct rspamd_controller_worker_ctx *ctx)
|
|
|
|
{
|
|
|
|
struct ucl_parser *parser;
|
|
|
|
ucl_object_t *obj;
|
|
|
|
const ucl_object_t *elt, *subelt;
|
|
|
|
struct rspamd_stat *stat, stat_copy;
|
|
|
|
gint i;
|
|
|
|
|
|
|
|
g_assert (ctx->saved_stats_path != NULL);
|
|
|
|
|
|
|
|
if (access (ctx->saved_stats_path, R_OK) == -1) {
|
|
|
|
msg_err_ctx ("cannot load controller stats from %s: %s",
|
|
|
|
ctx->saved_stats_path, strerror (errno));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
parser = ucl_parser_new (0);
|
|
|
|
|
|
|
|
if (!ucl_parser_add_file (parser, ctx->saved_stats_path)) {
|
|
|
|
msg_err_ctx ("cannot parse controller stats from %s: %s",
|
|
|
|
ctx->saved_stats_path, ucl_parser_get_error (parser));
|
|
|
|
ucl_parser_free (parser);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
obj = ucl_parser_get_object (parser);
|
|
|
|
ucl_parser_free (parser);
|
|
|
|
|
|
|
|
stat = ctx->srv->stat;
|
|
|
|
memcpy (&stat_copy, stat, sizeof (stat_copy));
|
|
|
|
|
2016-02-16 10:59:52 +01:00
|
|
|
elt = ucl_object_lookup (obj, "scanned");
|
2015-09-15 18:34:36 +02:00
|
|
|
|
|
|
|
if (elt != NULL && ucl_object_type (elt) == UCL_INT) {
|
|
|
|
stat_copy.messages_scanned = ucl_object_toint (elt);
|
|
|
|
}
|
|
|
|
|
2016-02-16 10:59:52 +01:00
|
|
|
elt = ucl_object_lookup (obj, "learned");
|
2015-09-25 14:46:42 +02:00
|
|
|
|
|
|
|
if (elt != NULL && ucl_object_type (elt) == UCL_INT) {
|
|
|
|
stat_copy.messages_learned = ucl_object_toint (elt);
|
|
|
|
}
|
|
|
|
|
2016-02-16 10:59:52 +01:00
|
|
|
elt = ucl_object_lookup (obj, "actions");
|
2015-09-15 18:34:36 +02:00
|
|
|
|
|
|
|
if (elt != NULL) {
|
|
|
|
for (i = METRIC_ACTION_REJECT; i <= METRIC_ACTION_NOACTION; i++) {
|
2016-02-16 10:59:52 +01:00
|
|
|
subelt = ucl_object_lookup (elt, rspamd_action_to_str (i));
|
2015-09-15 18:34:36 +02:00
|
|
|
|
|
|
|
if (subelt && ucl_object_type (subelt) == UCL_INT) {
|
|
|
|
stat_copy.actions_stat[i] = ucl_object_toint (subelt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-16 10:59:52 +01:00
|
|
|
elt = ucl_object_lookup (obj, "connections_count");
|
2015-09-15 18:34:36 +02:00
|
|
|
|
|
|
|
if (elt != NULL && ucl_object_type (elt) == UCL_INT) {
|
|
|
|
stat_copy.connections_count = ucl_object_toint (elt);
|
|
|
|
}
|
|
|
|
|
2016-02-16 10:59:52 +01:00
|
|
|
elt = ucl_object_lookup (obj, "control_connections_count");
|
2015-09-15 18:34:36 +02:00
|
|
|
|
|
|
|
if (elt != NULL && ucl_object_type (elt) == UCL_INT) {
|
|
|
|
stat_copy.control_connections_count = ucl_object_toint (elt);
|
|
|
|
}
|
|
|
|
|
|
|
|
ucl_object_unref (obj);
|
|
|
|
memcpy (stat, &stat_copy, sizeof (stat_copy));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
rspamd_controller_store_saved_stats (struct rspamd_controller_worker_ctx *ctx)
|
|
|
|
{
|
|
|
|
struct rspamd_stat *stat;
|
|
|
|
ucl_object_t *top, *sub;
|
2018-07-30 14:10:57 +02:00
|
|
|
struct ucl_emitter_functions *efuncs;
|
2015-09-15 18:34:36 +02:00
|
|
|
gint i, fd;
|
|
|
|
|
|
|
|
g_assert (ctx->saved_stats_path != NULL);
|
|
|
|
|
|
|
|
fd = open (ctx->saved_stats_path, O_WRONLY|O_CREAT|O_TRUNC, 00644);
|
|
|
|
|
|
|
|
if (fd == -1) {
|
2016-08-24 22:27:16 +02:00
|
|
|
msg_err_ctx ("cannot open for writing controller stats from %s: %s",
|
2015-09-15 18:34:36 +02:00
|
|
|
ctx->saved_stats_path, strerror (errno));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rspamd_file_lock (fd, FALSE) == -1) {
|
2016-08-24 22:27:16 +02:00
|
|
|
msg_err_ctx ("cannot lock controller stats in %s: %s",
|
2015-09-15 18:34:36 +02:00
|
|
|
ctx->saved_stats_path, strerror (errno));
|
|
|
|
close (fd);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
stat = ctx->srv->stat;
|
|
|
|
|
|
|
|
top = ucl_object_typed_new (UCL_OBJECT);
|
|
|
|
ucl_object_insert_key (top, ucl_object_fromint (
|
|
|
|
stat->messages_scanned), "scanned", 0, false);
|
2015-09-25 14:46:42 +02:00
|
|
|
ucl_object_insert_key (top, ucl_object_fromint (
|
|
|
|
stat->messages_learned), "learned", 0, false);
|
2015-09-15 18:34:36 +02:00
|
|
|
|
|
|
|
if (stat->messages_scanned > 0) {
|
|
|
|
sub = ucl_object_typed_new (UCL_OBJECT);
|
|
|
|
for (i = METRIC_ACTION_REJECT; i <= METRIC_ACTION_NOACTION; i++) {
|
|
|
|
ucl_object_insert_key (sub,
|
|
|
|
ucl_object_fromint (stat->actions_stat[i]),
|
|
|
|
rspamd_action_to_str (i), 0, false);
|
|
|
|
}
|
|
|
|
ucl_object_insert_key (top, sub, "actions", 0, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
ucl_object_insert_key (top,
|
|
|
|
ucl_object_fromint (stat->connections_count), "connections", 0, false);
|
|
|
|
ucl_object_insert_key (top,
|
|
|
|
ucl_object_fromint (stat->control_connections_count),
|
|
|
|
"control_connections", 0, false);
|
|
|
|
|
|
|
|
|
2018-07-30 14:10:57 +02:00
|
|
|
efuncs = ucl_object_emit_fd_funcs (fd);
|
2015-09-15 18:34:36 +02:00
|
|
|
ucl_object_emit_full (top, UCL_EMIT_JSON_COMPACT,
|
2018-07-30 14:10:57 +02:00
|
|
|
efuncs, NULL);
|
2015-09-15 18:34:36 +02:00
|
|
|
|
2017-10-14 14:18:07 +02:00
|
|
|
ucl_object_unref (top);
|
2015-09-15 18:34:36 +02:00
|
|
|
rspamd_file_unlock (fd, FALSE);
|
|
|
|
close (fd);
|
2018-07-30 14:10:57 +02:00
|
|
|
ucl_object_emit_funcs_free (efuncs);
|
2015-09-15 18:34:36 +02:00
|
|
|
}
|
|
|
|
|
2017-10-14 13:44:23 +02:00
|
|
|
static void
|
2019-06-19 18:07:56 +02:00
|
|
|
rspamd_controller_stats_save_periodic (EV_P_ ev_timer *w, int revents)
|
2017-10-14 13:44:23 +02:00
|
|
|
{
|
2019-06-19 18:07:56 +02:00
|
|
|
struct rspamd_controller_worker_ctx *ctx =
|
|
|
|
(struct rspamd_controller_worker_ctx *)w->data;
|
2017-10-14 13:44:23 +02:00
|
|
|
|
|
|
|
rspamd_controller_store_saved_stats (ctx);
|
2019-06-19 18:07:56 +02:00
|
|
|
ev_timer_again (EV_A_ w);
|
2017-10-14 13:44:23 +02:00
|
|
|
}
|
|
|
|
|
2015-04-24 16:58:25 +02:00
|
|
|
static void
|
2015-08-30 13:35:18 +02:00
|
|
|
rspamd_controller_password_sane (struct rspamd_controller_worker_ctx *ctx,
|
|
|
|
const gchar *password, const gchar *type)
|
2015-04-24 16:58:25 +02:00
|
|
|
{
|
|
|
|
const struct rspamd_controller_pbkdf *pbkdf = &pbkdf_list[0];
|
|
|
|
|
|
|
|
if (password == NULL) {
|
2015-08-30 13:35:18 +02:00
|
|
|
msg_warn_ctx ("%s is not set, so you should filter controller "
|
|
|
|
"availability "
|
2015-04-24 16:58:25 +02:00
|
|
|
"by using of firewall or `secure_ip` option", type);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_assert (pbkdf != NULL);
|
|
|
|
|
|
|
|
if (!rspamd_is_encrypted_password (password, NULL)) {
|
|
|
|
/* Suggest encryption to a user */
|
|
|
|
|
2015-09-15 19:01:27 +02:00
|
|
|
msg_warn_ctx ("your %s is not encrypted, we strongly "
|
|
|
|
"recommend to replace it with the encrypted one", type);
|
2015-04-24 16:58:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-03 19:57:38 +01:00
|
|
|
gpointer
|
2014-04-30 18:23:58 +02:00
|
|
|
init_controller_worker (struct rspamd_config *cfg)
|
2010-12-03 19:57:38 +01:00
|
|
|
{
|
2014-07-23 13:57:31 +02:00
|
|
|
struct rspamd_controller_worker_ctx *ctx;
|
|
|
|
GQuark type;
|
2010-12-03 19:57:38 +01:00
|
|
|
|
2014-05-01 00:14:03 +02:00
|
|
|
type = g_quark_try_string ("controller");
|
2014-04-30 18:23:37 +02:00
|
|
|
|
2017-01-17 17:52:45 +01:00
|
|
|
ctx = rspamd_mempool_alloc0 (cfg->cfg_pool,
|
|
|
|
sizeof (struct rspamd_controller_worker_ctx));
|
2010-12-03 19:57:38 +01:00
|
|
|
|
2016-04-04 17:07:14 +02:00
|
|
|
ctx->magic = rspamd_controller_ctx_magic;
|
2014-04-30 18:23:37 +02:00
|
|
|
ctx->timeout = DEFAULT_WORKER_IO_TIMEOUT;
|
2018-12-06 16:17:29 +01:00
|
|
|
ctx->task_timeout = NAN;
|
2010-12-03 19:57:38 +01:00
|
|
|
|
2015-12-27 13:58:23 +01:00
|
|
|
rspamd_rcl_register_worker_option (cfg,
|
|
|
|
type,
|
|
|
|
"password",
|
|
|
|
rspamd_rcl_parse_struct_string,
|
|
|
|
ctx,
|
|
|
|
G_STRUCT_OFFSET (struct rspamd_controller_worker_ctx, password),
|
|
|
|
0,
|
2015-12-29 19:06:25 +01:00
|
|
|
"Password for read-only commands");
|
2015-12-27 13:58:23 +01:00
|
|
|
|
|
|
|
rspamd_rcl_register_worker_option (cfg,
|
|
|
|
type,
|
|
|
|
"enable_password",
|
|
|
|
rspamd_rcl_parse_struct_string,
|
|
|
|
ctx,
|
|
|
|
G_STRUCT_OFFSET (struct rspamd_controller_worker_ctx,
|
|
|
|
enable_password),
|
|
|
|
0,
|
2015-12-29 19:06:25 +01:00
|
|
|
"Password for read and write commands");
|
2015-12-27 13:58:23 +01:00
|
|
|
|
|
|
|
rspamd_rcl_register_worker_option (cfg,
|
|
|
|
type,
|
|
|
|
"ssl",
|
|
|
|
rspamd_rcl_parse_struct_boolean,
|
|
|
|
ctx,
|
|
|
|
G_STRUCT_OFFSET (struct rspamd_controller_worker_ctx, use_ssl),
|
|
|
|
0,
|
2015-12-29 19:06:25 +01:00
|
|
|
"Unimplemented");
|
2015-12-27 13:58:23 +01:00
|
|
|
|
|
|
|
rspamd_rcl_register_worker_option (cfg,
|
|
|
|
type,
|
|
|
|
"ssl_cert",
|
|
|
|
rspamd_rcl_parse_struct_string,
|
|
|
|
ctx,
|
|
|
|
G_STRUCT_OFFSET (struct rspamd_controller_worker_ctx, ssl_cert),
|
|
|
|
0,
|
2015-12-29 19:06:25 +01:00
|
|
|
"Unimplemented");
|
2015-12-27 13:58:23 +01:00
|
|
|
|
|
|
|
rspamd_rcl_register_worker_option (cfg,
|
|
|
|
type,
|
|
|
|
"ssl_key",
|
|
|
|
rspamd_rcl_parse_struct_string,
|
|
|
|
ctx,
|
|
|
|
G_STRUCT_OFFSET (struct rspamd_controller_worker_ctx, ssl_key),
|
|
|
|
0,
|
2015-12-29 19:06:25 +01:00
|
|
|
"Unimplemented");
|
2015-12-27 13:58:23 +01:00
|
|
|
rspamd_rcl_register_worker_option (cfg,
|
|
|
|
type,
|
|
|
|
"timeout",
|
|
|
|
rspamd_rcl_parse_struct_time,
|
|
|
|
ctx,
|
|
|
|
G_STRUCT_OFFSET (struct rspamd_controller_worker_ctx,
|
|
|
|
timeout),
|
2019-06-19 18:07:56 +02:00
|
|
|
RSPAMD_CL_FLAG_TIME_FLOAT,
|
2015-12-29 19:06:25 +01:00
|
|
|
"Protocol timeout");
|
2015-12-27 13:58:23 +01:00
|
|
|
|
|
|
|
rspamd_rcl_register_worker_option (cfg,
|
|
|
|
type,
|
|
|
|
"secure_ip",
|
2016-05-10 16:29:05 +02:00
|
|
|
rspamd_rcl_parse_struct_ucl,
|
2015-12-27 13:58:23 +01:00
|
|
|
ctx,
|
|
|
|
G_STRUCT_OFFSET (struct rspamd_controller_worker_ctx, secure_ip),
|
|
|
|
0,
|
2015-12-29 19:06:25 +01:00
|
|
|
"List of IP addresses that are allowed for password-less access");
|
2015-12-27 13:58:23 +01:00
|
|
|
|
2016-03-18 10:55:14 +01:00
|
|
|
rspamd_rcl_register_worker_option (cfg,
|
|
|
|
type,
|
|
|
|
"trusted_ips",
|
2016-05-10 16:29:05 +02:00
|
|
|
rspamd_rcl_parse_struct_ucl,
|
2016-03-18 10:55:14 +01:00
|
|
|
ctx,
|
|
|
|
G_STRUCT_OFFSET (struct rspamd_controller_worker_ctx, secure_ip),
|
|
|
|
0,
|
|
|
|
"List of IP addresses that are allowed for password-less access");
|
|
|
|
|
2015-12-27 13:58:23 +01:00
|
|
|
rspamd_rcl_register_worker_option (cfg,
|
|
|
|
type,
|
|
|
|
"static_dir",
|
|
|
|
rspamd_rcl_parse_struct_string,
|
|
|
|
ctx,
|
|
|
|
G_STRUCT_OFFSET (struct rspamd_controller_worker_ctx,
|
|
|
|
static_files_dir),
|
|
|
|
0,
|
2015-12-29 19:06:25 +01:00
|
|
|
"Directory for static files served by controller's HTTP server");
|
2015-12-27 13:58:23 +01:00
|
|
|
|
|
|
|
rspamd_rcl_register_worker_option (cfg,
|
|
|
|
type,
|
|
|
|
"keypair",
|
|
|
|
rspamd_rcl_parse_struct_keypair,
|
|
|
|
ctx,
|
|
|
|
G_STRUCT_OFFSET (struct rspamd_controller_worker_ctx,
|
|
|
|
key),
|
|
|
|
0,
|
2015-12-29 19:06:25 +01:00
|
|
|
"Encryption keypair");
|
2015-12-27 13:58:23 +01:00
|
|
|
|
|
|
|
rspamd_rcl_register_worker_option (cfg,
|
|
|
|
type,
|
|
|
|
"stats_path",
|
|
|
|
rspamd_rcl_parse_struct_string,
|
|
|
|
ctx,
|
|
|
|
G_STRUCT_OFFSET (struct rspamd_controller_worker_ctx,
|
|
|
|
saved_stats_path),
|
|
|
|
0,
|
2015-12-29 19:06:25 +01:00
|
|
|
"Directory where controller saves server's statistics between restarts");
|
2015-09-15 18:34:36 +02:00
|
|
|
|
2018-12-06 16:17:29 +01:00
|
|
|
rspamd_rcl_register_worker_option (cfg,
|
|
|
|
type,
|
|
|
|
"task_timeout",
|
|
|
|
rspamd_rcl_parse_struct_time,
|
|
|
|
ctx,
|
|
|
|
G_STRUCT_OFFSET (struct rspamd_controller_worker_ctx,
|
|
|
|
task_timeout),
|
|
|
|
RSPAMD_CL_FLAG_TIME_FLOAT,
|
|
|
|
"Maximum task processing time, default: 8.0 seconds");
|
|
|
|
|
2010-12-03 19:57:38 +01:00
|
|
|
return ctx;
|
|
|
|
}
|
|
|
|
|
2016-09-24 21:42:12 +02:00
|
|
|
/* Lua bindings */
|
|
|
|
LUA_FUNCTION_DEF (csession, get_ev_base);
|
|
|
|
LUA_FUNCTION_DEF (csession, get_cfg);
|
|
|
|
LUA_FUNCTION_DEF (csession, send_ucl);
|
|
|
|
LUA_FUNCTION_DEF (csession, send_string);
|
|
|
|
LUA_FUNCTION_DEF (csession, send_error);
|
|
|
|
|
|
|
|
static const struct luaL_reg lua_csessionlib_m[] = {
|
|
|
|
LUA_INTERFACE_DEF (csession, get_ev_base),
|
|
|
|
LUA_INTERFACE_DEF (csession, get_cfg),
|
|
|
|
LUA_INTERFACE_DEF (csession, send_ucl),
|
|
|
|
LUA_INTERFACE_DEF (csession, send_string),
|
|
|
|
LUA_INTERFACE_DEF (csession, send_error),
|
|
|
|
{"__tostring", rspamd_lua_class_tostring},
|
|
|
|
{NULL, NULL}
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Basic functions of LUA API for worker object */
|
|
|
|
static void
|
|
|
|
luaopen_controller (lua_State * L)
|
|
|
|
{
|
2017-01-09 17:15:38 +01:00
|
|
|
rspamd_lua_new_class (L, "rspamd{csession}", lua_csessionlib_m);
|
2016-09-24 21:42:12 +02:00
|
|
|
lua_pop (L, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct rspamd_http_connection_entry *
|
|
|
|
lua_check_controller_entry (lua_State * L, gint pos)
|
|
|
|
{
|
|
|
|
void *ud = luaL_checkudata (L, pos, "rspamd{csession}");
|
|
|
|
luaL_argcheck (L, ud != NULL, pos, "'csession' expected");
|
|
|
|
return ud ? *((struct rspamd_http_connection_entry **)ud) : NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
lua_csession_get_ev_base (lua_State *L)
|
|
|
|
{
|
|
|
|
struct rspamd_http_connection_entry *c = lua_check_controller_entry (L, 1);
|
2019-06-15 17:37:58 +02:00
|
|
|
struct ev_loop **pbase;
|
2016-09-24 21:42:12 +02:00
|
|
|
struct rspamd_controller_session *s;
|
|
|
|
|
|
|
|
if (c) {
|
|
|
|
s = c->ud;
|
2019-06-15 17:37:58 +02:00
|
|
|
pbase = lua_newuserdata (L, sizeof (struct ev_loop *));
|
2016-09-24 21:42:12 +02:00
|
|
|
rspamd_lua_setclass (L, "rspamd{ev_base}", -1);
|
2019-06-19 15:13:39 +02:00
|
|
|
*pbase = s->ctx->event_loop;
|
2016-09-24 21:42:12 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
return luaL_error (L, "invalid arguments");
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
lua_csession_get_cfg (lua_State *L)
|
|
|
|
{
|
|
|
|
struct rspamd_http_connection_entry *c = lua_check_controller_entry (L, 1);
|
|
|
|
struct rspamd_config **pcfg;
|
|
|
|
struct rspamd_controller_session *s;
|
|
|
|
|
|
|
|
if (c) {
|
|
|
|
s = c->ud;
|
|
|
|
pcfg = lua_newuserdata (L, sizeof (gpointer));
|
|
|
|
rspamd_lua_setclass (L, "rspamd{config}", -1);
|
|
|
|
*pcfg = s->ctx->cfg;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return luaL_error (L, "invalid arguments");
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
lua_csession_send_ucl (lua_State *L)
|
|
|
|
{
|
|
|
|
struct rspamd_http_connection_entry *c = lua_check_controller_entry (L, 1);
|
2017-04-07 12:11:31 +02:00
|
|
|
ucl_object_t *obj = ucl_object_lua_import_escape (L, 2);
|
2016-09-24 21:42:12 +02:00
|
|
|
|
|
|
|
if (c) {
|
|
|
|
rspamd_controller_send_ucl (c, obj);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ucl_object_unref (obj);
|
|
|
|
return luaL_error (L, "invalid arguments");
|
|
|
|
}
|
|
|
|
|
|
|
|
ucl_object_unref (obj);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
lua_csession_send_error (lua_State *L)
|
|
|
|
{
|
|
|
|
struct rspamd_http_connection_entry *c = lua_check_controller_entry (L, 1);
|
|
|
|
guint err_code = lua_tonumber (L, 2);
|
|
|
|
const gchar *err_str = lua_tostring (L, 3);
|
|
|
|
|
|
|
|
if (c) {
|
|
|
|
rspamd_controller_send_error (c, err_code, "%s", err_str);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return luaL_error (L, "invalid arguments");
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
lua_csession_send_string (lua_State *L)
|
|
|
|
{
|
|
|
|
struct rspamd_http_connection_entry *c = lua_check_controller_entry (L, 1);
|
2018-07-17 16:27:06 +02:00
|
|
|
const gchar *str = lua_tostring (L, 2);
|
2016-09-24 21:42:12 +02:00
|
|
|
|
|
|
|
if (c) {
|
|
|
|
rspamd_controller_send_string (c, str);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return luaL_error (L, "invalid arguments");
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-06-20 22:56:11 +02:00
|
|
|
static void
|
2016-08-24 22:27:16 +02:00
|
|
|
rspamd_controller_on_terminate (struct rspamd_worker *worker)
|
|
|
|
{
|
|
|
|
struct rspamd_controller_worker_ctx *ctx = worker->ctx;
|
|
|
|
|
|
|
|
rspamd_controller_store_saved_stats (ctx);
|
|
|
|
|
|
|
|
if (ctx->rrd) {
|
2016-08-24 22:45:20 +02:00
|
|
|
msg_info ("closing rrd file: %s", ctx->rrd->filename);
|
2019-06-19 18:07:56 +02:00
|
|
|
ev_timer_stop (ctx->event_loop, &ctx->rrd_event);
|
2016-08-24 22:27:16 +02:00
|
|
|
rspamd_rrd_close (ctx->rrd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-07 19:58:14 +01:00
|
|
|
static void
|
|
|
|
rspamd_plugin_cbdata_dtor (gpointer p)
|
|
|
|
{
|
|
|
|
struct rspamd_controller_plugin_cbdata *cbd = p;
|
|
|
|
|
|
|
|
g_free (cbd->plugin);
|
|
|
|
ucl_object_unref (cbd->obj); /* This also releases lua references */
|
2017-10-27 12:21:40 +02:00
|
|
|
g_free (cbd);
|
2017-01-07 19:58:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
rspamd_controller_register_plugin_path (lua_State *L,
|
|
|
|
struct rspamd_controller_worker_ctx *ctx,
|
|
|
|
const ucl_object_t *webui_data,
|
|
|
|
const ucl_object_t *handler,
|
|
|
|
const gchar *path,
|
|
|
|
const gchar *plugin_name)
|
|
|
|
{
|
|
|
|
struct rspamd_controller_plugin_cbdata *cbd;
|
|
|
|
const ucl_object_t *elt;
|
|
|
|
GString *full_path;
|
|
|
|
|
2017-10-27 12:21:40 +02:00
|
|
|
cbd = g_malloc0 (sizeof (*cbd));
|
2017-01-07 19:58:14 +01:00
|
|
|
cbd->L = L;
|
|
|
|
cbd->ctx = ctx;
|
|
|
|
cbd->handler = ucl_object_toclosure (handler);
|
|
|
|
cbd->plugin = g_strdup (plugin_name);
|
|
|
|
cbd->obj = ucl_object_ref (webui_data);
|
|
|
|
|
2017-01-09 17:08:00 +01:00
|
|
|
elt = ucl_object_lookup (webui_data, "version");
|
|
|
|
|
|
|
|
if (elt) {
|
|
|
|
cbd->version = ucl_object_toint (elt);
|
|
|
|
}
|
|
|
|
|
2017-01-07 19:58:14 +01:00
|
|
|
elt = ucl_object_lookup (webui_data, "enable");
|
|
|
|
|
2017-10-27 12:21:40 +02:00
|
|
|
if (elt && ucl_object_toboolean (elt)) {
|
2017-01-07 19:58:14 +01:00
|
|
|
cbd->is_enable = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
elt = ucl_object_lookup (webui_data, "need_task");
|
|
|
|
|
|
|
|
if (elt && !!ucl_object_toboolean (elt)) {
|
|
|
|
cbd->need_task = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
full_path = g_string_new ("/plugins/");
|
|
|
|
rspamd_printf_gstring (full_path, "%s/%s",
|
|
|
|
plugin_name, path);
|
|
|
|
|
|
|
|
rspamd_http_router_add_path (ctx->http,
|
|
|
|
full_path->str,
|
|
|
|
rspamd_controller_handle_lua_plugin);
|
|
|
|
g_hash_table_insert (ctx->plugins, full_path->str, cbd);
|
|
|
|
g_string_free (full_path, FALSE); /* Do not free data */
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
rspamd_controller_register_plugins_paths (struct rspamd_controller_worker_ctx *ctx)
|
|
|
|
{
|
|
|
|
lua_State *L = ctx->cfg->lua_state;
|
|
|
|
ucl_object_t *webui_data;
|
|
|
|
const ucl_object_t *handler_obj, *cur;
|
|
|
|
ucl_object_iter_t it = NULL;
|
|
|
|
|
|
|
|
lua_getglobal (L, "rspamd_plugins");
|
|
|
|
|
|
|
|
if (lua_istable (L, -1)) {
|
|
|
|
|
|
|
|
for (lua_pushnil (L); lua_next (L, -2); lua_pop (L, 2)) {
|
|
|
|
lua_pushvalue (L, -2); /* Store key */
|
|
|
|
|
|
|
|
lua_pushstring (L, "webui");
|
|
|
|
lua_gettable (L, -3); /* value is at -3 index */
|
|
|
|
|
|
|
|
if (lua_istable (L, -1)) {
|
2017-04-07 12:11:31 +02:00
|
|
|
webui_data = ucl_object_lua_import_escape (L, -1);
|
2017-01-07 19:58:14 +01:00
|
|
|
|
|
|
|
while ((cur = ucl_object_iterate (webui_data, &it, true)) != NULL) {
|
|
|
|
handler_obj = ucl_object_lookup (cur, "handler");
|
|
|
|
|
|
|
|
if (handler_obj && ucl_object_key (cur)) {
|
|
|
|
rspamd_controller_register_plugin_path (L, ctx,
|
|
|
|
cur, handler_obj, ucl_object_key (cur),
|
|
|
|
lua_tostring (L, -2));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
msg_err_ctx ("bad webui definition for plugin: %s",
|
|
|
|
lua_tostring (L, -2));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ucl_object_unref (webui_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
lua_pop (L, 1); /* remove table value */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
lua_pop (L, 1); /* rspamd_plugins global */
|
|
|
|
}
|
|
|
|
|
2014-04-30 18:23:37 +02:00
|
|
|
/*
|
|
|
|
* Start worker process
|
|
|
|
*/
|
2008-10-23 17:10:40 +02:00
|
|
|
void
|
2014-04-30 18:23:58 +02:00
|
|
|
start_controller_worker (struct rspamd_worker *worker)
|
2008-10-23 17:10:40 +02:00
|
|
|
{
|
2014-04-30 18:23:58 +02:00
|
|
|
struct rspamd_controller_worker_ctx *ctx = worker->ctx;
|
2014-04-30 18:23:37 +02:00
|
|
|
struct module_ctx *mctx;
|
|
|
|
GHashTableIter iter;
|
|
|
|
gpointer key, value;
|
2018-07-30 16:40:31 +02:00
|
|
|
guint i;
|
2019-06-19 18:07:56 +02:00
|
|
|
const ev_tstamp save_stats_interval = 60; /* 1 minute */
|
2015-10-06 17:45:47 +02:00
|
|
|
gpointer m;
|
2008-10-23 17:10:40 +02:00
|
|
|
|
2019-06-19 15:13:39 +02:00
|
|
|
ctx->event_loop = rspamd_prepare_worker (worker,
|
2014-07-23 13:57:31 +02:00
|
|
|
"controller",
|
2017-07-24 09:13:45 +02:00
|
|
|
rspamd_controller_accept_socket);
|
2008-12-18 18:31:21 +01:00
|
|
|
|
2019-06-19 18:07:56 +02:00
|
|
|
ctx->start_time = ev_time ();
|
2014-04-30 18:23:37 +02:00
|
|
|
ctx->worker = worker;
|
|
|
|
ctx->cfg = worker->srv->cfg;
|
2013-01-05 17:29:26 +01:00
|
|
|
ctx->srv = worker->srv;
|
2014-04-30 18:23:37 +02:00
|
|
|
ctx->custom_commands = g_hash_table_new (rspamd_strcase_hash,
|
|
|
|
rspamd_strcase_equal);
|
2017-01-07 19:58:14 +01:00
|
|
|
ctx->plugins = g_hash_table_new_full (rspamd_strcase_hash,
|
|
|
|
rspamd_strcase_equal, g_free,
|
|
|
|
rspamd_plugin_cbdata_dtor);
|
2015-03-20 15:19:20 +01:00
|
|
|
|
2018-12-06 16:17:29 +01:00
|
|
|
if (isnan (ctx->task_timeout)) {
|
|
|
|
if (isnan (ctx->cfg->task_timeout)) {
|
|
|
|
ctx->task_timeout = 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ctx->task_timeout = ctx->cfg->task_timeout;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-30 13:01:33 +02:00
|
|
|
if (ctx->secure_ip != NULL) {
|
2016-06-25 14:40:00 +02:00
|
|
|
rspamd_config_radix_from_ucl (ctx->cfg, ctx->secure_ip,
|
|
|
|
"Allow unauthenticated requests from these addresses",
|
|
|
|
&ctx->secure_map, NULL);
|
2014-04-30 18:23:37 +02:00
|
|
|
}
|
2015-04-24 16:58:25 +02:00
|
|
|
|
2015-09-15 18:34:36 +02:00
|
|
|
if (ctx->saved_stats_path == NULL) {
|
|
|
|
/* Assume default path */
|
|
|
|
ctx->saved_stats_path = rspamd_mempool_strdup (worker->srv->cfg->cfg_pool,
|
|
|
|
DEFAULT_STATS_PATH);
|
|
|
|
}
|
|
|
|
|
|
|
|
rspamd_controller_load_saved_stats (ctx);
|
2018-01-01 19:58:01 +01:00
|
|
|
ctx->lang_det = ctx->cfg->lang_det;
|
2015-09-15 18:34:36 +02:00
|
|
|
|
2015-09-18 18:15:18 +02:00
|
|
|
/* RRD collector */
|
2015-09-19 22:40:07 +02:00
|
|
|
if (ctx->cfg->rrd_file && worker->index == 0) {
|
2016-07-01 17:29:43 +02:00
|
|
|
GError *rrd_err = NULL;
|
|
|
|
|
|
|
|
ctx->rrd = rspamd_rrd_file_default (ctx->cfg->rrd_file, &rrd_err);
|
2015-09-18 18:15:18 +02:00
|
|
|
|
|
|
|
if (ctx->rrd) {
|
2019-06-19 18:07:56 +02:00
|
|
|
ctx->rrd_event.data = ctx;
|
|
|
|
ev_timer_init (&ctx->rrd_event, rspamd_controller_rrd_update,
|
|
|
|
rrd_update_time, rrd_update_time);
|
|
|
|
ev_timer_start (ctx->event_loop, &ctx->rrd_event);
|
2015-09-18 18:15:18 +02:00
|
|
|
}
|
2016-07-01 17:29:43 +02:00
|
|
|
else if (rrd_err) {
|
|
|
|
msg_err ("cannot load rrd from %s: %e", ctx->cfg->rrd_file,
|
|
|
|
rrd_err);
|
|
|
|
g_error_free (rrd_err);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
msg_err ("cannot load rrd from %s: unknown error", ctx->cfg->rrd_file);
|
|
|
|
}
|
2015-09-18 18:15:18 +02:00
|
|
|
}
|
2015-09-19 22:40:07 +02:00
|
|
|
else {
|
|
|
|
ctx->rrd = NULL;
|
|
|
|
}
|
2015-09-18 18:15:18 +02:00
|
|
|
|
2015-08-30 13:35:18 +02:00
|
|
|
rspamd_controller_password_sane (ctx, ctx->password, "normal password");
|
|
|
|
rspamd_controller_password_sane (ctx, ctx->enable_password, "enable "
|
|
|
|
"password");
|
2015-04-24 16:58:25 +02:00
|
|
|
|
2014-04-30 18:23:37 +02:00
|
|
|
/* Accept event */
|
2019-06-19 15:13:39 +02:00
|
|
|
ctx->http_ctx = rspamd_http_context_create (ctx->cfg, ctx->event_loop,
|
2019-03-18 15:07:32 +01:00
|
|
|
ctx->cfg->ups_ctx);
|
2014-04-30 18:23:58 +02:00
|
|
|
ctx->http = rspamd_http_router_new (rspamd_controller_error_handler,
|
2019-06-19 18:07:56 +02:00
|
|
|
rspamd_controller_finish_handler, ctx->timeout,
|
2019-02-19 18:57:13 +01:00
|
|
|
ctx->static_files_dir, ctx->http_ctx);
|
2014-04-30 18:23:37 +02:00
|
|
|
|
|
|
|
/* Add callbacks for different methods */
|
2014-07-23 13:57:31 +02:00
|
|
|
rspamd_http_router_add_path (ctx->http,
|
2015-09-21 20:14:24 +02:00
|
|
|
PATH_AUTH,
|
|
|
|
rspamd_controller_handle_auth);
|
|
|
|
rspamd_http_router_add_path (ctx->http,
|
|
|
|
PATH_SYMBOLS,
|
|
|
|
rspamd_controller_handle_symbols);
|
2014-07-23 13:57:31 +02:00
|
|
|
rspamd_http_router_add_path (ctx->http,
|
2015-09-21 20:14:24 +02:00
|
|
|
PATH_ACTIONS,
|
|
|
|
rspamd_controller_handle_actions);
|
2014-07-23 13:57:31 +02:00
|
|
|
rspamd_http_router_add_path (ctx->http,
|
2015-09-21 20:14:24 +02:00
|
|
|
PATH_MAPS,
|
|
|
|
rspamd_controller_handle_maps);
|
2014-07-23 13:57:31 +02:00
|
|
|
rspamd_http_router_add_path (ctx->http,
|
2015-09-21 20:14:24 +02:00
|
|
|
PATH_GET_MAP,
|
|
|
|
rspamd_controller_handle_get_map);
|
2014-07-23 13:57:31 +02:00
|
|
|
rspamd_http_router_add_path (ctx->http,
|
2015-09-21 20:14:24 +02:00
|
|
|
PATH_PIE_CHART,
|
|
|
|
rspamd_controller_handle_pie_chart);
|
2014-07-23 13:57:31 +02:00
|
|
|
rspamd_http_router_add_path (ctx->http,
|
2015-09-21 20:14:24 +02:00
|
|
|
PATH_GRAPH,
|
|
|
|
rspamd_controller_handle_graph);
|
2014-07-23 13:57:31 +02:00
|
|
|
rspamd_http_router_add_path (ctx->http,
|
2015-09-21 20:14:24 +02:00
|
|
|
PATH_HISTORY,
|
|
|
|
rspamd_controller_handle_history);
|
2015-10-03 15:20:55 +02:00
|
|
|
rspamd_http_router_add_path (ctx->http,
|
|
|
|
PATH_HISTORY_RESET,
|
|
|
|
rspamd_controller_handle_history_reset);
|
2014-07-23 13:57:31 +02:00
|
|
|
rspamd_http_router_add_path (ctx->http,
|
2015-09-21 20:14:24 +02:00
|
|
|
PATH_LEARN_SPAM,
|
|
|
|
rspamd_controller_handle_learnspam);
|
2014-07-23 13:57:31 +02:00
|
|
|
rspamd_http_router_add_path (ctx->http,
|
2015-09-21 20:14:24 +02:00
|
|
|
PATH_LEARN_HAM,
|
|
|
|
rspamd_controller_handle_learnham);
|
2014-07-23 13:57:31 +02:00
|
|
|
rspamd_http_router_add_path (ctx->http,
|
2015-09-21 20:14:24 +02:00
|
|
|
PATH_SAVE_ACTIONS,
|
|
|
|
rspamd_controller_handle_saveactions);
|
2014-07-23 13:57:31 +02:00
|
|
|
rspamd_http_router_add_path (ctx->http,
|
2015-09-21 20:14:24 +02:00
|
|
|
PATH_SAVE_SYMBOLS,
|
|
|
|
rspamd_controller_handle_savesymbols);
|
2014-07-23 13:57:31 +02:00
|
|
|
rspamd_http_router_add_path (ctx->http,
|
|
|
|
PATH_SAVE_MAP,
|
2015-09-21 20:14:24 +02:00
|
|
|
rspamd_controller_handle_savemap);
|
2014-07-23 13:57:31 +02:00
|
|
|
rspamd_http_router_add_path (ctx->http,
|
2015-09-21 20:14:24 +02:00
|
|
|
PATH_SCAN,
|
|
|
|
rspamd_controller_handle_scan);
|
2014-07-23 13:57:31 +02:00
|
|
|
rspamd_http_router_add_path (ctx->http,
|
2015-09-21 20:14:24 +02:00
|
|
|
PATH_CHECK,
|
|
|
|
rspamd_controller_handle_scan);
|
2017-05-24 13:15:48 +02:00
|
|
|
rspamd_http_router_add_path (ctx->http,
|
|
|
|
PATH_CHECKV2,
|
|
|
|
rspamd_controller_handle_scan);
|
2014-07-23 13:57:31 +02:00
|
|
|
rspamd_http_router_add_path (ctx->http,
|
2015-09-21 20:14:24 +02:00
|
|
|
PATH_STAT,
|
|
|
|
rspamd_controller_handle_stat);
|
2014-07-23 13:57:31 +02:00
|
|
|
rspamd_http_router_add_path (ctx->http,
|
2015-09-21 20:14:24 +02:00
|
|
|
PATH_STAT_RESET,
|
|
|
|
rspamd_controller_handle_statreset);
|
2014-07-23 13:57:31 +02:00
|
|
|
rspamd_http_router_add_path (ctx->http,
|
|
|
|
PATH_COUNTERS,
|
2015-09-21 20:14:24 +02:00
|
|
|
rspamd_controller_handle_counters);
|
2016-11-14 16:57:32 +01:00
|
|
|
rspamd_http_router_add_path (ctx->http,
|
|
|
|
PATH_ERRORS,
|
|
|
|
rspamd_controller_handle_errors);
|
2017-01-05 18:20:13 +01:00
|
|
|
rspamd_http_router_add_path (ctx->http,
|
|
|
|
PATH_NEIGHBOURS,
|
|
|
|
rspamd_controller_handle_neighbours);
|
2017-01-09 17:08:00 +01:00
|
|
|
rspamd_http_router_add_path (ctx->http,
|
|
|
|
PATH_PLUGINS,
|
|
|
|
rspamd_controller_handle_plugins);
|
2017-07-05 09:22:57 +02:00
|
|
|
rspamd_http_router_add_path (ctx->http,
|
|
|
|
PATH_PING,
|
|
|
|
rspamd_controller_handle_ping);
|
2017-01-07 19:58:14 +01:00
|
|
|
rspamd_controller_register_plugins_paths (ctx);
|
2014-04-30 18:23:37 +02:00
|
|
|
|
2017-02-10 11:28:37 +01:00
|
|
|
#if 0
|
2016-09-24 21:42:12 +02:00
|
|
|
rspamd_regexp_t *lua_re = rspamd_regexp_new ("^/.*/.*\\.lua$", NULL, NULL);
|
|
|
|
rspamd_http_router_add_regexp (ctx->http, lua_re,
|
|
|
|
rspamd_controller_handle_lua);
|
|
|
|
rspamd_regexp_unref (lua_re);
|
2017-02-10 11:28:37 +01:00
|
|
|
#endif
|
2016-09-24 21:42:12 +02:00
|
|
|
luaopen_controller (ctx->cfg->lua_state);
|
|
|
|
|
2015-02-01 23:44:20 +01:00
|
|
|
if (ctx->key) {
|
|
|
|
rspamd_http_router_set_key (ctx->http, ctx->key);
|
|
|
|
}
|
|
|
|
|
2018-07-30 16:40:31 +02:00
|
|
|
PTR_ARRAY_FOREACH (ctx->cfg->c_modules, i, mctx) {
|
2015-04-17 17:38:19 +02:00
|
|
|
if (mctx->mod->module_attach_controller_func != NULL) {
|
|
|
|
mctx->mod->module_attach_controller_func (mctx,
|
|
|
|
ctx->custom_commands);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-20 12:58:36 +02:00
|
|
|
g_hash_table_iter_init (&iter, ctx->custom_commands);
|
|
|
|
while (g_hash_table_iter_next (&iter, &key, &value)) {
|
|
|
|
rspamd_http_router_add_path (ctx->http,
|
|
|
|
key,
|
|
|
|
rspamd_controller_handle_custom);
|
|
|
|
}
|
|
|
|
|
2017-01-11 14:02:18 +01:00
|
|
|
if (worker->srv->cfg->neighbours && worker->srv->cfg->neighbours->len > 0) {
|
|
|
|
rspamd_http_router_add_header (ctx->http,
|
|
|
|
"Access-Control-Allow-Origin", "*");
|
|
|
|
}
|
2015-04-20 12:58:36 +02:00
|
|
|
|
2017-01-11 14:50:17 +01:00
|
|
|
rspamd_http_router_set_unknown_handler (ctx->http,
|
|
|
|
rspamd_controller_handle_unknown);
|
|
|
|
|
2019-04-11 16:00:01 +02:00
|
|
|
ctx->resolver = rspamd_dns_resolver_init (worker->srv->logger,
|
2019-06-19 15:13:39 +02:00
|
|
|
ctx->event_loop,
|
2014-07-23 13:57:31 +02:00
|
|
|
worker->srv->cfg);
|
2011-07-14 15:48:51 +02:00
|
|
|
|
2015-11-20 18:03:51 +01:00
|
|
|
rspamd_upstreams_library_config (worker->srv->cfg, worker->srv->cfg->ups_ctx,
|
2019-06-19 15:13:39 +02:00
|
|
|
ctx->event_loop, ctx->resolver->r);
|
|
|
|
rspamd_symcache_start_refresh (worker->srv->cfg->cache, ctx->event_loop,
|
2017-01-24 15:12:20 +01:00
|
|
|
worker);
|
2019-06-19 15:13:39 +02:00
|
|
|
rspamd_stat_init (worker->srv->cfg, ctx->event_loop);
|
2009-03-13 16:03:29 +01:00
|
|
|
|
2017-07-06 20:08:15 +02:00
|
|
|
if (worker->index == 0) {
|
2017-07-20 09:18:10 +02:00
|
|
|
if (!ctx->cfg->disable_monitored) {
|
2019-06-19 15:13:39 +02:00
|
|
|
rspamd_worker_init_monitored (worker, ctx->event_loop, ctx->resolver);
|
2017-07-20 09:18:10 +02:00
|
|
|
}
|
|
|
|
|
2019-06-19 15:13:39 +02:00
|
|
|
rspamd_map_watch (worker->srv->cfg, ctx->event_loop,
|
2018-06-19 12:46:35 +02:00
|
|
|
ctx->resolver, worker, TRUE);
|
2018-12-14 10:13:00 +01:00
|
|
|
|
|
|
|
/* Schedule periodic stats saving, see #1823 */
|
2019-06-19 18:07:56 +02:00
|
|
|
ctx->save_stats_event.data = ctx;
|
|
|
|
ev_timer_init (&ctx->save_stats_event,
|
2018-12-14 10:13:00 +01:00
|
|
|
rspamd_controller_stats_save_periodic,
|
2019-06-19 18:07:56 +02:00
|
|
|
save_stats_interval, save_stats_interval);
|
|
|
|
ev_timer_start (ctx->event_loop, &ctx->save_stats_event);
|
2017-07-07 19:54:50 +02:00
|
|
|
}
|
|
|
|
else {
|
2019-06-19 15:13:39 +02:00
|
|
|
rspamd_map_watch (worker->srv->cfg, ctx->event_loop,
|
2018-06-19 12:46:35 +02:00
|
|
|
ctx->resolver, worker, FALSE);
|
2017-07-06 20:08:15 +02:00
|
|
|
}
|
|
|
|
|
2019-06-19 15:13:39 +02:00
|
|
|
rspamd_lua_run_postloads (ctx->cfg->lua_state, ctx->cfg, ctx->event_loop, worker);
|
2017-07-24 09:13:45 +02:00
|
|
|
|
2019-06-21 14:46:01 +02:00
|
|
|
#ifdef WITH_HYPERSCAN
|
|
|
|
rspamd_control_worker_add_cmd_handler (worker,
|
|
|
|
RSPAMD_CONTROL_HYPERSCAN_LOADED,
|
|
|
|
rspamd_worker_hyperscan_ready,
|
|
|
|
NULL);
|
|
|
|
#endif
|
|
|
|
|
2017-07-24 09:13:45 +02:00
|
|
|
/* Start event loop */
|
2019-06-19 18:07:56 +02:00
|
|
|
ev_loop (ctx->event_loop, 0);
|
2015-10-15 23:38:34 +02:00
|
|
|
rspamd_worker_block_signals ();
|
2019-06-20 22:56:11 +02:00
|
|
|
rspamd_controller_on_terminate (worker);
|
2011-07-14 15:48:51 +02:00
|
|
|
|
2015-06-19 16:25:10 +02:00
|
|
|
rspamd_stat_close ();
|
2015-02-11 23:13:02 +01:00
|
|
|
rspamd_http_router_free (ctx->http);
|
2015-09-18 18:15:18 +02:00
|
|
|
|
2015-09-22 10:25:14 +02:00
|
|
|
if (ctx->cached_password.len > 0) {
|
2015-10-06 17:45:47 +02:00
|
|
|
m = (gpointer)ctx->cached_password.begin;
|
|
|
|
munmap (m, ctx->cached_password.len);
|
2015-09-22 10:25:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ctx->cached_enable_password.len > 0) {
|
2015-10-06 17:45:47 +02:00
|
|
|
m = (gpointer) ctx->cached_enable_password.begin;
|
|
|
|
munmap (m, ctx->cached_enable_password.len);
|
2015-09-22 10:25:14 +02:00
|
|
|
}
|
|
|
|
|
2017-01-07 19:58:14 +01:00
|
|
|
g_hash_table_unref (ctx->plugins);
|
|
|
|
g_hash_table_unref (ctx->custom_commands);
|
2019-02-26 17:29:45 +01:00
|
|
|
|
|
|
|
struct rspamd_http_context *http_ctx = ctx->http_ctx;
|
2017-01-10 14:59:27 +01:00
|
|
|
REF_RELEASE (ctx->cfg);
|
2019-02-26 17:29:45 +01:00
|
|
|
rspamd_http_context_free (http_ctx);
|
2018-08-30 15:03:01 +02:00
|
|
|
rspamd_log_close (worker->srv->logger, TRUE);
|
2016-11-28 13:34:27 +01:00
|
|
|
|
2009-03-13 16:03:29 +01:00
|
|
|
exit (EXIT_SUCCESS);
|
2008-10-23 17:10:40 +02:00
|
|
|
}
|