2 * Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 2001-2003 Internet Software Consortium.
5 * Permission to use, copy, modify, and 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.
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.
18 /* $Id: controlconf.c,v 1.28.2.9.2.6 2004/03/08 09:04:14 marka Exp $ */
22 #include <isc/base64.h>
23 #include <isc/buffer.h>
24 #include <isc/event.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>
35 #include <isccfg/namedconf.h>
37 #include <bind9/check.h>
39 #include <isccc/alist.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>
48 #include <dns/result.h>
50 #include <named/config.h>
51 #include <named/control.h>
52 #include <named/log.h>
53 #include <named/server.h>
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.
61 typedef struct controlkey controlkey_t;
62 typedef ISC_LIST(controlkey_t) controlkeylist_t;
64 typedef struct controlconnection controlconnection_t;
65 typedef ISC_LIST(controlconnection_t) controlconnectionlist_t;
67 typedef struct controllistener controllistener_t;
68 typedef ISC_LIST(controllistener_t) controllistenerlist_t;
73 ISC_LINK(controlkey_t) link;
76 struct controlconnection {
79 isc_boolean_t ccmsg_valid;
80 isc_boolean_t sending;
82 unsigned char buffer[2048];
83 controllistener_t * listener;
85 ISC_LINK(controlconnection_t) link;
88 struct controllistener {
89 ns_controls_t * controls;
92 isc_sockaddr_t address;
95 isc_boolean_t listening;
96 isc_boolean_t exiting;
97 controlkeylist_t keys;
98 controlconnectionlist_t connections;
99 ISC_LINK(controllistener_t) link;
104 controllistenerlist_t listeners;
105 isc_boolean_t shuttingdown;
106 isccc_symtab_t *symtab;
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);
112 #define CLOCKSKEW 300
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));
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);
133 free_listener(controllistener_t *listener) {
134 INSIST(listener->exiting);
135 INSIST(!listener->listening);
136 INSIST(ISC_LIST_EMPTY(listener->connections));
138 if (listener->sock != NULL)
139 isc_socket_detach(&listener->sock);
141 free_controlkeylist(&listener->keys, listener->mctx);
143 if (listener->acl != NULL)
144 dns_acl_detach(&listener->acl);
146 isc_mem_put(listener->mctx, listener, sizeof(*listener));
150 maybe_free_listener(controllistener_t *listener) {
151 if (listener->exiting &&
152 !listener->listening &&
153 ISC_LIST_EMPTY(listener->connections))
154 free_listener(listener);
158 maybe_free_connection(controlconnection_t *conn) {
159 controllistener_t *listener = conn->listener;
161 if (conn->timer != NULL)
162 isc_timer_detach(&conn->timer);
164 if (conn->ccmsg_valid) {
165 isccc_ccmsg_cancelread(&conn->ccmsg);
170 isc_socket_cancel(conn->sock, listener->task,
171 ISC_SOCKCANCEL_SEND);
175 ISC_LIST_UNLINK(listener->connections, conn, link);
176 isc_mem_put(listener->mctx, conn, sizeof(*conn));
180 shutdown_listener(controllistener_t *listener) {
181 controlconnection_t *conn;
182 controlconnection_t *next;
184 if (!listener->exiting) {
185 char socktext[ISC_SOCKADDR_FORMATSIZE];
187 ISC_LIST_UNLINK(listener->controls->listeners, listener, link);
189 isc_sockaddr_format(&listener->address, 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;
197 for (conn = ISC_LIST_HEAD(listener->connections);
201 next = ISC_LIST_NEXT(conn, link);
202 maybe_free_connection(conn);
205 if (listener->listening)
206 isc_socket_cancel(listener->sock, listener->task,
207 ISC_SOCKCANCEL_ACCEPT);
209 maybe_free_listener(listener);
213 address_ok(isc_sockaddr_t *sockaddr, dns_acl_t *acl) {
214 isc_netaddr_t netaddr;
218 isc_netaddr_fromsockaddr(&netaddr, sockaddr);
220 result = dns_acl_match(&netaddr, NULL, acl,
221 &ns_g_server->aclenv, &match, NULL);
223 if (result != ISC_R_SUCCESS || match <= 0)
230 control_accept(controllistener_t *listener) {
232 result = isc_socket_accept(listener->sock,
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));
240 listener->listening = ISC_TRUE;
245 control_listen(controllistener_t *listener) {
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));
257 control_next(controllistener_t *listener) {
258 (void)control_accept(listener);
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;
269 REQUIRE(conn->sending);
273 conn->sending = ISC_FALSE;
275 if (sevent->result != ISC_R_SUCCESS &&
276 sevent->result != ISC_R_CANCELED)
278 char socktext[ISC_SOCKADDR_FORMATSIZE];
279 isc_sockaddr_t peeraddr;
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));
288 isc_event_free(&event);
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);
300 log_invalid(isccc_ccmsg_t *ccmsg, isc_result_t result) {
301 char socktext[ISC_SOCKADDR_FORMATSIZE];
302 isc_sockaddr_t peeraddr;
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));
313 control_recvmessage(isc_task_t *task, isc_event_t *event) {
314 controlconnection_t *conn;
315 controllistener_t *listener;
317 isccc_sexpr_t *request = NULL;
318 isccc_sexpr_t *response = NULL;
319 isccc_region_t ccregion;
320 isccc_region_t secret;
326 char textarray[1024];
328 isc_result_t eresult;
329 isccc_sexpr_t *_ctrl;
334 REQUIRE(event->ev_type == ISCCC_EVENT_CCMSG);
336 conn = event->ev_arg;
337 listener = conn->listener;
338 secret.rstart = NULL;
340 /* Is the server shutting down? */
341 if (listener->controls->shuttingdown)
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);
353 for (key = ISC_LIST_HEAD(listener->keys);
355 key = ISC_LIST_NEXT(key, link))
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)
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)
367 else if (result == ISCCC_R_BADAUTH) {
369 * For some reason, request is non-NULL when
370 * isccc_cc_fromwire returns ISCCC_R_BADAUTH.
373 isccc_sexpr_free(&request);
374 isc_mem_put(listener->mctx, secret.rstart,
375 REGION_SIZE(secret));
377 log_invalid(&conn->ccmsg, result);
383 log_invalid(&conn->ccmsg, ISCCC_R_BADAUTH);
387 /* We shouldn't be getting a reply. */
388 if (isccc_cc_isreply(request)) {
389 log_invalid(&conn->ccmsg, ISC_R_FAILURE);
393 isc_stdtime_get(&now);
396 * Limit exposure to replay attacks.
398 _ctrl = isccc_alist_lookup(request, "_ctrl");
400 log_invalid(&conn->ccmsg, ISC_R_FAILURE);
404 if (isccc_cc_lookupuint32(_ctrl, "_tim", &sent) == ISC_R_SUCCESS) {
405 if ((sent + CLOCKSKEW) < now || (sent - CLOCKSKEW) > now) {
406 log_invalid(&conn->ccmsg, ISCCC_R_CLOCKSKEW);
410 log_invalid(&conn->ccmsg, ISC_R_FAILURE);
415 * Expire messages that are too old.
417 if (isccc_cc_lookupuint32(_ctrl, "_exp", &exp) == ISC_R_SUCCESS &&
419 log_invalid(&conn->ccmsg, ISCCC_R_EXPIRED);
424 * Duplicate suppression (required for UDP).
426 isccc_cc_cleansymtab(listener->controls->symtab, now);
427 result = isccc_cc_checkdup(listener->controls->symtab, request, now);
428 if (result != ISC_R_SUCCESS) {
429 if (result == ISC_R_EXISTS)
430 result = ISCCC_R_DUPLICATE;
431 log_invalid(&conn->ccmsg, result);
435 if (conn->nonce != 0 &&
436 (isccc_cc_lookupuint32(_ctrl, "_nonce", &nonce) != ISC_R_SUCCESS ||
437 conn->nonce != nonce)) {
438 log_invalid(&conn->ccmsg, ISCCC_R_BADAUTH);
445 while (conn->nonce == 0)
446 isc_random_get(&conn->nonce);
448 isc_buffer_init(&text, textarray, sizeof(textarray));
449 eresult = ns_control_docommand(request, &text);
451 result = isccc_cc_createresponse(request, now, now + 60, &response);
452 if (result != ISC_R_SUCCESS)
454 if (eresult != ISC_R_SUCCESS) {
457 data = isccc_alist_lookup(response, "_data");
459 const char *estr = isc_result_totext(eresult);
460 if (isccc_cc_definestring(data, "err", estr) == NULL)
465 if (isc_buffer_usedlength(&text) > 0) {
468 data = isccc_alist_lookup(response, "_data");
470 char *str = (char *)isc_buffer_base(&text);
471 if (isccc_cc_definestring(data, "text", str) == NULL)
476 _ctrl = isccc_alist_lookup(response, "_ctrl");
478 isccc_cc_defineuint32(_ctrl, "_nonce", conn->nonce) == NULL)
481 ccregion.rstart = conn->buffer + 4;
482 ccregion.rend = conn->buffer + sizeof(conn->buffer);
483 result = isccc_cc_towire(response, &ccregion, &secret);
484 if (result != ISC_R_SUCCESS)
486 isc_buffer_init(&b, conn->buffer, 4);
487 len = sizeof(conn->buffer) - REGION_SIZE(ccregion);
488 isc_buffer_putuint32(&b, len - 4);
489 r.base = conn->buffer;
492 result = isc_socket_send(conn->sock, &r, task, control_senddone, conn);
493 if (result != ISC_R_SUCCESS)
495 conn->sending = ISC_TRUE;
497 if (secret.rstart != NULL)
498 isc_mem_put(listener->mctx, secret.rstart,
499 REGION_SIZE(secret));
501 isccc_sexpr_free(&request);
502 if (response != NULL)
503 isccc_sexpr_free(&response);
507 if (secret.rstart != NULL)
508 isc_mem_put(listener->mctx, secret.rstart,
509 REGION_SIZE(secret));
510 isc_socket_detach(&conn->sock);
511 isccc_ccmsg_invalidate(&conn->ccmsg);
512 conn->ccmsg_valid = ISC_FALSE;
513 maybe_free_connection(conn);
514 maybe_free_listener(listener);
516 isccc_sexpr_free(&request);
517 if (response != NULL)
518 isccc_sexpr_free(&response);
522 control_timeout(isc_task_t *task, isc_event_t *event) {
523 controlconnection_t *conn = event->ev_arg;
527 isc_timer_detach(&conn->timer);
528 maybe_free_connection(conn);
530 isc_event_free(&event);
534 newconnection(controllistener_t *listener, isc_socket_t *sock) {
535 controlconnection_t *conn;
536 isc_interval_t interval;
539 conn = isc_mem_get(listener->mctx, sizeof(*conn));
541 return (ISC_R_NOMEMORY);
544 isccc_ccmsg_init(listener->mctx, sock, &conn->ccmsg);
545 conn->ccmsg_valid = ISC_TRUE;
546 conn->sending = ISC_FALSE;
548 isc_interval_set(&interval, 60, 0);
549 result = isc_timer_create(ns_g_timermgr, isc_timertype_once,
550 NULL, &interval, listener->task,
551 control_timeout, conn, &conn->timer);
552 if (result != ISC_R_SUCCESS)
555 conn->listener = listener;
557 ISC_LINK_INIT(conn, link);
559 result = isccc_ccmsg_readmessage(&conn->ccmsg, listener->task,
560 control_recvmessage, conn);
561 if (result != ISC_R_SUCCESS)
563 isccc_ccmsg_setmaxsize(&conn->ccmsg, 2048);
565 ISC_LIST_APPEND(listener->connections, conn, link);
566 return (ISC_R_SUCCESS);
569 isccc_ccmsg_invalidate(&conn->ccmsg);
570 if (conn->timer != NULL)
571 isc_timer_detach(&conn->timer);
572 isc_mem_put(listener->mctx, conn, sizeof(*conn));
577 control_newconn(isc_task_t *task, isc_event_t *event) {
578 isc_socket_newconnev_t *nevent = (isc_socket_newconnev_t *)event;
579 controllistener_t *listener = event->ev_arg;
581 isc_sockaddr_t peeraddr;
586 listener->listening = ISC_FALSE;
588 if (nevent->result != ISC_R_SUCCESS) {
589 if (nevent->result == ISC_R_CANCELED) {
590 shutdown_listener(listener);
596 sock = nevent->newsocket;
597 (void)isc_socket_getpeername(sock, &peeraddr);
598 if (!address_ok(&peeraddr, listener->acl)) {
599 char socktext[ISC_SOCKADDR_FORMATSIZE];
600 isc_sockaddr_format(&peeraddr, socktext, sizeof(socktext));
601 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
602 NS_LOGMODULE_CONTROL, ISC_LOG_WARNING,
603 "rejected command channel message from %s",
605 isc_socket_detach(&sock);
609 result = newconnection(listener, sock);
610 if (result != ISC_R_SUCCESS) {
611 char socktext[ISC_SOCKADDR_FORMATSIZE];
612 isc_sockaddr_format(&peeraddr, socktext, sizeof(socktext));
613 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
614 NS_LOGMODULE_CONTROL, ISC_LOG_WARNING,
615 "dropped command channel from %s: %s",
616 socktext, isc_result_totext(result));
617 isc_socket_detach(&sock);
622 control_next(listener);
624 isc_event_free(&event);
628 controls_shutdown(ns_controls_t *controls) {
629 controllistener_t *listener;
630 controllistener_t *next;
632 for (listener = ISC_LIST_HEAD(controls->listeners);
637 * This is asynchronous. As listeners shut down, they will
638 * call their callbacks.
640 next = ISC_LIST_NEXT(listener, link);
641 shutdown_listener(listener);
646 ns_controls_shutdown(ns_controls_t *controls) {
647 controls_shutdown(controls);
648 controls->shuttingdown = ISC_TRUE;
652 cfgkeylist_find(cfg_obj_t *keylist, const char *keyname, cfg_obj_t **objp) {
653 cfg_listelt_t *element;
657 for (element = cfg_list_first(keylist);
659 element = cfg_list_next(element))
661 obj = cfg_listelt_value(element);
662 str = cfg_obj_asstring(cfg_map_getname(obj));
663 if (strcasecmp(str, keyname) == 0)
667 return (ISC_R_NOTFOUND);
668 obj = cfg_listelt_value(element);
670 return (ISC_R_SUCCESS);
674 controlkeylist_fromcfg(cfg_obj_t *keylist, isc_mem_t *mctx,
675 controlkeylist_t *keyids)
677 cfg_listelt_t *element;
681 controlkey_t *key = NULL;
683 for (element = cfg_list_first(keylist);
685 element = cfg_list_next(element))
687 obj = cfg_listelt_value(element);
688 str = cfg_obj_asstring(obj);
689 newstr = isc_mem_strdup(mctx, str);
692 key = isc_mem_get(mctx, sizeof(*key));
695 key->keyname = newstr;
696 key->secret.base = NULL;
697 key->secret.length = 0;
698 ISC_LINK_INIT(key, link);
699 ISC_LIST_APPEND(*keyids, key, link);
703 return (ISC_R_SUCCESS);
707 isc_mem_free(mctx, newstr);
709 isc_mem_put(mctx, key, sizeof(*key));
710 free_controlkeylist(keyids, mctx);
711 return (ISC_R_NOMEMORY);
715 register_keys(cfg_obj_t *control, cfg_obj_t *keylist,
716 controlkeylist_t *keyids, isc_mem_t *mctx, const char *socktext)
718 controlkey_t *keyid, *next;
725 * Find the keys corresponding to the keyids used by this listener.
727 for (keyid = ISC_LIST_HEAD(*keyids); keyid != NULL; keyid = next) {
728 next = ISC_LIST_NEXT(keyid, link);
730 result = cfgkeylist_find(keylist, keyid->keyname, &keydef);
731 if (result != ISC_R_SUCCESS) {
732 cfg_obj_log(control, ns_g_lctx, ISC_LOG_WARNING,
733 "couldn't find key '%s' for use with "
734 "command channel %s",
735 keyid->keyname, socktext);
736 ISC_LIST_UNLINK(*keyids, keyid, link);
737 free_controlkey(keyid, mctx);
739 cfg_obj_t *algobj = NULL;
740 cfg_obj_t *secretobj = NULL;
742 char *secretstr = NULL;
744 (void)cfg_map_get(keydef, "algorithm", &algobj);
745 (void)cfg_map_get(keydef, "secret", &secretobj);
746 INSIST(algobj != NULL && secretobj != NULL);
748 algstr = cfg_obj_asstring(algobj);
749 secretstr = cfg_obj_asstring(secretobj);
751 if (ns_config_getkeyalgorithm(algstr, NULL) !=
754 cfg_obj_log(control, ns_g_lctx,
756 "unsupported algorithm '%s' in "
757 "key '%s' for use with command "
759 algstr, keyid->keyname, socktext);
760 ISC_LIST_UNLINK(*keyids, keyid, link);
761 free_controlkey(keyid, mctx);
765 isc_buffer_init(&b, secret, sizeof(secret));
766 result = isc_base64_decodestring(secretstr, &b);
768 if (result != ISC_R_SUCCESS) {
769 cfg_obj_log(keydef, ns_g_lctx, ISC_LOG_WARNING,
770 "secret for key '%s' on "
771 "command channel %s: %s",
772 keyid->keyname, socktext,
773 isc_result_totext(result));
774 ISC_LIST_UNLINK(*keyids, keyid, link);
775 free_controlkey(keyid, mctx);
779 keyid->secret.length = isc_buffer_usedlength(&b);
780 keyid->secret.base = isc_mem_get(mctx,
781 keyid->secret.length);
782 if (keyid->secret.base == NULL) {
783 cfg_obj_log(keydef, ns_g_lctx, ISC_LOG_WARNING,
784 "couldn't register key '%s': "
785 "out of memory", keyid->keyname);
786 ISC_LIST_UNLINK(*keyids, keyid, link);
787 free_controlkey(keyid, mctx);
790 memcpy(keyid->secret.base, isc_buffer_base(&b),
791 keyid->secret.length);
799 if (result != ISC_R_SUCCESS) \
804 get_rndckey(isc_mem_t *mctx, controlkeylist_t *keyids) {
806 cfg_parser_t *pctx = NULL;
807 cfg_obj_t *config = NULL;
808 cfg_obj_t *key = NULL;
809 cfg_obj_t *algobj = NULL;
810 cfg_obj_t *secretobj = NULL;
812 char *secretstr = NULL;
813 controlkey_t *keyid = NULL;
817 CHECK(cfg_parser_create(mctx, ns_g_lctx, &pctx));
818 CHECK(cfg_parse_file(pctx, ns_g_keyfile, &cfg_type_rndckey, &config));
819 CHECK(cfg_map_get(config, "key", &key));
821 keyid = isc_mem_get(mctx, sizeof(*keyid));
823 CHECK(ISC_R_NOMEMORY);
824 keyid->keyname = isc_mem_strdup(mctx,
825 cfg_obj_asstring(cfg_map_getname(key)));
826 keyid->secret.base = NULL;
827 keyid->secret.length = 0;
828 ISC_LINK_INIT(keyid, link);
829 if (keyid->keyname == NULL)
830 CHECK(ISC_R_NOMEMORY);
832 CHECK(bind9_check_key(key, ns_g_lctx));
834 (void)cfg_map_get(key, "algorithm", &algobj);
835 (void)cfg_map_get(key, "secret", &secretobj);
836 INSIST(algobj != NULL && secretobj != NULL);
838 algstr = cfg_obj_asstring(algobj);
839 secretstr = cfg_obj_asstring(secretobj);
841 if (ns_config_getkeyalgorithm(algstr, NULL) != ISC_R_SUCCESS) {
842 cfg_obj_log(key, ns_g_lctx,
844 "unsupported algorithm '%s' in "
845 "key '%s' for use with command "
847 algstr, keyid->keyname);
851 isc_buffer_init(&b, secret, sizeof(secret));
852 result = isc_base64_decodestring(secretstr, &b);
854 if (result != ISC_R_SUCCESS) {
855 cfg_obj_log(key, ns_g_lctx, ISC_LOG_WARNING,
856 "secret for key '%s' on command channel: %s",
857 keyid->keyname, isc_result_totext(result));
861 keyid->secret.length = isc_buffer_usedlength(&b);
862 keyid->secret.base = isc_mem_get(mctx,
863 keyid->secret.length);
864 if (keyid->secret.base == NULL) {
865 cfg_obj_log(key, ns_g_lctx, ISC_LOG_WARNING,
866 "couldn't register key '%s': "
867 "out of memory", keyid->keyname);
868 CHECK(ISC_R_NOMEMORY);
870 memcpy(keyid->secret.base, isc_buffer_base(&b),
871 keyid->secret.length);
872 ISC_LIST_APPEND(*keyids, keyid, link);
874 result = ISC_R_SUCCESS;
878 free_controlkey(keyid, mctx);
880 cfg_obj_destroy(pctx, &config);
882 cfg_parser_destroy(&pctx);
887 * Ensures that both '*global_keylistp' and '*control_keylistp' are
888 * valid or both are NULL.
891 get_key_info(cfg_obj_t *config, cfg_obj_t *control,
892 cfg_obj_t **global_keylistp, cfg_obj_t **control_keylistp)
895 cfg_obj_t *control_keylist = NULL;
896 cfg_obj_t *global_keylist = NULL;
898 REQUIRE(global_keylistp != NULL && *global_keylistp == NULL);
899 REQUIRE(control_keylistp != NULL && *control_keylistp == NULL);
901 control_keylist = cfg_tuple_get(control, "keys");
903 if (!cfg_obj_isvoid(control_keylist) &&
904 cfg_list_first(control_keylist) != NULL) {
905 result = cfg_map_get(config, "key", &global_keylist);
907 if (result == ISC_R_SUCCESS) {
908 *global_keylistp = global_keylist;
909 *control_keylistp = control_keylist;
915 update_listener(ns_controls_t *cp,
916 controllistener_t **listenerp, cfg_obj_t *control,
917 cfg_obj_t *config, isc_sockaddr_t *addr,
918 ns_aclconfctx_t *aclconfctx, const char *socktext)
920 controllistener_t *listener;
922 cfg_obj_t *global_keylist = NULL;
923 cfg_obj_t *control_keylist = NULL;
924 dns_acl_t *new_acl = NULL;
925 controlkeylist_t keys;
926 isc_result_t result = ISC_R_SUCCESS;
928 for (listener = ISC_LIST_HEAD(cp->listeners);
930 listener = ISC_LIST_NEXT(listener, link))
931 if (isc_sockaddr_equal(addr, &listener->address))
934 if (listener == NULL) {
940 * There is already a listener for this sockaddr.
941 * Update the access list and key information.
943 * First try to deal with the key situation. There are a few
945 * (a) It had an explicit keylist and still has an explicit keylist.
946 * (b) It had an automagic key and now has an explicit keylist.
947 * (c) It had an explicit keylist and now needs an automagic key.
948 * (d) It has an automagic key and still needs the automagic key.
950 * (c) and (d) are the annoying ones. The caller needs to know
951 * that it should use the automagic configuration for key information
952 * in place of the named.conf configuration.
954 * XXXDCL There is one other hazard that has not been dealt with,
955 * the problem that if a key change is being caused by a control
956 * channel reload, then the response will be with the new key
957 * and not able to be decrypted by the client.
960 get_key_info(config, control, &global_keylist,
963 if (control_keylist != NULL) {
964 INSIST(global_keylist != NULL);
967 result = controlkeylist_fromcfg(control_keylist,
968 listener->mctx, &keys);
969 if (result == ISC_R_SUCCESS) {
970 free_controlkeylist(&listener->keys, listener->mctx);
971 listener->keys = keys;
972 register_keys(control, global_keylist, &listener->keys,
973 listener->mctx, socktext);
976 free_controlkeylist(&listener->keys, listener->mctx);
977 result = get_rndckey(listener->mctx, &listener->keys);
980 if (result != ISC_R_SUCCESS && global_keylist != NULL)
982 * This message might be a little misleading since the
983 * "new keys" might in fact be identical to the old ones,
984 * but tracking whether they are identical just for the
985 * sake of avoiding this message would be too much trouble.
987 cfg_obj_log(control, ns_g_lctx, ISC_LOG_WARNING,
988 "couldn't install new keys for "
989 "command channel %s: %s",
990 socktext, isc_result_totext(result));
994 * Now, keep the old access list unless a new one can be made.
996 if (control != NULL) {
997 allow = cfg_tuple_get(control, "allow");
998 result = ns_acl_fromconfig(allow, config, aclconfctx,
999 listener->mctx, &new_acl);
1001 result = dns_acl_any(listener->mctx, &new_acl);
1004 if (result == ISC_R_SUCCESS) {
1005 dns_acl_detach(&listener->acl);
1006 dns_acl_attach(new_acl, &listener->acl);
1007 dns_acl_detach(&new_acl);
1009 /* XXXDCL say the old acl is still used? */
1010 cfg_obj_log(control, ns_g_lctx, ISC_LOG_WARNING,
1011 "couldn't install new acl for "
1012 "command channel %s: %s",
1013 socktext, isc_result_totext(result));
1015 *listenerp = listener;
1019 add_listener(ns_controls_t *cp, controllistener_t **listenerp,
1020 cfg_obj_t *control, cfg_obj_t *config, isc_sockaddr_t *addr,
1021 ns_aclconfctx_t *aclconfctx, const char *socktext)
1023 isc_mem_t *mctx = cp->server->mctx;
1024 controllistener_t *listener;
1026 cfg_obj_t *global_keylist = NULL;
1027 cfg_obj_t *control_keylist = NULL;
1028 dns_acl_t *new_acl = NULL;
1029 isc_result_t result = ISC_R_SUCCESS;
1031 listener = isc_mem_get(mctx, sizeof(*listener));
1032 if (listener == NULL)
1033 result = ISC_R_NOMEMORY;
1035 if (result == ISC_R_SUCCESS) {
1036 listener->controls = cp;
1037 listener->mctx = mctx;
1038 listener->task = cp->server->task;
1039 listener->address = *addr;
1040 listener->sock = NULL;
1041 listener->listening = ISC_FALSE;
1042 listener->exiting = ISC_FALSE;
1043 listener->acl = NULL;
1044 ISC_LINK_INIT(listener, link);
1045 ISC_LIST_INIT(listener->keys);
1046 ISC_LIST_INIT(listener->connections);
1051 if (control != NULL) {
1052 allow = cfg_tuple_get(control, "allow");
1053 result = ns_acl_fromconfig(allow, config, aclconfctx,
1056 result = dns_acl_any(mctx, &new_acl);
1060 if (result == ISC_R_SUCCESS) {
1061 dns_acl_attach(new_acl, &listener->acl);
1062 dns_acl_detach(&new_acl);
1065 get_key_info(config, control, &global_keylist,
1068 if (control_keylist != NULL) {
1069 result = controlkeylist_fromcfg(control_keylist,
1072 if (result == ISC_R_SUCCESS)
1073 register_keys(control, global_keylist,
1075 listener->mctx, socktext);
1077 result = get_rndckey(mctx, &listener->keys);
1079 if (result != ISC_R_SUCCESS && control != NULL)
1080 cfg_obj_log(control, ns_g_lctx, ISC_LOG_WARNING,
1081 "couldn't install keys for "
1082 "command channel %s: %s",
1083 socktext, isc_result_totext(result));
1086 if (result == ISC_R_SUCCESS) {
1087 int pf = isc_sockaddr_pf(&listener->address);
1088 if ((pf == AF_INET && isc_net_probeipv4() != ISC_R_SUCCESS) ||
1089 (pf == AF_INET6 && isc_net_probeipv6() != ISC_R_SUCCESS))
1090 result = ISC_R_FAMILYNOSUPPORT;
1093 if (result == ISC_R_SUCCESS)
1094 result = isc_socket_create(ns_g_socketmgr,
1095 isc_sockaddr_pf(&listener->address),
1099 if (result == ISC_R_SUCCESS)
1100 result = isc_socket_bind(listener->sock,
1101 &listener->address);
1103 if (result == ISC_R_SUCCESS)
1104 result = control_listen(listener);
1106 if (result == ISC_R_SUCCESS)
1107 result = control_accept(listener);
1109 if (result == ISC_R_SUCCESS) {
1110 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
1111 NS_LOGMODULE_CONTROL, ISC_LOG_NOTICE,
1112 "command channel listening on %s", socktext);
1113 *listenerp = listener;
1116 if (listener != NULL) {
1117 listener->exiting = ISC_TRUE;
1118 free_listener(listener);
1121 if (control != NULL)
1122 cfg_obj_log(control, ns_g_lctx, ISC_LOG_WARNING,
1123 "couldn't add command channel %s: %s",
1124 socktext, isc_result_totext(result));
1126 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
1127 NS_LOGMODULE_CONTROL, ISC_LOG_NOTICE,
1128 "couldn't add command channel %s: %s",
1129 socktext, isc_result_totext(result));
1134 /* XXXDCL return error results? fail hard? */
1138 ns_controls_configure(ns_controls_t *cp, cfg_obj_t *config,
1139 ns_aclconfctx_t *aclconfctx)
1141 controllistener_t *listener;
1142 controllistenerlist_t new_listeners;
1143 cfg_obj_t *controlslist = NULL;
1144 cfg_listelt_t *element, *element2;
1145 char socktext[ISC_SOCKADDR_FORMATSIZE];
1147 ISC_LIST_INIT(new_listeners);
1150 * Get the list of named.conf 'controls' statements.
1152 (void)cfg_map_get(config, "controls", &controlslist);
1155 * Run through the new control channel list, noting sockets that
1156 * are already being listened on and moving them to the new list.
1158 * Identifying duplicate addr/port combinations is left to either
1159 * the underlying config code, or to the bind attempt getting an
1160 * address-in-use error.
1162 if (controlslist != NULL) {
1163 for (element = cfg_list_first(controlslist);
1165 element = cfg_list_next(element)) {
1166 cfg_obj_t *controls;
1167 cfg_obj_t *inetcontrols = NULL;
1169 controls = cfg_listelt_value(element);
1170 (void)cfg_map_get(controls, "inet", &inetcontrols);
1171 if (inetcontrols == NULL)
1174 for (element2 = cfg_list_first(inetcontrols);
1176 element2 = cfg_list_next(element2)) {
1179 isc_sockaddr_t *addr;
1182 * The parser handles BIND 8 configuration file
1183 * syntax, so it allows unix phrases as well
1184 * inet phrases with no keys{} clause.
1186 * "unix" phrases have been reported as
1187 * unsupported by the parser.
1189 control = cfg_listelt_value(element2);
1191 obj = cfg_tuple_get(control, "address");
1192 addr = cfg_obj_assockaddr(obj);
1193 if (isc_sockaddr_getport(addr) == 0)
1194 isc_sockaddr_setport(addr,
1197 isc_sockaddr_format(addr, socktext,
1200 isc_log_write(ns_g_lctx,
1201 NS_LOGCATEGORY_GENERAL,
1202 NS_LOGMODULE_CONTROL,
1204 "processing control channel %s",
1207 update_listener(cp, &listener, control, config,
1208 addr, aclconfctx, socktext);
1210 if (listener != NULL)
1212 * Remove the listener from the old
1213 * list, so it won't be shut down.
1215 ISC_LIST_UNLINK(cp->listeners,
1219 * This is a new listener.
1221 add_listener(cp, &listener, control,
1222 config, addr, aclconfctx,
1225 if (listener != NULL)
1226 ISC_LIST_APPEND(new_listeners,
1233 for (i = 0; i < 2; i++) {
1234 isc_sockaddr_t addr;
1237 struct in_addr localhost;
1239 if (isc_net_probeipv4() != ISC_R_SUCCESS)
1241 localhost.s_addr = htonl(INADDR_LOOPBACK);
1242 isc_sockaddr_fromin(&addr, &localhost, 0);
1244 if (isc_net_probeipv6() != ISC_R_SUCCESS)
1246 isc_sockaddr_fromin6(&addr,
1247 &in6addr_loopback, 0);
1249 isc_sockaddr_setport(&addr, NS_CONTROL_PORT);
1251 isc_sockaddr_format(&addr, socktext, sizeof(socktext));
1253 update_listener(cp, &listener, NULL, NULL,
1254 &addr, NULL, socktext);
1256 if (listener != NULL)
1258 * Remove the listener from the old
1259 * list, so it won't be shut down.
1261 ISC_LIST_UNLINK(cp->listeners,
1265 * This is a new listener.
1267 add_listener(cp, &listener, NULL, NULL,
1268 &addr, NULL, socktext);
1270 if (listener != NULL)
1271 ISC_LIST_APPEND(new_listeners,
1277 * ns_control_shutdown() will stop whatever is on the global
1278 * listeners list, which currently only has whatever sockaddrs
1279 * were in the previous configuration (if any) that do not
1280 * remain in the current configuration.
1282 controls_shutdown(cp);
1285 * Put all of the valid listeners on the listeners list.
1286 * Anything already on listeners in the process of shutting
1287 * down will be taken care of by listen_done().
1289 ISC_LIST_APPENDLIST(cp->listeners, new_listeners, link);
1290 return (ISC_R_SUCCESS);
1294 ns_controls_create(ns_server_t *server, ns_controls_t **ctrlsp) {
1295 isc_mem_t *mctx = server->mctx;
1296 isc_result_t result;
1297 ns_controls_t *controls = isc_mem_get(mctx, sizeof(*controls));
1299 if (controls == NULL)
1300 return (ISC_R_NOMEMORY);
1301 controls->server = server;
1302 ISC_LIST_INIT(controls->listeners);
1303 controls->shuttingdown = ISC_FALSE;
1304 controls->symtab = NULL;
1305 result = isccc_cc_createsymtab(&controls->symtab);
1306 if (result != ISC_R_SUCCESS) {
1307 isc_mem_put(server->mctx, controls, sizeof(*controls));
1311 return (ISC_R_SUCCESS);
1315 ns_controls_destroy(ns_controls_t **ctrlsp) {
1316 ns_controls_t *controls = *ctrlsp;
1318 REQUIRE(ISC_LIST_EMPTY(controls->listeners));
1320 isccc_symtab_destroy(&controls->symtab);
1321 isc_mem_put(controls->server->mctx, controls, sizeof(*controls));