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
|
/* +------------------------------------+
* | 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 "configreader.h"
#include "helperfuncs.h"
#include "m_sql.h"
#include "commands/cmd_oper.h"
/* $ModDesc: Allows storage of oper credentials in an SQL table */
/* Required for the FOREACH_MOD alias (OnOper event) */
extern int MODCOUNT;
extern ServerConfig* Config;
extern std::vector<Module*> modules;
extern std::vector<ircd_module*> factory;
class ModuleSQLOper : public Module
{
Server* Srv;
ConfigReader* Conf;
unsigned long dbid;
Module* SQLModule;
public:
bool ReadConfig()
{
dbid = Conf->ReadInteger("sqloper","dbid",0,true); // database id of a database configured in m_sql (see m_sql config)
SQLModule = Srv->FindModule("m_sql.so");
if (!SQLModule)
Srv->Log(DEFAULT,"WARNING: m_sqloper.so could not initialize because m_sql.so is not loaded. Load the module and rehash your server.");
return (SQLModule);
}
ModuleSQLOper(Server* Me)
: Module::Module(Me)
{
Srv = Me;
Conf = new ConfigReader();
ReadConfig();
}
virtual void OnRehash(const std::string ¶meter)
{
DELETE(Conf);
Conf = new ConfigReader();
ReadConfig();
}
void Implements(char* List)
{
List[I_OnRehash] = List[I_OnPreCommand] = 1;
}
virtual int OnPreCommand(const std::string &command, char **parameters, int pcnt, userrec *user, bool validated)
{
if ((command == "OPER") && (validated))
{
if (LookupOper(parameters[0],parameters[1],user))
return 1;
}
return 0;
}
bool LookupOper(const std::string &s_username, const std::string &s_password, userrec* user)
{
bool found = false;
// is the sql module loaded? If not, we don't attempt to do anything.
if (!SQLModule)
return false;
// sanitize the password (we dont want any mysql insertion exploits!)
std::string username = SQLQuery::Sanitise(s_username);
std::string password = SQLQuery::Sanitise(s_password);
// Create a request containing the SQL query and send it to m_sql.so
SQLRequest* query = new SQLRequest(SQL_RESULT,dbid,"SELECT username,password,hostname,type FROM ircd_opers WHERE username='"+username+"' AND password=md5('"+password+"')");
Request queryrequest((char*)query, this, SQLModule);
SQLResult* result = (SQLResult*)queryrequest.Send();
// Did we get "OK" as a result?
if (result->GetType() == SQL_OK)
{
Srv->Log(DEBUG,"An SQL based oper exists");
// if we did, this means we may now request a row... there should be only one row for each user, so,
// we don't need to loop to fetch multiple rows.
SQLRequest* rowrequest = new SQLRequest(SQL_ROW,dbid,"");
Request rowquery((char*)rowrequest, this, SQLModule);
SQLResult* rowresult = (SQLResult*)rowquery.Send();
// did we get a row? If we did, we can now do something with the fields
if (rowresult->GetType() == SQL_ROW)
{
if (rowresult->GetField("username") == username)
{
found = true;
// oper up the user.
for (int j =0; j < Conf->Enumerate("type"); j++)
{
std::string TypeName = Conf->ReadValue("type","name",j);
Srv->Log(DEBUG,"Scanning opertype: "+TypeName);
std::string pattern = std::string(user->ident) + "@" + std::string(user->host);
if((TypeName == rowresult->GetField("type")) && OneOfMatches(pattern.c_str(), rowresult->GetField("hostname").c_str()))
{
/* found this oper's opertype */
Srv->Log(DEBUG,"Host and type match: "+TypeName+" "+rowresult->GetField("type"));
std::string HostName = Conf->ReadValue("type","host",j);
if(HostName != "")
Srv->ChangeHost(user,HostName);
strlcpy(user->oper,rowresult->GetField("type").c_str(),NICKMAX-1);
WriteOpers("*** %s (%s@%s) is now an IRC operator of type %s",user->nick,user->ident,user->host,rowresult->GetField("type").c_str());
WriteServ(user->fd,"381 %s :You are now an IRC operator of type %s",user->nick,rowresult->GetField("type").c_str());
if(user->modes[UM_OPERATOR])
{
user->modes[UM_OPERATOR] = 1;
WriteServ(user->fd,"MODE %s :+o",user->nick);
FOREACH_MOD(I_OnOper,OnOper(user,rowresult->GetField("type")));
AddOper(user);
FOREACH_MOD(I_OnPostOper,OnPostOper(user,rowresult->GetField("type")));
log(DEFAULT,"OPER: %s!%s@%s opered as type: %s",user->nick,user->ident,user->host,rowresult->GetField("type").c_str());
}
break;
}
}
}
DELETE(rowresult);
}
else
{
// we didn't have a row.
found = false;
}
DELETE(rowrequest);
DELETE(result);
}
else
{
// the query was bad
found = false;
}
query->SetQueryType(SQL_DONE);
query->SetConnID(dbid);
Request donerequest((char*)query, this, SQLModule);
donerequest.Send();
DELETE(query);
return found;
}
virtual ~ModuleSQLOper()
{
DELETE(Conf);
}
virtual Version GetVersion()
{
return Version(1,0,0,1,VF_VENDOR);
}
};
class ModuleSQLOperFactory : public ModuleFactory
{
public:
ModuleSQLOperFactory()
{
}
~ModuleSQLOperFactory()
{
}
virtual Module * CreateModule(Server* Me)
{
return new ModuleSQLOper(Me);
}
};
extern "C" void * init_module( void )
{
return new ModuleSQLOperFactory;
}
|