]> www.average.org Git - pdns-pipe-nmc.git/blob - PowerDns.hs
next take on version-dependent build
[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
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:[]               -> Right (PdnsRequestAXFR (getInt x))
86       "Q":qn:"IN":qt:id:rip:xs  -> case rrType qt of
87                                      RRTypeError e ->
88                                        Left $ "Unrecognized RR type: " ++ e
89                                      rt ->
90                                        Right (PdnsRequestQ
91                                             { qName = qn
92                                             , qType = rrType qt
93                                             , iD = getInt id
94                                             , remoteIpAddress = rip
95                                             , localIpAddress = getLIp ver xs
96                                             , ednsSubnetAddress = getRIp ver xs
97                                             })
98       _                         -> Left $ "Unparseable PDNS Request: " ++ s
99
100 -- | Produce LOG entry followed by FAIL
101 pdnsReport :: String -> String
102 pdnsReport err = "LOG\tError: " ++ err ++ "\nFAIL\n"
103
104 -- | Produce answer to the Q request
105 pdnsOutQ :: Int -> Int -> Int -> String -> RRType -> Either String NmcDom -> String
106 pdnsOutQ ver id gen name rrt edom =
107   let
108     rrl = case rrt of
109       RRTypeANY -> [ RRTypeSRV, RRTypeA, RRTypeAAAA, RRTypeCNAME
110                    , RRTypeDNAME, RRTypeRP, RRTypeLOC, RRTypeNS
111                    , RRTypeDS, RRTypeMX -- SOA not included
112                    ]
113       x         -> [x]
114   in
115     case edom of
116       Left  err ->
117         pdnsReport $ err ++ " in the " ++ (show rrt) ++ " query for " ++ name
118       Right dom ->
119         formatDom ver id gen rrl name dom "END\n"
120
121 -- | Produce answer to the AXFR request
122 pdnsOutXfr :: Int -> Int -> Int -> String -> Either String NmcDom -> String
123 pdnsOutXfr ver id gen name edom =
124   let
125     allrrs = [ RRTypeSRV, RRTypeA, RRTypeAAAA, RRTypeCNAME
126              , RRTypeDNAME, RRTypeRP, RRTypeLOC, RRTypeNS
127              , RRTypeDS, RRTypeMX, RRTypeSOA
128              ]
129     walkDom f acc name dom =
130       f name dom $ case domSubmap dom of
131         Nothing -> acc
132         Just dm ->
133           foldrWithKey (\n d a -> walkDom f a (n ++ "." ++ name) d) acc dm
134   in
135     case edom of
136       Left  err ->
137         pdnsReport $ err ++ " in the AXFR request for " ++ name
138       Right dom ->
139         walkDom (formatDom ver id gen allrrs) "END\n" name dom
140
141 formatDom ver id gen rrl name dom acc =
142   foldr (\x a -> (formatRR ver id gen name dom x) ++ a) acc rrl
143
144 formatRR ver id gen name dom rrtype =
145   foldr (\x a -> "DATA\t" ++ v3ext ++ name ++ "\tIN\t" ++ (show rrtype)
146               ++ "\t" ++ ttl ++ "\t" ++ (show id) ++ "\t" ++ x ++ "\n" ++ a)
147         "" $ dataRR rrtype gen name dom
148     where
149       v3ext = case ver of
150         3 -> "0\t1\t"
151         _ -> ""
152       ttl = show 3600
153
154 justl accessor _ _ dom = case accessor dom of
155   Nothing -> []
156   Just xs -> xs
157
158 justv accessor _ _ dom = case accessor dom of
159   Nothing -> []
160   Just x  -> [x]
161
162 dotmail addr =
163   let (aname, adom) = break (== '@') addr
164   in case adom of
165     "" -> aname ++ "."
166     _  -> aname ++ "." ++ (tail adom) ++ "."
167
168 dataRR RRTypeSRV   = \ _ _ dom ->
169   case domSrv dom of
170     Nothing  -> []
171     Just srvs -> map srvStr srvs
172       where
173         srvStr x = (show (srvPrio x)) ++ "\t"
174                 ++ (show (srvWeight x)) ++ " "
175                 ++ (show (srvPort x)) ++ " "
176                 ++ (srvHost x)
177     
178 dataRR RRTypeMX    = justl domMx
179 dataRR RRTypeTLSA  = \ _ _ dom ->
180   case domTlsa dom of
181     Nothing  -> []
182     Just tlsas -> map tlsaStr tlsas
183       where
184         tlsaStr x = "(3 0 "
185                  ++ (show (tlsMatchType x)) ++ " "
186                  ++ (tlsMatchValue x) ++ ")"
187         -- tlsIncSubdoms is not displayed, it is used for `propagate`.
188
189 dataRR RRTypeA     = justl domIp
190 dataRR RRTypeAAAA  = justl domIp6
191 dataRR RRTypeCNAME = justv domAlias
192 dataRR RRTypeDNAME = justv domTranslate
193 dataRR RRTypeSOA   = \ gen name dom ->
194   let
195     ns = case domNs dom of
196       Just (x:_) -> x
197       _          -> "."
198     email = case domEmail dom of
199       Nothing   -> "hostmaster." ++ name ++ "."
200       Just addr -> dotmail addr
201   in
202     if dom == def then []
203     else
204     -- Follows a relatively ugly hack to figure if we are at the top
205     -- level domain ("something.bit"). Only in such case we provide
206     -- the synthetic SOA RR. Otherwise yield empty.
207     -- Alternative would be to carry "top-ness" as a parameter through
208     -- all the calls from the very top where we split the fqdn.
209       case splitOn (pack ".") (pack name) of
210         [_,_] -> [ns ++ " " ++ email ++ " " ++ (show gen)
211                      ++ " 10800 3600 604800 86400"]
212         _     -> []
213 dataRR RRTypeRP    = \ _ _ dom ->
214   case domEmail dom of
215     Nothing   -> []
216     Just addr -> [(dotmail addr) ++ " ."]
217 dataRR RRTypeLOC   = justv domLoc
218 dataRR RRTypeNS    = justl domNs
219 dataRR RRTypeDS    = \ _ _ dom ->
220   case domDs dom of
221     Nothing  -> []
222     Just dss -> map dsStr dss
223       where
224         dsStr x = (show (dsKeyTag x)) ++ " "
225                ++ (show (dsAlgo x)) ++ " "
226                ++ (show (dsHashType x)) ++ " "
227                ++ (dsHashValue x)
228 -- This only comes into play when data arrived _not_ from a PDNS request:
229 dataRR (RRTypeError e) = \ _ _ _ ->
230   ["; No data for bad request type " ++ e]