/*
* Copyright (C) 2002 Stichting NLnet, Netherlands, stichting@nlnet.nl.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the
* above copyright notice and this permission notice appear in all
* copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND STICHTING NLNET
* DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
* STICHTING NLNET BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
* OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE
* USE OR PERFORMANCE OF THIS SOFTWARE.
*
* The development of Dynamically Loadable Zones (DLZ) for BIND 9 was
* conceived and contributed by Rob Butler.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the
* above copyright notice and this permission notice appear in all
* copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND ROB BUTLER
* DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
* ROB BUTLER BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
* OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE
* USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/*
* Copyright (C) 1999-2001, 2013, 2016 Internet Systems Consortium, Inc. ("ISC")
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
/*
* This provides the externally loadable ldap DLZ module, without
* update support
*/
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdlib.h>
#include <dlz_minimal.h>
#include <dlz_list.h>
#include <dlz_dbi.h>
#include <dlz_pthread.h>
/*
* Need older API functions from ldap.h.
*/
#include <ldap.h>
/*%
* Structure to hold everthing needed by this "instance" of the LDAP
* driver remember, the driver code is only loaded once, but may have
* many separate instances.
*/
typedef struct {
#if PTHREADS
#else
#endif
/* Helper functions from the dlz_dlopen driver */
/* forward references */
#if DLZ_DLOPEN_VERSION < 3
#else
#endif
void
dlz_destroy(void *dbdata);
static void
/*
* Private methods
*/
/*% checks that the LDAP URL parameters make sense */
static isc_result_t
int ldap_result;
if (!ldap_is_ldap_url(URL)) {
"%s query is not a valid LDAP URL", msg);
goto cleanup;
}
goto cleanup;
}
"%s query must specify at least "
goto cleanup;
}
"%s query must not specify a host", msg);
goto cleanup;
}
"%s query must not specify a port", msg);
goto cleanup;
}
"%s query must specify a search base", msg);
goto cleanup;
}
"%s uses extensions. "
"The driver does not support LDAP extensions.", msg);
goto cleanup;
}
return (result);
}
/*% Connects / reconnects to LDAP server */
static isc_result_t
int ldap_result;
/* if we have a connection, get ride of it. */
}
/* now connect / reconnect. */
/* initialize. */
return (ISC_R_NOMEMORY);
/* set protocol version. */
if (ldap_result != LDAP_SUCCESS) {
goto cleanup;
}
/* "bind" to server. i.e. send username / pass */
if (ldap_result != LDAP_SUCCESS) {
goto cleanup;
}
return (ISC_R_SUCCESS);
/* cleanup if failure. */
}
return (result);
}
#if PTHREADS
/*%
* Properly cleans up a list of database instances.
* This function is only used when the driver is compiled for
* multithreaded operation.
*/
static void
/* get the first DBI in the list */
/* loop through the list */
/* get the next DBI in the list */
/* release DB connection */
/* release all memory that comprised a DBI */
}
/* release memory for the list structure */
}
/*%
* Loops through the list of DB instances, attempting to lock
* on the mutex. If successful, the DBI is reserved for use
* and the thread can perform queries against the database.
* If the lock fails, the next one in the list is tried.
* looping continues until a lock is obtained, or until
* the list has been searched dbc_search_limit times.
* This function is only used when the driver is compiled for
* multithreaded operation.
*/
static dbinstance_t *
int count = 0;
/* get top of list */
/* loop through list */
while (count < dbc_search_limit) {
/* try to lock on the mutex */
return (dbi); /* success, return the DBI for use. */
/* not successful, keep trying */
/* check to see if we have gone to the top of the list. */
count++;
}
}
"LDAP driver unable to find available connection "
"after searching %d times", count);
return (NULL);
}
#endif /* PTHREADS */
static isc_result_t
{
int i = 0;
int j;
int len;
int ttl;
/* get the first entry to process */
return (ISC_R_FAILURE);
}
/* loop through all entries returned */
/* reset for this loop */
ttl = 0;
len = 0;
i = 0;
/* determine how much space we need for data string */
/* get the list of values for this attribute. */
/* skip empty attributes. */
continue;
/*
* we only use the first value. this driver
* does not support multi-valued attributes.
*/
/* free vals for next loop */
}
/* allocate memory for data string */
"LDAP driver unable to allocate memory "
"while processing results");
goto cleanup;
}
/*
* Make sure data is null termed at the beginning so
* we can check if any data was stored to it later.
*/
data[0] = '\0';
/* reset j to re-use below */
j = 0;
/* loop through the attributes in the order specified. */
/* get the list of values for this attribute. */
/* skip empty attributes. */
/* increment attibute pointer */
/* start loop over */
continue;
}
/*
* j initially = 0. Increment j each time we
* set a field that way next loop will set
* next field.
*/
switch (j) {
case 0:
j++;
/*
* convert text to int, make sure it
* worked right
*/
"LDAP driver ttl must "
"be a postive number");
goto cleanup;
}
break;
case 1:
j++;
break;
case 2:
j++;
if (allnodes)
else
break;
case 3:
j++;
if (allnodes)
else {
}
break;
default:
break;
}
/* free values */
/* increment attibute pointer */
}
"LDAP driver unable to retrieve DNS type");
goto cleanup;
}
"LDAP driver unable to retrieve DNS data");
goto cleanup;
}
else
if (result != ISC_R_SUCCESS)
"ldap_dynamic: putnamedrr failed "
"for \"%s %s %u %s\" (%d)",
} else {
if (result != ISC_R_SUCCESS)
"ldap_dynamic: putrr failed "
"for \"%s %u %s\" (%s)",
}
if (result != ISC_R_SUCCESS) {
"LDAP driver failed "
"while sending data to BIND.");
goto cleanup;
}
/* free memory for type, data and host for next loop */
}
/* get the next entry to process */
}
/* de-allocate memory */
return (result);
}
/*%
* This function is the real core of the driver. Zone, record
* and client strings are passed in (or NULL is passed if the
* string is not available). The type of query we want to run
* is indicated by the query flag, and the dbdata object is passed
* passed in to. dbdata really holds either:
* 1) a list of database instances (in multithreaded mode) OR
* 2) a single database instance (in single threaded mode)
* The function will construct the query and obtain an available
* database instance (DBI). It will then run the query and hopefully
* obtain a result set.
*/
static isc_result_t
{
int ldap_result = 0;
int i;
int entries;
/* get db instance / connection */
#if PTHREADS
/* find an available DBI from the list */
#else /* PTHREADS */
/*
* only 1 DBI - no need to lock instance lock either
* only 1 thread in the whole process, no possible contention.
*/
#endif /* PTHREADS */
/* if DBI is null, can't do anything else */
return (ISC_R_FAILURE);
/* set fields */
goto cleanup;
}
} else
goto cleanup;
}
} else
goto cleanup;
}
} else
/* what type of query are we going to run? */
switch (query) {
case ALLNODES:
/*
* if the query was not passed in from the config file
* then we can't run it. return not_implemented, so
* it's like the code for that operation was never
* built into the driver.... AHHH flexibility!!!
*/
goto cleanup;
} else
break;
case ALLOWXFR:
/* same as comments as ALLNODES */
goto cleanup;
} else
break;
case AUTHORITY:
/* same as comments as ALLNODES */
goto cleanup;
} else
break;
case FINDZONE:
/* this is required. It's the whole point of DLZ! */
"No query specified for findzone. "
"Findzone requires a query");
goto cleanup;
} else
break;
case LOOKUP:
/* this is required. It's also a major point of DLZ! */
"No query specified for lookup. "
"Lookup requires a query");
goto cleanup;
} else
break;
default:
/*
* this should never happen. If it does, the code is
* screwed up!
*/
"Incorrect query flag passed to ldap_get_results");
goto cleanup;
}
/* if the querystring is null, Bummer, outta RAM. UPGRADE TIME!!! */
if (querystring == NULL) {
goto cleanup;
}
/*
* output the full query string during debug so we can see
* what lame error the query has.
*/
/* break URL down into it's component parts, if error cleanup */
goto cleanup;
}
for (i = 0; i < 3; i++) {
/*
* dbi->dbconn may be null if trying to reconnect on a
* previous query failed.
*/
"LDAP driver attempting to re-connect");
if (result != ISC_R_SUCCESS) {
continue;
}
}
/* perform ldap search syncronously */
/*
* check return code. No such object is ok, just
* didn't find what we wanted
*/
switch (ldap_result) {
case LDAP_NO_SUCH_OBJECT:
"No object found matching query requirements");
goto cleanup;
break;
case LDAP_SUCCESS: /* on success do nothing */
i = 3;
break;
case LDAP_SERVER_DOWN:
"LDAP driver attempting to re-connect");
if (result != ISC_R_SUCCESS)
break;
default:
/*
* other errors not ok. Log error message and
* get out
*/
goto cleanup;
break;
}
}
if (result != ISC_R_SUCCESS)
goto cleanup;
switch (query) {
case ALLNODES:
break;
case AUTHORITY:
case LOOKUP:
break;
case ALLOWXFR:
if (entries == 0)
else if (entries > 0)
else
break;
case FINDZONE:
if (entries == 0)
else if (entries > 0)
else
break;
default:
/*
* this should never happen. If it does, the code is
* screwed up!
*/
"Incorrect query flag passed to ldap_get_results");
}
/* it's always good to cleanup after yourself */
/* if we retrieved results, free them */
/* cleanup */
/* release the lock so another thread can use this dbi */
/* release query string */
if (querystring != NULL)
/* return result */
return (result);
}
/*
* DLZ methods
*/
/* check to see if we are authoritative for the zone first */
#if DLZ_DLOPEN_VERSION < 3
#else
#endif
if (result != ISC_R_SUCCESS) {
return (result);
}
/* get all the zone data */
return (result);
}
{
}
}
#if DLZ_DLOPEN_VERSION < 3
#else
#endif
{
#if DLZ_DLOPEN_VERSION >= 3
#endif
}
#if DLZ_DLOPEN_VERSION == 1
#else
#endif
{
#if DLZ_DLOPEN_VERSION >= 2
#endif
else
return (result);
}
void **dbdata, ...)
{
const char *helper_name;
int protocol;
int method;
#if PTHREADS
int dbcount;
char *endp;
int i;
#endif /* PTHREADS */
/* allocate memory for LDAP instance */
return (ISC_R_NOMEMORY);
/* Fill in the helper functions */
#if PTHREADS
/* if debugging, let user know we are multithreaded. */
#else /* PTHREADS */
/* if debugging, let user know we are single threaded. */
#endif /* PTHREADS */
if (argc < 9) {
"LDAP driver requires at least "
"8 command line args.");
goto cleanup;
}
/* no more than 13 arg's should be passed to the driver */
if (argc > 12) {
"LDAP driver cannot accept more than "
"11 command line args.");
goto cleanup;
}
/* determine protocol version. */
protocol = 2;
protocol = 3;
else {
"LDAP driver protocol must be either %s or %s",
goto cleanup;
}
/* determine connection method. */
else {
"LDAP driver authentication method must be "
goto cleanup;
}
/* multithreaded build can have multiple DB connections */
#if PTHREADS
/* check how many db connections we should create */
"LDAP driver database connection count "
"must be positive.");
goto cleanup;
}
#endif
/* check that LDAP URL parameters make sense */
switch (argc) {
case 12:
"allow zone transfer");
if (result != ISC_R_SUCCESS)
goto cleanup;
case 11:
if (result != ISC_R_SUCCESS)
goto cleanup;
case 10:
if (result != ISC_R_SUCCESS)
goto cleanup;
}
case 9:
if (result != ISC_R_SUCCESS)
goto cleanup;
if (result != ISC_R_SUCCESS)
goto cleanup;
break;
default:
/* not really needed, should shut up compiler. */
}
/* store info needed to automatically re-connect. */
goto cleanup;
}
goto cleanup;
}
goto cleanup;
}
#if PTHREADS
/* allocate memory for database connection list */
goto cleanup;
}
/* initialize DB connection list */
/*
* create the appropriate number of database instances (DBI)
* append each new DBI to the end of the list
*/
for (i = 0; i < dbcount; i++) {
#endif /* PTHREADS */
/* how many queries were passed in from config file? */
switch (argc) {
case 9:
break;
case 10:
break;
case 11:
break;
case 12:
break;
default:
/* not really needed, should shut up compiler. */
}
if (result == ISC_R_SUCCESS) {
"LDAP driver created "
"database instance object.");
} else { /* unsuccessful?, log err msg and cleanup. */
"LDAP driver could not create "
"database instance object.");
goto cleanup;
}
#if PTHREADS
/* when multithreaded, build a list of DBI's */
#else
/*
* when single threaded, hold onto the one connection
* instance.
*/
#endif
/* attempt to connect */
/*
* if db connection cannot be created, log err msg and
* cleanup.
*/
switch (result) {
/* success, do nothing */
case ISC_R_SUCCESS:
break;
/*
* no memory means ldap_init could not
* allocate memory
*/
case ISC_R_NOMEMORY:
#if PTHREADS
"LDAP driver could not allocate memory "
"for connection number %u", i + 1);
#else
"LDAP driver could not allocate memory "
"for connection");
#endif
goto cleanup;
/*
* no perm means ldap_set_option could not set
* protocol version
*/
case ISC_R_NOPERM:
"LDAP driver could not "
"set protocol version.");
goto cleanup;
/* failure means couldn't connect to ldap server */
case ISC_R_FAILURE:
#if PTHREADS
"LDAP driver could not bind "
"connection number %u to server.", i + 1);
#else
"LDAP driver could not "
"bind connection to server.");
#endif
goto cleanup;
/*
* default should never happen. If it does,
* major errors.
*/
default:
"dlz_create() failed (%d)", result);
goto cleanup;
}
#if PTHREADS
/* set DBI = null for next loop through. */
}
#endif /* PTHREADS */
/* set dbdata to the ldap_instance we created. */
return (ISC_R_SUCCESS);
return (result);
}
void
#if PTHREADS
/* cleanup the list of DBI's */
#else /* PTHREADS */
/* destroy single DB instance */
#endif /* PTHREADS */
}
}
/*
* Return the version of the API
*/
int
#if PTHREADS
#else
*flags &= ~DNS_SDLZFLAG_THREADSAFE;
#endif
return (DLZ_DLOPEN_VERSION);
}
/*
* Register a helper function from the bind9 dlz_dlopen driver
*/
static void
}