2013-09-07 22:38:00 +00:00
|
|
|
{- git remotes encrypted using git-remote-gcrypt
|
|
|
|
-
|
|
|
|
- Copyright 2013 Joey Hess <joey@kitenet.net>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
module Remote.GCrypt (remote, gen) where
|
|
|
|
|
|
|
|
import qualified Data.Map as M
|
2013-09-08 17:00:48 +00:00
|
|
|
import qualified Data.ByteString.Lazy as L
|
2013-09-07 22:38:00 +00:00
|
|
|
|
|
|
|
import Common.Annex
|
|
|
|
import Types.Remote
|
|
|
|
import Types.GitConfig
|
|
|
|
import Types.Crypto
|
|
|
|
import qualified Git
|
|
|
|
import qualified Git.Command
|
|
|
|
import qualified Git.Config
|
|
|
|
import qualified Git.GCrypt
|
2013-09-12 19:54:35 +00:00
|
|
|
import qualified Git.Construct
|
2013-09-07 22:38:00 +00:00
|
|
|
import qualified Git.Types as Git ()
|
|
|
|
import qualified Annex.Branch
|
|
|
|
import qualified Annex.Content
|
|
|
|
import Config
|
|
|
|
import Config.Cost
|
|
|
|
import Remote.Helper.Git
|
|
|
|
import Remote.Helper.Encryptable
|
2013-09-08 19:19:14 +00:00
|
|
|
import Remote.Helper.Special
|
2013-09-07 22:38:00 +00:00
|
|
|
import Utility.Metered
|
|
|
|
import Crypto
|
|
|
|
import Annex.UUID
|
2013-09-08 18:54:28 +00:00
|
|
|
import Annex.Ssh
|
|
|
|
import qualified Remote.Rsync
|
|
|
|
import Utility.Rsync
|
2013-09-12 19:54:35 +00:00
|
|
|
import Logs.Remote
|
2013-09-07 22:38:00 +00:00
|
|
|
|
|
|
|
remote :: RemoteType
|
|
|
|
remote = RemoteType {
|
|
|
|
typename = "gcrypt",
|
|
|
|
-- Remote.Git takes care of enumerating gcrypt remotes too,
|
|
|
|
-- and will call our gen on them.
|
|
|
|
enumerate = return [],
|
|
|
|
generate = gen,
|
|
|
|
setup = gCryptSetup
|
|
|
|
}
|
|
|
|
|
2013-09-12 19:54:35 +00:00
|
|
|
gen :: Git.Repo -> UUID -> RemoteConfig -> RemoteGitConfig -> Annex (Maybe Remote)
|
2013-09-07 22:38:00 +00:00
|
|
|
gen gcryptr u c gc = do
|
|
|
|
g <- gitRepo
|
|
|
|
-- get underlying git repo with real path, not gcrypt path
|
|
|
|
r <- liftIO $ Git.GCrypt.encryptedRepo g gcryptr
|
|
|
|
let r' = r { Git.remoteName = Git.remoteName gcryptr }
|
|
|
|
-- read config of underlying repo if it's local
|
|
|
|
r'' <- if Git.repoIsLocalUnknown r'
|
|
|
|
then liftIO $ catchDefaultIO r' $ Git.Config.read r'
|
|
|
|
else return r'
|
2013-09-12 19:54:35 +00:00
|
|
|
-- doublecheck that local cache matches underlying repo's gcrypt-id
|
|
|
|
-- (which might not be set)
|
|
|
|
case (Git.Config.getMaybe "core.gcrypt-id" r'', Git.GCrypt.remoteRepoId g (Git.remoteName gcryptr)) of
|
|
|
|
(Just gcryptid, Just cachedgcryptid)
|
|
|
|
| gcryptid /= cachedgcryptid -> resetup gcryptid r''
|
|
|
|
_ -> gen' r'' u c gc
|
|
|
|
where
|
|
|
|
-- A different drive may have been mounted, making a different
|
|
|
|
-- gcrypt remote available. So need to set the cached
|
|
|
|
-- gcrypt-id and annex-uuid of the remote to match the remote
|
|
|
|
-- that is now available. Also need to set the gcrypt particiants
|
|
|
|
-- correctly.
|
|
|
|
resetup gcryptid r = do
|
|
|
|
let u' = genUUIDInNameSpace gCryptNameSpace gcryptid
|
|
|
|
v <- (M.lookup u' <$> readRemoteLog)
|
|
|
|
case (Git.remoteName gcryptr, v) of
|
|
|
|
(Just remotename, Just c') -> do
|
|
|
|
setGcryptEncryption c' remotename
|
|
|
|
setConfig (remoteConfig gcryptr "uuid") (fromUUID u')
|
|
|
|
setConfig (ConfigKey $ Git.GCrypt.remoteConfigKey "gcrypt-id" remotename) gcryptid
|
|
|
|
gen' r u' c' gc
|
|
|
|
_ -> do
|
|
|
|
warning $ "not using unknown gcrypt repository pointed to by remote " ++ Git.repoDescribe r
|
|
|
|
return Nothing
|
2013-09-07 22:38:00 +00:00
|
|
|
|
2013-09-12 19:54:35 +00:00
|
|
|
gen' :: Git.Repo -> UUID -> RemoteConfig -> RemoteGitConfig -> Annex (Maybe Remote)
|
2013-09-08 18:54:28 +00:00
|
|
|
gen' r u c gc = do
|
|
|
|
cst <- remoteCost gc $
|
|
|
|
if repoCheap r then nearlyCheapRemoteCost else expensiveRemoteCost
|
|
|
|
(rsynctransport, rsyncurl) <- rsyncTransport r
|
|
|
|
let rsyncopts = Remote.Rsync.genRsyncOpts c gc rsynctransport rsyncurl
|
|
|
|
let this = Remote
|
|
|
|
{ uuid = u
|
|
|
|
, cost = cst
|
|
|
|
, name = Git.repoDescribe r
|
|
|
|
, storeKey = \_ _ _ -> noCrypto
|
|
|
|
, retrieveKeyFile = \_ _ _ _ -> noCrypto
|
|
|
|
, retrieveKeyFileCheap = \_ _ -> return False
|
|
|
|
, removeKey = remove this rsyncopts
|
|
|
|
, hasKey = checkPresent this rsyncopts
|
|
|
|
, hasKeyCheap = repoCheap r
|
|
|
|
, whereisKey = Nothing
|
|
|
|
, config = M.empty
|
|
|
|
, localpath = localpathCalc r
|
|
|
|
, repo = r
|
|
|
|
, gitconfig = gc { remoteGitConfig = Just $ extractGitConfig r }
|
|
|
|
, readonly = Git.repoIsHttp r
|
|
|
|
, globallyAvailable = globallyAvailableCalc r
|
|
|
|
, remotetype = remote
|
|
|
|
}
|
2013-09-12 19:54:35 +00:00
|
|
|
return $ Just $ encryptableRemote c
|
2013-09-08 18:54:28 +00:00
|
|
|
(store this rsyncopts)
|
|
|
|
(retrieve this rsyncopts)
|
2013-09-07 22:38:00 +00:00
|
|
|
this
|
2013-09-08 18:54:28 +00:00
|
|
|
|
|
|
|
rsyncTransport :: Git.Repo -> Annex ([CommandParam], String)
|
|
|
|
rsyncTransport r
|
|
|
|
| "ssh://" `isPrefixOf` loc = sshtransport $ break (== '/') $ drop (length "ssh://") loc
|
|
|
|
| "//:" `isInfixOf` loc = othertransport
|
|
|
|
| ":" `isInfixOf` loc = sshtransport $ separate (== ':') loc
|
|
|
|
| otherwise = othertransport
|
|
|
|
where
|
|
|
|
loc = Git.repoLocation r
|
|
|
|
sshtransport (host, path) = do
|
|
|
|
opts <- sshCachingOptions (host, Nothing) []
|
|
|
|
return (rsyncShell $ Param "ssh" : opts, host ++ ":" ++ path)
|
|
|
|
othertransport = return ([], loc)
|
2013-09-07 22:38:00 +00:00
|
|
|
|
|
|
|
noCrypto :: Annex a
|
|
|
|
noCrypto = error "cannot use gcrypt remote without encryption enabled"
|
|
|
|
|
2013-09-08 17:00:48 +00:00
|
|
|
unsupportedUrl :: Annex a
|
|
|
|
unsupportedUrl = error "using non-ssh remote repo url with gcrypt is not supported"
|
|
|
|
|
2013-09-07 22:38:00 +00:00
|
|
|
gCryptSetup :: Maybe UUID -> RemoteConfig -> Annex (RemoteConfig, UUID)
|
|
|
|
gCryptSetup mu c = go $ M.lookup "gitrepo" c
|
|
|
|
where
|
|
|
|
remotename = fromJust (M.lookup "name" c)
|
|
|
|
go Nothing = error "Specify gitrepo="
|
|
|
|
go (Just gitrepo) = do
|
|
|
|
c' <- encryptionSetup c
|
|
|
|
inRepo $ Git.Command.run
|
|
|
|
[ Params "remote add"
|
|
|
|
, Param remotename
|
|
|
|
, Param $ Git.GCrypt.urlPrefix ++ gitrepo
|
|
|
|
]
|
|
|
|
|
2013-09-12 19:54:35 +00:00
|
|
|
setGcryptEncryption c' remotename
|
2013-09-07 22:38:00 +00:00
|
|
|
|
|
|
|
{- Run a git fetch and a push to the git repo in order to get
|
|
|
|
- its gcrypt-id set up, so that later git annex commands
|
|
|
|
- will use the remote as a ggcrypt remote. The fetch is
|
|
|
|
- needed if the repo already exists; the push is needed
|
|
|
|
- if the repo has not yet been initialized by gcrypt. -}
|
|
|
|
void $ inRepo $ Git.Command.runBool
|
|
|
|
[ Param "fetch"
|
|
|
|
, Param remotename
|
|
|
|
]
|
|
|
|
void $ inRepo $ Git.Command.runBool
|
|
|
|
[ Param "push"
|
|
|
|
, Param remotename
|
|
|
|
, Param $ show $ Annex.Branch.fullname
|
|
|
|
]
|
|
|
|
g <- inRepo Git.Config.reRead
|
|
|
|
case Git.GCrypt.remoteRepoId g (Just remotename) of
|
|
|
|
Nothing -> error "unable to determine gcrypt-id of remote"
|
2013-09-12 19:54:35 +00:00
|
|
|
Just gcryptid -> do
|
|
|
|
let u = genUUIDInNameSpace gCryptNameSpace gcryptid
|
2013-09-07 22:38:00 +00:00
|
|
|
if Just u == mu || mu == Nothing
|
2013-09-08 19:19:14 +00:00
|
|
|
then do
|
2013-09-12 19:54:35 +00:00
|
|
|
-- Store gcrypt-id in local
|
|
|
|
-- gcrypt repository, for later
|
|
|
|
-- double-check.
|
|
|
|
r <- inRepo $ Git.Construct.fromRemoteLocation gitrepo
|
|
|
|
when (Git.repoIsLocalUnknown r) $ do
|
|
|
|
r' <- liftIO $ Git.Config.read r
|
|
|
|
liftIO $ Git.Command.run [Param "config", Param "core.gcrypt-id", Param gcryptid] r'
|
2013-09-08 19:19:14 +00:00
|
|
|
gitConfigSpecialRemote u c' "gcrypt" "true"
|
|
|
|
return (c', u)
|
2013-09-07 22:38:00 +00:00
|
|
|
else error "uuid mismatch"
|
|
|
|
|
2013-09-12 19:54:35 +00:00
|
|
|
{- Configure gcrypt to use the same list of keyids that
|
|
|
|
- were passed to initremote. (For shared encryption,
|
|
|
|
- gcrypt's default behavior is used.) -}
|
|
|
|
setGcryptEncryption :: RemoteConfig -> String -> Annex ()
|
|
|
|
setGcryptEncryption c remotename = do
|
|
|
|
let participants = ConfigKey $ Git.GCrypt.remoteParticipantConfigKey remotename
|
|
|
|
case extractCipher c of
|
|
|
|
Nothing -> noCrypto
|
|
|
|
Just (EncryptedCipher _ _ (KeyIds { keyIds = ks})) ->
|
|
|
|
setConfig participants (unwords ks)
|
|
|
|
Just (SharedCipher _) ->
|
|
|
|
unsetConfig participants
|
|
|
|
|
2013-09-08 18:54:28 +00:00
|
|
|
store :: Remote -> Remote.Rsync.RsyncOpts -> (Cipher, Key) -> Key -> MeterUpdate -> Annex Bool
|
|
|
|
store r rsyncopts (cipher, enck) k p
|
2013-09-07 22:38:00 +00:00
|
|
|
| not $ Git.repoIsUrl (repo r) = guardUsable (repo r) False $
|
|
|
|
sendwith $ \meterupdate h -> do
|
|
|
|
createDirectoryIfMissing True $ parentDir dest
|
|
|
|
readBytes (meteredWriteFile meterupdate dest) h
|
|
|
|
return True
|
2013-09-08 18:54:28 +00:00
|
|
|
| Git.repoIsSsh (repo r) = Remote.Rsync.storeEncrypted rsyncopts gpgopts (cipher, enck) k p
|
2013-09-08 17:00:48 +00:00
|
|
|
| otherwise = unsupportedUrl
|
2013-09-07 22:38:00 +00:00
|
|
|
where
|
2013-09-08 18:54:28 +00:00
|
|
|
gpgopts = getGpgEncParams r
|
2013-09-07 22:38:00 +00:00
|
|
|
dest = gCryptLocation r enck
|
|
|
|
sendwith a = metered (Just p) k $ \meterupdate ->
|
|
|
|
Annex.Content.sendAnnex k noop $ \src ->
|
|
|
|
liftIO $ catchBoolIO $
|
2013-09-08 18:54:28 +00:00
|
|
|
encrypt gpgopts cipher (feedFile src) (a meterupdate)
|
2013-09-07 22:38:00 +00:00
|
|
|
|
2013-09-08 18:54:28 +00:00
|
|
|
retrieve :: Remote -> Remote.Rsync.RsyncOpts -> (Cipher, Key) -> Key -> FilePath -> MeterUpdate -> Annex Bool
|
|
|
|
retrieve r rsyncopts (cipher, enck) k d p
|
2013-09-08 17:00:48 +00:00
|
|
|
| not $ Git.repoIsUrl (repo r) = guardUsable (repo r) False $ do
|
|
|
|
retrievewith $ L.readFile src
|
|
|
|
return True
|
2013-09-08 18:54:28 +00:00
|
|
|
| Git.repoIsSsh (repo r) = Remote.Rsync.retrieveEncrypted rsyncopts (cipher, enck) k d p
|
2013-09-08 17:00:48 +00:00
|
|
|
| otherwise = unsupportedUrl
|
|
|
|
where
|
|
|
|
src = gCryptLocation r enck
|
|
|
|
retrievewith a = metered (Just p) k $ \meterupdate -> liftIO $
|
|
|
|
a >>= \b ->
|
|
|
|
decrypt cipher (feedBytes b)
|
|
|
|
(readBytes $ meteredWriteFile meterupdate d)
|
2013-09-07 22:38:00 +00:00
|
|
|
|
2013-09-08 18:54:28 +00:00
|
|
|
remove :: Remote -> Remote.Rsync.RsyncOpts -> Key -> Annex Bool
|
|
|
|
remove r rsyncopts k
|
2013-09-08 17:00:48 +00:00
|
|
|
| not $ Git.repoIsUrl (repo r) = guardUsable (repo r) False $ do
|
|
|
|
liftIO $ removeDirectoryRecursive (parentDir dest)
|
|
|
|
return True
|
2013-09-08 18:54:28 +00:00
|
|
|
| Git.repoIsSsh (repo r) = Remote.Rsync.remove rsyncopts k
|
2013-09-08 17:00:48 +00:00
|
|
|
| otherwise = unsupportedUrl
|
|
|
|
where
|
|
|
|
dest = gCryptLocation r k
|
2013-09-07 22:38:00 +00:00
|
|
|
|
2013-09-08 18:54:28 +00:00
|
|
|
checkPresent :: Remote -> Remote.Rsync.RsyncOpts -> Key -> Annex (Either String Bool)
|
|
|
|
checkPresent r rsyncopts k
|
2013-09-07 22:38:00 +00:00
|
|
|
| not $ Git.repoIsUrl (repo r) =
|
|
|
|
guardUsable (repo r) unknown $
|
|
|
|
liftIO $ catchDefaultIO unknown $
|
|
|
|
Right <$> doesFileExist (gCryptLocation r k)
|
2013-09-08 18:54:28 +00:00
|
|
|
| Git.repoIsSsh (repo r) = Remote.Rsync.checkPresent (repo r) rsyncopts k
|
2013-09-08 17:00:48 +00:00
|
|
|
| otherwise = unsupportedUrl
|
2013-09-07 22:38:00 +00:00
|
|
|
where
|
|
|
|
unknown = Left $ "unable to check " ++ Git.repoDescribe (repo r) ++ show (repo r)
|
|
|
|
|
2013-09-08 18:54:28 +00:00
|
|
|
{- Annexed objects are stored directly under the top of the gcrypt repo
|
|
|
|
- (not in annex/objects), and are hashed using lower-case directories for max
|
|
|
|
- portability. -}
|
2013-09-07 22:38:00 +00:00
|
|
|
gCryptLocation :: Remote -> Key -> FilePath
|
2013-09-08 18:54:28 +00:00
|
|
|
gCryptLocation r key = Git.repoLocation (repo r) </> keyPath key hashDirLower
|