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
package/lib/tools.js
CHANGED
|
@@ -33,6 +33,7 @@ const mimeTypes = require('nodemailer/lib/mime-funcs/mime-types');
|
|
|
33
33
|
const { v3: murmurhash } = require('murmurhash');
|
|
34
34
|
const { isEmail } = require('./utils/is-email');
|
|
35
35
|
const { toCamelCase } = require('./utils/to-camel-case');
|
|
36
|
+
const { normalizePath } = require('./utils/mailbox-path');
|
|
36
37
|
const { compare: compareVersions, validate: validateVersion } = require('compare-versions');
|
|
37
38
|
const {
|
|
38
39
|
REDIS_PREFIX,
|
|
@@ -509,28 +510,65 @@ function cidReferenceRegex() {
|
|
|
509
510
|
return new RegExp(CID_REFERENCE_SOURCE, 'g');
|
|
510
511
|
}
|
|
511
512
|
|
|
512
|
-
//
|
|
513
|
-
//
|
|
514
|
-
//
|
|
515
|
-
//
|
|
516
|
-
//
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
513
|
+
// Hands a renewed certificate to every listener without restarting any of them.
|
|
514
|
+
//
|
|
515
|
+
// The list of listeners lives here rather than at each call site, because the bug this replaces was
|
|
516
|
+
// exactly that: the renewal path told the SMTP server and forgot the IMAP proxy, which went on
|
|
517
|
+
// offering the expired certificate until something unrelated restarted it.
|
|
518
|
+
//
|
|
519
|
+
// A restart was how this used to work, and it cut every SMTP submission and every IMAP session in
|
|
520
|
+
// flight for a certificate that was not going to expire for another month. Both mail server
|
|
521
|
+
// libraries build their secure contexts through updateSecureContext() and Node's own https server
|
|
522
|
+
// has setSecureContext(), so a running listener can be given new material between handshakes;
|
|
523
|
+
// connections already established keep the context they negotiated with, which stays valid for as
|
|
524
|
+
// long as they live.
|
|
525
|
+
async function reloadTlsCertificates(call, logger, context) {
|
|
526
|
+
// Sent together rather than one after another: nothing here depends on the order, each command
|
|
527
|
+
// reports its own failure, and the main thread hands them straight to the workers, so awaiting
|
|
528
|
+
// them in sequence only stacked up to three command timeouts in whatever request asked.
|
|
529
|
+
await Promise.all(
|
|
530
|
+
['apiReloadCertificates', 'smtpReloadCertificates', 'imapProxyReloadCertificates'].map(cmd =>
|
|
531
|
+
call({ cmd }).catch(err => logger.error(Object.assign({ msg: 'Failed to reload certificates', cmd, err }, context || {})))
|
|
532
|
+
)
|
|
533
|
+
);
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
// The settings a listener's certificate is resolved from: which stored sources are admitted
|
|
537
|
+
// (tlsProvisioning), and which names are served (tlsHostnames, plus the service URL's own
|
|
538
|
+
// hostname). Changing any of them changes what a running listener should be offering, and none of
|
|
539
|
+
// that depends on a certificate being ordered - the reconciler returns immediately for every mode
|
|
540
|
+
// but `acme`, and even there it only reloads when an order produced new material. So whoever writes
|
|
541
|
+
// one of these keys hands the result to the listeners, the same way HTTP_PROXY_SETTINGS above is
|
|
542
|
+
// what makes a proxy change reach the dispatchers.
|
|
543
|
+
const TLS_MATERIAL_SETTINGS = ['tlsProvisioning', 'tlsHostnames', 'serviceUrl'];
|
|
544
|
+
|
|
545
|
+
/**
|
|
546
|
+
* Reloads the listeners when the settings that were just written change what they serve.
|
|
547
|
+
*
|
|
548
|
+
* @param {Function} call Worker call function
|
|
549
|
+
* @param {Object} logger Worker logger
|
|
550
|
+
* @param {string[]} keys Setting keys that were written
|
|
551
|
+
* @param {Object} [context] Extra fields for the failure log
|
|
552
|
+
* @returns {Promise<boolean>} Whether a reload was sent
|
|
553
|
+
*/
|
|
554
|
+
async function maybeReloadTlsCertificates(call, logger, keys, context) {
|
|
555
|
+
if (!keys || !keys.some(key => TLS_MATERIAL_SETTINGS.includes(key))) {
|
|
556
|
+
return false;
|
|
526
557
|
}
|
|
558
|
+
|
|
559
|
+
await reloadTlsCertificates(call, logger, context);
|
|
560
|
+
return true;
|
|
527
561
|
}
|
|
528
562
|
|
|
529
563
|
module.exports = {
|
|
530
564
|
// keep the re-export, lib/utils/to-camel-case.js holds it so pure modules can use it too
|
|
531
565
|
toCamelCase,
|
|
532
566
|
|
|
533
|
-
|
|
567
|
+
reloadTlsCertificates,
|
|
568
|
+
|
|
569
|
+
maybeReloadTlsCertificates,
|
|
570
|
+
|
|
571
|
+
TLS_MATERIAL_SETTINGS,
|
|
534
572
|
|
|
535
573
|
cidReferenceRegex,
|
|
536
574
|
|
|
@@ -860,17 +898,9 @@ module.exports = {
|
|
|
860
898
|
return hasChanges ? changes : false;
|
|
861
899
|
},
|
|
862
900
|
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
}
|
|
867
|
-
|
|
868
|
-
if (/^INBOX$/i.test(path)) {
|
|
869
|
-
return 'INBOX';
|
|
870
|
-
}
|
|
871
|
-
|
|
872
|
-
return path;
|
|
873
|
-
},
|
|
901
|
+
// keep the re-export, lib/utils/mailbox-path.js holds it so pure modules can compare folder
|
|
902
|
+
// names without inheriting the Redis connections this module opens
|
|
903
|
+
normalizePath,
|
|
874
904
|
|
|
875
905
|
// Hashed component of a mailbox's per-folder Redis keys: a hash of the normalized path, so an
|
|
876
906
|
// arbitrarily long (or delimiter-heavy) folder name maps to a compact, stable key.
|
|
@@ -936,7 +966,22 @@ module.exports = {
|
|
|
936
966
|
|
|
937
967
|
let authResponse = await fetchCmd(parsed.toString(), { method: 'GET', headers, dispatcher: httpAgent.retry });
|
|
938
968
|
if (!authResponse.ok) {
|
|
939
|
-
|
|
969
|
+
// The status rides along in a request record, the way the OAuth2 clients attach
|
|
970
|
+
// tokenRequest/oauthRequest, so the callers can tell an authentication server that is
|
|
971
|
+
// having a bad minute (429, 5xx) from one that refused the account. Without it every
|
|
972
|
+
// blip was reported as an authentication failure for every account on the instance -
|
|
973
|
+
// see isTransientCredentialError() in lib/email-client/credential-errors.js.
|
|
974
|
+
//
|
|
975
|
+
// Deliberately NOT err.statusCode: that field is read all the way up as the status of
|
|
976
|
+
// the request EmailEngine is serving (route-helpers handleError, the cross-worker
|
|
977
|
+
// error rebuild in workers/api.js), so the operator's 401 or 404 became EmailEngine's
|
|
978
|
+
// answer to a caller holding a perfectly valid token. The record names the server and
|
|
979
|
+
// nothing else - the query carries the account this asked about, the userinfo the
|
|
980
|
+
// credential it asked with
|
|
981
|
+
let err = new Error(`Invalid response: ${authResponse.status} ${authResponse.statusText}`);
|
|
982
|
+
err.code = 'HTTPRequestError';
|
|
983
|
+
err.authRequest = { status: authResponse.status, url: parsed.origin };
|
|
984
|
+
throw err;
|
|
940
985
|
}
|
|
941
986
|
|
|
942
987
|
let authData = await authResponse.json();
|
|
@@ -17,9 +17,11 @@ const { redis, documentsQueue } = require('../db');
|
|
|
17
17
|
const getSecret = require('../get-secret');
|
|
18
18
|
const capa = require('../capa');
|
|
19
19
|
const { Account, authFailureDisabledAt, isOperatorDisabled } = require('../account');
|
|
20
|
+
const { formatGmailWatch } = require('../account/account-state');
|
|
20
21
|
const { Gateway } = require('../gateway');
|
|
21
22
|
const { autodetectImapSettings, resolveAppPassword, getAppPassword } = require('../autodetect-imap-settings');
|
|
22
23
|
const { atomicIncrement } = require('../redis-operations');
|
|
24
|
+
const { OAUTH_REDIRECT_FORM_DIRECTIVES } = require('../security-headers');
|
|
23
25
|
const { oauth2Apps, oauth2ProviderData, SERVICE_ACCOUNT_PROVIDERS } = require('../oauth2-apps');
|
|
24
26
|
const {
|
|
25
27
|
getServiceHostname,
|
|
@@ -790,7 +792,13 @@ function init(args) {
|
|
|
790
792
|
|
|
791
793
|
let serverSettings;
|
|
792
794
|
try {
|
|
793
|
-
|
|
795
|
+
// The password the user just typed is passed along because hosted Exchange refuses
|
|
796
|
+
// to answer autodiscovery anonymously; it only ever reaches the autodiscovery host
|
|
797
|
+
// named by the address domain's own DNS, and only after that host has asked for it
|
|
798
|
+
serverSettings = await autodetectImapSettings(request.payload.email, request.app.gt, {
|
|
799
|
+
user: request.payload.email,
|
|
800
|
+
pass: request.payload.password
|
|
801
|
+
});
|
|
794
802
|
} catch (err) {
|
|
795
803
|
request.logger.debug({ msg: 'Failed to resolve email server settings', email: request.payload.email, err });
|
|
796
804
|
}
|
|
@@ -1410,35 +1418,36 @@ function init(args) {
|
|
|
1410
1418
|
accountData.path = null;
|
|
1411
1419
|
}
|
|
1412
1420
|
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
|
|
1431
|
-
|
|
1432
|
-
|
|
1433
|
-
|
|
1434
|
-
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1421
|
+
// State comes from the same derivation the API reports, so the page and the response
|
|
1422
|
+
// cannot disagree about whether push is working. The reason and the failed request stay
|
|
1423
|
+
// here and out of the API: both are text the provider wrote, and this page is behind an
|
|
1424
|
+
// admin session. NB! a watch that is still armed but whose last renewal failed now
|
|
1425
|
+
// reads Failed rather than Active - it used to show the expiry alone and hide the
|
|
1426
|
+
// failure in a tooltip, which said nothing was wrong right up to the day it lapsed
|
|
1427
|
+
let watchState = formatGmailWatch(accountData);
|
|
1428
|
+
let gmailWatch = watchState
|
|
1429
|
+
? {
|
|
1430
|
+
lastCheckStr: watchState.lastCheck,
|
|
1431
|
+
expiresStr: watchState.expires || false,
|
|
1432
|
+
active: watchState.state === 'active',
|
|
1433
|
+
stateLabel: { active: 'Active', error: 'Failed', expired: 'Expired' }[watchState.state] || 'Unknown',
|
|
1434
|
+
// The reason and the failed request are read straight off the record here and
|
|
1435
|
+
// deliberately absent from the shared derivation: both carry text the
|
|
1436
|
+
// provider wrote, including the operator's own Pub/Sub topic path, and this
|
|
1437
|
+
// page is behind an admin session while the API field is not
|
|
1438
|
+
error: accountData.watchFailure ? accountData.watchFailure.err || 'Failed to set up Gmail pubsub watch' : false
|
|
1439
|
+
}
|
|
1440
|
+
: false;
|
|
1441
|
+
|
|
1442
|
+
// Not "watchFailure truthy means gmailWatch is": the derivation reports nothing for an
|
|
1443
|
+
// account whose application has no Pub/Sub app, whatever record a previous one left
|
|
1444
|
+
if (gmailWatch && accountData.watchFailure && accountData.watchFailure.req) {
|
|
1445
|
+
gmailWatch.request = {
|
|
1446
|
+
url: accountData.watchFailure.req.url,
|
|
1447
|
+
status: accountData.watchFailure.req.status,
|
|
1448
|
+
contentType: accountData.watchFailure.req.contentType,
|
|
1449
|
+
response: accountData.watchFailure.req.response
|
|
1450
|
+
};
|
|
1442
1451
|
}
|
|
1443
1452
|
|
|
1444
1453
|
const canReadMail = (accountData.imap || accountData.oauth2) && !(accountData.imap && accountData.imap.disabled) && !DISABLE_MESSAGE_BROWSER;
|
|
@@ -1565,6 +1574,10 @@ function init(args) {
|
|
|
1565
1574
|
},
|
|
1566
1575
|
|
|
1567
1576
|
options: {
|
|
1577
|
+
// The "Re-authenticate" modal posts to /accounts/new, which answers with a redirect to the
|
|
1578
|
+
// provider's authorization page. Without this the admin preset's `form-action 'self'` blocks
|
|
1579
|
+
// the submission, even though the form posts to this instance
|
|
1580
|
+
plugins: { securityHeaders: { directives: OAUTH_REDIRECT_FORM_DIRECTIVES } },
|
|
1568
1581
|
validate: {
|
|
1569
1582
|
options: {
|
|
1570
1583
|
stripUnknown: true,
|
|
@@ -20,7 +20,17 @@ const consts = require('../consts');
|
|
|
20
20
|
const getSecret = require('../get-secret');
|
|
21
21
|
const timezonesList = require('timezones-list').default;
|
|
22
22
|
const { redis, submitQueue, notifyQueue, documentsQueue } = require('../db');
|
|
23
|
-
const {
|
|
23
|
+
const {
|
|
24
|
+
getByteSize,
|
|
25
|
+
formatByteSize,
|
|
26
|
+
getDuration,
|
|
27
|
+
failAction,
|
|
28
|
+
readEnvValue,
|
|
29
|
+
readEnvList,
|
|
30
|
+
httpAgent,
|
|
31
|
+
splitUrlCredentials,
|
|
32
|
+
maybeReloadTlsCertificates
|
|
33
|
+
} = require('../tools');
|
|
24
34
|
const { validateWebhookTarget } = require('../webhook-egress');
|
|
25
35
|
const { sendWebhookRequest } = require('../webhook-request');
|
|
26
36
|
const { llmPreProcess } = require('../llm-pre-process');
|
|
@@ -372,7 +382,18 @@ function init(args) {
|
|
|
372
382
|
// write would leave the old URL in place after a save meant to clear it
|
|
373
383
|
data.serviceUrl = request.payload.serviceUrl ? new URL(request.payload.serviceUrl).origin : null;
|
|
374
384
|
|
|
385
|
+
// Compared before the write, and only for the reload: this form submits the service URL
|
|
386
|
+
// on every save, so reloading on the write alone would hand three listeners a new
|
|
387
|
+
// certificate every time somebody changed the timezone.
|
|
388
|
+
const serviceUrlChanged = data.serviceUrl !== (await settings.get('serviceUrl'));
|
|
389
|
+
|
|
375
390
|
await settings.setMulti(data);
|
|
391
|
+
|
|
392
|
+
// The service URL names the first hostname EmailEngine serves TLS for, so changing it
|
|
393
|
+
// here changes what the listeners have to offer exactly as the TLS page's own hostname
|
|
394
|
+
// list does. Without this the listeners kept answering with a certificate for the
|
|
395
|
+
// previous name until something restarted them.
|
|
396
|
+
await maybeReloadTlsCertificates(call, request.logger, serviceUrlChanged ? ['serviceUrl'] : [], { action: 'settings' });
|
|
376
397
|
}
|
|
377
398
|
});
|
|
378
399
|
|
|
@@ -169,7 +169,22 @@ function init(args) {
|
|
|
169
169
|
// discovery failure) so the local form remains as an emergency fallback.
|
|
170
170
|
let forceLogin = sso.OIDC_FORCED && !!providers.oidc;
|
|
171
171
|
if (forceLogin && !request.query.sso_denied && !request.query.loggedout) {
|
|
172
|
-
|
|
172
|
+
// Rendered rather than redirected, so that a form submission whose session expired
|
|
173
|
+
// is not killed by the admin form-action policy - see the view for the whole story.
|
|
174
|
+
// NB! rendering drains the flash queue, so a message queued just before the login
|
|
175
|
+
// bounce is shown for the frame this page is up rather than on the login form.
|
|
176
|
+
return h.view(
|
|
177
|
+
'account/login-redirect',
|
|
178
|
+
{
|
|
179
|
+
pageTitle: 'Login',
|
|
180
|
+
menuLogin: true,
|
|
181
|
+
providers,
|
|
182
|
+
ssoRedirectUrl: '/admin/login/oidc'
|
|
183
|
+
},
|
|
184
|
+
{
|
|
185
|
+
layout: 'login'
|
|
186
|
+
}
|
|
187
|
+
);
|
|
173
188
|
}
|
|
174
189
|
|
|
175
190
|
let passkeysAvailable = await passkeys.hasPasskeys();
|
|
@@ -12,14 +12,12 @@ const { Account } = require('../account');
|
|
|
12
12
|
const { redis } = require('../db');
|
|
13
13
|
const { DEFAULT_PAGE_SIZE } = require('../consts');
|
|
14
14
|
const { formatAccountData } = require('./route-helpers');
|
|
15
|
+
const { severityRank } = require('../utils/severity');
|
|
15
16
|
|
|
16
17
|
// Worker kinds in the order they are shown on the internals page (known kinds first; any
|
|
17
18
|
// unrecognized kind is appended so nothing is dropped).
|
|
18
19
|
const THREAD_TYPE_ORDER = ['main', 'api', 'imap', 'webhooks', 'submit', 'export', 'smtp', 'imapProxy', 'documents'];
|
|
19
20
|
|
|
20
|
-
// Severity ranking for the per-group health roll-up (higher wins).
|
|
21
|
-
const STATUS_RANK = { success: 0, neutral: 1, warning: 2, error: 3 };
|
|
22
|
-
|
|
23
21
|
// "Recently restarted" detection. Workers normally come online within seconds of the main
|
|
24
22
|
// process; one that came online much later was respawned after a crash (uncaught exception).
|
|
25
23
|
// A worker is flagged only when it started well after main (RESTART_GRACE, beyond any normal
|
|
@@ -130,7 +128,7 @@ function init(args) {
|
|
|
130
128
|
typeof threadInfo.online === 'number' &&
|
|
131
129
|
threadInfo.online - mainOnline > WORKER_RESTART_GRACE &&
|
|
132
130
|
now - threadInfo.online < WORKER_RESTART_RECENT_WINDOW &&
|
|
133
|
-
|
|
131
|
+
severityRank(threadInfo.statusVariant) < severityRank('warning')
|
|
134
132
|
) {
|
|
135
133
|
threadInfo.statusVariant = 'warning';
|
|
136
134
|
threadInfo.statusLabel = 'Recently restarted';
|
|
@@ -190,7 +188,7 @@ function init(args) {
|
|
|
190
188
|
// Health roll-up = worst member status.
|
|
191
189
|
let rollup = list.reduce(
|
|
192
190
|
(worst, threadInfo) =>
|
|
193
|
-
|
|
191
|
+
severityRank(threadInfo.statusVariant) > severityRank(worst.variant)
|
|
194
192
|
? { variant: threadInfo.statusVariant, label: threadInfo.statusLabel }
|
|
195
193
|
: worst,
|
|
196
194
|
{ variant: 'success', label: 'Healthy' }
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
// (/admin/config/{network*,imap-proxy,smtp*,browser}). Extracted verbatim from
|
|
5
5
|
// lib/routes-ui.js. The network page also reloads/deletes the autodetected public
|
|
6
6
|
// interfaces; the SMTP/IMAP-proxy pages manage the built-in MSA and IMAP proxy servers
|
|
7
|
-
// (
|
|
7
|
+
// (the TLS toggle each of them owns; the certificate itself lives on /admin/config/tls).
|
|
8
8
|
|
|
9
9
|
const Joi = require('joi');
|
|
10
10
|
const os = require('os');
|
|
@@ -13,10 +13,13 @@ const { parentPort } = require('worker_threads');
|
|
|
13
13
|
const settings = require('../settings');
|
|
14
14
|
const { redis } = require('../db');
|
|
15
15
|
const { REDIS_PREFIX } = require('../consts');
|
|
16
|
-
const { failAction, getServiceHostname,
|
|
16
|
+
const { failAction, getServiceHostname, maybeReloadTlsCertificates } = require('../tools');
|
|
17
17
|
const { ADDRESS_STRATEGIES, settingsSchema } = require('../schemas');
|
|
18
18
|
const { updatePublicInterfaces } = require('../utils/network');
|
|
19
19
|
const { getServerStatus, compiledFragments } = require('./route-helpers');
|
|
20
|
+
const { listenerCertificateSummary, reportedListenerTls } = require('../tls/status');
|
|
21
|
+
const { getCertificateHostnames } = require('../tls/store');
|
|
22
|
+
const { getProvisioningStatus } = require('../tls/provision');
|
|
20
23
|
const { registerSettingsPage } = require('./settings-page');
|
|
21
24
|
|
|
22
25
|
const configSmtpSchema = {
|
|
@@ -259,8 +262,17 @@ function init(args) {
|
|
|
259
262
|
}
|
|
260
263
|
});
|
|
261
264
|
|
|
262
|
-
// Context shared by the SMTP server and IMAP proxy pages: live server state,
|
|
263
|
-
//
|
|
265
|
+
// Context shared by the SMTP server and IMAP proxy pages: live server state, bindable
|
|
266
|
+
// interface addresses, and a summary of the certificate the listener is serving.
|
|
267
|
+
//
|
|
268
|
+
// A summary, not a control. These pages decide whether their listener speaks TLS; the
|
|
269
|
+
// certificate belongs to the instance and is managed on /admin/config/tls. Rendering it used to
|
|
270
|
+
// provision one as a side effect of ticking a checkbox.
|
|
271
|
+
//
|
|
272
|
+
// The certificate comes from the listener's own state payload rather than being inferred from
|
|
273
|
+
// the configuration: a listener reports what it loaded, which is the thing an operator is
|
|
274
|
+
// looking at this page to find out, and environment-supplied material is not visible any other
|
|
275
|
+
// way. The stored model is the fallback for a listener that has not reported yet.
|
|
264
276
|
const serverPageContext = async (h, serverType) => {
|
|
265
277
|
let availableAddresses = new Set(
|
|
266
278
|
Object.values(os.networkInterfaces())
|
|
@@ -269,13 +281,39 @@ function init(args) {
|
|
|
269
281
|
);
|
|
270
282
|
availableAddresses.add('0.0.0.0');
|
|
271
283
|
|
|
284
|
+
const [serverState, hostnames] = await Promise.all([getServerStatus(serverType), getCertificateHostnames()]);
|
|
285
|
+
|
|
286
|
+
// The name clients connect to, which is the first certificate hostname rather than "a name
|
|
287
|
+
// Let's Encrypt could validate" - a listener serves a self-signed certificate for an
|
|
288
|
+
// address literal just as much as an issued one for a public domain.
|
|
289
|
+
const primaryHostname = hostnames[0] || null;
|
|
290
|
+
const reported = reportedListenerTls(serverState);
|
|
291
|
+
|
|
292
|
+
// The provisioning state comes along with the certificate: it is what turns the badge into
|
|
293
|
+
// "Requesting", "Failed" or "Renewal failed". Without it this page could only ever read
|
|
294
|
+
// "Valid", including under a renewal that had been failing for weeks. Read only for a
|
|
295
|
+
// listener that reported one; the stored model below carries its own copy of it.
|
|
296
|
+
const provisioning = reported ? await getProvisioningStatus() : {};
|
|
297
|
+
|
|
298
|
+
let tlsSummary = listenerCertificateSummary({
|
|
299
|
+
reported,
|
|
300
|
+
hostname: primaryHostname,
|
|
301
|
+
status: provisioning[primaryHostname] || null
|
|
302
|
+
});
|
|
303
|
+
|
|
304
|
+
if (!tlsSummary.certificate) {
|
|
305
|
+
const status = await h.tlsStatus();
|
|
306
|
+
const primary = status.certificates[0] || null;
|
|
307
|
+
if (primary) {
|
|
308
|
+
tlsSummary = { hostname: primary.hostname, certificate: primary.certificate, label: primary.label };
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
|
|
272
312
|
return {
|
|
273
|
-
serverState
|
|
313
|
+
serverState,
|
|
274
314
|
availableAddresses: Array.from(availableAddresses).join(','),
|
|
275
315
|
|
|
276
|
-
|
|
277
|
-
serviceUrl: await settings.get('serviceUrl'),
|
|
278
|
-
certificateData: await h.getCertificate()
|
|
316
|
+
tlsSummary
|
|
279
317
|
};
|
|
280
318
|
};
|
|
281
319
|
|
|
@@ -358,55 +396,26 @@ function init(args) {
|
|
|
358
396
|
}
|
|
359
397
|
});
|
|
360
398
|
|
|
361
|
-
server.route({
|
|
362
|
-
method: 'POST',
|
|
363
|
-
path: '/admin/config/smtp/certificate',
|
|
364
|
-
async handler(request, h) {
|
|
365
|
-
try {
|
|
366
|
-
// What is installed before provisioning, so the reload below fires only when a new
|
|
367
|
-
// certificate actually arrived and repeated clicks do not keep restarting both
|
|
368
|
-
// servers.
|
|
369
|
-
let existingCertificateData = await h.getCertificate();
|
|
370
|
-
|
|
371
|
-
let certificateData = await h.getCertificate(true);
|
|
372
|
-
if (!certificateData) {
|
|
373
|
-
throw new Error(`Failed to provision a certificate`);
|
|
374
|
-
}
|
|
375
|
-
|
|
376
|
-
if (certificateData.fingerprint && certificateData.fingerprint !== (existingCertificateData && existingCertificateData.fingerprint)) {
|
|
377
|
-
await reloadTlsServers(call, request.logger, { domain: certificateData.domain });
|
|
378
|
-
}
|
|
379
|
-
|
|
380
|
-
return {
|
|
381
|
-
success: true,
|
|
382
|
-
domain: certificateData.domain,
|
|
383
|
-
fingerprint: certificateData.fingerprint,
|
|
384
|
-
altNames: certificateData.altNames,
|
|
385
|
-
validTo: certificateData.validTo && certificateData.validTo.toISOString(),
|
|
386
|
-
label: certificateData.label
|
|
387
|
-
};
|
|
388
|
-
} catch (err) {
|
|
389
|
-
request.logger.error({ msg: 'Failed to provision a certificate', err });
|
|
390
|
-
return {
|
|
391
|
-
success: false,
|
|
392
|
-
error: err.message
|
|
393
|
-
};
|
|
394
|
-
}
|
|
395
|
-
}
|
|
396
|
-
});
|
|
397
|
-
|
|
398
399
|
server.route({
|
|
399
400
|
method: 'POST',
|
|
400
401
|
path: '/admin/config/browser',
|
|
401
402
|
async handler(request) {
|
|
403
|
+
const written = [];
|
|
402
404
|
for (let key of ['serviceUrl', 'language', 'timezone']) {
|
|
403
405
|
if (request.payload[key]) {
|
|
404
406
|
let existingValue = await settings.get(key);
|
|
405
407
|
if (existingValue === null) {
|
|
406
408
|
await settings.set(key, request.payload[key]);
|
|
409
|
+
written.push(key);
|
|
407
410
|
}
|
|
408
411
|
}
|
|
409
412
|
}
|
|
413
|
+
|
|
414
|
+
// Learning the service URL for the first time is what gives a fresh install the name it
|
|
415
|
+
// serves TLS for. Until the listeners hear about it they keep the certificate they
|
|
416
|
+
// started with, which covers the localhost fallback and nothing an operator typed.
|
|
417
|
+
await maybeReloadTlsCertificates(call, request.logger, written, { action: 'settings' });
|
|
418
|
+
|
|
410
419
|
return { success: true };
|
|
411
420
|
},
|
|
412
421
|
options: {
|