2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2015 Adam <Adam@anope.org>
5 * Copyright (C) 2003-2015 Anope Team <team@anope.org>
7 * This file is part of InspIRCd. InspIRCd is free software: you can
8 * redistribute it and/or modify it under the terms of the GNU General Public
9 * License as published by the Free Software Foundation, version 2.
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 typedef int LDAPQuery;
24 class LDAPException : public ModuleException
27 LDAPException(const std::string& reason) : ModuleException(reason) { }
29 virtual ~LDAPException() throw() { }
32 struct LDAPModification
43 std::vector<std::string> values;
46 typedef std::vector<LDAPModification> LDAPMods;
48 struct LDAPAttributes : public std::map<std::string, std::vector<std::string> >
50 size_t size(const std::string& attr) const
52 const std::vector<std::string>& array = this->getArray(attr);
56 const std::vector<std::string> keys() const
58 std::vector<std::string> k;
59 for (const_iterator it = this->begin(), it_end = this->end(); it != it_end; ++it)
60 k.push_back(it->first);
64 const std::string& get(const std::string& attr) const
66 const std::vector<std::string>& array = this->getArray(attr);
68 throw LDAPException("Empty attribute " + attr + " in LDAPResult::get");
72 const std::vector<std::string>& getArray(const std::string& attr) const
74 const_iterator it = this->find(attr);
75 if (it == this->end())
76 throw LDAPException("Unknown attribute " + attr + " in LDAPResult::getArray");
94 std::vector<LDAPAttributes> messages;
101 : type(QUERY_UNKNOWN), id(-1)
107 return this->messages.size();
112 return this->messages.empty();
115 const LDAPAttributes& get(size_t sz) const
117 if (sz >= this->messages.size())
118 throw LDAPException("Index out of range");
119 return this->messages[sz];
122 const std::string& getError() const
133 LDAPInterface(Module* m) : creator(m) { }
134 virtual ~LDAPInterface() { }
136 virtual void OnResult(const LDAPResult& r) = 0;
137 virtual void OnError(const LDAPResult& err) = 0;
140 class LDAPProvider : public DataProvider
143 LDAPProvider(Module* Creator, const std::string& Name)
144 : DataProvider(Creator, Name) { }
146 /** Attempt to bind to the LDAP server as a manager
147 * @param i The LDAPInterface the result is sent to
149 virtual void BindAsManager(LDAPInterface* i) = 0;
152 * @param i The LDAPInterface the result is sent to
153 * @param who The binddn
154 * @param pass The password
156 virtual void Bind(LDAPInterface* i, const std::string& who, const std::string& pass) = 0;
158 /** Search ldap for the specified filter
159 * @param i The LDAPInterface the result is sent to
160 * @param base The base DN to search
161 * @param filter The filter to apply
163 virtual void Search(LDAPInterface* i, const std::string& base, const std::string& filter) = 0;
165 /** Add an entry to LDAP
166 * @param i The LDAPInterface the result is sent to
167 * @param dn The dn of the entry to add
168 * @param attributes The attributes
170 virtual void Add(LDAPInterface* i, const std::string& dn, LDAPMods& attributes) = 0;
172 /** Delete an entry from LDAP
173 * @param i The LDAPInterface the result is sent to
174 * @param dn The dn of the entry to delete
176 virtual void Del(LDAPInterface* i, const std::string& dn) = 0;
178 /** Modify an existing entry in LDAP
179 * @param i The LDAPInterface the result is sent to
180 * @param base The base DN to modify
181 * @param attributes The attributes to modify
183 virtual void Modify(LDAPInterface* i, const std::string& base, LDAPMods& attributes) = 0;
185 /** Compare an attribute in LDAP with our value
186 * @param i The LDAPInterface the result is sent to
187 * @param dn DN to use for comparing
188 * @param attr Attr of DN to compare with
189 * @param val value to compare attr of dn
191 virtual void Compare(LDAPInterface* i, const std::string& dn, const std::string& attr, const std::string& val) = 0;