From 5549472962657834243f0fd4354c519e42f049a2 Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Thu, 16 Oct 2014 12:30:50 +0200 Subject: [PATCH] Add missing files refs #7247 --- lib/cli/pkirequestcommand.cpp | 166 ++++++++++++++++++++++++++++++++++ lib/cli/pkirequestcommand.hpp | 50 ++++++++++ lib/cli/pkiticketcommand.cpp | 74 +++++++++++++++ lib/cli/pkiticketcommand.hpp | 50 ++++++++++ 4 files changed, 340 insertions(+) create mode 100644 lib/cli/pkirequestcommand.cpp create mode 100644 lib/cli/pkirequestcommand.hpp create mode 100644 lib/cli/pkiticketcommand.cpp create mode 100644 lib/cli/pkiticketcommand.hpp diff --git a/lib/cli/pkirequestcommand.cpp b/lib/cli/pkirequestcommand.cpp new file mode 100644 index 000000000..e8d9ce81f --- /dev/null +++ b/lib/cli/pkirequestcommand.cpp @@ -0,0 +1,166 @@ +/****************************************************************************** + * Icinga 2 * + * Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org) * + * * + * This program is free software; you can redistribute it and/or * + * modify it under the terms of the GNU General Public License * + * as published by the Free Software Foundation; either version 2 * + * of the License, or (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the Free Software Foundation * + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. * + ******************************************************************************/ + +#include "cli/pkirequestcommand.hpp" +#include "remote/jsonrpc.hpp" +#include "base/logger_fwd.hpp" +#include "base/clicommand.hpp" +#include "base/tlsutility.hpp" +#include "base/tlsstream.hpp" +#include "base/tcpsocket.hpp" +#include "base/utility.hpp" +#include "base/application.hpp" +#include + +using namespace icinga; +namespace po = boost::program_options; + +REGISTER_CLICOMMAND("pki/request", PKIRequestCommand); + +String PKIRequestCommand::GetDescription(void) const +{ + return "Sends a PKI request to Icinga 2."; +} + +String PKIRequestCommand::GetShortDescription(void) const +{ + return "requests a certificate"; +} + +void PKIRequestCommand::InitParameters(boost::program_options::options_description& visibleDesc, + boost::program_options::options_description& hiddenDesc, + ArgumentCompletionDescription& argCompletionDesc) const +{ + visibleDesc.add_options() + ("keyfile", po::value(), "Key file path") + ("certfile", po::value(), "Certificate file path (input + output)") + ("cafile", po::value(), "CA file path (output)") + ("host", po::value(), "Icinga 2 host") + ("port", po::value(), "Icinga 2 port") + ("ticket", po::value(), "Icinga 2 PKI ticket"); +} + +/** + * The entry point for the "pki request" CLI command. + * + * @returns An exit status. + */ +int PKIRequestCommand::Run(const boost::program_options::variables_map& vm, const std::vector& ap) const +{ + if (!vm.count("host")) { + Log(LogCritical, "cli", "Icinga 2 host (--host) must be specified."); + return 1; + } + + if (!vm.count("keyfile")) { + Log(LogCritical, "cli", "Key file path (--keyfile) must be specified."); + return 1; + } + + if (!vm.count("certfile")) { + Log(LogCritical, "cli", "Certificate file path (--certfile) must be specified."); + return 1; + } + + if (!vm.count("cafile")) { + Log(LogCritical, "cli", "CA certificate file path (--cafile) must be specified."); + return 1; + } + + if (!vm.count("ticket")) { + Log(LogCritical, "cli", "Ticket (--ticket) must be specified."); + return 1; + } + + TcpSocket::Ptr client = make_shared(); + + String port = "5665"; + + if (vm.count("port")) + port = vm["port"].as(); + + client->Connect(vm["host"].as(), port); + + String certfile = vm["certfile"].as(); + + shared_ptr sslContext = MakeSSLContext(certfile, vm["keyfile"].as()); + + TlsStream::Ptr stream = make_shared(client, RoleClient, sslContext); + + stream->Handshake(); + + Dictionary::Ptr request = make_shared(); + + String msgid = Utility::NewUniqueID(); + + request->Set("jsonrpc", "2.0"); + request->Set("id", msgid); + request->Set("method", "pki::RequestCertificate"); + + Dictionary::Ptr params = make_shared(); + params->Set("ticket", String(vm["ticket"].as())); + + request->Set("params", params); + + JsonRpc::SendMessage(stream, request); + + Dictionary::Ptr response; + + for (;;) { + response = JsonRpc::ReadMessage(stream); + + if (response->Get("id") != msgid) + continue; + + break; + } + + Dictionary::Ptr result = response->Get("result"); + + if (result->Contains("error")) { + Log(LogCritical, "cli", result->Get("error")); + return 1; + } + + String cafile = vm["cafile"].as(); + + std::ofstream fpcert; + fpcert.open(certfile.CStr()); + + if (!fpcert) { + Log(LogCritical, "cli", "Could not open certificate file '" + certfile + "' for writing."); + return 1; + } + + fpcert << result->Get("cert"); + fpcert.close(); + + std::ofstream fpca; + fpca.open(cafile.CStr()); + + if (!fpcert) { + Log(LogCritical, "cli", "Could not open CA certificate file '" + cafile + "' for writing."); + return 1; + } + + fpca << result->Get("ca"); + fpca.close(); + + return 0; +} diff --git a/lib/cli/pkirequestcommand.hpp b/lib/cli/pkirequestcommand.hpp new file mode 100644 index 000000000..357ded707 --- /dev/null +++ b/lib/cli/pkirequestcommand.hpp @@ -0,0 +1,50 @@ +/****************************************************************************** + * Icinga 2 * + * Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org) * + * * + * This program is free software; you can redistribute it and/or * + * modify it under the terms of the GNU General Public License * + * as published by the Free Software Foundation; either version 2 * + * of the License, or (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the Free Software Foundation * + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. * + ******************************************************************************/ + +#ifndef PKIREQUESTCOMMAND_H +#define PKIREQUESTCOMMAND_H + +#include "base/qstring.hpp" +#include "base/clicommand.hpp" + +namespace icinga +{ + +/** + * The "pki request" command. + * + * @ingroup cli + */ +class PKIRequestCommand : public CLICommand +{ +public: + DECLARE_PTR_TYPEDEFS(PKIRequestCommand); + + virtual String GetDescription(void) const; + virtual String GetShortDescription(void) const; + virtual void InitParameters(boost::program_options::options_description& visibleDesc, + boost::program_options::options_description& hiddenDesc, + ArgumentCompletionDescription& argCompletionDesc) const; + virtual int Run(const boost::program_options::variables_map& vm, const std::vector& ap) const; + +}; + +} + +#endif /* PKIREQUESTCOMMAND_H */ diff --git a/lib/cli/pkiticketcommand.cpp b/lib/cli/pkiticketcommand.cpp new file mode 100644 index 000000000..e35938691 --- /dev/null +++ b/lib/cli/pkiticketcommand.cpp @@ -0,0 +1,74 @@ +/****************************************************************************** + * Icinga 2 * + * Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org) * + * * + * This program is free software; you can redistribute it and/or * + * modify it under the terms of the GNU General Public License * + * as published by the Free Software Foundation; either version 2 * + * of the License, or (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the Free Software Foundation * + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. * + ******************************************************************************/ + +#include "cli/pkiticketcommand.hpp" +#include "remote/jsonrpc.hpp" +#include "base/logger_fwd.hpp" +#include "base/clicommand.hpp" +#include "base/tlsutility.hpp" +#include "base/tlsstream.hpp" +#include "base/tcpsocket.hpp" +#include "base/utility.hpp" +#include "base/application.hpp" + +using namespace icinga; +namespace po = boost::program_options; + +REGISTER_CLICOMMAND("pki/ticket", PKITicketCommand); + +String PKITicketCommand::GetDescription(void) const +{ + return "Generates an Icinga 2 ticket"; +} + +String PKITicketCommand::GetShortDescription(void) const +{ + return "generates a ticket"; +} + +void PKITicketCommand::InitParameters(boost::program_options::options_description& visibleDesc, + boost::program_options::options_description& hiddenDesc, + ArgumentCompletionDescription& argCompletionDesc) const +{ + visibleDesc.add_options() + ("cn", po::value(), "Certificate common name") + ("salt", po::value(), "Ticket salt"); +} + +/** + * The entry point for the "pki ticket" CLI command. + * + * @returns An exit status. + */ +int PKITicketCommand::Run(const boost::program_options::variables_map& vm, const std::vector& ap) const +{ + if (!vm.count("cn")) { + Log(LogCritical, "cli", "Common name (--cn) must be specified."); + return 1; + } + + if (!vm.count("salt")) { + Log(LogCritical, "cli", "Ticket salt (--salt) must be specified."); + return 1; + } + + std::cout << PBKDF2_SHA512(vm["cn"].as(), vm["salt"].as(), 50000) << std::endl; + + return 0; +} diff --git a/lib/cli/pkiticketcommand.hpp b/lib/cli/pkiticketcommand.hpp new file mode 100644 index 000000000..ee25b5973 --- /dev/null +++ b/lib/cli/pkiticketcommand.hpp @@ -0,0 +1,50 @@ +/****************************************************************************** + * Icinga 2 * + * Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org) * + * * + * This program is free software; you can redistribute it and/or * + * modify it under the terms of the GNU General Public License * + * as published by the Free Software Foundation; either version 2 * + * of the License, or (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the Free Software Foundation * + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. * + ******************************************************************************/ + +#ifndef PKITICKETCOMMAND_H +#define PKITICKETCOMMAND_H + +#include "base/qstring.hpp" +#include "base/clicommand.hpp" + +namespace icinga +{ + +/** + * The "pki ticket" command. + * + * @ingroup cli + */ +class PKITicketCommand : public CLICommand +{ +public: + DECLARE_PTR_TYPEDEFS(PKITicketCommand); + + virtual String GetDescription(void) const; + virtual String GetShortDescription(void) const; + virtual void InitParameters(boost::program_options::options_description& visibleDesc, + boost::program_options::options_description& hiddenDesc, + ArgumentCompletionDescription& argCompletionDesc) const; + virtual int Run(const boost::program_options::variables_map& vm, const std::vector& ap) const; + +}; + +} + +#endif /* PKITICKETCOMMAND_H */