2014-10-17 15:13:17 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* 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. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
2014-10-31 11:38:16 +01:00
|
|
|
#include "cli/nodesetupcommand.hpp"
|
|
|
|
#include "cli/nodeutility.hpp"
|
2014-10-21 18:24:35 +02:00
|
|
|
#include "cli/featureutility.hpp"
|
|
|
|
#include "cli/pkiutility.hpp"
|
2014-10-19 14:21:12 +02:00
|
|
|
#include "base/logger.hpp"
|
2014-10-21 18:24:35 +02:00
|
|
|
#include "base/console.hpp"
|
2014-10-17 15:13:17 +02:00
|
|
|
#include "base/application.hpp"
|
2014-10-23 15:05:12 +02:00
|
|
|
#include "base/tlsutility.hpp"
|
2014-10-31 10:35:03 +01:00
|
|
|
#include "base/scriptvariable.hpp"
|
2014-10-17 15:13:17 +02:00
|
|
|
#include <boost/foreach.hpp>
|
2014-10-21 21:33:21 +02:00
|
|
|
#include <boost/algorithm/string/classification.hpp>
|
2014-10-17 15:13:17 +02:00
|
|
|
#include <boost/algorithm/string/join.hpp>
|
|
|
|
#include <boost/algorithm/string/replace.hpp>
|
2014-10-21 21:33:21 +02:00
|
|
|
#include <boost/algorithm/string/split.hpp>
|
|
|
|
|
2014-10-18 21:06:28 +02:00
|
|
|
#include <iostream>
|
2014-10-17 15:13:17 +02:00
|
|
|
#include <fstream>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
namespace po = boost::program_options;
|
|
|
|
|
2014-10-31 11:38:16 +01:00
|
|
|
REGISTER_CLICOMMAND("node/setup", NodeSetupCommand);
|
2014-10-17 15:13:17 +02:00
|
|
|
|
2014-10-31 11:38:16 +01:00
|
|
|
String NodeSetupCommand::GetDescription(void) const
|
2014-10-17 15:13:17 +02:00
|
|
|
{
|
2014-10-31 11:53:44 +01:00
|
|
|
return "Sets up an Icinga 2 node.";
|
2014-10-17 15:13:17 +02:00
|
|
|
}
|
|
|
|
|
2014-10-31 11:38:16 +01:00
|
|
|
String NodeSetupCommand::GetShortDescription(void) const
|
2014-10-17 15:13:17 +02:00
|
|
|
{
|
2014-10-31 11:53:44 +01:00
|
|
|
return "set up node";
|
2014-10-17 15:13:17 +02:00
|
|
|
}
|
|
|
|
|
2014-10-31 11:38:16 +01:00
|
|
|
void NodeSetupCommand::InitParameters(boost::program_options::options_description& visibleDesc,
|
2014-10-17 15:13:17 +02:00
|
|
|
boost::program_options::options_description& hiddenDesc) const
|
|
|
|
{
|
|
|
|
visibleDesc.add_options()
|
2014-10-18 21:06:28 +02:00
|
|
|
("zone", po::value<std::string>(), "The name of the local zone")
|
2014-10-17 15:13:17 +02:00
|
|
|
("master_zone", po::value<std::string>(), "The name of the master zone")
|
2014-10-21 21:33:21 +02:00
|
|
|
("master_host", po::value<std::string>(), "The name of the master host for auto-signing the csr")
|
2014-10-23 04:38:54 +02:00
|
|
|
("endpoint", po::value<std::vector<std::string> >(), "Connect to remote endpoint; syntax: cn[,host,port]")
|
2014-10-18 21:06:28 +02:00
|
|
|
("listen", po::value<std::string>(), "Listen on host,port")
|
|
|
|
("ticket", po::value<std::string>(), "Generated ticket number for this request")
|
|
|
|
("trustedcert", po::value<std::string>(), "Trusted master certificate file")
|
|
|
|
("cn", po::value<std::string>(), "The certificate's common name")
|
2014-10-22 09:34:09 +02:00
|
|
|
("master", "Use setup for a master instance");
|
2014-10-17 15:13:17 +02:00
|
|
|
}
|
|
|
|
|
2014-10-31 11:38:16 +01:00
|
|
|
std::vector<String> NodeSetupCommand::GetArgumentSuggestions(const String& argument, const String& word) const
|
2014-10-21 18:24:35 +02:00
|
|
|
{
|
2014-10-22 15:36:39 +02:00
|
|
|
if (argument == "key" || argument == "cert" || argument == "trustedcert")
|
2014-10-21 18:24:35 +02:00
|
|
|
return GetBashCompletionSuggestions("file", word);
|
|
|
|
else if (argument == "host")
|
|
|
|
return GetBashCompletionSuggestions("hostname", word);
|
|
|
|
else if (argument == "port")
|
|
|
|
return GetBashCompletionSuggestions("service", word);
|
|
|
|
else
|
|
|
|
return CLICommand::GetArgumentSuggestions(argument, word);
|
|
|
|
}
|
|
|
|
|
2014-10-31 11:38:16 +01:00
|
|
|
ImpersonationLevel NodeSetupCommand::GetImpersonationLevel(void) const
|
2014-10-24 15:29:46 +02:00
|
|
|
{
|
|
|
|
return ImpersonateRoot;
|
|
|
|
}
|
|
|
|
|
2014-10-17 15:13:17 +02:00
|
|
|
/**
|
2014-10-31 11:53:44 +01:00
|
|
|
* The entry point for the "node setup" CLI command.
|
2014-10-17 15:13:17 +02:00
|
|
|
*
|
|
|
|
* @returns An exit status.
|
|
|
|
*/
|
2014-10-31 11:38:16 +01:00
|
|
|
int NodeSetupCommand::Run(const boost::program_options::variables_map& vm, const std::vector<std::string>& ap) const
|
2014-10-17 15:13:17 +02:00
|
|
|
{
|
|
|
|
if (!ap.empty()) {
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogWarning, "cli")
|
|
|
|
<< "Ignoring parameters: " << boost::algorithm::join(ap, " ");
|
2014-10-17 15:13:17 +02:00
|
|
|
}
|
|
|
|
|
2014-10-23 04:23:22 +02:00
|
|
|
if (vm.count("master"))
|
2014-10-21 18:24:35 +02:00
|
|
|
return SetupMaster(vm, ap);
|
2014-10-23 04:23:22 +02:00
|
|
|
else
|
2014-10-31 11:38:16 +01:00
|
|
|
return SetupNode(vm, ap);
|
2014-10-17 15:13:17 +02:00
|
|
|
}
|
2014-10-18 21:06:28 +02:00
|
|
|
|
2014-10-31 11:38:16 +01:00
|
|
|
int NodeSetupCommand::SetupMaster(const boost::program_options::variables_map& vm, const std::vector<std::string>& ap)
|
2014-10-18 21:06:28 +02:00
|
|
|
{
|
2014-10-23 03:35:01 +02:00
|
|
|
/* Ignore not required parameters */
|
2014-10-22 19:27:21 +02:00
|
|
|
if (vm.count("ticket"))
|
2014-10-31 11:38:16 +01:00
|
|
|
Log(LogWarning, "cli", "Master for Node setup: Ignoring --ticket");
|
2014-10-23 03:35:01 +02:00
|
|
|
|
2014-10-22 19:27:21 +02:00
|
|
|
if (vm.count("endpoint"))
|
2014-10-31 11:38:16 +01:00
|
|
|
Log(LogWarning, "cli", "Master for Node setup: Ignoring --endpoint");
|
2014-10-23 03:35:01 +02:00
|
|
|
|
2014-10-22 19:27:21 +02:00
|
|
|
if (vm.count("trustedcert"))
|
2014-10-31 11:38:16 +01:00
|
|
|
Log(LogWarning, "cli", "Master for Node setup: Ignoring --trustedcert");
|
2014-10-22 19:27:21 +02:00
|
|
|
|
2014-10-23 03:35:01 +02:00
|
|
|
/* Generate a new CA, if not already existing */
|
2014-10-21 21:33:21 +02:00
|
|
|
|
2014-10-23 03:35:01 +02:00
|
|
|
Log(LogInformation, "cli", "Generating new CA.");
|
2014-10-21 18:24:35 +02:00
|
|
|
|
|
|
|
if (PkiUtility::NewCa() > 0) {
|
2014-10-22 19:27:21 +02:00
|
|
|
Log(LogWarning, "cli", "Found CA, skipping and using the existing one.");
|
2014-10-21 16:04:20 +02:00
|
|
|
}
|
|
|
|
|
2014-10-23 03:35:01 +02:00
|
|
|
/* Generate a self signed certificate */
|
2014-10-21 21:33:21 +02:00
|
|
|
|
2014-10-22 08:06:06 +02:00
|
|
|
Log(LogInformation, "cli", "Generating new self-signed certificate.");
|
2014-10-21 18:24:35 +02:00
|
|
|
|
2014-10-23 03:35:01 +02:00
|
|
|
String pki_path = PkiUtility::GetPkiPath();
|
2014-10-21 18:24:35 +02:00
|
|
|
|
2014-10-23 03:35:01 +02:00
|
|
|
if (!Utility::MkDirP(pki_path, 0700)) {
|
2014-10-21 18:24:35 +02:00
|
|
|
Log(LogCritical, "cli")
|
2014-10-23 03:35:01 +02:00
|
|
|
<< "Could not create local pki directory '" << pki_path << "'.";
|
2014-10-21 18:24:35 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-10-31 10:35:03 +01:00
|
|
|
String user = ScriptVariable::Get("RunAsUser");
|
|
|
|
String group = ScriptVariable::Get("RunAsGroup");
|
2014-10-30 20:34:33 +01:00
|
|
|
|
|
|
|
if (!Utility::SetFileOwnership(pki_path, user, group)) {
|
|
|
|
Log(LogWarning, "cli")
|
|
|
|
<< "Cannot set ownership for user '" << user << "' group '" << group << "' on file '" << pki_path << "'. Verify it yourself!";
|
|
|
|
}
|
|
|
|
|
2014-10-21 18:24:35 +02:00
|
|
|
String cn = Utility::GetFQDN();
|
|
|
|
|
|
|
|
if (vm.count("cn"))
|
|
|
|
cn = vm["cn"].as<std::string>();
|
|
|
|
|
2014-10-23 03:35:01 +02:00
|
|
|
String key = pki_path + "/" + cn + ".key";
|
|
|
|
String csr = pki_path + "/" + cn + ".csr";
|
2014-10-21 18:24:35 +02:00
|
|
|
|
2014-10-23 03:35:01 +02:00
|
|
|
if (PkiUtility::NewCert(cn, key, csr, "") > 0) {
|
2014-10-22 08:06:06 +02:00
|
|
|
Log(LogCritical, "cli", "Failed to create self-signed certificate");
|
2014-10-23 03:35:01 +02:00
|
|
|
return 1;
|
2014-10-21 18:24:35 +02:00
|
|
|
}
|
2014-10-21 16:04:20 +02:00
|
|
|
|
2014-10-23 03:35:01 +02:00
|
|
|
/* Sign the CSR with the CA key */
|
2014-10-21 21:33:21 +02:00
|
|
|
|
2014-10-23 03:35:01 +02:00
|
|
|
String cert = pki_path + "/" + cn + ".crt";
|
|
|
|
|
|
|
|
if (PkiUtility::SignCsr(csr, cert) != 0) {
|
|
|
|
Log(LogCritical, "cli", "Could not sign CSR.");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy CA certificate to /etc/icinga2/pki */
|
2014-10-30 20:34:33 +01:00
|
|
|
String ca_path = PkiUtility::GetLocalCaPath();
|
|
|
|
String ca = ca_path + "/ca.crt";
|
|
|
|
String ca_key = ca_path + "/ca.key";
|
2014-11-02 19:38:35 +01:00
|
|
|
String serial = ca_path + "/serial.txt";
|
2014-10-30 15:19:09 +01:00
|
|
|
String target_ca = pki_path + "/ca.crt";
|
2014-10-21 18:24:35 +02:00
|
|
|
|
|
|
|
Log(LogInformation, "cli")
|
2014-10-30 15:19:09 +01:00
|
|
|
<< "Copying CA certificate to '" << target_ca << "'.";
|
2014-10-21 18:24:35 +02:00
|
|
|
|
2014-10-22 19:27:21 +02:00
|
|
|
/* does not overwrite existing files! */
|
|
|
|
Utility::CopyFile(ca, target_ca);
|
2014-10-21 16:04:20 +02:00
|
|
|
|
2014-10-30 20:34:33 +01:00
|
|
|
/* fix permissions: root -> icinga daemon user */
|
|
|
|
if (!Utility::SetFileOwnership(ca_path, user, group)) {
|
|
|
|
Log(LogWarning, "cli")
|
|
|
|
<< "Cannot set ownership for user '" << user << "' group '" << group << "' on file '" << ca_path << "'. Verify it yourself!";
|
|
|
|
}
|
|
|
|
if (!Utility::SetFileOwnership(ca, user, group)) {
|
|
|
|
Log(LogWarning, "cli")
|
|
|
|
<< "Cannot set ownership for user '" << user << "' group '" << group << "' on file '" << ca << "'. Verify it yourself!";
|
|
|
|
}
|
|
|
|
if (!Utility::SetFileOwnership(ca_key, user, group)) {
|
|
|
|
Log(LogWarning, "cli")
|
|
|
|
<< "Cannot set ownership for user '" << user << "' group '" << group << "' on file '" << ca_key << "'. Verify it yourself!";
|
|
|
|
}
|
2014-11-02 19:38:35 +01:00
|
|
|
if (!Utility::SetFileOwnership(serial, user, group)) {
|
|
|
|
Log(LogWarning, "cli")
|
|
|
|
<< "Cannot set ownership for user '" << user << "' group '" << group << "' on file '" << serial << "'. Verify it yourself!";
|
|
|
|
}
|
2014-10-30 20:34:33 +01:00
|
|
|
if (!Utility::SetFileOwnership(target_ca, user, group)) {
|
|
|
|
Log(LogWarning, "cli")
|
|
|
|
<< "Cannot set ownership for user '" << user << "' group '" << group << "' on file '" << target_ca << "'. Verify it yourself!";
|
|
|
|
}
|
|
|
|
if (!Utility::SetFileOwnership(key, user, group)) {
|
|
|
|
Log(LogWarning, "cli")
|
|
|
|
<< "Cannot set ownership for user '" << user << "' group '" << group << "' on file '" << key << "'. Verify it yourself!";
|
|
|
|
}
|
|
|
|
if (!Utility::SetFileOwnership(csr, user, group)) {
|
|
|
|
Log(LogWarning, "cli")
|
|
|
|
<< "Cannot set ownership for user '" << user << "' group '" << group << "' on file '" << csr << "'. Verify it yourself!";
|
|
|
|
}
|
2014-10-30 16:19:51 +01:00
|
|
|
|
2014-10-23 03:35:01 +02:00
|
|
|
/* read zones.conf and update with zone + endpoint information */
|
2014-10-21 21:33:21 +02:00
|
|
|
|
2014-10-22 08:06:06 +02:00
|
|
|
Log(LogInformation, "cli", "Generating zone and object configuration.");
|
2014-10-21 18:24:35 +02:00
|
|
|
|
2014-10-31 11:38:16 +01:00
|
|
|
NodeUtility::GenerateNodeMasterIcingaConfig(cn);
|
2014-10-21 16:04:20 +02:00
|
|
|
|
2014-10-28 21:13:15 +01:00
|
|
|
/* enable the ApiListener config */
|
2014-10-21 21:33:21 +02:00
|
|
|
|
2014-10-28 21:13:15 +01:00
|
|
|
Log(LogInformation, "cli", "Updating the APIListener feature.");
|
2014-10-21 16:04:20 +02:00
|
|
|
|
2014-10-21 21:33:21 +02:00
|
|
|
std::vector<std::string> enable;
|
|
|
|
enable.push_back("api");
|
|
|
|
FeatureUtility::EnableFeatures(enable);
|
|
|
|
|
2014-10-28 21:13:15 +01:00
|
|
|
String apipath = FeatureUtility::GetFeaturesAvailablePath() + "/api.conf";
|
2014-10-31 11:38:16 +01:00
|
|
|
NodeUtility::CreateBackupFile(apipath);
|
2014-10-28 21:13:15 +01:00
|
|
|
|
|
|
|
String apipathtmp = apipath + ".tmp";
|
|
|
|
|
|
|
|
std::ofstream fp;
|
|
|
|
fp.open(apipathtmp.CStr(), std::ofstream::out | std::ofstream::trunc);
|
|
|
|
|
|
|
|
fp << "/**\n"
|
|
|
|
<< " * The API listener is used for distributed monitoring setups.\n"
|
|
|
|
<< " */\n"
|
|
|
|
<< "object ApiListener \"api\" {\n"
|
|
|
|
<< " cert_path = SysconfDir + \"/icinga2/pki/\" + NodeName + \".crt\"\n"
|
|
|
|
<< " key_path = SysconfDir + \"/icinga2/pki/\" + NodeName + \".key\"\n"
|
|
|
|
<< " ca_path = SysconfDir + \"/icinga2/pki/ca.crt\"\n";
|
|
|
|
|
|
|
|
if (vm.count("listen")) {
|
|
|
|
std::vector<String> tokens;
|
|
|
|
boost::algorithm::split(tokens, vm["listen"].as<std::string>(), boost::is_any_of(","));
|
|
|
|
|
|
|
|
if (tokens.size() > 0)
|
|
|
|
fp << " bind_host = \"" << tokens[0] << "\"\n";
|
|
|
|
if (tokens.size() > 1)
|
|
|
|
fp << " bind_port = " << tokens[1] << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
fp << "\n"
|
|
|
|
<< " ticket_salt = TicketSalt\n"
|
|
|
|
<< "}\n";
|
|
|
|
|
|
|
|
fp.close();
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
_unlink(apipath.CStr());
|
|
|
|
#endif /* _WIN32 */
|
|
|
|
|
|
|
|
if (rename(apipathtmp.CStr(), apipath.CStr()) < 0) {
|
|
|
|
BOOST_THROW_EXCEPTION(posix_error()
|
|
|
|
<< boost::errinfo_api_function("rename")
|
|
|
|
<< boost::errinfo_errno(errno)
|
|
|
|
<< boost::errinfo_file_name(apipathtmp));
|
|
|
|
}
|
2014-10-22 19:27:21 +02:00
|
|
|
|
2014-10-23 03:35:01 +02:00
|
|
|
/* update constants.conf with NodeName = CN + TicketSalt = random value */
|
|
|
|
if (cn != Utility::GetFQDN()) {
|
|
|
|
Log(LogWarning, "cli")
|
|
|
|
<< "CN '" << cn << "' does not match the default FQDN '" << Utility::GetFQDN() << "'. Requires update for NodeName constant in constants.conf!";
|
|
|
|
}
|
|
|
|
|
2014-10-23 15:05:12 +02:00
|
|
|
Log(LogInformation, "cli", "Updating constants.conf.");
|
|
|
|
|
2014-10-31 11:38:16 +01:00
|
|
|
NodeUtility::CreateBackupFile(Application::GetSysconfDir() + "/icinga2/constants.conf");
|
2014-10-23 15:05:12 +02:00
|
|
|
|
2014-10-31 11:38:16 +01:00
|
|
|
NodeUtility::UpdateConstant("NodeName", cn);
|
2014-10-23 15:05:12 +02:00
|
|
|
|
|
|
|
String salt = RandomString(16);
|
|
|
|
|
2014-10-31 11:38:16 +01:00
|
|
|
NodeUtility::UpdateConstant("TicketSalt", salt);
|
2014-10-21 16:04:20 +02:00
|
|
|
|
2014-10-21 18:24:35 +02:00
|
|
|
Log(LogInformation, "cli")
|
2014-10-28 21:13:15 +01:00
|
|
|
<< "Edit the api feature config file '" << apipath << "' and set a secure 'ticket_salt' attribute.";
|
2014-10-21 18:24:35 +02:00
|
|
|
|
2014-10-23 03:35:01 +02:00
|
|
|
/* tell the user to reload icinga2 */
|
2014-10-21 21:33:21 +02:00
|
|
|
|
2014-10-22 08:06:06 +02:00
|
|
|
Log(LogInformation, "cli", "Make sure to restart Icinga 2.");
|
2014-10-21 18:24:35 +02:00
|
|
|
|
|
|
|
return 0;
|
2014-10-18 21:06:28 +02:00
|
|
|
}
|
|
|
|
|
2014-10-31 11:38:16 +01:00
|
|
|
int NodeSetupCommand::SetupNode(const boost::program_options::variables_map& vm, const std::vector<std::string>& ap)
|
2014-10-18 21:06:28 +02:00
|
|
|
{
|
2014-10-23 04:23:22 +02:00
|
|
|
/* require ticket number (generated on master) and at least one endpoint */
|
2014-10-21 21:33:21 +02:00
|
|
|
|
2014-10-21 18:24:35 +02:00
|
|
|
if (!vm.count("ticket")) {
|
|
|
|
Log(LogCritical, "cli")
|
|
|
|
<< "Please pass the ticket number generated on master\n"
|
2014-10-31 22:21:36 +01:00
|
|
|
<< "(Hint: 'icinga2 pki ticket --cn " << Utility::GetFQDN() << "').";
|
2014-10-21 18:24:35 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2014-10-23 04:23:22 +02:00
|
|
|
|
|
|
|
if (!vm.count("endpoint")) {
|
|
|
|
Log(LogCritical, "cli", "You need to specify at least one endpoint (--endpoint).");
|
|
|
|
return 1;
|
|
|
|
}
|
2014-10-21 18:24:35 +02:00
|
|
|
|
2014-10-31 22:21:36 +01:00
|
|
|
if (!vm.count("zone")) {
|
|
|
|
Log(LogCritical, "cli", "You need to specify the local zone (--zone).");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-10-21 18:24:35 +02:00
|
|
|
String ticket = vm["ticket"].as<std::string>();
|
|
|
|
|
|
|
|
Log(LogInformation, "cli")
|
|
|
|
<< "Verifying ticket '" << ticket << "'.";
|
2014-10-21 16:04:20 +02:00
|
|
|
|
2014-10-23 03:35:01 +02:00
|
|
|
/* require master host information for auto-signing requests */
|
2014-10-21 21:33:21 +02:00
|
|
|
|
|
|
|
if (!vm.count("master_host")) {
|
2014-10-23 15:05:12 +02:00
|
|
|
Log(LogCritical, "cli", "Please pass the master host connection information for auto-signing using '--master_host <host>'");
|
2014-10-21 21:33:21 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<String> tokens;
|
|
|
|
boost::algorithm::split(tokens, vm["master_host"].as<std::string>(), boost::is_any_of(","));
|
|
|
|
String master_host;
|
|
|
|
String master_port = "5665";
|
|
|
|
|
|
|
|
if (tokens.size() == 1 || tokens.size() == 2)
|
|
|
|
master_host = tokens[0];
|
|
|
|
|
|
|
|
if (tokens.size() == 2)
|
|
|
|
master_port = tokens[1];
|
|
|
|
|
|
|
|
Log(LogInformation, "cli")
|
|
|
|
<< "Verifying master host connection information: host '" << master_host << "', port '" << master_port << "'.";
|
|
|
|
|
2014-10-23 03:35:01 +02:00
|
|
|
/* trusted cert must be passed (retrieved by the user with 'pki save-cert' before) */
|
2014-10-21 21:33:21 +02:00
|
|
|
|
2014-10-21 18:24:35 +02:00
|
|
|
if (!vm.count("trustedcert")) {
|
|
|
|
Log(LogCritical, "cli")
|
|
|
|
<< "Please pass the trusted cert retrieved from the master\n"
|
2014-10-22 15:36:39 +02:00
|
|
|
<< "(Hint: 'icinga2 pki save-cert --host <masterhost> --port <5665> --key local.key --cert local.crt --trustedcert master.crt').";
|
2014-10-21 18:24:35 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
String trustedcert = vm["trustedcert"].as<std::string>();
|
|
|
|
|
|
|
|
Log(LogInformation, "cli")
|
|
|
|
<< "Verifying trusted certificate from file '" << trustedcert << "'.";
|
|
|
|
|
2014-10-23 03:35:01 +02:00
|
|
|
/* retrieve CN and pass it (defaults to FQDN) */
|
2014-10-22 15:29:54 +02:00
|
|
|
|
2014-10-21 18:24:35 +02:00
|
|
|
String cn = Utility::GetFQDN();
|
|
|
|
|
|
|
|
if (vm.count("cn"))
|
|
|
|
cn = vm["cn"].as<std::string>();
|
2014-10-21 16:04:20 +02:00
|
|
|
|
2014-10-21 18:24:35 +02:00
|
|
|
Log(LogInformation, "cli")
|
2014-10-21 21:33:21 +02:00
|
|
|
<< "Using the following CN (defaults to FQDN): '" << cn << "'.";
|
2014-10-21 18:24:35 +02:00
|
|
|
|
2014-10-23 03:35:01 +02:00
|
|
|
/* pki request a signed certificate from the master */
|
2014-10-21 21:33:21 +02:00
|
|
|
|
2014-10-22 21:49:41 +02:00
|
|
|
String pki_path = PkiUtility::GetPkiPath();
|
2014-10-21 21:33:21 +02:00
|
|
|
|
2014-10-22 21:49:41 +02:00
|
|
|
String key = pki_path + "/" + cn + ".key";
|
|
|
|
String cert = pki_path + "/" + cn + ".crt";
|
|
|
|
String ca = pki_path + "/ca.crt";
|
2014-10-22 15:29:54 +02:00
|
|
|
|
2014-10-30 20:34:33 +01:00
|
|
|
if (!Utility::MkDirP(pki_path, 0700)) {
|
|
|
|
Log(LogCritical, "cli")
|
|
|
|
<< "Could not create local pki directory '" << pki_path << "'.";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-10-31 10:35:03 +01:00
|
|
|
String user = ScriptVariable::Get("RunAsUser");
|
|
|
|
String group = ScriptVariable::Get("RunAsGroup");
|
2014-10-30 20:34:33 +01:00
|
|
|
|
|
|
|
if (!Utility::SetFileOwnership(pki_path, user, group)) {
|
|
|
|
Log(LogWarning, "cli")
|
|
|
|
<< "Cannot set ownership for user '" << user << "' group '" << group << "' on file '" << pki_path << "'. Verify it yourself!";
|
|
|
|
}
|
|
|
|
|
2014-10-22 21:49:41 +02:00
|
|
|
if (PkiUtility::NewCert(cn, key, String(), cert) != 0) {
|
|
|
|
Log(LogCritical, "cli", "Failed to generate new self-signed certificate.");
|
2014-10-22 15:29:54 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2014-10-21 18:24:35 +02:00
|
|
|
|
2014-10-30 20:34:33 +01:00
|
|
|
/* fix permissions: root -> icinga daemon user */
|
|
|
|
if (!Utility::SetFileOwnership(ca, user, group)) {
|
|
|
|
Log(LogWarning, "cli")
|
|
|
|
<< "Cannot set ownership for user '" << user << "' group '" << group << "' on file '" << ca << "'. Verify it yourself!";
|
|
|
|
}
|
|
|
|
if (!Utility::SetFileOwnership(cert, user, group)) {
|
|
|
|
Log(LogWarning, "cli")
|
|
|
|
<< "Cannot set ownership for user '" << user << "' group '" << group << "' on file '" << cert << "'. Verify it yourself!";
|
|
|
|
}
|
|
|
|
if (!Utility::SetFileOwnership(key, user, group)) {
|
|
|
|
Log(LogWarning, "cli")
|
|
|
|
<< "Cannot set ownership for user '" << user << "' group '" << group << "' on file '" << key << "'. Verify it yourself!";
|
|
|
|
}
|
|
|
|
|
2014-10-22 08:06:06 +02:00
|
|
|
Log(LogInformation, "cli", "Requesting a signed certificate from the master.");
|
2014-10-21 18:24:35 +02:00
|
|
|
|
2014-10-22 21:49:41 +02:00
|
|
|
if (PkiUtility::RequestCertificate(master_host, master_port, key, cert, ca, trustedcert, ticket) != 0) {
|
|
|
|
Log(LogCritical, "cli", "Failed to request certificate from Icinga 2 master.");
|
|
|
|
return 1;
|
|
|
|
}
|
2014-10-21 21:33:21 +02:00
|
|
|
|
2014-10-30 20:34:33 +01:00
|
|
|
/* fix permissions (again) when updating the signed certificate */
|
|
|
|
if (!Utility::SetFileOwnership(cert, user, group)) {
|
|
|
|
Log(LogWarning, "cli")
|
|
|
|
<< "Cannot set ownership for user '" << user << "' group '" << group << "' on file '" << cert << "'. Verify it yourself!";
|
|
|
|
}
|
|
|
|
|
2014-11-02 20:17:57 +01:00
|
|
|
/* disable the notifications feature */
|
|
|
|
Log(LogInformation, "cli", "Disabling the Notification feature.");
|
|
|
|
|
|
|
|
std::vector<std::string> disable;
|
|
|
|
disable.push_back("notification");
|
|
|
|
FeatureUtility::DisableFeatures(disable);
|
|
|
|
|
2014-10-28 21:13:15 +01:00
|
|
|
/* enable the ApiListener config */
|
2014-10-21 16:04:20 +02:00
|
|
|
|
2014-11-02 20:17:57 +01:00
|
|
|
Log(LogInformation, "cli", "Updating the ApiListener feature.");
|
2014-10-21 18:24:35 +02:00
|
|
|
|
2014-10-21 21:33:21 +02:00
|
|
|
std::vector<std::string> enable;
|
|
|
|
enable.push_back("api");
|
|
|
|
FeatureUtility::EnableFeatures(enable);
|
|
|
|
|
2014-10-28 21:13:15 +01:00
|
|
|
String apipath = FeatureUtility::GetFeaturesAvailablePath() + "/api.conf";
|
2014-10-31 11:38:16 +01:00
|
|
|
NodeUtility::CreateBackupFile(apipath);
|
2014-10-28 21:13:15 +01:00
|
|
|
|
|
|
|
String apipathtmp = apipath + ".tmp";
|
2014-10-21 21:33:21 +02:00
|
|
|
|
2014-10-28 21:13:15 +01:00
|
|
|
std::ofstream fp;
|
|
|
|
fp.open(apipathtmp.CStr(), std::ofstream::out | std::ofstream::trunc);
|
|
|
|
|
|
|
|
fp << "/**\n"
|
|
|
|
<< " * The API listener is used for distributed monitoring setups.\n"
|
|
|
|
<< " */\n"
|
|
|
|
<< "object ApiListener \"api\" {\n"
|
|
|
|
<< " cert_path = SysconfDir + \"/icinga2/pki/\" + NodeName + \".crt\"\n"
|
|
|
|
<< " key_path = SysconfDir + \"/icinga2/pki/\" + NodeName + \".key\"\n"
|
|
|
|
<< " ca_path = SysconfDir + \"/icinga2/pki/ca.crt\"\n";
|
|
|
|
|
|
|
|
if (vm.count("listen")) {
|
|
|
|
std::vector<String> tokens;
|
|
|
|
boost::algorithm::split(tokens, vm["listen"].as<std::string>(), boost::is_any_of(","));
|
|
|
|
|
|
|
|
if (tokens.size() > 0)
|
|
|
|
fp << " bind_host = \"" << tokens[0] << "\"\n";
|
|
|
|
if (tokens.size() > 1)
|
|
|
|
fp << " bind_port = " << tokens[1] << "\n";
|
|
|
|
}
|
2014-10-21 21:33:21 +02:00
|
|
|
|
2014-10-28 21:13:15 +01:00
|
|
|
fp << "\n"
|
|
|
|
<< " ticket_salt = TicketSalt\n"
|
|
|
|
<< "}\n";
|
2014-10-21 21:33:21 +02:00
|
|
|
|
2014-10-28 21:13:15 +01:00
|
|
|
fp.close();
|
2014-10-21 21:33:21 +02:00
|
|
|
|
2014-10-28 21:13:15 +01:00
|
|
|
#ifdef _WIN32
|
|
|
|
_unlink(apipath.CStr());
|
|
|
|
#endif /* _WIN32 */
|
2014-10-21 21:33:21 +02:00
|
|
|
|
2014-10-28 21:13:15 +01:00
|
|
|
if (rename(apipathtmp.CStr(), apipath.CStr()) < 0) {
|
|
|
|
BOOST_THROW_EXCEPTION(posix_error()
|
|
|
|
<< boost::errinfo_api_function("rename")
|
|
|
|
<< boost::errinfo_errno(errno)
|
|
|
|
<< boost::errinfo_file_name(apipathtmp));
|
|
|
|
}
|
2014-10-22 19:27:21 +02:00
|
|
|
|
2014-10-23 03:35:01 +02:00
|
|
|
/* generate local zones.conf with zone+endpoint */
|
2014-10-21 16:04:20 +02:00
|
|
|
|
2014-10-22 08:06:06 +02:00
|
|
|
Log(LogInformation, "cli", "Generating zone and object configuration.");
|
2014-10-21 18:24:35 +02:00
|
|
|
|
2014-11-18 16:48:58 +01:00
|
|
|
NodeUtility::GenerateNodeIcingaConfig(vm["endpoint"].as<std::vector<std::string> >(), cn, vm["zone"].as<std::string>());
|
2014-10-21 21:33:21 +02:00
|
|
|
|
2014-10-23 03:35:01 +02:00
|
|
|
/* update constants.conf with NodeName = CN */
|
2014-10-22 15:29:54 +02:00
|
|
|
if (cn != Utility::GetFQDN()) {
|
|
|
|
Log(LogWarning, "cli")
|
|
|
|
<< "CN '" << cn << "' does not match the default FQDN '" << Utility::GetFQDN() << "'. Requires update for NodeName constant in constants.conf!";
|
|
|
|
}
|
2014-10-21 21:33:21 +02:00
|
|
|
|
2014-10-23 15:05:12 +02:00
|
|
|
Log(LogInformation, "cli", "Updating constants.conf.");
|
|
|
|
|
2014-10-31 11:38:16 +01:00
|
|
|
NodeUtility::CreateBackupFile(Application::GetSysconfDir() + "/icinga2/constants.conf");
|
2014-10-23 15:05:12 +02:00
|
|
|
|
2014-10-31 11:38:16 +01:00
|
|
|
NodeUtility::UpdateConstant("NodeName", cn);
|
2014-10-21 21:33:21 +02:00
|
|
|
|
2014-10-23 03:35:01 +02:00
|
|
|
/* tell the user to reload icinga2 */
|
2014-10-21 16:04:20 +02:00
|
|
|
|
2014-10-22 08:06:06 +02:00
|
|
|
Log(LogInformation, "cli", "Make sure to restart Icinga 2.");
|
2014-10-21 16:04:20 +02:00
|
|
|
|
2014-10-21 18:24:35 +02:00
|
|
|
return 0;
|
2014-10-18 21:06:28 +02:00
|
|
|
}
|