aboutsummaryrefslogtreecommitdiff
path: root/Command
diff options
context:
space:
mode:
authorGravatar Joey Hess <joeyh@joeyh.name>2016-12-20 17:40:36 -0400
committerGravatar Joey Hess <joeyh@joeyh.name>2016-12-20 17:40:36 -0400
commit5387e0e1aeee46b94ad6e0a1d59b1422a8048665 (patch)
tree6b32d4f70d9ca5dfd511e08e89caf7a7dbee525f /Command
parente493cbaace4651d5e7da26834ab108cfae3df1dc (diff)
enable-tor: No longer needs to be run as root.
When run by not root, su's to root automatically. This commit was sponsored by Brock Spratlen on Patreon.
Diffstat (limited to 'Command')
-rw-r--r--Command/EnableTor.hs32
1 files changed, 29 insertions, 3 deletions
diff --git a/Command/EnableTor.hs b/Command/EnableTor.hs
index c6d477b4e..91d5af701 100644
--- a/Command/EnableTor.hs
+++ b/Command/EnableTor.hs
@@ -5,12 +5,20 @@
- Licensed under the GNU GPL version 3 or higher.
-}
+{-# LANGUAGE CPP #-}
+
module Command.EnableTor where
import Command
import P2P.Address
import Utility.Tor
import Annex.UUID
+import Config.Files
+
+#ifndef mingw32_HOST_OS
+import Utility.Su
+import System.Posix.User
+#endif
-- This runs as root, so avoid making any commits or initializing
-- git-annex, or doing other things that create root-owned files.
@@ -23,9 +31,27 @@ seek :: CmdParams -> CommandSeek
seek = withWords start
start :: [String] -> CommandStart
-start ps = case readish =<< headMaybe ps of
- Nothing -> giveup "Bad params"
- Just userid -> do
+start os = do
+#ifndef mingw32_HOST_OS
+ curruserid <- liftIO getEffectiveUserID
+ if curruserid == 0
+ then case readish =<< headMaybe os of
+ Nothing -> giveup "Need user-id parameter."
+ Just userid -> go userid
+ 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
+ go 0
+#endif
+ where
+ go userid = do
uuid <- getUUID
when (uuid == NoUUID) $
giveup "This can only be run in a git-annex repository."