]> www.average.org Git - pdns-pipe-nmc.git/blob - PowerDns.hs
accept request type in command-line args; handle wrong request type propoerly
[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: " ++ e
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 = case edom of
103   Left  err ->
104     pdnsReport $ err ++ " in a " ++ (show rrtype) ++ "query for " ++ name
105   Right dom ->
106     case rrtype of
107       RRTypeANY -> foldr (\x a -> (formatRR ver id name dom x) ++ a) "END\n"
108           [RRTypeSRV, RRTypeA, RRTypeAAAA, RRTypeCNAME, RRTypeDNAME,
109            RRTypeRP, RRTypeLOC, RRTypeNS, RRTypeDS, RRTypeMX]
110       _         -> (formatRR ver id name dom rrtype) ++ "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 formatRR ver id name dom rrtype =
117   foldr (\x a -> "DATA\t" ++ v3ext ++ name ++ "\tIN\t" ++ (show rrtype)
118             ++ "\t" ++ ttl ++ "\t" ++ (show id) ++ "\t" ++ x ++ "\n" ++ a)
119         "" $ dataRR rrtype name dom
120     where
121       v3ext = case ver of
122         3 -> "0\t1\t"
123         _ -> ""
124       ttl = show 3600
125
126 justl accessor _ dom = case accessor dom of
127   Nothing -> []
128   Just xs -> xs
129
130 justv accessor _ dom = case accessor dom of
131   Nothing -> []
132   Just x  -> [x]
133
134 dotmail addr =
135   let (aname, adom) = break (== '@') addr
136   in case adom of
137     "" -> aname ++ "."
138     _  -> aname ++ "." ++ (tail adom) ++ "."
139
140 dataRR RRTypeSRV   = justl domSrv
141 dataRR RRTypeMX    = justl domMx
142 dataRR RRTypeA     = justl domIp
143 dataRR RRTypeAAAA  = justl domIp6
144 dataRR RRTypeCNAME = justv domAlias
145 dataRR RRTypeDNAME = justv domTranslate
146 dataRR RRTypeSOA   = \ name dom -> -- FIXME make realistic version field
147   let
148     ns = case domNs dom of
149       Just (x:_) -> x           -- FIXME Terminate with a dot?
150       _          -> "."
151     email = case domEmail dom of
152       Nothing   -> "hostmaster." ++ name ++ "."
153       Just addr -> dotmail addr
154   in
155     if dom == emptyNmcDom then []
156     else
157     -- Follows a relatively ugly hack to figure if we are at the top
158     -- level domain ("something.bit"). Only in such case we provide
159     -- the synthetic SOA RR. Otherwise yield empty.
160     -- Alternative would be to carry "top-ness" as a parameter through
161     -- all the calls from the very top where we split the fqdn.
162       case splitOn (pack ".") (pack name) of
163         [_,_] -> [ns ++ " " ++ email ++ " 99999 10800 3600 604800 86400"]
164         _     -> []
165 dataRR RRTypeRP    = \ _ dom ->
166   case domEmail dom of
167     Nothing   -> []
168     Just addr -> [(dotmail addr) ++ " ."]
169 dataRR RRTypeLOC   = justv domLoc
170 dataRR RRTypeNS    = justl domNs -- FIXME Terminate with a dot?
171 dataRR RRTypeDS    = \ _ dom ->
172   case domDs dom of
173     Nothing  -> []
174     Just dss -> map dsStr dss
175       where
176         dsStr x = (show (dsKeyTag x)) ++ " "
177                ++ (show (dsAlgo x)) ++ " "
178                ++ (show (dsHashType x)) ++ " "
179                ++ (dsHashValue x)
180 -- This only comes into play when data arrived _not_ from a PDNS request:
181 dataRR (RRTypeError e) = \ _ _ ->
182   ["; No data for bad request type " ++ e]