#!/usr/bin/awk -f # bind_master2slave v0.2 # Take a bind master zone configuration file on stdin and generate # a corresponding slave zone configuration file (named.conf.local) on stdout # Customize the SLAVE_TEMPLATE variable below so it fits what your bind slaves need # Copyright (c) 2013 Daniel Lange, http://daniel-lange.com. # Released into the public domain. NO LIABILITY ACCEPTED WHATSOEVER. USE AT YOUR OWN RISK. # Changelog: # 130120 v0.1 Initial release # 120123 v0.2 Added minimal error checking BEGIN { # Template for slave zone entries # _ZONE_ will be replaced by the zone name read from the master file (well ... stdin) # End lines with a backslash (\) for continuation and escape quotation marks (\") SLAVE_TEMPLATE="zone \"_ZONE_\" IN {\n\ type slave;\n\ file \"/var/cache/bind/db._ZONE_\";\n\ masters { your; masterip; go; here; };\n\ };" # Shall we output a summary of records created to stderr (1=yes)? # Shall we additionally exit indicating error (return code 1) if the # input file was not well formed? (2=yes) VERBOSE=2 # Internal variables COUNTRECORDS=0 COUNTCOMMENTS=0 } # End of BEGIN statement # MAIN LOOP: { # Forward comments and empty lines unaltered if ($1 ~ /\/\/|#|^$/) { print $0 COUNTCOMMENTS=COUNTCOMMENTS+1 } # Create slave record for every master record if (tolower($1) == "zone") { ZONE=tolower($2) gsub(/"/,"",ZONE) if (ZONE == "") { print ZONE print "Found broken zone statement in line " NR " (\"" $0 "\"). Aborting." > "/dev/stderr" exit 2 } MYTEMPLATE=SLAVE_TEMPLATE gsub(/_ZONE_/,ZONE,MYTEMPLATE) print MYTEMPLATE COUNTRECORDS=COUNTRECORDS+1 } if (tolower($1) == "type" && tolower($2) == "slave") { print "Found slave zone statement in line " NR " (\"" $0 "\"). You need to supply master zones only! Aborting." > "/dev/stderr" exit 2 } } # End of MAIN LOOP END { if (COUNTRECORDS == 0 ) { print "Error: No zone statements found. Is this a BIND DNS master named.conf.local file?..." > "/dev/stderr" exit 3 } else { if (VERBOSE >= 1) print "Processed " NR " lines. Created " COUNTRECORDS " slave records." > "/dev/stderr" # Some very basic well-formed checking: if (VERBOSE >= 2) { if ((NR-COUNTCOMMENTS)%COUNTRECORDS != 0) { print "Warning: Master records processed were not all well-formed." > "/dev/stderr" exit 1 } } } } # End of END statement