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"
47 NTSTATUS
server_service_dns_init(void);
49 /* hold information about one dns socket */
51 struct dns_server
*dns
;
52 struct tsocket_address
*local_address
;
55 struct dns_udp_socket
{
56 struct dns_socket
*dns_socket
;
57 struct tdgram_context
*dgram
;
58 struct tevent_queue
*send_queue
;
62 state of an open tcp connection
64 struct dns_tcp_connection
{
65 /* stream connection we belong to */
66 struct stream_connection
*conn
;
68 /* the dns_server the connection belongs to */
69 struct dns_socket
*dns_socket
;
71 struct tstream_context
*tstream
;
73 struct tevent_queue
*send_queue
;
76 static void dns_tcp_terminate_connection(struct dns_tcp_connection
*dnsconn
, const char *reason
)
78 stream_terminate_connection(dnsconn
->conn
, reason
);
81 static void dns_tcp_recv(struct stream_connection
*conn
, uint16_t flags
)
83 struct dns_tcp_connection
*dnsconn
= talloc_get_type(conn
->private_data
,
84 struct dns_tcp_connection
);
85 /* this should never be triggered! */
86 dns_tcp_terminate_connection(dnsconn
, "dns_tcp_recv: called");
89 static void dns_tcp_send(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_send: called");
97 struct dns_process_state
{
99 struct dns_name_packet in_packet
;
100 struct dns_request_state state
;
102 struct dns_name_packet out_packet
;
106 static void dns_process_done(struct tevent_req
*subreq
);
108 static struct tevent_req
*dns_process_send(TALLOC_CTX
*mem_ctx
,
109 struct tevent_context
*ev
,
110 struct dns_server
*dns
,
113 struct tevent_req
*req
, *subreq
;
114 struct dns_process_state
*state
;
115 enum ndr_err_code ndr_err
;
118 req
= tevent_req_create(mem_ctx
, &state
, struct dns_process_state
);
124 if (in
->length
< 12) {
125 tevent_req_werror(req
, WERR_INVALID_PARAM
);
126 return tevent_req_post(req
, ev
);
128 dump_data(8, in
->data
, in
->length
);
130 ndr_err
= ndr_pull_struct_blob(
131 in
, state
, &state
->in_packet
,
132 (ndr_pull_flags_fn_t
)ndr_pull_dns_name_packet
);
134 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
135 state
->dns_err
= DNS_RCODE_FORMERR
;
136 tevent_req_done(req
);
137 return tevent_req_post(req
, ev
);
140 NDR_PRINT_DEBUG(dns_name_packet
, &state
->in_packet
);
143 state
->state
.flags
= state
->in_packet
.operation
;
144 state
->state
.flags
|= DNS_FLAG_REPLY
;
146 if (lpcfg_dns_recursive_queries(dns
->task
->lp_ctx
)) {
147 state
->state
.flags
|= DNS_FLAG_RECURSION_AVAIL
;
150 state
->out_packet
= state
->in_packet
;
152 switch (state
->in_packet
.operation
& DNS_OPCODE
) {
153 case DNS_OPCODE_QUERY
:
154 subreq
= dns_server_process_query_send(
155 state
, ev
, dns
, &state
->state
, &state
->in_packet
);
156 if (tevent_req_nomem(subreq
, req
)) {
157 return tevent_req_post(req
, ev
);
159 tevent_req_set_callback(subreq
, dns_process_done
, req
);
161 case DNS_OPCODE_UPDATE
:
162 ret
= dns_server_process_update(
163 dns
, &state
->state
, state
, &state
->in_packet
,
164 &state
->out_packet
.answers
, &state
->out_packet
.ancount
,
165 &state
->out_packet
.nsrecs
, &state
->out_packet
.nscount
,
166 &state
->out_packet
.additional
,
167 &state
->out_packet
.arcount
);
170 ret
= WERR_DNS_ERROR_RCODE_NOT_IMPLEMENTED
;
172 if (!W_ERROR_IS_OK(ret
)) {
173 state
->dns_err
= werr_to_dns_err(ret
);
175 tevent_req_done(req
);
176 return tevent_req_post(req
, ev
);
179 static void dns_process_done(struct tevent_req
*subreq
)
181 struct tevent_req
*req
= tevent_req_callback_data(
182 subreq
, struct tevent_req
);
183 struct dns_process_state
*state
= tevent_req_data(
184 req
, struct dns_process_state
);
187 ret
= dns_server_process_query_recv(
189 &state
->out_packet
.answers
, &state
->out_packet
.ancount
,
190 &state
->out_packet
.nsrecs
, &state
->out_packet
.nscount
,
191 &state
->out_packet
.additional
, &state
->out_packet
.arcount
);
194 if (!W_ERROR_IS_OK(ret
)) {
195 state
->dns_err
= werr_to_dns_err(ret
);
197 tevent_req_done(req
);
200 static WERROR
dns_process_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
203 struct dns_process_state
*state
= tevent_req_data(
204 req
, struct dns_process_state
);
205 enum ndr_err_code ndr_err
;
208 if (tevent_req_is_werror(req
, &ret
)) {
211 if (state
->dns_err
!= DNS_RCODE_OK
) {
214 state
->out_packet
.operation
|= state
->state
.flags
;
216 ndr_err
= ndr_push_struct_blob(
217 out
, mem_ctx
, &state
->out_packet
,
218 (ndr_push_flags_fn_t
)ndr_push_dns_name_packet
);
219 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
220 DEBUG(1, ("Failed to push packet: %s!\n",
221 ndr_errstr(ndr_err
)));
222 state
->dns_err
= DNS_RCODE_SERVFAIL
;
228 *out
= data_blob_talloc(mem_ctx
, state
->in
->data
, state
->in
->length
);
229 if (out
->data
== NULL
) {
232 out
->data
[2] |= 0x80; /* Toggle DNS_FLAG_REPLY */
233 out
->data
[3] |= state
->dns_err
;
237 struct dns_tcp_call
{
238 struct dns_tcp_connection
*dns_conn
;
242 struct iovec out_iov
[2];
245 static void dns_tcp_call_process_done(struct tevent_req
*subreq
);
246 static void dns_tcp_call_writev_done(struct tevent_req
*subreq
);
248 static void dns_tcp_call_loop(struct tevent_req
*subreq
)
250 struct dns_tcp_connection
*dns_conn
= tevent_req_callback_data(subreq
,
251 struct dns_tcp_connection
);
252 struct dns_server
*dns
= dns_conn
->dns_socket
->dns
;
253 struct dns_tcp_call
*call
;
256 call
= talloc(dns_conn
, struct dns_tcp_call
);
258 dns_tcp_terminate_connection(dns_conn
, "dns_tcp_call_loop: "
259 "no memory for dns_tcp_call");
262 call
->dns_conn
= dns_conn
;
264 status
= tstream_read_pdu_blob_recv(subreq
,
268 if (!NT_STATUS_IS_OK(status
)) {
271 reason
= talloc_asprintf(call
, "dns_tcp_call_loop: "
272 "tstream_read_pdu_blob_recv() - %s",
275 reason
= nt_errstr(status
);
278 dns_tcp_terminate_connection(dns_conn
, reason
);
282 DEBUG(10,("Received DNS TCP packet of length %lu from %s\n",
283 (long) call
->in
.length
,
284 tsocket_address_string(dns_conn
->conn
->remote_address
, call
)));
286 /* skip length header */
288 call
->in
.length
-= 2;
290 subreq
= dns_process_send(call
, dns
->task
->event_ctx
, dns
,
292 if (subreq
== NULL
) {
293 dns_tcp_terminate_connection(
294 dns_conn
, "dns_tcp_call_loop: dns_process_send "
298 tevent_req_set_callback(subreq
, dns_tcp_call_process_done
, call
);
301 * The dns tcp pdu's has the length as 2 byte (initial_read_size),
302 * packet_full_request_u16 provides the pdu length then.
304 subreq
= tstream_read_pdu_blob_send(dns_conn
,
305 dns_conn
->conn
->event
.ctx
,
307 2, /* initial_read_size */
308 packet_full_request_u16
,
310 if (subreq
== NULL
) {
311 dns_tcp_terminate_connection(dns_conn
, "dns_tcp_call_loop: "
312 "no memory for tstream_read_pdu_blob_send");
315 tevent_req_set_callback(subreq
, dns_tcp_call_loop
, dns_conn
);
318 static void dns_tcp_call_process_done(struct tevent_req
*subreq
)
320 struct dns_tcp_call
*call
= tevent_req_callback_data(subreq
,
321 struct dns_tcp_call
);
322 struct dns_tcp_connection
*dns_conn
= call
->dns_conn
;
325 err
= dns_process_recv(subreq
, call
, &call
->out
);
327 if (!W_ERROR_IS_OK(err
)) {
328 DEBUG(1, ("dns_process returned %s\n", win_errstr(err
)));
329 dns_tcp_terminate_connection(dns_conn
,
330 "dns_tcp_call_loop: process function failed");
334 /* First add the length of the out buffer */
335 RSSVAL(call
->out_hdr
, 0, call
->out
.length
);
336 call
->out_iov
[0].iov_base
= (char *) call
->out_hdr
;
337 call
->out_iov
[0].iov_len
= 2;
339 call
->out_iov
[1].iov_base
= (char *) call
->out
.data
;
340 call
->out_iov
[1].iov_len
= call
->out
.length
;
342 subreq
= tstream_writev_queue_send(call
,
343 dns_conn
->conn
->event
.ctx
,
345 dns_conn
->send_queue
,
347 if (subreq
== NULL
) {
348 dns_tcp_terminate_connection(dns_conn
, "dns_tcp_call_loop: "
349 "no memory for tstream_writev_queue_send");
352 tevent_req_set_callback(subreq
, dns_tcp_call_writev_done
, call
);
355 static void dns_tcp_call_writev_done(struct tevent_req
*subreq
)
357 struct dns_tcp_call
*call
= tevent_req_callback_data(subreq
,
358 struct dns_tcp_call
);
362 rc
= tstream_writev_queue_recv(subreq
, &sys_errno
);
367 reason
= talloc_asprintf(call
, "dns_tcp_call_writev_done: "
368 "tstream_writev_queue_recv() - %d:%s",
369 sys_errno
, strerror(sys_errno
));
371 reason
= "dns_tcp_call_writev_done: tstream_writev_queue_recv() failed";
374 dns_tcp_terminate_connection(call
->dns_conn
, reason
);
378 /* We don't care about errors */
384 called when we get a new connection
386 static void dns_tcp_accept(struct stream_connection
*conn
)
388 struct dns_socket
*dns_socket
;
389 struct dns_tcp_connection
*dns_conn
;
390 struct tevent_req
*subreq
;
393 dns_conn
= talloc_zero(conn
, struct dns_tcp_connection
);
394 if (dns_conn
== NULL
) {
395 stream_terminate_connection(conn
,
396 "dns_tcp_accept: out of memory");
400 dns_conn
->send_queue
= tevent_queue_create(conn
, "dns_tcp_accept");
401 if (dns_conn
->send_queue
== NULL
) {
402 stream_terminate_connection(conn
,
403 "dns_tcp_accept: out of memory");
407 dns_socket
= talloc_get_type(conn
->private_data
, struct dns_socket
);
409 TALLOC_FREE(conn
->event
.fde
);
411 rc
= tstream_bsd_existing_socket(dns_conn
,
412 socket_get_fd(conn
->socket
),
415 stream_terminate_connection(conn
,
416 "dns_tcp_accept: out of memory");
420 dns_conn
->conn
= conn
;
421 dns_conn
->dns_socket
= dns_socket
;
422 conn
->private_data
= dns_conn
;
425 * The dns tcp pdu's has the length as 2 byte (initial_read_size),
426 * packet_full_request_u16 provides the pdu length then.
428 subreq
= tstream_read_pdu_blob_send(dns_conn
,
429 dns_conn
->conn
->event
.ctx
,
431 2, /* initial_read_size */
432 packet_full_request_u16
,
434 if (subreq
== NULL
) {
435 dns_tcp_terminate_connection(dns_conn
, "dns_tcp_accept: "
436 "no memory for tstream_read_pdu_blob_send");
439 tevent_req_set_callback(subreq
, dns_tcp_call_loop
, dns_conn
);
442 static const struct stream_server_ops dns_tcp_stream_ops
= {
444 .accept_connection
= dns_tcp_accept
,
445 .recv_handler
= dns_tcp_recv
,
446 .send_handler
= dns_tcp_send
449 struct dns_udp_call
{
450 struct dns_udp_socket
*sock
;
451 struct tsocket_address
*src
;
456 static void dns_udp_call_process_done(struct tevent_req
*subreq
);
457 static void dns_udp_call_sendto_done(struct tevent_req
*subreq
);
459 static void dns_udp_call_loop(struct tevent_req
*subreq
)
461 struct dns_udp_socket
*sock
= tevent_req_callback_data(subreq
,
462 struct dns_udp_socket
);
463 struct dns_server
*dns
= sock
->dns_socket
->dns
;
464 struct dns_udp_call
*call
;
469 call
= talloc(sock
, struct dns_udp_call
);
476 len
= tdgram_recvfrom_recv(subreq
, &sys_errno
,
477 call
, &buf
, &call
->src
);
485 call
->in
.length
= len
;
487 DEBUG(10,("Received DNS UDP packet of length %lu from %s\n",
488 (long)call
->in
.length
,
489 tsocket_address_string(call
->src
, call
)));
491 subreq
= dns_process_send(call
, dns
->task
->event_ctx
, dns
,
493 if (subreq
== NULL
) {
497 tevent_req_set_callback(subreq
, dns_udp_call_process_done
, call
);
500 subreq
= tdgram_recvfrom_send(sock
,
501 sock
->dns_socket
->dns
->task
->event_ctx
,
503 if (subreq
== NULL
) {
504 task_server_terminate(sock
->dns_socket
->dns
->task
,
505 "no memory for tdgram_recvfrom_send",
509 tevent_req_set_callback(subreq
, dns_udp_call_loop
, sock
);
512 static void dns_udp_call_process_done(struct tevent_req
*subreq
)
514 struct dns_udp_call
*call
= tevent_req_callback_data(
515 subreq
, struct dns_udp_call
);
516 struct dns_udp_socket
*sock
= call
->sock
;
517 struct dns_server
*dns
= sock
->dns_socket
->dns
;
520 err
= dns_process_recv(subreq
, call
, &call
->out
);
522 if (!W_ERROR_IS_OK(err
)) {
523 DEBUG(1, ("dns_process returned %s\n", win_errstr(err
)));
528 subreq
= tdgram_sendto_queue_send(call
,
529 dns
->task
->event_ctx
,
535 if (subreq
== NULL
) {
539 tevent_req_set_callback(subreq
, dns_udp_call_sendto_done
, call
);
542 static void dns_udp_call_sendto_done(struct tevent_req
*subreq
)
544 struct dns_udp_call
*call
= tevent_req_callback_data(subreq
,
545 struct dns_udp_call
);
549 ret
= tdgram_sendto_queue_recv(subreq
, &sys_errno
);
551 /* We don't care about errors */
557 start listening on the given address
559 static NTSTATUS
dns_add_socket(struct dns_server
*dns
,
560 const struct model_ops
*model_ops
,
565 struct dns_socket
*dns_socket
;
566 struct dns_udp_socket
*dns_udp_socket
;
567 struct tevent_req
*udpsubreq
;
571 dns_socket
= talloc(dns
, struct dns_socket
);
572 NT_STATUS_HAVE_NO_MEMORY(dns_socket
);
574 dns_socket
->dns
= dns
;
576 ret
= tsocket_address_inet_from_strings(dns_socket
, "ip",
578 &dns_socket
->local_address
);
580 status
= map_nt_error_from_unix_common(errno
);
584 status
= stream_setup_socket(dns
->task
,
585 dns
->task
->event_ctx
,
589 "ip", address
, &port
,
590 lpcfg_socket_options(dns
->task
->lp_ctx
),
592 if (!NT_STATUS_IS_OK(status
)) {
593 DEBUG(0,("Failed to bind to %s:%u TCP - %s\n",
594 address
, port
, nt_errstr(status
)));
595 talloc_free(dns_socket
);
599 dns_udp_socket
= talloc(dns_socket
, struct dns_udp_socket
);
600 NT_STATUS_HAVE_NO_MEMORY(dns_udp_socket
);
602 dns_udp_socket
->dns_socket
= dns_socket
;
604 ret
= tdgram_inet_udp_socket(dns_socket
->local_address
,
607 &dns_udp_socket
->dgram
);
609 status
= map_nt_error_from_unix_common(errno
);
610 DEBUG(0,("Failed to bind to %s:%u UDP - %s\n",
611 address
, port
, nt_errstr(status
)));
615 dns_udp_socket
->send_queue
= tevent_queue_create(dns_udp_socket
,
616 "dns_udp_send_queue");
617 NT_STATUS_HAVE_NO_MEMORY(dns_udp_socket
->send_queue
);
619 udpsubreq
= tdgram_recvfrom_send(dns_udp_socket
,
620 dns
->task
->event_ctx
,
621 dns_udp_socket
->dgram
);
622 NT_STATUS_HAVE_NO_MEMORY(udpsubreq
);
623 tevent_req_set_callback(udpsubreq
, dns_udp_call_loop
, dns_udp_socket
);
629 setup our listening sockets on the configured network interfaces
631 static NTSTATUS
dns_startup_interfaces(struct dns_server
*dns
, struct loadparm_context
*lp_ctx
,
632 struct interface
*ifaces
)
634 const struct model_ops
*model_ops
;
636 TALLOC_CTX
*tmp_ctx
= talloc_new(dns
);
640 /* within the dns task we want to be a single process, so
641 ask for the single process model ops and pass these to the
642 stream_setup_socket() call. */
643 model_ops
= process_model_startup("single");
645 DEBUG(0,("Can't find 'single' process model_ops\n"));
646 return NT_STATUS_INTERNAL_ERROR
;
649 num_interfaces
= iface_list_count(ifaces
);
651 for (i
=0; i
<num_interfaces
; i
++) {
652 const char *address
= talloc_strdup(tmp_ctx
, iface_list_n_ip(ifaces
, i
));
654 status
= dns_add_socket(dns
, model_ops
, "dns", address
, DNS_SERVICE_PORT
);
655 NT_STATUS_NOT_OK_RETURN(status
);
658 talloc_free(tmp_ctx
);
663 static int dns_server_sort_zones(struct ldb_message
**m1
, struct ldb_message
**m2
)
668 n1
= ldb_msg_find_attr_as_string(*m1
, "name", NULL
);
669 n2
= ldb_msg_find_attr_as_string(*m2
, "name", NULL
);
674 /* If the string lengths are not equal just sort by length */
676 /* If m1 is the larger zone name, return it first */
680 /*TODO: We need to compare DNs here, we want the DomainDNSZones first */
684 static void dns_task_init(struct task_server
*task
)
686 struct dns_server
*dns
;
688 struct interface
*ifaces
;
690 struct ldb_result
*res
;
691 static const char * const attrs
[] = { "name", NULL
};
694 switch (lpcfg_server_role(task
->lp_ctx
)) {
695 case ROLE_STANDALONE
:
696 task_server_terminate(task
, "dns: no DNS required in standalone configuration", false);
698 case ROLE_DOMAIN_MEMBER
:
699 task_server_terminate(task
, "dns: no DNS required in member server configuration", false);
701 case ROLE_ACTIVE_DIRECTORY_DC
:
702 /* Yes, we want a DNS */
706 load_interface_list(task
, task
->lp_ctx
, &ifaces
);
708 if (iface_list_count(ifaces
) == 0) {
709 task_server_terminate(task
, "dns: no network interfaces configured", false);
713 task_server_set_title(task
, "task[dns]");
715 dns
= talloc_zero(task
, struct dns_server
);
717 task_server_terminate(task
, "dns: out of memory", true);
723 dns
->samdb
= samdb_connect(dns
, dns
->task
->event_ctx
, dns
->task
->lp_ctx
,
724 system_session(dns
->task
->lp_ctx
), 0);
726 task_server_terminate(task
, "dns: samdb_connect failed", true);
730 // TODO: this search does not work against windows
731 ret
= dsdb_search(dns
->samdb
, dns
, &res
, NULL
, LDB_SCOPE_SUBTREE
,
732 attrs
, DSDB_SEARCH_SEARCH_ALL_PARTITIONS
, "(objectClass=dnsZone)");
733 if (ret
!= LDB_SUCCESS
) {
734 task_server_terminate(task
,
735 "dns: failed to look up root DNS zones",
740 TYPESAFE_QSORT(res
->msgs
, res
->count
, dns_server_sort_zones
);
742 for (i
=0; i
< res
->count
; i
++) {
743 struct dns_server_zone
*z
;
745 z
= talloc_zero(dns
, struct dns_server_zone
);
749 z
->name
= ldb_msg_find_attr_as_string(res
->msgs
[i
], "name", NULL
);
750 z
->dn
= talloc_move(z
, &res
->msgs
[i
]->dn
);
752 DLIST_ADD_END(dns
->zones
, z
, NULL
);
755 status
= dns_startup_interfaces(dns
, task
->lp_ctx
, ifaces
);
756 if (!NT_STATUS_IS_OK(status
)) {
757 task_server_terminate(task
, "dns failed to setup interfaces", true);
762 NTSTATUS
server_service_dns_init(void)
764 return register_server_service("dns", dns_task_init
);