]> www.average.org Git - pdns-pipe-nmc.git/blob - PowerDns.hs
amend README
[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 -> String -> RRType -> Either String NmcDom -> String
72 pdnsOut ver id name rrtype edom =
73   case edom of
74     Left  err -> pdnsReport err
75     Right dom -> foldr addLine "END\n" $ nmc2pdns name rrtype dom
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 nmc2pdns :: String -> RRType -> NmcDom -> [(String, String, String)]
86 nmc2pdns name RRTypeANY   dom =
87   foldr (\r accum -> (nmc2pdns name r dom) ++ accum) []
88     [RRTypeA, RRTypeAAAA, RRTypeCNAME, RRTypeDNAME, -- no SRV here!
89      RRTypeSOA, RRTypeRP, RRTypeLOC, RRTypeNS, RRTypeDS]
90 nmc2pdns name RRTypeSRV   dom = [] -- FIXME
91 nmc2pdns name RRTypeA     dom = mapto name "A" $ domIp dom
92 nmc2pdns name RRTypeAAAA  dom = mapto name "AAAA" $ domIp6 dom
93 nmc2pdns name RRTypeCNAME dom = takejust name "CNAME" $ domAlias dom
94 nmc2pdns name RRTypeDNAME dom = takejust name "DNAME" $ domTranslate dom
95 nmc2pdns name RRTypeSOA   dom =
96   if dom == emptyNmcDom then []
97   else
98     let
99       email = case domEmail dom of
100         Nothing   -> "hostmaster." ++ name
101         Just addr ->
102           let (aname, adom) = break (== '@') addr
103           in case adom of
104             "" -> aname
105             _  -> aname ++ "." ++ (tail adom)
106     in [(name, "SOA", email ++ " 99999999 10800 3600 604800 86400")]
107 nmc2pdns name RRTypeRP    dom = [] --FIXME
108 nmc2pdns name RRTypeLOC   dom = takejust name "LOC" $ domLoc dom
109 nmc2pdns name RRTypeNS    dom = mapto name "NS" $ domNs dom
110 nmc2pdns name RRTypeDS    dom = [] --FIXME
111
112 mapto name rrstr maybel = case maybel of
113   Nothing  -> []
114   Just l   -> map (\x -> (name, rrstr, x)) l
115
116 takejust name rrstr maybestr = case maybestr of
117   Nothing  -> []
118   Just str -> [(name, rrstr, str)]