-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
436 lines (363 loc) · 11.3 KB
/
index.js
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
// dns-lists plugin
const dnsPromises = require('dns').promises
const dns = new dnsPromises.Resolver({ timeout: 25000, tries: 1 })
const net = require('net')
const net_utils = require('haraka-net-utils')
let redis_client
exports.register = function () {
this.load_config()
if (this.cfg.main.periodic_checks) {
this.check_zones(this.cfg.main.periodic_checks)
}
this.register_hook('connect', 'onConnect')
if (this.cfg['ips.backscatterer.org'].enable) {
this.register_hook('mail', 'check_backscatterer')
}
// IMPORTANT: don't run this on hook_rcpt else we're an open relay...
if (this.cfg['list.dnswl.org'].ok_helo) {
this.register_hook('helo', 'check_dnswl')
this.register_hook('ehlo', 'check_dnswl')
}
if (this.cfg['list.dnswl.org'].ok_mail) {
this.register_hook('mail', 'check_dnswl')
}
}
exports.load_config = function () {
this.cfg = this.config.get(
'dns-list.ini',
{
booleans: [
'-stats.enable',
'*.reject',
'*.ipv6',
'*.loopback_is_rejected',
'-ips.backscatterer.org.enable',
'-list.dnswl.org.ok_helo',
'-list.dnswl.org.ok_mail',
],
},
() => {
this.load_config()
},
)
if (Array.isArray(this.cfg.main.zones)) {
this.cfg.main.zones = new Set(this.cfg.main.zones)
} else {
this.cfg.main.zones = new Set(this.cfg.main.zones.split(/[\s,;]+/))
}
// Compatibility with old-plugin
for (const z in this.config.get('dnsbl.zones', 'list')) {
this.cfg.main.zones.add(z)
}
for (const z in this.config.get('dnswl.zones', 'list')) {
this.cfg.main.zones.add(z)
if (this.cfg[z] === undefined) this.cfg[z] = {}
this.cfg[z].type = 'allow'
}
// active zones
if (this.cfg.main.periodic_checks < 5) {
// all configured are enabled
this.zones = this.cfg.main.zones
} else {
this.zones = new Set() // populated by check_zones()
}
}
exports.should_skip = function (connection) {
if (!connection) return true
if (connection.remote.is_private) {
connection.results.add(this, {
skip: `private: ${connection.remote.ip}`,
emit: true,
})
return true
}
if (this.zones.length === 0) {
connection.results.add(this, { err: `no zones` })
return true
}
return false
}
exports.eachActiveDnsList = async function (connection, zone, nextOnce) {
const type = this.getListType(zone)
const ips = await this.lookup(connection.remote.ip, zone)
// console.log(`eachActiveDnsList ip ${connection.remote.ip} zone ${zone} type ${type} ips ${ips}`)
if (!ips) {
if (type === 'block') connection.results.add(this, { pass: zone })
return
}
for (const i of ips) {
if (this.cfg[zone] && this.cfg[zone][i]) {
connection.results.add(this, { msg: this.cfg[zone][i] })
}
}
if (type === 'allow') {
connection.notes.dnswl = true
connection.results.add(this, { pass: zone })
return nextOnce(OK, [zone])
}
if (type === 'karma') {
if (ips.includes('127.0.0.1')) {
connection.results.add(this, { pass: zone })
} else if (ips.includes('127.0.0.2')) {
connection.results.add(this, { fail: zone })
if (this.cfg.main.search === 'first') nextOnce(DENY, [zone])
} else {
connection.results.add(this, { msg: zone })
}
return
}
// type=block
connection.results.add(this, { fail: zone })
if (this.cfg.main.search === 'first') nextOnce(DENY, [zone])
return ips
}
exports.onConnect = function (next, connection) {
const plugin = this
if (this.should_skip(connection)) return next()
let calledNext = false
function nextOnce(code, zones) {
// console.log(`nextOnce: ${code} : ${zones}`)
if (calledNext) return
calledNext = true
connection.results.add(plugin, { emit: true })
if (code === undefined || zones === undefined) return next()
next(
code,
`host [${connection.remote.ip}] is listed on ${zones.join(', ')}`,
)
}
const promises = []
for (const zone of this.zones) {
promises.push(this.eachActiveDnsList(connection, zone, nextOnce))
}
Promise.all(promises).then(() => {
// console.log(`Promise.all`)
if (connection.results.get(this)?.fail?.length) {
nextOnce(DENY, connection.results.get(this).fail)
return
}
nextOnce()
})
}
exports.check_dnswl = (next, connection) =>
connection.notes.dnswl ? next(OK) : next()
exports.check_backscatterer = async function (next, connection, params) {
if (!this.cfg['ips.backscatterer.org'].enable) return next()
const user = params[0]?.user ? params[0].user.toLowerCase() : null
if (!(!user || user === 'postmaster')) return next()
try {
const a = await this.lookup(connection.remote.ip, 'ips.backscatterer.org')
if (a)
return next(
DENY,
`Host ${connection.remote.host} [${connection.remote.ip}] is listed by ips.backscatterer.org`,
)
} catch (err) {
connection.logerror(this, err)
}
next()
}
function ipQuery(ip, zone) {
// 1.2.3.4 -> 4.3.2.1.$zone.
if (net.isIPv6(ip)) return [net_utils.ipv6_reverse(ip), zone, ''].join('.')
// ::FFFF:7F00:2 -> 2.0.0.0.0.0.f.7.f.f.f.f.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.$zone.
if (net.isIPv4(ip))
return [ip.split('.').reverse().join('.'), zone, ''].join('.')
throw new Error('invalid IP: ${ip}')
}
exports.lookup = async function (ip, zone) {
if (!ip || !zone) throw new Error(`lookup: invalid request`)
let start
if (this.cfg.stats.enable) {
this.init_redis()
start = new Date().getTime()
}
try {
const query = ipQuery(ip, zone)
const a = await dns.resolve4(query, 'A')
// console.log(`lookup ${query} -> a: ${a}`)
this.stats_incr_zone(null, zone, start) // Statistics
if (this.hasSpecialResults(zone, a)) return
return a
} catch (err) {
this.stats_incr_zone(err, zone, start) // Statistics
if (err.code === dnsPromises.NOTFOUND) return // unlisted, not an error
if (err.code === dnsPromises.TIMEOUT) {
// list timed out
this.disable_zone(zone, err.code) // disable it
return
}
console.error(`err: ${err}`)
// throw err
}
}
exports.hasSpecialResults = function (zone, a) {
// Check for a result outside 127/8
// This should *never* happen on a proper DNS list
if (
a &&
a.find((rec) => {
return rec.split('.')[0] !== '127'
})
) {
this.disable_zone(zone, a)
return true
}
if (/spamhaus/.test(zone)) {
// https://www.spamhaus.org/news/article/807/using-our-public-mirrors-check-your-return-codes-now
if (
a?.includes('127.255.255.252') ||
a?.includes('127.255.255.254') ||
a?.includes('127.255.255.255')
) {
this.disable_zone(zone, a)
return true
}
}
// https://www.dnswl.org/?page_id=15
if ('list.dnswl.org' === zone && a?.includes('127.0.0.255')) {
this.disable_zone(zone, a)
return true
}
return false
}
exports.stats_incr_zone = function (err, zone, start) {
if (!this.cfg.stats.enable) return
const rkey = `dns-list-stat:${zone}`
const elapsed = new Date().getTime() - start
redis_client.hIncrBy(rkey, 'TOTAL', 1)
const foo = err ? err.code : 'LISTED'
redis_client.hIncrBy(rkey, foo, 1)
redis_client.hGet(rkey, 'AVG_RT').then((rt) => {
const avg = parseInt(rt)
? (parseInt(elapsed) + parseInt(rt)) / 2
: parseInt(elapsed)
redis_client.hSet(rkey, 'AVG_RT', avg)
})
}
exports.init_redis = function () {
if (redis_client) return
const redis = require('redis')
const host_port = this.cfg.stats.redis_host.split(':')
const host = host_port[0] || '127.0.0.1'
const port = parseInt(host_port[1], 10) || 6379
redis_client = redis.createClient(port, host)
redis_client.connect().then(() => {
redis_client.on('error', (err) => {
this.logerror(`Redis error: ${err}`)
redis_client.quit()
redis_client = null // should force a reconnect
// not sure if that's the right thing but better than nothing...
})
})
}
exports.getListType = function (zone) {
if (this.cfg[zone] === undefined) return 'block'
return this.cfg[zone]?.type || 'block'
}
exports.getListReject = function (zone) {
if (this.cfg[zone]?.reject === undefined) return true // default: true
return this.cfg[zone].reject
}
exports.checkZonePositive = async function (zone, ip) {
// RFC 5782 § 5
// IPv4-based DNSxLs MUST contain an entry for 127.0.0.2 for testing purposes.
const query = ipQuery(ip, zone)
try {
const a = await dns.resolve4(query, 'A')
if (a) {
// const txt = await dns.resolve4(query, 'TXT')
// console.log(`${query} -> ${a}\t${txt}`)
for (const e of a) {
if (this.cfg[zone] && this.cfg[zone][e]) {
// console.log(this.cfg[zone][e]); //
}
}
return true
} else {
this.logwarn(`${query}\tno response`)
this.disable_zone(zone, a)
}
} catch (err) {
console.error(`${query} -> ${err}`)
}
return false
}
exports.checkZoneNegative = async function (zone, ip) {
// RFC 5782 § 5
// IPv4-based DNSxLs MUST NOT contain an entry for 127.0.0.1.
// skip this test for DNS lists that don't follow the RFC
if (this.cfg[zone].loopback_is_rejected) return true
const query = ipQuery(ip, zone)
try {
const a = await dns.resolve4(query, 'A')
if (a) {
// results here are invalid
// const txt = await dns.resolve4(query, 'TXT')
// if (txt && txt !== a) console.warn(`${query} -> ${a}\t${txt}`)
this.disable_zone(zone, a)
}
} catch (err) {
switch (err.code) {
case dnsPromises.NOTFOUND: // IP not listed
return true
case dnsPromises.TIMEOUT: // list timed out
this.disable_zone(zone, err.code)
}
console.error(`${query} -> got err ${err}`)
}
return false
}
exports.check_zone = async function (zone) {
if (!(await this.checkZonePositive(zone, '127.0.0.2'))) return false
if (!(await this.checkZoneNegative(zone, '127.0.0.1'))) return false
this.enable_zone(zone) // both tests passed
if (this.cfg[zone].ipv6 === true) {
await this.checkZonePositive(zone, '::FFFF:7F00:2')
await this.checkZoneNegative(zone, '::FFFF:7F00:1')
}
return true
}
exports.check_zones = async function (interval) {
if (interval) interval = parseInt(interval)
const promises = []
for (const zone of this.cfg.main.zones) {
promises.push(this.check_zone(zone))
}
try {
await Promise.all(promises)
} catch (err) {
console.error(err)
}
// Set a timer to re-test
if (interval && interval >= 5 && !this._interval) {
this.loginfo(`will re-test list zones every ${interval} minutes`)
this._interval = setInterval(
() => {
this.check_zones()
},
interval * 60 * 1000,
)
this._interval.unref() // don't block node process from exiting
}
}
exports.shutdown = function () {
clearInterval(this._interval)
if (redis_client) redis_client.quit()
}
exports.enable_zone = function (zone) {
if (!zone) return false
const type = this.getListType(zone)
if (!this.zones.has(zone)) {
this.loginfo(`enabling ${type} zone ${zone}`)
this.zones.add(zone, true)
}
}
exports.disable_zone = function (zone, result) {
if (!zone) return false
const type = this.getListType(zone)
if (!this.zones.has(zone)) return false
this.logwarn(`disabling ${type} zone '${zone}' ${result ? result : ''}`)
this.zones.delete(zone)
return true
}