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