]> www.average.org Git - pdns-pipe-nmc.git/blob - PowerDns.hs
76bcba772d23ff6d0f27e64bda1a18a9684a0826
[pdns-pipe-nmc.git] / PowerDns.hs
1 module PowerDns ( RRType(..)
2                 , PdnsRequest(..)
3                 , pdnsParse
4                 , pdnsReport
5                 , pdnsOut
6                 ) where
7
8 import NmcDom
9
10 data RRType = RRTypeSRV   | RRTypeA   | RRTypeAAAA | RRTypeCNAME
11             | RRTypeDNAME | RRTypeSOA | RRTypeRP   | RRTypeLOC
12             | RRTypeNS    | RRTypeDS  | RRTypeMX
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       "MX"      -> RRTypeMX
42       "ANY"     -> RRTypeANY
43       _         -> RRTypeError qt
44     getLIp ver xs
45       | ver >= 2  = case xs of
46                       x:_       -> Just x
47                       _         -> Nothing
48       | otherwise = Nothing
49     getRIp ver xs
50       | ver >= 3  = case xs of
51                       _:x:_     -> Just x
52                       _         -> Nothing
53       | otherwise = Nothing
54   in
55     case words s of
56       "PING":[]                 -> Right PdnsRequestPing
57       "AXFR":x:[]               -> Right (PdnsRequestAXFR x)
58       "Q":qn:"IN":qt:id:rip:xs  -> Right (PdnsRequestQ
59                                             { qName = qn
60                                             , qType = getQt qt
61                                             , iD = id
62                                             , remoteIpAddress = rip
63                                             , localIpAddress = getLIp ver xs
64                                             , ednsSubnetAddress = getRIp ver xs
65                                             })
66       _                         -> Left $ "Unparseable PDNS Request: " ++ s
67
68 pdnsReport :: String -> String
69 pdnsReport err =
70   "LOG\tError: " ++ err ++ "\nFAIL\n"
71
72 pdnsOut :: Int -> String -> String -> RRType -> Either String NmcDom -> String
73 pdnsOut ver id name rrtype edom = case edom of
74   Left  err -> pdnsReport $ err ++ " in a query for " ++ name
75   Right dom -> foldr addLine "END\n" $ n2p rrtype
76     where
77       addLine (nm, ty, dt) accum =
78         "DATA\t" ++ v3ext ++ nm ++ "\tIN\t" ++ ty ++ "\t" ++ ttl ++
79             "\t" ++ id ++ "\t" ++ dt ++ "\n" ++ accum
80       v3ext = case ver of
81         3 -> "0\t1\t"
82         _ -> ""
83       ttl = show 3600
84
85       n2p RRTypeANY   =
86         foldr (\r accum -> (n2p r) ++ accum) []
87           [RRTypeSRV, RRTypeA, RRTypeAAAA, RRTypeCNAME, RRTypeDNAME,
88            RRTypeRP, RRTypeLOC, RRTypeNS, RRTypeDS, RRTypeMX]
89       n2p RRTypeSRV   = mapto    "SRV"   $ domSrv dom
90       n2p RRTypeMX    = mapto    "MX"    $ domMx dom
91       n2p RRTypeA     = mapto    "A"     $ domIp dom
92       n2p RRTypeAAAA  = mapto    "AAAA"  $ domIp6 dom
93       n2p RRTypeCNAME = takejust "CNAME" $ domAlias dom
94       n2p RRTypeDNAME = takejust "DNAME" $ domTranslate dom
95       n2p RRTypeSOA   = -- FIXME generate only for top domain
96                         -- FIXME make realistic version field
97                         -- FIXME make realistic nameserver field
98         if dom == emptyNmcDom then []
99         else [(name, "SOA", "ns " ++ email ++ " 99999 10800 3600 604800 86400")]
100           where
101             email = case domEmail dom of
102               Nothing   -> "hostmaster." ++ name
103               Just addr -> dotmail addr
104       n2p RRTypeRP    = case domEmail dom of
105         Nothing   -> []
106         Just addr -> [(name, "RP", (dotmail addr) ++ " .")]
107       n2p RRTypeLOC   = takejust "LOC"  $ domLoc dom
108       n2p RRTypeNS    = mapto    "NS"   $ domNs dom
109       n2p RRTypeDS    = case domDs dom of
110         Nothing  -> []
111         Just dss -> map (\x -> (name, "DS", dsStr x)) dss
112           where
113             dsStr x = (show (dsKeyTag x)) ++ " "
114                    ++ (show (dsAlgo x)) ++ " "
115                    ++ (show (dsHashType x)) ++ " "
116                    ++ (dsHashValue x)
117
118       dotmail addr = 
119         let (aname, adom) = break (== '@') addr
120         in case adom of
121           "" -> aname
122           _  -> aname ++ "." ++ (tail adom)
123       
124       mapto    rrstr maybel   = case maybel of
125         Nothing  -> []
126         Just l   -> map (\x -> (name, rrstr, x)) l
127       takejust rrstr maybestr = case maybestr of
128         Nothing  -> []
129         Just str -> [(name, rrstr, str)]