summaryrefslogtreecommitdiffstats
path: root/contrib/dlz/modules/common/dlz_dbi.c
blob: 0c742b59c382d7ce91bfa8933e7f7a308cbbd248 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
/*
 * Copyright (C) 2002 Stichting NLnet, Netherlands, stichting@nlnet.nl.
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the
 * above copyright notice and this permission notice appear in all
 * copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND STICHTING NLNET
 * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
 * STICHTING NLNET BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
 * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE
 * USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 * The development of Dynamically Loadable Zones (DLZ) for Bind 9 was
 * conceived and contributed by Rob Butler.
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the
 * above copyright notice and this permission notice appear in all
 * copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND ROB BUTLER
 * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
 * ROB BUTLER BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
 * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE
 * USE OR PERFORMANCE OF THIS SOFTWARE.
 */

/*
 * Copyright (C) 1999-2001, 2013, 2016  Internet Systems Consortium, Inc. ("ISC")
 * 
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <stdlib.h>
#include <ctype.h>

#include <sys/errno.h>

#include <dlz_minimal.h>
#include <dlz_list.h>
#include <dlz_dbi.h>
#include <dlz_pthread.h>

/*%
 * properly destroys a querylist by de-allocating the
 * memory for each query segment, and then the list itself
 */

void
destroy_querylist(query_list_t **querylist) {
	query_segment_t *tseg = NULL;
	query_segment_t *nseg = NULL;

	/* if query list is null, nothing to do */
	if (*querylist == NULL)
		return;

	/* start at the top of the list */
	nseg = DLZ_LIST_HEAD(**querylist);
	while (nseg != NULL) {	/* loop, until end of list */
		tseg = nseg;
		/*
		 * free the query segment's text string but only if it
		 * was really a query segment, and not a pointer to
		 * %zone%, or %record%, or %client%
		*/
		if (tseg->cmd != NULL && tseg->direct == true)
			free(tseg->cmd);
		/* get the next query segment, before we destroy this one. */
		nseg = DLZ_LIST_NEXT(nseg, link);
		/* deallocate this query segment. */
		free(tseg);
	}
	/* deallocate the query segment list */
	free(*querylist);
}

/*% constructs a query list by parsing a string into query segments */
isc_result_t
build_querylist(const char *query_str, char **zone, char **record,
		char **client, query_list_t **querylist, unsigned int flags, 
		log_t log)
{
	isc_result_t result;
	bool foundzone = false;
	bool foundrecord = false;
	bool foundclient = false;
	char *temp_str = NULL;
	char *right_str = NULL;
	query_list_t *tql;
	query_segment_t *tseg = NULL;

	/* if query string is null, or zero length */
	if (query_str == NULL || strlen(query_str) < 1) {
		if ((flags & REQUIRE_QUERY) == 0)
			/* we don't need it were ok. */
			return (ISC_R_SUCCESS);
		else
			/* we did need it, PROBLEM!!! */
			return (ISC_R_FAILURE);
	}

	/* allocate memory for query list */
	tql = calloc(1, sizeof(query_list_t));
	/* couldn't allocate memory.  Problem!! */
	if (tql == NULL)
		return (ISC_R_NOMEMORY);

	/* initialize the query segment list */
	DLZ_LIST_INIT(*tql);

	/* make a copy of query_str so we can chop it up */
	temp_str = right_str = strdup(query_str);
	/* couldn't make a copy, problem!! */
	if (right_str == NULL) {
		result = ISC_R_NOMEMORY;
		goto cleanup;
	}

	/* loop through the string and chop it up */
	while (right_str != NULL) {
		/* allocate memory for tseg */
		tseg = calloc(1, sizeof(query_segment_t));
		if (tseg  == NULL) {	/* no memory, clean everything up. */
			result = ISC_R_NOMEMORY;
			goto cleanup;
		}
		tseg->cmd = NULL;
		tseg->direct = false;
		/* initialize the query segment link */
		DLZ_LINK_INIT(tseg, link);
		/* append the query segment to the list */
		DLZ_LIST_APPEND(*tql, tseg, link);

		/*
		 * split string at the first "$". set query segment to
		 * left portion
		 */
		tseg->cmd = strdup(strsep(&right_str, "$"));
		if (tseg->cmd == NULL) {
			/* no memory, clean everything up. */
			result = ISC_R_NOMEMORY;
			goto cleanup;
		}
		/* tseg->cmd points directly to a string. */
		tseg->direct = true;
		tseg->strlen = strlen(tseg->cmd);

		/* check if we encountered "$zone$" token */
		if (strcasecmp(tseg->cmd, "zone") == 0) {
			/*
			 * we don't really need, or want the "zone"
			 * text, so get rid of it.
			 */
			free(tseg->cmd);
			/* set tseg->cmd to in-direct zone string */
			tseg->cmd = (char**) zone;
			tseg->strlen = 0;
			/* tseg->cmd points in-directly to a string */
			tseg->direct = false;
			foundzone = true;
			/* check if we encountered "$record$" token */
		} else if (strcasecmp(tseg->cmd, "record") == 0) {
			/*
			 * we don't really need, or want the "record"
			 * text, so get rid of it.
			 */
			free(tseg->cmd);
			/* set tseg->cmd to in-direct record string */
			tseg->cmd = (char**) record;
			tseg->strlen = 0;
			/* tseg->cmd points in-directly poinsts to a string */
			tseg->direct = false;
			foundrecord = true;
			/* check if we encountered "$client$" token */
		} else if (strcasecmp(tseg->cmd, "client") == 0) {
			/*
			 * we don't really need, or want the "client"
			 * text, so get rid of it.
			 */
			free(tseg->cmd);
			/* set tseg->cmd to in-direct record string */
			tseg->cmd = (char**) client;
			tseg->strlen = 0;
			/* tseg->cmd points in-directly poinsts to a string */
			tseg->direct = false;
			foundclient = true;
		}
	}

	/* we don't need temp_str any more */
	free(temp_str);
	/*
	 * add checks later to verify zone and record are found if
	 * necessary.
	 */

	/* if this query requires %client%, make sure we found it */
	if (((flags & REQUIRE_CLIENT) != 0) && (!foundclient) ) {
		/* Write error message to log */
		if (log != NULL)
			log(ISC_LOG_ERROR,
			    "Required token $client$ not found.");
		result = ISC_R_FAILURE;
		goto flag_fail;
	}

	/* if this query requires %record%, make sure we found it */
	if (((flags & REQUIRE_RECORD) != 0) && (!foundrecord) ) {
		/* Write error message to log */
		if (log != NULL)
			log(ISC_LOG_ERROR,
			    "Required token $record$ not found.");
		result = ISC_R_FAILURE;
		goto flag_fail;
	}

	/* if this query requires %zone%, make sure we found it */
	if (((flags & REQUIRE_ZONE) != 0) && (!foundzone) ) {
		/* Write error message to log */
		if (log != NULL)
			log(ISC_LOG_ERROR, "Required token $zone$ not found.");
		result = ISC_R_FAILURE;
		goto flag_fail;
	}

	/* pass back the query list */
	*querylist = (query_list_t *) tql;

	/* return success */
	return (ISC_R_SUCCESS);

 cleanup:
	/* get rid of temp_str */
	if (temp_str != NULL)
		free(temp_str);

 flag_fail:
	/* get rid of what was build of the query list */
	if (tql != NULL)
		destroy_querylist(&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 *
build_querystring(query_list_t *querylist) {
	query_segment_t *tseg = NULL;
	unsigned int length = 0;
	char *qs = NULL;

	/* start at the top of the list */
	tseg = DLZ_LIST_HEAD(*querylist);
	while (tseg != NULL) {
		/*
		 * if this is a query segment, use the
		 * precalculated string length
		 */
		if (tseg->direct == true)
			length += tseg->strlen;
		else	/* calculate string length for dynamic segments. */
			length += strlen(* (char**) tseg->cmd);
		/* get the next segment */
		tseg = DLZ_LIST_NEXT(tseg, link);
	}

	qs = malloc(length + 1);
	if (qs == NULL)
		return (NULL);

	*qs = '\0';
	/* start at the top of the list again */
	tseg = DLZ_LIST_HEAD(*querylist);
	while (tseg != NULL) {
		if (tseg->direct == true)
			/* query segments */
			strcat(qs, tseg->cmd);
		else
			/* dynamic segments */
			strcat(qs, * (char**) tseg->cmd);
		/* get the next segment */
		tseg = DLZ_LIST_NEXT(tseg, link);
	}

	return (qs);
}

/*% constructs a dbinstance (DBI) */
isc_result_t
build_dbinstance(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, log_t log)
{

	isc_result_t result;
	dbinstance_t *db = NULL;
	int err;

	/* allocate and zero memory for driver structure */
	db = calloc(1, sizeof(dbinstance_t));
	if (db == NULL) {
		if (log != NULL)
			log(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->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;

	/* initialize the reference count mutex */
	err = dlz_mutex_init(&db->lock, NULL);
	if (err == ENOMEM) {
		result = ISC_R_NOMEMORY;
		goto cleanup;
	} else if (err != 0) {
		result = ISC_R_UNEXPECTED;
		goto cleanup;
	}

	/* build the all nodes query list */
	result = build_querylist(allnodes_str, &db->zone, &db->record,
				 &db->client, &db->allnodes_q,
				 REQUIRE_ZONE, log);
	/* if unsuccessful, log err msg and cleanup */
	if (result != ISC_R_SUCCESS) {
		if (log != NULL)
			log(ISC_LOG_ERROR,
			    "Could not build all nodes query list");
		goto cleanup;
	}

	/* build the allow zone transfer query list */
	result = build_querylist(allowxfr_str, &db->zone, &db->record,
				 &db->client, &db->allowxfr_q,
				 REQUIRE_ZONE | REQUIRE_CLIENT,
				 log);
	/* if unsuccessful, log err msg and cleanup */
	if (result != ISC_R_SUCCESS) {
		if (log != NULL)
			log(ISC_LOG_ERROR,
			    "Could not build allow xfr query list");
		goto cleanup;
	}

	/* build the authority query, query list */
	result = build_querylist(authority_str, &db->zone, &db->record,
				 &db->client, &db->authority_q,
				 REQUIRE_ZONE, log);
	/* if unsuccessful, log err msg and cleanup */
	if (result != ISC_R_SUCCESS) {
		if (log != NULL)
			log(ISC_LOG_ERROR,
			    "Could not build authority query list");
		goto cleanup;
	}

	/* build findzone query, query list */
	result = build_querylist(findzone_str, &db->zone, &db->record,
				 &db->client, &db->findzone_q,
				 REQUIRE_ZONE, log);
	/* if unsuccessful, log err msg and cleanup */
	if (result != ISC_R_SUCCESS) {
		if (log != NULL)
			log(ISC_LOG_ERROR,
			    "Could not build find zone query list");
		goto cleanup;
	}

	/* build countzone query, query list */
	result = build_querylist(countzone_str, &db->zone, &db->record,
				 &db->client, &db->countzone_q,
				 REQUIRE_ZONE, log);
	/* if unsuccessful, log err msg and cleanup */
	if (result != ISC_R_SUCCESS) {
		if (log != NULL)
			log(ISC_LOG_ERROR,
			    "Could not build count zone query list");
		goto cleanup;
	}

	/* build lookup query, query list */
	result = build_querylist(lookup_str, &db->zone, &db->record,
				 &db->client, &db->lookup_q,
				 REQUIRE_RECORD, log);
	/* if unsuccessful, log err msg and cleanup */
	if (result != ISC_R_SUCCESS) {
		if (log != NULL)
			log(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_dbinstance(db);
	/* return failure */
	return (ISC_R_FAILURE);
}

void
destroy_dbinstance(dbinstance_t *dbi) {
	/* destroy any query lists we created */
	destroy_querylist(&dbi->allnodes_q);
	destroy_querylist(&dbi->allowxfr_q);
	destroy_querylist(&dbi->authority_q);
	destroy_querylist(&dbi->findzone_q);
	destroy_querylist(&dbi->countzone_q);
	destroy_querylist(&dbi->lookup_q);

	/* get rid of the mutex */
	(void) dlz_mutex_destroy(&dbi->lock);

	/* return, and detach the memory */
	free(dbi);
}

char *
get_parameter_value(const char *input, const char* key) {
	int keylen;
	char *keystart;
	char value[255];
	int i;

	if (key == NULL || input == NULL || *input == '\0')
		return (NULL);

	keylen = strlen(key);

	if (keylen < 1)
		return (NULL);

	keystart = strstr(input, key);

	if (keystart == NULL)
		return (NULL);

	for (i = 0; i < 255; i++) {
		value[i] = keystart[keylen + i];
		if (isspace(value[i]) || value[i] == '\0') {
			value[i] = '\0';
			break;
		}
	}

	return (strdup(value));
}