2021-02-09 21:03:27 +00:00
|
|
|
{- verification
|
|
|
|
-
|
|
|
|
- Copyright 2010-2021 Joey Hess <id@joeyh.name>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
2021-08-12 18:36:56 +00:00
|
|
|
{-# LANGUAGE CPP #-}
|
|
|
|
|
2021-07-27 18:07:23 +00:00
|
|
|
module Annex.Verify (
|
|
|
|
VerifyConfig(..),
|
|
|
|
shouldVerify,
|
2021-07-29 17:36:19 +00:00
|
|
|
verifyKeyContentPostRetrieval,
|
2021-07-27 18:07:23 +00:00
|
|
|
verifyKeyContent,
|
|
|
|
Verification(..),
|
|
|
|
unVerified,
|
|
|
|
warnUnverifiableInsecure,
|
|
|
|
isVerifiable,
|
|
|
|
startVerifyKeyContentIncrementally,
|
|
|
|
IncrementalVerifier(..),
|
2021-08-12 18:36:56 +00:00
|
|
|
tailVerify,
|
2021-07-27 18:07:23 +00:00
|
|
|
) where
|
2021-02-09 21:03:27 +00:00
|
|
|
|
|
|
|
import Annex.Common
|
|
|
|
import qualified Annex
|
|
|
|
import qualified Types.Remote
|
2021-07-27 18:07:23 +00:00
|
|
|
import qualified Types.Backend
|
|
|
|
import Types.Backend (IncrementalVerifier(..))
|
|
|
|
import qualified Backend
|
|
|
|
import Types.Remote (unVerified, Verification(..), RetrievalSecurityPolicy(..))
|
|
|
|
import Annex.WorkerPool
|
|
|
|
import Types.WorkerPool
|
|
|
|
import Types.Key
|
2021-02-09 21:03:27 +00:00
|
|
|
|
2021-08-13 20:36:33 +00:00
|
|
|
import Control.Concurrent.STM
|
2021-08-12 18:36:56 +00:00
|
|
|
#if WITH_INOTIFY
|
|
|
|
import qualified System.INotify as INotify
|
|
|
|
import qualified Data.ByteString as S
|
2021-08-13 17:39:02 +00:00
|
|
|
import qualified System.FilePath.ByteString as P
|
2021-08-13 16:32:01 +00:00
|
|
|
import Data.Time.Clock.POSIX
|
2021-08-12 18:36:56 +00:00
|
|
|
#endif
|
|
|
|
|
2021-02-09 21:03:27 +00:00
|
|
|
data VerifyConfig = AlwaysVerify | NoVerify | RemoteVerify Remote | DefaultVerify
|
|
|
|
|
|
|
|
shouldVerify :: VerifyConfig -> Annex Bool
|
|
|
|
shouldVerify AlwaysVerify = return True
|
|
|
|
shouldVerify NoVerify = return False
|
|
|
|
shouldVerify DefaultVerify = annexVerify <$> Annex.getGitConfig
|
|
|
|
shouldVerify (RemoteVerify r) =
|
|
|
|
(shouldVerify DefaultVerify
|
|
|
|
<&&> pure (remoteAnnexVerify (Types.Remote.gitconfig r)))
|
|
|
|
-- Export remotes are not key/value stores, so always verify
|
|
|
|
-- content from them even when verification is disabled.
|
|
|
|
<||> Types.Remote.isExportSupported r
|
2021-07-27 18:07:23 +00:00
|
|
|
|
|
|
|
{- Verifies that a file is the expected content of a key.
|
|
|
|
-
|
|
|
|
- Configuration can prevent verification, for either a
|
|
|
|
- particular remote or always, unless the RetrievalSecurityPolicy
|
|
|
|
- requires verification.
|
|
|
|
-
|
|
|
|
- Most keys have a known size, and if so, the file size is checked.
|
|
|
|
-
|
|
|
|
- When the key's backend allows verifying the content (via checksum),
|
|
|
|
- it is checked.
|
|
|
|
-
|
|
|
|
- If the RetrievalSecurityPolicy requires verification and the key's
|
|
|
|
- backend doesn't support it, the verification will fail.
|
|
|
|
-}
|
2021-07-29 17:36:19 +00:00
|
|
|
verifyKeyContentPostRetrieval :: RetrievalSecurityPolicy -> VerifyConfig -> Verification -> Key -> RawFilePath -> Annex Bool
|
|
|
|
verifyKeyContentPostRetrieval rsp v verification k f = case (rsp, verification) of
|
2021-07-27 18:07:23 +00:00
|
|
|
(_, Verified) -> return True
|
|
|
|
(RetrievalVerifiableKeysSecure, _) -> ifM (isVerifiable k)
|
|
|
|
( verify
|
|
|
|
, ifM (annexAllowUnverifiedDownloads <$> Annex.getGitConfig)
|
|
|
|
( verify
|
|
|
|
, warnUnverifiableInsecure k >> return False
|
|
|
|
)
|
|
|
|
)
|
|
|
|
(_, UnVerified) -> ifM (shouldVerify v)
|
|
|
|
( verify
|
|
|
|
, return True
|
|
|
|
)
|
|
|
|
(_, MustVerify) -> verify
|
|
|
|
where
|
2021-07-29 17:36:19 +00:00
|
|
|
verify = enteringStage VerifyStage $ verifyKeyContent k f
|
|
|
|
|
|
|
|
verifyKeyContent :: Key -> RawFilePath -> Annex Bool
|
|
|
|
verifyKeyContent k f = verifysize <&&> verifycontent
|
|
|
|
where
|
2021-07-27 18:07:23 +00:00
|
|
|
verifysize = case fromKey keySize k of
|
|
|
|
Nothing -> return True
|
|
|
|
Just size -> do
|
|
|
|
size' <- liftIO $ catchDefaultIO 0 $ getFileSize f
|
|
|
|
return (size' == size)
|
|
|
|
verifycontent = Backend.maybeLookupBackendVariety (fromKey keyVariety k) >>= \case
|
|
|
|
Nothing -> return True
|
|
|
|
Just b -> case Types.Backend.verifyKeyContent b of
|
|
|
|
Nothing -> return True
|
|
|
|
Just verifier -> verifier k f
|
|
|
|
|
|
|
|
warnUnverifiableInsecure :: Key -> Annex ()
|
|
|
|
warnUnverifiableInsecure k = warning $ unwords
|
|
|
|
[ "Getting " ++ kv ++ " keys with this remote is not secure;"
|
|
|
|
, "the content cannot be verified to be correct."
|
|
|
|
, "(Use annex.security.allow-unverified-downloads to bypass"
|
|
|
|
, "this safety check.)"
|
|
|
|
]
|
|
|
|
where
|
|
|
|
kv = decodeBS (formatKeyVariety (fromKey keyVariety k))
|
|
|
|
|
|
|
|
isVerifiable :: Key -> Annex Bool
|
|
|
|
isVerifiable k = maybe False (isJust . Types.Backend.verifyKeyContent)
|
|
|
|
<$> Backend.maybeLookupBackendVariety (fromKey keyVariety k)
|
|
|
|
|
|
|
|
startVerifyKeyContentIncrementally :: VerifyConfig -> Key -> Annex (Maybe IncrementalVerifier)
|
|
|
|
startVerifyKeyContentIncrementally verifyconfig k =
|
|
|
|
ifM (shouldVerify verifyconfig)
|
|
|
|
( Backend.maybeLookupBackendVariety (fromKey keyVariety k) >>= \case
|
|
|
|
Just b -> case Types.Backend.verifyKeyContentIncrementally b of
|
|
|
|
Just v -> Just <$> v k
|
|
|
|
Nothing -> return Nothing
|
|
|
|
Nothing -> return Nothing
|
|
|
|
, return Nothing
|
|
|
|
)
|
2021-08-12 18:36:56 +00:00
|
|
|
|
|
|
|
-- | Reads the file as it grows, and feeds it to the incremental verifier.
|
|
|
|
--
|
|
|
|
-- The TMVar must start out empty, and be filled once whatever is
|
|
|
|
-- writing to the file finishes.
|
|
|
|
--
|
|
|
|
-- The file does not need to exist yet when this is called. It will wait
|
|
|
|
-- for the file to appear before opening it and starting verification.
|
|
|
|
--
|
2021-08-13 17:39:02 +00:00
|
|
|
-- This is not supported for all OSs, and on OS's where it is not
|
|
|
|
-- supported, verification will fail.
|
2021-08-13 16:32:01 +00:00
|
|
|
--
|
2021-08-12 18:36:56 +00:00
|
|
|
-- Note that there are situations where the file may fail to verify despite
|
|
|
|
-- having the correct content. For example, when the file is written out
|
|
|
|
-- of order, or gets replaced part way through. To deal with such cases,
|
2021-08-13 17:39:02 +00:00
|
|
|
-- when verification fails, it should not be treated as if the file's
|
2021-08-12 18:36:56 +00:00
|
|
|
-- content is known to be incorrect, but instead as an indication that the
|
|
|
|
-- file should be verified again, once it's done being written to.
|
|
|
|
--
|
2021-08-13 16:32:01 +00:00
|
|
|
-- (It is also possible, in theory, for a file to verify despite having
|
|
|
|
-- incorrect content. For that to happen, the file would need to have
|
|
|
|
-- the right content when this checks it, but then the content gets
|
|
|
|
-- changed later by whatever is writing to the file.)
|
2021-08-12 18:36:56 +00:00
|
|
|
--
|
|
|
|
-- This should be fairly efficient, reading from the disk cache,
|
|
|
|
-- as long as the writer does not get very far ahead of it. However,
|
|
|
|
-- there are situations where it would be much less expensive to verify
|
|
|
|
-- chunks as they are being written. For example, when resuming with
|
|
|
|
-- a lot of content in the file, all that content needs to be read,
|
|
|
|
-- and if the disk is slow, the reader may never catch up to the writer,
|
|
|
|
-- and the disk cache may never speed up reads. So this should only be
|
|
|
|
-- used when there's not a better way to incrementally verify.
|
2021-08-13 16:32:01 +00:00
|
|
|
--
|
|
|
|
-- If the writer gets far ahead, this can still need to do a significant
|
|
|
|
-- amount off work once the writer is finished. That could lead to a long
|
|
|
|
-- pause with no indication to the user about what is being done. To deal
|
|
|
|
-- with this problem, it will do at most half a second of work after the
|
|
|
|
-- writer has finished. If there is more work still to do, it returns an IO
|
|
|
|
-- action that will complete the work. This way, the caller can display
|
|
|
|
-- something appropriate while that is running.
|
2021-08-13 17:39:02 +00:00
|
|
|
tailVerify :: IncrementalVerifier -> RawFilePath -> TMVar () -> IO (Maybe (IO ()))
|
2021-08-12 18:36:56 +00:00
|
|
|
#if WITH_INOTIFY
|
|
|
|
tailVerify iv f finished =
|
2021-08-13 20:16:46 +00:00
|
|
|
tryNonAsync go >>= \case
|
2021-08-13 17:39:02 +00:00
|
|
|
Right r -> return r
|
|
|
|
Left _ -> do
|
|
|
|
failIncremental iv
|
|
|
|
return Nothing
|
2021-08-12 18:36:56 +00:00
|
|
|
where
|
2021-08-13 17:39:02 +00:00
|
|
|
f' = fromRawFilePath f
|
2021-08-13 20:16:46 +00:00
|
|
|
waitforfiletoexist i = tryNonAsync (openBinaryFile f' ReadMode) >>= \case
|
2021-08-12 18:36:56 +00:00
|
|
|
Right h -> return (Just h)
|
|
|
|
Left _ -> do
|
|
|
|
hv <- newEmptyTMVarIO
|
|
|
|
wd <- inotifycreate i $
|
2021-08-13 20:16:46 +00:00
|
|
|
tryNonAsync (openBinaryFile f' ReadMode) >>= \case
|
2021-08-12 18:36:56 +00:00
|
|
|
Right h ->
|
|
|
|
unlessM (atomically $ tryPutTMVar hv h) $
|
|
|
|
hClose h
|
|
|
|
Left _ -> return ()
|
|
|
|
-- Wait for the file to appear, or for a signal
|
|
|
|
-- that the file is finished being written.
|
|
|
|
--
|
|
|
|
-- The TMVar is left full to prevent the file
|
|
|
|
-- being opened again if the inotify event
|
|
|
|
-- fires more than once.
|
|
|
|
v <- atomically $
|
|
|
|
(Just <$> readTMVar hv)
|
|
|
|
`orElse`
|
|
|
|
((const Nothing) <$> readTMVar finished)
|
2021-08-13 19:35:18 +00:00
|
|
|
void $ tryNonAsync $ INotify.removeWatch wd
|
2021-08-12 18:36:56 +00:00
|
|
|
return v
|
|
|
|
|
2021-08-13 17:39:02 +00:00
|
|
|
inotifycreate i cont = INotify.addWatch i evs (P.takeDirectory f) $ \case
|
2021-08-12 18:36:56 +00:00
|
|
|
-- Ignore changes to other files in the directory.
|
|
|
|
INotify.Created { INotify.filePath = fn }
|
2021-08-13 18:51:15 +00:00
|
|
|
| fn /= basef -> noop
|
2021-08-12 18:36:56 +00:00
|
|
|
INotify.MovedIn { INotify.filePath = fn }
|
2021-08-13 18:51:15 +00:00
|
|
|
| fn /= basef -> noop
|
2021-08-12 18:36:56 +00:00
|
|
|
INotify.Opened { INotify.maybeFilePath = fn }
|
2021-08-13 18:51:15 +00:00
|
|
|
| fn /= Just basef -> noop
|
2021-08-12 18:36:56 +00:00
|
|
|
INotify.Modified { INotify.maybeFilePath = fn }
|
2021-08-13 18:51:15 +00:00
|
|
|
| fn /= Just basef -> noop
|
2021-08-12 18:36:56 +00:00
|
|
|
_ -> cont
|
|
|
|
where
|
|
|
|
evs =
|
|
|
|
[ INotify.Create
|
|
|
|
, INotify.MoveIn
|
|
|
|
, INotify.Move
|
|
|
|
, INotify.Open
|
|
|
|
, INotify.Modify
|
|
|
|
]
|
2021-08-13 18:51:15 +00:00
|
|
|
basef = P.takeFileName f
|
2021-08-12 18:36:56 +00:00
|
|
|
|
2021-08-13 17:39:02 +00:00
|
|
|
go = INotify.withINotify $ \i -> do
|
|
|
|
h <- waitforfiletoexist i
|
2021-08-13 20:16:46 +00:00
|
|
|
tryNonAsync (go' i h) >>= \case
|
2021-08-13 17:39:02 +00:00
|
|
|
Right r -> return r
|
|
|
|
Left _ -> do
|
|
|
|
maybe noop hClose h
|
|
|
|
failIncremental iv
|
|
|
|
return Nothing
|
|
|
|
|
|
|
|
go' i (Just h) = do
|
2021-08-12 18:36:56 +00:00
|
|
|
modified <- newEmptyTMVarIO
|
2021-08-13 17:39:02 +00:00
|
|
|
wd <- INotify.addWatch i [INotify.Modify] f $ \_event ->
|
2021-08-12 18:36:56 +00:00
|
|
|
atomically $ void $ tryPutTMVar modified ()
|
|
|
|
r <- follow h modified
|
2021-08-13 19:35:18 +00:00
|
|
|
void $ tryNonAsync $ INotify.removeWatch wd
|
2021-08-12 18:36:56 +00:00
|
|
|
return r
|
2021-08-13 19:35:18 +00:00
|
|
|
|
2021-08-12 18:36:56 +00:00
|
|
|
-- File never showed up, but we've been told it's done being
|
|
|
|
-- written to.
|
2021-08-13 17:39:02 +00:00
|
|
|
go' _ Nothing = do
|
|
|
|
failIncremental iv
|
|
|
|
return Nothing
|
2021-08-12 18:36:56 +00:00
|
|
|
|
|
|
|
follow h modified = do
|
|
|
|
b <- S.hGetNonBlocking h chunk
|
|
|
|
if S.null b
|
|
|
|
then do
|
|
|
|
-- We've caught up to the writer.
|
|
|
|
-- Wait for the file to get modified again,
|
|
|
|
-- or until we're told it is done being
|
|
|
|
-- written.
|
|
|
|
cont <- atomically $
|
|
|
|
((const (follow h modified))
|
|
|
|
<$> takeTMVar modified)
|
|
|
|
`orElse`
|
2021-08-13 16:32:01 +00:00
|
|
|
((const (finish h =<< getPOSIXTime))
|
|
|
|
<$> takeTMVar finished)
|
2021-08-12 18:36:56 +00:00
|
|
|
cont
|
|
|
|
else do
|
|
|
|
updateIncremental iv b
|
2021-08-13 16:32:01 +00:00
|
|
|
atomically (tryTakeTMVar finished) >>= \case
|
|
|
|
Nothing -> follow h modified
|
|
|
|
Just () -> finish h =<< getPOSIXTime
|
2021-08-12 18:36:56 +00:00
|
|
|
|
|
|
|
-- We've been told the file is done being written to, but we
|
2021-08-13 16:32:01 +00:00
|
|
|
-- may not have reached the end of it yet.
|
|
|
|
finish h starttime = do
|
2021-08-12 18:36:56 +00:00
|
|
|
b <- S.hGet h chunk
|
|
|
|
if S.null b
|
2021-08-13 16:32:01 +00:00
|
|
|
then do
|
|
|
|
hClose h
|
2021-08-13 17:39:02 +00:00
|
|
|
return Nothing
|
2021-08-12 18:36:56 +00:00
|
|
|
else do
|
|
|
|
updateIncremental iv b
|
2021-08-13 16:32:01 +00:00
|
|
|
now <- getPOSIXTime
|
|
|
|
if now - starttime > 0.5
|
2021-08-13 17:39:02 +00:00
|
|
|
then return $ Just $
|
2021-08-13 20:16:46 +00:00
|
|
|
tryNonAsync (deferredfinish h) >>= \case
|
2021-08-13 17:39:02 +00:00
|
|
|
Right () -> noop
|
|
|
|
Left _ -> failIncremental iv
|
2021-08-13 16:32:01 +00:00
|
|
|
else finish h starttime
|
|
|
|
|
|
|
|
deferredfinish h = do
|
|
|
|
b <- S.hGet h chunk
|
|
|
|
if S.null b
|
2021-08-13 17:39:02 +00:00
|
|
|
then hClose h
|
2021-08-13 16:32:01 +00:00
|
|
|
else do
|
|
|
|
updateIncremental iv b
|
|
|
|
deferredfinish h
|
2021-08-12 18:36:56 +00:00
|
|
|
|
|
|
|
chunk = 65536
|
|
|
|
#else
|
2021-08-13 17:39:02 +00:00
|
|
|
tailVerify iv _ _ = do
|
|
|
|
failIncremental iv
|
|
|
|
return Nothing
|
2021-08-12 18:36:56 +00:00
|
|
|
#endif
|