]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_sqloper.cpp
Merge pull request #590 from SaberUK/master+module-logging
[user/henk/code/inspircd.git] / src / modules / m_sqloper.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
5  *
6  * This file is part of InspIRCd.  InspIRCd is free software: you can
7  * redistribute it and/or modify it under the terms of the GNU General Public
8  * License as published by the Free Software Foundation, version 2.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
13  * details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19
20 #include "inspircd.h"
21 #include "modules/sql.h"
22 #include "modules/hash.h"
23
24 class OpMeQuery : public SQLQuery
25 {
26  public:
27         const std::string uid, username, password;
28         OpMeQuery(Module* me, const std::string& u, const std::string& un, const std::string& pw)
29                 : SQLQuery(me), uid(u), username(un), password(pw)
30         {
31         }
32
33         void OnResult(SQLResult& res) CXX11_OVERRIDE
34         {
35                 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "result for %s", uid.c_str());
36                 User* user = ServerInstance->FindNick(uid);
37                 if (!user)
38                         return;
39
40                 // multiple rows may exist
41                 SQLEntries row;
42                 while (res.GetRow(row))
43                 {
44                         if (OperUser(user, row[0], row[1]))
45                                 return;
46                 }
47                 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "no matches for %s (checked %d rows)", uid.c_str(), res.Rows());
48                 // nobody succeeded... fall back to OPER
49                 fallback();
50         }
51
52         void OnError(SQLerror& error) CXX11_OVERRIDE
53         {
54                 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "query failed (%s)", error.Str());
55                 fallback();
56         }
57
58         void fallback()
59         {
60                 User* user = ServerInstance->FindNick(uid);
61                 if (!user)
62                         return;
63
64                 Command* oper_command = ServerInstance->Parser->GetHandler("OPER");
65
66                 if (oper_command)
67                 {
68                         std::vector<std::string> params;
69                         params.push_back(username);
70                         params.push_back(password);
71                         oper_command->Handle(params, user);
72                 }
73                 else
74                 {
75                         ServerInstance->Logs->Log(MODNAME, LOG_SPARSE, "BUG: WHAT?! Why do we have no OPER command?!");
76                 }
77         }
78
79         bool OperUser(User* user, const std::string &pattern, const std::string &type)
80         {
81                 OperIndex::iterator iter = ServerInstance->Config->oper_blocks.find(" " + type);
82                 if (iter == ServerInstance->Config->oper_blocks.end())
83                 {
84                         ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "bad type '%s' in returned row for oper %s", type.c_str(), username.c_str());
85                         return false;
86                 }
87                 OperInfo* ifo = iter->second;
88
89                 std::string hostname(user->ident);
90
91                 hostname.append("@").append(user->host);
92
93                 if (InspIRCd::MatchMask(pattern, hostname, user->GetIPString()))
94                 {
95                         /* Opertype and host match, looks like this is it. */
96
97                         user->Oper(ifo);
98                         return true;
99                 }
100
101                 return false;
102         }
103 };
104
105 class ModuleSQLOper : public Module
106 {
107         std::string query;
108         std::string hashtype;
109         dynamic_reference<SQLProvider> SQL;
110
111 public:
112         ModuleSQLOper() : SQL(this, "SQL") {}
113
114         void init() CXX11_OVERRIDE
115         {
116                 OnRehash(NULL);
117
118                 Implementation eventlist[] = { I_OnRehash, I_OnPreCommand };
119                 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
120         }
121
122         void OnRehash(User* user) CXX11_OVERRIDE
123         {
124                 ConfigTag* tag = ServerInstance->Config->ConfValue("sqloper");
125
126                 std::string dbid = tag->getString("dbid");
127                 if (dbid.empty())
128                         SQL.SetProvider("SQL");
129                 else
130                         SQL.SetProvider("SQL/" + dbid);
131
132                 hashtype = tag->getString("hash");
133                 query = tag->getString("query", "SELECT hostname as host, type FROM ircd_opers WHERE username='$username' AND password='$password'");
134         }
135
136         ModResult OnPreCommand(std::string &command, std::vector<std::string> &parameters, LocalUser *user, bool validated, const std::string &original_line) CXX11_OVERRIDE
137         {
138                 if (validated && command == "OPER" && parameters.size() >= 2)
139                 {
140                         if (SQL)
141                         {
142                                 LookupOper(user, parameters[0], parameters[1]);
143                                 /* Query is in progress, it will re-invoke OPER if needed */
144                                 return MOD_RES_DENY;
145                         }
146                         ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "database not present");
147                 }
148                 return MOD_RES_PASSTHRU;
149         }
150
151         void LookupOper(User* user, const std::string &username, const std::string &password)
152         {
153                 HashProvider* hash = ServerInstance->Modules->FindDataService<HashProvider>("hash/" + hashtype);
154
155                 ParamM userinfo;
156                 SQL->PopulateUserInfo(user, userinfo);
157                 userinfo["username"] = username;
158                 userinfo["password"] = hash ? hash->hexsum(password) : password;
159
160                 SQL->submit(new OpMeQuery(this, user->uuid, username, password), query, userinfo);
161         }
162
163         Version GetVersion() CXX11_OVERRIDE
164         {
165                 return Version("Allows storage of oper credentials in an SQL table", VF_VENDOR);
166         }
167 };
168
169 MODULE_INIT(ModuleSQLOper)