2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2013, 2015 Adam <Adam@anope.org>
6 * This file is part of InspIRCd. InspIRCd is free software: you can
7 * redistribute it and/or modify it under the terms of the GNU General Public
8 * License as published by the Free Software Foundation, version 2.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 typedef int LDAPQuery;
23 class LDAPException : public ModuleException
26 LDAPException(const std::string& reason) : ModuleException(reason) { }
28 virtual ~LDAPException() throw() { }
31 struct LDAPModification
42 std::vector<std::string> values;
45 typedef std::vector<LDAPModification> LDAPMods;
47 struct LDAPAttributes : public std::map<std::string, std::vector<std::string> >
49 size_t size(const std::string& attr) const
51 const std::vector<std::string>& array = this->getArray(attr);
55 const std::vector<std::string> keys() const
57 std::vector<std::string> k;
58 for (const_iterator it = this->begin(), it_end = this->end(); it != it_end; ++it)
59 k.push_back(it->first);
63 const std::string& get(const std::string& attr) const
65 const std::vector<std::string>& array = this->getArray(attr);
67 throw LDAPException("Empty attribute " + attr + " in LDAPResult::get");
71 const std::vector<std::string>& getArray(const std::string& attr) const
73 const_iterator it = this->find(attr);
74 if (it == this->end())
75 throw LDAPException("Unknown attribute " + attr + " in LDAPResult::getArray");
93 std::vector<LDAPAttributes> messages;
100 : type(QUERY_UNKNOWN), id(-1)
106 return this->messages.size();
111 return this->messages.empty();
114 const LDAPAttributes& get(size_t sz) const
116 if (sz >= this->messages.size())
117 throw LDAPException("Index out of range");
118 return this->messages[sz];
121 const std::string& getError() const
132 LDAPInterface(Module* m) : creator(m) { }
133 virtual ~LDAPInterface() { }
135 virtual void OnResult(const LDAPResult& r) = 0;
136 virtual void OnError(const LDAPResult& err) = 0;
139 class LDAPProvider : public DataProvider
142 LDAPProvider(Module* Creator, const std::string& Name)
143 : DataProvider(Creator, Name) { }
145 /** Attempt to bind to the LDAP server as a manager
146 * @param i The LDAPInterface the result is sent to
148 virtual void BindAsManager(LDAPInterface* i) = 0;
151 * @param i The LDAPInterface the result is sent to
152 * @param who The binddn
153 * @param pass The password
155 virtual void Bind(LDAPInterface* i, const std::string& who, const std::string& pass) = 0;
157 /** Search ldap for the specified filter
158 * @param i The LDAPInterface the result is sent to
159 * @param base The base DN to search
160 * @param filter The filter to apply
162 virtual void Search(LDAPInterface* i, const std::string& base, const std::string& filter) = 0;
164 /** Add an entry to LDAP
165 * @param i The LDAPInterface the result is sent to
166 * @param dn The dn of the entry to add
167 * @param attributes The attributes
169 virtual void Add(LDAPInterface* i, const std::string& dn, LDAPMods& attributes) = 0;
171 /** Delete an entry from LDAP
172 * @param i The LDAPInterface the result is sent to
173 * @param dn The dn of the entry to delete
175 virtual void Del(LDAPInterface* i, const std::string& dn) = 0;
177 /** Modify an existing entry in LDAP
178 * @param i The LDAPInterface the result is sent to
179 * @param base The base DN to modify
180 * @param attributes The attributes to modify
182 virtual void Modify(LDAPInterface* i, const std::string& base, LDAPMods& attributes) = 0;
184 /** Compare an attribute in LDAP with our value
185 * @param i The LDAPInterface the result is sent to
186 * @param dn DN to use for comparing
187 * @param attr Attr of DN to compare with
188 * @param val value to compare attr of dn
190 virtual void Compare(LDAPInterface* i, const std::string& dn, const std::string& attr, const std::string& val) = 0;