2012-11-14 23:32:27 +00:00
|
|
|
{- Credentials storage
|
|
|
|
-
|
2014-02-11 18:06:50 +00:00
|
|
|
- Copyright 2012-2014 Joey Hess <joey@kitenet.net>
|
2012-11-14 23:32:27 +00:00
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
2014-02-11 18:06:50 +00:00
|
|
|
module Creds (
|
|
|
|
module Types.Creds,
|
|
|
|
CredPairStorage(..),
|
|
|
|
setRemoteCredPair,
|
|
|
|
getRemoteCredPairFor,
|
|
|
|
getRemoteCredPair,
|
|
|
|
getEnvCredPair,
|
|
|
|
writeCacheCreds,
|
|
|
|
readCacheCreds,
|
2014-04-20 16:46:33 +00:00
|
|
|
removeCreds,
|
2014-02-11 18:06:50 +00:00
|
|
|
) where
|
2012-11-14 23:32:27 +00:00
|
|
|
|
|
|
|
import Common.Annex
|
2014-02-11 18:06:50 +00:00
|
|
|
import Types.Creds
|
2012-11-14 23:32:27 +00:00
|
|
|
import Annex.Perms
|
|
|
|
import Utility.FileMode
|
|
|
|
import Crypto
|
|
|
|
import Types.Remote (RemoteConfig, RemoteConfigKey)
|
2014-08-12 19:35:29 +00:00
|
|
|
import Remote.Helper.Encryptable (remoteCipher, remoteCipher', embedCreds)
|
2014-02-11 18:06:50 +00:00
|
|
|
import Utility.Env (getEnv)
|
2012-11-14 23:32:27 +00:00
|
|
|
|
|
|
|
import qualified Data.ByteString.Lazy.Char8 as L
|
|
|
|
import qualified Data.Map as M
|
|
|
|
import Utility.Base64
|
|
|
|
|
|
|
|
{- A CredPair can be stored in a file, or in the environment, or perhaps
|
|
|
|
- in a remote's configuration. -}
|
|
|
|
data CredPairStorage = CredPairStorage
|
|
|
|
{ credPairFile :: FilePath
|
|
|
|
, credPairEnvironment :: (String, String)
|
|
|
|
, credPairRemoteKey :: Maybe RemoteConfigKey
|
|
|
|
}
|
|
|
|
|
2012-11-19 21:32:58 +00:00
|
|
|
{- Stores creds in a remote's configuration, if the remote allows
|
2014-02-11 18:06:50 +00:00
|
|
|
- that. Otherwise, caches them locally.
|
|
|
|
- The creds are found in storage if not provided. -}
|
|
|
|
setRemoteCredPair :: RemoteConfig -> CredPairStorage -> Maybe CredPair -> Annex RemoteConfig
|
|
|
|
setRemoteCredPair c storage Nothing =
|
|
|
|
maybe (return c) (setRemoteCredPair c storage . Just)
|
2013-12-27 20:01:43 +00:00
|
|
|
=<< getRemoteCredPair c storage
|
2014-02-11 18:06:50 +00:00
|
|
|
setRemoteCredPair c storage (Just creds)
|
2013-12-27 20:01:43 +00:00
|
|
|
| embedCreds c = case credPairRemoteKey storage of
|
|
|
|
Nothing -> localcache
|
|
|
|
Just key -> storeconfig key =<< remoteCipher c
|
|
|
|
| otherwise = localcache
|
|
|
|
where
|
|
|
|
localcache = do
|
2012-11-19 21:32:58 +00:00
|
|
|
writeCacheCredPair creds storage
|
|
|
|
return c
|
|
|
|
|
2013-12-27 20:01:43 +00:00
|
|
|
storeconfig key (Just cipher) = do
|
2014-02-06 22:25:31 +00:00
|
|
|
s <- liftIO $ encrypt (getGpgEncParams c) cipher
|
2012-11-19 21:32:58 +00:00
|
|
|
(feedBytes $ L.pack $ encodeCredPair creds)
|
|
|
|
(readBytes $ return . L.unpack)
|
|
|
|
return $ M.insert key (toB64 s) c
|
2013-12-27 20:01:43 +00:00
|
|
|
storeconfig key Nothing =
|
2012-11-19 21:32:58 +00:00
|
|
|
return $ M.insert key (toB64 $ encodeCredPair creds) c
|
|
|
|
|
2012-11-14 23:32:27 +00:00
|
|
|
{- Gets a remote's credpair, from the environment if set, otherwise
|
2012-11-19 21:32:58 +00:00
|
|
|
- from the cache in gitAnnexCredsDir, or failing that, from the
|
2012-11-14 23:32:27 +00:00
|
|
|
- value in RemoteConfig. -}
|
2012-11-28 17:31:49 +00:00
|
|
|
getRemoteCredPairFor :: String -> RemoteConfig -> CredPairStorage -> Annex (Maybe CredPair)
|
|
|
|
getRemoteCredPairFor this c storage = maybe missing (return . Just) =<< getRemoteCredPair c storage
|
2012-11-20 20:43:58 +00:00
|
|
|
where
|
|
|
|
(loginvar, passwordvar) = credPairEnvironment storage
|
|
|
|
missing = do
|
|
|
|
warning $ unwords
|
|
|
|
[ "Set both", loginvar
|
|
|
|
, "and", passwordvar
|
|
|
|
, "to use", this
|
|
|
|
]
|
|
|
|
return Nothing
|
|
|
|
|
2012-11-28 17:31:49 +00:00
|
|
|
getRemoteCredPair :: RemoteConfig -> CredPairStorage -> Annex (Maybe CredPair)
|
|
|
|
getRemoteCredPair c storage = maybe fromcache (return . Just) =<< fromenv
|
2012-11-14 23:32:27 +00:00
|
|
|
where
|
|
|
|
fromenv = liftIO $ getEnvCredPair storage
|
|
|
|
fromcache = maybe fromconfig (return . Just) =<< readCacheCredPair storage
|
|
|
|
fromconfig = case credPairRemoteKey storage of
|
|
|
|
Just key -> do
|
2014-08-12 19:35:29 +00:00
|
|
|
mcipher <- remoteCipher' c
|
|
|
|
case (mcipher, M.lookup key c) of
|
|
|
|
(_, Nothing) -> return Nothing
|
|
|
|
(Just (_cipher, SharedCipher {}), Just bcreds) ->
|
|
|
|
-- When using a shared cipher, the
|
|
|
|
-- creds are not stored encrypted.
|
|
|
|
fromcreds $ fromB64 bcreds
|
|
|
|
(Just (cipher, _), Just enccreds) -> do
|
2012-11-18 19:27:44 +00:00
|
|
|
creds <- liftIO $ decrypt cipher
|
|
|
|
(feedBytes $ L.pack $ fromB64 enccreds)
|
|
|
|
(readBytes $ return . L.unpack)
|
2012-11-19 21:32:58 +00:00
|
|
|
fromcreds creds
|
2014-08-12 19:35:29 +00:00
|
|
|
(Nothing, Just bcreds) ->
|
2012-11-19 21:32:58 +00:00
|
|
|
fromcreds $ fromB64 bcreds
|
2012-11-14 23:32:27 +00:00
|
|
|
Nothing -> return Nothing
|
2012-11-19 21:32:58 +00:00
|
|
|
fromcreds creds = case decodeCredPair creds of
|
|
|
|
Just credpair -> do
|
|
|
|
writeCacheCredPair credpair storage
|
|
|
|
return $ Just credpair
|
2013-04-03 07:52:41 +00:00
|
|
|
_ -> error "bad creds"
|
2012-11-14 23:32:27 +00:00
|
|
|
|
|
|
|
{- Gets a CredPair from the environment. -}
|
|
|
|
getEnvCredPair :: CredPairStorage -> IO (Maybe CredPair)
|
|
|
|
getEnvCredPair storage = liftM2 (,)
|
2013-09-22 18:13:31 +00:00
|
|
|
<$> getEnv uenv
|
|
|
|
<*> getEnv penv
|
2012-11-14 23:32:27 +00:00
|
|
|
where
|
|
|
|
(uenv, penv) = credPairEnvironment storage
|
|
|
|
|
|
|
|
writeCacheCredPair :: CredPair -> CredPairStorage -> Annex ()
|
|
|
|
writeCacheCredPair credpair storage =
|
|
|
|
writeCacheCreds (encodeCredPair credpair) (credPairFile storage)
|
|
|
|
|
|
|
|
{- Stores the creds in a file inside gitAnnexCredsDir that only the user
|
|
|
|
- can read. -}
|
|
|
|
writeCacheCreds :: Creds -> FilePath -> Annex ()
|
|
|
|
writeCacheCreds creds file = do
|
|
|
|
d <- fromRepo gitAnnexCredsDir
|
|
|
|
createAnnexDirectory d
|
2013-05-09 17:57:31 +00:00
|
|
|
liftIO $ writeFileProtected (d </> file) creds
|
2012-11-14 23:32:27 +00:00
|
|
|
|
|
|
|
readCacheCredPair :: CredPairStorage -> Annex (Maybe CredPair)
|
|
|
|
readCacheCredPair storage = maybe Nothing decodeCredPair
|
|
|
|
<$> readCacheCreds (credPairFile storage)
|
|
|
|
|
|
|
|
readCacheCreds :: FilePath -> Annex (Maybe Creds)
|
|
|
|
readCacheCreds file = do
|
|
|
|
d <- fromRepo gitAnnexCredsDir
|
|
|
|
let f = d </> file
|
|
|
|
liftIO $ catchMaybeIO $ readFile f
|
|
|
|
|
|
|
|
encodeCredPair :: CredPair -> Creds
|
|
|
|
encodeCredPair (l, p) = unlines [l, p]
|
|
|
|
|
|
|
|
decodeCredPair :: Creds -> Maybe CredPair
|
|
|
|
decodeCredPair creds = case lines creds of
|
|
|
|
l:p:[] -> Just (l, p)
|
|
|
|
_ -> Nothing
|
2014-04-20 16:46:33 +00:00
|
|
|
|
|
|
|
removeCreds :: FilePath -> Annex ()
|
|
|
|
removeCreds file = do
|
|
|
|
d <- fromRepo gitAnnexCredsDir
|
|
|
|
let f = d </> file
|
|
|
|
liftIO $ nukeFile f
|