From: Michael Orlitzky Date: Sun, 28 Jul 2013 04:23:05 +0000 (-0400) Subject: Support hostnames as --server arguments. X-Git-Tag: 0.2.0~19 X-Git-Url: https://gitweb.michael.orlitzky.com/?a=commitdiff_plain;h=e8fe3d4c76b719d256a3c04d462819cd98218111;p=haeredes.git Support hostnames as --server arguments. Add basic support for authority section results in NS mode. Add ExitCodes module. --- diff --git a/haeredes.cabal b/haeredes.cabal index 1bef536..8c6754a 100644 --- a/haeredes.cabal +++ b/haeredes.cabal @@ -72,6 +72,7 @@ executable haeredes bytestring == 0.10.*, cmdargs == 0.10.*, dns >= 0.3.7, + iproute == 1.2.*, MissingH == 1.2.*, parallel-io == 0.3.* @@ -84,6 +85,7 @@ executable haeredes other-modules: CommandLine DNS + ExitCodes ghc-options: -Wall diff --git a/src/DNS.hs b/src/DNS.hs index f649e0a..a1ed2bd 100644 --- a/src/DNS.hs +++ b/src/DNS.hs @@ -2,10 +2,11 @@ module DNS ( LookupResult, lookupMX', lookupNS', - normalize - ) + normalize, + resolve_address ) where +import Control.Applicative ((<$>)) import Control.Monad (liftM) import qualified Data.ByteString.Char8 as BS ( append, @@ -13,14 +14,61 @@ import qualified Data.ByteString.Char8 as BS ( map, pack ) import Data.Char (toLower) +import Data.IP (IPv4) import Network.DNS ( Domain, + DNSFormat(..), Resolver, + RDATA(..), + TYPE(..), + defaultResolvConf, + lookupA, lookupMX, - lookupNS ) + lookupNS, + lookupRaw, + makeResolvSeed, + rdata, + rrtype, + withResolver ) +import Text.Read (readMaybe) type LookupResult = (Domain, Maybe [Domain]) +-- | Perform a query, but take the result from the authority section +-- of the response rather than the answer section. Code shamelessly +-- stolen from Network.DNS.lookup. +lookup_authority :: Resolver -> Domain -> TYPE -> IO (Maybe [RDATA]) +lookup_authority rlv dom typ = (>>= toRDATA) <$> lookupRaw rlv dom typ + where + correct r = rrtype r == typ + listToMaybe [] = Nothing + listToMaybe xs = Just xs + toRDATA = listToMaybe . map rdata . filter correct . authority + +-- | Like lookupNS, except we take the result from the authority +-- section of the response (as opposed to the answer section). +lookupNS_authority :: Resolver -> Domain -> IO (Maybe [Domain]) +lookupNS_authority rlv dom = toNS <$> DNS.lookup_authority rlv dom NS + where + toNS = fmap (map unTag) + unTag (RD_NS dm) = dm + unTag _ = error "lookupNS_authority" + +resolve_address :: String -> IO (Maybe IPv4) +resolve_address s = + case read_result of + Just _ -> return read_result + Nothing -> do + default_rs <- makeResolvSeed defaultResolvConf + withResolver default_rs $ \resolver -> do + result <- lookupA resolver (BS.pack s) + return $ case result of + Just (x:_) -> Just x + _ -> Nothing + where + read_result :: Maybe IPv4 + read_result = readMaybe s + lookupMX' :: Resolver -> Domain -> IO LookupResult lookupMX' resolver domain = liftM (pair_em . drop_priority) $ lookupMX resolver domain @@ -33,12 +81,20 @@ lookupMX' resolver domain = -- This function keeps the domain matches with its NS records. lookupNS' :: Resolver -> Domain -> IO LookupResult -lookupNS' resolver domain = - liftM pair_em $ lookupNS resolver domain +lookupNS' resolver domain = do + answer_result <- lookupNS resolver domain + auth_result <- lookupNS_authority resolver domain + liftM pair_em $ return $ combine answer_result auth_result where pair_em :: a -> (Domain, a) pair_em = (,) domain + combine :: (Maybe [Domain]) -> (Maybe [Domain]) -> (Maybe [Domain]) + combine Nothing Nothing = Nothing + combine m1 Nothing = m1 + combine Nothing m2 = m2 + combine (Just ds1) (Just ds2) = Just (ds1 ++ ds2) + -- | Normalize the given name by lowercasing and appending a trailing -- dot (the root) if necessary. normalize :: Domain -> Domain diff --git a/src/ExitCodes.hs b/src/ExitCodes.hs new file mode 100644 index 0000000..c03be9a --- /dev/null +++ b/src/ExitCodes.hs @@ -0,0 +1,6 @@ +module ExitCodes ( + exit_bad_server ) +where + +exit_bad_server :: Int +exit_bad_server = 1 diff --git a/src/Main.hs b/src/Main.hs index 4da3833..4aa0c32 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -11,7 +11,6 @@ import qualified Data.ByteString.Char8 as BS ( words ) import Data.List ((\\)) import Data.String.Utils (join) - import Network.DNS ( Domain, FileOrNumericHost(RCHostName), @@ -19,13 +18,18 @@ import Network.DNS ( defaultResolvConf, makeResolvSeed, withResolver ) +import System.Exit (ExitCode(..), exitWith) +import System.IO (hPutStrLn, stderr) + import CommandLine (Args(..), get_args) import DNS ( LookupResult, lookupMX', lookupNS', - normalize ) + normalize, + resolve_address ) +import ExitCodes (exit_bad_server) report :: (Domain, Maybe [Domain]) -> IO () report (d, Nothing) = @@ -63,9 +67,17 @@ main = do let nrml_domains = map normalize raw_domains let nrml_delegates = map normalize raw_delegates - let rc = case (server cfg) of - Nothing -> defaultResolvConf - Just s -> defaultResolvConf { resolvInfo = RCHostName s } + rc <- case (server cfg) of + Nothing -> return defaultResolvConf + Just s -> do + s' <- resolve_address s + case s' of + Nothing -> do + hPutStrLn stderr ("Bad DNS server or lookup error: " ++ s) + exitWith (ExitFailure exit_bad_server) + Just s'' -> + return $ defaultResolvConf { resolvInfo = + RCHostName (show s'') } rs <- makeResolvSeed rc