X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=src%2Fmodules%2Fm_cap.cpp;h=f035944baf87c24b7d9a13383fb270b8b5e97a6d;hb=HEAD;hp=6b4387fdd96ccec55db075d0391be07a6478c02f;hpb=9d81896b74fda1d2cd40723dcec33bf75f843038;p=user%2Fhenk%2Fcode%2Finspircd.git diff --git a/src/modules/m_cap.cpp b/src/modules/m_cap.cpp index 6b4387fdd..f035944ba 100644 --- a/src/modules/m_cap.cpp +++ b/src/modules/m_cap.cpp @@ -1,8 +1,8 @@ /* * InspIRCd -- Internet Relay Chat Daemon * - * Copyright (C) 2009 Daniel De Graaf - * Copyright (C) 2008 Craig Edwards + * Copyright (C) 2018-2020 Sadie Powell + * Copyright (C) 2015-2016, 2018 Attila Molnar * * This file is part of InspIRCd. InspIRCd is free software: you can * redistribute it and/or modify it under the terms of the GNU General Public @@ -19,108 +19,450 @@ #include "inspircd.h" -#include "m_cap.h" +#include "modules/reload.h" +#include "modules/cap.h" -/* $ModDesc: Provides the CAP negotiation mechanism seen in ratbox-derived ircds */ +enum +{ + // From IRCv3 capability-negotiation-3.1. + ERR_INVALIDCAPCMD = 410 +}; -/* -CAP LS -:alfred.staticbox.net CAP * LS :multi-prefix sasl -CAP REQ :multi-prefix -:alfred.staticbox.net CAP * ACK :multi-prefix -CAP CLEAR -:alfred.staticbox.net CAP * ACK :-multi-prefix -CAP REQ :multi-prefix -:alfred.staticbox.net CAP * ACK :multi-prefix -CAP LIST -:alfred.staticbox.net CAP * LIST :multi-prefix -CAP END -*/ - -/** Handle /CAP - */ -class CommandCAP : public Command +namespace Cap { - public: - LocalIntExt reghold; - CommandCAP (Module* mod) : Command(mod, "CAP", 1), - reghold("CAP_REGHOLD", mod) + class ManagerImpl; +} + +static Cap::ManagerImpl* managerimpl; + +class Cap::ManagerImpl : public Cap::Manager, public ReloadModule::EventListener +{ + /** Stores the cap state of a module being reloaded + */ + struct CapModData { - works_before_reg = true; + struct Data + { + std::string name; + std::vector users; + + Data(Capability* cap) + : name(cap->GetName()) + { + } + }; + std::vector caps; + }; + + typedef insp::flat_map CapMap; + + ExtItem capext; + CapMap caps; + Events::ModuleEventProvider& evprov; + + static bool CanRequest(LocalUser* user, Ext usercaps, Capability* cap, bool adding) + { + const bool hascap = ((usercaps & cap->GetMask()) != 0); + if (hascap == adding) + return true; + + return cap->OnRequest(user, adding); } - CmdResult Handle (const std::vector ¶meters, User *user) + Capability::Bit AllocateBit() const { - irc::string subcommand = parameters[0].c_str(); + Capability::Bit used = 0; + for (CapMap::const_iterator i = caps.begin(); i != caps.end(); ++i) + { + Capability* cap = i->second; + used |= cap->GetMask(); + } - if (subcommand == "REQ") + for (size_t i = 0; i < MAX_CAPS; i++) { - if (parameters.size() < 2) - return CMD_FAILURE; + Capability::Bit bit = (static_cast(1) << i); + if (!(used & bit)) + return bit; + } + throw ModuleException("Too many caps"); + } - CapEvent Data(creator, user, CapEvent::CAPEVENT_REQ); + void OnReloadModuleSave(Module* mod, ReloadModule::CustomData& cd) CXX11_OVERRIDE + { + ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "OnReloadModuleSave()"); + if (mod == creator) + return; + + CapModData* capmoddata = new CapModData; + cd.add(this, capmoddata); - // tokenize the input into a nice list of requested caps - std::string cap_; - irc::spacesepstream cap_stream(parameters[1]); + for (CapMap::iterator i = caps.begin(); i != caps.end(); ++i) + { + Capability* cap = i->second; + // Only save users of caps that belong to the module being reloaded + if (cap->creator != mod) + continue; - while (cap_stream.GetToken(cap_)) + ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Module being reloaded implements cap %s, saving cap users", cap->GetName().c_str()); + capmoddata->caps.push_back(CapModData::Data(cap)); + CapModData::Data& capdata = capmoddata->caps.back(); + + // Populate list with uuids of users who are using the cap + const UserManager::LocalList& list = ServerInstance->Users.GetLocalUsers(); + for (UserManager::LocalList::const_iterator j = list.begin(); j != list.end(); ++j) { - // Whilst the handling of extraneous spaces is not currently defined in the CAP specification - // every single other implementation ignores extraneous spaces. Lets copy them for - // compatibility purposes. - trim(cap_); - if (!cap_.empty()) - Data.wanted.push_back(cap_); + LocalUser* user = *j; + if (cap->get(user)) + capdata.users.push_back(user->uuid); } + } + } - reghold.set(user, 1); - Data.Send(); + void OnReloadModuleRestore(Module* mod, void* data) CXX11_OVERRIDE + { + CapModData* capmoddata = static_cast(data); + for (std::vector::const_iterator i = capmoddata->caps.begin(); i != capmoddata->caps.end(); ++i) + { + const CapModData::Data& capdata = *i; + Capability* cap = ManagerImpl::Find(capdata.name); + if (!cap) + { + ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Cap %s is no longer available after reload", capdata.name.c_str()); + continue; + } - if (Data.wanted.empty()) + // Set back the cap for all users who were using it before the reload + for (std::vector::const_iterator j = capdata.users.begin(); j != capdata.users.end(); ++j) { - user->WriteServ("CAP %s ACK :%s", user->nick.c_str(), parameters[1].c_str()); - return CMD_SUCCESS; + const std::string& uuid = *j; + User* user = ServerInstance->FindUUID(uuid); + if (!user) + { + ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "User %s is gone when trying to restore cap %s", uuid.c_str(), capdata.name.c_str()); + continue; + } + + cap->set(user, true); } + } + delete capmoddata; + } + + public: + ManagerImpl(Module* mod, Events::ModuleEventProvider& evprovref) + : Cap::Manager(mod) + , ReloadModule::EventListener(mod) + , capext(mod) + , evprov(evprovref) + { + managerimpl = this; + } + + ~ManagerImpl() + { + for (CapMap::iterator i = caps.begin(); i != caps.end(); ++i) + { + Capability* cap = i->second; + cap->Unregister(); + } + } + + void AddCap(Cap::Capability* cap) CXX11_OVERRIDE + { + // No-op if the cap is already registered. + // This allows modules to call SetActive() on a cap without checking if it's active first. + if (cap->IsRegistered()) + return; + + ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Registering cap %s", cap->GetName().c_str()); + cap->bit = AllocateBit(); + cap->extitem = &capext; + caps.insert(std::make_pair(cap->GetName(), cap)); + ServerInstance->Modules.AddReferent("cap/" + cap->GetName(), cap); + + FOREACH_MOD_CUSTOM(evprov, Cap::EventListener, OnCapAddDel, (cap, true)); + } + + void DelCap(Cap::Capability* cap) CXX11_OVERRIDE + { + // No-op if the cap is not registered, see AddCap() above + if (!cap->IsRegistered()) + return; + + ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Unregistering cap %s", cap->GetName().c_str()); + + // Fire the event first so modules can still see who is using the cap which is being unregistered + FOREACH_MOD_CUSTOM(evprov, Cap::EventListener, OnCapAddDel, (cap, false)); + + // Turn off the cap for all users + const UserManager::LocalList& list = ServerInstance->Users.GetLocalUsers(); + for (UserManager::LocalList::const_iterator i = list.begin(); i != list.end(); ++i) + { + LocalUser* user = *i; + cap->set(user, false); + } + + ServerInstance->Modules.DelReferent(cap); + cap->Unregister(); + caps.erase(cap->GetName()); + } + + Capability* Find(const std::string& capname) const CXX11_OVERRIDE + { + CapMap::const_iterator it = caps.find(capname); + if (it != caps.end()) + return it->second; + return NULL; + } - // HACK: reset all of the caps which were enabled on this user because a cap request is atomic. - for (std::vector >::iterator iter = Data.changed.begin(); iter != Data.changed.end(); ++iter) - iter->first->ext.set(user, iter->second); + void NotifyValueChange(Capability* cap) CXX11_OVERRIDE + { + ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Cap %s changed value", cap->GetName().c_str()); + FOREACH_MOD_CUSTOM(evprov, Cap::EventListener, OnCapValueChange, (cap)); + } - user->WriteServ("CAP %s NAK :%s", user->nick.c_str(), parameters[1].c_str()); + Protocol GetProtocol(LocalUser* user) const + { + return ((capext.get(user) & CAP_302_BIT) ? CAP_302 : CAP_LEGACY); + } + + void Set302Protocol(LocalUser* user) + { + capext.set(user, capext.get(user) | CAP_302_BIT); + } + + bool HandleReq(LocalUser* user, const std::string& reqlist) + { + Ext usercaps = capext.get(user); + irc::spacesepstream ss(reqlist); + for (std::string capname; ss.GetToken(capname); ) + { + bool remove = (capname[0] == '-'); + if (remove) + capname.erase(capname.begin()); + + Capability* cap = ManagerImpl::Find(capname); + if ((!cap) || (!CanRequest(user, usercaps, cap, !remove))) + return false; + + if (remove) + usercaps = cap->DelFromMask(usercaps); + else + usercaps = cap->AddToMask(usercaps); } - else if (subcommand == "END") + + capext.set(user, usercaps); + return true; + } + + void HandleList(std::vector& out, LocalUser* user, bool show_all, bool show_values, bool minus_prefix = false) const + { + Ext show_caps = (show_all ? ~0 : capext.get(user)); + + for (CapMap::const_iterator i = caps.begin(); i != caps.end(); ++i) { - reghold.set(user, 0); + Capability* cap = i->second; + if (!(show_caps & cap->GetMask())) + continue; + + if ((show_all) && (!cap->OnList(user))) + continue; + + std::string token; + if (minus_prefix) + token.push_back('-'); + token.append(cap->GetName()); + + if (show_values) + { + const std::string* capvalue = cap->GetValue(user); + if ((capvalue) && (!capvalue->empty()) && (capvalue->find(' ') == std::string::npos)) + { + token.push_back('='); + token.append(*capvalue, 0, MAX_VALUE_LENGTH); + } + } + out.push_back(token); } - else if ((subcommand == "LS") || (subcommand == "LIST")) + } + + void HandleClear(LocalUser* user, std::vector& result) + { + HandleList(result, user, false, false, true); + capext.unset(user); + } +}; + +namespace +{ + std::string SerializeCaps(const Extensible* container, void* item, bool human) + { + // XXX: Cast away the const because IS_LOCAL() doesn't handle it + LocalUser* user = IS_LOCAL(const_cast(static_cast(container))); + if (!user) + return std::string(); + + // List requested caps + std::vector result; + managerimpl->HandleList(result, user, false, false); + + // Serialize cap protocol version. If building a human-readable string append a + // new token, otherwise append only a single character indicating the version. + std::string version; + if (human) + version.append("capversion=3."); + switch (managerimpl->GetProtocol(user)) { - CapEvent Data(creator, user, subcommand == "LS" ? CapEvent::CAPEVENT_LS : CapEvent::CAPEVENT_LIST); + case Cap::CAP_302: + version.push_back('2'); + break; + default: + version.push_back('1'); + break; + } + result.push_back(version); + + return stdalgo::string::join(result, ' '); + } +} + +Cap::ExtItem::ExtItem(Module* mod) + : LocalIntExt("caps", ExtensionItem::EXT_USER, mod) +{ +} - reghold.set(user, 1); - Data.Send(); +std::string Cap::ExtItem::ToHuman(const Extensible* container, void* item) const +{ + return SerializeCaps(container, item, true); +} + +std::string Cap::ExtItem::ToInternal(const Extensible* container, void* item) const +{ + return SerializeCaps(container, item, false); +} + +void Cap::ExtItem::FromInternal(Extensible* container, const std::string& value) +{ + LocalUser* user = IS_LOCAL(static_cast(container)); + if (!user) + return; // Can't happen - std::string Result; - if (Data.wanted.size() > 0) - Result = irc::stringjoiner(" ", Data.wanted, 0, Data.wanted.size() - 1).GetJoined(); + // Process the cap protocol version which is a single character at the end of the serialized string + const char verchar = *value.rbegin(); + if (verchar == '2') + managerimpl->Set302Protocol(user); + + // Remove the version indicator from the string passed to HandleReq + std::string caplist(value, 0, value.size()-1); + managerimpl->HandleReq(user, caplist); +} + +class CapMessage : public Cap::MessageBase +{ + public: + CapMessage(LocalUser* user, const std::string& subcmd, const std::string& result, bool asterisk) + : Cap::MessageBase(subcmd) + { + SetUser(user); + if (asterisk) + PushParam("*"); + PushParamRef(result); + } +}; + +class CommandCap : public SplitCommand +{ + private: + Events::ModuleEventProvider evprov; + Cap::ManagerImpl manager; + ClientProtocol::EventProvider protoevprov; - user->WriteServ("CAP %s %s :%s", user->nick.c_str(), subcommand.c_str(), Result.c_str()); + void DisplayResult(LocalUser* user, const std::string& subcmd, std::vector result, bool asterisk) + { + size_t maxline = ServerInstance->Config->Limits.MaxLine - ServerInstance->Config->ServerName.size() - user->nick.length() - subcmd.length() - 11; + std::string line; + for (std::vector::const_iterator iter = result.begin(); iter != result.end(); ++iter) + { + if (line.length() + iter->length() < maxline) + { + line.append(*iter); + line.push_back(' '); + } + else + { + DisplaySingleResult(user, subcmd, line, asterisk); + line.clear(); + } } - else if (subcommand == "CLEAR") + DisplaySingleResult(user, subcmd, line, false); + } + + void DisplaySingleResult(LocalUser* user, const std::string& subcmd, const std::string& result, bool asterisk) + { + CapMessage msg(user, subcmd, result, asterisk); + ClientProtocol::Event ev(protoevprov, msg); + user->Send(ev); + } + + public: + LocalIntExt holdext; + + CommandCap(Module* mod) + : SplitCommand(mod, "CAP", 1) + , evprov(mod, "event/cap") + , manager(mod, evprov) + , protoevprov(mod, name) + , holdext("cap_hold", ExtensionItem::EXT_USER, mod) + { + works_before_reg = true; + } + + CmdResult HandleLocal(LocalUser* user, const Params& parameters) CXX11_OVERRIDE + { + if (user->registered != REG_ALL) + holdext.set(user, 1); + + const std::string& subcommand = parameters[0]; + if (irc::equals(subcommand, "REQ")) { - CapEvent Data(creator, user, CapEvent::CAPEVENT_CLEAR); + if (parameters.size() < 2) + return CMD_FAILURE; - reghold.set(user, 1); - Data.Send(); + const std::string replysubcmd = (manager.HandleReq(user, parameters[1]) ? "ACK" : "NAK"); + DisplaySingleResult(user, replysubcmd, parameters[1], false); + } + else if (irc::equals(subcommand, "END")) + { + holdext.unset(user); + } + else if (irc::equals(subcommand, "LS") || irc::equals(subcommand, "LIST")) + { + Cap::Protocol capversion = Cap::CAP_LEGACY; + const bool is_ls = (subcommand.length() == 2); + if ((is_ls) && (parameters.size() > 1)) + { + unsigned int version = ConvToNum(parameters[1]); + if (version >= 302) + { + capversion = Cap::CAP_302; + manager.Set302Protocol(user); + } + } - std::string Result; - if (!Data.ack.empty()) - Result = irc::stringjoiner(" ", Data.ack, 0, Data.ack.size() - 1).GetJoined(); - user->WriteServ("CAP %s ACK :%s", user->nick.c_str(), Result.c_str()); + std::vector result; + // Show values only if supports v3.2 and doing LS + manager.HandleList(result, user, is_ls, ((is_ls) && (capversion != Cap::CAP_LEGACY))); + DisplayResult(user, subcommand, result, (capversion != Cap::CAP_LEGACY)); + } + else if (irc::equals(subcommand, "CLEAR") && (manager.GetProtocol(user) == Cap::CAP_LEGACY)) + { + std::vector result; + manager.HandleClear(user, result); + DisplayResult(user, "ACK", result, false); } else { - user->WriteNumeric(ERR_INVALIDCAPSUBCOMMAND, "%s %s :Invalid CAP subcommand", user->nick.c_str(), subcommand.c_str()); + user->WriteNumeric(ERR_INVALIDCAPCMD, subcommand.empty() ? "*" : subcommand, "Invalid CAP subcommand"); return CMD_FAILURE; } @@ -128,42 +470,45 @@ class CommandCAP : public Command } }; -class ModuleCAP : public Module +class PoisonCap : public Cap::Capability { - CommandCAP cmd; public: - ModuleCAP() - : cmd(this) + PoisonCap(Module* mod) + : Cap::Capability(mod, "inspircd.org/poison") { } - void init() + bool OnRequest(LocalUser* user, bool adding) CXX11_OVERRIDE { - ServerInstance->Modules->AddService(cmd); - ServerInstance->Modules->AddService(cmd.reghold); - - Implementation eventlist[] = { I_OnCheckReady }; - ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation)); + // Reject the attempt to enable this capability. + return false; } +}; - ModResult OnCheckReady(LocalUser* user) - { - /* Users in CAP state get held until CAP END */ - if (cmd.reghold.get(user)) - return MOD_RES_DENY; +class ModuleCap : public Module +{ + private: + CommandCap cmd; + PoisonCap poisoncap; + Cap::Capability stdrplcap; - return MOD_RES_PASSTHRU; + public: + ModuleCap() + : cmd(this) + , poisoncap(this) + , stdrplcap(this, "inspircd.org/standard-replies") + { } - ~ModuleCAP() + ModResult OnCheckReady(LocalUser* user) CXX11_OVERRIDE { + return (cmd.holdext.get(user) ? MOD_RES_DENY : MOD_RES_PASSTHRU); } - Version GetVersion() + Version GetVersion() CXX11_OVERRIDE { - return Version("Client CAP extension support", VF_VENDOR); + return Version("Implements support for the IRCv3 Client Capability Negotiation extension.", VF_VENDOR); } }; -MODULE_INIT(ModuleCAP) - +MODULE_INIT(ModuleCap)