2012-09-10 19:20:18 +00:00
|
|
|
{- git-annex assistant ssh utilities
|
|
|
|
-
|
|
|
|
- Copyright 2012 Joey Hess <joey@kitenet.net>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
module Assistant.Ssh where
|
|
|
|
|
2012-09-11 01:55:59 +00:00
|
|
|
import Common.Annex
|
2012-09-10 19:20:18 +00:00
|
|
|
import Utility.TempFile
|
|
|
|
|
|
|
|
import Data.Text (Text)
|
|
|
|
import qualified Data.Text as T
|
|
|
|
import qualified Control.Exception as E
|
|
|
|
import System.Process (CreateProcess(..))
|
|
|
|
import Control.Concurrent
|
2012-09-10 21:53:51 +00:00
|
|
|
import Data.Char
|
2012-09-10 19:20:18 +00:00
|
|
|
|
|
|
|
data SshData = SshData
|
|
|
|
{ sshHostName :: Text
|
|
|
|
, sshUserName :: Maybe Text
|
|
|
|
, sshDirectory :: Text
|
|
|
|
, sshRepoName :: String
|
|
|
|
, needsPubKey :: Bool
|
|
|
|
, rsyncOnly :: Bool
|
|
|
|
}
|
|
|
|
deriving (Read, Show, Eq)
|
|
|
|
|
|
|
|
data SshKeyPair = SshKeyPair
|
|
|
|
{ sshPubKey :: String
|
|
|
|
, sshPrivKey :: String
|
|
|
|
}
|
|
|
|
|
2012-09-11 19:06:29 +00:00
|
|
|
instance Show SshKeyPair where
|
|
|
|
show = sshPubKey
|
|
|
|
|
2012-09-10 21:53:51 +00:00
|
|
|
type SshPubKey = String
|
|
|
|
|
2012-09-10 19:20:18 +00:00
|
|
|
{- ssh -ofoo=bar command-line option -}
|
|
|
|
sshOpt :: String -> String -> String
|
|
|
|
sshOpt k v = concat ["-o", k, "=", v]
|
|
|
|
|
|
|
|
sshDir :: IO FilePath
|
|
|
|
sshDir = do
|
|
|
|
home <- myHomeDir
|
|
|
|
return $ home </> ".ssh"
|
|
|
|
|
2012-09-11 01:55:59 +00:00
|
|
|
{- user@host or host -}
|
|
|
|
genSshHost :: Text -> Maybe Text -> String
|
|
|
|
genSshHost host user = maybe "" (\v -> T.unpack v ++ "@") user ++ T.unpack host
|
|
|
|
|
2012-09-10 21:53:51 +00:00
|
|
|
{- host_dir, with all / in dir replaced by _, and bad characters removed -}
|
|
|
|
genSshRepoName :: String -> FilePath -> String
|
|
|
|
genSshRepoName host dir
|
|
|
|
| null dir = filter legal host
|
|
|
|
| otherwise = filter legal $ host ++ "_" ++ replace "/" "_" dir
|
|
|
|
where
|
|
|
|
legal '_' = True
|
|
|
|
legal c = isAlphaNum c
|
|
|
|
|
2012-09-10 19:20:18 +00:00
|
|
|
{- The output of ssh, including both stdout and stderr. -}
|
|
|
|
sshTranscript :: [String] -> String -> IO (String, Bool)
|
|
|
|
sshTranscript opts input = do
|
|
|
|
(readf, writef) <- createPipe
|
|
|
|
readh <- fdToHandle readf
|
|
|
|
writeh <- fdToHandle writef
|
|
|
|
(Just inh, _, _, pid) <- createProcess $
|
|
|
|
(proc "ssh" opts)
|
|
|
|
{ std_in = CreatePipe
|
|
|
|
, std_out = UseHandle writeh
|
|
|
|
, std_err = UseHandle writeh
|
|
|
|
}
|
|
|
|
hClose writeh
|
|
|
|
|
|
|
|
-- fork off a thread to start consuming the output
|
|
|
|
transcript <- hGetContents readh
|
|
|
|
outMVar <- newEmptyMVar
|
|
|
|
_ <- forkIO $ E.evaluate (length transcript) >> putMVar outMVar ()
|
|
|
|
|
|
|
|
-- now write and flush any input
|
2012-09-13 04:57:52 +00:00
|
|
|
unless (null input) $ do
|
|
|
|
hPutStr inh input
|
|
|
|
hFlush inh
|
2012-09-10 19:20:18 +00:00
|
|
|
hClose inh -- done with stdin
|
|
|
|
|
|
|
|
-- wait on the output
|
|
|
|
takeMVar outMVar
|
|
|
|
hClose readh
|
|
|
|
|
|
|
|
ok <- checkSuccessProcess pid
|
|
|
|
return ()
|
|
|
|
return (transcript, ok)
|
|
|
|
|
2012-09-10 22:18:55 +00:00
|
|
|
{- Ensure that the ssh public key doesn't include any ssh options, like
|
|
|
|
- command=foo, or other weirdness -}
|
|
|
|
validateSshPubKey :: SshPubKey -> IO ()
|
|
|
|
validateSshPubKey pubkey = do
|
|
|
|
let ws = words pubkey
|
|
|
|
when (length ws > 3 || length ws < 2) $
|
|
|
|
error $ "wrong number of words in ssh public key " ++ pubkey
|
|
|
|
let (ssh, keytype) = separate (== '-') (ws !! 0)
|
|
|
|
unless (ssh == "ssh" && all isAlphaNum keytype) $
|
|
|
|
error $ "bad ssh public key prefix " ++ ws !! 0
|
|
|
|
when (length ws == 3) $
|
|
|
|
unless (all (\c -> isAlphaNum c || c == '@') (ws !! 2)) $
|
|
|
|
error $ "bad comment in ssh public key " ++ pubkey
|
2012-09-10 21:53:51 +00:00
|
|
|
|
2012-09-11 04:23:34 +00:00
|
|
|
addAuthorizedKeys :: Bool -> SshPubKey -> IO Bool
|
|
|
|
addAuthorizedKeys rsynconly pubkey = boolSystem "sh"
|
|
|
|
[ Param "-c" , Param $ addAuthorizedKeysCommand rsynconly pubkey ]
|
|
|
|
|
|
|
|
removeAuthorizedKeys :: Bool -> SshPubKey -> IO ()
|
|
|
|
removeAuthorizedKeys rsynconly pubkey = do
|
|
|
|
let keyline = authorizedKeysLine rsynconly pubkey
|
|
|
|
sshdir <- sshDir
|
|
|
|
let keyfile = sshdir </> ".authorized_keys"
|
|
|
|
ls <- lines <$> readFileStrict keyfile
|
2012-09-13 04:57:52 +00:00
|
|
|
writeFile keyfile $ unlines $ filter (/= keyline) ls
|
2012-09-10 21:53:51 +00:00
|
|
|
|
2012-09-10 19:20:18 +00:00
|
|
|
{- Implemented as a shell command, so it can be run on remote servers over
|
2012-09-26 22:59:18 +00:00
|
|
|
- ssh.
|
|
|
|
-
|
|
|
|
- The ~/.ssh/git-annex-shell wrapper script is created if not already
|
|
|
|
- present.
|
|
|
|
-}
|
2012-09-11 04:23:34 +00:00
|
|
|
addAuthorizedKeysCommand :: Bool -> SshPubKey -> String
|
2012-09-13 04:57:52 +00:00
|
|
|
addAuthorizedKeysCommand rsynconly pubkey = join "&&"
|
2012-09-10 21:53:51 +00:00
|
|
|
[ "mkdir -p ~/.ssh"
|
2012-09-26 22:59:18 +00:00
|
|
|
, join "; "
|
|
|
|
[ "if [ ! -e " ++ wrapper ++ " ]"
|
|
|
|
, "then (" ++ join ";" (map echoval script) ++ ") > " ++ wrapper
|
|
|
|
, "fi"
|
|
|
|
]
|
|
|
|
, "chmod 700 " ++ wrapper
|
2012-09-10 21:53:51 +00:00
|
|
|
, "touch ~/.ssh/authorized_keys"
|
|
|
|
, "chmod 600 ~/.ssh/authorized_keys"
|
|
|
|
, unwords
|
|
|
|
[ "echo"
|
|
|
|
, shellEscape $ authorizedKeysLine rsynconly pubkey
|
|
|
|
, ">>~/.ssh/authorized_keys"
|
2012-09-10 19:20:18 +00:00
|
|
|
]
|
2012-09-10 21:53:51 +00:00
|
|
|
]
|
2012-09-26 22:59:18 +00:00
|
|
|
where
|
|
|
|
echoval v = "echo " ++ shellEscape v
|
|
|
|
wrapper = "~/.ssh/git-annex-shell"
|
|
|
|
script =
|
|
|
|
[ "#!/bin/sh"
|
|
|
|
, "set -e"
|
|
|
|
, "exec git-annex-shell -c \"$SSH_ORIGINAL_COMMAND\""
|
|
|
|
]
|
2012-09-10 21:53:51 +00:00
|
|
|
|
|
|
|
authorizedKeysLine :: Bool -> SshPubKey -> String
|
|
|
|
authorizedKeysLine rsynconly pubkey
|
2012-09-10 19:20:18 +00:00
|
|
|
{- TODO: Locking down rsync is difficult, requiring a rather
|
|
|
|
- long perl script. -}
|
2012-09-10 21:53:51 +00:00
|
|
|
| rsynconly = pubkey
|
2012-09-26 22:59:18 +00:00
|
|
|
| otherwise = limitcommand ++ pubkey
|
2012-09-10 19:20:18 +00:00
|
|
|
where
|
2012-09-26 22:59:18 +00:00
|
|
|
limitcommand = "command=\"~/.ssh/git-annex-shell\",no-agent-forwarding,no-port-forwarding,no-X11-forwarding "
|
2012-09-10 19:20:18 +00:00
|
|
|
|
|
|
|
{- Generates a ssh key pair. -}
|
|
|
|
genSshKeyPair :: IO SshKeyPair
|
|
|
|
genSshKeyPair = withTempDir "git-annex-keygen" $ \dir -> do
|
|
|
|
ok <- boolSystem "ssh-keygen"
|
|
|
|
[ Param "-P", Param "" -- no password
|
|
|
|
, Param "-f", File $ dir </> "key"
|
|
|
|
]
|
|
|
|
unless ok $
|
|
|
|
error "ssh-keygen failed"
|
|
|
|
SshKeyPair
|
|
|
|
<$> readFile (dir </> "key.pub")
|
|
|
|
<*> readFile (dir </> "key")
|
|
|
|
|
|
|
|
{- Installs a ssh key pair, and sets up ssh config with a mangled hostname
|
|
|
|
- that will enable use of the key. This way we avoid changing the user's
|
|
|
|
- regular ssh experience at all. Returns a modified SshData containing the
|
|
|
|
- mangled hostname. -}
|
|
|
|
setupSshKeyPair :: SshKeyPair -> SshData -> IO SshData
|
|
|
|
setupSshKeyPair sshkeypair sshdata = do
|
|
|
|
sshdir <- sshDir
|
|
|
|
let configfile = sshdir </> "config"
|
|
|
|
createDirectoryIfMissing True sshdir
|
|
|
|
|
|
|
|
unlessM (doesFileExist $ sshdir </> sshprivkeyfile) $ do
|
|
|
|
h <- fdToHandle =<<
|
|
|
|
createFile (sshdir </> sshprivkeyfile)
|
|
|
|
(unionFileModes ownerWriteMode ownerReadMode)
|
|
|
|
hPutStr h (sshPrivKey sshkeypair)
|
|
|
|
hClose h
|
2012-09-13 04:57:52 +00:00
|
|
|
unlessM (doesFileExist $ sshdir </> sshpubkeyfile) $
|
2012-09-10 19:20:18 +00:00
|
|
|
writeFile (sshdir </> sshpubkeyfile) (sshPubKey sshkeypair)
|
|
|
|
|
|
|
|
unlessM (catchBoolIO $ isInfixOf mangledhost <$> readFile configfile) $
|
|
|
|
appendFile configfile $ unlines
|
|
|
|
[ ""
|
|
|
|
, "# Added automatically by git-annex"
|
|
|
|
, "Host " ++ mangledhost
|
|
|
|
, "\tHostname " ++ T.unpack (sshHostName sshdata)
|
|
|
|
, "\tIdentityFile ~/.ssh/" ++ sshprivkeyfile
|
|
|
|
]
|
|
|
|
|
|
|
|
return $ sshdata { sshHostName = T.pack mangledhost }
|
|
|
|
where
|
|
|
|
sshprivkeyfile = "key." ++ mangledhost
|
|
|
|
sshpubkeyfile = sshprivkeyfile ++ ".pub"
|
2012-09-13 20:47:44 +00:00
|
|
|
mangledhost = mangleSshHostName
|
|
|
|
(T.unpack $ sshHostName sshdata)
|
|
|
|
(T.unpack <$> sshUserName sshdata)
|
|
|
|
|
|
|
|
mangleSshHostName :: String -> Maybe String -> String
|
|
|
|
mangleSshHostName host user = "git-annex-" ++ host ++ (maybe "-" ('-':) user)
|
|
|
|
|
|
|
|
unMangleSshHostName :: String -> String
|
|
|
|
unMangleSshHostName h
|
|
|
|
| "git-annex-" `isPrefixOf` h = join "-" (beginning $ drop 2 dashbits)
|
|
|
|
| otherwise = h
|
|
|
|
where
|
|
|
|
dashbits = split "-" h
|
2012-09-11 01:55:59 +00:00
|
|
|
|
|
|
|
{- Does ssh have known_hosts data for a hostname? -}
|
|
|
|
knownHost :: Text -> IO Bool
|
|
|
|
knownHost hostname = do
|
|
|
|
sshdir <- sshDir
|
|
|
|
ifM (doesFileExist $ sshdir </> "known_hosts")
|
2012-09-27 15:27:16 +00:00
|
|
|
( not . null <$> checkhost
|
2012-09-11 01:55:59 +00:00
|
|
|
, return False
|
|
|
|
)
|
2012-09-27 15:27:16 +00:00
|
|
|
where
|
|
|
|
{- ssh-keygen -F can crash on some old known_hosts file -}
|
|
|
|
checkhost = catchDefaultIO "" $
|
|
|
|
readProcess "ssh-keygen" ["-F", T.unpack hostname]
|