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
|
/* +------------------------------------+
* | Inspire Internet Relay Chat Daemon |
* +------------------------------------+
*
* Inspire 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 "helperfuncs.h"
#include "m_sql.h"
/* $ModDesc: Allow/Deny connections based upon an arbitary SQL table */
Server *Srv;
class ModuleSQLAuth : public Module
{
ConfigReader* Conf;
std::string usertable;
std::string userfield;
std::string passfield;
std::string encryption;
std::string killreason;
std::string allowpattern;
bool WallOperFail;
unsigned long dbid;
Module* SQLModule;
public:
bool ReadConfig()
{
Conf = new ConfigReader();
usertable = Conf->ReadValue("sqlauth","usertable",0); // user table name
dbid = Conf->ReadInteger("sqlauth","dbid",0,true); // database id of a database configured in m_sql (see m_sql config)
userfield = Conf->ReadValue("sqlauth","userfield",0); // field name where username can be found
passfield = Conf->ReadValue("sqlauth","passfield",0); // field name where password can be found
killreason = Conf->ReadValue("sqlauth","killreason",0); // reason to give when access is denied to a user (put your reg details here)
encryption = Conf->ReadValue("sqlauth","encryption",0); // name of sql function used to encrypt password, e.g. "md5" or "passwd".
// define, but leave blank if no encryption is to be used.
WallOperFail = Conf->ReadFlag("sqlauth","verbose",0); // set to 1 if failed connects should be reported to operators
allowpattern = Conf->ReadValue("sqlauth","allowpattern",0); // allow nicks matching the pattern without requiring auth
delete Conf;
SQLModule = Srv->FindModule("m_sql.so");
if (!SQLModule)
Srv->Log(DEFAULT,"WARNING: m_sqlauth.so could not initialize because m_sql.so is not loaded. Load the module and rehash your server.");
return (SQLModule);
}
ModuleSQLAuth()
{
Srv = new Server;
ReadConfig();
}
virtual void OnRehash()
{
ReadConfig();
}
virtual void OnUserRegister(userrec* user)
{
if ((allowpattern != "") && (Srv->MatchText(user->nick,allowpattern)))
return;
if (!CheckCredentials(user->nick,user->password))
{
if (WallOperFail)
WriteOpers("Forbidden connection from %s!%s@%s (invalid login/password)",user->nick,user->ident,user->host);
Srv->QuitUser(user,killreason);
}
}
bool CheckCredentials(std::string username, std::string password)
{
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 temp = "";
for (int q = 0; q < password.length(); q++)
{
if (password[q] == '\'')
{
temp = temp + "\'";
}
else if (password[q] == '"')
{
temp = temp + "\\\"";
}
else temp = temp + password[q];
}
password = temp;
// Create a request containing the SQL query and send it to m_sql.so
SQLRequest* query = new SQLRequest(SQL_RESULT,dbid,"SELECT * FROM "+usertable+" WHERE "+userfield+"='"+username+"' AND "+passfield+"="+encryption+"('"+password+"')");
Request queryrequest((char*)query, this, SQLModule);
SQLResult* result = (SQLResult*)queryrequest.Send();
// Did we get "OK" as a result?
if (result->GetType() == SQL_OK)
{
// 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(userfield) == username)
{
// because the query directly asked for the password hash, we do not need to check it -
// if it didnt match it wont be returned in the first place from the SELECT.
// This just checks we didnt get an empty row by accident.
found = true;
}
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 ~ModuleSQLAuth()
{
delete Srv;
}
virtual Version GetVersion()
{
return Version(1,0,0,1,VF_VENDOR);
}
};
class ModuleSQLAuthFactory : public ModuleFactory
{
public:
ModuleSQLAuthFactory()
{
}
~ModuleSQLAuthFactory()
{
}
virtual Module * CreateModule()
{
return new ModuleSQLAuth;
}
};
extern "C" void * init_module( void )
{
return new ModuleSQLAuthFactory;
}
|