diff options
author | Daniel Vassdal <shutter@canternet.org> | 2014-02-13 09:29:39 -0800 |
---|---|---|
committer | Daniel Vassdal <shutter@canternet.org> | 2014-02-14 13:05:01 -0800 |
commit | 3c8009b4950a670618c3ee476f21f67a9468fdb2 (patch) | |
tree | fe5fc5472a5bf34c93ab91354c99c79c253d2a7d | |
parent | ca85df2d7475d42579c29d822f424d2c9713774b (diff) |
sasl.h, m_sasl.cpp: Add RemoveSession(), DeleteSessions(), and have active sessions closed when a Mechanism is unloaded
-rw-r--r-- | include/modules/sasl.h | 56 | ||||
-rw-r--r-- | modules/m_sasl.cpp | 70 | ||||
-rw-r--r-- | modules/protocol/charybdis.cpp | 4 | ||||
-rw-r--r-- | modules/protocol/inspircd12.cpp | 6 | ||||
-rw-r--r-- | modules/protocol/unreal.cpp | 4 |
5 files changed, 91 insertions, 49 deletions
diff --git a/include/modules/sasl.h b/include/modules/sasl.h index 836c26d19..f997ead76 100644 --- a/include/modules/sasl.h +++ b/include/modules/sasl.h @@ -18,6 +18,29 @@ namespace SASL }; class Mechanism; + struct Session; + + class Service : public ::Service + { + public: + Service(Module *o) : ::Service(o, "SASL::Service", "sasl") { } + + virtual void ProcessMessage(const Message &) = 0; + + virtual Anope::string GetAgent() = 0; + + virtual Session* GetSession(const Anope::string &uid) = 0; + + virtual void SendMessage(SASL::Session *session, const Anope::string &type, const Anope::string &data) = 0; + + virtual void Succeed(Session *, NickCore *) = 0; + virtual void Fail(Session *) = 0; + virtual void SendMechs(Session *) = 0; + virtual void DeleteSessions(Mechanism *, bool = false) = 0; + virtual void RemoveSession(Session *) = 0; + }; + + static ServiceReference<SASL::Service> sasl("SASL::Service", "sasl"); struct Session { @@ -26,11 +49,15 @@ namespace SASL Reference<Mechanism> mech; Session(Mechanism *m, const Anope::string &u) : created(Anope::CurTime), uid(u), mech(m) { } - virtual ~Session() { } + virtual ~Session() + { + if (sasl) + sasl->RemoveSession(this); + } }; /* PLAIN, EXTERNAL, etc */ - class Mechanism : public Service + class Mechanism : public ::Service { public: Mechanism(Module *o, const Anope::string &sname) : Service(o, "SASL::Mechanism", sname) { } @@ -38,26 +65,11 @@ namespace SASL virtual Session* CreateSession(const Anope::string &uid) { return new Session(this, uid); } virtual void ProcessMessage(Session *session, const Message &) = 0; - }; - - class Service : public ::Service - { - public: - Service(Module *o) : ::Service(o, "SASL::Service", "sasl") { } - - virtual void ProcessMessage(const Message &) = 0; - - virtual Anope::string GetAgent() = 0; - virtual Session* GetSession(const Anope::string &uid) = 0; - - virtual void SendMessage(SASL::Session *session, const Anope::string &type, const Anope::string &data) = 0; - - virtual void Succeed(Session *, NickCore *) = 0; - virtual void Fail(Session *) = 0; - virtual void SendMechs(Session *) = 0; + virtual ~Mechanism() + { + if (sasl) + sasl->DeleteSessions(this, true); + } }; } - -static ServiceReference<SASL::Service> sasl("SASL::Service", "sasl"); - diff --git a/modules/m_sasl.cpp b/modules/m_sasl.cpp index bdc0ef1a2..83ac85938 100644 --- a/modules/m_sasl.cpp +++ b/modules/m_sasl.cpp @@ -10,7 +10,9 @@ #include "modules/sasl.h" #include "modules/ns_cert.h" -class Plain : public SASL::Mechanism +using namespace SASL; + +class Plain : public Mechanism { class IdentifyRequest : public ::IdentifyRequest { @@ -28,9 +30,12 @@ class Plain : public SASL::Mechanism if (!na) return OnFail(); - SASL::Session *s = sasl->GetSession(uid); + Session *s = sasl->GetSession(uid); if (s) + { sasl->Succeed(s, na->nc); + delete s; + } } void OnFail() anope_override @@ -38,18 +43,21 @@ class Plain : public SASL::Mechanism if (!sasl) return; - SASL::Session *s = sasl->GetSession(uid); + Session *s = sasl->GetSession(uid); if (s) + { sasl->Fail(s); + delete s; + } Log(Config->GetClient("NickServ")) << "A user failed to identify for account " << this->GetAccount() << " using SASL"; } }; public: - Plain(Module *o) : SASL::Mechanism(o, "PLAIN") { } + Plain(Module *o) : Mechanism(o, "PLAIN") { } - void ProcessMessage(SASL::Session *sess, const SASL::Message &m) anope_override + void ProcessMessage(Session *sess, const SASL::Message &m) anope_override { if (m.type == "S") { @@ -82,7 +90,7 @@ class Plain : public SASL::Mechanism } }; -class External : public SASL::Mechanism +class External : public Mechanism { struct Session : SASL::Session { @@ -92,13 +100,13 @@ class External : public SASL::Mechanism }; public: - External(Module *o) : SASL::Mechanism(o, "EXTERNAL") + External(Module *o) : Mechanism(o, "EXTERNAL") { if (!IRCD || !IRCD->CanCertFP) throw ModuleException("No CertFP"); } - SASL::Session* CreateSession(const Anope::string &uid) anope_override + Session* CreateSession(const Anope::string &uid) anope_override { return new Session(this, uid); } @@ -122,6 +130,7 @@ class External : public SASL::Mechanism if (!na) { sasl->Fail(sess); + delete sess; return; } @@ -129,10 +138,12 @@ class External : public SASL::Mechanism if (cl == NULL || !cl->FindCert(mysess->cert)) { sasl->Fail(sess); + delete sess; return; } sasl->Succeed(sess, na->nc); + delete sess; } } }; @@ -146,7 +157,7 @@ class SASLService : public SASL::Service, public Timer ~SASLService() { - for (std::map<Anope::string, SASL::Session *>::iterator it = sessions.begin(); it != sessions.end();) + for (std::map<Anope::string, Session *>::iterator it = sessions.begin(); it != sessions.end(); it++) delete it->second; } @@ -163,14 +174,14 @@ class SASLService : public SASL::Service, public Timer } } - SASL::Session* &session = sessions[m.source]; + Session* &session = sessions[m.source]; if (m.type == "S") { - ServiceReference<SASL::Mechanism> mech("SASL::Mechanism", m.data); + ServiceReference<Mechanism> mech("SASL::Mechanism", m.data); if (!mech) { - SASL::Session tmp(NULL, m.source); + Session tmp(NULL, m.source); sasl->SendMechs(&tmp); sasl->Fail(&tmp); @@ -200,15 +211,34 @@ class SASLService : public SASL::Service, public Timer return agent; } - SASL::Session* GetSession(const Anope::string &uid) anope_override + Session* GetSession(const Anope::string &uid) anope_override { - std::map<Anope::string, SASL::Session *>::iterator it = sessions.find(uid); + std::map<Anope::string, Session *>::iterator it = sessions.find(uid); if (it != sessions.end()) return it->second; return NULL; } - void SendMessage(SASL::Session *session, const Anope::string &mtype, const Anope::string &data) anope_override + void RemoveSession(Session *sess) anope_override + { + sessions.erase(sess->uid); + } + + void DeleteSessions(Mechanism *mech, bool da) anope_override + { + for (std::map<Anope::string, Session *>::iterator it = sessions.begin(); it != sessions.end();) + { + std::map<Anope::string, Session *>::iterator del = it++; + if (*del->second->mech == mech) + { + if (da) + this->SendMessage(del->second, "D", "A"); + delete del->second; + } + } + } + + void SendMessage(Session *session, const Anope::string &mtype, const Anope::string &data) anope_override { SASL::Message msg; msg.source = this->GetAgent(); @@ -219,18 +249,18 @@ class SASLService : public SASL::Service, public Timer IRCD->SendSASLMessage(msg); } - void Succeed(SASL::Session *session, NickCore *nc) anope_override + void Succeed(Session *session, NickCore *nc) anope_override { IRCD->SendSVSLogin(session->uid, nc->display); this->SendMessage(session, "D", "S"); } - void Fail(SASL::Session *session) anope_override + void Fail(Session *session) anope_override { this->SendMessage(session, "D", "F"); } - void SendMechs(SASL::Session *session) anope_override + void SendMechs(Session *session) anope_override { std::vector<Anope::string> mechs = Service::GetServiceKeys("SASL::Mechanism"); Anope::string buf; @@ -242,10 +272,10 @@ class SASLService : public SASL::Service, public Timer void Tick(time_t) anope_override { - for (std::map<Anope::string, SASL::Session *>::iterator it = sessions.begin(); it != sessions.end();) + for (std::map<Anope::string, Session *>::iterator it = sessions.begin(); it != sessions.end();) { Anope::string key = it->first; - SASL::Session *s = it->second; + Session *s = it->second; ++it; if (!s || !s->mech || s->created + 60 < Anope::CurTime) diff --git a/modules/protocol/charybdis.cpp b/modules/protocol/charybdis.cpp index e9ea89673..a0c10be83 100644 --- a/modules/protocol/charybdis.cpp +++ b/modules/protocol/charybdis.cpp @@ -188,7 +188,7 @@ struct IRCDMessageEncap : IRCDMessage * * Charybdis only accepts messages from SASL agents; these must have umode +S */ - if (params[1] == "SASL" && sasl && params.size() >= 6) + if (params[1] == "SASL" && SASL::sasl && params.size() >= 6) { SASL::Message m; m.source = params[2]; @@ -197,7 +197,7 @@ struct IRCDMessageEncap : IRCDMessage m.data = params[5]; m.ext = params.size() > 6 ? params[6] : ""; - sasl->ProcessMessage(m); + SASL::sasl->ProcessMessage(m); } } }; diff --git a/modules/protocol/inspircd12.cpp b/modules/protocol/inspircd12.cpp index e64079dca..eded20306 100644 --- a/modules/protocol/inspircd12.cpp +++ b/modules/protocol/inspircd12.cpp @@ -873,7 +873,7 @@ struct IRCDMessageEncap : IRCDMessage if (Anope::Match(Me->GetSID(), params[0]) == false) return; - if (sasl && params[1] == "SASL" && params.size() >= 6) + if (SASL::sasl && params[1] == "SASL" && params.size() >= 6) { SASL::Message m; m.source = params[2]; @@ -882,7 +882,7 @@ struct IRCDMessageEncap : IRCDMessage m.data = params[5]; m.ext = params.size() > 6 ? params[6] : ""; - sasl->ProcessMessage(m); + SASL::sasl->ProcessMessage(m); } } }; @@ -1283,7 +1283,7 @@ struct IRCDMessageUID : IRCDMessage modes += " " + params[i]; NickAlias *na = NULL; - if (sasl) + if (SASL::sasl) for (std::list<SASLUser>::iterator it = saslusers.begin(); it != saslusers.end();) { SASLUser &u = *it; diff --git a/modules/protocol/unreal.cpp b/modules/protocol/unreal.cpp index e0f051aa6..f0d01ce9d 100644 --- a/modules/protocol/unreal.cpp +++ b/modules/protocol/unreal.cpp @@ -889,7 +889,7 @@ struct IRCDMessageSASL : IRCDMessage void Run(MessageSource &source, const std::vector<Anope::string> ¶ms) anope_override { size_t p = params[1].find('!'); - if (!sasl || p == Anope::string::npos) + if (!SASL::sasl || p == Anope::string::npos) return; SASL::Message m; @@ -899,7 +899,7 @@ struct IRCDMessageSASL : IRCDMessage m.data = params[3]; m.ext = params.size() > 4 ? params[4] : ""; - sasl->ProcessMessage(m); + SASL::sasl->ProcessMessage(m); } }; |