1 /******************************************************************************
3 * Copyright (C) 2012-2018 Icinga Development Team (https://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/casigncommand.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/sign", CASignCommand);
30 String CASignCommand::GetDescription() const
32 return "Signs an outstanding certificate request.";
35 String CASignCommand::GetShortDescription() const
37 return "signs an outstanding certificate request";
40 int CASignCommand::GetMinArguments() const
45 ImpersonationLevel CASignCommand::GetImpersonationLevel() const
47 return ImpersonateIcinga;
51 * The entry point for the "ca sign" CLI command.
53 * @returns An exit status.
55 int CASignCommand::Run(const boost::program_options::variables_map& vm, const std::vector<std::string>& ap) const
57 String requestFile = ApiListener::GetCertificateRequestsDir() + "/" + ap[0] + ".json";
59 if (!Utility::PathExists(requestFile)) {
60 Log(LogCritical, "cli")
61 << "No request exists for fingerprint '" << ap[0] << "'.";
65 Dictionary::Ptr request = Utility::LoadJsonFile(requestFile);
70 String certRequestText = request->Get("cert_request");
72 std::shared_ptr<X509> certRequest = StringToCertificate(certRequestText);
75 Log(LogCritical, "cli", "Certificate request is invalid. Could not parse X.509 certificate for the 'cert_request' attribute.");
79 std::shared_ptr<X509> certResponse = CreateCertIcingaCA(certRequest);
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);
86 length = BIO_get_mem_data(out, &data);
88 String subject = String(data, data + length);
92 Log(LogCritical, "cli")
93 << "Could not sign certificate for '" << subject << "'.";
97 request->Set("cert_response", CertificateToString(certResponse));
99 Utility::SaveJsonFile(requestFile, 0600, request);
101 Log(LogInformation, "cli")
102 << "Signed certificate for '" << subject << "'.";