webapp: When setting up authorized_keys, use GIT_ANNEX_SHELL_DIRECTORY.

This commit is contained in:
Joey Hess 2012-11-05 12:21:13 -04:00
parent bd230efa56
commit 0b8027e527
6 changed files with 26 additions and 21 deletions

View file

@ -18,10 +18,10 @@ import qualified Data.Text as T
{- Authorized keys are set up before pairing is complete, so that the other {- Authorized keys are set up before pairing is complete, so that the other
- side can immediately begin syncing. -} - side can immediately begin syncing. -}
setupAuthorizedKeys :: PairMsg -> IO () setupAuthorizedKeys :: PairMsg -> FilePath -> IO ()
setupAuthorizedKeys msg = do setupAuthorizedKeys msg repodir = do
validateSshPubKey pubkey validateSshPubKey pubkey
unlessM (liftIO $ addAuthorizedKeys False pubkey) $ unlessM (liftIO $ addAuthorizedKeys False repodir pubkey) $
error "failed setting up ssh authorized keys" error "failed setting up ssh authorized keys"
where where
pubkey = remoteSshPubKey $ pairMsgData msg pubkey = remoteSshPubKey $ pairMsgData msg

View file

@ -116,13 +116,13 @@ validateSshPubKey pubkey = either error return $ check $ words pubkey
| all (\c -> isAlphaNum c || c == '@' || c == '-' || c == '_') comment = ok | all (\c -> isAlphaNum c || c == '@' || c == '-' || c == '_') comment = ok
| otherwise = err "bad comment in ssh public key" | otherwise = err "bad comment in ssh public key"
addAuthorizedKeys :: Bool -> SshPubKey -> IO Bool addAuthorizedKeys :: Bool -> FilePath -> SshPubKey -> IO Bool
addAuthorizedKeys rsynconly pubkey = boolSystem "sh" addAuthorizedKeys rsynconly dir pubkey = boolSystem "sh"
[ Param "-c" , Param $ addAuthorizedKeysCommand rsynconly pubkey ] [ Param "-c" , Param $ addAuthorizedKeysCommand rsynconly dir pubkey ]
removeAuthorizedKeys :: Bool -> SshPubKey -> IO () removeAuthorizedKeys :: Bool -> FilePath -> SshPubKey -> IO ()
removeAuthorizedKeys rsynconly pubkey = do removeAuthorizedKeys rsynconly dir pubkey = do
let keyline = authorizedKeysLine rsynconly pubkey let keyline = authorizedKeysLine rsynconly dir pubkey
sshdir <- sshDir sshdir <- sshDir
let keyfile = sshdir </> ".authorized_keys" let keyfile = sshdir </> ".authorized_keys"
ls <- lines <$> readFileStrict keyfile ls <- lines <$> readFileStrict keyfile
@ -134,8 +134,8 @@ removeAuthorizedKeys rsynconly pubkey = do
- The ~/.ssh/git-annex-shell wrapper script is created if not already - The ~/.ssh/git-annex-shell wrapper script is created if not already
- present. - present.
-} -}
addAuthorizedKeysCommand :: Bool -> SshPubKey -> String addAuthorizedKeysCommand :: Bool -> FilePath -> SshPubKey -> String
addAuthorizedKeysCommand rsynconly pubkey = join "&&" addAuthorizedKeysCommand rsynconly dir pubkey = join "&&"
[ "mkdir -p ~/.ssh" [ "mkdir -p ~/.ssh"
, join "; " , join "; "
[ "if [ ! -e " ++ wrapper ++ " ]" [ "if [ ! -e " ++ wrapper ++ " ]"
@ -147,7 +147,7 @@ addAuthorizedKeysCommand rsynconly pubkey = join "&&"
, "chmod 600 ~/.ssh/authorized_keys" , "chmod 600 ~/.ssh/authorized_keys"
, unwords , unwords
[ "echo" [ "echo"
, shellEscape $ authorizedKeysLine rsynconly pubkey , shellEscape $ authorizedKeysLine rsynconly dir pubkey
, ">>~/.ssh/authorized_keys" , ">>~/.ssh/authorized_keys"
] ]
] ]
@ -160,14 +160,14 @@ addAuthorizedKeysCommand rsynconly pubkey = join "&&"
, "exec git-annex-shell -c \"$SSH_ORIGINAL_COMMAND\"" , "exec git-annex-shell -c \"$SSH_ORIGINAL_COMMAND\""
] ]
authorizedKeysLine :: Bool -> SshPubKey -> String authorizedKeysLine :: Bool -> FilePath -> SshPubKey -> String
authorizedKeysLine rsynconly pubkey authorizedKeysLine rsynconly dir pubkey
{- TODO: Locking down rsync is difficult, requiring a rather {- TODO: Locking down rsync is difficult, requiring a rather
- long perl script. -} - long perl script. -}
| rsynconly = pubkey | rsynconly = pubkey
| otherwise = limitcommand ++ pubkey | otherwise = limitcommand ++ pubkey
where where
limitcommand = "command=\"~/.ssh/git-annex-shell\",no-agent-forwarding,no-port-forwarding,no-X11-forwarding " limitcommand = "command=\"GIT_ANNEX_SHELL_DIRECTORY="++shellEscape dir++" ~/.ssh/git-annex-shell\",no-agent-forwarding,no-port-forwarding,no-X11-forwarding "
{- Generates a ssh key pair. -} {- Generates a ssh key pair. -}
genSshKeyPair :: IO SshKeyPair genSshKeyPair :: IO SshKeyPair

View file

@ -16,6 +16,7 @@ import Assistant.WebApp.Types
import Assistant.Alert import Assistant.Alert
import Assistant.DaemonStatus import Assistant.DaemonStatus
import Utility.ThreadScheduler import Utility.ThreadScheduler
import Git
import Network.Multicast import Network.Multicast
import Network.Socket import Network.Socket
@ -120,7 +121,8 @@ pairReqReceived False urlrenderer msg = do
pairAckReceived :: Bool -> Maybe PairingInProgress -> PairMsg -> [PairingInProgress] -> Assistant [PairingInProgress] pairAckReceived :: Bool -> Maybe PairingInProgress -> PairMsg -> [PairingInProgress] -> Assistant [PairingInProgress]
pairAckReceived True (Just pip) msg cache = do pairAckReceived True (Just pip) msg cache = do
stopSending pip stopSending pip
liftIO $ setupAuthorizedKeys msg repodir <- repoPath <$> liftAnnex gitRepo
liftIO $ setupAuthorizedKeys msg repodir
finishedPairing msg (inProgressSshKeyPair pip) finishedPairing msg (inProgressSshKeyPair pip)
startSending pip PairDone $ multicastPairMsg startSending pip PairDone $ multicastPairMsg
(Just 1) (inProgressSecret pip) (inProgressPairData pip) (Just 1) (inProgressSecret pip) (inProgressPairData pip)

View file

@ -37,6 +37,7 @@ import Assistant.Types.NetMessager
import Assistant.NetMessager import Assistant.NetMessager
#endif #endif
import Utility.UserInfo import Utility.UserInfo
import Git
import Yesod import Yesod
import Data.Text (Text) import Data.Text (Text)
@ -125,12 +126,13 @@ noLocalPairing = noPairing "local"
getFinishLocalPairR :: PairMsg -> Handler RepHtml getFinishLocalPairR :: PairMsg -> Handler RepHtml
#ifdef WITH_PAIRING #ifdef WITH_PAIRING
getFinishLocalPairR msg = promptSecret (Just msg) $ \_ secret -> do getFinishLocalPairR msg = promptSecret (Just msg) $ \_ secret -> do
liftIO $ setup repodir <- lift $ repoPath <$> runAnnex undefined gitRepo
startLocalPairing PairAck cleanup alert uuid "" secret liftIO $ setup repodir
startLocalPairing PairAck (cleanup repodir) alert uuid "" secret
where where
alert = pairRequestAcknowledgedAlert (pairRepo msg) . Just alert = pairRequestAcknowledgedAlert (pairRepo msg) . Just
setup = setupAuthorizedKeys msg setup repodir = setupAuthorizedKeys msg repodir
cleanup = removeAuthorizedKeys False $ cleanup repodir = removeAuthorizedKeys False repodir $
remoteSshPubKey $ pairMsgData msg remoteSshPubKey $ pairMsgData msg
uuid = Just $ pairUUID $ pairMsgData msg uuid = Just $ pairUUID $ pairMsgData msg
#else #else

View file

@ -276,7 +276,7 @@ makeSsh' rsync setup sshdata keypair =
, if rsync then Nothing else Just "git init --bare --shared" , if rsync then Nothing else Just "git init --bare --shared"
, if rsync then Nothing else Just "git annex init" , if rsync then Nothing else Just "git annex init"
, if needsPubKey sshdata , if needsPubKey sshdata
then addAuthorizedKeysCommand (rsyncOnly sshdata) . sshPubKey <$> keypair then addAuthorizedKeysCommand (rsyncOnly sshdata) remotedir . sshPubKey <$> keypair
else Nothing else Nothing
] ]

1
debian/changelog vendored
View file

@ -42,6 +42,7 @@ git-annex (3.20121018) UNRELEASED; urgency=low
name they were originally created using. name they were originally created using.
* git-annex-shell: GIT_ANNEX_SHELL_DIRECTORY can be set to limit it * git-annex-shell: GIT_ANNEX_SHELL_DIRECTORY can be set to limit it
to operating on a specified directory. to operating on a specified directory.
* webapp: When setting up authorized_keys, use GIT_ANNEX_SHELL_DIRECTORY.
-- Joey Hess <joeyh@debian.org> Wed, 17 Oct 2012 14:24:10 -0400 -- Joey Hess <joeyh@debian.org> Wed, 17 Oct 2012 14:24:10 -0400