1 # Unix SMB/CIFS implementation.
2 # Copyright (C) Catalyst.Net Ltd 2016
3 # Catalyst.Net's contribution was written by Douglas Bagnall
4 # <douglas.bagnall@catalyst.net.nz> and Garming Sam <garming@catalyst.net.nz>
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 # Based on the EchoServer example from python docs
20 from __future__ import print_function
24 from threading import Timer
25 from samba.dcerpc import dns
26 import samba.ndr as ndr
35 print("\033[00;36m%s\033[00m" % msg)
41 def answer_question(data, question):
43 r.name = question.name
44 r.rr_type = dns.DNS_QTYPE_CNAME
45 r.rr_class = dns.DNS_QCLASS_IN
52 class DnsHandler(SocketServer.BaseRequestHandler):
53 def make_answer(self, data):
54 data = ndr.ndr_unpack(dns.name_packet, data)
56 debug('answering this question:')
57 debug(data.__ndr_print__())
59 answer = answer_question(data, data.questions[0])
60 if answer is not None:
61 data.answers = [answer] * 1
63 debug('the answer was: ')
64 debug(data.__ndr_print__())
66 data.operation |= dns.DNS_FLAG_REPLY
68 return ndr.ndr_pack(data)
70 def really_handle(self, data, socket):
71 answer = self.make_answer(data)
72 socket.sendto(answer, self.client_address)
75 data, socket = self.request
76 debug("%s: %s wrote:" % (SERVER_ID, self.client_address[0]))
79 m = re.match('^timeout\s+([\d.]+)$', data.strip())
81 timeout = float(m.group(1))
82 debug("timing out at %s" % timeout)
85 t = Timer(timeout, self.really_handle, [data, socket])
90 host, port, SERVER_ID = sys.argv[1:]
91 server = SocketServer.UDPServer((host, int(port)), DnsHandler)
92 server.serve_forever()