]> git.ipfire.org Git - thirdparty/pdns.git/blob - modules/ldapbackend/powerldap.hh
clang-tidy: modernize deprecated header invarious places
[thirdparty/pdns.git] / modules / ldapbackend / powerldap.hh
1 /*
2 * This file is part of PowerDNS or dnsdist.
3 * Copyright -- PowerDNS.COM B.V. and its contributors
4 * originally authored by Norbert Sendetzky
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of version 2 of the GNU General Public License as
8 * published by the Free Software Foundation.
9 *
10 * In addition, for the avoidance of any doubt, permission is granted to
11 * link this program with OpenSSL and to (re)distribute the binaries
12 * produced as the result of such linking.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 */
23 #pragma once
24
25 #include <list>
26 #include <map>
27 #include <memory>
28 #include <string>
29 #include <vector>
30 #include <stdexcept>
31 #include <inttypes.h>
32 #include <cerrno>
33 #include <lber.h>
34 #include <ldap.h>
35
36 using std::list;
37 using std::map;
38 using std::string;
39 using std::vector;
40
41 class LdapAuthenticator;
42
43 class PowerLDAP
44 {
45 LDAP* d_ld;
46 string d_hosts;
47 int d_port;
48 bool d_tls;
49 int d_timeout;
50
51 const string getError(int rc = -1);
52 int waitResult(int msgid = LDAP_RES_ANY, LDAPMessage** result = NULL);
53 void ensureConnect();
54
55 public:
56 typedef map<string, vector<string>> sentry_t;
57 typedef vector<sentry_t> sresult_t;
58
59 class SearchResult
60 {
61 LDAP* d_ld;
62 int d_msgid;
63 bool d_finished;
64
65 SearchResult(const SearchResult& other);
66 SearchResult& operator=(const SearchResult& other);
67
68 public:
69 typedef std::unique_ptr<SearchResult> Ptr;
70
71 SearchResult(int msgid, LDAP* ld);
72 ~SearchResult();
73
74 bool getNext(PowerLDAP::sentry_t& entry, bool dn = false, int timeout = 5);
75 void getAll(PowerLDAP::sresult_t& results, bool dn = false, int timeout = 5);
76 };
77
78 PowerLDAP(const string& hosts, uint16_t port, bool tls, int timeout);
79 ~PowerLDAP();
80
81 bool connect();
82
83 void getOption(int option, int* value);
84 void setOption(int option, int value);
85
86 void bind(LdapAuthenticator* authenticator);
87 void bind(const string& ldapbinddn = "", const string& ldapsecret = "", int method = LDAP_AUTH_SIMPLE);
88 void simpleBind(const string& ldapbinddn = "", const string& ldapsecret = "");
89 SearchResult::Ptr search(const string& base, int scope, const string& filter, const char** attr = 0);
90 void add(const string& dn, LDAPMod* mods[]);
91 void modify(const string& dn, LDAPMod* mods[], LDAPControl** scontrols = 0, LDAPControl** ccontrols = 0);
92 void del(const string& dn);
93
94 bool getSearchEntry(int msgid, sentry_t& entry, bool dn = false);
95 void getSearchResults(int msgid, sresult_t& result, bool dn = false);
96
97 static const string escape(const string& tobe);
98 };