Browse Source

Fix loggin in Network.Xmpp.Tls and minor cleanups

master
Philipp Balzarek 13 years ago
parent
commit
c7c4a29219
  1. 1
      source/Network/Xmpp/Concurrent.hs
  2. 12
      source/Network/Xmpp/Sasl/Common.hs
  3. 6
      source/Network/Xmpp/Sasl/Mechanisms/Scram.hs
  4. 27
      source/Network/Xmpp/Tls.hs

1
source/Network/Xmpp/Concurrent.hs

@ -42,7 +42,6 @@ import Network.Xmpp.Tls
import Network.Xmpp.Types import Network.Xmpp.Types
import Network.Xmpp.Utilities import Network.Xmpp.Utilities
runHandlers :: (TChan Stanza) -> [StanzaHandler] -> Stanza -> IO () runHandlers :: (TChan Stanza) -> [StanzaHandler] -> Stanza -> IO ()
runHandlers _ [] _ = return () runHandlers _ [] _ = return ()
runHandlers outC (h:hands) sta = do runHandlers outC (h:hands) sta = do

12
source/Network/Xmpp/Sasl/Common.hs

@ -186,12 +186,12 @@ prepCredentials authcid authzid password = case credentials of
Just creds -> return creds Just creds -> return creds
where where
credentials = do credentials = do
ac <- normalizeUsername authcid ac <- normalizeUsername authcid
az <- case authzid of az <- case authzid of
Nothing -> Just Nothing Nothing -> Just Nothing
Just az' -> Just <$> normalizeUsername az' Just az' -> Just <$> normalizeUsername az'
pw <- normalizePassword password pw <- normalizePassword password
return (ac, az, pw) return (ac, az, pw)
-- | Bit-wise xor of byte strings -- | Bit-wise xor of byte strings
xorBS :: BS.ByteString -> BS.ByteString -> BS.ByteString xorBS :: BS.ByteString -> BS.ByteString -> BS.ByteString

6
source/Network/Xmpp/Sasl/Mechanisms/Scram.hs

@ -72,9 +72,9 @@ scram hToken authcid authzid password = do
gs2Header :: BS.ByteString gs2Header :: BS.ByteString
gs2Header = merge $ [ gs2CbindFlag gs2Header = merge $ [ gs2CbindFlag
, maybe "" id authzid'' , maybe "" id authzid''
, "" , ""
] ]
-- cbindData :: BS.ByteString -- cbindData :: BS.ByteString
-- cbindData = "" -- we don't support channel binding yet -- cbindData = "" -- we don't support channel binding yet

27
source/Network/Xmpp/Tls.hs

@ -18,7 +18,7 @@ import Data.XML.Types
import Network.TLS import Network.TLS
import Network.Xmpp.Stream import Network.Xmpp.Stream
import Network.Xmpp.Types import Network.Xmpp.Types
import System.Log.Logger (debugM, errorM) import System.Log.Logger (debugM, errorM, infoM)
mkBackend :: StreamHandle -> Backend mkBackend :: StreamHandle -> Backend
mkBackend con = Backend { backendSend = \bs -> void (streamSend con bs) mkBackend con = Backend { backendSend = \bs -> void (streamSend con bs)
@ -51,27 +51,29 @@ tls con = Ex.handle (return . Left . TlsError)
case sState of case sState of
Plain -> return () Plain -> return ()
Closed -> do Closed -> do
liftIO $ errorM "Pontarius.XMPP" "startTls: The stream is closed." liftIO $ errorM "Pontarius.Xmpp" "startTls: The stream is closed."
throwError XmppNoStream throwError XmppNoStream
Secured -> do Secured -> do
liftIO $ errorM "Pontarius.XMPP" "startTls: The stream is already secured." liftIO $ errorM "Pontarius.Xmpp" "startTls: The stream is already secured."
throwError TlsStreamSecured throwError TlsStreamSecured
features <- lift $ gets streamFeatures features <- lift $ gets streamFeatures
case (tlsBehaviour conf, streamTls features) of case (tlsBehaviour conf, streamTls features) of
(RequireTls , Just _ ) -> startTls (RequireTls , Just _ ) -> startTls
(RequireTls , Nothing ) -> throwError TlsNoServerSupport (RequireTls , Nothing ) -> throwError TlsNoServerSupport
(PreferTls , Just _ ) -> startTls (PreferTls , Just _ ) -> startTls
(PreferTls , Nothing ) -> return () (PreferTls , Nothing ) -> skipTls
(PreferPlain , Just True) -> startTls (PreferPlain , Just True) -> startTls
(PreferPlain , _ ) -> return () (PreferPlain , _ ) -> skipTls
(RefuseTls , Just True) -> throwError XmppOtherFailure (RefuseTls , Just True) -> throwError XmppOtherFailure
(RefuseTls , _ ) -> return () (RefuseTls , _ ) -> skipTls
where where
skipTls = liftIO $ infoM "Pontarius.Xmpp" "Skipping TLS negotiation"
startTls = do startTls = do
liftIO $ infoM "Pontarius.Xmpp" "Running StartTLS"
params <- gets $ tlsParams . streamConfiguration params <- gets $ tlsParams . streamConfiguration
sent <- ErrorT $ pushElement starttlsE sent <- ErrorT $ pushElement starttlsE
unless sent $ do unless sent $ do
liftIO $ errorM "Pontarius.XMPP" "startTls: Could not sent stanza." liftIO $ errorM "Pontarius.Xmpp" "startTls: Could not sent stanza."
throwError XmppOtherFailure throwError XmppOtherFailure
answer <- lift $ pullElement answer <- lift $ pullElement
case answer of case answer of
@ -79,10 +81,10 @@ tls con = Ex.handle (return . Left . TlsError)
Right (Element "{urn:ietf:params:xml:ns:xmpp-tls}proceed" [] []) -> Right (Element "{urn:ietf:params:xml:ns:xmpp-tls}proceed" [] []) ->
return () return ()
Right (Element "{urn:ietf:params:xml:ns:xmpp-tls}failure" _ _) -> do Right (Element "{urn:ietf:params:xml:ns:xmpp-tls}failure" _ _) -> do
liftIO $ errorM "Pontarius.XMPP" "startTls: TLS initiation failed." liftIO $ errorM "Pontarius.Xmpp" "startTls: TLS initiation failed."
throwError XmppOtherFailure throwError XmppOtherFailure
Right r -> Right r ->
liftIO $ errorM "Pontarius.XMPP" $ liftIO $ errorM "Pontarius.Xmpp" $
"startTls: Unexpected element: " ++ show r "startTls: Unexpected element: " ++ show r
hand <- gets streamHandle hand <- gets streamHandle
(_raw, _snk, psh, recv, ctx) <- lift $ tlsinit params (mkBackend hand) (_raw, _snk, psh, recv, ctx) <- lift $ tlsinit params (mkBackend hand)
@ -92,6 +94,7 @@ tls con = Ex.handle (return . Left . TlsError)
, streamClose = bye ctx >> streamClose hand , streamClose = bye ctx >> streamClose hand
} }
lift $ modify ( \x -> x {streamHandle = newHand}) lift $ modify ( \x -> x {streamHandle = newHand})
liftIO $ infoM "Pontarius.Xmpp" "Stream Secured."
either (lift . Ex.throwIO) return =<< lift restartStream either (lift . Ex.throwIO) return =<< lift restartStream
modify (\s -> s{streamConnectionState = Secured}) modify (\s -> s{streamConnectionState = Secured})
return () return ()
@ -127,15 +130,11 @@ tlsinit params backend = do
Nothing -> return () Nothing -> return ()
Just x -> do Just x -> do
sendData con (BL.fromChunks [x]) sendData con (BL.fromChunks [x])
liftIO $ debugM "Pontarius.Xmpp.TLS"
("out :" ++ BSC8.unpack x)
snk snk
readWithBuffer <- liftIO $ mkReadBuffer (recvData con) readWithBuffer <- liftIO $ mkReadBuffer (recvData con)
return ( src return ( src
, snk , snk
, \s -> do , \s -> sendData con $ BL.fromChunks [s]
liftIO $ debugM "Pontarius.Xmpp.TLS" ("out :" ++ BSC8.unpack s)
sendData con $ BL.fromChunks [s]
, liftIO . readWithBuffer , liftIO . readWithBuffer
, con , con
) )

Loading…
Cancel
Save