propellor/Property/Ssh.hs

53 lines
1.5 KiB
Haskell
Raw Normal View History

module Property.Ssh where
2014-03-30 19:31:57 +00:00
import Common
2014-03-30 17:12:33 +00:00
import qualified Property.File as File
2014-03-30 19:31:57 +00:00
import Property.User
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
2014-03-30 17:12:33 +00:00
[ sshdConfig `File.lacksLine` (sshline $ not allowed)
, sshdConfig `File.containsLine` (sshline allowed)
] `onChange` restartSshd
where
desc = unwords [ "ssh config:", setting, sshBool allowed ]
2014-03-30 05:49:11 +00:00
sshline v = setting ++ " " ++ sshBool v
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
2014-03-30 05:18:35 +00:00
restartSshd = cmdProperty "service" [Param "ssh", Param "restart"]
2014-03-30 03:45:48 +00:00
{- Blow away existing host keys and make new ones. Use a flag
- file to prevent doing this more than once. -}
uniqueHostKeys :: Property
uniqueHostKeys = flagFile prop "/etc/ssh/.unique_host_keys"
`onChange` restartSshd
where
2014-03-30 19:31:57 +00:00
prop = Property "ssh unique host keys" $ do
2014-03-30 03:45:48 +00:00
void $ boolSystem "sh"
[ Param "-c"
, Param "rm -f /etc/ssh/ssh_host_*"
]
ensureProperty $
cmdProperty "/var/lib/dpkg/info/openssh-server.postinst"
[Param "configure"]