03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews/*
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * Copyright (C) 2002 Stichting NLnet, Netherlands, stichting@nlnet.nl.
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews *
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * Permission to use, copy, modify, and distribute this software for any
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * purpose with or without fee is hereby granted, provided that the
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * above copyright notice and this permission notice appear in all
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * copies.
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews *
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * THE SOFTWARE IS PROVIDED "AS IS" AND STICHTING NLNET
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * STICHTING NLNET BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * USE OR PERFORMANCE OF THIS SOFTWARE.
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews *
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * The development of Dynamically Loadable Zones (DLZ) for Bind 9 was
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * conceived and contributed by Rob Butler.
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews *
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * Permission to use, copy, modify, and distribute this software for any
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * purpose with or without fee is hereby granted, provided that the
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * above copyright notice and this permission notice appear in all
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * copies.
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews *
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * THE SOFTWARE IS PROVIDED "AS IS" AND ROB BUTLER
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * ROB BUTLER BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * USE OR PERFORMANCE OF THIS SOFTWARE.
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews/*
02d54949f0f1db4729e14c3322b207f58d2578a4Mark Andrews * Copyright (C) 1999-2001, 2016 Internet Systems Consortium, Inc. ("ISC")
02d54949f0f1db4729e14c3322b207f58d2578a4Mark Andrews *
02d54949f0f1db4729e14c3322b207f58d2578a4Mark Andrews * This Source Code Form is subject to the terms of the Mozilla Public
02d54949f0f1db4729e14c3322b207f58d2578a4Mark Andrews * License, v. 2.0. If a copy of the MPL was not distributed with this
02d54949f0f1db4729e14c3322b207f58d2578a4Mark Andrews * file, You can obtain one at http://mozilla.org/MPL/2.0/.
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews#include <config.h>
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews#include <dns/log.h>
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews#include <dns/result.h>
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews#include <isc/mem.h>
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews#include <isc/result.h>
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews#include <isc/string.h>
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews#include <isc/util.h>
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews#include <dlz/sdlz_helper.h>
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews/*
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * sdlz helper methods
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews/*%
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * properly destroys a querylist by de-allocating the
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * memory for each query segment, and then the list itself
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrewsstatic void
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrewsdestroy_querylist(isc_mem_t *mctx, query_list_t **querylist)
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews{
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews query_segment_t *tseg = NULL;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews query_segment_t *nseg = NULL;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews REQUIRE(mctx != NULL);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* if query list is null, nothing to do */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews if (*querylist == NULL)
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews return;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* start at the top of the list */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews nseg = ISC_LIST_HEAD(**querylist);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews while (nseg != NULL) { /* loop, until end of list */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews tseg = nseg;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /*
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * free the query segment's text string but only if it
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * was really a query segment, and not a pointer to
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * %zone%, or %record%, or %client%
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews if (tseg->sql != NULL && tseg->direct == isc_boolean_true)
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews isc_mem_free(mctx, tseg->sql);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* get the next query segment, before we destroy this one. */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews nseg = ISC_LIST_NEXT(nseg, link);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* deallocate this query segment. */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews isc_mem_put(mctx, tseg, sizeof(query_segment_t));
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews }
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* deallocate the query segment list */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews isc_mem_put(mctx, *querylist, sizeof(query_list_t));
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews}
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews/*% constructs a query list by parsing a string into query segments */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrewsstatic isc_result_t
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrewsbuild_querylist(isc_mem_t *mctx, const char *query_str, char **zone,
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews char **record, char **client, query_list_t **querylist,
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews unsigned int flags)
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews{
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews isc_result_t result;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews isc_boolean_t foundzone = isc_boolean_false;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews isc_boolean_t foundrecord = isc_boolean_false;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews isc_boolean_t foundclient = isc_boolean_false;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews char *temp_str = NULL;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews char *right_str = NULL;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews query_list_t *tql;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews query_segment_t *tseg = NULL;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews REQUIRE(querylist != NULL && *querylist == NULL);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews REQUIRE(mctx != NULL);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* if query string is null, or zero length */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews if (query_str == NULL || strlen(query_str) < 1) {
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews if ((flags & SDLZH_REQUIRE_QUERY) == 0)
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* we don't need it were ok. */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews return (ISC_R_SUCCESS);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews else
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* we did need it, PROBLEM!!! */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews return (ISC_R_FAILURE);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews }
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* allocate memory for query list */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews tql = isc_mem_get(mctx, sizeof(query_list_t));
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* couldn't allocate memory. Problem!! */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews if (tql == NULL)
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews return (ISC_R_NOMEMORY);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* initialize the query segment list */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews ISC_LIST_INIT(*tql);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* make a copy of query_str so we can chop it up */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews temp_str = right_str = isc_mem_strdup(mctx, query_str);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* couldn't make a copy, problem!! */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews if (right_str == NULL) {
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews result = ISC_R_NOMEMORY;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews goto cleanup;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews }
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* loop through the string and chop it up */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews while (right_str != NULL) {
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* allocate memory for tseg */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews tseg = isc_mem_get(mctx, sizeof(query_segment_t));
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews if (tseg == NULL) { /* no memory, clean everything up. */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews result = ISC_R_NOMEMORY;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews goto cleanup;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews }
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews tseg->sql = NULL;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews tseg->direct = isc_boolean_false;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* initialize the query segment link */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews ISC_LINK_INIT(tseg, link);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* append the query segment to the list */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews ISC_LIST_APPEND(*tql, tseg, link);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /*
dc64df4479df5e7cb3ed1dead21888b5af98f67eMark Andrews * split string at the first "$". set query segment to
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * left portion
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews tseg->sql = isc_mem_strdup(mctx,
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews isc_string_separate(&right_str,
dc64df4479df5e7cb3ed1dead21888b5af98f67eMark Andrews "$"));
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews if (tseg->sql == NULL) {
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* no memory, clean everything up. */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews result = ISC_R_NOMEMORY;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews goto cleanup;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews }
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* tseg->sql points directly to a string. */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews tseg->direct = isc_boolean_true;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews tseg->strlen = strlen(tseg->sql);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
dc64df4479df5e7cb3ed1dead21888b5af98f67eMark Andrews /* check if we encountered "$zone$" token */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews if (strcasecmp(tseg->sql, "zone") == 0) {
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /*
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * we don't really need, or want the "zone"
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * text, so get rid of it.
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews isc_mem_free(mctx, tseg->sql);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* set tseg->sql to in-direct zone string */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews tseg->sql = (char**) zone;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews tseg->strlen = 0;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* tseg->sql points in-directly to a string */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews tseg->direct = isc_boolean_false;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews foundzone = isc_boolean_true;
dc64df4479df5e7cb3ed1dead21888b5af98f67eMark Andrews /* check if we encountered "$record$" token */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews } else if (strcasecmp(tseg->sql, "record") == 0) {
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /*
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * we don't really need, or want the "record"
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * text, so get rid of it.
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews isc_mem_free(mctx, tseg->sql);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* set tseg->sql to in-direct record string */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews tseg->sql = (char**) record;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews tseg->strlen = 0;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* tseg->sql points in-directly poinsts to a string */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews tseg->direct = isc_boolean_false;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews foundrecord = isc_boolean_true;
dc64df4479df5e7cb3ed1dead21888b5af98f67eMark Andrews /* check if we encountered "$client$" token */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews } else if (strcasecmp(tseg->sql, "client") == 0) {
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /*
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * we don't really need, or want the "client"
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * text, so get rid of it.
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews isc_mem_free(mctx, tseg->sql);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* set tseg->sql to in-direct record string */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews tseg->sql = (char**) client;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews tseg->strlen = 0;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* tseg->sql points in-directly poinsts to a string */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews tseg->direct = isc_boolean_false;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews foundclient = isc_boolean_true;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews }
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews }
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* we don't need temp_str any more */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews isc_mem_free(mctx, temp_str);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /*
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * add checks later to verify zone and record are found if
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * necessary.
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* if this query requires %client%, make sure we found it */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews if (((flags & SDLZH_REQUIRE_CLIENT) != 0) && (!foundclient) ) {
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* Write error message to log */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews DNS_LOGMODULE_DLZ, ISC_LOG_ERROR,
dc64df4479df5e7cb3ed1dead21888b5af98f67eMark Andrews "Required token $client$ not found.");
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews result = ISC_R_FAILURE;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews goto flag_fail;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews }
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* if this query requires %record%, make sure we found it */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews if (((flags & SDLZH_REQUIRE_RECORD) != 0) && (!foundrecord) ) {
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* Write error message to log */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews DNS_LOGMODULE_DLZ, ISC_LOG_ERROR,
dc64df4479df5e7cb3ed1dead21888b5af98f67eMark Andrews "Required token $record$ not found.");
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews result = ISC_R_FAILURE;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews goto flag_fail;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews }
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* if this query requires %zone%, make sure we found it */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews if (((flags & SDLZH_REQUIRE_ZONE) != 0) && (!foundzone) ) {
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* Write error message to log */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews DNS_LOGMODULE_DLZ, ISC_LOG_ERROR,
dc64df4479df5e7cb3ed1dead21888b5af98f67eMark Andrews "Required token $zone$ not found.");
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews result = ISC_R_FAILURE;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews goto flag_fail;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews }
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* pass back the query list */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews *querylist = (query_list_t *) tql;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* return success */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews return (ISC_R_SUCCESS);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews cleanup:
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* get rid of temp_str */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews if (temp_str != NULL)
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews isc_mem_free(mctx, temp_str);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews flag_fail:
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* get rid of what was build of the query list */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews if (tql != NULL)
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews destroy_querylist(mctx, &tql);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews return result;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews}
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews/*%
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * build a query string from query segments, and dynamic segments
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * dynamic segments replace where the tokens %zone%, %record%, %client%
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * used to be in our queries from named.conf
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrewschar *
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrewssdlzh_build_querystring(isc_mem_t *mctx, query_list_t *querylist)
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews{
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews query_segment_t *tseg = NULL;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews unsigned int length = 0;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews char *qs = NULL;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews REQUIRE(mctx != NULL);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews REQUIRE(querylist != NULL);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* start at the top of the list */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews tseg = ISC_LIST_HEAD(*querylist);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews while (tseg != NULL) {
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /*
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * if this is a query segment, use the
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * precalculated string length
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews if (tseg->direct == isc_boolean_true)
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews length += tseg->strlen;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews else /* calculate string length for dynamic segments. */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews length += strlen(* (char**) tseg->sql);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* get the next segment */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews tseg = ISC_LIST_NEXT(tseg, link);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews }
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* allocate memory for the string */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews qs = isc_mem_allocate(mctx, length + 1);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* couldn't allocate memory, We need more ram! */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews if (qs == NULL)
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews return NULL;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
611dc8876869036ab5e981e53ae7a446145d9354Mark Andrews *qs = 0;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* start at the top of the list again */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews tseg = ISC_LIST_HEAD(*querylist);
611dc8876869036ab5e981e53ae7a446145d9354Mark Andrews while (tseg != NULL) {
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews if (tseg->direct == isc_boolean_true)
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* query segments */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews strcat(qs, tseg->sql);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews else
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* dynamic segments */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews strcat(qs, * (char**) tseg->sql);
611dc8876869036ab5e981e53ae7a446145d9354Mark Andrews /* get the next segment */
611dc8876869036ab5e981e53ae7a446145d9354Mark Andrews tseg = ISC_LIST_NEXT(tseg, link);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews }
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews return qs;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews}
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews/*% constructs a sql dbinstance (DBI) */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrewsisc_result_t
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrewssdlzh_build_sqldbinstance(isc_mem_t *mctx, const char *allnodes_str,
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews const char *allowxfr_str, const char *authority_str,
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews const char *findzone_str, const char *lookup_str,
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews const char *countzone_str, dbinstance_t **dbi)
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews{
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews isc_result_t result;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews dbinstance_t *db = NULL;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews REQUIRE(dbi != NULL && *dbi == NULL);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews REQUIRE(mctx != NULL);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* allocate and zero memory for driver structure */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews db = isc_mem_get(mctx, sizeof(dbinstance_t));
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews if (db == NULL) {
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews DNS_LOGMODULE_DLZ, ISC_LOG_ERROR,
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews "Could not allocate memory for "
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews "database instance object.");
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews return (ISC_R_NOMEMORY);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews }
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews memset(db, 0, sizeof(dbinstance_t));
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews db->dbconn = NULL;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews db->client = NULL;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews db->record = NULL;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews db->zone = NULL;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews db->mctx = NULL;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews db->query_buf = NULL;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews db->allnodes_q = NULL;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews db->allowxfr_q = NULL;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews db->authority_q = NULL;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews db->findzone_q = NULL;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews db->countzone_q = NULL;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews db->lookup_q = NULL;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* attach to the memory context */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews isc_mem_attach(mctx, &db->mctx);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* initialize the reference count mutex */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews result = isc_mutex_init(&db->instance_lock);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews if (result != ISC_R_SUCCESS) {
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews UNEXPECTED_ERROR(__FILE__, __LINE__,
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews "isc_mutex_init() failed: %s",
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews isc_result_totext(result));
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews goto cleanup;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews }
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* build the all nodes query list */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews result = build_querylist(mctx, allnodes_str, &db->zone,
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews &db->record, &db->client,
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews &db->allnodes_q, SDLZH_REQUIRE_ZONE);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* if unsuccessful, log err msg and cleanup */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews if (result != ISC_R_SUCCESS) {
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews DNS_LOGMODULE_DLZ, ISC_LOG_ERROR,
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews "Could not build all nodes query list");
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews goto cleanup;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews }
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* build the allow zone transfer query list */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews result = build_querylist(mctx, allowxfr_str, &db->zone,
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews &db->record, &db->client,
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews &db->allowxfr_q,
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews SDLZH_REQUIRE_ZONE | SDLZH_REQUIRE_CLIENT);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* if unsuccessful, log err msg and cleanup */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews if (result != ISC_R_SUCCESS) {
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews DNS_LOGMODULE_DLZ, ISC_LOG_ERROR,
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews "Could not build allow xfr query list");
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews goto cleanup;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews }
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* build the authority query, query list */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews result = build_querylist(mctx, authority_str, &db->zone,
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews &db->record, &db->client,
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews &db->authority_q, SDLZH_REQUIRE_ZONE);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* if unsuccessful, log err msg and cleanup */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews if (result != ISC_R_SUCCESS) {
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews DNS_LOGMODULE_DLZ, ISC_LOG_ERROR,
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews "Could not build authority query list");
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews goto cleanup;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews }
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* build findzone query, query list */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews result = build_querylist(mctx, findzone_str, &db->zone,
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews &db->record, &db->client,
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews &db->findzone_q, SDLZH_REQUIRE_ZONE);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* if unsuccessful, log err msg and cleanup */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews if (result != ISC_R_SUCCESS) {
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews DNS_LOGMODULE_DLZ, ISC_LOG_ERROR,
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews "Could not build find zone query list");
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews goto cleanup;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews }
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* build countzone query, query list */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews result = build_querylist(mctx, countzone_str, &db->zone,
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews &db->record, &db->client,
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews &db->countzone_q, SDLZH_REQUIRE_ZONE);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* if unsuccessful, log err msg and cleanup */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews if (result != ISC_R_SUCCESS) {
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews DNS_LOGMODULE_DLZ, ISC_LOG_ERROR,
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews "Could not build count zone query list");
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews goto cleanup;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews }
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* build lookup query, query list */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews result = build_querylist(mctx, lookup_str, &db->zone,
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews &db->record, &db->client,
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews &db->lookup_q, SDLZH_REQUIRE_RECORD);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* if unsuccessful, log err msg and cleanup */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews if (result != ISC_R_SUCCESS) {
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews DNS_LOGMODULE_DLZ, ISC_LOG_ERROR,
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews "Could not build lookup query list");
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews goto cleanup;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews }
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* pass back the db instance */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews *dbi = (dbinstance_t *) db;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* return success */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews return (ISC_R_SUCCESS);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews cleanup:
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* destroy whatever was build of the db instance */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews destroy_sqldbinstance(db);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* return failure */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews return (ISC_R_FAILURE);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews}
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrewsvoid
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrewssdlzh_destroy_sqldbinstance(dbinstance_t *dbi)
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews{
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews isc_mem_t *mctx;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* save mctx for later */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews mctx = dbi->mctx;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* destroy any query lists we created */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews destroy_querylist(mctx, &dbi->allnodes_q);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews destroy_querylist(mctx, &dbi->allowxfr_q);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews destroy_querylist(mctx, &dbi->authority_q);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews destroy_querylist(mctx, &dbi->findzone_q);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews destroy_querylist(mctx, &dbi->countzone_q);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews destroy_querylist(mctx, &dbi->lookup_q);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* get rid of the mutex */
6f7abb89ec22aef5eda40ed60fcf605a42b78d4dMark Andrews (void) isc_mutex_destroy(&dbi->instance_lock);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* return, and detach the memory */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews isc_mem_put(mctx, dbi, sizeof(dbinstance_t));
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews isc_mem_detach(&mctx);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews}
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrewschar *
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrewssdlzh_get_parameter_value(isc_mem_t *mctx, const char *input, const char* key)
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews{
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews int keylen;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews char *keystart;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews char value[255];
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews int i;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews if (key == NULL || input == NULL || strlen(input) < 1)
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews return NULL;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews keylen = strlen(key);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews if (keylen < 1)
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews return NULL;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews keystart = strstr(input, key);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews if (keystart == NULL)
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews return NULL;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews REQUIRE(mctx != NULL);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews for (i = 0; i < 255; i++) {
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews value[i] = keystart[keylen + i];
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews if (value[i] == ' ' || value[i] == '\0') {
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews value[i] = '\0';
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews break;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews }
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews }
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews return isc_mem_strdup(mctx, value);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews}