-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
284 lines (242 loc) · 6.37 KB
/
Copy pathindex.js
File metadata and controls
284 lines (242 loc) · 6.37 KB
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
'use strict';
/*
Copyright (C) 2024 Mindbaz
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
--
SMTP authentication with Keycloak
Validates tokens via API calls
Auth username must be a concatenation of keycloak realm, client_id &
username, exemple : random-realm/random-client-id/random-username
*/
const fs = require ( 'fs' );
const path = require ( 'path' );
const config = require ( 'wild-config' );
/**
* All dkim keys
* @type {dict}
*/
const PRIV_KEYS = {};
/**
* Load a DKIM key : {app.config.keys_dir}/{key}.pem
*
* @param {PluginInstance} app Zone-mta instance
* @param {string} key Key file name without extention to load
*
* @returns {bool} False if an error occur during loading key. True otherwise
*/
const load_dkim_key = ( app, key ) => {
/**
* Current key dkim to load
* @type {string}
*/
let key_path = path.join (
app.config.keys_dir,
`${key}.pem`
);
app.logger.info (
'Plugins/multi-dkim',
'Load DKIM key : %s',
key_path
);
try {
PRIV_KEYS [ key ] = fs.readFileSync (
key_path,
'ascii'
).trim ();
} catch ( err ) {
app.logger.error (
'Plugins/multi-dkim',
'Failed loading key : %s : %s',
key_path,
err.message
);
return false;
}
return true;
};
/**
* Load all DKIM keys
*
* @param {PluginInstance} app Zone-mta instance
* @param {object} config Wild config
*
* @returns {bool} True if all keys are loaded. False otherwise
*/
const load_dkim_keys = ( app, config ) => {
for ( let key of config.multi_dkim.keys ) {
let is_dkim_loaded = load_dkim_key (
app,
key
);
if ( is_dkim_loaded === false ) {
return false;
}
}
return true;
};
/**
* Alias to get_dkim_key, used for recursive call
*
* @param {PluginInstance} app Zone-mta instance
* @param {dict} email_datas Email datas
*
* @returns {bool|string} Key if found & if dkim loaded. False otherwise
*/
const _get_dkim_key = ( app, email_datas ) => {
return get_dkim_key (
app,
email_datas
);
};
/**
* Get DKIM key to use
*
* @param {PluginInstance} app Zone-mta instance
* @param {dict} email_datas Email datas
*
* @returns {bool|string} Key if found & if dkim loaded. False otherwise
*/
const get_dkim_key = ( app, email_datas ) => {
if ( email_datas.headers.hasHeader ( app.config.key_header ) === false ) {
return false;
}
/**
* Current key dkim to use
* @type {string}
*/
let key = email_datas.headers.getFirst ( app.config.key_header );
if ( ( key in PRIV_KEYS ) === false ) {
let load = load_dkim_key (
app,
key
);
if ( load == true ) {
return _get_dkim_key (
app,
email_datas
);
}
return false;
}
return key;
};
/**
* Add dkim key datas on email datas
*
* @param {dict} delivery Email datas
* @param {dict} key_datas Key datas
*/
const delivery_dkim_push_key = ( delivery, key_datas ) => {
if ( ! delivery.dkim.keys ) {
delivery.dkim.keys = [];
}
delivery.dkim.keys.push ( key_datas );
};
/**
* Get from domain from email datas
*
* @param {dict} delivery Email datas
*
* @retuns {string} From domain
*/
const get_from_domain = ( delivery ) => {
/**
* Email from. RFC5322
* @type {string}
*/
let from = delivery.headers.getFirst ( 'from' );
from = from.substr (
from.lastIndexOf ( '@' ) + 1
);
from = from.split ( '>' ) [ 0 ];
return from.toLowerCase ();
};
/**
* Create header : missing dkim
*
* @param {dict} envelope Email datas
*/
const create_header_missing_dkim = ( envelope ) => {
envelope.headers.add (
'x-missing-dkim',
'Key not found'
);
};
module.exports.title = 'Multiple DKIM signer';
module.exports.init = ( app, done ) => {
/**
* Load all dkim keys
* @type {bool}
*/
let is_dkims_loaded = load_dkim_keys (
app,
config
);
if ( is_dkims_loaded === false ) {
return done ();
}
/**
* Hook : message:headers, part : receiver. Check if the email can be sign
*
* @param {dict} envelope Email datas
* @param {dict} messageInfo Email infos
* @param {function} next Callback
*/
app.addHook ( 'message:headers', ( envelope, messageInfo, next ) => {
/**
* DKIM key to use
* @type {bool|string}
*/
let key = get_dkim_key (
app,
envelope
);
if ( key === false ) {
create_header_missing_dkim (
envelope
);
}
next ();
} );
/**
* Hook : sender:connection (called for every message : even for cached connection), part : sender. Sign email
*
* @param {dict} delivery Email datas
* @param {dict} options Sender datas
* @param {function} next Callback
*/
app.addHook ( 'sender:connection', (delivery, options, next) => {
/**
* DKIM key to use
* @type {bool|string}
*/
let key = get_dkim_key (
app,
delivery
);
if ( key !== false ) {
// Store DKIM key datas
delivery_dkim_push_key (
delivery,
{
domainName: get_from_domain (
delivery
),
keySelector: app.config.selector,
privateKey: PRIV_KEYS [ key ]
}
);
}
next ();
} );
done ();
};