diff --git a/src/mail.js b/src/mail.js index 6f98d0132..674a71c1c 100644 --- a/src/mail.js +++ b/src/mail.js @@ -5,16 +5,11 @@ exports = module.exports = { getStatus: getStatus, checkRblStatus: checkRblStatus, - getMailFromValidation: getMailFromValidation, + get: get, + setMailFromValidation: setMailFromValidation, - setCatchAllAddress: setCatchAllAddress, - getCatchAllAddress: getCatchAllAddress, - - getMailRelay: getMailRelay, setMailRelay: setMailRelay, - - getMailConfig: getMailConfig, setMailConfig: setMailConfig, startMail: restartMail, @@ -37,6 +32,7 @@ var assert = require('assert'), dig = require('./dig.js'), domains = require('./domains.js'), infra = require('./infra_version.js'), + maildb = require('./maildb.js'), net = require('net'), nodemailer = require('nodemailer'), os = require('os'), @@ -408,7 +404,7 @@ function getStatus(callback) { }; } - getMailRelay(function (error, relay) { + get(config.fqdn(), function (error, mailConfig) { if (error) return callback(error); var checks = [ @@ -416,7 +412,7 @@ function getStatus(callback) { recordResult('dns.dmarc', checkDmarc) ]; - if (relay.provider === 'cloudron-smtp') { + if (mailConfig.relay.provider === 'cloudron-smtp') { // these tests currently only make sense when using Cloudron's SMTP server at this point checks.push( recordResult('dns.spf', checkSpf), @@ -426,7 +422,7 @@ function getStatus(callback) { recordResult('rbl', checkRblStatus) ); } else { - checks.push(recordResult('relay', checkSmtpRelay.bind(null, relay))); + checks.push(recordResult('relay', checkSmtpRelay.bind(null, mailConfig.relay))); } async.parallel(checks, function () { @@ -520,7 +516,7 @@ function restartMail(callback) { if (!safe.fs.writeFileSync(paths.ADDON_CONFIG_DIR + '/mail/tls_cert.pem', cert)) return callback(new Error('Could not create cert file:' + safe.error.message)); if (!safe.fs.writeFileSync(paths.ADDON_CONFIG_DIR + '/mail/tls_key.pem', key)) return callback(new Error('Could not create key file:' + safe.error.message)); - getMailConfig(function (error, mailConfig) { + get(config.fqdn(), function (error, mailConfig) { if (error) return callback(error); shell.execSync('startMail', 'docker rm -f mail || true'); @@ -564,14 +560,16 @@ function restartMail(callback) { }); } -function getMailFromValidation(callback) { +function get(domain, callback) { + assert.strictEqual(typeof domain, 'string'); assert.strictEqual(typeof callback, 'function'); - settingsdb.get(exports.MAIL_FROM_VALIDATION_KEY, function (error, enabled) { - if (error && error.reason === DatabaseError.NOT_FOUND) return callback(null, gDefaults[exports.MAIL_FROM_VALIDATION_KEY]); + maildb.get(domain, function (error, result) { + // TODO try to find subdomain entries maybe based on zoneNames or so + if (error && error.reason === DatabaseError.NOT_FOUND) return callback(new MailError(MailError.NOT_FOUND)); if (error) return callback(new MailError(MailError.INTERNAL_ERROR, error)); - callback(null, !!enabled); // settingsdb holds string values only + return callback(null, result); }); } @@ -588,17 +586,6 @@ function setMailFromValidation(enabled, callback) { }); } -function getCatchAllAddress(callback) { - assert.strictEqual(typeof callback, 'function'); - - settingsdb.get(exports.CATCH_ALL_ADDRESS_KEY, function (error, value) { - if (error && error.reason === DatabaseError.NOT_FOUND) return callback(null, gDefaults[exports.CATCH_ALL_ADDRESS_KEY]); - if (error) return callback(new MailError(MailError.INTERNAL_ERROR, error)); - - callback(null, JSON.parse(value)); - }); -} - function setCatchAllAddress(address, callback) { assert(Array.isArray(address)); assert.strictEqual(typeof callback, 'function'); @@ -612,17 +599,6 @@ function setCatchAllAddress(address, callback) { }); } -function getMailRelay(callback) { - assert.strictEqual(typeof callback, 'function'); - - settingsdb.get(exports.MAIL_RELAY_KEY, function (error, value) { - if (error && error.reason === DatabaseError.NOT_FOUND) return callback(null, gDefaults[exports.MAIL_RELAY_KEY]); - if (error) return callback(new MailError(MailError.INTERNAL_ERROR, error)); - - callback(null, JSON.parse(value)); - }); -} - function setMailRelay(relay, callback) { assert.strictEqual(typeof relay, 'object'); assert.strictEqual(typeof callback, 'function'); @@ -640,18 +616,6 @@ function setMailRelay(relay, callback) { }); } - -function getMailConfig(callback) { - assert.strictEqual(typeof callback, 'function'); - - settingsdb.get(exports.MAIL_CONFIG_KEY, function (error, value) { - if (error && error.reason === DatabaseError.NOT_FOUND) return callback(null, gDefaults[exports.MAIL_CONFIG_KEY]); - if (error) return callback(new MailError(MailError.INTERNAL_ERROR, error)); - - callback(null, JSON.parse(value)); - }); -} - function setMailConfig(mailConfig, callback) { assert.strictEqual(typeof mailConfig, 'object'); assert.strictEqual(typeof callback, 'function'); diff --git a/src/routes/mail.js b/src/routes/mail.js index 1c3199371..c4bdaa059 100644 --- a/src/routes/mail.js +++ b/src/routes/mail.js @@ -1,18 +1,13 @@ 'use strict'; exports = module.exports = { + get: get, + getStatus: getStatus, - getMailFromValidation: getMailFromValidation, setMailFromValidation: setMailFromValidation, - - getCatchAllAddress: getCatchAllAddress, setCatchAllAddress: setCatchAllAddress, - - getMailRelay: getMailRelay, setMailRelay: setMailRelay, - - getMailConfig: getMailConfig, setMailConfig: setMailConfig, }; @@ -22,6 +17,17 @@ var assert = require('assert'), HttpError = require('connect-lastmile').HttpError, HttpSuccess = require('connect-lastmile').HttpSuccess; +function get(req, res, next) { + assert.strictEqual(typeof req.params.domain, 'string'); + + mail.get(req.params.domain, function (error, result) { + if (error && error.reason === MailError.NOT_FOUND) return next(new HttpError(404, error.message)); + if (error) return next(new HttpError(500, error)); + + next(new HttpSuccess(200, result)); + }); +} + function getStatus(req, res, next) { mail.getStatus(function (error, records) { if (error) return next(new HttpError(500, error)); @@ -30,14 +36,6 @@ function getStatus(req, res, next) { }); } -function getMailFromValidation(req, res, next) { - mail.getMailFromValidation(function (error, enabled) { - if (error) return next(new HttpError(500, error)); - - next(new HttpSuccess(200, { enabled: enabled })); - }); -} - function setMailFromValidation(req, res, next) { assert.strictEqual(typeof req.body, 'object'); @@ -51,14 +49,6 @@ function setMailFromValidation(req, res, next) { }); } -function getCatchAllAddress(req, res, next) { - mail.getCatchAllAddress(function (error, address) { - if (error) return next(new HttpError(500, error)); - - next(new HttpSuccess(200, { address: address })); - }); -} - function setCatchAllAddress(req, res, next) { assert.strictEqual(typeof req.body, 'object'); @@ -76,14 +66,6 @@ function setCatchAllAddress(req, res, next) { }); } -function getMailRelay(req, res, next) { - mail.getMailRelay(function (error, mail) { - if (error) return next(new HttpError(500, error)); - - next(new HttpSuccess(200, mail)); - }); -} - function setMailRelay(req, res, next) { assert.strictEqual(typeof req.body, 'object'); @@ -101,14 +83,6 @@ function setMailRelay(req, res, next) { }); } -function getMailConfig(req, res, next) { - mail.getMailConfig(function (error, mail) { - if (error) return next(new HttpError(500, error)); - - next(new HttpSuccess(200, mail)); - }); -} - function setMailConfig(req, res, next) { assert.strictEqual(typeof req.body, 'object'); diff --git a/src/server.js b/src/server.js index a3adfcbbd..3fc9a93f4 100644 --- a/src/server.js +++ b/src/server.js @@ -213,14 +213,11 @@ function initializeExpressSync() { router.post('/api/v1/settings/appstore_config', settingsScope, routes.user.requireAdmin, routes.settings.setAppstoreConfig); // email routes + router.get ('/api/v1/mail/:domain', settingsScope, routes.user.requireAdmin, routes.mail.get); router.get ('/api/v1/mail/status', settingsScope, routes.user.requireAdmin, routes.mail.getStatus); - router.get ('/api/v1/mail/mail_from_validation', settingsScope, routes.user.requireAdmin, routes.mail.getMailFromValidation); router.post('/api/v1/mail/mail_from_validation', settingsScope, routes.user.requireAdmin, routes.mail.setMailFromValidation); - router.get ('/api/v1/mail/catch_all_address', settingsScope, routes.user.requireAdmin, routes.mail.getCatchAllAddress); router.put ('/api/v1/mail/catch_all_address', settingsScope, routes.user.requireAdmin, routes.mail.setCatchAllAddress); - router.get ('/api/v1/mail/mail_relay', settingsScope, routes.user.requireAdmin, routes.mail.getMailRelay); router.post('/api/v1/mail/mail_relay', settingsScope, routes.user.requireAdmin, routes.mail.setMailRelay); - router.get ('/api/v1/mail/mail_config', settingsScope, routes.user.requireAdmin, routes.mail.getMailConfig); router.post('/api/v1/mail/mail_config', settingsScope, routes.user.requireAdmin, routes.mail.setMailConfig); // feedback