2014-02-25 02:00:25 +00:00
|
|
|
{- Url downloading, with git-annex user agent and configured http
|
2018-06-17 17:05:30 +00:00
|
|
|
- headers, security restrictions, etc.
|
2013-09-28 18:35:21 +00:00
|
|
|
-
|
2018-04-04 19:00:51 +00:00
|
|
|
- Copyright 2013-2018 Joey Hess <id@joeyh.name>
|
2013-09-28 18:35:21 +00:00
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
module Annex.Url (
|
|
|
|
module U,
|
2014-02-25 02:00:25 +00:00
|
|
|
withUrlOptions,
|
2018-04-04 19:15:12 +00:00
|
|
|
getUrlOptions,
|
2013-09-28 18:35:21 +00:00
|
|
|
getUserAgent,
|
2018-06-17 18:46:22 +00:00
|
|
|
httpAddressesUnlimited,
|
2013-09-28 18:35:21 +00:00
|
|
|
) where
|
|
|
|
|
2016-01-20 20:36:33 +00:00
|
|
|
import Annex.Common
|
2013-09-28 18:35:21 +00:00
|
|
|
import qualified Annex
|
|
|
|
import Utility.Url as U
|
2018-06-17 17:05:30 +00:00
|
|
|
import Utility.IPAddress
|
|
|
|
import Utility.HttpManagerRestricted
|
2017-12-14 16:46:57 +00:00
|
|
|
import qualified BuildInfo
|
2013-09-28 18:35:21 +00:00
|
|
|
|
2018-06-17 17:05:30 +00:00
|
|
|
import Network.Socket
|
|
|
|
|
2013-09-28 18:35:21 +00:00
|
|
|
defaultUserAgent :: U.UserAgent
|
2017-12-14 16:46:57 +00:00
|
|
|
defaultUserAgent = "git-annex/" ++ BuildInfo.packageversion
|
2013-09-28 18:35:21 +00:00
|
|
|
|
2018-07-16 16:06:06 +00:00
|
|
|
getUserAgent :: Annex U.UserAgent
|
2013-09-28 18:35:21 +00:00
|
|
|
getUserAgent = Annex.getState $
|
2018-07-16 16:06:06 +00:00
|
|
|
fromMaybe defaultUserAgent . Annex.useragent
|
2013-09-28 18:35:21 +00:00
|
|
|
|
2018-04-04 19:15:12 +00:00
|
|
|
getUrlOptions :: Annex U.UrlOptions
|
|
|
|
getUrlOptions = Annex.getState Annex.urloptions >>= \case
|
|
|
|
Just uo -> return uo
|
2018-04-04 19:00:51 +00:00
|
|
|
Nothing -> do
|
|
|
|
uo <- mk
|
|
|
|
Annex.changeState $ \s -> s
|
|
|
|
{ Annex.urloptions = Just uo }
|
2018-04-04 19:15:12 +00:00
|
|
|
return uo
|
2014-02-25 02:00:25 +00:00
|
|
|
where
|
2018-06-17 17:05:30 +00:00
|
|
|
mk = do
|
|
|
|
(urldownloader, manager) <- checkallowedaddr
|
|
|
|
mkUrlOptions
|
2018-07-16 16:06:06 +00:00
|
|
|
<$> (Just <$> getUserAgent)
|
2018-06-17 17:05:30 +00:00
|
|
|
<*> headers
|
|
|
|
<*> pure urldownloader
|
|
|
|
<*> pure manager
|
|
|
|
<*> (annexAllowedUrlSchemes <$> Annex.getGitConfig)
|
|
|
|
|
2017-12-05 19:00:50 +00:00
|
|
|
headers = annexHttpHeadersCommand <$> Annex.getGitConfig >>= \case
|
|
|
|
Just cmd -> lines <$> liftIO (readProcess "sh" ["-c", cmd])
|
|
|
|
Nothing -> annexHttpHeaders <$> Annex.getGitConfig
|
2018-06-17 17:05:30 +00:00
|
|
|
|
|
|
|
checkallowedaddr = words . annexAllowedHttpAddresses <$> Annex.getGitConfig >>= \case
|
|
|
|
["all"] -> do
|
|
|
|
-- Only allow curl when all are allowed,
|
|
|
|
-- as its interface does not allow preventing
|
|
|
|
-- it from accessing specific IP addresses.
|
|
|
|
curlopts <- map Param . annexWebOptions <$> Annex.getGitConfig
|
|
|
|
let urldownloader = if null curlopts
|
2018-10-04 17:43:29 +00:00
|
|
|
then U.DownloadWithConduit
|
|
|
|
else U.DownloadWithCurl curlopts
|
2018-06-17 17:05:30 +00:00
|
|
|
manager <- liftIO $ U.newManager U.managerSettings
|
|
|
|
return (urldownloader, manager)
|
|
|
|
allowedaddrs -> do
|
|
|
|
addrmatcher <- liftIO $
|
|
|
|
(\l v -> any (\f -> f v) l) . catMaybes
|
|
|
|
<$> mapM makeAddressMatcher allowedaddrs
|
|
|
|
-- Default to not allowing access to loopback
|
|
|
|
-- and private IP addresses to avoid data
|
|
|
|
-- leakage.
|
|
|
|
let isallowed addr
|
|
|
|
| addrmatcher addr = True
|
|
|
|
| isLoopbackAddress addr = False
|
|
|
|
| isPrivateAddress addr = False
|
|
|
|
| otherwise = True
|
2018-06-19 18:21:41 +00:00
|
|
|
let connectionrestricted = addrConnectionRestricted
|
|
|
|
("Configuration of annex.security.allowed-http-addresses does not allow accessing address " ++)
|
2018-06-17 17:05:30 +00:00
|
|
|
let r = Restriction
|
|
|
|
{ addressRestriction = \addr ->
|
|
|
|
if isallowed (addrAddress addr)
|
|
|
|
then Nothing
|
2018-06-19 18:21:41 +00:00
|
|
|
else Just (connectionrestricted addr)
|
2018-06-17 17:05:30 +00:00
|
|
|
}
|
2018-06-18 17:32:20 +00:00
|
|
|
(settings, pr) <- liftIO $
|
2018-06-17 17:05:30 +00:00
|
|
|
restrictManagerSettings r U.managerSettings
|
2018-06-18 17:32:20 +00:00
|
|
|
case pr of
|
|
|
|
Nothing -> return ()
|
|
|
|
Just ProxyRestricted -> toplevelWarning True
|
|
|
|
"http proxy settings not used due to annex.security.allowed-http-addresses configuration"
|
|
|
|
manager <- liftIO $ U.newManager settings
|
2018-06-17 17:05:30 +00:00
|
|
|
return (U.DownloadWithConduit, manager)
|
2018-04-04 19:15:12 +00:00
|
|
|
|
2018-06-17 18:46:22 +00:00
|
|
|
httpAddressesUnlimited :: Annex Bool
|
|
|
|
httpAddressesUnlimited =
|
|
|
|
("all" == ) . annexAllowedHttpAddresses <$> Annex.getGitConfig
|
|
|
|
|
2018-04-04 19:15:12 +00:00
|
|
|
withUrlOptions :: (U.UrlOptions -> Annex a) -> Annex a
|
|
|
|
withUrlOptions a = a =<< getUrlOptions
|