-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
318 lines (257 loc) · 9.03 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
'use strict';
const tlds = require('haraka-tld');
exports.register = function () {
this.inherits('haraka-plugin-redis');
this.load_sender_ini();
this.register_hook('init_master', 'init_redis_plugin');
this.register_hook('init_child', 'init_redis_plugin');
this.register_hook('mail', 'is_authenticated');
this.register_hook('rcpt_ok', 'check_recipient');
this.register_hook('queue_ok', 'update_sender');
this.register_hook('data_post', 'is_dkim_authenticated');
}
exports.load_sender_ini = function () {
const plugin = this;
plugin.cfg = plugin.config.get('known-senders.ini', function () {
plugin.load_sender_ini();
});
plugin.merge_redis_ini();
}
/*
* Outbound Processing
*
* Identify and save to Redis domains the local users send email to
*
* Context: these functions run after a message has been queued.
*
*/
exports.update_sender = async function (next, connection, params) {
// queue_ok arguments: next, connection, msg
// ok 1390590369 qp 634 (F82E2DD5-9238-41DC-BC95-9C3A02716AD2.1)
let sender_od;
let rcpt_domains;
function errNext (err) {
connection.logerror(this, `update_sender: ${err}`);
next(null, null, sender_od, rcpt_domains);
}
// connection.loginfo(this, params);
if (!connection) return errNext('no connection');
if (!connection.transaction) return errNext('no transaction');
if (!connection.relaying) return next();
const txn = connection.transaction;
sender_od = this.get_sender_domain_by_txn(txn);
if (!sender_od) return errNext('no sender domain');
rcpt_domains = this.get_recipient_domains_by_txn(txn);
if (rcpt_domains.length === 0) {
return errNext(`no rcpt ODs for ${sender_od}`);
}
// within this function, the sender is a local domain
// and the recipient is an external domain
try {
const multi = this.db.multi();
for (let i = 0; i < rcpt_domains.length; i++) {
multi.hIncrBy(sender_od, rcpt_domains[i], 1);
}
const replies = await multi.exec()
for (let i = 0; i < rcpt_domains.length; i++) {
connection.loginfo(this, `saved ${sender_od} : ${rcpt_domains[i]} : ${replies[i]}`);
}
next(null, null, sender_od, rcpt_domains);
}
catch (err) {
connection.logerror(this, err);
next();
}
}
exports.get_sender_domain_by_txn = function (txn) {
const plugin = this;
if (!txn.mail_from) return;
if (!txn.mail_from.host) return;
const sender_od = tlds.get_organizational_domain(txn.mail_from.host);
if (txn.mail_from.host !== sender_od) {
plugin.logdebug(`sender: ${txn.mail_from.host} -> ${sender_od}`);
}
return sender_od;
}
exports.get_recipient_domains_by_txn = function (txn) {
const plugin = this;
const rcpt_domains = [];
if (!txn.rcpt_to) return rcpt_domains;
for (let i=0; i < txn.rcpt_to.length; i++) {
if (!txn.rcpt_to[i].host) continue;
const rcpt_od = tlds.get_organizational_domain(txn.rcpt_to[i].host);
if (txn.rcpt_to[i].host !== rcpt_od) {
plugin.loginfo(`rcpt: ${txn.rcpt_to[i].host} -> ${rcpt_od}`);
}
if (rcpt_domains.indexOf(rcpt_od) === -1) {
// not a duplicate, add to the list
rcpt_domains.push(rcpt_od);
}
}
return rcpt_domains;
}
/*
* Inbound Processing
*
* Look for sender domains we can validate against something. Anything..
* FCrDNS, SPF, DKIM, verified TLS host name, etc..
*
* When verified / validated sender domains are found, check to see if
* their recipients have ever sent mail to their domain.
*/
// early checks, on the mail hook
exports.is_authenticated = function (next, connection, params) {
const plugin = this;
// only validate inbound messages
if (connection.relaying) return next();
const sender_od = plugin.get_sender_domain_by_txn(connection.transaction);
if (plugin.has_fcrdns_match(sender_od, connection)) {
connection.logdebug(plugin, `+fcrdns: ${sender_od}`);
return next(null, null, sender_od);
}
if (plugin.has_spf_match(sender_od, connection)) {
connection.logdebug(plugin, `+spf: ${sender_od}`);
return next(null, null, sender_od);
}
// Maybe: TLS verified domain?
if (connection.tls.verified) {
// TODO: get the CN and Subject Alternative Names of the cert
// then look for match with sender_od
connection.logdebug(plugin, `+tls: ${sender_od}`);
// return next(null, null, sender_od);
}
return next();
}
exports.get_validated_sender_od = function (connection) {
const plugin = this;
if (!connection) return;
if (!connection.transaction) return;
const txn_res = connection.transaction.results.get(plugin.name);
if (!txn_res) return;
return txn_res.sender;
}
exports.get_rcpt_ods = function (connection) {
const plugin = this;
if (!connection) return [];
if (!connection.transaction) return [];
const txn_r = connection.transaction.results.get(plugin.name);
if (!txn_r) return [];
return txn_r.rcpt_ods;
}
exports.already_matched = function (connection) {
const res = connection.transaction.results.get(this);
if (!res) return false;
return (res.pass && res.pass.length) ? true : false;
}
exports.check_recipient = async function (next, connection, rcpt) {
// rcpt is a valid local email address. Some rcpt_to.* plugin has
// accepted it.
// inbound only
if (connection.relaying) return next();
function errNext (err) {
connection.logerror(this, `check_recipient: ${err}`);
next();
}
if (!rcpt.host) return errNext('rcpt.host unset?');
// reduce the host portion of the email address to an OD
const rcpt_od = tlds.get_organizational_domain(rcpt.host);
if (!rcpt_od) return errNext(`no rcpt od for ${rcpt.host}`);
connection.transaction.results.push(this, { rcpt_ods: rcpt_od });
// if no validated sender domain, there's nothing to do...yet
const sender_od = this.get_validated_sender_od(connection);
if (!sender_od) return next();
// The sender OD is validated, check Redis for a match
try {
const reply = await this.db.hGet(rcpt_od, sender_od)
connection.logdebug(this, `${rcpt_od} : ${sender_od} : ${reply}`);
if (reply) {
connection.transaction.results.add(this, { pass: rcpt_od, count: reply });
}
next(null, null, rcpt_od);
}
catch (err) {
this.logerror(err);
next();
}
}
exports.is_dkim_authenticated = async function (next, connection) {
if (connection.relaying) return next();
let rcpt_ods = [];
function errNext (err) {
connection.logerror(this, `is_dkim_authenticated: ${err}`);
next(null, null, rcpt_ods);
}
function infoNext (msg) {
connection.loginfo(this, `is_dkim_authenticated: ${msg}`);
next(null, null, rcpt_ods);
}
if (this.already_matched(connection)) return infoNext('already matched');
const sender_od = this.get_validated_sender_od(connection);
if (!sender_od) return errNext('no sender_od');
rcpt_ods = this.get_rcpt_ods(connection);
if (!rcpt_ods || ! rcpt_ods.length) return errNext('no rcpt_ods');
const dkim = connection.transaction.results.get('dkim_verify');
if (!dkim) return infoNext('no dkim_verify results');
if (!dkim.pass || !dkim.pass.length) return infoNext('no dkim pass')
try {
const multi = this.db.multi();
for (let i = 0; i < dkim.pass.length; i++) {
const dkim_od = tlds.get_organizational_domain(dkim.pass[i]);
if (dkim_od === sender_od) {
connection.transaction.results.add(this, { sender: sender_od, auth: 'dkim' });
for (let j = 0; j < rcpt_ods.length; j++) {
multi.hGet(rcpt_ods[j], sender_od);
}
}
}
const replies = await multi.exec()
for (let j = 0; j < rcpt_ods.length; j++) {
if (!replies[j]) continue
connection.transaction.results.add(this, {
pass: rcpt_ods[j],
count: replies[j],
emit: true
});
}
next(null, null, rcpt_ods);
}
catch (err) {
connection.logerror(this, err)
errNext(err)
}
}
exports.has_fcrdns_match = function (sender_od, connection) {
const fcrdns = connection.results.get('fcrdns');
if (!fcrdns) return false;
if (!fcrdns.fcrdns) return false;
connection.logdebug(this, fcrdns.fcrdns);
let mail_host = fcrdns.fcrdns;
if (Array.isArray(mail_host)) mail_host = fcrdns.fcrdns[0];
const fcrdns_od = tlds.get_organizational_domain(mail_host);
if (fcrdns_od !== sender_od) return false;
connection.transaction.results.add(this, {
sender: sender_od, auth: 'fcrdns', emit: true
});
return true;
}
exports.has_spf_match = function (sender_od, connection) {
let spf = connection.results.get('spf');
if (spf && spf.domain && spf.result === 'Pass') {
// scope=helo (HELO/EHLO)
if (tlds.get_organizational_domain(spf.domain) === sender_od) {
connection.transaction.results.add(this, {sender: sender_od});
return true;
}
}
spf = connection.transaction.results.get('spf');
if (spf && spf.domain && spf.result === 'Pass') {
// scope=mfrom (HELO/EHLO)
if (tlds.get_organizational_domain(spf.domain) === sender_od) {
connection.transaction.results.add(this, {
sender: sender_od, auth: 'spf', emit: true
});
return true;
}
}
return false;
}