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