2019-05-10 17:33:28 +02:00
|
|
|
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
|
2018-08-08 18:09:41 +02:00
|
|
|
|
|
|
|
#include "cli/carestorecommand.hpp"
|
|
|
|
#include "base/logger.hpp"
|
|
|
|
#include "base/application.hpp"
|
|
|
|
#include "base/tlsutility.hpp"
|
2019-05-10 17:33:28 +02:00
|
|
|
#include "remote/apilistener.hpp"
|
2018-08-08 18:09:41 +02:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
|
|
|
REGISTER_CLICOMMAND("ca/restore", CARestoreCommand);
|
|
|
|
|
2019-06-07 10:33:35 +02:00
|
|
|
/**
|
|
|
|
* Provide a long CLI description sentence.
|
|
|
|
*
|
|
|
|
* @return text
|
|
|
|
*/
|
2018-08-08 18:09:41 +02:00
|
|
|
String CARestoreCommand::GetDescription() const
|
|
|
|
{
|
|
|
|
return "Restores a previously removed certificate request.";
|
|
|
|
}
|
|
|
|
|
2019-06-07 10:33:35 +02:00
|
|
|
/**
|
|
|
|
* Provide a short CLI description.
|
|
|
|
*
|
|
|
|
* @return text
|
|
|
|
*/
|
2018-08-08 18:09:41 +02:00
|
|
|
String CARestoreCommand::GetShortDescription() const
|
|
|
|
{
|
|
|
|
return "restores a removed certificate request";
|
|
|
|
}
|
|
|
|
|
2019-06-07 10:33:35 +02:00
|
|
|
/**
|
|
|
|
* Define minimum arguments without key parameter.
|
|
|
|
*
|
|
|
|
* @return number of arguments
|
|
|
|
*/
|
2018-08-08 18:09:41 +02:00
|
|
|
int CARestoreCommand::GetMinArguments() const
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-06-07 10:33:35 +02:00
|
|
|
/**
|
|
|
|
* Impersonate as Icinga user.
|
|
|
|
*
|
|
|
|
* @return impersonate level
|
|
|
|
*/
|
2018-08-08 18:09:41 +02:00
|
|
|
ImpersonationLevel CARestoreCommand::GetImpersonationLevel() const
|
|
|
|
{
|
|
|
|
return ImpersonateIcinga;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The entry point for the "ca restore" CLI command.
|
|
|
|
*
|
|
|
|
* @returns An exit status.
|
|
|
|
*/
|
|
|
|
int CARestoreCommand::Run(const boost::program_options::variables_map& vm, const std::vector<std::string>& ap) const
|
|
|
|
{
|
2019-05-10 17:33:28 +02:00
|
|
|
String fingerPrint = ap[0];
|
|
|
|
String removedRequestFile = ApiListener::GetCertificateRequestsDir() + "/" + fingerPrint + ".removed";
|
2018-08-08 18:09:41 +02:00
|
|
|
|
2019-05-10 17:33:28 +02:00
|
|
|
if (!Utility::PathExists(removedRequestFile)) {
|
2018-08-08 18:09:41 +02:00
|
|
|
Log(LogCritical, "cli")
|
2019-05-10 17:33:28 +02:00
|
|
|
<< "Cannot find removed fingerprint '" << fingerPrint << "', bailing out.";
|
2018-08-08 18:09:41 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2018-08-08 20:59:58 +02:00
|
|
|
|
2019-05-10 17:33:28 +02:00
|
|
|
Dictionary::Ptr request = Utility::LoadJsonFile(removedRequestFile);
|
2018-08-08 20:59:58 +02:00
|
|
|
std::shared_ptr<X509> certRequest = StringToCertificate(request->Get("cert_request"));
|
|
|
|
|
|
|
|
if (!certRequest) {
|
|
|
|
Log(LogCritical, "cli", "Certificate request is invalid. Could not parse X.509 certificate for the 'cert_request' attribute.");
|
2019-05-10 17:33:28 +02:00
|
|
|
/* Purge the file when we know that it is broken. */
|
|
|
|
Utility::Remove(removedRequestFile);
|
2018-08-08 20:59:58 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-05-10 17:33:28 +02:00
|
|
|
Utility::SaveJsonFile(ApiListener::GetCertificateRequestsDir() + "/" + fingerPrint + ".json", 0600, request);
|
|
|
|
|
|
|
|
Utility::Remove(removedRequestFile);
|
2018-08-08 18:09:41 +02:00
|
|
|
|
|
|
|
Log(LogInformation, "cli")
|
2019-05-10 17:33:28 +02:00
|
|
|
<< "Restored certificate request for CN '" << GetCertificateCN(certRequest) << "', sign it with:\n"
|
|
|
|
<< "\"icinga2 ca sign " << fingerPrint << "\"";
|
2018-08-08 18:09:41 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|