89d2cd56d1a2aeaca07420d403e2e29da1e7abfe
[haeredes.git] / src / Main.hs
1 module Main (main)
2 where
3
4 import Control.Concurrent.ParallelIO.Global (
5 parallelInterleaved,
6 stopGlobalPool )
7 import Control.Monad (unless)
8 import qualified Data.ByteString.Char8 as BS (
9 getContents,
10 pack,
11 words )
12 import Data.List ( (\\), intersperse )
13 import Network.DNS (
14 Domain,
15 FileOrNumericHost(RCHostName),
16 ResolvConf(resolvInfo, resolvTimeout),
17 defaultResolvConf,
18 makeResolvSeed,
19 normalize,
20 normalizeCase,
21 withResolver )
22 import System.Exit (ExitCode(ExitFailure), exitWith)
23 import System.IO (hPutStrLn, stderr)
24
25 import CommandLine (
26 Args(NS,MX,delegates,no_append_root,server,timeout),
27 get_args)
28 import DNS (
29 LookupResult,
30 lookupMX',
31 lookupNS',
32 resolve_address )
33 import ExitCodes (exit_bad_server)
34 import Timeout (Timeout(seconds))
35
36
37 -- | Given a list of delegates, report results for this
38 -- 'LookupResult'.
39 --
40 -- If there's an empty list in the second component, there were no
41 -- query results, so we report that the domain was not delegated. If
42 -- there were some results and there are leftovers (after removing
43 -- the delegates), we report those as well.
44 --
45 -- Before processing, all names are normalized using the supplied
46 -- function @normalize_function@. Ideally this should be the same
47 -- function applied to the user-input names.
48 --
49 report :: [Domain] -- ^ The list of @delgts@
50 -> (Domain -> Domain) -- ^ Domain name normalization function,
51 -- @normalize_function@.
52 -> LookupResult
53 -> IO ()
54
55 -- If the lookup resulted in a DNS error, we just ignore the whole
56 -- thing.
57 report _ _ (_, Left _) = return ()
58
59 -- If the lookup succeeded but there were no results, report that the
60 -- domain is not delegated.
61 report _ _ (d, Right []) =
62 putStrLn $ "Domain " ++ (show d) ++ " not delegated."
63
64 -- Otherwise, subtract our delegates from the list of results and
65 -- report the leftovers.
66 report delgts normalize_function (d, Right raw_hosts) = do
67 let nrml_hosts = map normalize_function raw_hosts
68 let leftovers = nrml_hosts \\ delgts
69 unless (null leftovers) $
70 putStrLn $ "Domain " ++
71 (show d) ++
72 " delegates somewhere else: " ++
73 (join " " (map show leftovers))
74 where
75 -- Create one big string by joining together a list of smaller
76 -- strings and placing a delimiter between them.
77 join :: String -> [String] -> String
78 join delimiter strings = concat (intersperse delimiter strings)
79
80
81 main :: IO ()
82 main = do
83 cfg <- get_args
84
85 -- This reads stdin.
86 input <- BS.getContents
87
88 -- Split the input on any whitespace characters.
89 let raw_domains = BS.words input
90
91 -- Convert these to ByteStrings.
92 let raw_delegates = map BS.pack (delegates cfg)
93
94 let normalize_function =
95 if (no_append_root cfg)
96 then normalizeCase
97 else normalize
98
99 -- Normalize the given names and delegates
100 let nrml_domains = map normalize_function raw_domains
101 let nrml_delegates = map normalize_function raw_delegates
102
103 rc' <- case (server cfg) of
104 Nothing -> return defaultResolvConf
105 Just s -> do
106 s' <- resolve_address s
107 case s' of
108 Left err -> do
109 let errmsg = show err
110 hPutStrLn stderr ("Bad DNS server or lookup error: " ++ errmsg)
111 exitWith (ExitFailure exit_bad_server)
112 Right [] -> do
113 hPutStrLn stderr ("Hostname " ++ s ++ " has no 'A' records.")
114 exitWith (ExitFailure exit_bad_server)
115 Right (srv:_) ->
116 return $ defaultResolvConf { resolvInfo =
117 RCHostName (show srv) }
118
119 -- Set the timeout from the command line. The resolvTimeout field is
120 -- in microseconds, so we multiply by one million.
121 let a_million = 1000 * 1000 :: Int
122 let rc = rc' { resolvTimeout = a_million * seconds (timeout cfg) }
123 rs <- makeResolvSeed rc
124
125 let lookup_function = case cfg of
126 NS{} -> lookupNS'
127 MX{} -> lookupMX'
128
129 let lookup' d = withResolver rs $ \resolver ->
130 lookup_function resolver d
131
132 -- Construct a list of [IO whatever]. The withResolver calls
133 -- are the ones that should be run in parallel.
134 let actions = map lookup' nrml_domains
135
136 -- Run the lookup actions in parallel.
137 results <- parallelInterleaved actions
138
139 -- Output the results.
140 _ <- mapM (report nrml_delegates normalize_function) results
141
142 stopGlobalPool