From a3ebf05f47576fe4404d09694257974fe32b5435 Mon Sep 17 00:00:00 2001 From: Andris Reinman Date: Thu, 10 Aug 2023 13:57:58 +0300 Subject: [PATCH] Log client ID after authentication --- imap-core/lib/commands/authenticate-plain.js | 6 +++--- imap-core/lib/commands/id.js | 17 ++++------------- imap-core/lib/commands/login.js | 1 + imap-core/lib/imap-tools.js | 20 ++++++++++++++++++++ 4 files changed, 28 insertions(+), 16 deletions(-) diff --git a/imap-core/lib/commands/authenticate-plain.js b/imap-core/lib/commands/authenticate-plain.js index 21a7b903..4e4391fd 100644 --- a/imap-core/lib/commands/authenticate-plain.js +++ b/imap-core/lib/commands/authenticate-plain.js @@ -49,9 +49,7 @@ module.exports = { }; function authenticate(connection, token, requireClientToken, callback) { - let data = Buffer.from(token, 'base64') - .toString() - .split('\x00'); + let data = Buffer.from(token, 'base64').toString().split('\x00'); if ((!requireClientToken && data.length !== 3) || (requireClientToken && data.length !== 4)) { return callback(null, { @@ -134,7 +132,9 @@ function authenticate(connection, token, requireClientToken, callback) { connection.setUser(response.user); connection.state = 'Authenticated'; connection.setupNotificationListener(); + imapTools.sendCapabilityResponse(connection); + imapTools.logClientId(connection); callback(null, { response: 'OK', diff --git a/imap-core/lib/commands/id.js b/imap-core/lib/commands/id.js index 05528e75..dd3cd06f 100644 --- a/imap-core/lib/commands/id.js +++ b/imap-core/lib/commands/id.js @@ -2,6 +2,7 @@ const packageInfo = require('../../../package'); const imapHandler = require('../handler/imap-handler'); +const imapTools = require('../imap-tools'); const allowedKeys = ['name', 'version', 'os', 'os-version', 'vendor', 'support-url', 'address', 'date', 'command', 'arguments', 'environment']; @@ -33,10 +34,7 @@ module.exports = { if (Array.isArray(command.attributes[0])) { command.attributes[0].forEach(val => { if (key === false) { - key = (val.value || '') - .toString() - .toLowerCase() - .trim(); + key = (val.value || '').toString().toLowerCase().trim(); } else { if (allowedKeys.indexOf(key) >= 0) { clientId[key] = (val.value || '').toString(); @@ -55,17 +53,9 @@ module.exports = { this.id ); - let logdata = { - short_message: '[CLIENT ID]', - _mail_action: 'client_id', - _user: this.session && this.session.user && this.session.user.id && this.session.user.id.toString(), - _sess: this.id - }; - Object.keys(clientId) .sort((a, b) => allowedKeys.indexOf(a) - allowedKeys.indexOf(b)) .forEach(key => { - logdata[`_client_id_${key}`] = clientId[key]; this._server.logger.info( { tnx: 'id', @@ -79,7 +69,8 @@ module.exports = { ); }); - this._server.loggelf(logdata); + this.session.clientId = clientId; + imapTools.logClientId(this); } // Create response ID serverIdList diff --git a/imap-core/lib/commands/login.js b/imap-core/lib/commands/login.js index 809b1877..123ab710 100644 --- a/imap-core/lib/commands/login.js +++ b/imap-core/lib/commands/login.js @@ -124,6 +124,7 @@ module.exports = { this.state = 'Authenticated'; this.setupNotificationListener(); imapTools.sendCapabilityResponse(this); + imapTools.logClientId(this); callback(null, { response: 'OK', diff --git a/imap-core/lib/imap-tools.js b/imap-core/lib/imap-tools.js index 36f95189..017b9070 100644 --- a/imap-core/lib/imap-tools.js +++ b/imap-core/lib/imap-tools.js @@ -783,3 +783,23 @@ module.exports.validateSearchDate = internaldate => { } return /^\d{1,2}-(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)-\d{4}$/i.test(internaldate); }; + +module.exports.logClientId = connection => { + if (!connection.session.clientId) { + return false; + } + + let logdata = { + short_message: '[CLIENT ID]', + _mail_action: 'client_id', + _authenticated: !!connection.session && connection.session.user && connection.session.user.id ? 'yes' : 'no', + _user: connection.session && connection.session.user && connection.session.user.id && connection.session.user.id.toString(), + _sess: connection.id + }; + + Object.keys(connection.session.clientId || {}).forEach(key => { + logdata[`_client_id_${key}`] = connection.session.clientId[key]; + }); + + connection._server.loggelf(logdata); +};