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