-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNmcDom.hs
312 lines (281 loc) · 12.5 KB
/
NmcDom.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
{-# LANGUAGE OverloadedStrings #-}
module NmcDom ( NmcDom(..)
, NmcRRSrv(..)
, NmcRRI2p(..)
, NmcRRTlsa(..)
, NmcRRDs(..)
, merge
) where
import Prelude hiding (length)
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)
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
-- Variant of Aeson's `.:?` that interprets a String as a
-- single-element list, so it is possible to have either
-- "ip":["1.2.3.4"]
-- or
-- "ip":"1.2.3.4"
-- with the same result.
(.:/) :: (FromJSON a) => Object -> Text -> Parser (Maybe a)
obj .:/ key = case H.lookup key obj of
Nothing -> pure Nothing
Just v -> case v of
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 =
case H.lookup "service" o of
Nothing -> pure Nothing
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 = 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))
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
instance (Ord k, Mergeable a) => Mergeable (Map k a) where
merge mx my = unionWith merge my mx
-- Alas, the following is not possible in Haskell :-(
-- instance Mergeable String where
-- merge _ b = b
instance Mergeable Value where
merge _ b = b
instance Mergeable a => Mergeable (Maybe a) where
merge (Just x) (Just y) = Just (merge x y)
merge Nothing (Just y) = Just y
merge (Just x) Nothing = Just x
merge Nothing Nothing = Nothing
instance Eq a => Mergeable [a] where
merge xs ys = union xs ys
data NmcRRSrv = NmcRRSrv
{ srvPrio :: Int
, srvWeight :: Int
, srvPort :: Int
, srvHost :: String
} deriving (Show, Eq)
instance Mergeable NmcRRSrv where
merge _ b = b
data NmcRRI2p = NmcRRI2p
{ i2pDestination :: Maybe String
, i2pName :: Maybe String
, i2pB32 :: Maybe String
} deriving (Show, Eq)
instance FromJSON NmcRRI2p where
parseJSON (Object o) = NmcRRI2p
<$> o .:? "destination"
<*> o .:? "name"
<*> o .:? "b32"
parseJSON _ = empty
instance Mergeable NmcRRI2p where
merge _ b = b
data NmcRRTlsa = NmcRRTlsa
{ tlsMatchType :: Int -- 0:exact 1:sha256 2:sha512
, tlsMatchValue :: String
, 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
data NmcRRDs = NmcRRDs
{ dsKeyTag :: Int
, dsAlgo :: Int
, dsHashType :: Int
, dsHashValue :: String
} deriving (Show, Eq)
instance FromJSON NmcRRDs where
parseJSON (Array a) =
if length a == 4 then NmcRRDs
<$> parseJSON (a ! 0)
<*> parseJSON (a ! 1)
<*> parseJSON (a ! 2)
<*> parseJSON (a ! 3)
else empty
parseJSON _ = empty
instance Mergeable NmcRRDs where
merge _ b = b
data NmcDom = NmcDom { domIp :: Maybe [String]
, domIp6 :: Maybe [String]
, domTor :: Maybe String
, domI2p :: Maybe NmcRRI2p
, domFreenet :: Maybe String
, domAlias :: Maybe String
, domTranslate :: Maybe String
, domEmail :: Maybe String
, domLoc :: Maybe String
, domInfo :: Maybe Value
, domNs :: Maybe [String]
, domDelegate :: Maybe String
, domImport :: Maybe [String]
, domSubmap :: Maybe (Map String NmcDom)
, domFingerprint :: Maybe [String]
, domDs :: Maybe [NmcRRDs]
, domMx :: Maybe [String] -- Synthetic
, domSrv :: Maybe [NmcRRSrv] -- Synthetic
, domTlsa :: Maybe [NmcRRTlsa] -- Synthetic
} deriving (Show, Eq)
instance Default NmcDom where
def = NmcDom Nothing Nothing Nothing Nothing Nothing Nothing Nothing
Nothing Nothing Nothing Nothing Nothing Nothing Nothing
Nothing Nothing Nothing Nothing Nothing
instance FromJSON NmcDom where
-- Wherever we expect a domain object, there may be a string
-- containing IPv4 address. Interpret it as such.
-- Question: shall we try to recognize IPv6 addresses too?
parseJSON (String s) =
return $ if isIPv4 s'
then def { domIp = Just [s'] }
else def
where
s' = unpack s
isIPv4 x = all isNibble $ splitOn "." x
isNibble x =
if all isDigit x then (read x :: Int) < 256
else False
parseJSON (Object o) = NmcDom
<$> o .:/ "ip"
<*> o .:/ "ip6"
<*> o .:? "tor"
<*> o .:? "i2p"
<*> o .:? "freenet"
<*> o .:? "alias"
<*> o .:? "translate"
<*> o .:? "email"
<*> o .:? "loc"
<*> o .:? "info"
<*> o .:/ "ns"
<*> o .:? "delegate"
<*> o .:/ "import"
<*> makeSubmap o
<*> o .:/ "fingerprint"
<*> o .:? "ds"
<*> makeMx o
<*> return Nothing -- domSrv created in subdomains
<*> return Nothing -- domTlsa created in subdomains
parseJSON _ = empty
instance Mergeable NmcDom where
merge sub dom = dom { domIp = mergelm domIp
, domIp6 = mergelm domIp6
, domTor = choose domTor
, domI2p = mergelm domI2p
, domFreenet = choose domFreenet
, domAlias = choose domAlias
, domTranslate = choose domTranslate
, domEmail = choose domEmail
, domLoc = choose domLoc
, domInfo = mergelm domInfo
, domNs = mergelm domNs
, domDelegate = mergelm domDelegate
, domImport = mergelm domImport
, domSubmap = mergelm domSubmap
, domFingerprint = mergelm domFingerprint
, domDs = mergelm domDs
, domMx = mergelm domMx
, domSrv = mergelm domSrv
, domTlsa = mergelm domTlsa
}
where
mergelm x = merge (x sub) (x dom)
-- Because it is not possible to define instance of merge for Strings,
-- we have to treat string elements separately, otherwise strings are
-- 'unioned' along with the rest of lists. Ugly, but alternatives are worse.
choose field = case field dom of
Nothing -> field sub
Just x -> Just x