2014-04-14 19:35:29 +00:00
|
|
|
module Propellor.Property.Postfix where
|
|
|
|
|
|
|
|
import Propellor
|
|
|
|
import qualified Propellor.Property.Apt as Apt
|
2014-07-18 02:20:16 +00:00
|
|
|
import Propellor.Property.File
|
2014-07-18 05:03:05 +00:00
|
|
|
import qualified Propellor.Property.Service as Service
|
2014-07-18 02:20:16 +00:00
|
|
|
|
|
|
|
import qualified Data.Map as M
|
|
|
|
import Data.List
|
2014-07-18 02:23:49 +00:00
|
|
|
import Data.Char
|
2014-04-14 19:35:29 +00:00
|
|
|
|
|
|
|
installed :: Property
|
|
|
|
installed = Apt.serviceInstalledRunning "postfix"
|
|
|
|
|
2014-07-18 05:03:05 +00:00
|
|
|
restarted :: Property
|
|
|
|
restarted = Service.restarted "postfix"
|
|
|
|
|
2014-07-18 06:08:13 +00:00
|
|
|
reloaded :: Property
|
|
|
|
reloaded = Service.reloaded "postfix"
|
|
|
|
|
2014-04-14 19:35:29 +00:00
|
|
|
-- | Configures postfix as a satellite system, which
|
2014-07-18 17:03:13 +00:00
|
|
|
-- relays all mail through a relay host, which defaults to smtp.domain.
|
2014-04-14 19:35:29 +00:00
|
|
|
--
|
|
|
|
-- The smarthost may refuse to relay mail on to other domains, without
|
|
|
|
-- futher coniguration/keys. But this should be enough to get cron job
|
|
|
|
-- mail flowing to a place where it will be seen.
|
|
|
|
satellite :: Property
|
2014-07-18 18:36:51 +00:00
|
|
|
satellite = check (not <$> mainCfIsSet "relayhost") setup
|
2014-07-18 17:03:13 +00:00
|
|
|
`requires` installed
|
2014-04-14 19:42:26 +00:00
|
|
|
where
|
2014-04-18 07:59:06 +00:00
|
|
|
setup = trivial $ property "postfix satellite system" $ do
|
2014-06-01 00:48:23 +00:00
|
|
|
hn <- asks hostName
|
2014-07-18 17:03:13 +00:00
|
|
|
let (_, domain) = separate (== '.') hn
|
2014-07-18 17:09:55 +00:00
|
|
|
ensureProperties
|
|
|
|
[ Apt.reConfigure "postfix"
|
|
|
|
[ ("postfix/main_mailer_type", "select", "Satellite system")
|
|
|
|
, ("postfix/root_address", "string", "root")
|
|
|
|
, ("postfix/destinations", "string", " ")
|
|
|
|
, ("postfix/mailname", "string", hn)
|
|
|
|
]
|
2014-07-18 18:33:30 +00:00
|
|
|
, mainCf ("relayhost", domain)
|
2014-07-18 17:09:55 +00:00
|
|
|
`onChange` reloaded
|
2014-04-14 19:42:26 +00:00
|
|
|
]
|
2014-07-18 01:16:03 +00:00
|
|
|
|
|
|
|
-- | Sets up a file by running a property (which the filename is passed
|
|
|
|
-- to). If the setup property makes a change, postmap will be run on the
|
|
|
|
-- file, and postfix will be reloaded.
|
|
|
|
mappedFile :: FilePath -> (FilePath -> Property) -> Property
|
|
|
|
mappedFile f setup = setup f
|
2014-07-18 02:20:16 +00:00
|
|
|
`onChange` cmdProperty "postmap" [f]
|
|
|
|
|
2014-07-18 05:03:05 +00:00
|
|
|
-- | Run newaliases command, which should be done after changing
|
|
|
|
-- /etc/aliases.
|
|
|
|
newaliases :: Property
|
|
|
|
newaliases = trivial $ cmdProperty "newaliases" []
|
|
|
|
|
2014-07-18 18:33:30 +00:00
|
|
|
-- | The main config file for postfix.
|
|
|
|
mainCfFile :: FilePath
|
|
|
|
mainCfFile = "/etc/postfix/main.cf"
|
|
|
|
|
|
|
|
-- | Sets a main.cf name=value pair. Does not reload postfix immediately.
|
|
|
|
mainCf :: (String, String) -> Property
|
|
|
|
mainCf (name, value) = check notset set
|
|
|
|
`describe` ("postfix main.cf " ++ setting)
|
|
|
|
where
|
|
|
|
setting = name ++ "=" ++ value
|
|
|
|
notset = (/= Just value) <$> getMainCf name
|
|
|
|
set = cmdProperty "postconf" ["-e", setting]
|
|
|
|
|
|
|
|
-- | Gets a man.cf setting.
|
|
|
|
getMainCf :: String -> IO (Maybe String)
|
|
|
|
getMainCf name = parse . lines <$> readProcess "postconf" [name]
|
|
|
|
where
|
|
|
|
parse (l:_) = Just $
|
|
|
|
case separate (== '=') l of
|
|
|
|
(_, (' ':v)) -> v
|
|
|
|
(_, v) -> v
|
|
|
|
parse [] = Nothing
|
|
|
|
|
2014-07-18 18:36:51 +00:00
|
|
|
-- | Checks if a main.cf field is set. A field that is set to ""
|
|
|
|
-- is considered not set.
|
|
|
|
mainCfIsSet :: String -> IO Bool
|
|
|
|
mainCfIsSet name = do
|
|
|
|
v <- getMainCf name
|
|
|
|
return $ v == Nothing || v == Just ""
|
|
|
|
|
2014-07-18 02:20:16 +00:00
|
|
|
-- | Parses main.cf, and removes any initial configuration lines that are
|
|
|
|
-- overridden to other values later in the file.
|
|
|
|
--
|
|
|
|
-- For example, to add some settings, removing any old settings:
|
|
|
|
--
|
|
|
|
-- > mainCf `File.containsLines`
|
|
|
|
-- > [ "# I like bars."
|
|
|
|
-- > , "foo = bar"
|
|
|
|
-- > ] `onChange` dedupMainCf
|
|
|
|
--
|
|
|
|
-- Note that multiline configurations that continue onto the next line
|
|
|
|
-- are not currently supported.
|
|
|
|
dedupMainCf :: Property
|
2014-07-18 18:33:30 +00:00
|
|
|
dedupMainCf = fileProperty "postfix main.cf dedupped" dedupCf mainCfFile
|
2014-07-18 03:41:17 +00:00
|
|
|
|
|
|
|
dedupCf :: [String] -> [String]
|
|
|
|
dedupCf ls =
|
|
|
|
let parsed = map parse ls
|
|
|
|
in dedup [] (keycounts $ rights parsed) parsed
|
|
|
|
where
|
2014-07-18 02:20:16 +00:00
|
|
|
parse l
|
|
|
|
| "#" `isPrefixOf` l = Left l
|
2014-07-18 02:23:49 +00:00
|
|
|
| "=" `isInfixOf` l =
|
|
|
|
let (k, v) = separate (== '=') l
|
|
|
|
in Right ((filter (not . isSpace) k), v)
|
2014-07-18 02:20:16 +00:00
|
|
|
| otherwise = Left l
|
2014-07-18 03:43:33 +00:00
|
|
|
fmt k v = k ++ " =" ++ v
|
2014-07-18 02:20:16 +00:00
|
|
|
|
|
|
|
keycounts = M.fromListWith (+) . map (\(k, _v) -> (k, (1 :: Integer)))
|
|
|
|
|
|
|
|
dedup c _ [] = reverse c
|
|
|
|
dedup c kc ((Left v):rest) = dedup (v:c) kc rest
|
|
|
|
dedup c kc ((Right (k, v)):rest) = case M.lookup k kc of
|
|
|
|
Just n | n > 1 -> dedup c (M.insert k (n - 1) kc) rest
|
|
|
|
_ -> dedup (fmt k v:c) kc rest
|