]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_sqloper.cpp
Register channel mode Z when enabled
[user/henk/code/inspircd.git] / src / modules / m_sqloper.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2010 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 "hash.h"
18
19 /* $ModDesc: Allows storage of oper credentials in an SQL table */
20
21 typedef std::map<irc::string, Module*> hashymodules;
22
23 class ModuleSQLOper : public Module
24 {
25         LocalStringExt saved_user;
26         LocalStringExt saved_pass;
27         Module* SQLutils;
28         std::string databaseid;
29         std::string hashtype;
30         parameterlist names;
31
32 public:
33         ModuleSQLOper() : saved_user("sqloper_user", this), saved_pass("sqloper_pass", this)
34         {
35                 OnRehash(NULL);
36
37                 SQLutils = ServerInstance->Modules->Find("m_sqlutils.so");
38                 if (!SQLutils)
39                         throw ModuleException("Can't find m_sqlutils.so. Please load m_sqlutils.so before m_sqloper.so.");
40
41                 Implementation eventlist[] = { I_OnRehash, I_OnPreCommand, I_OnLoadModule };
42                 ServerInstance->Modules->Attach(eventlist, this, 3);
43                 ServerInstance->Modules->AddService(saved_user);
44                 ServerInstance->Modules->AddService(saved_pass);
45         }
46
47         bool OneOfMatches(const char* host, const char* ip, const char* hostlist)
48         {
49                 std::stringstream hl(hostlist);
50                 std::string xhost;
51                 while (hl >> xhost)
52                 {
53                         if (InspIRCd::Match(host, xhost, ascii_case_insensitive_map) || InspIRCd::MatchCIDR(ip, xhost, ascii_case_insensitive_map))
54                         {
55                                 return true;
56                         }
57                 }
58                 return false;
59         }
60
61         virtual void OnRehash(User* user)
62         {
63                 ConfigReader Conf;
64
65                 databaseid = Conf.ReadValue("sqloper", "dbid", 0); /* Database ID of a database configured for the service provider module */
66                 hashtype = Conf.ReadValue("sqloper", "hash", 0);
67         }
68
69         virtual ModResult OnPreCommand(std::string &command, std::vector<std::string> &parameters, LocalUser *user, bool validated, const std::string &original_line)
70         {
71                 if ((validated) && (command == "OPER"))
72                 {
73                         if (LookupOper(user, parameters[0], parameters[1]))
74                         {
75                                 /* Returning true here just means the query is in progress, or on it's way to being
76                                  * in progress. Nothing about the /oper actually being successful..
77                                  * If the oper lookup fails later, we pass the command to the original handler
78                                  * for /oper by calling its Handle method directly.
79                                  */
80                                 return MOD_RES_DENY;
81                         }
82                 }
83                 return MOD_RES_PASSTHRU;
84         }
85
86         bool LookupOper(User* user, const std::string &username, const std::string &password)
87         {
88                 ServiceProvider* prov = ServerInstance->Modules->FindService(SERVICE_DATA, "SQL");
89                 if (prov)
90                 {
91                         Module* target = prov->creator;
92                         HashProvider* hash = ServerInstance->Modules->FindDataService<HashProvider>("hash/" + hashtype);
93
94                         /* Make an MD5 hash of the password for using in the query */
95                         std::string md5_pass_hash = hash ? hash->hexsum(password) : password;
96
97                         /* We generate our own sum here because some database providers (e.g. SQLite) dont have a builtin md5/sha256 function,
98                          * also hashing it in the module and only passing a remote query containing a hash is more secure.
99                          */
100                         SQLrequest req = SQLrequest(this, target, databaseid,
101                                         SQLquery("SELECT username, password, hostname, type FROM ircd_opers WHERE username = '?' AND password='?'") % username % md5_pass_hash);
102
103                         /* When we get the query response from the service provider we will be given an ID to play with,
104                          * just an ID number which is unique to this query. We need a way of associating that ID with a User
105                          * so we insert it into a map mapping the IDs to users.
106                          * Thankfully m_sqlutils provides this, it will associate a ID with a user or channel, and if the user quits it removes the
107                          * association. This means that if the user quits during a query we will just get a failed lookup from m_sqlutils - telling
108                          * us to discard the query.
109                          */
110                         AssociateUser(this, SQLutils, req.id, user).Send();
111
112                         saved_user.set(user, username);
113                         saved_pass.set(user, password);
114
115                         return true;
116                 }
117                 else
118                 {
119                         ServerInstance->Logs->Log("m_sqloper",SPARSE, "WARNING: Couldn't find SQL provider module. NOBODY will be able to oper up unless their o:line is statically configured");
120                         return false;
121                 }
122         }
123
124         void OnRequest(Request& request)
125         {
126                 if (strcmp(SQLRESID, request.id) == 0)
127                 {
128                         SQLresult* res = static_cast<SQLresult*>(&request);
129
130                         User* user = GetAssocUser(this, SQLutils, res->id).S().user;
131                         UnAssociate(this, SQLutils, res->id).S();
132
133                         if (user)
134                         {
135                                 std::string* tried_user = saved_user.get(user);
136                                 std::string* tried_pass = saved_pass.get(user);
137                                 if (res->error.Id() == SQL_NO_ERROR)
138                                 {
139                                         if (res->Rows())
140                                         {
141                                                 /* We got a row in the result, this means there was a record for the oper..
142                                                  * now we just need to check if their host matches, and if it does then
143                                                  * oper them up.
144                                                  *
145                                                  * We now (previous versions of the module didn't) support multiple SQL
146                                                  * rows per-oper in the same way the config file does, all rows will be tried
147                                                  * until one is found which matches. This is useful to define several different
148                                                  * hosts for a single oper.
149                                                  *
150                                                  * The for() loop works as SQLresult::GetRowMap() returns an empty map when there
151                                                  * are no more rows to return.
152                                                  */
153
154                                                 for (SQLfieldMap& row = res->GetRowMap(); row.size(); row = res->GetRowMap())
155                                                 {
156                                                         if (OperUser(user, row["hostname"].d, row["type"].d))
157                                                         {
158                                                                 /* If/when one of the rows matches, stop checking and return */
159                                                                 saved_user.unset(user);
160                                                                 saved_pass.unset(user);
161                                                         }
162                                                         if (tried_user && tried_pass)
163                                                         {
164                                                                 LoginFail(user, *tried_user, *tried_pass);
165                                                                 saved_user.unset(user);
166                                                                 saved_pass.unset(user);
167                                                         }
168                                                 }
169                                         }
170                                         else
171                                         {
172                                                 /* No rows in result, this means there was no oper line for the user,
173                                                  * we should have already checked the o:lines so now we need an
174                                                  * "insufficient awesomeness" (invalid credentials) error
175                                                  */
176                                                 if (tried_user && tried_pass)
177                                                 {
178                                                         LoginFail(user, *tried_user, *tried_pass);
179                                                         saved_user.unset(user);
180                                                         saved_pass.unset(user);
181                                                 }
182                                         }
183                                 }
184                                 else
185                                 {
186                                         /* This one shouldn't happen, the query failed for some reason.
187                                          * We have to fail the /oper request and give them the same error
188                                          * as above.
189                                          */
190                                         if (tried_user && tried_pass)
191                                         {
192                                                 LoginFail(user, *tried_user, *tried_pass);
193                                                 saved_user.unset(user);
194                                                 saved_pass.unset(user);
195                                         }
196
197                                 }
198                         }
199                 }
200         }
201
202         void LoginFail(User* user, const std::string &username, const std::string &pass)
203         {
204                 Command* oper_command = ServerInstance->Parser->GetHandler("OPER");
205
206                 if (oper_command)
207                 {
208                         std::vector<std::string> params;
209                         params.push_back(username);
210                         params.push_back(pass);
211                         oper_command->Handle(params, user);
212                 }
213                 else
214                 {
215                         ServerInstance->Logs->Log("m_sqloper",DEBUG, "BUG: WHAT?! Why do we have no OPER command?!");
216                 }
217         }
218
219         bool OperUser(User* user, const std::string &pattern, const std::string &type)
220         {
221                 OperIndex::iterator iter = ServerInstance->Config->oper_blocks.find(" " + type);
222                 if (iter == ServerInstance->Config->oper_blocks.end())
223                         return false;
224                 OperInfo* ifo = iter->second;
225
226                 std::string hostname(user->ident);
227
228                 hostname.append("@").append(user->host);
229
230                 if (OneOfMatches(hostname.c_str(), user->GetIPString(), pattern.c_str()))
231                 {
232                         /* Opertype and host match, looks like this is it. */
233
234                         user->Oper(ifo);
235                         return true;
236                 }
237
238                 return false;
239         }
240
241         Version GetVersion()
242         {
243                 return Version("Allows storage of oper credentials in an SQL table", VF_VENDOR);
244         }
245
246 };
247
248 MODULE_INIT(ModuleSQLOper)