instance.c revision 1.1
1/*	$NetBSD: instance.c,v 1.1 2018/08/12 12:07:38 christos Exp $	*/
2
3/*
4 * Driver instance object.
5 *
6 * One instance is equivalent to dynamic-db section in named.conf.
7 * This module parses arguments and provide high-level operations
8 * instance init/zone load/instance destroy.
9 *
10 * Copyright (C) 2008-2015  Red Hat ; see COPYRIGHT for license
11 */
12
13#include <config.h>
14
15#include <isc/task.h>
16#include <isc/util.h>
17
18#include <dns/db.h>
19#include <dns/dyndb.h>
20#include <dns/fixedname.h>
21#include <dns/name.h>
22#include <dns/view.h>
23#include <dns/zone.h>
24
25#include "db.h"
26#include "util.h"
27#include "instance.h"
28#include "log.h"
29#include "zone.h"
30
31/*
32 * Parse parameters and convert them to zone names. Caller has to deallocate
33 * resulting DNS names.
34 *
35 * @param[in]  argv NULL-terminated string array of length 2 (excluding NULL)
36 * 		    Each string has to be a valid DNS name.
37 * @param[out] z1   Zone name from argv[0]
38 * @param[out] z2   Zone name from argv[1]
39 */
40static isc_result_t
41parse_params(isc_mem_t *mctx, int argc, char **argv,
42	     dns_name_t *z1, dns_name_t *z2)
43{
44	isc_result_t result;
45	int i;
46
47	REQUIRE(argv != NULL);
48	REQUIRE(z1 != NULL);
49	REQUIRE(z2 != NULL);
50
51	for (i = 0; i < argc; i++) {
52		log_info("param: '%s'", argv[i]);
53	}
54	log_info("number of params: %d", i);
55
56	if (argc != 2) {
57		log_error("exactly two parameters "
58			  "(absolute zone names) are required");
59		result = ISC_R_FAILURE;
60		goto cleanup;
61	}
62	CHECK(dns_name_fromstring2(z1, argv[0], dns_rootname, 0, mctx));
63	CHECK(dns_name_fromstring2(z2, argv[1], dns_rootname, 0, mctx));
64
65	result = ISC_R_SUCCESS;
66
67cleanup:
68	return (result);
69}
70
71/*
72 * Initialize new driver instance. It will not create zones until
73 * load_sample_instance_zones() is called.
74 */
75isc_result_t
76new_sample_instance(isc_mem_t *mctx, const char *db_name,
77		    int argc, char **argv, const dns_dyndbctx_t *dctx,
78		    sample_instance_t **sample_instp)
79{
80	isc_result_t result;
81	sample_instance_t *inst = NULL;
82
83	REQUIRE(sample_instp != NULL && *sample_instp == NULL);
84
85	CHECKED_MEM_GET_PTR(mctx, inst);
86	ZERO_PTR(inst);
87	isc_mem_attach(mctx, &inst->mctx);
88
89	inst->db_name = isc_mem_strdup(mctx, db_name);
90	if (inst->db_name == NULL) {
91		result = ISC_R_NOMEMORY;
92		goto cleanup;
93	}
94
95	inst->zone1_name = dns_fixedname_initname(&inst->zone1_fn);
96	inst->zone2_name = dns_fixedname_initname(&inst->zone2_fn);
97
98	CHECK(parse_params(mctx, argc, argv,
99			   inst->zone1_name, inst->zone2_name));
100
101	dns_view_attach(dctx->view, &inst->view);
102	dns_zonemgr_attach(dctx->zmgr, &inst->zmgr);
103	isc_task_attach(dctx->task, &inst->task);
104
105	/* Register new DNS DB implementation. */
106	CHECK(dns_db_register(db_name, create_db, inst, mctx, &inst->db_imp));
107
108	*sample_instp = inst;
109	result = ISC_R_SUCCESS;
110
111cleanup:
112	if (result != ISC_R_SUCCESS)
113		destroy_sample_instance(&inst);
114	return (result);
115}
116
117/*
118 * Create empty zones, add fake SOA, NS, and A records, load fake zones
119 * and add them to inst->view.
120 */
121isc_result_t
122load_sample_instance_zones(sample_instance_t *inst) {
123	isc_result_t result;
124
125	CHECK(create_zone(inst, inst->zone1_name, &inst->zone1));
126	CHECK(activate_zone(inst, inst->zone1));
127
128	CHECK(create_zone(inst, inst->zone2_name, &inst->zone2));
129	CHECK(activate_zone(inst, inst->zone2));
130
131cleanup:
132	return (result);
133}
134
135void
136destroy_sample_instance(sample_instance_t **instp) {
137	sample_instance_t *inst;
138	REQUIRE(instp != NULL);
139
140	inst = *instp;
141	if (inst == NULL)
142		return;
143
144	if (inst->db_name != NULL)
145		isc_mem_free(inst->mctx, inst->db_name);
146	if (inst->zone1 != NULL)
147		dns_zone_detach(&inst->zone1);
148	if (inst->zone2 != NULL)
149		dns_zone_detach(&inst->zone2);
150	if (inst->db_imp != NULL)
151		dns_db_unregister(&inst->db_imp);
152
153	dns_view_detach(&inst->view);
154	dns_zonemgr_detach(&inst->zmgr);
155	isc_task_detach(&inst->task);
156
157	MEM_PUT_AND_DETACH(inst);
158
159	*instp = NULL;
160}
161