]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/extra/m_sqlauth.cpp
Pedantic clean
[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 /* $CompileFlags: -Wno-variadic-macros */
24
25 class ModuleSQLAuth : public Module
26 {
27         Module* SQLutils;
28         Module* SQLprovider;
29
30         std::string usertable;
31         std::string userfield;
32         std::string passfield;
33         std::string encryption;
34         std::string killreason;
35         std::string allowpattern;
36         std::string databaseid;
37         
38         bool verbose;
39         
40 public:
41         ModuleSQLAuth(InspIRCd* Me)
42         : Module::Module(Me)
43         {
44                 ServerInstance->Modules->UseInterface("SQLutils");
45                 ServerInstance->Modules->UseInterface("SQL");
46
47                 SQLutils = ServerInstance->Modules->Find("m_sqlutils.so");
48                 if (!SQLutils)
49                         throw ModuleException("Can't find m_sqlutils.so. Please load m_sqlutils.so before m_sqlauth.so.");
50
51                 SQLprovider = ServerInstance->Modules->FindFeature("SQL");
52                 if (!SQLprovider)
53                         throw ModuleException("Can't find an SQL provider module. Please load one before attempting to load m_sqlauth.");
54
55                 OnRehash(NULL,"");
56         }
57
58         virtual ~ModuleSQLAuth()
59         {
60                 ServerInstance->Modules->DoneWithInterface("SQL");
61                 ServerInstance->Modules->DoneWithInterface("SQLutils");
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(User* user, const std::string &parameter)
70         {
71                 ConfigReader Conf(ServerInstance);
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(User* user)
91         {
92                 if ((!allowpattern.empty()) && (ServerInstance->MatchText(user->nick,allowpattern)))
93                 {
94                         user->Extend("sqlauthed");
95                         return 0;
96                 }
97                 
98                 if (!CheckCredentials(user))
99                 {
100                         User::QuitUser(ServerInstance,user,killreason);
101                         return 1;
102                 }
103                 return 0;
104         }
105
106         bool CheckCredentials(User* user)
107         {
108                 SQLrequest req = SQLreq(this, SQLprovider, databaseid, "SELECT ? FROM ? WHERE ? = '?' AND ? = ?'?')", userfield, usertable, userfield, user->nick, passfield, encryption, user->password);
109                         
110                 if(req.Send())
111                 {
112                         /* When we get the query response from the service provider we will be given an ID to play with,
113                          * just an ID number which is unique to this query. We need a way of associating that ID with a User
114                          * so we insert it into a map mapping the IDs to users.
115                          * Thankfully m_sqlutils provides this, it will associate a ID with a user or channel, and if the user quits it removes the
116                          * association. This means that if the user quits during a query we will just get a failed lookup from m_sqlutils - telling
117                          * us to discard the query.
118                          */
119                         AssociateUser(this, SQLutils, req.id, user).Send();
120                                 
121                         return true;
122                 }
123                 else
124                 {
125                         if (verbose)
126                                 ServerInstance->WriteOpers("Forbidden connection from %s!%s@%s (SQL query failed: %s)", user->nick, user->ident, user->host, req.error.Str());
127                         return false;
128                 }
129         }
130         
131         virtual char* OnRequest(Request* request)
132         {
133                 if(strcmp(SQLRESID, request->GetId()) == 0)
134                 {
135                         SQLresult* res = static_cast<SQLresult*>(request);
136
137                         User* user = GetAssocUser(this, SQLutils, res->id).S().user;
138                         UnAssociate(this, SQLutils, res->id).S();
139                         
140                         if(user)
141                         {
142                                 if(res->error.Id() == NO_ERROR)
143                                 {
144                                         if(res->Rows())
145                                         {
146                                                 /* We got a row in the result, this is enough really */
147                                                 user->Extend("sqlauthed");
148                                         }
149                                         else if (verbose)
150                                         {
151                                                 /* No rows in result, this means there was no record matching the user */
152                                                 ServerInstance->WriteOpers("Forbidden connection from %s!%s@%s (SQL query returned no matches)", user->nick, user->ident, user->host);
153                                                 user->Extend("sqlauth_failed");
154                                         }
155                                 }
156                                 else if (verbose)
157                                 {
158                                         ServerInstance->WriteOpers("Forbidden connection from %s!%s@%s (SQL query failed: %s)", user->nick, user->ident, user->host, res->error.Str());
159                                         user->Extend("sqlauth_failed");
160                                 }
161                         }
162                         else
163                         {
164                                 return NULL;
165                         }
166
167                         if (!user->GetExt("sqlauthed"))
168                         {
169                                 User::QuitUser(ServerInstance,user,killreason);
170                         }
171                         return SQLSUCCESS;
172                 }               
173                 return NULL;
174         }
175         
176         virtual void OnUserDisconnect(User* user)
177         {
178                 user->Shrink("sqlauthed");
179                 user->Shrink("sqlauth_failed");         
180         }
181         
182         virtual bool OnCheckReady(User* user)
183         {
184                 return user->GetExt("sqlauthed");
185         }
186
187         virtual Version GetVersion()
188         {
189                 return Version(1,1,1,0,VF_VENDOR,API_VERSION);
190         }
191         
192 };
193
194 MODULE_INIT(ModuleSQLAuth)