anope

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

ldap.h (3915B)

      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 #ifndef ANOPE_LDAP_H
     10 #define ANOPE_LDAP_H
     11 
     12 class LDAPException : public ModuleException
     13 {
     14  public:
     15 	LDAPException(const Anope::string &reason) : ModuleException(reason) { }
     16 
     17 	virtual ~LDAPException() throw() { }
     18 };
     19 
     20 struct LDAPModification
     21 {
     22 	enum LDAPOperation
     23 	{
     24 		LDAP_ADD,
     25 		LDAP_DEL,
     26 		LDAP_REPLACE
     27 	};
     28 
     29 	LDAPOperation op;
     30 	Anope::string name;
     31 	std::vector<Anope::string> values;
     32 };
     33 typedef std::vector<LDAPModification> LDAPMods;
     34 
     35 struct LDAPAttributes : public std::map<Anope::string, std::vector<Anope::string> >
     36 {
     37 	size_t size(const Anope::string &attr) const
     38 	{
     39 		const std::vector<Anope::string>& array = this->getArray(attr);
     40 		return array.size();
     41 	}
     42 
     43 	const std::vector<Anope::string> keys() const
     44 	{
     45 		std::vector<Anope::string> k;
     46 		for (const_iterator it = this->begin(), it_end = this->end(); it != it_end; ++it)
     47 			k.push_back(it->first);
     48 		return k;
     49 	}
     50 
     51 	const Anope::string &get(const Anope::string &attr) const
     52 	{
     53 		const std::vector<Anope::string>& array = this->getArray(attr);
     54 		if (array.empty())
     55 			throw LDAPException("Empty attribute " + attr + " in LDAPResult::get");
     56 		return array[0];
     57 	}
     58 
     59 	const std::vector<Anope::string>& getArray(const Anope::string &attr) const
     60 	{
     61 		const_iterator it = this->find(attr);
     62 		if (it == this->end())
     63 			throw LDAPException("Unknown attribute " + attr + " in LDAPResult::getArray");
     64 		return it->second;
     65 	}
     66 };
     67 
     68 enum QueryType
     69 {
     70 	QUERY_UNKNOWN,
     71 	QUERY_BIND,
     72 	QUERY_SEARCH,
     73 	QUERY_ADD,
     74 	QUERY_DELETE,
     75 	QUERY_MODIFY
     76 };
     77 
     78 struct LDAPResult
     79 {
     80 	std::vector<LDAPAttributes> messages;
     81 	Anope::string error;
     82 
     83 	QueryType type;
     84 
     85 	LDAPResult()
     86 	{
     87 		this->type = QUERY_UNKNOWN;
     88 	}
     89 
     90 	size_t size() const
     91 	{
     92 		return this->messages.size();
     93 	}
     94 
     95 	bool empty() const
     96 	{
     97 		return this->messages.empty();
     98 	}
     99 
    100 	const LDAPAttributes &get(size_t sz) const
    101 	{
    102 		if (sz >= this->messages.size())
    103 			throw LDAPException("Index out of range");
    104 		return this->messages[sz];
    105 	}
    106 
    107 	const Anope::string &getError() const
    108 	{
    109 		return this->error;
    110 	}
    111 };
    112 
    113 class LDAPInterface
    114 {
    115  public:
    116 	Module *owner;
    117 
    118 	LDAPInterface(Module *m) : owner(m) { }
    119 	virtual ~LDAPInterface() { }
    120 
    121 	virtual void OnResult(const LDAPResult &r) = 0;
    122 	virtual void OnError(const LDAPResult &err) = 0;
    123 	virtual void OnDelete() { }
    124 };
    125 
    126 class LDAPProvider : public Service
    127 {
    128  public:
    129 	LDAPProvider(Module *c, const Anope::string &n) : Service(c, "LDAPProvider", n) { }
    130 
    131 	/** Attempt to bind to the LDAP server as an admin
    132 	 * @param i The LDAPInterface the result is sent to
    133 	 */
    134 	virtual void BindAsAdmin(LDAPInterface *i) = 0;
    135 
    136 	/** Bind to LDAP
    137 	 * @param i The LDAPInterface the result is sent to
    138 	 * @param who The binddn
    139 	 * @param pass The password
    140 	 */
    141 	virtual void Bind(LDAPInterface *i, const Anope::string &who, const Anope::string &pass) = 0;
    142 
    143 	/** Search ldap for the specified filter
    144 	 * @param i The LDAPInterface the result is sent to
    145 	 * @param base The base DN to search
    146 	 * @param filter The filter to apply
    147 	 */
    148 	virtual void Search(LDAPInterface *i, const Anope::string &base, const Anope::string &filter) = 0;
    149 
    150 	/** Add an entry to LDAP
    151 	 * @param i The LDAPInterface the result is sent to
    152 	 * @param dn The dn of the entry to add
    153 	 * @param attributes The attributes
    154 	 */
    155 	virtual void Add(LDAPInterface *i, const Anope::string &dn, LDAPMods &attributes) = 0;
    156 
    157 	/** Delete an entry from LDAP
    158 	 * @param i The LDAPInterface the result is sent to
    159 	 * @param dn The dn of the entry to delete
    160 	 */
    161 	virtual void Del(LDAPInterface *i, const Anope::string &dn) = 0;
    162 
    163 	/** Modify an existing entry in LDAP
    164 	 * @param i The LDAPInterface the result is sent to
    165 	 * @param base The base DN to modify
    166 	 * @param attributes The attributes to modify
    167 	 */
    168 	virtual void Modify(LDAPInterface *i, const Anope::string &base, LDAPMods &attributes) = 0;
    169 };
    170 
    171 #endif // ANOPE_LDAP_H