dns: Use new DNS debugclass in DNS server
[Samba.git] / source4 / dns_server / dns_server.c
blob6e0285ffc417d6e74f75a182bd5d84c1f81302bf
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 #undef DBGC_CLASS
50 #define DBGC_CLASS DBGC_DNS
52 NTSTATUS server_service_dns_init(void);
54 /* hold information about one dns socket */
55 struct dns_socket {
56 struct dns_server *dns;
57 struct tsocket_address *local_address;
60 struct dns_udp_socket {
61 struct dns_socket *dns_socket;
62 struct tdgram_context *dgram;
63 struct tevent_queue *send_queue;
67 state of an open tcp connection
69 struct dns_tcp_connection {
70 /* stream connection we belong to */
71 struct stream_connection *conn;
73 /* the dns_server the connection belongs to */
74 struct dns_socket *dns_socket;
76 struct tstream_context *tstream;
78 struct tevent_queue *send_queue;
81 static void dns_tcp_terminate_connection(struct dns_tcp_connection *dnsconn, const char *reason)
83 stream_terminate_connection(dnsconn->conn, reason);
86 static void dns_tcp_recv(struct stream_connection *conn, uint16_t flags)
88 struct dns_tcp_connection *dnsconn = talloc_get_type(conn->private_data,
89 struct dns_tcp_connection);
90 /* this should never be triggered! */
91 dns_tcp_terminate_connection(dnsconn, "dns_tcp_recv: called");
94 static void dns_tcp_send(struct stream_connection *conn, uint16_t flags)
96 struct dns_tcp_connection *dnsconn = talloc_get_type(conn->private_data,
97 struct dns_tcp_connection);
98 /* this should never be triggered! */
99 dns_tcp_terminate_connection(dnsconn, "dns_tcp_send: called");
102 struct dns_process_state {
103 DATA_BLOB *in;
104 struct dns_server *dns;
105 struct dns_name_packet in_packet;
106 struct dns_request_state state;
107 uint16_t dns_err;
108 struct dns_name_packet out_packet;
109 DATA_BLOB out;
112 static void dns_process_done(struct tevent_req *subreq);
114 static struct tevent_req *dns_process_send(TALLOC_CTX *mem_ctx,
115 struct tevent_context *ev,
116 struct dns_server *dns,
117 DATA_BLOB *in)
119 struct tevent_req *req, *subreq;
120 struct dns_process_state *state;
121 enum ndr_err_code ndr_err;
122 WERROR ret;
123 const char *forwarder = lpcfg_dns_forwarder(dns->task->lp_ctx);
124 req = tevent_req_create(mem_ctx, &state, struct dns_process_state);
125 if (req == NULL) {
126 return NULL;
128 state->in = in;
130 state->dns = dns;
132 if (in->length < 12) {
133 tevent_req_werror(req, WERR_INVALID_PARAM);
134 return tevent_req_post(req, ev);
136 dump_data(8, in->data, in->length);
138 ndr_err = ndr_pull_struct_blob(
139 in, state, &state->in_packet,
140 (ndr_pull_flags_fn_t)ndr_pull_dns_name_packet);
142 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
143 state->dns_err = DNS_RCODE_FORMERR;
144 tevent_req_done(req);
145 return tevent_req_post(req, ev);
147 if (DEBUGLVL(8)) {
148 NDR_PRINT_DEBUG(dns_name_packet, &state->in_packet);
151 ret = dns_verify_tsig(dns, state, &state->state, &state->in_packet, in);
152 if (!W_ERROR_IS_OK(ret)) {
153 DEBUG(1, ("Failed to verify TSIG!\n"));
154 state->dns_err = werr_to_dns_err(ret);
155 tevent_req_done(req);
156 return tevent_req_post(req, ev);
159 state->state.flags = state->in_packet.operation;
160 state->state.flags |= DNS_FLAG_REPLY;
163 if (forwarder && *forwarder) {
164 state->state.flags |= DNS_FLAG_RECURSION_AVAIL;
167 state->out_packet = state->in_packet;
169 switch (state->in_packet.operation & DNS_OPCODE) {
170 case DNS_OPCODE_QUERY:
171 subreq = dns_server_process_query_send(
172 state, ev, dns, &state->state, &state->in_packet);
173 if (tevent_req_nomem(subreq, req)) {
174 return tevent_req_post(req, ev);
176 tevent_req_set_callback(subreq, dns_process_done, req);
177 return req;
178 case DNS_OPCODE_UPDATE:
179 ret = dns_server_process_update(
180 dns, &state->state, state, &state->in_packet,
181 &state->out_packet.answers, &state->out_packet.ancount,
182 &state->out_packet.nsrecs, &state->out_packet.nscount,
183 &state->out_packet.additional,
184 &state->out_packet.arcount);
185 break;
186 default:
187 ret = WERR_DNS_ERROR_RCODE_NOT_IMPLEMENTED;
189 if (!W_ERROR_IS_OK(ret)) {
190 state->dns_err = werr_to_dns_err(ret);
192 tevent_req_done(req);
193 return tevent_req_post(req, ev);
196 static void dns_process_done(struct tevent_req *subreq)
198 struct tevent_req *req = tevent_req_callback_data(
199 subreq, struct tevent_req);
200 struct dns_process_state *state = tevent_req_data(
201 req, struct dns_process_state);
202 WERROR ret;
204 ret = dns_server_process_query_recv(
205 subreq, state,
206 &state->out_packet.answers, &state->out_packet.ancount,
207 &state->out_packet.nsrecs, &state->out_packet.nscount,
208 &state->out_packet.additional, &state->out_packet.arcount);
209 TALLOC_FREE(subreq);
211 if (!W_ERROR_IS_OK(ret)) {
212 state->dns_err = werr_to_dns_err(ret);
214 tevent_req_done(req);
217 static WERROR dns_process_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
218 DATA_BLOB *out)
220 struct dns_process_state *state = tevent_req_data(
221 req, struct dns_process_state);
222 enum ndr_err_code ndr_err;
223 WERROR ret;
225 if (tevent_req_is_werror(req, &ret)) {
226 return ret;
228 if (state->dns_err != DNS_RCODE_OK) {
229 goto drop;
231 state->out_packet.operation |= state->state.flags;
233 if (state->state.sign) {
234 ret = dns_sign_tsig(state->dns, mem_ctx, &state->state,
235 &state->out_packet, 0);
236 if (!W_ERROR_IS_OK(ret)) {
237 state->dns_err = DNS_RCODE_SERVFAIL;
238 goto drop;
242 ndr_err = ndr_push_struct_blob(
243 out, mem_ctx, &state->out_packet,
244 (ndr_push_flags_fn_t)ndr_push_dns_name_packet);
245 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
246 DEBUG(1, ("Failed to push packet: %s!\n",
247 ndr_errstr(ndr_err)));
248 state->dns_err = DNS_RCODE_SERVFAIL;
249 goto drop;
251 return WERR_OK;
253 drop:
254 *out = data_blob_talloc(mem_ctx, state->in->data, state->in->length);
255 if (out->data == NULL) {
256 return WERR_NOMEM;
258 out->data[2] |= 0x80; /* Toggle DNS_FLAG_REPLY */
259 out->data[3] |= state->dns_err;
260 return WERR_OK;
263 struct dns_tcp_call {
264 struct dns_tcp_connection *dns_conn;
265 DATA_BLOB in;
266 DATA_BLOB out;
267 uint8_t out_hdr[4];
268 struct iovec out_iov[2];
271 static void dns_tcp_call_process_done(struct tevent_req *subreq);
272 static void dns_tcp_call_writev_done(struct tevent_req *subreq);
274 static void dns_tcp_call_loop(struct tevent_req *subreq)
276 struct dns_tcp_connection *dns_conn = tevent_req_callback_data(subreq,
277 struct dns_tcp_connection);
278 struct dns_server *dns = dns_conn->dns_socket->dns;
279 struct dns_tcp_call *call;
280 NTSTATUS status;
282 call = talloc(dns_conn, struct dns_tcp_call);
283 if (call == NULL) {
284 dns_tcp_terminate_connection(dns_conn, "dns_tcp_call_loop: "
285 "no memory for dns_tcp_call");
286 return;
288 call->dns_conn = dns_conn;
290 status = tstream_read_pdu_blob_recv(subreq,
291 call,
292 &call->in);
293 TALLOC_FREE(subreq);
294 if (!NT_STATUS_IS_OK(status)) {
295 const char *reason;
297 reason = talloc_asprintf(call, "dns_tcp_call_loop: "
298 "tstream_read_pdu_blob_recv() - %s",
299 nt_errstr(status));
300 if (!reason) {
301 reason = nt_errstr(status);
304 dns_tcp_terminate_connection(dns_conn, reason);
305 return;
308 DEBUG(10,("Received DNS TCP packet of length %lu from %s\n",
309 (long) call->in.length,
310 tsocket_address_string(dns_conn->conn->remote_address, call)));
312 /* skip length header */
313 call->in.data += 2;
314 call->in.length -= 2;
316 subreq = dns_process_send(call, dns->task->event_ctx, dns,
317 &call->in);
318 if (subreq == NULL) {
319 dns_tcp_terminate_connection(
320 dns_conn, "dns_tcp_call_loop: dns_process_send "
321 "failed\n");
322 return;
324 tevent_req_set_callback(subreq, dns_tcp_call_process_done, call);
327 * The dns tcp pdu's has the length as 2 byte (initial_read_size),
328 * packet_full_request_u16 provides the pdu length then.
330 subreq = tstream_read_pdu_blob_send(dns_conn,
331 dns_conn->conn->event.ctx,
332 dns_conn->tstream,
333 2, /* initial_read_size */
334 packet_full_request_u16,
335 dns_conn);
336 if (subreq == NULL) {
337 dns_tcp_terminate_connection(dns_conn, "dns_tcp_call_loop: "
338 "no memory for tstream_read_pdu_blob_send");
339 return;
341 tevent_req_set_callback(subreq, dns_tcp_call_loop, dns_conn);
344 static void dns_tcp_call_process_done(struct tevent_req *subreq)
346 struct dns_tcp_call *call = tevent_req_callback_data(subreq,
347 struct dns_tcp_call);
348 struct dns_tcp_connection *dns_conn = call->dns_conn;
349 WERROR err;
351 err = dns_process_recv(subreq, call, &call->out);
352 TALLOC_FREE(subreq);
353 if (!W_ERROR_IS_OK(err)) {
354 DEBUG(1, ("dns_process returned %s\n", win_errstr(err)));
355 dns_tcp_terminate_connection(dns_conn,
356 "dns_tcp_call_loop: process function failed");
357 return;
360 /* First add the length of the out buffer */
361 RSSVAL(call->out_hdr, 0, call->out.length);
362 call->out_iov[0].iov_base = (char *) call->out_hdr;
363 call->out_iov[0].iov_len = 2;
365 call->out_iov[1].iov_base = (char *) call->out.data;
366 call->out_iov[1].iov_len = call->out.length;
368 subreq = tstream_writev_queue_send(call,
369 dns_conn->conn->event.ctx,
370 dns_conn->tstream,
371 dns_conn->send_queue,
372 call->out_iov, 2);
373 if (subreq == NULL) {
374 dns_tcp_terminate_connection(dns_conn, "dns_tcp_call_loop: "
375 "no memory for tstream_writev_queue_send");
376 return;
378 tevent_req_set_callback(subreq, dns_tcp_call_writev_done, call);
381 static void dns_tcp_call_writev_done(struct tevent_req *subreq)
383 struct dns_tcp_call *call = tevent_req_callback_data(subreq,
384 struct dns_tcp_call);
385 int sys_errno;
386 int rc;
388 rc = tstream_writev_queue_recv(subreq, &sys_errno);
389 TALLOC_FREE(subreq);
390 if (rc == -1) {
391 const char *reason;
393 reason = talloc_asprintf(call, "dns_tcp_call_writev_done: "
394 "tstream_writev_queue_recv() - %d:%s",
395 sys_errno, strerror(sys_errno));
396 if (!reason) {
397 reason = "dns_tcp_call_writev_done: tstream_writev_queue_recv() failed";
400 dns_tcp_terminate_connection(call->dns_conn, reason);
401 return;
404 /* We don't care about errors */
406 talloc_free(call);
410 called when we get a new connection
412 static void dns_tcp_accept(struct stream_connection *conn)
414 struct dns_socket *dns_socket;
415 struct dns_tcp_connection *dns_conn;
416 struct tevent_req *subreq;
417 int rc;
419 dns_conn = talloc_zero(conn, struct dns_tcp_connection);
420 if (dns_conn == NULL) {
421 stream_terminate_connection(conn,
422 "dns_tcp_accept: out of memory");
423 return;
426 dns_conn->send_queue = tevent_queue_create(conn, "dns_tcp_accept");
427 if (dns_conn->send_queue == NULL) {
428 stream_terminate_connection(conn,
429 "dns_tcp_accept: out of memory");
430 return;
433 dns_socket = talloc_get_type(conn->private_data, struct dns_socket);
435 TALLOC_FREE(conn->event.fde);
437 rc = tstream_bsd_existing_socket(dns_conn,
438 socket_get_fd(conn->socket),
439 &dns_conn->tstream);
440 if (rc < 0) {
441 stream_terminate_connection(conn,
442 "dns_tcp_accept: out of memory");
443 return;
446 dns_conn->conn = conn;
447 dns_conn->dns_socket = dns_socket;
448 conn->private_data = dns_conn;
451 * The dns tcp pdu's has the length as 2 byte (initial_read_size),
452 * packet_full_request_u16 provides the pdu length then.
454 subreq = tstream_read_pdu_blob_send(dns_conn,
455 dns_conn->conn->event.ctx,
456 dns_conn->tstream,
457 2, /* initial_read_size */
458 packet_full_request_u16,
459 dns_conn);
460 if (subreq == NULL) {
461 dns_tcp_terminate_connection(dns_conn, "dns_tcp_accept: "
462 "no memory for tstream_read_pdu_blob_send");
463 return;
465 tevent_req_set_callback(subreq, dns_tcp_call_loop, dns_conn);
468 static const struct stream_server_ops dns_tcp_stream_ops = {
469 .name = "dns_tcp",
470 .accept_connection = dns_tcp_accept,
471 .recv_handler = dns_tcp_recv,
472 .send_handler = dns_tcp_send
475 struct dns_udp_call {
476 struct dns_udp_socket *sock;
477 struct tsocket_address *src;
478 DATA_BLOB in;
479 DATA_BLOB out;
482 static void dns_udp_call_process_done(struct tevent_req *subreq);
483 static void dns_udp_call_sendto_done(struct tevent_req *subreq);
485 static void dns_udp_call_loop(struct tevent_req *subreq)
487 struct dns_udp_socket *sock = tevent_req_callback_data(subreq,
488 struct dns_udp_socket);
489 struct dns_server *dns = sock->dns_socket->dns;
490 struct dns_udp_call *call;
491 uint8_t *buf;
492 ssize_t len;
493 int sys_errno;
495 call = talloc(sock, struct dns_udp_call);
496 if (call == NULL) {
497 talloc_free(call);
498 goto done;
500 call->sock = sock;
502 len = tdgram_recvfrom_recv(subreq, &sys_errno,
503 call, &buf, &call->src);
504 TALLOC_FREE(subreq);
505 if (len == -1) {
506 talloc_free(call);
507 goto done;
510 call->in.data = buf;
511 call->in.length = len;
513 DEBUG(10,("Received DNS UDP packet of length %lu from %s\n",
514 (long)call->in.length,
515 tsocket_address_string(call->src, call)));
517 subreq = dns_process_send(call, dns->task->event_ctx, dns,
518 &call->in);
519 if (subreq == NULL) {
520 TALLOC_FREE(call);
521 goto done;
523 tevent_req_set_callback(subreq, dns_udp_call_process_done, call);
525 done:
526 subreq = tdgram_recvfrom_send(sock,
527 sock->dns_socket->dns->task->event_ctx,
528 sock->dgram);
529 if (subreq == NULL) {
530 task_server_terminate(sock->dns_socket->dns->task,
531 "no memory for tdgram_recvfrom_send",
532 true);
533 return;
535 tevent_req_set_callback(subreq, dns_udp_call_loop, sock);
538 static void dns_udp_call_process_done(struct tevent_req *subreq)
540 struct dns_udp_call *call = tevent_req_callback_data(
541 subreq, struct dns_udp_call);
542 struct dns_udp_socket *sock = call->sock;
543 struct dns_server *dns = sock->dns_socket->dns;
544 WERROR err;
546 err = dns_process_recv(subreq, call, &call->out);
547 TALLOC_FREE(subreq);
548 if (!W_ERROR_IS_OK(err)) {
549 DEBUG(1, ("dns_process returned %s\n", win_errstr(err)));
550 TALLOC_FREE(call);
551 return;
554 subreq = tdgram_sendto_queue_send(call,
555 dns->task->event_ctx,
556 sock->dgram,
557 sock->send_queue,
558 call->out.data,
559 call->out.length,
560 call->src);
561 if (subreq == NULL) {
562 talloc_free(call);
563 return;
565 tevent_req_set_callback(subreq, dns_udp_call_sendto_done, call);
568 static void dns_udp_call_sendto_done(struct tevent_req *subreq)
570 struct dns_udp_call *call = tevent_req_callback_data(subreq,
571 struct dns_udp_call);
572 int sys_errno;
574 tdgram_sendto_queue_recv(subreq, &sys_errno);
576 /* We don't care about errors */
578 talloc_free(call);
582 start listening on the given address
584 static NTSTATUS dns_add_socket(struct dns_server *dns,
585 const struct model_ops *model_ops,
586 const char *name,
587 const char *address,
588 uint16_t port)
590 struct dns_socket *dns_socket;
591 struct dns_udp_socket *dns_udp_socket;
592 struct tevent_req *udpsubreq;
593 NTSTATUS status;
594 int ret;
596 dns_socket = talloc(dns, struct dns_socket);
597 NT_STATUS_HAVE_NO_MEMORY(dns_socket);
599 dns_socket->dns = dns;
601 ret = tsocket_address_inet_from_strings(dns_socket, "ip",
602 address, port,
603 &dns_socket->local_address);
604 if (ret != 0) {
605 status = map_nt_error_from_unix_common(errno);
606 return status;
609 status = stream_setup_socket(dns->task,
610 dns->task->event_ctx,
611 dns->task->lp_ctx,
612 model_ops,
613 &dns_tcp_stream_ops,
614 "ip", address, &port,
615 lpcfg_socket_options(dns->task->lp_ctx),
616 dns_socket);
617 if (!NT_STATUS_IS_OK(status)) {
618 DEBUG(0,("Failed to bind to %s:%u TCP - %s\n",
619 address, port, nt_errstr(status)));
620 talloc_free(dns_socket);
621 return status;
624 dns_udp_socket = talloc(dns_socket, struct dns_udp_socket);
625 NT_STATUS_HAVE_NO_MEMORY(dns_udp_socket);
627 dns_udp_socket->dns_socket = dns_socket;
629 ret = tdgram_inet_udp_socket(dns_socket->local_address,
630 NULL,
631 dns_udp_socket,
632 &dns_udp_socket->dgram);
633 if (ret != 0) {
634 status = map_nt_error_from_unix_common(errno);
635 DEBUG(0,("Failed to bind to %s:%u UDP - %s\n",
636 address, port, nt_errstr(status)));
637 return status;
640 dns_udp_socket->send_queue = tevent_queue_create(dns_udp_socket,
641 "dns_udp_send_queue");
642 NT_STATUS_HAVE_NO_MEMORY(dns_udp_socket->send_queue);
644 udpsubreq = tdgram_recvfrom_send(dns_udp_socket,
645 dns->task->event_ctx,
646 dns_udp_socket->dgram);
647 NT_STATUS_HAVE_NO_MEMORY(udpsubreq);
648 tevent_req_set_callback(udpsubreq, dns_udp_call_loop, dns_udp_socket);
650 return NT_STATUS_OK;
654 setup our listening sockets on the configured network interfaces
656 static NTSTATUS dns_startup_interfaces(struct dns_server *dns, struct loadparm_context *lp_ctx,
657 struct interface *ifaces)
659 const struct model_ops *model_ops;
660 int num_interfaces;
661 TALLOC_CTX *tmp_ctx = talloc_new(dns);
662 NTSTATUS status;
663 int i;
665 /* within the dns task we want to be a single process, so
666 ask for the single process model ops and pass these to the
667 stream_setup_socket() call. */
668 model_ops = process_model_startup("single");
669 if (!model_ops) {
670 DEBUG(0,("Can't find 'single' process model_ops\n"));
671 return NT_STATUS_INTERNAL_ERROR;
674 if (ifaces != NULL) {
675 num_interfaces = iface_list_count(ifaces);
677 for (i=0; i<num_interfaces; i++) {
678 const char *address = talloc_strdup(tmp_ctx,
679 iface_list_n_ip(ifaces, i));
681 status = dns_add_socket(dns, model_ops, "dns", address,
682 DNS_SERVICE_PORT);
683 NT_STATUS_NOT_OK_RETURN(status);
685 } else {
686 const char **wcard;
687 wcard = iface_list_wildcard(tmp_ctx, lp_ctx);
688 if (wcard == NULL) {
689 DEBUG(0, ("No wildcard address available\n"));
690 return NT_STATUS_INTERNAL_ERROR;
692 for (i = 0; wcard[i] != NULL; i++) {
693 status = dns_add_socket(dns, model_ops, "dns", wcard[i],
694 DNS_SERVICE_PORT);
695 NT_STATUS_NOT_OK_RETURN(status);
699 talloc_free(tmp_ctx);
701 return NT_STATUS_OK;
704 static int dns_server_sort_zones(struct ldb_message **m1, struct ldb_message **m2)
706 const char *n1, *n2;
707 size_t l1, l2;
709 n1 = ldb_msg_find_attr_as_string(*m1, "name", NULL);
710 n2 = ldb_msg_find_attr_as_string(*m2, "name", NULL);
712 l1 = strlen(n1);
713 l2 = strlen(n2);
715 /* If the string lengths are not equal just sort by length */
716 if (l1 != l2) {
717 /* If m1 is the larger zone name, return it first */
718 return l2 - l1;
721 /*TODO: We need to compare DNs here, we want the DomainDNSZones first */
722 return 0;
725 static struct dns_server_tkey_store *tkey_store_init(TALLOC_CTX *mem_ctx,
726 uint16_t size)
728 struct dns_server_tkey_store *buffer = talloc_zero(mem_ctx,
729 struct dns_server_tkey_store);
731 if (buffer == NULL) {
732 return NULL;
735 buffer->size = size;
736 buffer->next_idx = 0;
738 buffer->tkeys = talloc_zero_array(buffer, struct dns_server_tkey *, size);
739 if (buffer->tkeys == NULL) {
740 TALLOC_FREE(buffer);
743 return buffer;
746 static void dns_task_init(struct task_server *task)
748 struct dns_server *dns;
749 NTSTATUS status;
750 struct interface *ifaces = NULL;
751 int ret;
752 struct ldb_result *res;
753 static const char * const attrs[] = { "name", NULL};
754 static const char * const attrs_none[] = { NULL};
755 unsigned int i;
756 struct ldb_message *dns_acc;
757 char *hostname_lower;
758 char *dns_spn;
760 switch (lpcfg_server_role(task->lp_ctx)) {
761 case ROLE_STANDALONE:
762 task_server_terminate(task, "dns: no DNS required in standalone configuration", false);
763 return;
764 case ROLE_DOMAIN_MEMBER:
765 task_server_terminate(task, "dns: no DNS required in member server configuration", false);
766 return;
767 case ROLE_ACTIVE_DIRECTORY_DC:
768 /* Yes, we want a DNS */
769 break;
772 if (lpcfg_interfaces(task->lp_ctx) && lpcfg_bind_interfaces_only(task->lp_ctx)) {
773 load_interface_list(task, task->lp_ctx, &ifaces);
775 if (iface_list_count(ifaces) == 0) {
776 task_server_terminate(task, "dns: no network interfaces configured", false);
777 return;
781 task_server_set_title(task, "task[dns]");
783 dns = talloc_zero(task, struct dns_server);
784 if (dns == NULL) {
785 task_server_terminate(task, "dns: out of memory", true);
786 return;
789 dns->task = task;
791 dns->server_credentials = cli_credentials_init(dns);
792 if (!dns->server_credentials) {
793 task_server_terminate(task, "Failed to init server credentials\n", true);
794 return;
797 dns->samdb = samdb_connect(dns, dns->task->event_ctx, dns->task->lp_ctx,
798 system_session(dns->task->lp_ctx), 0);
799 if (!dns->samdb) {
800 task_server_terminate(task, "dns: samdb_connect failed", true);
801 return;
804 cli_credentials_set_conf(dns->server_credentials, task->lp_ctx);
806 hostname_lower = strlower_talloc(dns, lpcfg_netbios_name(task->lp_ctx));
807 dns_spn = talloc_asprintf(dns, "DNS/%s.%s",
808 hostname_lower,
809 lpcfg_dnsdomain(task->lp_ctx));
810 TALLOC_FREE(hostname_lower);
812 ret = dsdb_search_one(dns->samdb, dns, &dns_acc,
813 ldb_get_default_basedn(dns->samdb), LDB_SCOPE_SUBTREE,
814 attrs_none, 0, "(servicePrincipalName=%s)",
815 dns_spn);
816 if (ret == LDB_SUCCESS) {
817 TALLOC_FREE(dns_acc);
818 if (!dns_spn) {
819 task_server_terminate(task, "dns: talloc_asprintf failed", true);
820 return;
822 status = cli_credentials_set_stored_principal(dns->server_credentials, task->lp_ctx, dns_spn);
823 if (!NT_STATUS_IS_OK(status)) {
824 task_server_terminate(task,
825 talloc_asprintf(task, "Failed to obtain server credentials for DNS, "
826 "despite finding it in the samdb! %s\n",
827 nt_errstr(status)),
828 true);
829 return;
831 } else {
832 TALLOC_FREE(dns_spn);
833 status = cli_credentials_set_machine_account(dns->server_credentials, task->lp_ctx);
834 if (!NT_STATUS_IS_OK(status)) {
835 task_server_terminate(task,
836 talloc_asprintf(task, "Failed to obtain server credentials, perhaps a standalone server?: %s\n",
837 nt_errstr(status)),
838 true);
839 return;
843 dns->tkeys = tkey_store_init(dns, TKEY_BUFFER_SIZE);
844 if (!dns->tkeys) {
845 task_server_terminate(task, "Failed to allocate tkey storage\n", true);
846 return;
849 // TODO: this search does not work against windows
850 ret = dsdb_search(dns->samdb, dns, &res, NULL, LDB_SCOPE_SUBTREE,
851 attrs, DSDB_SEARCH_SEARCH_ALL_PARTITIONS, "(objectClass=dnsZone)");
852 if (ret != LDB_SUCCESS) {
853 task_server_terminate(task,
854 "dns: failed to look up root DNS zones",
855 true);
856 return;
859 TYPESAFE_QSORT(res->msgs, res->count, dns_server_sort_zones);
861 for (i=0; i < res->count; i++) {
862 struct dns_server_zone *z;
864 z = talloc_zero(dns, struct dns_server_zone);
865 if (z == NULL) {
866 task_server_terminate(task, "dns failed to allocate memory", true);
869 z->name = ldb_msg_find_attr_as_string(res->msgs[i], "name", NULL);
870 z->dn = talloc_move(z, &res->msgs[i]->dn);
872 * Ignore the RootDNSServers zone and zones that we don't support yet
873 * RootDNSServers should never be returned (Windows DNS server don't)
874 * ..TrustAnchors should never be returned as is, (Windows returns
875 * TrustAnchors) and for the moment we don't support DNSSEC so we'd better
876 * not return this zone.
878 if ((strcmp(z->name, "RootDNSServers") == 0) ||
879 (strcmp(z->name, "..TrustAnchors") == 0))
881 DEBUG(10, ("Ignoring zone %s\n", z->name));
882 talloc_free(z);
883 continue;
885 DLIST_ADD_END(dns->zones, z, NULL);
888 status = dns_startup_interfaces(dns, task->lp_ctx, ifaces);
889 if (!NT_STATUS_IS_OK(status)) {
890 task_server_terminate(task, "dns failed to setup interfaces", true);
891 return;
895 NTSTATUS server_service_dns_init(void)
897 return register_server_service("dns", dns_task_init);