Fix dumb message name; allow numbers and underscores in database names
[hcoop/domtool2.git] / src / main.sml
index 6644d76..660c892 100644 (file)
@@ -176,6 +176,7 @@ fun context x =
     (OpenSSL.context false x)
     handle e as OpenSSL.OpenSSL s =>
           (print "Couldn't find your certificate.\nYou probably haven't been given any Domtool privileges.\n";
+           print ("I looked in: " ^ #1 x ^ "\n");
            print ("Additional information: " ^ s ^ "\n");
            raise e)
 
@@ -256,7 +257,7 @@ fun requestDir dname =
                else
                    (print ("Can't access " ^ dname ^ ".\n");
                     print "Did you mean to run domtool on a specific file, instead of asking for all\n";
-                    print "files in your ~/domtool directory?\n";
+                    print "files in your ~/.domtool directory?\n";
                     OS.Process.exit OS.Process.failure)
 
        val _ = ErrorMsg.reset ()
@@ -523,7 +524,7 @@ fun requestDbTable p =
     let
        val (user, bio) = requestBio (fn () => ())
     in
-       Msg.send (bio, MsgCreateDbTable p);
+       Msg.send (bio, MsgCreateDb p);
        case Msg.recv bio of
            NONE => print "Server closed connection unexpectedly.\n"
          | SOME m =>
@@ -549,6 +550,21 @@ fun requestDbDrop p =
        OpenSSL.close bio
     end
 
+fun requestDbGrant p =
+    let
+       val (user, bio) = requestBio (fn () => ())
+    in
+       Msg.send (bio, MsgGrantDb p);
+       case Msg.recv bio of
+           NONE => print "Server closed connection unexpectedly.\n"
+         | SOME m =>
+           case m of
+               MsgOk => print ("You've been granted all allowed privileges to database " ^ user ^ "_" ^ #dbname p ^ ".\n")
+             | MsgError s => print ("Grant failed: " ^ s ^ "\n")
+             | _ => print "Unexpected server reply.\n";
+       OpenSSL.close bio
+    end
+
 fun requestListMailboxes domain =
     let
        val (_, bio) = requestBio (fn () => ())
@@ -560,7 +576,7 @@ fun requestListMailboxes domain =
             case m of
                 MsgMailboxes users => (Msg.send (bio, MsgOk);
                                        Vmail.Listing users)
-              | MsgError s => Vmail.Error ("Creation failed: " ^ s)
+              | MsgError s => Vmail.Error ("Listing failed: " ^ s)
               | _ => Vmail.Error "Unexpected server reply.")
        before OpenSSL.close bio
     end
@@ -664,6 +680,21 @@ fun requestSmtpLog domain =
        OpenSSL.close bio
     end
 
+fun requestMysqlFixperms () =
+    let
+       val (_, bio) = requestBio (fn () => ())
+    in
+       Msg.send (bio, MsgMysqlFixperms);
+       case Msg.recv bio of
+           NONE => print "Server closed connection unexpectedly.\n"
+         | SOME m =>
+           case m of
+               MsgOk => print "Permissions granted.\n"
+             | MsgError s => print ("Failed: " ^ s ^ "\n")
+             | _ => print "Unexpected server reply.\n";
+       OpenSSL.close bio
+    end
+
 fun requestApt {node, pkg} =
     let
        val (user, context) = requestContext (fn () => ())
@@ -908,7 +939,9 @@ fun regenerate context =
                else
                    ()
            end
-           handle IO.Io _ => ()
+           handle IO.Io {name, function, ...} =>
+                  (print ("IO error processing user " ^ user ^ ": " ^ function ^ ": " ^ name ^ "\n");
+                   ok := false)
                 | OS.SysErr (s, _) => (print ("System error processing user " ^ user ^ ": " ^ s ^ "\n");
                                        ok := false)
                 | ErrorMsg.Error => (ErrorMsg.reset ();
@@ -1252,7 +1285,7 @@ fun service () =
                                                   SOME ("Error adding user: " ^ msg)))
                                      (fn () => ())
 
-                              | MsgCreateDbTable {dbtype, dbname} =>
+                              | MsgCreateDb {dbtype, dbname} =>
                                 doIt (fn () =>
                                          if Dbms.validDbname dbname then
                                              case Dbms.lookup dbtype of
@@ -1286,6 +1319,23 @@ fun service () =
                                               SOME ("Invalid database name " ^ dbname)))
                                      (fn () => ())
 
+                              | MsgGrantDb {dbtype, dbname} =>
+                                doIt (fn () =>
+                                         if Dbms.validDbname dbname then
+                                             case Dbms.lookup dbtype of
+                                                 NONE => ("Database drop request with unknown datatype type " ^ dbtype,
+                                                          SOME ("Unknown database type " ^ dbtype))
+                                               | SOME handler =>
+                                                 case #grant handler {user = user, dbname = dbname} of
+                                                     NONE => ("Grant permissions to database " ^ user ^ "_" ^ dbname ^ ".",
+                                                              NONE)
+                                                   | SOME msg => ("Error granting permissions to database " ^ user ^ "_" ^ dbname ^ ": " ^ msg,
+                                                                  SOME ("Error granting permissions to database: " ^ msg))
+                                         else
+                                             ("Invalid database name " ^ user ^ "_" ^ dbname,
+                                              SOME ("Invalid database name " ^ dbname)))
+                                     (fn () => ())
+
                               | MsgListMailboxes domain =>
                                 doIt (fn () =>
                                          if not (Domain.yourDomain domain) then
@@ -1313,7 +1363,8 @@ fun service () =
                                               SOME "Invalid password; may only contain printable, non-space characters")
                                          else if not (Domain.yourPath mailbox) then
                                              ("User wasn't authorized to add a mailbox at " ^ mailbox,
-                                              SOME "You're not authorized to use that mailbox location.")
+                                              SOME ("You're not authorized to use that mailbox location. ("
+                                                    ^ mailbox ^ ")"))
                                          else
                                              case Vmail.add {requester = user,
                                                              domain = domain, user = emailUser,
@@ -1400,6 +1451,16 @@ fun service () =
                                                  NONE)))
                                      (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)
+                                                else
+                                                    ("Requested mysql-fixperms, but execution failed!",
+                                                     SOME "Script execution failed."))
+                                     (fn () => ())
+
                               | _ =>
                                 doIt (fn () => ("Unexpected command",
                                                 SOME "Unexpected command"))