2012-01-20 19:34:52 +00:00
|
|
|
{- git-annex ssh interface, with connection caching
|
|
|
|
-
|
2013-02-19 18:56:24 +00:00
|
|
|
- Copyright 2012,2013 Joey Hess <joey@kitenet.net>
|
2012-01-20 19:34:52 +00:00
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
2012-10-15 18:49:40 +00:00
|
|
|
{-# LANGUAGE CPP #-}
|
|
|
|
|
2012-01-20 19:34:52 +00:00
|
|
|
module Annex.Ssh (
|
2013-04-13 22:10:49 +00:00
|
|
|
sshCachingOptions,
|
2012-01-20 19:34:52 +00:00
|
|
|
sshCleanup,
|
2013-05-14 17:53:29 +00:00
|
|
|
sshCacheDir,
|
2013-04-13 22:10:49 +00:00
|
|
|
sshReadPort,
|
2012-01-20 19:34:52 +00:00
|
|
|
) where
|
|
|
|
|
|
|
|
import qualified Data.Map as M
|
2013-07-22 19:06:47 +00:00
|
|
|
import Data.Hash.MD5
|
2012-01-20 19:34:52 +00:00
|
|
|
|
|
|
|
import Common.Annex
|
|
|
|
import Annex.LockPool
|
2012-02-25 23:15:29 +00:00
|
|
|
import qualified Build.SysConfig as SysConfig
|
2012-12-30 03:10:18 +00:00
|
|
|
import qualified Annex
|
2013-02-19 18:56:24 +00:00
|
|
|
import Config
|
2013-05-11 22:23:41 +00:00
|
|
|
import Utility.Env
|
2013-08-04 17:12:18 +00:00
|
|
|
#ifndef mingw32_HOST_OS
|
|
|
|
import Annex.Perms
|
|
|
|
#endif
|
2012-01-20 19:34:52 +00:00
|
|
|
|
|
|
|
{- Generates parameters to ssh to a given host (or user@host) on a given
|
|
|
|
- port, with connection caching. -}
|
2013-04-13 22:10:49 +00:00
|
|
|
sshCachingOptions :: (String, Maybe Integer) -> [CommandParam] -> Annex [CommandParam]
|
|
|
|
sshCachingOptions (host, port) opts = go =<< sshInfo (host, port)
|
2012-12-13 04:24:19 +00:00
|
|
|
where
|
|
|
|
go (Nothing, params) = ret params
|
|
|
|
go (Just socketfile, params) = do
|
|
|
|
cleanstale
|
|
|
|
liftIO $ createDirectoryIfMissing True $ parentDir socketfile
|
|
|
|
lockFile $ socket2lock socketfile
|
|
|
|
ret params
|
2013-04-13 22:10:49 +00:00
|
|
|
ret ps = return $ ps ++ opts ++ portParams port ++ [Param "-T"]
|
2012-12-13 04:24:19 +00:00
|
|
|
-- If the lock pool is empty, this is the first ssh of this
|
|
|
|
-- run. There could be stale ssh connections hanging around
|
|
|
|
-- from a previous git-annex run that was interrupted.
|
|
|
|
cleanstale = whenM (not . any isLock . M.keys <$> getPool) $
|
|
|
|
sshCleanup
|
2012-01-20 19:34:52 +00:00
|
|
|
|
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. -}
|
2012-01-20 21:13:36 +00:00
|
|
|
sshInfo :: (String, Maybe Integer) -> Annex (Maybe FilePath, [CommandParam])
|
2013-02-19 18:56:24 +00:00
|
|
|
sshInfo (host, port) = go =<< sshCacheDir
|
|
|
|
where
|
|
|
|
go Nothing = return (Nothing, [])
|
|
|
|
go (Just dir) = do
|
2012-03-14 21:43:34 +00:00
|
|
|
let socketfile = dir </> hostport2socket host port
|
2012-09-13 23:26:39 +00:00
|
|
|
if valid_unix_socket_path socketfile
|
2013-07-21 18:14:54 +00:00
|
|
|
then return (Just socketfile, sshConnectionCachingParams socketfile)
|
2012-09-13 23:26:39 +00:00
|
|
|
else do
|
|
|
|
socketfile' <- liftIO $ relPathCwdToFile socketfile
|
|
|
|
if valid_unix_socket_path socketfile'
|
2013-07-21 18:14:54 +00:00
|
|
|
then return (Just socketfile', sshConnectionCachingParams socketfile')
|
2012-09-13 23:26:39 +00:00
|
|
|
else return (Nothing, [])
|
2013-07-21 18:14:54 +00:00
|
|
|
|
|
|
|
sshConnectionCachingParams :: FilePath -> [CommandParam]
|
|
|
|
sshConnectionCachingParams socketfile =
|
|
|
|
[ Param "-S", Param socketfile
|
|
|
|
, Params "-o ControlMaster=auto -o ControlPersist=yes"
|
|
|
|
]
|
2012-01-20 19:34:52 +00:00
|
|
|
|
2013-06-18 02:13:28 +00:00
|
|
|
{- ssh connection caching creates sockets, so will not work on a
|
|
|
|
- crippled filesystem. A GIT_ANNEX_TMP_DIR can be provided to use
|
2013-02-19 18:56:24 +00:00
|
|
|
- a different filesystem. -}
|
|
|
|
sshCacheDir :: Annex (Maybe FilePath)
|
|
|
|
sshCacheDir
|
2013-06-18 02:13:28 +00:00
|
|
|
| SysConfig.sshconnectioncaching = ifM crippledFileSystem
|
|
|
|
( maybe (return Nothing) usetmpdir =<< gettmpdir
|
|
|
|
, ifM (fromMaybe True . annexSshCaching <$> Annex.getGitConfig)
|
2013-02-19 18:56:24 +00:00
|
|
|
( Just <$> fromRepo gitAnnexSshDir
|
2013-06-18 02:13:28 +00:00
|
|
|
, return Nothing
|
2013-02-19 18:56:24 +00:00
|
|
|
)
|
2013-06-18 02:13:28 +00:00
|
|
|
)
|
2013-02-19 18:56:24 +00:00
|
|
|
| otherwise = return Nothing
|
|
|
|
where
|
|
|
|
gettmpdir = liftIO $ getEnv "GIT_ANNEX_TMP_DIR"
|
2013-02-19 21:31:08 +00:00
|
|
|
usetmpdir tmpdir = liftIO $ catchMaybeIO $ do
|
|
|
|
createDirectoryIfMissing True tmpdir
|
2013-04-03 07:52:41 +00:00
|
|
|
return tmpdir
|
2012-01-20 19:34:52 +00:00
|
|
|
|
|
|
|
portParams :: Maybe Integer -> [CommandParam]
|
|
|
|
portParams Nothing = []
|
|
|
|
portParams (Just port) = [Param "-p", Param $ show port]
|
|
|
|
|
|
|
|
{- Stop any unused ssh processes. -}
|
|
|
|
sshCleanup :: Annex ()
|
2013-02-19 18:56:24 +00:00
|
|
|
sshCleanup = go =<< sshCacheDir
|
2012-12-13 04:24:19 +00:00
|
|
|
where
|
2013-02-19 18:56:24 +00:00
|
|
|
go Nothing = noop
|
|
|
|
go (Just dir) = do
|
|
|
|
sockets <- filter (not . isLock) <$>
|
|
|
|
liftIO (catchDefaultIO [] $ dirContents dir)
|
|
|
|
forM_ sockets cleanup
|
2012-12-13 04:24:19 +00:00
|
|
|
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.
|
|
|
|
let lockfile = socket2lock socketfile
|
|
|
|
unlockFile lockfile
|
|
|
|
mode <- annexFileMode
|
|
|
|
fd <- liftIO $ noUmask mode $
|
|
|
|
openFd lockfile ReadWrite (Just mode) defaultFileFlags
|
|
|
|
v <- liftIO $ tryIO $
|
|
|
|
setLock fd (WriteLock, AbsoluteSeek, 0, 0)
|
|
|
|
case v of
|
|
|
|
Left _ -> noop
|
|
|
|
Right _ -> stopssh socketfile
|
|
|
|
liftIO $ closeFd fd
|
2013-05-11 20:03:00 +00:00
|
|
|
#else
|
|
|
|
stopssh socketfile
|
|
|
|
#endif
|
2012-12-13 04:24:19 +00:00
|
|
|
stopssh socketfile = do
|
2013-07-21 18:14:54 +00:00
|
|
|
let params = sshConnectionCachingParams socketfile
|
2012-12-13 04:24:19 +00:00
|
|
|
-- "ssh -O stop" is noisy on stderr even with -q
|
|
|
|
void $ liftIO $ catchMaybeIO $
|
|
|
|
withQuietOutput createProcessSuccess $
|
|
|
|
proc "ssh" $ toCommand $
|
|
|
|
[ Params "-O stop"
|
2013-07-21 18:14:54 +00:00
|
|
|
] ++ params ++ [Param "any"]
|
2012-12-13 04:24:19 +00:00
|
|
|
-- Cannot remove the lock file; other processes may
|
|
|
|
-- be waiting on our exclusive lock to use it.
|
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.
|
|
|
|
-}
|
2012-01-20 19:34:52 +00:00
|
|
|
hostport2socket :: String -> Maybe Integer -> FilePath
|
2013-07-22 19:06:47 +00:00
|
|
|
hostport2socket host Nothing = hostport2socket' host
|
|
|
|
hostport2socket host (Just port) = hostport2socket' $ host ++ "!" ++ show port
|
|
|
|
hostport2socket' :: String -> FilePath
|
|
|
|
hostport2socket' s
|
|
|
|
| length s > 32 = md5s (Str s)
|
|
|
|
| otherwise = s
|
2012-01-20 19:34:52 +00:00
|
|
|
|
|
|
|
socket2lock :: FilePath -> FilePath
|
|
|
|
socket2lock socket = socket ++ lockExt
|
|
|
|
|
|
|
|
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. -}
|
|
|
|
valid_unix_socket_path :: FilePath -> Bool
|
|
|
|
valid_unix_socket_path f = length (decodeW8 f) < 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
|