1 /******************************************************************************
3 * Copyright (C) 2012-2018 Icinga Development Team (https://www.icinga.com/) *
5 * This program is free software; you can redistribute it and/or *
6 * modify it under the terms of the GNU General Public License *
7 * as published by the Free Software Foundation; either version 2 *
8 * of the License, or (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the Free Software Foundation *
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ******************************************************************************/
20 #include "cli/carestorecommand.hpp"
21 #include "remote/apilistener.hpp"
22 #include "base/logger.hpp"
23 #include "base/application.hpp"
24 #include "base/tlsutility.hpp"
26 using namespace icinga;
28 REGISTER_CLICOMMAND("ca/restore", CARestoreCommand);
30 String CARestoreCommand::GetDescription() const
32 return "Restores a previously removed certificate request.";
35 String CARestoreCommand::GetShortDescription() const
37 return "restores a removed certificate request";
40 int CARestoreCommand::GetMinArguments() const
45 ImpersonationLevel CARestoreCommand::GetImpersonationLevel() const
47 return ImpersonateIcinga;
51 * The entry point for the "ca restore" CLI command.
53 * @returns An exit status.
55 int CARestoreCommand::Run(const boost::program_options::variables_map& vm, const std::vector<std::string>& ap) const
57 String requestFile = ApiListener::GetCertificateRequestsDir() + "/" + ap[0] + ".removed";
59 if (!Utility::PathExists(requestFile)) {
60 Log(LogCritical, "cli")
61 << "No removed request exists for fingerprint '" << ap[0] << "'.";
64 Utility::SaveJsonFile(ApiListener::GetCertificateRequestsDir() + "/" + ap[0] + ".json", 700, Utility::LoadJsonFile(requestFile));
65 if(remove(requestFile.CStr()) != 0)
68 Log(LogInformation, "cli")
69 << "Certificate " << ap[0] << " restored, you can now sign it using:\n"
70 << "\"icinga2 ca sign " << ap[0] << "\"";