]> www.average.org Git - pdns-pipe-nmc.git/blob - PowerDns.hs
Revert "wip TLSA"
[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 domMap 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   = justl domSrv
169 dataRR RRTypeMX    = justl domMx
170 dataRR RRTypeTLSA  = justl domTlsa
171 dataRR RRTypeA     = justl domIp
172 dataRR RRTypeAAAA  = justl domIp6
173 dataRR RRTypeCNAME = justv domAlias
174 dataRR RRTypeDNAME = justv domTranslate
175 dataRR RRTypeSOA   = \ gen name dom ->
176   let
177     ns = case domNs dom of
178       Just (x:_) -> x
179       _          -> "."
180     email = case domEmail dom of
181       Nothing   -> "hostmaster." ++ name ++ "."
182       Just addr -> dotmail addr
183   in
184     if dom == def then []
185     else
186     -- Follows a relatively ugly hack to figure if we are at the top
187     -- level domain ("something.bit"). Only in such case we provide
188     -- the synthetic SOA RR. Otherwise yield empty.
189     -- Alternative would be to carry "top-ness" as a parameter through
190     -- all the calls from the very top where we split the fqdn.
191       case splitOn (pack ".") (pack name) of
192         [_,_] -> [ns ++ " " ++ email ++ " " ++ (show gen)
193                      ++ " 10800 3600 604800 86400"]
194         _     -> []
195 dataRR RRTypeRP    = \ _ _ dom ->
196   case domEmail dom of
197     Nothing   -> []
198     Just addr -> [(dotmail addr) ++ " ."]
199 dataRR RRTypeLOC   = justv domLoc
200 dataRR RRTypeNS    = justl domNs
201 dataRR RRTypeDS    = \ _ _ dom ->
202   case domDs dom of
203     Nothing  -> []
204     Just dss -> map dsStr dss
205       where
206         dsStr x = (show (dsKeyTag x)) ++ " "
207                ++ (show (dsAlgo x)) ++ " "
208                ++ (show (dsHashType x)) ++ " "
209                ++ (dsHashValue x)
210 -- This only comes into play when data arrived _not_ from a PDNS request:
211 dataRR (RRTypeError e) = \ _ _ _ ->
212   ["; No data for bad request type " ++ e]