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