From c7f67142c0693e73355e1a5748f729fad04415e0 Mon Sep 17 00:00:00 2001 From: Kai Blin Date: Tue, 6 Mar 2012 08:49:16 +0100 Subject: s4 dns: Fix TCP handling in the DNS server Autobuild-User: Kai Blin Autobuild-Date: Tue Mar 6 21:17:19 CET 2012 on sn-devel-104 --- source4/dns_server/dns_server.c | 26 +++++++++++----------- source4/lib/stream/packet.c | 12 ++++++++++ source4/lib/stream/packet.h | 1 + source4/scripting/python/samba/tests/dns.py | 34 +++++++++++++++++++++++++++++ 4 files changed, 60 insertions(+), 13 deletions(-) diff --git a/source4/dns_server/dns_server.c b/source4/dns_server/dns_server.c index 25873c2bf4..cf43590b63 100644 --- a/source4/dns_server/dns_server.c +++ b/source4/dns_server/dns_server.c @@ -233,13 +233,13 @@ static void dns_tcp_call_loop(struct tevent_req *subreq) return; } - DEBUG(10,("Received krb5 TCP packet of length %lu from %s\n", + DEBUG(10,("Received DNS TCP packet of length %lu from %s\n", (long) call->in.length, tsocket_address_string(dns_conn->conn->remote_address, call))); /* skip length header */ - call->in.data += 4; - call->in.length -= 4; + call->in.data += 2; + call->in.length -= 2; /* Call dns */ status = dns_process(dns_conn->dns_socket->dns, call, &call->in, &call->out); @@ -251,9 +251,9 @@ static void dns_tcp_call_loop(struct tevent_req *subreq) } /* First add the length of the out buffer */ - RSIVAL(call->out_hdr, 0, call->out.length); + RSSVAL(call->out_hdr, 0, call->out.length); call->out_iov[0].iov_base = (char *) call->out_hdr; - call->out_iov[0].iov_len = 4; + call->out_iov[0].iov_len = 2; call->out_iov[1].iov_base = (char *) call->out.data; call->out_iov[1].iov_len = call->out.length; @@ -271,14 +271,14 @@ static void dns_tcp_call_loop(struct tevent_req *subreq) tevent_req_set_callback(subreq, dns_tcp_call_writev_done, call); /* - * The krb5 tcp pdu's has the length as 4 byte (initial_read_size), - * packet_full_request_u32 provides the pdu length then. + * The dns tcp pdu's has the length as 2 byte (initial_read_size), + * packet_full_request_u16 provides the pdu length then. */ subreq = tstream_read_pdu_blob_send(dns_conn, dns_conn->conn->event.ctx, dns_conn->tstream, - 4, /* initial_read_size */ - packet_full_request_u32, + 2, /* initial_read_size */ + packet_full_request_u16, dns_conn); if (subreq == NULL) { dns_tcp_terminate_connection(dns_conn, "dns_tcp_call_loop: " @@ -358,14 +358,14 @@ static void dns_tcp_accept(struct stream_connection *conn) conn->private_data = dns_conn; /* - * The krb5 tcp pdu's has the length as 4 byte (initial_read_size), - * packet_full_request_u32 provides the pdu length then. + * The dns tcp pdu's has the length as 2 byte (initial_read_size), + * packet_full_request_u16 provides the pdu length then. */ subreq = tstream_read_pdu_blob_send(dns_conn, dns_conn->conn->event.ctx, dns_conn->tstream, - 4, /* initial_read_size */ - packet_full_request_u32, + 2, /* initial_read_size */ + packet_full_request_u16, dns_conn); if (subreq == NULL) { dns_tcp_terminate_connection(dns_conn, "dns_tcp_accept: " diff --git a/source4/lib/stream/packet.c b/source4/lib/stream/packet.c index 3a7f600560..b36d6507ef 100644 --- a/source4/lib/stream/packet.c +++ b/source4/lib/stream/packet.c @@ -612,3 +612,15 @@ _PUBLIC_ NTSTATUS packet_full_request_u32(void *private_data, DATA_BLOB blob, si } return NT_STATUS_OK; } + +_PUBLIC_ NTSTATUS packet_full_request_u16(void *private_data, DATA_BLOB blob, size_t *size) +{ + if (blob.length < 2) { + return STATUS_MORE_ENTRIES; + } + *size = 2 + RSVAL(blob.data, 0); + if (*size > blob.length) { + return STATUS_MORE_ENTRIES; + } + return NT_STATUS_OK; +} diff --git a/source4/lib/stream/packet.h b/source4/lib/stream/packet.h index a274bd397c..32c26686c3 100644 --- a/source4/lib/stream/packet.h +++ b/source4/lib/stream/packet.h @@ -61,5 +61,6 @@ void packet_queue_run(struct packet_context *pc); */ NTSTATUS packet_full_request_nbt(void *private_data, DATA_BLOB blob, size_t *size); NTSTATUS packet_full_request_u32(void *private_data, DATA_BLOB blob, size_t *size); +NTSTATUS packet_full_request_u16(void *private_data, DATA_BLOB blob, size_t *size); diff --git a/source4/scripting/python/samba/tests/dns.py b/source4/scripting/python/samba/tests/dns.py index ca9edbf500..26f8089822 100644 --- a/source4/scripting/python/samba/tests/dns.py +++ b/source4/scripting/python/samba/tests/dns.py @@ -99,6 +99,22 @@ class DNSTest(TestCase): if s is not None: s.close() + def dns_transaction_tcp(self, packet, host=os.getenv('DC_SERVER_IP')): + "send a DNS query and read the reply" + s = None + try: + send_packet = ndr.ndr_pack(packet) + s = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0) + s.connect((host, 53)) + tcp_packet = struct.pack('!H', len(send_packet)) + tcp_packet += send_packet + s.send(tcp_packet, 0) + recv_packet = s.recv(0xffff + 2, 0) + return ndr.ndr_unpack(dns.name_packet, recv_packet[2:]) + finally: + if s is not None: + s.close() + def test_one_a_query(self): "create a query packet containing one query record" p = self.make_name_packet(dns.DNS_OPCODE_QUERY) @@ -117,6 +133,24 @@ class DNSTest(TestCase): self.assertEquals(response.answers[0].rdata, os.getenv('DC_SERVER_IP')) + def test_one_a_query_tcp(self): + "create a query packet containing one query record via TCP" + p = self.make_name_packet(dns.DNS_OPCODE_QUERY) + questions = [] + + name = "%s.%s" % (os.getenv('DC_SERVER'), self.get_dns_domain()) + q = self.make_name_question(name, dns.DNS_QTYPE_A, dns.DNS_QCLASS_IN) + print "asking for ", q.name + questions.append(q) + + self.finish_name_packet(p, questions) + response = self.dns_transaction_tcp(p) + self.assert_dns_rcode_equals(response, dns.DNS_RCODE_OK) + self.assert_dns_opcode_equals(response, dns.DNS_OPCODE_QUERY) + self.assertEquals(response.ancount, 1) + self.assertEquals(response.answers[0].rdata, + os.getenv('DC_SERVER_IP')) + def test_two_queries(self): "create a query packet containing two query records" p = self.make_name_packet(dns.DNS_OPCODE_QUERY) -- cgit