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