emailengine-app 2.79.9 → 2.80.0
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.
- package/.release-please-manifest.json +1 -1
- package/CHANGELOG.md +49 -0
- package/config/default.toml +5 -1
- package/data/google-crawlers.json +1 -1
- package/encrypt.js +43 -0
- package/lib/account/account-state.js +62 -0
- package/lib/account.js +34 -2
- package/lib/api-routes/account-routes.js +20 -0
- package/lib/api-routes/index.js +1 -1
- package/lib/api-routes/settings-routes.js +17 -2
- package/lib/autodetect-imap-settings.js +349 -47
- package/lib/cert-handler.js +62 -0
- package/lib/consts.js +50 -6
- package/lib/db.js +12 -0
- package/lib/egress-fetch.js +21 -9
- package/lib/email-client/base-client.js +234 -66
- package/lib/email-client/credential-errors.js +118 -0
- package/lib/email-client/gmail/gmail-api.js +2 -1
- package/lib/email-client/gmail-client.js +70 -64
- package/lib/email-client/imap/connection-errors.js +52 -0
- package/lib/email-client/imap/listing-diff.js +47 -6
- package/lib/email-client/imap/mailbox.js +457 -379
- package/lib/email-client/imap/subconnection.js +15 -11
- package/lib/email-client/imap/sync-operations.js +46 -59
- package/lib/email-client/imap-client.js +136 -95
- package/lib/email-client/message-builder.js +11 -11
- package/lib/email-client/outlook/graph-api.js +2 -1
- package/lib/email-client/outlook-client.js +219 -106
- package/lib/imap-proxy-auth.js +58 -3
- package/lib/imapproxy/imap-server.js +53 -62
- package/lib/lua/h-del-if-equals.lua +39 -0
- package/lib/lua/h-set-if-equals.lua +39 -0
- package/lib/routes-ui.js +4 -34
- package/lib/schemas.js +32 -0
- package/lib/security-headers.js +15 -0
- package/lib/tls/context.js +376 -0
- package/lib/tls/der.js +223 -0
- package/lib/tls/provision.js +555 -0
- package/lib/tls/self-signed.js +282 -0
- package/lib/tls/status.js +431 -0
- package/lib/tls/store.js +666 -0
- package/lib/tools.js +72 -27
- package/lib/ui-routes/account-routes.js +43 -30
- package/lib/ui-routes/admin-config-routes.js +22 -1
- package/lib/ui-routes/auth-routes.js +16 -1
- package/lib/ui-routes/internals-routes.js +3 -5
- package/lib/ui-routes/network-config-routes.js +54 -45
- package/lib/ui-routes/tls-config-routes.js +581 -0
- package/lib/utils/mailbox-path.js +51 -0
- package/lib/utils/severity.js +31 -0
- package/package.json +4 -4
- package/sbom.json +1 -1
- package/server.js +16 -0
- package/static/css/flyonui.css +1 -1
- package/static/css/src/app.tailwind.css +11 -0
- package/static/js/app.js +34 -0
- package/static/js/public.js +6 -2
- package/static/js/ui.js +8 -25
- package/static/licenses.html +328 -1257
- package/translations/messages.pot +87 -87
- package/views/account/login-redirect.hbs +22 -0
- package/views/config/imap-proxy.hbs +0 -3
- package/views/config/smtp.hbs +0 -3
- package/views/config/tls-upload.hbs +81 -0
- package/views/config/tls.hbs +492 -0
- package/views/partials/side_menu.hbs +1 -0
- package/views/partials/tls_security_card.hbs +25 -53
- package/views/partials/ui/alert-banner.hbs +3 -2
- package/views/partials/ui/badge.hbs +6 -2
- package/views/partials/ui/btn.hbs +5 -2
- package/workers/api.js +93 -134
- package/workers/imap-proxy.js +4 -1
- package/workers/smtp.js +51 -44
- package/views/partials/tls_security_js.hbs +0 -64
- package/views/partials/tls_security_modal.hbs +0 -22
|
@@ -18,6 +18,9 @@
|
|
|
18
18
|
id - element id (optional)
|
|
19
19
|
class - extra classes (optional)
|
|
20
20
|
overlay - value for data-overlay to open/close a ui/modal (optional)
|
|
21
|
+
dataName/dataValue - one data-* attribute, e.g. dataName="file-target", for a
|
|
22
|
+
delegated handler that reads it (optional). Pass literals only: the
|
|
23
|
+
attribute name is interpolated unescaped, so it must never come from data
|
|
21
24
|
disabled - truthy to render the button disabled. Also adds pointer-events-none,
|
|
22
25
|
because a disabled control dispatches no mouse events and a wrapping
|
|
23
26
|
{{#> ui/tooltip}} would never see the hover that is meant to explain
|
|
@@ -32,7 +35,7 @@ btn{{#unless bare}} btn-{{#if variant}}{{variant}}{{else}}primary{{/if}}{{/unles
|
|
|
32
35
|
{{#if icon}}<span class="{{icon}} size-4 shrink-0"></span>{{/if}}{{label}}
|
|
33
36
|
{{~/inline}}
|
|
34
37
|
{{#if href}}
|
|
35
|
-
<a href="{{href}}" {{#if id}}id="{{id}}" {{/if}}{{#if external}}target="_blank" rel="noopener noreferrer" referrerpolicy="no-referrer" {{/if}}class="{{> btnClasses}}">{{> btnContent}}</a>
|
|
38
|
+
<a href="{{href}}" {{#if id}}id="{{id}}" {{/if}}{{#if dataName}}data-{{dataName}}="{{dataValue}}" {{/if}}{{#if external}}target="_blank" rel="noopener noreferrer" referrerpolicy="no-referrer" {{/if}}class="{{> btnClasses}}">{{> btnContent}}</a>
|
|
36
39
|
{{else}}
|
|
37
|
-
<button type="{{#if type}}{{type}}{{else}}button{{/if}}" {{#if id}}id="{{id}}" {{/if}}{{#if overlay}}data-overlay="{{overlay}}" {{/if}}{{#if disabled}}disabled {{/if}}class="{{> btnClasses}}">{{> btnContent}}</button>
|
|
40
|
+
<button type="{{#if type}}{{type}}{{else}}button{{/if}}" {{#if id}}id="{{id}}" {{/if}}{{#if overlay}}data-overlay="{{overlay}}" {{/if}}{{#if dataName}}data-{{dataName}}="{{dataValue}}" {{/if}}{{#if disabled}}disabled {{/if}}class="{{> btnClasses}}">{{> btnContent}}</button>
|
|
38
41
|
{{/if}}
|
package/workers/api.js
CHANGED
|
@@ -35,15 +35,13 @@ const {
|
|
|
35
35
|
hasEnvValue,
|
|
36
36
|
getBoolean,
|
|
37
37
|
loadTlsConfig,
|
|
38
|
-
httpAgent,
|
|
39
38
|
resolveOAuthErrorStatus,
|
|
40
39
|
constantTimeEqual,
|
|
41
40
|
verifyServiceSignature,
|
|
42
41
|
claimFormNonce,
|
|
43
42
|
releaseFormNonce,
|
|
44
43
|
setAdminSession,
|
|
45
|
-
isEndedSession
|
|
46
|
-
reloadTlsServers
|
|
44
|
+
isEndedSession
|
|
47
45
|
} = require('../lib/tools');
|
|
48
46
|
const { matchIp, resolveClientIp, detectAutomatedRequest } = require('../lib/utils/network');
|
|
49
47
|
|
|
@@ -94,16 +92,16 @@ const specOptions = require('../lib/swagger-options');
|
|
|
94
92
|
const { registerOpenApiRoute } = require('../lib/openapi');
|
|
95
93
|
const { getModel: getApiReferenceModel, clearServiceUrlCache } = require('../lib/api-reference');
|
|
96
94
|
|
|
97
|
-
const {
|
|
98
|
-
const {
|
|
99
|
-
const
|
|
95
|
+
const { createCertHandler } = require('../lib/cert-handler');
|
|
96
|
+
const { createTlsContext, applyTlsContext, apiTlsConfig } = require('../lib/tls/context');
|
|
97
|
+
const { reconcileCertificates, requestProvisioning } = require('../lib/tls/provision');
|
|
98
|
+
const { buildCertificateStatus } = require('../lib/tls/status');
|
|
100
99
|
|
|
101
100
|
const consts = require('../lib/consts');
|
|
102
101
|
const {
|
|
103
102
|
TRACK_OPEN_NOTIFY,
|
|
104
103
|
TRACK_CLICK_NOTIFY,
|
|
105
104
|
REDIS_PREFIX,
|
|
106
|
-
BLOCK_TLS_RENEW,
|
|
107
105
|
TLS_RENEW_CHECK_INTERVAL,
|
|
108
106
|
DEFAULT_CORS_MAX_AGE,
|
|
109
107
|
LIST_UNSUBSCRIBE_NOTIFY,
|
|
@@ -200,7 +198,7 @@ const API_PORT =
|
|
|
200
198
|
const API_HOST = readEnvValue('EENGINE_HOST') || config.api.host;
|
|
201
199
|
|
|
202
200
|
// Either an object (TLS enabled) or `false` (TLS disabled)
|
|
203
|
-
const API_TLS =
|
|
201
|
+
const API_TLS = apiTlsConfig();
|
|
204
202
|
|
|
205
203
|
// Merge TLS settings from config params and environment
|
|
206
204
|
loadTlsConfig(API_TLS, 'EENGINE_API_TLS_');
|
|
@@ -378,6 +376,12 @@ async function onCommand(command) {
|
|
|
378
376
|
switch (command.cmd) {
|
|
379
377
|
case 'resource-usage':
|
|
380
378
|
return threadStats.usage();
|
|
379
|
+
|
|
380
|
+
case 'apiReloadCertificates':
|
|
381
|
+
// Every API worker gets this, not just the one that ordered the certificate: the
|
|
382
|
+
// reconciler is a singleton but the HTTPS listeners are not, and a sibling worker that
|
|
383
|
+
// kept its old context would go on serving the certificate that was just replaced.
|
|
384
|
+
return refreshApiTls ? await refreshApiTls() : { updated: false };
|
|
381
385
|
default:
|
|
382
386
|
logger.debug({ msg: 'Unhandled command', command });
|
|
383
387
|
return 999;
|
|
@@ -420,6 +424,10 @@ function publishChangeEvent(data) {
|
|
|
420
424
|
// strategy that does not exist yet or race the boot-time server.auth.default() call
|
|
421
425
|
let sessionAuthServer = null;
|
|
422
426
|
|
|
427
|
+
// Installs a renewed certificate on this worker's own HTTPS listener. Published by init() when the
|
|
428
|
+
// API is serving TLS, and called through the apiReloadCertificates command.
|
|
429
|
+
let refreshApiTls = null;
|
|
430
|
+
|
|
423
431
|
parentPort.on('message', message => {
|
|
424
432
|
if (message && message.cmd === 'resp' && message.mid && callQueue.has(message.mid)) {
|
|
425
433
|
let { resolve, reject, timer } = callQueue.get(message.mid);
|
|
@@ -523,6 +531,24 @@ const init = async () => {
|
|
|
523
531
|
}
|
|
524
532
|
};
|
|
525
533
|
|
|
534
|
+
let certHandler = createCertHandler(logger);
|
|
535
|
+
|
|
536
|
+
// The admin UI and REST API over HTTPS. EENGINE_API_TLS_* and [api.tls] supply the certificate
|
|
537
|
+
// when the operator has one; everything they leave out comes from the same sources every other
|
|
538
|
+
// listener uses, so an instance that already holds a Let's Encrypt certificate for its own
|
|
539
|
+
// service domain no longer needs a second, external pipeline to serve it here.
|
|
540
|
+
let apiTlsContext = null;
|
|
541
|
+
if (API_TLS) {
|
|
542
|
+
apiTlsContext = await createTlsContext({
|
|
543
|
+
certs: certHandler,
|
|
544
|
+
logger,
|
|
545
|
+
// The operator's material and handshake settings are snapshotted inside, at entry,
|
|
546
|
+
// before the resolved options are merged back into this same object below
|
|
547
|
+
listenerOptions: API_TLS
|
|
548
|
+
});
|
|
549
|
+
Object.assign(API_TLS, apiTlsContext.options);
|
|
550
|
+
}
|
|
551
|
+
|
|
526
552
|
// With multiple API workers we provide our own listener and bind it ourselves with
|
|
527
553
|
// SO_REUSEPORT so the kernel load-balances connections. Hapi forbids port/host when
|
|
528
554
|
// autoListen is false and needs a truthy `tls` flag to treat a provided HTTPS
|
|
@@ -543,108 +569,58 @@ const init = async () => {
|
|
|
543
569
|
|
|
544
570
|
const server = Hapi.server(serverOptions);
|
|
545
571
|
|
|
572
|
+
if (apiTlsContext && typeof server.listener.setSecureContext === 'function') {
|
|
573
|
+
// Connections already established keep the context they negotiated with; the next handshake
|
|
574
|
+
// gets the new one, which is why this does not restart anything.
|
|
575
|
+
refreshApiTls = async () =>
|
|
576
|
+
await applyTlsContext({
|
|
577
|
+
context: apiTlsContext,
|
|
578
|
+
// setSecureContext() replaces the context rather than merging into it: anything
|
|
579
|
+
// left out reverts to Node's default, so a reload that sent only the certificate
|
|
580
|
+
// and key dropped the operator's ca, ciphers and version bounds at the first
|
|
581
|
+
// renewal. SNICallback is a listener option and is not one of them.
|
|
582
|
+
apply: options => {
|
|
583
|
+
const secureContext = Object.assign({}, options);
|
|
584
|
+
delete secureContext.SNICallback;
|
|
585
|
+
server.listener.setSecureContext(secureContext);
|
|
586
|
+
},
|
|
587
|
+
logger
|
|
588
|
+
});
|
|
589
|
+
}
|
|
590
|
+
|
|
546
591
|
let assertPreconditionResult;
|
|
547
592
|
server.decorate('toolkit', 'getESClient', async (...args) => await getESClient(...args));
|
|
548
593
|
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
let hostname = (parsedUrl.hostname || '').toString().toLowerCase().trim();
|
|
560
|
-
if (!hostname || net.isIP(hostname) || ['localhost'].includes(hostname) || /(\.local|\.lan)$/i.test(hostname)) {
|
|
561
|
-
// empty string, not false: the value is interpolated into
|
|
562
|
-
// templates, where a boolean would render as the text "false"
|
|
563
|
-
return '';
|
|
564
|
-
}
|
|
565
|
-
return hostname;
|
|
594
|
+
// A read-only view of the certificate store for routes. Ordering a certificate is the
|
|
595
|
+
// reconciler's job - it owns the state a page follows, the retry pacing and the listener
|
|
596
|
+
// reload - and the difference between reading and ordering is one argument
|
|
597
|
+
// (`getCertificate(host, false)`), which is not a difference a route should be able to make by
|
|
598
|
+
// accident. Routes that need one ask through requestCertificates() below.
|
|
599
|
+
const readOnlyCerts = {
|
|
600
|
+
getCertificate: async hostname => await certHandler.getCertificate(hostname, true),
|
|
601
|
+
deleteCertificateData: async hostname => await certHandler.deleteCertificateData(hostname),
|
|
602
|
+
routeHandler: async (...args) => await certHandler.routeHandler(...args)
|
|
566
603
|
};
|
|
567
604
|
|
|
568
|
-
|
|
569
|
-
redis,
|
|
570
|
-
namespace: `${REDIS_PREFIX}`,
|
|
571
|
-
|
|
572
|
-
acme: {
|
|
573
|
-
environment: 'emailengine',
|
|
574
|
-
directoryUrl: 'https://acme-v02.api.letsencrypt.org/directory'
|
|
575
|
-
//directoryUrl: 'https://acme-staging-v02.api.letsencrypt.org/directory',
|
|
576
|
-
},
|
|
605
|
+
server.decorate('toolkit', 'certs', readOnlyCerts);
|
|
577
606
|
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
encryptFn: async value => {
|
|
584
|
-
const encryptSecret = await getSecret();
|
|
585
|
-
return encrypt(value, encryptSecret);
|
|
586
|
-
},
|
|
587
|
-
|
|
588
|
-
decryptFn: async value => {
|
|
589
|
-
const encryptSecret = await getSecret();
|
|
590
|
-
return decrypt(value, encryptSecret);
|
|
591
|
-
}
|
|
592
|
-
});
|
|
593
|
-
|
|
594
|
-
server.decorate('toolkit', 'serviceDomain', getServiceDomain);
|
|
595
|
-
server.decorate('toolkit', 'certs', certHandler);
|
|
607
|
+
server.decorate(
|
|
608
|
+
'toolkit',
|
|
609
|
+
'requestCertificates',
|
|
610
|
+
async (hostnames, requestLogger) => await requestProvisioning({ certs: certHandler, logger: requestLogger || logger, call, hostnames })
|
|
611
|
+
);
|
|
596
612
|
|
|
597
613
|
server.decorate('toolkit', 'checkRateLimit', checkRateLimit);
|
|
598
614
|
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
if (hostname) {
|
|
604
|
-
certificateData = await certHandler.getCertificate(hostname, !provision);
|
|
605
|
-
}
|
|
606
|
-
|
|
607
|
-
if (!certificateData) {
|
|
608
|
-
certificateData = {
|
|
609
|
-
domain: hostname,
|
|
610
|
-
status: 'self_signed',
|
|
611
|
-
label: { type: 'warning', text: 'Self-signed', title: 'Using a self-signed certificate' }
|
|
612
|
-
};
|
|
613
|
-
} else if (certificateData.status !== 'valid') {
|
|
614
|
-
switch (certificateData.status) {
|
|
615
|
-
case 'pending':
|
|
616
|
-
certificateData.label = { type: 'info', text: 'Provisioning...', title: 'Currently provisioning a certificate' };
|
|
617
|
-
break;
|
|
618
|
-
case 'failed':
|
|
619
|
-
certificateData.label = {
|
|
620
|
-
type: 'error',
|
|
621
|
-
text: 'Failed',
|
|
622
|
-
title: (certificateData.lastError && certificateData.lastError.err) || 'Failed to generate a certificate'
|
|
623
|
-
};
|
|
624
|
-
break;
|
|
625
|
-
}
|
|
626
|
-
} else if (certificateData.validFrom > new Date()) {
|
|
627
|
-
certificateData.label = {
|
|
628
|
-
type: 'warning',
|
|
629
|
-
text: 'Future certificate',
|
|
630
|
-
title: 'Certificate is not yet valid'
|
|
631
|
-
};
|
|
632
|
-
} else if (certificateData.validTo < new Date()) {
|
|
633
|
-
certificateData.label = {
|
|
634
|
-
type: 'warning',
|
|
635
|
-
text: 'Expired certificate',
|
|
636
|
-
title: (certificateData.lastError && certificateData.lastError.err) || 'Certificate has been expired'
|
|
637
|
-
};
|
|
638
|
-
} else {
|
|
639
|
-
certificateData.label = {
|
|
640
|
-
type: 'success',
|
|
641
|
-
text: 'Valid certificate',
|
|
642
|
-
title: certificateData.fingerprint
|
|
643
|
-
};
|
|
644
|
-
}
|
|
615
|
+
// The certificate model the admin UI renders. Reading it never provisions anything: ordering a
|
|
616
|
+
// certificate is the reconciler's job, and a page render that could start an ACME order was
|
|
617
|
+
// exactly how a checkbox came to hold an HTTP request open for the length of one.
|
|
618
|
+
server.decorate('toolkit', 'tlsStatus', async () => await buildCertificateStatus({ certs: readOnlyCerts }));
|
|
645
619
|
|
|
646
|
-
|
|
647
|
-
|
|
620
|
+
server.decorate('toolkit', 'apiTlsInfo', () => ({
|
|
621
|
+
enabled: !!API_TLS,
|
|
622
|
+
active: apiTlsContext ? apiTlsContext.active : null
|
|
623
|
+
}));
|
|
648
624
|
|
|
649
625
|
server.ext('onPreAuth', async (request, h) => {
|
|
650
626
|
const tags = (request.route && request.route.settings && request.route.settings.tags) || [];
|
|
@@ -3336,12 +3312,21 @@ const init = async () => {
|
|
|
3336
3312
|
});
|
|
3337
3313
|
});
|
|
3338
3314
|
|
|
3339
|
-
//
|
|
3315
|
+
// Keep the configured hostnames supplied with certificates.
|
|
3316
|
+
//
|
|
3317
|
+
// This used to be a renewal timer and nothing else: it read the certificate for the service
|
|
3318
|
+
// domain and, if one existed, asked whether it was due for replacement. A hostname with no
|
|
3319
|
+
// certificate at all was therefore never provisioned by it - not on a fresh install, not after
|
|
3320
|
+
// a flushed Redis, not after a restored backup - and the only way to get a first certificate
|
|
3321
|
+
// was a checkbox in the admin UI that ordered one in the foreground. An instance whose record
|
|
3322
|
+
// was lost stayed down until somebody noticed and clicked it twice.
|
|
3323
|
+
//
|
|
3324
|
+
// The reconciler in lib/tls/provision.js owns the whole decision now, including the renewal
|
|
3325
|
+
// one, and it is the only thing that orders certificates. The admin UI's button asks it to run
|
|
3326
|
+
// immediately rather than doing the work itself. A first order still needs a listener with TLS
|
|
3327
|
+
// switched on, which is what the checkbox used to be; see listenersServeTls() there.
|
|
3340
3328
|
setInterval(() => {
|
|
3341
3329
|
async function handler() {
|
|
3342
|
-
let serviceDomain = await getServiceDomain();
|
|
3343
|
-
let currentCert = await certHandler.getCertificate(serviceDomain, true);
|
|
3344
|
-
|
|
3345
3330
|
try {
|
|
3346
3331
|
await runPrechecks(redis);
|
|
3347
3332
|
assertPreconditionResult = false;
|
|
@@ -3349,37 +3334,11 @@ const init = async () => {
|
|
|
3349
3334
|
assertPreconditionResult = Boom.boomify(err);
|
|
3350
3335
|
}
|
|
3351
3336
|
|
|
3352
|
-
|
|
3353
|
-
|
|
3354
|
-
// Date.now() - RENEW_TLS_AFTER, which is the expiry date thirty days in the past: it
|
|
3355
|
-
// read as "renew with thirty days left" but meant "renew thirty days after it died",
|
|
3356
|
-
// so the automatic path only ever ran once the certificate had been broken for a
|
|
3357
|
-
// month. Let's Encrypt is on the way from 90 day certificates to 45, which would have
|
|
3358
|
-
// left a service unreachable for two thirds of every certificate's life.
|
|
3359
|
-
if (
|
|
3360
|
-
IS_PRIMARY_API_WORKER &&
|
|
3361
|
-
currentCert &&
|
|
3362
|
-
isRenewalDue(currentCert) &&
|
|
3363
|
-
(!currentCert.lastCheck || currentCert.lastCheck < new Date(Date.now() - BLOCK_TLS_RENEW))
|
|
3364
|
-
) {
|
|
3365
|
-
try {
|
|
3366
|
-
let renewedCert = await certHandler.acquireCert(serviceDomain);
|
|
3367
|
-
|
|
3368
|
-
// Only when the certificate actually changed, so a renewal that was skipped
|
|
3369
|
-
// or failed does not restart both servers for nothing.
|
|
3370
|
-
if (renewedCert && renewedCert.fingerprint && renewedCert.fingerprint !== currentCert.fingerprint) {
|
|
3371
|
-
await reloadTlsServers(call, logger, { serviceDomain });
|
|
3372
|
-
}
|
|
3373
|
-
} catch (err) {
|
|
3374
|
-
logger.error({ msg: 'Failed to acquire TLS certificate', serviceDomain, err });
|
|
3375
|
-
} finally {
|
|
3376
|
-
try {
|
|
3377
|
-
await certHandler.setCertificateData(serviceDomain, { lastCheck: new Date() });
|
|
3378
|
-
} catch (err) {
|
|
3379
|
-
logger.error({ msg: 'Failed to set certificate data', serviceDomain, err });
|
|
3380
|
-
}
|
|
3381
|
-
}
|
|
3337
|
+
if (!IS_PRIMARY_API_WORKER) {
|
|
3338
|
+
return;
|
|
3382
3339
|
}
|
|
3340
|
+
|
|
3341
|
+
await reconcileCertificates({ certs: certHandler, logger, call });
|
|
3383
3342
|
}
|
|
3384
3343
|
|
|
3385
3344
|
handler().catch(err => logger.error({ msg: 'Failed to run certificate handler', err }));
|
package/workers/imap-proxy.js
CHANGED
|
@@ -7,7 +7,7 @@ const logger = require('../lib/logger');
|
|
|
7
7
|
|
|
8
8
|
const { threadStats } = require('../lib/tools');
|
|
9
9
|
|
|
10
|
-
const { run } = require('../lib/imapproxy/imap-server');
|
|
10
|
+
const { run, reloadCertificates } = require('../lib/imapproxy/imap-server');
|
|
11
11
|
|
|
12
12
|
const { initSentry } = require('../lib/sentry');
|
|
13
13
|
initSentry('imapProxy');
|
|
@@ -16,6 +16,9 @@ async function onCommand(command) {
|
|
|
16
16
|
switch (command.cmd) {
|
|
17
17
|
case 'resource-usage':
|
|
18
18
|
return threadStats.usage();
|
|
19
|
+
|
|
20
|
+
case 'imapProxyReloadCertificates':
|
|
21
|
+
return await reloadCertificates();
|
|
19
22
|
default:
|
|
20
23
|
logger.debug({ msg: 'Unhandled command', command });
|
|
21
24
|
return 999;
|
package/workers/smtp.js
CHANGED
|
@@ -6,7 +6,8 @@ const packageData = require('../package.json');
|
|
|
6
6
|
const config = require('@zone-eu/wild-config');
|
|
7
7
|
const logger = require('../lib/logger');
|
|
8
8
|
|
|
9
|
-
const { getDuration, emitChangeEvent, readEnvValue, threadStats, loadTlsConfig, assertTlsCredentials, getByteSize
|
|
9
|
+
const { getDuration, emitChangeEvent, readEnvValue, threadStats, loadTlsConfig, assertTlsCredentials, getByteSize } = require('../lib/tools');
|
|
10
|
+
const { createTlsContext, applyTlsContext } = require('../lib/tls/context');
|
|
10
11
|
const { createSmtpAuthHandler, createSmtpAccountResolver } = require('../lib/smtp-auth');
|
|
11
12
|
|
|
12
13
|
const { initSentry } = require('../lib/sentry');
|
|
@@ -14,13 +15,10 @@ initSentry('smtp');
|
|
|
14
15
|
|
|
15
16
|
const { SMTPServer } = require('smtp-server');
|
|
16
17
|
const util = require('util');
|
|
17
|
-
const { redis } = require('../lib/db');
|
|
18
|
-
const getSecret = require('../lib/get-secret');
|
|
19
18
|
const { collectMessage } = require('../lib/smtp-message-processor');
|
|
20
19
|
const settings = require('../lib/settings');
|
|
21
20
|
|
|
22
|
-
const {
|
|
23
|
-
const { Certs } = require('@postalsys/certs');
|
|
21
|
+
const { createCertHandler } = require('../lib/cert-handler');
|
|
24
22
|
|
|
25
23
|
config.smtp = config.smtp || {
|
|
26
24
|
enabled: false,
|
|
@@ -32,7 +30,7 @@ config.smtp = config.smtp || {
|
|
|
32
30
|
|
|
33
31
|
config.service = config.service || {};
|
|
34
32
|
|
|
35
|
-
const {
|
|
33
|
+
const { DEFAULT_MAX_SMTP_MESSAGE_SIZE } = require('../lib/consts');
|
|
36
34
|
|
|
37
35
|
const DEFAULT_EENGINE_TIMEOUT = 10 * 1000;
|
|
38
36
|
|
|
@@ -47,6 +45,12 @@ const SMTP_MAX_CLIENTS = Number(readEnvValue('EENGINE_SMTP_MAX_CLIENTS') || conf
|
|
|
47
45
|
|
|
48
46
|
const ACCOUNT_CACHE = new WeakMap();
|
|
49
47
|
|
|
48
|
+
// The running listener and the TLS material it serves, kept at module level so the certificate
|
|
49
|
+
// reload command can reach both. A renewal used to terminate this worker, which cut every
|
|
50
|
+
// submission in flight; the listener can be handed a new secure context instead.
|
|
51
|
+
let smtpServer = null;
|
|
52
|
+
let tlsContext = null;
|
|
53
|
+
|
|
50
54
|
let callQueue = new Map();
|
|
51
55
|
let mids = 0;
|
|
52
56
|
|
|
@@ -132,27 +136,8 @@ async function init() {
|
|
|
132
136
|
useProxy: await settings.get('smtpServerProxy')
|
|
133
137
|
};
|
|
134
138
|
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
namespace: `${REDIS_PREFIX}`,
|
|
138
|
-
|
|
139
|
-
environment: 'ee',
|
|
140
|
-
|
|
141
|
-
// long-lived client, see LiveDispatcher in lib/tools.js
|
|
142
|
-
dispatcher: httpAgent.live,
|
|
143
|
-
|
|
144
|
-
logger: logger.child({ sub: 'acme' }),
|
|
145
|
-
|
|
146
|
-
encryptFn: async value => {
|
|
147
|
-
const encryptSecret = await getSecret();
|
|
148
|
-
return encrypt(value, encryptSecret);
|
|
149
|
-
},
|
|
150
|
-
|
|
151
|
-
decryptFn: async value => {
|
|
152
|
-
const encryptSecret = await getSecret();
|
|
153
|
-
return decrypt(value, encryptSecret);
|
|
154
|
-
}
|
|
155
|
-
});
|
|
139
|
+
// Reads what the API worker provisioned; this one never orders a certificate itself.
|
|
140
|
+
let certs = createCertHandler(logger);
|
|
156
141
|
|
|
157
142
|
// check and update authentication settings on connection
|
|
158
143
|
serverOptions.onConnect = (session, callback) => {
|
|
@@ -258,25 +243,20 @@ async function init() {
|
|
|
258
243
|
serverOptions.secure = true;
|
|
259
244
|
serverOptions.allowInsecureAuth = false;
|
|
260
245
|
|
|
246
|
+
// Environment and config-file material first. It used to be loaded here and then
|
|
247
|
+
// overwritten by whatever Let's Encrypt had provisioned, so an operator who pinned a
|
|
248
|
+
// certificate through EENGINE_SMTP_TLS_CERT was silently served a different one.
|
|
249
|
+
// lib/tls/context.js keeps it ahead of every automatic source instead.
|
|
261
250
|
loadTlsConfig(serverOptions, 'EENGINE_SMTP_TLS_');
|
|
262
251
|
|
|
263
|
-
//
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
if (certificateData && certificateData.status === 'valid') {
|
|
269
|
-
serverOptions.cert =
|
|
270
|
-
certificateData.cert +
|
|
271
|
-
'\n' +
|
|
272
|
-
[]
|
|
273
|
-
.concat(certificateData.ca || [])
|
|
274
|
-
.flatMap(entry => entry)
|
|
275
|
-
.join('\n');
|
|
276
|
-
serverOptions.key = certificateData.privateKey;
|
|
277
|
-
}
|
|
278
|
-
}
|
|
252
|
+
// The material loaded above is snapshotted inside, at entry. The resolved material is
|
|
253
|
+
// written back into serverOptions below, so a later refresh reading this object again would
|
|
254
|
+
// believe the operator had supplied whatever it resolved through the environment.
|
|
255
|
+
tlsContext = await createTlsContext({ certs, logger, listenerOptions: serverOptions });
|
|
256
|
+
Object.assign(serverOptions, tlsContext.options);
|
|
279
257
|
|
|
258
|
+
// Reached only when there is no material at all, which now means the self-signed fallback
|
|
259
|
+
// could not be generated either - a broken instance rather than a missing certificate.
|
|
280
260
|
assertTlsCredentials(serverOptions, 'The SMTP server');
|
|
281
261
|
} else {
|
|
282
262
|
serverOptions.disabledCommands = ['STARTTLS'];
|
|
@@ -284,6 +264,7 @@ async function init() {
|
|
|
284
264
|
}
|
|
285
265
|
|
|
286
266
|
server = new SMTPServer(serverOptions);
|
|
267
|
+
smtpServer = server;
|
|
287
268
|
|
|
288
269
|
let port = await settings.get('smtpServerPort');
|
|
289
270
|
let host = await settings.get('smtpServerHost');
|
|
@@ -304,7 +285,7 @@ async function init() {
|
|
|
304
285
|
resolve();
|
|
305
286
|
});
|
|
306
287
|
});
|
|
307
|
-
await emitChangeEvent(logger, null, 'smtpServerState', 'listening');
|
|
288
|
+
await emitChangeEvent(logger, null, 'smtpServerState', 'listening', { tls: tlsContext ? tlsContext.active : null });
|
|
308
289
|
} catch (err) {
|
|
309
290
|
await emitChangeEvent(logger, null, 'smtpServerState', 'failed', {
|
|
310
291
|
error: { message: err.message, code: err.code || null }
|
|
@@ -315,10 +296,36 @@ async function init() {
|
|
|
315
296
|
return server;
|
|
316
297
|
}
|
|
317
298
|
|
|
299
|
+
/**
|
|
300
|
+
* Re-resolves the TLS material and hands it to the running listener.
|
|
301
|
+
*
|
|
302
|
+
* Connections already established keep the context they negotiated with; the next handshake gets
|
|
303
|
+
* the new one. Nothing here restarts the server, which is the whole point: a renewal is not a
|
|
304
|
+
* reason to drop a submission that is halfway through DATA.
|
|
305
|
+
*
|
|
306
|
+
* @returns {Promise<Object>} What the listener is serving after the reload
|
|
307
|
+
*/
|
|
308
|
+
async function reloadCertificates() {
|
|
309
|
+
const result = await applyTlsContext({
|
|
310
|
+
context: smtpServer ? tlsContext : null,
|
|
311
|
+
apply: options => smtpServer.updateSecureContext(options),
|
|
312
|
+
logger
|
|
313
|
+
});
|
|
314
|
+
|
|
315
|
+
if (result.updated) {
|
|
316
|
+
await emitChangeEvent(logger, null, 'smtpServerState', 'listening', { tls: result.tls });
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
return result;
|
|
320
|
+
}
|
|
321
|
+
|
|
318
322
|
async function onCommand(command) {
|
|
319
323
|
switch (command.cmd) {
|
|
320
324
|
case 'resource-usage':
|
|
321
325
|
return threadStats.usage();
|
|
326
|
+
|
|
327
|
+
case 'smtpReloadCertificates':
|
|
328
|
+
return await reloadCertificates();
|
|
322
329
|
default:
|
|
323
330
|
logger.debug({ msg: 'Unhandled command', command });
|
|
324
331
|
return 999;
|
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
{{!-- certificate provisioning logic for tls_security_card; include INSIDE
|
|
2
|
-
the page's DOMContentLoaded script block. Both config pages provision
|
|
3
|
-
through the same endpoint - the certificate belongs to the service
|
|
4
|
-
domain, not to either server. Expects the page to define
|
|
5
|
-
renderExamples() (both do).
|
|
6
|
-
Params:
|
|
7
|
-
field - the TLS checkbox id (matches tls_security_card) --}}
|
|
8
|
-
let loadingCertData = false;
|
|
9
|
-
let loadedCertData = false;
|
|
10
|
-
|
|
11
|
-
function loadCertData() {
|
|
12
|
-
loadingCertData = true;
|
|
13
|
-
|
|
14
|
-
for (let elm of document.querySelectorAll('#tlsModal .modal-show-error')) {
|
|
15
|
-
elm.classList.add('hidden')
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
for (let elm of document.querySelectorAll('#tlsModal .modal-show-default')) {
|
|
19
|
-
elm.classList.remove('hidden')
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
uiModal.open('#tlsModal');
|
|
23
|
-
|
|
24
|
-
uiPostJson('/admin/config/smtp/certificate').then(data => {
|
|
25
|
-
if (!data || !data.success) {
|
|
26
|
-
throw new Error(data && data.error || 'Request failed')
|
|
27
|
-
}
|
|
28
|
-
console.log(data)
|
|
29
|
-
uiModal.close('#tlsModal');
|
|
30
|
-
loadingCertData = false;
|
|
31
|
-
loadedCertData = true;
|
|
32
|
-
document.getElementById('{{field}}').dataset.certificate = data.fingerprint
|
|
33
|
-
|
|
34
|
-
paintCertData(data);
|
|
35
|
-
}).catch(err => {
|
|
36
|
-
console.error(err)
|
|
37
|
-
loadingCertData = false;
|
|
38
|
-
|
|
39
|
-
document.getElementById('{{field}}').checked = false;
|
|
40
|
-
renderExamples();
|
|
41
|
-
|
|
42
|
-
for (let elm of document.querySelectorAll('#tlsModal .modal-show-error')) {
|
|
43
|
-
elm.classList.remove('hidden')
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
for (let elm of document.querySelectorAll('#tlsModal .modal-show-default')) {
|
|
47
|
-
elm.classList.add('hidden')
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
document.querySelector('#tlsModal .error-content').textContent = err.message;
|
|
51
|
-
});
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
function checkLoadCerts() {
|
|
55
|
-
if (document.getElementById('{{field}}').checked &&
|
|
56
|
-
!loadingCertData && !loadedCertData &&
|
|
57
|
-
document.getElementById('{{field}}').dataset.domain) {
|
|
58
|
-
|
|
59
|
-
loadCertData();
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
document.getElementById('{{field}}').addEventListener('click', checkLoadCerts);
|
|
64
|
-
document.getElementById('{{field}}').addEventListener('change', checkLoadCerts);
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
{{!-- certificate provisioning progress/error modal for tls_security_card;
|
|
2
|
-
rendered outside the settings form (its Close button must not submit) --}}
|
|
3
|
-
{{#> ui/modal id="tlsModal" hideClose=true dialogClass="max-w-3xl"}}
|
|
4
|
-
<div class="modal-body">
|
|
5
|
-
<h3 class="modal-title modal-show-default mb-4">Provisioning Certificate <span
|
|
6
|
-
class="loading loading-spinner loading-sm align-middle"></span></h3>
|
|
7
|
-
<h3 class="modal-title modal-show-error hidden mb-4">Certificate Request Failed</h3>
|
|
8
|
-
|
|
9
|
-
<div class="modal-show-default">
|
|
10
|
-
Setting up TLS certificate for <em class="show-service-domain">{{#if serviceDomain}}{{serviceDomain}}{{/if}}</em>. This may take a
|
|
11
|
-
moment...
|
|
12
|
-
</div>
|
|
13
|
-
<div class="modal-show-error hidden">
|
|
14
|
-
<p class="mb-2">Unable to provision certificate:</p>
|
|
15
|
-
|
|
16
|
-
<div class="font-mono error-content alert alert-soft alert-error"></div>
|
|
17
|
-
</div>
|
|
18
|
-
</div>
|
|
19
|
-
<div class="modal-footer modal-show-error hidden">
|
|
20
|
-
{{> ui/btn label="Close" variant="neutral" style="soft" overlay="#tlsModal"}}
|
|
21
|
-
</div>
|
|
22
|
-
{{/ui/modal}}
|