]> granicus.if.org Git - icinga2/blob - lib/cli/casigncommand.cpp
ApiListener#ApiTimerHandler(): delete all replayed logs
[icinga2] / lib / cli / casigncommand.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2018 Icinga Development Team (https://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/casigncommand.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/sign", CASignCommand);
29
30 String CASignCommand::GetDescription() const
31 {
32         return "Signs an outstanding certificate request.";
33 }
34
35 String CASignCommand::GetShortDescription() const
36 {
37         return "signs an outstanding certificate request";
38 }
39
40 int CASignCommand::GetMinArguments() const
41 {
42         return 1;
43 }
44
45 ImpersonationLevel CASignCommand::GetImpersonationLevel() const
46 {
47         return ImpersonateIcinga;
48 }
49
50 /**
51  * The entry point for the "ca sign" CLI command.
52  *
53  * @returns An exit status.
54  */
55 int CASignCommand::Run(const boost::program_options::variables_map& vm, const std::vector<std::string>& ap) const
56 {
57         String requestFile = ApiListener::GetCertificateRequestsDir() + "/" + ap[0] + ".json";
58
59         if (!Utility::PathExists(requestFile)) {
60                 Log(LogCritical, "cli")
61                         << "No request exists for fingerprint '" << ap[0] << "'.";
62                 return 1;
63         }
64
65         Dictionary::Ptr request = Utility::LoadJsonFile(requestFile);
66
67         if (!request)
68                 return 1;
69
70         String certRequestText = request->Get("cert_request");
71
72         std::shared_ptr<X509> certRequest = StringToCertificate(certRequestText);
73
74         if (!certRequest) {
75                 Log(LogCritical, "cli", "Certificate request is invalid. Could not parse X.509 certificate for the 'cert_request' attribute.");
76                 return 1;
77         }
78
79         std::shared_ptr<X509> certResponse = CreateCertIcingaCA(certRequest);
80
81         BIO *out = BIO_new(BIO_s_mem());
82         X509_NAME_print_ex(out, X509_get_subject_name(certRequest.get()), 0, XN_FLAG_ONELINE & ~ASN1_STRFLGS_ESC_MSB);
83
84         char *data;
85         long length;
86         length = BIO_get_mem_data(out, &data);
87
88         String subject = String(data, data + length);
89         BIO_free(out);
90
91         if (!certResponse) {
92                 Log(LogCritical, "cli")
93                         << "Could not sign certificate for '" << subject << "'.";
94                 return 1;
95         }
96
97         request->Set("cert_response", CertificateToString(certResponse));
98
99         Utility::SaveJsonFile(requestFile, 0600, request);
100
101         Log(LogInformation, "cli")
102                 << "Signed certificate for '" << subject << "'.";
103
104         return 0;
105 }