Commits

Bryan O'Sullivan committed 20aa9bc

Add SSL connection support.

Comments (0)

Files changed (2)

Database/MySQL.hs

     -- $mgmt
     -- * Types
       ConnectInfo(..)
+    , SSLInfo(..)
     , Option(..)
     , defaultConnectInfo
+    , defaultSSLInfo
     , Connection
     , Result
     , Field
     , connectDatabase :: String
     , connectOptions :: [Option]
     , connectPath :: FilePath
+    , connectSSL :: Maybe SSLInfo
+    } deriving (Eq, Read, Show, Typeable)
+
+data SSLInfo = SSLInfo {
+      sslKey :: FilePath
+    , sslCert :: FilePath
+    , sslCA :: FilePath
+    , sslCAPath :: FilePath
+    , sslCiphers :: String -- ^ Comma-separated list of cipher names.
     } deriving (Eq, Read, Show, Typeable)
 
 data MySQLError = ConnectionError {
                      , connectDatabase = "test"
                      , connectOptions = []
                      , connectPath = ""
+                     , connectSSL = Nothing
                      }
 
+defaultSSLInfo :: SSLInfo
+defaultSSLInfo = SSLInfo {
+                   sslKey = ""
+                 , sslCert = ""
+                 , sslCA = ""
+                 , sslCAPath = ""
+                 , sslCiphers = ""
+                 }
+
 connect :: ConnectInfo -> IO Connection
 connect ConnectInfo{..} = do
   closed <- newIORef False
   ptr0 <- mysql_init nullPtr
+  case connectSSL of
+    Nothing -> return ()
+    Just SSLInfo{..} -> withString sslKey $ \ckey ->
+                         withString sslCert $ \ccert ->
+                          withString sslCA $ \cca ->
+                           withString sslCAPath $ \ccapath ->
+                            withString sslCiphers $ \ccipher ->
+                             mysql_ssl_set ptr0 ckey ccert cca ccapath ccipher
+                             >> return ()
   ptr <- withString connectHost $ \chost ->
           withString connectUser $ \cuser ->
            withString connectPassword $ \cpass ->

Database/MySQL/C.hsc

     (
     -- * Connection management
       mysql_init
+    , mysql_ssl_set
     , mysql_real_connect
     , mysql_close
     , mysql_ping
     -> IO (Ptr MYSQL)
 
 foreign import ccall unsafe mysql_real_connect
-    :: Ptr MYSQL -- ^ context (from 'mysql_init')
-    -> CString   -- ^ hostname
-    -> CString   -- ^ username
-    -> CString   -- ^ password
-    -> CString   -- ^ database
-    -> CInt      -- ^ port
-    -> CString   -- ^ unix socket
+    :: Ptr MYSQL -- ^ Context (from 'mysql_init').
+    -> CString   -- ^ Host name.
+    -> CString   -- ^ User name.
+    -> CString   -- ^ Password.
+    -> CString   -- ^ Database.
+    -> CInt      -- ^ Port.
+    -> CString   -- ^ Unix socket.
     -> IO (Ptr MYSQL)
 
+foreign import ccall safe mysql_ssl_set
+    :: Ptr MYSQL
+    -> CString                  -- ^ Key.
+    -> CString                  -- ^ Cert.
+    -> CString                  -- ^ CA.
+    -> CString                  -- ^ CA path.
+    -> CString                  -- ^ Ciphers.
+    -> IO MyBool
+
 foreign import ccall unsafe mysql_close
     :: Ptr MYSQL -> IO ()