2016-11-14 17:26:34 +00:00
|
|
|
{- tor interface
|
|
|
|
-
|
|
|
|
- Copyright 2016 Joey Hess <id@joeyh.name>
|
|
|
|
-
|
2019-03-13 19:48:14 +00:00
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
2016-11-14 17:26:34 +00:00
|
|
|
-}
|
|
|
|
|
2019-07-30 16:49:37 +00:00
|
|
|
{-# LANGUAGE CPP #-}
|
|
|
|
|
2019-11-23 15:07:22 +00:00
|
|
|
module Utility.Tor (
|
|
|
|
OnionPort,
|
|
|
|
OnionAddress(..),
|
|
|
|
OnionSocket,
|
|
|
|
UniqueIdent,
|
|
|
|
AppName,
|
|
|
|
connectHiddenService,
|
|
|
|
addHiddenService,
|
|
|
|
getHiddenServiceSocketFile,
|
|
|
|
torIsInstalled,
|
|
|
|
) where
|
2016-11-14 17:26:34 +00:00
|
|
|
|
|
|
|
import Common
|
|
|
|
import Utility.ThreadScheduler
|
2016-11-29 17:02:19 +00:00
|
|
|
import Utility.FileMode
|
|
|
|
|
2016-11-14 20:35:45 +00:00
|
|
|
import System.PosixCompat.Types
|
2016-11-14 17:26:34 +00:00
|
|
|
import Data.Char
|
2016-11-21 21:27:38 +00:00
|
|
|
import Network.Socket
|
|
|
|
import Network.Socks5
|
|
|
|
import qualified Data.ByteString.UTF8 as BU8
|
|
|
|
import qualified System.Random as R
|
2016-11-14 17:26:34 +00:00
|
|
|
|
|
|
|
type OnionPort = Int
|
2016-11-21 23:24:55 +00:00
|
|
|
|
2016-11-21 21:27:38 +00:00
|
|
|
newtype OnionAddress = OnionAddress String
|
2016-11-29 21:30:27 +00:00
|
|
|
deriving (Show, Eq)
|
2016-11-21 23:24:55 +00:00
|
|
|
|
2016-11-14 20:35:45 +00:00
|
|
|
type OnionSocket = FilePath
|
2016-11-21 23:24:55 +00:00
|
|
|
|
2016-12-20 20:01:10 +00:00
|
|
|
-- | A unique identifier for a hidden service.
|
2016-11-20 19:45:01 +00:00
|
|
|
type UniqueIdent = String
|
2016-11-14 17:26:34 +00:00
|
|
|
|
2016-12-20 20:01:10 +00:00
|
|
|
-- | Name of application that is providing a hidden service.
|
|
|
|
type AppName = String
|
|
|
|
|
2016-11-21 21:27:38 +00:00
|
|
|
connectHiddenService :: OnionAddress -> OnionPort -> IO Socket
|
|
|
|
connectHiddenService (OnionAddress address) port = do
|
|
|
|
(s, _) <- socksConnect torsockconf socksaddr
|
|
|
|
return s
|
|
|
|
where
|
|
|
|
torsocksport = 9050
|
2019-07-30 16:49:37 +00:00
|
|
|
#if MIN_VERSION_socks(0,6,0)
|
|
|
|
torsockconf = defaultSocksConf $ SockAddrInet torsocksport $
|
|
|
|
tupleToHostAddress (127,0,0,1)
|
|
|
|
#else
|
2016-11-21 21:27:38 +00:00
|
|
|
torsockconf = defaultSocksConf "127.0.0.1" torsocksport
|
2019-07-30 16:49:37 +00:00
|
|
|
#endif
|
2016-11-21 21:27:38 +00:00
|
|
|
socksdomain = SocksAddrDomainName (BU8.fromString address)
|
|
|
|
socksaddr = SocksAddress socksdomain (fromIntegral port)
|
|
|
|
|
2016-11-14 20:35:45 +00:00
|
|
|
-- | Adds a hidden service connecting to localhost, using some kind
|
|
|
|
-- of unique identifier.
|
|
|
|
--
|
2016-11-14 17:26:34 +00:00
|
|
|
-- This will only work if run as root, and tor has to already be running.
|
|
|
|
--
|
2016-11-21 21:27:38 +00:00
|
|
|
-- Picks a random high port number for the hidden service that is not
|
|
|
|
-- used by any other hidden service. Returns the hidden service's
|
2016-11-14 20:35:45 +00:00
|
|
|
-- onion address, port, and the unix socket file to use.
|
|
|
|
--
|
|
|
|
-- If there is already a hidden service for the specified unique
|
|
|
|
-- identifier, returns its information without making any changes.
|
2016-12-20 20:01:10 +00:00
|
|
|
addHiddenService :: AppName -> UserID -> UniqueIdent -> IO (OnionAddress, OnionPort)
|
|
|
|
addHiddenService appname uid ident = do
|
|
|
|
prepHiddenServiceSocketDir appname uid ident
|
2016-12-28 19:12:31 +00:00
|
|
|
ls <- lines <$> (readFile =<< findTorrc)
|
2016-11-14 20:35:45 +00:00
|
|
|
let portssocks = mapMaybe (parseportsock . separate isSpace) ls
|
|
|
|
case filter (\(_, s) -> s == sockfile) portssocks of
|
|
|
|
((p, _s):_) -> waithiddenservice 1 p
|
|
|
|
_ -> do
|
2016-11-29 21:30:27 +00:00
|
|
|
highports <- R.getStdRandom mkhighports
|
2016-11-14 20:35:45 +00:00
|
|
|
let newport = Prelude.head $
|
2016-11-21 21:27:38 +00:00
|
|
|
filter (`notElem` map fst portssocks) highports
|
2016-12-28 19:12:31 +00:00
|
|
|
torrc <- findTorrc
|
2016-11-14 17:26:34 +00:00
|
|
|
writeFile torrc $ unlines $
|
2016-11-14 20:35:45 +00:00
|
|
|
ls ++
|
2016-11-14 17:26:34 +00:00
|
|
|
[ ""
|
2016-12-21 18:31:27 +00:00
|
|
|
, "HiddenServiceDir " ++ hiddenServiceDir appname uid ident
|
2016-11-14 17:26:34 +00:00
|
|
|
, "HiddenServicePort " ++ show newport ++
|
2016-11-14 20:35:45 +00:00
|
|
|
" unix:" ++ sockfile
|
2016-11-14 17:26:34 +00:00
|
|
|
]
|
|
|
|
-- Reload tor, so it will see the new hidden
|
|
|
|
-- service and generate the hostname file for it.
|
|
|
|
reloaded <- anyM (uncurry boolSystem)
|
|
|
|
[ ("systemctl", [Param "reload", Param "tor"])
|
2016-12-18 21:13:06 +00:00
|
|
|
, ("service", [Param "tor", Param "reload"])
|
2016-11-14 17:26:34 +00:00
|
|
|
]
|
|
|
|
unless reloaded $
|
2016-11-29 21:30:27 +00:00
|
|
|
giveup "failed to reload tor, perhaps the tor service is not running"
|
2016-11-14 20:35:45 +00:00
|
|
|
waithiddenservice 120 newport
|
2016-11-14 17:26:34 +00:00
|
|
|
where
|
2016-11-14 20:35:45 +00:00
|
|
|
parseportsock ("HiddenServicePort", l) = do
|
|
|
|
p <- readish $ takeWhile (not . isSpace) l
|
|
|
|
return (p, drop 1 (dropWhile (/= ':') l))
|
|
|
|
parseportsock _ = Nothing
|
|
|
|
|
2016-12-20 20:01:10 +00:00
|
|
|
sockfile = hiddenServiceSocketFile appname uid ident
|
2016-11-14 20:35:45 +00:00
|
|
|
|
2016-11-21 21:27:38 +00:00
|
|
|
-- An infinite random list of high ports.
|
2016-11-29 21:30:27 +00:00
|
|
|
mkhighports g =
|
2016-11-21 21:27:38 +00:00
|
|
|
let (g1, g2) = R.split g
|
|
|
|
in (R.randomRs (1025, 65534) g1, g2)
|
|
|
|
|
|
|
|
waithiddenservice :: Int -> OnionPort -> IO (OnionAddress, OnionPort)
|
2016-11-29 21:30:27 +00:00
|
|
|
waithiddenservice 0 _ = giveup "tor failed to create hidden service, perhaps the tor service is not running"
|
2016-11-14 20:35:45 +00:00
|
|
|
waithiddenservice n p = do
|
2016-12-21 18:31:27 +00:00
|
|
|
v <- tryIO $ readFile $ hiddenServiceHostnameFile appname uid ident
|
2016-11-14 17:26:34 +00:00
|
|
|
case v of
|
2016-11-29 17:02:19 +00:00
|
|
|
Right s | ".onion\n" `isSuffixOf` s ->
|
2016-11-21 21:27:38 +00:00
|
|
|
return (OnionAddress (takeWhile (/= '\n') s), p)
|
2016-11-14 17:26:34 +00:00
|
|
|
_ -> do
|
|
|
|
threadDelaySeconds (Seconds 1)
|
2016-11-14 20:35:45 +00:00
|
|
|
waithiddenservice (n-1) p
|
2016-11-14 17:26:34 +00:00
|
|
|
|
2016-11-29 17:02:19 +00:00
|
|
|
-- | A hidden service directory to use.
|
|
|
|
--
|
2016-12-21 18:31:27 +00:00
|
|
|
-- Has to be inside the torLibDir so tor can create it.
|
|
|
|
--
|
|
|
|
-- Has to end with "uid_ident" so getHiddenServiceSocketFile can find it.
|
|
|
|
hiddenServiceDir :: AppName -> UserID -> UniqueIdent -> FilePath
|
|
|
|
hiddenServiceDir appname uid ident = torLibDir </> appname ++ "_" ++ show uid ++ "_" ++ ident
|
2016-11-29 17:02:19 +00:00
|
|
|
|
2016-12-21 18:31:27 +00:00
|
|
|
hiddenServiceHostnameFile :: AppName -> UserID -> UniqueIdent -> FilePath
|
|
|
|
hiddenServiceHostnameFile appname uid ident = hiddenServiceDir appname uid ident </> "hostname"
|
2016-11-29 17:02:19 +00:00
|
|
|
|
|
|
|
-- | Location of the socket for a hidden service.
|
|
|
|
--
|
|
|
|
-- This has to be a location that tor can read from, and that the user
|
2016-12-20 20:01:10 +00:00
|
|
|
-- can write to. Since torLibDir is locked down, it can't go in there.
|
2016-11-29 17:02:19 +00:00
|
|
|
--
|
|
|
|
-- Note that some unix systems limit socket paths to 92 bytes long.
|
|
|
|
-- That should not be a problem if the UniqueIdent is around the length of
|
2016-12-20 20:01:10 +00:00
|
|
|
-- a UUID, and the AppName is short.
|
|
|
|
hiddenServiceSocketFile :: AppName -> UserID -> UniqueIdent -> FilePath
|
|
|
|
hiddenServiceSocketFile appname uid ident = varLibDir </> appname </> show uid ++ "_" ++ ident </> "s"
|
|
|
|
|
|
|
|
-- | Parse torrc, to get the socket file used for a hidden service with
|
|
|
|
-- the specified UniqueIdent.
|
2016-12-21 18:31:27 +00:00
|
|
|
getHiddenServiceSocketFile :: AppName -> UserID -> UniqueIdent -> IO (Maybe FilePath)
|
|
|
|
getHiddenServiceSocketFile _appname uid ident =
|
2016-12-28 19:12:31 +00:00
|
|
|
parse . map words . lines <$> catchDefaultIO "" (readFile =<< findTorrc)
|
2016-12-20 20:01:10 +00:00
|
|
|
where
|
|
|
|
parse [] = Nothing
|
|
|
|
parse (("HiddenServiceDir":hsdir:[]):("HiddenServicePort":_hsport:hsaddr:[]):rest)
|
2016-12-21 18:31:27 +00:00
|
|
|
| "unix:" `isPrefixOf` hsaddr && hasident hsdir =
|
2016-12-20 20:01:10 +00:00
|
|
|
Just (drop (length "unix:") hsaddr)
|
|
|
|
| otherwise = parse rest
|
|
|
|
parse (_:rest) = parse rest
|
|
|
|
|
2016-12-21 18:31:27 +00:00
|
|
|
-- Don't look for AppName in the hsdir, because it didn't used to
|
|
|
|
-- be included.
|
|
|
|
hasident hsdir = (show uid ++ "_" ++ ident) `isSuffixOf` takeFileName hsdir
|
2016-11-29 17:02:19 +00:00
|
|
|
|
|
|
|
-- | Sets up the directory for the socketFile, with appropriate
|
|
|
|
-- permissions. Must run as root.
|
2016-12-20 20:01:10 +00:00
|
|
|
prepHiddenServiceSocketDir :: AppName -> UserID -> UniqueIdent -> IO ()
|
|
|
|
prepHiddenServiceSocketDir appname uid ident = do
|
2016-11-29 17:02:19 +00:00
|
|
|
createDirectoryIfMissing True d
|
|
|
|
setOwnerAndGroup d uid (-1)
|
2020-11-05 22:45:37 +00:00
|
|
|
modifyFileMode (toRawFilePath d) $
|
2016-11-29 17:02:19 +00:00
|
|
|
addModes [ownerReadMode, ownerExecuteMode, ownerWriteMode]
|
|
|
|
where
|
2016-12-20 20:01:10 +00:00
|
|
|
d = takeDirectory $ hiddenServiceSocketFile appname uid ident
|
2016-11-29 17:02:19 +00:00
|
|
|
|
2016-12-28 19:12:31 +00:00
|
|
|
-- | Finds the system's torrc file, in any of the typical locations of it.
|
|
|
|
-- Returns the first found. If there is no system torrc file, defaults to
|
|
|
|
-- /etc/tor/torrc.
|
|
|
|
findTorrc :: IO FilePath
|
|
|
|
findTorrc = fromMaybe "/etc/tor/torrc" <$> firstM doesFileExist
|
|
|
|
-- Debian
|
|
|
|
[ "/etc/tor/torrc"
|
|
|
|
-- Some systems put it here instead.
|
|
|
|
, "/etc/torrc"
|
|
|
|
-- Default when installed from source
|
|
|
|
, "/usr/local/etc/tor/torrc"
|
|
|
|
]
|
2016-11-14 17:26:34 +00:00
|
|
|
|
2016-12-20 20:01:10 +00:00
|
|
|
torLibDir :: FilePath
|
|
|
|
torLibDir = "/var/lib/tor"
|
2016-11-14 20:35:45 +00:00
|
|
|
|
2016-12-20 20:01:10 +00:00
|
|
|
varLibDir :: FilePath
|
|
|
|
varLibDir = "/var/lib"
|
2016-12-24 21:08:03 +00:00
|
|
|
|
|
|
|
torIsInstalled :: IO Bool
|
|
|
|
torIsInstalled = inPath "tor"
|