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