3 # Unix SMB/CIFS implementation.
4 # Copyright (C) Volker Lendecke 2017
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
25 from samba.dcerpc import dns
26 import samba.ndr as ndr
28 if sys.version_info[0] < 3:
30 sserver = SocketServer
33 sserver = socketserver
36 class DnsHandler(sserver.BaseRequestHandler):
37 def dns_transaction_udp(self, packet, host):
38 "send a DNS query and read the reply"
41 send_packet = ndr.ndr_pack(packet)
42 s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, 0)
45 s.sendall(send_packet, 0)
46 recv_packet = s.recv(2048, 0)
47 return ndr.ndr_unpack(dns.name_packet, recv_packet)
48 except socket.error as err:
49 print("Error sending to host %s for name %s: %s\n" %
50 (host, packet.questions[0].name, err.errno))
57 def forwarder(self, name):
60 if lname.endswith('an-address-that-will-not-resolve'):
62 if lname.endswith('dsfsdfs'):
64 if lname.endswith('adnonssdom.samba.example.com'):
66 if lname.endswith('adnontlmdom.samba.example.com'):
68 if lname.endswith('samba2000.example.com'):
70 if lname.endswith('samba2003.example.com'):
72 if lname.endswith('samba2008r2.example.com'):
74 if lname.endswith('addom.samba.example.com'):
76 if lname.endswith('sub.samba.example.com'):
78 if lname.endswith('chgdcpassword.samba.example.com'):
80 if lname.endswith('backupdom.samba.example.com'):
82 if lname.endswith('renamedom.samba.example.com'):
84 if lname.endswith('labdom.samba.example.com'):
86 if lname.endswith('samba.example.com'):
91 data, sock = self.request
92 query = ndr.ndr_unpack(dns.name_packet, data)
93 name = query.questions[0].name
94 forwarder = self.forwarder(name)
97 if forwarder is 'ignore':
99 elif forwarder is 'fail':
101 elif forwarder is not None:
102 response = self.dns_transaction_udp(query, forwarder)
105 response.operation |= dns.DNS_FLAG_REPLY
106 response.operation |= dns.DNS_FLAG_RECURSION_AVAIL
107 response.operation |= dns.DNS_RCODE_NXDOMAIN
111 response.operation |= dns.DNS_FLAG_REPLY
112 response.operation |= dns.DNS_FLAG_RECURSION_AVAIL
113 response.operation |= dns.DNS_RCODE_SERVFAIL
115 send_packet = ndr.ndr_pack(response)
117 print("dns_hub: sending %s to address %s for name %s\n" %
118 (forwarder, self.client_address, name))
121 sock.sendto(send_packet, self.client_address)
122 except socket.error as err:
123 print("Error sending %s to address %s for name %s: %s\n" %
124 (forwarder, self.client_address, name, err))
127 class server_thread(threading.Thread):
128 def __init__(self, server):
129 threading.Thread.__init__(self)
133 self.server.serve_forever()
134 print("dns_hub: after serve_forever()")
138 timeout = int(sys.argv[1]) * 1000
139 timeout = min(timeout, 2**31 - 1) # poll with 32-bit int can't take more
141 server = sserver.UDPServer((host, int(53)), DnsHandler)
142 t = server_thread(server)
145 stdin = sys.stdin.fileno()
146 p.register(stdin, select.POLLIN)
148 print("dns_hub: after poll()")
151 print("dns_hub: before exit()")