1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
|
/**
* This file is part of Hercules.
* http://herc.ws - http://github.com/HerculesWS/Hercules
*
* Copyright (C) 2012-2018 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 <http://www.gnu.org/licenses/>.
*/
#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 <stdlib.h> // exit
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.
unsigned long loginlog_failedattempts(uint32 ip, unsigned int minutes)
{
unsigned long failures = 0;
if( !loginlog->enabled )
return 0;
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(loginlog->sql_handle) )
{
char* data;
SQL->GetData(loginlog->sql_handle, 0, &data, NULL);
failures = strtoul(data, NULL, 10);
SQL->FreeResult(loginlog->sql_handle);
}
return failures;
}
/*=============================================
* Records an event in the login log
*---------------------------------------------*/
// TODO: add an enum of rcode values
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];
int retcode;
nullpo_retv(username);
nullpo_retv(message);
if( !loginlog->enabled )
return;
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(loginlog->sql_handle,
"INSERT INTO `%s`(`time`,`ip`,`user`,`rcode`,`log`) VALUES (NOW(), '%s', '%s', '%d', '%s')",
loginlog->dbs->log_login_db, sockt->ip2str(ip,NULL), esc_username, rcode, esc_message);
if( retcode != SQL_SUCCESS )
Sql_ShowDebug(loginlog->sql_handle);
}
bool loginlog_init(void)
{
loginlog->sql_handle = SQL->Malloc();
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 (loginlog->dbs->log_codepage[0] != '\0' && SQL_ERROR == SQL->SetEncoding(loginlog->sql_handle, loginlog->dbs->log_codepage))
Sql_ShowDebug(loginlog->sql_handle);
loginlog->enabled = true;
return true;
}
bool loginlog_final(void)
{
SQL->Free(loginlog->sql_handle);
loginlog->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", loginlog->dbs->log_login_db, sizeof(loginlog->dbs->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", 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", &loginlog->dbs->log_db_port);
libconfig->setting_lookup_mutable_string(setting, "codepage", loginlog->dbs->log_codepage, sizeof(loginlog->dbs->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;
}
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;
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;
}
|