anope

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

m_ldap_oper.cpp (3276B)

      1 /*
      2  *
      3  * (C) 2011-2022 Anope Team
      4  * Contact us at team@anope.org
      5  *
      6  * Please read COPYING and README for further details.
      7  */
      8 
      9 #include "module.h"
     10 #include "modules/ldap.h"
     11 
     12 static std::set<Oper *> my_opers;
     13 static Anope::string opertype_attribute;
     14 
     15 class IdentifyInterface : public LDAPInterface
     16 {
     17 	Reference<User> u;
     18 
     19  public:
     20 	IdentifyInterface(Module *m, User *user) : LDAPInterface(m), u(user)
     21 	{
     22 	}
     23 
     24 	void OnResult(const LDAPResult &r) anope_override
     25 	{
     26 		if (!u || !u->Account())
     27 			return;
     28 
     29 		NickCore *nc = u->Account();
     30 
     31 		try
     32 		{
     33 			const LDAPAttributes &attr = r.get(0);
     34 
     35 			const Anope::string &opertype = attr.get(opertype_attribute);
     36 
     37 			OperType *ot = OperType::Find(opertype);
     38 			if (ot != NULL && (nc->o == NULL || ot != nc->o->ot))
     39 			{
     40 				Oper *o = nc->o;
     41 				if (o != NULL && my_opers.count(o) > 0)
     42 				{
     43 					my_opers.erase(o);
     44 					delete o;
     45 				}
     46 				o = new Oper(u->nick, ot);
     47 				my_opers.insert(o);
     48 				nc->o = o;
     49 				Log(this->owner) << "Tied " << u->nick << " (" << nc->display << ") to opertype " << ot->GetName();
     50 			}
     51 		}
     52 		catch (const LDAPException &ex)
     53 		{
     54 			if (nc->o != NULL)
     55 			{
     56 				if (my_opers.count(nc->o) > 0)
     57 				{
     58 					my_opers.erase(nc->o);
     59 					delete nc->o;
     60 				}
     61 				nc->o = NULL;
     62 
     63 				Log(this->owner) << "Removed services operator from " << u->nick << " (" << nc->display << ")";
     64 			}
     65 		}
     66 	}
     67 
     68 	void OnError(const LDAPResult &r) anope_override
     69 	{
     70 	}
     71 
     72 	void OnDelete() anope_override
     73 	{
     74 		delete this;
     75 	}
     76 };
     77 
     78 class LDAPOper : public Module
     79 {
     80 	ServiceReference<LDAPProvider> ldap;
     81 
     82 	Anope::string binddn;
     83 	Anope::string password;
     84 	Anope::string basedn;
     85 	Anope::string filter;
     86  public:
     87 	LDAPOper(const Anope::string &modname, const Anope::string &creator) :
     88 		Module(modname, creator, EXTRA | VENDOR), ldap("LDAPProvider", "ldap/main")
     89 	{
     90 
     91 	}
     92 
     93 	void OnReload(Configuration::Conf *conf) anope_override
     94 	{
     95 		Configuration::Block *config = Config->GetModule(this);
     96 
     97 		this->binddn = config->Get<const Anope::string>("binddn");
     98 		this->password = config->Get<const Anope::string>("password");
     99 		this->basedn = config->Get<const Anope::string>("basedn");
    100 		this->filter = config->Get<const Anope::string>("filter");
    101 		opertype_attribute = config->Get<const Anope::string>("opertype_attribute");
    102 
    103 		for (std::set<Oper *>::iterator it = my_opers.begin(), it_end = my_opers.end(); it != it_end; ++it)
    104 			delete *it;
    105 		my_opers.clear();
    106 	}
    107 
    108 	void OnNickIdentify(User *u) anope_override
    109 	{
    110 		try
    111 		{
    112 			if (!this->ldap)
    113 				throw LDAPException("No LDAP interface. Is m_ldap loaded and configured correctly?");
    114 			else if (this->basedn.empty() || this->filter.empty() || opertype_attribute.empty())
    115 				throw LDAPException("Could not search LDAP for opertype settings, invalid configuration.");
    116 
    117 			if (!this->binddn.empty())
    118 				this->ldap->Bind(NULL, this->binddn.replace_all_cs("%a", u->Account()->display), this->password.c_str());
    119 			this->ldap->Search(new IdentifyInterface(this, u), this->basedn, this->filter.replace_all_cs("%a", u->Account()->display));
    120 		}
    121 		catch (const LDAPException &ex)
    122 		{
    123 			Log() << ex.GetReason();
    124 		}
    125 	}
    126 
    127 	void OnDelCore(NickCore *nc) anope_override
    128 	{
    129 		if (nc->o != NULL && my_opers.count(nc->o) > 0)
    130 		{
    131 			my_opers.erase(nc->o);
    132 			delete nc->o;
    133 			nc->o = NULL;
    134 		}
    135 	}
    136 };
    137 
    138 MODULE_INIT(LDAPOper)