2019-02-25 14:48:22 +01:00
|
|
|
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
|
2015-06-24 15:09:22 +02:00
|
|
|
|
|
|
|
#include "cli/apisetuputility.hpp"
|
|
|
|
#include "cli/nodeutility.hpp"
|
|
|
|
#include "cli/featureutility.hpp"
|
2017-09-05 14:21:30 +02:00
|
|
|
#include "remote/apilistener.hpp"
|
2017-09-05 14:44:56 +02:00
|
|
|
#include "remote/pkiutility.hpp"
|
2015-06-24 15:09:22 +02:00
|
|
|
#include "base/logger.hpp"
|
|
|
|
#include "base/console.hpp"
|
|
|
|
#include "base/application.hpp"
|
|
|
|
#include "base/tlsutility.hpp"
|
|
|
|
#include "base/scriptglobal.hpp"
|
|
|
|
#include "base/exception.hpp"
|
2019-04-01 18:54:13 +02:00
|
|
|
#include "base/utility.hpp"
|
2015-06-24 15:09:22 +02:00
|
|
|
#include <boost/algorithm/string/join.hpp>
|
|
|
|
#include <boost/algorithm/string/replace.hpp>
|
|
|
|
#include <boost/algorithm/string/case_conv.hpp>
|
|
|
|
#include <iostream>
|
|
|
|
#include <string>
|
|
|
|
#include <fstream>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
String ApiSetupUtility::GetConfdPath()
|
2015-06-24 15:09:22 +02:00
|
|
|
{
|
2018-08-09 15:37:23 +02:00
|
|
|
return Configuration::ConfigDir + "/conf.d";
|
2018-05-08 16:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
String ApiSetupUtility::GetApiUsersConfPath()
|
|
|
|
{
|
|
|
|
return ApiSetupUtility::GetConfdPath() + "/api-users.conf";
|
2015-06-24 15:09:22 +02:00
|
|
|
}
|
|
|
|
|
2018-07-26 17:09:06 +02:00
|
|
|
bool ApiSetupUtility::SetupMaster(const String& cn, bool prompt_restart)
|
2015-06-24 15:09:22 +02:00
|
|
|
{
|
2015-10-22 15:56:27 +02:00
|
|
|
if (!SetupMasterCertificates(cn))
|
|
|
|
return false;
|
2015-07-08 15:32:11 +02:00
|
|
|
|
2015-10-22 15:56:27 +02:00
|
|
|
if (!SetupMasterApiUser())
|
|
|
|
return false;
|
|
|
|
|
2018-07-26 17:09:06 +02:00
|
|
|
if (!SetupMasterEnableApi())
|
2015-10-22 15:56:27 +02:00
|
|
|
return false;
|
|
|
|
|
2017-11-28 13:38:53 +01:00
|
|
|
if (!SetupMasterUpdateConstants(cn))
|
|
|
|
return false;
|
|
|
|
|
2018-07-26 17:09:06 +02:00
|
|
|
if (prompt_restart) {
|
2015-10-26 07:03:41 +01:00
|
|
|
std::cout << "Done.\n\n";
|
|
|
|
std::cout << "Now restart your Icinga 2 daemon to finish the installation!\n\n";
|
|
|
|
}
|
|
|
|
|
2015-10-22 15:56:27 +02:00
|
|
|
return true;
|
2015-07-08 15:32:11 +02:00
|
|
|
}
|
|
|
|
|
2015-10-22 15:56:27 +02:00
|
|
|
bool ApiSetupUtility::SetupMasterCertificates(const String& cn)
|
2015-07-08 15:32:11 +02:00
|
|
|
{
|
2015-10-26 07:03:41 +01:00
|
|
|
Log(LogInformation, "cli", "Generating new CA.");
|
2015-06-24 15:09:22 +02:00
|
|
|
|
2015-10-22 15:56:27 +02:00
|
|
|
if (PkiUtility::NewCa() > 0)
|
2015-06-24 15:09:22 +02:00
|
|
|
Log(LogWarning, "cli", "Found CA, skipping and using the existing one.");
|
|
|
|
|
2017-09-06 12:11:48 +02:00
|
|
|
String pki_path = ApiListener::GetCertsDir();
|
2015-08-24 15:11:49 +02:00
|
|
|
Utility::MkDirP(pki_path, 0700);
|
2015-06-24 15:09:22 +02:00
|
|
|
|
2018-08-09 15:37:23 +02:00
|
|
|
String user = Configuration::RunAsUser;
|
|
|
|
String group = Configuration::RunAsGroup;
|
2015-06-24 15:09:22 +02:00
|
|
|
|
|
|
|
if (!Utility::SetFileOwnership(pki_path, user, group)) {
|
|
|
|
Log(LogWarning, "cli")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Cannot set ownership for user '" << user << "' group '" << group << "' on file '" << pki_path << "'.";
|
2015-06-24 15:09:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
String key = pki_path + "/" + cn + ".key";
|
|
|
|
String csr = pki_path + "/" + cn + ".csr";
|
|
|
|
|
2015-11-08 14:19:06 +01:00
|
|
|
if (Utility::PathExists(key)) {
|
|
|
|
Log(LogInformation, "cli")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Private key file '" << key << "' already exists, not generating new certificate.";
|
2015-11-08 14:19:06 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-06-24 15:09:22 +02:00
|
|
|
Log(LogInformation, "cli")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Generating new CSR in '" << csr << "'.";
|
2015-06-24 15:09:22 +02:00
|
|
|
|
|
|
|
if (Utility::PathExists(key))
|
|
|
|
NodeUtility::CreateBackupFile(key, true);
|
|
|
|
if (Utility::PathExists(csr))
|
|
|
|
NodeUtility::CreateBackupFile(csr);
|
|
|
|
|
|
|
|
if (PkiUtility::NewCert(cn, key, csr, "") > 0) {
|
|
|
|
Log(LogCritical, "cli", "Failed to create certificate signing request.");
|
2015-10-22 15:56:27 +02:00
|
|
|
return false;
|
2015-06-24 15:09:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Sign the CSR with the CA key */
|
|
|
|
String cert = pki_path + "/" + cn + ".crt";
|
|
|
|
|
|
|
|
Log(LogInformation, "cli")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Signing CSR with CA and writing certificate to '" << cert << "'.";
|
2015-06-24 15:09:22 +02:00
|
|
|
|
|
|
|
if (Utility::PathExists(cert))
|
|
|
|
NodeUtility::CreateBackupFile(cert);
|
|
|
|
|
|
|
|
if (PkiUtility::SignCsr(csr, cert) != 0) {
|
|
|
|
Log(LogCritical, "cli", "Could not sign CSR.");
|
2015-10-22 15:56:27 +02:00
|
|
|
return false;
|
2015-06-24 15:09:22 +02:00
|
|
|
}
|
|
|
|
|
2015-10-22 15:56:27 +02:00
|
|
|
/* Copy CA certificate to /etc/icinga2/pki */
|
2017-09-05 14:21:30 +02:00
|
|
|
String ca_path = ApiListener::GetCaDir();
|
2015-06-24 15:09:22 +02:00
|
|
|
String ca = ca_path + "/ca.crt";
|
|
|
|
String ca_key = ca_path + "/ca.key";
|
|
|
|
String target_ca = pki_path + "/ca.crt";
|
|
|
|
|
|
|
|
Log(LogInformation, "cli")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Copying CA certificate to '" << target_ca << "'.";
|
2015-06-24 15:09:22 +02:00
|
|
|
|
|
|
|
if (Utility::PathExists(target_ca))
|
|
|
|
NodeUtility::CreateBackupFile(target_ca);
|
|
|
|
|
|
|
|
/* does not overwrite existing files! */
|
|
|
|
Utility::CopyFile(ca, target_ca);
|
|
|
|
|
|
|
|
/* fix permissions: root -> icinga daemon user */
|
2017-11-30 08:19:58 +01:00
|
|
|
for (const String& file : { ca_path, ca, ca_key, target_ca, key, csr, cert }) {
|
2015-06-24 15:09:22 +02:00
|
|
|
if (!Utility::SetFileOwnership(file, user, group)) {
|
|
|
|
Log(LogWarning, "cli")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Cannot set ownership for user '" << user << "' group '" << group << "' on file '" << file << "'.";
|
2015-06-24 15:09:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-22 15:56:27 +02:00
|
|
|
return true;
|
2015-07-08 15:32:11 +02:00
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
bool ApiSetupUtility::SetupMasterApiUser()
|
2015-07-08 15:32:11 +02:00
|
|
|
{
|
2018-11-01 19:16:26 +01:00
|
|
|
if (!Utility::PathExists(GetConfdPath())) {
|
|
|
|
Log(LogWarning, "cli")
|
|
|
|
<< "Path '" << GetConfdPath() << "' do not exist.";
|
|
|
|
Log(LogInformation, "cli")
|
|
|
|
<< "Creating path '" << GetConfdPath() << "'.";
|
|
|
|
|
|
|
|
Utility::MkDirP(GetConfdPath(), 0755);
|
|
|
|
}
|
|
|
|
|
2015-10-22 15:56:27 +02:00
|
|
|
String api_username = "root"; // TODO make this available as cli parameter?
|
2015-06-24 15:09:22 +02:00
|
|
|
String api_password = RandomString(8);
|
2016-02-22 16:47:41 +01:00
|
|
|
String apiUsersPath = GetConfdPath() + "/api-users.conf";
|
2015-06-24 15:09:22 +02:00
|
|
|
|
2016-02-22 16:47:41 +01:00
|
|
|
if (Utility::PathExists(apiUsersPath)) {
|
2015-11-08 14:19:06 +01:00
|
|
|
Log(LogInformation, "cli")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "API user config file '" << apiUsersPath << "' already exists, not creating config file.";
|
2015-11-08 14:19:06 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-06-24 15:09:22 +02:00
|
|
|
Log(LogInformation, "cli")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Adding new ApiUser '" << api_username << "' in '" << apiUsersPath << "'.";
|
2015-06-24 15:09:22 +02:00
|
|
|
|
2016-02-22 16:47:41 +01:00
|
|
|
NodeUtility::CreateBackupFile(apiUsersPath);
|
2015-06-24 15:09:22 +02:00
|
|
|
|
2016-02-22 16:47:41 +01:00
|
|
|
std::fstream fp;
|
2016-02-24 13:55:25 +01:00
|
|
|
String tempFilename = Utility::CreateTempFile(apiUsersPath + ".XXXXXX", 0644, fp);
|
2015-06-24 15:09:22 +02:00
|
|
|
|
|
|
|
fp << "/**\n"
|
2018-07-17 14:23:20 +02:00
|
|
|
<< " * The ApiUser objects are used for authentication against the API.\n"
|
2017-12-19 15:50:05 +01:00
|
|
|
<< " */\n"
|
|
|
|
<< "object ApiUser \"" << api_username << "\" {\n"
|
|
|
|
<< " password = \"" << api_password << "\"\n"
|
|
|
|
<< " // client_cn = \"\"\n"
|
|
|
|
<< "\n"
|
|
|
|
<< " permissions = [ \"*\" ]\n"
|
|
|
|
<< "}\n";
|
2015-06-24 15:09:22 +02:00
|
|
|
|
|
|
|
fp.close();
|
|
|
|
|
2019-04-10 13:44:13 +02:00
|
|
|
Utility::RenameFile(tempFilename, apiUsersPath);
|
2015-06-24 15:09:22 +02:00
|
|
|
|
2015-10-22 15:56:27 +02:00
|
|
|
return true;
|
2015-07-08 15:32:11 +02:00
|
|
|
}
|
2015-06-24 15:09:22 +02:00
|
|
|
|
2018-07-26 17:09:06 +02:00
|
|
|
bool ApiSetupUtility::SetupMasterEnableApi()
|
2015-07-08 15:32:11 +02:00
|
|
|
{
|
2018-08-23 22:08:02 +02:00
|
|
|
/*
|
|
|
|
* Ensure the api-users.conf file is included, when conf.d inclusion is disabled.
|
|
|
|
*/
|
|
|
|
if (!NodeUtility::GetConfigurationIncludeState("\"conf.d\"", true))
|
|
|
|
NodeUtility::UpdateConfiguration("\"conf.d/api-users.conf\"", true, false);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Enable the API feature
|
|
|
|
*/
|
2015-10-26 07:03:41 +01:00
|
|
|
Log(LogInformation, "cli", "Enabling the 'api' feature.");
|
2015-06-24 15:09:22 +02:00
|
|
|
|
2018-07-26 17:09:06 +02:00
|
|
|
FeatureUtility::EnableFeatures({ "api" });
|
2015-06-24 15:09:22 +02:00
|
|
|
|
2015-10-22 15:56:27 +02:00
|
|
|
return true;
|
2015-06-24 15:09:22 +02:00
|
|
|
}
|
2017-11-28 13:38:53 +01:00
|
|
|
|
|
|
|
bool ApiSetupUtility::SetupMasterUpdateConstants(const String& cn)
|
|
|
|
{
|
|
|
|
NodeUtility::UpdateConstant("NodeName", cn);
|
|
|
|
NodeUtility::UpdateConstant("ZoneName", cn);
|
2017-12-12 11:54:47 +01:00
|
|
|
|
|
|
|
return true;
|
2017-11-28 13:38:53 +01:00
|
|
|
}
|