Skip to content

Commit

Permalink
Include $ORIGIN in file output
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Castle committed Jun 24, 2015
1 parent 190c55d commit 54ea326
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
21 changes: 14 additions & 7 deletions rimudns/rimudns.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,26 +190,33 @@ def list_records(self, zone_name, all_records=False):
if str(root.is_ok).startswith('OK'):
for record in root.actions.action.iterchildren():
record_type = record.attrib['type']
if self.expect_absolute and record_type != 'SOA':
# Zonomi returns absolute path without period
for key in ['name', 'content']:
if record.attrib.has_key(key) and self.is_valid_hostname(record.attrib[key]):
record.set(key, record.attrib[key] + '.')
self.add_trailing_periods(record_type, record)

if not records.has_key(record_type): records[record_type] = []
records[record_type].append(record.attrib)
return records
except Exception, e:
raise e


# Zonomi returns absolute path without period
def add_trailing_periods(self, record_type, record):
if not self.expect_absolute or record_type == 'SOA':
return

record.set('name', record.attrib['name'] + '.')

if self.is_valid_hostname(record.attrib['content']):
record.set('content', record.attrib['content'] + '.')

def is_valid_hostname(self, hostname):
import re
if len(hostname) > 255:
return False
if hostname[-1] == ".":
hostname = hostname[:-1] # strip exactly one dot from the right, if present
allowed = re.compile("(?!-)[A-Z\d-]{1,63}(?<!-)$", re.IGNORECASE)
return all(allowed.match(x) for x in hostname.split("."))
digits = re.compile("^[\d]{0,3}$")
return all(allowed.match(x) for x in hostname.split(".")) and not all(digits.match(x) for x in hostname.split("."))

def set_record(self, host, value, record_type='A', prio=None, ttl=None):
'''Set an IP Address (A) record
Expand Down
10 changes: 5 additions & 5 deletions rimudns/zonehandle.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import dns.rdtypes.IN.A
import dns.rdtypes.IN
import re
import sys

class ZoneHandle:
IMPORT_AXFR = 1
Expand Down Expand Up @@ -100,14 +101,12 @@ def from_records_dict(self, records_dict):
if record['content']==self.domain_name:
record['content'] = '@'
zone_text += '%s %s IN CNAME %s\n' % (record['name'], ttl, record['content'])

if self.debug: print zone_text

self.zone = dns.zone.from_text(zone_text)
return self.zone

except Exception, e:
print e
if self.debug: print e
raise e

Expand Down Expand Up @@ -279,7 +278,8 @@ def _guess_txt(self):

def to_file(self, file, sorted=True, relativize=True):
try:
self.zone.to_file(file, sorted, relativize)
file.write("$ORIGIN " + self.domain_name + ".\n")
file.write(self.zone.to_text(sorted, relativize))
return True
except Exception, e:
raise e
raise e

0 comments on commit 54ea326

Please sign in to comment.