dsdb-acl: make use of acl_check_access_on_objectclass() for the object in acl_delete()
[Samba/gebeck_regimport.git] / source4 / dns_server / dns_server.c
blob29953c3f4da05de21246987da7a181a3deec75bb
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;
120 const char *forwarder = lpcfg_dns_forwarder(dns->task->lp_ctx);
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 ret = dns_verify_tsig(dns, state, &state->state, &state->in_packet, in);
149 if (!W_ERROR_IS_OK(ret)) {
150 DEBUG(1, ("Failed to verify TSIG!\n"));
151 state->dns_err = werr_to_dns_err(ret);
152 tevent_req_done(req);
153 return tevent_req_post(req, ev);
156 state->state.flags = state->in_packet.operation;
157 state->state.flags |= DNS_FLAG_REPLY;
160 if (forwarder && *forwarder) {
161 state->state.flags |= DNS_FLAG_RECURSION_AVAIL;
164 state->out_packet = state->in_packet;
166 switch (state->in_packet.operation & DNS_OPCODE) {
167 case DNS_OPCODE_QUERY:
168 subreq = dns_server_process_query_send(
169 state, ev, dns, &state->state, &state->in_packet);
170 if (tevent_req_nomem(subreq, req)) {
171 return tevent_req_post(req, ev);
173 tevent_req_set_callback(subreq, dns_process_done, req);
174 return req;
175 case DNS_OPCODE_UPDATE:
176 ret = dns_server_process_update(
177 dns, &state->state, state, &state->in_packet,
178 &state->out_packet.answers, &state->out_packet.ancount,
179 &state->out_packet.nsrecs, &state->out_packet.nscount,
180 &state->out_packet.additional,
181 &state->out_packet.arcount);
182 break;
183 default:
184 ret = WERR_DNS_ERROR_RCODE_NOT_IMPLEMENTED;
186 if (!W_ERROR_IS_OK(ret)) {
187 state->dns_err = werr_to_dns_err(ret);
189 tevent_req_done(req);
190 return tevent_req_post(req, ev);
193 static void dns_process_done(struct tevent_req *subreq)
195 struct tevent_req *req = tevent_req_callback_data(
196 subreq, struct tevent_req);
197 struct dns_process_state *state = tevent_req_data(
198 req, struct dns_process_state);
199 WERROR ret;
201 ret = dns_server_process_query_recv(
202 subreq, state,
203 &state->out_packet.answers, &state->out_packet.ancount,
204 &state->out_packet.nsrecs, &state->out_packet.nscount,
205 &state->out_packet.additional, &state->out_packet.arcount);
206 TALLOC_FREE(subreq);
208 if (!W_ERROR_IS_OK(ret)) {
209 state->dns_err = werr_to_dns_err(ret);
211 tevent_req_done(req);
214 static WERROR dns_process_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
215 DATA_BLOB *out)
217 struct dns_process_state *state = tevent_req_data(
218 req, struct dns_process_state);
219 enum ndr_err_code ndr_err;
220 WERROR ret;
222 if (tevent_req_is_werror(req, &ret)) {
223 return ret;
225 if (state->dns_err != DNS_RCODE_OK) {
226 goto drop;
228 state->out_packet.operation |= state->state.flags;
230 if (state->state.sign) {
231 ret = dns_sign_tsig(state->dns, mem_ctx, &state->state,
232 &state->out_packet, 0);
233 if (!W_ERROR_IS_OK(ret)) {
234 state->dns_err = DNS_RCODE_SERVFAIL;
235 goto drop;
239 ndr_err = ndr_push_struct_blob(
240 out, mem_ctx, &state->out_packet,
241 (ndr_push_flags_fn_t)ndr_push_dns_name_packet);
242 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
243 DEBUG(1, ("Failed to push packet: %s!\n",
244 ndr_errstr(ndr_err)));
245 state->dns_err = DNS_RCODE_SERVFAIL;
246 goto drop;
248 return WERR_OK;
250 drop:
251 *out = data_blob_talloc(mem_ctx, state->in->data, state->in->length);
252 if (out->data == NULL) {
253 return WERR_NOMEM;
255 out->data[2] |= 0x80; /* Toggle DNS_FLAG_REPLY */
256 out->data[3] |= state->dns_err;
257 return WERR_OK;
260 struct dns_tcp_call {
261 struct dns_tcp_connection *dns_conn;
262 DATA_BLOB in;
263 DATA_BLOB out;
264 uint8_t out_hdr[4];
265 struct iovec out_iov[2];
268 static void dns_tcp_call_process_done(struct tevent_req *subreq);
269 static void dns_tcp_call_writev_done(struct tevent_req *subreq);
271 static void dns_tcp_call_loop(struct tevent_req *subreq)
273 struct dns_tcp_connection *dns_conn = tevent_req_callback_data(subreq,
274 struct dns_tcp_connection);
275 struct dns_server *dns = dns_conn->dns_socket->dns;
276 struct dns_tcp_call *call;
277 NTSTATUS status;
279 call = talloc(dns_conn, struct dns_tcp_call);
280 if (call == NULL) {
281 dns_tcp_terminate_connection(dns_conn, "dns_tcp_call_loop: "
282 "no memory for dns_tcp_call");
283 return;
285 call->dns_conn = dns_conn;
287 status = tstream_read_pdu_blob_recv(subreq,
288 call,
289 &call->in);
290 TALLOC_FREE(subreq);
291 if (!NT_STATUS_IS_OK(status)) {
292 const char *reason;
294 reason = talloc_asprintf(call, "dns_tcp_call_loop: "
295 "tstream_read_pdu_blob_recv() - %s",
296 nt_errstr(status));
297 if (!reason) {
298 reason = nt_errstr(status);
301 dns_tcp_terminate_connection(dns_conn, reason);
302 return;
305 DEBUG(10,("Received DNS TCP packet of length %lu from %s\n",
306 (long) call->in.length,
307 tsocket_address_string(dns_conn->conn->remote_address, call)));
309 /* skip length header */
310 call->in.data += 2;
311 call->in.length -= 2;
313 subreq = dns_process_send(call, dns->task->event_ctx, dns,
314 &call->in);
315 if (subreq == NULL) {
316 dns_tcp_terminate_connection(
317 dns_conn, "dns_tcp_call_loop: dns_process_send "
318 "failed\n");
319 return;
321 tevent_req_set_callback(subreq, dns_tcp_call_process_done, call);
324 * The dns tcp pdu's has the length as 2 byte (initial_read_size),
325 * packet_full_request_u16 provides the pdu length then.
327 subreq = tstream_read_pdu_blob_send(dns_conn,
328 dns_conn->conn->event.ctx,
329 dns_conn->tstream,
330 2, /* initial_read_size */
331 packet_full_request_u16,
332 dns_conn);
333 if (subreq == NULL) {
334 dns_tcp_terminate_connection(dns_conn, "dns_tcp_call_loop: "
335 "no memory for tstream_read_pdu_blob_send");
336 return;
338 tevent_req_set_callback(subreq, dns_tcp_call_loop, dns_conn);
341 static void dns_tcp_call_process_done(struct tevent_req *subreq)
343 struct dns_tcp_call *call = tevent_req_callback_data(subreq,
344 struct dns_tcp_call);
345 struct dns_tcp_connection *dns_conn = call->dns_conn;
346 WERROR err;
348 err = dns_process_recv(subreq, call, &call->out);
349 TALLOC_FREE(subreq);
350 if (!W_ERROR_IS_OK(err)) {
351 DEBUG(1, ("dns_process returned %s\n", win_errstr(err)));
352 dns_tcp_terminate_connection(dns_conn,
353 "dns_tcp_call_loop: process function failed");
354 return;
357 /* First add the length of the out buffer */
358 RSSVAL(call->out_hdr, 0, call->out.length);
359 call->out_iov[0].iov_base = (char *) call->out_hdr;
360 call->out_iov[0].iov_len = 2;
362 call->out_iov[1].iov_base = (char *) call->out.data;
363 call->out_iov[1].iov_len = call->out.length;
365 subreq = tstream_writev_queue_send(call,
366 dns_conn->conn->event.ctx,
367 dns_conn->tstream,
368 dns_conn->send_queue,
369 call->out_iov, 2);
370 if (subreq == NULL) {
371 dns_tcp_terminate_connection(dns_conn, "dns_tcp_call_loop: "
372 "no memory for tstream_writev_queue_send");
373 return;
375 tevent_req_set_callback(subreq, dns_tcp_call_writev_done, call);
378 static void dns_tcp_call_writev_done(struct tevent_req *subreq)
380 struct dns_tcp_call *call = tevent_req_callback_data(subreq,
381 struct dns_tcp_call);
382 int sys_errno;
383 int rc;
385 rc = tstream_writev_queue_recv(subreq, &sys_errno);
386 TALLOC_FREE(subreq);
387 if (rc == -1) {
388 const char *reason;
390 reason = talloc_asprintf(call, "dns_tcp_call_writev_done: "
391 "tstream_writev_queue_recv() - %d:%s",
392 sys_errno, strerror(sys_errno));
393 if (!reason) {
394 reason = "dns_tcp_call_writev_done: tstream_writev_queue_recv() failed";
397 dns_tcp_terminate_connection(call->dns_conn, reason);
398 return;
401 /* We don't care about errors */
403 talloc_free(call);
407 called when we get a new connection
409 static void dns_tcp_accept(struct stream_connection *conn)
411 struct dns_socket *dns_socket;
412 struct dns_tcp_connection *dns_conn;
413 struct tevent_req *subreq;
414 int rc;
416 dns_conn = talloc_zero(conn, struct dns_tcp_connection);
417 if (dns_conn == NULL) {
418 stream_terminate_connection(conn,
419 "dns_tcp_accept: out of memory");
420 return;
423 dns_conn->send_queue = tevent_queue_create(conn, "dns_tcp_accept");
424 if (dns_conn->send_queue == NULL) {
425 stream_terminate_connection(conn,
426 "dns_tcp_accept: out of memory");
427 return;
430 dns_socket = talloc_get_type(conn->private_data, struct dns_socket);
432 TALLOC_FREE(conn->event.fde);
434 rc = tstream_bsd_existing_socket(dns_conn,
435 socket_get_fd(conn->socket),
436 &dns_conn->tstream);
437 if (rc < 0) {
438 stream_terminate_connection(conn,
439 "dns_tcp_accept: out of memory");
440 return;
443 dns_conn->conn = conn;
444 dns_conn->dns_socket = dns_socket;
445 conn->private_data = dns_conn;
448 * The dns tcp pdu's has the length as 2 byte (initial_read_size),
449 * packet_full_request_u16 provides the pdu length then.
451 subreq = tstream_read_pdu_blob_send(dns_conn,
452 dns_conn->conn->event.ctx,
453 dns_conn->tstream,
454 2, /* initial_read_size */
455 packet_full_request_u16,
456 dns_conn);
457 if (subreq == NULL) {
458 dns_tcp_terminate_connection(dns_conn, "dns_tcp_accept: "
459 "no memory for tstream_read_pdu_blob_send");
460 return;
462 tevent_req_set_callback(subreq, dns_tcp_call_loop, dns_conn);
465 static const struct stream_server_ops dns_tcp_stream_ops = {
466 .name = "dns_tcp",
467 .accept_connection = dns_tcp_accept,
468 .recv_handler = dns_tcp_recv,
469 .send_handler = dns_tcp_send
472 struct dns_udp_call {
473 struct dns_udp_socket *sock;
474 struct tsocket_address *src;
475 DATA_BLOB in;
476 DATA_BLOB out;
479 static void dns_udp_call_process_done(struct tevent_req *subreq);
480 static void dns_udp_call_sendto_done(struct tevent_req *subreq);
482 static void dns_udp_call_loop(struct tevent_req *subreq)
484 struct dns_udp_socket *sock = tevent_req_callback_data(subreq,
485 struct dns_udp_socket);
486 struct dns_server *dns = sock->dns_socket->dns;
487 struct dns_udp_call *call;
488 uint8_t *buf;
489 ssize_t len;
490 int sys_errno;
492 call = talloc(sock, struct dns_udp_call);
493 if (call == NULL) {
494 talloc_free(call);
495 goto done;
497 call->sock = sock;
499 len = tdgram_recvfrom_recv(subreq, &sys_errno,
500 call, &buf, &call->src);
501 TALLOC_FREE(subreq);
502 if (len == -1) {
503 talloc_free(call);
504 goto done;
507 call->in.data = buf;
508 call->in.length = len;
510 DEBUG(10,("Received DNS UDP packet of length %lu from %s\n",
511 (long)call->in.length,
512 tsocket_address_string(call->src, call)));
514 subreq = dns_process_send(call, dns->task->event_ctx, dns,
515 &call->in);
516 if (subreq == NULL) {
517 TALLOC_FREE(call);
518 goto done;
520 tevent_req_set_callback(subreq, dns_udp_call_process_done, call);
522 done:
523 subreq = tdgram_recvfrom_send(sock,
524 sock->dns_socket->dns->task->event_ctx,
525 sock->dgram);
526 if (subreq == NULL) {
527 task_server_terminate(sock->dns_socket->dns->task,
528 "no memory for tdgram_recvfrom_send",
529 true);
530 return;
532 tevent_req_set_callback(subreq, dns_udp_call_loop, sock);
535 static void dns_udp_call_process_done(struct tevent_req *subreq)
537 struct dns_udp_call *call = tevent_req_callback_data(
538 subreq, struct dns_udp_call);
539 struct dns_udp_socket *sock = call->sock;
540 struct dns_server *dns = sock->dns_socket->dns;
541 WERROR err;
543 err = dns_process_recv(subreq, call, &call->out);
544 TALLOC_FREE(subreq);
545 if (!W_ERROR_IS_OK(err)) {
546 DEBUG(1, ("dns_process returned %s\n", win_errstr(err)));
547 TALLOC_FREE(call);
548 return;
551 subreq = tdgram_sendto_queue_send(call,
552 dns->task->event_ctx,
553 sock->dgram,
554 sock->send_queue,
555 call->out.data,
556 call->out.length,
557 call->src);
558 if (subreq == NULL) {
559 talloc_free(call);
560 return;
562 tevent_req_set_callback(subreq, dns_udp_call_sendto_done, call);
565 static void dns_udp_call_sendto_done(struct tevent_req *subreq)
567 struct dns_udp_call *call = tevent_req_callback_data(subreq,
568 struct dns_udp_call);
569 int sys_errno;
571 tdgram_sendto_queue_recv(subreq, &sys_errno);
573 /* We don't care about errors */
575 talloc_free(call);
579 start listening on the given address
581 static NTSTATUS dns_add_socket(struct dns_server *dns,
582 const struct model_ops *model_ops,
583 const char *name,
584 const char *address,
585 uint16_t port)
587 struct dns_socket *dns_socket;
588 struct dns_udp_socket *dns_udp_socket;
589 struct tevent_req *udpsubreq;
590 NTSTATUS status;
591 int ret;
593 dns_socket = talloc(dns, struct dns_socket);
594 NT_STATUS_HAVE_NO_MEMORY(dns_socket);
596 dns_socket->dns = dns;
598 ret = tsocket_address_inet_from_strings(dns_socket, "ip",
599 address, port,
600 &dns_socket->local_address);
601 if (ret != 0) {
602 status = map_nt_error_from_unix_common(errno);
603 return status;
606 status = stream_setup_socket(dns->task,
607 dns->task->event_ctx,
608 dns->task->lp_ctx,
609 model_ops,
610 &dns_tcp_stream_ops,
611 "ip", address, &port,
612 lpcfg_socket_options(dns->task->lp_ctx),
613 dns_socket);
614 if (!NT_STATUS_IS_OK(status)) {
615 DEBUG(0,("Failed to bind to %s:%u TCP - %s\n",
616 address, port, nt_errstr(status)));
617 talloc_free(dns_socket);
618 return status;
621 dns_udp_socket = talloc(dns_socket, struct dns_udp_socket);
622 NT_STATUS_HAVE_NO_MEMORY(dns_udp_socket);
624 dns_udp_socket->dns_socket = dns_socket;
626 ret = tdgram_inet_udp_socket(dns_socket->local_address,
627 NULL,
628 dns_udp_socket,
629 &dns_udp_socket->dgram);
630 if (ret != 0) {
631 status = map_nt_error_from_unix_common(errno);
632 DEBUG(0,("Failed to bind to %s:%u UDP - %s\n",
633 address, port, nt_errstr(status)));
634 return status;
637 dns_udp_socket->send_queue = tevent_queue_create(dns_udp_socket,
638 "dns_udp_send_queue");
639 NT_STATUS_HAVE_NO_MEMORY(dns_udp_socket->send_queue);
641 udpsubreq = tdgram_recvfrom_send(dns_udp_socket,
642 dns->task->event_ctx,
643 dns_udp_socket->dgram);
644 NT_STATUS_HAVE_NO_MEMORY(udpsubreq);
645 tevent_req_set_callback(udpsubreq, dns_udp_call_loop, dns_udp_socket);
647 return NT_STATUS_OK;
651 setup our listening sockets on the configured network interfaces
653 static NTSTATUS dns_startup_interfaces(struct dns_server *dns, struct loadparm_context *lp_ctx,
654 struct interface *ifaces)
656 const struct model_ops *model_ops;
657 int num_interfaces;
658 TALLOC_CTX *tmp_ctx = talloc_new(dns);
659 NTSTATUS status;
660 int i;
662 /* within the dns task we want to be a single process, so
663 ask for the single process model ops and pass these to the
664 stream_setup_socket() call. */
665 model_ops = process_model_startup("single");
666 if (!model_ops) {
667 DEBUG(0,("Can't find 'single' process model_ops\n"));
668 return NT_STATUS_INTERNAL_ERROR;
671 if (ifaces != NULL) {
672 num_interfaces = iface_list_count(ifaces);
674 for (i=0; i<num_interfaces; i++) {
675 const char *address = talloc_strdup(tmp_ctx,
676 iface_list_n_ip(ifaces, i));
678 status = dns_add_socket(dns, model_ops, "dns", address,
679 DNS_SERVICE_PORT);
680 NT_STATUS_NOT_OK_RETURN(status);
682 } else {
683 const char **wcard;
684 wcard = iface_list_wildcard(tmp_ctx, lp_ctx);
685 if (wcard == NULL) {
686 DEBUG(0, ("No wildcard address available\n"));
687 return NT_STATUS_INTERNAL_ERROR;
689 for (i = 0; wcard[i] != NULL; i++) {
690 status = dns_add_socket(dns, model_ops, "dns", wcard[i],
691 DNS_SERVICE_PORT);
692 NT_STATUS_NOT_OK_RETURN(status);
696 talloc_free(tmp_ctx);
698 return NT_STATUS_OK;
701 static int dns_server_sort_zones(struct ldb_message **m1, struct ldb_message **m2)
703 const char *n1, *n2;
704 size_t l1, l2;
706 n1 = ldb_msg_find_attr_as_string(*m1, "name", NULL);
707 n2 = ldb_msg_find_attr_as_string(*m2, "name", NULL);
709 l1 = strlen(n1);
710 l2 = strlen(n2);
712 /* If the string lengths are not equal just sort by length */
713 if (l1 != l2) {
714 /* If m1 is the larger zone name, return it first */
715 return l2 - l1;
718 /*TODO: We need to compare DNs here, we want the DomainDNSZones first */
719 return 0;
722 static struct dns_server_tkey_store *tkey_store_init(TALLOC_CTX *mem_ctx,
723 uint16_t size)
725 struct dns_server_tkey_store *buffer = talloc_zero(mem_ctx,
726 struct dns_server_tkey_store);
728 if (buffer == NULL) {
729 return NULL;
732 buffer->size = size;
733 buffer->next_idx = 0;
735 buffer->tkeys = talloc_zero_array(buffer, struct dns_server_tkey *, size);
736 if (buffer->tkeys == NULL) {
737 TALLOC_FREE(buffer);
740 return buffer;
743 static void dns_task_init(struct task_server *task)
745 struct dns_server *dns;
746 NTSTATUS status;
747 struct interface *ifaces = NULL;
748 int ret;
749 struct ldb_result *res;
750 static const char * const attrs[] = { "name", NULL};
751 static const char * const attrs_none[] = { NULL};
752 unsigned int i;
753 struct ldb_message *dns_acc;
754 char *hostname_lower;
755 char *dns_spn;
757 switch (lpcfg_server_role(task->lp_ctx)) {
758 case ROLE_STANDALONE:
759 task_server_terminate(task, "dns: no DNS required in standalone configuration", false);
760 return;
761 case ROLE_DOMAIN_MEMBER:
762 task_server_terminate(task, "dns: no DNS required in member server configuration", false);
763 return;
764 case ROLE_ACTIVE_DIRECTORY_DC:
765 /* Yes, we want a DNS */
766 break;
769 if (lpcfg_interfaces(task->lp_ctx) && lpcfg_bind_interfaces_only(task->lp_ctx)) {
770 load_interface_list(task, task->lp_ctx, &ifaces);
772 if (iface_list_count(ifaces) == 0) {
773 task_server_terminate(task, "dns: no network interfaces configured", false);
774 return;
778 task_server_set_title(task, "task[dns]");
780 dns = talloc_zero(task, struct dns_server);
781 if (dns == NULL) {
782 task_server_terminate(task, "dns: out of memory", true);
783 return;
786 dns->task = task;
788 dns->server_credentials = cli_credentials_init(dns);
789 if (!dns->server_credentials) {
790 task_server_terminate(task, "Failed to init server credentials\n", true);
791 return;
794 dns->samdb = samdb_connect(dns, dns->task->event_ctx, dns->task->lp_ctx,
795 system_session(dns->task->lp_ctx), 0);
796 if (!dns->samdb) {
797 task_server_terminate(task, "dns: samdb_connect failed", true);
798 return;
801 cli_credentials_set_conf(dns->server_credentials, task->lp_ctx);
803 hostname_lower = strlower_talloc(dns, lpcfg_netbios_name(task->lp_ctx));
804 dns_spn = talloc_asprintf(dns, "DNS/%s.%s",
805 hostname_lower,
806 lpcfg_dnsdomain(task->lp_ctx));
807 TALLOC_FREE(hostname_lower);
809 ret = dsdb_search_one(dns->samdb, dns, &dns_acc,
810 ldb_get_default_basedn(dns->samdb), LDB_SCOPE_SUBTREE,
811 attrs_none, 0, "(servicePrincipalName=%s)",
812 dns_spn);
813 if (ret == LDB_SUCCESS) {
814 TALLOC_FREE(dns_acc);
815 if (!dns_spn) {
816 task_server_terminate(task, "dns: talloc_asprintf failed", true);
817 return;
819 status = cli_credentials_set_stored_principal(dns->server_credentials, task->lp_ctx, dns_spn);
820 if (!NT_STATUS_IS_OK(status)) {
821 task_server_terminate(task,
822 talloc_asprintf(task, "Failed to obtain server credentials for DNS, "
823 "despite finding it in the samdb! %s\n",
824 nt_errstr(status)),
825 true);
826 return;
828 } else {
829 TALLOC_FREE(dns_spn);
830 status = cli_credentials_set_machine_account(dns->server_credentials, task->lp_ctx);
831 if (!NT_STATUS_IS_OK(status)) {
832 task_server_terminate(task,
833 talloc_asprintf(task, "Failed to obtain server credentials, perhaps a standalone server?: %s\n",
834 nt_errstr(status)),
835 true);
836 return;
840 dns->tkeys = tkey_store_init(dns, TKEY_BUFFER_SIZE);
841 if (!dns->tkeys) {
842 task_server_terminate(task, "Failed to allocate tkey storage\n", true);
843 return;
846 // TODO: this search does not work against windows
847 ret = dsdb_search(dns->samdb, dns, &res, NULL, LDB_SCOPE_SUBTREE,
848 attrs, DSDB_SEARCH_SEARCH_ALL_PARTITIONS, "(objectClass=dnsZone)");
849 if (ret != LDB_SUCCESS) {
850 task_server_terminate(task,
851 "dns: failed to look up root DNS zones",
852 true);
853 return;
856 TYPESAFE_QSORT(res->msgs, res->count, dns_server_sort_zones);
858 for (i=0; i < res->count; i++) {
859 struct dns_server_zone *z;
861 z = talloc_zero(dns, struct dns_server_zone);
862 if (z == NULL) {
863 task_server_terminate(task, "dns failed to allocate memory", true);
866 z->name = ldb_msg_find_attr_as_string(res->msgs[i], "name", NULL);
867 z->dn = talloc_move(z, &res->msgs[i]->dn);
869 * Ignore the RootDNSServers zone and zones that we don't support yet
870 * RootDNSServers should never be returned (Windows DNS server don't)
871 * ..TrustAnchors should never be returned as is, (Windows returns
872 * TrustAnchors) and for the moment we don't support DNSSEC so we'd better
873 * not return this zone.
875 if ((strcmp(z->name, "RootDNSServers") == 0) ||
876 (strcmp(z->name, "..TrustAnchors") == 0))
878 DEBUG(10, ("Ignoring zone %s\n", z->name));
879 talloc_free(z);
880 continue;
882 DLIST_ADD_END(dns->zones, z, NULL);
885 status = dns_startup_interfaces(dns, task->lp_ctx, ifaces);
886 if (!NT_STATUS_IS_OK(status)) {
887 task_server_terminate(task, "dns failed to setup interfaces", true);
888 return;
892 NTSTATUS server_service_dns_init(void)
894 return register_server_service("dns", dns_task_init);