]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/modules/ldap.h
Merge branch 'master+whoiscontext'
[user/henk/code/inspircd.git] / include / modules / ldap.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2013 Adam <Adam@anope.org>
5  *   Copyright (C) 2003-2013 Anope Team <team@anope.org>
6  *
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.
10  *
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
14  * details.
15  *
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/>.
18  */
19
20 #pragma once
21
22 typedef int LDAPQuery;
23
24 class LDAPException : public ModuleException
25 {
26  public:
27         LDAPException(const std::string& reason) : ModuleException(reason) { }
28
29         virtual ~LDAPException() throw() { }
30 };
31
32 struct LDAPModification
33 {
34         enum LDAPOperation
35         {
36                 LDAP_ADD,
37                 LDAP_DEL,
38                 LDAP_REPLACE
39         };
40
41         LDAPOperation op;
42         std::string name;
43         std::vector<std::string> values;
44 };
45
46 typedef std::vector<LDAPModification> LDAPMods;
47
48 struct LDAPAttributes : public std::map<std::string, std::vector<std::string> >
49 {
50         size_t size(const std::string& attr) const
51         {
52                 const std::vector<std::string>& array = this->getArray(attr);
53                 return array.size();
54         }
55
56         const std::vector<std::string> keys() const
57         {
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);
61                 return k;
62         }
63
64         const std::string& get(const std::string& attr) const
65         {
66                 const std::vector<std::string>& array = this->getArray(attr);
67                 if (array.empty())
68                         throw LDAPException("Empty attribute " + attr + " in LDAPResult::get");
69                 return array[0];
70         }
71
72         const std::vector<std::string>& getArray(const std::string& attr) const
73         {
74                 const_iterator it = this->find(attr);
75                 if (it == this->end())
76                         throw LDAPException("Unknown attribute " + attr + " in LDAPResult::getArray");
77                 return it->second;
78         }
79 };
80
81 struct LDAPResult
82 {
83         std::vector<LDAPAttributes> messages;
84         std::string error;
85
86         enum QueryType
87         {
88                 QUERY_UNKNOWN,
89                 QUERY_BIND,
90                 QUERY_SEARCH,
91                 QUERY_ADD,
92                 QUERY_DELETE,
93                 QUERY_MODIFY,
94                 QUERY_COMPARE
95         };
96
97         QueryType type;
98         LDAPQuery id;
99
100         LDAPResult()
101                 : type(QUERY_UNKNOWN), id(-1)
102         {
103         }
104
105         size_t size() const
106         {
107                 return this->messages.size();
108         }
109
110         bool empty() const
111         {
112                 return this->messages.empty();
113         }
114
115         const LDAPAttributes& get(size_t sz) const
116         {
117                 if (sz >= this->messages.size())
118                         throw LDAPException("Index out of range");
119                 return this->messages[sz];
120         }
121
122         const std::string& getError() const
123         {
124                 return this->error;
125         }
126 };
127
128 class LDAPInterface
129 {
130  public:
131         ModuleRef creator;
132
133         LDAPInterface(Module* m) : creator(m) { }
134         virtual ~LDAPInterface() { }
135
136         virtual void OnResult(const LDAPResult& r) = 0;
137         virtual void OnError(const LDAPResult& err) = 0;
138 };
139
140 class LDAPProvider : public DataProvider
141 {
142  public:
143         LDAPProvider(Module* Creator, const std::string& Name)
144                 : DataProvider(Creator, Name) { }
145
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
149          */
150         virtual LDAPQuery BindAsManager(LDAPInterface *i) = 0;
151
152         /** Bind to LDAP
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
157          */
158         virtual LDAPQuery Bind(LDAPInterface* i, const std::string& who, const std::string& pass) = 0;
159
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
165          */
166         virtual LDAPQuery Search(LDAPInterface* i, const std::string& base, const std::string& filter) = 0;
167
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
173          */
174         virtual LDAPQuery Add(LDAPInterface* i, const std::string& dn, LDAPMods& attributes) = 0;
175
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
180          */
181         virtual LDAPQuery Del(LDAPInterface* i, const std::string& dn) = 0;
182
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
188          */
189         virtual LDAPQuery Modify(LDAPInterface* i, const std::string& base, LDAPMods& attributes) = 0;
190
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
197          */
198         virtual LDAPQuery Compare(LDAPInterface* i, const std::string& dn, const std::string& attr, const std::string& val) = 0;
199 };