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/nodewizardcommand.hpp"
|
|
|
|
#include "cli/nodeutility.hpp"
|
2014-10-30 16:19:51 +01:00
|
|
|
#include "cli/pkiutility.hpp"
|
|
|
|
#include "cli/featureutility.hpp"
|
2014-10-19 14:21:12 +02:00
|
|
|
#include "base/logger.hpp"
|
2014-10-30 16:19:51 +01:00
|
|
|
#include "base/console.hpp"
|
2014-10-17 15:13:17 +02:00
|
|
|
#include "base/application.hpp"
|
2014-10-30 16:19:51 +01: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>
|
|
|
|
#include <boost/algorithm/string/join.hpp>
|
|
|
|
#include <boost/algorithm/string/replace.hpp>
|
2014-10-30 16:19:51 +01:00
|
|
|
#include <boost/algorithm/string/case_conv.hpp>
|
2014-10-18 21:06:28 +02:00
|
|
|
#include <iostream>
|
2014-10-30 16:19:51 +01:00
|
|
|
#include <string>
|
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/wizard", NodeWizardCommand);
|
2014-10-17 15:13:17 +02:00
|
|
|
|
2014-10-31 11:38:16 +01:00
|
|
|
String NodeWizardCommand::GetDescription(void) const
|
2014-10-17 15:13:17 +02:00
|
|
|
{
|
2014-10-31 11:53:44 +01:00
|
|
|
return "Wizard for Icinga 2 node setup.";
|
2014-10-17 15:13:17 +02:00
|
|
|
}
|
|
|
|
|
2014-10-31 11:38:16 +01:00
|
|
|
String NodeWizardCommand::GetShortDescription(void) const
|
2014-10-17 15:13:17 +02:00
|
|
|
{
|
2014-10-31 11:53:44 +01:00
|
|
|
return "wizard for node setup";
|
2014-10-17 15:13:17 +02:00
|
|
|
}
|
|
|
|
|
2014-10-31 11:38:16 +01:00
|
|
|
ImpersonationLevel NodeWizardCommand::GetImpersonationLevel(void) const
|
2014-10-30 20:34:33 +01:00
|
|
|
{
|
|
|
|
return ImpersonateRoot;
|
|
|
|
}
|
|
|
|
|
2014-10-31 11:38:16 +01:00
|
|
|
int NodeWizardCommand::GetMaxArguments(void) const
|
2014-10-30 20:34:33 +01:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-10-17 15:13:17 +02:00
|
|
|
/**
|
2014-10-31 11:53:44 +01:00
|
|
|
* The entry point for the "node wizard" CLI command.
|
2014-10-17 15:13:17 +02:00
|
|
|
*
|
|
|
|
* @returns An exit status.
|
|
|
|
*/
|
2014-10-31 11:38:16 +01:00
|
|
|
int NodeWizardCommand::Run(const boost::program_options::variables_map& vm, const std::vector<std::string>& ap) const
|
2014-10-17 15:13:17 +02:00
|
|
|
{
|
2014-10-22 19:26:31 +02:00
|
|
|
/*
|
|
|
|
* The wizard will get all information from the user,
|
|
|
|
* and then call all required functions.
|
|
|
|
*/
|
|
|
|
|
|
|
|
std::cout << "Welcome to the Icinga 2 Setup Wizard!\n"
|
|
|
|
<< "\n"
|
|
|
|
<< "We'll guide you through all required configuration details.\n"
|
|
|
|
<< "\n"
|
|
|
|
<< "If you have questions, please consult the documentation at http://docs.icinga.org\n"
|
|
|
|
<< "or join the community support channels at https://support.icinga.org\n"
|
|
|
|
<< "\n\n";
|
|
|
|
|
|
|
|
//TODO: Add sort of bash completion to path input?
|
|
|
|
|
2014-10-31 11:53:44 +01:00
|
|
|
/* 0. master or node setup?
|
2014-10-22 19:26:31 +02:00
|
|
|
* 1. Ticket
|
|
|
|
* 2. Master information for autosigning
|
|
|
|
* 3. Trusted cert location
|
|
|
|
* 4. CN to use (defaults to FQDN)
|
|
|
|
* 5. Local CA
|
|
|
|
* 6. New self signed certificate
|
|
|
|
* 7. Request signed certificate from master
|
|
|
|
* 8. copy key information to /etc/icinga2/pki
|
|
|
|
* 9. enable ApiListener feature
|
|
|
|
* 10. generate zones.conf with endpoints and zone objects
|
|
|
|
* 11. set NodeName = cn in constants.conf
|
|
|
|
* 12. reload icinga2, or tell the user to
|
|
|
|
*/
|
|
|
|
|
2014-10-30 16:19:51 +01:00
|
|
|
std::string answer;
|
2014-10-31 11:53:44 +01:00
|
|
|
bool is_node_setup = true;
|
2014-10-30 16:19:51 +01:00
|
|
|
|
2014-10-31 11:53:44 +01:00
|
|
|
std::cout << "Please specify if this is an node setup ('no' installs a master setup) [Y/n]: ";
|
2014-10-30 16:19:51 +01:00
|
|
|
std::getline (std::cin, answer);
|
|
|
|
|
|
|
|
boost::algorithm::to_lower(answer);
|
|
|
|
|
2014-10-30 18:21:15 +01:00
|
|
|
String choice = answer;
|
2014-10-30 16:19:51 +01:00
|
|
|
|
2014-10-30 18:21:15 +01:00
|
|
|
if (choice.Contains("n"))
|
2014-10-31 11:53:44 +01:00
|
|
|
is_node_setup = false;
|
2014-10-30 16:19:51 +01:00
|
|
|
|
2014-10-31 11:53:44 +01:00
|
|
|
if (is_node_setup) {
|
|
|
|
/* node setup part */
|
2014-10-31 11:38:16 +01:00
|
|
|
std::cout << "Starting the Node setup routine...\n";
|
2014-10-30 16:19:51 +01:00
|
|
|
|
|
|
|
/* CN */
|
|
|
|
std::cout << "Please specifiy the common name (CN) [" << Utility::GetFQDN() << "]: ";
|
|
|
|
|
|
|
|
std::getline(std::cin, answer);
|
|
|
|
boost::algorithm::to_lower(answer);
|
|
|
|
|
|
|
|
if (answer.empty())
|
|
|
|
answer = Utility::GetFQDN();
|
|
|
|
|
|
|
|
String cn = answer;
|
|
|
|
cn.Trim();
|
|
|
|
|
2014-10-31 22:21:36 +01:00
|
|
|
std::cout << "Please specifiy the local zone name [" << cn << "]: ";
|
|
|
|
|
|
|
|
std::getline(std::cin, answer);
|
|
|
|
boost::algorithm::to_lower(answer);
|
|
|
|
|
|
|
|
if (answer.empty())
|
|
|
|
answer = cn;
|
|
|
|
|
|
|
|
String local_zone = answer;
|
|
|
|
local_zone.Trim();
|
|
|
|
|
2014-10-30 16:19:51 +01:00
|
|
|
std::vector<std::string> endpoints;
|
|
|
|
|
|
|
|
String endpoint_buffer;
|
|
|
|
|
2014-10-31 11:53:44 +01:00
|
|
|
std::cout << "Please specify the master endpoint(s) this node should connect to:\n";
|
2014-10-30 16:19:51 +01:00
|
|
|
String master_endpoint_name;
|
|
|
|
|
|
|
|
wizard_endpoint_loop_start:
|
|
|
|
|
|
|
|
std::cout << "Master CN: ";
|
|
|
|
|
|
|
|
std::getline(std::cin, answer);
|
|
|
|
boost::algorithm::to_lower(answer);
|
|
|
|
|
|
|
|
if(answer.empty()) {
|
|
|
|
Log(LogWarning, "cli", "Master CN is required! Please retry.");
|
|
|
|
goto wizard_endpoint_loop_start;
|
|
|
|
}
|
|
|
|
|
|
|
|
endpoint_buffer = answer;
|
|
|
|
endpoint_buffer.Trim();
|
|
|
|
|
|
|
|
std::cout << "Master endpoint host: ";
|
|
|
|
|
|
|
|
std::getline(std::cin, answer);
|
|
|
|
boost::algorithm::to_lower(answer);
|
|
|
|
|
|
|
|
if (!answer.empty()) {
|
|
|
|
String tmp = answer;
|
|
|
|
tmp.Trim();
|
|
|
|
endpoint_buffer += "," + tmp;
|
|
|
|
master_endpoint_name = tmp; //store the endpoint name for later
|
|
|
|
}
|
|
|
|
|
2014-10-31 22:21:36 +01:00
|
|
|
std::cout << "Master endpoint port (optional) []: ";
|
2014-10-30 16:19:51 +01:00
|
|
|
|
|
|
|
std::getline(std::cin, answer);
|
|
|
|
boost::algorithm::to_lower(answer);
|
|
|
|
|
|
|
|
if (!answer.empty()) {
|
|
|
|
String tmp = answer;
|
|
|
|
tmp.Trim();
|
|
|
|
endpoint_buffer += "," + answer;
|
|
|
|
}
|
|
|
|
|
|
|
|
endpoints.push_back(endpoint_buffer);
|
|
|
|
|
|
|
|
std::cout << "Add more master endpoints? [y/N]";
|
|
|
|
std::getline (std::cin, answer);
|
|
|
|
|
|
|
|
boost::algorithm::to_lower(answer);
|
|
|
|
|
2014-10-30 18:21:15 +01:00
|
|
|
String choice = answer;
|
2014-10-30 16:19:51 +01:00
|
|
|
|
2014-10-30 18:21:15 +01:00
|
|
|
if (choice.Contains("y") || choice.Contains("j"))
|
|
|
|
goto wizard_endpoint_loop_start;
|
2014-10-30 16:19:51 +01:00
|
|
|
|
|
|
|
std::cout << "Please specify the master connection for auto-signing:\n";
|
|
|
|
|
|
|
|
wizard_master_host:
|
|
|
|
std::cout << "Host [" << master_endpoint_name << "]: ";
|
|
|
|
|
|
|
|
std::getline(std::cin, answer);
|
|
|
|
boost::algorithm::to_lower(answer);
|
|
|
|
|
|
|
|
if (answer.empty() && !master_endpoint_name.IsEmpty())
|
|
|
|
answer = master_endpoint_name;
|
|
|
|
|
|
|
|
if (answer.empty() && master_endpoint_name.IsEmpty())
|
|
|
|
goto wizard_master_host;
|
|
|
|
|
|
|
|
String master_host = answer;
|
|
|
|
master_host.Trim();
|
|
|
|
|
|
|
|
std::cout << "Port [5665]: ";
|
|
|
|
|
|
|
|
std::getline(std::cin, answer);
|
|
|
|
boost::algorithm::to_lower(answer);
|
|
|
|
|
|
|
|
if (answer.empty())
|
|
|
|
answer = "5665";
|
|
|
|
|
|
|
|
String master_port = answer;
|
|
|
|
master_port.Trim();
|
|
|
|
|
2014-10-30 20:34:33 +01:00
|
|
|
/* workaround for fetching the master cert */
|
|
|
|
String pki_path = PkiUtility::GetPkiPath();
|
2014-10-31 11:53:44 +01:00
|
|
|
String node_cert = pki_path + "/" + cn + ".crt";
|
|
|
|
String node_key = pki_path + "/" + cn + ".key";
|
2014-10-30 16:19:51 +01:00
|
|
|
|
|
|
|
//new-ca, new-cert
|
|
|
|
PkiUtility::NewCa();
|
|
|
|
|
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-31 11:53:44 +01:00
|
|
|
if (PkiUtility::NewCert(cn, node_key, Empty, node_cert) > 0) {
|
2014-10-30 16:19:51 +01:00
|
|
|
Log(LogCritical, "cli")
|
|
|
|
<< "Failed to create new self-signed certificate for CN '" << cn << "'. Please try again.";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* store ca in /etc/icinga2/pki */
|
2014-10-30 20:34:33 +01:00
|
|
|
String ca_path = PkiUtility::GetLocalCaPath();
|
|
|
|
String ca_key = ca_path + "/ca.key";
|
|
|
|
String ca = ca_path + "/ca.crt";
|
|
|
|
|
|
|
|
/* 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-10-31 11:53:44 +01:00
|
|
|
if (!Utility::SetFileOwnership(node_cert, user, group)) {
|
2014-10-30 20:34:33 +01:00
|
|
|
Log(LogWarning, "cli")
|
2014-10-31 11:53:44 +01:00
|
|
|
<< "Cannot set ownership for user '" << user << "' group '" << group << "' on file '" << node_cert << "'. Verify it yourself!";
|
2014-10-30 20:34:33 +01:00
|
|
|
}
|
2014-10-31 11:53:44 +01:00
|
|
|
if (!Utility::SetFileOwnership(node_key, user, group)) {
|
2014-10-30 20:34:33 +01:00
|
|
|
Log(LogWarning, "cli")
|
2014-10-31 11:53:44 +01:00
|
|
|
<< "Cannot set ownership for user '" << user << "' group '" << group << "' on file '" << node_key << "'. Verify it yourself!";
|
2014-10-30 20:34:33 +01:00
|
|
|
}
|
2014-10-30 16:19:51 +01:00
|
|
|
|
|
|
|
String target_ca = pki_path + "/ca.crt";
|
|
|
|
|
|
|
|
Utility::CopyFile(ca, target_ca);
|
|
|
|
|
2014-10-30 20:34:33 +01:00
|
|
|
/* fix permissions: root -> icinga daemon user */
|
|
|
|
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!";
|
|
|
|
}
|
|
|
|
|
2014-10-30 16:19:51 +01:00
|
|
|
//save-cert and store the master certificate somewhere
|
|
|
|
|
|
|
|
std::cout << "Generating self-signed certifiate:\n";
|
|
|
|
|
|
|
|
|
|
|
|
std::cout << "Fetching public certificate from master ("
|
|
|
|
<< master_host << ", " << master_port << "):\n";
|
|
|
|
|
|
|
|
String trusted_cert = PkiUtility::GetPkiPath() + "/trusted-master.crt";
|
|
|
|
|
2014-10-31 11:53:44 +01:00
|
|
|
if (PkiUtility::SaveCert(master_host, master_port, node_key, node_cert, trusted_cert) > 0) {
|
2014-10-30 16:19:51 +01:00
|
|
|
Log(LogCritical, "cli")
|
|
|
|
<< "Failed to fetch trusted master certificate. Please try again.";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::cout << "Stored trusted master certificate in '" << trusted_cert << "'.\n";
|
|
|
|
|
|
|
|
wizard_ticket:
|
2014-10-30 18:07:00 +01:00
|
|
|
std::cout << "Please specify the request ticket generated on your Icinga 2 master."
|
|
|
|
<< " (Hint: '# icinga2 pki ticket --cn " << cn << "'):\n";
|
2014-10-30 16:19:51 +01:00
|
|
|
|
|
|
|
std::getline(std::cin, answer);
|
|
|
|
boost::algorithm::to_lower(answer);
|
|
|
|
|
|
|
|
if (answer.empty())
|
|
|
|
goto wizard_ticket;
|
|
|
|
|
|
|
|
String ticket = answer;
|
|
|
|
ticket.Trim();
|
|
|
|
|
|
|
|
std::cout << "Processing self-signed certificate request. Ticket '" << ticket << "'.\n";
|
|
|
|
|
2014-10-31 11:53:44 +01:00
|
|
|
if (PkiUtility::RequestCertificate(master_host, master_port, node_key, node_cert, ca, trusted_cert, ticket) > 0) {
|
2014-10-30 16:19:51 +01:00
|
|
|
Log(LogCritical, "cli")
|
|
|
|
<< "Failed to fetch signed certificate from master '" << master_host << ", "
|
|
|
|
<< master_port <<"'. Please try again.";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-10-30 20:34:33 +01:00
|
|
|
/* fix permissions (again) when updating the signed certificate */
|
2014-10-31 11:53:44 +01:00
|
|
|
if (!Utility::SetFileOwnership(node_cert, user, group)) {
|
2014-10-30 20:34:33 +01:00
|
|
|
Log(LogWarning, "cli")
|
2014-10-31 11:53:44 +01:00
|
|
|
<< "Cannot set ownership for user '" << user << "' group '" << group << "' on file '" << node_cert << "'. Verify it yourself!";
|
2014-10-30 20:34:33 +01:00
|
|
|
}
|
|
|
|
|
2014-10-30 16:19:51 +01:00
|
|
|
/* apilistener config */
|
|
|
|
std::cout << "Please specify the API bind host/port (optional):\n";
|
|
|
|
std::cout << "Bind Host []: ";
|
|
|
|
|
|
|
|
std::getline(std::cin, answer);
|
|
|
|
boost::algorithm::to_lower(answer);
|
|
|
|
|
|
|
|
String bind_host = answer;
|
|
|
|
bind_host.Trim();
|
|
|
|
|
|
|
|
std::cout << "Bind Port []: ";
|
|
|
|
|
|
|
|
std::getline(std::cin, answer);
|
|
|
|
boost::algorithm::to_lower(answer);
|
|
|
|
|
|
|
|
String bind_port = answer;
|
|
|
|
bind_port.Trim();
|
|
|
|
|
|
|
|
std::cout << "Enabling the APIlistener feature.\n";
|
|
|
|
|
|
|
|
std::vector<std::string> enable;
|
|
|
|
enable.push_back("api");
|
|
|
|
FeatureUtility::EnableFeatures(enable);
|
|
|
|
|
|
|
|
String apipath = FeatureUtility::GetFeaturesAvailablePath() + "/api.conf";
|
2014-10-31 11:38:16 +01:00
|
|
|
NodeUtility::CreateBackupFile(apipath);
|
2014-10-30 16:19:51 +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 (!bind_host.IsEmpty())
|
|
|
|
fp << " bind_host = \"" << bind_host << "\"\n";
|
|
|
|
if (!bind_port.IsEmpty())
|
|
|
|
fp << " bind_port = " << bind_port << "\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));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* apilistener config */
|
|
|
|
std::cout << "Generating local zones.conf.\n";
|
|
|
|
|
2014-10-31 22:21:36 +01:00
|
|
|
NodeUtility::GenerateNodeIcingaConfig(endpoints, cn, local_zone);
|
2014-10-30 16:19:51 +01: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!";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::cout << "Updating constants.conf\n";
|
|
|
|
|
2014-10-31 11:38:16 +01:00
|
|
|
NodeUtility::CreateBackupFile(Application::GetSysconfDir() + "/icinga2/constants.conf");
|
2014-10-30 16:19:51 +01:00
|
|
|
|
2014-10-31 11:38:16 +01:00
|
|
|
NodeUtility::UpdateConstant("NodeName", cn);
|
2014-10-30 16:19:51 +01:00
|
|
|
|
|
|
|
} else {
|
|
|
|
/* master setup */
|
|
|
|
std::cout << "Starting the Master setup routine...\n";
|
|
|
|
|
|
|
|
/* CN */
|
2014-10-30 20:34:33 +01:00
|
|
|
std::cout << "Please specifiy the common name (CN) [" << Utility::GetFQDN() << "]: ";
|
2014-10-30 16:19:51 +01:00
|
|
|
|
|
|
|
std::getline(std::cin, answer);
|
|
|
|
boost::algorithm::to_lower(answer);
|
|
|
|
|
|
|
|
if (answer.empty())
|
|
|
|
answer = Utility::GetFQDN();
|
|
|
|
|
|
|
|
String cn = answer;
|
|
|
|
cn.Trim();
|
|
|
|
|
|
|
|
if (PkiUtility::NewCa() > 0) {
|
|
|
|
Log(LogWarning, "cli", "Found CA, skipping and using the existing one.");
|
|
|
|
}
|
|
|
|
|
|
|
|
String pki_path = PkiUtility::GetPkiPath();
|
|
|
|
|
|
|
|
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-30 16:19:51 +01:00
|
|
|
String key = pki_path + "/" + cn + ".key";
|
|
|
|
String csr = pki_path + "/" + cn + ".csr";
|
|
|
|
|
|
|
|
if (PkiUtility::NewCert(cn, key, csr, "") > 0) {
|
|
|
|
Log(LogCritical, "cli", "Failed to create self-signed certificate");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Sign the CSR with the CA key */
|
|
|
|
|
|
|
|
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-10-30 16:19:51 +01:00
|
|
|
String target_ca = pki_path + "/ca.crt";
|
|
|
|
|
|
|
|
Log(LogInformation, "cli")
|
|
|
|
<< "Copying CA certificate to '" << target_ca << "'.";
|
|
|
|
|
|
|
|
/* does not overwrite existing files! */
|
|
|
|
Utility::CopyFile(ca, target_ca);
|
|
|
|
|
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!";
|
|
|
|
}
|
|
|
|
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-31 11:38:16 +01:00
|
|
|
NodeUtility::GenerateNodeMasterIcingaConfig(cn);
|
2014-10-30 16:19:51 +01:00
|
|
|
|
|
|
|
/* apilistener config */
|
|
|
|
std::cout << "Please specify the API bind host/port (optional):\n";
|
|
|
|
std::cout << "Bind Host []: ";
|
|
|
|
|
|
|
|
std::getline(std::cin, answer);
|
|
|
|
boost::algorithm::to_lower(answer);
|
|
|
|
|
|
|
|
String bind_host = answer;
|
|
|
|
bind_host.Trim();
|
|
|
|
|
|
|
|
std::cout << "Bind Port []: ";
|
|
|
|
|
|
|
|
std::getline(std::cin, answer);
|
|
|
|
boost::algorithm::to_lower(answer);
|
|
|
|
|
|
|
|
String bind_port = answer;
|
|
|
|
bind_port.Trim();
|
|
|
|
|
|
|
|
std::cout << "Enabling the APIlistener feature.\n";
|
|
|
|
|
|
|
|
std::vector<std::string> enable;
|
|
|
|
enable.push_back("api");
|
|
|
|
FeatureUtility::EnableFeatures(enable);
|
|
|
|
|
|
|
|
String apipath = FeatureUtility::GetFeaturesAvailablePath() + "/api.conf";
|
2014-10-31 11:38:16 +01:00
|
|
|
NodeUtility::CreateBackupFile(apipath);
|
2014-10-30 16:19:51 +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 (!bind_host.IsEmpty())
|
|
|
|
fp << " bind_host = \"" << bind_host << "\"\n";
|
|
|
|
if (!bind_port.IsEmpty())
|
|
|
|
fp << " bind_port = " << bind_port << "\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));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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!";
|
|
|
|
}
|
|
|
|
|
|
|
|
Log(LogInformation, "cli", "Updating constants.conf.");
|
|
|
|
|
2014-10-31 11:38:16 +01:00
|
|
|
NodeUtility::CreateBackupFile(Application::GetSysconfDir() + "/icinga2/constants.conf");
|
2014-10-30 16:19:51 +01:00
|
|
|
|
2014-10-31 11:38:16 +01:00
|
|
|
NodeUtility::UpdateConstant("NodeName", cn);
|
2014-10-30 16:19:51 +01:00
|
|
|
|
|
|
|
String salt = RandomString(16);
|
|
|
|
|
2014-10-31 11:38:16 +01:00
|
|
|
NodeUtility::UpdateConstant("TicketSalt", salt);
|
2014-10-30 16:19:51 +01:00
|
|
|
|
|
|
|
Log(LogInformation, "cli")
|
|
|
|
<< "Edit the api feature config file '" << apipath << "' and set a secure 'ticket_salt' attribute.";
|
|
|
|
}
|
|
|
|
|
2014-10-31 11:53:44 +01:00
|
|
|
std::cout << "Now restart your Icinga 2 to finish the installation!\n";
|
2014-10-30 16:19:51 +01:00
|
|
|
|
|
|
|
std::cout << "If you encounter problems or bugs, please do not hesitate to\n"
|
|
|
|
<< "get in touch with the community at https://support.icinga.org" << std::endl;
|
|
|
|
|
2014-10-17 15:13:17 +02:00
|
|
|
return 0;
|
|
|
|
}
|