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