File: ldapbackend.cc

package info (click to toggle)
pdns 5.0.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,824 kB
  • sloc: cpp: 101,247; sh: 5,616; makefile: 2,318; sql: 860; ansic: 675; python: 635; yacc: 245; perl: 161; lex: 131
file content (308 lines) | stat: -rw-r--r-- 10,786 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/*
 * This file is part of PowerDNS or dnsdist.
 * Copyright -- PowerDNS.COM B.V. and its contributors
 * originally authored by Norbert Sendetzky
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of version 2 of the GNU General Public License as
 * published by the Free Software Foundation.
 *
 * In addition, for the avoidance of any doubt, permission is granted to
 * link this program with OpenSSL and to (re)distribute the binaries
 * produced as the result of such linking.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "exceptions.hh"
#include "ldapauthenticator_p.hh"
#include "ldapbackend.hh"
#include <cstdlib>

unsigned int ldap_host_index = 0;

LdapBackend::LdapBackend(const string& suffix)
{
  string hoststr;
  unsigned int i, idx;
  vector<string> hosts;

  try {
    d_qname.clear();
    d_pldap = nullptr;
    d_authenticator = nullptr;
    d_qlog = arg().mustDo("query-logging");
    d_default_ttl = arg().asNum("default-ttl");
    d_myname = "[LdapBackend]";
    d_in_list = false;

    setArgPrefix("ldap" + suffix);

    d_getdn = false;
    d_reconnect_attempts = getArgAsNum("reconnect-attempts");
    d_list_fcnt = &LdapBackend::list_simple;
    d_lookup_fcnt = &LdapBackend::lookup_simple;

    if (getArg("method") == "tree") {
      d_lookup_fcnt = &LdapBackend::lookup_tree;
    }

    if (getArg("method") == "strict" || mustDo("disable-ptrrecord")) {
      d_list_fcnt = &LdapBackend::list_strict;
      d_lookup_fcnt = &LdapBackend::lookup_strict;
    }

    stringtok(hosts, getArg("host"), ", ");
    idx = ldap_host_index++ % hosts.size();
    hoststr = hosts[idx];

    for (i = 1; i < hosts.size(); i++) {
      hoststr += " " + hosts[(idx + i) % hosts.size()];
    }

    g_log << Logger::Info << d_myname << " LDAP servers = " << hoststr << endl;

    d_pldap = new PowerLDAP(hoststr.c_str(), LDAP_PORT, mustDo("starttls"), getArgAsNum("timeout"));
    d_pldap->setOption(LDAP_OPT_DEREF, LDAP_DEREF_ALWAYS);

    string bindmethod = getArg("bindmethod");
    if (bindmethod == "gssapi") {
      setenv("KRB5CCNAME", getArg("krb5-ccache").c_str(), 1);
      d_authenticator = new LdapGssapiAuthenticator(getArg("krb5-keytab"), getArg("krb5-ccache"), getArgAsNum("timeout"));
    }
    else {
      d_authenticator = new LdapSimpleAuthenticator(getArg("binddn"), getArg("secret"), getArgAsNum("timeout"));
    }
    d_pldap->bind(d_authenticator);

    g_log << Logger::Notice << d_myname << " Ldap connection succeeded" << endl;
    return;
  }
  catch (LDAPTimeout& lt) {
    g_log << Logger::Error << d_myname << " Ldap connection to server failed because of timeout" << endl;
  }
  catch (LDAPException& le) {
    g_log << Logger::Error << d_myname << " Ldap connection to server failed: " << le.what() << endl;
  }
  catch (std::exception& e) {
    g_log << Logger::Error << d_myname << " Caught STL exception: " << e.what() << endl;
  }

  if (d_pldap != nullptr) {
    delete (d_pldap);
  }
  throw PDNSException("Unable to connect to ldap server");
}

LdapBackend::~LdapBackend()
{
  d_search.reset(); // This is necessary otherwise d_pldap will get deleted first and
                    // we may hang in SearchResult::~SearchResult() waiting for the
                    // current operation to be abandoned
  delete (d_pldap);
  delete (d_authenticator);
  g_log << Logger::Notice << d_myname << " Ldap connection closed" << endl;
}

bool LdapBackend::reconnect()
{
  int attempts = d_reconnect_attempts;
  bool connected = false;
  while (!connected && attempts > 0) {
    g_log << Logger::Debug << d_myname << " Reconnection attempts left: " << attempts << endl;
    connected = d_pldap->connect();
    if (!connected)
      Utility::usleep(250);
    --attempts;
  }

  if (connected)
    d_pldap->bind(d_authenticator);

  return connected;
}

void LdapBackend::extract_common_attributes(DNSResult& result)
{
  if (d_result.count("dNSTTL") && !d_result["dNSTTL"].empty()) {
    char* endptr;
    uint32_t ttl = (uint32_t)strtol(d_result["dNSTTL"][0].c_str(), &endptr, 10);

    if (*endptr != '\0') {
      // NOTE: this will not give the entry for which the TTL was off.
      // TODO: improve this.
      //   - Check how d_getdn is used, because if it's never false then we
      //     might as well use it.
      g_log << Logger::Warning << d_myname << " Invalid time to live for " << d_qname << ": " << d_result["dNSTTL"][0] << endl;
    }
    else {
      result.ttl = ttl;
    }

    // We have to erase the attribute, otherwise this will mess up the records retrieval later.
    d_result.erase("dNSTTL");
  }

  if (d_result.count("modifyTimestamp") && !d_result["modifyTimestamp"].empty()) {
    time_t tstamp = 0;
    if ((tstamp = str2tstamp(d_result["modifyTimestamp"][0])) == 0) {
      // Same note as above, we don't know which entry failed here
      g_log << Logger::Warning << d_myname << " Invalid modifyTimestamp for " << d_qname << ": " << d_result["modifyTimestamp"][0] << endl;
    }
    else {
      result.lastmod = tstamp;
    }

    // Here too we have to erase this attribute.
    d_result.erase("modifyTimestamp");
  }
}

void LdapBackend::extract_entry_results(const DNSName& domain, const DNSResult& result_template, QType qtype)
{
  std::string attrname, qstr;
  QType qt;
  bool has_records = false;

  for (const auto& attribute : d_result) {
    // Find if we're dealing with a record attribute
    if (attribute.first.length() > 6 && attribute.first.compare(attribute.first.length() - 6, 6, "Record") == 0) {
      has_records = true;
      attrname = attribute.first;
      // extract qtype string from ldap attribute name by removing the 'Record' suffix.
      qstr = attrname.substr(0, attrname.length() - 6);
      qt = toUpper(qstr);

      for (const auto& value : attribute.second) {
        if (qtype != qt && qtype != QType::ANY) {
          continue;
        }

        DNSResult local_result = result_template;
        local_result.qtype = qt;
        local_result.qname = domain;
        local_result.value = value;
        local_result.auth = true;

        // Now let's see if we have some PDNS record data

        // TTL
        if (d_result.count("PdnsRecordTTL") && !d_result["PdnsRecordTTL"].empty()) {
          for (const auto& rdata : d_result["PdnsRecordTTL"]) {
            std::string qtype2;
            std::size_t pos = rdata.find_first_of('|', 0);
            if (pos == std::string::npos)
              continue;

            qtype2 = rdata.substr(0, pos);
            if (qtype2 != QType(local_result.qtype).toString())
              continue;

            pdns::checked_stoi_into(local_result.ttl, rdata.substr(pos + 1));
          }
        }

        // Not authoritative
        if (d_result.count("PdnsRecordNoAuth") && !d_result["PdnsRecordNoAuth"].empty()) {
          for (const auto& rdata : d_result["PdnsRecordNoAuth"]) {
            if (rdata == QType(local_result.qtype).toString())
              local_result.auth = false;
          }
        }

        // Ordername
        if (d_result.count("PdnsRecordOrdername") && !d_result["PdnsRecordOrdername"].empty()) {
          std::string defaultOrdername;

          for (const auto& rdata : d_result["PdnsRecordOrdername"]) {
            std::string qtype2;
            std::size_t pos = rdata.find_first_of('|', 0);
            if (pos == std::string::npos) {
              // This is the default ordername for all records in this entry
              defaultOrdername = rdata;
              continue;
            }

            qtype2 = rdata.substr(0, pos);
            if (qtype2 != QType(local_result.qtype).toString())
              continue;

            local_result.ordername = rdata.substr(pos + 1);
          }

          if (local_result.ordername.empty() && !defaultOrdername.empty())
            local_result.ordername = defaultOrdername;
        }

        d_results_cache.push_back(local_result);
      }
    }
  }

  if (!has_records) {
    // This is an ENT
    DNSResult local_result = result_template;
    local_result.qname = domain;
    if (!d_result.count("PdnsRecordOrdername") || d_result["PdnsRecordOrdername"].empty()) {
      // An ENT with an order name is authoritative
      local_result.auth = false;
    }
    d_results_cache.push_back(local_result);
  }
}

class LdapFactory : public BackendFactory
{
public:
  LdapFactory() :
    BackendFactory("ldap") {}

  void declareArguments(const string& suffix = "") override
  {
    declare(suffix, "host", "One or more LDAP server with ports or LDAP URIs (separated by spaces)", "ldap://127.0.0.1:389/");
    declare(suffix, "starttls", "Use TLS to encrypt connection (unused for LDAP URIs)", "no");
    declare(suffix, "basedn", "Search root in ldap tree (must be set)", "");
    declare(suffix, "basedn-axfr-override", "Override base dn for AXFR subtree search", "no");
    declare(suffix, "bindmethod", "Bind method to use (simple or gssapi)", "simple");
    declare(suffix, "binddn", "User dn for non anonymous binds", "");
    declare(suffix, "secret", "User password for non anonymous binds", "");
    declare(suffix, "krb5-keytab", "The keytab to use for GSSAPI authentication", "");
    declare(suffix, "krb5-ccache", "The credentials cache used for GSSAPI authentication", "");
    declare(suffix, "timeout", "Seconds before connecting to server fails", "5");
    declare(suffix, "method", "How to search entries (simple, strict or tree)", "simple");
    declare(suffix, "filter-axfr", "LDAP filter for limiting AXFR results", "(:target:)");
    declare(suffix, "filter-lookup", "LDAP filter for limiting IP or name lookups", "(:target:)");
    declare(suffix, "disable-ptrrecord", "Deprecated, use ldap-method=strict instead", "no");
    declare(suffix, "reconnect-attempts", "Number of attempts to re-establish a lost LDAP connection", "5");
  }

  DNSBackend* make(const string& suffix = "") override
  {
    return new LdapBackend(suffix);
  }
};

class LdapLoader
{
public:
  LdapLoader()
  {
    BackendMakers().report(std::make_unique<LdapFactory>());
    g_log << Logger::Info << "[ldapbackend] This is the ldap backend version " VERSION
#ifndef REPRODUCIBLE
          << " (" __DATE__ " " __TIME__ ")"
#endif
          << " reporting" << endl;
  }
};

static LdapLoader ldaploader;