s4 dns: Store TKEYs in a ringbuffer
[Samba.git] / source4 / dns_server / dns_server.c
blob887fc8ee1d6391109e628e78729c79aa5c587cca
1 /*
2 Unix SMB/CIFS implementation.
4 DNS server startup
6 Copyright (C) 2010 Kai Blin <kai@samba.org>
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "includes.h"
23 #include "smbd/service_task.h"
24 #include "smbd/service.h"
25 #include "smbd/service_stream.h"
26 #include "smbd/process_model.h"
27 #include "lib/events/events.h"
28 #include "lib/socket/socket.h"
29 #include "lib/tsocket/tsocket.h"
30 #include "libcli/util/tstream.h"
31 #include "libcli/util/ntstatus.h"
32 #include "system/network.h"
33 #include "lib/stream/packet.h"
34 #include "lib/socket/netif.h"
35 #include "dns_server/dns_server.h"
36 #include "param/param.h"
37 #include "librpc/ndr/libndr.h"
38 #include "librpc/gen_ndr/ndr_dns.h"
39 #include "librpc/gen_ndr/ndr_dnsp.h"
40 #include <ldb.h>
41 #include "dsdb/samdb/samdb.h"
42 #include "dsdb/common/util.h"
43 #include "auth/session.h"
44 #include "lib/util/dlinklist.h"
45 #include "lib/util/tevent_werror.h"
46 #include "auth/auth.h"
47 #include "auth/credentials/credentials.h"
49 NTSTATUS server_service_dns_init(void);
51 /* hold information about one dns socket */
52 struct dns_socket {
53 struct dns_server *dns;
54 struct tsocket_address *local_address;
57 struct dns_udp_socket {
58 struct dns_socket *dns_socket;
59 struct tdgram_context *dgram;
60 struct tevent_queue *send_queue;
64 state of an open tcp connection
66 struct dns_tcp_connection {
67 /* stream connection we belong to */
68 struct stream_connection *conn;
70 /* the dns_server the connection belongs to */
71 struct dns_socket *dns_socket;
73 struct tstream_context *tstream;
75 struct tevent_queue *send_queue;
78 static void dns_tcp_terminate_connection(struct dns_tcp_connection *dnsconn, const char *reason)
80 stream_terminate_connection(dnsconn->conn, reason);
83 static void dns_tcp_recv(struct stream_connection *conn, uint16_t flags)
85 struct dns_tcp_connection *dnsconn = talloc_get_type(conn->private_data,
86 struct dns_tcp_connection);
87 /* this should never be triggered! */
88 dns_tcp_terminate_connection(dnsconn, "dns_tcp_recv: called");
91 static void dns_tcp_send(struct stream_connection *conn, uint16_t flags)
93 struct dns_tcp_connection *dnsconn = talloc_get_type(conn->private_data,
94 struct dns_tcp_connection);
95 /* this should never be triggered! */
96 dns_tcp_terminate_connection(dnsconn, "dns_tcp_send: called");
99 struct dns_process_state {
100 DATA_BLOB *in;
101 struct dns_name_packet in_packet;
102 struct dns_request_state state;
103 uint16_t dns_err;
104 struct dns_name_packet out_packet;
105 DATA_BLOB out;
108 static void dns_process_done(struct tevent_req *subreq);
110 static struct tevent_req *dns_process_send(TALLOC_CTX *mem_ctx,
111 struct tevent_context *ev,
112 struct dns_server *dns,
113 DATA_BLOB *in)
115 struct tevent_req *req, *subreq;
116 struct dns_process_state *state;
117 enum ndr_err_code ndr_err;
118 WERROR ret;
120 req = tevent_req_create(mem_ctx, &state, struct dns_process_state);
121 if (req == NULL) {
122 return NULL;
124 state->in = in;
126 if (in->length < 12) {
127 tevent_req_werror(req, WERR_INVALID_PARAM);
128 return tevent_req_post(req, ev);
130 dump_data(8, in->data, in->length);
132 ndr_err = ndr_pull_struct_blob(
133 in, state, &state->in_packet,
134 (ndr_pull_flags_fn_t)ndr_pull_dns_name_packet);
136 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
137 state->dns_err = DNS_RCODE_FORMERR;
138 tevent_req_done(req);
139 return tevent_req_post(req, ev);
141 if (DEBUGLVL(8)) {
142 NDR_PRINT_DEBUG(dns_name_packet, &state->in_packet);
145 state->state.flags = state->in_packet.operation;
146 state->state.flags |= DNS_FLAG_REPLY;
148 if (lpcfg_dns_recursive_queries(dns->task->lp_ctx)) {
149 state->state.flags |= DNS_FLAG_RECURSION_AVAIL;
152 state->out_packet = state->in_packet;
154 switch (state->in_packet.operation & DNS_OPCODE) {
155 case DNS_OPCODE_QUERY:
156 subreq = dns_server_process_query_send(
157 state, ev, dns, &state->state, &state->in_packet);
158 if (tevent_req_nomem(subreq, req)) {
159 return tevent_req_post(req, ev);
161 tevent_req_set_callback(subreq, dns_process_done, req);
162 return req;
163 case DNS_OPCODE_UPDATE:
164 ret = dns_server_process_update(
165 dns, &state->state, state, &state->in_packet,
166 &state->out_packet.answers, &state->out_packet.ancount,
167 &state->out_packet.nsrecs, &state->out_packet.nscount,
168 &state->out_packet.additional,
169 &state->out_packet.arcount);
170 break;
171 default:
172 ret = WERR_DNS_ERROR_RCODE_NOT_IMPLEMENTED;
174 if (!W_ERROR_IS_OK(ret)) {
175 state->dns_err = werr_to_dns_err(ret);
177 tevent_req_done(req);
178 return tevent_req_post(req, ev);
181 static void dns_process_done(struct tevent_req *subreq)
183 struct tevent_req *req = tevent_req_callback_data(
184 subreq, struct tevent_req);
185 struct dns_process_state *state = tevent_req_data(
186 req, struct dns_process_state);
187 WERROR ret;
189 ret = dns_server_process_query_recv(
190 subreq, state,
191 &state->out_packet.answers, &state->out_packet.ancount,
192 &state->out_packet.nsrecs, &state->out_packet.nscount,
193 &state->out_packet.additional, &state->out_packet.arcount);
194 TALLOC_FREE(subreq);
196 if (!W_ERROR_IS_OK(ret)) {
197 state->dns_err = werr_to_dns_err(ret);
199 tevent_req_done(req);
202 static WERROR dns_process_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
203 DATA_BLOB *out)
205 struct dns_process_state *state = tevent_req_data(
206 req, struct dns_process_state);
207 enum ndr_err_code ndr_err;
208 WERROR ret;
210 if (tevent_req_is_werror(req, &ret)) {
211 return ret;
213 if (state->dns_err != DNS_RCODE_OK) {
214 goto drop;
216 state->out_packet.operation |= state->state.flags;
218 ndr_err = ndr_push_struct_blob(
219 out, mem_ctx, &state->out_packet,
220 (ndr_push_flags_fn_t)ndr_push_dns_name_packet);
221 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
222 DEBUG(1, ("Failed to push packet: %s!\n",
223 ndr_errstr(ndr_err)));
224 state->dns_err = DNS_RCODE_SERVFAIL;
225 goto drop;
227 return WERR_OK;
229 drop:
230 *out = data_blob_talloc(mem_ctx, state->in->data, state->in->length);
231 if (out->data == NULL) {
232 return WERR_NOMEM;
234 out->data[2] |= 0x80; /* Toggle DNS_FLAG_REPLY */
235 out->data[3] |= state->dns_err;
236 return WERR_OK;
239 struct dns_tcp_call {
240 struct dns_tcp_connection *dns_conn;
241 DATA_BLOB in;
242 DATA_BLOB out;
243 uint8_t out_hdr[4];
244 struct iovec out_iov[2];
247 static void dns_tcp_call_process_done(struct tevent_req *subreq);
248 static void dns_tcp_call_writev_done(struct tevent_req *subreq);
250 static void dns_tcp_call_loop(struct tevent_req *subreq)
252 struct dns_tcp_connection *dns_conn = tevent_req_callback_data(subreq,
253 struct dns_tcp_connection);
254 struct dns_server *dns = dns_conn->dns_socket->dns;
255 struct dns_tcp_call *call;
256 NTSTATUS status;
258 call = talloc(dns_conn, struct dns_tcp_call);
259 if (call == NULL) {
260 dns_tcp_terminate_connection(dns_conn, "dns_tcp_call_loop: "
261 "no memory for dns_tcp_call");
262 return;
264 call->dns_conn = dns_conn;
266 status = tstream_read_pdu_blob_recv(subreq,
267 call,
268 &call->in);
269 TALLOC_FREE(subreq);
270 if (!NT_STATUS_IS_OK(status)) {
271 const char *reason;
273 reason = talloc_asprintf(call, "dns_tcp_call_loop: "
274 "tstream_read_pdu_blob_recv() - %s",
275 nt_errstr(status));
276 if (!reason) {
277 reason = nt_errstr(status);
280 dns_tcp_terminate_connection(dns_conn, reason);
281 return;
284 DEBUG(10,("Received DNS TCP packet of length %lu from %s\n",
285 (long) call->in.length,
286 tsocket_address_string(dns_conn->conn->remote_address, call)));
288 /* skip length header */
289 call->in.data += 2;
290 call->in.length -= 2;
292 subreq = dns_process_send(call, dns->task->event_ctx, dns,
293 &call->in);
294 if (subreq == NULL) {
295 dns_tcp_terminate_connection(
296 dns_conn, "dns_tcp_call_loop: dns_process_send "
297 "failed\n");
298 return;
300 tevent_req_set_callback(subreq, dns_tcp_call_process_done, call);
303 * The dns tcp pdu's has the length as 2 byte (initial_read_size),
304 * packet_full_request_u16 provides the pdu length then.
306 subreq = tstream_read_pdu_blob_send(dns_conn,
307 dns_conn->conn->event.ctx,
308 dns_conn->tstream,
309 2, /* initial_read_size */
310 packet_full_request_u16,
311 dns_conn);
312 if (subreq == NULL) {
313 dns_tcp_terminate_connection(dns_conn, "dns_tcp_call_loop: "
314 "no memory for tstream_read_pdu_blob_send");
315 return;
317 tevent_req_set_callback(subreq, dns_tcp_call_loop, dns_conn);
320 static void dns_tcp_call_process_done(struct tevent_req *subreq)
322 struct dns_tcp_call *call = tevent_req_callback_data(subreq,
323 struct dns_tcp_call);
324 struct dns_tcp_connection *dns_conn = call->dns_conn;
325 WERROR err;
327 err = dns_process_recv(subreq, call, &call->out);
328 TALLOC_FREE(subreq);
329 if (!W_ERROR_IS_OK(err)) {
330 DEBUG(1, ("dns_process returned %s\n", win_errstr(err)));
331 dns_tcp_terminate_connection(dns_conn,
332 "dns_tcp_call_loop: process function failed");
333 return;
336 /* First add the length of the out buffer */
337 RSSVAL(call->out_hdr, 0, call->out.length);
338 call->out_iov[0].iov_base = (char *) call->out_hdr;
339 call->out_iov[0].iov_len = 2;
341 call->out_iov[1].iov_base = (char *) call->out.data;
342 call->out_iov[1].iov_len = call->out.length;
344 subreq = tstream_writev_queue_send(call,
345 dns_conn->conn->event.ctx,
346 dns_conn->tstream,
347 dns_conn->send_queue,
348 call->out_iov, 2);
349 if (subreq == NULL) {
350 dns_tcp_terminate_connection(dns_conn, "dns_tcp_call_loop: "
351 "no memory for tstream_writev_queue_send");
352 return;
354 tevent_req_set_callback(subreq, dns_tcp_call_writev_done, call);
357 static void dns_tcp_call_writev_done(struct tevent_req *subreq)
359 struct dns_tcp_call *call = tevent_req_callback_data(subreq,
360 struct dns_tcp_call);
361 int sys_errno;
362 int rc;
364 rc = tstream_writev_queue_recv(subreq, &sys_errno);
365 TALLOC_FREE(subreq);
366 if (rc == -1) {
367 const char *reason;
369 reason = talloc_asprintf(call, "dns_tcp_call_writev_done: "
370 "tstream_writev_queue_recv() - %d:%s",
371 sys_errno, strerror(sys_errno));
372 if (!reason) {
373 reason = "dns_tcp_call_writev_done: tstream_writev_queue_recv() failed";
376 dns_tcp_terminate_connection(call->dns_conn, reason);
377 return;
380 /* We don't care about errors */
382 talloc_free(call);
386 called when we get a new connection
388 static void dns_tcp_accept(struct stream_connection *conn)
390 struct dns_socket *dns_socket;
391 struct dns_tcp_connection *dns_conn;
392 struct tevent_req *subreq;
393 int rc;
395 dns_conn = talloc_zero(conn, struct dns_tcp_connection);
396 if (dns_conn == NULL) {
397 stream_terminate_connection(conn,
398 "dns_tcp_accept: out of memory");
399 return;
402 dns_conn->send_queue = tevent_queue_create(conn, "dns_tcp_accept");
403 if (dns_conn->send_queue == NULL) {
404 stream_terminate_connection(conn,
405 "dns_tcp_accept: out of memory");
406 return;
409 dns_socket = talloc_get_type(conn->private_data, struct dns_socket);
411 TALLOC_FREE(conn->event.fde);
413 rc = tstream_bsd_existing_socket(dns_conn,
414 socket_get_fd(conn->socket),
415 &dns_conn->tstream);
416 if (rc < 0) {
417 stream_terminate_connection(conn,
418 "dns_tcp_accept: out of memory");
419 return;
422 dns_conn->conn = conn;
423 dns_conn->dns_socket = dns_socket;
424 conn->private_data = dns_conn;
427 * The dns tcp pdu's has the length as 2 byte (initial_read_size),
428 * packet_full_request_u16 provides the pdu length then.
430 subreq = tstream_read_pdu_blob_send(dns_conn,
431 dns_conn->conn->event.ctx,
432 dns_conn->tstream,
433 2, /* initial_read_size */
434 packet_full_request_u16,
435 dns_conn);
436 if (subreq == NULL) {
437 dns_tcp_terminate_connection(dns_conn, "dns_tcp_accept: "
438 "no memory for tstream_read_pdu_blob_send");
439 return;
441 tevent_req_set_callback(subreq, dns_tcp_call_loop, dns_conn);
444 static const struct stream_server_ops dns_tcp_stream_ops = {
445 .name = "dns_tcp",
446 .accept_connection = dns_tcp_accept,
447 .recv_handler = dns_tcp_recv,
448 .send_handler = dns_tcp_send
451 struct dns_udp_call {
452 struct dns_udp_socket *sock;
453 struct tsocket_address *src;
454 DATA_BLOB in;
455 DATA_BLOB out;
458 static void dns_udp_call_process_done(struct tevent_req *subreq);
459 static void dns_udp_call_sendto_done(struct tevent_req *subreq);
461 static void dns_udp_call_loop(struct tevent_req *subreq)
463 struct dns_udp_socket *sock = tevent_req_callback_data(subreq,
464 struct dns_udp_socket);
465 struct dns_server *dns = sock->dns_socket->dns;
466 struct dns_udp_call *call;
467 uint8_t *buf;
468 ssize_t len;
469 int sys_errno;
471 call = talloc(sock, struct dns_udp_call);
472 if (call == NULL) {
473 talloc_free(call);
474 goto done;
476 call->sock = sock;
478 len = tdgram_recvfrom_recv(subreq, &sys_errno,
479 call, &buf, &call->src);
480 TALLOC_FREE(subreq);
481 if (len == -1) {
482 talloc_free(call);
483 goto done;
486 call->in.data = buf;
487 call->in.length = len;
489 DEBUG(10,("Received DNS UDP packet of length %lu from %s\n",
490 (long)call->in.length,
491 tsocket_address_string(call->src, call)));
493 subreq = dns_process_send(call, dns->task->event_ctx, dns,
494 &call->in);
495 if (subreq == NULL) {
496 TALLOC_FREE(call);
497 goto done;
499 tevent_req_set_callback(subreq, dns_udp_call_process_done, call);
501 done:
502 subreq = tdgram_recvfrom_send(sock,
503 sock->dns_socket->dns->task->event_ctx,
504 sock->dgram);
505 if (subreq == NULL) {
506 task_server_terminate(sock->dns_socket->dns->task,
507 "no memory for tdgram_recvfrom_send",
508 true);
509 return;
511 tevent_req_set_callback(subreq, dns_udp_call_loop, sock);
514 static void dns_udp_call_process_done(struct tevent_req *subreq)
516 struct dns_udp_call *call = tevent_req_callback_data(
517 subreq, struct dns_udp_call);
518 struct dns_udp_socket *sock = call->sock;
519 struct dns_server *dns = sock->dns_socket->dns;
520 WERROR err;
522 err = dns_process_recv(subreq, call, &call->out);
523 TALLOC_FREE(subreq);
524 if (!W_ERROR_IS_OK(err)) {
525 DEBUG(1, ("dns_process returned %s\n", win_errstr(err)));
526 TALLOC_FREE(call);
527 return;
530 subreq = tdgram_sendto_queue_send(call,
531 dns->task->event_ctx,
532 sock->dgram,
533 sock->send_queue,
534 call->out.data,
535 call->out.length,
536 call->src);
537 if (subreq == NULL) {
538 talloc_free(call);
539 return;
541 tevent_req_set_callback(subreq, dns_udp_call_sendto_done, call);
544 static void dns_udp_call_sendto_done(struct tevent_req *subreq)
546 struct dns_udp_call *call = tevent_req_callback_data(subreq,
547 struct dns_udp_call);
548 ssize_t ret;
549 int sys_errno;
551 ret = tdgram_sendto_queue_recv(subreq, &sys_errno);
553 /* We don't care about errors */
555 talloc_free(call);
559 start listening on the given address
561 static NTSTATUS dns_add_socket(struct dns_server *dns,
562 const struct model_ops *model_ops,
563 const char *name,
564 const char *address,
565 uint16_t port)
567 struct dns_socket *dns_socket;
568 struct dns_udp_socket *dns_udp_socket;
569 struct tevent_req *udpsubreq;
570 NTSTATUS status;
571 int ret;
573 dns_socket = talloc(dns, struct dns_socket);
574 NT_STATUS_HAVE_NO_MEMORY(dns_socket);
576 dns_socket->dns = dns;
578 ret = tsocket_address_inet_from_strings(dns_socket, "ip",
579 address, port,
580 &dns_socket->local_address);
581 if (ret != 0) {
582 status = map_nt_error_from_unix_common(errno);
583 return status;
586 status = stream_setup_socket(dns->task,
587 dns->task->event_ctx,
588 dns->task->lp_ctx,
589 model_ops,
590 &dns_tcp_stream_ops,
591 "ip", address, &port,
592 lpcfg_socket_options(dns->task->lp_ctx),
593 dns_socket);
594 if (!NT_STATUS_IS_OK(status)) {
595 DEBUG(0,("Failed to bind to %s:%u TCP - %s\n",
596 address, port, nt_errstr(status)));
597 talloc_free(dns_socket);
598 return status;
601 dns_udp_socket = talloc(dns_socket, struct dns_udp_socket);
602 NT_STATUS_HAVE_NO_MEMORY(dns_udp_socket);
604 dns_udp_socket->dns_socket = dns_socket;
606 ret = tdgram_inet_udp_socket(dns_socket->local_address,
607 NULL,
608 dns_udp_socket,
609 &dns_udp_socket->dgram);
610 if (ret != 0) {
611 status = map_nt_error_from_unix_common(errno);
612 DEBUG(0,("Failed to bind to %s:%u UDP - %s\n",
613 address, port, nt_errstr(status)));
614 return status;
617 dns_udp_socket->send_queue = tevent_queue_create(dns_udp_socket,
618 "dns_udp_send_queue");
619 NT_STATUS_HAVE_NO_MEMORY(dns_udp_socket->send_queue);
621 udpsubreq = tdgram_recvfrom_send(dns_udp_socket,
622 dns->task->event_ctx,
623 dns_udp_socket->dgram);
624 NT_STATUS_HAVE_NO_MEMORY(udpsubreq);
625 tevent_req_set_callback(udpsubreq, dns_udp_call_loop, dns_udp_socket);
627 return NT_STATUS_OK;
631 setup our listening sockets on the configured network interfaces
633 static NTSTATUS dns_startup_interfaces(struct dns_server *dns, struct loadparm_context *lp_ctx,
634 struct interface *ifaces)
636 const struct model_ops *model_ops;
637 int num_interfaces;
638 TALLOC_CTX *tmp_ctx = talloc_new(dns);
639 NTSTATUS status;
640 int i;
642 /* within the dns task we want to be a single process, so
643 ask for the single process model ops and pass these to the
644 stream_setup_socket() call. */
645 model_ops = process_model_startup("single");
646 if (!model_ops) {
647 DEBUG(0,("Can't find 'single' process model_ops\n"));
648 return NT_STATUS_INTERNAL_ERROR;
651 num_interfaces = iface_list_count(ifaces);
653 for (i=0; i<num_interfaces; i++) {
654 const char *address = talloc_strdup(tmp_ctx, iface_list_n_ip(ifaces, i));
656 status = dns_add_socket(dns, model_ops, "dns", address, DNS_SERVICE_PORT);
657 NT_STATUS_NOT_OK_RETURN(status);
660 talloc_free(tmp_ctx);
662 return NT_STATUS_OK;
665 static int dns_server_sort_zones(struct ldb_message **m1, struct ldb_message **m2)
667 const char *n1, *n2;
668 size_t l1, l2;
670 n1 = ldb_msg_find_attr_as_string(*m1, "name", NULL);
671 n2 = ldb_msg_find_attr_as_string(*m2, "name", NULL);
673 l1 = strlen(n1);
674 l2 = strlen(n2);
676 /* If the string lengths are not equal just sort by length */
677 if (l1 != l2) {
678 /* If m1 is the larger zone name, return it first */
679 return l2 - l1;
682 /*TODO: We need to compare DNs here, we want the DomainDNSZones first */
683 return 0;
686 static struct dns_server_tkey_store *tkey_store_init(TALLOC_CTX *mem_ctx,
687 uint16_t size)
689 struct dns_server_tkey_store *buffer = talloc_zero(mem_ctx,
690 struct dns_server_tkey_store);
692 if (buffer == NULL) {
693 return NULL;
696 buffer->size = size;
697 buffer->next_idx = 0;
699 buffer->tkeys = talloc_zero_array(buffer, struct dns_server_tkey *, size);
700 if (buffer->tkeys == NULL) {
701 TALLOC_FREE(buffer);
704 return buffer;
707 static void dns_task_init(struct task_server *task)
709 struct dns_server *dns;
710 NTSTATUS status;
711 struct interface *ifaces;
712 int ret;
713 struct ldb_result *res;
714 static const char * const attrs[] = { "name", NULL};
715 unsigned int i;
717 switch (lpcfg_server_role(task->lp_ctx)) {
718 case ROLE_STANDALONE:
719 task_server_terminate(task, "dns: no DNS required in standalone configuration", false);
720 return;
721 case ROLE_DOMAIN_MEMBER:
722 task_server_terminate(task, "dns: no DNS required in member server configuration", false);
723 return;
724 case ROLE_ACTIVE_DIRECTORY_DC:
725 /* Yes, we want a DNS */
726 break;
729 load_interface_list(task, task->lp_ctx, &ifaces);
731 if (iface_list_count(ifaces) == 0) {
732 task_server_terminate(task, "dns: no network interfaces configured", false);
733 return;
736 task_server_set_title(task, "task[dns]");
738 dns = talloc_zero(task, struct dns_server);
739 if (dns == NULL) {
740 task_server_terminate(task, "dns: out of memory", true);
741 return;
744 dns->task = task;
746 dns->server_credentials = cli_credentials_init(dns);
747 if (!dns->server_credentials) {
748 task_server_terminate(task, "Failed to init server credentials\n", true);
749 return;
752 cli_credentials_set_conf(dns->server_credentials, task->lp_ctx);
753 status = cli_credentials_set_machine_account(dns->server_credentials, task->lp_ctx);
754 if (!NT_STATUS_IS_OK(status)) {
755 task_server_terminate(task,
756 talloc_asprintf(task, "Failed to obtain server credentials, perhaps a standalone server?: %s\n",
757 nt_errstr(status)),
758 true);
759 return;
762 dns->tkeys = tkey_store_init(dns, TKEY_BUFFER_SIZE);
763 if (!dns->tkeys) {
764 task_server_terminate(task, "Failed to allocate tkey storage\n", true);
765 return;
768 dns->samdb = samdb_connect(dns, dns->task->event_ctx, dns->task->lp_ctx,
769 system_session(dns->task->lp_ctx), 0);
770 if (!dns->samdb) {
771 task_server_terminate(task, "dns: samdb_connect failed", true);
772 return;
775 // TODO: this search does not work against windows
776 ret = dsdb_search(dns->samdb, dns, &res, NULL, LDB_SCOPE_SUBTREE,
777 attrs, DSDB_SEARCH_SEARCH_ALL_PARTITIONS, "(objectClass=dnsZone)");
778 if (ret != LDB_SUCCESS) {
779 task_server_terminate(task,
780 "dns: failed to look up root DNS zones",
781 true);
782 return;
785 TYPESAFE_QSORT(res->msgs, res->count, dns_server_sort_zones);
787 for (i=0; i < res->count; i++) {
788 struct dns_server_zone *z;
790 z = talloc_zero(dns, struct dns_server_zone);
791 if (z == NULL) {
794 z->name = ldb_msg_find_attr_as_string(res->msgs[i], "name", NULL);
795 z->dn = talloc_move(z, &res->msgs[i]->dn);
797 DLIST_ADD_END(dns->zones, z, NULL);
800 status = dns_startup_interfaces(dns, task->lp_ctx, ifaces);
801 if (!NT_STATUS_IS_OK(status)) {
802 task_server_terminate(task, "dns failed to setup interfaces", true);
803 return;
807 NTSTATUS server_service_dns_init(void)
809 return register_server_service("dns", dns_task_init);