Haraka 3.0.3 → 3.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (239) hide show
  1. package/.eslintrc.yaml +4 -9
  2. package/CONTRIBUTORS.md +11 -0
  3. package/Changes.md +1397 -1213
  4. package/Plugins.md +117 -105
  5. package/README.md +4 -13
  6. package/bin/haraka +198 -298
  7. package/config/auth_flat_file.ini +1 -0
  8. package/config/dhparams.pem +8 -0
  9. package/config/mail_from.is_resolvable.ini +4 -2
  10. package/config/me +1 -0
  11. package/config/outbound.ini +0 -2
  12. package/config/plugins +35 -36
  13. package/config/smtp.ini +1 -1
  14. package/config/smtp.json +17 -0
  15. package/config/tls.ini +2 -0
  16. package/config/tls_cert.pem +23 -0
  17. package/config/tls_key.pem +28 -0
  18. package/connection.js +46 -73
  19. package/contrib/bsd-rc.d/haraka +3 -1
  20. package/contrib/plugin2npm.sh +6 -36
  21. package/docs/CoreConfig.md +2 -2
  22. package/docs/Logging.md +7 -21
  23. package/docs/Outbound.md +104 -201
  24. package/docs/Plugins.md +2 -2
  25. package/docs/Transaction.md +59 -82
  26. package/docs/plugins/queue/smtp_proxy.md +5 -10
  27. package/docs/plugins/tls.md +37 -9
  28. package/endpoint.js +16 -13
  29. package/haraka.js +10 -14
  30. package/host_pool.js +5 -5
  31. package/http/html/index.html +6 -5
  32. package/line_socket.js +3 -4
  33. package/logger.js +44 -28
  34. package/outbound/client_pool.js +27 -23
  35. package/outbound/config.js +4 -6
  36. package/outbound/fsync_writestream.js +1 -1
  37. package/outbound/hmail.js +178 -218
  38. package/outbound/index.js +86 -99
  39. package/outbound/qfile.js +1 -1
  40. package/outbound/queue.js +51 -44
  41. package/outbound/timer_queue.js +3 -2
  42. package/outbound/tls.js +19 -7
  43. package/package.json +60 -51
  44. package/plugins/.eslintrc.yaml +0 -6
  45. package/plugins/auth/auth_base.js +4 -2
  46. package/plugins/auth/auth_proxy.js +14 -12
  47. package/plugins/auth/auth_vpopmaild.js +1 -1
  48. package/plugins/block_me.js +1 -1
  49. package/plugins/data.signatures.js +2 -4
  50. package/plugins/early_talker.js +2 -1
  51. package/plugins/mail_from.is_resolvable.js +65 -135
  52. package/plugins/queue/deliver.js +4 -5
  53. package/plugins/queue/lmtp.js +11 -12
  54. package/plugins/queue/qmail-queue.js +2 -2
  55. package/plugins/queue/quarantine.js +2 -2
  56. package/plugins/queue/rabbitmq.js +16 -17
  57. package/plugins/queue/smtp_forward.js +3 -3
  58. package/plugins/queue/smtp_proxy.js +10 -1
  59. package/plugins/queue/test.js +2 -2
  60. package/plugins/rcpt_to.host_list_base.js +5 -5
  61. package/plugins/rcpt_to.in_host_list.js +2 -2
  62. package/plugins/relay.js +6 -7
  63. package/plugins/reseed_rng.js +1 -1
  64. package/plugins/status.js +37 -33
  65. package/plugins/tls.js +2 -2
  66. package/plugins/xclient.js +3 -2
  67. package/plugins.js +50 -54
  68. package/run_tests +3 -30
  69. package/server.js +190 -190
  70. package/smtp_client.js +30 -23
  71. package/{tests → test}/config/plugins +0 -2
  72. package/{tests → test}/config/smtp.ini +3 -1
  73. package/test/config/tls/example.com/_.example.com.key +28 -0
  74. package/test/config/tls/example.com/example.com.crt +25 -0
  75. package/{tests/loud → test}/config/tls.ini +4 -2
  76. package/test/connection.js +302 -0
  77. package/test/endpoint.js +94 -0
  78. package/{tests → test}/fixtures/line_socket.js +1 -1
  79. package/{tests → test}/fixtures/util_hmailitem.js +19 -25
  80. package/{tests → test}/host_pool.js +42 -57
  81. package/test/logger.js +258 -0
  82. package/test/outbound/hmail.js +141 -0
  83. package/test/outbound/index.js +220 -0
  84. package/test/outbound/qfile.js +126 -0
  85. package/test/outbound_bounce_net_errors.js +142 -0
  86. package/{tests → test}/outbound_bounce_rfc3464.js +110 -122
  87. package/test/plugins/auth/auth_base.js +484 -0
  88. package/test/plugins/auth/auth_vpopmaild.js +83 -0
  89. package/test/plugins/early_talker.js +104 -0
  90. package/test/plugins/mail_from.is_resolvable.js +35 -0
  91. package/test/plugins/queue/smtp_forward.js +206 -0
  92. package/test/plugins/rcpt_to.host_list_base.js +122 -0
  93. package/test/plugins/rcpt_to.in_host_list.js +193 -0
  94. package/test/plugins/relay.js +303 -0
  95. package/test/plugins/status.js +130 -0
  96. package/test/plugins/tls.js +70 -0
  97. package/test/plugins.js +228 -0
  98. package/test/rfc1869.js +73 -0
  99. package/test/server.js +491 -0
  100. package/test/smtp_client.js +299 -0
  101. package/test/tls_socket.js +277 -0
  102. package/test/transaction.js +270 -0
  103. package/tls_socket.js +202 -252
  104. package/transaction.js +8 -23
  105. package/CONTRIBUTING.md +0 -1
  106. package/bin/dkimverify +0 -40
  107. package/config/access.domains +0 -13
  108. package/config/attachment.ctype.regex +0 -2
  109. package/config/attachment.filename.regex +0 -1
  110. package/config/avg.ini +0 -5
  111. package/config/bounce.ini +0 -15
  112. package/config/data.headers.ini +0 -61
  113. package/config/dkim/dkim_key_gen.sh +0 -78
  114. package/config/dkim_sign.ini +0 -4
  115. package/config/dkim_verify.ini +0 -7
  116. package/config/dnsbl.ini +0 -23
  117. package/config/greylist.ini +0 -43
  118. package/config/helo.checks.ini +0 -52
  119. package/config/messagesniffer.ini +0 -18
  120. package/config/spamassassin.ini +0 -56
  121. package/dkim.js +0 -614
  122. package/docs/plugins/avg.md +0 -35
  123. package/docs/plugins/bounce.md +0 -69
  124. package/docs/plugins/clamd.md +0 -147
  125. package/docs/plugins/esets.md +0 -8
  126. package/docs/plugins/greylist.md +0 -90
  127. package/docs/plugins/helo.checks.md +0 -135
  128. package/docs/plugins/messagesniffer.md +0 -163
  129. package/docs/plugins/spamassassin.md +0 -180
  130. package/outbound/mx_lookup.js +0 -70
  131. package/plugins/auth/auth_ldap.js +0 -3
  132. package/plugins/avg.js +0 -162
  133. package/plugins/backscatterer.js +0 -25
  134. package/plugins/bounce.js +0 -381
  135. package/plugins/clamd.js +0 -382
  136. package/plugins/data.uribl.js +0 -4
  137. package/plugins/dkim_sign.js +0 -395
  138. package/plugins/dkim_verify.js +0 -62
  139. package/plugins/dns_list_base.js +0 -221
  140. package/plugins/dnsbl.js +0 -146
  141. package/plugins/dnswl.js +0 -58
  142. package/plugins/esets.js +0 -71
  143. package/plugins/graph.js +0 -5
  144. package/plugins/greylist.js +0 -645
  145. package/plugins/helo.checks.js +0 -533
  146. package/plugins/messagesniffer.js +0 -381
  147. package/plugins/rcpt_to.ldap.js +0 -3
  148. package/plugins/rcpt_to.max_count.js +0 -24
  149. package/plugins/spamassassin.js +0 -384
  150. package/tests/config/dkim/example.com/dns +0 -29
  151. package/tests/config/dkim/example.com/private +0 -6
  152. package/tests/config/dkim/example.com/public +0 -4
  153. package/tests/config/dkim/example.com/selector +0 -1
  154. package/tests/config/dkim.private.key +0 -6
  155. package/tests/config/dkim_sign.ini +0 -4
  156. package/tests/config/helo.checks.ini +0 -52
  157. package/tests/connection.js +0 -327
  158. package/tests/endpoint.js +0 -128
  159. package/tests/fixtures/vm_harness.js +0 -59
  160. package/tests/logger.js +0 -327
  161. package/tests/outbound/hmail.js +0 -112
  162. package/tests/outbound/index.js +0 -324
  163. package/tests/outbound/qfile.js +0 -67
  164. package/tests/outbound_bounce_net_errors.js +0 -173
  165. package/tests/plugins/auth/auth_base.js +0 -463
  166. package/tests/plugins/auth/auth_vpopmaild.js +0 -91
  167. package/tests/plugins/bounce.js +0 -307
  168. package/tests/plugins/clamd.js +0 -224
  169. package/tests/plugins/deprecated/relay_acl.js +0 -140
  170. package/tests/plugins/deprecated/relay_all.js +0 -59
  171. package/tests/plugins/dkim_sign.js +0 -315
  172. package/tests/plugins/dkim_signer.js +0 -108
  173. package/tests/plugins/dns_list_base.js +0 -259
  174. package/tests/plugins/dnsbl.js +0 -101
  175. package/tests/plugins/early_talker.js +0 -115
  176. package/tests/plugins/greylist.js +0 -58
  177. package/tests/plugins/helo.checks.js +0 -525
  178. package/tests/plugins/mail_from.is_resolvable.js +0 -116
  179. package/tests/plugins/queue/smtp_forward.js +0 -221
  180. package/tests/plugins/rcpt_to.host_list_base.js +0 -132
  181. package/tests/plugins/rcpt_to.in_host_list.js +0 -218
  182. package/tests/plugins/relay.js +0 -339
  183. package/tests/plugins/spamassassin.js +0 -171
  184. package/tests/plugins/status.js +0 -138
  185. package/tests/plugins/tls.js +0 -84
  186. package/tests/plugins.js +0 -247
  187. package/tests/rfc1869.js +0 -61
  188. package/tests/server.js +0 -510
  189. package/tests/smtp_client/auth.js +0 -105
  190. package/tests/smtp_client/basic.js +0 -101
  191. package/tests/smtp_client.js +0 -80
  192. package/tests/tls_socket.js +0 -333
  193. package/tests/transaction.js +0 -284
  194. /package/docs/{plugins → deprecated}/dkim_sign.md +0 -0
  195. /package/docs/{plugins → deprecated}/dkim_verify.md +0 -0
  196. /package/docs/{plugins → deprecated}/dnsbl.md +0 -0
  197. /package/docs/{plugins → deprecated}/dnswl.md +0 -0
  198. /package/{tests → test}/.eslintrc.yaml +0 -0
  199. /package/{tests → test}/config/auth_flat_file.ini +0 -0
  200. /package/{tests → test}/config/dhparams.pem +0 -0
  201. /package/{tests → test}/config/host_list +0 -0
  202. /package/{tests → test}/config/outbound_tls_cert.pem +0 -0
  203. /package/{tests → test}/config/outbound_tls_key.pem +0 -0
  204. /package/{tests → test}/config/smtp_forward.ini +0 -0
  205. /package/{tests → test}/config/tls/ec.pem +0 -0
  206. /package/{tests → test}/config/tls/haraka.local.pem +0 -0
  207. /package/{tests → test}/config/tls/mismatched.pem +0 -0
  208. /package/{tests → test}/config/tls_cert.pem +0 -0
  209. /package/{tests → test}/config/tls_key.pem +0 -0
  210. /package/{tests → test}/fixtures/todo_qfile.txt +0 -0
  211. /package/{tests → test}/installation/config/test-plugin-flat +0 -0
  212. /package/{tests → test}/installation/config/test-plugin.ini +0 -0
  213. /package/{tests → test}/installation/config/tls.ini +0 -0
  214. /package/{tests → test}/installation/node_modules/load_first/index.js +0 -0
  215. /package/{tests → test}/installation/node_modules/load_first/package.json +0 -0
  216. /package/{tests → test}/installation/node_modules/test-plugin/config/test-plugin-flat +0 -0
  217. /package/{tests → test}/installation/node_modules/test-plugin/config/test-plugin.ini +0 -0
  218. /package/{tests → test}/installation/node_modules/test-plugin/package.json +0 -0
  219. /package/{tests → test}/installation/node_modules/test-plugin/test-plugin.js +0 -0
  220. /package/{tests → test}/installation/plugins/base_plugin.js +0 -0
  221. /package/{tests → test}/installation/plugins/folder_plugin/index.js +0 -0
  222. /package/{tests → test}/installation/plugins/folder_plugin/package.json +0 -0
  223. /package/{tests → test}/installation/plugins/inherits.js +0 -0
  224. /package/{tests → test}/installation/plugins/load_first.js +0 -0
  225. /package/{tests → test}/installation/plugins/plugin.js +0 -0
  226. /package/{tests → test}/installation/plugins/tls.js +0 -0
  227. /package/{tests → test}/loud/config/dhparams.pem +0 -0
  228. /package/{tests → test}/loud/config/tls/goobered.pem +0 -0
  229. /package/{tests → test/loud}/config/tls.ini +0 -0
  230. /package/{tests → test}/mail_specimen/base64-root-part.txt +0 -0
  231. /package/{tests → test}/mail_specimen/varied-fold-lengths-preserve-data.txt +0 -0
  232. /package/{tests → test}/queue/1507509981169_1507509981169_0_61403_e0Y0Ym_1_fixed +0 -0
  233. /package/{tests → test}/queue/1507509981169_1507509981169_0_61403_e0Y0Ym_1_haraka +0 -0
  234. /package/{tests → test}/queue/1508269674999_1508269674999_0_34002_socVUF_1_haraka +0 -0
  235. /package/{tests → test}/queue/1508455115683_1508455115683_0_90253_9Q4o4V_1_haraka +0 -0
  236. /package/{tests → test}/queue/multibyte +0 -0
  237. /package/{tests → test}/queue/plain +0 -0
  238. /package/{tests → test}/queue/zero-length +0 -0
  239. /package/{tests → test}/test-queue/delete-me +0 -0
package/plugins/bounce.js DELETED
@@ -1,381 +0,0 @@
1
- // bounce tests
2
- const tlds = require('haraka-tld');
3
- const { SPF } = require('haraka-plugin-spf');
4
-
5
- const net_utils = require('haraka-net-utils');
6
-
7
- exports.register = function () {
8
- this.load_bounce_ini();
9
- this.load_bounce_bad_rcpt();
10
-
11
- this.register_hook('mail', 'reject_all');
12
- this.register_hook('data', 'single_recipient');
13
- this.register_hook('data', 'bad_rcpt');
14
- this.register_hook('data_post', 'empty_return_path');
15
- this.register_hook('data', 'bounce_spf_enable');
16
- this.register_hook('data_post', 'bounce_spf');
17
- this.register_hook('data_post', 'non_local_msgid');
18
- }
19
-
20
- exports.load_bounce_bad_rcpt = function () {
21
-
22
- const new_list = this.config.get('bounce_bad_rcpt', 'list', () => {
23
- this.load_bounce_bad_rcpt();
24
- });
25
-
26
- const invalids = {};
27
- for (const element of new_list) {
28
- invalids[element] = true;
29
- }
30
-
31
- this.cfg.invalid_addrs = invalids;
32
- }
33
-
34
- exports.load_bounce_ini = function () {
35
- this.cfg = this.config.get('bounce.ini', {
36
- booleans: [
37
- '-check.reject_all',
38
- '+check.single_recipient',
39
- '-check.empty_return_path',
40
- '+check.bad_rcpt',
41
- '+check.bounce_spf',
42
- '+check.non_local_msgid',
43
-
44
- '+reject.single_recipient',
45
- '-reject.empty_return_path',
46
- '-reject.bounce_spf',
47
- '-reject.non_local_msgid',
48
- ],
49
- }, () => {
50
- this.load_bounce_ini();
51
- });
52
-
53
- // Legacy config handling
54
- if (this.cfg.main.reject_invalid) {
55
- this.logerror('bounce.ini is out of date, please update!');
56
- this.cfg.check.single_recipient=true;
57
- this.cfg.reject.single_recipient=true;
58
- }
59
-
60
- if (this.cfg.main.reject_all) {
61
- this.logerror('bounce.ini is out of date, please update!');
62
- this.cfg.check.reject_all=true;
63
- }
64
- }
65
-
66
- exports.reject_all = function (next, connection, params) {
67
- if (!this.cfg.check.reject_all) return next();
68
-
69
- const mail_from = params[0];
70
- // bounce messages are from null senders
71
- if (!this.has_null_sender(connection, mail_from)) return next();
72
-
73
- connection.transaction.results.add(this, {fail: 'bounces_accepted', emit: true });
74
- return next(DENY, 'No bounces accepted here');
75
- }
76
-
77
- exports.single_recipient = function (next, connection) {
78
- if (!this?.cfg?.check?.single_recipient) return next();
79
- if (!this?.has_null_sender(connection)) return next();
80
- const { transaction, relaying, remote } = connection;
81
-
82
- // Valid bounces have a single recipient
83
- if (transaction.rcpt_to.length === 1) {
84
- transaction.results.add(this, {pass: 'single_recipient', emit: true });
85
- return next();
86
- }
87
-
88
- // Skip this check for relays or private_ips
89
- // This is because Microsoft Exchange will send mail
90
- // to distribution groups using the null-sender if
91
- // the option 'Do not send delivery reports' is
92
- // checked (not sure if this is default or not)
93
- if (relaying) {
94
- transaction.results.add(this, {skip: 'single_recipient(relay)', emit: true });
95
- return next();
96
- }
97
- if (remote.is_private) {
98
- transaction.results.add(this, {skip: 'single_recipient(private_ip)', emit: true });
99
- return next();
100
- }
101
-
102
- connection.loginfo(this, `bounce with too many recipients to: ${transaction.rcpt_to.join(',')}`);
103
-
104
- transaction.results.add(this, {fail: 'single_recipient', emit: true });
105
-
106
- if (!this.cfg.reject.single_recipient) return next();
107
-
108
- return next(DENY, 'this bounce message does not have 1 recipient');
109
- }
110
-
111
- exports.empty_return_path = function (next, connection) {
112
- if (!this.cfg.check.empty_return_path) return next();
113
- if (!this.has_null_sender(connection)) return next();
114
-
115
- const { transaction } = connection;
116
- // Bounce messages generally do not have a Return-Path set. This checks
117
- // for that. But whether it should is worth questioning...
118
-
119
- // On Jan 20, 2014, Matt Simerson examined the most recent 50,000 mail
120
- // connections for the presence of Return-Path in bounce messages. I
121
- // found 14 hits, 12 of which were from Google, in response to
122
- // undeliverable DMARC reports (IE, automated messages that Google
123
- // shouldn't have replied to). Another appears to be a valid bounce from
124
- // a poorly configured mailer, and the 14th was a confirmed spam kill.
125
- // Unless new data demonstrate otherwise, this should remain disabled.
126
-
127
- // Return-Path, aka Reverse-PATH, Envelope FROM, RFC5321.MailFrom
128
- // validate that the Return-Path header is empty, RFC 3834
129
-
130
- const rp = transaction.header.get('Return-Path');
131
- if (!rp) {
132
- transaction.results.add(this, {pass: 'empty_return_path' });
133
- return next();
134
- }
135
-
136
- if (rp === '<>') {
137
- transaction.results.add(this, {pass: 'empty_return_path' });
138
- return next();
139
- }
140
-
141
- transaction.results.add(this, {fail: 'empty_return_path', emit: true });
142
- return next(DENY, 'bounce with non-empty Return-Path (RFC 3834)');
143
- }
144
-
145
- exports.bad_rcpt = function (next, connection) {
146
- if (!this.cfg.check.bad_rcpt) return next();
147
- if (!this.has_null_sender(connection)) return next();
148
- if (!this.cfg.invalid_addrs) return next();
149
-
150
- const { transaction } = connection;
151
- for (const element of transaction.rcpt_to) {
152
- const rcpt = element.address();
153
- if (!this.cfg.invalid_addrs[rcpt]) continue;
154
- transaction.results.add(this, {fail: 'bad_rcpt', emit: true });
155
- return next(DENY, 'That recipient does not accept bounces');
156
- }
157
- transaction.results.add(this, {pass: 'bad_rcpt'});
158
-
159
- return next();
160
- }
161
-
162
- exports.has_null_sender = function (connection, mail_from) {
163
- // ok ?
164
- const transaction = connection?.transaction;
165
- if (!transaction) return false;
166
-
167
- if (!mail_from) mail_from = transaction.mail_from;
168
-
169
- // bounces have a null sender.
170
- // null sender could also be tested with mail_from.user
171
- // Why would isNull() exist if it wasn't the right way to test this?
172
- if (mail_from.isNull()) {
173
- transaction.results.add(this, {isa: 'yes'});
174
- return true;
175
- }
176
-
177
- transaction.results.add(this, {isa: 'no'});
178
- return false;
179
- }
180
-
181
- const message_id_re = /^Message-ID:\s*(<?[^>]+>?)/mig;
182
-
183
- function find_message_id_headers (headers, body, connection, self) {
184
- if (!body) return;
185
-
186
- let match;
187
- while ((match = message_id_re.exec(body.bodytext))) {
188
- const mid = match[1];
189
- headers[mid] = true;
190
- }
191
-
192
- for (let i=0,l=body.children.length; i < l; i++) {
193
- // Recure to any MIME children
194
- find_message_id_headers(headers, body.children[i], connection, self);
195
- }
196
- }
197
-
198
- exports.non_local_msgid = function (next, connection) {
199
- if (!this.cfg.check.non_local_msgid) return next();
200
- if (!this.has_null_sender(connection)) return next();
201
-
202
- const transaction = connection?.transaction;
203
- if (!transaction) return next();
204
- // Bounce messages usually contain the headers of the original message
205
- // in the body. This parses the body, searching for the Message-ID header.
206
- // It then inspects the contents of that header, extracting the domain part,
207
- // and then checks to see if that domain is local to this server.
208
-
209
- // NOTE: this only works reliably if *every* message sent has a local
210
- // domain in the Message-ID. In practice, that means outbound MXes MUST
211
- // check Message-ID on outbound and modify non-conforming Message-IDs.
212
- //
213
- // NOTE 2: Searching the bodytext of a bounce is too simple. The bounce
214
- // message should exist as a MIME Encoded part. See here for ideas
215
- // http://lamsonproject.org/blog/2009-07-09.html
216
- // http://lamsonproject.org/docs/bounce_detection.html
217
-
218
- let matches = {}
219
- find_message_id_headers(matches, transaction.body, connection, this);
220
- matches = Object.keys(matches);
221
- connection.logdebug(this, `found Message-IDs: ${matches.join(', ')}`);
222
-
223
- if (!matches.length) {
224
- connection.loginfo(this, 'no Message-ID matches');
225
- transaction.results.add(this, { fail: 'Message-ID' });
226
- if (!this.cfg.reject.non_local_msgid) return next();
227
- return next(DENY, `bounce without Message-ID in headers, unable to verify that I sent it`);
228
- }
229
-
230
- const domains=[];
231
- for (const match of matches) {
232
- const res = match.match(/@([^>]*)>?/i);
233
- if (!res) continue;
234
- domains.push(res[1]);
235
- }
236
-
237
- if (domains.length === 0) {
238
- connection.loginfo(this, 'no domain(s) parsed from Message-ID headers');
239
- transaction.results.add(this, { fail: 'Message-ID parseable' });
240
- if (!this.cfg.reject.non_local_msgid) return next();
241
- return next(DENY, `bounce with invalid Message-ID, I didn't send it.`);
242
- }
243
-
244
- connection.logdebug(this, domains);
245
-
246
- const valid_domains=[];
247
- for (const domain of domains) {
248
- const org_dom = tlds.get_organizational_domain(domain);
249
- if (!org_dom) { continue; }
250
- valid_domains.push(org_dom);
251
- }
252
-
253
- if (valid_domains.length === 0) {
254
- transaction.results.add(this, { fail: 'Message-ID valid domain' });
255
- if (!this.cfg.reject.non_local_msgid) return next();
256
- return next(DENY, `bounce Message-ID without valid domain, I didn't send it.`);
257
- }
258
-
259
- return next();
260
-
261
- /* The code below needs some kind of test to say the domain isn't local.
262
- this would be hard to do without knowing how you have Haraka configured.
263
- e.g. it could be config/host_list, or it could be some other way.
264
- - hence I added the return next() above or this test can never be correct.
265
- */
266
- // we wouldn't have accepted the bounce if the recipient wasn't local
267
- // transaction.results.add(plugin,
268
- // {fail: 'Message-ID not local', emit: true });
269
- // if (!plugin.cfg.reject.non_local_msgid) return next();
270
- // return next(DENY, "bounce with non-local Message-ID (RFC 3834)");
271
- }
272
-
273
- // Lazy regexp to get IPs from Received: headers in bounces
274
- const received_re = net_utils.get_ipany_re('^Received:[\\s\\S]*?[\\[\\(](?:IPv6:)?', '[\\]\\)]');
275
-
276
- function find_received_headers (ips, body, connection, self) {
277
- if (!body) return;
278
- let match;
279
- while ((match = received_re.exec(body.bodytext))) {
280
- const ip = match[1];
281
- if (net_utils.is_private_ip(ip)) continue;
282
- ips[ip] = true;
283
- }
284
- for (let i=0,l=body.children.length; i < l; i++) {
285
- // Recurse in any MIME children
286
- find_received_headers(ips, body.children[i], connection, self);
287
- }
288
- }
289
-
290
- exports.bounce_spf_enable = function (next, connection) {
291
- if (!connection.transaction) return next();
292
- if (this.cfg.check.bounce_spf) {
293
- connection.transaction.parse_body = true;
294
- }
295
- return next();
296
- }
297
-
298
- exports.bounce_spf = function (next, connection) {
299
- if (!this.cfg.check.bounce_spf) return next();
300
- if (!this.has_null_sender(connection)) return next();
301
-
302
- const txn = connection?.transaction;
303
- if (!txn) return next();
304
-
305
- // Recurse through all textual parts and store all parsed IPs
306
- // in an object to remove any duplicates which might appear.
307
- let ips = {};
308
- find_received_headers(ips, txn.body, connection, this);
309
- ips = Object.keys(ips);
310
- if (!ips.length) {
311
- connection.loginfo(this, 'No received headers found in message');
312
- return next();
313
- }
314
-
315
- connection.logdebug(this, `found IPs to check: ${ips.join(', ')}`);
316
-
317
- let pending = 0;
318
- let aborted = false;
319
- let called_cb = false;
320
- let timer;
321
-
322
- function run_cb (abort, retval, msg) {
323
- if (aborted) return;
324
- if (abort) aborted = true;
325
- if (!aborted && pending > 0) return;
326
- if (called_cb) return;
327
- clearTimeout(timer);
328
- called_cb = true;
329
- return next(retval, msg);
330
- }
331
-
332
- timer = setTimeout(() => {
333
- connection.logerror(this, 'Timed out');
334
- txn.results.add(this, { skip: 'bounce_spf(timeout)' });
335
- return run_cb(true);
336
- }, (this.timeout - 1) * 1000);
337
-
338
- ips.forEach(ip => {
339
- if (aborted) return;
340
- const spf = new SPF();
341
- pending++;
342
- spf.check_host(ip, txn.rcpt_to[0].host, txn.rcpt_to[0].address(),
343
- (err, result) => {
344
- if (aborted) return;
345
- pending--;
346
- if (err) {
347
- connection.logerror(this, err.message);
348
- return run_cb();
349
- }
350
- connection.logdebug(this, `ip=${ip} spf_result=${spf.result(result)}`);
351
- switch (result) {
352
- case (spf.SPF_NONE):
353
- // falls through, domain doesn't publish an SPF record
354
- case (spf.SPF_TEMPERROR):
355
- case (spf.SPF_PERMERROR):
356
- // Abort as all subsequent lookups will return this
357
- connection.logdebug(this, `Aborted: SPF returned ${spf.result(result)}`);
358
- txn.results.add(this, { skip: 'bounce_spf' });
359
- return run_cb(true);
360
- case (spf.SPF_PASS):
361
- // Presume this is a valid bounce
362
- // TODO: this could be spoofed; could weight each IP to combat
363
- connection.loginfo(this, `Valid bounce originated from ${ip}`);
364
- txn.results.add(this, { pass: 'bounce_spf' });
365
- return run_cb(true);
366
- }
367
- if (pending === 0 && !aborted) {
368
- // We've checked all the IPs and none of them returned Pass
369
- txn.results.add(this, {fail: 'bounce_spf', emit: true });
370
- if (!this.cfg.reject.bounce_spf) return run_cb();
371
- return run_cb(false, DENY, 'Invalid bounce (spoofed sender)');
372
- }
373
- }
374
- );
375
- if (pending === 0 && !aborted) {
376
- // No lookups run for some reason
377
- return run_cb();
378
- }
379
- });
380
- }
381
-