summaryrefslogtreecommitdiff
path: root/Assistant/Pairing.hs
blob: 5d097ab7df9f322e3f63a9c088ff9d9dded05191 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
{- git-annex assistant repo pairing, core data types
 -
 - Copyright 2012 Joey Hess <joey@kitenet.net>
 -
 - Licensed under the GNU GPL version 3 or higher.
 -}

module Assistant.Pairing where

import Common.Annex
import Utility.Verifiable
import Assistant.Ssh

import Control.Concurrent
import Network.Socket
import Data.Char
import qualified Data.Text as T

data PairStage
	{- "I'll pair with anybody who shares the secret that can be used
	 - to verify this request." -}
	 = PairReq
	{- "I've verified your request, and you can verify this to see
	 - that I know the secret. I set up your ssh key already.
	 - Here's mine for you to set up." -}
	| PairAck
	{- "I saw your PairAck; you can stop sending them." -}
	| PairDone
	deriving (Eq, Read, Show, Ord)

newtype PairMsg = PairMsg (Verifiable (PairStage, PairData, SomeAddr))
	deriving (Eq, Read, Show)

verifiedPairMsg :: PairMsg -> PairingInProgress -> Bool
verifiedPairMsg (PairMsg m) pip = verify m $ inProgressSecret pip

fromPairMsg :: PairMsg -> (Verifiable (PairStage, PairData, SomeAddr))
fromPairMsg (PairMsg m) = m

pairMsgStage :: PairMsg -> PairStage
pairMsgStage (PairMsg (Verifiable (s, _, _) _)) = s

pairMsgData :: PairMsg -> PairData
pairMsgData (PairMsg (Verifiable (_, d, _) _)) = d

pairMsgAddr :: PairMsg -> SomeAddr
pairMsgAddr (PairMsg (Verifiable (_, _, a) _)) = a

data PairData = PairData
	-- uname -n output, not a full domain name
	{ remoteHostName :: Maybe HostName
	, remoteUserName :: UserName
	, remoteDirectory :: FilePath
	, remoteSshPubKey :: SshPubKey
	, pairUUID :: UUID
	}
	deriving (Eq, Read, Show)

type UserName = String

{- A pairing that is in progress has a secret, a thread that is
 - broadcasting pairing messages, and a SshKeyPair that has not yet been
 - set up on disk. -}
data PairingInProgress = PairingInProgress
	{ inProgressSecret :: Secret
	, inProgressThreadId :: Maybe ThreadId
	, inProgressSshKeyPair :: SshKeyPair
	, inProgressPairData :: PairData
	, inProgressPairStage :: PairStage
	}
	deriving (Show)

data SomeAddr = IPv4Addr HostAddress | IPv6Addr HostAddress6
	deriving (Ord, Eq, Read, Show)

{- This contains the whole secret, just lightly obfuscated to make it not
 - too obvious. It's only displayed in the user's web browser. -}
data SecretReminder = SecretReminder [Int]
	deriving (Show, Eq, Ord, Read)

toSecretReminder :: T.Text -> SecretReminder
toSecretReminder = SecretReminder . map ord . T.unpack

fromSecretReminder :: SecretReminder -> T.Text
fromSecretReminder (SecretReminder s) = T.pack $ map chr s