2 Unix SMB/CIFS implementation.
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/>.
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"
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"
48 #include "librpc/gen_ndr/ndr_irpc.h"
49 #include "lib/messaging/irpc.h"
50 #include "libds/common/roles.h"
53 #define DBGC_CLASS DBGC_DNS
55 NTSTATUS
server_service_dns_init(void);
57 /* hold information about one dns socket */
59 struct dns_server
*dns
;
60 struct tsocket_address
*local_address
;
63 struct dns_udp_socket
{
64 struct dns_socket
*dns_socket
;
65 struct tdgram_context
*dgram
;
66 struct tevent_queue
*send_queue
;
70 state of an open tcp connection
72 struct dns_tcp_connection
{
73 /* stream connection we belong to */
74 struct stream_connection
*conn
;
76 /* the dns_server the connection belongs to */
77 struct dns_socket
*dns_socket
;
79 struct tstream_context
*tstream
;
81 struct tevent_queue
*send_queue
;
84 static void dns_tcp_terminate_connection(struct dns_tcp_connection
*dnsconn
, const char *reason
)
86 stream_terminate_connection(dnsconn
->conn
, reason
);
89 static void dns_tcp_recv(struct stream_connection
*conn
, uint16_t flags
)
91 struct dns_tcp_connection
*dnsconn
= talloc_get_type(conn
->private_data
,
92 struct dns_tcp_connection
);
93 /* this should never be triggered! */
94 dns_tcp_terminate_connection(dnsconn
, "dns_tcp_recv: called");
97 static void dns_tcp_send(struct stream_connection
*conn
, uint16_t flags
)
99 struct dns_tcp_connection
*dnsconn
= talloc_get_type(conn
->private_data
,
100 struct dns_tcp_connection
);
101 /* this should never be triggered! */
102 dns_tcp_terminate_connection(dnsconn
, "dns_tcp_send: called");
105 struct dns_process_state
{
107 struct dns_server
*dns
;
108 struct dns_name_packet in_packet
;
109 struct dns_request_state state
;
111 struct dns_name_packet out_packet
;
115 static void dns_process_done(struct tevent_req
*subreq
);
117 static struct tevent_req
*dns_process_send(TALLOC_CTX
*mem_ctx
,
118 struct tevent_context
*ev
,
119 struct dns_server
*dns
,
122 struct tevent_req
*req
, *subreq
;
123 struct dns_process_state
*state
;
124 enum ndr_err_code ndr_err
;
126 const char **forwarder
= lpcfg_dns_forwarder(dns
->task
->lp_ctx
);
127 req
= tevent_req_create(mem_ctx
, &state
, struct dns_process_state
);
131 state
->state
.mem_ctx
= state
;
136 if (in
->length
< 12) {
137 tevent_req_werror(req
, WERR_INVALID_PARAMETER
);
138 return tevent_req_post(req
, ev
);
140 dump_data_dbgc(DBGC_DNS
, 8, in
->data
, in
->length
);
142 ndr_err
= ndr_pull_struct_blob(
143 in
, state
, &state
->in_packet
,
144 (ndr_pull_flags_fn_t
)ndr_pull_dns_name_packet
);
146 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
147 state
->dns_err
= DNS_RCODE_FORMERR
;
148 tevent_req_done(req
);
149 return tevent_req_post(req
, ev
);
151 if (DEBUGLVLC(DBGC_DNS
, 8)) {
152 NDR_PRINT_DEBUGC(DBGC_DNS
, dns_name_packet
, &state
->in_packet
);
155 if (state
->in_packet
.operation
& DNS_FLAG_REPLY
) {
156 DEBUG(1, ("Won't reply to replies.\n"));
157 tevent_req_werror(req
, WERR_INVALID_PARAMETER
);
158 return tevent_req_post(req
, ev
);
161 state
->state
.flags
= state
->in_packet
.operation
;
162 state
->state
.flags
|= DNS_FLAG_REPLY
;
165 if (forwarder
&& *forwarder
&& **forwarder
) {
166 state
->state
.flags
|= DNS_FLAG_RECURSION_AVAIL
;
169 state
->out_packet
= state
->in_packet
;
171 ret
= dns_verify_tsig(dns
, state
, &state
->state
, &state
->out_packet
, in
);
172 if (!W_ERROR_IS_OK(ret
)) {
173 state
->dns_err
= werr_to_dns_err(ret
);
174 tevent_req_done(req
);
175 return tevent_req_post(req
, ev
);
178 switch (state
->in_packet
.operation
& DNS_OPCODE
) {
179 case DNS_OPCODE_QUERY
:
180 subreq
= dns_server_process_query_send(
181 state
, ev
, dns
, &state
->state
, &state
->in_packet
);
182 if (tevent_req_nomem(subreq
, req
)) {
183 return tevent_req_post(req
, ev
);
185 tevent_req_set_callback(subreq
, dns_process_done
, req
);
187 case DNS_OPCODE_UPDATE
:
188 ret
= dns_server_process_update(
189 dns
, &state
->state
, state
, &state
->in_packet
,
190 &state
->out_packet
.answers
, &state
->out_packet
.ancount
,
191 &state
->out_packet
.nsrecs
, &state
->out_packet
.nscount
,
192 &state
->out_packet
.additional
,
193 &state
->out_packet
.arcount
);
196 ret
= WERR_DNS_ERROR_RCODE_NOT_IMPLEMENTED
;
198 if (!W_ERROR_IS_OK(ret
)) {
199 state
->dns_err
= werr_to_dns_err(ret
);
201 tevent_req_done(req
);
202 return tevent_req_post(req
, ev
);
205 static void dns_process_done(struct tevent_req
*subreq
)
207 struct tevent_req
*req
= tevent_req_callback_data(
208 subreq
, struct tevent_req
);
209 struct dns_process_state
*state
= tevent_req_data(
210 req
, struct dns_process_state
);
213 ret
= dns_server_process_query_recv(
215 &state
->out_packet
.answers
, &state
->out_packet
.ancount
,
216 &state
->out_packet
.nsrecs
, &state
->out_packet
.nscount
,
217 &state
->out_packet
.additional
, &state
->out_packet
.arcount
);
220 if (!W_ERROR_IS_OK(ret
)) {
221 state
->dns_err
= werr_to_dns_err(ret
);
223 tevent_req_done(req
);
226 static WERROR
dns_process_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
229 struct dns_process_state
*state
= tevent_req_data(
230 req
, struct dns_process_state
);
231 enum ndr_err_code ndr_err
;
234 if (tevent_req_is_werror(req
, &ret
)) {
237 if ((state
->dns_err
!= DNS_RCODE_OK
) &&
238 (state
->dns_err
!= DNS_RCODE_NXDOMAIN
) &&
239 (state
->dns_err
!= DNS_RCODE_NOTAUTH
))
243 if (state
->dns_err
!= DNS_RCODE_OK
) {
244 state
->out_packet
.operation
|= state
->dns_err
;
246 state
->out_packet
.operation
|= state
->state
.flags
;
248 if (state
->state
.sign
) {
249 ret
= dns_sign_tsig(state
->dns
, mem_ctx
, &state
->state
,
250 &state
->out_packet
, 0);
251 if (!W_ERROR_IS_OK(ret
)) {
252 state
->dns_err
= DNS_RCODE_SERVFAIL
;
257 if (DEBUGLVLC(DBGC_DNS
, 8)) {
258 NDR_PRINT_DEBUGC(DBGC_DNS
, dns_name_packet
, &state
->out_packet
);
261 ndr_err
= ndr_push_struct_blob(
262 out
, mem_ctx
, &state
->out_packet
,
263 (ndr_push_flags_fn_t
)ndr_push_dns_name_packet
);
264 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
265 DEBUG(1, ("Failed to push packet: %s!\n",
266 ndr_errstr(ndr_err
)));
267 state
->dns_err
= DNS_RCODE_SERVFAIL
;
273 *out
= data_blob_talloc(mem_ctx
, state
->in
->data
, state
->in
->length
);
274 if (out
->data
== NULL
) {
275 return WERR_NOT_ENOUGH_MEMORY
;
277 out
->data
[2] |= 0x80; /* Toggle DNS_FLAG_REPLY */
278 out
->data
[3] |= state
->dns_err
;
282 struct dns_tcp_call
{
283 struct dns_tcp_connection
*dns_conn
;
287 struct iovec out_iov
[2];
290 static void dns_tcp_call_process_done(struct tevent_req
*subreq
);
291 static void dns_tcp_call_writev_done(struct tevent_req
*subreq
);
293 static void dns_tcp_call_loop(struct tevent_req
*subreq
)
295 struct dns_tcp_connection
*dns_conn
= tevent_req_callback_data(subreq
,
296 struct dns_tcp_connection
);
297 struct dns_server
*dns
= dns_conn
->dns_socket
->dns
;
298 struct dns_tcp_call
*call
;
301 call
= talloc(dns_conn
, struct dns_tcp_call
);
303 dns_tcp_terminate_connection(dns_conn
, "dns_tcp_call_loop: "
304 "no memory for dns_tcp_call");
307 call
->dns_conn
= dns_conn
;
309 status
= tstream_read_pdu_blob_recv(subreq
,
313 if (!NT_STATUS_IS_OK(status
)) {
316 reason
= talloc_asprintf(call
, "dns_tcp_call_loop: "
317 "tstream_read_pdu_blob_recv() - %s",
320 reason
= nt_errstr(status
);
323 dns_tcp_terminate_connection(dns_conn
, reason
);
327 DEBUG(10,("Received DNS TCP packet of length %lu from %s\n",
328 (long) call
->in
.length
,
329 tsocket_address_string(dns_conn
->conn
->remote_address
, call
)));
331 /* skip length header */
333 call
->in
.length
-= 2;
335 subreq
= dns_process_send(call
, dns
->task
->event_ctx
, dns
,
337 if (subreq
== NULL
) {
338 dns_tcp_terminate_connection(
339 dns_conn
, "dns_tcp_call_loop: dns_process_send "
343 tevent_req_set_callback(subreq
, dns_tcp_call_process_done
, call
);
346 * The dns tcp pdu's has the length as 2 byte (initial_read_size),
347 * packet_full_request_u16 provides the pdu length then.
349 subreq
= tstream_read_pdu_blob_send(dns_conn
,
350 dns_conn
->conn
->event
.ctx
,
352 2, /* initial_read_size */
353 packet_full_request_u16
,
355 if (subreq
== NULL
) {
356 dns_tcp_terminate_connection(dns_conn
, "dns_tcp_call_loop: "
357 "no memory for tstream_read_pdu_blob_send");
360 tevent_req_set_callback(subreq
, dns_tcp_call_loop
, dns_conn
);
363 static void dns_tcp_call_process_done(struct tevent_req
*subreq
)
365 struct dns_tcp_call
*call
= tevent_req_callback_data(subreq
,
366 struct dns_tcp_call
);
367 struct dns_tcp_connection
*dns_conn
= call
->dns_conn
;
370 err
= dns_process_recv(subreq
, call
, &call
->out
);
372 if (!W_ERROR_IS_OK(err
)) {
373 DEBUG(1, ("dns_process returned %s\n", win_errstr(err
)));
374 dns_tcp_terminate_connection(dns_conn
,
375 "dns_tcp_call_loop: process function failed");
379 /* First add the length of the out buffer */
380 RSSVAL(call
->out_hdr
, 0, call
->out
.length
);
381 call
->out_iov
[0].iov_base
= (char *) call
->out_hdr
;
382 call
->out_iov
[0].iov_len
= 2;
384 call
->out_iov
[1].iov_base
= (char *) call
->out
.data
;
385 call
->out_iov
[1].iov_len
= call
->out
.length
;
387 subreq
= tstream_writev_queue_send(call
,
388 dns_conn
->conn
->event
.ctx
,
390 dns_conn
->send_queue
,
392 if (subreq
== NULL
) {
393 dns_tcp_terminate_connection(dns_conn
, "dns_tcp_call_loop: "
394 "no memory for tstream_writev_queue_send");
397 tevent_req_set_callback(subreq
, dns_tcp_call_writev_done
, call
);
400 static void dns_tcp_call_writev_done(struct tevent_req
*subreq
)
402 struct dns_tcp_call
*call
= tevent_req_callback_data(subreq
,
403 struct dns_tcp_call
);
407 rc
= tstream_writev_queue_recv(subreq
, &sys_errno
);
412 reason
= talloc_asprintf(call
, "dns_tcp_call_writev_done: "
413 "tstream_writev_queue_recv() - %d:%s",
414 sys_errno
, strerror(sys_errno
));
416 reason
= "dns_tcp_call_writev_done: tstream_writev_queue_recv() failed";
419 dns_tcp_terminate_connection(call
->dns_conn
, reason
);
423 /* We don't care about errors */
429 called when we get a new connection
431 static void dns_tcp_accept(struct stream_connection
*conn
)
433 struct dns_socket
*dns_socket
;
434 struct dns_tcp_connection
*dns_conn
;
435 struct tevent_req
*subreq
;
438 dns_conn
= talloc_zero(conn
, struct dns_tcp_connection
);
439 if (dns_conn
== NULL
) {
440 stream_terminate_connection(conn
,
441 "dns_tcp_accept: out of memory");
445 dns_conn
->send_queue
= tevent_queue_create(conn
, "dns_tcp_accept");
446 if (dns_conn
->send_queue
== NULL
) {
447 stream_terminate_connection(conn
,
448 "dns_tcp_accept: out of memory");
452 dns_socket
= talloc_get_type(conn
->private_data
, struct dns_socket
);
454 TALLOC_FREE(conn
->event
.fde
);
456 rc
= tstream_bsd_existing_socket(dns_conn
,
457 socket_get_fd(conn
->socket
),
460 stream_terminate_connection(conn
,
461 "dns_tcp_accept: out of memory");
465 dns_conn
->conn
= conn
;
466 dns_conn
->dns_socket
= dns_socket
;
467 conn
->private_data
= dns_conn
;
470 * The dns tcp pdu's has the length as 2 byte (initial_read_size),
471 * packet_full_request_u16 provides the pdu length then.
473 subreq
= tstream_read_pdu_blob_send(dns_conn
,
474 dns_conn
->conn
->event
.ctx
,
476 2, /* initial_read_size */
477 packet_full_request_u16
,
479 if (subreq
== NULL
) {
480 dns_tcp_terminate_connection(dns_conn
, "dns_tcp_accept: "
481 "no memory for tstream_read_pdu_blob_send");
484 tevent_req_set_callback(subreq
, dns_tcp_call_loop
, dns_conn
);
487 static const struct stream_server_ops dns_tcp_stream_ops
= {
489 .accept_connection
= dns_tcp_accept
,
490 .recv_handler
= dns_tcp_recv
,
491 .send_handler
= dns_tcp_send
494 struct dns_udp_call
{
495 struct dns_udp_socket
*sock
;
496 struct tsocket_address
*src
;
501 static void dns_udp_call_process_done(struct tevent_req
*subreq
);
502 static void dns_udp_call_sendto_done(struct tevent_req
*subreq
);
504 static void dns_udp_call_loop(struct tevent_req
*subreq
)
506 struct dns_udp_socket
*sock
= tevent_req_callback_data(subreq
,
507 struct dns_udp_socket
);
508 struct dns_server
*dns
= sock
->dns_socket
->dns
;
509 struct dns_udp_call
*call
;
514 call
= talloc(sock
, struct dns_udp_call
);
521 len
= tdgram_recvfrom_recv(subreq
, &sys_errno
,
522 call
, &buf
, &call
->src
);
530 call
->in
.length
= len
;
532 DEBUG(10,("Received DNS UDP packet of length %lu from %s\n",
533 (long)call
->in
.length
,
534 tsocket_address_string(call
->src
, call
)));
536 subreq
= dns_process_send(call
, dns
->task
->event_ctx
, dns
,
538 if (subreq
== NULL
) {
542 tevent_req_set_callback(subreq
, dns_udp_call_process_done
, call
);
545 subreq
= tdgram_recvfrom_send(sock
,
546 sock
->dns_socket
->dns
->task
->event_ctx
,
548 if (subreq
== NULL
) {
549 task_server_terminate(sock
->dns_socket
->dns
->task
,
550 "no memory for tdgram_recvfrom_send",
554 tevent_req_set_callback(subreq
, dns_udp_call_loop
, sock
);
557 static void dns_udp_call_process_done(struct tevent_req
*subreq
)
559 struct dns_udp_call
*call
= tevent_req_callback_data(
560 subreq
, struct dns_udp_call
);
561 struct dns_udp_socket
*sock
= call
->sock
;
562 struct dns_server
*dns
= sock
->dns_socket
->dns
;
565 err
= dns_process_recv(subreq
, call
, &call
->out
);
567 if (!W_ERROR_IS_OK(err
)) {
568 DEBUG(1, ("dns_process returned %s\n", win_errstr(err
)));
573 subreq
= tdgram_sendto_queue_send(call
,
574 dns
->task
->event_ctx
,
580 if (subreq
== NULL
) {
584 tevent_req_set_callback(subreq
, dns_udp_call_sendto_done
, call
);
587 static void dns_udp_call_sendto_done(struct tevent_req
*subreq
)
589 struct dns_udp_call
*call
= tevent_req_callback_data(subreq
,
590 struct dns_udp_call
);
593 tdgram_sendto_queue_recv(subreq
, &sys_errno
);
595 /* We don't care about errors */
601 start listening on the given address
603 static NTSTATUS
dns_add_socket(struct dns_server
*dns
,
604 const struct model_ops
*model_ops
,
609 struct dns_socket
*dns_socket
;
610 struct dns_udp_socket
*dns_udp_socket
;
611 struct tevent_req
*udpsubreq
;
615 dns_socket
= talloc(dns
, struct dns_socket
);
616 NT_STATUS_HAVE_NO_MEMORY(dns_socket
);
618 dns_socket
->dns
= dns
;
620 ret
= tsocket_address_inet_from_strings(dns_socket
, "ip",
622 &dns_socket
->local_address
);
624 status
= map_nt_error_from_unix_common(errno
);
628 status
= stream_setup_socket(dns
->task
,
629 dns
->task
->event_ctx
,
633 "ip", address
, &port
,
634 lpcfg_socket_options(dns
->task
->lp_ctx
),
636 if (!NT_STATUS_IS_OK(status
)) {
637 DEBUG(0,("Failed to bind to %s:%u TCP - %s\n",
638 address
, port
, nt_errstr(status
)));
639 talloc_free(dns_socket
);
643 dns_udp_socket
= talloc(dns_socket
, struct dns_udp_socket
);
644 NT_STATUS_HAVE_NO_MEMORY(dns_udp_socket
);
646 dns_udp_socket
->dns_socket
= dns_socket
;
648 ret
= tdgram_inet_udp_socket(dns_socket
->local_address
,
651 &dns_udp_socket
->dgram
);
653 status
= map_nt_error_from_unix_common(errno
);
654 DEBUG(0,("Failed to bind to %s:%u UDP - %s\n",
655 address
, port
, nt_errstr(status
)));
659 dns_udp_socket
->send_queue
= tevent_queue_create(dns_udp_socket
,
660 "dns_udp_send_queue");
661 NT_STATUS_HAVE_NO_MEMORY(dns_udp_socket
->send_queue
);
663 udpsubreq
= tdgram_recvfrom_send(dns_udp_socket
,
664 dns
->task
->event_ctx
,
665 dns_udp_socket
->dgram
);
666 NT_STATUS_HAVE_NO_MEMORY(udpsubreq
);
667 tevent_req_set_callback(udpsubreq
, dns_udp_call_loop
, dns_udp_socket
);
673 setup our listening sockets on the configured network interfaces
675 static NTSTATUS
dns_startup_interfaces(struct dns_server
*dns
,
676 struct interface
*ifaces
)
678 const struct model_ops
*model_ops
;
680 TALLOC_CTX
*tmp_ctx
= talloc_new(dns
);
684 /* within the dns task we want to be a single process, so
685 ask for the single process model ops and pass these to the
686 stream_setup_socket() call. */
687 model_ops
= process_model_startup("single");
689 DEBUG(0,("Can't find 'single' process model_ops\n"));
690 return NT_STATUS_INTERNAL_ERROR
;
693 if (ifaces
!= NULL
) {
694 num_interfaces
= iface_list_count(ifaces
);
696 for (i
=0; i
<num_interfaces
; i
++) {
697 const char *address
= talloc_strdup(tmp_ctx
,
698 iface_list_n_ip(ifaces
, i
));
700 status
= dns_add_socket(dns
, model_ops
, "dns", address
,
702 NT_STATUS_NOT_OK_RETURN(status
);
707 wcard
= iface_list_wildcard(tmp_ctx
);
709 DEBUG(0, ("No wildcard address available\n"));
710 return NT_STATUS_INTERNAL_ERROR
;
712 for (i
= 0; wcard
[i
] != NULL
; i
++) {
713 status
= dns_add_socket(dns
, model_ops
, "dns", wcard
[i
],
715 if (NT_STATUS_IS_OK(status
)) {
719 if (num_binds
== 0) {
720 talloc_free(tmp_ctx
);
721 return NT_STATUS_INVALID_PARAMETER_MIX
;
725 talloc_free(tmp_ctx
);
730 static struct dns_server_tkey_store
*tkey_store_init(TALLOC_CTX
*mem_ctx
,
733 struct dns_server_tkey_store
*buffer
= talloc_zero(mem_ctx
,
734 struct dns_server_tkey_store
);
736 if (buffer
== NULL
) {
741 buffer
->next_idx
= 0;
743 buffer
->tkeys
= talloc_zero_array(buffer
, struct dns_server_tkey
*, size
);
744 if (buffer
->tkeys
== NULL
) {
751 static NTSTATUS
dns_server_reload_zones(struct dns_server
*dns
)
754 struct dns_server_zone
*new_list
= NULL
;
755 struct dns_server_zone
*old_list
= NULL
;
756 struct dns_server_zone
*old_zone
;
757 status
= dns_common_zones(dns
->samdb
, dns
, &new_list
);
758 if (!NT_STATUS_IS_OK(status
)) {
761 dns
->zones
= new_list
;
762 while ((old_zone
= DLIST_TAIL(old_list
)) != NULL
) {
763 DLIST_REMOVE(old_list
, old_zone
);
764 talloc_free(old_zone
);
771 * Called when the internal DNS server should reload the zones from DB, for
772 * example, when zones are added or deleted through RPC or replicated by
775 static NTSTATUS
dns_reload_zones(struct irpc_message
*msg
,
776 struct dnssrv_reload_dns_zones
*r
)
778 struct dns_server
*dns
;
780 dns
= talloc_get_type(msg
->private_data
, struct dns_server
);
782 r
->out
.result
= NT_STATUS_INTERNAL_ERROR
;
783 return NT_STATUS_INTERNAL_ERROR
;
786 r
->out
.result
= dns_server_reload_zones(dns
);
791 static void dns_task_init(struct task_server
*task
)
793 struct dns_server
*dns
;
795 struct interface
*ifaces
= NULL
;
797 static const char * const attrs_none
[] = { NULL
};
798 struct ldb_message
*dns_acc
;
799 char *hostname_lower
;
802 switch (lpcfg_server_role(task
->lp_ctx
)) {
803 case ROLE_STANDALONE
:
804 task_server_terminate(task
, "dns: no DNS required in standalone configuration", false);
806 case ROLE_DOMAIN_MEMBER
:
807 task_server_terminate(task
, "dns: no DNS required in member server configuration", false);
809 case ROLE_ACTIVE_DIRECTORY_DC
:
810 /* Yes, we want a DNS */
814 if (lpcfg_interfaces(task
->lp_ctx
) && lpcfg_bind_interfaces_only(task
->lp_ctx
)) {
815 load_interface_list(task
, task
->lp_ctx
, &ifaces
);
817 if (iface_list_count(ifaces
) == 0) {
818 task_server_terminate(task
, "dns: no network interfaces configured", false);
823 task_server_set_title(task
, "task[dns]");
825 dns
= talloc_zero(task
, struct dns_server
);
827 task_server_terminate(task
, "dns: out of memory", true);
832 /*FIXME: Make this a configurable option */
833 dns
->max_payload
= 4096;
835 dns
->server_credentials
= cli_credentials_init(dns
);
836 if (!dns
->server_credentials
) {
837 task_server_terminate(task
, "Failed to init server credentials\n", true);
841 dns
->samdb
= samdb_connect(dns
, dns
->task
->event_ctx
, dns
->task
->lp_ctx
,
842 system_session(dns
->task
->lp_ctx
), 0);
844 task_server_terminate(task
, "dns: samdb_connect failed", true);
848 cli_credentials_set_conf(dns
->server_credentials
, task
->lp_ctx
);
850 hostname_lower
= strlower_talloc(dns
, lpcfg_netbios_name(task
->lp_ctx
));
851 dns_spn
= talloc_asprintf(dns
, "DNS/%s.%s",
853 lpcfg_dnsdomain(task
->lp_ctx
));
854 TALLOC_FREE(hostname_lower
);
856 ret
= dsdb_search_one(dns
->samdb
, dns
, &dns_acc
,
857 ldb_get_default_basedn(dns
->samdb
), LDB_SCOPE_SUBTREE
,
858 attrs_none
, 0, "(servicePrincipalName=%s)",
860 if (ret
== LDB_SUCCESS
) {
861 TALLOC_FREE(dns_acc
);
863 task_server_terminate(task
, "dns: talloc_asprintf failed", true);
866 status
= cli_credentials_set_stored_principal(dns
->server_credentials
, task
->lp_ctx
, dns_spn
);
867 if (!NT_STATUS_IS_OK(status
)) {
868 task_server_terminate(task
,
869 talloc_asprintf(task
, "Failed to obtain server credentials for DNS, "
870 "despite finding it in the samdb! %s\n",
876 TALLOC_FREE(dns_spn
);
877 status
= cli_credentials_set_machine_account(dns
->server_credentials
, task
->lp_ctx
);
878 if (!NT_STATUS_IS_OK(status
)) {
879 task_server_terminate(task
,
880 talloc_asprintf(task
, "Failed to obtain server credentials, perhaps a standalone server?: %s\n",
887 dns
->tkeys
= tkey_store_init(dns
, TKEY_BUFFER_SIZE
);
889 task_server_terminate(task
, "Failed to allocate tkey storage\n", true);
893 status
= dns_server_reload_zones(dns
);
894 if (!NT_STATUS_IS_OK(status
)) {
895 task_server_terminate(task
, "dns: failed to load DNS zones", true);
899 status
= dns_startup_interfaces(dns
, ifaces
);
900 if (!NT_STATUS_IS_OK(status
)) {
901 task_server_terminate(task
, "dns failed to setup interfaces", true);
905 /* Setup the IRPC interface and register handlers */
906 status
= irpc_add_name(task
->msg_ctx
, "dnssrv");
907 if (!NT_STATUS_IS_OK(status
)) {
908 task_server_terminate(task
, "dns: failed to register IRPC name", true);
912 status
= IRPC_REGISTER(task
->msg_ctx
, irpc
, DNSSRV_RELOAD_DNS_ZONES
,
913 dns_reload_zones
, dns
);
914 if (!NT_STATUS_IS_OK(status
)) {
915 task_server_terminate(task
, "dns: failed to setup reload handler", true);
920 NTSTATUS
server_service_dns_init(void)
922 return register_server_service("dns", dns_task_init
);