-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
315 lines (256 loc) · 9.92 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
'use strict'
// built in node modules
const dns = require('dns')
const net = require('net')
const { Resolver } = require('dns').promises;
const resolver = new Resolver();
// NPM modules
const constants = require('haraka-constants')
const net_utils = require('haraka-net-utils')
const tlds = require('haraka-tld')
exports.register = function () {
this.load_fcrdns_ini()
this.register_hook('connect_init', 'initialize_fcrdns')
this.register_hook('lookup_rdns', 'do_dns_lookups')
this.register_hook('data', 'add_message_headers')
}
exports.load_fcrdns_ini = function () {
const plugin = this
plugin.cfg = plugin.config.get('fcrdns.ini', {
booleans: [
'-reject.no_rdns',
'-reject.no_fcrdns',
'-reject.invalid_tld',
'-reject.generic_rdns',
]
}, function () {
plugin.load_fcrdns_ini()
})
if (isNaN(plugin.cfg.main.timeout)) {
plugin.cfg.main.timeout = plugin.timeout || 30;
}
}
exports.initialize_fcrdns = function (next, connection) {
// always init, so results.get is deterministic
connection.results.add(this, {
fcrdns: [], // PTR host names that resolve to this IP
invalid_tlds: [], // rDNS names with invalid TLDs
other_ips: [], // IPs from names that didn't match
ptr_names: [], // Array of host names from PTR query
ptr_multidomain: false, // Multiple host names in different domains
has_rdns: false, // does IP have PTR records?
ptr_name_has_ips: false, // PTR host has IP address(es)
ptr_name_to_ip: {}, // host names and their IP addresses
})
next()
}
exports.resolve_ptr_names = async function (ptr_names, connection, next) {
// Fetch A & AAAA records for each PTR host name
const promises = []
const forwardNames = []
for (const ptr_domain of ptr_names) {
// Make sure TLD is valid
if (!tlds.get_organizational_domain(ptr_domain.toLowerCase())) {
connection.results.add(this, {fail: `valid_tld(${ptr_domain})`})
if (!this.cfg.reject.invalid_tld) continue
if (this.is_whitelisted(connection)) continue
if (net_utils.is_private_ip(connection.remote.ip)) continue
return next(constants.DENY, `client [${connection.remote.ip}] rejected; invalid TLD in rDNS (${ptr_domain})`)
}
connection.logdebug(this, `PTRdomain: ${ptr_domain}`)
forwardNames.push(ptr_domain.toLowerCase())
promises.push(net_utils.get_ips_by_host(ptr_domain.toLowerCase()))
}
Promise.all(promises).then(ipsPerHost => {
const resultsByForwardName = {}
for (let i = 0; i < ipsPerHost.length; i++) {
resultsByForwardName[forwardNames[i]] = ipsPerHost[i]
if (ipsPerHost[i].length === 0) {
connection.results.add(this, { fail: `ptr_valid(${forwardNames[i]})` })
}
}
connection.results.add(this, { ptr_name_to_ip: resultsByForwardName })
this.check_fcrdns(connection, resultsByForwardName, next)
})
}
exports.do_dns_lookups = async function (next, connection) {
if (connection.remote.is_private) {
connection.results.add(this, {skip: 'private_ip'})
return next()
}
const rip = connection.remote.ip
// Set-up timer
const timeoutMs = (this.cfg.main.timeout - 1) * 1000
const timer = setTimeout(() => {
connection.results.add(this, {err: 'timeout', emit: true})
if (!this.cfg.reject.no_rdns) return nextOnce()
if (this.is_whitelisted(connection)) return nextOnce()
nextOnce(DENYSOFT, `client [${rip}] rDNS lookup timeout`)
}, timeoutMs)
let called_next = 0
function nextOnce (code, msg) {
if (called_next) return
called_next++
clearTimeout(timer)
next(code, msg)
}
try {
const ptr_names = await resolver.reverse(rip)
if (called_next) return // timed out
connection.logdebug(this, `rdns.reverse(${rip})`)
connection.results.add(this, {ptr_names})
connection.results.add(this, {has_rdns: true})
this.resolve_ptr_names(ptr_names, connection, nextOnce);
}
catch (err) {
this.handle_ptr_error(connection, err, nextOnce)
}
}
exports.add_message_headers = function (next, connection) {
const txn = connection.transaction;
['rDNS', 'FCrDNS', 'rDNS-OtherIPs', 'HostID' ].forEach((h) => {
txn.remove_header(`X-Haraka-${h}`)
})
const fcrdns = connection.results.get('fcrdns')
if (!fcrdns) {
connection.results.add(this, {err: "no fcrdns results!?"})
return next()
}
if (fcrdns.name && fcrdns.name.length) {
txn.add_header('X-Haraka-rDNS', fcrdns.name.join(' '))
}
if (fcrdns.fcrdns && fcrdns.fcrdns.length) {
txn.add_header('X-Haraka-FCrDNS', fcrdns.fcrdns.join(' '))
}
if (fcrdns.other_ips && fcrdns.other_ips.length) {
txn.add_header('X-Haraka-rDNS-OtherIPs', fcrdns.other_ips.join(' '))
}
next()
}
exports.handle_ptr_error = function (connection, err, next) {
const rip = connection.remote.ip
switch (err.code) {
case dns.NOTFOUND:
case dns.NXDOMAIN:
case dns.NODATA:
connection.results.add(this, {fail: 'has_rdns', emit: true})
if (!this.cfg.reject.no_rdns) return next()
if (this.is_whitelisted((connection))) return next()
return next(DENY, `client [${rip}] rejected; no rDNS`)
}
connection.results.add(this, {err: err.code})
if (!this.cfg.reject.no_rdns) return next()
if (this.is_whitelisted(connection)) return next()
next(DENYSOFT, `client [${rip}] rDNS lookup error (${err})`)
}
exports.check_fcrdns = function (connection, results, next) {
let last_domain
for (const fdom in results) { // mail.example.com
if (!fdom) continue
const org_domain = tlds.get_organizational_domain(fdom); // example.com
// Multiple domains?
if (last_domain && last_domain !== org_domain) {
connection.results.add(this, {ptr_multidomain: true})
}
else {
last_domain = org_domain
}
// FCrDNS? PTR -> (A | AAAA) 3. PTR comparison
this.ptr_compare(results[fdom], connection, fdom)
connection.results.add(this, {ptr_name_has_ips: true})
if (this.is_generic_rdns(connection, fdom) &&
this.cfg.reject.generic_rdns &&
!this.is_whitelisted(connection)) {
return next(DENY, `client ${fdom} [${connection.remote.ip}] rejected;` +
` generic rDNS, please use your ISPs SMTP relay`)
}
}
this.log_summary(connection)
this.save_auth_results(connection)
const r = connection.results.get('fcrdns')
if (!r) return next()
if (r.fcrdns && r.fcrdns.length) return next()
if (this.cfg.reject.no_fcrdns) {
return next(DENY, 'Sorry, no FCrDNS match found')
}
next()
}
exports.ptr_compare = function (ip_list, connection, domain) {
if (!ip_list || !ip_list.length) return false
if (ip_list.includes(connection.remote.ip)) {
connection.results.add(this, {pass: 'fcrdns' })
connection.results.push(this, {fcrdns: domain})
return true
}
const ip_list_v4 = ip_list.filter(net.isIPv4)
if (ip_list_v4.length && net_utils.same_ipv4_network(connection.remote.ip, ip_list_v4)) {
connection.results.add(this, {pass: 'fcrdns(net)' })
connection.results.push(this, {fcrdns: domain})
return true
}
for (const ip of ip_list) {
connection.results.push(this, {other_ips: ip})
}
return false
}
exports.save_auth_results = function (connection) {
const r = connection.results.get('fcrdns')
if (!r) return
if (r.fcrdns && r.fcrdns.length) {
connection.auth_results('iprev=pass')
return true
}
if (!r.has_rdns) {
connection.auth_results('iprev=permerror')
return false
}
if (r.err.length) {
connection.auth_results('iprev=temperror')
return false
}
connection.auth_results('iprev=fail')
return false
}
exports.is_generic_rdns = function (connection, domain) {
if (!domain) return false
if (!net_utils.is_ip_in_str(connection.remote.ip, domain)) {
connection.results.add(this, {pass: 'is_generic_rdns'})
return false
}
connection.results.add(this, {fail: 'is_generic_rdns'})
const orgDom = tlds.get_organizational_domain(domain)
if (!orgDom) {
connection.loginfo(this, `no org domain for: ${domain}`)
return false
}
const host_part = domain.split('.').slice(0,orgDom.split('.').length+1)
if (/(?:static|business)/.test(host_part)) {
// Allow some obvious generic but static ranges
// EHLO/HELO checks will still catch out hosts that use generic rDNS there
connection.loginfo(this, 'allowing generic static rDNS')
return false
}
return true
}
function hostNamesAsStr (list) {
if (!list) return ''
if (list.length > 2) return `${list.slice(0,2).join(',')}...`
return list.join(',')
}
exports.log_summary = function (connection) {
if (!connection) return; // connection went away
const r = connection.results.get('fcrdns')
if (!r) return
connection.loginfo(this, `ip=${connection.remote.ip} ` +
` rdns="${hostNamesAsStr(r.ptr_names)}" rdns_len=${r.ptr_names.length}` +
` fcrdns="${hostNamesAsStr(r.fcrdns)}" fcrdns_len=${r.fcrdns.length}` +
` other_ips_len=${r.other_ips.length} invalid_tlds=${r.invalid_tlds.length}` +
` generic_rdns=${((r.ptr_name_has_ips) ? 'true' : 'false')}`
)
}
exports.is_whitelisted = function (connection) {
// allow rdns_acccess whitelist to override
if (!connection.notes.rdns_access) return false
if (connection.notes.rdns_access !== 'white') return false
return true
}