]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/extra/m_sqlauth.cpp
Remove InspIRCd* parameters and fields
[user/henk/code/inspircd.git] / src / modules / extra / m_sqlauth.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/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 "m_sqlv2.h"
16 #include "m_sqlutils.h"
17 #include "m_hash.h"
18
19 /* $ModDesc: Allow/Deny connections based upon an arbitary SQL table */
20 /* $ModDep: m_sqlv2.h m_sqlutils.h m_hash.h */
21
22 class ModuleSQLAuth : public Module
23 {
24         Module* SQLutils;
25         Module* SQLprovider;
26
27         std::string freeformquery;
28         std::string killreason;
29         std::string allowpattern;
30         std::string databaseid;
31
32         bool verbose;
33
34 public:
35         ModuleSQLAuth()
36                 {
37                 ServerInstance->Modules->UseInterface("SQLutils");
38                 ServerInstance->Modules->UseInterface("SQL");
39
40                 SQLutils = ServerInstance->Modules->Find("m_sqlutils.so");
41                 if (!SQLutils)
42                         throw ModuleException("Can't find m_sqlutils.so. Please load m_sqlutils.so before m_sqlauth.so.");
43
44                 SQLprovider = ServerInstance->Modules->FindFeature("SQL");
45                 if (!SQLprovider)
46                         throw ModuleException("Can't find an SQL provider module. Please load one before attempting to load m_sqlauth.");
47
48                 OnRehash(NULL);
49                 Implementation eventlist[] = { I_OnUserDisconnect, I_OnCheckReady, I_OnRequest, I_OnRehash, I_OnUserRegister };
50                 ServerInstance->Modules->Attach(eventlist, this, 5);
51         }
52
53         virtual ~ModuleSQLAuth()
54         {
55                 ServerInstance->Modules->DoneWithInterface("SQL");
56                 ServerInstance->Modules->DoneWithInterface("SQLutils");
57         }
58
59
60         virtual void OnRehash(User* user)
61         {
62                 ConfigReader Conf;
63
64                 databaseid      = Conf.ReadValue("sqlauth", "dbid", 0);                 /* Database ID, given to the SQL service provider */
65                 freeformquery   = Conf.ReadValue("sqlauth", "query", 0);        /* Field name where username can be found */
66                 killreason      = Conf.ReadValue("sqlauth", "killreason", 0);   /* Reason to give when access is denied to a user (put your reg details here) */
67                 allowpattern    = Conf.ReadValue("sqlauth", "allowpattern",0 ); /* Allow nicks matching this pattern without requiring auth */
68                 verbose         = Conf.ReadFlag("sqlauth", "verbose", 0);               /* Set to true if failed connects should be reported to operators */
69         }
70
71         virtual ModResult OnUserRegister(User* user)
72         {
73                 if ((!allowpattern.empty()) && (InspIRCd::Match(user->nick,allowpattern)))
74                 {
75                         user->Extend("sqlauthed");
76                         return MOD_RES_PASSTHRU;
77                 }
78
79                 if (!CheckCredentials(user))
80                 {
81                         ServerInstance->Users->QuitUser(user, killreason);
82                         return MOD_RES_DENY;
83                 }
84                 return MOD_RES_PASSTHRU;
85         }
86
87         bool CheckCredentials(User* user)
88         {
89                 std::string thisquery = freeformquery;
90                 std::string safepass = user->password;
91                 std::string safegecos = user->fullname;
92
93                 /* Search and replace the escaped nick and escaped pass into the query */
94
95                 SearchAndReplace(safepass, std::string("\""), std::string("\\\""));
96                 SearchAndReplace(safegecos, std::string("\""), std::string("\\\""));
97
98                 SearchAndReplace(thisquery, std::string("$nick"), user->nick);
99                 SearchAndReplace(thisquery, std::string("$pass"), safepass);
100                 SearchAndReplace(thisquery, std::string("$host"), user->host);
101                 SearchAndReplace(thisquery, std::string("$ip"), std::string(user->GetIPString()));
102                 SearchAndReplace(thisquery, std::string("$gecos"), safegecos);
103                 SearchAndReplace(thisquery, std::string("$ident"), user->ident);
104                 SearchAndReplace(thisquery, std::string("$server"), std::string(user->server));
105                 SearchAndReplace(thisquery, std::string("$uuid"), user->uuid);
106
107                 Module* HashMod = ServerInstance->Modules->Find("m_md5.so");
108
109                 if (HashMod)
110                 {
111                         HashResetRequest(this, HashMod).Send();
112                         SearchAndReplace(thisquery, std::string("$md5pass"), std::string(HashSumRequest(this, HashMod, user->password).Send()));
113                 }
114
115                 HashMod = ServerInstance->Modules->Find("m_sha256.so");
116
117                 if (HashMod)
118                 {
119                         HashResetRequest(this, HashMod).Send();
120                         SearchAndReplace(thisquery, std::string("$sha256pass"), std::string(HashSumRequest(this, HashMod, user->password).Send()));
121                 }
122
123                 /* Build the query */
124                 SQLrequest req = SQLrequest(this, SQLprovider, databaseid, SQLquery(thisquery));
125
126                 if(req.Send())
127                 {
128                         /* When we get the query response from the service provider we will be given an ID to play with,
129                          * just an ID number which is unique to this query. We need a way of associating that ID with a User
130                          * so we insert it into a map mapping the IDs to users.
131                          * Thankfully m_sqlutils provides this, it will associate a ID with a user or channel, and if the user quits it removes the
132                          * association. This means that if the user quits during a query we will just get a failed lookup from m_sqlutils - telling
133                          * us to discard the query.
134                          */
135                         AssociateUser(this, SQLutils, req.id, user).Send();
136
137                         return true;
138                 }
139                 else
140                 {
141                         if (verbose)
142                                 ServerInstance->SNO->WriteGlobalSno('a', "Forbidden connection from %s!%s@%s (SQL query failed: %s)", user->nick.c_str(), user->ident.c_str(), user->host.c_str(), req.error.Str());
143                         return false;
144                 }
145         }
146
147         virtual const char* OnRequest(Request* request)
148         {
149                 if(strcmp(SQLRESID, request->GetId()) == 0)
150                 {
151                         SQLresult* res = static_cast<SQLresult*>(request);
152
153                         User* user = GetAssocUser(this, SQLutils, res->id).S().user;
154                         UnAssociate(this, SQLutils, res->id).S();
155
156                         if(user)
157                         {
158                                 if(res->error.Id() == SQL_NO_ERROR)
159                                 {
160                                         if(res->Rows())
161                                         {
162                                                 /* We got a row in the result, this is enough really */
163                                                 user->Extend("sqlauthed");
164                                         }
165                                         else if (verbose)
166                                         {
167                                                 /* No rows in result, this means there was no record matching the user */
168                                                 ServerInstance->SNO->WriteGlobalSno('a', "Forbidden connection from %s!%s@%s (SQL query returned no matches)", user->nick.c_str(), user->ident.c_str(), user->host.c_str());
169                                                 user->Extend("sqlauth_failed");
170                                         }
171                                 }
172                                 else if (verbose)
173                                 {
174                                         ServerInstance->SNO->WriteGlobalSno('a', "Forbidden connection from %s!%s@%s (SQL query failed: %s)", user->nick.c_str(), user->ident.c_str(), user->host.c_str(), res->error.Str());
175                                         user->Extend("sqlauth_failed");
176                                 }
177                         }
178                         else
179                         {
180                                 return NULL;
181                         }
182
183                         if (!user->GetExt("sqlauthed"))
184                         {
185                                 ServerInstance->Users->QuitUser(user, killreason);
186                         }
187                         return SQLSUCCESS;
188                 }
189                 return NULL;
190         }
191
192         virtual void OnUserDisconnect(User* user)
193         {
194                 user->Shrink("sqlauthed");
195                 user->Shrink("sqlauth_failed");
196         }
197
198         virtual ModResult OnCheckReady(User* user)
199         {
200                 return user->GetExt("sqlauthed") ? MOD_RES_PASSTHRU : MOD_RES_DENY;
201         }
202
203         virtual Version GetVersion()
204         {
205                 return Version("Allow/Deny connections based upon an arbitary SQL table", VF_VENDOR, API_VERSION);
206         }
207
208 };
209
210 MODULE_INIT(ModuleSQLAuth)