-
Notifications
You must be signed in to change notification settings - Fork 0
/
distributionlistplugin.js
247 lines (188 loc) · 7.84 KB
/
distributionlistplugin.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
const ldap = require('ldapjs');
let Address;
class DistributionListPlugin {
constructor (plugin, require) {
this.plugin = plugin;
Address = require('address-rfc2821').Address;
this.outbound = require('outbound');
this.lookup_table = {};
this.is_loaded = false;
this.ldap_loading_interval = null;
}
register () {
this.plugin.register_hook('rcpt', this.onRcpt.bind(this));
this.plugin.register_hook('queue', this.onQueue.bind(this));
this.plugin.config.get('distribution-list-ldap.yaml', 'yaml', () => {
this.loadConfig();
});
this.loadConfig();
}
loadConfig () {
this.cfg = this.plugin.config.get('distribution-list-ldap.yaml');
this.loadLdapConfiguration();
if (this.ldap_loading_interval) {
clear_interval(this.ldap_loading_interval);
}
var interval = (this.cfg.settings.refresh_interval || 600) * 1000;
this.ldap_loading_interval = setInterval(this.loadLdapConfiguration.bind(this), interval);
this.plugin.loginfo('Set reloading interval to ' + interval + ' ms');
}
async getLdapConnection () {
return new Promise((resolve, reject) => {
if (this.ldapConnection) {
return this.ldapConnection;
}
const conn = ldap.createClient({ url: this.cfg.settings.url });
conn.bind(this.cfg.settings.bind.dn, this.cfg.settings.bind.pw, (err) => {
if (err) {
return reject(err);
}
resolve(conn);
});
});
}
async ldapSearch (client, filter) {
const rawTypes = [ 'objectGUID' ];
const arrayTypes = [ 'member', 'proxyAddresses' ]
return new Promise((resolve, reject) => {
const options = { scope: 'sub', attributes: [ 'dn', 'objectGUID', 'member', 'mail', 'proxyAddresses', 'groupType' ], filter: filter };
client.search(this.cfg.settings.basedn, options, (err, emitter) => {
if (err) {
return reject(err);
}
const items = [];
emitter.on('searchEntry', (entry) => {
const object = { dn: entry.dn };
for (let i = 0; i < entry.attributes.length; i++) {
const attr = entry.attributes[i];
if (rawTypes.indexOf(attr.type) !== -1) {
object[attr.type] = attr.buffers[0].toString('hex');
} else if (arrayTypes.indexOf(attr.type) !== -1) {
object[attr.type] = (attr.vals instanceof Array ? attr.vals : [ attr.vals ]);
} else {
object[attr.type] = attr.vals[0];
}
}
items.push(object);
});
emitter.on('error', (err) => {
reject(err);
});
emitter.on('end', (result) => {
resolve(items);
});
});
});
}
assignEmailToObject(container, object, mail) {
// No op, we don't do anything if empty
if (!mail) {
return;
}
if (mail in container) {
this.log('Object has double address: ', mail, object);
} else {
container[mail] = object;
}
}
async loadLdapConfiguration () {
try {
const time_start = Date.now();
const client = await this.getLdapConnection();
// load all groups
const groups = await this.ldapSearch(client, this.cfg.groups.filter);
// load all users
const users = await this.ldapSearch(client, this.cfg.users.filter);
const time_assign = Date.now()
// build dictionary of groups:
const final_emails = {};
const dn_map_users = {};
// build dictionary for users
for (let i = 0; i < users.length; i++) {
const user = users[i];
dn_map_users[user.dn] = user;
this.assignEmailToObject(final_emails, user, user.mail);
if (user.proxyAddresses) {
for (let x = 0; x < user.proxyAddresses.length; x++) {
this.assignEmailToObject(final_emails, user, user.proxyAddresses[x]);
}
}
}
for (let i = 0; i < groups.length; i++) {
const group = groups[i];
this.assignEmailToObject(final_emails, group, group.mail);
if (group.proxyAddresses) {
for (let x = 0; x < group.proxyAddresses.length; x++) {
this.assignEmailToObject(final_emails, group, group.proxyAddresses[x]);
}
}
// Security Groups don't resolve to all members, only distribution lists
if (group.member && (((group.groupType & 0x80000000) >>> 0) == 0)) {
group.members_resolved = [];
group.is_distribution_list = true;
for (let x = 0; x < group.member.length; x++) {
if (group.member[x] in dn_map_users) {
group.members_resolved.push(dn_map_users[group.member[x]].mail);
} else {
this.plugin.logerror('Unable to find member in users list: ', group.dn, group.member[x]);
}
}
} else {
group.is_distribution_list = false;
}
}
const time_done = Date.now();
this.plugin.logdebug('Ldap Configuration Update took: ' + (time_done - time_start) + ' ms, Assign: ' + (time_done - time_assign) + ' ms');
this.lookup_table = final_emails;
this.is_loaded = true;
} catch (err) {
console.log(err);
}
}
onQueue (next, connection, params) {
const plugin = this.plugin;
const txn = connection.transaction;
const results = txn.results.get('distribution-list-ldap');
if (!results || !results.recipients || !results.recipients[0] || !results.recipients[0].is_distribution_list) {
return next();
}
const recipient = results.recipients[0];
let members = recipient.members_resolved;
txn.rcpt_to = members;
next();
}
onRcpt (next, connection, params) {
const plugin = this.plugin;
// verify if we got a transaction
const txn = connection.transaction;
if (!txn) {
return next();
}
// in case there's more than one recipient, log an error
if (txn.rcpt_to.length > 1) {
connection.logerror(plugin, 'Received more than one recipient!');
return next();
}
const rcpt = txn.rcpt_to[txn.rcpt_to.length - 1];
// check if we got a host part
if (!rcpt.host) {
txn.results.add(plugin, { fail: '!domain' });
return next();
}
if (!this.is_loaded) {
connection.logerror(plugin, 'Not loaded yet');
return next(DENYSOFT, 'Backend failure. Please retry later.');
}
const plain_rcpt = rcpt.address().toLowerCase();
// check if we can receive the mentioned email
if (plain_rcpt in this.lookup_table) {
txn.results.add(plugin, { recipients: [ this.lookup_table[plain_rcpt] ] });
next(OK);
} else if (this.cfg.settings.deny_unknown) {
next(DENY, 'Sorry - no mailbox here by that name');
} else {
next();
}
}
}
module.exports = DistributionListPlugin;