-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
300 lines (254 loc) · 8.35 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
// validate an email address is local, using qmail-deliverabled
const http = require('http')
const querystring = require('querystring')
const url = require('url')
let outbound
exports.register = function () {
this.load_qmd_ini()
if (process.env.HARAKA) {
// permit testing outside of Haraka
outbound = this.haraka_require('outbound')
}
if (this.cfg.main.check_mail_from) {
this.register_hook('mail', 'check_mail_from')
}
}
exports.load_qmd_ini = function () {
this.cfg = this.config.get(
'qmail-deliverable.ini',
{
booleans: ['+main.check_mail_from', '*.check_mail_from'],
},
() => {
this.load_qmd_ini()
},
)
}
exports.check_mail_from = function (next, connection, params) {
if (!this.cfg.main.check_mail_from) return next()
// determine if MAIL FROM domain is local
const txn = connection.transaction
const email = params[0].address()
if (!email) {
// likely an IP with relaying permission
txn.results.add(this, { skip: 'mail_from.null', emit: true })
return next()
}
const domain = params[0].host.toLowerCase()
this.get_qmd_response(connection, params[0], (err, qmd_r) => {
if (err) {
txn.results.add(this, { err })
return next(DENYSOFT, err)
}
// the MAIL FROM sender is verified as a local address
if (qmd_r[0] === OK) {
txn.results.add(this, { pass: `mail_from.${qmd_r[1]}` })
txn.notes.local_sender = domain
return next()
}
if (qmd_r[0] === undefined) {
txn.results.add(this, { err: `mail_from.${qmd_r[1]}` })
return next()
}
txn.results.add(this, { msg: `mail_from.${qmd_r[1]}` })
next(CONT, `mail_from.${qmd_r[1]}`)
})
}
function do_relaying(plugin, txn, next) {
// any RCPT is acceptable for txns with relaying privileges
// this is called in several places where errors or non-local rcpt would
// otherwise not be allowed
txn.results.add(plugin, {
pass: `relaying${txn.notes.local_sender ? ' local sender' : ''}`,
})
txn.notes.set('queue.wants', 'outbound')
next(OK)
}
exports.hook_rcpt = function (next, connection, params) {
const txn = connection.transaction
const rcpt = params[0]
// Qmail::Deliverable::Client does a rfc2822 "atext" test
// but Haraka has already validated for us
this.get_qmd_response(connection, rcpt, (err, qmd_res) => {
if (err) {
if (connection.relaying) return do_relaying(this, txn, next)
txn.results.add(this, { err })
return next(DENYSOFT, 'error validating email address')
}
this.do_qmd_response(qmd_res, connection, rcpt, next)
})
}
exports.do_qmd_response = function (qmd_res, connection, rcpt, next) {
const txn = connection.transaction
const [r_code, dst_type] = qmd_res
if (r_code === undefined) {
if (connection.relaying) return do_relaying(this, txn, next)
txn.results.add(this, { err: `rcpt.${dst_type}` })
return next()
}
if (r_code !== OK) {
if (connection.relaying) return do_relaying(this, txn, next)
// no need to DENY[SOFT] for invalid addresses. If no rcpt_to.* plugin
// returns OK, then the address is not accepted.
txn.results.add(this, { msg: `rcpt.${dst_type}` })
return next(CONT, dst_type)
}
const domain = rcpt.host.toLowerCase()
const dom_cfg = this.cfg[domain] || this.cfg.main
txn.notes.local_recipient = domain
txn.results.add(this, { pass: `rcpt.${dst_type}` })
let queue = this.get_queue(domain)
let next_hop = this.get_next_hop(domain, queue)
if (dst_type === 'vpopmail dir' && next_hop) {
if (/^lmtp/.test(next_hop)) queue = 'lmtp'
next_hop = this.get_next_hop(domain, queue)
}
if (this.is_split(txn, queue, next_hop)) {
if (dom_cfg?.split === 'defer') {
if (connection.relaying) return do_relaying(this, txn, next)
return next(DENYSOFT, 'Split transaction, retry soon')
}
txn.results.add(this, { msg: `split queue.wants=outbound`, emit: true })
txn.notes.set('queue.wants', 'outbound')
delete txn.notes?.queue?.next_hop
} else {
if (!txn.notes.get('queue.wants')) {
txn.results.add(this, {
msg: `queue.wants=${queue}, next_hop=${next_hop}`,
emit: true,
})
txn.notes.set('queue.wants', queue)
txn.notes.set('queue.next_hop', next_hop)
}
}
next(OK)
}
exports.is_split = function (txn, queue, next_hop) {
if (txn.rcpt_to.length > 1) {
const qw = txn.notes.get('queue.wants')
if (qw && qw !== queue) return true
const qnh = txn.notes.get('queue.next_hop')
if (qnh && qnh !== next_hop) return true
}
return false // identical destinations
}
exports.get_next_hop = function (domain, queue) {
const hop = this.cfg[domain]?.next_hop || this.cfg.main.next_hop
if (hop) return hop
return `${queue === 'lmtp' ? 'lmtp' : 'smtp'}://${this.get_host(domain)}`
}
exports.get_queue = function (domain) {
// lmtp, outbound, smtp_forward, qmail-queue
return this.cfg[domain]?.queue || this.cfg.main.queue
}
exports.get_host = function (domain) {
return this.cfg[domain]?.host || this.cfg.main.host || '127.0.0.1'
}
exports.get_port = function (domain) {
return this.cfg[domain]?.port || this.cfg.main.port || 8998
}
exports.get_qmd_response = function (connection, addr, cb) {
const plugin = this
const domain = addr.host.toLowerCase()
const email = addr.address()
const options = {
method: 'get',
host: plugin.get_host(domain),
port: plugin.get_port(domain),
}
connection.logdebug(plugin, `checking ${email}`)
options.path = `/qd1/deliverable?${querystring.escape(email)}`
// connection.logdebug(plugin, 'PATH: ' + options.path);
http
.get(options, function (res) {
connection.logprotocol(plugin, `STATUS: ${res.statusCode}`)
connection.logprotocol(plugin, `HEADERS: ${JSON.stringify(res.headers)}`)
res.setEncoding('utf8')
res.on('data', function (chunk) {
connection.logprotocol(plugin, `BODY: ${chunk}`)
const hexnum = new Number(chunk).toString(16)
const arr = plugin.decode_qmd_response(connection, hexnum)
connection.logdebug(plugin, arr[1])
cb(undefined, arr)
})
})
.on('error', cb)
}
exports.decode_qmd_response = function (connection, hexnum) {
connection.logprotocol(this, `HEXRV: ${hexnum}`)
switch (hexnum) {
case '11':
return [DENYSOFT, 'permission failure']
case '12':
return [OK, 'qmail-command in dot-qmail']
case '13':
return [OK, 'bouncesaying with program']
case '14': {
const from = connection.transaction.mail_from.address()
if (!from || from === '<>') {
return [DENY, 'mailing lists do not accept null senders']
}
return [OK, 'ezmlm list']
}
case '21':
return [DENYSOFT, 'Temporarily undeliverable: group/world writable']
case '22':
return [DENYSOFT, 'Temporarily undeliverable: sticky home directory']
case '2f':
return [DENYSOFT, 'error communicating with qmail-deliverabled.']
case 'f1':
return [OK, 'normal delivery']
case 'f2':
return [OK, 'vpopmail dir']
case 'f3':
return [OK, 'vpopmail alias']
case 'f4':
return [OK, 'vpopmail catchall']
case 'f5':
return [OK, 'vpopmail vuser']
case 'f6':
return [OK, 'vpopmail qmail-ext']
case 'fe':
return [DENYSOFT, 'SHOULD NOT HAPPEN']
case 'ff':
return [DENY, 'not local']
case '0':
return [DENY, 'not deliverable']
default:
return [undefined, `unknown rv(${hexnum})`]
}
}
exports.hook_queue = function (next, connection) {
const qw = connection.transaction.notes.get('queue.wants')
switch (qw) {
case 'lmtp':
case 'outbound':
this.logdebug(`routing to outbound: queue.wants=${qw}`)
outbound.send_trans_email(connection.transaction, next)
break
default:
next() // do nothing
}
}
exports.hook_get_mx = function (next, hmail, domain) {
if (hmail.todo.notes.get('queue.wants') !== 'lmtp') return next()
const mx = {
using_lmtp: true,
priority: 0,
port: 24,
exchange: this.get_host(domain.toLowerCase()),
}
const nh = hmail.todo.notes.get('queue.next_hop')
if (nh) {
const dest = new url.URL(nh)
if (dest.hostname) mx.exchange = dest.hostname
if (dest.port) mx.port = dest.port
if (dest.auth) {
mx.auth_type = 'plain'
mx.auth_user = dest.auth.split(':')[0]
mx.auth_pass = dest.auth.split(':')[1]
}
}
this.logdebug(mx)
next(OK, mx)
}