ip_of_node support
[hcoop/domtool2.git] / src / domain.sml
index 6b645cb..15903a2 100644 (file)
 
 structure Domain :> DOMAIN = struct
 
+open MsgTypes
+
+structure SM = DataStructures.StringMap
+structure SS = DataStructures.StringSet
+
+val ssl_context = ref (NONE : OpenSSL.context option)
+fun set_context ctx = ssl_context := SOME ctx
+
+val nodes = map #1 Config.nodeIps
+val nodeMap = foldl (fn ((node, ip), mp) => SM.insert (mp, node, ip))
+                   SM.empty Config.nodeIps
+fun nodeIp node = valOf (SM.find (nodeMap, node))
+
+val usr = ref ""
+fun getUser () = !usr
+
+val your_doms = ref SS.empty
+fun your_domains () = !your_doms
+
+val your_usrs = ref SS.empty
+fun your_users () = !your_usrs
+
+val your_grps = ref SS.empty
+fun your_groups () = !your_grps
+
+val your_pths = ref SS.empty
+fun your_paths () = !your_pths
+
+fun setUser user =
+    (usr := user;
+     your_doms := Acl.class {user = getUser (),
+                            class = "domain"};
+     your_usrs := Acl.class {user = getUser (),
+                            class = "user"};
+     your_grps := Acl.class {user = getUser (),
+                            class = "group"};
+     your_pths := Acl.class {user = getUser (),
+                            class = "path"})
+
 fun validIp s =
     case map Int.fromString (String.fields (fn ch => ch = #".") s) of
        [SOME n1, SOME n2, SOME n3, SOME n4] =>
@@ -36,6 +75,40 @@ fun validDomain s =
     size s > 0 andalso size s < 100
     andalso List.all validHost (String.fields (fn ch => ch = #".") s)
 
+fun validNode s = List.exists (fn s' => s = s') nodes
+
+fun yourDomain s = SS.member (your_domains (), s)
+fun yourUser s = SS.member (your_users (), s)
+fun yourGroup s = SS.member (your_groups (), s)
+fun yourPath path =
+    List.all (fn s => s <> "..") (String.fields (fn ch => ch = #"/") path)
+    andalso CharVector.all (fn ch => Char.isAlphaNum ch orelse ch = #"." orelse ch = #"/"
+                                    orelse ch = #"-" orelse ch = #"_") path
+    andalso SS.exists (fn s' => path = s' orelse String.isPrefix (s' ^ "/") path) (your_paths ())
+
+fun yourDomainHost s =
+    yourDomain s
+    orelse let
+       val (pref, suf) = Substring.splitl (fn ch => ch <> #".") (Substring.full s)
+    in
+       Substring.size suf > 0
+       andalso validHost (Substring.string pref)
+       andalso yourDomain (Substring.string
+                               (Substring.slice (suf, 1, NONE)))
+    end
+
+fun validUser s = size s > 0 andalso size s < 20
+                 andalso CharVector.all Char.isAlphaNum s
+
+val validGroup = validUser
+
+val _ = Env.type_one "no_spaces"
+                    Env.string
+                    (CharVector.all (fn ch => not (Char.isSpace ch)))
+val _ = Env.type_one "no_newlines"
+                    Env.string
+                    (CharVector.all (fn ch => ch <> #"\n" andalso ch <> #"\r"))
+
 val _ = Env.type_one "ip"
        Env.string
        validIp
@@ -48,6 +121,38 @@ val _ = Env.type_one "domain"
        Env.string
        validDomain
 
+val _ = Env.type_one "your_domain"
+       Env.string
+       yourDomain
+
+val _ = Env.type_one "your_domain_host"
+       Env.string
+       yourDomainHost
+
+val _ = Env.type_one "user"
+       Env.string
+       validUser
+
+val _ = Env.type_one "group"
+       Env.string
+       validGroup
+
+val _ = Env.type_one "your_user"
+       Env.string
+       yourUser
+
+val _ = Env.type_one "your_group"
+       Env.string
+       yourGroup
+
+val _ = Env.type_one "your_path"
+       Env.string
+       yourPath
+
+val _ = Env.type_one "node"
+       Env.string
+       validNode
+
 open Ast
 
 val dl = ErrorMsg.dummyLoc
@@ -63,14 +168,23 @@ val soaD = multiApp ((EVar "soa", dl),
                     dl,
                     [nsD, serialD, refD, retD, expD, minD])
 
-val _ = Main.registerDefault ("DNS",
-                             (TBase "dnsKind", dl),
-                             (EApp ((EVar "master", dl),
-                                    soaD), dl))
+val masterD = (EApp ((EVar "internalMaster", dl),
+                    (EString Config.defaultNode, dl)),
+              dl)
 
-val _ = Main.registerDefault ("TTL",
-                             (TBase "int", dl),
-                             (EInt Config.Bind.defaultTTL, dl))
+val _ = Defaults.registerDefault ("Mailbox",
+                                 (TBase "email", dl),
+                                 (fn () => (EString (getUser ()), dl)))
+
+val _ = Defaults.registerDefault ("DNS",
+                                 (TBase "dnsKind", dl),
+                                 (fn () => multiApp ((EVar "useDns", dl),
+                                                     dl,
+                                                     [soaD, masterD, (EList [], dl)])))
+
+val _ = Defaults.registerDefault ("TTL",
+                                 (TBase "int", dl),
+                                 (fn () => (EInt Config.Bind.defaultTTL, dl)))
 
 type soa = {ns : string,
            serial : int option,
@@ -103,13 +217,33 @@ val soa = fn (EApp ((EApp ((EApp ((EApp ((EApp ((EApp
                | _ => NONE)
           | _ => NONE
 
+datatype master =
+        ExternalMaster of string
+       | InternalMaster of string
+
+val ip = fn (EApp ((EVar "ip_of_node", _), e), _) => Option.map nodeIp (Env.string e)
+         | e => Env.string e
+
+val master = fn (EApp ((EVar "externalMaster", _), e), _) => Option.map ExternalMaster (ip e)
+             | (EApp ((EVar "internalMaster", _), e), _) => Option.map InternalMaster (ip e)
+             | _ => NONE
+
 datatype dnsKind =
-        Master of soa
-       | Slave of soa
+        UseDns of {soa : soa,
+                   master : master,
+                   slaves : string list}
        | NoDns
 
-val dnsKind = fn (EApp ((EVar "master", _), e), _) => Option.map Master (soa e)
-              | (EApp ((EVar "slave", _), e), _) => Option.map Slave (soa e)
+val dnsKind = fn (EApp ((EApp ((EApp 
+                                   ((EVar "useDns", _), sa), _),
+                              mstr), _),
+                       slaves), _) =>
+                (case (soa sa, master mstr, Env.list Env.string slaves) of
+                     (SOME sa, SOME mstr, SOME slaves) =>
+                     SOME (UseDns {soa = sa,
+                                   master = mstr,
+                                   slaves = slaves})
+                   | _ => NONE)
               | (EVar "noDns", _) => SOME NoDns
               | _ => NONE
 
@@ -131,13 +265,14 @@ fun registerAfter f =
     end
 
 val current = ref ""
-val currentPath = ref ""
+val currentPath = ref (fn (_ : string) => "")
 
 val scratch = ref ""
 
 fun currentDomain () = !current
 
-fun domainFile name = TextIO.openOut (!currentPath ^ name)
+fun domainFile {node, name} = ((*print ("Opening " ^ !currentPath node ^ name ^ "\n");*)
+                              TextIO.openOut (!currentPath node ^ name))
 
 fun getPath domain =
     let
@@ -146,29 +281,36 @@ fun getPath domain =
        val elems = foldr (fn (piece, elems) =>
                              let
                                  val elems = piece :: elems
-                                 val path = String.concatWith "/" (Config.resultRoot :: rev elems)
-                                 val tmpPath = String.concatWith "/" (Config.tmpDir :: rev elems)
-                             in
-                                 (if Posix.FileSys.ST.isDir
-                                         (Posix.FileSys.stat path) then
-                                      ()
-                                  else
-                                      (OS.FileSys.remove path;
-                                       OS.FileSys.mkDir path))
-                                 handle OS.SysErr _ => OS.FileSys.mkDir path;
-
-                                 (if Posix.FileSys.ST.isDir
-                                         (Posix.FileSys.stat tmpPath) then
-                                      ()
-                                  else
-                                      (OS.FileSys.remove tmpPath;
-                                       OS.FileSys.mkDir tmpPath))
-                                 handle OS.SysErr _ => OS.FileSys.mkDir tmpPath;
 
+                                 fun doNode node =
+                                     let
+                                         val path = String.concatWith "/"
+                                                                      (Config.resultRoot :: node :: rev elems)
+                                         val tmpPath = String.concatWith "/"
+                                                                         (Config.tmpDir :: node :: rev elems)
+                                     in
+                                         (if Posix.FileSys.ST.isDir
+                                                 (Posix.FileSys.stat path) then
+                                              ()
+                                          else
+                                              (OS.FileSys.remove path;
+                                               OS.FileSys.mkDir path))
+                                         handle OS.SysErr _ => OS.FileSys.mkDir path;
+
+                                         (if Posix.FileSys.ST.isDir
+                                                 (Posix.FileSys.stat tmpPath) then
+                                              ()
+                                          else
+                                              (OS.FileSys.remove tmpPath;
+                                               OS.FileSys.mkDir tmpPath))
+                                         handle OS.SysErr _ => OS.FileSys.mkDir tmpPath
+                                     end
+                             in
+                                 app doNode nodes;
                                  elems
                              end) [] toks
     in
-       fn root => String.concatWith "/" (root :: rev ("" :: elems))
+       fn (root, site) => String.concatWith "/" (root :: site :: rev ("" :: elems))
     end
 
 datatype file_action' =
@@ -176,10 +318,13 @@ datatype file_action' =
        | Delete' of string
        | Modify' of {src : string, dst : string}
 
-fun findDiffs dom =
+fun findDiffs (site, dom, acts) =
     let
-       val realPath = getPath dom Config.resultRoot
-       val tmpPath = !currentPath
+       val gp = getPath dom
+       val realPath = gp (Config.resultRoot, site)
+       val tmpPath = gp (Config.tmpDir, site)
+
+       (*val _ = print ("getDiffs(" ^ site ^ ", " ^ dom ^ ")... " ^ realPath ^ "; " ^ tmpPath ^ "\n")*)
 
        val dir = Posix.FileSys.opendir realPath
 
@@ -200,12 +345,12 @@ fun findDiffs dom =
                        if Slave.shell [Config.diff, " ", real, " ",  tmp] then
                            loopReal acts
                        else
-                           loopReal (Modify' {src = tmp, dst = real} :: acts)
+                           loopReal ((site, dom, realPath, Modify' {src = tmp, dst = real}) :: acts)
                    else
-                       loopReal (Delete' real :: acts)
+                       loopReal ((site, dom, realPath, Delete' real) :: acts)
                end
 
-       val acts = loopReal []
+       val acts = loopReal acts
 
        val dir = Posix.FileSys.opendir tmpPath
 
@@ -225,7 +370,7 @@ fun findDiffs dom =
                    else if Posix.FileSys.access (real, []) then
                        loopTmp acts
                    else
-                       loopTmp (Add' {src = tmp, dst = real} :: acts)
+                       loopTmp ((site, dom, realPath, Add' {src = tmp, dst = real}) :: acts)
                end
 
        val acts = loopTmp acts
@@ -233,6 +378,59 @@ fun findDiffs dom =
        acts
     end
 
+fun findAllDiffs () =
+    let
+       val dir = Posix.FileSys.opendir Config.tmpDir
+       val len = length (String.fields (fn ch => ch = #"/") Config.tmpDir) + 1
+
+       fun exploreSites diffs =
+           case Posix.FileSys.readdir dir of
+               NONE => diffs
+             | SOME site =>
+               let
+                   fun explore (dname, diffs) =
+                       let
+                           val dir = Posix.FileSys.opendir dname
+
+                           fun loop diffs =
+                               case Posix.FileSys.readdir dir of
+                                   NONE => diffs
+                                 | SOME name =>
+                                   let
+                                       val fname = OS.Path.joinDirFile {dir = dname,
+                                                                        file = name}
+                                   in
+                                       loop (if Posix.FileSys.ST.isDir (Posix.FileSys.stat fname) then
+                                                 let
+                                                     val dom = String.fields (fn ch => ch = #"/") fname
+                                                     val dom = List.drop (dom, len)
+                                                     val dom = String.concatWith "." (rev dom)
+
+                                                     val dname' = OS.Path.joinDirFile {dir = dname,
+                                                                                       file = name}
+                                                 in
+                                                     explore (dname',
+                                                              findDiffs (site, dom, diffs))
+                                                 end
+                                             else
+                                                 diffs)
+                                   end
+                       in
+                           loop diffs
+                           before Posix.FileSys.closedir dir
+                       end
+               in
+                   exploreSites (explore (OS.Path.joinDirFile {dir = Config.tmpDir,
+                                                               file = site}, diffs))
+               end
+    in
+       exploreSites []
+       before Posix.FileSys.closedir dir
+    end
+
+val masterNode : string option ref = ref NONE
+fun dnsMaster () = !masterNode
+
 val _ = Env.containerV_one "domain"
                           ("domain", Env.string)
                           (fn (evs, dom) =>
@@ -240,15 +438,14 @@ val _ = Env.containerV_one "domain"
                                   val kind = Env.env dnsKind (evs, "DNS")
                                   val ttl = Env.env Env.int (evs, "TTL")
 
-                                  val path = getPath dom Config.tmpDir
+                                  val path = getPath dom
 
                                   val () = (current := dom;
-                                            currentPath := path;
-                                            !befores dom)
+                                            currentPath := (fn site => path (Config.tmpDir, site)))
 
-                                  fun saveSoa (kind, soa : soa) =
+                                  fun saveSoa (kind, soa : soa) node =
                                       let
-                                          val outf = domainFile "soa"
+                                          val outf = domainFile {node = node, name = "soa"}
                                       in
                                           TextIO.output (outf, kind);
                                           TextIO.output (outf, "\n");
@@ -271,73 +468,138 @@ val _ = Env.containerV_one "domain"
                                           TextIO.closeOut outf
                                       end
 
-                                  fun saveNamed (kind, soa : soa) =
+                                  fun saveNamed (kind, soa : soa, masterIp) node =
                                       let
-                                          val outf = domainFile "named.conf"
+                                          val outf = domainFile {node = node, name = "named.conf"}
                                       in
                                           TextIO.output (outf, "\nzone \"");
                                           TextIO.output (outf, dom);
                                           TextIO.output (outf, "\" IN {\n\ttype ");
                                           TextIO.output (outf, kind);
                                           TextIO.output (outf, ";\n\tfile \"");
-                                          TextIO.output (outf, Config.Bind.zonePath);
+                                          TextIO.output (outf, Config.Bind.zonePath_real);
                                           TextIO.output (outf, "/");
                                           TextIO.output (outf, dom);
                                           TextIO.output (outf, ".zone\";\n");
                                           case kind of
                                               "master" => TextIO.output (outf, "\tallow-update { none; };\n")
-                                            | _ => TextIO.output (outf, "\tmasters { 1.2.3.4; };\n");
-                                          TextIO.output (outf, "}\n");
+                                            | _ => (TextIO.output (outf, "\tmasters { ");
+                                                    TextIO.output (outf, masterIp);
+                                                    TextIO.output (outf, " };\n"));
+                                          TextIO.output (outf, "};\n");
                                           TextIO.closeOut outf
                                       end
-
-                                  fun saveBoth ks = (saveSoa ks; saveNamed ks)
                               in
                                   case kind of
-                                      NoDns => ()
-                                    | Master soa => saveBoth ("master", soa)
-                                    | Slave soa => saveBoth ("slave", soa)
+                                      NoDns => masterNode := NONE
+                                    | UseDns dns =>
+                                      let
+                                          val masterIp =
+                                              case #master dns of
+                                                  InternalMaster node => valOf (SM.find (nodeMap, node))
+                                                | ExternalMaster ip => ip
+                                      in
+                                          app (saveSoa ("slave", #soa dns)) (#slaves dns);
+                                          app (saveNamed ("slave", #soa dns, masterIp)) (#slaves dns);
+                                          case #master dns of
+                                              InternalMaster node =>
+                                              (masterNode := SOME node;
+                                               saveSoa ("master", #soa dns) node;
+                                               saveNamed ("master", #soa dns, masterIp) node)
+                                            | _ => masterNode := NONE;
+                                          !befores dom
+                                      end
                               end,
-                           fn () =>
-                              let
-                                  val dom = !current
-                                  val () = !afters dom
-
-                                  val diffs = findDiffs dom
-
-                                  val dir = getPath dom Config.resultRoot
+                           fn () => !afters (!current))
+
+val () = Env.registerPre (fn () => (ignore (Slave.shellF ([Config.rm, " -rf ", Config.tmpDir, ""],
+                                                      fn cl => "Temp file cleanup failed: " ^ cl));
+                                   OS.FileSys.mkDir Config.tmpDir;
+                                   app (fn node => OS.FileSys.mkDir
+                                                       (OS.Path.joinDirFile {dir = Config.tmpDir,
+                                                                             file = node}))
+                                       nodes;
+                                   app (fn node => OS.FileSys.mkDir
+                                                       (OS.Path.joinDirFile {dir = Config.resultRoot,
+                                                                             file = node})
+                                           handle OS.SysErr _ => ())
+                                       nodes))
+
+val () = Env.registerPost (fn () =>
+                             let
+                                 val diffs = findAllDiffs ()
 
-                                  val diffs = map (fn Add' {src, dst} =>
-                                                      (Slave.shellF ([Config.cp, " ", src, " ", dst],
-                                                                  fn cl => "Copy failed: " ^ cl);
+                                 val diffs = map (fn (site, dom, dir, Add' {src, dst}) =>
+                                                     (Slave.shellF ([Config.cp, " ", src, " ", dst],
+                                                                 fn cl => "Copy failed: " ^ cl);
+                                                      (site,
                                                        {action = Slave.Add,
                                                         domain = dom,
                                                         dir = dir,
-                                                        file = dst})
-                                                    | Delete' dst =>
-                                                      (OS.FileSys.remove dst
-                                                       handle OS.SysErr _ =>
-                                                              ErrorMsg.error NONE ("Delete failed for " ^ dst);
+                                                        file = dst}))
+                                                   | (site, dom, dir, Delete' dst) =>
+                                                     (OS.FileSys.remove dst
+                                                      handle OS.SysErr _ =>
+                                                             ErrorMsg.error NONE ("Delete failed for " ^ dst);
+                                                      (site,
                                                        {action = Slave.Delete,
                                                         domain = dom,
                                                         dir = dir,
-                                                        file = dst})
-                                                    | Modify' {src, dst} =>
-                                                      (Slave.shellF ([Config.cp, " ", src, " ", dst],
-                                                                  fn cl => "Copy failed: " ^ cl);
+                                                        file = dst}))
+                                                   | (site, dom, dir, Modify' {src, dst}) =>
+                                                     (Slave.shellF ([Config.cp, " ", src, " ", dst],
+                                                                 fn cl => "Copy failed: " ^ cl);
+                                                      (site,
                                                        {action = Slave.Modify,
                                                         domain = dom,
                                                         dir = dir,
-                                                        file = dst})) diffs
-                              in
-                                  if !ErrorMsg.anyErrors then
-                                      ()
-                                  else
-                                      Slave.handleChanges diffs;
-                                  ignore (Slave.shellF ([Config.rm, " -rf ", Config.tmpDir, "/*"],
-                                                     fn cl => "Temp file cleanup failed: " ^ cl))
-                              end)
-
-
+                                                        file = dst}))) diffs
+                             in
+                                 if !ErrorMsg.anyErrors then
+                                     ()
+                                 else let
+                                         val changed = foldl (fn ((site, file), changed) =>
+                                                                 let
+                                                                     val ls = case SM.find (changed, site) of
+                                                                                  NONE => []
+                                                                                | SOME ls => ls
+                                                                 in
+                                                                     SM.insert (changed, site, file :: ls)
+                                                                 end) SM.empty diffs
+
+                                         fun handleSite (site, files) =
+                                             let
+                                                 
+                                             in
+                                                 print ("New configuration for node " ^ site ^ "\n");
+                                                 if site = Config.defaultNode then
+                                                     Slave.handleChanges files
+                                                 else let
+                                                         val bio = OpenSSL.connect (valOf (!ssl_context),
+                                                                                    nodeIp site
+                                                                                    ^ ":"
+                                                                                    ^ Int.toString Config.slavePort)
+                                                     in
+                                                         app (fn file => Msg.send (bio, MsgFile file)) files;
+                                                         Msg.send (bio, MsgDoFiles);
+                                                         case Msg.recv bio of
+                                                             NONE => print "Slave closed connection unexpectedly\n"
+                                                           | SOME m =>
+                                                             case m of
+                                                                 MsgOk => print ("Slave " ^ site ^ " finished\n")
+                                                               | MsgError s => print ("Slave " ^ site
+                                                                                      ^ " returned error: " ^
+                                                                                      s ^ "\n")
+                                                               | _ => print ("Slave " ^ site
+                                                                             ^ " returned unexpected command\n");
+                                                         OpenSSL.close bio
+                                                     end
+                                             end
+                                     in
+                                         SM.appi handleSite changed
+                                     end;
+                                 ignore (Slave.shellF ([Config.rm, " -rf ", Config.tmpDir, ""],
+                                                    fn cl => "Temp file cleanup failed: " ^ cl))
+                             end)
 
 end