-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
204 lines (172 loc) · 5.71 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
'use strict';
var Stream = require('stream').Stream;
var util = require('util');
var constants = require('haraka-constants');
class OpenDKIMVerifyStream extends Stream {
constructor (cb, plugin, opendkim) {
super();
var self = this;
this.plugin = plugin;
this.writable = true;
this.chunks = [];
this.chunksLen = 0;
this.timeout = ((plugin.timeout) ? plugin.timeout - 1 : 30);
this.opendkim = opendkim;
this.callback = cb;
}
}
OpenDKIMVerifyStream.prototype.debug = function (str) {
this.plugin.logdebug(str);
};
OpenDKIMVerifyStream.prototype._get_chunk = function (buf) {
var chunk = buf
.toString('utf8')
.replace(/\nTo:\s+/g, '\nTo: '); // fixes wrapped To: header from yahoo
return chunk;
};
OpenDKIMVerifyStream.prototype._build_result = function (error) {
var result = {};
if (error === undefined) {
// The pass case.
result.result = 'pass';
} else if (error.message === 'No signature') {
// The none case. We can't know if this is a fail until DMARC.
result.result = 'none';
result.error = error.message;
} else if (error.message === 'Key retrieval failed' ||
error.message === 'Resource unavailable' ||
error.message === 'Try again later') {
// The 'tempfail' case, where we can retry
result.result = 'tempfail';
result.error = error.message;
} else if (error.message === 'Invalid parameter' ||
error.message === 'Invalid result') {
// The 'invalid' case.
result.result = 'invalid';
result.error = error.message;
} else if (error.message === 'chunk(): length must be defined and non-zero') {
// The 'invalid' case.
result.result = 'invalid';
result.error = 'Invalid Message Size';
} else {
// Everything else is simply a 'fail'
result.result = 'fail';
result.error = error.message || 'No error message';
}
// This data may be missing in many cases, but we care about
// it in all cases.
try {
result.identity = this.opendkim.sig_getidentity() || '';
} catch(err) {
this.plugin.logdebug('missing identity: ' + err.message);
result.identity = '';
}
try {
result.domain = this.opendkim.sig_getdomain() || '';
} catch(err) {
this.plugin.logdebug('missing domain: ' + err.message);
result.domain = '';
}
try {
result.selector = this.opendkim.sig_getselector() || '';
} catch(err) {
this.plugin.logdebug('missing selector: ' + err.message);
result.selector = '';
}
return result;
};
OpenDKIMVerifyStream.prototype.write = function (buf) {
if (buf && buf.length) {
this.chunks.push(buf);
this.chunksLen += buf.length;
}
return true;
};
OpenDKIMVerifyStream.prototype.end = function (buf) {
if (buf && buf.length) {
// There is still a buffer, we need to tack it on.
this.chunks.push(buf);
this.chunksLen += buf.length;
}
var chunk = this._get_chunk(Buffer.concat(this.chunks, this.chunksLen));
this.chunks = [];
this.chunksLen = 0;
try {
var options = {
message: chunk,
length: chunk.length
};
this.opendkim.chunk(options)
.then(result => {
return this.opendkim.chunk_end();
}).then(result => {
this.callback(undefined, this._build_result());
}).catch(error => {
this.callback(error, this._build_result(error));
});
} catch (err) {
this.callback(err, this._build_result(err));
}
};
//
// Plugin entry point
//
exports.register = function () {
var plugin = this;
plugin.OpenDKIM = require('node-opendkim');
plugin.cfg = plugin.config.get('opendkim.ini');
plugin.cfg.general = plugin.cfg.general || {};
plugin.cfg.verify = plugin.cfg.verify || {};
plugin.cfg.sign = plugin.cfg.sign || {};
plugin.logdebug('haraka-plugin-opendkim: registering plugin...');
plugin.register_hook('data_post', 'verify', -25);
};
exports.verify = function (next, connection) {
var plugin = this;
plugin.logdebug('haraka-plugin-opendkim: hooked data_post');
if (!connection || !connection.transaction) {
return next();
}
var txn = connection.transaction;
var opendkim = new plugin.OpenDKIM();
if (this.cfg.general.query_method && this.cfg.general.query_info) {
opendkim.query_method(this.cfg.general.query_method);
opendkim.query_info(this.cfg.general.query_info);
}
opendkim.verify_sync({id: txn.uuid});
var verifier = new OpenDKIMVerifyStream(function (err, res) {
connection.auth_results(
'dkim=' + res.result +
((res.error) ? ' (' + res.error + ')' : '') +
' header.i=' + res.identity
);
connection.loginfo(plugin, 'identity="' + res.identity + '" ' +
'domain="' + res.domain + '" ' +
'selector="' + res.selector + '" ' +
'result=' + res.result +
((res.error) ? ' (' + res.error + ')' : ''));
// Add individual results to ResultStore
if (res.result === 'pass') {
txn.results.add(plugin, { pass: res.domain });
} else if (res.result === 'none') {
txn.results.add(plugin, {
skip: ((res.error) ? '(' + res.error + ')' : '')
});
} else if (res.result === 'fail') {
txn.results.add(plugin, {
fail: res.domain + ((res.error) ? ' (' + res.error + ')' : '')
});
} else {
txn.results.add(plugin, {
err: res.domain + ((res.error) ? ' (' + res.error + ')' : '')
});
}
connection.logdebug(plugin, JSON.stringify(res));
// Store results for other plugins
txn.notes.opendkim_result = res;
return process.nextTick(() => {
next();
});
}, plugin, opendkim);
txn.message_stream.pipe(verifier, { line_endings: '\r\n' });
};