loginlog_sql.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. // Copyright (c) Athena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. #include "../common/cbasetypes.h"
  4. #include "../common/mmo.h"
  5. #include "../common/socket.h"
  6. #include "../common/sql.h"
  7. #include "../common/strlib.h"
  8. #include <string.h>
  9. #include <stdlib.h> // exit
  10. // global sql settings (in ipban_sql.c)
  11. static char global_db_hostname[32] = "127.0.0.1";
  12. static uint16 global_db_port = 3306;
  13. static char global_db_username[32] = "ragnarok";
  14. static char global_db_password[32] = "";
  15. static char global_db_database[32] = "ragnarok";
  16. static char global_codepage[32] = "";
  17. // local sql settings
  18. static char log_db_hostname[32] = "";
  19. static uint16 log_db_port = 0;
  20. static char log_db_username[32] = "";
  21. static char log_db_password[32] = "";
  22. static char log_db_database[32] = "";
  23. static char log_codepage[32] = "";
  24. static char log_login_db[256] = "loginlog";
  25. static Sql* sql_handle = NULL;
  26. static bool enabled = false;
  27. // Returns the number of failed login attemps by the ip in the last minutes.
  28. unsigned long loginlog_failedattempts(uint32 ip, unsigned int minutes)
  29. {
  30. unsigned long failures = 0;
  31. if( !enabled )
  32. return 0;
  33. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT count(*) FROM `%s` WHERE `ip` = '%s' AND `rcode` = '1' AND `time` > NOW() - INTERVAL %d MINUTE",
  34. log_login_db, ip2str(ip,NULL), minutes) )// how many times failed account? in one ip.
  35. Sql_ShowDebug(sql_handle);
  36. if( SQL_SUCCESS == Sql_NextRow(sql_handle) )
  37. {
  38. char* data;
  39. Sql_GetData(sql_handle, 0, &data, NULL);
  40. failures = strtoul(data, NULL, 10);
  41. Sql_FreeResult(sql_handle);
  42. }
  43. return failures;
  44. }
  45. /*=============================================
  46. * Records an event in the login log
  47. *---------------------------------------------*/
  48. void login_log(uint32 ip, const char* username, int rcode, const char* message)
  49. {
  50. char esc_username[NAME_LENGTH*2+1];
  51. char esc_message[255*2+1];
  52. int retcode;
  53. if( !enabled )
  54. return;
  55. Sql_EscapeStringLen(sql_handle, esc_username, username, strnlen(username, NAME_LENGTH));
  56. Sql_EscapeStringLen(sql_handle, esc_message, message, strnlen(message, 255));
  57. retcode = Sql_Query(sql_handle,
  58. "INSERT INTO `%s`(`time`,`ip`,`user`,`rcode`,`log`) VALUES (NOW(), '%s', '%s', '%d', '%s')",
  59. log_login_db, ip2str(ip,NULL), esc_username, rcode, esc_message);
  60. if( retcode != SQL_SUCCESS )
  61. Sql_ShowDebug(sql_handle);
  62. }
  63. bool loginlog_init(void)
  64. {
  65. const char* username;
  66. const char* password;
  67. const char* hostname;
  68. uint16 port;
  69. const char* database;
  70. const char* codepage;
  71. if( log_db_hostname[0] != '\0' )
  72. {// local settings
  73. username = log_db_username;
  74. password = log_db_password;
  75. hostname = log_db_hostname;
  76. port = log_db_port;
  77. database = log_db_database;
  78. codepage = log_codepage;
  79. }
  80. else
  81. {// global settings
  82. username = global_db_username;
  83. password = global_db_password;
  84. hostname = global_db_hostname;
  85. port = global_db_port;
  86. database = global_db_database;
  87. codepage = global_codepage;
  88. }
  89. sql_handle = Sql_Malloc();
  90. if( SQL_ERROR == Sql_Connect(sql_handle, username, password, hostname, port, database) )
  91. {
  92. Sql_ShowDebug(sql_handle);
  93. Sql_Free(sql_handle);
  94. exit(EXIT_FAILURE);
  95. }
  96. if( codepage[0] != '\0' && SQL_ERROR == Sql_SetEncoding(sql_handle, codepage) )
  97. Sql_ShowDebug(sql_handle);
  98. enabled = true;
  99. return true;
  100. }
  101. bool loginlog_final(void)
  102. {
  103. Sql_Free(sql_handle);
  104. sql_handle = NULL;
  105. return true;
  106. }
  107. bool loginlog_config_read(const char* key, const char* value)
  108. {
  109. const char* signature;
  110. signature = "sql.";
  111. if( strncmpi(key, signature, strlen(signature)) == 0 )
  112. {
  113. key += strlen(signature);
  114. if( strcmpi(key, "db_hostname") == 0 )
  115. safestrncpy(global_db_hostname, value, sizeof(global_db_hostname));
  116. else
  117. if( strcmpi(key, "db_port") == 0 )
  118. global_db_port = (uint16)strtoul(value, NULL, 10);
  119. else
  120. if( strcmpi(key, "db_username") == 0 )
  121. safestrncpy(global_db_username, value, sizeof(global_db_username));
  122. else
  123. if( strcmpi(key, "db_password") == 0 )
  124. safestrncpy(global_db_password, value, sizeof(global_db_password));
  125. else
  126. if( strcmpi(key, "db_database") == 0 )
  127. safestrncpy(global_db_database, value, sizeof(global_db_database));
  128. else
  129. if( strcmpi(key, "codepage") == 0 )
  130. safestrncpy(global_codepage, value, sizeof(global_codepage));
  131. else
  132. return false;// not found
  133. return true;
  134. }
  135. if( strcmpi(key, "log_db_ip") == 0 )
  136. safestrncpy(log_db_hostname, value, sizeof(log_db_hostname));
  137. else
  138. if( strcmpi(key, "log_db_port") == 0 )
  139. log_db_port = (uint16)strtoul(value, NULL, 10);
  140. else
  141. if( strcmpi(key, "log_db_id") == 0 )
  142. safestrncpy(log_db_username, value, sizeof(log_db_username));
  143. else
  144. if( strcmpi(key, "log_db_pw") == 0 )
  145. safestrncpy(log_db_password, value, sizeof(log_db_password));
  146. else
  147. if( strcmpi(key, "log_db_db") == 0 )
  148. safestrncpy(log_db_database, value, sizeof(log_db_database));
  149. else
  150. if( strcmpi(key, "log_codepage") == 0 )
  151. safestrncpy(log_codepage, value, sizeof(log_codepage));
  152. else
  153. if( strcmpi(key, "log_login_db") == 0 )
  154. safestrncpy(log_login_db, value, sizeof(log_login_db));
  155. else
  156. return false;
  157. return true;
  158. }