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 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568
|
#include "ldapbackend.hh"
unsigned int ldap_host_index = 0;
LdapBackend::LdapBackend( const string &suffix )
{
string hoststr;
unsigned int i, idx;
vector<string> hosts;
try
{
m_msgid = 0;
m_qname = "";
m_pldap = NULL;
m_qlog = arg().mustDo( "query-logging" );
m_default_ttl = arg().asNum( "default-ttl" );
m_myname = "[LdapBackend]";
setArgPrefix( "ldap" + suffix );
m_getdn = false;
m_list_fcnt = &LdapBackend::list_simple;
m_lookup_fcnt = &LdapBackend::lookup_simple;
m_prepare_fcnt = &LdapBackend::prepare_simple;
if( getArg( "method" ) == "tree" )
{
m_lookup_fcnt = &LdapBackend::lookup_tree;
}
if( getArg( "method" ) == "strict" || mustDo( "disable-ptrrecord" ) )
{
m_list_fcnt = &LdapBackend::list_strict;
m_lookup_fcnt = &LdapBackend::lookup_strict;
m_prepare_fcnt = &LdapBackend::prepare_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() ];
}
L << Logger::Info << m_myname << " LDAP servers = " << hoststr << endl;
m_pldap = new PowerLDAP( hoststr.c_str(), LDAP_PORT, mustDo( "starttls" ) );
m_pldap->setOption( LDAP_OPT_DEREF, LDAP_DEREF_ALWAYS );
m_pldap->bind( getArg( "binddn" ), getArg( "secret" ), LDAP_AUTH_SIMPLE, getArgAsNum( "timeout" ) );
L << Logger::Notice << m_myname << " Ldap connection succeeded" << endl;
return;
}
catch( LDAPTimeout < )
{
L << Logger::Error << m_myname << " Ldap connection to server failed because of timeout" << endl;
}
catch( LDAPException &le )
{
L << Logger::Error << m_myname << " Ldap connection to server failed: " << le.what() << endl;
}
catch( std::exception &e )
{
L << Logger::Error << m_myname << " Caught STL exception: " << e.what() << endl;
}
if( m_pldap != NULL ) { delete( m_pldap ); }
throw( AhuException( "Unable to connect to ldap server" ) );
}
LdapBackend::~LdapBackend()
{
if( m_pldap != NULL ) { delete( m_pldap ); }
L << Logger::Notice << m_myname << " Ldap connection closed" << endl;
}
bool LdapBackend::list( const string& target, int domain_id )
{
try
{
m_qname = target;
m_axfrqlen = target.length();
m_adomain = m_adomains.end(); // skip loops in get() first time
return (this->*m_list_fcnt)( target, domain_id );
}
catch( LDAPTimeout < )
{
L << Logger::Warning << m_myname << " Unable to get zone " + target + " from LDAP directory: " << lt.what() << endl;
throw( DBException( "LDAP server timeout" ) );
}
catch( LDAPException &le )
{
L << Logger::Error << m_myname << " Unable to get zone " + target + " from LDAP directory: " << le.what() << endl;
throw( AhuException( "LDAP server unreachable" ) ); // try to reconnect to another server
}
catch( std::exception &e )
{
L << Logger::Error << m_myname << " Caught STL exception for target " << target << ": " << e.what() << endl;
throw( DBException( "STL exception" ) );
}
return false;
}
inline bool LdapBackend::list_simple( const string& target, int domain_id )
{
string dn;
string filter;
string qesc;
dn = getArg( "basedn" );
qesc = toLower( m_pldap->escape( target ) );
// search for SOARecord of target
filter = strbind( ":target:", "&(associatedDomain=" + qesc + ")(sOARecord=*)", getArg( "filter-axfr" ) );
m_msgid = m_pldap->search( dn, LDAP_SCOPE_SUBTREE, filter, (const char**) ldap_attrany );
m_pldap->getSearchEntry( m_msgid, m_result, true );
if( m_result.count( "dn" ) && !m_result["dn"].empty() )
{
dn = m_result["dn"][0];
m_result.erase( "dn" );
}
prepare();
filter = strbind( ":target:", "associatedDomain=*." + qesc, getArg( "filter-axfr" ) );
DLOG( L << Logger::Debug << m_myname << " Search = basedn: " << dn << ", filter: " << filter << endl );
m_msgid = m_pldap->search( dn, LDAP_SCOPE_SUBTREE, filter, (const char**) ldap_attrany );
return true;
}
inline bool LdapBackend::list_strict( const string& target, int domain_id )
{
if( target.size() > 13 && target.substr( target.size() - 13, 13 ) == ".in-addr.arpa" ||
target.size() > 9 && target.substr( target.size() - 9, 9 ) == ".ip6.arpa" )
{
L << Logger::Warning << m_myname << " Request for reverse zone AXFR, but this is not supported in strict mode" << endl;
return false; // AXFR isn't supported in strict mode. Use simple mode and additional PTR records
}
return list_simple( target, domain_id );
}
void LdapBackend::lookup( const QType &qtype, const string &qname, DNSPacket *dnspkt, int zoneid )
{
try
{
m_axfrqlen = 0;
m_qname = qname;
m_adomain = m_adomains.end(); // skip loops in get() first time
if( m_qlog ) { L.log( "Query: '" + qname + "|" + qtype.getName() + "'", Logger::Error ); }
(this->*m_lookup_fcnt)( qtype, qname, dnspkt, zoneid );
}
catch( LDAPTimeout < )
{
L << Logger::Warning << m_myname << " Unable to search LDAP directory: " << lt.what() << endl;
throw( DBException( "LDAP server timeout" ) );
}
catch( LDAPException &le )
{
L << Logger::Error << m_myname << " Unable to search LDAP directory: " << le.what() << endl;
throw( AhuException( "LDAP server unreachable" ) ); // try to reconnect to another server
}
catch( std::exception &e )
{
L << Logger::Error << m_myname << " Caught STL exception for qname " << qname << ": " << e.what() << endl;
throw( DBException( "STL exception" ) );
}
}
void LdapBackend::lookup_simple( const QType &qtype, const string &qname, DNSPacket *dnspkt, int zoneid )
{
string filter, attr, qesc;
const char** attributes = ldap_attrany + 1; // skip associatedDomain
const char* attronly[] = { NULL, "dNSTTL", "modifyTimestamp", NULL };
qesc = toLower( m_pldap->escape( qname ) );
filter = "associatedDomain=" + qesc;
if( qtype.getCode() != QType::ANY )
{
attr = qtype.getName() + "Record";
filter = "&(" + filter + ")(" + attr + "=*)";
attronly[0] = attr.c_str();
attributes = attronly;
}
filter = strbind( ":target:", filter, getArg( "filter-lookup" ) );
DLOG( L << Logger::Debug << m_myname << " Search = basedn: " << getArg( "basedn" ) << ", filter: " << filter << ", qtype: " << qtype.getName() << endl );
m_msgid = m_pldap->search( getArg( "basedn" ), LDAP_SCOPE_SUBTREE, filter, attributes );
}
void LdapBackend::lookup_strict( const QType &qtype, const string &qname, DNSPacket *dnspkt, int zoneid )
{
int len;
vector<string> parts;
string filter, attr, qesc;
const char** attributes = ldap_attrany + 1; // skip associatedDomain
const char* attronly[] = { NULL, "dNSTTL", "modifyTimestamp", NULL };
qesc = toLower( m_pldap->escape( qname ) );
stringtok( parts, qesc, "." );
len = qesc.length();
if( parts.size() == 6 && len > 13 && qesc.substr( len - 13, 13 ) == ".in-addr.arpa" ) // IPv4 reverse lookups
{
filter = "aRecord=" + ptr2ip4( parts );
attronly[0] = "associatedDomain";
attributes = attronly;
}
else if( parts.size() == 34 && len > 9 && ( qesc.substr( len - 9, 9 ) == ".ip6.arpa" ) ) // IPv6 reverse lookups
{
filter = "aAAARecord=" + ptr2ip6( parts );
attronly[0] = "associatedDomain";
attributes = attronly;
}
else // IPv4 and IPv6 lookups
{
filter = "associatedDomain=" + qesc;
if( qtype.getCode() != QType::ANY )
{
attr = qtype.getName() + "Record";
filter = "&(" + filter + ")(" + attr + "=*)";
attronly[0] = attr.c_str();
attributes = attronly;
}
}
filter = strbind( ":target:", filter, getArg( "filter-lookup" ) );
DLOG( L << Logger::Debug << m_myname << " Search = basedn: " << getArg( "basedn" ) << ", filter: " << filter << ", qtype: " << qtype.getName() << endl );
m_msgid = m_pldap->search( getArg( "basedn" ), LDAP_SCOPE_SUBTREE, filter, attributes );
}
void LdapBackend::lookup_tree( const QType &qtype, const string &qname, DNSPacket *dnspkt, int zoneid )
{
string filter, attr, qesc, dn;
const char** attributes = ldap_attrany + 1; // skip associatedDomain
const char* attronly[] = { NULL, "dNSTTL", "modifyTimestamp", NULL };
vector<string>::reverse_iterator i;
vector<string> parts;
qesc = toLower( m_pldap->escape( qname ) );
filter = "associatedDomain=" + qesc;
if( qtype.getCode() != QType::ANY )
{
attr = qtype.getName() + "Record";
filter = "&(" + filter + ")(" + attr + "=*)";
attronly[0] = attr.c_str();
attributes = attronly;
}
filter = strbind( ":target:", filter, getArg( "filter-lookup" ) );
stringtok( parts, toLower( qname ), "." );
for( i = parts.rbegin(); i != parts.rend(); i++ )
{
dn = "dc=" + *i + "," + dn;
}
DLOG( L << Logger::Debug << m_myname << " Search = basedn: " << dn + getArg( "basedn" ) << ", filter: " << filter << ", qtype: " << qtype.getName() << endl );
m_msgid = m_pldap->search( dn + getArg( "basedn" ), LDAP_SCOPE_BASE, filter, attributes );
}
inline bool LdapBackend::prepare()
{
m_adomains.clear();
m_ttl = m_default_ttl;
m_last_modified = 0;
if( m_result.count( "dNSTTL" ) && !m_result["dNSTTL"].empty() )
{
char* endptr;
m_ttl = (uint32_t) strtol( m_result["dNSTTL"][0].c_str(), &endptr, 10 );
if( *endptr != '\0' )
{
L << Logger::Warning << m_myname << " Invalid time to life for " << m_qname << ": " << m_result["dNSTTL"][0] << endl;
m_ttl = m_default_ttl;
}
m_result.erase( "dNSTTL" );
}
if( m_result.count( "modifyTimestamp" ) && !m_result["modifyTimestamp"].empty() )
{
if( ( m_last_modified = str2tstamp( m_result["modifyTimestamp"][0] ) ) == 0 )
{
L << Logger::Warning << m_myname << " Invalid modifyTimestamp for " << m_qname << ": " << m_result["modifyTimestamp"][0] << endl;
}
m_result.erase( "modifyTimestamp" );
}
if( !(this->*m_prepare_fcnt)() )
{
return false;
}
m_adomain = m_adomains.begin();
m_attribute = m_result.begin();
m_value = m_attribute->second.begin();
return true;
}
inline bool LdapBackend::prepare_simple()
{
if( !m_axfrqlen ) // request was a normal lookup()
{
m_adomains.push_back( m_qname );
}
else // request was a list() for AXFR
{
if( m_result.count( "associatedDomain" ) )
{
vector<string>::iterator i;
for( i = m_result["associatedDomain"].begin(); i != m_result["associatedDomain"].end(); i++ ) {
if( i->size() >= m_axfrqlen && i->substr( i->size() - m_axfrqlen, m_axfrqlen ) == m_qname ) {
m_adomains.push_back( *i );
}
}
m_result.erase( "associatedDomain" );
}
}
return true;
}
inline bool LdapBackend::prepare_strict()
{
if( !m_axfrqlen ) // request was a normal lookup()
{
m_adomains.push_back( m_qname );
if( m_result.count( "associatedDomain" ) )
{
m_result["PTRRecord"] = m_result["associatedDomain"];
m_result.erase( "associatedDomain" );
}
}
else // request was a list() for AXFR
{
if( m_result.count( "associatedDomain" ) )
{
vector<string>::iterator i;
for( i = m_result["associatedDomain"].begin(); i != m_result["associatedDomain"].end(); i++ ) {
if( i->size() >= m_axfrqlen && i->substr( i->size() - m_axfrqlen, m_axfrqlen ) == m_qname ) {
m_adomains.push_back( *i );
}
}
m_result.erase( "associatedDomain" );
}
}
return true;
}
bool LdapBackend::get( DNSResourceRecord &rr )
{
QType qt;
vector<string> parts;
string attrname, content, qstr;
try
{
do
{
while( m_adomain != m_adomains.end() )
{
while( m_attribute != m_result.end() )
{
attrname = m_attribute->first;
qstr = attrname.substr( 0, attrname.length() - 6 ); // extract qtype string from ldap attribute name
qt = const_cast<char*>(toUpper( qstr ).c_str());
while( m_value != m_attribute->second.end() )
{
content = *m_value;
rr.qtype = qt;
rr.qname = *m_adomain;
rr.priority = 0;
rr.ttl = m_ttl;
rr.last_modified = m_last_modified;
if( qt.getCode() == QType::MX || qt.getCode() == QType::SRV ) // Priority, e.g. 10 smtp.example.com
{
char* endptr;
string::size_type first = content.find_first_of( " " );
if( first == string::npos )
{
L << Logger::Warning << m_myname << " Invalid " << attrname << " without priority for " << m_qname << ": " << content << endl;
m_value++;
continue;
}
rr.priority = (uint16_t) strtoul( (content.substr( 0, first )).c_str(), &endptr, 10 );
if( *endptr != '\0' )
{
L << Logger::Warning << m_myname << " Invalid " << attrname << " without priority for " << m_qname << ": " << content << endl;
m_value++;
continue;
}
content = content.substr( first + 1, content.length() - first - 1 );
}
rr.content = content;
m_value++;
DLOG( L << Logger::Debug << m_myname << " Record = qname: " << rr.qname << ", qtype: " << (rr.qtype).getName() << ", priority: " << rr.priority << ", ttl: " << rr.ttl << ", content: " << rr.content << endl );
return true;
}
m_attribute++;
m_value = m_attribute->second.begin();
}
m_adomain++;
m_attribute = m_result.begin();
m_value = m_attribute->second.begin();
}
}
while( m_pldap->getSearchEntry( m_msgid, m_result, m_getdn ) && prepare() );
}
catch( LDAPTimeout < )
{
L << Logger::Warning << m_myname << " Search failed: " << lt.what() << endl;
throw( DBException( "LDAP server timeout" ) );
}
catch( LDAPException &le )
{
L << Logger::Error << m_myname << " Search failed: " << le.what() << endl;
throw( AhuException( "LDAP server unreachable" ) ); // try to reconnect to another server
}
catch( std::exception &e )
{
L << Logger::Error << m_myname << " Caught STL exception for " << m_qname << ": " << e.what() << endl;
throw( DBException( "STL exception" ) );
}
return false;
}
bool LdapBackend::getDomainInfo( const string& domain, DomainInfo& di )
{
string filter;
SOAData sd;
const char* attronly[] = { "sOARecord", NULL };
// search for SOARecord of domain
filter = "(&(associatedDomain=" + toLower( m_pldap->escape( domain ) ) + ")(SOARecord=*))";
m_msgid = m_pldap->search( getArg( "basedn" ), LDAP_SCOPE_SUBTREE, filter, attronly );
m_pldap->getSearchEntry( m_msgid, m_result );
if( m_result.count( "sOARecord" ) && !m_result["sOARecord"].empty() )
{
sd.serial = 0;
fillSOAData( m_result["sOARecord"][0], sd );
di.id = 0;
di.serial = sd.serial;
di.zone = domain;
di.last_check = 0;
di.backend = this;
di.kind = DomainInfo::Master;
return true;
}
return false;
}
class LdapFactory : public BackendFactory
{
public:
LdapFactory() : BackendFactory( "ldap" ) {}
void declareArguments( const string &suffix="" )
{
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, "binddn", "User dn for non anonymous binds","" );
declare( suffix, "secret", "User password for non anonymous binds", "" );
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" );
}
DNSBackend* make( const string &suffix="" )
{
return new LdapBackend( suffix );
}
};
class LdapLoader
{
LdapFactory factory;
public:
LdapLoader()
{
BackendMakers().report( &factory );
L << Logger::Info << " [LdapBackend] This is the ldap module version "VERSION" ("__DATE__", "__TIME__") reporting" << endl;
}
};
static LdapLoader ldaploader;
|