Men & Mice Men & Mice Community
  [Search] Search   [Recent Topics] Recent Topics   [Members]  Member Listing   [Groups] Back to home page 
[Register] Register / 
[Login] Login 
Script to convert djb tinydns datafiles into RFC1035 zonefiles and BIND 9 configuration  XML
Forum Index -> Scripting and Customizing
Author Message
Carsten Strotmann
Men & Mice Staff
[Avatar]

Joined: 26/07/2007 13:08:39
Messages: 159
Location: Germany
Offline

This script is written in REXX (http://en.wikipedia.org/wiki/REXX)

Code:
 /*
 
   Syntax:
   tiny2bind <tinydns_datafile>
 
   converts djb tinydns datafiles into BIND 9 configuration
 
   Version 0.1, 2002.06.13, Carsten Strotmann (cas), Men & Mice
 
 */
 
 PARSE ARG datafile
 
 PARSE VERSION version
 scriptname = "tiny2bind"
 scriptversion = 0.1
 hostmaster = "hostmaster"
 
 SAY scriptname scriptversion ':' version
 
 masterzonefilepath = 'var/named/master'
 slavezonefilepath  = 'var/named/slave'
 namedmasters       = 'etc/named.conf.masters'
 namedslaves        = 'etc/named.conf.slaves'
 secondarynameservers = 'ns2.example.com'
 
 rc = Stream(datafile, 'C', 'OPEN READ')
 
 zonenum = 1
 zone.0 = zonenum
 
 i = 1
 
 DO WHILE Lines(datafile) > 0
   line.i = LineIn(datafile)
   i = i + 1
 END
 line.0 = i - 1
 
 DO i = 1 to line.0
   IF Left(line.i,1,1) = "." THEN
   DO
     PARSE VALUE line.i WITH '.'zone.zonenum.fqdn':'ipaddr':'zone.zonenum.ns.1':'reminder
     zname = zone.zonenum.fqdn
     IF zone.zname <> 1 THEN
     DO
       zone.zonenum.ns.0 = 1
       zone.zonenum.a.0  = 0
       zone.zonenum.mx.0 = 0
       zone.zonenum.cn.0 = 0
       zone.zname = 1
 
       DO u = 1 TO line.0
 
         /* find additional nameserver for domain */
         IF Left(line.u,1,1) = "." THEN
         DO
           PARSE VALUE line.u WITH '.'zfqdn':'ipaddr':'host':'reminder
 
           IF zfqdn == zname THEN
           DO
 
             f = 0
             DO xx = 1 to zone.zonenum.ns.0
               IF host = zone.zonenum.ns.xx THEN
               DO
                f = 1
               END
             END
 
             IF f = 0 THEN
             DO
               cnt = zone.zonenum.ns.0
               cnt = cnt + 1
               zone.zonenum.ns.cnt = host
               zone.zonenum.ns.0 = cnt
             END
           END
         END
 
         /* find mailroutes (MX) for domain */
 
         IF Left(line.u,1,1) = "@" THEN
         DO
           PARSE VALUE line.u WITH '@'zfqdn':'ipaddr':'host':'zpref':'reminder
           IF zfqdn == zname THEN
           DO
             f = 0
             DO xx = 1 to zone.zonenum.mx.0
               IF host = zone.zonenum.mx.xx THEN f = 1
             END
 
             IF f = 0 THEN
             DO
               cnt = zone.zonenum.mx.0
               cnt = cnt + 1
               zone.zonenum.mx.cnt = host
               zone.zonenum.mx.cnt.pref = zpref
               zone.zonenum.mx.0 = cnt
             END
           END
         END
 
 
         /* find Aliases (CNAMES) for domain */
 
         IF Left(line.u,1,1) = "C" THEN
         DO
           PARSE VALUE line.u WITH 'C'zfqhn':'zcname':'reminder
           PARSE VALUE zfqhn WITH host'.'zfqdn
 
           IF zfqdn == zname THEN
           DO
             f = 0
             DO xx = 1 to zone.zonenum.cn.0
               IF host = zone.zonenum.cn.xx THEN f = 1
             END
 
             IF f = 0 THEN
             DO
               cnt = zone.zonenum.cn.0
               cnt = cnt + 1
               zone.zonenum.cn.cnt = zfqhn
               zone.zonenum.cn.cnt.cname = zcname
 
               zone.zonenum.cn.0 = cnt
             END
           END
         END
 
 
         /* find Address Records (A) for domain */
 
         IF Left(line.u,1,1) = "=" THEN
         DO
           PARSE VALUE line.u WITH '='zfqhn':'ipaddr':'reminder
           PARSE VALUE zfqhn WITH host'.'zfqdn
 
           IF zfqdn == zname THEN
           DO
             f = 0
             DO xx = 1 to zone.zonenum.a.0
               IF host = zone.zonenum.a.xx THEN f = 1
             END
 
             IF f = 0 THEN
             DO
               cnt = zone.zonenum.a.0
               cnt = cnt + 1
               zone.zonenum.a.cnt = zfqhn
               zone.zonenum.a.cnt.ip = ipaddr
 
               zone.zonenum.a.0 = cnt
             END
           END
         END
 
       END
 
       zonenum = zonenum + 1
     END
   END
 END
 
 zone.0 =zonenum -1
 
 DO z = 1 to zone.0
 
   zonefile = masterzonefilepath'/'zone.z.fqdn
   rc = Stream(zonefile,'C', 'OPEN READ')
   IF Pos('NOTREADY',rc) = 0 THEN
   DO
       SAY 'ERROR: Zonefile' zonefile 'already exists. Please check if zone is already there!'
       rc = Stream(zonefile,'C', 'CLOSE')
       RETURN
   END
 
   SAY "Creating new domain" zone.z.fqdn "with nameserver "
   SAY 'Creating Zonefile' zonefile 'with' zone.z.ns.0 'NS,' zone.z.mx.0 'MX,' zone.z.cn.0 'CNAME und' zone.z.a.0 'A Records.'
 
   rc = Stream(zonefile,'C', 'OPEN WRITE')
   rc = LineOut(zonefile,"; Zonefile for" zone.z.fqdn " created " date() "with script" scriptname scriptversion version)
   rc = LineOut(zonefile,zone.z.fqdn || '. IN SOA' zone.z.ns.1'.' hostmaster'.'zone.z.fqdn'. (')
   rc = LineOut(zonefile,'              ' Date('S') || '00       ; Serial yymmddnn')
   rc = LineOut(zonefile,'               3h               ; Refresh after 3 hour(s)')
   rc = LineOut(zonefile,'               1h               ; Retry after 1 hour(s)')
   rc = LineOut(zonefile,'               1w               ; Expire after 1 week')
   rc = LineOut(zonefile,'               1h )             ; Negative caching TTL of 1h')
 
   IF zone.z.ns.0 > 0 THEN DO
     rc = LineOut(zonefile,';')
     rc = LineOut(zonefile,'; nameserver')
   END
 
   DO j = 1 TO zone.z.ns.0
     rc = LineOut(zonefile, zone.z.fqdn || '.' Right('IN NS   ', 60-Length(zone.z.fqdn)) zone.z.ns.j'.')
   END
 
   IF zone.z.mx.0 > 0 THEN DO
     rc = LineOut(zonefile,';')
     rc = LineOut(zonefile,'; mailroutes')
   END
 
   DO j = 1 TO zone.z.mx.0
     rc = LineOut(zonefile, zone.z.fqdn || '.' Right('IN MX   ', 60-Length(zone.z.fqdn)) zone.z.mx.j.pref zone.z.mx.j'.')
   END
 
   IF zone.z.cn.0 > 0 THEN DO
     rc = LineOut(zonefile,';')
     rc = LineOut(zonefile,'; Alias Names (CNAMES)')
   END
 
   DO j = 1 TO zone.z.cn.0
     rc = LineOut(zonefile, zone.z.cn.j || '.' Right('IN CNAME', 60-Length(zone.z.cn.j)) zone.z.cn.j.cname'.')
   END
 
   IF zone.z.a.0 > 0 THEN DO
     rc = LineOut(zonefile,';')
     rc = LineOut(zonefile,'; Address Records (A Records)')
   END
 
   DO j = 1 TO zone.z.a.0
     rc = LineOut(zonefile, zone.z.a.j || '.' Right('IN A    ', 60-Length(zone.z.a.j)) zone.z.a.j.ip)
   END
 
   rc = Stream(zonefile,'C', 'CLOSE')
 
   rc = Stream(namedmasters,'C','OPEN WRITE')
   rc = Stream(namedmasters,'C','SEEK < 0')
 
   rc = LineOut(namedmasters,' ')
   rc = LineOut(namedmasters,'/* ##'zone.z.fqdn'## -- Start of Zone Configuration for' zone.z.fqdn '  */')
   rc = LineOut(namedmasters,' ')
   rc = LineOut(namedmasters,'zone "'zone.z.fqdn'" in')
   rc = LineOut(namedmasters,'{')
   rc = LineOut(namedmasters,'   type master;')
   rc = LineOut(namedmasters,'   file "master/'zone.z.fqdn'";')
   rc = LineOut(namedmasters,'   /* allow zone transfer for slaves */')
   rc = LineOut(namedmasters,'   allow-transfer { 'secondarynameservers'; };')
   rc = LineOut(namedmasters,'};')
   rc = LineOut(namedmasters,' ')
   rc = LineOut(namedmasters,'/* ##'zone.z.fqdn'## -- End of Zone Configuration for' zone.z.fqdn '  */')
 
   rc = Stream(namedmasters,'C','CLOSE')
 
 END
 
 RETURN
 
 

----
Men & Mice Support Team
support@menandmice.com
 
Forum Index -> Scripting and Customizing
Go to:   
Powered by JForum 2.1.7 © JForum Team