summaryrefslogtreecommitdiffstats
path: root/bin/tests/system/dyndb/driver/instance.c
blob: 9e90a2c2f55a2fbb817cd1b9b78cfbabc8aefc80 (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
/*
 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
 *
 * SPDX-License-Identifier: MPL-2.0 AND 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 https://mozilla.org/MPL/2.0/.
 *
 * See the COPYRIGHT file distributed with this work for additional
 * information regarding copyright ownership.
 */

/*
 * Copyright (C) 2009-2015 Red Hat
 *
 * Permission to use, copy, modify, and/or 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 AUTHORS DISCLAIMS ALL WARRANTIES WITH
 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS.  IN NO EVENT SHALL ISC 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.
 */

/*
 * Driver instance object.
 *
 * One instance is equivalent to dynamic-db section in named.conf.
 * This module parses arguments and provide high-level operations
 * instance init/zone load/instance destroy.
 */

#include "instance.h"

#include <isc/task.h>
#include <isc/util.h>

#include <dns/db.h>
#include <dns/dyndb.h>
#include <dns/fixedname.h>
#include <dns/name.h>
#include <dns/view.h>
#include <dns/zone.h>

#include "db.h"
#include "log.h"
#include "util.h"
#include "zone.h"

/*
 * Parse parameters and convert them to zone names. Caller has to deallocate
 * resulting DNS names.
 *
 * @param[in]  argv NULL-terminated string array of length 2 (excluding NULL)
 * 		    Each string has to be a valid DNS name.
 * @param[out] z1   Zone name from argv[0]
 * @param[out] z2   Zone name from argv[1]
 */
static isc_result_t
parse_params(isc_mem_t *mctx, int argc, char **argv, dns_name_t *z1,
	     dns_name_t *z2) {
	isc_result_t result;
	int i;

	REQUIRE(argv != NULL);
	REQUIRE(z1 != NULL);
	REQUIRE(z2 != NULL);

	for (i = 0; i < argc; i++) {
		log_info("param: '%s'", argv[i]);
	}
	log_info("number of params: %d", i);

	if (argc != 2) {
		log_error("exactly two parameters "
			  "(absolute zone names) are required");
		result = ISC_R_FAILURE;
		goto cleanup;
	}
	result = dns_name_fromstring2(z1, argv[0], dns_rootname, 0, mctx);
	if (result != ISC_R_SUCCESS) {
		log_write(ISC_LOG_ERROR,
			  "parse_params: dns_name_fromstring2 -> %s",
			  isc_result_totext(result));
		goto cleanup;
	}
	result = dns_name_fromstring2(z2, argv[1], dns_rootname, 0, mctx);
	if (result != ISC_R_SUCCESS) {
		log_write(ISC_LOG_ERROR,
			  "parse_params: dns_name_fromstring2 -> %s",
			  isc_result_totext(result));
		goto cleanup;
	}

	result = ISC_R_SUCCESS;

cleanup:
	return (result);
}

/*
 * Initialize new driver instance. It will not create zones until
 * load_sample_instance_zones() is called.
 */
isc_result_t
new_sample_instance(isc_mem_t *mctx, const char *db_name, int argc, char **argv,
		    const dns_dyndbctx_t *dctx,
		    sample_instance_t **sample_instp) {
	isc_result_t result;
	sample_instance_t *inst = NULL;

	REQUIRE(sample_instp != NULL && *sample_instp == NULL);

	CHECKED_MEM_GET_PTR(mctx, inst);
	ZERO_PTR(inst);
	isc_mem_attach(mctx, &inst->mctx);

	inst->db_name = isc_mem_strdup(mctx, db_name);

	inst->zone1_name = dns_fixedname_initname(&inst->zone1_fn);
	inst->zone2_name = dns_fixedname_initname(&inst->zone2_fn);

	result = parse_params(mctx, argc, argv, inst->zone1_name,
			      inst->zone2_name);
	if (result != ISC_R_SUCCESS) {
		log_write(ISC_LOG_ERROR,
			  "new_sample_instance: parse_params -> %s",
			  isc_result_totext(result));
		goto cleanup;
	}

	dns_view_attach(dctx->view, &inst->view);
	dns_zonemgr_attach(dctx->zmgr, &inst->zmgr);
	isc_task_attach(dctx->task, &inst->task);

	/* Register new DNS DB implementation. */
	result = dns_db_register(db_name, create_db, inst, mctx, &inst->db_imp);
	if (result != ISC_R_SUCCESS) {
		log_write(ISC_LOG_ERROR,
			  "new_sample_instance: dns_db_register -> %s",
			  isc_result_totext(result));
		goto cleanup;
	}

	*sample_instp = inst;
	result = ISC_R_SUCCESS;

cleanup:
	if (result != ISC_R_SUCCESS) {
		destroy_sample_instance(&inst);
	}
	return (result);
}

/*
 * Create empty zones, add fake SOA, NS, and A records, load fake zones
 * and add them to inst->view.
 */
isc_result_t
load_sample_instance_zones(sample_instance_t *inst) {
	isc_result_t result;

	result = create_zone(inst, inst->zone1_name, &inst->zone1);
	if (result != ISC_R_SUCCESS) {
		log_write(ISC_LOG_ERROR,
			  "load_sample_instance_zones: create_zone -> %s",
			  isc_result_totext(result));
		goto cleanup;
	}
	result = activate_zone(inst, inst->zone1);
	if (result != ISC_R_SUCCESS) {
		log_write(ISC_LOG_ERROR,
			  "load_sample_instance_zones: activate_zone -> %s",
			  isc_result_totext(result));
		goto cleanup;
	}

	result = create_zone(inst, inst->zone2_name, &inst->zone2);
	if (result != ISC_R_SUCCESS) {
		log_write(ISC_LOG_ERROR,
			  "load_sample_instance_zones: create_zone -> %s",
			  isc_result_totext(result));
		goto cleanup;
	}
	result = activate_zone(inst, inst->zone2);
	if (result != ISC_R_SUCCESS) {
		log_write(ISC_LOG_ERROR,
			  "load_sample_instance_zones: activate_zone -> %s",
			  isc_result_totext(result));
		goto cleanup;
	}

cleanup:
	return (result);
}

void
destroy_sample_instance(sample_instance_t **instp) {
	sample_instance_t *inst;
	REQUIRE(instp != NULL);

	inst = *instp;
	*instp = NULL;
	if (inst == NULL) {
		return;
	}

	if (inst->db_name != NULL) {
		isc_mem_free(inst->mctx, inst->db_name);
	}
	if (inst->zone1 != NULL) {
		dns_zone_detach(&inst->zone1);
	}
	if (inst->zone2 != NULL) {
		dns_zone_detach(&inst->zone2);
	}
	if (inst->db_imp != NULL) {
		dns_db_unregister(&inst->db_imp);
	}

	dns_view_detach(&inst->view);
	dns_zonemgr_detach(&inst->zmgr);
	isc_task_detach(&inst->task);

	MEM_PUT_AND_DETACH(inst);
}