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