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_err_to_ntstatus(enum dns_rcode rcode
)
97 case DNS_RCODE_OK
: return NT_STATUS_OK
;
98 case DNS_RCODE_FORMERR
: return NT_STATUS_INVALID_PARAMETER
;
99 case DNS_RCODE_SERVFAIL
: return NT_STATUS_INTERNAL_ERROR
;
100 case DNS_RCODE_NXDOMAIN
: return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
101 case DNS_RCODE_NOTIMP
: return NT_STATUS_NOT_IMPLEMENTED
;
102 case DNS_RCODE_REFUSED
: return NT_STATUS_ACCESS_DENIED
;
103 case DNS_RCODE_NOTAUTH
: return NT_STATUS_FOOBAR
;
104 default: return NT_STATUS_NONE_MAPPED
;
108 static uint8_t ntstatus_to_dns_err(NTSTATUS status
)
110 if (NT_STATUS_EQUAL(NT_STATUS_OK
, status
)) {
112 } else if (NT_STATUS_EQUAL(NT_STATUS_INVALID_PARAMETER
, status
)) {
113 return DNS_RCODE_FORMERR
;
114 } else if (NT_STATUS_EQUAL(NT_STATUS_INTERNAL_ERROR
, status
)) {
115 return DNS_RCODE_SERVFAIL
;
116 } else if (NT_STATUS_EQUAL(NT_STATUS_OBJECT_NAME_NOT_FOUND
, status
)) {
117 return DNS_RCODE_NXDOMAIN
;
118 } else if (NT_STATUS_EQUAL(NT_STATUS_NOT_IMPLEMENTED
, status
)) {
119 return DNS_RCODE_NOTIMP
;
120 } else if (NT_STATUS_EQUAL(NT_STATUS_ACCESS_DENIED
, status
)) {
121 return DNS_RCODE_REFUSED
;
122 } else if (NT_STATUS_EQUAL(NT_STATUS_FOOBAR
, status
)) {
123 return DNS_RCODE_NOTAUTH
;
125 DEBUG(0, ("No mapping exists for %s\n", nt_errstr(status
)));
128 static bool dns_name_match(const char *zone
, const char *name
, size_t *host_part_len
)
130 size_t zl
= strlen(zone
);
131 size_t nl
= strlen(name
);
133 static const size_t fixup
= 'a' - 'A';
139 for (zi
= zl
, ni
= nl
; zi
>= 0; zi
--, ni
--) {
143 /* convert to lower case */
144 if (zc
>= 'A' && zc
<= 'Z') {
147 if (nc
>= 'A' && nc
<= 'Z') {
157 if (name
[ni
] != '.') {
164 *host_part_len
= ni
+1;
169 static NTSTATUS
dns_name2dn(struct dns_server
*dns
,
176 const struct dns_server_zone
*z
;
177 size_t host_part_len
= 0;
180 return NT_STATUS_INVALID_PARAMETER
;
183 /*TODO: Check if 'name' is a valid DNS name */
185 if (strcmp(name
, "") == 0) {
186 base
= ldb_get_default_basedn(dns
->samdb
);
187 dn
= ldb_dn_copy(mem_ctx
, base
);
188 ldb_dn_add_child_fmt(dn
, "DC=@,DC=RootDNSServers,CN=MicrosoftDNS,CN=System");
193 for (z
= dns
->zones
; z
!= NULL
; z
= z
->next
) {
196 match
= dns_name_match(z
->name
, name
, &host_part_len
);
203 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
206 if (host_part_len
== 0) {
207 dn
= ldb_dn_copy(mem_ctx
, z
->dn
);
208 ldb_dn_add_child_fmt(dn
, "DC=@");
213 dn
= ldb_dn_copy(mem_ctx
, z
->dn
);
214 ldb_dn_add_child_fmt(dn
, "DC=%*.*s", (int)host_part_len
, (int)host_part_len
, name
);
219 static NTSTATUS
handle_question(struct dns_server
*dns
,
221 const struct dns_name_question
*question
,
222 struct dns_res_rec
**answers
, uint16_t *ancount
)
224 struct dns_res_rec
*ans
;
225 struct ldb_dn
*dn
= NULL
;
227 static const char * const attrs
[] = { "dnsRecord", NULL
};
229 uint16_t ai
= *ancount
;
231 struct ldb_message
*msg
= NULL
;
232 struct dnsp_DnssrvRpcRecord
*recs
;
233 struct ldb_message_element
*el
;
235 status
= dns_name2dn(dns
, mem_ctx
, question
->name
, &dn
);
236 NT_STATUS_NOT_OK_RETURN(status
);
238 ret
= dsdb_search_one(dns
->samdb
, mem_ctx
, &msg
, dn
,
239 LDB_SCOPE_BASE
, attrs
, 0, "%s", "(objectClass=dnsNode)");
240 if (ret
!= LDB_SUCCESS
) {
241 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
244 el
= ldb_msg_find_element(msg
, attrs
[0]);
246 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
249 recs
= talloc_array(mem_ctx
, struct dnsp_DnssrvRpcRecord
, el
->num_values
);
250 for (ri
= 0; ri
< el
->num_values
; ri
++) {
251 struct ldb_val
*v
= &el
->values
[ri
];
252 enum ndr_err_code ndr_err
;
254 ndr_err
= ndr_pull_struct_blob(v
, recs
, &recs
[ri
],
255 (ndr_pull_flags_fn_t
)ndr_pull_dnsp_DnssrvRpcRecord
);
256 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
257 DEBUG(0, ("Failed to grab dnsp_DnssrvRpcRecord\n"));
258 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
262 ans
= talloc_realloc(mem_ctx
, *answers
, struct dns_res_rec
,
263 ai
+ el
->num_values
);
264 NT_STATUS_HAVE_NO_MEMORY(ans
);
266 switch (question
->question_type
) {
267 case DNS_QTYPE_CNAME
:
268 for (ri
= 0; ri
< el
->num_values
; ri
++) {
269 if (recs
[ri
].wType
!= question
->question_type
) {
273 ZERO_STRUCT(ans
[ai
]);
274 ans
[ai
].name
= talloc_strdup(ans
, question
->name
);
275 ans
[ai
].rr_type
= DNS_QTYPE_CNAME
;
276 ans
[ai
].rr_class
= DNS_QCLASS_IP
;
277 ans
[ai
].ttl
= recs
[ri
].dwTtlSeconds
;
278 ans
[ai
].length
= UINT16_MAX
;
279 ans
[ai
].rdata
.cname_record
= talloc_strdup(ans
, recs
[ri
].data
.cname
);
284 for (ri
= 0; ri
< el
->num_values
; ri
++) {
285 if (recs
[ri
].wType
!= question
->question_type
) {
289 ZERO_STRUCT(ans
[ai
]);
290 ans
[ai
].name
= talloc_strdup(ans
, question
->name
);
291 ans
[ai
].rr_type
= DNS_QTYPE_A
;
292 ans
[ai
].rr_class
= DNS_QCLASS_IP
;
293 ans
[ai
].ttl
= recs
[ri
].dwTtlSeconds
;
294 ans
[ai
].length
= UINT16_MAX
;
295 ans
[ai
].rdata
.ipv4_record
= talloc_strdup(ans
, recs
[ri
].data
.ipv4
);
300 for (ri
= 0; ri
< el
->num_values
; ri
++) {
301 if (recs
[ri
].wType
!= question
->question_type
) {
305 ZERO_STRUCT(ans
[ai
]);
306 ans
[ai
].name
= talloc_strdup(ans
, question
->name
);
307 ans
[ai
].rr_type
= DNS_QTYPE_AAAA
;
308 ans
[ai
].rr_class
= DNS_QCLASS_IP
;
309 ans
[ai
].ttl
= recs
[ri
].dwTtlSeconds
;
310 ans
[ai
].length
= UINT16_MAX
;
311 ans
[ai
].rdata
.ipv6_record
= recs
[ri
].data
.ipv6
;
316 for (ri
= 0; ri
< el
->num_values
; ri
++) {
317 if (recs
[ri
].wType
!= question
->question_type
) {
321 ZERO_STRUCT(ans
[ai
]);
322 ans
[ai
].name
= question
->name
;
323 ans
[ai
].rr_type
= DNS_QTYPE_NS
;
324 ans
[ai
].rr_class
= DNS_QCLASS_IP
;
325 ans
[ai
].ttl
= recs
[ri
].dwTtlSeconds
;
326 ans
[ai
].length
= UINT16_MAX
;
327 ans
[ai
].rdata
.ns_record
= recs
[ri
].data
.ns
;
332 for (ri
= 0; ri
< el
->num_values
; ri
++) {
333 if (recs
[ri
].wType
!= question
->question_type
) {
337 ZERO_STRUCT(ans
[ai
]);
338 ans
[ai
].name
= question
->name
;
339 ans
[ai
].rr_type
= DNS_QTYPE_SRV
;
340 ans
[ai
].rr_class
= DNS_QCLASS_IP
;
341 ans
[ai
].ttl
= recs
[ri
].dwTtlSeconds
;
342 ans
[ai
].length
= UINT16_MAX
;
343 ans
[ai
].rdata
.srv_record
.priority
= recs
[ri
].data
.srv
.wPriority
;
344 ans
[ai
].rdata
.srv_record
.weight
= recs
[ri
].data
.srv
.wWeight
;
345 ans
[ai
].rdata
.srv_record
.port
= recs
[ri
].data
.srv
.wPort
;
346 ans
[ai
].rdata
.srv_record
.target
= recs
[ri
].data
.srv
.nameTarget
;
351 for (ri
= 0; ri
< el
->num_values
; ri
++) {
352 if (recs
[ri
].wType
!= question
->question_type
) {
356 ZERO_STRUCT(ans
[ai
]);
357 ans
[ai
].name
= question
->name
;
358 ans
[ai
].rr_type
= DNS_QTYPE_SOA
;
359 ans
[ai
].rr_class
= DNS_QCLASS_IP
;
360 ans
[ai
].ttl
= recs
[ri
].dwTtlSeconds
;
361 ans
[ai
].length
= UINT16_MAX
;
362 ans
[ai
].rdata
.soa_record
.mname
= recs
[ri
].data
.soa
.mname
;
363 ans
[ai
].rdata
.soa_record
.rname
= recs
[ri
].data
.soa
.rname
;
364 ans
[ai
].rdata
.soa_record
.serial
= recs
[ri
].data
.soa
.serial
;
365 ans
[ai
].rdata
.soa_record
.refresh
= recs
[ri
].data
.soa
.refresh
;
366 ans
[ai
].rdata
.soa_record
.retry
= recs
[ri
].data
.soa
.retry
;
367 ans
[ai
].rdata
.soa_record
.expire
= recs
[ri
].data
.soa
.expire
;
368 ans
[ai
].rdata
.soa_record
.minimum
= recs
[ri
].data
.soa
.minimum
;
373 return NT_STATUS_NOT_IMPLEMENTED
;
376 if (*ancount
== ai
) {
377 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
387 static NTSTATUS
dns_server_process_query(struct dns_server
*dns
,
389 struct dns_name_packet
*in
,
390 struct dns_res_rec
**answers
, uint16_t *ancount
,
391 struct dns_res_rec
**nsrecs
, uint16_t *nscount
,
392 struct dns_res_rec
**additional
, uint16_t *arcount
)
394 uint16_t num_answers
=0;
395 struct dns_res_rec
*ans
=NULL
;
399 ans
= talloc_array(mem_ctx
, struct dns_res_rec
, 0);
400 if (answers
== NULL
) return NT_STATUS_NO_MEMORY
;
402 for (i
= 0; i
< in
->qdcount
; ++i
) {
403 status
= handle_question(dns
, mem_ctx
, &in
->questions
[i
], &ans
, &num_answers
);
404 NT_STATUS_NOT_OK_RETURN(status
);
408 *ancount
= num_answers
;
410 /*FIXME: Do something for these */
420 static NTSTATUS
dns_server_process_update(struct dns_server
*dns
,
422 struct dns_name_packet
*in
,
423 struct dns_res_rec
**prereqs
, uint16_t *prereq_count
,
424 struct dns_res_rec
**updates
, uint16_t *update_count
,
425 struct dns_res_rec
**additional
, uint16_t *arcount
)
427 struct dns_name_question
*zone
;
429 if (in
->qdcount
!= 1) {
430 return NT_STATUS_NOT_IMPLEMENTED
;
433 zone
= in
->questions
;
435 DEBUG(0, ("Got a dns update request.\n"));
438 return NT_STATUS_NOT_IMPLEMENTED
;
441 static NTSTATUS
dns_process(struct dns_server
*dns
,
446 enum ndr_err_code ndr_err
;
448 struct dns_name_packet
*in_packet
;
449 struct dns_name_packet
*out_packet
;
450 struct dns_res_rec
*answers
= NULL
, *nsrecs
= NULL
, *additional
= NULL
;
451 uint16_t num_answers
= 0 , num_nsrecs
= 0, num_additional
= 0;
454 if (in
->length
< 12) {
455 return NT_STATUS_INVALID_PARAMETER
;
458 in_packet
= talloc_zero(mem_ctx
, struct dns_name_packet
);
459 /* TODO: We don't really need an out_packet. */
460 out_packet
= talloc_zero(mem_ctx
, struct dns_name_packet
);
462 if (in_packet
== NULL
) return NT_STATUS_NO_MEMORY
;
463 if (out_packet
== NULL
) return NT_STATUS_NO_MEMORY
;
465 dump_data(2, in
->data
, in
->length
);
467 ndr_err
= ndr_pull_struct_blob(in
, in_packet
, in_packet
,
468 (ndr_pull_flags_fn_t
)ndr_pull_dns_name_packet
);
469 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
470 TALLOC_FREE(in_packet
);
471 DEBUG(0, ("Failed to parse packet %d!\n", ndr_err
));
474 out
->data
[2] |= 0x80; /* Toggle DNS_FLAG_REPLY */
475 out
->data
[3] |= DNS_RCODE_FORMERR
;
480 NDR_PRINT_DEBUG(dns_name_packet
, in_packet
);
481 *out_packet
= *in_packet
;
482 out_packet
->operation
|= DNS_FLAG_REPLY
;
484 switch (in_packet
->operation
& DNS_OPCODE
) {
485 case DNS_OPCODE_QUERY
:
487 ret
= dns_server_process_query(dns
, out_packet
, in_packet
,
488 &answers
, &num_answers
,
489 &nsrecs
, &num_nsrecs
,
490 &additional
, &num_additional
);
491 reply_code
= DNS_RCODE_NXDOMAIN
;
494 case DNS_OPCODE_REGISTER
:
495 ret
= dns_server_process_update(dns
, out_packet
, in_packet
,
496 &answers
, &num_answers
,
497 &nsrecs
, &num_nsrecs
,
498 &additional
, &num_additional
);
499 reply_code
= ntstatus_to_dns_err(ret
);
502 ret
= NT_STATUS_NOT_IMPLEMENTED
;
503 reply_code
= DNS_RCODE_NOTIMP
;
507 if (NT_STATUS_IS_OK(ret
)) {
508 out_packet
->ancount
= num_answers
;
509 out_packet
->answers
= answers
;
511 out_packet
->nscount
= num_nsrecs
;
512 out_packet
->nsrecs
= nsrecs
;
514 out_packet
->arcount
= num_additional
;
515 out_packet
->additional
= additional
;
517 out_packet
->operation
|= reply_code
;
520 NDR_PRINT_DEBUG(dns_name_packet
, out_packet
);
521 ndr_err
= ndr_push_struct_blob(out
, out_packet
, out_packet
,
522 (ndr_push_flags_fn_t
)ndr_push_dns_name_packet
);
523 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
524 TALLOC_FREE(in_packet
);
525 TALLOC_FREE(out_packet
);
526 DEBUG(0, ("Failed to push packet %d!\n", ndr_err
));
529 out
->data
[2] |= 0x80; /* Toggle DNS_FLAG_REPLY */
530 out
->data
[3] |= DNS_RCODE_SERVFAIL
;
535 dump_data(2, out
->data
, out
->length
);
539 struct dns_tcp_call
{
540 struct dns_tcp_connection
*dns_conn
;
544 struct iovec out_iov
[2];
547 static void dns_tcp_call_writev_done(struct tevent_req
*subreq
);
549 static void dns_tcp_call_loop(struct tevent_req
*subreq
)
551 struct dns_tcp_connection
*dns_conn
= tevent_req_callback_data(subreq
,
552 struct dns_tcp_connection
);
553 struct dns_tcp_call
*call
;
556 call
= talloc(dns_conn
, struct dns_tcp_call
);
558 dns_tcp_terminate_connection(dns_conn
, "dns_tcp_call_loop: "
559 "no memory for dns_tcp_call");
562 call
->dns_conn
= dns_conn
;
564 status
= tstream_read_pdu_blob_recv(subreq
,
568 if (!NT_STATUS_IS_OK(status
)) {
571 reason
= talloc_asprintf(call
, "dns_tcp_call_loop: "
572 "tstream_read_pdu_blob_recv() - %s",
575 reason
= nt_errstr(status
);
578 dns_tcp_terminate_connection(dns_conn
, reason
);
582 DEBUG(10,("Received krb5 TCP packet of length %lu from %s\n",
583 (long) call
->in
.length
,
584 tsocket_address_string(dns_conn
->conn
->remote_address
, call
)));
586 /* skip length header */
588 call
->in
.length
-= 4;
591 status
= dns_process(dns_conn
->dns_socket
->dns
, call
, &call
->in
, &call
->out
);
592 if (!NT_STATUS_IS_OK(status
)) {
593 DEBUG(0, ("dns_process returned %s\n", nt_errstr(status
)));
594 dns_tcp_terminate_connection(dns_conn
,
595 "dns_tcp_call_loop: process function failed");
599 /* First add the length of the out buffer */
600 RSIVAL(call
->out_hdr
, 0, call
->out
.length
);
601 call
->out_iov
[0].iov_base
= (char *) call
->out_hdr
;
602 call
->out_iov
[0].iov_len
= 4;
604 call
->out_iov
[1].iov_base
= (char *) call
->out
.data
;
605 call
->out_iov
[1].iov_len
= call
->out
.length
;
607 subreq
= tstream_writev_queue_send(call
,
608 dns_conn
->conn
->event
.ctx
,
610 dns_conn
->send_queue
,
612 if (subreq
== NULL
) {
613 dns_tcp_terminate_connection(dns_conn
, "dns_tcp_call_loop: "
614 "no memory for tstream_writev_queue_send");
617 tevent_req_set_callback(subreq
, dns_tcp_call_writev_done
, call
);
620 * The krb5 tcp pdu's has the length as 4 byte (initial_read_size),
621 * packet_full_request_u32 provides the pdu length then.
623 subreq
= tstream_read_pdu_blob_send(dns_conn
,
624 dns_conn
->conn
->event
.ctx
,
626 4, /* initial_read_size */
627 packet_full_request_u32
,
629 if (subreq
== NULL
) {
630 dns_tcp_terminate_connection(dns_conn
, "dns_tcp_call_loop: "
631 "no memory for tstream_read_pdu_blob_send");
634 tevent_req_set_callback(subreq
, dns_tcp_call_loop
, dns_conn
);
637 static void dns_tcp_call_writev_done(struct tevent_req
*subreq
)
639 struct dns_tcp_call
*call
= tevent_req_callback_data(subreq
,
640 struct dns_tcp_call
);
644 rc
= tstream_writev_queue_recv(subreq
, &sys_errno
);
649 reason
= talloc_asprintf(call
, "dns_tcp_call_writev_done: "
650 "tstream_writev_queue_recv() - %d:%s",
651 sys_errno
, strerror(sys_errno
));
653 reason
= "dns_tcp_call_writev_done: tstream_writev_queue_recv() failed";
656 dns_tcp_terminate_connection(call
->dns_conn
, reason
);
660 /* We don't care about errors */
666 called when we get a new connection
668 static void dns_tcp_accept(struct stream_connection
*conn
)
670 struct dns_socket
*dns_socket
;
671 struct dns_tcp_connection
*dns_conn
;
672 struct tevent_req
*subreq
;
675 dns_conn
= talloc_zero(conn
, struct dns_tcp_connection
);
676 if (dns_conn
== NULL
) {
677 stream_terminate_connection(conn
,
678 "dns_tcp_accept: out of memory");
682 dns_conn
->send_queue
= tevent_queue_create(conn
, "dns_tcp_accept");
683 if (dns_conn
->send_queue
== NULL
) {
684 stream_terminate_connection(conn
,
685 "dns_tcp_accept: out of memory");
689 dns_socket
= talloc_get_type(conn
->private_data
, struct dns_socket
);
691 TALLOC_FREE(conn
->event
.fde
);
693 rc
= tstream_bsd_existing_socket(dns_conn
,
694 socket_get_fd(conn
->socket
),
697 stream_terminate_connection(conn
,
698 "dns_tcp_accept: out of memory");
702 dns_conn
->conn
= conn
;
703 dns_conn
->dns_socket
= dns_socket
;
704 conn
->private_data
= dns_conn
;
707 * The krb5 tcp pdu's has the length as 4 byte (initial_read_size),
708 * packet_full_request_u32 provides the pdu length then.
710 subreq
= tstream_read_pdu_blob_send(dns_conn
,
711 dns_conn
->conn
->event
.ctx
,
713 4, /* initial_read_size */
714 packet_full_request_u32
,
716 if (subreq
== NULL
) {
717 dns_tcp_terminate_connection(dns_conn
, "dns_tcp_accept: "
718 "no memory for tstream_read_pdu_blob_send");
721 tevent_req_set_callback(subreq
, dns_tcp_call_loop
, dns_conn
);
724 static const struct stream_server_ops dns_tcp_stream_ops
= {
726 .accept_connection
= dns_tcp_accept
,
727 .recv_handler
= dns_tcp_recv
,
728 .send_handler
= dns_tcp_send
731 struct dns_udp_call
{
732 struct tsocket_address
*src
;
737 static void dns_udp_call_sendto_done(struct tevent_req
*subreq
);
739 static void dns_udp_call_loop(struct tevent_req
*subreq
)
741 struct dns_udp_socket
*sock
= tevent_req_callback_data(subreq
,
742 struct dns_udp_socket
);
743 struct dns_udp_call
*call
;
749 call
= talloc(sock
, struct dns_udp_call
);
755 len
= tdgram_recvfrom_recv(subreq
, &sys_errno
,
756 call
, &buf
, &call
->src
);
764 call
->in
.length
= len
;
766 DEBUG(10,("Received krb5 UDP packet of length %lu from %s\n",
767 (long)call
->in
.length
,
768 tsocket_address_string(call
->src
, call
)));
771 status
= dns_process(sock
->dns_socket
->dns
, call
, &call
->in
, &call
->out
);
772 if (!NT_STATUS_IS_OK(status
)) {
774 DEBUG(0, ("dns_process returned %s\n", nt_errstr(status
)));
778 subreq
= tdgram_sendto_queue_send(call
,
779 sock
->dns_socket
->dns
->task
->event_ctx
,
785 if (subreq
== NULL
) {
789 tevent_req_set_callback(subreq
, dns_udp_call_sendto_done
, call
);
792 subreq
= tdgram_recvfrom_send(sock
,
793 sock
->dns_socket
->dns
->task
->event_ctx
,
795 if (subreq
== NULL
) {
796 task_server_terminate(sock
->dns_socket
->dns
->task
,
797 "no memory for tdgram_recvfrom_send",
801 tevent_req_set_callback(subreq
, dns_udp_call_loop
, sock
);
804 static void dns_udp_call_sendto_done(struct tevent_req
*subreq
)
806 struct dns_udp_call
*call
= tevent_req_callback_data(subreq
,
807 struct dns_udp_call
);
811 ret
= tdgram_sendto_queue_recv(subreq
, &sys_errno
);
813 /* We don't care about errors */
819 start listening on the given address
821 static NTSTATUS
dns_add_socket(struct dns_server
*dns
,
822 const struct model_ops
*model_ops
,
827 struct dns_socket
*dns_socket
;
828 struct dns_udp_socket
*dns_udp_socket
;
829 struct tevent_req
*udpsubreq
;
833 dns_socket
= talloc(dns
, struct dns_socket
);
834 NT_STATUS_HAVE_NO_MEMORY(dns_socket
);
836 dns_socket
->dns
= dns
;
838 ret
= tsocket_address_inet_from_strings(dns_socket
, "ip",
840 &dns_socket
->local_address
);
842 status
= map_nt_error_from_unix(errno
);
846 status
= stream_setup_socket(dns
->task
->event_ctx
,
850 "ip", address
, &port
,
851 lpcfg_socket_options(dns
->task
->lp_ctx
),
853 if (!NT_STATUS_IS_OK(status
)) {
854 DEBUG(0,("Failed to bind to %s:%u TCP - %s\n",
855 address
, port
, nt_errstr(status
)));
856 talloc_free(dns_socket
);
860 dns_udp_socket
= talloc(dns_socket
, struct dns_udp_socket
);
861 NT_STATUS_HAVE_NO_MEMORY(dns_udp_socket
);
863 dns_udp_socket
->dns_socket
= dns_socket
;
865 ret
= tdgram_inet_udp_socket(dns_socket
->local_address
,
868 &dns_udp_socket
->dgram
);
870 status
= map_nt_error_from_unix(errno
);
871 DEBUG(0,("Failed to bind to %s:%u UDP - %s\n",
872 address
, port
, nt_errstr(status
)));
876 dns_udp_socket
->send_queue
= tevent_queue_create(dns_udp_socket
,
877 "dns_udp_send_queue");
878 NT_STATUS_HAVE_NO_MEMORY(dns_udp_socket
->send_queue
);
880 udpsubreq
= tdgram_recvfrom_send(dns_udp_socket
,
881 dns
->task
->event_ctx
,
882 dns_udp_socket
->dgram
);
883 NT_STATUS_HAVE_NO_MEMORY(udpsubreq
);
884 tevent_req_set_callback(udpsubreq
, dns_udp_call_loop
, dns_udp_socket
);
890 setup our listening sockets on the configured network interfaces
892 static NTSTATUS
dns_startup_interfaces(struct dns_server
*dns
, struct loadparm_context
*lp_ctx
,
893 struct interface
*ifaces
)
895 const struct model_ops
*model_ops
;
897 TALLOC_CTX
*tmp_ctx
= talloc_new(dns
);
901 /* within the dns task we want to be a single process, so
902 ask for the single process model ops and pass these to the
903 stream_setup_socket() call. */
904 model_ops
= process_model_startup(dns
->task
->event_ctx
, "single");
906 DEBUG(0,("Can't find 'single' process model_ops\n"));
907 return NT_STATUS_INTERNAL_ERROR
;
910 num_interfaces
= iface_count(ifaces
);
912 for (i
=0; i
<num_interfaces
; i
++) {
913 const char *address
= talloc_strdup(tmp_ctx
, iface_n_ip(ifaces
, i
));
915 status
= dns_add_socket(dns
, model_ops
, "dns", address
, DNS_SERVICE_PORT
);
916 NT_STATUS_NOT_OK_RETURN(status
);
919 talloc_free(tmp_ctx
);
924 static int dns_server_sort_zones(struct ldb_message
**m1
, struct ldb_message
**m2
)
929 n1
= ldb_msg_find_attr_as_string(*m1
, "name", NULL
);
930 n2
= ldb_msg_find_attr_as_string(*m2
, "name", NULL
);
935 /* If the string lengths are not equal just sort by length */
937 /* If m1 is the larger zone name, return it first */
941 /*TODO: We need to compare DNs here, we want the DomainDNSZones first */
945 static void dns_task_init(struct task_server
*task
)
947 struct dns_server
*dns
;
949 struct interface
*ifaces
;
951 struct ldb_result
*res
;
952 struct ldb_dn
*rootdn
;
953 static const char * const attrs
[] = { "name", NULL
};
957 switch (lpcfg_server_role(task
->lp_ctx
)) {
958 case ROLE_STANDALONE
:
959 task_server_terminate(task
, "dns: no DNS required in standalone configuration", false);
961 case ROLE_DOMAIN_MEMBER
:
962 task_server_terminate(task
, "dns: no DNS required in member server configuration", false);
964 case ROLE_DOMAIN_CONTROLLER
:
965 /* Yes, we want a DNS */
969 load_interfaces(task
, lpcfg_interfaces(task
->lp_ctx
), &ifaces
);
971 if (iface_count(ifaces
) == 0) {
972 task_server_terminate(task
, "dns: no network interfaces configured", false);
976 task_server_set_title(task
, "task[dns]");
978 dns
= talloc_zero(task
, struct dns_server
);
980 task_server_terminate(task
, "dns: out of memory", true);
986 dns
->samdb
= samdb_connect(dns
, dns
->task
->event_ctx
, dns
->task
->lp_ctx
,
987 system_session(dns
->task
->lp_ctx
), 0);
989 task_server_terminate(task
, "dns: samdb_connect failed", true);
993 rootdn
= ldb_dn_new(dns
, dns
->samdb
, "");
994 if (rootdn
== NULL
) {
995 task_server_terminate(task
, "dns: out of memory", true);
999 // TODO: this search does not work against windows
1000 ret
= dsdb_search(dns
->samdb
, dns
, &res
, rootdn
, LDB_SCOPE_SUBTREE
,
1001 attrs
, DSDB_SEARCH_SEARCH_ALL_PARTITIONS
, "(objectClass=dnsZone)");
1002 if (ret
!= LDB_SUCCESS
) {
1003 task_server_terminate(task
,
1004 "dns: failed to look up root DNS zones",
1009 TYPESAFE_QSORT(res
->msgs
, res
->count
, dns_server_sort_zones
);
1011 for (i
=0; i
< res
->count
; i
++) {
1012 struct dns_server_zone
*z
;
1014 z
= talloc_zero(dns
, struct dns_server_zone
);
1018 z
->name
= ldb_msg_find_attr_as_string(res
->msgs
[i
], "name", NULL
);
1019 z
->dn
= talloc_move(z
, &res
->msgs
[i
]->dn
);
1021 DLIST_ADD_END(dns
->zones
, z
, NULL
);
1024 status
= dns_startup_interfaces(dns
, task
->lp_ctx
, ifaces
);
1025 if (!NT_STATUS_IS_OK(status
)) {
1026 task_server_terminate(task
, "dns failed to setup interfaces", true);
1031 NTSTATUS
server_service_dns_init(void)
1033 return register_server_service("dns", dns_task_init
);