summaryrefslogtreecommitdiff
path: root/Property/Ssh.hs
blob: cca021a4c2d8e590a2a03334b2a8114ca24bd633 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
module Property.Ssh where

import Control.Applicative
import Control.Monad
import System.FilePath

import Property
import Property.User
import Utility.SafeCommand
import Utility.Exception

sshBool :: Bool -> String
sshBool True = "yes"
sshBool False = "no"

sshdConfig :: FilePath
sshdConfig = "/etc/ssh/sshd_config"

setSshdConfig :: String -> Bool -> Property
setSshdConfig setting allowed = combineProperties desc
	[ lineNotInFile sshdConfig (setting ++ sshBool (not allowed))
	, lineInFile sshdConfig (setting ++ sshBool allowed)
	] `onChange` restartSshd
  where
	desc = unwords [ "ssh config:", setting, sshBool allowed ]

permitRootLogin :: Bool -> Property
permitRootLogin = setSshdConfig "PermitRootLogin"

passwordAuthentication :: Bool -> Property
passwordAuthentication = setSshdConfig "PasswordAuthentication"

hasAuthorizedKeys :: UserName -> IO Bool
hasAuthorizedKeys = go <=< homedir
  where
	go Nothing = return False
	go (Just home) = not . null <$> catchDefaultIO ""
		(readFile $ home </> ".ssh" </> "authorized_keys")

restartSshd :: Property
restartSshd = CmdProperty "ssh restart" "service" [Param "sshd", Param "restart"]