03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * Copyright (C) 2002 Stichting NLnet, Netherlands, stichting@nlnet.nl.
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 * 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 * The development of Dynamically Loadable Zones (DLZ) for Bind 9 was
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * conceived and contributed by Rob Butler.
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 * 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.
02d54949f0f1db4729e14c3322b207f58d2578a4Mark Andrews * Copyright (C) 1999-2001, 2016 Internet Systems Consortium, Inc. ("ISC")
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 * sdlz helper methods
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * properly destroys a querylist by de-allocating the
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * memory for each query segment, and then the list itself
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrewsdestroy_querylist(isc_mem_t *mctx, query_list_t **querylist)
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* if query list is null, nothing to do */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* start at the top of the list */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews while (nseg != NULL) { /* loop, until end of list */
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 if (tseg->sql != NULL && tseg->direct == isc_boolean_true)
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* get the next query segment, before we destroy this one. */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* deallocate this query segment. */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews isc_mem_put(mctx, tseg, sizeof(query_segment_t));
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* deallocate the query segment list */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews isc_mem_put(mctx, *querylist, sizeof(query_list_t));
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews/*% constructs a query list by parsing a string into query segments */
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 REQUIRE(querylist != NULL && *querylist == NULL);
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* if query string is null, or zero length */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews if (query_str == NULL || strlen(query_str) < 1) {
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* we don't need it were ok. */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* we did need it, PROBLEM!!! */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* allocate memory for query list */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* couldn't allocate memory. Problem!! */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* initialize the query segment list */
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 /* loop through the string and chop it up */
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 /* initialize the query segment link */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* append the query segment to the list */
dc64df4479df5e7cb3ed1dead21888b5af98f67eMark Andrews * split string at the first "$". set query segment to
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * left portion
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* no memory, clean everything up. */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* tseg->sql points directly to a string. */
dc64df4479df5e7cb3ed1dead21888b5af98f67eMark Andrews /* check if we encountered "$zone$" token */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * we don't really need, or want the "zone"
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * text, so get rid of it.
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* set tseg->sql to in-direct zone string */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* tseg->sql points in-directly to a string */
dc64df4479df5e7cb3ed1dead21888b5af98f67eMark Andrews /* check if we encountered "$record$" token */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews } else if (strcasecmp(tseg->sql, "record") == 0) {
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * we don't really need, or want the "record"
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * text, so get rid of it.
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* set tseg->sql to in-direct record string */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* tseg->sql points in-directly poinsts to a string */
dc64df4479df5e7cb3ed1dead21888b5af98f67eMark Andrews /* check if we encountered "$client$" token */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews } else if (strcasecmp(tseg->sql, "client") == 0) {
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * we don't really need, or want the "client"
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * text, so get rid of it.
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* set tseg->sql to in-direct record string */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* tseg->sql points in-directly poinsts to a string */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* we don't need temp_str any more */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * add checks later to verify zone and record are found if
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * necessary.
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,
dc64df4479df5e7cb3ed1dead21888b5af98f67eMark Andrews "Required token $client$ not found.");
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,
dc64df4479df5e7cb3ed1dead21888b5af98f67eMark Andrews "Required token $record$ not found.");
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,
dc64df4479df5e7cb3ed1dead21888b5af98f67eMark Andrews "Required token $zone$ not found.");
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* pass back the query list */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* return success */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* get rid of temp_str */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* get rid of what was build of the query list */
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 Andrewssdlzh_build_querystring(isc_mem_t *mctx, query_list_t *querylist)
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* start at the top of the list */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * if this is a query segment, use the
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews * precalculated string length
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews else /* calculate string length for dynamic segments. */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* get the next segment */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* allocate memory for the string */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* couldn't allocate memory, We need more ram! */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* start at the top of the list again */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* query segments */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* dynamic segments */
611dc8876869036ab5e981e53ae7a446145d9354Mark Andrews /* get the next segment */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews/*% constructs a sql dbinstance (DBI) */
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 /* allocate and zero memory for driver structure */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews "Could not allocate memory for "
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews "database instance object.");
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* attach to the memory context */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* initialize the reference count mutex */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews "isc_mutex_init() failed: %s",
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* build the all nodes query list */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews result = build_querylist(mctx, allnodes_str, &db->zone,
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* if unsuccessful, log err msg and cleanup */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews "Could not build all nodes query list");
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* build the allow zone transfer query list */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews result = build_querylist(mctx, allowxfr_str, &db->zone,
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* if unsuccessful, log err msg and cleanup */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews "Could not build allow xfr query list");
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* build the authority query, query list */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews result = build_querylist(mctx, authority_str, &db->zone,
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* if unsuccessful, log err msg and cleanup */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews "Could not build authority query list");
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* build findzone query, query list */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews result = build_querylist(mctx, findzone_str, &db->zone,
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* if unsuccessful, log err msg and cleanup */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews "Could not build find zone query list");
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* build countzone query, query list */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews result = build_querylist(mctx, countzone_str, &db->zone,
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* if unsuccessful, log err msg and cleanup */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews "Could not build count zone query list");
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* build lookup query, query list */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews result = build_querylist(mctx, lookup_str, &db->zone,
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* if unsuccessful, log err msg and cleanup */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews "Could not build lookup query list");
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* pass back the db instance */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* return success */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* destroy whatever was build of the db instance */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* return failure */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* save mctx for later */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* destroy any query lists we created */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* get rid of the mutex */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /* return, and detach the memory */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrewssdlzh_get_parameter_value(isc_mem_t *mctx, const char *input, const char* key)
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews if (key == NULL || input == NULL || strlen(input) < 1)
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews for (i = 0; i < 255; i++) {