diff options
author | Andrei Karas <akaras@inbox.ru> | 2018-01-24 02:55:56 +0300 |
---|---|---|
committer | Andrei Karas <akaras@inbox.ru> | 2018-02-09 18:34:44 +0300 |
commit | a58b96c5dab055e5563de3ff92482c92ce0bc50c (patch) | |
tree | eabadce3905644f00b029e71ae6dc145fd4f3be5 /src/login | |
parent | a4cbec10932f12f849a942dff29d282975aabd45 (diff) | |
download | hercules-a58b96c5dab055e5563de3ff92482c92ce0bc50c.tar.gz hercules-a58b96c5dab055e5563de3ff92482c92ce0bc50c.tar.bz2 hercules-a58b96c5dab055e5563de3ff92482c92ce0bc50c.tar.xz hercules-a58b96c5dab055e5563de3ff92482c92ce0bc50c.zip |
Add interface into ipban.c.
Diffstat (limited to 'src/login')
-rw-r--r-- | src/login/HPMlogin.c | 1 | ||||
-rw-r--r-- | src/login/ipban.c | 118 | ||||
-rw-r--r-- | src/login/ipban.h | 46 | ||||
-rw-r--r-- | src/login/lclif.c | 2 | ||||
-rw-r--r-- | src/login/login.c | 8 |
5 files changed, 105 insertions, 70 deletions
diff --git a/src/login/HPMlogin.c b/src/login/HPMlogin.c index 4bb375edc..65bdb1a71 100644 --- a/src/login/HPMlogin.c +++ b/src/login/HPMlogin.c @@ -25,6 +25,7 @@ #include "common/cbasetypes.h" #include "login/account.h" +#include "login/ipban.h" #include "login/lclif.h" #include "login/lclif.p.h" #include "login/login.h" diff --git a/src/login/ipban.c b/src/login/ipban.c index d74e6c4fa..46798ea45 100644 --- a/src/login/ipban.c +++ b/src/login/ipban.c @@ -34,49 +34,36 @@ #include <stdlib.h> -// 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"; - -// globals -static struct Sql *sql_handle = NULL; -static int cleanup_timer_id = INVALID_TIMER; -static bool ipban_inited = false; - -int ipban_cleanup(int tid, int64 tick, int id, intptr_t data); - +struct ipban_interface ipban_s; +struct ipban_interface *ipban; +struct s_ipban_dbs ipbandbs; // initialize void ipban_init(void) { - ipban_inited = true; + ipban->inited = true; if (!login->config->ipban) return;// ipban disabled // establish connections - sql_handle = SQL->Malloc(); - 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); + ipban->sql_handle = SQL->Malloc(); + if (SQL_ERROR == SQL->Connect(ipban->sql_handle, ipban->dbs->db_username, ipban->dbs->db_password, + ipban->dbs->db_hostname, ipban->dbs->db_port, ipban->dbs->db_database)) { + Sql_ShowDebug(ipban->sql_handle); + SQL->Free(ipban->sql_handle); exit(EXIT_FAILURE); } - if (ipban_codepage[0] != '\0' && SQL_ERROR == SQL->SetEncoding(sql_handle, ipban_codepage)) - Sql_ShowDebug(sql_handle); + if (ipban->dbs->codepage[0] != '\0' && SQL_ERROR == SQL->SetEncoding(ipban->sql_handle, ipban->dbs->codepage)) + Sql_ShowDebug(ipban->sql_handle); 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); + timer->add_func_list(ipban->cleanup, "ipban_cleanup"); + ipban->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 - ipban_cleanup(0,0,0,0); + ipban->cleanup(0,0,0,0); } } @@ -88,13 +75,13 @@ void ipban_final(void) if (login->config->ipban_cleanup_interval > 0) // release data - timer->delete(cleanup_timer_id, ipban_cleanup); + timer->delete(ipban->cleanup_timer_id, ipban->cleanup); - ipban_cleanup(0,0,0,0); // always clean up on login-server stop + ipban->cleanup(0,0,0,0); // always clean up on login-server stop // close connections - SQL->Free(sql_handle); - sql_handle = NULL; + SQL->Free(ipban->sql_handle); + ipban->sql_handle = NULL; } /** @@ -126,14 +113,14 @@ bool ipban_config_read_inter(const char *filename, bool imported) 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)); + libconfig->setting_lookup_mutable_string(setting, "ipban_table", ipban->dbs->table, sizeof(ipban->dbs->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)) + if (!ipban->config_read_inter(import, true)) retval = false; } } @@ -165,13 +152,13 @@ bool ipban_config_read_connection(const char *filename, struct config_t *config, return false; } - 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_hostname", ipban->dbs->db_hostname, sizeof(ipban->dbs->db_hostname)); + libconfig->setting_lookup_mutable_string(setting, "db_database", ipban->dbs->db_database, sizeof(ipban->dbs->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); + libconfig->setting_lookup_mutable_string(setting, "db_username", ipban->dbs->db_username, sizeof(ipban->dbs->db_username)); + libconfig->setting_lookup_mutable_string(setting, "db_password", ipban->dbs->db_password, sizeof(ipban->dbs->db_password)); + libconfig->setting_lookup_mutable_string(setting, "codepage", ipban->dbs->codepage, sizeof(ipban->dbs->codepage)); + libconfig->setting_lookup_uint16(setting, "db_port", &ipban->dbs->db_port); return true; } @@ -224,7 +211,7 @@ bool ipban_config_read(const char *filename, struct config_t *config, bool impor nullpo_retr(false, filename); nullpo_retr(false, config); - if (ipban_inited) + if (ipban->inited) return false; // settings can only be changed before init if ((setting = libconfig->lookup(config, "login_configuration/account/ipban")) == NULL) { @@ -256,20 +243,20 @@ bool ipban_check(uint32 ip) if (!login->config->ipban) return false;// ipban disabled - if( SQL_ERROR == SQL->Query(sql_handle, "SELECT count(*) FROM `%s` WHERE `rtime` > NOW() AND (`list` = '%u.*.*.*' OR `list` = '%u.%u.*.*' OR `list` = '%u.%u.%u.*' OR `list` = '%u.%u.%u.%u')", - ipban_table, p[3], p[3], p[2], p[3], p[2], p[1], p[3], p[2], p[1], p[0]) ) + if( SQL_ERROR == SQL->Query(ipban->sql_handle, "SELECT count(*) FROM `%s` WHERE `rtime` > NOW() AND (`list` = '%u.*.*.*' OR `list` = '%u.%u.*.*' OR `list` = '%u.%u.%u.*' OR `list` = '%u.%u.%u.%u')", + ipban->dbs->table, p[3], p[3], p[2], p[3], p[2], p[1], p[3], p[2], p[1], p[0]) ) { - Sql_ShowDebug(sql_handle); + Sql_ShowDebug(ipban->sql_handle); // close connection because we can't verify their connectivity. return true; } - if( SQL_SUCCESS != SQL->NextRow(sql_handle) ) + if( SQL_SUCCESS != SQL->NextRow(ipban->sql_handle) ) return false; - SQL->GetData(sql_handle, 0, &data, NULL); + SQL->GetData(ipban->sql_handle, 0, &data, NULL); matches = atoi(data); - SQL->FreeResult(sql_handle); + SQL->FreeResult(ipban->sql_handle); return( matches > 0 ); } @@ -288,10 +275,10 @@ void ipban_log(uint32 ip) if (failures >= login->config->dynamic_pass_failure_ban_limit) { uint8* p = (uint8*)&ip; - if (SQL_ERROR == SQL->Query(sql_handle, "INSERT INTO `%s`(`list`,`btime`,`rtime`,`reason`) VALUES ('%u.%u.%u.*', NOW() , NOW() + INTERVAL %u MINUTE ,'Password error ban')", - ipban_table, p[3], p[2], p[1], login->config->dynamic_pass_failure_ban_duration)) + if (SQL_ERROR == SQL->Query(ipban->sql_handle, "INSERT INTO `%s`(`list`,`btime`,`rtime`,`reason`) VALUES ('%u.%u.%u.*', NOW() , NOW() + INTERVAL %u MINUTE ,'Password error ban')", + ipban->dbs->table, p[3], p[2], p[1], login->config->dynamic_pass_failure_ban_duration)) { - Sql_ShowDebug(sql_handle); + Sql_ShowDebug(ipban->sql_handle); } } } @@ -301,8 +288,37 @@ int ipban_cleanup(int tid, int64 tick, int id, intptr_t data) { if (!login->config->ipban) return 0;// ipban disabled - if( SQL_ERROR == SQL->Query(sql_handle, "DELETE FROM `%s` WHERE `rtime` <= NOW()", ipban_table) ) - Sql_ShowDebug(sql_handle); + if( SQL_ERROR == SQL->Query(ipban->sql_handle, "DELETE FROM `%s` WHERE `rtime` <= NOW()", ipban->dbs->table) ) + Sql_ShowDebug(ipban->sql_handle); return 0; } + +void ipban_defaults(void) { + ipban = &ipban_s; + + ipban->dbs = &ipbandbs; + + ipban->sql_handle = NULL; + ipban->cleanup_timer_id = INVALID_TIMER; + ipban->inited = false; + + // Sql settings + strcpy(ipban->dbs->db_hostname, "127.0.0.1"); + ipban->dbs->db_port = 3306; + strcpy(ipban->dbs->db_username, "ragnarok"); + strcpy(ipban->dbs->db_password, "ragnarok"); + strcpy(ipban->dbs->db_database, "ragnarok"); + *ipban->dbs->codepage = 0; + strcpy(ipban->dbs->table, "ipbanlist"); + + ipban->init = ipban_init; + ipban->final = ipban_final; + ipban->cleanup = ipban_cleanup; + ipban->config_read_inter = ipban_config_read_inter; + ipban->config_read_connection = ipban_config_read_connection; + ipban->config_read_dynamic = ipban_config_read_dynamic; + ipban->config_read = ipban_config_read; + ipban->check = ipban_check; + ipban->log = ipban_log; +} diff --git a/src/login/ipban.h b/src/login/ipban.h index 104e3a8a3..29aafba9d 100644 --- a/src/login/ipban.h +++ b/src/login/ipban.h @@ -22,26 +22,44 @@ #define LOGIN_IPBAN_H #include "common/cbasetypes.h" +#include "common/hercules.h" /* Forward Declarations */ struct config_t; // common/conf.h -#ifdef HERCULES_CORE -// TODO: Interface -// initialize -void ipban_init(void); - -// finalize -void ipban_final(void); - -// check ip against ban list -bool ipban_check(uint32 ip); +struct s_ipban_dbs { + char db_hostname[32]; + uint16 db_port; + char db_username[32]; + char db_password[100]; + char db_database[32]; + char codepage[32]; + char table[32]; +}; -// increases failure count for the specified IP -void ipban_log(uint32 ip); +/** + * Ipban.c Interface + **/ +struct ipban_interface { + struct s_ipban_dbs *dbs; + struct Sql *sql_handle; + int cleanup_timer_id; + bool inited; + void (*init) (void); + void (*final) (void); + int (*cleanup) (int tid, int64 tick, int id, intptr_t data); + bool (*config_read_inter) (const char *filename, bool imported); + bool (*config_read_connection) (const char *filename, struct config_t *config, bool imported); + bool (*config_read_dynamic) (const char *filename, struct config_t *config, bool imported); + bool (*config_read) (const char *filename, struct config_t *config, bool imported); + bool (*check) (uint32 ip); + void (*log) (uint32 ip); +}; -// parses configuration options -bool ipban_config_read(const char *filename, struct config_t *config, bool imported); +#ifdef HERCULES_CORE +void ipban_defaults(void); #endif // HERCULES_CORE +HPShared struct ipban_interface *ipban; + #endif /* LOGIN_IPBAN_H */ diff --git a/src/login/lclif.c b/src/login/lclif.c index 4c0e63173..33445f92c 100644 --- a/src/login/lclif.c +++ b/src/login/lclif.c @@ -373,7 +373,7 @@ int lclif_parse(int fd) if ((sd = sockt->session[fd]->session_data) == NULL) { // Perform ip-ban check - if (login->config->ipban && !sockt->trusted_ip_check(ipl) && ipban_check(ipl)) { + if (login->config->ipban && !sockt->trusted_ip_check(ipl) && ipban->check(ipl)) { ShowStatus("Connection refused: IP isn't authorized (deny/allow, ip: %s).\n", ip); loginlog_log(ipl, "unknown", -3, "ip banned"); lclif->login_error(fd, 3); // 3 = Rejected from Server diff --git a/src/login/login.c b/src/login/login.c index 86cc93240..44fee5ccd 100644 --- a/src/login/login.c +++ b/src/login/login.c @@ -1326,7 +1326,7 @@ void login_auth_failed(struct login_session_data *sd, int result) } if (result == 1 && login->config->dynamic_pass_failure_ban && !sockt->trusted_ip_check(ip)) - ipban_log(ip); // log failed password attempt + ipban->log(ip); // log failed password attempt if (result == 6) { struct mmo_account acc = { 0 }; @@ -1630,7 +1630,7 @@ bool login_config_read_account(const char *filename, struct config_t *config, bo if (!db->set_property(db, config, imported)) retval = false; - if (!ipban_config_read(filename, config, imported)) + if (!ipban->config_read(filename, config, imported)) retval = false; return retval; @@ -1978,7 +1978,7 @@ int do_final(void) if (login->config->log_login) loginlog_final(); - ipban_final(); + ipban->final(); if( account_engine[0].db ) {// destroy account engine @@ -2154,7 +2154,7 @@ int do_init(int argc, char** argv) loginlog_init(); // initialize static and dynamic ipban system - ipban_init(); + ipban->init(); // Online user database init login->online_db = idb_alloc(DB_OPT_RELEASE_DATA); |