]> www.average.org Git - pdns-pipe-nmc.git/blobdiff - NmcDom.hs
wip on enforcing TLSA on subdoms
[pdns-pipe-nmc.git] / NmcDom.hs
index 98275f7d06c42c784c72551a16d12e514e62ba1c..dac614ba123e241be7c68e3b6c5bc379ff30f85f 100644 (file)
--- a/NmcDom.hs
+++ b/NmcDom.hs
@@ -9,14 +9,16 @@ module NmcDom   ( NmcDom(..)
                 ) where
 
 import Prelude hiding (length)
-import Control.Applicative ((<$>), (<*>), empty, pure)
+import Control.Applicative ((<$>), (<*>), liftA2, empty, pure)
 import Data.Char
 import Data.Text (Text, unpack)
 import Data.List (union)
 import Data.List.Split
-import Data.Vector ((!), length, singleton)
-import Data.Map (Map, unionWith)
-import qualified Data.HashMap.Strict as H (lookup)
+import Data.Vector ((!), length)
+import qualified Data.Vector as V (singleton)
+import Data.Map (Map, unionWith, foldrWithKey)
+import qualified Data.Map as M (singleton, empty, insert, insertWith)
+import qualified Data.HashMap.Strict as H (lookup, foldrWithKey)
 import Data.Aeson
 import Data.Aeson.Types
 import Data.Default.Class
@@ -31,22 +33,103 @@ import Data.Default.Class
 obj .:/ key = case H.lookup key obj of
                Nothing -> pure Nothing
                Just v  -> case v of
-                        String s -> parseJSON $ Array (singleton v)
+                        String s -> parseJSON $ Array (V.singleton v)
                         _        -> parseJSON v
 
+data IntRRService = IntRRService { isvName       :: String
+                                 , isvProto      :: String
+                                 , isvPrio       :: Int
+                                 , isvWeight     :: Int
+                                 , isvPort       :: Int
+                                 , isvHost       :: String
+                                 } deriving (Show, Eq)
+
+instance FromJSON IntRRService where
+        parseJSON (Array a) =
+                if length a == 6 then IntRRService
+                        <$> parseJSON (a ! 0)
+                        <*> parseJSON (a ! 1)
+                        <*> parseJSON (a ! 2)
+                        <*> parseJSON (a ! 3)
+                        <*> parseJSON (a ! 4)
+                        <*> parseJSON (a ! 5)
+                else empty
+        parseJSON _ = empty
+
 makeMx :: Object -> Parser (Maybe [String])
-makeMx o = return Nothing -- FIXME
-{-
+makeMx o =
   case H.lookup "service" o of
     Nothing          -> pure Nothing
-    Just (Array saa) -> return $ Just $ fmap mxStr $ filter mxMatch saa
-      where
-        mxMatch sa = (sa ! 0) == "smtp" && (sa ! 1) == "tcp" && (sa ! 4) == 25
-        mxStr sa = (sa ! 2) ++ "\t" ++ (sa ! 5)
-    _                -> empty
--}
+    Just (Array a) -> do
+      isvl <- parseJSON (Array a)
+      return $ Just $ map mxStr $ filter mxMatch isvl
+        where
+          mxMatch isv = isvName isv  == "smtp"
+                     && isvProto isv == "tcp"
+                     && isvPort isv  == 25
+          mxStr isv = (show (isvPrio isv)) ++ "\t" ++ (isvHost isv)
+    Just _ -> empty
+
 makeSubmap :: Object -> Parser (Maybe (Map String NmcDom))
-makeSubmap o = o .:? "map" -- FIXME
+makeSubmap o = takeTls o `fmerge` takeSrv o `fmerge` takeMap o
+  where fmerge = liftA2 merge
+
+takeMap :: Object -> Parser (Maybe (Map String NmcDom))
+takeMap o =
+  case H.lookup "map" o of
+    Nothing          -> pure Nothing
+    Just (Object mo) -> H.foldrWithKey addmapentry (pure (Just M.empty)) mo
+      where
+        addmapentry "" v acc = parseJSON v >>= inject acc ""
+        addmapentry k  v acc = nest (splitOn "." (unpack k)) v acc
+        nest []     v acc = empty -- does not happen as a result of splitOn
+        nest [""]   v acc = empty -- empty element of fqdn forbidden
+        nest [d]    v acc = parseJSON v >>= inject acc d
+        nest (d:ds) v acc =
+          nest ds v acc >>= (inject acc d) . (\r -> def { domSubmap = r })
+        inject acc d r = (fmap.fmap) (M.insertWith merge d r) acc
+    _ -> empty
+
+takeSrv :: Object -> Parser (Maybe (Map String NmcDom))
+takeSrv o =
+  case H.lookup "service" o of
+    Nothing          -> pure Nothing
+    Just (Array a) -> do
+      isvl <- parseJSON (Array a)
+      return $ foldr addSrv (Just M.empty) isvl
+        where
+          addSrv isv acc = subm `merge` acc
+            where
+              subm = Just (M.singleton ("_" ++ isvProto isv) sub2)
+              sub2 = def { domSubmap =
+                             Just (M.singleton ("_" ++ isvName isv) sub3) }
+              sub3 = def { domSrv = Just [ NmcRRSrv (isvPrio isv)
+                                                    (isvWeight isv)
+                                                    (isvPort isv)
+                                                    (isvHost isv) ] }
+    Just _ -> empty
+
+-- takeTls is almost, but not quite, entirely unlike takeSrv
+takeTls :: Object -> Parser (Maybe (Map String NmcDom))
+takeTls o =
+  case H.lookup "tls" o of
+    Nothing         -> pure Nothing
+    Just (Object t) ->
+      (parseJSON (Object t) :: Parser (Map String (Map String [NmcRRTlsa])))
+        >>= tmap2dmap
+          where
+            tmap2dmap :: Map String (Map String [NmcRRTlsa])
+                      -> Parser (Maybe (Map String NmcDom))
+                -- FIXME return parse error on invalid proto or port
+            tmap2dmap m1 = return $ foldrWithKey addprotoelem (Just M.empty) m1
+            addprotoelem k1 m2 acc = protoelem k1 m2 `merge` acc
+            protoelem k1 m2 = Just (M.singleton ("_" ++ k1) (pmap2dmap m2))
+            pmap2dmap m2 = foldrWithKey addportelem def m2
+            addportelem k2 v acc = portelem k2 v `merge` acc
+            portelem k2 v =
+              def { domSubmap = Just (M.singleton ("_" ++ k2)
+                                      def { domTlsa = Just v }) }
+    Just _ -> empty
 
 class Mergeable a where
         merge :: a -> a -> a -- bias towads second arg
@@ -99,9 +182,21 @@ instance Mergeable NmcRRI2p where
 data NmcRRTlsa = NmcRRTlsa
                         { tlsMatchType  :: Int -- 0:exact 1:sha256 2:sha512
                         , tlsMatchValue :: String
-                        , tlsIncSubdoms :: Int -- 1:enforce on subdoms 0:no
+                        , tlsIncSubdoms :: Bool -- enforce on subdoms?
                         } deriving (Show, Eq)
 
+instance FromJSON NmcRRTlsa where
+        parseJSON (Array a) =
+                if length a == 3 then NmcRRTlsa
+                        <$> parseJSON (a ! 0)
+                        <*> parseJSON (a ! 1)
+                        <*> case (a ! 2) of
+                              Number 0 -> return False
+                              Number 1 -> return True
+                              _        -> empty
+                else empty
+        parseJSON _ = empty
+
 instance Mergeable NmcRRTlsa where
         merge _ b = b