2016-11-14 17:26:34 +00:00
|
|
|
{- git-annex command
|
|
|
|
-
|
|
|
|
- Copyright 2016 Joey Hess <id@joeyh.name>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
2016-12-20 21:40:36 +00:00
|
|
|
{-# LANGUAGE CPP #-}
|
|
|
|
|
2016-11-14 17:26:34 +00:00
|
|
|
module Command.EnableTor where
|
|
|
|
|
|
|
|
import Command
|
2016-11-29 21:30:27 +00:00
|
|
|
import P2P.Address
|
2016-11-14 17:26:34 +00:00
|
|
|
import Utility.Tor
|
2016-11-29 21:30:27 +00:00
|
|
|
import Annex.UUID
|
2016-12-20 21:40:36 +00:00
|
|
|
import Config.Files
|
|
|
|
|
|
|
|
#ifndef mingw32_HOST_OS
|
|
|
|
import Utility.Su
|
|
|
|
import System.Posix.User
|
|
|
|
#endif
|
2016-11-14 17:26:34 +00:00
|
|
|
|
2016-11-14 20:35:45 +00:00
|
|
|
-- This runs as root, so avoid making any commits or initializing
|
2016-11-29 21:30:27 +00:00
|
|
|
-- git-annex, or doing other things that create root-owned files.
|
2016-11-14 17:26:34 +00:00
|
|
|
cmd :: Command
|
|
|
|
cmd = noCommit $ dontCheck repoExists $
|
2016-11-29 21:30:27 +00:00
|
|
|
command "enable-tor" SectionSetup "enable tor hidden service"
|
|
|
|
"uid" (withParams seek)
|
2016-11-14 17:26:34 +00:00
|
|
|
|
|
|
|
seek :: CmdParams -> CommandSeek
|
|
|
|
seek = withWords start
|
|
|
|
|
2016-11-29 21:30:27 +00:00
|
|
|
start :: [String] -> CommandStart
|
2016-12-20 21:40:36 +00:00
|
|
|
start os = do
|
2016-12-20 21:46:14 +00:00
|
|
|
uuid <- getUUID
|
|
|
|
when (uuid == NoUUID) $
|
|
|
|
giveup "This can only be run in a git-annex repository."
|
2016-12-20 21:40:36 +00:00
|
|
|
#ifndef mingw32_HOST_OS
|
|
|
|
curruserid <- liftIO getEffectiveUserID
|
|
|
|
if curruserid == 0
|
|
|
|
then case readish =<< headMaybe os of
|
|
|
|
Nothing -> giveup "Need user-id parameter."
|
2016-12-20 21:46:14 +00:00
|
|
|
Just userid -> go uuid userid
|
2016-12-20 21:40:36 +00:00
|
|
|
else do
|
|
|
|
liftIO $ putStrLn "Need root access to enable tor..."
|
|
|
|
gitannex <- liftIO readProgramFile
|
|
|
|
let ps = [Param (cmdname cmd), Param (show curruserid)]
|
|
|
|
ifM (liftIO $ runAsRoot gitannex ps)
|
|
|
|
( stop
|
|
|
|
, giveup $ unwords $
|
|
|
|
[ "Failed to run as root:" , gitannex ] ++ toCommand ps
|
|
|
|
)
|
|
|
|
#else
|
2016-12-20 21:46:14 +00:00
|
|
|
go uuid 0
|
2016-12-20 21:40:36 +00:00
|
|
|
#endif
|
|
|
|
where
|
2016-12-20 21:46:14 +00:00
|
|
|
go uuid userid = do
|
2016-11-29 21:30:27 +00:00
|
|
|
(onionaddr, onionport) <- liftIO $
|
2016-12-20 20:01:10 +00:00
|
|
|
addHiddenService "tor-annex" userid (fromUUID uuid)
|
2016-11-29 21:30:27 +00:00
|
|
|
storeP2PAddress $ TorAnnex onionaddr onionport
|
2016-11-14 17:26:34 +00:00
|
|
|
stop
|