zone-eu / zone-mta

📤 Modern outbound MTA cross platform and extendable server application
European Union Public License 1.2
602 stars 96 forks source link

How to avoid ERR! in logs #297

Closed matteomattei closed 2 years ago

matteomattei commented 2 years ago

I would exclude "ERR!" in logs when something programatically went wrong and we want to close smtp connection. For example if I decide the authentication fails, the documentation says to return an instance of Error to the next callback like this:

'use strict';
module.exports.title = 'Handle authentication';

module.exports.init = (app, done) => {
    app.addHook('smtp:auth', (auth, session, next) => {
        if (auth.username !== "test@hello.com" and auth.password !== 'helloworld') {
                let error = new Error('Authentication failed - wrong credentials');
                error.responseCode = 535;
                return next(error);
        }
        return next();
    });
    done();
};

Anyway this fills the logs with several lines like the following:

Feb  3 15:00:37 stg-smtp02 zone-mta[17324]: ERR! Plugins "Handle authentication" for "smtp:auth" failed with: Error: Authentication failed - wrong credentials
Feb  3 15:00:37 stg-smtp02 zone-mta[17324]: ERR! Plugins     at /home/mta/zone-mta/plugins/auth.js:19:33
Feb  3 15:00:37 stg-smtp02 zone-mta[17324]: ERR! Plugins     at Socket.<anonymous> (/home/mta/zone-mta/node_modules/simple-sasl/index.js:49:7)
Feb  3 15:00:37 stg-smtp02 zone-mta[17324]: ERR! Plugins     at Socket.emit (node:events:390:28)
Feb  3 15:00:37 stg-smtp02 zone-mta[17324]: ERR! Plugins     at addChunk (node:internal/streams/readable:315:12)
Feb  3 15:00:37 stg-smtp02 zone-mta[17324]: ERR! Plugins     at readableAddChunk (node:internal/streams/readable:289:9)
Feb  3 15:00:37 stg-smtp02 zone-mta[17324]: ERR! Plugins     at Socket.Readable.push (node:internal/streams/readable:228:10)
Feb  3 15:00:37 stg-smtp02 zone-mta[17324]: ERR! Plugins     at TCP.onStreamRead (node:internal/stream_base_commons:199:23)
Feb  3 15:00:37 stg-smtp02 zone-mta[17324]: info SMTP/feeder/17351 AUTHFAIL id=teoerdilimixxmsi user="matteo.mattei@example.com" srs=[xxx.xxx.xxx.xxx] proto=PLAIN error=Authentication failed - wrong credentials

I would want only the last info line containing the AUTHFAIL, the user which tried to perform authentication and its IP address, not all other ERR! lines.

Any idea?

matteomattei commented 2 years ago

Maybe I have just found it:

return next({ responseCode: 535, message: 'Authentication failed - wrong credentials' });
andris9 commented 2 years ago

If you set err.name = 'SMTPResponse' then it does not throw

let error = new Error('Authentication failed - wrong credentials');
error.responseCode = 535;
err.name = 'SMTPResponse'
return next(error);
matteomattei commented 2 years ago

Great it works! Thank you