2016-08-05 07:41:42 +00:00
|
|
|
'use strict';
|
2018-07-04 06:59:11 +00:00
|
|
|
/* global Promise */
|
2016-08-05 07:41:42 +00:00
|
|
|
|
2018-07-04 06:59:11 +00:00
|
|
|
var PromiseA;
|
|
|
|
try {
|
|
|
|
PromiseA = require('bluebird');
|
|
|
|
} catch(e) {
|
|
|
|
PromiseA = Promise;
|
|
|
|
}
|
|
|
|
var util = require('util');
|
|
|
|
function promisifyAll(obj) {
|
|
|
|
var aobj = {};
|
|
|
|
Object.keys(obj).forEach(function (key) {
|
|
|
|
aobj[key] = util.promisify(obj[key]);
|
|
|
|
});
|
|
|
|
return aobj;
|
|
|
|
}
|
|
|
|
var mkdirpAsync = util.promisify(require('mkdirp'));
|
2016-08-05 07:41:42 +00:00
|
|
|
var path = require('path');
|
2018-07-04 06:59:11 +00:00
|
|
|
var fs = require('fs');
|
|
|
|
var readFileAsync = util.promisify(fs.readFile);
|
|
|
|
var readdirAsync = util.promisify(fs.readdir);
|
2016-08-05 07:41:42 +00:00
|
|
|
var sfs = require('safe-replace');
|
2018-05-13 00:35:28 +00:00
|
|
|
var os = require('os');
|
2018-05-13 00:49:48 +00:00
|
|
|
var symlink = require('fs-symlink');
|
2016-08-05 07:41:42 +00:00
|
|
|
|
2016-08-08 22:41:51 +00:00
|
|
|
function log(debug) {
|
|
|
|
if (debug) {
|
|
|
|
var args = Array.prototype.slice.call(arguments);
|
|
|
|
args.shift();
|
|
|
|
args.unshift("[le-store-certbot]");
|
|
|
|
console.log.apply(console, args);
|
2016-08-05 07:41:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function writeRenewalConfig(args) {
|
|
|
|
var pyobj = args.pyobj;
|
|
|
|
pyobj.checkpoints = parseInt(pyobj.checkpoints, 10) || 0;
|
|
|
|
|
2018-07-04 06:59:11 +00:00
|
|
|
var pyconf = promisifyAll(require('pyconf'));
|
2016-08-05 07:41:42 +00:00
|
|
|
|
|
|
|
var liveDir = args.liveDir || path.join(args.configDir, 'live', args.domains[0]);
|
|
|
|
|
|
|
|
var certPath = args.certPath || pyobj.cert || path.join(liveDir, 'cert.pem');
|
|
|
|
var fullchainPath = args.fullchainPath || pyobj.fullchain || path.join(liveDir, 'fullchain.pem');
|
|
|
|
var chainPath = args.chainPath || pyobj.chain || path.join(liveDir, 'chain.pem');
|
|
|
|
var privkeyPath = args.privkeyPath || pyobj.privkey
|
|
|
|
//|| args.domainPrivateKeyPath || args.domainKeyPath || pyobj.keyPath
|
|
|
|
|| path.join(liveDir, 'privkey.pem');
|
|
|
|
|
2016-08-08 22:41:51 +00:00
|
|
|
log(args.debug, 'writeRenewalConfig privkeyPath', privkeyPath);
|
2016-08-05 07:41:42 +00:00
|
|
|
|
|
|
|
var updates = {
|
|
|
|
account: args.account.id
|
|
|
|
, configDir: args.configDir
|
|
|
|
, domains: args.domains
|
|
|
|
|
|
|
|
, email: args.email
|
|
|
|
, tos: args.agreeTos && true
|
|
|
|
// yes, it's an array. weird, right?
|
|
|
|
, webrootPath: args.webrootPath && [args.webrootPath] || []
|
|
|
|
, server: args.server || args.acmeDiscoveryUrl
|
|
|
|
|
|
|
|
, privkey: privkeyPath
|
|
|
|
, fullchain: fullchainPath
|
|
|
|
, cert: certPath
|
|
|
|
, chain: chainPath
|
|
|
|
|
|
|
|
, http01Port: args.http01Port
|
|
|
|
, keyPath: args.domainPrivateKeyPath || args.privkeyPath
|
|
|
|
, rsaKeySize: args.rsaKeySize
|
|
|
|
, checkpoints: pyobj.checkpoints
|
|
|
|
/* // TODO XXX what's the deal with these? they don't make sense
|
|
|
|
// are they just old junk? or do they have a meaning that I don't know about?
|
|
|
|
, fullchainPath: path.join(args.configDir, 'chain.pem')
|
|
|
|
, certPath: path.join(args.configDir, 'cert.pem')
|
|
|
|
, chainPath: path.join(args.configDir, 'chain.pem')
|
|
|
|
*/ // TODO XXX end
|
|
|
|
, workDir: args.workDir
|
|
|
|
, logsDir: args.logsDir
|
|
|
|
};
|
|
|
|
|
|
|
|
// final section is completely dynamic
|
|
|
|
// :hostname = :webroot_path
|
|
|
|
args.domains.forEach(function (hostname) {
|
|
|
|
updates[hostname] = args.webrootPath;
|
|
|
|
});
|
|
|
|
|
|
|
|
// must write back to the original pyobject or
|
|
|
|
// annotations will be lost
|
|
|
|
Object.keys(updates).forEach(function (key) {
|
|
|
|
pyobj[key] = updates[key];
|
|
|
|
});
|
|
|
|
|
|
|
|
return mkdirpAsync(path.dirname(args.renewalPath)).then(function () {
|
|
|
|
return pyconf.writeFileAsync(args.renewalPath, pyobj);
|
|
|
|
}).then(function () {
|
|
|
|
// NOTE
|
|
|
|
// writing twice seems to causes a bug,
|
|
|
|
// so instead we re-read the file from the disk
|
|
|
|
return pyconf.readFileAsync(args.renewalPath);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function pyToJson(pyobj) {
|
|
|
|
if (!pyobj) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
var jsobj = {};
|
|
|
|
Object.keys(pyobj).forEach(function (key) {
|
|
|
|
jsobj[key] = pyobj[key];
|
|
|
|
});
|
|
|
|
jsobj.__lines = undefined;
|
|
|
|
jsobj.__keys = undefined;
|
|
|
|
|
|
|
|
return jsobj;
|
|
|
|
}
|
|
|
|
|
2018-06-15 02:04:26 +00:00
|
|
|
var crypto = require('crypto');
|
|
|
|
var rnd = crypto.randomBytes(8).toString('hex');
|
2016-08-05 07:41:42 +00:00
|
|
|
var defaults = {
|
2018-05-13 04:03:06 +00:00
|
|
|
configDir: [ os.homedir(), 'letsencrypt', 'etc' ].join(path.sep) // /etc/letsencrypt/
|
2018-06-15 02:04:26 +00:00
|
|
|
, logsDir: [ os.tmpdir(), 'acme-' + rnd, 'log' ].join(path.sep) // /var/log/letsencrypt/
|
|
|
|
, webrootPath: [ os.tmpdir(), 'acme-' + rnd, 'acme-challenge' ].join(path.sep)
|
2016-08-08 22:41:51 +00:00
|
|
|
|
|
|
|
, accountsDir: [ ':configDir', 'accounts', ':serverDir' ].join(path.sep)
|
|
|
|
, renewalPath: [ ':configDir', 'renewal', ':hostname.conf' ].join(path.sep)
|
|
|
|
, renewalDir: [ ':configDir', 'renewal', '' ].join(path.sep)
|
|
|
|
, serverDirGet: function (copy) {
|
|
|
|
return (copy.server || '').replace('https://', '').replace(/(\/)$/, '').replace(/\//g, path.sep);
|
|
|
|
}
|
2016-08-05 07:41:42 +00:00
|
|
|
|
2016-08-10 02:07:55 +00:00
|
|
|
, privkeyPath: ':configDir/live/:hostname/privkey.pem'.split(/\//).join(path.sep)
|
2016-08-08 22:41:51 +00:00
|
|
|
, fullchainPath: [ ':configDir', 'live', ':hostname', 'fullchain.pem' ].join(path.sep)
|
|
|
|
, certPath: [ ':configDir', 'live', ':hostname', 'cert.pem' ].join(path.sep)
|
|
|
|
, chainPath: [ ':configDir', 'live', ':hostname', 'chain.pem' ].join(path.sep)
|
2016-08-05 07:41:42 +00:00
|
|
|
|
|
|
|
, rsaKeySize: 2048
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports.create = function (configs) {
|
|
|
|
var mergedConfigs;
|
|
|
|
|
2016-08-07 06:05:31 +00:00
|
|
|
var store = {
|
2016-08-08 22:41:51 +00:00
|
|
|
getOptions: function () {
|
2016-08-05 07:41:42 +00:00
|
|
|
if (mergedConfigs) {
|
|
|
|
return configs;
|
|
|
|
}
|
|
|
|
|
2016-08-09 19:53:44 +00:00
|
|
|
if (!configs.domainKeyPath) {
|
|
|
|
configs.domainKeyPath = configs.privkeyPath || defaults.privkeyPath;
|
|
|
|
}
|
2016-08-10 02:07:55 +00:00
|
|
|
|
2016-08-05 07:41:42 +00:00
|
|
|
Object.keys(defaults).forEach(function (key) {
|
2016-08-10 02:07:55 +00:00
|
|
|
if (!configs[key]) {
|
2016-08-05 07:41:42 +00:00
|
|
|
configs[key] = defaults[key];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
mergedConfigs = configs;
|
|
|
|
return configs;
|
|
|
|
}
|
2016-08-07 06:05:31 +00:00
|
|
|
|
2016-08-08 22:41:51 +00:00
|
|
|
, keypairs: {
|
|
|
|
checkAsync: function (keypath, format) {
|
2016-08-10 01:28:47 +00:00
|
|
|
if (!keypath) {
|
|
|
|
return null;
|
|
|
|
}
|
2018-07-04 06:59:11 +00:00
|
|
|
return readFileAsync(keypath, 'ascii').then(function (key) {
|
2016-08-08 22:41:51 +00:00
|
|
|
if ('jwk' === format) {
|
|
|
|
return { privateKeyJwk: JSON.parse(key) };
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return { privateKeyPem: key };
|
|
|
|
}
|
2016-08-09 19:53:44 +00:00
|
|
|
}, function (err) {
|
|
|
|
if ('ENOENT' !== err.code) {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
2016-08-08 22:41:51 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
, setAsync: function (keypath, keypair, format) {
|
|
|
|
return mkdirpAsync(path.dirname(keypath)).then(function () {
|
|
|
|
var key;
|
|
|
|
|
|
|
|
if ('jwk' === format) {
|
|
|
|
key = JSON.stringify(keypair.privateKeyJwk, null, ' ');
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
key = keypair.privateKeyPem;
|
|
|
|
}
|
|
|
|
|
|
|
|
return fs.writeFileAsync(keypath, key, 'ascii').then(function () {
|
|
|
|
return keypair;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-07 06:05:31 +00:00
|
|
|
//
|
|
|
|
// Certificates
|
|
|
|
//
|
|
|
|
, certificates: {
|
2016-08-08 22:41:51 +00:00
|
|
|
// Certificates
|
2016-08-07 06:05:31 +00:00
|
|
|
checkKeypairAsync: function (args) {
|
|
|
|
if (!args.domainKeyPath) {
|
|
|
|
return PromiseA.reject(new Error("missing options.domainKeyPath"));
|
|
|
|
}
|
|
|
|
|
2016-08-09 19:53:44 +00:00
|
|
|
return store.keypairs.checkAsync(args.domainKeyPath, 'pem');
|
2016-08-07 06:05:31 +00:00
|
|
|
}
|
2016-08-08 22:41:51 +00:00
|
|
|
// Certificates
|
2016-08-07 06:05:31 +00:00
|
|
|
, setKeypairAsync: function (args, keypair) {
|
2016-08-09 19:53:44 +00:00
|
|
|
return store.keypairs.setAsync(args.domainKeyPath, keypair, 'pem');
|
2016-08-07 06:05:31 +00:00
|
|
|
}
|
2016-08-08 22:41:51 +00:00
|
|
|
// Certificates
|
2016-08-07 06:05:31 +00:00
|
|
|
, checkAsync: function (args) {
|
|
|
|
if (!args.fullchainPath || !args.privkeyPath || !args.certPath || !args.chainPath) {
|
|
|
|
return PromiseA.reject(new Error("missing one or more of privkeyPath, fullchainPath, certPath, chainPath from options"));
|
|
|
|
}
|
2016-08-05 07:41:42 +00:00
|
|
|
|
2018-07-04 06:59:11 +00:00
|
|
|
//, readFileAsync(fullchainPath, 'ascii')
|
2016-08-07 06:05:31 +00:00
|
|
|
// note: if this ^^ gets added back in, the arrays below must change
|
|
|
|
return PromiseA.all([
|
2018-07-04 06:59:11 +00:00
|
|
|
readFileAsync(args.privkeyPath, 'ascii') // 0
|
|
|
|
, readFileAsync(args.certPath, 'ascii') // 1
|
|
|
|
, readFileAsync(args.chainPath, 'ascii') // 2
|
2016-08-07 06:05:31 +00:00
|
|
|
|
|
|
|
// stat the file, not the link
|
|
|
|
, fs.statAsync(args.certPath) // 3
|
|
|
|
]).then(function (arr) {
|
|
|
|
return {
|
2016-08-08 17:10:27 +00:00
|
|
|
privkey: arr[0] // privkey.pem
|
2016-08-07 06:05:31 +00:00
|
|
|
, cert: arr[1] // cert.pem
|
2016-08-08 17:10:27 +00:00
|
|
|
, chain: arr[2] // chain.pem
|
2016-08-07 06:05:31 +00:00
|
|
|
/*
|
2016-08-08 17:10:27 +00:00
|
|
|
// TODO populate these values only if they are known
|
2016-08-07 06:05:31 +00:00
|
|
|
, issuedAt: arr[3].mtime.valueOf()
|
|
|
|
, expiresAt: arr[3].mtime.valueOf() + (90 * 24 * 60 * 60 * 100)
|
|
|
|
*/
|
|
|
|
};
|
|
|
|
}, function (err) {
|
|
|
|
if (args.debug) {
|
|
|
|
console.error("[le-store-certbot] certificates.check");
|
|
|
|
console.error(err.stack);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
}
|
2016-08-08 22:41:51 +00:00
|
|
|
// Certificates
|
2016-08-07 06:05:31 +00:00
|
|
|
, setAsync: function (args) {
|
2016-08-09 19:53:44 +00:00
|
|
|
return store.configs.getAsync(args).then(function (pyobj) {
|
|
|
|
var pems = args.pems;
|
|
|
|
|
|
|
|
pyobj.checkpoints = parseInt(pyobj.checkpoints, 10) || 0;
|
|
|
|
|
|
|
|
var liveDir = args.liveDir || path.join(args.configDir, 'live', args.domains[0]);
|
|
|
|
|
|
|
|
var certPath = args.certPath || pyobj.cert || path.join(liveDir, 'cert.pem');
|
|
|
|
var fullchainPath = args.fullchainPath || pyobj.fullchain || path.join(liveDir, 'fullchain.pem');
|
|
|
|
var chainPath = args.chainPath || pyobj.chain || path.join(liveDir, 'chain.pem');
|
|
|
|
var privkeyPath = args.privkeyPath || pyobj.privkey
|
|
|
|
|| args.domainKeyPath
|
|
|
|
|| path.join(liveDir, 'privkey.pem');
|
|
|
|
|
|
|
|
var archiveDir = args.archiveDir || path.join(args.configDir, 'archive', args.domains[0]);
|
|
|
|
|
|
|
|
var checkpoints = pyobj.checkpoints.toString();
|
|
|
|
var certArchive = path.join(archiveDir, 'cert' + checkpoints + '.pem');
|
|
|
|
var fullchainArchive = path.join(archiveDir, 'fullchain' + checkpoints + '.pem');
|
|
|
|
var chainArchive = path.join(archiveDir, 'chain'+ checkpoints + '.pem');
|
|
|
|
var privkeyArchive = path.join(archiveDir, 'privkey' + checkpoints + '.pem');
|
|
|
|
|
|
|
|
return mkdirpAsync(archiveDir).then(function () {
|
|
|
|
return PromiseA.all([
|
|
|
|
sfs.writeFileAsync(certArchive, pems.cert, 'ascii')
|
|
|
|
, sfs.writeFileAsync(chainArchive, pems.chain, 'ascii')
|
2018-07-04 06:59:48 +00:00
|
|
|
, sfs.writeFileAsync(fullchainArchive, [ pems.cert, pems.chain ].join('\n'), 'ascii')
|
2016-08-09 19:53:44 +00:00
|
|
|
, sfs.writeFileAsync(privkeyArchive, pems.privkey, 'ascii')
|
|
|
|
]);
|
|
|
|
}).then(function () {
|
|
|
|
return mkdirpAsync(liveDir);
|
|
|
|
}).then(function () {
|
|
|
|
return PromiseA.all([
|
2018-05-13 00:49:48 +00:00
|
|
|
symlink(certArchive, certPath)
|
|
|
|
, symlink(chainArchive, chainPath)
|
|
|
|
, symlink(fullchainArchive, fullchainPath)
|
|
|
|
, symlink(privkeyArchive, privkeyPath)
|
2016-08-09 19:53:44 +00:00
|
|
|
]);
|
|
|
|
}).then(function () {
|
|
|
|
pyobj.checkpoints += 1;
|
|
|
|
args.checkpoints += 1;
|
|
|
|
|
|
|
|
// TODO other than for compatibility this is optional, right?
|
|
|
|
// or is it actually needful for renewal? (i.e. list of domains)
|
|
|
|
return writeRenewalConfig(args);
|
|
|
|
}).then(function () {
|
|
|
|
return {
|
|
|
|
privkey: pems.privkey
|
|
|
|
, cert: pems.cert
|
|
|
|
, chain: pems.chain
|
|
|
|
|
|
|
|
/*
|
|
|
|
// TODO populate these only if they are actually known
|
|
|
|
, issuedAt: Date.now()
|
|
|
|
, expiresAt: Date.now() + (90 * 24 * 60 * 60 * 100)
|
|
|
|
*/
|
|
|
|
};
|
|
|
|
});
|
2016-08-07 06:05:31 +00:00
|
|
|
});
|
|
|
|
}
|
2016-08-08 17:10:27 +00:00
|
|
|
|
2016-08-05 07:41:42 +00:00
|
|
|
}
|
|
|
|
|
2016-08-07 06:05:31 +00:00
|
|
|
//
|
|
|
|
// Accounts
|
|
|
|
//
|
|
|
|
, accounts: {
|
2016-08-08 22:41:51 +00:00
|
|
|
// Accounts
|
|
|
|
_getAccountKeyPath: function (args) {
|
|
|
|
var promise = PromiseA.resolve(args.accountId);
|
|
|
|
|
|
|
|
if (args.email && !args.accountKeyPath && !args.accountId) {
|
|
|
|
promise = store.accounts._getAccountIdByEmail(args);
|
|
|
|
}
|
|
|
|
|
|
|
|
return promise.then(function (accountId) {
|
|
|
|
if (!accountId) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return args.accountKeyPath || path.join(args.accountsDir, accountId, 'private_key.json');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
// Accounts
|
|
|
|
, _getAccountIdByEmail: function (args) {
|
|
|
|
// If we read 10,000 account directories looking for
|
|
|
|
// just one email address, that could get crazy.
|
|
|
|
// We should have a folder per email and list
|
|
|
|
// each account as a file in the folder
|
|
|
|
// TODO
|
|
|
|
var email = args.email;
|
|
|
|
if ('string' !== typeof email) {
|
|
|
|
log(args.debug, "No email given");
|
|
|
|
return PromiseA.resolve(null);
|
|
|
|
}
|
2018-07-04 06:59:11 +00:00
|
|
|
return readdirAsync(args.accountsDir).then(function (nodes) {
|
2016-08-08 22:41:51 +00:00
|
|
|
log(args.debug, "success reading arg.accountsDir");
|
|
|
|
|
|
|
|
return PromiseA.all(nodes.map(function (node) {
|
2018-07-04 06:59:11 +00:00
|
|
|
return readFileAsync(path.join(args.accountsDir, node, 'regr.json'), 'utf8').then(function (text) {
|
2016-08-08 22:41:51 +00:00
|
|
|
var regr = JSON.parse(text);
|
|
|
|
regr.__accountId = node;
|
|
|
|
|
|
|
|
return regr;
|
|
|
|
});
|
|
|
|
})).then(function (regrs) {
|
|
|
|
var accountId;
|
|
|
|
|
|
|
|
log(args.debug, "regrs.length", regrs.length);
|
|
|
|
|
|
|
|
regrs.some(function (regr) {
|
|
|
|
return regr.body.contact.some(function (contact) {
|
|
|
|
var match = contact.toLowerCase() === 'mailto:' + email.toLowerCase();
|
|
|
|
if (match) {
|
|
|
|
accountId = regr.__accountId;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!accountId) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return accountId;
|
|
|
|
});
|
|
|
|
}).then(function (accountId) {
|
|
|
|
return accountId;
|
|
|
|
}, function (err) {
|
|
|
|
if ('ENOENT' === err.code) {
|
|
|
|
// ignore error
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return PromiseA.reject(err);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
// Accounts
|
2016-08-08 23:15:24 +00:00
|
|
|
, _getAccountIdByPublicKey: function (keypair) {
|
|
|
|
// we use insecure md5 - even though we know it's bad - because that's how the python client did
|
2018-06-15 02:04:26 +00:00
|
|
|
var pubkey = keypair.publicKeyPem.replace(/\r/g, '');
|
|
|
|
return crypto.createHash('md5').update(pubkey).digest('hex');
|
2016-08-08 23:15:24 +00:00
|
|
|
}
|
|
|
|
// Accounts
|
2016-08-08 22:41:51 +00:00
|
|
|
, checkKeypairAsync: function (args) {
|
|
|
|
if (!(args.accountKeyPath || args.accountsDir)) {
|
|
|
|
return PromiseA.reject(new Error("must provide one of options.accountKeyPath or options.accountsDir"));
|
|
|
|
}
|
|
|
|
|
|
|
|
return store.accounts._getAccountKeyPath(args).then(function (keypath) {
|
|
|
|
return store.keypairs.checkAsync(keypath, 'jwk');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
// Accounts
|
|
|
|
, setKeypairAsync: function (args, keypair) {
|
|
|
|
var accountId;
|
|
|
|
|
|
|
|
if (args.email) {
|
2016-08-08 23:15:24 +00:00
|
|
|
accountId = store.accounts._getAccountIdByPublicKey(keypair);
|
2016-08-08 22:41:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return store.accounts._getAccountKeyPath({
|
|
|
|
accountsDir: args.accountsDir
|
|
|
|
, email: args.email
|
|
|
|
, accountId: args.accountId || accountId
|
|
|
|
}).then(function (keypath) {
|
|
|
|
return store.keypairs.setAsync(keypath, keypair, 'jwk');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
// Accounts
|
|
|
|
, checkAsync: function (args) {
|
2016-08-07 06:05:31 +00:00
|
|
|
var promise;
|
|
|
|
var files = {};
|
|
|
|
var accountId;
|
|
|
|
|
|
|
|
if (args.accountId) {
|
|
|
|
promise = PromiseA.resolve(args.accountId);
|
|
|
|
}
|
|
|
|
else if (args.email) {
|
2016-08-08 22:41:51 +00:00
|
|
|
promise = store.accounts._getAccountIdByEmail(args);
|
2016-08-07 06:05:31 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
promise = PromiseA.reject(new Error("must provide accountId or email"));
|
|
|
|
}
|
|
|
|
|
2016-08-08 22:41:51 +00:00
|
|
|
return promise.then(function (_accountId) {
|
|
|
|
log(args.debug, 'accountId:', _accountId);
|
|
|
|
if (!_accountId) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-08-07 06:05:31 +00:00
|
|
|
accountId = _accountId;
|
|
|
|
var accountDir = path.join(args.accountsDir, accountId);
|
|
|
|
var configs = [ 'meta.json', 'private_key.json', 'regr.json' ];
|
|
|
|
|
|
|
|
return PromiseA.all(configs.map(function (filename) {
|
|
|
|
var keyname = filename.slice(0, -5);
|
|
|
|
|
2018-07-04 06:59:11 +00:00
|
|
|
return readFileAsync(path.join(accountDir, filename), 'utf8').then(function (text) {
|
2016-08-07 06:05:31 +00:00
|
|
|
var data;
|
|
|
|
|
|
|
|
try {
|
|
|
|
data = JSON.parse(text);
|
|
|
|
} catch(e) {
|
|
|
|
files[keyname] = { error: e };
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
files[keyname] = data;
|
2016-08-08 22:41:51 +00:00
|
|
|
|
|
|
|
return true;
|
2016-08-07 06:05:31 +00:00
|
|
|
}, function (err) {
|
2016-08-08 22:41:51 +00:00
|
|
|
log(args.debug, 'Error reading account files:', err);
|
2016-08-07 06:05:31 +00:00
|
|
|
files[keyname] = { error: err };
|
|
|
|
});
|
|
|
|
}));
|
2016-08-08 22:41:51 +00:00
|
|
|
}).then(function (hasAccount) {
|
|
|
|
if (!hasAccount) {
|
|
|
|
return null;
|
|
|
|
}
|
2016-08-07 06:05:31 +00:00
|
|
|
var err;
|
|
|
|
|
|
|
|
if (!Object.keys(files).every(function (key) {
|
|
|
|
return !files[key].error;
|
|
|
|
}) || !files.private_key || !files.private_key.n) {
|
2016-08-08 22:41:51 +00:00
|
|
|
err = new Error("Account '" + accountId + "' was corrupt (had id, but was missing files).");
|
2016-08-07 06:05:31 +00:00
|
|
|
err.code = 'E_ACCOUNT_CORRUPT';
|
|
|
|
err.data = files;
|
|
|
|
return PromiseA.reject(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
//files.private_key;
|
|
|
|
//files.regr;
|
|
|
|
//files.meta;
|
|
|
|
files.accountId = accountId; // preserve current account id
|
|
|
|
files.id = accountId;
|
|
|
|
files.keypair = { privateKeyJwk: files.private_key };
|
|
|
|
|
|
|
|
return files;
|
2016-08-05 07:41:42 +00:00
|
|
|
});
|
2016-08-07 06:05:31 +00:00
|
|
|
}
|
2016-08-08 22:41:51 +00:00
|
|
|
// Accounts
|
|
|
|
, setAsync: function (args, reg) {
|
2016-08-07 06:05:31 +00:00
|
|
|
var os = require("os");
|
2016-08-08 23:15:24 +00:00
|
|
|
var accountId = store.accounts._getAccountIdByPublicKey(reg.keypair);
|
2016-08-08 22:41:51 +00:00
|
|
|
var accountDir = path.join(args.accountsDir, accountId);
|
|
|
|
var accountMeta = {
|
|
|
|
creation_host: os.hostname()
|
|
|
|
, creation_dt: new Date().toISOString()
|
2016-08-07 06:05:31 +00:00
|
|
|
};
|
2018-05-13 00:49:48 +00:00
|
|
|
var uri = args.server.replace(/\/directory.*/,
|
|
|
|
'/acme/reg/' + accountId);
|
2016-08-07 06:05:31 +00:00
|
|
|
|
|
|
|
return mkdirpAsync(accountDir).then(function () {
|
2018-05-15 13:49:04 +00:00
|
|
|
var regrBody = {
|
|
|
|
body: reg.receipt,
|
|
|
|
uri: uri,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (typeof reg.newAuthzUrl !== 'undefined') {
|
|
|
|
regrBody.new_authzr_uri = reg.newAuthzUrl;
|
|
|
|
}
|
2016-08-07 06:05:31 +00:00
|
|
|
|
|
|
|
// TODO abstract file writing
|
|
|
|
return PromiseA.all([
|
|
|
|
// meta.json {"creation_host": "ns1.redirect-www.org", "creation_dt": "2015-12-11T04:14:38Z"}
|
2016-08-08 22:41:51 +00:00
|
|
|
fs.writeFileAsync(path.join(accountDir, 'meta.json'), JSON.stringify(accountMeta), 'utf8')
|
2016-08-07 06:05:31 +00:00
|
|
|
// private_key.json { "e", "d", "n", "q", "p", "kty", "qi", "dp", "dq" }
|
2016-08-08 22:41:51 +00:00
|
|
|
, fs.writeFileAsync(path.join(accountDir, 'private_key.json'), JSON.stringify(reg.keypair.privateKeyJwk), 'utf8')
|
2016-08-07 06:05:31 +00:00
|
|
|
// regr.json:
|
|
|
|
/*
|
|
|
|
{ body: { contact: [ 'mailto:coolaj86@gmail.com' ],
|
|
|
|
agreement: 'https://letsencrypt.org/documents/LE-SA-v1.0.1-July-27-2015.pdf',
|
|
|
|
key: { e: 'AQAB', kty: 'RSA', n: '...' } },
|
|
|
|
uri: 'https://acme-v01.api.letsencrypt.org/acme/reg/71272',
|
|
|
|
new_authzr_uri: 'https://acme-v01.api.letsencrypt.org/acme/new-authz',
|
|
|
|
terms_of_service: 'https://letsencrypt.org/documents/LE-SA-v1.0.1-July-27-2015.pdf' }
|
|
|
|
*/
|
2018-05-13 00:49:48 +00:00
|
|
|
, fs.writeFileAsync(path.join(accountDir, 'regr.json'),
|
2018-05-15 13:49:04 +00:00
|
|
|
JSON.stringify(regrBody),
|
2018-05-13 00:49:48 +00:00
|
|
|
'utf8')
|
2016-08-07 06:05:31 +00:00
|
|
|
]);
|
2016-08-08 23:15:24 +00:00
|
|
|
}).then(function () {
|
|
|
|
return {
|
|
|
|
id: accountId
|
|
|
|
, accountId: accountId
|
|
|
|
, email: args.email
|
|
|
|
, keypair: reg.keypair
|
|
|
|
, receipt: reg.receipt
|
|
|
|
};
|
2016-08-07 06:05:31 +00:00
|
|
|
});
|
|
|
|
}
|
2016-08-08 22:41:51 +00:00
|
|
|
// Accounts
|
2016-08-07 06:05:31 +00:00
|
|
|
, getAccountIdAsync: function (args) {
|
2018-07-04 06:59:11 +00:00
|
|
|
var pyconf = promisifyAll(require('pyconf'));
|
2016-08-07 06:05:31 +00:00
|
|
|
|
|
|
|
return pyconf.readFileAsync(args.renewalPath).then(function (renewal) {
|
|
|
|
var accountId = renewal.account;
|
|
|
|
renewal = renewal.account;
|
|
|
|
|
|
|
|
return accountId;
|
|
|
|
}, function (err) {
|
|
|
|
if ("ENOENT" === err.code) {
|
2016-08-08 22:41:51 +00:00
|
|
|
return store.accounts._getAccountIdByEmail(args);
|
2016-08-07 06:05:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return PromiseA.reject(err);
|
|
|
|
});
|
|
|
|
}
|
2016-08-05 07:41:42 +00:00
|
|
|
}
|
|
|
|
|
2016-08-07 06:05:31 +00:00
|
|
|
//
|
|
|
|
// Configs
|
|
|
|
//
|
|
|
|
, configs: {
|
2016-08-08 22:41:51 +00:00
|
|
|
// Configs
|
2016-08-07 06:05:31 +00:00
|
|
|
checkAsync: function (copy) {
|
|
|
|
copy.domains = [];
|
|
|
|
|
|
|
|
return store.configs._checkHelperAsync(copy).then(function (pyobj) {
|
|
|
|
var exists = pyobj.checkpoints >= 0;
|
|
|
|
if (!exists) {
|
|
|
|
return null;
|
|
|
|
}
|
2016-08-05 07:41:42 +00:00
|
|
|
|
2016-08-07 06:05:31 +00:00
|
|
|
return pyToJson(pyobj);
|
|
|
|
});
|
|
|
|
}
|
2016-08-08 22:41:51 +00:00
|
|
|
// Configs
|
2016-08-07 06:05:31 +00:00
|
|
|
, _checkHelperAsync: function (args) {
|
2018-07-04 06:59:11 +00:00
|
|
|
var pyconf = promisifyAll(require('pyconf'));
|
2016-08-07 06:05:31 +00:00
|
|
|
|
|
|
|
return pyconf.readFileAsync(args.renewalPath).then(function (pyobj) {
|
|
|
|
return pyobj;
|
|
|
|
}, function () {
|
|
|
|
return pyconf.readFileAsync(path.join(__dirname, 'renewal.conf.tpl')).then(function (pyobj) {
|
|
|
|
return pyobj;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2016-08-08 22:41:51 +00:00
|
|
|
// Configs
|
2016-08-07 06:05:31 +00:00
|
|
|
, getAsync: function (args) {
|
|
|
|
return store.configs._checkHelperAsync(args).then(function (pyobj) {
|
|
|
|
var minver = pyobj.checkpoints >= 0;
|
2016-08-05 07:41:42 +00:00
|
|
|
|
2016-08-07 06:05:31 +00:00
|
|
|
args.pyobj = pyobj;
|
|
|
|
|
|
|
|
if (!minver) {
|
|
|
|
args.checkpoints = 0;
|
|
|
|
pyobj.checkpoints = 0;
|
|
|
|
return writeRenewalConfig(args);
|
|
|
|
}
|
|
|
|
|
|
|
|
// args.account.id = pyobj.account
|
|
|
|
// args.configDir = args.configDir || pyobj.configDir;
|
|
|
|
|
|
|
|
args.checkpoints = pyobj.checkpoints;
|
|
|
|
|
|
|
|
args.agreeTos = (args.agreeTos || pyobj.tos) && true;
|
|
|
|
args.email = args.email || pyobj.email;
|
|
|
|
args.domains = args.domains || pyobj.domains;
|
|
|
|
|
|
|
|
// yes, it's an array. weird, right?
|
|
|
|
args.webrootPath = args.webrootPath || pyobj.webrootPath[0];
|
|
|
|
args.server = args.server || args.acmeDiscoveryUrl || pyobj.server;
|
|
|
|
|
|
|
|
args.certPath = args.certPath || pyobj.cert;
|
|
|
|
args.privkeyPath = args.privkeyPath || pyobj.privkey;
|
|
|
|
args.chainPath = args.chainPath || pyobj.chain;
|
|
|
|
args.fullchainPath = args.fullchainPath || pyobj.fullchain;
|
|
|
|
|
|
|
|
//, workDir: args.workDir
|
|
|
|
//, logsDir: args.logsDir
|
|
|
|
args.rsaKeySize = args.rsaKeySize || pyobj.rsaKeySize;
|
|
|
|
args.http01Port = args.http01Port || pyobj.http01Port;
|
2016-08-09 19:53:44 +00:00
|
|
|
args.domainKeyPath = args.domainKeyPath || args.keyPath || pyobj.keyPath;
|
2016-08-07 06:05:31 +00:00
|
|
|
|
|
|
|
return writeRenewalConfig(args);
|
|
|
|
});
|
|
|
|
}
|
2016-08-08 22:41:51 +00:00
|
|
|
// Configs
|
2016-08-07 06:05:31 +00:00
|
|
|
, allAsync: function (copy) {
|
|
|
|
copy.domains = [];
|
|
|
|
|
2018-07-04 06:59:11 +00:00
|
|
|
return readdirAsync(copy.renewalDir).then(function (nodes) {
|
2016-08-07 06:05:31 +00:00
|
|
|
nodes = nodes.filter(function (node) {
|
|
|
|
return /^[a-z0-9]+.*\.conf$/.test(node);
|
|
|
|
});
|
|
|
|
|
|
|
|
return PromiseA.all(nodes.map(function (node) {
|
|
|
|
copy.domains = [node.replace(/\.conf$/, '')];
|
|
|
|
return store.configs.getAsync(copy);
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
}
|
2016-08-05 07:41:42 +00:00
|
|
|
}
|
2016-08-07 06:05:31 +00:00
|
|
|
|
2016-08-05 07:41:42 +00:00
|
|
|
};
|
2016-08-07 06:05:31 +00:00
|
|
|
|
|
|
|
return store;
|
2016-08-05 07:41:42 +00:00
|
|
|
};
|