propellor/src/Propellor/Property/Ssh.hs

197 lines
6.2 KiB
Haskell
Raw Normal View History

2014-04-03 06:27:17 +00:00
module Propellor.Property.Ssh (
setSshdConfig,
permitRootLogin,
passwordAuthentication,
hasAuthorizedKeys,
2014-11-24 04:51:36 +00:00
authorizedKey,
restarted,
2014-04-13 07:09:00 +00:00
randomHostKeys,
2014-07-07 15:32:29 +00:00
hostKeys,
2014-04-13 07:09:00 +00:00
hostKey,
2014-04-13 06:28:40 +00:00
keyImported,
knownHost,
2014-08-21 18:04:26 +00:00
authorizedKeys,
listenPort
2014-04-03 06:27:17 +00:00
) where
2014-03-31 03:55:59 +00:00
import Propellor
2014-03-31 03:37:54 +00:00
import qualified Propellor.Property.File as File
import qualified Propellor.Property.Service as Service
2014-03-31 03:37:54 +00:00
import Propellor.Property.User
2014-03-31 03:55:59 +00:00
import Utility.SafeCommand
2014-04-13 01:34:25 +00:00
import Utility.FileMode
import System.PosixCompat
sshBool :: Bool -> String
sshBool True = "yes"
sshBool False = "no"
sshdConfig :: FilePath
sshdConfig = "/etc/ssh/sshd_config"
setSshdConfig :: String -> Bool -> Property
2014-04-01 21:32:37 +00:00
setSshdConfig setting allowed = combineProperties "sshd config"
2014-03-30 17:12:33 +00:00
[ sshdConfig `File.lacksLine` (sshline $ not allowed)
, sshdConfig `File.containsLine` (sshline allowed)
2014-03-30 20:11:00 +00:00
]
`onChange` restarted
2014-03-30 20:11:00 +00:00
`describe` unwords [ "ssh config:", setting, sshBool allowed ]
where
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"
2014-04-13 06:28:40 +00:00
dotDir :: UserName -> IO FilePath
dotDir user = do
h <- homedir user
return $ h </> ".ssh"
dotFile :: FilePath -> UserName -> IO FilePath
dotFile f user = do
d <- dotDir user
return $ d </> f
hasAuthorizedKeys :: UserName -> IO Bool
2014-04-13 06:28:40 +00:00
hasAuthorizedKeys = go <=< dotFile "authorized_keys"
where
2014-04-13 06:28:40 +00:00
go f = not . null <$> catchDefaultIO "" (readFile f)
restarted :: Property
restarted = Service.restarted "ssh"
2014-03-30 03:45:48 +00:00
2014-04-03 06:27:17 +00:00
-- | Blows away existing host keys and make new ones.
-- Useful for systems installed from an image that might reuse host keys.
-- A flag file is used to only ever do this once.
2014-04-13 07:09:00 +00:00
randomHostKeys :: Property
randomHostKeys = flagFile prop "/etc/ssh/.unique_host_keys"
`onChange` restarted
2014-03-30 03:45:48 +00:00
where
prop = property "ssh random host keys" $ do
void $ liftIO $ boolSystem "sh"
2014-03-30 03:45:48 +00:00
[ Param "-c"
, Param "rm -f /etc/ssh/ssh_host_*"
]
2014-07-05 21:57:19 +00:00
ensureProperty $ scriptProperty
2014-07-05 22:00:53 +00:00
[ "DPKG_MAINTSCRIPT_NAME=postinst DPKG_MAINTSCRIPT_PACKAGE=openssh-server /var/lib/dpkg/info/openssh-server.postinst configure" ]
2014-04-13 01:34:25 +00:00
2014-07-07 15:32:29 +00:00
-- | Sets all types of ssh host keys from the privdata.
2014-12-07 19:21:55 +00:00
hostKeys :: IsContext c => c -> Property
2014-07-07 15:32:29 +00:00
hostKeys ctx = propertyList "known ssh host keys"
[ hostKey SshDsa ctx
, hostKey SshRsa ctx
, hostKey SshEcdsa ctx
]
-- | Sets a single ssh host key from the privdata.
hostKey :: IsContext c => SshKeyType -> c -> Property
2014-07-06 19:56:56 +00:00
hostKey keytype context = combineProperties desc
[ installkey (SshPubKey keytype "") (install writeFile ".pub")
, installkey (SshPrivKey keytype "") (install writeFileProtected "")
2014-04-13 07:09:00 +00:00
]
`onChange` restarted
2014-04-13 07:09:00 +00:00
where
desc = "known ssh host key (" ++ fromKeyType keytype ++ ")"
2014-07-06 19:56:56 +00:00
installkey p a = withPrivData p context $ \getkey ->
property desc $ getkey a
install writer ext key = do
2014-04-13 07:28:53 +00:00
let f = "/etc/ssh/ssh_host_" ++ fromKeyType keytype ++ "_key" ++ ext
2014-04-13 07:49:24 +00:00
s <- liftIO $ readFileStrict f
if s == key
then noChange
else makeChange $ writer f key
2014-04-13 07:09:00 +00:00
2014-07-06 19:56:56 +00:00
-- | Sets up a user with a ssh private key and public key pair from the
-- PrivData.
keyImported :: IsContext c => SshKeyType -> UserName -> c -> Property
2014-07-06 19:56:56 +00:00
keyImported keytype user context = combineProperties desc
[ installkey (SshPubKey keytype user) (install writeFile ".pub")
, installkey (SshPrivKey keytype user) (install writeFileProtected "")
2014-04-13 01:43:30 +00:00
]
2014-04-13 01:34:25 +00:00
where
2014-04-13 07:21:02 +00:00
desc = user ++ " has ssh key (" ++ fromKeyType keytype ++ ")"
2014-07-06 19:56:56 +00:00
installkey p a = withPrivData p context $ \getkey ->
property desc $ getkey a
install writer ext key = do
2014-04-13 01:43:30 +00:00
f <- liftIO $ keyfile ext
2014-04-13 01:34:25 +00:00
ifM (liftIO $ doesFileExist f)
( noChange
2014-05-21 18:57:04 +00:00
, ensureProperties
2014-07-06 19:56:56 +00:00
[ property desc $ makeChange $ do
createDirectoryIfMissing True (takeDirectory f)
writer f key
2014-04-13 21:16:31 +00:00
, File.ownerGroup f user user
2014-05-21 18:57:04 +00:00
, File.ownerGroup (takeDirectory f) user user
2014-04-13 21:16:31 +00:00
]
2014-04-13 01:34:25 +00:00
)
2014-04-13 01:43:30 +00:00
keyfile ext = do
2014-04-13 01:34:25 +00:00
home <- homeDirectory <$> getUserEntryForName user
2014-04-13 07:09:00 +00:00
return $ home </> ".ssh" </> "id_" ++ fromKeyType keytype ++ ext
fromKeyType :: SshKeyType -> String
fromKeyType SshRsa = "rsa"
fromKeyType SshDsa = "dsa"
2014-04-13 15:58:22 +00:00
fromKeyType SshEcdsa = "ecdsa"
fromKeyType SshEd25519 = "ed25519"
2014-04-13 06:28:40 +00:00
-- | Puts some host's ssh public key into the known_hosts file for a user.
knownHost :: [Host] -> HostName -> UserName -> Property
knownHost hosts hn user = property desc $
2014-04-13 06:28:40 +00:00
go =<< fromHost hosts hn getSshPubKey
where
desc = user ++ " knows ssh key for " ++ hn
go (Just (Just k)) = do
f <- liftIO $ dotFile "known_hosts" user
2014-04-13 07:28:53 +00:00
ensureProperty $ combineProperties desc
2014-04-13 06:28:40 +00:00
[ File.dirExists (takeDirectory f)
, f `File.containsLine` (hn ++ " " ++ k)
2014-04-13 21:16:31 +00:00
, File.ownerGroup f user user
2014-04-13 06:28:40 +00:00
]
2014-07-23 16:27:38 +00:00
go _ = do
warningMessage $ "no configred sshPubKey for " ++ hn
2014-04-13 06:28:40 +00:00
return FailedChange
2014-04-13 07:09:00 +00:00
-- | Makes a user have authorized_keys from the PrivData
2014-11-24 04:51:36 +00:00
--
-- This removes any other lines from the file.
authorizedKeys :: IsContext c => UserName -> c -> Property
2014-07-06 19:56:56 +00:00
authorizedKeys user context = withPrivData (SshAuthorizedKeys user) context $ \get ->
property (user ++ " has authorized_keys") $ get $ \v -> do
2014-04-13 07:09:00 +00:00
f <- liftIO $ dotFile "authorized_keys" user
2014-04-13 21:16:31 +00:00
liftIO $ do
createDirectoryIfMissing True (takeDirectory f)
writeFileProtected f v
2014-05-21 18:57:04 +00:00
ensureProperties
[ File.ownerGroup f user user
, File.ownerGroup (takeDirectory f) user user
]
2014-08-21 18:04:26 +00:00
2014-11-24 04:51:36 +00:00
-- | Ensures that a user's authorized_keys contains a line.
-- Any other lines in the file are preserved as-is.
authorizedKey :: UserName -> String -> Property
authorizedKey user l = property (user ++ " has autorized_keys line " ++ l) $ do
f <- liftIO $ dotFile "authorized_keys" user
ensureProperty $
f `File.containsLine` l
`requires` File.dirExists (takeDirectory f)
`onChange` File.mode f (combineModes [ownerWriteMode, ownerReadMode])
2014-08-21 18:04:26 +00:00
-- | Makes the ssh server listen on a given port, in addition to any other
-- ports it is configured to listen on.
--
-- Revert to prevent it listening on a particular port.
listenPort :: Int -> RevertableProperty
listenPort port = RevertableProperty enable disable
where
portline = "Port " ++ show port
enable = sshdConfig `File.containsLine` portline
`describe` ("ssh listening on " ++ portline)
`onChange` restarted
2014-08-21 18:04:26 +00:00
disable = sshdConfig `File.lacksLine` portline
`describe` ("ssh not listening on " ++ portline)
`onChange` restarted