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