Changes to support IMAP on hopper all compile but are not tested yet
[hcoop/domtool2.git] / src / main.sml
index 9bc87cd..d46deac 100644 (file)
@@ -1,5 +1,5 @@
 (* HCoop Domtool (http://hcoop.sourceforge.net/)
- * Copyright (c) 2006-2007, Adam Chlipala
+ * Copyright (c) 2006-2009, Adam Chlipala
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
@@ -25,16 +25,32 @@ open Ast MsgTypes Print
 structure SM = StringMap
 
 fun init () = Acl.read Config.aclFile
-           
+
+fun isLib fname = OS.Path.file fname = "lib.dtl"
+
+fun wrapFile (fname, file) =
+    case (isLib fname, file) of
+       (true, (comment, ds, SOME e)) =>
+       let
+           val (_, loc) = e
+       in
+           (comment, ds, SOME (ELocal (e, (ESkip, loc)), loc))
+       end
+      | _ => file
+
 fun check' G fname =
     let
        val prog = Parse.parse fname
+       val prog = wrapFile (fname, prog)
     in
        if !ErrorMsg.anyErrors then
            G
        else
-           (Option.app (Unused.check G) (#3 prog);
-            Tycheck.checkFile G (Defaults.tInit ()) prog)
+           (if isLib fname then
+                ()
+            else
+                Option.app (Unused.check G) (#3 prog);
+            Tycheck.checkFile G (Defaults.tInit prog) prog)
     end
 
 fun basis () =
@@ -64,12 +80,12 @@ fun basis () =
             before Tycheck.disallowExterns ())
     end
 
-fun check fname =
+(* val b = basis () *)
+
+fun check G fname =
     let
        val _ = ErrorMsg.reset ()
        val _ = Env.preTycheck ()
-
-       val b = basis ()
     in
        if !ErrorMsg.anyErrors then
            raise ErrorMsg.Error
@@ -78,17 +94,21 @@ fun check fname =
                val _ = Tycheck.disallowExterns ()
                val _ = ErrorMsg.reset ()
                val prog = Parse.parse fname
+               val prog = wrapFile (fname, prog)
            in
                if !ErrorMsg.anyErrors then
                    raise ErrorMsg.Error
                else
                    let
-                       val G' = Tycheck.checkFile b (Defaults.tInit ()) prog
+                       val G' = Tycheck.checkFile G (Defaults.tInit prog) prog
                    in
                        if !ErrorMsg.anyErrors then
                            raise ErrorMsg.Error
                        else
-                           (Option.app (Unused.check b) (#3 prog);
+                           (if isLib fname then
+                                ()
+                            else
+                                Option.app (Unused.check G) (#3 prog);
                             (G', #3 prog))
                    end
            end
@@ -150,12 +170,12 @@ fun checkDir dname =
     (setupUser ();
      checkDir' dname)
 
-fun reduce fname =
+fun reduce fname =
     let
-       val (G, body) = check fname
+       val (G, body) = check fname
     in
        if !ErrorMsg.anyErrors then
-           NONE
+           (G, NONE)
        else
            case body of
                SOME body =>
@@ -166,28 +186,25 @@ fun reduce fname =
                                         [PD.string "Result:",
                                          PD.space 1,
                                          p_exp body']))*)
-                   SOME body'
+                   (G, SOME body')
                end
-             | _ => NONE
+             | _ => (G, NONE)
     end
 
-fun eval fname =
-    case reduce fname of
-       (SOME body') =>
-       if !ErrorMsg.anyErrors then
-           raise ErrorMsg.Error
-       else
-           Eval.exec (Defaults.eInit ()) body'
-      | NONE => ()
+(*(Defaults.eInit ())*)
 
-fun eval' fname =
-    case reduce fname of
-       (SOME body') =>
+fun eval G evs fname =
+    case reduce fname of
+       (G, SOME body') =>
        if !ErrorMsg.anyErrors then
            raise ErrorMsg.Error
        else
-           ignore (Eval.exec' (Defaults.eInit ()) body')
-      | NONE => ()
+           let
+               val evs' = Eval.exec' evs body'
+           in
+               (G, evs')
+           end
+      | (G, NONE) => (G, evs)
 
 val dispatcher =
     Config.dispatcher ^ ":" ^ Int.toString Config.dispatcherPort
@@ -216,35 +233,55 @@ fun requestContext f =
        (user, context)
     end
 
-fun requestBio f =
+fun requestBio' printErr f =
     let
        val (user, context) = requestContext f
     in
-       (user, OpenSSL.connect (context, dispatcher))
+       (user, OpenSSL.connect printErr (context, dispatcher))
     end
 
-fun requestSlaveBio () =
+val requestBio = requestBio' true
+
+fun requestSlaveBio' printErr =
     let
        val (user, context) = requestContext (fn () => ())
     in
-       (user, OpenSSL.connect (context, self))
+       (user, OpenSSL.connect printErr (context, self))
     end
 
-fun request fname =
-    let
-       val (user, bio) = requestBio (fn () => ignore (check fname))
+fun requestSlaveBio () = requestSlaveBio' true
 
-       val inf = TextIO.openIn fname
+fun request (fname, libOpt) =
+    let
+       val (user, bio) = requestBio (fn () =>
+                                        let
+                                            val env = basis ()
+                                            val env = case libOpt of
+                                                          NONE => env
+                                                        | SOME lib => #1 (check env lib)
+                                        in
+                                            ignore (check env fname)
+                                        end)
+
+       fun readFile fname =
+           let
+               val inf = TextIO.openIn fname
 
-       fun loop lines =
-           case TextIO.inputLine inf of
-               NONE => String.concat (List.rev lines)
-             | SOME line => loop (line :: lines)
+               fun loop lines =
+                   case TextIO.inputLine inf of
+                       NONE => String.concat (rev lines)
+                     | SOME line => loop (line :: lines)
+           in
+               loop []
+               before TextIO.closeIn inf
+           end
 
-       val code = loop []
+       val code = readFile fname
+       val msg = case libOpt of
+                     NONE => MsgConfig code
+                   | SOME fname' => MsgMultiConfig [readFile fname', code]
     in
-       TextIO.closeIn inf;
-       Msg.send (bio, MsgConfig code);
+       Msg.send (bio, msg);
        case Msg.recv bio of
            NONE => print "Server closed connection unexpectedly.\n"
          | SOME m =>
@@ -290,7 +327,7 @@ fun requestDir dname =
        val (_, files) = Order.order (SOME b) files
 
        val _ = if !ErrorMsg.anyErrors then
-                   (print "J\n";raise ErrorMsg.Error)
+                   raise ErrorMsg.Error
                else
                    ()
 
@@ -324,7 +361,7 @@ fun requestDir dname =
 
 fun requestPing () =
     let
-       val (_, bio) = requestBio (fn () => ())
+       val (_, bio) = requestBio' false (fn () => ())
     in
        OpenSSL.close bio;
        OS.Process.success
@@ -337,7 +374,7 @@ fun requestShutdown () =
     in
        Msg.send (bio, MsgShutdown);
        case Msg.recv bio of
-           NONE => print "Server closed connection unexpectedly.\n"
+           NONE => ()
          | SOME m =>
            case m of
                MsgOk => print "Shutdown begun.\n"
@@ -348,7 +385,7 @@ fun requestShutdown () =
 
 fun requestSlavePing () =
     let
-       val (_, bio) = requestSlaveBio ()
+       val (_, bio) = requestSlaveBio' false
     in
        OpenSSL.close bio;
        OS.Process.success
@@ -361,7 +398,7 @@ fun requestSlaveShutdown () =
     in
        Msg.send (bio, MsgShutdown);
        case Msg.recv bio of
-           NONE => print "Server closed connection unexpectedly.\n"
+           NONE => ()
          | SOME m =>
            case m of
                MsgOk => print "Shutdown begun.\n"
@@ -704,10 +741,10 @@ fun requestMysqlFixperms () =
 fun requestApt {node, pkg} =
     let
        val (user, context) = requestContext (fn () => ())
-       val bio = OpenSSL.connect (context, if node = Config.masterNode then
-                                               dispatcher
-                                           else
-                                               Domain.nodeIp node ^ ":" ^ Int.toString Config.slavePort)
+       val bio = OpenSSL.connect true (context, if node = Config.masterNode then
+                                                    dispatcher
+                                                else
+                                                    Domain.nodeIp node ^ ":" ^ Int.toString Config.slavePort)
 
        val _ = Msg.send (bio, MsgQuery (QApt pkg))
 
@@ -733,10 +770,10 @@ fun requestApt {node, pkg} =
 fun requestCron {node, uname} =
     let
        val (user, context) = requestContext (fn () => ())
-       val bio = OpenSSL.connect (context, if node = Config.masterNode then
-                                               dispatcher
-                                           else
-                                               Domain.nodeIp node ^ ":" ^ Int.toString Config.slavePort)
+       val bio = OpenSSL.connect true (context, if node = Config.masterNode then
+                                                    dispatcher
+                                                else
+                                                    Domain.nodeIp node ^ ":" ^ Int.toString Config.slavePort)
 
        val _ = Msg.send (bio, MsgQuery (QCron uname))
 
@@ -762,10 +799,10 @@ fun requestCron {node, uname} =
 fun requestFtp {node, uname} =
     let
        val (user, context) = requestContext (fn () => ())
-       val bio = OpenSSL.connect (context, if node = Config.masterNode then
-                                               dispatcher
-                                           else
-                                               Domain.nodeIp node ^ ":" ^ Int.toString Config.slavePort)
+       val bio = OpenSSL.connect true (context, if node = Config.masterNode then
+                                                    dispatcher
+                                                else
+                                                    Domain.nodeIp node ^ ":" ^ Int.toString Config.slavePort)
 
        val _ = Msg.send (bio, MsgQuery (QFtp uname))
 
@@ -791,10 +828,10 @@ fun requestFtp {node, uname} =
 fun requestTrustedPath {node, uname} =
     let
        val (user, context) = requestContext (fn () => ())
-       val bio = OpenSSL.connect (context, if node = Config.masterNode then
-                                               dispatcher
-                                           else
-                                               Domain.nodeIp node ^ ":" ^ Int.toString Config.slavePort)
+       val bio = OpenSSL.connect true (context, if node = Config.masterNode then
+                                                    dispatcher
+                                                else
+                                                    Domain.nodeIp node ^ ":" ^ Int.toString Config.slavePort)
 
        val _ = Msg.send (bio, MsgQuery (QTrustedPath uname))
 
@@ -820,10 +857,10 @@ fun requestTrustedPath {node, uname} =
 fun requestSocketPerm {node, uname} =
     let
        val (user, context) = requestContext (fn () => ())
-       val bio = OpenSSL.connect (context, if node = Config.masterNode then
-                                               dispatcher
-                                           else
-                                               Domain.nodeIp node ^ ":" ^ Int.toString Config.slavePort)
+       val bio = OpenSSL.connect true (context, if node = Config.masterNode then
+                                                    dispatcher
+                                                else
+                                                    Domain.nodeIp node ^ ":" ^ Int.toString Config.slavePort)
 
        val _ = Msg.send (bio, MsgQuery (QSocket uname))
 
@@ -851,11 +888,11 @@ fun requestSocketPerm {node, uname} =
 fun requestFirewall {node, uname} =
     let
        val (user, context) = requestContext (fn () => ())
-       val bio = OpenSSL.connect (context, if node = Config.masterNode then
-                                               dispatcher
-                                           else
-                                               Domain.nodeIp node ^ ":" ^ Int.toString Config.slavePort)
-
+       val bio = OpenSSL.connect true (context, if node = Config.masterNode then
+                                                    dispatcher
+                                                else
+                                                    Domain.nodeIp node ^ ":" ^ Int.toString Config.slavePort)
+                 
        val _ = Msg.send (bio, MsgQuery (QFirewall uname))
 
        fun loop () =
@@ -890,6 +927,21 @@ fun requestDescribe dom =
        OpenSSL.close bio
     end
 
+fun requestReUsers () =
+    let
+       val (_, bio) = requestBio (fn () => ())
+    in
+       Msg.send (bio, MsgReUsers);
+       case Msg.recv bio of
+           NONE => print "Server closed connection unexpectedly.\n"
+         | SOME m =>
+           case m of
+               MsgOk => print "Callbacks run.\n"
+             | MsgError s => print ("Failed: " ^ s ^ "\n")
+             | _ => print "Unexpected server reply.\n";
+       OpenSSL.close bio
+    end
+
 structure SS = StringSet
 
 fun domainList dname =
@@ -972,10 +1024,10 @@ fun regenerateEither tc checker context =
            if node = Config.defaultNode then
                Domain.resetLocal ()
            else let
-                   val bio = OpenSSL.connect (context,
-                                              ip
-                                              ^ ":"
-                                              ^ Int.toString Config.slavePort)
+                   val bio = OpenSSL.connect true (context,
+                                                   ip
+                                                   ^ ":"
+                                                   ^ Int.toString Config.slavePort)
                in
                    Msg.send (bio, MsgRegenerate);
                    case Msg.recv bio of
@@ -1017,6 +1069,9 @@ fun regenerateEither tc checker context =
 
                        val files = loop []
                        val (_, files) = Order.order (SOME b) files
+
+                       fun checker' (file, (G, evs)) =
+                           checker G evs file
                    in
                        if !ErrorMsg.anyErrors then
                            (ErrorMsg.reset ();
@@ -1024,10 +1079,13 @@ fun regenerateEither tc checker context =
                             ok := false)
                        else
                            ();
-                       app checker files
+                       ignore (foldl checker' (basis (), Defaults.eInit ()) files)
                    end
+               else if String.isSuffix "_admin" user then
+                   ()    
                else
-                   ()
+                   (print ("Couldn't access " ^ user ^ "'s ~/.domtool directory.\n");
+                    ok := false)
            end
            handle IO.Io {name, function, ...} =>
                   (print ("IO error processing user " ^ user ^ ": " ^ function ^ ": " ^ name ^ "\n");
@@ -1062,8 +1120,14 @@ fun regenerateEither tc checker context =
        !ok
     end
 
-val regenerate = regenerateEither false eval'
-val regenerateTc = regenerateEither true (ignore o check)
+val regenerate = regenerateEither false eval
+val regenerateTc = regenerateEither true
+                                   (fn G => fn evs => fn file =>
+                                                         (#1 (check G file), evs))
+
+fun usersChanged () =
+    (Domain.onUsersChange ();
+     ignore (OS.Process.system Config.publish_reusers))
 
 fun rmuser user =
     let
@@ -1074,7 +1138,8 @@ fun rmuser user =
                                     | _ => false) (StringSet.listItems doms)
     in
        Acl.rmuser user;
-       Domain.rmdom doms
+       Domain.rmdom doms;
+       usersChanged ()
     end
 
 fun now () = Date.toString (Date.fromTimeUniv (Time.now ()))
@@ -1099,10 +1164,12 @@ fun describeQuery q =
 
 fun service () =
     let
+       val host = Slave.hostname ()
+
        val () = Acl.read Config.aclFile
-       
-       val context = context (Config.serverCert,
-                              Config.serverKey,
+
+       val context = context (Config.certDir ^ "/" ^ host ^ ".pem",
+                              Config.keyDir ^ "/" ^ host ^ "/key.pem",
                               Config.trustStore)
        val _ = Domain.set_context context
 
@@ -1162,17 +1229,17 @@ fun service () =
 
                             val outname = OS.FileSys.tmpName ()
 
-                            fun doOne code =
+                            fun doOne (code, (G, evs)) =
                                 let
                                     val outf = TextIO.openOut outname
                                 in
                                     TextIO.output (outf, code);
                                     TextIO.closeOut outf;
-                                    eval' outname
+                                    eval G evs outname
                                 end
                         in
                             doIt (fn () => (Env.pre ();
-                                            app doOne codes;
+                                            ignore (foldl doOne (basis (), Defaults.eInit ()) codes);
                                             Env.post ();
                                             Msg.send (bio, MsgOk);
                                             ("Configuration complete.", NONE)))
@@ -1218,6 +1285,10 @@ fun service () =
                                          if Acl.query {user = user, class = "priv", value = "all"} then
                                              (Acl.grant acl;
                                               Acl.write Config.aclFile;
+                                              if #class acl = "user" then
+                                                  usersChanged ()
+                                              else
+                                                  ();
                                               ("Granted permission " ^ #value acl ^ " to " ^ #user acl ^ " in " ^ #class acl ^ ".",
                                                NONE))
                                          else
@@ -1254,11 +1325,12 @@ fun service () =
                               | MsgRmdom doms =>
                                 doIt (fn () =>
                                          if Acl.query {user = user, class = "priv", value = "all"}
-                                            orelse List.all (fn dom => Acl.query {user = user, class = "domain", value = dom}) doms then
+                                            orelse List.all (fn dom => Domain.validDomain dom
+                                                                       andalso Acl.queryDomain {user = user, domain = dom}) doms then
                                              (Domain.rmdom doms;
-                                              app (fn dom =>
+                                              (*app (fn dom =>
                                                       Acl.revokeFromAll {class = "domain", value = dom}) doms;
-                                              Acl.write Config.aclFile;
+                                              Acl.write Config.aclFile;*)
                                               ("Removed domains" ^ foldl (fn (d, s) => s ^ " " ^ d) "" doms ^ ".",
                                                NONE))
                                          else
@@ -1336,18 +1408,22 @@ fun service () =
                                                   SOME ("Error adding user: " ^ msg)))
                                      (fn () => ())
 
-                              | MsgCreateDb {dbtype, dbname} =>
+                              | MsgCreateDb {dbtype, dbname, encoding} =>
                                 doIt (fn () =>
                                          if Dbms.validDbname dbname then
                                              case Dbms.lookup dbtype of
                                                  NONE => ("Database creation request with unknown datatype type " ^ dbtype,
                                                           SOME ("Unknown database type " ^ dbtype))
                                                | SOME handler =>
-                                                 case #createdb handler {user = user, dbname = dbname} of
-                                                     NONE => ("Created database " ^ user ^ "_" ^ dbname ^ ".",
-                                                              NONE)
-                                                   | SOME msg => ("Error creating database " ^ user ^ "_" ^ dbname ^ ": " ^ msg,
-                                                                  SOME ("Error creating database: " ^ msg))
+                                                 if not (Dbms.validEncoding encoding) then
+                                                     ("Invalid encoding " ^ valOf encoding ^ " requested for database creation.",
+                                                      SOME "Invalid encoding")
+                                                 else
+                                                     case #createdb handler {user = user, dbname = dbname, encoding = encoding} of
+                                                         NONE => ("Created database " ^ user ^ "_" ^ dbname ^ ".",
+                                                                  NONE)
+                                                       | SOME msg => ("Error creating database " ^ user ^ "_" ^ dbname ^ ": " ^ msg,
+                                                                      SOME ("Error creating database: " ^ msg))
                                          else
                                              ("Invalid database name " ^ user ^ "_" ^ dbname,
                                               SOME ("Invalid database name " ^ dbname)))
@@ -1503,14 +1579,15 @@ fun service () =
                                      (fn () => ())
 
                               | MsgMysqlFixperms =>
-                                doIt (fn () => if OS.Process.isSuccess
-                                                      (OS.Process.system "/usr/bin/sudo -H /afs/hcoop.net/common/etc/scripts/mysql-grant-table-drop") then
-                                                   ("Requested mysql-fixperms",
-                                                    NONE)
+                                (print "Starting mysql-fixperms\n";
+                                 doIt (fn () => if OS.Process.isSuccess
+                                                       (OS.Process.system "/usr/bin/sudo -H /afs/hcoop.net/common/etc/scripts/mysql-grant-table-drop") then
+                                                    ("Requested mysql-fixperms",
+                                                     NONE)
                                                 else
                                                     ("Requested mysql-fixperms, but execution failed!",
                                                      SOME "Script execution failed."))
-                                     (fn () => ())
+                                      (fn () => ()))
 
                               | MsgDescribe dom =>
                                 doIt (fn () => if not (Domain.validDomain dom) then
@@ -1526,6 +1603,16 @@ fun service () =
                                                      NONE)))
                                      (fn () => ())
 
+                              | MsgReUsers =>
+                                doIt (fn () => if Acl.query {user = user, class = "priv", value = "regen"}
+                                                  orelse Acl.query {user = user, class = "priv", value = "all"} then
+                                                   (usersChanged ();
+                                                    ("Users change callbacks run", NONE))
+                                               else
+                                                   ("Unauthorized user asked to reusers!",
+                                                    SOME "You aren't authorized to regenerate files."))
+                                     (fn () => ())
+
                               | _ =>
                                 doIt (fn () => ("Unexpected command",
                                                 SOME "Unexpected command"))
@@ -1596,69 +1683,80 @@ fun slave () =
        val _ = print ("Slave server starting at " ^ now () ^ "\n")
 
        fun loop () =
-           case OpenSSL.accept sock of
-               NONE => ()
-             | SOME bio =>
-               let
-                   val peer = OpenSSL.peerCN bio
-                   val () = print ("\nConnection from " ^ peer ^ " at " ^ now () ^ "\n")
-               in
-                   if peer = Config.dispatcherName then let
-                           fun loop' files =
-                               case Msg.recv bio of
-                                   NONE => print "Dispatcher closed connection unexpectedly\n"
-                                 | SOME m =>
-                                   case m of
-                                       MsgFile file => loop' (file :: files)
-                                     | MsgDoFiles => (Slave.handleChanges files;
-                                                      Msg.send (bio, MsgOk))
-                                     | MsgRegenerate => (Domain.resetLocal ();
-                                                         Msg.send (bio, MsgOk))
-                                     | _ => (print "Dispatcher sent unexpected command\n";
-                                             Msg.send (bio, MsgError "Unexpected command"))
-                       in
-                           loop' [];
-                           ignore (OpenSSL.readChar bio);
-                           OpenSSL.close bio;
-                           loop ()
-                       end
-                   else if peer = "domtool" then
-                       case Msg.recv bio of
-                           SOME MsgShutdown => (OpenSSL.close bio;
-                                                print ("Shutting down at " ^ now () ^ "\n\n"))
-                         | _ => (OpenSSL.close bio;
-                                 loop ())
-                   else
-                       case Msg.recv bio of
-                           SOME (MsgQuery q) => (print (describeQuery q ^ "\n");
-                                                 Msg.send (bio, answerQuery q);
-                                                 ignore (OpenSSL.readChar bio);
-                                                 OpenSSL.close bio;
-                                                 loop ())
-                         | _ => (OpenSSL.close bio;
-                                 loop ())
-               end handle OpenSSL.OpenSSL s =>
-                          (print ("OpenSSL error: " ^ s ^ "\n");
-                           OpenSSL.close bio
-                                         handle OpenSSL.OpenSSL _ => ();
-                           loop ())
-                        | e as OS.SysErr (s, _) =>
-                          (app (fn s => print (s ^ "\n")) (SMLofNJ.exnHistory e);
-                           print ("System error: "^ s ^ "\n");
-                           OpenSSL.close bio
-                           handle OpenSSL.OpenSSL _ => ();
-                           loop ())
-                        | IO.Io {function, name, ...} =>
-                          (print ("IO error: " ^ function ^ ": " ^ name ^ "\n");
-                           OpenSSL.close bio
-                           handle OpenSSL.OpenSSL _ => ();
-                           loop ())
-                        | e =>
-                          (app (fn s => print (s ^ "\n")) (SMLofNJ.exnHistory e);
-                           print "Uncaught exception!\n";
-                           OpenSSL.close bio
-                           handle OpenSSL.OpenSSL _ => ();
-                           loop ())
+           (case OpenSSL.accept sock of
+                NONE => ()
+              | SOME bio =>
+                let
+                    val peer = OpenSSL.peerCN bio
+                    val () = print ("\nConnection from " ^ peer ^ " at " ^ now () ^ "\n")
+                in
+                    if peer = Config.dispatcherName then let
+                            fun loop' files =
+                                case Msg.recv bio of
+                                    NONE => print "Dispatcher closed connection unexpectedly\n"
+                                  | SOME m =>
+                                    case m of
+                                        MsgFile file => loop' (file :: files)
+                                      | MsgDoFiles => (Slave.handleChanges files;
+                                                       Msg.send (bio, MsgOk))
+                                      | MsgRegenerate => (Domain.resetLocal ();
+                                                          Msg.send (bio, MsgOk))
+                                      | MsgVmailChanged => (if Vmail.doChanged () then
+                                                                Msg.send (bio, MsgOk)
+                                                            else
+                                                                Msg.send (bio, MsgError "userdb update failed"))
+                                      | _ => (print "Dispatcher sent unexpected command\n";
+                                              Msg.send (bio, MsgError "Unexpected command"))
+                        in
+                            loop' [];
+                            ignore (OpenSSL.readChar bio);
+                            OpenSSL.close bio;
+                            loop ()
+                        end
+                    else if peer = "domtool" then
+                        case Msg.recv bio of
+                            SOME MsgShutdown => (OpenSSL.close bio;
+                                                 print ("Shutting down at " ^ now () ^ "\n\n"))
+                          | _ => (OpenSSL.close bio;
+                                  loop ())
+                    else
+                        case Msg.recv bio of
+                            SOME (MsgQuery q) => (print (describeQuery q ^ "\n");
+                                                  Msg.send (bio, answerQuery q);
+                                                  ignore (OpenSSL.readChar bio);
+                                                  OpenSSL.close bio;
+                                                  loop ())
+                          | _ => (OpenSSL.close bio;
+                                  loop ())
+                end handle OpenSSL.OpenSSL s =>
+                           (print ("OpenSSL error: " ^ s ^ "\n");
+                            OpenSSL.close bio
+                            handle OpenSSL.OpenSSL _ => ();
+                            loop ())
+                         | e as OS.SysErr (s, _) =>
+                           (app (fn s => print (s ^ "\n")) (SMLofNJ.exnHistory e);
+                            print ("System error: "^ s ^ "\n");
+                            OpenSSL.close bio
+                            handle OpenSSL.OpenSSL _ => ();
+                            loop ())
+                         | IO.Io {function, name, ...} =>
+                           (print ("IO error: " ^ function ^ ": " ^ name ^ "\n");
+                            OpenSSL.close bio
+                            handle OpenSSL.OpenSSL _ => ();
+                            loop ())
+                         | e =>
+                           (app (fn s => print (s ^ "\n")) (SMLofNJ.exnHistory e);
+                            print "Uncaught exception!\n";
+                            OpenSSL.close bio
+                            handle OpenSSL.OpenSSL _ => ();
+                            loop ()))
+           handle OpenSSL.OpenSSL s =>
+                  (print ("OpenSSL error: " ^ s ^ "\n");
+                   loop ())
+                | e =>
+                  (app (fn s => print (s ^ "\n")) (SMLofNJ.exnHistory e);
+                   print "Uncaught exception!\n";
+                   loop ())
     in
        loop ();
        OpenSSL.shutdown sock