]> www.average.org Git - pdns-pipe-nmc.git/blob - PowerDns.hs
9a2a4987443c9d052a40257f005a04dcd1aa8343
[pdns-pipe-nmc.git] / PowerDns.hs
1 module PowerDns ( RRType(..)
2                 , PdnsRequest(..)
3                 , pdnsParse
4                 , pdnsReport
5                 , pdnsOut
6                 ) where
7
8 import NmcJson
9
10 data RRType = RRTypeSRV   | RRTypeA   | RRTypeAAAA | RRTypeCNAME
11             | RRTypeDNAME | RRTypeSOA | RRTypeRP   | RRTypeLOC
12             | RRTypeNS    | RRTypeDS
13             | RRTypeANY   | RRTypeError String 
14         deriving (Show)
15
16 data PdnsRequest = PdnsRequestQ
17                    { qName              :: String
18                    , qType              :: RRType
19                    , iD                 :: String
20                    , remoteIpAddress    :: String
21                    , localIpAddress     :: Maybe String
22                    , ednsSubnetAddress  :: Maybe String
23                    }
24                  | PdnsRequestAXFR String
25                  | PdnsRequestPing
26         deriving (Show)
27
28 pdnsParse ver s =
29   let
30     getQt qt = case qt of
31       "SRV"     -> RRTypeSRV   
32       "A"       -> RRTypeA
33       "AAAA"    -> RRTypeAAAA
34       "CNAME"   -> RRTypeCNAME
35       "DNAME"   -> RRTypeDNAME 
36       "SOA"     -> RRTypeSOA 
37       "RP"      -> RRTypeRP   
38       "LOC"     -> RRTypeLOC
39       "NS"      -> RRTypeNS    
40       "DS"      -> RRTypeDS
41       "ANY"     -> RRTypeANY
42       _         -> RRTypeError qt
43     getLIp ver xs
44       | ver >= 2  = case xs of
45                       x:_       -> Just x
46                       _         -> Nothing
47       | otherwise = Nothing
48     getRIp ver xs
49       | ver >= 3  = case xs of
50                       _:x:_     -> Just x
51                       _         -> Nothing
52       | otherwise = Nothing
53   in
54     case words s of
55       "PING":[]                 -> Right PdnsRequestPing
56       "AXFR":x:[]               -> Right (PdnsRequestAXFR x)
57       "Q":qn:"IN":qt:id:rip:xs  -> Right (PdnsRequestQ
58                                             { qName = qn
59                                             , qType = getQt qt
60                                             , iD = id
61                                             , remoteIpAddress = rip
62                                             , localIpAddress = getLIp ver xs
63                                             , ednsSubnetAddress = getRIp ver xs
64                                             })
65       _                         -> Left $ "Unparseable PDNS Request: " ++ s
66
67 pdnsReport :: String -> String
68 pdnsReport err =
69   "LOG\tError: " ++ err ++ "\nFAIL\n"
70
71 pdnsOut :: Int -> String -> RRType -> Either String NmcDom -> String
72 pdnsOut ver id rrtype edom =
73   case edom of
74     Left  err -> pdnsReport err
75     Right dom -> pdnsAmend ver id rrtype dom "END\n"
76
77 pdnsAmend :: Int -> String -> RRType -> NmcDom -> String -> String
78 pdnsAmend ver id rrtype dom accum =
79   "DATA\t" ++ (show dom) ++ "\n" ++ accum --FIXME