]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/extra/m_sqlauth.cpp
Dont echo the JOIN to the user whos speaking
[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(User* 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(User* 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                         User::QuitUser(ServerInstance,user,killreason);
100                         return 1;
101                 }
102                 return 0;
103         }
104
105         bool CheckCredentials(User* user)
106         {
107                 SQLrequest req = SQLrequest(this, SQLprovider, databaseid, SQLquery("SELECT ? FROM ? WHERE ? = '?' AND ? = ?'?')") % userfield % usertable % userfield % user->nick %
108                                 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)