2012-09-08 04:26:47 +00:00
{- git - annex assistant webapp configurator for pairing
-
- Copyright 2012 Joey Hess < joey @ kitenet . net >
-
2012-09-24 18:48:47 +00:00
- Licensed under the GNU AGPL version 3 or higher .
2012-09-08 04:26:47 +00:00
- }
{- # LANGUAGE TypeFamilies, QuasiQuotes, MultiParamTypeClasses, TemplateHaskell, OverloadedStrings, RankNTypes # -}
2012-09-08 19:07:44 +00:00
{- # LANGUAGE CPP # -}
2012-09-08 04:26:47 +00:00
module Assistant.WebApp.Configurators.Pairing where
import Assistant.Pairing
2012-09-10 19:20:18 +00:00
import Assistant.WebApp
import Assistant.WebApp.Types
import Assistant.WebApp.SideBar
2012-11-03 01:13:06 +00:00
import Assistant.WebApp.Configurators.XMPP
import Assistant.Types.Buddies
2012-09-10 19:20:18 +00:00
import Utility.Yesod
2012-09-08 19:21:34 +00:00
# ifdef WITH_PAIRING
2012-09-10 21:53:51 +00:00
import Assistant.Common
2012-09-08 19:21:34 +00:00
import Assistant.Pairing.Network
2012-09-11 04:23:34 +00:00
import Assistant.Pairing.MakeRemote
2012-09-10 19:20:18 +00:00
import Assistant.Ssh
2012-09-09 20:24:34 +00:00
import Assistant.Alert
2012-10-30 18:34:48 +00:00
import Assistant.DaemonStatus
2012-09-08 06:02:39 +00:00
import Utility.Verifiable
2012-09-08 19:21:34 +00:00
import Utility.Network
2012-09-11 07:16:00 +00:00
import Annex.UUID
2012-09-08 19:21:34 +00:00
# endif
2012-11-03 01:13:06 +00:00
# ifdef WITH_XMPP
2012-11-04 02:52:41 +00:00
import Assistant.XMPP
2012-11-03 01:13:06 +00:00
import Assistant.XMPP.Client
2012-11-03 20:00:38 +00:00
import Assistant.XMPP.Buddies
2012-11-03 01:13:06 +00:00
import Network.Protocol.XMPP
2012-11-03 18:25:06 +00:00
import Assistant.Types.NetMessager
import Assistant.NetMessager
2012-11-03 01:13:06 +00:00
# endif
2012-10-25 22:17:32 +00:00
import Utility.UserInfo
2012-11-05 16:21:13 +00:00
import Git
2012-09-08 04:26:47 +00:00
import Yesod
import Data.Text ( Text )
2012-09-08 19:21:34 +00:00
# ifdef WITH_PAIRING
2012-09-08 04:26:47 +00:00
import qualified Data.Text as T
2012-09-08 06:02:39 +00:00
import qualified Data.Text.Encoding as T
import qualified Data.ByteString.Lazy as B
import Data.Char
2012-09-09 20:24:34 +00:00
import qualified Control.Exception as E
import Control.Concurrent
2012-09-08 19:21:34 +00:00
# endif
2012-11-03 21:34:19 +00:00
# ifdef WITH_XMPP
import qualified Data.Set as S
# endif
2012-09-08 04:26:47 +00:00
2012-11-03 01:13:06 +00:00
{- Starts either kind of pairing. -}
2012-09-08 04:26:47 +00:00
getStartPairR :: Handler RepHtml
2012-11-03 01:13:06 +00:00
# ifdef WITH_XMPP
2012-11-03 20:00:38 +00:00
getStartPairR = do
xmppconfigured <- isJust <$> runAnnex Nothing getXMPPCreds
2012-11-03 01:13:06 +00:00
# ifdef WITH_PAIRING
let localsupported = True
# else
let localsupported = False
# endif
2012-11-03 18:25:06 +00:00
{- Ask buddies to send presence info, to get the buddy list
- populated . - }
2012-11-03 20:00:38 +00:00
liftAssistant $ sendNetMessage QueryPresence
pairPage $
$ ( widgetFile " configurators/pairing/start " )
2012-11-03 01:13:06 +00:00
# else
# ifdef WITH_PAIRING
getStartPairR = redirect StartLocalPairR
# else
getStartPairR = noPairing " local or jabber "
# endif
# endif
2012-11-03 20:00:38 +00:00
{- Starts pairing with an XMPP buddy, or with other clients sharing an
- XMPP account . - }
2012-11-03 21:34:19 +00:00
getStartXMPPPairR :: BuddyKey -> Handler RepHtml
2012-11-03 01:13:06 +00:00
# ifdef WITH_XMPP
2012-11-03 21:34:19 +00:00
getStartXMPPPairR bid = do
buddy <- liftAssistant $ getBuddy bid <<~ buddyList
2012-11-05 19:40:56 +00:00
go $ S . toList . buddyAssistants <$> buddy
where
go ( Just ( clients @ ( ( Client exemplar ) : _ ) ) ) = do
creds <- runAnnex Nothing getXMPPCreds
let ourjid = fromJust $ parseJID =<< xmppJID <$> creds
let samejid = baseJID ourjid == baseJID exemplar
let account = formatJID $ baseJID exemplar
liftAssistant $ do
u <- liftAnnex getUUID
forM_ clients $ \ ( Client c ) -> sendNetMessage $
PairingNotification PairReq ( formatJID c ) u
pairPage $ do
let name = buddyName exemplar
$ ( widgetFile " configurators/pairing/xmpp/inprogress " )
-- A buddy could have logged out, or the XMPP client restarted,
-- and there be no clients to message; handle unforseen by going back.
go _ = redirect StartPairR
2012-11-03 01:13:06 +00:00
# else
2012-11-03 21:34:19 +00:00
getStartXMPPPairR _ = noXMPPPairing
noXMPPPairing :: Handler RepHtml
noXMPPPairing = noPairing " XMPP "
2012-11-03 01:13:06 +00:00
# endif
{- Starts local pairing. -}
getStartLocalPairR :: Handler RepHtml
2012-09-08 19:07:44 +00:00
# ifdef WITH_PAIRING
2012-11-03 01:13:06 +00:00
getStartLocalPairR = promptSecret Nothing $
startLocalPairing PairReq noop pairingAlert Nothing
2012-09-10 21:53:51 +00:00
# else
2012-11-03 01:13:06 +00:00
getStartLocalPairR = noLocalPairing
noLocalPairing :: Handler RepHtml
noLocalPairing = noPairing " local "
2012-09-10 21:53:51 +00:00
# endif
2012-11-03 01:13:06 +00:00
{- Runs on the system that responds to a local pair request; sets up the ssh
2012-09-11 04:23:34 +00:00
- authorized key first so that the originating host can immediately sync
- with us . - }
2012-11-03 01:13:06 +00:00
getFinishLocalPairR :: PairMsg -> Handler RepHtml
2012-09-10 21:53:51 +00:00
# ifdef WITH_PAIRING
2012-11-03 01:13:06 +00:00
getFinishLocalPairR msg = promptSecret ( Just msg ) $ \ _ secret -> do
2012-11-05 16:21:13 +00:00
repodir <- lift $ repoPath <$> runAnnex undefined gitRepo
liftIO $ setup repodir
startLocalPairing PairAck ( cleanup repodir ) alert uuid " " secret
2012-10-31 06:34:03 +00:00
where
alert = pairRequestAcknowledgedAlert ( pairRepo msg ) . Just
2012-11-05 16:21:13 +00:00
setup repodir = setupAuthorizedKeys msg repodir
cleanup repodir = removeAuthorizedKeys False repodir $
2012-10-31 06:34:03 +00:00
remoteSshPubKey $ pairMsgData msg
uuid = Just $ pairUUID $ pairMsgData msg
2012-09-10 21:53:51 +00:00
# else
2012-11-03 01:13:06 +00:00
getFinishLocalPairR _ = noLocalPairing
2012-09-10 21:53:51 +00:00
# endif
2012-11-03 21:34:19 +00:00
getFinishXMPPPairR :: PairKey -> Handler RepHtml
# ifdef WITH_XMPP
getFinishXMPPPairR ( PairKey u t ) = case parseJID t of
Nothing -> error " bad JID "
Just jid -> error " TODO "
# else
getFinishXMPPPairR _ _ = noXMPPPairing
# endif
2012-11-03 01:13:06 +00:00
getRunningLocalPairR :: SecretReminder -> Handler RepHtml
2012-09-10 21:53:51 +00:00
# ifdef WITH_PAIRING
2012-11-03 01:13:06 +00:00
getRunningLocalPairR s = pairPage $ do
2012-09-11 16:26:42 +00:00
let secret = fromSecretReminder s
2012-11-03 01:13:06 +00:00
$ ( widgetFile " configurators/pairing/local/inprogress " )
2012-09-10 21:53:51 +00:00
# else
2012-11-03 01:13:06 +00:00
getRunningLocalPairR _ = noLocalPairing
2012-09-10 21:53:51 +00:00
# endif
# ifdef WITH_PAIRING
2012-11-03 01:13:06 +00:00
{- Starts local pairing, at either the PairReq (initiating host) or
2012-09-10 21:53:51 +00:00
- PairAck ( responding host ) stage .
-
- Displays an alert , and starts a thread sending the pairing message ,
- which will continue running until the other host responds , or until
- canceled by the user . If canceled by the user , runs the oncancel action .
-
- Redirects to the pairing in progress page .
- }
2012-11-03 01:13:06 +00:00
startLocalPairing :: PairStage -> IO () -> ( AlertButton -> Alert ) -> Maybe UUID -> Text -> Secret -> Widget
startLocalPairing stage oncancel alert muuid displaysecret secret = do
2012-09-09 20:24:34 +00:00
urlrender <- lift getUrlRender
2012-09-11 19:51:27 +00:00
reldir <- fromJust . relDir <$> lift getYesod
2012-10-30 21:14:26 +00:00
sendrequests <- lift $ liftAssistant $ asIO2 $ mksendrequests urlrender
2012-09-11 19:51:27 +00:00
{- Generating a ssh key pair can take a while, so do it in the
- background . - }
2012-10-30 21:14:26 +00:00
thread <- lift $ liftAssistant $ asIO $ do
keypair <- liftIO $ genSshKeyPair
pairdata <- liftIO $ PairData
2012-09-11 19:51:27 +00:00
<$> getHostname
2012-10-25 22:17:32 +00:00
<*> myUserName
2012-09-11 19:51:27 +00:00
<*> pure reldir
<*> pure ( sshPubKey keypair )
<*> ( maybe genUUID return muuid )
2012-09-11 16:58:00 +00:00
let sender = multicastPairMsg Nothing secret pairdata
let pip = PairingInProgress secret Nothing keypair pairdata stage
2012-10-30 21:14:26 +00:00
startSending pip stage $ sendrequests sender
void $ liftIO $ forkIO thread
2012-09-11 19:51:27 +00:00
2012-11-03 01:13:06 +00:00
lift $ redirect $ RunningLocalPairR $ toSecretReminder displaysecret
2012-10-31 06:34:03 +00:00
where
{- Sends pairing messages until the thread is killed,
- and shows an activity alert while doing it .
-
- The cancel button returns the user to the HomeR . This is
- not ideal , but they have to be sent somewhere , and could
- have been on a page specific to the in - process pairing
- that just stopped , so can't go back there .
- }
mksendrequests urlrender sender _stage = do
tid <- liftIO myThreadId
let selfdestruct = AlertButton
{ buttonLabel = " Cancel "
, buttonUrl = urlrender HomeR
, buttonAction = Just $ const $ do
oncancel
killThread tid
}
alertDuring ( alert selfdestruct ) $ liftIO $ do
_ <- E . try ( sender stage ) :: IO ( Either E . SomeException () )
return ()
2012-09-08 04:26:47 +00:00
2012-09-08 06:02:39 +00:00
data InputSecret = InputSecret { secretText :: Maybe Text }
2012-09-10 21:53:51 +00:00
{- If a PairMsg is passed in, ensures that the user enters a secret
- that can validate it . - }
2012-09-09 01:06:10 +00:00
promptSecret :: Maybe PairMsg -> ( Text -> Secret -> Widget ) -> Handler RepHtml
2012-09-10 21:53:51 +00:00
promptSecret msg cont = pairPage $ do
2012-09-08 06:02:39 +00:00
( ( result , form ) , enctype ) <- lift $
runFormGet $ renderBootstrap $
InputSecret <$> aopt textField " Secret phrase " Nothing
case result of
FormSuccess v -> do
2012-09-08 17:04:19 +00:00
let rawsecret = fromMaybe " " $ secretText v
let secret = toSecret rawsecret
2012-09-09 01:06:10 +00:00
case msg of
2012-09-08 06:02:39 +00:00
Nothing -> case secretProblem secret of
2012-09-08 17:04:19 +00:00
Nothing -> cont rawsecret secret
2012-09-08 06:02:39 +00:00
Just problem ->
showform form enctype $ Just problem
2012-09-09 01:06:10 +00:00
Just m ->
2012-09-10 21:53:51 +00:00
if verify ( fromPairMsg m ) secret
2012-09-08 17:04:19 +00:00
then cont rawsecret secret
2012-09-08 06:02:39 +00:00
else showform form enctype $ Just
" That's not the right secret phrase. "
_ -> showform form enctype Nothing
2012-10-31 06:34:03 +00:00
where
showform form enctype mproblem = do
let start = isNothing msg
let badphrase = isJust mproblem
let problem = fromMaybe " " mproblem
let ( username , hostname ) = maybe ( " " , " " )
( \ ( _ , v , a ) -> ( T . pack $ remoteUserName v , T . pack $ fromMaybe ( showAddr a ) ( remoteHostName v ) ) )
( verifiableVal . fromPairMsg <$> msg )
u <- T . pack <$> liftIO myUserName
let sameusername = username == u
let authtoken = webAppFormAuthToken
2012-11-03 01:13:06 +00:00
$ ( widgetFile " configurators/pairing/local/prompt " )
2012-09-08 06:02:39 +00:00
{- This counts unicode characters as more than one character,
- but that's ok ; they * do * provide additional entropy . - }
secretProblem :: Secret -> Maybe Text
secretProblem s
| B . null s = Just " The secret phrase cannot be left empty. (Remember that punctuation and white space is ignored.) "
| B . length s < 7 = Just " Enter a longer secret phrase, at least 6 characters, but really, a phrase is best! This is not a password you'll need to enter every day. "
| s == toSecret sampleQuote = Just " Speaking of foolishness, don't paste in the example I gave. Enter a different phrase, please! "
| otherwise = Nothing
toSecret :: Text -> Secret
toSecret s = B . fromChunks [ T . encodeUtf8 $ T . toLower $ T . filter isAlphaNum s ]
{- From Dickens -}
sampleQuote :: Text
sampleQuote = T . unwords
[ " It was the best of times, "
, " it was the worst of times, "
, " it was the age of wisdom, "
, " it was the age of foolishness. "
]
2012-09-08 17:04:19 +00:00
2012-09-08 19:07:44 +00:00
# else
2012-11-03 01:13:06 +00:00
noPairing :: Text -> Handler RepHtml
noPairing pairingtype = pairPage $
2012-09-09 03:32:08 +00:00
$ ( widgetFile " configurators/pairing/disabled " )
2012-09-08 19:07:44 +00:00
# endif
2012-10-26 16:56:19 +00:00
pairPage :: Widget -> Handler RepHtml
pairPage w = bootstrap ( Just Config ) $ do
sideBarDisplay
setTitle " Pairing "
w