1 /*
2 * Copyright (C) 2002 Stichting NLnet, Netherlands, stichting@nlnet.nl.
3 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
4 *
5 * SPDX-License-Identifier: MPL-2.0
6 *
7 * This Source Code Form is subject to the terms of the Mozilla Public
8 * License, v. 2.0. If a copy of the MPL was not distributed with this
9 * file, you can obtain one at https://mozilla.org/MPL/2.0/.
10 *
11 * See the COPYRIGHT file distributed with this work for additional
12 * information regarding copyright ownership.
13 */
14
15 /*
16 * Copyright (C) 1999-2001, 2016 Internet Systems Consortium, Inc. ("ISC")
17 *
18 * This Source Code Form is subject to the terms of the Mozilla Public
19 * License, v. 2.0. If a copy of the MPL was not distributed with this
20 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
21 */
22
23 #ifdef DLZ_STUB
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 #include <isc/mem.h>
29 #include <isc/print.h>
30 #include <isc/result.h>
31 #include <isc/util.h>
32
33 #include <dns/log.h>
34 #include <dns/result.h>
35 #include <dns/sdlz.h>
36
37 #include <dlz/dlz_stub_driver.h>
38 #include <named/globals.h>
39
40 static dns_sdlzimplementation_t *dlz_stub = NULL;
41
42 typedef struct config_data {
43 char *myzone;
44 char *myname;
45 char *myip;
46 isc_mem_t *mctx;
47 } config_data_t;
48
49 /*
50 * SDLZ methods
51 */
52
53 static isc_result_t
stub_dlz_allnodes(const char * zone,void * driverarg,void * dbdata,dns_sdlzallnodes_t * allnodes)54 stub_dlz_allnodes(const char *zone, void *driverarg, void *dbdata,
55 dns_sdlzallnodes_t *allnodes) {
56 config_data_t *cd;
57 isc_result_t result;
58
59 UNUSED(zone);
60 UNUSED(driverarg);
61
62 cd = (config_data_t *)dbdata;
63
64 result = dns_sdlz_putnamedrr(allnodes, cd->myname, "soa", 86400,
65 "web root.localhost. "
66 "0 28800 7200 604800 86400");
67 if (result != ISC_R_SUCCESS) {
68 return (ISC_R_FAILURE);
69 }
70 result = dns_sdlz_putnamedrr(allnodes, "ns", "ns", 86400, cd->myname);
71 if (result != ISC_R_SUCCESS) {
72 return (ISC_R_FAILURE);
73 }
74 result = dns_sdlz_putnamedrr(allnodes, cd->myname, "a", 1, cd->myip);
75 if (result != ISC_R_SUCCESS) {
76 return (ISC_R_FAILURE);
77 }
78 return (ISC_R_SUCCESS);
79 }
80
81 static isc_result_t
stub_dlz_allowzonexfr(void * driverarg,void * dbdata,const char * name,const char * client)82 stub_dlz_allowzonexfr(void *driverarg, void *dbdata, const char *name,
83 const char *client) {
84 config_data_t *cd;
85
86 UNUSED(driverarg);
87 UNUSED(client);
88
89 cd = (config_data_t *)dbdata;
90
91 if (strcmp(name, cd->myname) == 0) {
92 return (ISC_R_SUCCESS);
93 }
94 return (ISC_R_NOTFOUND);
95 }
96
97 static isc_result_t
stub_dlz_authority(const char * zone,void * driverarg,void * dbdata,dns_sdlzlookup_t * lookup)98 stub_dlz_authority(const char *zone, void *driverarg, void *dbdata,
99 dns_sdlzlookup_t *lookup) {
100 isc_result_t result;
101 config_data_t *cd;
102
103 UNUSED(driverarg);
104
105 cd = (config_data_t *)dbdata;
106
107 if (strcmp(zone, cd->myzone) == 0) {
108 result = dns_sdlz_putsoa(lookup, cd->myname, "root.localhost.",
109 0);
110 if (result != ISC_R_SUCCESS) {
111 return (ISC_R_FAILURE);
112 }
113
114 result = dns_sdlz_putrr(lookup, "ns", 86400, cd->myname);
115 if (result != ISC_R_SUCCESS) {
116 return (ISC_R_FAILURE);
117 }
118
119 return (ISC_R_SUCCESS);
120 }
121 return (ISC_R_NOTFOUND);
122 }
123
124 static isc_result_t
stub_dlz_findzonedb(void * driverarg,void * dbdata,const char * name,dns_clientinfomethods_t * methods,dns_clientinfo_t * clientinfo)125 stub_dlz_findzonedb(void *driverarg, void *dbdata, const char *name,
126 dns_clientinfomethods_t *methods,
127 dns_clientinfo_t *clientinfo) {
128 config_data_t *cd;
129
130 UNUSED(driverarg);
131 UNUSED(methods);
132 UNUSED(clientinfo);
133
134 cd = (config_data_t *)dbdata;
135
136 /* Write info message to log */
137 isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE, DNS_LOGMODULE_DLZ,
138 ISC_LOG_DEBUG(2), "dlz_stub findzone looking for '%s'",
139 name);
140
141 if (strcmp(cd->myzone, name) == 0) {
142 return (ISC_R_SUCCESS);
143 } else {
144 return (ISC_R_NOTFOUND);
145 }
146 }
147
148 static isc_result_t
stub_dlz_lookup(const char * zone,const char * name,void * driverarg,void * dbdata,dns_sdlzlookup_t * lookup,dns_clientinfomethods_t * methods,dns_clientinfo_t * clientinfo)149 stub_dlz_lookup(const char *zone, const char *name, void *driverarg,
150 void *dbdata, dns_sdlzlookup_t *lookup,
151 dns_clientinfomethods_t *methods,
152 dns_clientinfo_t *clientinfo) {
153 isc_result_t result;
154 config_data_t *cd;
155
156 UNUSED(zone);
157 UNUSED(driverarg);
158 UNUSED(methods);
159 UNUSED(clientinfo);
160
161 cd = (config_data_t *)dbdata;
162
163 if (strcmp(name, cd->myname) == 0) {
164 result = dns_sdlz_putrr(lookup, "a", 1, cd->myip);
165 if (result != ISC_R_SUCCESS) {
166 return (ISC_R_FAILURE);
167 }
168
169 return (ISC_R_SUCCESS);
170 }
171 return (ISC_R_FAILURE);
172 }
173
174 static isc_result_t
stub_dlz_create(const char * dlzname,unsigned int argc,char * argv[],void * driverarg,void ** dbdata)175 stub_dlz_create(const char *dlzname, unsigned int argc, char *argv[],
176 void *driverarg, void **dbdata) {
177 config_data_t *cd;
178
179 UNUSED(driverarg);
180
181 if (argc < 4) {
182 return (ISC_R_FAILURE);
183 }
184 /*
185 * Write info message to log
186 */
187 isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE, DNS_LOGMODULE_DLZ,
188 ISC_LOG_INFO,
189 "Loading '%s' using DLZ_stub driver. "
190 "Zone: %s, Name: %s IP: %s",
191 dlzname, argv[1], argv[2], argv[3]);
192
193 cd = isc_mem_get(named_g_mctx, sizeof(config_data_t));
194 if ((cd) == NULL) {
195 return (ISC_R_NOMEMORY);
196 }
197
198 memset(cd, 0, sizeof(config_data_t));
199
200 cd->myzone = isc_mem_strdup(named_g_mctx, argv[1]);
201
202 cd->myname = isc_mem_strdup(named_g_mctx, argv[2]);
203
204 cd->myip = isc_mem_strdup(named_g_mctx, argv[3]);
205
206 isc_mem_attach(named_g_mctx, &cd->mctx);
207
208 *dbdata = cd;
209
210 return (ISC_R_SUCCESS);
211 }
212
213 static void
stub_dlz_destroy(void * driverarg,void * dbdata)214 stub_dlz_destroy(void *driverarg, void *dbdata) {
215 config_data_t *cd;
216 isc_mem_t *mctx;
217
218 UNUSED(driverarg);
219
220 cd = (config_data_t *)dbdata;
221
222 /*
223 * Write debugging message to log
224 */
225 isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE, DNS_LOGMODULE_DLZ,
226 ISC_LOG_DEBUG(2), "Unloading DLZ_stub driver.");
227
228 isc_mem_free(named_g_mctx, cd->myzone);
229 isc_mem_free(named_g_mctx, cd->myname);
230 isc_mem_free(named_g_mctx, cd->myip);
231 mctx = cd->mctx;
232 isc_mem_putanddetach(&mctx, cd, sizeof(config_data_t));
233 }
234
235 static dns_sdlzmethods_t dlz_stub_methods = {
236 stub_dlz_create,
237 stub_dlz_destroy,
238 stub_dlz_findzonedb,
239 stub_dlz_lookup,
240 stub_dlz_authority,
241 stub_dlz_allnodes,
242 stub_dlz_allowzonexfr,
243 NULL,
244 NULL,
245 NULL,
246 NULL,
247 NULL,
248 NULL,
249 NULL,
250 };
251
252 /*%
253 * Wrapper around dns_sdlzregister().
254 */
255 isc_result_t
dlz_stub_init(void)256 dlz_stub_init(void) {
257 isc_result_t result;
258
259 /*
260 * Write debugging message to log
261 */
262 isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE, DNS_LOGMODULE_DLZ,
263 ISC_LOG_DEBUG(2), "Registering DLZ_stub driver.");
264
265 result = dns_sdlzregister("dlz_stub", &dlz_stub_methods, NULL,
266 DNS_SDLZFLAG_RELATIVEOWNER |
267 DNS_SDLZFLAG_RELATIVERDATA,
268 named_g_mctx, &dlz_stub);
269 if (result != ISC_R_SUCCESS) {
270 UNEXPECTED_ERROR(__FILE__, __LINE__,
271 "dns_sdlzregister() failed: %s",
272 isc_result_totext(result));
273 result = ISC_R_UNEXPECTED;
274 }
275
276 return (result);
277 }
278
279 /*
280 * Wrapper around dns_sdlzunregister().
281 */
282 void
dlz_stub_clear(void)283 dlz_stub_clear(void) {
284 /*
285 * Write debugging message to log
286 */
287 isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE, DNS_LOGMODULE_DLZ,
288 ISC_LOG_DEBUG(2), "Unregistering DLZ_stub driver.");
289
290 if (dlz_stub != NULL) {
291 dns_sdlzunregister(&dlz_stub);
292 }
293 }
294
295 #endif /* ifdef DLZ_STUB */
296