]> www.average.org Git - pdns-pipe-nmc.git/blob - PowerDns.hs
cleanup pdns response generator
[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 =
74   case edom of
75     Left  err -> pdnsReport $ err ++ " in a query for " ++ name
76     Right dom -> foldr addLine "END\n" $ nmc2pdns name rrtype dom
77       where
78         addLine (nm, ty, dt) accum =
79           "DATA\t" ++ v3ext ++ nm ++ "\tIN\t" ++ ty ++ "\t" ++ ttl ++
80               "\t" ++ id ++ "\t" ++ dt ++ "\n" ++ accum
81         v3ext = case ver of
82           3 -> "0\t1\t"
83           _ -> ""
84         ttl = show 3600
85
86 nmc2pdns :: String -> RRType -> NmcDom -> [(String, String, String)]
87 nmc2pdns name RRTypeANY   dom =
88   foldr (\r accum -> (nmc2pdns r) ++ accum) []
89     [RRTypeSRV, RRTypeA, RRTypeAAAA, RRTypeCNAME, RRTypeDNAME,
90      RRTypeSOA, RRTypeRP, RRTypeLOC, RRTypeNS, RRTypeDS, RRTypeMX]
91   where
92     nmc2pdns RRTypeSRV   = makesrv  "SRV"   $ domService dom
93     nmc2pdns RRTypeMX    = mapto    "MX"    $ domMx dom
94     nmc2pdns RRTypeA     = mapto    "A"     $ domIp dom
95     nmc2pdns RRTypeAAAA  = mapto    "AAAA"  $ domIp6 dom
96     nmc2pdns RRTypeCNAME = takejust "CNAME" $ domAlias dom
97     nmc2pdns RRTypeDNAME = takejust "DNAME" $ domTranslate dom
98     nmc2pdns RRTypeSOA   = -- FIXME generate only for top domain
99       if dom == emptyNmcDom then []
100       else
101         let
102           email = case domEmail dom of
103             Nothing   -> "hostmaster." ++ name
104             Just addr ->
105               let (aname, adom) = break (== '@') addr
106               in case adom of
107                 "" -> aname
108                 _  -> aname ++ "." ++ (tail adom)
109         in [(name, "SOA", email ++ " 99999999 10800 3600 604800 86400")]
110     nmc2pdns RRTypeRP    = [] --FIXME
111     nmc2pdns RRTypeLOC   = takejust "LOC"  $ domLoc dom
112     nmc2pdns RRTypeNS    = mapto    "NS"   $ domNs dom
113     nmc2pdns RRTypeDS    = [] --FIXME
114     
115     mapto rrstr maybel = case maybel of
116       Nothing  -> []
117       Just l   -> map (\x -> (name, rrstr, x)) l
118     
119     takejust rrstr maybestr = case maybestr of
120       Nothing  -> []
121       Just str -> [(name, rrstr, str)]
122     
123     makesrv rrstr mayberl = case mayberl of
124       Nothing  -> []
125       Just srl  -> map (\x -> (name, rrstr, fmtsrv x)) srl
126         where
127           fmtsrv rl = (show (srvPrio rl)) ++ " "
128                     ++ (show (srvWeight rl)) ++ " "
129                     ++ (show (srvPort rl)) ++ " "
130                     ++ (srvHost rl)