2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2013 Adam <Adam@anope.org>
5 * Copyright (C) 2003-2013 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");
83 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
148 * @return The query ID
150 virtual LDAPQuery BindAsManager(LDAPInterface *i) = 0;
153 * @param i The LDAPInterface the result is sent to
154 * @param who The binddn
155 * @param pass The password
156 * @return The query ID
158 virtual LDAPQuery Bind(LDAPInterface* i, const std::string& who, const std::string& pass) = 0;
160 /** Search ldap for the specified filter
161 * @param i The LDAPInterface the result is sent to
162 * @param base The base DN to search
163 * @param filter The filter to apply
164 * @return The query ID
166 virtual LDAPQuery Search(LDAPInterface* i, const std::string& base, const std::string& filter) = 0;
168 /** Add an entry to LDAP
169 * @param i The LDAPInterface the result is sent to
170 * @param dn The dn of the entry to add
171 * @param attributes The attributes
172 * @return The query ID
174 virtual LDAPQuery Add(LDAPInterface* i, const std::string& dn, LDAPMods& attributes) = 0;
176 /** Delete an entry from LDAP
177 * @param i The LDAPInterface the result is sent to
178 * @param dn The dn of the entry to delete
179 * @return The query ID
181 virtual LDAPQuery Del(LDAPInterface* i, const std::string& dn) = 0;
183 /** Modify an existing entry in LDAP
184 * @param i The LDAPInterface the result is sent to
185 * @param base The base DN to modify
186 * @param attributes The attributes to modify
187 * @return The query ID
189 virtual LDAPQuery Modify(LDAPInterface* i, const std::string& base, LDAPMods& attributes) = 0;
191 /** Compare an attribute in LDAP with our value
192 * @param i The LDAPInterface the result is sent to
193 * @param dn DN to use for comparing
194 * @param attr Attr of DN to compare with
195 * @param val value to compare attr of dn
196 * @return the query ID
198 virtual LDAPQuery Compare(LDAPInterface* i, const std::string& dn, const std::string& attr, const std::string& val) = 0;