1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
|
/*****
*
* Copyright (C) 2002-2020 CS GROUP - France. All Rights Reserved.
* Author: Nicolas Delon <nicolas.delon@prelude-ids.com>
* Author: Krzysztof Zaraska <kzaraska@student.uci.agh.edu.pl>
*
* This file is part of the Prelude-Manager program.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*****/
#include "config.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include <sys/types.h>
#include <libprelude/idmef.h>
#include <libprelude/prelude-inttypes.h>
#include <libprelude/prelude-error.h>
#include <libprelude/idmef.h>
#include <libprelude/prelude-error.h>
#include <libpreludedb/preludedb-sql-settings.h>
#include <libpreludedb/preludedb-sql.h>
#include <libpreludedb/preludedb-error.h>
#include <libpreludedb/preludedb-path-selection.h>
#include <libpreludedb/preludedb.h>
#include "prelude-manager.h"
#define DEFAULT_DATABASE_TYPE "mysql"
int db_LTX_prelude_plugin_version(void);
int db_LTX_manager_plugin_init(prelude_plugin_entry_t *pe, void *rootopt);
#define param_value(param) (param ? param : "")
typedef struct {
char *type;
char *log;
char *host;
char *file;
char *port;
char *name;
char *user;
char *pass;
preludedb_t *db;
} db_plugin_t;
PRELUDE_PLUGIN_OPTION_DECLARE_STRING_CB(db, db_plugin_t, type)
PRELUDE_PLUGIN_OPTION_DECLARE_STRING_CB(db, db_plugin_t, log)
PRELUDE_PLUGIN_OPTION_DECLARE_STRING_CB(db, db_plugin_t, host)
PRELUDE_PLUGIN_OPTION_DECLARE_STRING_CB(db, db_plugin_t, port)
PRELUDE_PLUGIN_OPTION_DECLARE_STRING_CB(db, db_plugin_t, name)
PRELUDE_PLUGIN_OPTION_DECLARE_STRING_CB(db, db_plugin_t, user)
PRELUDE_PLUGIN_OPTION_DECLARE_STRING_CB(db, db_plugin_t, pass)
PRELUDE_PLUGIN_OPTION_DECLARE_STRING_CB(db, db_plugin_t, file)
static int db_run(prelude_plugin_instance_t *pi, idmef_message_t *message)
{
int ret;
db_plugin_t *plugin = prelude_plugin_instance_get_plugin_data(pi);
ret = preludedb_insert_message(plugin->db, message);
if ( ret < 0 ) {
prelude_log(PRELUDE_LOG_WARN, "could not insert message into database: %s.\n", preludedb_strerror(ret));
if ( prelude_error_get_code(ret) == PRELUDEDB_ERROR_CONNECTION )
ret = MANAGER_REPORT_PLUGIN_FAILURE_GLOBAL;
else
ret = MANAGER_REPORT_PLUGIN_FAILURE_SINGLE;
}
return ret;
}
static void db_destroy(prelude_plugin_instance_t *pi, prelude_string_t *out)
{
db_plugin_t *plugin = prelude_plugin_instance_get_plugin_data(pi);
if ( plugin->type )
free(plugin->type);
if ( plugin->host )
free(plugin->host);
if ( plugin->file )
free(plugin->file);
if ( plugin->name )
free(plugin->name);
if ( plugin->user )
free(plugin->user);
if ( plugin->pass )
free(plugin->pass);
if ( plugin->port )
free(plugin->port);
if ( plugin->log )
free(plugin->log);
if ( plugin->db )
preludedb_destroy(plugin->db);
free(plugin);
preludedb_deinit();
}
static int db_init(prelude_plugin_instance_t *pi, prelude_string_t *out)
{
int ret;
preludedb_t *db;
preludedb_sql_t *sql;
preludedb_sql_settings_t *settings;
db_plugin_t *plugin = prelude_plugin_instance_get_plugin_data(pi);
ret = preludedb_sql_settings_new(&settings);
if ( ret < 0 )
return ret;
if ( plugin->host )
preludedb_sql_settings_set_host(settings, plugin->host);
if ( plugin->file )
preludedb_sql_settings_set_file(settings, plugin->file);
if ( plugin->port )
preludedb_sql_settings_set_port(settings, plugin->port);
if ( plugin->user )
preludedb_sql_settings_set_user(settings, plugin->user);
if ( plugin->pass )
preludedb_sql_settings_set_pass(settings, plugin->pass);
if ( plugin->name )
preludedb_sql_settings_set_name(settings, plugin->name);
ret = preludedb_sql_new(&sql, plugin->type, settings);
if ( ret < 0 ) {
prelude_string_sprintf(out, "error initializing libpreludedb SQL interface: %s", preludedb_strerror(ret));
preludedb_sql_settings_destroy(settings);
return ret;
}
if ( ! plugin->log )
preludedb_sql_disable_query_logging(sql);
else {
ret = preludedb_sql_enable_query_logging(sql, (strcmp(plugin->log, "-") == 0) ? NULL : plugin->log);
if ( ret < 0 ) {
preludedb_sql_destroy(sql);
prelude_string_sprintf(out, "could not enable queries logging with log file '%s': %s",
plugin->log, preludedb_strerror(ret));
return ret;
}
}
ret = preludedb_new(&db, sql, NULL, NULL, 0);
if ( ret < 0 ) {
preludedb_sql_destroy(sql);
prelude_string_sprintf(out, "could not initialize libpreludedb: %s", preludedb_strerror(ret));
return ret;
}
if ( plugin->db )
preludedb_destroy(plugin->db);
plugin->db = db;
return 0;
}
static int db_activate(prelude_option_t *opt, const char *optarg, prelude_string_t *err, void *context)
{
int ret;
db_plugin_t *new;
ret = preludedb_init();
if ( ret < 0 ) {
prelude_log(PRELUDE_LOG_ERR, "error initializing libpreludedb: %s", preludedb_strerror(ret));
return ret;
}
new = calloc(1, sizeof(*new));
if ( ! new )
return prelude_error_from_errno(errno);
new->type = strdup(DEFAULT_DATABASE_TYPE);
if ( ! new->type ) {
free(new);
return prelude_error_from_errno(errno);
}
prelude_plugin_instance_set_plugin_data(context, new);
return 0;
}
int db_LTX_manager_plugin_init(prelude_plugin_entry_t *pe, void *rootopt)
{
int ret;
prelude_option_t *opt;
static manager_report_plugin_t db_plugin;
int hook = PRELUDE_OPTION_TYPE_CLI|PRELUDE_OPTION_TYPE_CFG|PRELUDE_OPTION_TYPE_WIDE;
ret = prelude_option_add(rootopt, &opt, hook, 0, "db", "Options for the libpreludedb plugin",
PRELUDE_OPTION_ARGUMENT_OPTIONAL, db_activate, NULL);
if ( ret < 0 )
return ret;
prelude_plugin_set_activation_option(pe, opt, db_init);
ret = prelude_option_add(opt, NULL, hook, 't', "type", "Type of database (mysql/pgsql)",
PRELUDE_OPTION_ARGUMENT_REQUIRED, db_set_type, db_get_type);
if ( ret < 0 )
return ret;
ret = prelude_option_add(opt, NULL, hook, 'l', "log",
"Log all queries in a file, should be only used for debugging purpose",
PRELUDE_OPTION_ARGUMENT_OPTIONAL, db_set_log, db_get_log);
if ( ret < 0 )
return ret;
ret = prelude_option_add(opt, NULL, hook, 'h', PRELUDEDB_SQL_SETTING_HOST,
"The host where the database server is running (in case of client/server database)",
PRELUDE_OPTION_ARGUMENT_REQUIRED, db_set_host, db_get_host);
if ( ret < 0 )
return ret;
ret = prelude_option_add(opt, NULL, hook, 'f', PRELUDEDB_SQL_SETTING_FILE,
"The file where the database is stored (in case of file based database)",
PRELUDE_OPTION_ARGUMENT_REQUIRED, db_set_file, db_get_file);
if ( ret < 0 )
return ret;
ret = prelude_option_add(opt, NULL, hook, 'p', PRELUDEDB_SQL_SETTING_PORT,
"The port where the database server is listening (in case of client/server database)",
PRELUDE_OPTION_ARGUMENT_REQUIRED, db_set_port, db_get_port);
if ( ret < 0 )
return ret;
ret = prelude_option_add(opt, NULL, hook, 'd', PRELUDEDB_SQL_SETTING_NAME,
"The name of the database where the alerts will be stored",
PRELUDE_OPTION_ARGUMENT_REQUIRED, db_set_name, db_get_name);
if ( ret < 0 )
return ret;
ret = prelude_option_add(opt, NULL, hook, 'u', PRELUDEDB_SQL_SETTING_USER,
"User of the database (in case of client/server database)",
PRELUDE_OPTION_ARGUMENT_REQUIRED, db_set_user, db_get_user);
if ( ret < 0 )
return ret;
ret = prelude_option_add(opt, NULL, hook, 'P', PRELUDEDB_SQL_SETTING_PASS,
"Password for the user (in case of client/server database)",
PRELUDE_OPTION_ARGUMENT_REQUIRED, db_set_pass, db_get_pass);
if ( ret < 0 )
return ret;
prelude_plugin_set_name(&db_plugin, "db");
prelude_plugin_set_destroy_func(&db_plugin, db_destroy);
manager_report_plugin_set_running_func(&db_plugin, db_run);
prelude_plugin_entry_set_plugin(pe, (void *) &db_plugin);
return 0;
}
int db_LTX_prelude_plugin_version(void)
{
return PRELUDE_PLUGIN_API_VERSION;
}
|