]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/extra/m_sqlauth.cpp
Review and optimize
[user/henk/code/inspircd.git] / src / modules / extra / m_sqlauth.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  Inspire is copyright (C) 2002-2004 ChatSpike-Dev.
6  *                       E-mail:
7  *                <brain@chatspike.net>
8  *                <Craig@chatspike.net>
9  *     
10  * Written by Craig Edwards, Craig McLure, and others.
11  * This program is free but copyrighted software; see
12  *            the file COPYING for details.
13  *
14  * ---------------------------------------------------
15  */
16
17 using namespace std;
18
19 #include <stdio.h>
20 #include <string>
21 #include <stdlib.h>
22 #include <time.h>
23 #include <sys/types.h>
24 #include <sys/socket.h>
25 #include <sys/time.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <errno.h>
29 #include <fcntl.h>
30 #include <poll.h>
31 #include "users.h"
32 #include "channels.h"
33 #include "modules.h"
34 #include "inspircd.h"
35 #include "helperfuncs.h"
36 #include "m_sql.h"
37
38 /* $ModDesc: Allow/Deny connections based upon an arbitary SQL table */
39
40 Server *Srv;
41
42 class ModuleSQLAuth : public Module
43 {
44         ConfigReader* Conf;
45         std::string usertable;
46         std::string userfield;
47         std::string passfield;
48         std::string encryption;
49         std::string killreason;
50         std::string allowpattern;
51         bool WallOperFail;
52         unsigned long dbid;
53         Module* SQLModule;
54
55  public:
56         bool ReadConfig()
57         {
58                 Conf = new ConfigReader();
59                 usertable = Conf->ReadValue("sqlauth","usertable",0);   // user table name
60                 dbid = Conf->ReadInteger("sqlauth","dbid",0,true);      // database id of a database configured in m_sql (see m_sql config)
61                 userfield = Conf->ReadValue("sqlauth","userfield",0);   // field name where username can be found
62                 passfield = Conf->ReadValue("sqlauth","passfield",0);   // field name where password can be found
63                 killreason = Conf->ReadValue("sqlauth","killreason",0); // reason to give when access is denied to a user (put your reg details here)
64                 encryption = Conf->ReadValue("sqlauth","encryption",0); // name of sql function used to encrypt password, e.g. "md5" or "passwd".
65                                                                         // define, but leave blank if no encryption is to be used.
66                 WallOperFail = Conf->ReadFlag("sqlauth","verbose",0);   // set to 1 if failed connects should be reported to operators
67                 allowpattern = Conf->ReadValue("sqlauth","allowpattern",0);     // allow nicks matching the pattern without requiring auth
68                 delete Conf;
69                 SQLModule = Srv->FindModule("m_sql.so");
70                 if (!SQLModule)
71                         Srv->Log(DEFAULT,"WARNING: m_sqlauth.so could not initialize because m_sql.so is not loaded. Load the module and rehash your server.");
72                 return (SQLModule);
73         }
74
75         ModuleSQLAuth(Server* Me)
76                 : Module::Module(Me)
77         {
78                 Srv = Me;
79                 ReadConfig();
80         }
81
82         virtual void OnRehash(std::string parameter)
83         {
84                 ReadConfig();
85         }
86
87         virtual void OnUserRegister(userrec* user)
88         {
89                 if ((allowpattern != "") && (Srv->MatchText(user->nick,allowpattern)))
90                         return;
91                 
92                 if (!CheckCredentials(user->nick,user->password))
93                 {
94                         if (WallOperFail)
95                                 WriteOpers("Forbidden connection from %s!%s@%s (invalid login/password)",user->nick,user->ident,user->host);
96                         Srv->QuitUser(user,killreason);
97                 }
98         }
99
100         bool CheckCredentials(std::string username, std::string password)
101         {
102                 bool found = false;
103
104                 // is the sql module loaded? If not, we don't attempt to do anything.
105                 if (!SQLModule)
106                         return false;
107
108                 // sanitize the password (we dont want any mysql insertion exploits!)
109                 std::string temp = "";
110                 for (unsigned int q = 0; q < password.length(); q++)
111                 {
112                         if (password[q] == '\'')
113                         {
114                                 temp = temp + "\'";
115                         }
116                         else if (password[q] == '"')
117                         {
118                                 temp = temp + "\\\"";
119                         }
120                         else temp = temp + password[q];
121                 }
122                 password = temp;
123
124                 // Create a request containing the SQL query and send it to m_sql.so
125                 SQLRequest* query = new SQLRequest(SQL_RESULT,dbid,"SELECT * FROM "+usertable+" WHERE "+userfield+"='"+username+"' AND "+passfield+"="+encryption+"('"+password+"')");
126                 Request queryrequest((char*)query, this, SQLModule);
127                 SQLResult* result = (SQLResult*)queryrequest.Send();
128
129                 // Did we get "OK" as a result?
130                 if (result->GetType() == SQL_OK)
131                 {
132
133                         // if we did, this means we may now request a row... there should be only one row for each user, so,
134                         // we don't need to loop to fetch multiple rows.
135                         SQLRequest* rowrequest = new SQLRequest(SQL_ROW,dbid,"");
136                         Request rowquery((char*)rowrequest, this, SQLModule);
137                         SQLResult* rowresult = (SQLResult*)rowquery.Send();
138
139                         // did we get a row? If we did, we can now do something with the fields
140                         if (rowresult->GetType() == SQL_ROW)
141                         {
142                                 if (rowresult->GetField(userfield) == username)
143                                 {
144                                         // because the query directly asked for the password hash, we do not need to check it -
145                                         // if it didnt match it wont be returned in the first place from the SELECT.
146                                         // This just checks we didnt get an empty row by accident.
147                                         found = true;
148                                 }
149                                 delete rowresult;
150                         }
151                         else
152                         {
153                                 // we didn't have a row.
154                                 found = false;
155                         }
156                         delete rowrequest;
157                         delete result;
158                 }
159                 else
160                 {
161                         // the query was bad
162                         found = false;
163                 }
164                 query->SetQueryType(SQL_DONE);
165                 query->SetConnID(dbid);
166                 Request donerequest((char*)query, this, SQLModule);
167                 donerequest.Send();
168                 delete query;
169                 return found;
170         }
171
172         virtual ~ModuleSQLAuth()
173         {
174         }
175         
176         virtual Version GetVersion()
177         {
178                 return Version(1,0,0,1,VF_VENDOR);
179         }
180         
181 };
182
183 class ModuleSQLAuthFactory : public ModuleFactory
184 {
185  public:
186         ModuleSQLAuthFactory()
187         {
188         }
189         
190         ~ModuleSQLAuthFactory()
191         {
192         }
193         
194         virtual Module * CreateModule(Server* Me)
195         {
196                 return new ModuleSQLAuth(Me);
197         }
198         
199 };
200
201
202 extern "C" void * init_module( void )
203 {
204         return new ModuleSQLAuthFactory;
205 }
206