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"
46 /* hold information about one dns socket */
48 struct dns_server
*dns
;
49 struct tsocket_address
*local_address
;
52 struct dns_udp_socket
{
53 struct dns_socket
*dns_socket
;
54 struct tdgram_context
*dgram
;
55 struct tevent_queue
*send_queue
;
59 state of an open tcp connection
61 struct dns_tcp_connection
{
62 /* stream connection we belong to */
63 struct stream_connection
*conn
;
65 /* the dns_server the connection belongs to */
66 struct dns_socket
*dns_socket
;
68 struct tstream_context
*tstream
;
70 struct tevent_queue
*send_queue
;
73 static void dns_tcp_terminate_connection(struct dns_tcp_connection
*dnsconn
, const char *reason
)
75 stream_terminate_connection(dnsconn
->conn
, reason
);
78 static void dns_tcp_recv(struct stream_connection
*conn
, uint16_t flags
)
80 struct dns_tcp_connection
*dnsconn
= talloc_get_type(conn
->private_data
,
81 struct dns_tcp_connection
);
82 /* this should never be triggered! */
83 dns_tcp_terminate_connection(dnsconn
, "dns_tcp_recv: called");
86 static void dns_tcp_send(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_send: called");
94 static NTSTATUS
dns_process(struct dns_server
*dns
,
99 enum ndr_err_code ndr_err
;
101 struct dns_name_packet
*in_packet
;
102 struct dns_name_packet
*out_packet
;
103 struct dns_res_rec
*answers
= NULL
, *nsrecs
= NULL
, *additional
= NULL
;
104 uint16_t num_answers
= 0 , num_nsrecs
= 0, num_additional
= 0;
106 if (in
->length
< 12) {
107 return NT_STATUS_INVALID_PARAMETER
;
110 in_packet
= talloc_zero(mem_ctx
, struct dns_name_packet
);
111 /* TODO: We don't really need an out_packet. */
112 out_packet
= talloc_zero(mem_ctx
, struct dns_name_packet
);
114 if (in_packet
== NULL
) return NT_STATUS_NO_MEMORY
;
115 if (out_packet
== NULL
) return NT_STATUS_NO_MEMORY
;
117 dump_data(2, in
->data
, in
->length
);
119 ndr_err
= ndr_pull_struct_blob(in
, in_packet
, in_packet
,
120 (ndr_pull_flags_fn_t
)ndr_pull_dns_name_packet
);
121 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
122 TALLOC_FREE(in_packet
);
123 DEBUG(0, ("Failed to parse packet %d!\n", ndr_err
));
126 out
->data
[2] |= 0x80; /* Toggle DNS_FLAG_REPLY */
127 out
->data
[3] |= DNS_RCODE_FORMERR
;
132 NDR_PRINT_DEBUG(dns_name_packet
, in_packet
);
133 *out_packet
= *in_packet
;
134 out_packet
->operation
|= DNS_FLAG_REPLY
;
136 switch (in_packet
->operation
& DNS_OPCODE
) {
137 case DNS_OPCODE_QUERY
:
139 ret
= dns_server_process_query(dns
, out_packet
, in_packet
,
140 &answers
, &num_answers
,
141 &nsrecs
, &num_nsrecs
,
142 &additional
, &num_additional
);
145 case DNS_OPCODE_REGISTER
:
146 ret
= dns_server_process_update(dns
, out_packet
, in_packet
,
147 answers
, num_answers
,
148 &nsrecs
, &num_nsrecs
,
149 &additional
, &num_additional
);
152 ret
= WERR_DNS_ERROR_RCODE_NOT_IMPLEMENTED
;
156 if (W_ERROR_IS_OK(ret
)) {
157 out_packet
->ancount
= num_answers
;
158 out_packet
->answers
= answers
;
160 out_packet
->nscount
= num_nsrecs
;
161 out_packet
->nsrecs
= nsrecs
;
163 out_packet
->arcount
= num_additional
;
164 out_packet
->additional
= additional
;
166 out_packet
->operation
|= werr_to_dns_err(ret
);
169 NDR_PRINT_DEBUG(dns_name_packet
, out_packet
);
170 ndr_err
= ndr_push_struct_blob(out
, out_packet
, out_packet
,
171 (ndr_push_flags_fn_t
)ndr_push_dns_name_packet
);
172 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
173 TALLOC_FREE(in_packet
);
174 TALLOC_FREE(out_packet
);
175 DEBUG(0, ("Failed to push packet %d!\n", ndr_err
));
178 out
->data
[2] |= 0x80; /* Toggle DNS_FLAG_REPLY */
179 out
->data
[3] |= DNS_RCODE_SERVFAIL
;
184 dump_data(2, out
->data
, out
->length
);
188 struct dns_tcp_call
{
189 struct dns_tcp_connection
*dns_conn
;
193 struct iovec out_iov
[2];
196 static void dns_tcp_call_writev_done(struct tevent_req
*subreq
);
198 static void dns_tcp_call_loop(struct tevent_req
*subreq
)
200 struct dns_tcp_connection
*dns_conn
= tevent_req_callback_data(subreq
,
201 struct dns_tcp_connection
);
202 struct dns_tcp_call
*call
;
205 call
= talloc(dns_conn
, struct dns_tcp_call
);
207 dns_tcp_terminate_connection(dns_conn
, "dns_tcp_call_loop: "
208 "no memory for dns_tcp_call");
211 call
->dns_conn
= dns_conn
;
213 status
= tstream_read_pdu_blob_recv(subreq
,
217 if (!NT_STATUS_IS_OK(status
)) {
220 reason
= talloc_asprintf(call
, "dns_tcp_call_loop: "
221 "tstream_read_pdu_blob_recv() - %s",
224 reason
= nt_errstr(status
);
227 dns_tcp_terminate_connection(dns_conn
, reason
);
231 DEBUG(10,("Received krb5 TCP packet of length %lu from %s\n",
232 (long) call
->in
.length
,
233 tsocket_address_string(dns_conn
->conn
->remote_address
, call
)));
235 /* skip length header */
237 call
->in
.length
-= 4;
240 status
= dns_process(dns_conn
->dns_socket
->dns
, call
, &call
->in
, &call
->out
);
241 if (!NT_STATUS_IS_OK(status
)) {
242 DEBUG(0, ("dns_process returned %s\n", nt_errstr(status
)));
243 dns_tcp_terminate_connection(dns_conn
,
244 "dns_tcp_call_loop: process function failed");
248 /* First add the length of the out buffer */
249 RSIVAL(call
->out_hdr
, 0, call
->out
.length
);
250 call
->out_iov
[0].iov_base
= (char *) call
->out_hdr
;
251 call
->out_iov
[0].iov_len
= 4;
253 call
->out_iov
[1].iov_base
= (char *) call
->out
.data
;
254 call
->out_iov
[1].iov_len
= call
->out
.length
;
256 subreq
= tstream_writev_queue_send(call
,
257 dns_conn
->conn
->event
.ctx
,
259 dns_conn
->send_queue
,
261 if (subreq
== NULL
) {
262 dns_tcp_terminate_connection(dns_conn
, "dns_tcp_call_loop: "
263 "no memory for tstream_writev_queue_send");
266 tevent_req_set_callback(subreq
, dns_tcp_call_writev_done
, call
);
269 * The krb5 tcp pdu's has the length as 4 byte (initial_read_size),
270 * packet_full_request_u32 provides the pdu length then.
272 subreq
= tstream_read_pdu_blob_send(dns_conn
,
273 dns_conn
->conn
->event
.ctx
,
275 4, /* initial_read_size */
276 packet_full_request_u32
,
278 if (subreq
== NULL
) {
279 dns_tcp_terminate_connection(dns_conn
, "dns_tcp_call_loop: "
280 "no memory for tstream_read_pdu_blob_send");
283 tevent_req_set_callback(subreq
, dns_tcp_call_loop
, dns_conn
);
286 static void dns_tcp_call_writev_done(struct tevent_req
*subreq
)
288 struct dns_tcp_call
*call
= tevent_req_callback_data(subreq
,
289 struct dns_tcp_call
);
293 rc
= tstream_writev_queue_recv(subreq
, &sys_errno
);
298 reason
= talloc_asprintf(call
, "dns_tcp_call_writev_done: "
299 "tstream_writev_queue_recv() - %d:%s",
300 sys_errno
, strerror(sys_errno
));
302 reason
= "dns_tcp_call_writev_done: tstream_writev_queue_recv() failed";
305 dns_tcp_terminate_connection(call
->dns_conn
, reason
);
309 /* We don't care about errors */
315 called when we get a new connection
317 static void dns_tcp_accept(struct stream_connection
*conn
)
319 struct dns_socket
*dns_socket
;
320 struct dns_tcp_connection
*dns_conn
;
321 struct tevent_req
*subreq
;
324 dns_conn
= talloc_zero(conn
, struct dns_tcp_connection
);
325 if (dns_conn
== NULL
) {
326 stream_terminate_connection(conn
,
327 "dns_tcp_accept: out of memory");
331 dns_conn
->send_queue
= tevent_queue_create(conn
, "dns_tcp_accept");
332 if (dns_conn
->send_queue
== NULL
) {
333 stream_terminate_connection(conn
,
334 "dns_tcp_accept: out of memory");
338 dns_socket
= talloc_get_type(conn
->private_data
, struct dns_socket
);
340 TALLOC_FREE(conn
->event
.fde
);
342 rc
= tstream_bsd_existing_socket(dns_conn
,
343 socket_get_fd(conn
->socket
),
346 stream_terminate_connection(conn
,
347 "dns_tcp_accept: out of memory");
351 dns_conn
->conn
= conn
;
352 dns_conn
->dns_socket
= dns_socket
;
353 conn
->private_data
= dns_conn
;
356 * The krb5 tcp pdu's has the length as 4 byte (initial_read_size),
357 * packet_full_request_u32 provides the pdu length then.
359 subreq
= tstream_read_pdu_blob_send(dns_conn
,
360 dns_conn
->conn
->event
.ctx
,
362 4, /* initial_read_size */
363 packet_full_request_u32
,
365 if (subreq
== NULL
) {
366 dns_tcp_terminate_connection(dns_conn
, "dns_tcp_accept: "
367 "no memory for tstream_read_pdu_blob_send");
370 tevent_req_set_callback(subreq
, dns_tcp_call_loop
, dns_conn
);
373 static const struct stream_server_ops dns_tcp_stream_ops
= {
375 .accept_connection
= dns_tcp_accept
,
376 .recv_handler
= dns_tcp_recv
,
377 .send_handler
= dns_tcp_send
380 struct dns_udp_call
{
381 struct tsocket_address
*src
;
386 static void dns_udp_call_sendto_done(struct tevent_req
*subreq
);
388 static void dns_udp_call_loop(struct tevent_req
*subreq
)
390 struct dns_udp_socket
*sock
= tevent_req_callback_data(subreq
,
391 struct dns_udp_socket
);
392 struct dns_udp_call
*call
;
398 call
= talloc(sock
, struct dns_udp_call
);
404 len
= tdgram_recvfrom_recv(subreq
, &sys_errno
,
405 call
, &buf
, &call
->src
);
413 call
->in
.length
= len
;
415 DEBUG(10,("Received krb5 UDP packet of length %lu from %s\n",
416 (long)call
->in
.length
,
417 tsocket_address_string(call
->src
, call
)));
420 status
= dns_process(sock
->dns_socket
->dns
, call
, &call
->in
, &call
->out
);
421 if (!NT_STATUS_IS_OK(status
)) {
423 DEBUG(0, ("dns_process returned %s\n", nt_errstr(status
)));
427 subreq
= tdgram_sendto_queue_send(call
,
428 sock
->dns_socket
->dns
->task
->event_ctx
,
434 if (subreq
== NULL
) {
438 tevent_req_set_callback(subreq
, dns_udp_call_sendto_done
, call
);
441 subreq
= tdgram_recvfrom_send(sock
,
442 sock
->dns_socket
->dns
->task
->event_ctx
,
444 if (subreq
== NULL
) {
445 task_server_terminate(sock
->dns_socket
->dns
->task
,
446 "no memory for tdgram_recvfrom_send",
450 tevent_req_set_callback(subreq
, dns_udp_call_loop
, sock
);
453 static void dns_udp_call_sendto_done(struct tevent_req
*subreq
)
455 struct dns_udp_call
*call
= tevent_req_callback_data(subreq
,
456 struct dns_udp_call
);
460 ret
= tdgram_sendto_queue_recv(subreq
, &sys_errno
);
462 /* We don't care about errors */
468 start listening on the given address
470 static NTSTATUS
dns_add_socket(struct dns_server
*dns
,
471 const struct model_ops
*model_ops
,
476 struct dns_socket
*dns_socket
;
477 struct dns_udp_socket
*dns_udp_socket
;
478 struct tevent_req
*udpsubreq
;
482 dns_socket
= talloc(dns
, struct dns_socket
);
483 NT_STATUS_HAVE_NO_MEMORY(dns_socket
);
485 dns_socket
->dns
= dns
;
487 ret
= tsocket_address_inet_from_strings(dns_socket
, "ip",
489 &dns_socket
->local_address
);
491 status
= map_nt_error_from_unix(errno
);
495 status
= stream_setup_socket(dns
->task
,
496 dns
->task
->event_ctx
,
500 "ip", address
, &port
,
501 lpcfg_socket_options(dns
->task
->lp_ctx
),
503 if (!NT_STATUS_IS_OK(status
)) {
504 DEBUG(0,("Failed to bind to %s:%u TCP - %s\n",
505 address
, port
, nt_errstr(status
)));
506 talloc_free(dns_socket
);
510 dns_udp_socket
= talloc(dns_socket
, struct dns_udp_socket
);
511 NT_STATUS_HAVE_NO_MEMORY(dns_udp_socket
);
513 dns_udp_socket
->dns_socket
= dns_socket
;
515 ret
= tdgram_inet_udp_socket(dns_socket
->local_address
,
518 &dns_udp_socket
->dgram
);
520 status
= map_nt_error_from_unix(errno
);
521 DEBUG(0,("Failed to bind to %s:%u UDP - %s\n",
522 address
, port
, nt_errstr(status
)));
526 dns_udp_socket
->send_queue
= tevent_queue_create(dns_udp_socket
,
527 "dns_udp_send_queue");
528 NT_STATUS_HAVE_NO_MEMORY(dns_udp_socket
->send_queue
);
530 udpsubreq
= tdgram_recvfrom_send(dns_udp_socket
,
531 dns
->task
->event_ctx
,
532 dns_udp_socket
->dgram
);
533 NT_STATUS_HAVE_NO_MEMORY(udpsubreq
);
534 tevent_req_set_callback(udpsubreq
, dns_udp_call_loop
, dns_udp_socket
);
540 setup our listening sockets on the configured network interfaces
542 static NTSTATUS
dns_startup_interfaces(struct dns_server
*dns
, struct loadparm_context
*lp_ctx
,
543 struct interface
*ifaces
)
545 const struct model_ops
*model_ops
;
547 TALLOC_CTX
*tmp_ctx
= talloc_new(dns
);
551 /* within the dns task we want to be a single process, so
552 ask for the single process model ops and pass these to the
553 stream_setup_socket() call. */
554 model_ops
= process_model_startup("single");
556 DEBUG(0,("Can't find 'single' process model_ops\n"));
557 return NT_STATUS_INTERNAL_ERROR
;
560 num_interfaces
= iface_count(ifaces
);
562 for (i
=0; i
<num_interfaces
; i
++) {
563 const char *address
= talloc_strdup(tmp_ctx
, iface_n_ip(ifaces
, i
));
565 status
= dns_add_socket(dns
, model_ops
, "dns", address
, DNS_SERVICE_PORT
);
566 NT_STATUS_NOT_OK_RETURN(status
);
569 talloc_free(tmp_ctx
);
574 static int dns_server_sort_zones(struct ldb_message
**m1
, struct ldb_message
**m2
)
579 n1
= ldb_msg_find_attr_as_string(*m1
, "name", NULL
);
580 n2
= ldb_msg_find_attr_as_string(*m2
, "name", NULL
);
585 /* If the string lengths are not equal just sort by length */
587 /* If m1 is the larger zone name, return it first */
591 /*TODO: We need to compare DNs here, we want the DomainDNSZones first */
595 static void dns_task_init(struct task_server
*task
)
597 struct dns_server
*dns
;
599 struct interface
*ifaces
;
601 struct ldb_result
*res
;
602 struct ldb_dn
*rootdn
;
603 static const char * const attrs
[] = { "name", NULL
};
606 switch (lpcfg_server_role(task
->lp_ctx
)) {
607 case ROLE_STANDALONE
:
608 task_server_terminate(task
, "dns: no DNS required in standalone configuration", false);
610 case ROLE_DOMAIN_MEMBER
:
611 task_server_terminate(task
, "dns: no DNS required in member server configuration", false);
613 case ROLE_DOMAIN_CONTROLLER
:
614 /* Yes, we want a DNS */
618 load_interfaces(task
, lpcfg_interfaces(task
->lp_ctx
), &ifaces
);
620 if (iface_count(ifaces
) == 0) {
621 task_server_terminate(task
, "dns: no network interfaces configured", false);
625 task_server_set_title(task
, "task[dns]");
627 dns
= talloc_zero(task
, struct dns_server
);
629 task_server_terminate(task
, "dns: out of memory", true);
635 dns
->samdb
= samdb_connect(dns
, dns
->task
->event_ctx
, dns
->task
->lp_ctx
,
636 system_session(dns
->task
->lp_ctx
), 0);
638 task_server_terminate(task
, "dns: samdb_connect failed", true);
642 rootdn
= ldb_dn_new(dns
, dns
->samdb
, "");
643 if (rootdn
== NULL
) {
644 task_server_terminate(task
, "dns: out of memory", true);
648 // TODO: this search does not work against windows
649 ret
= dsdb_search(dns
->samdb
, dns
, &res
, rootdn
, LDB_SCOPE_SUBTREE
,
650 attrs
, DSDB_SEARCH_SEARCH_ALL_PARTITIONS
, "(objectClass=dnsZone)");
651 if (ret
!= LDB_SUCCESS
) {
652 task_server_terminate(task
,
653 "dns: failed to look up root DNS zones",
658 TYPESAFE_QSORT(res
->msgs
, res
->count
, dns_server_sort_zones
);
660 for (i
=0; i
< res
->count
; i
++) {
661 struct dns_server_zone
*z
;
663 z
= talloc_zero(dns
, struct dns_server_zone
);
667 z
->name
= ldb_msg_find_attr_as_string(res
->msgs
[i
], "name", NULL
);
668 z
->dn
= talloc_move(z
, &res
->msgs
[i
]->dn
);
670 DLIST_ADD_END(dns
->zones
, z
, NULL
);
673 status
= dns_startup_interfaces(dns
, task
->lp_ctx
, ifaces
);
674 if (!NT_STATUS_IS_OK(status
)) {
675 task_server_terminate(task
, "dns failed to setup interfaces", true);
680 NTSTATUS
server_service_dns_init(void)
682 return register_server_service("dns", dns_task_init
);