diff options
author | FlavioJS <FlavioJS@54d463be-8e91-2dee-dedb-b68131a5f0ec> | 2008-08-19 15:48:57 +0000 |
---|---|---|
committer | FlavioJS <FlavioJS@54d463be-8e91-2dee-dedb-b68131a5f0ec> | 2008-08-19 15:48:57 +0000 |
commit | 73c06082f78f6ba79e40bf66c531d5ff99082fee (patch) | |
tree | adf94e026ce531dc63c690ac3f16fff1dc30ed6d | |
parent | db6e4aaaeea4e7053e17576db730def8b3cd4fd9 (diff) | |
download | hercules-73c06082f78f6ba79e40bf66c531d5ff99082fee.tar.gz hercules-73c06082f78f6ba79e40bf66c531d5ff99082fee.tar.bz2 hercules-73c06082f78f6ba79e40bf66c531d5ff99082fee.tar.xz hercules-73c06082f78f6ba79e40bf66c531d5ff99082fee.zip |
* Added global sql settings 'sql.*' to inter_athena.conf.
* Made account, ipban and loginlog use the global sql settings when the hostname is empty.
* Commented 'ipban.sql.*' and 'account.sql.*' so they default to the global settings.
* Removed deprecated settings 'case_sensitive' and 'add_to_unlimited_account' from in login_athena.conf.
* Made inter_athena.conf be imported from login_athena.conf instead of being handled separate.
* Changed ipban to get the failed login attempts of an ip from loginlog (respects if loginlog is enabled or not).
git-svn-id: https://rathena.svn.sourceforge.net/svnroot/rathena/trunk@13092 54d463be-8e91-2dee-dedb-b68131a5f0ec
-rw-r--r-- | Changelog-Trunk.txt | 8 | ||||
-rw-r--r-- | conf/Changelog.txt | 5 | ||||
-rw-r--r-- | conf/inter_athena.conf | 11 | ||||
-rw-r--r-- | conf/login_athena.conf | 41 | ||||
-rw-r--r-- | src/login/account_sql.c | 271 | ||||
-rw-r--r-- | src/login/ipban_sql.c | 226 | ||||
-rw-r--r-- | src/login/login.c | 38 | ||||
-rw-r--r-- | src/login/login.h | 1 | ||||
-rw-r--r-- | src/login/loginlog.h | 1 | ||||
-rw-r--r-- | src/login/loginlog_sql.c | 110 | ||||
-rw-r--r-- | src/login/loginlog_txt.c | 7 |
11 files changed, 462 insertions, 257 deletions
diff --git a/Changelog-Trunk.txt b/Changelog-Trunk.txt index 41abbfb9f..b984aa66c 100644 --- a/Changelog-Trunk.txt +++ b/Changelog-Trunk.txt @@ -3,6 +3,14 @@ Date Added AS OF SVN REV. 5091, WE ARE NOW USING TRUNK. ALL UNTESTED BUGFIXES/FEATURES GO INTO TRUNK. IF YOU HAVE A WORKING AND TESTED BUGFIX PUT IT INTO STABLE AS WELL AS TRUNK. +2008/08/19 + * Added global sql settings 'sql.*' to inter_athena.conf. + * Made account, ipban and loginlog use the global sql settings when the hostname is empty. + * Commented 'ipban.sql.*' and 'account.sql.*' so they default to the global settings. + * Removed deprecated settings 'case_sensitive' and 'add_to_unlimited_account' from in login_athena.conf. + * Made inter_athena.conf be imported from login_athena.conf instead of being handled separate. + * Changed ipban to get the failed login attempts of an ip from loginlog + (respects if loginlog is enabled or not). [FlavioJS] 2008/08/16 * Added a mapreg txt->sql converter script to /tools, because the sql mapserver doesn't read the txt mapreg savefile anymore and people diff --git a/conf/Changelog.txt b/conf/Changelog.txt index ebcf65639..55d8143d0 100644 --- a/conf/Changelog.txt +++ b/conf/Changelog.txt @@ -1,5 +1,10 @@ Date Added +2008/08/19 + * Added global sql settings 'sql.*' to inter_athena.conf. + * Commented 'ipban.sql.*' and 'account.sql.*' so they default to the global settings. + * Removed deprecated settings 'case_sensitive' and 'add_to_unlimited_account' from in login_athena.conf. + * Made inter_athena.conf be imported from login_athena.conf. [FlavioJS] 2008/08/13 * Added config settings mapreg_txt and mapreg_db to inter config [ultramage] 2008/08/10 diff --git a/conf/inter_athena.conf b/conf/inter_athena.conf index 00d3f155f..e7fe1a3bd 100644 --- a/conf/inter_athena.conf +++ b/conf/inter_athena.conf @@ -54,6 +54,16 @@ mapreg_txt: save/mapreg.txt // "Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)" // and you have localhost, switch it to 127.0.0.1 +// Global SQL settings +// overriden by local settings when the hostname is defined there +// (currently only the login-server reads/obeys these settings) +sql.db_hostname: 127.0.0.1 +sql.db_port: 3306 +sql.db_username: ragnarok +sql.db_password: ragnarok +sql.db_database: ragnarok +sql.codepage: + // MySQL Character SQL server char_server_ip: 127.0.0.1 char_server_port: 3306 @@ -74,6 +84,7 @@ log_db_port: 3306 log_db_id: ragnarok log_db_pw: ragnarok log_db_db: log +log_codepage: // DO NOT CHANGE ANYTHING BEYOND THIS LINE UNLESS YOU KNOW YOUR DATABASE DAMN WELL // this is meant for people who KNOW their stuff, and for some reason want to change their diff --git a/conf/login_athena.conf b/conf/login_athena.conf index 28f022c42..6026b7670 100644 --- a/conf/login_athena.conf +++ b/conf/login_athena.conf @@ -50,9 +50,6 @@ admin_allowed_host: 127.0.0.1 // This prevents usage of >& log.file console: off -// Are logins case sensitive? (SQL only) -case_sensitive: yes - // Can you use _M/_F to make new accounts on the server? new_account: yes @@ -81,11 +78,6 @@ date_format: %Y-%m-%d %H:%M:%S // 0: all players (normal player are 0. it's default), 1-99: GM level at least with level x min_level_to_connect: 0 -// Give possibility to adjust (ladmin command: timeadd) the time of an unlimited account. -// If set to on/1/yes..., the adjustment is be done from actual time to set the final time of the account. -// If set to no/0/no..., the adjustment can not be done on an unlimited account. You must set (ladmin command: timeset) a final time before to adjust (ladmin command: timeadd) -add_to_unlimited_account: off - // Starting additional sec from now for the limited time at creation of account // -1: new account are created with UNlimited time (default value) // 0 or more: new accounts was created by addition of the value (in sec) to the actual time (to set first limited time) @@ -103,12 +95,12 @@ use_MD5_passwords: no // Ipban features (SQL only) ipban.enable: yes -ipban.sql.db_hostname: 127.0.0.1 -ipban.sql.db_port: 3306 -ipban.sql.db_username: ragnarok -ipban.sql.db_password: ragnarok -ipban.sql.db_database: ragnarok -ipban.sql.ipban_table: ipbanlist +//ipban.sql.db_hostname: 127.0.0.1 +//ipban.sql.db_port: 3306 +//ipban.sql.db_username: ragnarok +//ipban.sql.db_password: ragnarok +//ipban.sql.db_database: ragnarok +//ipban.sql.ipban_table: ipbanlist // Dynamic password failure ipban system ipban.dynamic_pass_failure_ban: yes ipban.dynamic_pass_failure_ban_interval: 5 @@ -135,14 +127,15 @@ account.engine: auto account.txt.account_db: save/account.txt account.txt.case_sensitive: no // SQL -account.sql.db_hostname: 127.0.0.1 -account.sql.db_port: 3306 -account.sql.db_username: ragnarok -account.sql.db_password: ragnarok -account.sql.db_database: ragnarok -account.sql.codepage: -account.sql.case_sensitive: no -account.sql.account_db: login -account.sql.accreg_db: global_reg_value - +//account.sql.db_hostname: 127.0.0.1 +//account.sql.db_port: 3306 +//account.sql.db_username: ragnarok +//account.sql.db_password: ragnarok +//account.sql.db_database: ragnarok +//account.sql.codepage: +//account.sql.case_sensitive: no +//account.sql.account_db: login +//account.sql.accreg_db: global_reg_value + +import: conf/inter_athena.conf import: conf/import/login_conf.txt diff --git a/src/login/account_sql.c b/src/login/account_sql.c index 40b87829c..c2f4a9965 100644 --- a/src/login/account_sql.c +++ b/src/login/account_sql.c @@ -21,12 +21,21 @@ typedef struct AccountDB_SQL Sql* accounts; // SQL accounts storage - char db_hostname[32]; + // global sql settings + char global_db_hostname[32]; + uint16 global_db_port; + char global_db_username[32]; + char global_db_password[32]; + char global_db_database[32]; + char global_codepage[32]; + // local sql settings + char db_hostname[32]; uint16 db_port; - char db_username[32]; - char db_password[32]; - char db_database[32]; - char codepage[32]; + char db_username[32]; + char db_password[32]; + char db_database[32]; + char codepage[32]; + // other settings bool case_sensitive; char account_db[32]; char accreg_db[32]; @@ -78,12 +87,21 @@ AccountDB* account_db_sql(void) // initialize to default values db->accounts = NULL; - safestrncpy(db->db_hostname, "127.0.0.1", sizeof(db->db_hostname)); + // global sql settings + safestrncpy(db->global_db_hostname, "127.0.0.1", sizeof(db->global_db_hostname)); + db->global_db_port = 3306; + safestrncpy(db->global_db_username, "ragnarok", sizeof(db->global_db_username)); + safestrncpy(db->global_db_password, "ragnarok", sizeof(db->global_db_password)); + safestrncpy(db->global_db_database, "ragnarok", sizeof(db->global_db_database)); + safestrncpy(db->global_codepage, "", sizeof(db->global_codepage)); + // local sql settings + safestrncpy(db->db_hostname, "", sizeof(db->db_hostname)); db->db_port = 3306; - safestrncpy(db->db_username, "ragnarok", sizeof(db->db_username)); - safestrncpy(db->db_password, "ragnarok", sizeof(db->db_password)); - safestrncpy(db->db_database, "ragnarok", sizeof(db->db_database)); + safestrncpy(db->db_username, "", sizeof(db->db_username)); + safestrncpy(db->db_password, "", sizeof(db->db_password)); + safestrncpy(db->db_database, "", sizeof(db->db_database)); safestrncpy(db->codepage, "", sizeof(db->codepage)); + // other settings db->case_sensitive = false; safestrncpy(db->account_db, "login", sizeof(db->account_db)); safestrncpy(db->accreg_db, "global_reg_value", sizeof(db->accreg_db)); @@ -100,11 +118,36 @@ static bool account_db_sql_init(AccountDB* self) { AccountDB_SQL* db = (AccountDB_SQL*)self; Sql* sql_handle; + const char* username; + const char* password; + const char* hostname; + uint16 port; + const char* database; + const char* codepage; db->accounts = Sql_Malloc(); sql_handle = db->accounts; - if( SQL_ERROR == Sql_Connect(sql_handle, db->db_username, db->db_password, db->db_hostname, db->db_port, db->db_database) ) + if( db->db_hostname[0] != '\0' ) + {// local settings + username = db->db_username; + password = db->db_password; + hostname = db->db_hostname; + port = db->db_port; + database = db->db_database; + codepage = db->codepage; + } + else + {// global settings + username = db->global_db_username; + password = db->global_db_password; + hostname = db->global_db_hostname; + port = db->global_db_port; + database = db->global_db_database; + codepage = db->global_codepage; + } + + if( SQL_ERROR == Sql_Connect(sql_handle, username, password, hostname, port, database) ) { Sql_ShowDebug(sql_handle); Sql_Free(db->accounts); @@ -112,7 +155,7 @@ static bool account_db_sql_init(AccountDB* self) return false; } - if( db->codepage[0] != '\0' && SQL_ERROR == Sql_SetEncoding(sql_handle, db->codepage) ) + if( codepage[0] != '\0' && SQL_ERROR == Sql_SetEncoding(sql_handle, codepage) ) Sql_ShowDebug(sql_handle); return true; @@ -132,102 +175,158 @@ static void account_db_sql_destroy(AccountDB* self) static bool account_db_sql_get_property(AccountDB* self, const char* key, char* buf, size_t buflen) { AccountDB_SQL* db = (AccountDB_SQL*)self; - const char* signature = "account.sql."; + const char* signature; - if( strcmp(key, "engine.name") == 0 ) + signature = "engine."; + if( strncmpi(key, signature, strlen(signature)) == 0 ) { - safesnprintf(buf, buflen, "sql"); + key += strlen(signature); + if( strcmpi(key, "name") == 0 ) + safesnprintf(buf, buflen, "sql"); + else + if( strcmpi(key, "version") == 0 ) + safesnprintf(buf, buflen, "%d", ACCOUNT_SQL_DB_VERSION); + else + if( strcmpi(key, "comment") == 0 ) + safesnprintf(buf, buflen, "SQL Account Database"); + else + return false;// not found return true; } - if( strcmp(key, "engine.version") == 0 ) + + signature = "sql."; + if( strncmpi(key, signature, strlen(signature)) == 0 ) { - safesnprintf(buf, buflen, "%d", ACCOUNT_SQL_DB_VERSION); + key += strlen(signature); + if( strcmpi(key, "db_hostname") == 0 ) + safesnprintf(buf, buflen, "%s", db->global_db_hostname); + else + if( strcmpi(key, "db_port") == 0 ) + safesnprintf(buf, buflen, "%d", db->global_db_port); + else + if( strcmpi(key, "db_username") == 0 ) + safesnprintf(buf, buflen, "%s", db->global_db_username); + else + if( strcmpi(key, "db_password") == 0 ) + safesnprintf(buf, buflen, "%s", db->global_db_password); + else + if( strcmpi(key, "db_database") == 0 ) + safesnprintf(buf, buflen, "%s", db->global_db_database); + else + if( strcmpi(key, "codepage") == 0 ) + safesnprintf(buf, buflen, "%s", db->global_codepage); + else + return false;// not found return true; } - if( strcmp(key, "engine.comment") == 0 ) + + signature = "account.sql."; + if( strncmpi(key, signature, strlen(signature)) == 0 ) { - safesnprintf(buf, buflen, "SQL Account Database"); + key += strlen(signature); + if( strcmpi(key, "db_hostname") == 0 ) + safesnprintf(buf, buflen, "%s", db->db_hostname); + else + if( strcmpi(key, "db_port") == 0 ) + safesnprintf(buf, buflen, "%d", db->db_port); + else + if( strcmpi(key, "db_username") == 0 ) + safesnprintf(buf, buflen, "%s", db->db_username); + else + if( strcmpi(key, "db_password") == 0 ) + safesnprintf(buf, buflen, "%s", db->db_password); + else + if( strcmpi(key, "db_database") == 0 ) + safesnprintf(buf, buflen, "%s", db->db_database); + else + if( strcmpi(key, "codepage") == 0 ) + safesnprintf(buf, buflen, "%s", db->codepage); + else + if( strcmpi(key, "case_sensitive") == 0 ) + safesnprintf(buf, buflen, "%d", (db->case_sensitive ? 1 : 0)); + else + if( strcmpi(key, "account_db") == 0 ) + safesnprintf(buf, buflen, "%s", db->account_db); + else + if( strcmpi(key, "accreg_db") == 0 ) + safesnprintf(buf, buflen, "%s", db->accreg_db); + else + return false;// not found return true; } - if( strncmp(key, signature, strlen(signature)) != 0 ) - return false; - - key += strlen(signature); - - if( strcmpi(key, "db_hostname") == 0 ) - safesnprintf(buf, buflen, "%s", db->db_hostname); - else - if( strcmpi(key, "db_port") == 0 ) - safesnprintf(buf, buflen, "%d", db->db_port); - else - if( strcmpi(key, "db_username") == 0 ) - safesnprintf(buf, buflen, "%s", db->db_username); - else - if( strcmpi(key, "db_password") == 0 ) - safesnprintf(buf, buflen, "%s", db->db_password); - else - if( strcmpi(key, "db_database") == 0 ) - safesnprintf(buf, buflen, "%s", db->db_database); - else - if( strcmpi(key, "codepage") == 0 ) - safesnprintf(buf, buflen, "%s", db->codepage); - else - if( strcmpi(key, "case_sensitive") == 0 ) - safesnprintf(buf, buflen, "%d", (db->case_sensitive ? 1 : 0)); - else - if( strcmpi(key, "account_db") == 0 ) - safesnprintf(buf, buflen, "%s", db->account_db); - else - if( strcmpi(key, "accreg_db") == 0 ) - safesnprintf(buf, buflen, "%s", db->accreg_db); - else - return false;// not found - - return true; + return false;// not found } /// if the option is supported, adjusts the internal state static bool account_db_sql_set_property(AccountDB* self, const char* key, const char* value) { AccountDB_SQL* db = (AccountDB_SQL*)self; - const char* signature = "account.sql."; + const char* signature; - if( strncmp(key, signature, strlen(signature)) != 0 ) - return false; - key += strlen(signature); + signature = "sql."; + if( strncmp(key, signature, strlen(signature)) == 0 ) + { + key += strlen(signature); + if( strcmpi(key, "db_hostname") == 0 ) + safestrncpy(db->global_db_hostname, value, sizeof(db->global_db_hostname)); + else + if( strcmpi(key, "db_port") == 0 ) + db->global_db_port = (uint16)strtoul(value, NULL, 10); + else + if( strcmpi(key, "db_username") == 0 ) + safestrncpy(db->global_db_username, value, sizeof(db->global_db_username)); + else + if( strcmpi(key, "db_password") == 0 ) + safestrncpy(db->global_db_password, value, sizeof(db->global_db_password)); + else + if( strcmpi(key, "db_database") == 0 ) + safestrncpy(db->global_db_database, value, sizeof(db->global_db_database)); + else + if( strcmpi(key, "codepage") == 0 ) + safestrncpy(db->global_codepage, value, sizeof(db->global_codepage)); + else + return false;// not found + return true; + } - if( strcmpi(key, "db_hostname") == 0 ) - safestrncpy(db->db_hostname, value, sizeof(db->db_hostname)); - else - if( strcmpi(key, "db_port") == 0 ) - db->db_port = (uint16)strtoul(value, NULL, 10); - else - if( strcmpi(key, "db_username") == 0 ) - safestrncpy(db->db_username, value, sizeof(db->db_username)); - else - if( strcmpi(key, "db_password") == 0 ) - safestrncpy(db->db_password, value, sizeof(db->db_password)); - else - if( strcmpi(key, "db_database") == 0 ) - safestrncpy(db->db_database, value, sizeof(db->db_database)); - else - if( strcmpi(key, "codepage") == 0 ) - safestrncpy(db->codepage, value, sizeof(db->codepage)); - else - if( strcmpi(key, "case_sensitive") == 0 ) - db->case_sensitive = config_switch(value); - else - if( strcmpi(key, "account_db") == 0 ) - safestrncpy(db->account_db, value, sizeof(db->account_db)); - else - if( strcmpi(key, "accreg_db") == 0 ) - safestrncpy(db->accreg_db, value, sizeof(db->accreg_db)); - else // no match - return false; + signature = "account.sql."; + if( strncmp(key, signature, strlen(signature)) == 0 ) + { + key += strlen(signature); + if( strcmpi(key, "db_hostname") == 0 ) + safestrncpy(db->db_hostname, value, sizeof(db->db_hostname)); + else + if( strcmpi(key, "db_port") == 0 ) + db->db_port = (uint16)strtoul(value, NULL, 10); + else + if( strcmpi(key, "db_username") == 0 ) + safestrncpy(db->db_username, value, sizeof(db->db_username)); + else + if( strcmpi(key, "db_password") == 0 ) + safestrncpy(db->db_password, value, sizeof(db->db_password)); + else + if( strcmpi(key, "db_database") == 0 ) + safestrncpy(db->db_database, value, sizeof(db->db_database)); + else + if( strcmpi(key, "codepage") == 0 ) + safestrncpy(db->codepage, value, sizeof(db->codepage)); + else + if( strcmpi(key, "case_sensitive") == 0 ) + db->case_sensitive = config_switch(value); + else + if( strcmpi(key, "account_db") == 0 ) + safestrncpy(db->account_db, value, sizeof(db->account_db)); + else + if( strcmpi(key, "accreg_db") == 0 ) + safestrncpy(db->accreg_db, value, sizeof(db->accreg_db)); + else + return false;// not found + return true; + } - return true; + return false;// not found } /// create a new account entry diff --git a/src/login/ipban_sql.c b/src/login/ipban_sql.c index 7354ea5b5..aec74e06b 100644 --- a/src/login/ipban_sql.c +++ b/src/login/ipban_sql.c @@ -10,29 +10,28 @@ #include "../common/timer.h" #include "login.h" #include "ipban.h" +#include "loginlog.h" #include <stdlib.h> #include <string.h> -// database options -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[32] = "ragnarok"; -static char ipban_db_database[32] = "ragnarok"; +// 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[32] = "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[32] = ""; +static char ipban_db_database[32] = ""; +static char ipban_codepage[32] = ""; static char ipban_table[32] = "ipbanlist"; -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[32] = "ragnarok"; -static char log_db_database[32] = "ragnarok"; -static char loginlog_table[32] = "loginlog"; - -static char default_codepage[32] = ""; - // globals -static Sql* sql_handle; -static Sql* logsql_handle; +static Sql* sql_handle = NULL; static int cleanup_timer_id = INVALID_TIMER; int ipban_cleanup(int tid, unsigned int tick, int id, intptr data); @@ -41,27 +40,43 @@ int ipban_cleanup(int tid, unsigned int tick, int id, intptr data); // initialize void ipban_init(void) { + const char* username; + const char* password; + const char* hostname; + uint16 port; + const char* database; + const char* codepage; + + 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, ipban_db_username, ipban_db_password, ipban_db_hostname, ipban_db_port, ipban_db_database) ) + if( SQL_ERROR == Sql_Connect(sql_handle, username, password, hostname, port, database) ) { Sql_ShowDebug(sql_handle); Sql_Free(sql_handle); exit(EXIT_FAILURE); } - if( default_codepage[0] != '\0' && SQL_ERROR == Sql_SetEncoding(sql_handle, default_codepage) ) + if( codepage[0] != '\0' && SQL_ERROR == Sql_SetEncoding(sql_handle, codepage) ) Sql_ShowDebug(sql_handle); - logsql_handle = Sql_Malloc(); - if( SQL_ERROR == Sql_Connect(logsql_handle, log_db_username, log_db_password, log_db_hostname, log_db_port, log_db_database) ) - { - Sql_ShowDebug(logsql_handle); - Sql_Free(logsql_handle); - exit(EXIT_FAILURE); - } - if( default_codepage[0] != '\0' && SQL_ERROR == Sql_SetEncoding(logsql_handle, default_codepage) ) - Sql_ShowDebug(logsql_handle); - // set up periodic cleanup of connection history and active bans add_timer_func_list(ipban_cleanup, "ipban_cleanup"); cleanup_timer_id = add_timer_interval(gettick()+10, ipban_cleanup, 0, 0, 60*1000); @@ -76,75 +91,92 @@ void ipban_final(void) // close connections Sql_Free(sql_handle); sql_handle = NULL; - Sql_Free(logsql_handle); - logsql_handle = NULL; } // load configuration options bool ipban_config_read(const char* key, const char* value) { - // login server settings - if( strcmpi(key, "ipban.enable") == 0 ) - login_config.ipban = (bool)config_switch(value); - else - if( strcmpi(key, "ipban.dynamic_pass_failure_ban") == 0 ) - login_config.dynamic_pass_failure_ban = (bool)config_switch(value); - else - if( strcmpi(key, "ipban.dynamic_pass_failure_ban_interval") == 0 ) - login_config.dynamic_pass_failure_ban_interval = atoi(value); - else - if( strcmpi(key, "ipban.dynamic_pass_failure_ban_limit") == 0 ) - login_config.dynamic_pass_failure_ban_limit = atoi(value); - else - if( strcmpi(key, "ipban.dynamic_pass_failure_ban_duration") == 0 ) - login_config.dynamic_pass_failure_ban_duration = atoi(value); - else + const char* signature; - // ipban table settings - if( strcmpi(key, "ipban.sql.db_hostname") == 0 ) - safestrncpy(ipban_db_hostname, value, sizeof(ipban_db_hostname)); - else - if( strcmpi(key, "ipban.sql.db_port") == 0 ) - ipban_db_port = (uint16)strtoul(value, NULL, 10); - else - if( strcmpi(key, "ipban.sql.db_username") == 0 ) - safestrncpy(ipban_db_username, value, sizeof(ipban_db_username)); - else - if( strcmpi(key, "ipban.sql.db_password") == 0 ) - safestrncpy(ipban_db_password, value, sizeof(ipban_db_password)); - else - if( strcmpi(key, "ipban.sql.db_database") == 0 ) - safestrncpy(ipban_db_database, value, sizeof(ipban_db_database)); - else - if( strcmpi(key, "ipban.sql.ipban_table") == 0 ) - safestrncpy(ipban_table, value, sizeof(ipban_table)); - else + 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; + } - // interserver settings - if( strcmpi(key, "log_db_ip") == 0 ) - safestrncpy(log_db_hostname, value, sizeof(log_db_hostname)); - else - if( strcmpi(key, "log_db_port") == 0 ) - log_db_port = (uint16)strtoul(value, NULL, 10); - else - if( strcmpi(key, "log_db_id") == 0 ) - safestrncpy(log_db_username, value, sizeof(log_db_username)); - else - if( strcmpi(key, "log_db_pw") == 0 ) - safestrncpy(log_db_password, value, sizeof(log_db_password)); - else - if( strcmpi(key, "log_db_db") == 0 ) - safestrncpy(log_db_database, value, sizeof(log_db_database)); - else - if( strcmpi(key, "loginlog_db") == 0 ) - safestrncpy(loginlog_table, value, sizeof(loginlog_table)); - else - if( strcmpi(key, "default_codepage") == 0 ) - safestrncpy(default_codepage, value, sizeof(default_codepage)); - else - return 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; + } + + 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; + } - return true; + return false;// not found } // check ip against active bans list @@ -175,19 +207,7 @@ bool ipban_check(uint32 ip) // log failed attempt void ipban_log(uint32 ip) { - unsigned long failures = 0; - if( SQL_ERROR == Sql_Query(logsql_handle, "SELECT count(*) FROM `%s` WHERE `ip` = '%s' AND `rcode` = '1' AND `time` > NOW() - INTERVAL %d MINUTE", - loginlog_table, ip2str(ip,NULL), login_config.dynamic_pass_failure_ban_interval) )// how many times failed account? in one ip. - Sql_ShowDebug(sql_handle); - - //check query result - if( SQL_SUCCESS == Sql_NextRow(logsql_handle) ) - { - char* data; - Sql_GetData(logsql_handle, 0, &data, NULL); - failures = strtoul(data, NULL, 10); - Sql_FreeResult(logsql_handle); - } + unsigned long 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/login.c b/src/login/login.c index d458a8b88..c92c8a970 100644 --- a/src/login/login.c +++ b/src/login/login.c @@ -1572,9 +1572,7 @@ int login_config_read(const char* cfgName) else if(!strcmpi(w1, "import")) login_config_read(w2); else - if( ipban_config_read(w1, w2) ) - continue; - else if(!strcmpi(w1, "account.engine")) + if(!strcmpi(w1, "account.engine")) safestrncpy(login_config.account_engine, w2, sizeof(login_config.account_engine)); else {// try the account engines @@ -1585,6 +1583,9 @@ int login_config_read(const char* cfgName) if( db && db->set_property(db, w1, w2) ) break; } + // try others + ipban_config_read(w1, w2); + loginlog_config_read(w1, w2); } } fclose(fp); @@ -1592,36 +1593,6 @@ int login_config_read(const char* cfgName) return 0; } -//----------------------------------- -// Reading interserver configuration file -//----------------------------------- -void inter_config_read(const char* cfgName) -{ - char line[1024], w1[1024], w2[1024]; - FILE* fp = fopen(cfgName, "r"); - if(fp == NULL) { - ShowError("file not found: %s\n", cfgName); - return; - } - ShowInfo("reading configuration file %s...\n", cfgName); - while(fgets(line, sizeof(line), fp)) - { - if (line[0] == '/' && line[1] == '/') - continue; - if (sscanf(line, "%[^:]: %[^\r\n]", w1, w2) < 2) - continue; - - // settings common for multiple components - ipban_config_read(w1,w2); - loginlog_config_read(w1,w2); - - if (!strcmpi(w1, "import")) - inter_config_read(w2); - } - fclose(fp); - ShowInfo("Done reading %s.\n", cfgName); -} - /// Get the engine selected in the config settings. /// Updates the config setting with the selected engine if 'auto'. static AccountDB* get_account_engine(void) @@ -1712,7 +1683,6 @@ int do_init(int argc, char** argv) login_set_defaults(); login_config_read((argc > 1) ? argv[1] : LOGIN_CONF_NAME); login_lan_config_read((argc > 2) ? argv[2] : LAN_CONF_NAME); - inter_config_read(INTER_CONF_NAME); srand((unsigned int)time(NULL)); diff --git a/src/login/login.h b/src/login/login.h index 7ec15464d..d9d37c344 100644 --- a/src/login/login.h +++ b/src/login/login.h @@ -7,7 +7,6 @@ #include "../common/mmo.h" // NAME_LENGTH #define LOGIN_CONF_NAME "conf/login_athena.conf" -#define INTER_CONF_NAME "conf/inter_athena.conf" #define LAN_CONF_NAME "conf/subnet_athena.conf" // supported encryption types: 1- passwordencrypt, 2- passwordencrypt2, 3- both diff --git a/src/login/loginlog.h b/src/login/loginlog.h index 749d0fbf1..a1ffaae85 100644 --- a/src/login/loginlog.h +++ b/src/login/loginlog.h @@ -5,6 +5,7 @@ #define __LOGINLOG_H_INCLUDED__ +unsigned long loginlog_failedattempts(uint32 ip, unsigned int minutes); void login_log(uint32 ip, const char* username, int rcode, const char* message); bool loginlog_init(void); bool loginlog_final(void); diff --git a/src/login/loginlog_sql.c b/src/login/loginlog_sql.c index 780236679..055594083 100644 --- a/src/login/loginlog_sql.c +++ b/src/login/loginlog_sql.c @@ -9,15 +9,47 @@ #include <string.h> #include <stdlib.h> // exit -char log_db_hostname[32] = "127.0.0.1"; -uint16 log_db_port = 3306; -char log_db_username[32] = "ragnarok"; -char log_db_password[32] = "ragnarok"; -char log_db_database[32] = "log"; -char loginlog_table[256] = "loginlog"; +// global sql settings (in ipban_sql.c) +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[32] = "ragnarok"; +static char global_db_database[32] = "ragnarok"; +static char global_codepage[32] = ""; +// local sql settings +static char log_db_hostname[32] = ""; +static uint16 log_db_port = 0; +static char log_db_username[32] = ""; +static char log_db_password[32] = ""; +static char log_db_database[32] = ""; +static char log_codepage[32] = ""; +static char loginlog_table[256] = "loginlog"; + +static Sql* sql_handle = NULL; +static bool enabled = false; + + +// Returns the number of failed login attemps 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 %d MINUTE", + loginlog_table, ip2str(ip,NULL), minutes) )// how many times failed account? in one ip. + Sql_ShowDebug(sql_handle); -Sql* sql_handle; -bool enabled = false; + 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; +} /*============================================= @@ -45,15 +77,44 @@ void login_log(uint32 ip, const char* username, int rcode, const char* message) bool loginlog_init(void) { + const char* username; + const char* password; + const char* hostname; + uint16 port; + const char* database; + const char* codepage; + + if( log_db_hostname[0] != '\0' ) + {// local settings + username = log_db_username; + password = log_db_password; + hostname = log_db_hostname; + port = log_db_port; + database = log_db_database; + codepage = log_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; + } + 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) ) + if( SQL_ERROR == Sql_Connect(sql_handle, username, password, hostname, port, database) ) { Sql_ShowDebug(sql_handle); Sql_Free(sql_handle); exit(EXIT_FAILURE); } + if( codepage[0] != '\0' && SQL_ERROR == Sql_SetEncoding(sql_handle, codepage) ) + Sql_ShowDebug(sql_handle); + enabled = true; return true; @@ -68,6 +129,34 @@ bool loginlog_final(void) bool loginlog_config_read(const char* key, const char* value) { + const char* signature; + + 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( strcmpi(key, "log_db_ip") == 0 ) safestrncpy(log_db_hostname, value, sizeof(log_db_hostname)); else @@ -83,6 +172,9 @@ bool loginlog_config_read(const char* key, const char* value) if( strcmpi(key, "log_db_db") == 0 ) safestrncpy(log_db_database, value, sizeof(log_db_database)); else + if( strcmpi(key, "log_codepage") == 0 ) + safestrncpy(log_codepage, value, sizeof(log_codepage)); + else if( strcmpi(key, "loginlog_db") == 0 ) safestrncpy(loginlog_table, value, sizeof(loginlog_table)); else diff --git a/src/login/loginlog_txt.c b/src/login/loginlog_txt.c index d40142110..724156ddc 100644 --- a/src/login/loginlog_txt.c +++ b/src/login/loginlog_txt.c @@ -18,6 +18,13 @@ char login_log_filename[1024] = "log/login.log"; +// Returns the number of failed login attemps by the ip in the last minutes. +unsigned long loginlog_failedattempts(uint32 ip, unsigned int minutes) +{ + // XXX not implemented + return 0; +} + /*============================================= * Records an event in the login log |