]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/modules/ldap.h
Merge pull request #1302 from Adam-/master+txt
[user/henk/code/inspircd.git] / include / modules / ldap.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2015 Adam <Adam@anope.org>
5  *   Copyright (C) 2003-2015 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 enum QueryType
82 {
83         QUERY_UNKNOWN,
84         QUERY_BIND,
85         QUERY_SEARCH,
86         QUERY_ADD,
87         QUERY_DELETE,
88         QUERY_MODIFY,
89         QUERY_COMPARE
90 };
91
92 struct LDAPResult
93 {
94         std::vector<LDAPAttributes> messages;
95         std::string error;
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          */
149         virtual void BindAsManager(LDAPInterface* i) = 0;
150
151         /** Bind to LDAP
152          * @param i The LDAPInterface the result is sent to
153          * @param who The binddn
154          * @param pass The password
155          */
156         virtual void Bind(LDAPInterface* i, const std::string& who, const std::string& pass) = 0;
157
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
162          */
163         virtual void Search(LDAPInterface* i, const std::string& base, const std::string& filter) = 0;
164
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
169          */
170         virtual void Add(LDAPInterface* i, const std::string& dn, LDAPMods& attributes) = 0;
171
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
175          */
176         virtual void Del(LDAPInterface* i, const std::string& dn) = 0;
177
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
182          */
183         virtual void Modify(LDAPInterface* i, const std::string& base, LDAPMods& attributes) = 0;
184
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
190          */
191         virtual void Compare(LDAPInterface* i, const std::string& dn, const std::string& attr, const std::string& val) = 0;
192 };