zoneconf.in: Expect zones to carry their own DS records.
[zoneconf] / zoneconf.in
index 21cff51..859fa67 100755 (executable)
@@ -198,6 +198,13 @@ proc run {what command args} {
   }
 }
 
+proc isolate {body} {
+  ## Evaluate BODY without changing the caller's variables.  Return its
+  ## result.
+
+  eval $body
+}
+
 ###--------------------------------------------------------------------------
 ### Configuration spaces.
 ###
@@ -942,12 +949,21 @@ proc host-canonify {host} {
 
 proc local-address-p {addr} {
   ## Answer whether the ADDR is one of the host's addresses.
+  global env
 
-  if {[catch { set sk [socket -server {} -myaddr $addr 0] }]} {
-    return false
+  if {[info exists env(ZONECONF_LOCAL_ADDRESSES)]} {
+    if {[string match "*:$addr:*" ":$env(ZONECONF_LOCAL_ADDRESSES):"]} {
+      return true
+    } else {
+      return false
+    }
   } else {
-    close $sk
-    return true
+    if {[catch { set sk [socket -server {} -myaddr $addr 0] }]} {
+      return false
+    } else {
+      close $sk
+      return true
+    }
   }
 }
 
@@ -992,15 +1008,15 @@ define-configuration-space zone ZONECFG {
   define-simple dir-mode 2775
   define-simple zone-file "%v/%z.zone"
   define-simple soa-format increment
+  define-simple allow-query nil
   define-list views *
   define-list sign-views {}
   define-list signzone-command \
       [list "$BINDPROGS/dnssec-signzone" \
-          "-g" \
           "-S" \
           "-K%h/key" \
           "-d%h/ds" \
-          "-s-3600" "-e+176400" \
+          "-s-3600" "-e+176400" "-i90000" \
           "-N%q" \
           "-o%z" \
           "-f%o" \
@@ -1016,12 +1032,19 @@ define-configuration-space zone ZONECFG {
           "-Sfail" \
           "-Wfail" \
           "%z" "%f"]
+  define-list also-notify nil
 
   define setvar {name value} {
     dict set ZONECFG(var) $name $value
   }
 
   define primary {map} {
+    ## There's a grim hack here: a primary-address entry may have the form
+    ## REAL!FAKE.  If the REAL address is not a local address then this
+    ## is used as the master address; otherwise the FAKE address is used.
+    ## This is useful for inter-view updates of dynamic zones on the same
+    ## host.  I suggest abusing 127.0.0.0/8 addresses for this kind of
+    ## chicanery.
     if {[llength $map] % 2} {
       error "master map must have an even number of items"
     }
@@ -1141,13 +1164,28 @@ proc compute-zone-properties {view config} {
   if {[info exists zone(mapped-view)]} {
     foreach {outview hosts} $zone(master-map) {
       if {[string match $outview $zone(mapped-view)]} {
-       set zone(masters) $hosts
+       set masters {}
        set zone(config-type) slave
        foreach host $hosts {
-         if {[local-address-p $host]} {
+         set bang [string first "!" $host]
+         if {$bang >= 0} {
+           set after [string range $host [expr {$bang + 1}] end]
+           if {$bang} {
+             set before [string range $host 0 [expr {$bang - 1}]]
+           } else {
+             set before $after
+           }
+           if {[local-address-p $before]} {
+             set host $after
+           } else {
+             set host $before
+           }
+         } elseif {[local-address-p $host]} {
            set zone(config-type) master
          }
+         lappend masters $host
        }
+       set zone(masters) $masters
        break
       }
     }
@@ -1259,6 +1297,9 @@ proc write-zone-stanza {view chan config} {
     master {
       puts $chan "\ttype master;"
       puts $chan "\tfile \"$zone(server-file-name)\";"
+      if {![string equal $zone(also-notify) "nil"]} {
+       puts $chan "\talso-notify { [join $zone(also-notify) {; }]; };"
+      }
       switch -exact -- $zone(type) {
        dynamic {
          write-ddns-update-policy "\t" $chan $config
@@ -1279,6 +1320,9 @@ proc write-zone-stanza {view chan config} {
       }
     }
   }
+  if {![string equal $zone(allow-query) nil]} {
+    puts $chan "\tallow-query {$zone(allow-query)};"
+  }
   puts $chan "};";
 }
 
@@ -1292,7 +1336,7 @@ defcmd outputs {} {
 } {
   global ZONECFG CONFFILE
 
-  confspc-eval toplevel [list source $CONFFILE]
+  isolate [list confspc-eval toplevel [list source $CONFFILE]]
   foreach view $ZONECFG(all-views) { puts [output-file-name $view] }
 }
 
@@ -1302,7 +1346,7 @@ defcmd update {} {
   global ZONECFG ZONES CONFFILE
 
   ## Read the configuration.
-  confspc-eval toplevel [list source $CONFFILE]
+  isolate [list confspc-eval toplevel [list source $CONFFILE]]
 
   ## Safely update the files.
   set win false
@@ -1357,7 +1401,7 @@ provided by the named USER."
   global QUIS ZONECFG ZONES CONFFILE errorInfo errorCode
 
   ## Read the configuration.
-  confspc-eval toplevel [list source $CONFFILE]
+  isolate [list confspc-eval toplevel [list source $CONFFILE]]
 
   ## Make sure there's a temporary directory.
   file mkdir [file join $ZONECFG(home-dir) "tmp"]
@@ -1456,7 +1500,7 @@ defcmd sign {} {
   set rc 0
 
   ## Read the configuration.
-  confspc-eval toplevel [list source $CONFFILE]
+  isolate [list confspc-eval toplevel [list source $CONFFILE]]
 
   ## Grind through all of the zones.
   array unset seen