]> www.average.org Git - pdns-pipe-nmc.git/blob - PowerDns.hs
ece3ac79a5dadb4d96469bfcbb77ea2d776afdcc
[pdns-pipe-nmc.git] / PowerDns.hs
1 module PowerDns ( RRType(..)
2                 , rrType
3                 , PdnsRequest(..)
4                 , pdnsParse
5                 , pdnsReport
6                 , pdnsOutQ
7                 , pdnsOutXfr
8                 ) where
9
10 import Data.Text.Lazy (splitOn, pack)
11 import Data.Map.Lazy (foldrWithKey)
12 import Data.Default.Class (def)
13
14 import NmcDom
15
16 data RRType = RRTypeSRV   | RRTypeA   | RRTypeAAAA | RRTypeCNAME
17             | RRTypeDNAME | RRTypeSOA | RRTypeRP   | RRTypeLOC
18             | RRTypeNS    | RRTypeDS  | RRTypeMX
19             | RRTypeANY   | RRTypeError String
20
21 instance Show RRType where
22   show RRTypeSRV       = "SRV"
23   show RRTypeA         = "A"
24   show RRTypeAAAA      = "AAAA"
25   show RRTypeCNAME     = "CNAME"
26   show RRTypeDNAME     = "DNAME"
27   show RRTypeSOA       = "SOA"
28   show RRTypeRP        = "RP"
29   show RRTypeLOC       = "LOC"
30   show RRTypeNS        = "NS"
31   show RRTypeDS        = "DS"
32   show RRTypeMX        = "MX"
33   show RRTypeANY       = "ANY"
34   show (RRTypeError s) = "Unknown RR type: " ++ (show s)
35
36 rrType qt = case qt of
37   "SRV"     -> RRTypeSRV
38   "A"       -> RRTypeA
39   "AAAA"    -> RRTypeAAAA
40   "CNAME"   -> RRTypeCNAME
41   "DNAME"   -> RRTypeDNAME
42   "SOA"     -> RRTypeSOA
43   "RP"      -> RRTypeRP
44   "LOC"     -> RRTypeLOC
45   "NS"      -> RRTypeNS
46   "DS"      -> RRTypeDS
47   "MX"      -> RRTypeMX
48   "ANY"     -> RRTypeANY
49   _         -> RRTypeError qt
50
51 data PdnsRequest = PdnsRequestQ
52                    { qName              :: String
53                    , qType              :: RRType
54                    , iD                 :: Int
55                    , remoteIpAddress    :: String
56                    , localIpAddress     :: Maybe String
57                    , ednsSubnetAddress  :: Maybe String
58                    }
59                  | PdnsRequestAXFR Int
60                  | PdnsRequestPing
61         deriving (Show)
62
63 -- | Parse request string read from the core PowerDNS process
64 pdnsParse :: Int -> String -> Either String PdnsRequest
65 pdnsParse ver s =
66   let
67     getInt s = case reads s :: [(Int, String)] of
68       [(x, _)] -> x
69       _        -> (-1)
70     getLIp ver xs
71       | ver >= 2  = case xs of
72                       x:_       -> Just x
73                       _         -> Nothing
74       | otherwise = Nothing
75     getRIp ver xs
76       | ver >= 3  = case xs of
77                       _:x:_     -> Just x
78                       _         -> Nothing
79       | otherwise = Nothing
80   in
81     case words s of
82       "PING":[]                 -> Right PdnsRequestPing
83       "AXFR":x:[]               -> Right (PdnsRequestAXFR (getInt x))
84       "Q":qn:"IN":qt:id:rip:xs  -> case rrType qt of
85                                      RRTypeError e ->
86                                        Left $ "Unrecognized RR type: " ++ e
87                                      rt ->
88                                        Right (PdnsRequestQ
89                                             { qName = qn
90                                             , qType = rrType qt
91                                             , iD = getInt id
92                                             , remoteIpAddress = rip
93                                             , localIpAddress = getLIp ver xs
94                                             , ednsSubnetAddress = getRIp ver xs
95                                             })
96       _                         -> Left $ "Unparseable PDNS Request: " ++ s
97
98 -- | Produce LOG entry followed by FAIL
99 pdnsReport :: String -> String
100 pdnsReport err = "LOG\tError: " ++ err ++ "\nFAIL\n"
101
102 -- | Produce answer to the Q request
103 pdnsOutQ :: Int -> Int -> Int -> String -> RRType -> Either String NmcDom -> String
104 pdnsOutQ ver id gen name rrt edom =
105   let
106     rrl = case rrt of
107       RRTypeANY -> [ RRTypeSRV, RRTypeA, RRTypeAAAA, RRTypeCNAME
108                    , RRTypeDNAME, RRTypeRP, RRTypeLOC, RRTypeNS
109                    , RRTypeDS, RRTypeMX -- SOA not included
110                    ]
111       x         -> [x]
112   in
113     case edom of
114       Left  err ->
115         pdnsReport $ err ++ " in the " ++ (show rrt) ++ " query for " ++ name
116       Right dom ->
117         formatDom ver id gen rrl name dom "END\n"
118
119 -- | Produce answer to the AXFR request
120 pdnsOutXfr :: Int -> Int -> Int -> String -> Either String NmcDom -> String
121 pdnsOutXfr ver id gen name edom =
122   let
123     allrrs = [ RRTypeSRV, RRTypeA, RRTypeAAAA, RRTypeCNAME
124              , RRTypeDNAME, RRTypeRP, RRTypeLOC, RRTypeNS
125              , RRTypeDS, RRTypeMX, RRTypeSOA
126              ]
127     walkDom f acc name dom =
128       f name dom $ case domMap dom of
129         Nothing -> acc
130         Just dm ->
131           foldrWithKey (\n d a -> walkDom f a (n ++ "." ++ name) d) acc dm
132   in
133     case edom of
134       Left  err ->
135         pdnsReport $ err ++ " in the AXFR request for " ++ name
136       Right dom ->
137         walkDom (formatDom ver id gen allrrs) "END\n" name dom
138
139 formatDom ver id gen rrl name dom acc =
140   foldr (\x a -> (formatRR ver id gen name dom x) ++ a) acc rrl
141
142 formatRR ver id gen name dom rrtype =
143   foldr (\x a -> "DATA\t" ++ v3ext ++ name ++ "\tIN\t" ++ (show rrtype)
144               ++ "\t" ++ ttl ++ "\t" ++ (show id) ++ "\t" ++ x ++ "\n" ++ a)
145         "" $ dataRR rrtype gen name dom
146     where
147       v3ext = case ver of
148         3 -> "0\t1\t"
149         _ -> ""
150       ttl = show 3600
151
152 justl accessor _ _ dom = case accessor dom of
153   Nothing -> []
154   Just xs -> xs
155
156 justv accessor _ _ dom = case accessor dom of
157   Nothing -> []
158   Just x  -> [x]
159
160 dotmail addr =
161   let (aname, adom) = break (== '@') addr
162   in case adom of
163     "" -> aname ++ "."
164     _  -> aname ++ "." ++ (tail adom) ++ "."
165
166 dataRR RRTypeSRV   = justl domSrv
167 dataRR RRTypeMX    = justl domMx
168 dataRR RRTypeA     = justl domIp
169 dataRR RRTypeAAAA  = justl domIp6
170 dataRR RRTypeCNAME = justv domAlias
171 dataRR RRTypeDNAME = justv domTranslate
172 dataRR RRTypeSOA   = \ gen name dom ->
173   let
174     ns = case domNs dom of
175       Just (x:_) -> x
176       _          -> "."
177     email = case domEmail dom of
178       Nothing   -> "hostmaster." ++ name ++ "."
179       Just addr -> dotmail addr
180   in
181     if dom == def then []
182     else
183     -- Follows a relatively ugly hack to figure if we are at the top
184     -- level domain ("something.bit"). Only in such case we provide
185     -- the synthetic SOA RR. Otherwise yield empty.
186     -- Alternative would be to carry "top-ness" as a parameter through
187     -- all the calls from the very top where we split the fqdn.
188       case splitOn (pack ".") (pack name) of
189         [_,_] -> [ns ++ " " ++ email ++ " " ++ (show gen)
190                      ++ " 10800 3600 604800 86400"]
191         _     -> []
192 dataRR RRTypeRP    = \ _ _ dom ->
193   case domEmail dom of
194     Nothing   -> []
195     Just addr -> [(dotmail addr) ++ " ."]
196 dataRR RRTypeLOC   = justv domLoc
197 dataRR RRTypeNS    = justl domNs
198 dataRR RRTypeDS    = \ _ _ dom ->
199   case domDs dom of
200     Nothing  -> []
201     Just dss -> map dsStr dss
202       where
203         dsStr x = (show (dsKeyTag x)) ++ " "
204                ++ (show (dsAlgo x)) ++ " "
205                ++ (show (dsHashType x)) ++ " "
206                ++ (dsHashValue x)
207 -- This only comes into play when data arrived _not_ from a PDNS request:
208 dataRR (RRTypeError e) = \ _ _ _ ->
209   ["; No data for bad request type " ++ e]