2012-09-08 04:26:47 +00:00
{- git - annex assistant webapp configurator for pairing
-
- Pairing works like this :
-
2012-09-08 06:02:39 +00:00
- * The user opens StartPairR , which prompts them for a secret .
2012-09-08 17:04:19 +00:00
- * The user submits it . The pairing secret is stored for later .
- A PairReq is broadcast out .
2012-09-08 04:26:47 +00:00
- * On another device , it's received , and that causes its webapp to
- display an Alert .
- * The user there clicks the button , which opens FinishPairR ,
- which prompts them for the same secret .
- * The secret is used to verify the PairReq . If it checks out ,
- a PairAck is sent , and the other device adds the ssh key from the
2012-09-10 21:53:51 +00:00
- PairReq to its authorized_keys , and sets up the remote .
2012-09-08 04:26:47 +00:00
- * The PairAck is received back at the device that started the process .
- It's verified using the stored secret . The ssh key from the PairAck
- is added . An Alert is displayed noting that the pairing has been set
2012-09-08 17:04:19 +00:00
- up . The pairing secret is removed to prevent anyone cracking the
2012-09-10 21:53:51 +00:00
- crypto . Syncing starts . A PairDone is sent .
- * The PairDone is received , and an alert shown indicating pairing is
- done .
2012-09-08 04:26:47 +00:00
-
- Copyright 2012 Joey Hess < joey @ kitenet . net >
-
- Licensed under the GNU GPL version 3 or higher .
- }
{- # 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
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-10 19:20:18 +00:00
import Assistant.Ssh
2012-09-09 20:24:34 +00:00
import Assistant.Alert
2012-09-08 17:04:19 +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
# endif
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
import System.Posix.User
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-09-08 04:26:47 +00:00
getStartPairR :: Handler RepHtml
2012-09-08 19:07:44 +00:00
# ifdef WITH_PAIRING
2012-09-11 01:55:59 +00:00
getStartPairR = promptSecret Nothing $ startPairing PairReq noop
2012-09-10 21:53:51 +00:00
# else
getStartPairR = noPairing
# endif
getFinishPairR :: PairMsg -> Handler RepHtml
# ifdef WITH_PAIRING
getFinishPairR msg = promptSecret ( Just msg ) $ \ _ secret -> do
2012-09-11 01:55:59 +00:00
setup
startPairing PairAck cleanup " " secret
2012-09-10 21:53:51 +00:00
where
pubkey = remoteSshPubKey $ pairMsgData msg
setup = do
2012-09-11 01:55:59 +00:00
liftIO $ validateSshPubKey pubkey
2012-09-10 21:53:51 +00:00
unlessM ( liftIO $ makeAuthorizedKeys False pubkey ) $
error " failed setting up ssh authorized keys "
cleanup = error " TODO clean up authorized keys and generated ssh key and remove git remote "
# else
getFinishPairR _ = noPairing
# endif
getInprogressPairR :: Text -> Handler RepHtml
# ifdef WITH_PAIRING
getInprogressPairR secret = pairPage $ do
$ ( widgetFile " configurators/pairing/inprogress " )
# else
getInprogressPairR _ = noPairing
# endif
# ifdef WITH_PAIRING
{- Starts pairing, at either the PairReq (initiating host) or
- 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-09-11 01:55:59 +00:00
startPairing :: PairStage -> IO () -> Text -> Secret -> Widget
startPairing stage oncancel displaysecret secret = do
keypair <- liftIO $ genSshKeyPair
2012-09-09 20:24:34 +00:00
dstatus <- daemonStatus <$> lift getYesod
urlrender <- lift getUrlRender
let homeurl = urlrender HomeR
2012-09-11 01:55:59 +00:00
pairdata <- PairData
<$> liftIO getHostname
<*> liftIO getUserName
<*> ( fromJust . relDir <$> lift getYesod )
<*> pure ( sshPubKey keypair )
2012-09-09 20:24:34 +00:00
liftIO $ do
2012-09-11 01:55:59 +00:00
let sender = multicastPairMsg Nothing secret stage pairdata
let pip = PairingInProgress secret Nothing keypair pairdata
startSending dstatus pip $ sendrequests sender dstatus homeurl
2012-09-10 21:53:51 +00:00
lift $ redirect $ InprogressPairR displaysecret
2012-09-09 20:24:34 +00:00
where
{- Sends pairing messages until the thread is killed,
- and shows an activity alert while doing it .
-
2012-09-10 21:53:51 +00:00
- The cancel button returns the user to the HomeR . This is
2012-09-09 20:24:34 +00:00
- not ideal , but they have to be sent somewhere , and could
- have been on a page specific to the in - process pairing
2012-09-10 21:53:51 +00:00
- that just stopped , so can't go back there .
2012-09-09 20:24:34 +00:00
- }
2012-09-11 01:55:59 +00:00
sendrequests sender dstatus homeurl = do
2012-09-09 20:24:34 +00:00
tid <- myThreadId
let selfdestruct = AlertButton
{ buttonLabel = " Cancel "
, buttonUrl = homeurl
2012-09-10 21:53:51 +00:00
, buttonAction = Just $ const $ do
oncancel
killThread tid
2012-09-09 20:24:34 +00:00
}
2012-09-10 21:53:51 +00:00
alertDuring dstatus ( pairingAlert selfdestruct ) $ do
_ <- E . try sender :: IO ( Either E . SomeException () )
2012-09-09 20:24:34 +00:00
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
where
showform form enctype mproblem = do
2012-09-09 01:06:10 +00:00
let start = isNothing msg
2012-09-08 06:02:39 +00:00
let badphrase = isJust mproblem
2012-09-09 01:06:10 +00:00
let problem = fromMaybe " " mproblem
2012-09-08 06:02:39 +00:00
let ( username , hostname ) = maybe ( " " , " " )
2012-09-11 01:55:59 +00:00
( \ ( _ , v , a ) -> ( T . pack $ remoteUserName v , T . pack $ fromMaybe ( showAddr a ) ( remoteHostName v ) ) )
2012-09-09 01:06:10 +00:00
( verifiableVal . fromPairMsg <$> msg )
2012-09-08 17:04:19 +00:00
u <- T . pack <$> liftIO getUserName
2012-09-08 06:02:39 +00:00
let sameusername = username == u
let authtoken = webAppFormAuthToken
2012-09-09 03:32:08 +00:00
$ ( widgetFile " configurators/pairing/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 ]
2012-09-10 21:53:51 +00:00
getUserName :: IO String
getUserName = userName <$> ( getUserEntryForID =<< getEffectiveUserID )
pairPage :: Widget -> Handler RepHtml
pairPage w = bootstrap ( Just Config ) $ do
sideBarDisplay
setTitle " Pairing "
w
2012-09-08 06:02:39 +00:00
{- 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
noPairing :: Handler RepHtml
2012-09-10 21:53:51 +00:00
noPairing = pairPage $
2012-09-09 03:32:08 +00:00
$ ( widgetFile " configurators/pairing/disabled " )
2012-09-08 19:07:44 +00:00
# endif