generated from haraka/haraka-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
227 lines (188 loc) · 5.58 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
// relay
//
// documentation via: haraka -h relay
const net = require('node:net')
const ipaddr = require('ipaddr.js')
exports.register = function () {
this.load_relay_ini() // plugin.cfg = { }
if (this.cfg.relay.acl) {
this.load_acls() // plugin.acl_allow = [..]
this.register_hook('connect_init', 'acl')
this.register_hook('connect', 'pass_relaying')
}
if (this.cfg.relay.force_routing || this.cfg.relay.dest_domains) {
this.load_dest_domains() // plugin.dest.domains = { }
}
if (this.cfg.relay.force_routing) {
this.register_hook('get_mx', 'force_routing')
}
if (this.cfg.relay.dest_domains) {
this.register_hook('rcpt', 'dest_domains')
}
if (this.cfg.relay.all) {
this.register_hook('rcpt', 'all')
}
}
exports.load_relay_ini = function () {
this.cfg = this.config.get(
'relay.ini',
{
booleans: [
'+relay.acl',
'+relay.force_routing',
'-relay.all',
'-relay.dest_domains',
],
},
() => {
this.load_relay_ini()
},
)
}
exports.load_dest_domains = function () {
this.dest = this.config.get('relay_dest_domains.ini', 'ini', () => {
this.load_dest_domains()
})
}
exports.load_acls = function () {
const file_name = 'relay_acl_allow'
// load with a self-referential callback
this.acl_allow = this.config.get(file_name, 'list', () => {
this.load_acls()
})
for (let i = 0; i < this.acl_allow.length; i++) {
const cidr = this.acl_allow[i].split('/')
if (!net.isIP(cidr[0])) {
this.logerror(this, `invalid entry in ${file_name}: ${cidr[0]}`)
}
if (!cidr[1]) {
this.logerror(this, `appending missing CIDR suffix in: ${file_name}`)
this.acl_allow[i] = `${cidr[0]}/32`
}
}
}
exports.acl = function (next, connection) {
if (!this.cfg.relay.acl) {
return next()
}
connection.logdebug(
this,
`checking ${connection.remote.ip} in relay_acl_allow`,
)
if (!this.is_acl_allowed(connection)) {
connection.results.add(this, { skip: 'acl(unlisted)' })
return next()
}
connection.results.add(this, { pass: 'acl' })
connection.relaying = true
return next(OK)
}
exports.pass_relaying = (next, connection) => {
if (connection.relaying) return next(OK)
next()
}
exports.is_acl_allowed = function (connection) {
if (!this.acl_allow) {
return false
}
if (!this.acl_allow.length) {
return false
}
const { ip } = connection.remote
for (const item of this.acl_allow) {
connection.logdebug(this, `checking if ${ip} is in ${item}`)
const cidr = item.split('/')
const c_net = cidr[0]
const c_mask = cidr[1] || 32
if (!net.isIP(c_net)) continue // bad config entry
if (net.isIPv4(ip) && net.isIPv6(c_net)) continue
if (net.isIPv6(ip) && net.isIPv4(c_net)) continue
if (ipaddr.parse(ip).match(ipaddr.parse(c_net), c_mask)) {
connection.logdebug(this, `checking if ${ip} is in ${item}: yes`)
return true
}
}
return false
}
exports.dest_domains = function (next, connection, params) {
if (!this.cfg.relay.dest_domains) {
return next()
}
const { relaying, transaction } = connection ?? {}
if (!transaction) return next()
// Skip this if the host is already allowed to relay
if (relaying) {
transaction.results.add(this, { skip: 'relay_dest_domain(relay)' })
return next()
}
if (!this.dest) {
transaction.results.add(this, { err: 'relay_dest_domain(no config!)' })
return next()
}
if (!this.dest.domains) {
transaction.results.add(this, { skip: 'relay_dest_domain(config)' })
return next()
}
const dest_domain = params[0].host
connection.logdebug(this, `dest_domain = ${dest_domain}`)
const dst_cfg = this.dest.domains[dest_domain]
if (!dst_cfg) {
transaction.results.add(this, { fail: 'relay_dest_domain' })
return next(DENY, 'You are not allowed to relay')
}
const { action } = JSON.parse(dst_cfg)
connection.logdebug(this, `found config for ${dest_domain}: ${action}`)
switch (action) {
case 'accept':
// why enable relaying here? Returning next(OK) will allow the
// address to be considered 'local'. What advantage does relaying
// bring?
connection.relaying = true
transaction.results.add(this, { pass: 'relay_dest_domain' })
return next(OK)
case 'continue':
// why oh why? Only reason I can think of is to enable outbound.
connection.relaying = true
transaction.results.add(this, { pass: 'relay_dest_domain' })
return next(CONT) // same as next()
case 'deny':
transaction.results.add(this, { fail: 'relay_dest_domain' })
return next(DENY, 'You are not allowed to relay')
}
transaction.results.add(this, { fail: 'relay_dest_domain' })
next(DENY, 'Mail for that recipient is not accepted here.')
}
exports.force_routing = function (next, hmail, domain) {
if (!this.cfg.relay.force_routing) {
return next()
}
if (!this.dest) {
return next()
}
if (!this.dest.domains) {
return next()
}
let route = this.dest.domains[domain]
if (!route) {
route = this.dest.domains.any
if (!route) {
this.logdebug(this, `using normal MX lookup for: ${domain}`)
return next()
}
}
const { nexthop } = JSON.parse(route)
if (!nexthop) {
this.logdebug(this, `using normal MX lookup for: ${domain}`)
return next()
}
this.logdebug(this, `using ${nexthop} for: ${domain}`)
next(OK, nexthop)
}
exports.all = function (next, connection, params) {
if (!this.cfg.relay.all) {
return next()
}
connection.loginfo(this, `confirming recipient ${params[0]}`)
connection.relaying = true
next(OK)
}