anope

- supernets anope source code & configuration
git clone git://git.acid.vegas/anope.git
Log | Files | Refs | Archive | README

sasl.h (3561B)

      1 /*
      2  *
      3  * (C) 2014-2022 Anope Team
      4  * Contact us at team@anope.org
      5  *
      6  * Please read COPYING and README for further details.
      7  */
      8 
      9 namespace SASL
     10 {
     11 	struct Message
     12 	{
     13 		Anope::string source;
     14 		Anope::string target;
     15 		Anope::string type;
     16 		Anope::string data;
     17 		Anope::string ext;
     18 	};
     19 
     20 	class Mechanism;
     21 	struct Session;
     22 
     23 	class Service : public ::Service
     24 	{
     25 	 public:
     26 		Service(Module *o) : ::Service(o, "SASL::Service", "sasl") { }
     27 
     28 		virtual void ProcessMessage(const Message &) = 0;
     29 
     30 		virtual Anope::string GetAgent() = 0;
     31 
     32 		virtual Session* GetSession(const Anope::string &uid) = 0;
     33 
     34 		virtual void SendMessage(SASL::Session *session, const Anope::string &type, const Anope::string &data) = 0;
     35 
     36 		virtual void Succeed(Session *, NickCore *) = 0;
     37 		virtual void Fail(Session *) = 0;
     38 		virtual void SendMechs(Session *) = 0;
     39 		virtual void DeleteSessions(Mechanism *, bool = false) = 0;
     40 		virtual void RemoveSession(Session *) = 0;
     41 	};
     42 
     43 	static ServiceReference<SASL::Service> sasl("SASL::Service", "sasl");
     44 
     45 	struct Session
     46 	{
     47 		time_t created;
     48 		Anope::string uid;
     49 		Anope::string hostname, ip;
     50 		Reference<Mechanism> mech;
     51 
     52 		Session(Mechanism *m, const Anope::string &u) : created(Anope::CurTime), uid(u), mech(m) { }
     53 		virtual ~Session()
     54 		{
     55 			if (sasl)
     56 				sasl->RemoveSession(this);
     57 		}
     58 	};
     59 
     60 	/* PLAIN, EXTERNAL, etc */
     61 	class Mechanism : public ::Service
     62 	{
     63 	 public:
     64 		Mechanism(Module *o, const Anope::string &sname) : Service(o, "SASL::Mechanism", sname) { }
     65 
     66 		virtual Session* CreateSession(const Anope::string &uid) { return new Session(this, uid); }
     67 
     68 		virtual void ProcessMessage(Session *session, const Message &) = 0;
     69 
     70 		virtual ~Mechanism()
     71 		{
     72 			if (sasl)
     73 				sasl->DeleteSessions(this, true);
     74 		}
     75 	};
     76 
     77 	class IdentifyRequest : public ::IdentifyRequest
     78 	{
     79 		Anope::string uid;
     80 		Anope::string hostname, ip;
     81 
     82 	 public:
     83 		IdentifyRequest(Module *m, const Anope::string &id, const Anope::string &acc, const Anope::string &pass, const Anope::string &h, const Anope::string &i) : ::IdentifyRequest(m, acc, pass), uid(id), hostname(h), ip(i) { }
     84 
     85 		void OnSuccess() anope_override
     86 		{
     87 			if (!sasl)
     88 				return;
     89 
     90 			NickAlias *na = NickAlias::Find(GetAccount());
     91 			if (!na || na->nc->HasExt("NS_SUSPENDED") || na->nc->HasExt("UNCONFIRMED"))
     92 				return OnFail();
     93 
     94 			unsigned int maxlogins = Config->GetModule("ns_identify")->Get<unsigned int>("maxlogins");
     95 			if (maxlogins && na->nc->users.size() >= maxlogins)
     96 				return OnFail();
     97 
     98 			Session *s = sasl->GetSession(uid);
     99 			if (s)
    100 			{
    101 				Anope::string user = "A user";
    102 				if (!hostname.empty() && !ip.empty())
    103 					user = hostname + " (" + ip + ")";
    104 
    105 				Log(this->GetOwner(), "sasl", Config->GetClient("NickServ")) << user << " identified to account " << this->GetAccount() << " using SASL";
    106 				sasl->Succeed(s, na->nc);
    107 				delete s;
    108 			}
    109 		}
    110 
    111 		void OnFail() anope_override
    112 		{
    113 			if (!sasl)
    114 				return;
    115 
    116 			Session *s = sasl->GetSession(uid);
    117 			if (s)
    118 			{
    119 				sasl->Fail(s);
    120 				delete s;
    121 			}
    122 
    123 			Anope::string accountstatus;
    124 			NickAlias *na = NickAlias::Find(GetAccount());
    125 			if (!na)
    126 				accountstatus = "nonexistent ";
    127 			else if (na->nc->HasExt("NS_SUSPENDED"))
    128 				accountstatus = "suspended ";
    129 			else if (na->nc->HasExt("UNCONFIRMED"))
    130 				accountstatus = "unconfirmed ";
    131 
    132 			Anope::string user = "A user";
    133 			if (!hostname.empty() && !ip.empty())
    134 				user = hostname + " (" + ip + ")";
    135 
    136 			Log(this->GetOwner(), "sasl", Config->GetClient("NickServ")) << user << " failed to identify for " << accountstatus << "account " << this->GetAccount() << " using SASL";
    137 		}
    138 	};
    139 }