]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/bind9/bin/named/controlconf.c
Copy xen includes in to RELENG_6 branch
[FreeBSD/FreeBSD.git] / contrib / bind9 / bin / named / controlconf.c
1 /*
2  * Copyright (C) 2004, 2006, 2008  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 2001-2003  Internet Software Consortium.
4  *
5  * Permission to use, copy, modify, and/or distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15  * PERFORMANCE OF THIS SOFTWARE.
16  */
17
18 /* $Id: controlconf.c,v 1.28.2.9.2.13.4.2 2008/07/23 23:16:25 marka Exp $ */
19
20 #include <config.h>
21
22 #include <isc/base64.h>
23 #include <isc/buffer.h>
24 #include <isc/event.h>
25 #include <isc/mem.h>
26 #include <isc/net.h>
27 #include <isc/netaddr.h>
28 #include <isc/random.h>
29 #include <isc/result.h>
30 #include <isc/stdtime.h>
31 #include <isc/string.h>
32 #include <isc/timer.h>
33 #include <isc/util.h>
34
35 #include <isccfg/namedconf.h>
36
37 #include <bind9/check.h>
38
39 #include <isccc/alist.h>
40 #include <isccc/cc.h>
41 #include <isccc/ccmsg.h>
42 #include <isccc/events.h>
43 #include <isccc/result.h>
44 #include <isccc/sexpr.h>
45 #include <isccc/symtab.h>
46 #include <isccc/util.h>
47
48 #include <dns/result.h>
49
50 #include <named/config.h>
51 #include <named/control.h>
52 #include <named/log.h>
53 #include <named/server.h>
54
55 /*
56  * Note: Listeners and connections are not locked.  All event handlers are
57  * executed by the server task, and all callers of exported routines must
58  * be running under the server task.
59  */
60
61 typedef struct controlkey controlkey_t;
62 typedef ISC_LIST(controlkey_t) controlkeylist_t;
63
64 typedef struct controlconnection controlconnection_t;
65 typedef ISC_LIST(controlconnection_t) controlconnectionlist_t;
66
67 typedef struct controllistener controllistener_t;
68 typedef ISC_LIST(controllistener_t) controllistenerlist_t;
69
70 struct controlkey {
71         char *                          keyname;
72         isc_region_t                    secret;
73         ISC_LINK(controlkey_t)          link;
74 };
75
76 struct controlconnection {
77         isc_socket_t *                  sock;
78         isccc_ccmsg_t                   ccmsg;
79         isc_boolean_t                   ccmsg_valid;
80         isc_boolean_t                   sending;
81         isc_timer_t *                   timer;
82         unsigned char                   buffer[2048];
83         controllistener_t *             listener;
84         isc_uint32_t                    nonce;
85         ISC_LINK(controlconnection_t)   link;
86 };
87
88 struct controllistener {
89         ns_controls_t *                 controls;
90         isc_mem_t *                     mctx;
91         isc_task_t *                    task;
92         isc_sockaddr_t                  address;
93         isc_socket_t *                  sock;
94         dns_acl_t *                     acl;
95         isc_boolean_t                   listening;
96         isc_boolean_t                   exiting;
97         controlkeylist_t                keys;
98         controlconnectionlist_t         connections;
99         ISC_LINK(controllistener_t)     link;
100 };
101
102 struct ns_controls {
103         ns_server_t                     *server;
104         controllistenerlist_t           listeners;
105         isc_boolean_t                   shuttingdown;
106         isccc_symtab_t                  *symtab;
107 };
108
109 static void control_newconn(isc_task_t *task, isc_event_t *event);
110 static void control_recvmessage(isc_task_t *task, isc_event_t *event);
111
112 #define CLOCKSKEW 300
113
114 static void
115 free_controlkey(controlkey_t *key, isc_mem_t *mctx) {
116         if (key->keyname != NULL)
117                 isc_mem_free(mctx, key->keyname);
118         if (key->secret.base != NULL)
119                 isc_mem_put(mctx, key->secret.base, key->secret.length);
120         isc_mem_put(mctx, key, sizeof(*key));
121 }
122
123 static void
124 free_controlkeylist(controlkeylist_t *keylist, isc_mem_t *mctx) {
125         while (!ISC_LIST_EMPTY(*keylist)) {
126                 controlkey_t *key = ISC_LIST_HEAD(*keylist);
127                 ISC_LIST_UNLINK(*keylist, key, link);
128                 free_controlkey(key, mctx);
129         }
130 }
131
132 static void
133 free_listener(controllistener_t *listener) {
134         INSIST(listener->exiting);
135         INSIST(!listener->listening);
136         INSIST(ISC_LIST_EMPTY(listener->connections));
137
138         if (listener->sock != NULL)
139                 isc_socket_detach(&listener->sock);
140
141         free_controlkeylist(&listener->keys, listener->mctx);
142
143         if (listener->acl != NULL)
144                 dns_acl_detach(&listener->acl);
145
146         isc_mem_put(listener->mctx, listener, sizeof(*listener));
147 }
148
149 static void
150 maybe_free_listener(controllistener_t *listener) {
151         if (listener->exiting &&
152             !listener->listening &&
153             ISC_LIST_EMPTY(listener->connections))
154                 free_listener(listener);
155 }
156
157 static void
158 maybe_free_connection(controlconnection_t *conn) {
159         controllistener_t *listener = conn->listener;
160
161         if (conn->timer != NULL)
162                 isc_timer_detach(&conn->timer);
163
164         if (conn->ccmsg_valid) {
165                 isccc_ccmsg_cancelread(&conn->ccmsg);
166                 return;
167         }
168
169         if (conn->sending) {
170                 isc_socket_cancel(conn->sock, listener->task,
171                                   ISC_SOCKCANCEL_SEND);
172                 return;
173         }
174
175         ISC_LIST_UNLINK(listener->connections, conn, link);
176         isc_mem_put(listener->mctx, conn, sizeof(*conn));
177 }
178
179 static void
180 shutdown_listener(controllistener_t *listener) {
181         controlconnection_t *conn;
182         controlconnection_t *next;
183
184         if (!listener->exiting) {
185                 char socktext[ISC_SOCKADDR_FORMATSIZE];
186
187                 ISC_LIST_UNLINK(listener->controls->listeners, listener, link);
188
189                 isc_sockaddr_format(&listener->address, socktext,
190                                     sizeof(socktext));
191                 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
192                               NS_LOGMODULE_CONTROL, ISC_LOG_NOTICE,
193                               "stopping command channel on %s", socktext);
194                 listener->exiting = ISC_TRUE;
195         }
196
197         for (conn = ISC_LIST_HEAD(listener->connections);
198              conn != NULL;
199              conn = next)
200         {
201                 next = ISC_LIST_NEXT(conn, link);
202                 maybe_free_connection(conn);
203         }
204
205         if (listener->listening)
206                 isc_socket_cancel(listener->sock, listener->task,
207                                   ISC_SOCKCANCEL_ACCEPT);
208
209         maybe_free_listener(listener);
210 }
211
212 static isc_boolean_t
213 address_ok(isc_sockaddr_t *sockaddr, dns_acl_t *acl) {
214         isc_netaddr_t netaddr;
215         isc_result_t result;
216         int match;
217
218         isc_netaddr_fromsockaddr(&netaddr, sockaddr);
219
220         result = dns_acl_match(&netaddr, NULL, acl,
221                                &ns_g_server->aclenv, &match, NULL);
222
223         if (result != ISC_R_SUCCESS || match <= 0)
224                 return (ISC_FALSE);
225         else
226                 return (ISC_TRUE);
227 }
228
229 static isc_result_t
230 control_accept(controllistener_t *listener) {
231         isc_result_t result;
232         result = isc_socket_accept(listener->sock,
233                                    listener->task,
234                                    control_newconn, listener);
235         if (result != ISC_R_SUCCESS)
236                 UNEXPECTED_ERROR(__FILE__, __LINE__,
237                                  "isc_socket_accept() failed: %s",
238                                  isc_result_totext(result));
239         else
240                 listener->listening = ISC_TRUE;
241         return (result);
242 }
243
244 static isc_result_t
245 control_listen(controllistener_t *listener) {
246         isc_result_t result;
247
248         result = isc_socket_listen(listener->sock, 0);
249         if (result != ISC_R_SUCCESS)
250                 UNEXPECTED_ERROR(__FILE__, __LINE__,
251                                  "isc_socket_listen() failed: %s",
252                                  isc_result_totext(result));
253         return (result);
254 }
255
256 static void
257 control_next(controllistener_t *listener) {
258         (void)control_accept(listener);
259 }
260
261 static void
262 control_senddone(isc_task_t *task, isc_event_t *event) {
263         isc_socketevent_t *sevent = (isc_socketevent_t *) event;
264         controlconnection_t *conn = event->ev_arg;
265         controllistener_t *listener = conn->listener;
266         isc_socket_t *sock = (isc_socket_t *)sevent->ev_sender;
267         isc_result_t result;
268
269         REQUIRE(conn->sending);
270
271         UNUSED(task);
272
273         conn->sending = ISC_FALSE;
274
275         if (sevent->result != ISC_R_SUCCESS &&
276             sevent->result != ISC_R_CANCELED)
277         {
278                 char socktext[ISC_SOCKADDR_FORMATSIZE];
279                 isc_sockaddr_t peeraddr;
280
281                 (void)isc_socket_getpeername(sock, &peeraddr);
282                 isc_sockaddr_format(&peeraddr, socktext, sizeof(socktext));
283                 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
284                               NS_LOGMODULE_CONTROL, ISC_LOG_WARNING,
285                               "error sending command response to %s: %s",
286                               socktext, isc_result_totext(sevent->result));
287         }
288         isc_event_free(&event);
289
290         result = isccc_ccmsg_readmessage(&conn->ccmsg, listener->task,
291                                          control_recvmessage, conn);
292         if (result != ISC_R_SUCCESS) {
293                 isc_socket_detach(&conn->sock);
294                 maybe_free_connection(conn);
295                 maybe_free_listener(listener);
296         }
297 }
298
299 static inline void
300 log_invalid(isccc_ccmsg_t *ccmsg, isc_result_t result) {
301         char socktext[ISC_SOCKADDR_FORMATSIZE];
302         isc_sockaddr_t peeraddr;
303
304         (void)isc_socket_getpeername(ccmsg->sock, &peeraddr);
305         isc_sockaddr_format(&peeraddr, socktext, sizeof(socktext));
306         isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
307                       NS_LOGMODULE_CONTROL, ISC_LOG_ERROR,
308                       "invalid command from %s: %s",
309                       socktext, isc_result_totext(result));
310 }
311
312 static void
313 control_recvmessage(isc_task_t *task, isc_event_t *event) {
314         controlconnection_t *conn;
315         controllistener_t *listener;
316         controlkey_t *key;
317         isccc_sexpr_t *request = NULL;
318         isccc_sexpr_t *response = NULL;
319         isccc_region_t ccregion;
320         isccc_region_t secret;
321         isc_stdtime_t now;
322         isc_buffer_t b;
323         isc_region_t r;
324         isc_uint32_t len;
325         isc_buffer_t text;
326         char textarray[1024];
327         isc_result_t result;
328         isc_result_t eresult;
329         isccc_sexpr_t *_ctrl;
330         isccc_time_t sent;
331         isccc_time_t exp;
332         isc_uint32_t nonce;
333
334         REQUIRE(event->ev_type == ISCCC_EVENT_CCMSG);
335
336         conn = event->ev_arg;
337         listener = conn->listener;
338         secret.rstart = NULL;
339
340         /* Is the server shutting down? */
341         if (listener->controls->shuttingdown)
342                 goto cleanup;
343
344         if (conn->ccmsg.result != ISC_R_SUCCESS) {
345                 if (conn->ccmsg.result != ISC_R_CANCELED &&
346                     conn->ccmsg.result != ISC_R_EOF)
347                         log_invalid(&conn->ccmsg, conn->ccmsg.result);
348                 goto cleanup;
349         }
350
351         request = NULL;
352
353         for (key = ISC_LIST_HEAD(listener->keys);
354              key != NULL;
355              key = ISC_LIST_NEXT(key, link))
356         {
357                 ccregion.rstart = isc_buffer_base(&conn->ccmsg.buffer);
358                 ccregion.rend = isc_buffer_used(&conn->ccmsg.buffer);
359                 secret.rstart = isc_mem_get(listener->mctx, key->secret.length);
360                 if (secret.rstart == NULL)
361                         goto cleanup;
362                 memcpy(secret.rstart, key->secret.base, key->secret.length);
363                 secret.rend = secret.rstart + key->secret.length;
364                 result = isccc_cc_fromwire(&ccregion, &request, &secret);
365                 if (result == ISC_R_SUCCESS)
366                         break;
367                 isc_mem_put(listener->mctx, secret.rstart, REGION_SIZE(secret));
368                 if (result == ISCCC_R_BADAUTH) {
369                         /*
370                          * For some reason, request is non-NULL when
371                          * isccc_cc_fromwire returns ISCCC_R_BADAUTH.
372                          */
373                         if (request != NULL)
374                                 isccc_sexpr_free(&request);
375                 } else {
376                         log_invalid(&conn->ccmsg, result);
377                         goto cleanup;
378                 }
379         }
380
381         if (key == NULL) {
382                 log_invalid(&conn->ccmsg, ISCCC_R_BADAUTH);
383                 goto cleanup;
384         }
385
386         /* We shouldn't be getting a reply. */
387         if (isccc_cc_isreply(request)) {
388                 log_invalid(&conn->ccmsg, ISC_R_FAILURE);
389                 goto cleanup_request;
390         }
391
392         isc_stdtime_get(&now);
393
394         /*
395          * Limit exposure to replay attacks.
396          */
397         _ctrl = isccc_alist_lookup(request, "_ctrl");
398         if (_ctrl == NULL) {
399                 log_invalid(&conn->ccmsg, ISC_R_FAILURE);
400                 goto cleanup_request;
401         }
402
403         if (isccc_cc_lookupuint32(_ctrl, "_tim", &sent) == ISC_R_SUCCESS) {
404                 if ((sent + CLOCKSKEW) < now || (sent - CLOCKSKEW) > now) {
405                         log_invalid(&conn->ccmsg, ISCCC_R_CLOCKSKEW);
406                         goto cleanup_request;
407                 }
408         } else {
409                 log_invalid(&conn->ccmsg, ISC_R_FAILURE);
410                 goto cleanup_request;
411         }
412
413         /*
414          * Expire messages that are too old.
415          */
416         if (isccc_cc_lookupuint32(_ctrl, "_exp", &exp) == ISC_R_SUCCESS &&
417             now > exp) {
418                 log_invalid(&conn->ccmsg, ISCCC_R_EXPIRED);
419                 goto cleanup_request;
420         }
421
422         /*
423          * Duplicate suppression (required for UDP).
424          */
425         isccc_cc_cleansymtab(listener->controls->symtab, now);
426         result = isccc_cc_checkdup(listener->controls->symtab, request, now);
427         if (result != ISC_R_SUCCESS) {
428                 if (result == ISC_R_EXISTS)
429                         result = ISCCC_R_DUPLICATE;
430                 log_invalid(&conn->ccmsg, result);
431                 goto cleanup_request;
432         }
433
434         if (conn->nonce != 0 &&
435             (isccc_cc_lookupuint32(_ctrl, "_nonce", &nonce) != ISC_R_SUCCESS ||
436              conn->nonce != nonce)) {
437                 log_invalid(&conn->ccmsg, ISCCC_R_BADAUTH);
438                 goto cleanup_request;
439         }
440
441         /*
442          * Establish nonce.
443          */
444         while (conn->nonce == 0)
445                 isc_random_get(&conn->nonce);
446
447         isc_buffer_init(&text, textarray, sizeof(textarray));
448         eresult = ns_control_docommand(request, &text);
449
450         result = isccc_cc_createresponse(request, now, now + 60, &response);
451         if (result != ISC_R_SUCCESS)
452                 goto cleanup_request;
453         if (eresult != ISC_R_SUCCESS) {
454                 isccc_sexpr_t *data;
455
456                 data = isccc_alist_lookup(response, "_data");
457                 if (data != NULL) {
458                         const char *estr = isc_result_totext(eresult);
459                         if (isccc_cc_definestring(data, "err", estr) == NULL)
460                                 goto cleanup_response;
461                 }
462         }
463
464         if (isc_buffer_usedlength(&text) > 0) {
465                 isccc_sexpr_t *data;
466
467                 data = isccc_alist_lookup(response, "_data");
468                 if (data != NULL) {
469                         char *str = (char *)isc_buffer_base(&text);
470                         if (isccc_cc_definestring(data, "text", str) == NULL)
471                                 goto cleanup_response;
472                 }
473         }
474
475         _ctrl = isccc_alist_lookup(response, "_ctrl");
476         if (_ctrl == NULL ||
477             isccc_cc_defineuint32(_ctrl, "_nonce", conn->nonce) == NULL)
478                 goto cleanup_response;
479
480         ccregion.rstart = conn->buffer + 4;
481         ccregion.rend = conn->buffer + sizeof(conn->buffer);
482         result = isccc_cc_towire(response, &ccregion, &secret);
483         if (result != ISC_R_SUCCESS)
484                 goto cleanup_response;
485         isc_buffer_init(&b, conn->buffer, 4);
486         len = sizeof(conn->buffer) - REGION_SIZE(ccregion);
487         isc_buffer_putuint32(&b, len - 4);
488         r.base = conn->buffer;
489         r.length = len;
490
491         result = isc_socket_send(conn->sock, &r, task, control_senddone, conn);
492         if (result != ISC_R_SUCCESS)
493                 goto cleanup_response;
494         conn->sending = ISC_TRUE;
495
496         isc_mem_put(listener->mctx, secret.rstart, REGION_SIZE(secret));
497         isccc_sexpr_free(&request);
498         isccc_sexpr_free(&response);
499         return;
500
501  cleanup_response:
502         isccc_sexpr_free(&response);
503
504  cleanup_request:
505         isccc_sexpr_free(&request);
506         isc_mem_put(listener->mctx, secret.rstart, REGION_SIZE(secret));
507
508  cleanup:
509         isc_socket_detach(&conn->sock);
510         isccc_ccmsg_invalidate(&conn->ccmsg);
511         conn->ccmsg_valid = ISC_FALSE;
512         maybe_free_connection(conn);
513         maybe_free_listener(listener);
514 }
515
516 static void
517 control_timeout(isc_task_t *task, isc_event_t *event) {
518         controlconnection_t *conn = event->ev_arg;
519
520         UNUSED(task);
521
522         isc_timer_detach(&conn->timer);
523         maybe_free_connection(conn);
524
525         isc_event_free(&event);
526 }
527
528 static isc_result_t
529 newconnection(controllistener_t *listener, isc_socket_t *sock) {
530         controlconnection_t *conn;
531         isc_interval_t interval;
532         isc_result_t result;
533
534         conn = isc_mem_get(listener->mctx, sizeof(*conn));
535         if (conn == NULL)
536                 return (ISC_R_NOMEMORY);
537
538         conn->sock = sock;
539         isccc_ccmsg_init(listener->mctx, sock, &conn->ccmsg);
540         conn->ccmsg_valid = ISC_TRUE;
541         conn->sending = ISC_FALSE;
542         conn->timer = NULL;
543         isc_interval_set(&interval, 60, 0);
544         result = isc_timer_create(ns_g_timermgr, isc_timertype_once,
545                                   NULL, &interval, listener->task,
546                                   control_timeout, conn, &conn->timer);
547         if (result != ISC_R_SUCCESS)
548                 goto cleanup;
549
550         conn->listener = listener;
551         conn->nonce = 0;
552         ISC_LINK_INIT(conn, link);
553
554         result = isccc_ccmsg_readmessage(&conn->ccmsg, listener->task,
555                                          control_recvmessage, conn);
556         if (result != ISC_R_SUCCESS)
557                 goto cleanup;
558         isccc_ccmsg_setmaxsize(&conn->ccmsg, 2048);
559
560         ISC_LIST_APPEND(listener->connections, conn, link);
561         return (ISC_R_SUCCESS);
562
563  cleanup:
564         isccc_ccmsg_invalidate(&conn->ccmsg);
565         if (conn->timer != NULL)
566                 isc_timer_detach(&conn->timer);
567         isc_mem_put(listener->mctx, conn, sizeof(*conn));
568         return (result);
569 }
570
571 static void
572 control_newconn(isc_task_t *task, isc_event_t *event) {
573         isc_socket_newconnev_t *nevent = (isc_socket_newconnev_t *)event;
574         controllistener_t *listener = event->ev_arg;
575         isc_socket_t *sock;
576         isc_sockaddr_t peeraddr;
577         isc_result_t result;
578
579         UNUSED(task);
580
581         listener->listening = ISC_FALSE;
582
583         if (nevent->result != ISC_R_SUCCESS) {
584                 if (nevent->result == ISC_R_CANCELED) {
585                         shutdown_listener(listener);
586                         goto cleanup;
587                 }
588                 goto restart;
589         }
590
591         sock = nevent->newsocket;
592         (void)isc_socket_getpeername(sock, &peeraddr);
593         if (!address_ok(&peeraddr, listener->acl)) {
594                 char socktext[ISC_SOCKADDR_FORMATSIZE];
595                 isc_sockaddr_format(&peeraddr, socktext, sizeof(socktext));
596                 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
597                               NS_LOGMODULE_CONTROL, ISC_LOG_WARNING,
598                               "rejected command channel message from %s",
599                               socktext);
600                 isc_socket_detach(&sock);
601                 goto restart;
602         }
603
604         result = newconnection(listener, sock);
605         if (result != ISC_R_SUCCESS) {
606                 char socktext[ISC_SOCKADDR_FORMATSIZE];
607                 isc_sockaddr_format(&peeraddr, socktext, sizeof(socktext));
608                 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
609                               NS_LOGMODULE_CONTROL, ISC_LOG_WARNING,
610                               "dropped command channel from %s: %s",
611                               socktext, isc_result_totext(result));
612                 isc_socket_detach(&sock);
613                 goto restart;
614         }
615
616  restart:
617         control_next(listener);
618  cleanup:
619         isc_event_free(&event);
620 }
621
622 static void
623 controls_shutdown(ns_controls_t *controls) {
624         controllistener_t *listener;
625         controllistener_t *next;
626
627         for (listener = ISC_LIST_HEAD(controls->listeners);
628              listener != NULL;
629              listener = next)
630         {
631                 /*
632                  * This is asynchronous.  As listeners shut down, they will
633                  * call their callbacks.
634                  */
635                 next = ISC_LIST_NEXT(listener, link);
636                 shutdown_listener(listener);
637         }
638 }
639
640 void
641 ns_controls_shutdown(ns_controls_t *controls) {
642         controls_shutdown(controls);
643         controls->shuttingdown = ISC_TRUE;
644 }
645
646 static isc_result_t
647 cfgkeylist_find(const cfg_obj_t *keylist, const char *keyname,
648                 const cfg_obj_t **objp)
649 {
650         const cfg_listelt_t *element;
651         const char *str;
652         const cfg_obj_t *obj;
653
654         for (element = cfg_list_first(keylist);
655              element != NULL;
656              element = cfg_list_next(element))
657         {
658                 obj = cfg_listelt_value(element);
659                 str = cfg_obj_asstring(cfg_map_getname(obj));
660                 if (strcasecmp(str, keyname) == 0)
661                         break;
662         }
663         if (element == NULL)
664                 return (ISC_R_NOTFOUND);
665         obj = cfg_listelt_value(element);
666         *objp = obj;
667         return (ISC_R_SUCCESS);
668 }
669
670 static isc_result_t
671 controlkeylist_fromcfg(const cfg_obj_t *keylist, isc_mem_t *mctx,
672                        controlkeylist_t *keyids)
673 {
674         const cfg_listelt_t *element;
675         char *newstr = NULL;
676         const char *str;
677         const cfg_obj_t *obj;
678         controlkey_t *key;
679
680         for (element = cfg_list_first(keylist);
681              element != NULL;
682              element = cfg_list_next(element))
683         {
684                 obj = cfg_listelt_value(element);
685                 str = cfg_obj_asstring(obj);
686                 newstr = isc_mem_strdup(mctx, str);
687                 if (newstr == NULL)
688                         goto cleanup;
689                 key = isc_mem_get(mctx, sizeof(*key));
690                 if (key == NULL)
691                         goto cleanup;
692                 key->keyname = newstr;
693                 key->secret.base = NULL;
694                 key->secret.length = 0;
695                 ISC_LINK_INIT(key, link);
696                 ISC_LIST_APPEND(*keyids, key, link);
697                 newstr = NULL;
698         }
699         return (ISC_R_SUCCESS);
700
701  cleanup:
702         if (newstr != NULL)
703                 isc_mem_free(mctx, newstr);
704         free_controlkeylist(keyids, mctx);
705         return (ISC_R_NOMEMORY);
706 }
707
708 static void
709 register_keys(const cfg_obj_t *control, const cfg_obj_t *keylist,
710               controlkeylist_t *keyids, isc_mem_t *mctx, const char *socktext)
711 {
712         controlkey_t *keyid, *next;
713         const cfg_obj_t *keydef;
714         char secret[1024];
715         isc_buffer_t b;
716         isc_result_t result;
717
718         /*
719          * Find the keys corresponding to the keyids used by this listener.
720          */
721         for (keyid = ISC_LIST_HEAD(*keyids); keyid != NULL; keyid = next) {
722                 next = ISC_LIST_NEXT(keyid, link);
723
724                 result = cfgkeylist_find(keylist, keyid->keyname, &keydef);
725                 if (result != ISC_R_SUCCESS) {
726                         cfg_obj_log(control, ns_g_lctx, ISC_LOG_WARNING,
727                                     "couldn't find key '%s' for use with "
728                                     "command channel %s",
729                                     keyid->keyname, socktext);
730                         ISC_LIST_UNLINK(*keyids, keyid, link);
731                         free_controlkey(keyid, mctx);
732                 } else {
733                         const cfg_obj_t *algobj = NULL;
734                         const cfg_obj_t *secretobj = NULL;
735                         const char *algstr = NULL;
736                         const char *secretstr = NULL;
737
738                         (void)cfg_map_get(keydef, "algorithm", &algobj);
739                         (void)cfg_map_get(keydef, "secret", &secretobj);
740                         INSIST(algobj != NULL && secretobj != NULL);
741
742                         algstr = cfg_obj_asstring(algobj);
743                         secretstr = cfg_obj_asstring(secretobj);
744
745                         if (ns_config_getkeyalgorithm(algstr, NULL) !=
746                             ISC_R_SUCCESS)
747                         {
748                                 cfg_obj_log(control, ns_g_lctx,
749                                             ISC_LOG_WARNING,
750                                             "unsupported algorithm '%s' in "
751                                             "key '%s' for use with command "
752                                             "channel %s",
753                                             algstr, keyid->keyname, socktext);
754                                 ISC_LIST_UNLINK(*keyids, keyid, link);
755                                 free_controlkey(keyid, mctx);
756                                 continue;
757                         }
758
759                         isc_buffer_init(&b, secret, sizeof(secret));
760                         result = isc_base64_decodestring(secretstr, &b);
761
762                         if (result != ISC_R_SUCCESS) {
763                                 cfg_obj_log(keydef, ns_g_lctx, ISC_LOG_WARNING,
764                                             "secret for key '%s' on "
765                                             "command channel %s: %s",
766                                             keyid->keyname, socktext,
767                                             isc_result_totext(result));
768                                 ISC_LIST_UNLINK(*keyids, keyid, link);
769                                 free_controlkey(keyid, mctx);
770                                 continue;
771                         }
772
773                         keyid->secret.length = isc_buffer_usedlength(&b);
774                         keyid->secret.base = isc_mem_get(mctx,
775                                                          keyid->secret.length);
776                         if (keyid->secret.base == NULL) {
777                                 cfg_obj_log(keydef, ns_g_lctx, ISC_LOG_WARNING,
778                                            "couldn't register key '%s': "
779                                            "out of memory", keyid->keyname);
780                                 ISC_LIST_UNLINK(*keyids, keyid, link);
781                                 free_controlkey(keyid, mctx);
782                                 break;
783                         }
784                         memcpy(keyid->secret.base, isc_buffer_base(&b),
785                                keyid->secret.length);
786                 }
787         }
788 }
789
790 #define CHECK(x) \
791         do { \
792                  result = (x); \
793                  if (result != ISC_R_SUCCESS) \
794                         goto cleanup; \
795         } while (0)
796
797 static isc_result_t
798 get_rndckey(isc_mem_t *mctx, controlkeylist_t *keyids) {
799         isc_result_t result;
800         cfg_parser_t *pctx = NULL;
801         cfg_obj_t *config = NULL;
802         const cfg_obj_t *key = NULL;
803         const cfg_obj_t *algobj = NULL;
804         const cfg_obj_t *secretobj = NULL;
805         const char *algstr = NULL;
806         const char *secretstr = NULL;
807         controlkey_t *keyid = NULL;
808         char secret[1024];
809         isc_buffer_t b;
810
811         CHECK(cfg_parser_create(mctx, ns_g_lctx, &pctx));
812         CHECK(cfg_parse_file(pctx, ns_g_keyfile, &cfg_type_rndckey, &config));
813         CHECK(cfg_map_get(config, "key", &key));
814
815         keyid = isc_mem_get(mctx, sizeof(*keyid));
816         if (keyid == NULL)
817                 CHECK(ISC_R_NOMEMORY);
818         keyid->keyname = isc_mem_strdup(mctx,
819                                         cfg_obj_asstring(cfg_map_getname(key)));
820         keyid->secret.base = NULL;
821         keyid->secret.length = 0;
822         ISC_LINK_INIT(keyid, link);
823         if (keyid->keyname == NULL)
824                 CHECK(ISC_R_NOMEMORY);
825
826         CHECK(bind9_check_key(key, ns_g_lctx));
827
828         (void)cfg_map_get(key, "algorithm", &algobj);
829         (void)cfg_map_get(key, "secret", &secretobj);
830         INSIST(algobj != NULL && secretobj != NULL);
831
832         algstr = cfg_obj_asstring(algobj);
833         secretstr = cfg_obj_asstring(secretobj);
834
835         if (ns_config_getkeyalgorithm(algstr, NULL) != ISC_R_SUCCESS) {
836                 cfg_obj_log(key, ns_g_lctx,
837                             ISC_LOG_WARNING,
838                             "unsupported algorithm '%s' in "
839                             "key '%s' for use with command "
840                             "channel",
841                             algstr, keyid->keyname);
842                 goto cleanup;
843         }
844
845         isc_buffer_init(&b, secret, sizeof(secret));
846         result = isc_base64_decodestring(secretstr, &b);
847
848         if (result != ISC_R_SUCCESS) {
849                 cfg_obj_log(key, ns_g_lctx, ISC_LOG_WARNING,
850                             "secret for key '%s' on command channel: %s",
851                             keyid->keyname, isc_result_totext(result));
852                 CHECK(result);
853         }
854
855         keyid->secret.length = isc_buffer_usedlength(&b);
856         keyid->secret.base = isc_mem_get(mctx,
857                                          keyid->secret.length);
858         if (keyid->secret.base == NULL) {
859                 cfg_obj_log(key, ns_g_lctx, ISC_LOG_WARNING,
860                            "couldn't register key '%s': "
861                            "out of memory", keyid->keyname);
862                 CHECK(ISC_R_NOMEMORY);
863         }
864         memcpy(keyid->secret.base, isc_buffer_base(&b),
865                keyid->secret.length);
866         ISC_LIST_APPEND(*keyids, keyid, link);
867         keyid = NULL;
868         result = ISC_R_SUCCESS;
869
870   cleanup:
871         if (keyid != NULL)
872                 free_controlkey(keyid, mctx);
873         if (config != NULL)
874                 cfg_obj_destroy(pctx, &config);
875         if (pctx != NULL)
876                 cfg_parser_destroy(&pctx);
877         return (result);
878 }
879
880 /*
881  * Ensures that both '*global_keylistp' and '*control_keylistp' are
882  * valid or both are NULL.
883  */
884 static void
885 get_key_info(const cfg_obj_t *config, const cfg_obj_t *control,
886              const cfg_obj_t **global_keylistp,
887              const cfg_obj_t **control_keylistp)
888 {
889         isc_result_t result;
890         const cfg_obj_t *control_keylist = NULL;
891         const cfg_obj_t *global_keylist = NULL;
892
893         REQUIRE(global_keylistp != NULL && *global_keylistp == NULL);
894         REQUIRE(control_keylistp != NULL && *control_keylistp == NULL);
895
896         control_keylist = cfg_tuple_get(control, "keys");
897
898         if (!cfg_obj_isvoid(control_keylist) &&
899             cfg_list_first(control_keylist) != NULL) {
900                 result = cfg_map_get(config, "key", &global_keylist);
901
902                 if (result == ISC_R_SUCCESS) {
903                         *global_keylistp = global_keylist;
904                         *control_keylistp = control_keylist;
905                 }
906         }
907 }
908
909 static void
910 update_listener(ns_controls_t *cp, controllistener_t **listenerp,
911                 const cfg_obj_t *control, const cfg_obj_t *config,
912                 isc_sockaddr_t *addr, ns_aclconfctx_t *aclconfctx,
913                 const char *socktext)
914 {
915         controllistener_t *listener;
916         const cfg_obj_t *allow;
917         const cfg_obj_t *global_keylist = NULL;
918         const cfg_obj_t *control_keylist = NULL;
919         dns_acl_t *new_acl = NULL;
920         controlkeylist_t keys;
921         isc_result_t result = ISC_R_SUCCESS;
922
923         for (listener = ISC_LIST_HEAD(cp->listeners);
924              listener != NULL;
925              listener = ISC_LIST_NEXT(listener, link))
926                 if (isc_sockaddr_equal(addr, &listener->address))
927                         break;
928
929         if (listener == NULL) {
930                 *listenerp = NULL;
931                 return;
932         }
933
934         /*
935          * There is already a listener for this sockaddr.
936          * Update the access list and key information.
937          *
938          * First try to deal with the key situation.  There are a few
939          * possibilities:
940          *  (a) It had an explicit keylist and still has an explicit keylist.
941          *  (b) It had an automagic key and now has an explicit keylist.
942          *  (c) It had an explicit keylist and now needs an automagic key.
943          *  (d) It has an automagic key and still needs the automagic key.
944          *
945          * (c) and (d) are the annoying ones.  The caller needs to know
946          * that it should use the automagic configuration for key information
947          * in place of the named.conf configuration.
948          *
949          * XXXDCL There is one other hazard that has not been dealt with,
950          * the problem that if a key change is being caused by a control
951          * channel reload, then the response will be with the new key
952          * and not able to be decrypted by the client.
953          */
954         if (control != NULL)
955                 get_key_info(config, control, &global_keylist,
956                              &control_keylist);
957
958         if (control_keylist != NULL) {
959                 INSIST(global_keylist != NULL);
960
961                 ISC_LIST_INIT(keys);
962                 result = controlkeylist_fromcfg(control_keylist,
963                                                 listener->mctx, &keys);
964                 if (result == ISC_R_SUCCESS) {
965                         free_controlkeylist(&listener->keys, listener->mctx);
966                         listener->keys = keys;
967                         register_keys(control, global_keylist, &listener->keys,
968                                       listener->mctx, socktext);
969                 }
970         } else {
971                 free_controlkeylist(&listener->keys, listener->mctx);
972                 result = get_rndckey(listener->mctx, &listener->keys);
973         }
974
975         if (result != ISC_R_SUCCESS && global_keylist != NULL) {
976                 /*
977                  * This message might be a little misleading since the
978                  * "new keys" might in fact be identical to the old ones,
979                  * but tracking whether they are identical just for the
980                  * sake of avoiding this message would be too much trouble.
981                  */
982                 if (control != NULL)
983                         cfg_obj_log(control, ns_g_lctx, ISC_LOG_WARNING,
984                                     "couldn't install new keys for "
985                                     "command channel %s: %s",
986                                     socktext, isc_result_totext(result));
987                 else
988                         isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
989                                       NS_LOGMODULE_CONTROL, ISC_LOG_WARNING,
990                                       "couldn't install new keys for "
991                                       "command channel %s: %s",
992                                       socktext, isc_result_totext(result));
993         }
994
995         /*
996          * Now, keep the old access list unless a new one can be made.
997          */
998         if (control != NULL) {
999                 allow = cfg_tuple_get(control, "allow");
1000                 result = ns_acl_fromconfig(allow, config, aclconfctx,
1001                                            listener->mctx, &new_acl);
1002         } else {
1003                 result = dns_acl_any(listener->mctx, &new_acl);
1004         }
1005
1006         if (result == ISC_R_SUCCESS) {
1007                 dns_acl_detach(&listener->acl);
1008                 dns_acl_attach(new_acl, &listener->acl);
1009                 dns_acl_detach(&new_acl);
1010                 /* XXXDCL say the old acl is still used? */
1011         } else if (control != NULL)
1012                 cfg_obj_log(control, ns_g_lctx, ISC_LOG_WARNING,
1013                             "couldn't install new acl for "
1014                             "command channel %s: %s",
1015                             socktext, isc_result_totext(result));
1016         else
1017                 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
1018                               NS_LOGMODULE_CONTROL, ISC_LOG_WARNING,
1019                               "couldn't install new acl for "
1020                               "command channel %s: %s",
1021                               socktext, isc_result_totext(result));
1022
1023         *listenerp = listener;
1024 }
1025
1026 static void
1027 add_listener(ns_controls_t *cp, controllistener_t **listenerp,
1028              const cfg_obj_t *control, const cfg_obj_t *config,
1029              isc_sockaddr_t *addr, ns_aclconfctx_t *aclconfctx,
1030              const char *socktext)
1031 {
1032         isc_mem_t *mctx = cp->server->mctx;
1033         controllistener_t *listener;
1034         const cfg_obj_t *allow;
1035         const cfg_obj_t *global_keylist = NULL;
1036         const cfg_obj_t *control_keylist = NULL;
1037         dns_acl_t *new_acl = NULL;
1038         isc_result_t result = ISC_R_SUCCESS;
1039
1040         listener = isc_mem_get(mctx, sizeof(*listener));
1041         if (listener == NULL)
1042                 result = ISC_R_NOMEMORY;
1043
1044         if (result == ISC_R_SUCCESS) {
1045                 listener->controls = cp;
1046                 listener->mctx = mctx;
1047                 listener->task = cp->server->task;
1048                 listener->address = *addr;
1049                 listener->sock = NULL;
1050                 listener->listening = ISC_FALSE;
1051                 listener->exiting = ISC_FALSE;
1052                 listener->acl = NULL;
1053                 ISC_LINK_INIT(listener, link);
1054                 ISC_LIST_INIT(listener->keys);
1055                 ISC_LIST_INIT(listener->connections);
1056
1057                 /*
1058                  * Make the acl.
1059                  */
1060                 if (control != NULL) {
1061                         allow = cfg_tuple_get(control, "allow");
1062                         result = ns_acl_fromconfig(allow, config, aclconfctx,
1063                                                    mctx, &new_acl);
1064                 } else {
1065                         result = dns_acl_any(mctx, &new_acl);
1066                 }
1067         }
1068
1069         if (result == ISC_R_SUCCESS) {
1070                 dns_acl_attach(new_acl, &listener->acl);
1071                 dns_acl_detach(&new_acl);
1072
1073                 if (config != NULL)
1074                         get_key_info(config, control, &global_keylist,
1075                                      &control_keylist);
1076
1077                 if (control_keylist != NULL) {
1078                         result = controlkeylist_fromcfg(control_keylist,
1079                                                         listener->mctx,
1080                                                         &listener->keys);
1081                         if (result == ISC_R_SUCCESS)
1082                                 register_keys(control, global_keylist,
1083                                               &listener->keys,
1084                                               listener->mctx, socktext);
1085                 } else
1086                         result = get_rndckey(mctx, &listener->keys);
1087
1088                 if (result != ISC_R_SUCCESS && control != NULL)
1089                         cfg_obj_log(control, ns_g_lctx, ISC_LOG_WARNING,
1090                                     "couldn't install keys for "
1091                                     "command channel %s: %s",
1092                                     socktext, isc_result_totext(result));
1093         }
1094
1095         if (result == ISC_R_SUCCESS) {
1096                 int pf = isc_sockaddr_pf(&listener->address);
1097                 if ((pf == AF_INET && isc_net_probeipv4() != ISC_R_SUCCESS) ||
1098                     (pf == AF_INET6 && isc_net_probeipv6() != ISC_R_SUCCESS))
1099                         result = ISC_R_FAMILYNOSUPPORT;
1100         }
1101
1102         if (result == ISC_R_SUCCESS)
1103                 result = isc_socket_create(ns_g_socketmgr,
1104                                            isc_sockaddr_pf(&listener->address),
1105                                            isc_sockettype_tcp,
1106                                            &listener->sock);
1107
1108         if (result == ISC_R_SUCCESS)
1109                 result = isc_socket_bind(listener->sock, &listener->address,
1110                                          ISC_SOCKET_REUSEADDRESS);
1111
1112         if (result == ISC_R_SUCCESS)
1113                 result = control_listen(listener);
1114
1115         if (result == ISC_R_SUCCESS)
1116                 result = control_accept(listener);
1117
1118         if (result == ISC_R_SUCCESS) {
1119                 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
1120                               NS_LOGMODULE_CONTROL, ISC_LOG_NOTICE,
1121                               "command channel listening on %s", socktext);
1122                 *listenerp = listener;
1123
1124         } else {
1125                 if (listener != NULL) {
1126                         listener->exiting = ISC_TRUE;
1127                         free_listener(listener);
1128                 }
1129
1130                 if (control != NULL)
1131                         cfg_obj_log(control, ns_g_lctx, ISC_LOG_WARNING,
1132                                     "couldn't add command channel %s: %s",
1133                                     socktext, isc_result_totext(result));
1134                 else
1135                         isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
1136                                       NS_LOGMODULE_CONTROL, ISC_LOG_NOTICE,
1137                                       "couldn't add command channel %s: %s",
1138                                       socktext, isc_result_totext(result));
1139
1140                 *listenerp = NULL;
1141         }
1142
1143         /* XXXDCL return error results? fail hard? */
1144 }
1145
1146 isc_result_t
1147 ns_controls_configure(ns_controls_t *cp, const cfg_obj_t *config,
1148                       ns_aclconfctx_t *aclconfctx)
1149 {
1150         controllistener_t *listener;
1151         controllistenerlist_t new_listeners;
1152         const cfg_obj_t *controlslist = NULL;
1153         const cfg_listelt_t *element, *element2;
1154         char socktext[ISC_SOCKADDR_FORMATSIZE];
1155
1156         ISC_LIST_INIT(new_listeners);
1157
1158         /*
1159          * Get the list of named.conf 'controls' statements.
1160          */
1161         (void)cfg_map_get(config, "controls", &controlslist);
1162
1163         /*
1164          * Run through the new control channel list, noting sockets that
1165          * are already being listened on and moving them to the new list.
1166          *
1167          * Identifying duplicate addr/port combinations is left to either
1168          * the underlying config code, or to the bind attempt getting an
1169          * address-in-use error.
1170          */
1171         if (controlslist != NULL) {
1172                 for (element = cfg_list_first(controlslist);
1173                      element != NULL;
1174                      element = cfg_list_next(element)) {
1175                         const cfg_obj_t *controls;
1176                         const cfg_obj_t *inetcontrols = NULL;
1177
1178                         controls = cfg_listelt_value(element);
1179                         (void)cfg_map_get(controls, "inet", &inetcontrols);
1180                         if (inetcontrols == NULL)
1181                                 continue;
1182
1183                         for (element2 = cfg_list_first(inetcontrols);
1184                              element2 != NULL;
1185                              element2 = cfg_list_next(element2)) {
1186                                 const cfg_obj_t *control;
1187                                 const cfg_obj_t *obj;
1188                                 isc_sockaddr_t addr;
1189
1190                                 /*
1191                                  * The parser handles BIND 8 configuration file
1192                                  * syntax, so it allows unix phrases as well
1193                                  * inet phrases with no keys{} clause.
1194                                  *
1195                                  * "unix" phrases have been reported as
1196                                  * unsupported by the parser.
1197                                  */
1198                                 control = cfg_listelt_value(element2);
1199
1200                                 obj = cfg_tuple_get(control, "address");
1201                                 addr = *cfg_obj_assockaddr(obj);
1202                                 if (isc_sockaddr_getport(&addr) == 0)
1203                                         isc_sockaddr_setport(&addr,
1204                                                              NS_CONTROL_PORT);
1205
1206                                 isc_sockaddr_format(&addr, socktext,
1207                                                     sizeof(socktext));
1208
1209                                 isc_log_write(ns_g_lctx,
1210                                               NS_LOGCATEGORY_GENERAL,
1211                                               NS_LOGMODULE_CONTROL,
1212                                               ISC_LOG_DEBUG(9),
1213                                               "processing control channel %s",
1214                                               socktext);
1215
1216                                 update_listener(cp, &listener, control, config,
1217                                                 &addr, aclconfctx, socktext);
1218
1219                                 if (listener != NULL)
1220                                         /*
1221                                          * Remove the listener from the old
1222                                          * list, so it won't be shut down.
1223                                          */
1224                                         ISC_LIST_UNLINK(cp->listeners,
1225                                                         listener, link);
1226                                 else
1227                                         /*
1228                                          * This is a new listener.
1229                                          */
1230                                         add_listener(cp, &listener, control,
1231                                                      config, &addr, aclconfctx,
1232                                                      socktext);
1233
1234                                 if (listener != NULL)
1235                                         ISC_LIST_APPEND(new_listeners,
1236                                                         listener, link);
1237                         }
1238                 }
1239         } else {
1240                 int i;
1241
1242                 for (i = 0; i < 2; i++) {
1243                         isc_sockaddr_t addr;
1244
1245                         if (i == 0) {
1246                                 struct in_addr localhost;
1247
1248                                 if (isc_net_probeipv4() != ISC_R_SUCCESS)
1249                                         continue;
1250                                 localhost.s_addr = htonl(INADDR_LOOPBACK);
1251                                 isc_sockaddr_fromin(&addr, &localhost, 0);
1252                         } else {
1253                                 if (isc_net_probeipv6() != ISC_R_SUCCESS)
1254                                         continue;
1255                                 isc_sockaddr_fromin6(&addr,
1256                                                      &in6addr_loopback, 0);
1257                         }
1258                         isc_sockaddr_setport(&addr, NS_CONTROL_PORT);
1259
1260                         isc_sockaddr_format(&addr, socktext, sizeof(socktext));
1261
1262                         update_listener(cp, &listener, NULL, NULL,
1263                                         &addr, NULL, socktext);
1264
1265                         if (listener != NULL)
1266                                 /*
1267                                  * Remove the listener from the old
1268                                  * list, so it won't be shut down.
1269                                  */
1270                                 ISC_LIST_UNLINK(cp->listeners,
1271                                                 listener, link);
1272                         else
1273                                 /*
1274                                  * This is a new listener.
1275                                  */
1276                                 add_listener(cp, &listener, NULL, NULL,
1277                                              &addr, NULL, socktext);
1278
1279                         if (listener != NULL)
1280                                 ISC_LIST_APPEND(new_listeners,
1281                                                 listener, link);
1282                 }
1283         }
1284
1285         /*
1286          * ns_control_shutdown() will stop whatever is on the global
1287          * listeners list, which currently only has whatever sockaddrs
1288          * were in the previous configuration (if any) that do not
1289          * remain in the current configuration.
1290          */
1291         controls_shutdown(cp);
1292
1293         /*
1294          * Put all of the valid listeners on the listeners list.
1295          * Anything already on listeners in the process of shutting
1296          * down will be taken care of by listen_done().
1297          */
1298         ISC_LIST_APPENDLIST(cp->listeners, new_listeners, link);
1299         return (ISC_R_SUCCESS);
1300 }
1301
1302 isc_result_t
1303 ns_controls_create(ns_server_t *server, ns_controls_t **ctrlsp) {
1304         isc_mem_t *mctx = server->mctx;
1305         isc_result_t result;
1306         ns_controls_t *controls = isc_mem_get(mctx, sizeof(*controls));
1307
1308         if (controls == NULL)
1309                 return (ISC_R_NOMEMORY);
1310         controls->server = server;
1311         ISC_LIST_INIT(controls->listeners);
1312         controls->shuttingdown = ISC_FALSE;
1313         controls->symtab = NULL;
1314         result = isccc_cc_createsymtab(&controls->symtab);
1315         if (result != ISC_R_SUCCESS) {
1316                 isc_mem_put(server->mctx, controls, sizeof(*controls));
1317                 return (result);
1318         }
1319         *ctrlsp = controls;
1320         return (ISC_R_SUCCESS);
1321 }
1322
1323 void
1324 ns_controls_destroy(ns_controls_t **ctrlsp) {
1325         ns_controls_t *controls = *ctrlsp;
1326
1327         REQUIRE(ISC_LIST_EMPTY(controls->listeners));
1328
1329         isccc_symtab_destroy(&controls->symtab);
1330         isc_mem_put(controls->server->mctx, controls, sizeof(*controls));
1331         *ctrlsp = NULL;
1332 }