s4 dns: Handle GSS-TSIG signature creation
[Samba/gebeck_regimport.git] / source4 / dns_server / dns_server.c
blob795b7198aa9c61909f155d261b16a68dae508b24
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_server *dns;
102 struct dns_name_packet in_packet;
103 struct dns_request_state state;
104 uint16_t dns_err;
105 struct dns_name_packet out_packet;
106 DATA_BLOB out;
109 static void dns_process_done(struct tevent_req *subreq);
111 static struct tevent_req *dns_process_send(TALLOC_CTX *mem_ctx,
112 struct tevent_context *ev,
113 struct dns_server *dns,
114 DATA_BLOB *in)
116 struct tevent_req *req, *subreq;
117 struct dns_process_state *state;
118 enum ndr_err_code ndr_err;
119 WERROR ret;
121 req = tevent_req_create(mem_ctx, &state, struct dns_process_state);
122 if (req == NULL) {
123 return NULL;
125 state->in = in;
127 state->dns = dns;
129 if (in->length < 12) {
130 tevent_req_werror(req, WERR_INVALID_PARAM);
131 return tevent_req_post(req, ev);
133 dump_data(8, in->data, in->length);
135 ndr_err = ndr_pull_struct_blob(
136 in, state, &state->in_packet,
137 (ndr_pull_flags_fn_t)ndr_pull_dns_name_packet);
139 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
140 state->dns_err = DNS_RCODE_FORMERR;
141 tevent_req_done(req);
142 return tevent_req_post(req, ev);
144 if (DEBUGLVL(8)) {
145 NDR_PRINT_DEBUG(dns_name_packet, &state->in_packet);
148 state->state.flags = state->in_packet.operation;
149 state->state.flags |= DNS_FLAG_REPLY;
151 if (lpcfg_dns_recursive_queries(dns->task->lp_ctx)) {
152 state->state.flags |= DNS_FLAG_RECURSION_AVAIL;
155 state->out_packet = state->in_packet;
157 switch (state->in_packet.operation & DNS_OPCODE) {
158 case DNS_OPCODE_QUERY:
159 subreq = dns_server_process_query_send(
160 state, ev, dns, &state->state, &state->in_packet);
161 if (tevent_req_nomem(subreq, req)) {
162 return tevent_req_post(req, ev);
164 tevent_req_set_callback(subreq, dns_process_done, req);
165 return req;
166 case DNS_OPCODE_UPDATE:
167 ret = dns_server_process_update(
168 dns, &state->state, state, &state->in_packet,
169 &state->out_packet.answers, &state->out_packet.ancount,
170 &state->out_packet.nsrecs, &state->out_packet.nscount,
171 &state->out_packet.additional,
172 &state->out_packet.arcount);
173 break;
174 default:
175 ret = WERR_DNS_ERROR_RCODE_NOT_IMPLEMENTED;
177 if (!W_ERROR_IS_OK(ret)) {
178 state->dns_err = werr_to_dns_err(ret);
180 tevent_req_done(req);
181 return tevent_req_post(req, ev);
184 static void dns_process_done(struct tevent_req *subreq)
186 struct tevent_req *req = tevent_req_callback_data(
187 subreq, struct tevent_req);
188 struct dns_process_state *state = tevent_req_data(
189 req, struct dns_process_state);
190 WERROR ret;
192 ret = dns_server_process_query_recv(
193 subreq, state,
194 &state->out_packet.answers, &state->out_packet.ancount,
195 &state->out_packet.nsrecs, &state->out_packet.nscount,
196 &state->out_packet.additional, &state->out_packet.arcount);
197 TALLOC_FREE(subreq);
199 if (!W_ERROR_IS_OK(ret)) {
200 state->dns_err = werr_to_dns_err(ret);
202 tevent_req_done(req);
205 static WERROR dns_process_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
206 DATA_BLOB *out)
208 struct dns_process_state *state = tevent_req_data(
209 req, struct dns_process_state);
210 enum ndr_err_code ndr_err;
211 WERROR ret;
213 if (tevent_req_is_werror(req, &ret)) {
214 return ret;
216 if (state->dns_err != DNS_RCODE_OK) {
217 goto drop;
219 state->out_packet.operation |= state->state.flags;
221 if (state->state.sign) {
222 ret = dns_sign_tsig(state->dns, mem_ctx, &state->state,
223 &state->out_packet, 0);
224 if (!W_ERROR_IS_OK(ret)) {
225 state->dns_err = DNS_RCODE_SERVFAIL;
226 goto drop;
230 ndr_err = ndr_push_struct_blob(
231 out, mem_ctx, &state->out_packet,
232 (ndr_push_flags_fn_t)ndr_push_dns_name_packet);
233 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
234 DEBUG(1, ("Failed to push packet: %s!\n",
235 ndr_errstr(ndr_err)));
236 state->dns_err = DNS_RCODE_SERVFAIL;
237 goto drop;
239 return WERR_OK;
241 drop:
242 *out = data_blob_talloc(mem_ctx, state->in->data, state->in->length);
243 if (out->data == NULL) {
244 return WERR_NOMEM;
246 out->data[2] |= 0x80; /* Toggle DNS_FLAG_REPLY */
247 out->data[3] |= state->dns_err;
248 return WERR_OK;
251 struct dns_tcp_call {
252 struct dns_tcp_connection *dns_conn;
253 DATA_BLOB in;
254 DATA_BLOB out;
255 uint8_t out_hdr[4];
256 struct iovec out_iov[2];
259 static void dns_tcp_call_process_done(struct tevent_req *subreq);
260 static void dns_tcp_call_writev_done(struct tevent_req *subreq);
262 static void dns_tcp_call_loop(struct tevent_req *subreq)
264 struct dns_tcp_connection *dns_conn = tevent_req_callback_data(subreq,
265 struct dns_tcp_connection);
266 struct dns_server *dns = dns_conn->dns_socket->dns;
267 struct dns_tcp_call *call;
268 NTSTATUS status;
270 call = talloc(dns_conn, struct dns_tcp_call);
271 if (call == NULL) {
272 dns_tcp_terminate_connection(dns_conn, "dns_tcp_call_loop: "
273 "no memory for dns_tcp_call");
274 return;
276 call->dns_conn = dns_conn;
278 status = tstream_read_pdu_blob_recv(subreq,
279 call,
280 &call->in);
281 TALLOC_FREE(subreq);
282 if (!NT_STATUS_IS_OK(status)) {
283 const char *reason;
285 reason = talloc_asprintf(call, "dns_tcp_call_loop: "
286 "tstream_read_pdu_blob_recv() - %s",
287 nt_errstr(status));
288 if (!reason) {
289 reason = nt_errstr(status);
292 dns_tcp_terminate_connection(dns_conn, reason);
293 return;
296 DEBUG(10,("Received DNS TCP packet of length %lu from %s\n",
297 (long) call->in.length,
298 tsocket_address_string(dns_conn->conn->remote_address, call)));
300 /* skip length header */
301 call->in.data += 2;
302 call->in.length -= 2;
304 subreq = dns_process_send(call, dns->task->event_ctx, dns,
305 &call->in);
306 if (subreq == NULL) {
307 dns_tcp_terminate_connection(
308 dns_conn, "dns_tcp_call_loop: dns_process_send "
309 "failed\n");
310 return;
312 tevent_req_set_callback(subreq, dns_tcp_call_process_done, call);
315 * The dns tcp pdu's has the length as 2 byte (initial_read_size),
316 * packet_full_request_u16 provides the pdu length then.
318 subreq = tstream_read_pdu_blob_send(dns_conn,
319 dns_conn->conn->event.ctx,
320 dns_conn->tstream,
321 2, /* initial_read_size */
322 packet_full_request_u16,
323 dns_conn);
324 if (subreq == NULL) {
325 dns_tcp_terminate_connection(dns_conn, "dns_tcp_call_loop: "
326 "no memory for tstream_read_pdu_blob_send");
327 return;
329 tevent_req_set_callback(subreq, dns_tcp_call_loop, dns_conn);
332 static void dns_tcp_call_process_done(struct tevent_req *subreq)
334 struct dns_tcp_call *call = tevent_req_callback_data(subreq,
335 struct dns_tcp_call);
336 struct dns_tcp_connection *dns_conn = call->dns_conn;
337 WERROR err;
339 err = dns_process_recv(subreq, call, &call->out);
340 TALLOC_FREE(subreq);
341 if (!W_ERROR_IS_OK(err)) {
342 DEBUG(1, ("dns_process returned %s\n", win_errstr(err)));
343 dns_tcp_terminate_connection(dns_conn,
344 "dns_tcp_call_loop: process function failed");
345 return;
348 /* First add the length of the out buffer */
349 RSSVAL(call->out_hdr, 0, call->out.length);
350 call->out_iov[0].iov_base = (char *) call->out_hdr;
351 call->out_iov[0].iov_len = 2;
353 call->out_iov[1].iov_base = (char *) call->out.data;
354 call->out_iov[1].iov_len = call->out.length;
356 subreq = tstream_writev_queue_send(call,
357 dns_conn->conn->event.ctx,
358 dns_conn->tstream,
359 dns_conn->send_queue,
360 call->out_iov, 2);
361 if (subreq == NULL) {
362 dns_tcp_terminate_connection(dns_conn, "dns_tcp_call_loop: "
363 "no memory for tstream_writev_queue_send");
364 return;
366 tevent_req_set_callback(subreq, dns_tcp_call_writev_done, call);
369 static void dns_tcp_call_writev_done(struct tevent_req *subreq)
371 struct dns_tcp_call *call = tevent_req_callback_data(subreq,
372 struct dns_tcp_call);
373 int sys_errno;
374 int rc;
376 rc = tstream_writev_queue_recv(subreq, &sys_errno);
377 TALLOC_FREE(subreq);
378 if (rc == -1) {
379 const char *reason;
381 reason = talloc_asprintf(call, "dns_tcp_call_writev_done: "
382 "tstream_writev_queue_recv() - %d:%s",
383 sys_errno, strerror(sys_errno));
384 if (!reason) {
385 reason = "dns_tcp_call_writev_done: tstream_writev_queue_recv() failed";
388 dns_tcp_terminate_connection(call->dns_conn, reason);
389 return;
392 /* We don't care about errors */
394 talloc_free(call);
398 called when we get a new connection
400 static void dns_tcp_accept(struct stream_connection *conn)
402 struct dns_socket *dns_socket;
403 struct dns_tcp_connection *dns_conn;
404 struct tevent_req *subreq;
405 int rc;
407 dns_conn = talloc_zero(conn, struct dns_tcp_connection);
408 if (dns_conn == NULL) {
409 stream_terminate_connection(conn,
410 "dns_tcp_accept: out of memory");
411 return;
414 dns_conn->send_queue = tevent_queue_create(conn, "dns_tcp_accept");
415 if (dns_conn->send_queue == NULL) {
416 stream_terminate_connection(conn,
417 "dns_tcp_accept: out of memory");
418 return;
421 dns_socket = talloc_get_type(conn->private_data, struct dns_socket);
423 TALLOC_FREE(conn->event.fde);
425 rc = tstream_bsd_existing_socket(dns_conn,
426 socket_get_fd(conn->socket),
427 &dns_conn->tstream);
428 if (rc < 0) {
429 stream_terminate_connection(conn,
430 "dns_tcp_accept: out of memory");
431 return;
434 dns_conn->conn = conn;
435 dns_conn->dns_socket = dns_socket;
436 conn->private_data = dns_conn;
439 * The dns tcp pdu's has the length as 2 byte (initial_read_size),
440 * packet_full_request_u16 provides the pdu length then.
442 subreq = tstream_read_pdu_blob_send(dns_conn,
443 dns_conn->conn->event.ctx,
444 dns_conn->tstream,
445 2, /* initial_read_size */
446 packet_full_request_u16,
447 dns_conn);
448 if (subreq == NULL) {
449 dns_tcp_terminate_connection(dns_conn, "dns_tcp_accept: "
450 "no memory for tstream_read_pdu_blob_send");
451 return;
453 tevent_req_set_callback(subreq, dns_tcp_call_loop, dns_conn);
456 static const struct stream_server_ops dns_tcp_stream_ops = {
457 .name = "dns_tcp",
458 .accept_connection = dns_tcp_accept,
459 .recv_handler = dns_tcp_recv,
460 .send_handler = dns_tcp_send
463 struct dns_udp_call {
464 struct dns_udp_socket *sock;
465 struct tsocket_address *src;
466 DATA_BLOB in;
467 DATA_BLOB out;
470 static void dns_udp_call_process_done(struct tevent_req *subreq);
471 static void dns_udp_call_sendto_done(struct tevent_req *subreq);
473 static void dns_udp_call_loop(struct tevent_req *subreq)
475 struct dns_udp_socket *sock = tevent_req_callback_data(subreq,
476 struct dns_udp_socket);
477 struct dns_server *dns = sock->dns_socket->dns;
478 struct dns_udp_call *call;
479 uint8_t *buf;
480 ssize_t len;
481 int sys_errno;
483 call = talloc(sock, struct dns_udp_call);
484 if (call == NULL) {
485 talloc_free(call);
486 goto done;
488 call->sock = sock;
490 len = tdgram_recvfrom_recv(subreq, &sys_errno,
491 call, &buf, &call->src);
492 TALLOC_FREE(subreq);
493 if (len == -1) {
494 talloc_free(call);
495 goto done;
498 call->in.data = buf;
499 call->in.length = len;
501 DEBUG(10,("Received DNS UDP packet of length %lu from %s\n",
502 (long)call->in.length,
503 tsocket_address_string(call->src, call)));
505 subreq = dns_process_send(call, dns->task->event_ctx, dns,
506 &call->in);
507 if (subreq == NULL) {
508 TALLOC_FREE(call);
509 goto done;
511 tevent_req_set_callback(subreq, dns_udp_call_process_done, call);
513 done:
514 subreq = tdgram_recvfrom_send(sock,
515 sock->dns_socket->dns->task->event_ctx,
516 sock->dgram);
517 if (subreq == NULL) {
518 task_server_terminate(sock->dns_socket->dns->task,
519 "no memory for tdgram_recvfrom_send",
520 true);
521 return;
523 tevent_req_set_callback(subreq, dns_udp_call_loop, sock);
526 static void dns_udp_call_process_done(struct tevent_req *subreq)
528 struct dns_udp_call *call = tevent_req_callback_data(
529 subreq, struct dns_udp_call);
530 struct dns_udp_socket *sock = call->sock;
531 struct dns_server *dns = sock->dns_socket->dns;
532 WERROR err;
534 err = dns_process_recv(subreq, call, &call->out);
535 TALLOC_FREE(subreq);
536 if (!W_ERROR_IS_OK(err)) {
537 DEBUG(1, ("dns_process returned %s\n", win_errstr(err)));
538 TALLOC_FREE(call);
539 return;
542 subreq = tdgram_sendto_queue_send(call,
543 dns->task->event_ctx,
544 sock->dgram,
545 sock->send_queue,
546 call->out.data,
547 call->out.length,
548 call->src);
549 if (subreq == NULL) {
550 talloc_free(call);
551 return;
553 tevent_req_set_callback(subreq, dns_udp_call_sendto_done, call);
556 static void dns_udp_call_sendto_done(struct tevent_req *subreq)
558 struct dns_udp_call *call = tevent_req_callback_data(subreq,
559 struct dns_udp_call);
560 ssize_t ret;
561 int sys_errno;
563 ret = tdgram_sendto_queue_recv(subreq, &sys_errno);
565 /* We don't care about errors */
567 talloc_free(call);
571 start listening on the given address
573 static NTSTATUS dns_add_socket(struct dns_server *dns,
574 const struct model_ops *model_ops,
575 const char *name,
576 const char *address,
577 uint16_t port)
579 struct dns_socket *dns_socket;
580 struct dns_udp_socket *dns_udp_socket;
581 struct tevent_req *udpsubreq;
582 NTSTATUS status;
583 int ret;
585 dns_socket = talloc(dns, struct dns_socket);
586 NT_STATUS_HAVE_NO_MEMORY(dns_socket);
588 dns_socket->dns = dns;
590 ret = tsocket_address_inet_from_strings(dns_socket, "ip",
591 address, port,
592 &dns_socket->local_address);
593 if (ret != 0) {
594 status = map_nt_error_from_unix_common(errno);
595 return status;
598 status = stream_setup_socket(dns->task,
599 dns->task->event_ctx,
600 dns->task->lp_ctx,
601 model_ops,
602 &dns_tcp_stream_ops,
603 "ip", address, &port,
604 lpcfg_socket_options(dns->task->lp_ctx),
605 dns_socket);
606 if (!NT_STATUS_IS_OK(status)) {
607 DEBUG(0,("Failed to bind to %s:%u TCP - %s\n",
608 address, port, nt_errstr(status)));
609 talloc_free(dns_socket);
610 return status;
613 dns_udp_socket = talloc(dns_socket, struct dns_udp_socket);
614 NT_STATUS_HAVE_NO_MEMORY(dns_udp_socket);
616 dns_udp_socket->dns_socket = dns_socket;
618 ret = tdgram_inet_udp_socket(dns_socket->local_address,
619 NULL,
620 dns_udp_socket,
621 &dns_udp_socket->dgram);
622 if (ret != 0) {
623 status = map_nt_error_from_unix_common(errno);
624 DEBUG(0,("Failed to bind to %s:%u UDP - %s\n",
625 address, port, nt_errstr(status)));
626 return status;
629 dns_udp_socket->send_queue = tevent_queue_create(dns_udp_socket,
630 "dns_udp_send_queue");
631 NT_STATUS_HAVE_NO_MEMORY(dns_udp_socket->send_queue);
633 udpsubreq = tdgram_recvfrom_send(dns_udp_socket,
634 dns->task->event_ctx,
635 dns_udp_socket->dgram);
636 NT_STATUS_HAVE_NO_MEMORY(udpsubreq);
637 tevent_req_set_callback(udpsubreq, dns_udp_call_loop, dns_udp_socket);
639 return NT_STATUS_OK;
643 setup our listening sockets on the configured network interfaces
645 static NTSTATUS dns_startup_interfaces(struct dns_server *dns, struct loadparm_context *lp_ctx,
646 struct interface *ifaces)
648 const struct model_ops *model_ops;
649 int num_interfaces;
650 TALLOC_CTX *tmp_ctx = talloc_new(dns);
651 NTSTATUS status;
652 int i;
654 /* within the dns task we want to be a single process, so
655 ask for the single process model ops and pass these to the
656 stream_setup_socket() call. */
657 model_ops = process_model_startup("single");
658 if (!model_ops) {
659 DEBUG(0,("Can't find 'single' process model_ops\n"));
660 return NT_STATUS_INTERNAL_ERROR;
663 num_interfaces = iface_list_count(ifaces);
665 for (i=0; i<num_interfaces; i++) {
666 const char *address = talloc_strdup(tmp_ctx, iface_list_n_ip(ifaces, i));
668 status = dns_add_socket(dns, model_ops, "dns", address, DNS_SERVICE_PORT);
669 NT_STATUS_NOT_OK_RETURN(status);
672 talloc_free(tmp_ctx);
674 return NT_STATUS_OK;
677 static int dns_server_sort_zones(struct ldb_message **m1, struct ldb_message **m2)
679 const char *n1, *n2;
680 size_t l1, l2;
682 n1 = ldb_msg_find_attr_as_string(*m1, "name", NULL);
683 n2 = ldb_msg_find_attr_as_string(*m2, "name", NULL);
685 l1 = strlen(n1);
686 l2 = strlen(n2);
688 /* If the string lengths are not equal just sort by length */
689 if (l1 != l2) {
690 /* If m1 is the larger zone name, return it first */
691 return l2 - l1;
694 /*TODO: We need to compare DNs here, we want the DomainDNSZones first */
695 return 0;
698 static struct dns_server_tkey_store *tkey_store_init(TALLOC_CTX *mem_ctx,
699 uint16_t size)
701 struct dns_server_tkey_store *buffer = talloc_zero(mem_ctx,
702 struct dns_server_tkey_store);
704 if (buffer == NULL) {
705 return NULL;
708 buffer->size = size;
709 buffer->next_idx = 0;
711 buffer->tkeys = talloc_zero_array(buffer, struct dns_server_tkey *, size);
712 if (buffer->tkeys == NULL) {
713 TALLOC_FREE(buffer);
716 return buffer;
719 static void dns_task_init(struct task_server *task)
721 struct dns_server *dns;
722 NTSTATUS status;
723 struct interface *ifaces;
724 int ret;
725 struct ldb_result *res;
726 static const char * const attrs[] = { "name", NULL};
727 unsigned int i;
729 switch (lpcfg_server_role(task->lp_ctx)) {
730 case ROLE_STANDALONE:
731 task_server_terminate(task, "dns: no DNS required in standalone configuration", false);
732 return;
733 case ROLE_DOMAIN_MEMBER:
734 task_server_terminate(task, "dns: no DNS required in member server configuration", false);
735 return;
736 case ROLE_ACTIVE_DIRECTORY_DC:
737 /* Yes, we want a DNS */
738 break;
741 load_interface_list(task, task->lp_ctx, &ifaces);
743 if (iface_list_count(ifaces) == 0) {
744 task_server_terminate(task, "dns: no network interfaces configured", false);
745 return;
748 task_server_set_title(task, "task[dns]");
750 dns = talloc_zero(task, struct dns_server);
751 if (dns == NULL) {
752 task_server_terminate(task, "dns: out of memory", true);
753 return;
756 dns->task = task;
758 dns->server_credentials = cli_credentials_init(dns);
759 if (!dns->server_credentials) {
760 task_server_terminate(task, "Failed to init server credentials\n", true);
761 return;
764 cli_credentials_set_conf(dns->server_credentials, task->lp_ctx);
765 status = cli_credentials_set_machine_account(dns->server_credentials, task->lp_ctx);
766 if (!NT_STATUS_IS_OK(status)) {
767 task_server_terminate(task,
768 talloc_asprintf(task, "Failed to obtain server credentials, perhaps a standalone server?: %s\n",
769 nt_errstr(status)),
770 true);
771 return;
774 dns->tkeys = tkey_store_init(dns, TKEY_BUFFER_SIZE);
775 if (!dns->tkeys) {
776 task_server_terminate(task, "Failed to allocate tkey storage\n", true);
777 return;
780 dns->samdb = samdb_connect(dns, dns->task->event_ctx, dns->task->lp_ctx,
781 system_session(dns->task->lp_ctx), 0);
782 if (!dns->samdb) {
783 task_server_terminate(task, "dns: samdb_connect failed", true);
784 return;
787 // TODO: this search does not work against windows
788 ret = dsdb_search(dns->samdb, dns, &res, NULL, LDB_SCOPE_SUBTREE,
789 attrs, DSDB_SEARCH_SEARCH_ALL_PARTITIONS, "(objectClass=dnsZone)");
790 if (ret != LDB_SUCCESS) {
791 task_server_terminate(task,
792 "dns: failed to look up root DNS zones",
793 true);
794 return;
797 TYPESAFE_QSORT(res->msgs, res->count, dns_server_sort_zones);
799 for (i=0; i < res->count; i++) {
800 struct dns_server_zone *z;
802 z = talloc_zero(dns, struct dns_server_zone);
803 if (z == NULL) {
806 z->name = ldb_msg_find_attr_as_string(res->msgs[i], "name", NULL);
807 z->dn = talloc_move(z, &res->msgs[i]->dn);
809 DLIST_ADD_END(dns->zones, z, NULL);
812 status = dns_startup_interfaces(dns, task->lp_ctx, ifaces);
813 if (!NT_STATUS_IS_OK(status)) {
814 task_server_terminate(task, "dns failed to setup interfaces", true);
815 return;
819 NTSTATUS server_service_dns_init(void)
821 return register_server_service("dns", dns_task_init);