]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_sqloper.cpp
Fix the cloaking module on C++98 compilers.
[user/henk/code/inspircd.git] / src / modules / m_sqloper.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2019 B00mX0r <b00mx0r@aureus.pw>
5  *   Copyright (C) 2018 Dylan Frank <b00mx0r@aureus.pw>
6  *   Copyright (C) 2013-2014, 2018 Attila Molnar <attilamolnar@hush.com>
7  *   Copyright (C) 2013, 2017-2018, 2020 Sadie Powell <sadie@witchery.services>
8  *   Copyright (C) 2012, 2019 Robby <robby@chatbelgie.be>
9  *   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
10  *   Copyright (C) 2009 Uli Schlachter <psychon@inspircd.org>
11  *   Copyright (C) 2007-2008 Dennis Friis <peavey@inspircd.org>
12  *   Copyright (C) 2005-2007, 2010 Craig Edwards <brain@inspircd.org>
13  *
14  * This file is part of InspIRCd.  InspIRCd is free software: you can
15  * redistribute it and/or modify it under the terms of the GNU General Public
16  * License as published by the Free Software Foundation, version 2.
17  *
18  * This program is distributed in the hope that it will be useful, but WITHOUT
19  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
20  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
21  * details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25  */
26
27
28 #include "inspircd.h"
29 #include "modules/sql.h"
30
31 class OperQuery : public SQL::Query
32 {
33  public:
34         // This variable will store all the OPER blocks from the DB
35         std::vector<std::string>& my_blocks;
36         /** We want to store the username and password if this is called during an /OPER, as we're responsible for /OPER post-DB fetch
37          *  Note: uid will be empty if this DB update was not called as a result of a user command (i.e. /REHASH)
38          */
39         const std::string uid, username, password;
40         OperQuery(Module* me, std::vector<std::string>& mb, const std::string& u, const std::string& un, const std::string& pw)
41                 : SQL::Query(me)
42                 , my_blocks(mb)
43                 , uid(u)
44                 , username(un)
45                 , password(pw)
46         {
47         }
48         OperQuery(Module* me, std::vector<std::string>& mb)
49                 : SQL::Query(me)
50                 , my_blocks(mb)
51         {
52         }
53
54         void OnResult(SQL::Result& res) CXX11_OVERRIDE
55         {
56                 ServerConfig::OperIndex& oper_blocks = ServerInstance->Config->oper_blocks;
57
58                 // Remove our previous blocks from oper_blocks for a clean update
59                 for (std::vector<std::string>::const_iterator i = my_blocks.begin(); i != my_blocks.end(); ++i)
60                 {
61                         oper_blocks.erase(*i);
62                 }
63                 my_blocks.clear();
64
65                 SQL::Row row;
66                 // Iterate through DB results to create oper blocks from sqloper rows
67                 while (res.GetRow(row))
68                 {
69                         std::vector<std::string> cols;
70                         res.GetCols(cols);
71
72                         // Create the oper tag as if we were the conf file.
73                         ConfigItems* items;
74                         reference<ConfigTag> tag = ConfigTag::create("oper", MODNAME, 0, items);
75
76                         /** Iterate through each column in the SQLOpers table. An infinite number of fields can be specified.
77                          *  Column 'x' with cell value 'y' will be the same as x=y in an OPER block in opers.conf.
78                          */
79                         for (unsigned int i=0; i < cols.size(); ++i)
80                         {
81                                 if (!row[i].IsNull())
82                                         (*items)[cols[i]] = row[i];
83                         }
84                         const std::string name = tag->getString("name");
85
86                         // Skip both duplicate sqloper blocks and sqloper blocks that attempt to override conf blocks.
87                         if (oper_blocks.find(name) != oper_blocks.end())
88                                 continue;
89
90                         const std::string type = tag->getString("type");
91                         ServerConfig::OperIndex::iterator tblk = ServerInstance->Config->OperTypes.find(type);
92                         if (tblk == ServerInstance->Config->OperTypes.end())
93                         {
94                                 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Sqloper block " + name + " has missing type " + type);
95                                 ServerInstance->SNO->WriteGlobalSno('a', "m_sqloper: Oper block %s has missing type %s", name.c_str(), type.c_str());
96                                 continue;
97                         }
98
99                         OperInfo* ifo = new OperInfo(type);
100
101                         ifo->type_block = tblk->second->type_block;
102                         ifo->oper_block = tag;
103                         ifo->class_blocks.assign(tblk->second->class_blocks.begin(), tblk->second->class_blocks.end());
104                         oper_blocks[name] = ifo;
105                         my_blocks.push_back(name);
106                         row.clear();
107                 }
108
109                 // If this was done as a result of /OPER and not a config read
110                 if (!uid.empty())
111                 {
112                         // Now that we've updated the DB, call any other /OPER hooks and then call /OPER
113                         OperExec();
114                 }
115         }
116
117         void OnError(SQL::Error& error) CXX11_OVERRIDE
118         {
119                 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "query failed (%s)", error.ToString());
120                 ServerInstance->SNO->WriteGlobalSno('a', "m_sqloper: Failed to update blocks from database");
121                 if (!uid.empty())
122                 {
123                         // Fallback. We don't want to block a netadmin from /OPER
124                         OperExec();
125                 }
126         }
127
128         // Call /oper after placing all blocks from the SQL table into the config->oper_blocks list.
129         void OperExec()
130         {
131                 User* user = ServerInstance->FindNick(uid);
132                 LocalUser* localuser = IS_LOCAL(user);
133                 // This should never be true
134                 if (!localuser)
135                         return;
136
137                 Command* oper_command = ServerInstance->Parser.GetHandler("OPER");
138
139                 if (oper_command)
140                 {
141                         CommandBase::Params params;
142                         params.push_back(username);
143                         params.push_back(password);
144
145                         // Begin callback to other modules (i.e. sslinfo) now that we completed the DB fetch
146                         ModResult MOD_RESULT;
147
148                         std::string origin = "OPER";
149                         FIRST_MOD_RESULT(OnPreCommand, MOD_RESULT, (origin, params, localuser, true));
150                         if (MOD_RESULT == MOD_RES_DENY)
151                                 return;
152
153                         // Now handle /OPER.
154                         ClientProtocol::TagMap tags;
155                         oper_command->Handle(user, CommandBase::Params(params, tags));
156                 }
157                 else
158                 {
159                         ServerInstance->Logs->Log(MODNAME, LOG_SPARSE, "BUG: WHAT?! Why do we have no OPER command?!");
160                 }
161         }
162 };
163
164 class ModuleSQLOper : public Module
165 {
166         // Whether OperQuery is running
167         bool active;
168         std::string query;
169         // Stores oper blocks from DB
170         std::vector<std::string> my_blocks;
171         dynamic_reference<SQL::Provider> SQL;
172
173 public:
174         ModuleSQLOper()
175                 : active(false)
176                 , SQL(this, "SQL")
177         {
178         }
179
180         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
181         {
182                 // Clear list of our blocks, as ConfigReader just wiped them anyway
183                 my_blocks.clear();
184
185                 ConfigTag* tag = ServerInstance->Config->ConfValue("sqloper");
186
187                 std::string dbid = tag->getString("dbid");
188                 if (dbid.empty())
189                         SQL.SetProvider("SQL");
190                 else
191                         SQL.SetProvider("SQL/" + dbid);
192
193                 query = tag->getString("query", "SELECT * FROM ircd_opers WHERE active=1;", 1);
194                 // Update sqloper list from the database.
195                 GetOperBlocks();
196         }
197
198         ~ModuleSQLOper()
199         {
200                 // Remove all oper blocks that were from the DB
201                 for (std::vector<std::string>::const_iterator i = my_blocks.begin(); i != my_blocks.end(); ++i)
202                 {
203                         ServerInstance->Config->oper_blocks.erase(*i);
204                 }
205         }
206
207         ModResult OnPreCommand(std::string& command, CommandBase::Params& parameters, LocalUser* user, bool validated) CXX11_OVERRIDE
208         {
209                 // If we are not in the middle of an existing /OPER and someone is trying to oper-up
210                 if (validated && command == "OPER" && parameters.size() >= 2 && !active)
211                 {
212                         if (SQL)
213                         {
214                                 GetOperBlocks(user->uuid, parameters[0], parameters[1]);
215                                 /** We need to reload oper blocks from the DB before other
216                                  *  hooks can run (i.e. sslinfo). We will re-call /OPER later.
217                                  */
218                                 return MOD_RES_DENY;
219                         }
220                         ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "database not present");
221                 }
222                 else if (active)
223                 {
224                         active = false;
225                 }
226                 // There is either no DB or we successfully reloaded oper blocks
227                 return MOD_RES_PASSTHRU;
228         }
229
230         // The one w/o params is for non-/OPER DB updates, such as a rehash.
231         void GetOperBlocks()
232         {
233                 SQL->Submit(new OperQuery(this, my_blocks), query);
234         }
235         void GetOperBlocks(const std::string u, const std::string& un, const std::string& pw)
236         {
237                 active = true;
238                 // Call to SQL query to fetch oper list from SQL table.
239                 SQL->Submit(new OperQuery(this, my_blocks, u, un, pw), query);
240         }
241
242         void Prioritize() CXX11_OVERRIDE
243         {
244                 /** Run before other /OPER hooks that expect populated blocks, i.e. sslinfo or a TOTP module.
245                  *  We issue a DENY first, and will re-run OnPreCommand later to trigger the other hooks post-DB update.
246                  */
247                 ServerInstance->Modules.SetPriority(this, I_OnPreCommand, PRIORITY_FIRST);
248         }
249
250         Version GetVersion() CXX11_OVERRIDE
251         {
252                 return Version("Allows server operators to be authenticated against an SQL table.", VF_VENDOR);
253         }
254 };
255
256 MODULE_INIT(ModuleSQLOper)