def _dns_resolve(query, resolver=""): dr = dns.resolver.Resolver() if resolver: try: dr.nameservers = [socket.gethostbyname(resolver)] except: pass result = {"query": query, "resolver": dr.nameservers} rr = ["A", "AAAA", "CNAME", "MX", "NS", "SOA", "TXT"] #rr = ["A", "AAAA", "CNAME", "MX", "NS", "TXT"] for r in rr: rdata = [] try: answer = dr.query(query, r) for a in answer: if not str(a) in rdata: rdata.append(str(a)) except Exception as e: #logger.debug(str(e)) pass if rdata: rdata.sort() result[r] = rdata result["AXFR"] = None try: ns = result["NS"][-1] xfr = dns.query.xfr(ns, query) zone = dns.zone.from_xfr(xfr) result["AXFR"] = zone.to_text() except Exception as e: result["AXFR"] = str(e) return result
def run(self, domain, record_type=None, name=None, nameserver=None, content=None, tags=None, tags_and=None ): response = self.getAPI("/api/1.0/dns/records/", { "domain": domain, "type": record_type, "name": name, "nameserver": nameserver, "content": content, "tags": tags, "tags_and": tags_and }) result = "" if len(response["records"]) > 0: zone = dns.zone.Zone(dns.name.from_text( response["records"][0]["dns_zone"] )) for record in response["records"]: try: rdtype = dns.rdatatype.from_text(record["type"]) rdata = dns.rdata.from_text( dns.rdataclass.IN, rdtype, record["content"] ) n = zone.get_rdataset(record["name"], rdtype, create=True) n.add(rdata, record["ttl"]) except Exception, e: print "Error: %s" % e.message print "Record Name: %s" % record["name"] print "Record Type: %s" % record["type"] print "Record Content: %s" % record["content"] print "Record TTL: %d" % record["ttl"] print "---" result = zone.to_text()
def _dns_resolve(query, resolver=""): dr = dns.resolver.Resolver() if resolver: try: dr.nameservers = [socket.gethostbyname(resolver)] except: pass result = {"query":query, "resolver":dr.nameservers} rr = ["A", "AAAA", "CNAME", "MX", "NS", "SOA", "TXT"] #rr = ["A", "AAAA", "CNAME", "MX", "NS", "TXT"] for r in rr: rdata = [] try: answer = dr.query(query, r) for a in answer: if not str(a) in rdata: rdata.append(str(a)) except Exception as e: #logger.debug(str(e)) pass if rdata: rdata.sort() result[r] = rdata result["AXFR"] = None try: ns = result["NS"][-1] xfr = dns.query.xfr(ns, query) zone = dns.zone.from_xfr(xfr) result["AXFR"] = zone.to_text() except Exception as e: result["AXFR"] = str(e) return result
def test_zone_basic(zone): with zone.writer() as txn: txn.delete(dns.name.from_text("bar.foo", None)) rd = dns.rdata.from_text("in", "ns", "ns3") txn.add(dns.name.empty, 3600, rd) rd = dns.rdata.from_text("in", "a", "10.0.0.3") txn.add(dns.name.from_text("ns3", None), 3600, rd) output = zone.to_text() assert output == example_text_output
def test_zone_basic(zone): with zone.writer() as txn: txn.delete(dns.name.from_text('bar.foo', None)) rd = dns.rdata.from_text('in', 'ns', 'ns3') txn.add(dns.name.empty, 3600, rd) rd = dns.rdata.from_text('in', 'a', '10.0.0.3') txn.add(dns.name.from_text('ns3', None), 3600, rd) output = zone.to_text() assert output == example_text_output
def run(self): zone = dns.zone.from_xfr(dns.query.xfr(**self.config["query"]), **self.config["zone"]) if self.save: if not os.isdir("./dns-zones"): os.mkdir("./dns-zones/") zone.to_file(f"./dns-zones/{self.name}.txt", sorted=True) else: print(colored(zone.to_text().decode("ascii"), dark=True))
def test_zone_get_deleted(zone): with zone.writer() as txn: print(zone.to_text()) ns1 = dns.name.from_text('ns1', None) assert txn.get(ns1, dns.rdatatype.A) is not None txn.delete(ns1) assert txn.get(ns1, dns.rdatatype.A) is None ns2 = dns.name.from_text('ns2', None) txn.delete(ns2, dns.rdatatype.A) assert txn.get(ns2, dns.rdatatype.A) is None
def handle(self, *args, **options): for domain_list in options['domain']: try: domain = Domain.objects.get(name=domain_list) get_nameserver1 = domain.nameserver1 get_nameserver2 = domain.nameserver2 get_records = Record.objects.filter(domain=domain) except Domain.DoesNotExist: raise CommandError('Domain "%s" does not exist' % domain) except Record.DoesNotExist: raise CommandError('Records for "%s" do not exist' % domain) if domain and get_records: self.stdout.write('Domain "%s"' % domain) self.stdout.write('Records for zone: \n') for record in get_records: self.stdout.write("%s\n" % record) # create empty zone zone = dns.zone.Zone(origin=domain.name) nameservers = {'ns': [domain.nameserver1, domain.nameserver2]} # add records for record in get_records: if "@" not in record.name: fqdn = "{0}.{1}".format(record.name, domain) else: fqdn = record record_type = getattr( dns.rdatatype, "%s" % record.record_type ) # Directly use built in rdatatypes instead of looking up against list zone[fqdn] = dns.rdataset.from_text( 'IN', record_type, record.ttl, str(record.content)) zone_template = loader.get_template('bind/template.zone') # zone_text = zone.to_text('{0}.zone'.format(domain.name)) zone_text = zone.to_text() context = { "domain": domain, "ns": {domain.nameserver1, domain.nameserver2}, "records": zone_text } fp = "{0}.zone".format(domain.name) with open(fp, 'w+') as f: f.seek(0) f.write(zone_template.render(context))
async def start_powerdns(nursery, parent: Thread, path: Path, zone: dns.zone.Zone, # tuple is (udpfd, listening tcpfd) ipv4_sockets: t.List[t.Tuple[handle.FileDescriptor, handle.FileDescriptor]], ipv6_sockets: t.List[t.Tuple[handle.FileDescriptor, handle.FileDescriptor]], ) -> Powerdns: pdns_server = Command(Path("/home/sbaugh/.local/src/pdns/pdns/pdns_server"), ['pdns_server'], {}) # pdns_server = await parent.environ.which("pdns_server") thread = await parent.fork() # we pretend to pass addresses like 0.0.0.1 etc # we add one so we don't pass 0.0.0.0 and make powerdns think it's bound to everything ipv4s = {str(i+1): (udp.move(thread.task), tcp.move(thread.task)) for i, (udp, tcp) in enumerate(ipv4_sockets)} ipv6s = {str(i+1): (udp.move(thread.task), tcp.move(thread.task)) for i, (udp, tcp) in enumerate(ipv6_sockets)} await thread.unshare_files(going_to_exec=True) config = { "config-dir": os.fsdecode(path), # TODO control-console seems to be a feature where it will listen on stdin or something? # we should use that instead of this socketdir "socket-dir": os.fsdecode(path), # more logging "loglevel": "9", "log-dns-queries": "yes", # backend "launch": "bind", "bind-config": os.fsdecode(await thread.spit(path/"named.conf", 'zone "%s" { file "%s"; };' % ( zone.origin.to_text(), os.fsdecode(await thread.spit(path/"zone", zone.to_text()))))), "enable-lua-records": "yes", # relevant stuff "local-address": ",".join(ipv4s), "local-address-udp-fds": ",".join([f"{i}={await fd.as_argument()}" for i, (fd, _) in ipv4s.items()]), "local-address-tcp-fds": ",".join([f"{i}={await fd.as_argument()}" for i, (_, fd) in ipv4s.items()]), "local-ipv6": ",".join(ipv6s), "local-ipv6-udp-fds": ",".join([f"{i}={await fd.as_argument()}" for i, (fd, _) in ipv6s.items()]), "local-ipv6-tcp-fds": ",".join([f"{i}={await fd.as_argument()}" for i, (_, fd) in ipv6s.items()]), } print(config['local-address-udp-fds']) child = await thread.exec(pdns_server.args(*[f"--{name}={value}" for name, value in config.items()])) nursery.start_soon(child.check) return Powerdns()