propellor/src/Propellor/Property/Ssh.hs

270 lines
8.9 KiB
Haskell
Raw Normal View History

2014-04-03 06:27:17 +00:00
module Propellor.Property.Ssh (
2015-01-19 18:26:18 +00:00
PubKeyText,
2015-03-12 02:18:50 +00:00
sshdConfig,
2014-04-03 06:27:17 +00:00
setSshdConfig,
permitRootLogin,
passwordAuthentication,
2015-04-23 15:58:37 +00:00
noPasswords,
2014-04-03 06:27:17 +00:00
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,
2015-01-04 20:54:43 +00:00
pubKey,
getPubKey,
2014-04-13 06:28:40 +00:00
keyImported,
2015-02-11 00:29:04 +00:00
keyImported',
2014-04-13 06:28:40 +00:00
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
import qualified Data.Map as M
2015-01-04 20:54:43 +00:00
type PubKeyText = String
sshBool :: Bool -> String
sshBool True = "yes"
sshBool False = "no"
sshdConfig :: FilePath
sshdConfig = "/etc/ssh/sshd_config"
setSshdConfig :: String -> Bool -> Property NoInfo
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 NoInfo
permitRootLogin = setSshdConfig "PermitRootLogin"
passwordAuthentication :: Bool -> Property NoInfo
passwordAuthentication = setSshdConfig "PasswordAuthentication"
2015-04-23 15:58:37 +00:00
-- | Configure ssh to not allow password logins.
--
-- To prevent lock-out, this is done only once root's
-- authorized_keys is in place.
noPasswords :: Property NoInfo
noPasswords = check (hasAuthorizedKeys (User "root")) $
passwordAuthentication False
dotDir :: User -> IO FilePath
2014-04-13 06:28:40 +00:00
dotDir user = do
h <- homedir user
return $ h </> ".ssh"
dotFile :: FilePath -> User -> IO FilePath
2014-04-13 06:28:40 +00:00
dotFile f user = do
d <- dotDir user
return $ d </> f
hasAuthorizedKeys :: User -> 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 NoInfo
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.
randomHostKeys :: Property NoInfo
2014-04-13 07:09:00 +00:00
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
2015-01-04 20:54:43 +00:00
-- | Installs the specified list of ssh host keys.
--
-- The corresponding private keys come from the privdata.
--
-- Any host keysthat are not in the list are removed from the host.
hostKeys :: IsContext c => c -> [(SshKeyType, PubKeyText)] -> Property HasInfo
2015-01-04 20:54:43 +00:00
hostKeys ctx l = propertyList desc $ catMaybes $
map (\(t, pub) -> Just $ hostKey ctx t pub) l ++ [cleanup]
where
desc = "ssh host keys configured " ++ typelist (map fst l)
typelist tl = "(" ++ unwords (map fromKeyType tl) ++ ")"
alltypes = [minBound..maxBound]
2015-01-04 21:00:08 +00:00
staletypes = let have = map fst l in filter (`notElem` have) alltypes
2015-01-04 20:54:43 +00:00
removestale b = map (File.notPresent . flip keyFile b) staletypes
cleanup
2015-01-04 21:14:07 +00:00
| null staletypes || null l = Nothing
| otherwise = Just $ toProp $
property ("any other ssh host keys removed " ++ typelist staletypes) $
ensureProperty $
combineProperties desc (removestale True ++ removestale False)
`onChange` restarted
2014-07-07 15:32:29 +00:00
-- | Installs a single ssh host key of a particular type.
--
2015-01-04 20:54:43 +00:00
-- The public key is provided to this function;
-- the private key comes from the privdata;
hostKey :: IsContext c => c -> SshKeyType -> PubKeyText -> Property HasInfo
2015-01-04 20:54:43 +00:00
hostKey context keytype pub = combineProperties desc
[ pubKey keytype pub
, toProp $ property desc $ install writeFile True pub
, withPrivData (keysrc "" (SshPrivKey keytype "")) context $ \getkey ->
2015-01-04 20:54:43 +00:00
property desc $ getkey $ install writeFileProtected False
2014-04-13 07:09:00 +00:00
]
`onChange` restarted
2014-04-13 07:09:00 +00:00
where
2015-01-04 20:54:43 +00:00
desc = "ssh host key configured (" ++ fromKeyType keytype ++ ")"
install writer ispub key = do
let f = keyFile keytype ispub
s <- liftIO $ catchDefaultIO "" $ readFileStrict f
2014-04-13 07:49:24 +00:00
if s == key
then noChange
else makeChange $ writer f key
keysrc ext field = PrivDataSourceFileFromCommand field ("sshkey"++ext)
("ssh-keygen -t " ++ sshKeyTypeParam keytype ++ " -f sshkey")
2014-04-13 07:09:00 +00:00
2015-01-04 20:54:43 +00:00
keyFile :: SshKeyType -> Bool -> FilePath
keyFile keytype ispub = "/etc/ssh/ssh_host_" ++ fromKeyType keytype ++ "_key" ++ ext
where
ext = if ispub then ".pub" else ""
-- | Indicates the host key that is used by a Host, but does not actually
-- configure the host to use it. Normally this does not need to be used;
-- use 'hostKey' instead.
pubKey :: SshKeyType -> PubKeyText -> Property HasInfo
2015-01-04 20:54:43 +00:00
pubKey t k = pureInfoProperty ("ssh pubkey known") $
mempty { _sshPubKey = M.singleton t k }
getPubKey :: Propellor (M.Map SshKeyType String)
getPubKey = asks (_sshPubKey . hostInfo)
2014-07-06 19:56:56 +00:00
-- | Sets up a user with a ssh private key and public key pair from the
-- PrivData.
2015-02-11 00:29:04 +00:00
--
-- If the user already has a private/public key, it is left unchanged.
keyImported :: IsContext c => SshKeyType -> User -> c -> Property HasInfo
2015-02-11 00:29:04 +00:00
keyImported = keyImported' Nothing
-- | A file can be speficied to write the key to somewhere other than
-- usual. Allows a user to have multiple keys for different roles.
keyImported' :: IsContext c => Maybe FilePath -> SshKeyType -> User -> c -> Property HasInfo
keyImported' dest keytype user@(User u) context = combineProperties desc
[ installkey (SshPubKey keytype u) (install writeFile ".pub")
, installkey (SshPrivKey keytype u) (install writeFileProtected "")
2014-04-13 01:43:30 +00:00
]
2014-04-13 01:34:25 +00:00
where
2015-02-11 00:56:57 +00:00
desc = unwords $ catMaybes
[ Just u
2015-02-11 00:56:57 +00:00
, Just "has ssh key"
, dest
, Just $ "(" ++ 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
, File.ownerGroup f user (userGroup user)
, File.ownerGroup (takeDirectory f) user (userGroup user)
2014-04-13 21:16:31 +00:00
]
2014-04-13 01:34:25 +00:00
)
2015-02-11 00:29:04 +00:00
keyfile ext = case dest of
Nothing -> do
home <- homeDirectory <$> getUserEntryForName u
2015-02-11 00:29:04 +00:00
return $ home </> ".ssh" </> "id_" ++ fromKeyType keytype ++ ext
Just f -> return $ f ++ ext
2014-04-13 07:09:00 +00:00
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
2015-02-01 20:19:37 +00:00
-- | Puts some host's ssh public key(s), as set using 'pubKey' or 'hostKey'
2015-01-04 19:36:10 +00:00
-- into the known_hosts file for a user.
knownHost :: [Host] -> HostName -> User -> Property NoInfo
knownHost hosts hn user@(User u) = property desc $
2015-01-04 19:36:10 +00:00
go =<< fromHost hosts hn getPubKey
2014-04-13 06:28:40 +00:00
where
desc = u ++ " knows ssh key for " ++ hn
go (Just m) | not (M.null m) = do
2014-04-13 06:28:40 +00:00
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.containsLines`
(map (\k -> hn ++ " " ++ k) (M.elems m))
, File.ownerGroup f user (userGroup user)
, File.ownerGroup (takeDirectory f) user (userGroup user)
2014-04-13 06:28:40 +00:00
]
2014-07-23 16:27:38 +00:00
go _ = do
warningMessage $ "no configred pubKey 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 => User -> c -> Property HasInfo
authorizedKeys user@(User u) context = withPrivData (SshAuthorizedKeys u) context $ \get ->
property (u ++ " 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 (userGroup user)
, File.ownerGroup (takeDirectory f) user (userGroup user)
2014-05-21 18:57:04 +00:00
]
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 :: User -> String -> Property NoInfo
authorizedKey user@(User u) l = property desc $ do
2014-11-24 04:51:36 +00:00
f <- liftIO $ dotFile "authorized_keys" user
ensureProperty $ combineProperties desc
[ f `File.containsLine` l
2014-11-24 04:51:36 +00:00
`requires` File.dirExists (takeDirectory f)
`onChange` File.mode f (combineModes [ownerWriteMode, ownerReadMode])
, File.ownerGroup f user (userGroup user)
, File.ownerGroup (takeDirectory f) user (userGroup user)
]
where
desc = u ++ " has autorized_keys"
2014-11-24 04:51:36 +00:00
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 = enable <!> disable
2014-08-21 18:04:26 +00:00
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