ip_of_node support
[hcoop/domtool2.git] / src / plugins / bind.sml
index d358078..8dc1a72 100644 (file)
@@ -32,15 +32,15 @@ val () = Slave.registerPreHandler (fn () => (namedChanged := false;
                                             didDomain := ""))
 
 val dns : TextIO.outstream option ref = ref NONE
                                             didDomain := ""))
 
 val dns : TextIO.outstream option ref = ref NONE
-fun dnsF () = valOf (!dns)
-
-fun write s = TextIO.output (dnsF (), s)
 
 val _ = Domain.registerBefore
 
 val _ = Domain.registerBefore
-           (fn _ => dns := SOME (Domain.domainFile "dns"))
+           (fn _ => dns := Option.map (fn node => Domain.domainFile {node = node,
+                                                                     name = "dns"})
+                                      (Domain.dnsMaster ()))
        
 val _ = Domain.registerAfter
        
 val _ = Domain.registerAfter
-           (fn _ => TextIO.closeOut (dnsF ()))
+           (fn _ => (Option.app TextIO.closeOut (!dns);
+                     dns := NONE))
 
 val dl = ErrorMsg.dummyLoc
 
 
 val dl = ErrorMsg.dummyLoc
 
@@ -51,7 +51,7 @@ datatype dns_record =
        | NS of string
 
 val record = fn (EApp ((EApp ((EVar "dnsA", _), e1), _), e2), _) =>
        | NS of string
 
 val record = fn (EApp ((EApp ((EVar "dnsA", _), e1), _), e2), _) =>
-               (case (Env.string e1, Env.string e2) of
+               (case (Env.string e1, Domain.ip e2) of
                     (SOME v1, SOME v2) => SOME (A (v1, v2))
                   | _ => NONE)
              | (EApp ((EApp ((EVar "dnsCNAME", _), e1), _), e2), _) =>
                     (SOME v1, SOME v2) => SOME (A (v1, v2))
                   | _ => NONE)
              | (EApp ((EApp ((EVar "dnsCNAME", _), e1), _), e2), _) =>
@@ -67,39 +67,43 @@ val record = fn (EApp ((EApp ((EVar "dnsA", _), e1), _), e2), _) =>
              | _ => NONE
 
 fun writeRecord (evs, r) =
              | _ => NONE
 
 fun writeRecord (evs, r) =
-    let
-       val ttl = Env.env Env.int (evs, "TTL")
-    in
-       case r of
-           A (from, to) => (write from;
-                            write ".";
-                            write (Domain.currentDomain ());
-                            write ".\t";
-                            write (Int.toString ttl);
-                            write "\tIN\tA\t";
-                            write to;
-                            write "\n")
-         | CNAME (from, to) => (write from;
+    case !dns of
+       NONE => print "Warning: DNS directive ignored because no master DNS server is configured for this domain\n"
+      | SOME file =>
+       let
+           fun write s = TextIO.output (file, s)
+           val ttl = Env.env Env.int (evs, "TTL")
+       in
+           case r of
+               A (from, to) => (write from;
                                 write ".";
                                 write (Domain.currentDomain ());
                                 write ".\t";
                                 write (Int.toString ttl);
                                 write ".";
                                 write (Domain.currentDomain ());
                                 write ".\t";
                                 write (Int.toString ttl);
-                                write "\tIN\tCNAME\t";
+                                write "\tIN\tA\t";
                                 write to;
                                 write to;
-                                write ".\n")
-         | MX (num, host) => (write "\t";
-                              write (Int.toString ttl);
-                              write "\tIN\tMX\t";
-                              write (Int.toString num);
-                              write "\t";
-                              write host;
-                              write ".\n")
-         | NS host => (write "\t";
-                       write (Int.toString ttl);
-                       write "\tIN\tNS\t";
-                       write host;
-                       write ".\n")
-    end
+                                write "\n")
+             | CNAME (from, to) => (write from;
+                                    write ".";
+                                    write (Domain.currentDomain ());
+                                    write ".\t";
+                                    write (Int.toString ttl);
+                                    write "\tIN\tCNAME\t";
+                                    write to;
+                                    write ".\n")
+             | MX (num, host) => (write "\t";
+                                  write (Int.toString ttl);
+                                  write "\tIN\tMX\t";
+                                  write (Int.toString num);
+                                  write "\t";
+                                  write host;
+                                  write ".\n")
+             | NS host => (write "\t";
+                           write (Int.toString ttl);
+                           write "\tIN\tNS\t";
+                           write host;
+                           write ".\n")
+       end
 
 val () = Env.actionV_one "dns"
                         ("record", record)
 
 val () = Env.actionV_one "dns"
                         ("record", record)
@@ -126,7 +130,8 @@ val () = Slave.registerFileHandler (fn fs =>
                                                       val fname = OS.Path.joinDirFile {dir = Config.Bind.zonePath,
                                                                                        file = fname}
                                                   in
                                                       val fname = OS.Path.joinDirFile {dir = Config.Bind.zonePath,
                                                                                        file = fname}
                                                   in
-                                                      OS.FileSys.remove fname
+                                                      Slave.shellF ([Config.rm, " -f ", fname],
+                                                                 fn cl => "Error deleting file: " ^ cl)
                                                   end
                                               else
                                                   let
                                                   end
                                               else
                                                   let
@@ -173,8 +178,11 @@ val () = Slave.registerFileHandler (fn fs =>
                                                       TextIO.output (outf, Int.toString min);
                                                       TextIO.output (outf, " )\n\n");
                                                       TextIO.closeOut outf;
                                                       TextIO.output (outf, Int.toString min);
                                                       TextIO.output (outf, " )\n\n");
                                                       TextIO.closeOut outf;
-                                                      Slave.shellF ([Config.cat, " ", dns, " >>", fname],
-                                                                 fn cl => "Error concatenating file: " ^ cl);
+                                                      if Posix.FileSys.access (dns, []) then
+                                                          Slave.shellF ([Config.cat, " ", dns, " >>", fname],
+                                                                     fn cl => "Error concatenating file: " ^ cl)
+                                                      else
+                                                          ();
                                                       didDomain := #domain fs
                                                   end
                                       in
                                                       didDomain := #domain fs
                                                   end
                                       in