]> granicus.if.org Git - icinga2/blob - lib/cli/carestorecommand.cpp
Added ca restore command+docs to undo effects of ca remove
[icinga2] / lib / cli / carestorecommand.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2018 Icinga Development Team (https://www.icinga.com/)  *
4  *                                                                            *
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.                     *
9  *                                                                            *
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.                               *
14  *                                                                            *
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  ******************************************************************************/
19
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"
25
26 using namespace icinga;
27
28 REGISTER_CLICOMMAND("ca/restore", CARestoreCommand);
29
30 String CARestoreCommand::GetDescription() const
31 {
32         return "Restores a previously removed certificate request.";
33 }
34
35 String CARestoreCommand::GetShortDescription() const
36 {
37         return "restores a removed certificate request";
38 }
39
40 int CARestoreCommand::GetMinArguments() const
41 {
42         return 1;
43 }
44
45 ImpersonationLevel CARestoreCommand::GetImpersonationLevel() const
46 {
47         return ImpersonateIcinga;
48 }
49
50 /**
51  * The entry point for the "ca restore" CLI command.
52  *
53  * @returns An exit status.
54  */
55 int CARestoreCommand::Run(const boost::program_options::variables_map& vm, const std::vector<std::string>& ap) const
56 {
57         String requestFile = ApiListener::GetCertificateRequestsDir() + "/" + ap[0] + ".removed";
58
59         if (!Utility::PathExists(requestFile)) {
60                 Log(LogCritical, "cli")
61                         << "No removed request exists for fingerprint '" << ap[0] << "'.";
62                 return 1;
63         }
64         Utility::SaveJsonFile(ApiListener::GetCertificateRequestsDir() + "/" + ap[0] + ".json", 700, Utility::LoadJsonFile(requestFile));
65         if(remove(requestFile.CStr()) != 0)
66                 return 1;
67
68         Log(LogInformation, "cli")
69                 << "Certificate " << ap[0] << " restored, you can now sign it using:\n"
70                 << "\"icinga2 ca sign " << ap[0] << "\"";
71
72         return 0;
73 }