summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--src/Propellor/Property/SiteSpecific/SendIP.hs52
1 files changed, 52 insertions, 0 deletions
diff --git a/src/Propellor/Property/SiteSpecific/SendIP.hs b/src/Propellor/Property/SiteSpecific/SendIP.hs
new file mode 100644
index 00000000..62f5885f
--- /dev/null
+++ b/src/Propellor/Property/SiteSpecific/SendIP.hs
@@ -0,0 +1,52 @@
+module Propellor.Property.SendIP (updatesTo) where
+
+import Propellor
+import qualified Propellor.Property.Apt as Apt
+import qualified Propellor.Property.File as File
+import qualified Propellor.Property.User as User
+
+import System.Process (readProcessWithExitCode)
+
+-- | Check external IP address daily, and e-mail when it changes. Needs
+-- outgoing port 25 not to be blocked.
+updatesTo :: String -> RevertableProperty
+updatesTo recp = setup <!> disable
+ where
+ setup = property ("sending external IP updates to " ++ recp) $ do
+ _ <- ensureProperty $ Apt.installed ["heirloom-mailx", "dnsutils"]
+ home <- liftIO $ User.homedir (User "root")
+ let domain = getAddressDomain recp
+ (_, dig, _) <- liftIO $ readProcessWithExitCode "dig" ["+short", "MX", domain] ""
+ let mx = getMXFromDig dig
+ _ <- ensureProperty $ File.hasContent "/etc/cron.daily/sendip" (script home recp mx)
+ ensureProperty $ cmdProperty "chmod" ["755", "/etc/cron.daily/sendip"]
+ disable = File.notPresent "/etc/cron.daily/sendip"
+ `describe` "not sending external IP address updates"
+
+script :: String -> String -> String -> [String]
+script home recp mx =
+ [ "HOME=" ++ home
+ , "RECP=" ++ recp
+
+ -- we require the recipient's MX server because we're not assuming a
+ -- properly configured local MTA
+ , "RECPMX=" ++ mx
+
+ , "newip=$(dig +short myip.opendns.com @resolver1.opendns.com)"
+ , "oldip=$(cat $HOME/local/extip 2>/dev/null)"
+
+ , "mkdir -p $HOME/local"
+ , "if ! [ \"$newip\" = \"$oldip\" ]; then"
+ , "echo $newip > $HOME/local/extip"
+ , "echo \"\n\nMy external IP address today is ${newip}\n\nYours,\n\n$(hostname -f)\""
+ , "| mailx -r \"root@$(hostname -f)\" -s \"IP for $(hostname -f) at $(date)\""
+ , "-S smtp=\"${RECPMX}\" $RECP"
+ , "fi"
+ ]
+
+getAddressDomain :: String -> String
+getAddressDomain [] = ""
+getAddressDomain (x:xs) = if x == '@' then xs else getAddressDomain xs
+
+getMXFromDig :: String -> String
+getMXFromDig dig = unwords . drop 1 . words . firstLine $ dig