summaryrefslogtreecommitdiff
path: root/src/login/ipban_sql.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/login/ipban_sql.c')
-rw-r--r--src/login/ipban_sql.c281
1 files changed, 155 insertions, 126 deletions
diff --git a/src/login/ipban_sql.c b/src/login/ipban_sql.c
index bec0217f4..d74e6c4fa 100644
--- a/src/login/ipban_sql.c
+++ b/src/login/ipban_sql.c
@@ -2,7 +2,7 @@
* This file is part of Hercules.
* http://herc.ws - http://github.com/HerculesWS/Hercules
*
- * Copyright (C) 2012-2015 Hercules Dev Team
+ * Copyright (C) 2012-2016 Hercules Dev Team
* Copyright (C) Athena Dev Teams
*
* Hercules is free software: you can redistribute it and/or modify
@@ -25,26 +25,21 @@
#include "login/login.h"
#include "login/loginlog.h"
#include "common/cbasetypes.h"
+#include "common/conf.h"
#include "common/nullpo.h"
+#include "common/showmsg.h"
#include "common/sql.h"
#include "common/strlib.h"
#include "common/timer.h"
#include <stdlib.h>
-// global sql settings
-static char global_db_hostname[32] = "127.0.0.1";
-static uint16 global_db_port = 3306;
-static char global_db_username[32] = "ragnarok";
-static char global_db_password[100] = "ragnarok";
-static char global_db_database[32] = "ragnarok";
-static char global_codepage[32] = "";
-// local sql settings
-static char ipban_db_hostname[32] = "";
-static uint16 ipban_db_port = 0;
-static char ipban_db_username[32] = "";
-static char ipban_db_password[100] = "";
-static char ipban_db_database[32] = "";
+// Sql settings
+static char ipban_db_hostname[32] = "127.0.0.1";
+static uint16 ipban_db_port = 3306;
+static char ipban_db_username[32] = "ragnarok";
+static char ipban_db_password[100] = "ragnarok";
+static char ipban_db_database[32] = "ragnarok";
static char ipban_codepage[32] = "";
static char ipban_table[32] = "ipbanlist";
@@ -59,54 +54,30 @@ int ipban_cleanup(int tid, int64 tick, int id, intptr_t data);
// initialize
void ipban_init(void)
{
- const char* username;
- const char* password;
- const char* hostname;
- uint16 port;
- const char* database;
- const char* codepage;
-
ipban_inited = true;
if (!login->config->ipban)
return;// ipban disabled
- if( ipban_db_hostname[0] != '\0' )
- {// local settings
- username = ipban_db_username;
- password = ipban_db_password;
- hostname = ipban_db_hostname;
- port = ipban_db_port;
- database = ipban_db_database;
- codepage = ipban_codepage;
- }
- else
- {// global settings
- username = global_db_username;
- password = global_db_password;
- hostname = global_db_hostname;
- port = global_db_port;
- database = global_db_database;
- codepage = global_codepage;
- }
-
// establish connections
sql_handle = SQL->Malloc();
- if( SQL_ERROR == SQL->Connect(sql_handle, username, password, hostname, port, database) )
- {
+ if (SQL_ERROR == SQL->Connect(sql_handle, ipban_db_username, ipban_db_password,
+ ipban_db_hostname, ipban_db_port, ipban_db_database)) {
Sql_ShowDebug(sql_handle);
SQL->Free(sql_handle);
exit(EXIT_FAILURE);
}
- if( codepage[0] != '\0' && SQL_ERROR == SQL->SetEncoding(sql_handle, codepage) )
+ if (ipban_codepage[0] != '\0' && SQL_ERROR == SQL->SetEncoding(sql_handle, ipban_codepage))
Sql_ShowDebug(sql_handle);
- if (login->config->ipban_cleanup_interval > 0)
- { // set up periodic cleanup of connection history and active bans
+ if (login->config->ipban_cleanup_interval > 0) {
+ // set up periodic cleanup of connection history and active bans
timer->add_func_list(ipban_cleanup, "ipban_cleanup");
cleanup_timer_id = timer->add_interval(timer->gettick()+10, ipban_cleanup, 0, 0, login->config->ipban_cleanup_interval*1000);
- } else // make sure it gets cleaned up on login-server start regardless of interval-based cleanups
+ } else {
+ // make sure it gets cleaned up on login-server start regardless of interval-based cleanups
ipban_cleanup(0,0,0,0);
+ }
}
// finalize
@@ -126,95 +97,153 @@ void ipban_final(void)
sql_handle = NULL;
}
-// load configuration options
-bool ipban_config_read(const char* key, const char* value)
+/**
+ * Reads 'inter_configuration' and initializes required variables/Sets global
+ * configuration.
+ *
+ * @param filename Path to configuration file (used in error and warning messages).
+ * @param imported Whether the current config is imported from another file.
+ *
+ * @retval false in case of error.
+
+ */
+bool ipban_config_read_inter(const char *filename, bool imported)
{
- const char* signature;
+ struct config_t config;
+ struct config_setting_t *setting = NULL;
+ const char *import = NULL;
+ bool retval = true;
- nullpo_ret(key);
- nullpo_ret(value);
- if( ipban_inited )
- return false;// settings can only be changed before init
+ nullpo_retr(false, filename);
- signature = "sql.";
- if( strncmpi(key, signature, strlen(signature)) == 0 )
- {
- key += strlen(signature);
- if( strcmpi(key, "db_hostname") == 0 )
- safestrncpy(global_db_hostname, value, sizeof(global_db_hostname));
- else
- if( strcmpi(key, "db_port") == 0 )
- global_db_port = (uint16)strtoul(value, NULL, 10);
- else
- if( strcmpi(key, "db_username") == 0 )
- safestrncpy(global_db_username, value, sizeof(global_db_username));
- else
- if( strcmpi(key, "db_password") == 0 )
- safestrncpy(global_db_password, value, sizeof(global_db_password));
- else
- if( strcmpi(key, "db_database") == 0 )
- safestrncpy(global_db_database, value, sizeof(global_db_database));
- else
- if( strcmpi(key, "codepage") == 0 )
- safestrncpy(global_codepage, value, sizeof(global_codepage));
- else
- return false;// not found
- return true;
+ if (!libconfig->load_file(&config, filename))
+ return false; // Error message is already shown by libconfig->read_file
+
+ if ((setting = libconfig->lookup(&config, "inter_configuration/database_names")) == NULL) {
+ libconfig->destroy(&config);
+ if (imported)
+ return true;
+ ShowError("ipban_config_read: inter_configuration/database_names was not found!\n");
+ return false;
+ }
+ libconfig->setting_lookup_mutable_string(setting, "ipban_table", ipban_table, sizeof(ipban_table));
+
+ // import should overwrite any previous configuration, so it should be called last
+ if (libconfig->lookup_string(&config, "import", &import) == CONFIG_TRUE) {
+ if (strcmp(import, filename) == 0 || strcmp(import, "conf/common/inter-server.conf") == 0) {
+ ShowWarning("ipban_config_read_inter: Loop detected! Skipping 'import'...\n");
+ } else {
+ if (!ipban_config_read_inter(import, true))
+ retval = false;
+ }
}
- signature = "ipban.sql.";
- if( strncmpi(key, signature, strlen(signature)) == 0 )
- {
- key += strlen(signature);
- if( strcmpi(key, "db_hostname") == 0 )
- safestrncpy(ipban_db_hostname, value, sizeof(ipban_db_hostname));
- else
- if( strcmpi(key, "db_port") == 0 )
- ipban_db_port = (uint16)strtoul(value, NULL, 10);
- else
- if( strcmpi(key, "db_username") == 0 )
- safestrncpy(ipban_db_username, value, sizeof(ipban_db_username));
- else
- if( strcmpi(key, "db_password") == 0 )
- safestrncpy(ipban_db_password, value, sizeof(ipban_db_password));
- else
- if( strcmpi(key, "db_database") == 0 )
- safestrncpy(ipban_db_database, value, sizeof(ipban_db_database));
- else
- if( strcmpi(key, "codepage") == 0 )
- safestrncpy(ipban_codepage, value, sizeof(ipban_codepage));
- else
- if( strcmpi(key, "ipban_table") == 0 )
- safestrncpy(ipban_table, value, sizeof(ipban_table));
- else
- return false;// not found
- return true;
+ libconfig->destroy(&config);
+ return retval;
+}
+
+/**
+ * Reads login_configuration/account/ipban/sql_connection and loads configuration options.
+ *
+ * @param filename Path to configuration file (used in error and warning messages).
+ * @param config The current config being parsed.
+ * @param imported Whether the current config is imported from another file.
+ *
+ * @retval false in case of error.
+ */
+bool ipban_config_read_connection(const char *filename, struct config_t *config, bool imported)
+{
+ struct config_setting_t *setting = NULL;
+
+ nullpo_retr(false, filename);
+ nullpo_retr(false, config);
+
+ if ((setting = libconfig->lookup(config, "login_configuration/account/ipban/sql_connection")) == NULL) {
+ if (imported)
+ return true;
+ ShowError("account_db_sql_set_property: login_configuration/account/ipban/sql_connection was not found in %s!\n", filename);
+ return false;
}
- signature = "ipban.";
- if( strncmpi(key, signature, strlen(signature)) == 0 )
- {
- key += strlen(signature);
- if( strcmpi(key, "enable") == 0 )
- login->config->ipban = (bool)config_switch(value);
- else
- if( strcmpi(key, "dynamic_pass_failure_ban") == 0 )
- login->config->dynamic_pass_failure_ban = (bool)config_switch(value);
- else
- if( strcmpi(key, "dynamic_pass_failure_ban_interval") == 0 )
- login->config->dynamic_pass_failure_ban_interval = atoi(value);
- else
- if( strcmpi(key, "dynamic_pass_failure_ban_limit") == 0 )
- login->config->dynamic_pass_failure_ban_limit = atoi(value);
- else
- if( strcmpi(key, "dynamic_pass_failure_ban_duration") == 0 )
- login->config->dynamic_pass_failure_ban_duration = atoi(value);
- else
- return false;// not found
- return true;
+ libconfig->setting_lookup_mutable_string(setting, "db_hostname", ipban_db_hostname, sizeof(ipban_db_hostname));
+ libconfig->setting_lookup_mutable_string(setting, "db_database", ipban_db_database, sizeof(ipban_db_database));
+
+ libconfig->setting_lookup_mutable_string(setting, "db_username", ipban_db_username, sizeof(ipban_db_username));
+ libconfig->setting_lookup_mutable_string(setting, "db_password", ipban_db_password, sizeof(ipban_db_password));
+ libconfig->setting_lookup_mutable_string(setting, "codepage", ipban_codepage, sizeof(ipban_codepage));
+ libconfig->setting_lookup_uint16(setting, "db_port", &ipban_db_port);
+
+ return true;
+}
+
+/**
+ * Reads login_configuration/account/ipban/dynamic_pass_failure and loads configuration options.
+ *
+ * @param filename Path to configuration file (used in error and warning messages).
+ * @param config The current config being parsed.
+ * @param imported Whether the current config is imported from another file.
+ *
+ * @retval false in case of error.
+ */
+bool ipban_config_read_dynamic(const char *filename, struct config_t *config, bool imported)
+{
+ struct config_setting_t *setting = NULL;
+
+ nullpo_retr(false, filename);
+ nullpo_retr(false, config);
+
+ if ((setting = libconfig->lookup(config, "login_configuration/account/ipban/dynamic_pass_failure")) == NULL) {
+ if (imported)
+ return true;
+ ShowError("account_db_sql_set_property: login_configuration/account/ipban/dynamic_pass_failure was not found in %s!\n", filename);
+ return false;
+ }
+
+ libconfig->setting_lookup_bool_real(setting, "enabled", &login->config->dynamic_pass_failure_ban);
+ libconfig->setting_lookup_uint32(setting, "ban_interval", &login->config->dynamic_pass_failure_ban_interval);
+ libconfig->setting_lookup_uint32(setting, "ban_limit", &login->config->dynamic_pass_failure_ban_limit);
+ libconfig->setting_lookup_uint32(setting, "ban_duration", &login->config->dynamic_pass_failure_ban_duration);
+
+ return true;
+}
+
+/**
+ * Reads login_configuration.account.ipban and loads configuration options.
+ *
+ * @param filename Path to configuration file (used in error and warning messages).
+ * @param config The current config being parsed.
+ * @param imported Whether the current config is imported from another file.
+ *
+ * @retval false in case of error.
+ */
+bool ipban_config_read(const char *filename, struct config_t *config, bool imported)
+{
+ struct config_setting_t *setting = NULL;
+ bool retval = true;
+
+ nullpo_retr(false, filename);
+ nullpo_retr(false, config);
+
+ if (ipban_inited)
+ return false; // settings can only be changed before init
+
+ if ((setting = libconfig->lookup(config, "login_configuration/account/ipban")) == NULL) {
+ if (!imported)
+ ShowError("login_config_read: login_configuration/log was not found in %s!\n", filename);
+ return false;
}
- return false;// not found
+ libconfig->setting_lookup_bool_real(setting, "enabled", &login->config->ipban);
+ libconfig->setting_lookup_uint32(setting, "cleanup_interval", &login->config->ipban_cleanup_interval);
+
+ if (!ipban_config_read_inter("conf/common/inter-server.conf", imported))
+ retval = false;
+ if (!ipban_config_read_connection(filename, config, imported))
+ retval = false;
+ if (!ipban_config_read_dynamic(filename, config, imported))
+ retval = false;
+
+ return retval;
}
// check ip against active bans list