dlz_filesystem_driver.c 25.4 KB
Newer Older
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
/*
 * 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.
 */

/*
Mark Andrews's avatar
Mark Andrews committed
37
 * Copyright (C) 1999-2001, 2016  Internet Systems Consortium, Inc. ("ISC")
Ondřej Surý's avatar
Ondřej Surý committed
38
 *
Mark Andrews's avatar
Mark Andrews committed
39
40
41
 * 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/.
42
43
44
45
46
 */

#ifdef DLZ_FILESYSTEM

#include <config.h>
47
#include <stdbool.h>
48
49
#include <stdio.h>
#include <stdlib.h>
50
#include <string.h>
51
52
53
54
55
56
57
58
59
60
61
62

#include <sys/stat.h>

#include <dns/log.h>
#include <dns/sdlz.h>
#include <dns/result.h>

#include <isc/dir.h>
#include <isc/mem.h>
#include <isc/platform.h>
#include <isc/print.h>
#include <isc/result.h>
63
#include <isc/string.h>
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <isc/util.h>

#include <named/globals.h>

#include <dlz/dlz_filesystem_driver.h>

static dns_sdlzimplementation_t *dlz_fs = NULL;

typedef struct config_data {
	char		*basedir;
	int		basedirsize;
	char		*datadir;
	int		datadirsize;
	char		*xfrdir;
	int		xfrdirsize;
	int		splitcnt;
	char		separator;
	char		pathsep;
	isc_mem_t	*mctx;
} config_data_t;

typedef struct dir_entry dir_entry_t;

struct dir_entry {
Ondřej Surý's avatar
Ondřej Surý committed
88
	char dirpath[PATH_MAX];
89
90
91
92
93
94
95
96
97
98
99
100
101
102
	ISC_LINK(dir_entry_t)	link;
};

typedef ISC_LIST(dir_entry_t) dlist_t;

/* forward reference */

static void
fs_destroy(void *driverarg, void *dbdata);

/*
 * Private methods
 */

103
static bool
Evan Hunt's avatar
Evan Hunt committed
104
is_safe(const char *input) {
105
106
107
108
109
110
111
112
113
	unsigned int i;
	unsigned int len = strlen(input);

        /* check that only allowed characters  are in the domain name */
	for (i=0; i < len; i++) {
		/* '.' is allowed, but has special requirements */
		if (input[i] == '.') {
			/* '.' is not allowed as first char */
			if (i == 0)
114
				return (false);
115
116
			/* '..', two dots together is not allowed. */
			else if (input[i-1] == '.')
117
				return (false);
118
119
			/* '.' is not allowed as last char */
			if (i == len)
120
				return (false);
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
			/* only 1 dot in ok location, continue at next char */
			continue;
		}
		/* '-' is allowed, continue at next char */
		if (input[i] == '-')
			continue;
		/* 0-9 is allowed, continue at next char */
		if (input[i] >= '0' && input[i] <= '9')
			continue;
		/* A-Z uppercase is allowed, continue at next char */
		if (input[i] >= 'A' && input[i] <= 'Z')
			continue;
		/* a-z lowercase is allowed, continue at next char */
		if (input[i] >= 'a' && input[i] <= 'z')
			continue;

		/*
		 * colon needs to be allowed for IPV6 client
		 * addresses.  Not dangerous in domain names, as not a
		 * special char.
		 */
		if (input[i] == ':')
			continue;

		/*
		 * '@' needs to be allowed for in zone data.  Not
		 * dangerous in domain names, as not a special char.
		 */
		if (input[i] == '@')
			continue;

		/*
		 * if we reach this point we have encountered a
		 * disallowed char!
		 */
156
		return (false);
157
158
	}
        /* everything ok. */
159
	return (true);
160
161
162
}

static isc_result_t
Evan Hunt's avatar
Evan Hunt committed
163
create_path_helper(char *out, const char *in, config_data_t *cd) {
164
165
166
167
	char *tmpString;
	char *tmpPtr;
	int i;

168
	tmpString = isc_mem_strdup(named_g_mctx, in);
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
	if (tmpString == NULL)
		return (ISC_R_NOMEMORY);

	/*
	 * don't forget is_safe guarantees '.' will NOT be the
	 * first/last char
	 */
	while ((tmpPtr = strrchr(tmpString, '.')) != NULL) {
		i = 0;
		while (tmpPtr[i+1] != '\0') {
			if (cd->splitcnt < 1)
				strcat(out, (char *) &tmpPtr[i+1]);
			else
				strncat(out, (char *) &tmpPtr[i+1],
					cd->splitcnt);
			strncat(out, (char *) &cd->pathsep, 1);
			if (cd->splitcnt == 0)
				break;
			if (strlen((char *) &tmpPtr[i+1]) <=
			    (unsigned int) cd->splitcnt)
				break;
			i += cd->splitcnt;
		}
		tmpPtr[0] = '\0';
	}

	/* handle the "first" label properly */
	i=0;
	tmpPtr = tmpString;
	while (tmpPtr[i] != '\0') {
		if (cd->splitcnt < 1)
			strcat(out, (char *) &tmpPtr[i]);
		else
			strncat(out, (char *) &tmpPtr[i], cd->splitcnt);
		strncat(out, (char *) &cd->pathsep, 1);
		if (cd->splitcnt == 0)
			break;
		if (strlen((char *) &tmpPtr[i]) <=
		    (unsigned int) cd->splitcnt)
			break;
		i += cd->splitcnt;
	}

212
	isc_mem_free(named_g_mctx, tmpString);
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
	return (ISC_R_SUCCESS);
}

/*%
 * Checks to make sure zone and host are safe.  If safe, then
 * hashes zone and host strings to build a path.  If zone / host
 * are not safe an error is returned.
 */

static isc_result_t
create_path(const char *zone, const char *host, const char *client,
	    config_data_t *cd, char **path)
{

	char *tmpPath;
	int pathsize;
	int len;
	isc_result_t result;
231
	bool isroot = false;
232
233
234
235
236
237
238
239
240
241
242
243
244
245

	/* we require a zone & cd parameter */
	REQUIRE(zone != NULL);
	REQUIRE(cd != NULL);
	/* require path to be a pointer to NULL */
	REQUIRE(path != NULL && *path == NULL);
	/*
	 * client and host may both be NULL, but they can't both be
	 * NON-NULL
	 */
	REQUIRE( (host == NULL && client == NULL) ||
		 (host != NULL && client == NULL) ||
		 (host == NULL && client != NULL) );

Evan Hunt's avatar
Evan Hunt committed
246
247
	/* special case for root zone */
	if (strcmp(zone, ".") == 0)
248
		isroot = true;
Evan Hunt's avatar
Evan Hunt committed
249

250
	/* if the requested zone is "unsafe", return error */
Evan Hunt's avatar
Evan Hunt committed
251
	if (!isroot && !is_safe(zone))
252
253
254
		return (ISC_R_FAILURE);

	/* if host was passed, verify that it is safe */
Evan Hunt's avatar
Evan Hunt committed
255
	if (host != NULL && !is_safe(host))
256
257
		return (ISC_R_FAILURE);

Francis Dupont's avatar
Francis Dupont committed
258
	/* if client was passed, verify that it is safe */
Evan Hunt's avatar
Evan Hunt committed
259
	if (client != NULL && !is_safe(client))
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
		return (ISC_R_FAILURE);

	/* Determine how much memory the split up string will require */
	if (host != NULL)
		len = strlen(zone) + strlen(host);
	else if (client != NULL)
		len = strlen(zone) + strlen(client);
	else
		len = strlen(zone);

	/*
	 * even though datadir and xfrdir will never be in the same
	 * string we only waste a few bytes by allocating for both,
	 * and then we are safe from buffer overruns.
	 */
	pathsize = len + cd->basedirsize +
		   cd->datadirsize + cd->xfrdirsize + 4;

	/* if we are splitting names, we will need extra space. */
	if (cd->splitcnt > 0)
		pathsize += len/cd->splitcnt;

282
	tmpPath = isc_mem_allocate(named_g_mctx , pathsize * sizeof(char));
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
	if (tmpPath == NULL) {
		/* write error message */
		isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
			      DNS_LOGMODULE_DLZ, ISC_LOG_ERROR,
			      "Filesystem driver unable to "
			      "allocate memory in create_path().");
		result = ISC_R_NOMEMORY;
		goto cleanup_mem;
	}

	/*
	 * build path string.
	 * start out with base directory.
	 */
	strcpy(tmpPath, cd->basedir);

	/* add zone name - parsed properly */
Evan Hunt's avatar
Evan Hunt committed
300
301
302
303
304
	if (!isroot) {
		result = create_path_helper(tmpPath, zone, cd);
		if (result != ISC_R_SUCCESS)
			goto cleanup_mem;
	}
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

	/*
	 * When neither client or host is passed we are building a
	 * path to see if a zone is supported.  We require that a zone
	 * path have the "data dir" directory contained within it so
	 * that we know this zone is really supported.  Otherwise,
	 * this zone may not really be supported because we are
	 * supporting a delagated sub zone.
	 *
	 * Example:
	 *
	 * We are supporting long.domain.com and using a splitcnt of
	 * 0.  the base dir is "/base-dir/" and the data dir is
	 * "/.datadir" We want to see if we are authoritative for
	 * domain.com.  Path /base-dir/com/domain/.datadir since
	 * /base-dir/com/domain/.datadir does not exist, we are not
	 * authoritative for the domain "domain.com".  However we are
	 * authoritative for the domain "long.domain.com" because the
	 * path /base-dir/com/domain/long/.datadir does exist!
	 */

	/* if client is passed append xfr dir, otherwise append data dir */
	if (client != NULL) {
		strcat(tmpPath, cd->xfrdir);
		strncat(tmpPath, (char *) &cd->pathsep, 1);
		strcat(tmpPath, client);
	} else {
		strcat(tmpPath, cd->datadir);
	}

	/* if host not null, add it. */
	if (host != NULL) {
		strncat(tmpPath, (char *) &cd->pathsep, 1);
		if ((result = create_path_helper(tmpPath, host,
						 cd)) != ISC_R_SUCCESS)
			goto cleanup_mem;
	}

	/* return the path we built. */
	*path = tmpPath;

	/* return success */
	result = ISC_R_SUCCESS;

 cleanup_mem:
	/* cleanup memory */

	/* free tmpPath memory */
	if (tmpPath != NULL && result != ISC_R_SUCCESS)
354
		isc_mem_free(named_g_mctx, tmpPath);
355
356

	/* free tmpPath memory */
Evan Hunt's avatar
Evan Hunt committed
357
	return (result);
358
359
360
}

static isc_result_t
Evan Hunt's avatar
Evan Hunt committed
361
process_dir(isc_dir_t *dir, void *passback, config_data_t *cd,
362
363
364
	    dlist_t *dir_list, unsigned int basedirlen)
{

Ondřej Surý's avatar
Ondřej Surý committed
365
	char tmp[PATH_MAX + NAME_MAX];
366
367
368
369
370
371
372
	int astPos;
	struct stat	sb;
	isc_result_t result = ISC_R_FAILURE;
	char *endp;
	char *type;
	char *ttlStr;
	char *data;
Ondřej Surý's avatar
Ondřej Surý committed
373
	char host[NAME_MAX];
374
375
376
377
378
379
	char *tmpString;
	char *tmpPtr;
	int ttl;
	int i;
	int len;
	dir_entry_t *direntry;
380
	bool foundHost;
381
382
383

	tmp[0] = '\0'; /* set 1st byte to '\0' so strcpy works right. */
	host[0] = '\0';
384
	foundHost = false;
385
386

	/* copy base directory name to tmp. */
Evan Hunt's avatar
Evan Hunt committed
387
	strcpy(tmp, dir->dirname);
388

Evan Hunt's avatar
Evan Hunt committed
389
390
	/* dir->dirname will always have '*' as the last char. */
	astPos = strlen(dir->dirname) - 1;
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407

	/* if dir_list != NULL, were are performing a zone xfr */
	if (dir_list != NULL) {
		/* if splitcnt == 0, determine host from path. */
		if (cd->splitcnt == 0) {
			if (strlen(tmp) - 3 > basedirlen) {
				tmp[astPos-1] = '\0';
				tmpString = (char *) &tmp[basedirlen+1];
				/* handle filesystem's special wildcard "-"  */
				if (strcmp(tmpString, "-") == 0) {
					strcpy(host, "*");
				} else {
					/*
					 * not special wildcard -- normal name
					 */
					while ((tmpPtr = strrchr(tmpString,
								 cd->pathsep))
Evan Hunt's avatar
Evan Hunt committed
408
409
410
411
					       != NULL)
					{
						if ((strlen(host) +
						     strlen(tmpPtr + 1) + 2)
Ondřej Surý's avatar
Ondřej Surý committed
412
						    > NAME_MAX)
Evan Hunt's avatar
Evan Hunt committed
413
							continue;
414
415
416
417
						strcat(host, tmpPtr + 1);
						strcat(host, ".");
						tmpPtr[0] = '\0';
					}
Evan Hunt's avatar
Evan Hunt committed
418
419
					if ((strlen(host) +
					     strlen(tmpString) + 1)
Ondřej Surý's avatar
Ondřej Surý committed
420
					    <= NAME_MAX)
Evan Hunt's avatar
Evan Hunt committed
421
						strcat(host, tmpString);
422
423
				}

424
				foundHost = true;
425
				/* set tmp again for use later */
Evan Hunt's avatar
Evan Hunt committed
426
				strcpy(tmp, dir->dirname);
427
428
429
430
431
432
			}
		} else {
			/*
			 * if splitcnt != 0 determine host from
			 * ".host" directory entry
			 */
Evan Hunt's avatar
Evan Hunt committed
433
			while (isc_dir_read(dir) == ISC_R_SUCCESS) {
434
				if (strncasecmp(".host",
Evan Hunt's avatar
Evan Hunt committed
435
						dir->entry.name, 5) == 0) {
436
437
438
439
					/*
					 * handle filesystem's special
					 * wildcard "-"
					 */
Evan Hunt's avatar
Evan Hunt committed
440
					if (strcmp((char *) &dir->entry.name[6],
441
						   "-") == 0)
442
443
444
445
					{
						strlcpy(host, "*", sizeof(host));
					} else {
						strlcpy(host,
Evan Hunt's avatar
Evan Hunt committed
446
						   (char *) &dir->entry.name[6],
447
						   sizeof(host));
Evan Hunt's avatar
Evan Hunt committed
448
					}
449
					foundHost = true;
450
451
452
453
					break;
				}
			}
			/* reset dir list for use later */
Evan Hunt's avatar
Evan Hunt committed
454
			isc_dir_reset(dir);
455
456
457
		} /* end of else */
	}

Evan Hunt's avatar
Evan Hunt committed
458
	while (isc_dir_read(dir) == ISC_R_SUCCESS) {
459
460
461
462
463

		isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
			      DNS_LOGMODULE_DLZ, ISC_LOG_DEBUG(1),
			      "Filesystem driver Dir name:"
			      " '%s' Dir entry: '%s'\n",
Evan Hunt's avatar
Evan Hunt committed
464
			      dir->dirname, dir->entry.name);
465
466

		/* skip any entries starting with "." */
Evan Hunt's avatar
Evan Hunt committed
467
		if (dir->entry.name[0] == '.')
468
469
470
471
472
473
474
475
476
477
478
479
			continue;

		/*
		 * get rid of '*', set to NULL.  Effectively trims
		 * string from previous loop to base directory only
		 * while still leaving memory for concat to be
		 * performed next.
		 */

		tmp[astPos] = '\0';

		/* add name to base directory name. */
Evan Hunt's avatar
Evan Hunt committed
480
		strcat(tmp, dir->entry.name);
481
482
483
484
485
486
487
488
489
490
491

		/* make sure we can stat entry */
		if (stat(tmp, &sb) == 0 ) {
			/* if entry is a directory */
			if ((sb.st_mode & S_IFDIR) != 0) {
				/*
				 * if dir list is NOT NULL, add dir to
				 * dir list
				 */
				if (dir_list != NULL) {
					direntry =
492
					    isc_mem_get(named_g_mctx,
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
							sizeof(dir_entry_t));
					if (direntry == NULL)
						return (ISC_R_NOMEMORY);
					strcpy(direntry->dirpath, tmp);
					ISC_LINK_INIT(direntry, link);
					ISC_LIST_APPEND(*dir_list, direntry,
							link);
					result = ISC_R_SUCCESS;
				}
				continue;

				/*
				 * if entry is a file be sure we do
				 * not add entry to DNS results if we
				 * are performing a zone xfr and we
				 * could not find a host entry.
				 */

			} else if (dir_list != NULL &&
512
				   foundHost == false) {
513
514
515
516
517
				continue;
			}
		} else /* if we cannot stat entry, skip it. */
			continue;

Evan Hunt's avatar
Evan Hunt committed
518
		type = dir->entry.name;
519
520
521
522
523
524
525
		ttlStr = strchr(type,  cd->separator);
		if (ttlStr == NULL) {
			isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
				      DNS_LOGMODULE_DLZ, ISC_LOG_ERROR,
				      "Filesystem driver: "
				      "%s could not be parsed properly",
				      tmp);
Evan Hunt's avatar
Evan Hunt committed
526
			return (ISC_R_FAILURE);
527
528
529
530
531
532
533
534
535
536
537
538
539
540
		}

		/* replace separator char with NULL to split string */
		ttlStr[0] = '\0';
		/* start string after NULL of previous string */
		ttlStr = (char *) &ttlStr[1];

		data = strchr(ttlStr, cd->separator);
		if (data == NULL) {
			isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
				      DNS_LOGMODULE_DLZ, ISC_LOG_ERROR,
				      "Filesystem driver: "
				      "%s could not be parsed properly",
				      tmp);
Evan Hunt's avatar
Evan Hunt committed
541
			return (ISC_R_FAILURE);
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
		}

		/* replace separator char with NULL to split string */
		data[0] = '\0';

		/* start string after NULL of previous string */
		data = (char *) &data[1];

		/* replace all cd->separator chars with a space. */
		len = strlen(data);

		for (i=0; i < len; i++) {
			if (data[i] == cd->separator)
				data[i] = ' ';
		}

		/* convert text to int, make sure it worked right */
		ttl = strtol(ttlStr, &endp, 10);
		if (*endp != '\0' || ttl < 0) {
			isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
				      DNS_LOGMODULE_DLZ, ISC_LOG_ERROR,
				      "Filesystem driver "
				      "ttl must be a postive number");
		}

		/* pass data back to Bind */
		if (dir_list == NULL)
			result = dns_sdlz_putrr((dns_sdlzlookup_t *) passback,
						type, ttl, data);
		else
			result = dns_sdlz_putnamedrr((dns_sdlzallnodes_t *)
						     passback,
						     (char *) host,
						     type, ttl, data);

		/* if error, return error right away */
		if (result != ISC_R_SUCCESS)
Evan Hunt's avatar
Evan Hunt committed
579
			return (result);
580
581
	} /* end of while loop */

Evan Hunt's avatar
Evan Hunt committed
582
	return (result);
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
}

/*
 * SDLZ interface methods
 */

static isc_result_t
fs_allowzonexfr(void *driverarg, void *dbdata, const char *name,
		const char *client)
{

	isc_result_t result;
	char *path;
	struct stat	sb;
	config_data_t *cd;
	path = NULL;

	UNUSED(driverarg);

	cd = (config_data_t *) dbdata;

	if (create_path(name, NULL, client, cd, &path) != ISC_R_SUCCESS) {
		return (ISC_R_NOTFOUND);
	}

	if (stat(path, &sb) != 0) {
		result = ISC_R_NOTFOUND;
		goto complete_AXFR;
	}

	if ((sb.st_mode & S_IFREG) != 0) {
		result = ISC_R_SUCCESS;
		goto complete_AXFR;
	}

	result = ISC_R_NOTFOUND;

 complete_AXFR:
621
	isc_mem_free(named_g_mctx, path);
Evan Hunt's avatar
Evan Hunt committed
622
	return (result);
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
}

static isc_result_t
fs_allnodes(const char *zone, void *driverarg, void *dbdata,
	    dns_sdlzallnodes_t *allnodes)
{

	isc_result_t result;
	dlist_t *dir_list;
	config_data_t *cd;
	char *basepath;
	unsigned int basepathlen;
	struct stat	sb;
	isc_dir_t dir;
	dir_entry_t *dir_entry;
	dir_entry_t *next_de;

	basepath = NULL;
	dir_list = NULL;

	UNUSED(driverarg);
	UNUSED(allnodes);

	cd = (config_data_t *) dbdata;

	/* allocate memory for list */
649
	dir_list = isc_mem_get(named_g_mctx, sizeof(dlist_t));
650
651
652
653
654
655
656
657
658
	if (dir_list == NULL) {
		result = ISC_R_NOTFOUND;
		goto complete_allnds;
	}

	/* initialize list */
	ISC_LIST_INIT(*dir_list);

	if (create_path(zone, NULL, NULL, cd, &basepath) != ISC_R_SUCCESS) {
659
660
		result = ISC_R_NOTFOUND;
		goto complete_allnds;
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
	}

	/* remove path separator at end of path so stat works properly */
	basepathlen = strlen(basepath);

	if (stat(basepath, &sb) != 0) {
		result = ISC_R_NOTFOUND;
		goto complete_allnds;
	}

	if ((sb.st_mode & S_IFDIR) == 0) {
		result = ISC_R_NOTFOUND;
		goto complete_allnds;
	}

	/* initialize and open directory */
	isc_dir_init(&dir);
	result = isc_dir_open(&dir, basepath);

	/* if directory open failed, return error. */
	if (result != ISC_R_SUCCESS) {
		isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
			      DNS_LOGMODULE_DLZ, ISC_LOG_ERROR,
			      "Unable to open %s directory to read entries.",
			      basepath);
		result = ISC_R_FAILURE;
		goto complete_allnds;
	}

	/* process the directory */
Evan Hunt's avatar
Evan Hunt committed
691
	result = process_dir(&dir, allnodes, cd, dir_list, basepathlen);
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715

	/* close the directory */
	isc_dir_close(&dir);

	if (result != ISC_R_SUCCESS)
		goto complete_allnds;

	/* get first dir entry from list. */
	dir_entry = ISC_LIST_HEAD(*dir_list);
	while (dir_entry != NULL) {

		result = isc_dir_open(&dir, dir_entry->dirpath);
		/* if directory open failed, return error. */
		if (result != ISC_R_SUCCESS) {
			isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
				      DNS_LOGMODULE_DLZ, ISC_LOG_ERROR,
				      "Unable to open %s "
				      "directory to read entries.",
				      basepath);
			result = ISC_R_FAILURE;
			goto complete_allnds;
		}

		/* process the directory */
Evan Hunt's avatar
Evan Hunt committed
716
		result = process_dir(&dir, allnodes, cd, dir_list, basepathlen);
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732

		/* close the directory */
		isc_dir_close(&dir);

		if (result != ISC_R_SUCCESS)
			goto complete_allnds;

		dir_entry = ISC_LIST_NEXT(dir_entry, link);
	} /* end while */

 complete_allnds:
	if (dir_list != NULL) {
		/* clean up entries from list. */
		dir_entry = ISC_LIST_HEAD(*dir_list);
		while (dir_entry != NULL) {
			next_de = ISC_LIST_NEXT(dir_entry, link);
733
			isc_mem_put(named_g_mctx, dir_entry, sizeof(dir_entry_t));
734
735
			dir_entry = next_de;
		} /* end while */
736
		isc_mem_put(named_g_mctx, dir_list, sizeof(dlist_t));
737
738
739
	}

	if (basepath != NULL)
740
		isc_mem_free(named_g_mctx, basepath);
741

Evan Hunt's avatar
Evan Hunt committed
742
	return (result);
743
744
745
}

static isc_result_t
746
747
fs_findzone(void *driverarg, void *dbdata, const char *name,
	    dns_clientinfomethods_t *methods, dns_clientinfo_t *clientinfo)
748
749
750
751
752
753
754
755
{

	isc_result_t result;
	char *path;
	struct stat	sb;
	path = NULL;

	UNUSED(driverarg);
756
757
	UNUSED(methods);
	UNUSED(clientinfo);
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782

	if (create_path(name, NULL, NULL, (config_data_t *) dbdata,
			&path) != ISC_R_SUCCESS) {
		return (ISC_R_NOTFOUND);
	}

	isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
		      DNS_LOGMODULE_DLZ, ISC_LOG_DEBUG(1),
		      "Filesystem driver Findzone() Checking for path: '%s'\n",
		      path);

	if (stat(path, &sb) != 0) {
		result = ISC_R_NOTFOUND;
		goto complete_FZ;
	}

	if ((sb.st_mode & S_IFDIR) != 0) {
		result = ISC_R_SUCCESS;
		goto complete_FZ;
	}

	result = ISC_R_NOTFOUND;

 complete_FZ:

783
	isc_mem_free(named_g_mctx, path);
Evan Hunt's avatar
Evan Hunt committed
784
	return (result);
785
786
787
788
}

static isc_result_t
fs_lookup(const char *zone, const char *name, void *driverarg,
789
790
	  void *dbdata, dns_sdlzlookup_t *lookup,
	  dns_clientinfomethods_t *methods, dns_clientinfo_t *clientinfo)
791
792
793
794
795
796
797
798
799
{
	isc_result_t result;
	char *path;
	struct stat	sb;
	isc_dir_t dir;
	path = NULL;

	UNUSED(driverarg);
	UNUSED(lookup);
800
801
	UNUSED(methods);
	UNUSED(clientinfo);
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850

	if (strcmp(name, "*") == 0)
		/*
		 * handle filesystem's special wildcard "-"
		 */
		result = create_path(zone, "-", NULL,
				     (config_data_t *) dbdata, &path);
	else
		result = create_path(zone, name, NULL,
				     (config_data_t *) dbdata, &path);

	if ( result != ISC_R_SUCCESS) {
		return (ISC_R_NOTFOUND);
	}

	/* remove path separator at end of path so stat works properly */
	path[strlen(path)-1] = '\0';

	isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
		      DNS_LOGMODULE_DLZ, ISC_LOG_DEBUG(1),
		      "Filesystem driver lookup() Checking for path: '%s'\n",
		      path);


	if (stat(path, &sb) != 0) {
		result = ISC_R_NOTFOUND;
		goto complete_lkup;
	}

	if ((sb.st_mode & S_IFDIR) == 0) {
		result = ISC_R_NOTFOUND;
		goto complete_lkup;
	}

	/* initialize and open directory */
	isc_dir_init(&dir);
	result = isc_dir_open(&dir, path);

	/* if directory open failed, return error. */
	if (result != ISC_R_SUCCESS) {
		isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
			      DNS_LOGMODULE_DLZ, ISC_LOG_ERROR,
			      "Unable to open %s directory to read entries.",
			      path);
		result = ISC_R_FAILURE;
		goto complete_lkup;
	}

	/* process any records in the directory */
Evan Hunt's avatar
Evan Hunt committed
851
	result = process_dir(&dir, lookup, (config_data_t *) dbdata, NULL, 0);
852
853
854
855
856
857

	/* close the directory */
	isc_dir_close(&dir);

 complete_lkup:

858
	isc_mem_free(named_g_mctx, path);
Evan Hunt's avatar
Evan Hunt committed
859
	return (result);
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
}

static isc_result_t
fs_create(const char *dlzname, unsigned int argc, char *argv[],
	  void *driverarg, void **dbdata)
{
	config_data_t *cd;
	char *endp;
	int len;
	char pathsep;

	UNUSED(driverarg);
	UNUSED(dlzname);

	/* we require 5 command line args. */
	if (argc != 6) {
		isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
			      DNS_LOGMODULE_DLZ, ISC_LOG_ERROR,
			      "Filesystem driver requires "
			      "6 command line args.");
		return (ISC_R_FAILURE);
	}

	if (strlen(argv[5]) > 1) {
		isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
			      DNS_LOGMODULE_DLZ, ISC_LOG_ERROR,
			      "Filesystem driver can only "
			      "accept a single character for separator.");
		return (ISC_R_FAILURE);
	}

	/* verify base dir ends with '/' or '\' */
	len = strlen(argv[1]);
	if (argv[1][len-1] != '\\' && argv[1][len-1] != '/') {
		isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
			      DNS_LOGMODULE_DLZ, ISC_LOG_ERROR,
			      "Base dir parameter for filesystem driver "
			      "should end with %s",
			      "either '/' or '\\' ");
		return (ISC_R_FAILURE);
	}

	/* determine and save path separator for later */
	if (argv[1][len-1] == '\\')
		pathsep = '\\';
	else
		pathsep = '/';

	/* allocate memory for our config data */
909
	cd = isc_mem_get(named_g_mctx, sizeof(config_data_t));
910
911
912
913
914
915
916
917
918
	if (cd == NULL)
		goto no_mem;

	/* zero the memory */
	memset(cd, 0, sizeof(config_data_t));

	cd->pathsep = pathsep;

	/* get and store our base directory */
919
	cd->basedir = isc_mem_strdup(named_g_mctx, argv[1]);
920
921
922
923
924
	if (cd->basedir == NULL)
		goto no_mem;
	cd->basedirsize = strlen(cd->basedir);

	/* get and store our data sub-dir */
925
	cd->datadir = isc_mem_strdup(named_g_mctx, argv[2]);
926
927
928
929
930
	if (cd->datadir == NULL)
		goto no_mem;
	cd->datadirsize = strlen(cd->datadir);

	/* get and store our zone xfr sub-dir */
931
	cd->xfrdir = isc_mem_strdup(named_g_mctx, argv[3]);
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
	if (cd->xfrdir == NULL)
		goto no_mem;
	cd->xfrdirsize = strlen(cd->xfrdir);

	/* get and store our directory split count */
	cd->splitcnt = strtol(argv[4], &endp, 10);
	if (*endp != '\0' || cd->splitcnt < 0) {
		isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
			      DNS_LOGMODULE_DLZ, ISC_LOG_ERROR,
			      "Directory split count must be zero (0) "
			      "or a postive number");
	}

	/* get and store our separator character */
	cd->separator = *argv[5];

	/* attach config data to memory context */
949
	isc_mem_attach(named_g_mctx, &cd->mctx);
950
951
952
953
954

	/* pass back config data */
	*dbdata = cd;

	/* return success */
Evan Hunt's avatar
Evan Hunt committed
955
	return (ISC_R_SUCCESS);
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988

	/* handle no memory error */
 no_mem:

	/* if we allocated a config data object clean it up */
	if (cd != NULL)
		fs_destroy(NULL, cd);

	/* write error message */
	isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
		      DNS_LOGMODULE_DLZ, ISC_LOG_ERROR,
		      "Filesystem driver unable to "
		      "allocate memory for config data.");

	/* return error */
	return (ISC_R_NOMEMORY);
}

static void
fs_destroy(void *driverarg, void *dbdata)
{
	isc_mem_t *mctx;
	config_data_t *cd;

	UNUSED(driverarg);

	cd = (config_data_t *) dbdata;

	/*
	 * free memory for each section of config data that was
	 * allocated
	 */
	if (cd->basedir != NULL)
989
		isc_mem_free(named_g_mctx, cd->basedir);
990
991

	if (cd->datadir != NULL)
992
		isc_mem_free(named_g_mctx, cd->datadir);
993
994

	if (cd->xfrdir != NULL)
995
		isc_mem_free(named_g_mctx, cd->xfrdir);
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013

	/* hold memory context to use later */
	mctx = cd->mctx;

	/* free config data memory */
	isc_mem_put(mctx, cd, sizeof(config_data_t));

	/* detach memory from context */
	isc_mem_detach(&mctx);
}

static dns_sdlzmethods_t dlz_fs_methods = {
	fs_create,
	fs_destroy,
	fs_findzone,
	fs_lookup,
	NULL,
	fs_allnodes,
1014
1015
1016
1017
1018
1019
1020
1021
	fs_allowzonexfr,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
};

/*%
 * Wrapper around dns_sdlzregister().
 */
isc_result_t
dlz_fs_init(void)
{
	isc_result_t result;

	/*
	 * Write debugging message to log
	 */
	isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
		      DNS_LOGMODULE_DLZ, ISC_LOG_DEBUG(2),
		      "Registering DLZ filesystem driver.");

	result = dns_sdlzregister("filesystem", &dlz_fs_methods, NULL,
				  DNS_SDLZFLAG_RELATIVEOWNER |
				  DNS_SDLZFLAG_RELATIVERDATA,
1042
				  named_g_mctx, &dlz_fs);
1043
1044
1045
1046
1047
1048
1049
	if (result != ISC_R_SUCCESS) {
		UNEXPECTED_ERROR(__FILE__, __LINE__,
				 "dns_sdlzregister() failed: %s",
				 isc_result_totext(result));
		result = ISC_R_UNEXPECTED;
	}

Evan Hunt's avatar
Evan Hunt committed
1050
	return (result);
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
}

/*%
 * Wrapper around dns_sdlzunregister().
 */
void
dlz_fs_clear(void) {

	/*
	 * Write debugging message to log
	 */
	isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
		      DNS_LOGMODULE_DLZ, ISC_LOG_DEBUG(2),
		      "Unregistering DLZ filesystem driver.");

	if (dlz_fs != NULL)
		dns_sdlzunregister(&dlz_fs);
}

#endif