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