2015-12-15 15:21:27 +00:00
|
|
|
'use strict';
|
|
|
|
|
2015-12-19 19:59:50 +00:00
|
|
|
var path = require('path');
|
2017-04-11 08:53:04 +00:00
|
|
|
var homeRe = new RegExp("^~(\\/|\\\\|\\" + path.sep + ")");
|
2018-04-27 17:38:15 +00:00
|
|
|
// very basic check. Allows *.example.com.
|
2018-04-27 17:44:49 +00:00
|
|
|
var re = /^(\*\.)?[a-zA-Z0-9\.\-]+$/;
|
2015-12-19 10:18:32 +00:00
|
|
|
var punycode = require('punycode');
|
2018-04-24 17:43:27 +00:00
|
|
|
var promisify = (require('util').promisify || require('bluebird').promisify);
|
|
|
|
var dnsResolveMxAsync = promisify(require('dns').resolveMx);
|
2015-12-19 10:18:32 +00:00
|
|
|
|
2016-08-06 05:32:59 +00:00
|
|
|
module.exports.attachCertInfo = function (results) {
|
|
|
|
// XXX Note: Parsing the certificate info comes at a great cost (~500kb)
|
2016-08-11 16:04:30 +00:00
|
|
|
var getCertInfo = require('certpem').info;
|
2016-08-06 05:32:59 +00:00
|
|
|
var certInfo = getCertInfo(results.cert);
|
|
|
|
|
2016-08-09 19:19:29 +00:00
|
|
|
// subject, altnames, issuedAt, expiresAt
|
|
|
|
Object.keys(certInfo).forEach(function (key) {
|
|
|
|
results[key] = certInfo[key];
|
|
|
|
});
|
2016-08-06 05:32:59 +00:00
|
|
|
|
|
|
|
return results;
|
|
|
|
};
|
|
|
|
|
2015-12-19 19:59:50 +00:00
|
|
|
module.exports.isValidDomain = function (domain) {
|
2015-12-19 10:18:32 +00:00
|
|
|
if (re.test(domain)) {
|
|
|
|
return domain;
|
|
|
|
}
|
|
|
|
|
|
|
|
domain = punycode.toASCII(domain);
|
|
|
|
|
|
|
|
if (re.test(domain)) {
|
|
|
|
return domain;
|
|
|
|
}
|
|
|
|
|
|
|
|
return '';
|
|
|
|
};
|
|
|
|
|
2016-08-04 22:49:35 +00:00
|
|
|
module.exports.merge = function (/*defaults, args*/) {
|
|
|
|
var allDefaults = Array.prototype.slice.apply(arguments);
|
2016-08-08 19:17:09 +00:00
|
|
|
var args = allDefaults.shift();
|
2015-12-17 08:46:40 +00:00
|
|
|
var copy = {};
|
|
|
|
|
2016-08-04 22:49:35 +00:00
|
|
|
allDefaults.forEach(function (defaults) {
|
|
|
|
Object.keys(defaults).forEach(function (key) {
|
|
|
|
copy[key] = defaults[key];
|
|
|
|
});
|
2015-12-17 08:46:40 +00:00
|
|
|
});
|
2016-08-04 22:49:35 +00:00
|
|
|
|
2015-12-17 08:46:40 +00:00
|
|
|
Object.keys(args).forEach(function (key) {
|
|
|
|
copy[key] = args[key];
|
|
|
|
});
|
|
|
|
|
|
|
|
return copy;
|
|
|
|
};
|
|
|
|
|
2016-08-04 22:49:35 +00:00
|
|
|
module.exports.tplCopy = function (copy) {
|
2015-12-19 19:59:50 +00:00
|
|
|
var homedir = require('homedir')();
|
2016-08-08 22:11:25 +00:00
|
|
|
var tplKeys;
|
|
|
|
|
|
|
|
copy.hostnameGet = function (copy) {
|
|
|
|
return (copy.domains || [])[0] || copy.domain;
|
2015-12-20 00:27:48 +00:00
|
|
|
};
|
|
|
|
|
2015-12-17 08:46:40 +00:00
|
|
|
Object.keys(copy).forEach(function (key) {
|
2016-08-08 22:11:25 +00:00
|
|
|
var newName;
|
|
|
|
if (!/Get$/.test(key)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
newName = key.replace(/Get$/, '');
|
|
|
|
copy[newName] = copy[newName] || copy[key](copy);
|
|
|
|
});
|
|
|
|
|
|
|
|
tplKeys = Object.keys(copy);
|
|
|
|
tplKeys.sort(function (a, b) {
|
|
|
|
return b.length - a.length;
|
|
|
|
});
|
|
|
|
|
|
|
|
tplKeys.forEach(function (key) {
|
2016-08-05 08:14:40 +00:00
|
|
|
if ('string' !== typeof copy[key]) {
|
|
|
|
return;
|
2015-12-17 08:46:40 +00:00
|
|
|
}
|
|
|
|
|
2016-08-05 08:14:40 +00:00
|
|
|
copy[key] = copy[key].replace(homeRe, homedir + path.sep);
|
2016-08-08 22:11:25 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
tplKeys.forEach(function (key) {
|
|
|
|
if ('string' !== typeof copy[key]) {
|
|
|
|
return;
|
|
|
|
}
|
2016-08-05 08:14:40 +00:00
|
|
|
|
2016-08-08 22:11:25 +00:00
|
|
|
tplKeys.forEach(function (tplname) {
|
|
|
|
if (!copy[tplname]) {
|
2016-08-05 08:14:40 +00:00
|
|
|
// what can't be templated now may be templatable later
|
|
|
|
return;
|
|
|
|
}
|
2016-08-08 22:11:25 +00:00
|
|
|
copy[key] = copy[key].replace(':' + tplname, copy[tplname]);
|
2016-08-05 08:14:40 +00:00
|
|
|
});
|
|
|
|
});
|
2016-08-07 06:02:02 +00:00
|
|
|
|
|
|
|
return copy;
|
2015-12-17 08:46:40 +00:00
|
|
|
};
|
2016-08-08 19:17:09 +00:00
|
|
|
|
|
|
|
module.exports.testEmail = function (email) {
|
|
|
|
var parts = (email||'').split('@');
|
|
|
|
var err;
|
|
|
|
|
|
|
|
if (2 !== parts.length || !parts[0] || !parts[1]) {
|
|
|
|
err = new Error("malformed email address '" + email + "'");
|
|
|
|
err.code = 'E_EMAIL';
|
2018-04-20 05:19:02 +00:00
|
|
|
return Promise.reject(err);
|
2016-08-08 19:17:09 +00:00
|
|
|
}
|
|
|
|
|
2018-04-20 05:19:02 +00:00
|
|
|
return dnsResolveMxAsync(parts[1]).then(function (records) {
|
2016-08-08 19:17:09 +00:00
|
|
|
// records only returns when there is data
|
|
|
|
if (!records.length) {
|
|
|
|
throw new Error("sanity check fail: success, but no MX records returned");
|
|
|
|
}
|
|
|
|
return email;
|
|
|
|
}, function (err) {
|
|
|
|
if ('ENODATA' === err.code) {
|
|
|
|
err = new Error("no MX records found for '" + parts[1] + "'");
|
|
|
|
err.code = 'E_EMAIL';
|
2018-04-20 05:19:02 +00:00
|
|
|
return Promise.reject(err);
|
2016-08-08 19:17:09 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|