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