]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_cap.cpp
Use irc::equals instead of transforming to upper case in CAP.
[user/henk/code/inspircd.git] / src / modules / m_cap.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2018-2019 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 (unsigned int i = 0; i < MAX_CAPS; i++)
82                 {
83                         Capability::Bit 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::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                         if (minus_prefix)
272                                 out.push_back('-');
273                         out.append(cap->GetName());
274
275                         if (show_values)
276                         {
277                                 const std::string* capvalue = cap->GetValue(user);
278                                 if ((capvalue) && (!capvalue->empty()) && (capvalue->find(' ') == std::string::npos))
279                                 {
280                                         out.push_back('=');
281                                         out.append(*capvalue, 0, MAX_VALUE_LENGTH);
282                                 }
283                         }
284                         out.push_back(' ');
285                 }
286         }
287
288         void HandleClear(LocalUser* user, std::string& result)
289         {
290                 HandleList(result, user, false, false, true);
291                 capext.unset(user);
292         }
293 };
294
295 namespace
296 {
297         std::string SerializeCaps(const Extensible* container, void* item, bool human)
298         {
299                 // XXX: Cast away the const because IS_LOCAL() doesn't handle it
300                 LocalUser* user = IS_LOCAL(const_cast<User*>(static_cast<const User*>(container)));
301                 if (!user)
302                         return std::string();
303
304                 // List requested caps
305                 std::string ret;
306                 managerimpl->HandleList(ret, user, false, false);
307
308                 // Serialize cap protocol version. If building a human-readable string append a new token, otherwise append only a single character indicating the version.
309                 Cap::Protocol protocol = managerimpl->GetProtocol(user);
310                 if (human)
311                         ret.append("capversion=3.");
312                 else if (!ret.empty())
313                         ret.erase(ret.length()-1);
314
315                 if (protocol == Cap::CAP_302)
316                         ret.push_back('2');
317                 else
318                         ret.push_back('1');
319
320                 return ret;
321         }
322 }
323
324 Cap::ExtItem::ExtItem(Module* mod)
325         : LocalIntExt("caps", ExtensionItem::EXT_USER, mod)
326 {
327 }
328
329 std::string Cap::ExtItem::ToHuman(const Extensible* container, void* item) const
330 {
331         return SerializeCaps(container, item, true);
332 }
333
334 std::string Cap::ExtItem::ToInternal(const Extensible* container, void* item) const
335 {
336         return SerializeCaps(container, item, false);
337 }
338
339 void Cap::ExtItem::FromInternal(Extensible* container, const std::string& value)
340 {
341         LocalUser* user = IS_LOCAL(static_cast<User*>(container));
342         if (!user)
343                 return; // Can't happen
344
345         // Process the cap protocol version which is a single character at the end of the serialized string
346         const char verchar = *value.rbegin();
347         if (verchar == '2')
348                 managerimpl->Set302Protocol(user);
349
350         // Remove the version indicator from the string passed to HandleReq
351         std::string caplist(value, 0, value.size()-1);
352         managerimpl->HandleReq(user, caplist);
353 }
354
355 class CapMessage : public Cap::MessageBase
356 {
357  public:
358         CapMessage(LocalUser* user, const std::string& subcmd, const std::string& result)
359                 : Cap::MessageBase(subcmd)
360         {
361                 SetUser(user);
362                 PushParamRef(result);
363         }
364 };
365
366 class CommandCap : public SplitCommand
367 {
368         Events::ModuleEventProvider evprov;
369         Cap::ManagerImpl manager;
370         ClientProtocol::EventProvider protoevprov;
371
372         void DisplayResult(LocalUser* user, const std::string& subcmd, std::string& result)
373         {
374                 if (*result.rbegin() == ' ')
375                         result.erase(result.end()-1);
376                 DisplayResult2(user, subcmd, result);
377         }
378
379         void DisplayResult2(LocalUser* user, const std::string& subcmd, const std::string& result)
380         {
381                 CapMessage msg(user, subcmd, result);
382                 ClientProtocol::Event ev(protoevprov, msg);
383                 user->Send(ev);
384         }
385
386  public:
387         LocalIntExt holdext;
388
389         CommandCap(Module* mod)
390                 : SplitCommand(mod, "CAP", 1)
391                 , evprov(mod, "event/cap")
392                 , manager(mod, evprov)
393                 , protoevprov(mod, name)
394                 , holdext("cap_hold", ExtensionItem::EXT_USER, mod)
395         {
396                 works_before_reg = true;
397         }
398
399         CmdResult HandleLocal(LocalUser* user, const Params& parameters) CXX11_OVERRIDE
400         {
401                 if (user->registered != REG_ALL)
402                         holdext.set(user, 1);
403
404                 const std::string& subcommand = parameters[0];
405                 if (irc::equals(subcommand, "REQ"))
406                 {
407                         if (parameters.size() < 2)
408                                 return CMD_FAILURE;
409
410                         const std::string replysubcmd = (manager.HandleReq(user, parameters[1]) ? "ACK" : "NAK");
411                         DisplayResult2(user, replysubcmd, parameters[1]);
412                 }
413                 else if (irc::equals(subcommand, "END"))
414                 {
415                         holdext.unset(user);
416                 }
417                 else if (irc::equals(subcommand, "LS") || irc::equals(subcommand, "LIST"))
418                 {
419                         Cap::Protocol capversion = Cap::CAP_LEGACY;
420                         const bool is_ls = (subcommand.length() == 2);
421                         if ((is_ls) && (parameters.size() > 1))
422                         {
423                                 unsigned int version = ConvToNum<unsigned int>(parameters[1]);
424                                 if (version >= 302)
425                                 {
426                                         capversion = Cap::CAP_302;
427                                         manager.Set302Protocol(user);
428                                 }
429                         }
430
431                         std::string result;
432                         // Show values only if supports v3.2 and doing LS
433                         manager.HandleList(result, user, is_ls, ((is_ls) && (capversion != Cap::CAP_LEGACY)));
434                         DisplayResult(user, subcommand, result);
435                 }
436                 else if (irc::equals(subcommand, "CLEAR") && (manager.GetProtocol(user) == Cap::CAP_LEGACY))
437                 {
438                         std::string result;
439                         manager.HandleClear(user, result);
440                         DisplayResult(user, "ACK", result);
441                 }
442                 else
443                 {
444                         user->WriteNumeric(ERR_INVALIDCAPCMD, subcommand.empty() ? "*" : subcommand, "Invalid CAP subcommand");
445                         return CMD_FAILURE;
446                 }
447
448                 return CMD_SUCCESS;
449         }
450 };
451
452 class ModuleCap : public Module
453 {
454         CommandCap cmd;
455
456  public:
457         ModuleCap()
458                 : cmd(this)
459         {
460         }
461
462         ModResult OnCheckReady(LocalUser* user) CXX11_OVERRIDE
463         {
464                 return (cmd.holdext.get(user) ? MOD_RES_DENY : MOD_RES_PASSTHRU);
465         }
466
467         Version GetVersion() CXX11_OVERRIDE
468         {
469                 return Version("Provides support for CAP capability negotiation", VF_VENDOR);
470         }
471 };
472
473 MODULE_INIT(ModuleCap)