Proper handling of vhost deletions and ownership changes
authorAdam Chlipala <adamc@hcoop.net>
Sat, 17 Feb 2007 23:10:51 +0000 (23:10 +0000)
committerAdam Chlipala <adamc@hcoop.net>
Sat, 17 Feb 2007 23:10:51 +0000 (23:10 +0000)
src/plugins/apache.sml

index 3d7c199..558c998 100644 (file)
@@ -211,71 +211,104 @@ fun findVhostUser fname =
     in
        loop ()
        before TextIO.closeIn inf
-    end
+    end handle _ => NONE
 
 val () = Slave.registerFileHandler (fn fs =>
                                       let
                                           val spl = OS.Path.splitDirFile (#file fs)
                                       in
                                           if String.isSuffix ".vhost" (#file spl)
-                                             orelse String.isSuffix ".vhost_ssl" (#file spl) then
-                                              case findVhostUser (#file fs) of
-                                                  NONE => print ("Can't find user in " ^ #file fs ^ "!  Taking no action.\n")
-                                                | SOME user =>
-                                                  let
-                                                      val realVhostFile = OS.Path.joinDirFile
-                                                                              {dir = Config.Apache.confDir,
-                                                                               file = #file spl}
-
-                                                      val realLogDir = OS.Path.joinDirFile
-                                                                           {dir = Config.homeBase,
-                                                                            file = user}
-                                                      val realLogDir = OS.Path.joinDirFile
-                                                                           {dir = realLogDir,
-                                                                            file = "apache"}
-                                                      val realLogDir = OS.Path.joinDirFile
-                                                                           {dir = realLogDir,
-                                                                            file = "log"}
-                                                      val realLogDir = OS.Path.joinDirFile
-                                                                           {dir = realLogDir,
-                                                                            file = Slave.hostname ()}
-                                                      val {base, ...} = OS.Path.splitBaseExt (#file spl)
-                                                      val realLogDir = OS.Path.joinDirFile
-                                                                           {dir = realLogDir,
-                                                                            file = base}
-                                                  in
-                                                      vhostsChanged := true;
-                                                      case #action fs of
-                                                          Slave.Delete =>
-                                                          (if !logDeleted then
-                                                               ()
-                                                           else
-                                                               (ignore (OS.Process.system Config.Apache.down);
-                                                                logDeleted := true);
-                                                           ignore (OS.Process.system (Config.rm
-                                                                                      ^ " -rf "
-                                                                                      ^ realVhostFile));
-                                                           ignore (OS.Process.system (Config.rm
-                                                                                      ^ " -rf "
-                                                                                      ^ realLogDir)))
-                                                        | Slave.Add =>
-                                                          (ignore (OS.Process.system (Config.cp
-                                                                                      ^ " "
-                                                                                      ^ #file fs
-                                                                                      ^ " "
-                                                                                      ^ realVhostFile));
-                                                           if Posix.FileSys.access (realLogDir, []) then
-                                                               ()
-                                                           else
-                                                               OS.FileSys.mkDir realLogDir)
-                                                          
-                                                        | _ =>
-                                                          ignore (OS.Process.system (Config.cp
-                                                                                     ^ " "
-                                                                                     ^ #file fs
-                                                                                     ^ " "
-                                                                                     ^ realVhostFile))
-                                                  end
+                                             orelse String.isSuffix ".vhost_ssl" (#file spl) then let
+                                                  val realVhostFile = OS.Path.joinDirFile
+                                                                          {dir = Config.Apache.confDir,
+                                                                           file = #file spl}
+
+                                                  val user = findVhostUser (#file fs)
+                                                  val oldUser = findVhostUser realVhostFile
+                                              in
+                                                  if (oldUser = NONE andalso #action fs <> Slave.Add)
+                                                     orelse (user = NONE andalso #action fs <> Slave.Delete) then
+                                                      print ("Can't find user in " ^ #file fs ^ " or " ^ realVhostFile ^ "!  Taking no action.\n")
+                                                  else
+                                                      let
+                                                          fun realLogDir user =
+                                                              let
+                                                                  val realLogDir = OS.Path.joinDirFile
+                                                                                       {dir = Config.homeBase,
+                                                                                        file = valOf user}
+                                                                  val realLogDir = OS.Path.joinDirFile
+                                                                                       {dir = realLogDir,
+                                                                                        file = "apache"}
+                                                                  val realLogDir = OS.Path.joinDirFile
+                                                                                       {dir = realLogDir,
+                                                                                        file = "log"}
+                                                                  val realLogDir = OS.Path.joinDirFile
+                                                                                       {dir = realLogDir,
+                                                                                        file = Slave.hostname ()}
+                                                                  val {base, ...} = OS.Path.splitBaseExt (#file spl)
+                                                              in
+                                                                  OS.Path.joinDirFile
+                                                                      {dir = realLogDir,
+                                                                       file = base}
+                                                              end
+                                                      in
+                                                          vhostsChanged := true;
+                                                          case #action fs of
+                                                              Slave.Delete =>
+                                                              (if !logDeleted then
+                                                                   ()
+                                                               else
+                                                                   (ignore (OS.Process.system Config.Apache.down);
+                                                                    logDeleted := true);
+                                                               ignore (OS.Process.system (Config.rm
+                                                                                          ^ " -rf "
+                                                                                          ^ realVhostFile));
+                                                               ignore (OS.Process.system (Config.rm
+                                                                                          ^ " -rf "
+                                                                                          ^ realLogDir oldUser)))
+                                                            | Slave.Add =>
+                                                              let
+                                                                  val rld = realLogDir user
+                                                              in
+                                                                  ignore (OS.Process.system (Config.cp
+                                                                                             ^ " "
+                                                                                             ^ #file fs
+                                                                                             ^ " "
+                                                                                             ^ realVhostFile));
+                                                                  if Posix.FileSys.access (rld, []) then
+                                                                      ()
+                                                                  else
+                                                                      OS.FileSys.mkDir rld
+                                                              end
+                                                              
+                                                            | _ =>
+                                                              (ignore (OS.Process.system (Config.cp
+                                                                                          ^ " "
+                                                                                          ^ #file fs
+                                                                                          ^ " "
+                                                                                          ^ realVhostFile));
+                                                               if user <> oldUser then
+                                                                   let
+                                                                       val old = realLogDir oldUser
+                                                                       val rld = realLogDir user
+                                                                   in
+                                                                       if !logDeleted then
+                                                                           ()
+                                                                       else
+                                                                           (ignore (OS.Process.system Config.Apache.down);
+                                                                            logDeleted := true);
+                                                                       ignore (OS.Process.system (Config.rm
+                                                                                                  ^ " -rf "
+                                                                                                  ^ realLogDir oldUser));
+                                                                       if Posix.FileSys.access (rld, []) then
+                                                                           ()
+                                                                       else
+                                                                           OS.FileSys.mkDir rld
+                                                                   end
+                                                               else
+                                                                   ())
+                                                      end
+                                              end
                                           else
                                               ()
                                       end)
@@ -395,6 +428,10 @@ val () = Env.containerV_one "vhost"
                 write docroot;
                 write "\n\tServerAdmin ";
                 write sadmin;
+                if ssl then
+                    (write "\n\tSSLEngine on")
+                else
+                    ();
                 write "\n";
                 !pre {user = user, nodes = nodes, id = vhostId, hostname = fullHost}
             end,