2012-01-20 19:34:52 +00:00
|
|
|
{- git-annex ssh interface, with connection caching
|
|
|
|
-
|
2020-01-23 16:35:46 +00:00
|
|
|
- Copyright 2012-2020 Joey Hess <id@joeyh.name>
|
2012-01-20 19:34:52 +00:00
|
|
|
-
|
2019-03-13 19:48:14 +00:00
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
2012-01-20 19:34:52 +00:00
|
|
|
-}
|
|
|
|
|
2012-10-15 18:49:40 +00:00
|
|
|
{-# LANGUAGE CPP #-}
|
|
|
|
|
2012-01-20 19:34:52 +00:00
|
|
|
module Annex.Ssh (
|
2017-02-15 19:08:46 +00:00
|
|
|
ConsumeStdin(..),
|
2017-03-17 20:02:47 +00:00
|
|
|
SshCommand,
|
|
|
|
sshCommand,
|
2015-02-12 19:44:10 +00:00
|
|
|
sshOptions,
|
2013-05-14 17:53:29 +00:00
|
|
|
sshCacheDir,
|
2013-04-13 22:10:49 +00:00
|
|
|
sshReadPort,
|
2014-04-12 20:32:59 +00:00
|
|
|
forceSshCleanup,
|
2015-02-12 20:12:32 +00:00
|
|
|
sshOptionsEnv,
|
|
|
|
sshOptionsTo,
|
|
|
|
inRepoWithSshOptionsTo,
|
|
|
|
runSshOptions,
|
2014-04-29 22:08:10 +00:00
|
|
|
sshAskPassEnv,
|
|
|
|
runSshAskPass
|
2012-01-20 19:34:52 +00:00
|
|
|
) where
|
|
|
|
|
2016-01-20 20:36:33 +00:00
|
|
|
import Annex.Common
|
2014-07-10 04:32:23 +00:00
|
|
|
import Annex.LockFile
|
2017-12-14 16:46:57 +00:00
|
|
|
import qualified BuildInfo
|
2012-12-30 03:10:18 +00:00
|
|
|
import qualified Annex
|
2014-04-12 19:59:34 +00:00
|
|
|
import qualified Git
|
|
|
|
import qualified Git.Url
|
2013-02-19 18:56:24 +00:00
|
|
|
import Config
|
2015-02-28 21:23:13 +00:00
|
|
|
import Annex.Path
|
2013-05-11 22:23:41 +00:00
|
|
|
import Utility.Env
|
2017-05-15 22:10:13 +00:00
|
|
|
import Utility.Hash
|
2014-03-13 23:30:13 +00:00
|
|
|
import Types.CleanupActions
|
2020-09-16 15:41:28 +00:00
|
|
|
import Annex.Concurrent.Utility
|
2017-05-13 17:13:13 +00:00
|
|
|
import Types.Concurrency
|
2016-02-25 17:46:31 +00:00
|
|
|
import Git.Env
|
2017-03-17 20:02:47 +00:00
|
|
|
import Git.Ssh
|
2020-10-29 16:02:46 +00:00
|
|
|
import qualified Utility.RawFilePath as R
|
2013-08-04 17:12:18 +00:00
|
|
|
import Annex.Perms
|
2020-03-20 15:47:09 +00:00
|
|
|
#ifndef mingw32_HOST_OS
|
2015-11-12 22:05:45 +00:00
|
|
|
import Annex.LockPool
|
2013-08-04 17:12:18 +00:00
|
|
|
#endif
|
2012-01-20 19:34:52 +00:00
|
|
|
|
2017-05-11 22:29:51 +00:00
|
|
|
import Control.Concurrent.STM
|
2019-12-18 17:26:06 +00:00
|
|
|
import qualified Data.ByteString as S
|
2020-10-29 16:02:46 +00:00
|
|
|
import qualified System.FilePath.ByteString as P
|
2017-05-11 21:33:18 +00:00
|
|
|
|
2017-02-15 19:08:46 +00:00
|
|
|
{- Some ssh commands are fed stdin on a pipe and so should be allowed to
|
|
|
|
- consume it. But ssh commands that are not piped stdin should generally
|
|
|
|
- not be allowed to consume the process's stdin. -}
|
|
|
|
data ConsumeStdin = ConsumeStdin | NoConsumeStdin
|
|
|
|
|
2017-03-17 20:02:47 +00:00
|
|
|
{- Generates a command to ssh to a given host (or user@host) on a given
|
|
|
|
- port. This includes connection caching parameters, and any ssh-options.
|
2017-04-07 15:35:20 +00:00
|
|
|
- If GIT_SSH or GIT_SSH_COMMAND is enabled, they are used instead. -}
|
2017-03-17 20:02:47 +00:00
|
|
|
sshCommand :: ConsumeStdin -> (SshHost, Maybe SshPort) -> RemoteGitConfig -> SshCommand -> Annex (FilePath, [CommandParam])
|
2017-04-07 15:35:20 +00:00
|
|
|
sshCommand cs (host, port) gc remotecmd = ifM (liftIO safe_GIT_SSH)
|
|
|
|
( maybe go return
|
|
|
|
=<< liftIO (gitSsh' host port remotecmd (consumeStdinParams cs))
|
|
|
|
, go
|
|
|
|
)
|
2017-03-17 20:02:47 +00:00
|
|
|
where
|
2017-03-21 03:35:29 +00:00
|
|
|
go = do
|
2017-03-17 20:02:47 +00:00
|
|
|
ps <- sshOptions cs (host, port) gc []
|
2017-08-18 02:11:31 +00:00
|
|
|
return ("ssh", Param (fromSshHost host):ps++[Param remotecmd])
|
2017-03-17 20:02:47 +00:00
|
|
|
|
2012-01-20 19:34:52 +00:00
|
|
|
{- Generates parameters to ssh to a given host (or user@host) on a given
|
2017-03-17 20:02:47 +00:00
|
|
|
- port. This includes connection caching parameters, and any
|
|
|
|
- ssh-options. Note that the host to ssh to and the command to run
|
|
|
|
- are not included in the returned options. -}
|
2017-08-18 02:11:31 +00:00
|
|
|
sshOptions :: ConsumeStdin -> (SshHost, Maybe Integer) -> RemoteGitConfig -> [CommandParam] -> Annex [CommandParam]
|
2017-02-15 19:08:46 +00:00
|
|
|
sshOptions cs (host, port) gc opts = go =<< sshCachingInfo (host, port)
|
2012-12-13 04:24:19 +00:00
|
|
|
where
|
2017-05-11 21:33:18 +00:00
|
|
|
go (Nothing, params) = return $ mkparams cs params
|
2012-12-13 04:24:19 +00:00
|
|
|
go (Just socketfile, params) = do
|
2018-06-05 16:51:17 +00:00
|
|
|
prepSocket socketfile host (mkparams NoConsumeStdin params)
|
2017-05-11 21:33:18 +00:00
|
|
|
|
|
|
|
return $ mkparams cs params
|
|
|
|
mkparams cs' ps = concat
|
2016-12-13 16:05:05 +00:00
|
|
|
[ ps
|
|
|
|
, map Param (remoteAnnexSshOptions gc)
|
|
|
|
, opts
|
|
|
|
, portParams port
|
2017-05-11 21:33:18 +00:00
|
|
|
, consumeStdinParams cs'
|
2016-12-13 16:05:05 +00:00
|
|
|
, [Param "-T"]
|
|
|
|
]
|
2012-01-20 19:34:52 +00:00
|
|
|
|
2017-04-07 15:35:20 +00:00
|
|
|
{- Due to passing -n to GIT_SSH and GIT_SSH_COMMAND, some settings
|
|
|
|
- of those that expect exactly git's parameters will break. So only
|
|
|
|
- use those if the user set GIT_ANNEX_USE_GIT_SSH to say it's ok. -}
|
|
|
|
safe_GIT_SSH :: IO Bool
|
|
|
|
safe_GIT_SSH = (== Just "1") <$> getEnv "GIT_ANNEX_USE_GIT_SSH"
|
|
|
|
|
2017-03-17 20:02:47 +00:00
|
|
|
consumeStdinParams :: ConsumeStdin -> [CommandParam]
|
|
|
|
consumeStdinParams ConsumeStdin = []
|
|
|
|
consumeStdinParams NoConsumeStdin = [Param "-n"]
|
|
|
|
|
2013-02-19 18:56:24 +00:00
|
|
|
{- Returns a filename to use for a ssh connection caching socket, and
|
|
|
|
- parameters to enable ssh connection caching. -}
|
2017-08-18 02:11:31 +00:00
|
|
|
sshCachingInfo :: (SshHost, Maybe Integer) -> Annex (Maybe FilePath, [CommandParam])
|
2020-02-14 18:21:03 +00:00
|
|
|
sshCachingInfo (host, port) = go =<< sshCacheDir'
|
2013-02-19 18:56:24 +00:00
|
|
|
where
|
2020-02-14 18:21:03 +00:00
|
|
|
go (Right dir) =
|
2020-10-29 16:02:46 +00:00
|
|
|
liftIO (bestSocketPath $ dir P.</> hostport2socket host port) >>= return . \case
|
2013-10-07 00:43:22 +00:00
|
|
|
Nothing -> (Nothing, [])
|
2020-10-29 16:02:46 +00:00
|
|
|
Just socketfile ->
|
|
|
|
let socketfile' = fromRawFilePath socketfile
|
|
|
|
in (Just socketfile', sshConnectionCachingParams socketfile')
|
2020-01-23 16:35:46 +00:00
|
|
|
-- No connection caching with concurrency is not a good
|
|
|
|
-- combination, so warn the user.
|
2020-02-14 18:21:03 +00:00
|
|
|
go (Left whynocaching) = do
|
2020-09-16 15:41:28 +00:00
|
|
|
getConcurrency >>= \case
|
2020-01-23 16:35:46 +00:00
|
|
|
NonConcurrent -> return ()
|
2020-02-14 18:21:03 +00:00
|
|
|
Concurrent {} -> warnnocaching whynocaching
|
|
|
|
ConcurrentPerCpu -> warnnocaching whynocaching
|
2020-01-23 16:35:46 +00:00
|
|
|
return (Nothing, [])
|
|
|
|
|
2021-05-27 16:37:39 +00:00
|
|
|
warnnocaching whynocaching =
|
|
|
|
whenM (annexAdviceNoSshCaching <$> Annex.getGitConfig) $ do
|
filter out control characters in warning messages
Converted warning and similar to use StringContainingQuotedPath. Most
warnings are static strings, some do refer to filepaths that need to be
quoted, and others don't need quoting.
Note that, since quote filters out control characters of even
UnquotedString, this makes all warnings safe, even when an attacker
sneaks in a control character in some other way.
When json is being output, no quoting is done, since json gets its own
quoting.
This does, as a side effect, make warning messages in json output not
be indented. The indentation is only needed to offset warning messages
underneath the display of the file they apply to, so that's ok.
Sponsored-by: Brett Eisenberg on Patreon
2023-04-10 18:47:32 +00:00
|
|
|
warning $ UnquotedString nocachingwarning
|
|
|
|
warning $ UnquotedString whynocaching
|
2020-01-23 16:35:46 +00:00
|
|
|
|
|
|
|
nocachingwarning = unwords
|
2020-02-14 18:21:03 +00:00
|
|
|
[ "You have enabled concurrency, but git-annex is not able"
|
|
|
|
, "to use ssh connection caching. This may result in"
|
|
|
|
, "multiple ssh processes prompting for passwords at the"
|
|
|
|
, "same time."
|
2020-01-23 16:35:46 +00:00
|
|
|
]
|
2013-10-07 00:43:22 +00:00
|
|
|
|
|
|
|
{- Given an absolute path to use for a socket file,
|
|
|
|
- returns whichever is shorter of that or the relative path to the same
|
|
|
|
- file.
|
|
|
|
-
|
|
|
|
- If no path can be constructed that is a valid socket, returns Nothing. -}
|
2020-10-29 16:02:46 +00:00
|
|
|
bestSocketPath :: RawFilePath -> IO (Maybe RawFilePath)
|
2013-10-07 00:43:22 +00:00
|
|
|
bestSocketPath abssocketfile = do
|
|
|
|
relsocketfile <- liftIO $ relPathCwdToFile abssocketfile
|
2020-10-29 16:02:46 +00:00
|
|
|
let socketfile = if S.length abssocketfile <= S.length relsocketfile
|
2013-10-07 00:43:22 +00:00
|
|
|
then abssocketfile
|
|
|
|
else relsocketfile
|
2020-10-29 16:02:46 +00:00
|
|
|
return $ if valid_unix_socket_path socketfile sshgarbagelen
|
2013-10-07 00:43:22 +00:00
|
|
|
then Just socketfile
|
|
|
|
else Nothing
|
|
|
|
where
|
2014-10-09 18:53:13 +00:00
|
|
|
-- ssh appends a 16 char extension to the socket when setting it
|
2013-10-07 00:43:22 +00:00
|
|
|
-- up, which needs to be taken into account when checking
|
|
|
|
-- that a valid socket was constructed.
|
2020-10-29 16:02:46 +00:00
|
|
|
sshgarbagelen = 1+16
|
2013-07-21 18:14:54 +00:00
|
|
|
|
|
|
|
sshConnectionCachingParams :: FilePath -> [CommandParam]
|
|
|
|
sshConnectionCachingParams socketfile =
|
|
|
|
[ Param "-S", Param socketfile
|
2015-06-01 17:52:23 +00:00
|
|
|
, Param "-o", Param "ControlMaster=auto"
|
|
|
|
, Param "-o", Param "ControlPersist=yes"
|
2013-07-21 18:14:54 +00:00
|
|
|
]
|
2012-01-20 19:34:52 +00:00
|
|
|
|
2020-01-23 16:35:46 +00:00
|
|
|
sshSocketDirEnv :: String
|
|
|
|
sshSocketDirEnv = "GIT_ANNEX_SSH_SOCKET_DIR"
|
|
|
|
|
2020-03-06 16:52:20 +00:00
|
|
|
{- Returns the directory where ssh connection caching sockets can be
|
|
|
|
- stored.
|
|
|
|
-
|
|
|
|
- The directory will be created if it does not exist.
|
|
|
|
-}
|
2020-10-29 16:02:46 +00:00
|
|
|
sshCacheDir :: Annex (Maybe RawFilePath)
|
2020-02-14 18:21:03 +00:00
|
|
|
sshCacheDir = eitherToMaybe <$> sshCacheDir'
|
|
|
|
|
2020-10-29 16:02:46 +00:00
|
|
|
sshCacheDir' :: Annex (Either String RawFilePath)
|
2020-02-14 18:21:03 +00:00
|
|
|
sshCacheDir' =
|
|
|
|
ifM (fromMaybe BuildInfo.sshconnectioncaching . annexSshCaching <$> Annex.getGitConfig)
|
|
|
|
( ifM crippledFileSystem
|
|
|
|
( gettmpdir >>= \case
|
|
|
|
Nothing ->
|
|
|
|
return (Left crippledfswarning)
|
|
|
|
Just tmpdir ->
|
|
|
|
liftIO $ catchMsgIO $
|
|
|
|
usetmpdir tmpdir
|
2020-03-06 16:52:20 +00:00
|
|
|
, do
|
|
|
|
d <- fromRepo gitAnnexSshDir
|
|
|
|
createAnnexDirectory d
|
|
|
|
return (Right d)
|
2013-02-19 18:56:24 +00:00
|
|
|
)
|
2020-02-14 18:21:03 +00:00
|
|
|
, return (Left "annex.sshcaching is not set to true")
|
|
|
|
)
|
2013-02-19 18:56:24 +00:00
|
|
|
where
|
2020-01-23 16:35:46 +00:00
|
|
|
gettmpdir = liftIO $ getEnv sshSocketDirEnv
|
2020-02-14 18:21:03 +00:00
|
|
|
|
|
|
|
usetmpdir tmpdir = do
|
2014-04-20 20:56:01 +00:00
|
|
|
let socktmp = tmpdir </> "ssh"
|
|
|
|
createDirectoryIfMissing True socktmp
|
2020-10-29 16:02:46 +00:00
|
|
|
return (toRawFilePath socktmp)
|
2020-02-14 18:21:03 +00:00
|
|
|
|
|
|
|
crippledfswarning = unwords
|
|
|
|
[ "This repository is on a crippled filesystem, so unix named"
|
|
|
|
, "pipes probably don't work, and ssh connection caching"
|
|
|
|
, "relies on those. One workaround is to set"
|
|
|
|
, sshSocketDirEnv
|
|
|
|
, "to point to a directory on a non-crippled filesystem."
|
|
|
|
]
|
2012-01-20 19:34:52 +00:00
|
|
|
|
|
|
|
portParams :: Maybe Integer -> [CommandParam]
|
|
|
|
portParams Nothing = []
|
|
|
|
portParams (Just port) = [Param "-p", Param $ show port]
|
|
|
|
|
2017-05-11 21:33:18 +00:00
|
|
|
{- Prepare to use a socket file for ssh connection caching.
|
|
|
|
-
|
|
|
|
- When concurrency is enabled, this blocks until a ssh connection
|
|
|
|
- has been made to the host. So, any password prompting by ssh will
|
|
|
|
- happen in this call, and only one ssh process will prompt at a time.
|
|
|
|
-
|
|
|
|
- Locks the socket lock file to prevent other git-annex processes from
|
|
|
|
- stopping the ssh multiplexer on this socket.
|
|
|
|
-}
|
2018-06-05 16:51:17 +00:00
|
|
|
prepSocket :: FilePath -> SshHost -> [CommandParam] -> Annex ()
|
|
|
|
prepSocket socketfile sshhost sshparams = do
|
2017-05-11 21:33:18 +00:00
|
|
|
-- There could be stale ssh connections hanging around
|
2014-04-12 19:59:34 +00:00
|
|
|
-- from a previous git-annex run that was interrupted.
|
2017-05-11 22:29:51 +00:00
|
|
|
-- This must run only once, before we have made any ssh connection,
|
|
|
|
-- and any other prepSocket calls must block while it's run.
|
2021-04-02 19:26:21 +00:00
|
|
|
tv <- Annex.getRead Annex.sshstalecleaned
|
2017-05-11 22:29:51 +00:00
|
|
|
join $ liftIO $ atomically $ do
|
|
|
|
cleaned <- takeTMVar tv
|
|
|
|
if cleaned
|
|
|
|
then do
|
|
|
|
putTMVar tv cleaned
|
|
|
|
return noop
|
|
|
|
else return $ do
|
|
|
|
sshCleanup
|
|
|
|
liftIO $ atomically $ putTMVar tv True
|
2017-05-11 21:33:18 +00:00
|
|
|
-- Cleanup at shutdown.
|
2020-12-11 19:28:58 +00:00
|
|
|
Annex.addCleanupAction SshCachingCleanup sshCleanup
|
2017-05-11 21:33:18 +00:00
|
|
|
|
|
|
|
let socketlock = socket2lock socketfile
|
|
|
|
|
2020-09-16 15:41:28 +00:00
|
|
|
getConcurrency >>= \case
|
2019-05-10 17:24:31 +00:00
|
|
|
NonConcurrent -> return ()
|
2018-06-05 16:51:17 +00:00
|
|
|
Concurrent {} -> makeconnection socketlock
|
2019-05-10 17:24:31 +00:00
|
|
|
ConcurrentPerCpu -> makeconnection socketlock
|
2017-05-13 17:13:13 +00:00
|
|
|
|
2017-05-11 21:33:18 +00:00
|
|
|
lockFileCached socketlock
|
2017-05-16 19:28:06 +00:00
|
|
|
where
|
|
|
|
-- When the LockCache already has the socketlock in it,
|
|
|
|
-- the connection has already been started. Otherwise,
|
|
|
|
-- get the connection started now.
|
2018-11-19 19:00:24 +00:00
|
|
|
makeconnection socketlock = debugLocks $
|
2018-03-07 21:25:42 +00:00
|
|
|
whenM (isNothing <$> fromLockCache socketlock) $
|
|
|
|
-- See if ssh can connect in batch mode,
|
|
|
|
-- if so there's no need to block for a password
|
|
|
|
-- prompt.
|
2018-11-20 15:43:33 +00:00
|
|
|
unlessM (tryssh ["-o", "BatchMode=true"]) $
|
2018-03-07 21:25:42 +00:00
|
|
|
-- ssh needs to prompt (probably)
|
|
|
|
-- If the user enters the wrong password,
|
|
|
|
-- ssh will tell them, so we can ignore
|
|
|
|
-- failure.
|
2018-11-20 15:43:33 +00:00
|
|
|
void $ prompt $ tryssh []
|
2018-03-07 21:25:42 +00:00
|
|
|
-- Try to ssh to the host quietly. Returns True if ssh apparently
|
|
|
|
-- connected to the host successfully. If ssh failed to connect,
|
|
|
|
-- returns False.
|
|
|
|
-- Even if ssh is forced to run some specific command, this will
|
|
|
|
-- return True.
|
|
|
|
-- (Except there's an unlikely false positive where a forced
|
|
|
|
-- ssh command exits 255.)
|
2018-03-15 19:00:19 +00:00
|
|
|
tryssh extraps = liftIO $ withNullHandle $ \nullh -> do
|
2020-06-03 19:48:09 +00:00
|
|
|
let p = (proc "ssh" $ concat
|
2018-03-07 21:25:42 +00:00
|
|
|
[ extraps
|
|
|
|
, toCommand sshparams
|
|
|
|
, [fromSshHost sshhost, "true"]
|
2020-06-03 19:48:09 +00:00
|
|
|
])
|
2018-03-15 19:00:19 +00:00
|
|
|
{ std_out = UseHandle nullh
|
|
|
|
, std_err = UseHandle nullh
|
|
|
|
}
|
2020-06-03 19:48:09 +00:00
|
|
|
withCreateProcess p $ \_ _ _ pid -> do
|
|
|
|
exitcode <- waitForProcess pid
|
|
|
|
return $ case exitcode of
|
|
|
|
ExitFailure 255 -> False
|
|
|
|
_ -> True
|
2014-04-12 19:59:34 +00:00
|
|
|
|
2016-10-26 17:16:41 +00:00
|
|
|
{- Find ssh socket files.
|
|
|
|
-
|
|
|
|
- The check that the lock file exists makes only socket files
|
|
|
|
- that were set up by prepSocket be found. On some NFS systems,
|
|
|
|
- a deleted socket file may linger for a while under another filename;
|
|
|
|
- and this check makes such files be skipped since the corresponding lock
|
|
|
|
- file won't exist.
|
|
|
|
-}
|
2014-04-12 20:32:59 +00:00
|
|
|
enumSocketFiles :: Annex [FilePath]
|
2016-10-26 17:16:41 +00:00
|
|
|
enumSocketFiles = liftIO . go =<< sshCacheDir
|
2014-04-12 20:32:59 +00:00
|
|
|
where
|
|
|
|
go Nothing = return []
|
2020-10-29 16:02:46 +00:00
|
|
|
go (Just dir) = filterM (R.doesPathExist . socket2lock)
|
2016-10-26 17:16:41 +00:00
|
|
|
=<< filter (not . isLock)
|
2020-10-29 16:02:46 +00:00
|
|
|
<$> catchDefaultIO [] (dirContents (fromRawFilePath dir))
|
2014-04-12 20:32:59 +00:00
|
|
|
|
|
|
|
{- Stop any unused ssh connection caching processes. -}
|
2012-01-20 19:34:52 +00:00
|
|
|
sshCleanup :: Annex ()
|
2014-04-12 20:32:59 +00:00
|
|
|
sshCleanup = mapM_ cleanup =<< enumSocketFiles
|
2012-12-13 04:24:19 +00:00
|
|
|
where
|
|
|
|
cleanup socketfile = do
|
2013-08-02 16:27:32 +00:00
|
|
|
#ifndef mingw32_HOST_OS
|
2012-12-13 04:24:19 +00:00
|
|
|
-- Drop any shared lock we have, and take an
|
|
|
|
-- exclusive lock, without blocking. If the lock
|
|
|
|
-- succeeds, nothing is using this ssh, and it can
|
|
|
|
-- be stopped.
|
2014-04-12 20:32:59 +00:00
|
|
|
--
|
|
|
|
-- After ssh is stopped cannot remove the lock file;
|
|
|
|
-- other processes may be waiting on our exclusive
|
|
|
|
-- lock to use it.
|
2012-12-13 04:24:19 +00:00
|
|
|
let lockfile = socket2lock socketfile
|
|
|
|
unlockFile lockfile
|
|
|
|
mode <- annexFileMode
|
2017-12-05 19:00:50 +00:00
|
|
|
noUmask mode (tryLockExclusive (Just mode) lockfile) >>= \case
|
2014-08-20 22:56:25 +00:00
|
|
|
Nothing -> noop
|
|
|
|
Just lck -> do
|
|
|
|
forceStopSsh socketfile
|
|
|
|
liftIO $ dropLock lck
|
2013-05-11 20:03:00 +00:00
|
|
|
#else
|
2014-04-12 20:32:59 +00:00
|
|
|
forceStopSsh socketfile
|
2013-05-11 20:03:00 +00:00
|
|
|
#endif
|
2014-04-12 20:32:59 +00:00
|
|
|
|
|
|
|
{- Stop all ssh connection caching processes, even when they're in use. -}
|
|
|
|
forceSshCleanup :: Annex ()
|
|
|
|
forceSshCleanup = mapM_ forceStopSsh =<< enumSocketFiles
|
|
|
|
|
|
|
|
forceStopSsh :: FilePath -> Annex ()
|
2020-06-04 19:36:34 +00:00
|
|
|
forceStopSsh socketfile = withNullHandle $ \nullh -> do
|
2014-04-12 20:32:59 +00:00
|
|
|
let (dir, base) = splitFileName socketfile
|
2020-06-04 19:36:34 +00:00
|
|
|
let p = (proc "ssh" $ toCommand $
|
|
|
|
[ Param "-O", Param "stop" ] ++
|
|
|
|
sshConnectionCachingParams base ++
|
|
|
|
[Param "localhost"])
|
|
|
|
{ cwd = Just dir
|
|
|
|
-- "ssh -O stop" is noisy on stderr even with -q
|
|
|
|
, std_out = UseHandle nullh
|
|
|
|
, std_err = UseHandle nullh
|
|
|
|
}
|
|
|
|
void $ liftIO $ catchMaybeIO $ withCreateProcess p $ \_ _ _ pid ->
|
|
|
|
forceSuccessProcess p pid
|
2020-11-24 16:38:12 +00:00
|
|
|
liftIO $ removeWhenExistsWith R.removeLink (toRawFilePath socketfile)
|
2012-01-20 19:34:52 +00:00
|
|
|
|
2013-07-22 19:06:47 +00:00
|
|
|
{- This needs to be as short as possible, due to limitations on the length
|
|
|
|
- of the path to a socket file. At the same time, it needs to be unique
|
|
|
|
- for each host.
|
|
|
|
-}
|
2020-10-29 16:02:46 +00:00
|
|
|
hostport2socket :: SshHost -> Maybe Integer -> RawFilePath
|
2017-08-18 02:11:31 +00:00
|
|
|
hostport2socket host Nothing = hostport2socket' $ fromSshHost host
|
|
|
|
hostport2socket host (Just port) = hostport2socket' $
|
|
|
|
fromSshHost host ++ "!" ++ show port
|
2020-10-29 16:02:46 +00:00
|
|
|
hostport2socket' :: String -> RawFilePath
|
2013-07-22 19:06:47 +00:00
|
|
|
hostport2socket' s
|
2020-10-29 16:02:46 +00:00
|
|
|
| length s > lengthofmd5s = toRawFilePath $ show $ md5 $ encodeBL s
|
|
|
|
| otherwise = toRawFilePath s
|
2013-10-07 00:43:22 +00:00
|
|
|
where
|
|
|
|
lengthofmd5s = 32
|
2012-01-20 19:34:52 +00:00
|
|
|
|
2020-10-29 16:02:46 +00:00
|
|
|
socket2lock :: FilePath -> RawFilePath
|
|
|
|
socket2lock socket = toRawFilePath (socket ++ lockExt)
|
2012-01-20 19:34:52 +00:00
|
|
|
|
|
|
|
isLock :: FilePath -> Bool
|
|
|
|
isLock f = lockExt `isSuffixOf` f
|
|
|
|
|
|
|
|
lockExt :: String
|
|
|
|
lockExt = ".lock"
|
2012-09-13 23:26:39 +00:00
|
|
|
|
|
|
|
{- This is the size of the sun_path component of sockaddr_un, which
|
|
|
|
- is the limit to the total length of the filename of a unix socket.
|
|
|
|
-
|
|
|
|
- On Linux, this is 108. On OSX, 104. TODO: Probe
|
|
|
|
-}
|
|
|
|
sizeof_sockaddr_un_sun_path :: Int
|
|
|
|
sizeof_sockaddr_un_sun_path = 100
|
|
|
|
|
|
|
|
{- Note that this looks at the true length of the path in bytes, as it will
|
|
|
|
- appear on disk. -}
|
2020-10-29 16:02:46 +00:00
|
|
|
valid_unix_socket_path :: RawFilePath -> Int -> Bool
|
|
|
|
valid_unix_socket_path f n = S.length f + n < sizeof_sockaddr_un_sun_path
|
2013-04-13 22:10:49 +00:00
|
|
|
|
|
|
|
{- Parses the SSH port, and returns the other OpenSSH options. If
|
|
|
|
- several ports are found, the last one takes precedence. -}
|
|
|
|
sshReadPort :: [String] -> (Maybe Integer, [String])
|
|
|
|
sshReadPort params = (port, reverse args)
|
|
|
|
where
|
|
|
|
(port,args) = aux (Nothing, []) params
|
|
|
|
aux (p,ps) [] = (p,ps)
|
|
|
|
aux (_,ps) ("-p":p:rest) = aux (readPort p, ps) rest
|
|
|
|
aux (p,ps) (q:rest) | "-p" `isPrefixOf` q = aux (readPort $ drop 2 q, ps) rest
|
|
|
|
| otherwise = aux (p,q:ps) rest
|
|
|
|
readPort p = fmap fst $ listToMaybe $ reads p
|
2014-04-12 19:59:34 +00:00
|
|
|
|
2015-02-12 20:12:32 +00:00
|
|
|
{- When this env var is set, git-annex runs ssh with the specified
|
|
|
|
- options. (The options are separated by newlines.)
|
2014-04-12 19:59:34 +00:00
|
|
|
-
|
2014-04-29 22:08:10 +00:00
|
|
|
- This is a workaround for GIT_SSH not being able to contain
|
2017-02-20 16:58:08 +00:00
|
|
|
- additional parameters to pass to ssh. (GIT_SSH_COMMAND can,
|
|
|
|
- but is not supported by older versions of git.) -}
|
2015-02-12 20:12:32 +00:00
|
|
|
sshOptionsEnv :: String
|
|
|
|
sshOptionsEnv = "GIT_ANNEX_SSHOPTION"
|
|
|
|
|
|
|
|
toSshOptionsEnv :: [CommandParam] -> String
|
|
|
|
toSshOptionsEnv = unlines . toCommand
|
|
|
|
|
|
|
|
fromSshOptionsEnv :: String -> [CommandParam]
|
|
|
|
fromSshOptionsEnv = map Param . lines
|
2014-04-12 19:59:34 +00:00
|
|
|
|
|
|
|
{- Enables ssh caching for git push/pull to a particular
|
|
|
|
- remote git repo. (Can safely be used on non-ssh remotes.)
|
|
|
|
-
|
2015-02-12 20:12:32 +00:00
|
|
|
- Also propigates any configured ssh-options.
|
|
|
|
-
|
2014-04-12 19:59:34 +00:00
|
|
|
- Like inRepo, the action is run with the local git repo.
|
|
|
|
- But here it's a modified version, with gitEnv to set GIT_SSH=git-annex,
|
2015-02-12 20:12:32 +00:00
|
|
|
- and sshOptionsEnv set so that git-annex will know what socket
|
2014-04-12 19:59:34 +00:00
|
|
|
- file to use. -}
|
2015-02-12 20:12:32 +00:00
|
|
|
inRepoWithSshOptionsTo :: Git.Repo -> RemoteGitConfig -> (Git.Repo -> IO a) -> Annex a
|
|
|
|
inRepoWithSshOptionsTo remote gc a =
|
|
|
|
liftIO . a =<< sshOptionsTo remote gc =<< gitRepo
|
2014-04-12 19:59:34 +00:00
|
|
|
|
2015-02-12 20:12:32 +00:00
|
|
|
{- To make any git commands be run with ssh caching enabled,
|
|
|
|
- and configured ssh-options alters the local Git.Repo's gitEnv
|
2015-11-10 20:52:30 +00:00
|
|
|
- to set GIT_SSH=git-annex, and set sshOptionsEnv when running git
|
2017-03-17 20:02:47 +00:00
|
|
|
- commands.
|
|
|
|
-
|
2017-04-07 15:35:20 +00:00
|
|
|
- If GIT_SSH or GIT_SSH_COMMAND are enabled, this has no effect. -}
|
2015-02-12 20:12:32 +00:00
|
|
|
sshOptionsTo :: Git.Repo -> RemoteGitConfig -> Git.Repo -> Annex Git.Repo
|
2015-11-10 20:52:30 +00:00
|
|
|
sshOptionsTo remote gc localr
|
2015-08-04 19:27:48 +00:00
|
|
|
| not (Git.repoIsUrl remote) || Git.repoIsHttp remote = unchanged
|
2014-04-14 01:39:04 +00:00
|
|
|
| otherwise = case Git.Url.hostuser remote of
|
2015-08-04 19:44:46 +00:00
|
|
|
Nothing -> unchanged
|
2017-04-07 15:35:20 +00:00
|
|
|
Just host -> ifM (liftIO $ safe_GIT_SSH <&&> gitSshEnvSet)
|
2017-03-17 20:02:47 +00:00
|
|
|
( unchanged
|
|
|
|
, do
|
2017-05-11 21:33:18 +00:00
|
|
|
let port = Git.Url.port remote
|
2023-04-10 17:38:14 +00:00
|
|
|
let sshhost = either giveup id (mkSshHost host)
|
2017-08-18 02:11:31 +00:00
|
|
|
(msockfile, cacheparams) <- sshCachingInfo (sshhost, port)
|
2017-03-17 20:02:47 +00:00
|
|
|
case msockfile of
|
|
|
|
Nothing -> use []
|
|
|
|
Just sockfile -> do
|
2018-06-05 16:51:17 +00:00
|
|
|
prepSocket sockfile sshhost $ concat
|
2017-08-18 02:11:31 +00:00
|
|
|
[ cacheparams
|
|
|
|
, map Param (remoteAnnexSshOptions gc)
|
|
|
|
, portParams port
|
|
|
|
, consumeStdinParams NoConsumeStdin
|
|
|
|
, [Param "-T"]
|
|
|
|
]
|
2017-05-11 21:33:18 +00:00
|
|
|
use cacheparams
|
2017-03-17 20:02:47 +00:00
|
|
|
)
|
2014-04-14 01:39:04 +00:00
|
|
|
where
|
2015-11-10 20:52:30 +00:00
|
|
|
unchanged = return localr
|
2014-04-12 19:59:34 +00:00
|
|
|
|
2015-05-31 02:01:52 +00:00
|
|
|
use opts = do
|
2015-08-04 20:53:38 +00:00
|
|
|
let sshopts = concat
|
2015-05-31 02:01:52 +00:00
|
|
|
[ opts
|
|
|
|
, map Param (remoteAnnexSshOptions gc)
|
|
|
|
]
|
2015-08-04 19:27:48 +00:00
|
|
|
if null sshopts
|
|
|
|
then unchanged
|
|
|
|
else do
|
|
|
|
command <- liftIO programPath
|
|
|
|
liftIO $ do
|
2015-11-10 20:52:30 +00:00
|
|
|
localr' <- addGitEnv localr sshOptionsEnv
|
2015-08-04 21:07:45 +00:00
|
|
|
(toSshOptionsEnv sshopts)
|
2017-03-17 20:02:47 +00:00
|
|
|
addGitEnv localr' gitSshEnv command
|
2015-05-31 02:01:52 +00:00
|
|
|
|
2015-02-12 20:12:32 +00:00
|
|
|
runSshOptions :: [String] -> String -> IO ()
|
|
|
|
runSshOptions args s = do
|
|
|
|
let args' = toCommand (fromSshOptionsEnv s) ++ args
|
2014-04-12 19:59:34 +00:00
|
|
|
let p = proc "ssh" args'
|
2020-06-03 19:48:09 +00:00
|
|
|
exitcode <- withCreateProcess p $ \_ _ _ pid -> waitForProcess pid
|
|
|
|
exitWith exitcode
|
2014-04-29 22:08:10 +00:00
|
|
|
|
|
|
|
{- When this env var is set, git-annex is being used as a ssh-askpass
|
|
|
|
- program, and should read the password from the specified location,
|
|
|
|
- and output it for ssh to read. -}
|
|
|
|
sshAskPassEnv :: String
|
|
|
|
sshAskPassEnv = "GIT_ANNEX_SSHASKPASS"
|
|
|
|
|
|
|
|
runSshAskPass :: FilePath -> IO ()
|
|
|
|
runSshAskPass passfile = putStrLn =<< readFile passfile
|