]> git.ipfire.org Git - thirdparty/rspamd.git/commitdiff
Implement custom commands in webui.
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Wed, 30 Apr 2014 13:31:59 +0000 (14:31 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Wed, 30 Apr 2014 13:31:59 +0000 (14:31 +0100)
src/libserver/worker_util.h
src/webui.c

index 67ad9e83cd4fa952d252eddc85949890e3f90705..c8b7c1000871549cac40ce25a5ea82c507f3fef8 100644 (file)
@@ -25,6 +25,7 @@
 
 #include "config.h"
 #include "util.h"
+#include "http.h"
 
 /**
  * Return worker's control structure by its type
@@ -64,4 +65,15 @@ rspamd_prepare_worker (struct rspamd_worker *worker, const char *name,
  */
 void rspamd_worker_stop_accept (struct rspamd_worker *worker);
 
+typedef gint (*rspamd_controller_func_t) (
+               struct rspamd_http_connection_entry *conn_ent,
+               struct rspamd_http_message *msg);
+
+struct rspamd_custom_controller_command {
+       const gchar *command;
+       gboolean privilleged;
+       gboolean require_message;
+       rspamd_controller_func_t handler;
+};
+
 #endif /* WORKER_UTIL_H_ */
index 852cf2e1141eb2cf9128563c95789068b5ddb32b..3c276e3123cfd78585deb830380633e8c4827c5f 100644 (file)
@@ -118,6 +118,9 @@ struct rspamd_webui_worker_ctx {
        /* Static files dir */
        gchar *static_files_dir;
 
+       /* Custom commands registered by plugins */
+       GHashTable *custom_commands;
+
        /* Worker */
        struct rspamd_worker *worker;
 };
@@ -1507,6 +1510,32 @@ rspamd_webui_handle_counters (struct rspamd_http_connection_entry *conn_ent,
        return 0;
 }
 
+static int
+rspamd_webui_handle_custom (struct rspamd_http_connection_entry *conn_ent,
+               struct rspamd_http_message *msg)
+{
+       struct rspamd_webui_session                     *session = conn_ent->ud;
+       struct rspamd_custom_controller_command *cmd;
+
+       cmd = g_hash_table_lookup (session->ctx->custom_commands, msg->url->str);
+       if (cmd == NULL || cmd->handler == NULL) {
+               msg_err ("custom command %V has not been found", msg->url);
+               rspamd_webui_send_error (conn_ent, 404, "No command associated");
+               return 0;
+       }
+
+       if (!rspamd_webui_check_password (conn_ent, session, msg, cmd->privilleged)) {
+               return 0;
+       }
+       if (cmd->require_message && msg->body->len == 0) {
+               msg_err ("got zero length body, cannot continue");
+               rspamd_webui_send_error (conn_ent, 400, "Empty body is not permitted");
+               return 0;
+       }
+
+       return cmd->handler (conn_ent, msg);
+}
+
 static void
 rspamd_webui_error_handler (struct rspamd_http_connection_entry *conn_ent, GError *err)
 {
@@ -1615,6 +1644,12 @@ void
 start_webui_worker (struct rspamd_worker *worker)
 {
        struct rspamd_webui_worker_ctx *ctx = worker->ctx;
+       GList *cur;
+       struct filter *f;
+       struct module_ctx *mctx;
+       GHashTableIter iter;
+       gpointer key, value;
+
 
        ctx->ev_base = rspamd_prepare_worker (worker, "controller", rspamd_webui_accept_socket);
        msec_to_tv (ctx->timeout, &ctx->io_tv);
@@ -1623,6 +1658,8 @@ start_webui_worker (struct rspamd_worker *worker)
        ctx->worker = worker;
        ctx->cfg = worker->srv->cfg;
        ctx->srv = worker->srv;
+       ctx->custom_commands = g_hash_table_new (rspamd_strcase_hash,
+                       rspamd_strcase_equal);
        if (ctx->secure_ip != NULL) {
                if (!add_map (worker->srv->cfg, ctx->secure_ip, "Allow webui access from the specified IP",
                                read_radix_list, fin_radix_list, (void **)&ctx->secure_map)) {
@@ -1654,6 +1691,22 @@ start_webui_worker (struct rspamd_worker *worker)
        rspamd_http_router_add_path (ctx->http, PATH_STAT_RESET, rspamd_webui_handle_statreset);
        rspamd_http_router_add_path (ctx->http, PATH_COUNTERS, rspamd_webui_handle_counters);
 
+       /* Attach plugins */
+       cur = g_list_first (ctx->cfg->filters);
+       while (cur) {
+               f = cur->data;
+               mctx = g_hash_table_lookup (ctx->cfg->c_modules, f->module->name);
+               if (mctx != NULL && f->module->module_init_func != NULL) {
+                       f->module->module_attach_controller_func (mctx, ctx->custom_commands);
+               }
+               cur = g_list_next (cur);
+       }
+
+       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_webui_handle_custom);
+       }
+
 #if 0
        rspamd_http_router_add_path (ctx->http, PATH_GRAPH, rspamd_webui_handle_graph, ctx);
 #endif