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