def testMakeISC(self): self.assertEqual(iscpy.MakeISC( {'level1': {'level2': {'level3': {'level4': { 'test1': True, 'test2': True, 'test3': True}}}}, 'newarg': 'newval', 'new_stanza': {'test': True}}), 'new_stanza { test; };\n' 'level1 { level2 { level3 { level4 { test1;\n' 'test3;\n' 'test2; }; }; }; };\n' 'newarg newval;') self.assertEqual(iscpy.MakeISC(iscpy.ParseISCString(self.named_file)), 'acl control-hosts { 127.0.0.1/32;\n' '192.168.1.3/32; };\n' 'acl admin { 192.168.1.2/32;\n' '192.168.1.4/32;\n' '192.168.0.0/16; };\n' 'view "authorized" { zone "smtp.university.edu" { masters { 192.168.11.37; };\n' 'type master;\n' 'file "test_data/test_zone.db"; };\n' 'allow-query-cache { network-authorized; };\n' 'allow-recursion { network-authorized; };\n' 'recursion yes;\n' 'zone "university.edu" { check-names ignore;\n' 'masters { 192.168.11.37; };\n' 'type slave;\n' 'file "test_data/university.db.bak"; };\n' 'match-clients { network-authorized; };\n' 'zone "." { type hint;\n' 'file "named.ca"; };\n' 'additional-from-cache yes;\n' 'additional-from-auth yes; };\n' 'controls { inet * allow { control-hosts; } keys { rndc-key; }; };\n' 'view "unauthorized" { zone "1.210.128.in-addr.arpa" { allow-query { network-unauthorized; };\n' 'type master;\n' 'file "test_data/test_reverse_zone.db"; };\n' 'recursion no;\n' 'match-clients { network-unauthorized; };\n' 'zone "." { type hint;\n' 'file "named.ca"; };\n' 'zone "0.0.127.in-addr.arpa" { masters { 192.168.1.3; };\n' 'type slave;\n' 'file "test_data/university.rev.bak"; };\n' 'additional-from-cache no;\n' 'additional-from-auth no; };\n' 'logging { category "update-security" { "security"; };\n' 'category "queries" { "query_logging"; };\n' 'channel "query_logging" { syslog local5;\n' 'severity info; };\n' 'category "client" { "null"; };\n' 'channel "security" { file "/var/log/named-security.log" versions 10 size 10m;\n' 'print-time yes; }; };\n' 'include "/etc/rndc.key";\n' 'options { directory "/var/domain";\n' 'recursion yes;\n' 'allow-query { any; };\n' 'max-cache-size 512M; };')
def testParse(self): self.assertEqual(iscpy.Explode(iscpy.ScrubComments(self.named_file)), [ 'include "/home/jcollins/roster-dns-management/test/test_data/rndc.key"', ';', 'options', '{', 'pid-file "test_data/named.pid"', ';', '}', ';', 'controls', '{', 'inet 127.0.0.1 port 35638 allow', '{', 'localhost', ';', '}', 'keys', '{', 'rndc-key', ';', '}', ';', '}', ';' ]) self.assertEqual( iscpy.ParseISCString(self.named_file), { 'include': '"/home/jcollins/roster-dns-management/test/test_data/rndc.key"', 'options': { 'pid-file': '"test_data/named.pid"' }, 'controls': [{ 'inet 127.0.0.1 port 35638 allow': { 'localhost': True } }, { 'keys': { 'rndc-key': True } }] }) self.assertEqual( iscpy.MakeISC(iscpy.ParseISCString(self.named_file)), 'include "/home/jcollins/roster-dns-management/test/test_data/rndc.key";\n' 'options { pid-file "test_data/named.pid"; };\n' 'controls { inet 127.0.0.1 port 35638 allow { localhost; } keys { rndc-key; }; };' )
def DumpNamedHeader(named_data): """This function dumps the named header from a named_data dict Inputs: named_data: named dict from MakeNamedDict Outputs: str: stirng of named header """ return iscpy.MakeISC(named_data['options'])
def MakeZoneViewOptions(named_data): """Makes zone and view data into strings to load into database. Inputs: named_data: named dict from MakeNamedDict Outputs: dict: dict with keys {'views': {}, 'zones': {}} """ options_dict = {'views': {}, 'zones': {}} for view in named_data['views']: options_dict['views'][view] = iscpy.MakeISC( named_data['views'][view]['options']) for zone in named_data['views'][view]['zones']: options_dict['zones'][zone] = iscpy.MakeISC( named_data['views'][view]['zones'][zone]['options']) for zone in named_data['orphan_zones']: options_dict['zones'][zone] = iscpy.MakeISC( named_data['orphan_zones'][zone]['options']) return options_dict
def NamedHeaderChangeDirectory(self, named_conf_header, new_directory): """Adds/Changes directory in named.conf header Inputs: named_conf_header: string of namedconf header new_directory: {} Outputs: string: string of namedconf header """ named_conf_header_contents = iscpy.ParseISCString(named_conf_header) if ('options' not in named_conf_header_contents): named_conf_header_contents['options'] = {} named_conf_header_contents['options'][ 'directory'] = '"%s"' % new_directory return iscpy.MakeISC(named_conf_header_contents)
def write_named(): named = {} f = open('/tmp/named.ipifier.conf', 'w') domains = Domain.objects.all() for domain in domains: named['zone "' + domain.name + '" IN'] = { 'type': domain.type, 'file': '"named.' + domain.name + '"', 'allow-update': { 'none': '' } } ipprefix = get_prefix() for prefix in ipprefix: named['zone "0.0.0.' + prefix + '" IN'] = { 'type': 'master', 'file': '"named.0.0.0.' + prefix + '"', 'allow-update': { 'none': '' } } f.write(iscpy.MakeISC(named)) f.close() if not os.path.exists('/tmp/pri'): os.makedirs('/tmp/pri') for prefix in ipprefix: try: f = open('/tmp/pri/named.0.0.0.' + prefix, 'r') i = 1 n = '00' lines = f.readlines() for line in lines: if i == 3: n = line[-2:] break i = i + 1 num = int(n) num = num + 1 if num < 10: n = '0' + str(num) else: n = str(num) if num == 100: n = '00' except IOError: n = '00' f = open('/tmp/pri/named.0.0.0.' + prefix, 'w') f.write('') f.close f = open('/tmp/pri/named.0.0.0.' + prefix, 'a') f.write('$TTL 1D\n@\tIN\tSOA\tbigwells.net root.bigwells.net. (\n') now = datetime.datetime.now() f.write('\t\t\t' + str(now.year) + str(now.month) + str(now.day) + n + '\n') f.write('\t\t\t8H\n\t\t\t2H\n\t\t\t4W\n\t\t\t1D )\n') f.write('\t\tNS\tns1.he.net.\n') f.write('\t\tNS\tns2.he.net.\n') records = Record.objects.all().filter(type='A').filter( content__startswith=prefix) for record in records: r = record.content.split('.') f.write(r[3] + '.' + r[2] + '.' + r[1] + '.' + r[0] + '.in-addr.arpa.\t\tPTR\t' + record.name) domainname = Domain.objects.get(id=record.domain_id.id) f.write('.' + domainname.name + '.\n') for domain in domains: try: f = open('/tmp/pri/named.' + domain.name, 'r') i = 1 n = '00' lines = f.readlines() for line in lines: if i == 3: n = line[-2:] break i = i + 1 num = int(n) num = num + 1 if num < 10: n = '0' + str(num) else: n = str(num) if num == 100: n = '00' except IOError: n = '00' f = open('/tmp/pri/named.' + domain.name, 'w') f.write('') f.close() f = open('/tmp/pri/named.' + domain.name, 'a') f.write('$TTL 1D\n@\tIN\tSOA\t' + domain.name + '. root.' + domain.name + '. (\n') now = datetime.datetime.now() f.write('\t\t\t' + str(now.year) + str(now.month) + str(now.day) + n + '\n') f.write('\t\t\t8H\n\t\t\t2H\n\t\t\t4W\n\t\t\t1D )\n') nameservers = Record.objects.all().filter( Q(domain_id=domain.id) & Q(type='NS')) for ns in nameservers: f.write('\t\tNS\t' + ns.content + '.\n') mailexchanges = Record.objects.all().filter( Q(domain_id=domain.id) & Q(type='MX')) for mx in mailexchanges: f.write('\t\tMX\t' + str(mx.pri) + ' ' + mx.content + '.\n') records = Record.objects.all().filter( Q(domain_id=domain.id) & Q(type='A')) for record in records: f.write(record.name) if record.name[-3:] == 'com' or record.name[ -3:] == 'org' or record.name[-3:] == 'net': f.write('.') f.write('\tA\t' + record.content + '\n') records = Record.objects.all().filter( Q(domain_id=domain.id) & Q(type='AAAA')) for record in records: f.write(record.name) if record.name[-3:] == 'com' or record.name[ -3:] == 'org' or record.name[-3:] == 'net': f.write('.') f.write('\tAAAA\t' + record.content + '\n') records = Record.objects.all().filter( Q(domain_id=domain.id) & Q(type='SRV')) for record in records: f.write(record.name + '\tSRV' + '\t0 ' + record.content) if not record.content[-1] == '.': f.write('.') f.write('\n') records = Record.objects.all().filter( Q(domain_id=domain.id) & Q(type='TXT')) for record in records: f.write(record.name + '\tIN TXT\t' + '"' + record.content + '"\n') records = Record.objects.all().filter( Q(domain_id=domain.id) & Q(type='CNAME')) for record in records: f.write(record.name + '\tCNAME\t' + '' + record.content) if record.content[-3:] == 'com' or record.content[ -3:] == 'org' or record.content[-3:] == 'net': f.write('.') f.write('\n') records = Record.objects.all().filter( Q(domain_id=domain.id) & Q(type='HINFO')) for record in records: f.write(record.name + '\tIN HINFO\t' + '"' + record.content + '"\n') records = Record.objects.all().filter( Q(domain_id=domain.id) & Q(type='NAPTR')) for record in records: f.write(record.name + '\tIN NAPTR\t' + '"' + record.content + '"\n') records = Record.objects.all().filter( Q(domain_id=domain.id) & Q(type='PTR')) for record in records: f.write(record.name + '\tIN PTR\t' + '"' + record.content + '"\n') records = Record.objects.all().filter( Q(domain_id=domain.id) & Q(type='SPF')) for record in records: f.write(record.name) if record.name[-3:] == 'com' or record.name[ -3:] == 'org' or record.name[-3:] == 'net': f.write('.') f.write('\tIN SPF\t' + record.content + '\n') records = Record.objects.all().filter( Q(domain_id=domain.id) & Q(type='SSHFP')) for record in records: f.write(record.name + '\tIN SSHFP\t' + '"' + record.content + '"\n') records = Record.objects.all().filter( Q(domain_id=domain.id) & Q(type='URL')) for record in records: f.write(record.name + '\tURL\t' + '"' + record.content + '"\n') records = Record.objects.all().filter( Q(domain_id=domain.id) & Q(type='MBOXFW')) for record in records: f.write(record.name + '\tMBOXFW\t' + '"' + record.content + '"\n') f.close()