]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_cap.cpp
Fix the cloaking module on C++98 compilers.
[user/henk/code/inspircd.git] / src / modules / m_cap.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2018-2020 Sadie Powell <sadie@witchery.services>
5  *   Copyright (C) 2015-2016, 2018 Attila Molnar <attilamolnar@hush.com>
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/reload.h"
23 #include "modules/cap.h"
24
25 enum
26 {
27         // From IRCv3 capability-negotiation-3.1.
28         ERR_INVALIDCAPCMD = 410
29 };
30
31 namespace Cap
32 {
33         class ManagerImpl;
34 }
35
36 static Cap::ManagerImpl* managerimpl;
37
38 class Cap::ManagerImpl : public Cap::Manager, public ReloadModule::EventListener
39 {
40         /** Stores the cap state of a module being reloaded
41          */
42         struct CapModData
43         {
44                 struct Data
45                 {
46                         std::string name;
47                         std::vector<std::string> users;
48
49                         Data(Capability* cap)
50                                 : name(cap->GetName())
51                         {
52                         }
53                 };
54                 std::vector<Data> caps;
55         };
56
57         typedef insp::flat_map<std::string, Capability*, irc::insensitive_swo> CapMap;
58
59         ExtItem capext;
60         CapMap caps;
61         Events::ModuleEventProvider& evprov;
62
63         static bool CanRequest(LocalUser* user, Ext usercaps, Capability* cap, bool adding)
64         {
65                 const bool hascap = ((usercaps & cap->GetMask()) != 0);
66                 if (hascap == adding)
67                         return true;
68
69                 return cap->OnRequest(user, adding);
70         }
71
72         Capability::Bit AllocateBit() const
73         {
74                 Capability::Bit used = 0;
75                 for (CapMap::const_iterator i = caps.begin(); i != caps.end(); ++i)
76                 {
77                         Capability* cap = i->second;
78                         used |= cap->GetMask();
79                 }
80
81                 for (size_t i = 0; i < MAX_CAPS; i++)
82                 {
83                         Capability::Bit bit = (static_cast<Capability::Bit>(1) << i);
84                         if (!(used & bit))
85                                 return bit;
86                 }
87                 throw ModuleException("Too many caps");
88         }
89
90         void OnReloadModuleSave(Module* mod, ReloadModule::CustomData& cd) CXX11_OVERRIDE
91         {
92                 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "OnReloadModuleSave()");
93                 if (mod == creator)
94                         return;
95
96                 CapModData* capmoddata = new CapModData;
97                 cd.add(this, capmoddata);
98
99                 for (CapMap::iterator i = caps.begin(); i != caps.end(); ++i)
100                 {
101                         Capability* cap = i->second;
102                         // Only save users of caps that belong to the module being reloaded
103                         if (cap->creator != mod)
104                                 continue;
105
106                         ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Module being reloaded implements cap %s, saving cap users", cap->GetName().c_str());
107                         capmoddata->caps.push_back(CapModData::Data(cap));
108                         CapModData::Data& capdata = capmoddata->caps.back();
109
110                         // Populate list with uuids of users who are using the cap
111                         const UserManager::LocalList& list = ServerInstance->Users.GetLocalUsers();
112                         for (UserManager::LocalList::const_iterator j = list.begin(); j != list.end(); ++j)
113                         {
114                                 LocalUser* user = *j;
115                                 if (cap->get(user))
116                                         capdata.users.push_back(user->uuid);
117                         }
118                 }
119         }
120
121         void OnReloadModuleRestore(Module* mod, void* data) CXX11_OVERRIDE
122         {
123                 CapModData* capmoddata = static_cast<CapModData*>(data);
124                 for (std::vector<CapModData::Data>::const_iterator i = capmoddata->caps.begin(); i != capmoddata->caps.end(); ++i)
125                 {
126                         const CapModData::Data& capdata = *i;
127                         Capability* cap = ManagerImpl::Find(capdata.name);
128                         if (!cap)
129                         {
130                                 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Cap %s is no longer available after reload", capdata.name.c_str());
131                                 continue;
132                         }
133
134                         // Set back the cap for all users who were using it before the reload
135                         for (std::vector<std::string>::const_iterator j = capdata.users.begin(); j != capdata.users.end(); ++j)
136                         {
137                                 const std::string& uuid = *j;
138                                 User* user = ServerInstance->FindUUID(uuid);
139                                 if (!user)
140                                 {
141                                         ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "User %s is gone when trying to restore cap %s", uuid.c_str(), capdata.name.c_str());
142                                         continue;
143                                 }
144
145                                 cap->set(user, true);
146                         }
147                 }
148                 delete capmoddata;
149         }
150
151  public:
152         ManagerImpl(Module* mod, Events::ModuleEventProvider& evprovref)
153                 : Cap::Manager(mod)
154                 , ReloadModule::EventListener(mod)
155                 , capext(mod)
156                 , evprov(evprovref)
157         {
158                 managerimpl = this;
159         }
160
161         ~ManagerImpl()
162         {
163                 for (CapMap::iterator i = caps.begin(); i != caps.end(); ++i)
164                 {
165                         Capability* cap = i->second;
166                         cap->Unregister();
167                 }
168         }
169
170         void AddCap(Cap::Capability* cap) CXX11_OVERRIDE
171         {
172                 // No-op if the cap is already registered.
173                 // This allows modules to call SetActive() on a cap without checking if it's active first.
174                 if (cap->IsRegistered())
175                         return;
176
177                 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Registering cap %s", cap->GetName().c_str());
178                 cap->bit = AllocateBit();
179                 cap->extitem = &capext;
180                 caps.insert(std::make_pair(cap->GetName(), cap));
181                 ServerInstance->Modules.AddReferent("cap/" + cap->GetName(), cap);
182
183                 FOREACH_MOD_CUSTOM(evprov, Cap::EventListener, OnCapAddDel, (cap, true));
184         }
185
186         void DelCap(Cap::Capability* cap) CXX11_OVERRIDE
187         {
188                 // No-op if the cap is not registered, see AddCap() above
189                 if (!cap->IsRegistered())
190                         return;
191
192                 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Unregistering cap %s", cap->GetName().c_str());
193
194                 // Fire the event first so modules can still see who is using the cap which is being unregistered
195                 FOREACH_MOD_CUSTOM(evprov, Cap::EventListener, OnCapAddDel, (cap, false));
196
197                 // Turn off the cap for all users
198                 const UserManager::LocalList& list = ServerInstance->Users.GetLocalUsers();
199                 for (UserManager::LocalList::const_iterator i = list.begin(); i != list.end(); ++i)
200                 {
201                         LocalUser* user = *i;
202                         cap->set(user, false);
203                 }
204
205                 ServerInstance->Modules.DelReferent(cap);
206                 cap->Unregister();
207                 caps.erase(cap->GetName());
208         }
209
210         Capability* Find(const std::string& capname) const CXX11_OVERRIDE
211         {
212                 CapMap::const_iterator it = caps.find(capname);
213                 if (it != caps.end())
214                         return it->second;
215                 return NULL;
216         }
217
218         void NotifyValueChange(Capability* cap) CXX11_OVERRIDE
219         {
220                 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Cap %s changed value", cap->GetName().c_str());
221                 FOREACH_MOD_CUSTOM(evprov, Cap::EventListener, OnCapValueChange, (cap));
222         }
223
224         Protocol GetProtocol(LocalUser* user) const
225         {
226                 return ((capext.get(user) & CAP_302_BIT) ? CAP_302 : CAP_LEGACY);
227         }
228
229         void Set302Protocol(LocalUser* user)
230         {
231                 capext.set(user, capext.get(user) | CAP_302_BIT);
232         }
233
234         bool HandleReq(LocalUser* user, const std::string& reqlist)
235         {
236                 Ext usercaps = capext.get(user);
237                 irc::spacesepstream ss(reqlist);
238                 for (std::string capname; ss.GetToken(capname); )
239                 {
240                         bool remove = (capname[0] == '-');
241                         if (remove)
242                                 capname.erase(capname.begin());
243
244                         Capability* cap = ManagerImpl::Find(capname);
245                         if ((!cap) || (!CanRequest(user, usercaps, cap, !remove)))
246                                 return false;
247
248                         if (remove)
249                                 usercaps = cap->DelFromMask(usercaps);
250                         else
251                                 usercaps = cap->AddToMask(usercaps);
252                 }
253
254                 capext.set(user, usercaps);
255                 return true;
256         }
257
258         void HandleList(std::vector<std::string>& out, LocalUser* user, bool show_all, bool show_values, bool minus_prefix = false) const
259         {
260                 Ext show_caps = (show_all ? ~0 : capext.get(user));
261
262                 for (CapMap::const_iterator i = caps.begin(); i != caps.end(); ++i)
263                 {
264                         Capability* cap = i->second;
265                         if (!(show_caps & cap->GetMask()))
266                                 continue;
267
268                         if ((show_all) && (!cap->OnList(user)))
269                                 continue;
270
271                         std::string token;
272                         if (minus_prefix)
273                                 token.push_back('-');
274                         token.append(cap->GetName());
275
276                         if (show_values)
277                         {
278                                 const std::string* capvalue = cap->GetValue(user);
279                                 if ((capvalue) && (!capvalue->empty()) && (capvalue->find(' ') == std::string::npos))
280                                 {
281                                         token.push_back('=');
282                                         token.append(*capvalue, 0, MAX_VALUE_LENGTH);
283                                 }
284                         }
285                         out.push_back(token);
286                 }
287         }
288
289         void HandleClear(LocalUser* user, std::vector<std::string>& result)
290         {
291                 HandleList(result, user, false, false, true);
292                 capext.unset(user);
293         }
294 };
295
296 namespace
297 {
298         std::string SerializeCaps(const Extensible* container, void* item, bool human)
299         {
300                 // XXX: Cast away the const because IS_LOCAL() doesn't handle it
301                 LocalUser* user = IS_LOCAL(const_cast<User*>(static_cast<const User*>(container)));
302                 if (!user)
303                         return std::string();
304
305                 // List requested caps
306                 std::vector<std::string> result;
307                 managerimpl->HandleList(result, user, false, false);
308
309                 // Serialize cap protocol version. If building a human-readable string append a
310                 // new token, otherwise append only a single character indicating the version.
311                 std::string version;
312                 if (human)
313                         version.append("capversion=3.");
314                 switch (managerimpl->GetProtocol(user))
315                 {
316                         case Cap::CAP_302:
317                                 version.push_back('2');
318                                 break;
319                         default:
320                                 version.push_back('1');
321                                 break;
322                 }
323                 result.push_back(version);
324
325                 return stdalgo::string::join(result, ' ');
326         }
327 }
328
329 Cap::ExtItem::ExtItem(Module* mod)
330         : LocalIntExt("caps", ExtensionItem::EXT_USER, mod)
331 {
332 }
333
334 std::string Cap::ExtItem::ToHuman(const Extensible* container, void* item) const
335 {
336         return SerializeCaps(container, item, true);
337 }
338
339 std::string Cap::ExtItem::ToInternal(const Extensible* container, void* item) const
340 {
341         return SerializeCaps(container, item, false);
342 }
343
344 void Cap::ExtItem::FromInternal(Extensible* container, const std::string& value)
345 {
346         LocalUser* user = IS_LOCAL(static_cast<User*>(container));
347         if (!user)
348                 return; // Can't happen
349
350         // Process the cap protocol version which is a single character at the end of the serialized string
351         const char verchar = *value.rbegin();
352         if (verchar == '2')
353                 managerimpl->Set302Protocol(user);
354
355         // Remove the version indicator from the string passed to HandleReq
356         std::string caplist(value, 0, value.size()-1);
357         managerimpl->HandleReq(user, caplist);
358 }
359
360 class CapMessage : public Cap::MessageBase
361 {
362  public:
363         CapMessage(LocalUser* user, const std::string& subcmd, const std::string& result, bool asterisk)
364                 : Cap::MessageBase(subcmd)
365         {
366                 SetUser(user);
367                 if (asterisk)
368                         PushParam("*");
369                 PushParamRef(result);
370         }
371 };
372
373 class CommandCap : public SplitCommand
374 {
375  private:
376         Events::ModuleEventProvider evprov;
377         Cap::ManagerImpl manager;
378         ClientProtocol::EventProvider protoevprov;
379
380         void DisplayResult(LocalUser* user, const std::string& subcmd, std::vector<std::string> result, bool asterisk)
381         {
382                 size_t maxline = ServerInstance->Config->Limits.MaxLine - ServerInstance->Config->ServerName.size() - user->nick.length() - subcmd.length() - 11;
383                 std::string line;
384                 for (std::vector<std::string>::const_iterator iter = result.begin(); iter != result.end(); ++iter)
385                 {
386                         if (line.length() + iter->length() < maxline)
387                         {
388                                 line.append(*iter);
389                                 line.push_back(' ');
390                         }
391                         else
392                         {
393                                 DisplaySingleResult(user, subcmd, line, asterisk);
394                                 line.clear();
395                         }
396                 }
397                 DisplaySingleResult(user, subcmd, line, false);
398         }
399
400         void DisplaySingleResult(LocalUser* user, const std::string& subcmd, const std::string& result, bool asterisk)
401         {
402                 CapMessage msg(user, subcmd, result, asterisk);
403                 ClientProtocol::Event ev(protoevprov, msg);
404                 user->Send(ev);
405         }
406
407  public:
408         LocalIntExt holdext;
409
410         CommandCap(Module* mod)
411                 : SplitCommand(mod, "CAP", 1)
412                 , evprov(mod, "event/cap")
413                 , manager(mod, evprov)
414                 , protoevprov(mod, name)
415                 , holdext("cap_hold", ExtensionItem::EXT_USER, mod)
416         {
417                 works_before_reg = true;
418         }
419
420         CmdResult HandleLocal(LocalUser* user, const Params& parameters) CXX11_OVERRIDE
421         {
422                 if (user->registered != REG_ALL)
423                         holdext.set(user, 1);
424
425                 const std::string& subcommand = parameters[0];
426                 if (irc::equals(subcommand, "REQ"))
427                 {
428                         if (parameters.size() < 2)
429                                 return CMD_FAILURE;
430
431                         const std::string replysubcmd = (manager.HandleReq(user, parameters[1]) ? "ACK" : "NAK");
432                         DisplaySingleResult(user, replysubcmd, parameters[1], false);
433                 }
434                 else if (irc::equals(subcommand, "END"))
435                 {
436                         holdext.unset(user);
437                 }
438                 else if (irc::equals(subcommand, "LS") || irc::equals(subcommand, "LIST"))
439                 {
440                         Cap::Protocol capversion = Cap::CAP_LEGACY;
441                         const bool is_ls = (subcommand.length() == 2);
442                         if ((is_ls) && (parameters.size() > 1))
443                         {
444                                 unsigned int version = ConvToNum<unsigned int>(parameters[1]);
445                                 if (version >= 302)
446                                 {
447                                         capversion = Cap::CAP_302;
448                                         manager.Set302Protocol(user);
449                                 }
450                         }
451
452                         std::vector<std::string> result;
453                         // Show values only if supports v3.2 and doing LS
454                         manager.HandleList(result, user, is_ls, ((is_ls) && (capversion != Cap::CAP_LEGACY)));
455                         DisplayResult(user, subcommand, result, (capversion != Cap::CAP_LEGACY));
456                 }
457                 else if (irc::equals(subcommand, "CLEAR") && (manager.GetProtocol(user) == Cap::CAP_LEGACY))
458                 {
459                         std::vector<std::string> result;
460                         manager.HandleClear(user, result);
461                         DisplayResult(user, "ACK", result, false);
462                 }
463                 else
464                 {
465                         user->WriteNumeric(ERR_INVALIDCAPCMD, subcommand.empty() ? "*" : subcommand, "Invalid CAP subcommand");
466                         return CMD_FAILURE;
467                 }
468
469                 return CMD_SUCCESS;
470         }
471 };
472
473 class PoisonCap : public Cap::Capability
474 {
475  public:
476         PoisonCap(Module* mod)
477                 : Cap::Capability(mod, "inspircd.org/poison")
478         {
479         }
480
481         bool OnRequest(LocalUser* user, bool adding) CXX11_OVERRIDE
482         {
483                 // Reject the attempt to enable this capability.
484                 return false;
485         }
486 };
487
488 class ModuleCap : public Module
489 {
490  private:
491         CommandCap cmd;
492         PoisonCap poisoncap;
493         Cap::Capability stdrplcap;
494
495  public:
496         ModuleCap()
497                 : cmd(this)
498                 , poisoncap(this)
499                 , stdrplcap(this, "inspircd.org/standard-replies")
500         {
501         }
502
503         ModResult OnCheckReady(LocalUser* user) CXX11_OVERRIDE
504         {
505                 return (cmd.holdext.get(user) ? MOD_RES_DENY : MOD_RES_PASSTHRU);
506         }
507
508         Version GetVersion() CXX11_OVERRIDE
509         {
510                 return Version("Implements support for the IRCv3 Client Capability Negotiation extension.", VF_VENDOR);
511         }
512 };
513
514 MODULE_INIT(ModuleCap)