ldapdb.c revision bf49a52178db60df60f2316a3977b2249f7c0edb
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson/*
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson * Copyright (C) 2001 Stig Venaas
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson *
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson * Permission to use, copy, modify, and distribute this software for any
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson * purpose with or without fee is hereby granted, provided that the above
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson * copyright notice and this permission notice appear in all copies.
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson */
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson#include <config.h>
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson#include <string.h>
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson#include <stdio.h>
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson#include <stdlib.h>
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson#include <ctype.h>
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson#include <isc/mem.h>
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson#include <isc/print.h>
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson#include <isc/result.h>
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson#include <isc/util.h>
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson#include <dns/sdb.h>
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson#include <named/globals.h>
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson#include <ldap.h>
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson#include "ldapdb.h"
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson/*
999ae80184e3df1016ac74514124b0459ace4d01Andreas Gustafsson * A simple database driver for LDAP. Not production quality yet.
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson */
d60f5b9bc8c1e1f7ddebc6c7834f7550a8e8be6fBob Halley
d60f5b9bc8c1e1f7ddebc6c7834f7550a8e8be6fBob Halleystatic dns_sdbimplementation_t *ldapdb = NULL;
d60f5b9bc8c1e1f7ddebc6c7834f7550a8e8be6fBob Halley
d60f5b9bc8c1e1f7ddebc6c7834f7550a8e8be6fBob Halleystruct ldapdb_data {
d60f5b9bc8c1e1f7ddebc6c7834f7550a8e8be6fBob Halley char *hostname;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson int portno;
d60f5b9bc8c1e1f7ddebc6c7834f7550a8e8be6fBob Halley char *base;
d60f5b9bc8c1e1f7ddebc6c7834f7550a8e8be6fBob Halley int defaultttl;
d60f5b9bc8c1e1f7ddebc6c7834f7550a8e8be6fBob Halley LDAP *ld;
d60f5b9bc8c1e1f7ddebc6c7834f7550a8e8be6fBob Halley};
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafssonstatic isc_result_t
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafssonldapdb_create(const char *zone, int argc, char **argv,
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson void *driverdata, void **dbdata)
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson{
d60f5b9bc8c1e1f7ddebc6c7834f7550a8e8be6fBob Halley struct ldapdb_data *data;
d60f5b9bc8c1e1f7ddebc6c7834f7550a8e8be6fBob Halley char *s;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson int defaultttl;
d60f5b9bc8c1e1f7ddebc6c7834f7550a8e8be6fBob Halley
d60f5b9bc8c1e1f7ddebc6c7834f7550a8e8be6fBob Halley UNUSED(zone);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson UNUSED(driverdata);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson if ((argc < 2)
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson || (argv[0] != strstr( argv[0], "ldap://"))
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson || ((defaultttl = atoi(argv[1])) < 1))
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson return (ISC_R_FAILURE);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson data = isc_mem_get(ns_g_mctx, sizeof(struct ldapdb_data));
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson if (data == NULL)
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson return (ISC_R_NOMEMORY);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson data->hostname = isc_mem_strdup(ns_g_mctx, argv[0] + strlen("ldap://"));
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson if (data->hostname == NULL) {
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson isc_mem_put(ns_g_mctx, data, sizeof(struct ldapdb_data));
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson return (ISC_R_NOMEMORY);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson }
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson data->defaultttl = defaultttl;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson s = strchr(data->hostname, '/');
f4c0131a46ea183238027ef9c3400cc6079b8b85Andreas Gustafsson if (s != NULL) {
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson *s++ = '\0';
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson data->base = *s != '\0' ? s : NULL;
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson }
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson s = strchr(data->hostname, ':');
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson if (s != NULL) {
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson *s++ = '\0';
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson data->portno = atoi(s);
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson } else
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson data->portno = LDAP_PORT;
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson data->ld = NULL;
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson *dbdata = data;
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson return (ISC_R_SUCCESS);
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson}
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafssonstatic void
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafssonldapdb_destroy(const char *zone, void *driverdata, void **dbdata) {
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson struct ldapdb_data *data = *dbdata;
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson UNUSED(zone);
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson UNUSED(driverdata);
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson if (data == NULL)
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson return;
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson if (data->ld != NULL)
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson ldap_unbind(data->ld);
f4c0131a46ea183238027ef9c3400cc6079b8b85Andreas Gustafsson if (data->hostname != NULL)
f4c0131a46ea183238027ef9c3400cc6079b8b85Andreas Gustafsson isc_mem_free(ns_g_mctx, data->hostname);
f4c0131a46ea183238027ef9c3400cc6079b8b85Andreas Gustafsson isc_mem_put(ns_g_mctx, data, sizeof(struct ldapdb_data));
f4c0131a46ea183238027ef9c3400cc6079b8b85Andreas Gustafsson}
f4c0131a46ea183238027ef9c3400cc6079b8b85Andreas Gustafsson
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafssonstatic void
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafssonldapdb_bind(struct ldapdb_data *data)
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson{
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson if (data->ld != NULL)
f4c0131a46ea183238027ef9c3400cc6079b8b85Andreas Gustafsson ldap_unbind(data->ld);
f4c0131a46ea183238027ef9c3400cc6079b8b85Andreas Gustafsson data->ld = ldap_open(data->hostname, data->portno);
f4c0131a46ea183238027ef9c3400cc6079b8b85Andreas Gustafsson if (data->ld == NULL)
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson return;
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson if (ldap_simple_bind_s(data->ld, NULL, NULL) != LDAP_SUCCESS) {
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson ldap_unbind(data->ld);
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson data->ld = NULL;
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson }
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson}
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafssonstatic isc_result_t
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafssonldapdb_lookup(const char *zone, const char *name, void *dbdata,
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson dns_sdblookup_t *lookup)
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson{
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson isc_result_t result = ISC_R_NOTFOUND;
f4c0131a46ea183238027ef9c3400cc6079b8b85Andreas Gustafsson struct ldapdb_data *data = dbdata;
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson LDAPMessage *res, *e;
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson char *fltr, *a, **vals;
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson char type[64];
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson BerElement *ptr;
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson int i;
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson UNUSED(zone);
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson if (data->ld == NULL) {
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson ldapdb_bind(data);
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson if (data->ld == NULL)
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson return (ISC_R_FAILURE);
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson }
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson fltr = isc_mem_get(ns_g_mctx, strlen(name) + strlen("(dc=)") + 1);
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson if (fltr == NULL)
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson return (ISC_R_NOMEMORY);
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson strcpy(fltr, "(dc=");
8803b0510877fd08044542edbb55e2be72fae36fAndreas Gustafsson strcat(fltr, name);
f4c0131a46ea183238027ef9c3400cc6079b8b85Andreas Gustafsson strcat(fltr, ")");
f4c0131a46ea183238027ef9c3400cc6079b8b85Andreas Gustafsson if (ldap_search_s(data->ld, data->base, LDAP_SCOPE_ONELEVEL, fltr, NULL, 0, &res) != LDAP_SUCCESS) {
f4c0131a46ea183238027ef9c3400cc6079b8b85Andreas Gustafsson ldapdb_bind(data);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson if (data->ld != NULL)
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ldap_search_s(data->ld, data->base, LDAP_SCOPE_ONELEVEL, fltr, NULL, 0, &res);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson }
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson isc_mem_put(ns_g_mctx, fltr, strlen(fltr) + 1);
581db30788a4920ba8558287a0dccf3c1a210c5aAndreas Gustafsson if (data->ld == NULL)
581db30788a4920ba8558287a0dccf3c1a210c5aAndreas Gustafsson goto exit;
581db30788a4920ba8558287a0dccf3c1a210c5aAndreas Gustafsson
581db30788a4920ba8558287a0dccf3c1a210c5aAndreas Gustafsson for (e = ldap_first_entry(data->ld, res); e != NULL;
581db30788a4920ba8558287a0dccf3c1a210c5aAndreas Gustafsson e = ldap_next_entry(data->ld, e)) {
581db30788a4920ba8558287a0dccf3c1a210c5aAndreas Gustafsson LDAP *ld = data->ld;
581db30788a4920ba8558287a0dccf3c1a210c5aAndreas Gustafsson int ttl = data->defaultttl;
581db30788a4920ba8558287a0dccf3c1a210c5aAndreas Gustafsson
581db30788a4920ba8558287a0dccf3c1a210c5aAndreas Gustafsson for (a = ldap_first_attribute(ld, e, &ptr); a != NULL;
581db30788a4920ba8558287a0dccf3c1a210c5aAndreas Gustafsson a = ldap_next_attribute(ld, e, ptr)) {
581db30788a4920ba8558287a0dccf3c1a210c5aAndreas Gustafsson if (!strcmp(a, "dNSTTL")) {
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson vals = ldap_get_values(ld, e, a);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ttl = atoi(vals[0]);
581db30788a4920ba8558287a0dccf3c1a210c5aAndreas Gustafsson ldap_value_free(vals);
581db30788a4920ba8558287a0dccf3c1a210c5aAndreas Gustafsson ldap_memfree(a);
581db30788a4920ba8558287a0dccf3c1a210c5aAndreas Gustafsson break;
581db30788a4920ba8558287a0dccf3c1a210c5aAndreas Gustafsson }
581db30788a4920ba8558287a0dccf3c1a210c5aAndreas Gustafsson ldap_memfree(a);
581db30788a4920ba8558287a0dccf3c1a210c5aAndreas Gustafsson }
581db30788a4920ba8558287a0dccf3c1a210c5aAndreas Gustafsson for (a = ldap_first_attribute(ld, e, &ptr); a != NULL;
581db30788a4920ba8558287a0dccf3c1a210c5aAndreas Gustafsson a = ldap_next_attribute(ld, e, ptr)) {
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson char *s;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson for (s = a; *s; s++)
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson *s = toupper(*s);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson s = strstr(a, "RECORD");
b2c71d98dfc4dab5c6b8c8f39cf8fed3d899e94cAndreas Gustafsson if ((s == NULL) || (s == a)
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson || (s - a >= (signed int)sizeof(type))) {
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ldap_memfree(a);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson continue;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson }
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson strncpy(type, a, s - a);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson type[s - a] = '\0';
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson vals = ldap_get_values(ld, e, a);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson for (i=0; vals[i] != NULL; i++) {
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson result = dns_sdb_putrr(lookup, type, ttl, vals[i]);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson if (result != ISC_R_SUCCESS) {
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ldap_value_free(vals);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ldap_memfree(a);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson result = ISC_R_FAILURE;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson goto exit;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson }
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson }
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ldap_value_free(vals);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ldap_memfree(a);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson }
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson }
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson exit:
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ldap_msgfree(res);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson return (result);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson}
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafssonstatic isc_result_t
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafssonldapdb_allnodes(const char *zone, void *dbdata, dns_sdballnodes_t *allnodes) {
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson isc_result_t result = ISC_R_NOTFOUND;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson struct ldapdb_data *data = dbdata;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson LDAPMessage *res, *e;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson char type[64];
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson char *a, **vals;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson BerElement *ptr;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson int i;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson UNUSED(zone);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson if (data->ld == NULL) {
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ldapdb_bind(data);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson if (data->ld == NULL)
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson return (ISC_R_FAILURE);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson }
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson if (ldap_search_s(data->ld, data->base, LDAP_SCOPE_ONELEVEL, "(objectclass=*)", NULL, 0, &res) != LDAP_SUCCESS) {
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ldapdb_bind(data);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson if (data->ld != NULL)
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ldap_search_s(data->ld, data->base, LDAP_SCOPE_ONELEVEL, "(objectclass=*)", NULL, 0, &res);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson }
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson for (e = ldap_first_entry(data->ld, res); e != NULL;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson e = ldap_next_entry(data->ld, e)) {
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson LDAP *ld = data->ld;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson char *name = NULL;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson int ttl = data->defaultttl;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson for (a = ldap_first_attribute(ld, e, &ptr); a != NULL;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson a = ldap_next_attribute(ld, e, ptr)) {
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson if (!strcmp(a, "dNSTTL")) {
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson vals = ldap_get_values(ld, e, a);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ttl = atoi(vals[0]);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ldap_value_free(vals);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson } else if (!strcmp(a, "dc")) {
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson vals = ldap_get_values(ld, e, a);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson name = isc_mem_strdup(ns_g_mctx, vals[0]);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ldap_value_free(vals);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson }
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ldap_memfree(a);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson }
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson if (name == NULL)
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson continue;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson for (a = ldap_first_attribute(ld, e, &ptr); a != NULL;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson a = ldap_next_attribute(ld, e, ptr)) {
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson char *s;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson for (s = a; *s; s++)
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson *s = toupper(*s);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson s = strstr(a, "RECORD");
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson if ((s == NULL) || (s == a)
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson || (s - a >= (signed int)sizeof(type))) {
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ldap_memfree(a);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson continue;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson }
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson strncpy(type, a, s - a);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson type[s - a] = '\0';
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson vals = ldap_get_values(ld, e, a);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson for (i=0; vals[i] != NULL; i++) {
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson result = dns_sdb_putnamedrr(allnodes, name, type, ttl, vals[i]);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson if (result != ISC_R_SUCCESS) {
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ldap_value_free(vals);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ldap_memfree(a);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson isc_mem_free(ns_g_mctx, name);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson result = ISC_R_FAILURE;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson goto exit;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson }
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson }
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ldap_value_free(vals);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ldap_memfree(a);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson }
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson isc_mem_free(ns_g_mctx, name);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson }
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson exit:
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ldap_msgfree(res);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson return (result);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson}
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafssonstatic dns_sdbmethods_t ldapdb_methods = {
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ldapdb_lookup,
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson NULL, /* authority */
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ldapdb_allnodes,
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ldapdb_create,
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ldapdb_destroy
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson};
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson/*
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson * Wrapper around dns_sdb_register().
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson */
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafssonisc_result_t
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafssonldapdb_init(void) {
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson unsigned int flags;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson flags = DNS_SDBFLAG_RELATIVEOWNER | DNS_SDBFLAG_RELATIVERDATA;
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson return (dns_sdb_register("ldap", &ldapdb_methods, NULL, flags,
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson ns_g_mctx, &ldapdb));
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson}
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson/*
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson * Wrapper around dns_sdb_unregister().
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson */
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafssonvoid
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafssonldapdb_clear(void) {
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson if (ldapdb != NULL)
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson dns_sdb_unregister(&ldapdb);
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson}
4cd3d6df39927315e3fadc07a8da3788175f4195Andreas Gustafsson