Allow some of a user's config to survive regen, even when some doesn't type-check
[hcoop/domtool2.git] / src / main.sml
index 4198901..d769a61 100644 (file)
@@ -96,7 +96,24 @@ fun notTmp s =
     String.sub (s, 0) <> #"."
     andalso CharVector.all (fn ch => Char.isAlphaNum ch orelse ch = #"." orelse ch = #"_" orelse ch = #"-") s
 
-fun checkDir dname =
+fun setupUser () =
+    let
+       val user =
+           case Posix.ProcEnv.getenv "DOMTOOL_USER" of
+               NONE =>
+               let
+                   val uid = Posix.ProcEnv.getuid ()
+               in
+                   Posix.SysDB.Passwd.name (Posix.SysDB.getpwuid uid)
+               end
+             | SOME user => user
+    in
+       Acl.read Config.aclFile;
+       Domain.setUser user;
+       user
+    end
+
+fun checkDir' dname =
     let
        val b = basis ()
 
@@ -127,6 +144,10 @@ fun checkDir dname =
                 ())
     end
 
+fun checkDir dname =
+    (setupUser ();
+     checkDir' dname)
+
 fun reduce fname =
     let
        val (G, body) = check fname
@@ -180,23 +201,6 @@ fun context x =
            print ("Additional information: " ^ s ^ "\n");
            raise e)
 
-fun setupUser () =
-    let
-       val user =
-           case Posix.ProcEnv.getenv "DOMTOOL_USER" of
-               NONE =>
-               let
-                   val uid = Posix.ProcEnv.getuid ()
-               in
-                   Posix.SysDB.Passwd.name (Posix.SysDB.getpwuid uid)
-               end
-             | SOME user => user
-    in
-       Acl.read Config.aclFile;
-       Domain.setUser user;
-       user
-    end
-
 fun requestContext f =
     let
        val user = setupUser ()
@@ -262,7 +266,7 @@ fun requestDir dname =
 
        val _ = ErrorMsg.reset ()
 
-       val (user, bio) = requestBio (fn () => checkDir dname)
+       val (user, bio) = requestBio (fn () => checkDir' dname)
 
        val b = basis ()
 
@@ -869,8 +873,80 @@ fun requestFirewall {node, uname} =
        before OpenSSL.close bio
     end
 
+fun requestDescribe dom =
+    let
+       val (_, bio) = requestBio (fn () => ())
+    in
+       Msg.send (bio, MsgDescribe dom);
+       case Msg.recv bio of
+           NONE => print "Server closed connection unexpectedly.\n"
+         | SOME m =>
+           case m of
+               MsgDescription s => print s
+             | MsgError s => print ("Description failed: " ^ s ^ "\n")
+             | _ => print "Unexpected server reply.\n";
+       OpenSSL.close bio
+    end
+
+structure SS = StringSet
+
+fun domainList dname =
+    let
+       val dir = Posix.FileSys.opendir dname
+
+       fun visitNode dset =
+           case Posix.FileSys.readdir dir of
+               NONE => dset
+             | SOME node =>
+               let
+                   val path = OS.Path.joinDirFile {dir = dname,
+                                                   file = node}
+
+                   fun visitDomains (path, bfor, dset) =
+                       let
+                           val dir = Posix.FileSys.opendir path
+
+                           fun loop dset =
+                               case Posix.FileSys.readdir dir of
+                                   NONE => dset
+                                 | SOME dname =>
+                                   let
+                                       val path = OS.Path.joinDirFile {dir = path,
+                                                                       file = dname}
+                                   in
+                                       if Posix.FileSys.ST.isDir (Posix.FileSys.stat path) then
+                                           let
+                                               val bfor = dname :: bfor
+                                           in
+                                               loop (visitDomains (path, bfor,
+                                                                   SS.add (dset,
+                                                                           String.concatWith "." bfor)))
+                                           end
+                                       else
+                                           loop dset
+                                   end
+                       in
+                           loop dset
+                           before Posix.FileSys.closedir dir
+                       end
+               in
+                   visitNode (visitDomains (path, [], dset))
+               end
+    in
+       visitNode SS.empty
+       before Posix.FileSys.closedir dir
+    end
+
 fun regenerateEither tc checker context =
     let
+       val () = print "Starting regeneration....\n"
+
+       val domainsBefore =
+           if tc then
+               SS.empty
+           else
+               domainList Config.resultRoot
+
        fun ifReal f =
            if tc then
                ()
@@ -882,7 +958,11 @@ fun regenerateEither tc checker context =
        val b = basis ()
        val () = Tycheck.disallowExterns ()
 
-       val () = ifReal Domain.resetGlobal
+       val () = ifReal (fn () =>
+                           (ignore (OS.Process.system ("rm -rf " ^ Config.oldResultRoot ^ "/*"));
+                            ignore (OS.Process.system ("cp -r " ^ Config.resultRoot
+                                                       ^ "/* " ^ Config.oldResultRoot ^ "/"));
+                            Domain.resetGlobal ()))
 
        val ok = ref true
  
@@ -938,9 +1018,11 @@ fun regenerateEither tc checker context =
                    in
                        if !ErrorMsg.anyErrors then
                            (ErrorMsg.reset ();
-                            print ("User " ^ user ^ "'s configuration has errors!\n"))
+                            print ("User " ^ user ^ "'s configuration has errors!\n");
+                            ok := false)
                        else
-                           app checker files
+                           ();
+                       app checker files
                    end
                else
                    ()
@@ -959,7 +1041,22 @@ fun regenerateEither tc checker context =
        ifReal (fn () => (app contactNode Config.nodeIps;
                          Env.pre ()));
        app doUser (Acl.users ());
-       ifReal Env.post;
+       ifReal (fn () =>
+                  let
+                      val domainsAfter = domainList Config.resultRoot
+                      val domainsGone = SS.difference (domainsBefore, domainsAfter)
+                  in
+                      if SS.isEmpty domainsGone then
+                          ()
+                      else
+                          (print "Domains to kill:";
+                           SS.app (fn s => (print " "; print s)) domainsGone;
+                           print "\n";
+
+                           Domain.rmdom' Config.oldResultRoot (SS.listItems domainsGone));
+                      
+                      Env.post ()
+                  end);
        !ok
     end
 
@@ -1413,6 +1510,20 @@ fun service () =
                                                      SOME "Script execution failed."))
                                      (fn () => ())
 
+                              | MsgDescribe dom =>
+                                doIt (fn () => if not (Domain.validDomain dom) then
+                                                   ("Requested description of invalid domain " ^ dom,
+                                                    SOME "Invalid domain name")
+                                               else if not (Domain.yourDomain dom
+                                                            orelse Acl.query {user = user, class = "priv", value = "all"}) then
+                                                   ("Requested description of " ^ dom ^ ", but not allowed access",
+                                                    SOME "Access denied")
+                                               else
+                                                   (Msg.send (bio, MsgDescription (Domain.describe dom));
+                                                    ("Sent description of domain " ^ dom,
+                                                     NONE)))
+                                     (fn () => ())
+
                               | _ =>
                                 doIt (fn () => ("Unexpected command",
                                                 SOME "Unexpected command"))
@@ -1437,6 +1548,11 @@ fun service () =
                         OpenSSL.close bio
                         handle OpenSSL.OpenSSL _ => ();
                         loop ())
+                     | OS.Path.InvalidArc =>
+                       (print "Invalid arc\n";
+                        OpenSSL.close bio
+                        handle OpenSSL.OpenSSL _ => ();
+                        loop ())
                      | e =>
                        (print "Unknown exception in main loop!\n";
                         app (fn x => print (x ^ "\n")) (SMLofNJ.exnHistory e);