diff --git a/src/cloudron.js b/src/cloudron.js index edd679be9..7811cc582 100644 --- a/src/cloudron.js +++ b/src/cloudron.js @@ -467,8 +467,6 @@ function addDnsRecords() { } gUpdatingDns = true; - var DKIM_SELECTOR = 'cloudron'; - var dkimKey = readDkimPublicKeySync(); if (!dkimKey) return callback(new CloudronError(CloudronError.INTERNAL_ERROR, new Error('Failed to read dkim public key'))); @@ -477,7 +475,7 @@ function addDnsRecords() { var webadminRecord = { subdomain: constants.ADMIN_LOCATION, type: 'A', values: [ ip ] }; // t=s limits the domainkey to this domain and not it's subdomains - var dkimRecord = { subdomain: DKIM_SELECTOR + '._domainkey', type: 'TXT', values: [ '"v=DKIM1; t=s; p=' + dkimKey + '"' ] }; + var dkimRecord = { subdomain: constants.DKIM_SELECTOR + '._domainkey', type: 'TXT', values: [ '"v=DKIM1; t=s; p=' + dkimKey + '"' ] }; var records = [ ]; if (config.isCustomDomain()) { diff --git a/src/constants.js b/src/constants.js index 6d5c3a9d4..1d9f32a05 100644 --- a/src/constants.js +++ b/src/constants.js @@ -32,6 +32,8 @@ exports = module.exports = { DEFAULT_MEMORY_LIMIT: (256 * 1024 * 1024), // see also client.js - DEMO_USERNAME: 'cloudron' + DEMO_USERNAME: 'cloudron', + + DKIM_SELECTOR: 'cloudron' }; diff --git a/src/settings.js b/src/settings.js index 29c145050..738e532e6 100644 --- a/src/settings.js +++ b/src/settings.js @@ -58,6 +58,7 @@ exports = module.exports = { var assert = require('assert'), backups = require('./backups.js'), config = require('./config.js'), + constants = require('./constants.js'), CronJob = require('cron').CronJob, DatabaseError = require('./databaseerror.js'), debug = require('debug')('box:settings'), @@ -131,24 +132,39 @@ function getEmailDnsRecords(callback) { var records = {}; - // DKIM - var DKIM_SELECTOR = 'cloudron'; var dkimKey = cloudron.readDkimPublicKeySync(); if (!dkimKey) return callback(new CloudronError(CloudronError.INTERNAL_ERROR, new Error('Failed to read dkim public key'))); - records.dkim = {subdomain: DKIM_SELECTOR + '._domainkey', type: 'TXT', expected: 'v=DKIM1; t=s; p=' + dkimKey, value: null, status: false}; + records.dkim = { + subdomain: constants.DKIM_SELECTOR + '._domainkey', + type: 'TXT', + expected: 'v=DKIM1; t=s; p=' + dkimKey, + value: null, + status: false + }; + + records.spf = { + subdomain: '', + type: 'TXT', + value: null, + expected: null, + status: false + }; + + // check if DKIM is already setup dns.resolveTxt(records.dkim.subdomain + '.' + config.fqdn(), function (error, txtRecords) { if (error) return callback(error); + for (var i = 0; i < txtRecords.length; i++) { records.dkim.value = txtRecords[i].join(' '); records.dkim.status = (records.dkim.value == records.dkim.value); break; } - // SPF - records.spf = {subdomain: '', type: 'TXT', value: null, expected: null, status: false}; + // check if SPF is already setup dns.resolveTxt(config.fqdn(), function (error, txtRecords) { if (error) return callback(error); + var i; for (i = 0; i < txtRecords.length; i++) { if (txtRecords[i].join(' ').indexOf('v=spf1 ') !== 0) continue; // not SPF @@ -159,14 +175,14 @@ function getEmailDnsRecords(callback) { if (records.spf.status) { records.spf.expected = records.spf.value; - } else if (i == txtRecords.length) { + } else if (i === txtRecords.length) { records.spf.expected = 'v=spf1 a:' + config.adminFqdn() + ' ~all'; } else { records.spf.expected = 'v=spf1 a:' + config.adminFqdn() + ' ' + records.spf.value.slice('v=spf1 '.length); } + return callback(null, records); }); - }); }