From 6813c20bb80ccbb390b320539b2d186aeb989f33 Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Tue, 16 Jan 2018 02:34:27 +0300 Subject: Remove suffix "_sql" from files in login directory. --- src/login/loginlog.c | 225 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 225 insertions(+) create mode 100644 src/login/loginlog.c (limited to 'src/login/loginlog.c') diff --git a/src/login/loginlog.c b/src/login/loginlog.c new file mode 100644 index 000000000..7dff14990 --- /dev/null +++ b/src/login/loginlog.c @@ -0,0 +1,225 @@ +/** + * This file is part of Hercules. + * http://herc.ws - http://github.com/HerculesWS/Hercules + * + * Copyright (C) 2012-2016 Hercules Dev Team + * Copyright (C) Athena Dev Teams + * + * Hercules 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 3 of the License, 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, see . + */ +#define HERCULES_CORE + +#include "loginlog.h" + +#include "common/cbasetypes.h" +#include "common/conf.h" +#include "common/mmo.h" +#include "common/nullpo.h" +#include "common/showmsg.h" +#include "common/socket.h" +#include "common/sql.h" +#include "common/strlib.h" + +#include // exit + +// Sql settings +static char log_db_hostname[32] = "127.0.0.1"; +static uint16 log_db_port = 3306; +static char log_db_username[32] = "ragnarok"; +static char log_db_password[100] = "ragnarok"; +static char log_db_database[32] = "ragnarok"; +static char log_codepage[32] = ""; +static char log_login_db[256] = "loginlog"; + +static struct Sql *sql_handle = NULL; +static bool enabled = false; + + +// Returns the number of failed login attempts by the ip in the last minutes. +unsigned long loginlog_failedattempts(uint32 ip, unsigned int minutes) +{ + unsigned long failures = 0; + + if( !enabled ) + return 0; + + if( SQL_ERROR == SQL->Query(sql_handle, "SELECT count(*) FROM `%s` WHERE `ip` = '%s' AND `rcode` = '1' AND `time` > NOW() - INTERVAL %u MINUTE", + log_login_db, sockt->ip2str(ip,NULL), minutes) )// how many times failed account? in one ip. + Sql_ShowDebug(sql_handle); + + if( SQL_SUCCESS == SQL->NextRow(sql_handle) ) + { + char* data; + SQL->GetData(sql_handle, 0, &data, NULL); + failures = strtoul(data, NULL, 10); + SQL->FreeResult(sql_handle); + } + return failures; +} + + +/*============================================= + * Records an event in the login log + *---------------------------------------------*/ +// TODO: add an enum of rcode values +void login_log(uint32 ip, const char* username, int rcode, const char* message) +{ + char esc_username[NAME_LENGTH*2+1]; + char esc_message[255*2+1]; + int retcode; + + nullpo_retv(username); + nullpo_retv(message); + if( !enabled ) + return; + + SQL->EscapeStringLen(sql_handle, esc_username, username, strnlen(username, NAME_LENGTH)); + SQL->EscapeStringLen(sql_handle, esc_message, message, strnlen(message, 255)); + + retcode = SQL->Query(sql_handle, + "INSERT INTO `%s`(`time`,`ip`,`user`,`rcode`,`log`) VALUES (NOW(), '%s', '%s', '%d', '%s')", + log_login_db, sockt->ip2str(ip,NULL), esc_username, rcode, esc_message); + + if( retcode != SQL_SUCCESS ) + Sql_ShowDebug(sql_handle); +} + +bool loginlog_init(void) +{ + sql_handle = SQL->Malloc(); + + if (SQL_ERROR == SQL->Connect(sql_handle, log_db_username, log_db_password, + log_db_hostname, log_db_port, log_db_database)) { + Sql_ShowDebug(sql_handle); + SQL->Free(sql_handle); + exit(EXIT_FAILURE); + } + + if (log_codepage[0] != '\0' && SQL_ERROR == SQL->SetEncoding(sql_handle, log_codepage)) + Sql_ShowDebug(sql_handle); + + enabled = true; + + return true; +} + +bool loginlog_final(void) +{ + SQL->Free(sql_handle); + sql_handle = NULL; + return true; +} + +/** + * Reads 'inter_configuration/database_names' and initializes required + * variables/Sets global configuration. + * + * @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 loginlog_config_read_names(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, "inter_configuration/database_names")) == NULL) { + if (imported) + return true; + ShowError("loginlog_config_read: inter_configuration/database_names was not found in %s!\n", filename); + return false; + } + + libconfig->setting_lookup_mutable_string(setting, "login_db", log_login_db, sizeof(log_login_db)); + + return true; +} + +/** + * Reads 'inter_configuration.log' and initializes required variables/Sets + * global configuration. + * + * @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 loginlog_config_read_log(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, "inter_configuration/log/sql_connection")) == NULL) { + if (imported) + return true; + ShowError("loginlog_config_read: inter_configuration/log/sql_connection was not found in %s!\n", filename); + return false; + } + + libconfig->setting_lookup_mutable_string(setting, "db_hostname", log_db_hostname, sizeof(log_db_hostname)); + libconfig->setting_lookup_mutable_string(setting, "db_database", log_db_database, sizeof(log_db_database)); + libconfig->setting_lookup_mutable_string(setting, "db_username", log_db_username, sizeof(log_db_username)); + libconfig->setting_lookup_mutable_string(setting, "db_password", log_db_password, sizeof(log_db_password)); + + libconfig->setting_lookup_uint16(setting, "db_port", &log_db_port); + libconfig->setting_lookup_mutable_string(setting, "codepage", log_codepage, sizeof(log_codepage)); + + return true; +} + +/** + * Reads 'inter_configuration' and initializes required variables/Sets global + * configuration. + * + * @param filename Path to configuration file. + * @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 loginlog_config_read(const char *filename, bool imported) +{ + struct config_t config; + const char *import = NULL; + bool retval = true; + + nullpo_retr(false, filename); + + if (!libconfig->load_file(&config, filename)) + return false; // Error message is already shown by libconfig->load_file + + if (!loginlog_config_read_names(filename, &config, imported)) + retval = false; + if (!loginlog_config_read_log(filename, &config, imported)) + retval = false; + + if (libconfig->lookup_string(&config, "import", &import) == CONFIG_TRUE) { + if (strcmp(import, filename) == 0 || strcmp(import, "conf/common/inter-server.conf") == 0) { + ShowWarning("inter_config_read: Loop detected! Skipping 'import'...\n"); + } else { + if (!loginlog_config_read(import, true)) + retval = false; + } + } + + libconfig->destroy(&config); + return retval; +} -- cgit v1.2.3-60-g2f50 From 55152d7e642eac14f8ca848a593a17bbf1b57334 Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Wed, 17 Jan 2018 01:39:37 +0300 Subject: Add loginlog_ prefix to all functions in loginlog.c --- src/login/lclif.c | 2 +- src/login/login.c | 12 ++++++------ src/login/loginlog.c | 2 +- src/login/loginlog.h | 2 +- 4 files changed, 9 insertions(+), 9 deletions(-) (limited to 'src/login/loginlog.c') diff --git a/src/login/lclif.c b/src/login/lclif.c index 1870f9bc9..d03f2fbe7 100644 --- a/src/login/lclif.c +++ b/src/login/lclif.c @@ -375,7 +375,7 @@ int lclif_parse(int fd) // Perform ip-ban check 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); - login_log(ipl, "unknown", -3, "ip banned"); + loginlog_log(ipl, "unknown", -3, "ip banned"); lclif->login_error(fd, 3); // 3 = Rejected from Server sockt->eof(fd); return 0; diff --git a/src/login/login.c b/src/login/login.c index 0b540d95a..7cbb14db2 100644 --- a/src/login/login.c +++ b/src/login/login.c @@ -725,7 +725,7 @@ bool login_fromchar_parse_wrong_pincode(int fd) return true; } - login_log(sockt->host2ip(acc.last_ip), acc.userid, 100, "PIN Code check failed"); // FIXME: Do we really want to log this with the same code as successful logins? + loginlog_log(sockt->host2ip(acc.last_ip), acc.userid, 100, "PIN Code check failed"); // FIXME: Do we really want to log this with the same code as successful logins? } login->remove_online_user(acc.account_id); @@ -1258,7 +1258,7 @@ void login_auth_ok(struct login_session_data* sd) return; } - login_log(ip, sd->userid, 100, "login ok"); + loginlog_log(ip, sd->userid, 100, "login ok"); ShowStatus("Connection of the account '%s' accepted.\n", sd->userid); // create temporary auth entry @@ -1322,7 +1322,7 @@ void login_auth_failed(struct login_session_data *sd, int result) default : error = "Unknown Error."; break; } - login_log(ip, sd->userid, result, error); // FIXME: result can be 100, conflicting with the value 100 we use for successful login... + loginlog_log(ip, sd->userid, result, error); // FIXME: result can be 100, conflicting with the value 100 we use for successful login... } if (result == 1 && login->config->dynamic_pass_failure_ban && !sockt->trusted_ip_check(ip)) @@ -1430,7 +1430,7 @@ void login_parse_request_connection(int fd, struct login_session_data* sd, const ShowInfo("Connection request of the char-server '%s' @ %u.%u.%u.%u:%u (account: '%s', pass: '%s', ip: '%s')\n", server_name, CONVIP(server_ip), server_port, sd->userid, sd->passwd, ip); sprintf(message, "charserver - %s@%u.%u.%u.%u:%u", server_name, CONVIP(server_ip), server_port); - login_log(sockt->session[fd]->client_addr, sd->userid, 100, message); + loginlog_log(sockt->session[fd]->client_addr, sd->userid, 100, message); result = login->mmo_auth(sd, true); if (core->runflag == LOGINSERVER_ST_RUNNING && @@ -1973,7 +1973,7 @@ int do_final(void) login->clear_client_hash_nodes(); login->clear_dnsbl_servers(); - login_log(0, "login server", 100, "login server shutdown"); + loginlog_log(0, "login server", 100, "login server shutdown"); if (login->config->log_login) loginlog_final(); @@ -2196,7 +2196,7 @@ int do_init(int argc, char** argv) #endif // CONSOLE_INPUT ShowStatus("The login-server is "CL_GREEN"ready"CL_RESET" (Server is listening on the port %u).\n\n", login->config->login_port); - login_log(0, "login server", 100, "login server started"); + loginlog_log(0, "login server", 100, "login server started"); HPM->event(HPET_READY); diff --git a/src/login/loginlog.c b/src/login/loginlog.c index 7dff14990..90dc3fde8 100644 --- a/src/login/loginlog.c +++ b/src/login/loginlog.c @@ -73,7 +73,7 @@ unsigned long loginlog_failedattempts(uint32 ip, unsigned int minutes) * Records an event in the login log *---------------------------------------------*/ // TODO: add an enum of rcode values -void login_log(uint32 ip, const char* username, int rcode, const char* message) +void loginlog_log(uint32 ip, const char* username, int rcode, const char* message) { char esc_username[NAME_LENGTH*2+1]; char esc_message[255*2+1]; diff --git a/src/login/loginlog.h b/src/login/loginlog.h index 589bc4fa1..8153dd769 100644 --- a/src/login/loginlog.h +++ b/src/login/loginlog.h @@ -26,7 +26,7 @@ #ifdef HERCULES_CORE // TODO: Interface unsigned long loginlog_failedattempts(uint32 ip, unsigned int minutes); -void login_log(uint32 ip, const char* username, int rcode, const char* message); +void loginlog_log(uint32 ip, const char* username, int rcode, const char* message); bool loginlog_init(void); bool loginlog_final(void); bool loginlog_config_read(const char *filename, bool imported); -- cgit v1.2.3-60-g2f50 From f9c920a728d1a8406fa4a15a395eee23b9bccc7a Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Tue, 6 Feb 2018 20:49:01 +0300 Subject: Add interface into loginlog.c. --- src/login/HPMlogin.c | 1 + src/login/ipban.c | 2 +- src/login/lclif.c | 2 +- src/login/login.c | 19 ++++++++++--------- src/login/loginlog.c | 21 ++++++++++++++++++--- src/login/loginlog.h | 25 +++++++++++++++++++------ src/plugins/HPMHooking.c | 1 + 7 files changed, 51 insertions(+), 20 deletions(-) (limited to 'src/login/loginlog.c') diff --git a/src/login/HPMlogin.c b/src/login/HPMlogin.c index 65bdb1a71..4f63da6ac 100644 --- a/src/login/HPMlogin.c +++ b/src/login/HPMlogin.c @@ -29,6 +29,7 @@ #include "login/lclif.h" #include "login/lclif.p.h" #include "login/login.h" +#include "login/loginlog.h" #include "common/HPMi.h" #include "common/conf.h" #include "common/console.h" diff --git a/src/login/ipban.c b/src/login/ipban.c index 46798ea45..60a90fec9 100644 --- a/src/login/ipban.c +++ b/src/login/ipban.c @@ -269,7 +269,7 @@ void ipban_log(uint32 ip) if (!login->config->ipban) return;// ipban disabled - failures = loginlog_failedattempts(ip, login->config->dynamic_pass_failure_ban_interval);// how many times failed account? in one ip. + failures = loginlog->failedattempts(ip, login->config->dynamic_pass_failure_ban_interval);// how many times failed account? in one ip. // if over the limit, add a temporary ban entry if (failures >= login->config->dynamic_pass_failure_ban_limit) diff --git a/src/login/lclif.c b/src/login/lclif.c index 33445f92c..ae9f035e3 100644 --- a/src/login/lclif.c +++ b/src/login/lclif.c @@ -375,7 +375,7 @@ int lclif_parse(int fd) // Perform ip-ban check 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"); + loginlog->log(ipl, "unknown", -3, "ip banned"); lclif->login_error(fd, 3); // 3 = Rejected from Server sockt->eof(fd); return 0; diff --git a/src/login/login.c b/src/login/login.c index 26f7d6da3..24d1799df 100644 --- a/src/login/login.c +++ b/src/login/login.c @@ -727,7 +727,7 @@ bool login_fromchar_parse_wrong_pincode(int fd) return true; } - loginlog_log(sockt->host2ip(acc.last_ip), acc.userid, 100, "PIN Code check failed"); // FIXME: Do we really want to log this with the same code as successful logins? + loginlog->log(sockt->host2ip(acc.last_ip), acc.userid, 100, "PIN Code check failed"); // FIXME: Do we really want to log this with the same code as successful logins? } login->remove_online_user(acc.account_id); @@ -1260,7 +1260,7 @@ void login_auth_ok(struct login_session_data* sd) return; } - loginlog_log(ip, sd->userid, 100, "login ok"); + loginlog->log(ip, sd->userid, 100, "login ok"); ShowStatus("Connection of the account '%s' accepted.\n", sd->userid); // create temporary auth entry @@ -1324,7 +1324,7 @@ void login_auth_failed(struct login_session_data *sd, int result) default : error = "Unknown Error."; break; } - loginlog_log(ip, sd->userid, result, error); // FIXME: result can be 100, conflicting with the value 100 we use for successful login... + loginlog->log(ip, sd->userid, result, error); // FIXME: result can be 100, conflicting with the value 100 we use for successful login... } if (result == 1 && login->config->dynamic_pass_failure_ban && !sockt->trusted_ip_check(ip)) @@ -1432,7 +1432,7 @@ void login_parse_request_connection(int fd, struct login_session_data* sd, const ShowInfo("Connection request of the char-server '%s' @ %u.%u.%u.%u:%u (account: '%s', pass: '%s', ip: '%s')\n", server_name, CONVIP(server_ip), server_port, sd->userid, sd->passwd, ip); sprintf(message, "charserver - %s@%u.%u.%u.%u:%u", server_name, CONVIP(server_ip), server_port); - loginlog_log(sockt->session[fd]->client_addr, sd->userid, 100, message); + loginlog->log(sockt->session[fd]->client_addr, sd->userid, 100, message); result = login->mmo_auth(sd, true); if (core->runflag == LOGINSERVER_ST_RUNNING && @@ -1915,7 +1915,7 @@ bool login_config_read(const char *filename, bool imported) if (!login->config_read_users(filename, &config, imported)) retval = false; - if (!loginlog_config_read("conf/common/inter-server.conf", imported)) // Only inter-server + if (!loginlog->config_read("conf/common/inter-server.conf", imported)) // Only inter-server retval = false; if (!HPM->parse_conf(&config, filename, HPCT_LOGIN, imported)) @@ -1975,10 +1975,10 @@ int do_final(void) login->clear_client_hash_nodes(); login->clear_dnsbl_servers(); - loginlog_log(0, "login server", 100, "login server shutdown"); + loginlog->log(0, "login server", 100, "login server shutdown"); if (login->config->log_login) - loginlog_final(); + loginlog->final(); ipban->final(); @@ -2111,6 +2111,7 @@ int do_init(int argc, char** argv) lchrif_defaults(); login_defaults(); lclif_defaults(); + loginlog_defaults(); // read login-server configuration login->config_set_defaults(); @@ -2154,7 +2155,7 @@ int do_init(int argc, char** argv) // initialize logging if (login->config->log_login) - loginlog_init(); + loginlog->init(); // initialize static and dynamic ipban system ipban->init(); @@ -2203,7 +2204,7 @@ int do_init(int argc, char** argv) #endif // CONSOLE_INPUT ShowStatus("The login-server is "CL_GREEN"ready"CL_RESET" (Server is listening on the port %u).\n\n", login->config->login_port); - loginlog_log(0, "login server", 100, "login server started"); + loginlog->log(0, "login server", 100, "login server started"); HPM->event(HPET_READY); diff --git a/src/login/loginlog.c b/src/login/loginlog.c index 90dc3fde8..38be53193 100644 --- a/src/login/loginlog.c +++ b/src/login/loginlog.c @@ -45,6 +45,9 @@ static char log_login_db[256] = "loginlog"; static struct Sql *sql_handle = NULL; static bool enabled = false; +struct loginlog_interface loginlog_s; +struct loginlog_interface *loginlog; + // Returns the number of failed login attempts by the ip in the last minutes. unsigned long loginlog_failedattempts(uint32 ip, unsigned int minutes) @@ -206,16 +209,16 @@ bool loginlog_config_read(const char *filename, bool imported) if (!libconfig->load_file(&config, filename)) return false; // Error message is already shown by libconfig->load_file - if (!loginlog_config_read_names(filename, &config, imported)) + if (!loginlog->config_read_names(filename, &config, imported)) retval = false; - if (!loginlog_config_read_log(filename, &config, imported)) + if (!loginlog->config_read_log(filename, &config, imported)) retval = false; if (libconfig->lookup_string(&config, "import", &import) == CONFIG_TRUE) { if (strcmp(import, filename) == 0 || strcmp(import, "conf/common/inter-server.conf") == 0) { ShowWarning("inter_config_read: Loop detected! Skipping 'import'...\n"); } else { - if (!loginlog_config_read(import, true)) + if (!loginlog->config_read(import, true)) retval = false; } } @@ -223,3 +226,15 @@ bool loginlog_config_read(const char *filename, bool imported) libconfig->destroy(&config); return retval; } + +void loginlog_defaults(void) +{ + loginlog = &loginlog_s; + loginlog->failedattempts = loginlog_failedattempts; + loginlog->log = loginlog_log; + loginlog->init = loginlog_init; + loginlog->final = loginlog_final; + loginlog->config_read_names = loginlog_config_read_names; + loginlog->config_read_log = loginlog_config_read_log; + loginlog->config_read = loginlog_config_read; +} diff --git a/src/login/loginlog.h b/src/login/loginlog.h index 8153dd769..8edbedbaa 100644 --- a/src/login/loginlog.h +++ b/src/login/loginlog.h @@ -21,15 +21,28 @@ #ifndef LOGIN_LOGINLOG_H #define LOGIN_LOGINLOG_H +#include "common/hercules.h" #include "common/cbasetypes.h" +struct config_t; + +/** + * Loginlog.c Interface + **/ +struct loginlog_interface { + unsigned long (*failedattempts) (uint32 ip, unsigned int minutes); + void (*log) (uint32 ip, const char* username, int rcode, const char* message); + bool (*init) (void); + bool (*final) (void); + bool (*config_read_names) (const char *filename, struct config_t *config, bool imported); + bool (*config_read_log) (const char *filename, struct config_t *config, bool imported); + bool (*config_read) (const char *filename, bool imported); +}; + #ifdef HERCULES_CORE -// TODO: Interface -unsigned long loginlog_failedattempts(uint32 ip, unsigned int minutes); -void loginlog_log(uint32 ip, const char* username, int rcode, const char* message); -bool loginlog_init(void); -bool loginlog_final(void); -bool loginlog_config_read(const char *filename, bool imported); +void loginlog_defaults(void); #endif // HERCULES_CORE +HPShared struct loginlog_interface *loginlog; + #endif /* LOGIN_LOGINLOG_H */ diff --git a/src/plugins/HPMHooking.c b/src/plugins/HPMHooking.c index 3e556881e..8686a07be 100644 --- a/src/plugins/HPMHooking.c +++ b/src/plugins/HPMHooking.c @@ -39,6 +39,7 @@ PRAGMA_GCC5(GCC diagnostic ignored "-Wdiscarded-qualifiers") #include "login/lclif.h" #include "login/lclif.p.h" #include "login/login.h" +#include "login/loginlog.h" #elif defined (HPMHOOKING_CHAR) #define HPM_SERVER_TYPE SERVER_TYPE_CHAR #define HPM_CORE_INCLUDE "HPMHooking/HPMHooking_char.HPMHooksCore.inc" -- cgit v1.2.3-60-g2f50 From 5c799fd9628efb9d45f32ef1138363bbf814c9a2 Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Tue, 6 Feb 2018 21:13:29 +0300 Subject: Add global variables into loginlog interface. --- src/login/loginlog.c | 86 +++++++++++++++++++++++++++------------------------- src/login/loginlog.h | 13 ++++++++ 2 files changed, 58 insertions(+), 41 deletions(-) (limited to 'src/login/loginlog.c') diff --git a/src/login/loginlog.c b/src/login/loginlog.c index 38be53193..0e4cc79f0 100644 --- a/src/login/loginlog.c +++ b/src/login/loginlog.c @@ -33,20 +33,10 @@ #include // exit -// Sql settings -static char log_db_hostname[32] = "127.0.0.1"; -static uint16 log_db_port = 3306; -static char log_db_username[32] = "ragnarok"; -static char log_db_password[100] = "ragnarok"; -static char log_db_database[32] = "ragnarok"; -static char log_codepage[32] = ""; -static char log_login_db[256] = "loginlog"; - -static struct Sql *sql_handle = NULL; -static bool enabled = false; struct loginlog_interface loginlog_s; struct loginlog_interface *loginlog; +struct s_loginlog_dbs loginlogdbs; // Returns the number of failed login attempts by the ip in the last minutes. @@ -54,19 +44,19 @@ unsigned long loginlog_failedattempts(uint32 ip, unsigned int minutes) { unsigned long failures = 0; - if( !enabled ) + if( !loginlog->enabled ) return 0; - if( SQL_ERROR == SQL->Query(sql_handle, "SELECT count(*) FROM `%s` WHERE `ip` = '%s' AND `rcode` = '1' AND `time` > NOW() - INTERVAL %u MINUTE", - log_login_db, sockt->ip2str(ip,NULL), minutes) )// how many times failed account? in one ip. - Sql_ShowDebug(sql_handle); + if( SQL_ERROR == SQL->Query(loginlog->sql_handle, "SELECT count(*) FROM `%s` WHERE `ip` = '%s' AND `rcode` = '1' AND `time` > NOW() - INTERVAL %u MINUTE", + loginlog->dbs->log_login_db, sockt->ip2str(ip,NULL), minutes) )// how many times failed account? in one ip. + Sql_ShowDebug(loginlog->sql_handle); - if( SQL_SUCCESS == SQL->NextRow(sql_handle) ) + if( SQL_SUCCESS == SQL->NextRow(loginlog->sql_handle) ) { char* data; - SQL->GetData(sql_handle, 0, &data, NULL); + SQL->GetData(loginlog->sql_handle, 0, &data, NULL); failures = strtoul(data, NULL, 10); - SQL->FreeResult(sql_handle); + SQL->FreeResult(loginlog->sql_handle); } return failures; } @@ -84,43 +74,43 @@ void loginlog_log(uint32 ip, const char* username, int rcode, const char* messag nullpo_retv(username); nullpo_retv(message); - if( !enabled ) + if( !loginlog->enabled ) return; - SQL->EscapeStringLen(sql_handle, esc_username, username, strnlen(username, NAME_LENGTH)); - SQL->EscapeStringLen(sql_handle, esc_message, message, strnlen(message, 255)); + SQL->EscapeStringLen(loginlog->sql_handle, esc_username, username, strnlen(username, NAME_LENGTH)); + SQL->EscapeStringLen(loginlog->sql_handle, esc_message, message, strnlen(message, 255)); - retcode = SQL->Query(sql_handle, + retcode = SQL->Query(loginlog->sql_handle, "INSERT INTO `%s`(`time`,`ip`,`user`,`rcode`,`log`) VALUES (NOW(), '%s', '%s', '%d', '%s')", - log_login_db, sockt->ip2str(ip,NULL), esc_username, rcode, esc_message); + loginlog->dbs->log_login_db, sockt->ip2str(ip,NULL), esc_username, rcode, esc_message); if( retcode != SQL_SUCCESS ) - Sql_ShowDebug(sql_handle); + Sql_ShowDebug(loginlog->sql_handle); } bool loginlog_init(void) { - sql_handle = SQL->Malloc(); + loginlog->sql_handle = SQL->Malloc(); - if (SQL_ERROR == SQL->Connect(sql_handle, log_db_username, log_db_password, - log_db_hostname, log_db_port, log_db_database)) { - Sql_ShowDebug(sql_handle); - SQL->Free(sql_handle); + if (SQL_ERROR == SQL->Connect(loginlog->sql_handle, loginlog->dbs->log_db_username, loginlog->dbs->log_db_password, + loginlog->dbs->log_db_hostname, loginlog->dbs->log_db_port, loginlog->dbs->log_db_database)) { + Sql_ShowDebug(loginlog->sql_handle); + SQL->Free(loginlog->sql_handle); exit(EXIT_FAILURE); } - if (log_codepage[0] != '\0' && SQL_ERROR == SQL->SetEncoding(sql_handle, log_codepage)) - Sql_ShowDebug(sql_handle); + if (loginlog->dbs->log_codepage[0] != '\0' && SQL_ERROR == SQL->SetEncoding(loginlog->sql_handle, loginlog->dbs->log_codepage)) + Sql_ShowDebug(loginlog->sql_handle); - enabled = true; + loginlog->enabled = true; return true; } bool loginlog_final(void) { - SQL->Free(sql_handle); - sql_handle = NULL; + SQL->Free(loginlog->sql_handle); + loginlog->sql_handle = NULL; return true; } @@ -148,7 +138,7 @@ bool loginlog_config_read_names(const char *filename, struct config_t *config, b return false; } - libconfig->setting_lookup_mutable_string(setting, "login_db", log_login_db, sizeof(log_login_db)); + libconfig->setting_lookup_mutable_string(setting, "login_db", loginlog->dbs->log_login_db, sizeof(loginlog->dbs->log_login_db)); return true; } @@ -177,13 +167,13 @@ bool loginlog_config_read_log(const char *filename, struct config_t *config, boo return false; } - libconfig->setting_lookup_mutable_string(setting, "db_hostname", log_db_hostname, sizeof(log_db_hostname)); - libconfig->setting_lookup_mutable_string(setting, "db_database", log_db_database, sizeof(log_db_database)); - libconfig->setting_lookup_mutable_string(setting, "db_username", log_db_username, sizeof(log_db_username)); - libconfig->setting_lookup_mutable_string(setting, "db_password", log_db_password, sizeof(log_db_password)); + libconfig->setting_lookup_mutable_string(setting, "db_hostname", loginlog->dbs->log_db_hostname, sizeof(loginlog->dbs->log_db_hostname)); + libconfig->setting_lookup_mutable_string(setting, "db_database", loginlog->dbs->log_db_database, sizeof(loginlog->dbs->log_db_database)); + libconfig->setting_lookup_mutable_string(setting, "db_username", loginlog->dbs->log_db_username, sizeof(loginlog->dbs->log_db_username)); + libconfig->setting_lookup_mutable_string(setting, "db_password", loginlog->dbs->log_db_password, sizeof(loginlog->dbs->log_db_password)); - libconfig->setting_lookup_uint16(setting, "db_port", &log_db_port); - libconfig->setting_lookup_mutable_string(setting, "codepage", log_codepage, sizeof(log_codepage)); + libconfig->setting_lookup_uint16(setting, "db_port", &loginlog->dbs->log_db_port); + libconfig->setting_lookup_mutable_string(setting, "codepage", loginlog->dbs->log_codepage, sizeof(loginlog->dbs->log_codepage)); return true; } @@ -230,6 +220,20 @@ bool loginlog_config_read(const char *filename, bool imported) void loginlog_defaults(void) { loginlog = &loginlog_s; + loginlog->dbs = &loginlogdbs; + + loginlog->sql_handle = NULL; + loginlog->enabled = false; + + // Sql settings + strcpy(loginlog->dbs->log_db_hostname, "127.0.0.1"); + loginlog->dbs->log_db_port = 3306; + strcpy(loginlog->dbs->log_db_username, "ragnarok"); + strcpy(loginlog->dbs->log_db_password, "ragnarok"); + strcpy(loginlog->dbs->log_db_database, "ragnarok"); + *loginlog->dbs->log_codepage = 0; + strcpy(loginlog->dbs->log_login_db, "loginlog"); + loginlog->failedattempts = loginlog_failedattempts; loginlog->log = loginlog_log; loginlog->init = loginlog_init; diff --git a/src/login/loginlog.h b/src/login/loginlog.h index 8edbedbaa..fecb9b364 100644 --- a/src/login/loginlog.h +++ b/src/login/loginlog.h @@ -26,10 +26,23 @@ struct config_t; +struct s_loginlog_dbs { + char log_db_hostname[32]; + uint16 log_db_port; + char log_db_username[32]; + char log_db_password[100]; + char log_db_database[32]; + char log_codepage[32]; + char log_login_db[256]; +}; + /** * Loginlog.c Interface **/ struct loginlog_interface { + struct Sql *sql_handle; + bool enabled; + struct s_loginlog_dbs *dbs; unsigned long (*failedattempts) (uint32 ip, unsigned int minutes); void (*log) (uint32 ip, const char* username, int rcode, const char* message); bool (*init) (void); -- cgit v1.2.3-60-g2f50