]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/extra/m_sqlauth.cpp
I yell 'LIES' in the face of anyone who says I don't commit
[user/henk/code/inspircd.git] / src / modules / extra / m_sqlauth.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd is copyright (C) 2002-2004 ChatSpike-Dev.
6  *                       E-mail:
7  *                <brain@chatspike.net>
8  *                <Craig@chatspike.net>
9  *               <omster@gmail.com>
10  *     
11  * Written by Craig Edwards, Craig McLure, and others.
12  * This program is free but copyrighted software; see
13  *            the file COPYING for details.
14  *
15  * ---------------------------------------------------
16  */
17
18 #include <string>
19
20 #include "users.h"
21 #include "channels.h"
22 #include "modules.h"
23 #include "inspircd.h"
24
25 #include "m_sqlv2.h"
26 #include "m_sqlutils.h"
27
28 /* $ModDesc: Allow/Deny connections based upon an arbitary SQL table */
29 /* $ModDep: m_sqlv2.h m_sqlutils.h */
30
31 class ModuleSQLAuth : public Module
32 {
33         InspIRCd* Srv;
34         Module* SQLutils;
35
36         std::string usertable;
37         std::string userfield;
38         std::string passfield;
39         std::string encryption;
40         std::string killreason;
41         std::string allowpattern;
42         std::string databaseid;
43         
44         bool verbose;
45         
46 public:
47         ModuleSQLAuth(InspIRCd* Me)
48         : Module::Module(Me), Srv(Me)
49         {
50                 SQLutils = Srv->FindFeature("SQLutils");
51                 
52                 if(SQLutils)
53                 {
54                         ServerInstance->Log(DEBUG, "Successfully got SQLutils pointer");
55                 }
56                 else
57                 {
58                         ServerInstance->Log(DEFAULT, "ERROR: This module requires a module offering the 'SQLutils' feature (usually m_sqlutils.so). Please load it and try again.");
59                         throw ModuleException("This module requires a module offering the 'SQLutils' feature (usually m_sqlutils.so). Please load it and try again.");
60                 }
61                                 
62                 OnRehash("");
63         }
64
65         void Implements(char* List)
66         {
67                 List[I_OnUserDisconnect] = List[I_OnCheckReady] = List[I_OnRequest] = List[I_OnRehash] = List[I_OnUserRegister] = 1;
68         }
69
70         virtual void OnRehash(const std::string &parameter)
71         {
72                 ConfigReader Conf(Srv);
73                 
74                 usertable       = Conf.ReadValue("sqlauth", "usertable", 0);    /* User table name */
75                 databaseid      = Conf.ReadValue("sqlauth", "dbid", 0);                 /* Database ID, given to the SQL service provider */
76                 userfield       = Conf.ReadValue("sqlauth", "userfield", 0);    /* Field name where username can be found */
77                 passfield       = Conf.ReadValue("sqlauth", "passfield", 0);    /* Field name where password can be found */
78                 killreason      = Conf.ReadValue("sqlauth", "killreason", 0);   /* Reason to give when access is denied to a user (put your reg details here) */
79                 allowpattern= Conf.ReadValue("sqlauth", "allowpattern",0 );     /* Allow nicks matching this pattern without requiring auth */
80                 encryption      = Conf.ReadValue("sqlauth", "encryption", 0);   /* Name of sql function used to encrypt password, e.g. "md5" or "passwd".
81                                                                                                                                          * define, but leave blank if no encryption is to be used.
82                                                                                                                                          */
83                 verbose         = Conf.ReadFlag("sqlauth", "verbose", 0);               /* Set to true if failed connects should be reported to operators */
84                 
85                 if (encryption.find("(") == std::string::npos)
86                 {
87                         encryption.append("(");
88                 }
89         }       
90
91         virtual int OnUserRegister(userrec* user)
92         {
93                 if ((allowpattern != "") && (Srv->MatchText(user->nick,allowpattern)))
94                 {
95                         user->Extend("sqlauthed");
96                         return 0;
97                 }
98                 
99                 if (!CheckCredentials(user))
100                 {
101                         userrec::QuitUser(Srv,user,killreason);
102                         return 1;
103                 }
104                 return 0;
105         }
106
107         bool CheckCredentials(userrec* user)
108         {
109                 Module* target;
110                 
111                 target = Srv->FindFeature("SQL");
112                 
113                 if(target)
114                 {
115                         SQLrequest req = SQLreq(this, target, databaseid, "SELECT ? FROM ? WHERE ? = '?' AND ? = ?'?')", userfield, usertable, userfield, user->nick, passfield, encryption, user->password);
116                         
117                         if(req.Send())
118                         {
119                                 /* When we get the query response from the service provider we will be given an ID to play with,
120                                  * just an ID number which is unique to this query. We need a way of associating that ID with a userrec
121                                  * so we insert it into a map mapping the IDs to users.
122                                  * Thankfully m_sqlutils provides this, it will associate a ID with a user or channel, and if the user quits it removes the
123                                  * association. This means that if the user quits during a query we will just get a failed lookup from m_sqlutils - telling
124                                  * us to discard the query.
125                                  */
126                                 ServerInstance->Log(DEBUG, "Sent query, got given ID %lu", req.id);
127                                 
128                                 AssociateUser(this, SQLutils, req.id, user).Send();
129                                         
130                                 return true;
131                         }
132                         else
133                         {
134                                 ServerInstance->Log(DEBUG, "SQLrequest failed: %s", req.error.Str());
135                         
136                                 if (verbose)
137                                         Srv->WriteOpers("Forbidden connection from %s!%s@%s (SQL query failed: %s)", user->nick, user->ident, user->host, req.error.Str());
138                         
139                                 return false;
140                         }
141                 }
142                 else
143                 {
144                         ServerInstance->Log(SPARSE, "WARNING: Couldn't find SQL provider module. NOBODY will be allowed to connect until it comes back unless they match an exception");
145                         return false;
146                 }
147         }
148         
149         virtual char* OnRequest(Request* request)
150         {
151                 if(strcmp(SQLRESID, request->GetId()) == 0)
152                 {
153                         SQLresult* res;
154                 
155                         res = static_cast<SQLresult*>(request);
156                         
157                         ServerInstance->Log(DEBUG, "Got SQL result (%s) with ID %lu", res->GetId(), res->id);
158                         
159                         userrec* user = GetAssocUser(this, SQLutils, res->id).S().user;
160                         UnAssociate(this, SQLutils, res->id).S();
161                         
162                         if(user)
163                         {
164                                 if(res->error.Id() == NO_ERROR)
165                                 {                               
166                                         ServerInstance->Log(DEBUG, "Associated query ID %lu with user %s", res->id, user->nick);                        
167                                         ServerInstance->Log(DEBUG, "Got result with %d rows and %d columns", res->Rows(), res->Cols());
168                         
169                                         if(res->Rows())
170                                         {
171                                                 /* We got a row in the result, this is enough really */
172                                                 user->Extend("sqlauthed");
173                                         }
174                                         else if (verbose)
175                                         {
176                                                 /* No rows in result, this means there was no record matching the user */
177                                                 Srv->WriteOpers("Forbidden connection from %s!%s@%s (SQL query returned no matches)", user->nick, user->ident, user->host);
178                                                 user->Extend("sqlauth_failed");
179                                         }
180                                 }
181                                 else if (verbose)
182                                 {
183                                         ServerInstance->Log(DEBUG, "Query failed: %s", res->error.Str());
184                                         Srv->WriteOpers("Forbidden connection from %s!%s@%s (SQL query failed: %s)", user->nick, user->ident, user->host, res->error.Str());
185                                         user->Extend("sqlauth_failed");
186                                 }
187                         }
188                         else
189                         {
190                                 ServerInstance->Log(DEBUG, "Got query with unknown ID, this probably means the user quit while the query was in progress");
191                                 return NULL;
192                         }
193
194                         if (!user->GetExt("sqlauthed"))
195                         {
196                                 userrec::QuitUser(Srv,user,killreason);
197                         }
198                         return SQLSUCCESS;
199                 }
200                 
201                 ServerInstance->Log(DEBUG, "Got unsupported API version string: %s", request->GetId());
202                 
203                 return NULL;
204         }
205         
206         virtual void OnUserDisconnect(userrec* user)
207         {
208                 user->Shrink("sqlauthed");
209                 user->Shrink("sqlauth_failed");         
210         }
211         
212         virtual bool OnCheckReady(userrec* user)
213         {
214                 return user->GetExt("sqlauthed");
215         }
216
217         virtual ~ModuleSQLAuth()
218         {
219         }
220         
221         virtual Version GetVersion()
222         {
223                 return Version(1,1,1,0,VF_VENDOR,API_VERSION);
224         }
225         
226 };
227
228 class ModuleSQLAuthFactory : public ModuleFactory
229 {
230  public:
231         ModuleSQLAuthFactory()
232         {
233         }
234         
235         ~ModuleSQLAuthFactory()
236         {
237         }
238         
239         virtual Module * CreateModule(InspIRCd* Me)
240         {
241                 return new ModuleSQLAuth(Me);
242         }
243         
244 };
245
246
247 extern "C" void * init_module( void )
248 {
249         return new ModuleSQLAuthFactory;
250 }