2012-11-15 00:25:00 +00:00
|
|
|
{- WebDAV remotes.
|
|
|
|
-
|
|
|
|
- Copyright 2012 Joey Hess <joey@kitenet.net>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
2012-11-16 04:09:22 +00:00
|
|
|
{-# LANGUAGE ScopedTypeVariables #-}
|
|
|
|
|
2012-11-28 17:31:49 +00:00
|
|
|
module Remote.WebDAV (remote, davCreds, setCredsEnv) where
|
2012-11-15 00:25:00 +00:00
|
|
|
|
|
|
|
import Network.Protocol.HTTP.DAV
|
|
|
|
import qualified Data.Map as M
|
2012-11-15 17:46:16 +00:00
|
|
|
import qualified Data.ByteString.UTF8 as B8
|
2012-11-16 21:58:58 +00:00
|
|
|
import qualified Data.ByteString.Lazy.UTF8 as L8
|
2012-11-15 00:25:00 +00:00
|
|
|
import qualified Data.ByteString.Lazy as L
|
2012-11-16 04:09:22 +00:00
|
|
|
import Network.URI (normalizePathSegments)
|
|
|
|
import qualified Control.Exception as E
|
|
|
|
import Network.HTTP.Conduit (HttpException(..))
|
|
|
|
import Network.HTTP.Types
|
2012-11-17 03:16:18 +00:00
|
|
|
import System.IO.Error
|
2012-11-15 00:25:00 +00:00
|
|
|
|
|
|
|
import Common.Annex
|
|
|
|
import Types.Remote
|
|
|
|
import qualified Git
|
|
|
|
import Config
|
|
|
|
import Remote.Helper.Special
|
|
|
|
import Remote.Helper.Encryptable
|
2012-11-16 21:58:58 +00:00
|
|
|
import Remote.Helper.Chunked
|
2012-11-15 00:25:00 +00:00
|
|
|
import Crypto
|
|
|
|
import Creds
|
2012-11-19 01:48:42 +00:00
|
|
|
import Meters
|
2012-11-15 00:25:00 +00:00
|
|
|
|
2012-11-15 17:46:16 +00:00
|
|
|
type DavUrl = String
|
|
|
|
type DavUser = B8.ByteString
|
|
|
|
type DavPass = B8.ByteString
|
|
|
|
|
2012-11-15 00:25:00 +00:00
|
|
|
remote :: RemoteType
|
|
|
|
remote = RemoteType {
|
|
|
|
typename = "webdav",
|
|
|
|
enumerate = findSpecialRemotes "webdav",
|
|
|
|
generate = gen,
|
|
|
|
setup = webdavSetup
|
|
|
|
}
|
|
|
|
|
2012-11-30 04:55:59 +00:00
|
|
|
gen :: Git.Repo -> UUID -> RemoteConfig -> Annex Remote
|
|
|
|
gen r u c = new <$> remoteCost r expensiveRemoteCost
|
|
|
|
where
|
|
|
|
new cst = encryptableRemote c
|
2012-11-15 00:25:00 +00:00
|
|
|
(storeEncrypted this)
|
|
|
|
(retrieveEncrypted this)
|
|
|
|
this
|
2012-11-30 04:55:59 +00:00
|
|
|
where
|
|
|
|
this = Remote {
|
|
|
|
uuid = u,
|
|
|
|
cost = cst,
|
|
|
|
name = Git.repoDescribe r,
|
|
|
|
storeKey = store this,
|
|
|
|
retrieveKeyFile = retrieve this,
|
|
|
|
retrieveKeyFileCheap = retrieveCheap this,
|
|
|
|
removeKey = remove this,
|
|
|
|
hasKey = checkPresent this,
|
|
|
|
hasKeyCheap = False,
|
|
|
|
whereisKey = Nothing,
|
|
|
|
config = c,
|
|
|
|
repo = r,
|
|
|
|
localpath = Nothing,
|
|
|
|
readonly = False,
|
|
|
|
remotetype = remote
|
|
|
|
}
|
2012-11-15 00:25:00 +00:00
|
|
|
|
|
|
|
webdavSetup :: UUID -> RemoteConfig -> Annex RemoteConfig
|
|
|
|
webdavSetup u c = do
|
|
|
|
let url = fromMaybe (error "Specify url=") $
|
|
|
|
M.lookup "url" c
|
|
|
|
c' <- encryptionSetup c
|
|
|
|
creds <- getCreds c' u
|
|
|
|
testDav url creds
|
|
|
|
gitConfigSpecialRemote u c' "webdav" "true"
|
2012-11-16 17:32:18 +00:00
|
|
|
setRemoteCredPair c' (davCreds u)
|
2012-11-15 00:25:00 +00:00
|
|
|
|
|
|
|
store :: Remote -> Key -> AssociatedFile -> MeterUpdate -> Annex Bool
|
2012-11-19 00:06:28 +00:00
|
|
|
store r k _f p = metered (Just p) k $ \meterupdate ->
|
|
|
|
davAction r False $ \(baseurl, user, pass) -> do
|
|
|
|
f <- inRepo $ gitAnnexLocation k
|
2012-11-19 01:48:42 +00:00
|
|
|
liftIO $ withMeteredFile f meterupdate $
|
2012-11-19 18:08:39 +00:00
|
|
|
storeHelper r k baseurl user pass
|
2012-11-15 00:25:00 +00:00
|
|
|
|
|
|
|
storeEncrypted :: Remote -> (Cipher, Key) -> Key -> MeterUpdate -> Annex Bool
|
2012-11-19 00:06:28 +00:00
|
|
|
storeEncrypted r (cipher, enck) k p = metered (Just p) k $ \meterupdate ->
|
|
|
|
davAction r False $ \(baseurl, user, pass) -> do
|
|
|
|
f <- inRepo $ gitAnnexLocation k
|
2012-11-19 02:49:07 +00:00
|
|
|
liftIO $ encrypt cipher (streamMeteredFile f meterupdate) $
|
2012-11-19 18:08:39 +00:00
|
|
|
readBytes $ storeHelper r enck baseurl user pass
|
2012-11-16 21:58:58 +00:00
|
|
|
|
2012-11-19 17:18:23 +00:00
|
|
|
storeHelper :: Remote -> Key -> DavUrl -> DavUser -> DavPass -> L.ByteString -> IO Bool
|
2012-11-19 18:08:39 +00:00
|
|
|
storeHelper r k baseurl user pass b = catchBoolIO $ do
|
|
|
|
davMkdir tmpurl user pass
|
|
|
|
storeChunks k tmpurl keyurl chunksize storer recorder finalizer
|
2012-11-16 21:58:58 +00:00
|
|
|
where
|
2012-11-19 18:08:39 +00:00
|
|
|
tmpurl = tmpLocation baseurl k
|
|
|
|
keyurl = davLocation baseurl k
|
2012-11-16 21:58:58 +00:00
|
|
|
chunksize = chunkSize $ config r
|
|
|
|
storer urls = storeChunked chunksize urls storehttp b
|
|
|
|
recorder url s = storehttp url (L8.fromString s)
|
2012-11-19 18:08:39 +00:00
|
|
|
finalizer srcurl desturl = do
|
|
|
|
void $ catchMaybeHttp (deleteContent desturl user pass)
|
|
|
|
davMkdir (urlParent desturl) user pass
|
2012-11-16 21:58:58 +00:00
|
|
|
moveContent srcurl (B8.fromString desturl) user pass
|
2012-12-01 21:12:41 +00:00
|
|
|
storehttp url v = putContent url user pass
|
|
|
|
(contentType, v)
|
2012-11-15 00:25:00 +00:00
|
|
|
|
|
|
|
retrieveCheap :: Remote -> Key -> FilePath -> Annex Bool
|
|
|
|
retrieveCheap _ _ _ = return False
|
|
|
|
|
2012-11-17 03:16:18 +00:00
|
|
|
retrieve :: Remote -> Key -> AssociatedFile -> FilePath -> Annex Bool
|
|
|
|
retrieve r k _f d = metered Nothing k $ \meterupdate ->
|
|
|
|
davAction r False $ \(baseurl, user, pass) -> liftIO $ catchBoolIO $
|
|
|
|
withStoredFiles r k baseurl user pass onerr $ \urls -> do
|
2012-11-18 22:27:53 +00:00
|
|
|
meteredWriteFileChunks meterupdate d urls $ \url -> do
|
|
|
|
mb <- davGetUrlContent url user pass
|
|
|
|
case mb of
|
|
|
|
Nothing -> throwIO "download failed"
|
|
|
|
Just b -> return b
|
2012-11-17 03:16:18 +00:00
|
|
|
return True
|
|
|
|
where
|
|
|
|
onerr _ = return False
|
2012-11-16 17:32:18 +00:00
|
|
|
|
2012-11-17 03:16:18 +00:00
|
|
|
retrieveEncrypted :: Remote -> (Cipher, Key) -> Key -> FilePath -> Annex Bool
|
|
|
|
retrieveEncrypted r (cipher, enck) k d = metered Nothing k $ \meterupdate ->
|
|
|
|
davAction r False $ \(baseurl, user, pass) -> liftIO $ catchBoolIO $
|
|
|
|
withStoredFiles r enck baseurl user pass onerr $ \urls -> do
|
2012-11-18 19:27:44 +00:00
|
|
|
decrypt cipher (feeder user pass urls) $
|
|
|
|
readBytes $ meteredWriteFile meterupdate d
|
2012-11-17 03:16:18 +00:00
|
|
|
return True
|
2012-11-16 17:32:18 +00:00
|
|
|
where
|
2012-11-17 03:16:18 +00:00
|
|
|
onerr _ = return False
|
|
|
|
|
2012-11-18 19:27:44 +00:00
|
|
|
feeder _ _ [] _ = noop
|
|
|
|
feeder user pass (url:urls) h = do
|
2012-11-17 03:16:18 +00:00
|
|
|
mb <- davGetUrlContent url user pass
|
|
|
|
case mb of
|
2012-11-17 19:30:11 +00:00
|
|
|
Nothing -> throwIO "download failed"
|
2012-11-18 19:27:44 +00:00
|
|
|
Just b -> do
|
|
|
|
L.hPut h b
|
|
|
|
feeder user pass urls h
|
2012-11-15 00:25:00 +00:00
|
|
|
|
|
|
|
remove :: Remote -> Key -> Annex Bool
|
2012-11-16 22:09:28 +00:00
|
|
|
remove r k = davAction r False $ \(baseurl, user, pass) -> liftIO $ do
|
|
|
|
-- Delete the key's whole directory, including any chunked
|
|
|
|
-- files, etc, in a single action.
|
2012-11-19 18:08:39 +00:00
|
|
|
let url = davLocation baseurl k
|
2012-11-16 22:09:28 +00:00
|
|
|
isJust <$> catchMaybeHttp (deleteContent url user pass)
|
2012-11-15 00:25:00 +00:00
|
|
|
|
|
|
|
checkPresent :: Remote -> Key -> Annex (Either String Bool)
|
2012-11-17 03:16:18 +00:00
|
|
|
checkPresent r k = davAction r noconn go
|
2012-11-15 00:25:00 +00:00
|
|
|
where
|
|
|
|
noconn = Left $ error $ name r ++ " not configured"
|
|
|
|
|
2012-11-17 03:16:18 +00:00
|
|
|
go (baseurl, user, pass) = do
|
|
|
|
showAction $ "checking " ++ name r
|
|
|
|
liftIO $ withStoredFiles r k baseurl user pass onerr check
|
|
|
|
where
|
|
|
|
check [] = return $ Right True
|
|
|
|
check (url:urls) = do
|
|
|
|
v <- davUrlExists url user pass
|
|
|
|
if v == Right True
|
|
|
|
then check urls
|
|
|
|
else return v
|
|
|
|
|
|
|
|
{- Failed to read the chunkcount file; see if it's missing,
|
|
|
|
- or if there's a problem accessing it,
|
|
|
|
- or perhaps this was an intermittent error. -}
|
|
|
|
onerr url = do
|
|
|
|
v <- davUrlExists url user pass
|
|
|
|
if v == Right True
|
|
|
|
then return $ Left $ "failed to read " ++ url
|
|
|
|
else return v
|
|
|
|
|
|
|
|
withStoredFiles
|
|
|
|
:: Remote
|
|
|
|
-> Key
|
|
|
|
-> DavUrl
|
|
|
|
-> DavUser
|
|
|
|
-> DavPass
|
|
|
|
-> (DavUrl -> IO a)
|
|
|
|
-> ([DavUrl] -> IO a)
|
|
|
|
-> IO a
|
|
|
|
withStoredFiles r k baseurl user pass onerr a
|
|
|
|
| isJust $ chunkSize $ config r = do
|
2012-11-19 18:08:39 +00:00
|
|
|
let chunkcount = keyurl ++ chunkCount
|
|
|
|
maybe (onerr chunkcount) (a . listChunks keyurl . L8.toString)
|
2012-11-17 03:16:18 +00:00
|
|
|
=<< davGetUrlContent chunkcount user pass
|
2012-11-19 18:08:39 +00:00
|
|
|
| otherwise = a [keyurl]
|
2012-11-17 03:16:18 +00:00
|
|
|
where
|
2012-11-19 18:08:39 +00:00
|
|
|
keyurl = davLocation baseurl k ++ keyFile k
|
2012-11-17 03:16:18 +00:00
|
|
|
|
2012-11-15 17:46:16 +00:00
|
|
|
davAction :: Remote -> a -> ((DavUrl, DavUser, DavPass) -> Annex a) -> Annex a
|
2012-11-30 04:55:59 +00:00
|
|
|
davAction r unconfigured action = do
|
|
|
|
mcreds <- getCreds (config r) (uuid r)
|
|
|
|
case (mcreds, M.lookup "url" $ config r) of
|
|
|
|
(Just (user, pass), Just url) ->
|
|
|
|
action (url, toDavUser user, toDavPass pass)
|
|
|
|
_ -> return unconfigured
|
2012-11-15 17:46:16 +00:00
|
|
|
|
|
|
|
toDavUser :: String -> DavUser
|
|
|
|
toDavUser = B8.fromString
|
2012-11-15 00:25:00 +00:00
|
|
|
|
2012-11-15 17:46:16 +00:00
|
|
|
toDavPass :: String -> DavPass
|
|
|
|
toDavPass = B8.fromString
|
|
|
|
|
2012-11-19 18:08:39 +00:00
|
|
|
{- The directory where files(s) for a key are stored. -}
|
2012-11-16 04:42:33 +00:00
|
|
|
davLocation :: DavUrl -> Key -> DavUrl
|
2012-11-19 18:08:39 +00:00
|
|
|
davLocation baseurl k = addTrailingPathSeparator $
|
|
|
|
davUrl baseurl $ hashDirLower k </> keyFile k
|
|
|
|
|
|
|
|
{- Where we store temporary data for a key as it's being uploaded. -}
|
|
|
|
tmpLocation :: DavUrl -> Key -> DavUrl
|
|
|
|
tmpLocation baseurl k = addTrailingPathSeparator $
|
|
|
|
davUrl baseurl $ "tmp" </> keyFile k
|
2012-11-15 17:46:16 +00:00
|
|
|
|
|
|
|
davUrl :: DavUrl -> FilePath -> DavUrl
|
2012-11-16 04:09:22 +00:00
|
|
|
davUrl baseurl file = baseurl </> file
|
|
|
|
|
2012-11-17 03:16:18 +00:00
|
|
|
davUrlExists :: DavUrl -> DavUser -> DavPass -> IO (Either String Bool)
|
|
|
|
davUrlExists url user pass = decode <$> catchHttp (getProps url user pass)
|
|
|
|
where
|
|
|
|
decode (Right _) = Right True
|
|
|
|
decode (Left (Left (StatusCodeException status _)))
|
|
|
|
| statusCode status == statusCode notFound404 = Right False
|
2012-11-17 19:30:11 +00:00
|
|
|
decode (Left e) = Left $ showEitherException e
|
2012-11-17 03:16:18 +00:00
|
|
|
|
|
|
|
davGetUrlContent :: DavUrl -> DavUser -> DavPass -> IO (Maybe L.ByteString)
|
|
|
|
davGetUrlContent url user pass = fmap (snd . snd) <$>
|
|
|
|
catchMaybeHttp (getPropsAndContent url user pass)
|
|
|
|
|
2012-11-16 04:09:22 +00:00
|
|
|
{- Creates a directory in WebDAV, if not already present; also creating
|
|
|
|
- any missing parent directories. -}
|
|
|
|
davMkdir :: DavUrl -> DavUser -> DavPass -> IO ()
|
|
|
|
davMkdir url user pass = go url
|
|
|
|
where
|
|
|
|
make u = makeCollection u user pass
|
|
|
|
|
|
|
|
go u = do
|
|
|
|
r <- E.try (make u) :: IO (Either E.SomeException Bool)
|
|
|
|
case r of
|
|
|
|
{- Parent directory is missing. Recurse to create
|
|
|
|
- it, and try once more to create the directory. -}
|
|
|
|
Right False -> do
|
|
|
|
go (urlParent u)
|
|
|
|
void $ make u
|
|
|
|
{- Directory created successfully -}
|
|
|
|
Right True -> return ()
|
|
|
|
{- Directory already exists, or some other error
|
|
|
|
- occurred. In the latter case, whatever wanted
|
|
|
|
- to use this directory will fail. -}
|
|
|
|
Left _ -> return ()
|
|
|
|
|
|
|
|
{- Catches HTTP and IO exceptions. -}
|
|
|
|
catchMaybeHttp :: IO a -> IO (Maybe a)
|
|
|
|
catchMaybeHttp a = (Just <$> a) `E.catches`
|
|
|
|
[ E.Handler $ \(_e :: HttpException) -> return Nothing
|
|
|
|
, E.Handler $ \(_e :: E.IOException) -> return Nothing
|
|
|
|
]
|
|
|
|
|
|
|
|
{- Catches HTTP and IO exceptions -}
|
2012-11-17 19:30:11 +00:00
|
|
|
catchHttp :: IO a -> IO (Either EitherException a)
|
2012-11-16 04:09:22 +00:00
|
|
|
catchHttp a = (Right <$> a) `E.catches`
|
|
|
|
[ E.Handler $ \(e :: HttpException) -> return $ Left $ Left e
|
|
|
|
, E.Handler $ \(e :: E.IOException) -> return $ Left $ Right e
|
|
|
|
]
|
|
|
|
|
2012-11-17 19:30:11 +00:00
|
|
|
type EitherException = Either HttpException E.IOException
|
|
|
|
|
|
|
|
showEitherException :: EitherException -> String
|
|
|
|
showEitherException (Left (StatusCodeException status _)) = show $ statusMessage status
|
|
|
|
showEitherException (Left httpexception) = show httpexception
|
|
|
|
showEitherException (Right ioexception) = show ioexception
|
|
|
|
|
|
|
|
throwIO :: String -> IO a
|
|
|
|
throwIO msg = ioError $ mkIOError userErrorType msg Nothing Nothing
|
|
|
|
|
2012-11-16 04:09:22 +00:00
|
|
|
urlParent :: DavUrl -> DavUrl
|
2012-11-19 18:08:39 +00:00
|
|
|
urlParent url = dropTrailingPathSeparator $
|
|
|
|
normalizePathSegments (dropTrailingPathSeparator url ++ "/..")
|
|
|
|
where
|
2012-11-15 00:25:00 +00:00
|
|
|
|
|
|
|
{- Test if a WebDAV store is usable, by writing to a test file, and then
|
2012-11-17 19:30:11 +00:00
|
|
|
- deleting the file. Exits with an IO error if not. -}
|
2012-11-15 00:25:00 +00:00
|
|
|
testDav :: String -> Maybe CredPair -> Annex ()
|
|
|
|
testDav baseurl (Just (u, p)) = do
|
|
|
|
showSideAction "testing WebDAV server"
|
2012-12-01 18:32:50 +00:00
|
|
|
test "make directory" $ davMkdir baseurl user pass
|
2012-12-01 21:12:41 +00:00
|
|
|
test "write file" $ putContent testurl user pass
|
|
|
|
(contentType, L.empty)
|
2012-12-01 18:32:50 +00:00
|
|
|
test "delete file" $ deleteContent testurl user pass
|
2012-11-17 19:30:11 +00:00
|
|
|
where
|
2012-12-01 18:32:50 +00:00
|
|
|
test desc a = liftIO $
|
|
|
|
either (\e -> throwIO $ "WebDAV failed to " ++ desc ++ ": " ++ showEitherException e)
|
|
|
|
(const noop)
|
|
|
|
=<< catchHttp a
|
2012-11-17 19:30:11 +00:00
|
|
|
|
2012-11-16 04:09:22 +00:00
|
|
|
user = toDavUser u
|
|
|
|
pass = toDavPass p
|
2012-11-15 00:25:00 +00:00
|
|
|
testurl = davUrl baseurl "git-annex-test"
|
2012-11-16 17:32:18 +00:00
|
|
|
testDav _ Nothing = error "Need to configure webdav username and password."
|
2012-11-15 00:25:00 +00:00
|
|
|
|
|
|
|
{- Content-Type to use for files uploaded to WebDAV. -}
|
|
|
|
contentType :: Maybe B8.ByteString
|
2012-11-15 17:46:16 +00:00
|
|
|
contentType = Just $ B8.fromString "application/octet-stream"
|
2012-11-15 00:25:00 +00:00
|
|
|
|
|
|
|
getCreds :: RemoteConfig -> UUID -> Annex (Maybe CredPair)
|
2012-11-28 17:31:49 +00:00
|
|
|
getCreds c u = getRemoteCredPairFor "webdav" c (davCreds u)
|
2012-11-15 00:25:00 +00:00
|
|
|
|
|
|
|
davCreds :: UUID -> CredPairStorage
|
|
|
|
davCreds u = CredPairStorage
|
|
|
|
{ credPairFile = fromUUID u
|
|
|
|
, credPairEnvironment = ("WEBDAV_USERNAME", "WEBDAV_PASSWORD")
|
|
|
|
, credPairRemoteKey = Just "davcreds"
|
|
|
|
}
|
2012-11-17 19:30:11 +00:00
|
|
|
|
|
|
|
setCredsEnv :: (String, String) -> IO ()
|
|
|
|
setCredsEnv creds = setEnvCredPair creds $ davCreds undefined
|