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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
|
/* +------------------------------------+
* | Inspire Internet Relay Chat Daemon |
* +------------------------------------+
*
* InspIRCd is copyright (C) 2002-2004 ChatSpike-Dev.
* E-mail:
* <brain@chatspike.net>
* <Craig@chatspike.net>
*
* Written by Craig Edwards, Craig McLure, and others.
* This program is free but copyrighted software; see
* the file COPYING for details.
*
* ---------------------------------------------------
*/
using namespace std;
#include <stdio.h>
#include <string>
#include <stdlib.h>
#include <time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <poll.h>
#include "users.h"
#include "channels.h"
#include "modules.h"
#include "inspircd.h"
#include "m_sql.h"
enum LogTypes { LT_OPER = 1, LT_KILL, LT_SERVLINK, LT_XLINE, LT_CONNECT, LT_DISCONNECT, LT_FLOOD, LT_LOADMODULE };
/* $ModDesc: Logs network-wide data to an SQL database */
class ModuleSQLLog : public Module
{
Server* Srv;
ConfigReader* Conf;
unsigned long dbid;
Module* SQLModule;
public:
bool ReadConfig()
{
Conf = new ConfigReader();
dbid = Conf->ReadInteger("sqllog","dbid",0,true); // database id of a database configured in m_sql (see m_sql config)
delete Conf;
SQLModule = Srv->FindModule("m_sql.so");
if (!SQLModule)
Srv->Log(DEFAULT,"WARNING: m_SQLLog.so could not initialize because m_sql.so is not loaded. Load the module and rehash your server.");
return (SQLModule);
}
ModuleSQLLog(Server* Me)
: Module::Module(Me)
{
Srv = Me;
ReadConfig();
}
void Implements(char* List)
{
List[I_OnRehash] = List[I_OnOper] = List[I_OnGlobalOper] = List[I_OnKill] = 1;
List[I_OnPreCommand] = List[I_OnUserConnect] = List[I_OnGlobalConnect] = 1;
List[I_OnUserQuit] = List[I_OnLoadModule] = 1;
}
virtual void OnRehash(std::string parameter)
{
ReadConfig();
}
long InsertNick(std::string nick)
{
long nid = -1;
SQLRequest* query = new SQLRequest(SQL_RESULT,dbid,"SELECT id,actor FROM ircd_log_actors WHERE actor='"+nick+"'");
Request queryrequest((char*)query, this, SQLModule);
SQLResult* result = (SQLResult*)queryrequest.Send();
if (result->GetType() == SQL_OK)
{
SQLRequest* rowrequest = new SQLRequest(SQL_ROW,dbid,"");
Request rowquery((char*)rowrequest, this, SQLModule);
SQLResult* rowresult = (SQLResult*)rowquery.Send();
if (rowresult->GetType() == SQL_ROW)
{
nid = atoi(rowresult->GetField("id").c_str());
delete rowresult;
}
delete rowrequest;
delete result;
}
query->SetQueryType(SQL_DONE);
query->SetConnID(dbid);
Request donerequest((char*)query, this, SQLModule);
donerequest.Send();
delete query;
if (nid < 1)
{
SQLRequest* query2 = new SQLRequest(SQL_COUNT,dbid,"INSERT INTO ircd_log_actors VALUES('','"+nick+"')");
Request queryrequest2((char*)query2, this, SQLModule);
SQLResult* result2 = (SQLResult*)queryrequest2.Send();
if (result2->GetType() == SQL_ERROR)
{
Srv->Log(DEFAULT,"SQL log error: " + result2->GetError());
}
if (result2)
delete result;
if (query2)
delete query2;
nid = InsertNick(nick);
}
return nid;
}
void InsertEntry(unsigned long category,unsigned long nickid,unsigned long hostid,unsigned long sourceid,unsigned long date)
{
char querybuffer[MAXBUF];
snprintf(querybuffer,MAXBUF,"INSERT INTO ircd_log VALUES('',%lu,%lu,%lu,%lu,%lu)",(unsigned long)category,(unsigned long)nickid,(unsigned long)hostid,(unsigned long)sourceid,(unsigned long)date);
SQLRequest* query = new SQLRequest(SQL_COUNT,dbid,querybuffer);
Request queryrequest((char*)query, this, SQLModule);
SQLResult* result = (SQLResult*)queryrequest.Send();
if (result->GetType() == SQL_ERROR)
{
Srv->Log(DEFAULT,"SQL log error: " + result->GetError());
}
if (result)
delete result;
if (query)
delete query;
return;
}
long InsertHost(std::string host)
{
long hid = -1;
SQLRequest* query = new SQLRequest(SQL_RESULT,dbid,"SELECT id,hostname FROM ircd_log_hosts WHERE hostname='"+host+"'");
Request queryrequest((char*)query, this, SQLModule);
SQLResult* result = (SQLResult*)queryrequest.Send();
if (result->GetType() == SQL_OK)
{
SQLRequest* rowrequest = new SQLRequest(SQL_ROW,dbid,"");
Request rowquery((char*)rowrequest, this, SQLModule);
SQLResult* rowresult = (SQLResult*)rowquery.Send();
if (rowresult->GetType() == SQL_ROW)
{
hid = atoi(rowresult->GetField("id").c_str());
delete rowresult;
}
delete rowrequest;
delete result;
}
query->SetQueryType(SQL_DONE);
query->SetConnID(dbid);
Request donerequest((char*)query, this, SQLModule);
donerequest.Send();
delete query;
if (hid < 1)
{
SQLRequest* query2 = new SQLRequest(SQL_COUNT,dbid,"INSERT INTO ircd_log_hosts VALUES('','"+host+"')");
Request queryrequest2((char*)query2, this, SQLModule);
SQLResult* result2 = (SQLResult*)queryrequest2.Send();
if (result2->GetType() == SQL_ERROR)
{
Srv->Log(DEFAULT,"SQL log error: " + result2->GetError());
}
if (result)
delete result2;
if (query)
delete query2;
hid = InsertHost(host);
}
return hid;
}
void AddLogEntry(int category, std::string nick, std::string host, std::string source)
{
// is the sql module loaded? If not, we don't attempt to do anything.
if (!SQLModule)
return;
long nickid = InsertNick(nick);
long sourceid = InsertNick(source);
long hostid = InsertHost(host);
InsertEntry((unsigned)category,(unsigned)nickid,(unsigned)hostid,(unsigned)sourceid,(unsigned long)time(NULL));
}
virtual void OnOper(userrec* user, std::string opertype)
{
AddLogEntry(LT_OPER,user->nick,user->host,user->server);
}
virtual void OnGlobalOper(userrec* user)
{
AddLogEntry(LT_OPER,user->nick,user->host,user->server);
}
virtual int OnKill(userrec* source, userrec* dest, std::string reason)
{
AddLogEntry(LT_KILL,dest->nick,dest->host,source->nick);
return 0;
}
virtual int OnPreCommand(std::string command, char **parameters, int pcnt, userrec *user)
{
if ((command == "GLINE") || (command == "KLINE") || (command == "ELINE") || (command == "ZLINE"))
{
AddLogEntry(LT_XLINE,user->nick,command[0]+std::string(":")+std::string(parameters[0]),user->server);
}
return 0;
}
virtual void OnUserConnect(userrec* user)
{
AddLogEntry(LT_CONNECT,user->nick,user->host,user->server);
}
virtual void OnGlobalConnect(userrec* user)
{
AddLogEntry(LT_CONNECT,user->nick,user->host,user->server);
}
virtual void OnUserQuit(userrec* user, std::string reason)
{
AddLogEntry(LT_DISCONNECT,user->nick,user->host,user->server);
}
virtual void OnLoadModule(Module* mod,std::string name)
{
AddLogEntry(LT_LOADMODULE,name,Srv->GetServerName(),Srv->GetServerName());
}
virtual ~ModuleSQLLog()
{
}
virtual Version GetVersion()
{
return Version(1,0,0,1,VF_VENDOR);
}
};
class ModuleSQLLogFactory : public ModuleFactory
{
public:
ModuleSQLLogFactory()
{
}
~ModuleSQLLogFactory()
{
}
virtual Module * CreateModule(Server* Me)
{
return new ModuleSQLLog(Me);
}
};
extern "C" void * init_module( void )
{
return new ModuleSQLLogFactory;
}
|