2014-03-31 03:37:54 +00:00
|
|
|
module Propellor.Property.User where
|
2014-03-30 03:10:52 +00:00
|
|
|
|
|
|
|
import System.Posix
|
|
|
|
|
2014-03-31 03:55:59 +00:00
|
|
|
import Propellor
|
2015-04-20 01:00:21 +00:00
|
|
|
import qualified Propellor.Property.File as File
|
2014-03-30 03:10:52 +00:00
|
|
|
|
2014-03-30 20:53:31 +00:00
|
|
|
data Eep = YesReallyDeleteHome
|
|
|
|
|
2015-04-22 17:04:39 +00:00
|
|
|
accountFor :: User -> Property NoInfo
|
|
|
|
accountFor user@(User u) = check (isNothing <$> catchMaybeIO (homedir user)) $ cmdProperty "adduser"
|
2014-03-31 03:55:59 +00:00
|
|
|
[ "--disabled-password"
|
|
|
|
, "--gecos", ""
|
2015-04-22 17:04:39 +00:00
|
|
|
, u
|
2014-03-30 03:10:52 +00:00
|
|
|
]
|
2015-04-22 17:04:39 +00:00
|
|
|
`describe` ("account for " ++ u)
|
2014-03-30 03:10:52 +00:00
|
|
|
|
2014-04-01 20:58:11 +00:00
|
|
|
-- | Removes user home directory!! Use with caution.
|
2015-04-22 17:04:39 +00:00
|
|
|
nuked :: User -> Eep -> Property NoInfo
|
|
|
|
nuked user@(User u) _ = check (isJust <$> catchMaybeIO (homedir user)) $ cmdProperty "userdel"
|
2014-03-31 03:55:59 +00:00
|
|
|
[ "-r"
|
2015-04-22 17:04:39 +00:00
|
|
|
, u
|
2014-03-30 04:17:44 +00:00
|
|
|
]
|
2015-04-22 17:04:39 +00:00
|
|
|
`describe` ("nuked user " ++ u)
|
2014-03-30 04:17:44 +00:00
|
|
|
|
2014-04-01 20:58:11 +00:00
|
|
|
-- | Only ensures that the user has some password set. It may or may
|
2014-12-14 19:24:10 +00:00
|
|
|
-- not be a password from the PrivData.
|
2015-04-22 17:04:39 +00:00
|
|
|
hasSomePassword :: User -> Property HasInfo
|
2014-12-07 18:57:35 +00:00
|
|
|
hasSomePassword user = hasSomePassword' user hostContext
|
2014-07-06 19:56:56 +00:00
|
|
|
|
2014-11-23 20:39:49 +00:00
|
|
|
-- | While hasSomePassword uses the name of the host as context,
|
|
|
|
-- this allows specifying a different context. This is useful when
|
|
|
|
-- you want to use the same password on multiple hosts, for example.
|
2015-04-22 17:04:39 +00:00
|
|
|
hasSomePassword' :: IsContext c => User -> c -> Property HasInfo
|
2014-11-23 20:39:49 +00:00
|
|
|
hasSomePassword' user context = check ((/= HasPassword) <$> getPasswordStatus user) $
|
|
|
|
hasPassword' user context
|
|
|
|
|
2014-12-14 19:24:10 +00:00
|
|
|
-- | Ensures that a user's password is set to a password from the PrivData.
|
2014-11-23 20:39:49 +00:00
|
|
|
-- (Will change any existing password.)
|
2014-12-14 19:24:10 +00:00
|
|
|
--
|
|
|
|
-- A user's password can be stored in the PrivData in either of two forms;
|
|
|
|
-- the full cleartext <Password> or a <CryptPassword> hash. The latter
|
|
|
|
-- is obviously more secure.
|
2015-04-22 17:04:39 +00:00
|
|
|
hasPassword :: User -> Property HasInfo
|
2014-12-07 18:57:35 +00:00
|
|
|
hasPassword user = hasPassword' user hostContext
|
2014-11-23 20:39:49 +00:00
|
|
|
|
2015-04-22 17:04:39 +00:00
|
|
|
hasPassword' :: IsContext c => User -> c -> Property HasInfo
|
|
|
|
hasPassword' (User u) context = go `requires` shadowConfig True
|
2014-12-05 20:33:23 +00:00
|
|
|
where
|
2014-12-14 20:14:05 +00:00
|
|
|
go = withSomePrivData srcs context $
|
2015-04-22 17:04:39 +00:00
|
|
|
property (u ++ " has password") . setPassword
|
2014-12-14 20:14:05 +00:00
|
|
|
srcs =
|
2015-04-22 17:04:39 +00:00
|
|
|
[ PrivDataSource (CryptPassword u)
|
2014-12-14 20:14:05 +00:00
|
|
|
"a crypt(3)ed password, which can be generated by, for example: perl -e 'print crypt(shift, q{$6$}.shift)' 'somepassword' 'somesalt'"
|
2015-04-22 17:04:39 +00:00
|
|
|
, PrivDataSource (Password u) ("a password for " ++ u)
|
2014-12-14 20:14:05 +00:00
|
|
|
]
|
2014-12-07 18:57:35 +00:00
|
|
|
|
2014-12-14 19:24:10 +00:00
|
|
|
setPassword :: (((PrivDataField, PrivData) -> Propellor Result) -> Propellor Result) -> Propellor Result
|
|
|
|
setPassword getpassword = getpassword $ go
|
|
|
|
where
|
|
|
|
go (Password user, password) = set user password []
|
|
|
|
go (CryptPassword user, hash) = set user hash ["--encrypted"]
|
|
|
|
go (f, _) = error $ "Unexpected type of privdata: " ++ show f
|
|
|
|
|
|
|
|
set user v ps = makeChange $ withHandle StdinHandle createProcessSuccess
|
|
|
|
(proc "chpasswd" ps) $ \h -> do
|
|
|
|
hPutStrLn h $ user ++ ":" ++ v
|
2014-12-07 18:57:35 +00:00
|
|
|
hClose h
|
2014-03-30 23:10:32 +00:00
|
|
|
|
2015-04-22 17:04:39 +00:00
|
|
|
lockedPassword :: User -> Property NoInfo
|
|
|
|
lockedPassword user@(User u) = check (not <$> isLockedPassword user) $ cmdProperty "passwd"
|
2014-03-31 03:55:59 +00:00
|
|
|
[ "--lock"
|
2015-04-22 17:04:39 +00:00
|
|
|
, u
|
2014-03-30 04:17:44 +00:00
|
|
|
]
|
2015-04-22 17:04:39 +00:00
|
|
|
`describe` ("locked " ++ u ++ " password")
|
2014-03-30 04:17:44 +00:00
|
|
|
|
2014-03-31 00:18:45 +00:00
|
|
|
data PasswordStatus = NoPassword | LockedPassword | HasPassword
|
|
|
|
deriving (Eq)
|
|
|
|
|
2015-04-22 17:04:39 +00:00
|
|
|
getPasswordStatus :: User -> IO PasswordStatus
|
|
|
|
getPasswordStatus (User u) = parse . words <$> readProcess "passwd" ["-S", u]
|
2014-03-30 05:57:10 +00:00
|
|
|
where
|
2014-03-31 00:18:45 +00:00
|
|
|
parse (_:"L":_) = LockedPassword
|
|
|
|
parse (_:"NP":_) = NoPassword
|
|
|
|
parse (_:"P":_) = HasPassword
|
|
|
|
parse _ = NoPassword
|
|
|
|
|
2015-04-22 17:04:39 +00:00
|
|
|
isLockedPassword :: User -> IO Bool
|
2014-03-31 00:18:45 +00:00
|
|
|
isLockedPassword user = (== LockedPassword) <$> getPasswordStatus user
|
2014-03-30 05:57:10 +00:00
|
|
|
|
2015-04-22 17:04:39 +00:00
|
|
|
homedir :: User -> IO FilePath
|
|
|
|
homedir (User user) = homeDirectory <$> getUserEntryForName user
|
2014-11-23 17:25:39 +00:00
|
|
|
|
2015-04-22 17:04:39 +00:00
|
|
|
hasGroup :: User -> Group -> Property NoInfo
|
|
|
|
hasGroup (User user) (Group group') = check test $ cmdProperty "adduser"
|
2014-11-23 17:25:39 +00:00
|
|
|
[ user
|
|
|
|
, group'
|
|
|
|
]
|
|
|
|
`describe` unwords ["user", user, "in group", group']
|
|
|
|
where
|
2014-11-23 18:37:37 +00:00
|
|
|
test = not . elem group' . words <$> readProcess "groups" [user]
|
2014-12-04 21:11:15 +00:00
|
|
|
|
|
|
|
-- | Controls whether shadow passwords are enabled or not.
|
2015-01-25 02:38:10 +00:00
|
|
|
shadowConfig :: Bool -> Property NoInfo
|
2014-12-04 21:11:15 +00:00
|
|
|
shadowConfig True = check (not <$> shadowExists) $
|
|
|
|
cmdProperty "shadowconfig" ["on"]
|
|
|
|
`describe` "shadow passwords enabled"
|
|
|
|
shadowConfig False = check shadowExists $
|
|
|
|
cmdProperty "shadowconfig" ["off"]
|
|
|
|
`describe` "shadow passwords disabled"
|
|
|
|
|
|
|
|
shadowExists :: IO Bool
|
|
|
|
shadowExists = doesFileExist "/etc/shadow"
|
2015-04-20 01:00:21 +00:00
|
|
|
|
|
|
|
-- | Ensures that a user has a specified login shell, and that the shell
|
|
|
|
-- is enabled in /etc/shells.
|
2015-04-22 17:04:39 +00:00
|
|
|
hasLoginShell :: User -> FilePath -> Property NoInfo
|
2015-04-20 01:00:21 +00:00
|
|
|
hasLoginShell user loginshell = shellSetTo user loginshell `requires` shellEnabled loginshell
|
|
|
|
|
2015-04-22 17:04:39 +00:00
|
|
|
shellSetTo :: User -> FilePath -> Property NoInfo
|
|
|
|
shellSetTo (User u) loginshell = check needchangeshell $
|
|
|
|
cmdProperty "chsh" ["--shell", loginshell, u]
|
|
|
|
`describe` (u ++ " has login shell " ++ loginshell)
|
2015-04-20 01:00:21 +00:00
|
|
|
where
|
|
|
|
needchangeshell = do
|
2015-04-22 17:04:39 +00:00
|
|
|
currshell <- userShell <$> getUserEntryForName u
|
2015-04-20 01:00:21 +00:00
|
|
|
return (currshell /= loginshell)
|
|
|
|
|
|
|
|
-- | Ensures that /etc/shells contains a shell.
|
|
|
|
shellEnabled :: FilePath -> Property NoInfo
|
|
|
|
shellEnabled loginshell = "/etc/shells" `File.containsLine` loginshell
|