2014-10-17 15:13:17 +02:00
/******************************************************************************
* Icinga 2 *
2015-01-22 12:00:23 +01:00
* Copyright ( C ) 2012 - 2015 Icinga Development Team ( http : //www.icinga.org) *
2014-10-17 15:13:17 +02:00
* *
* 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"
2015-06-25 10:41:23 +02:00
# include "cli/apisetuputility.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-12-14 11:33:45 +01:00
# include "base/scriptglobal.hpp"
2014-12-15 10:16:06 +01:00
# include "base/exception.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 .
*/
2014-11-02 20:17:57 +01:00
std : : cout < < ConsoleColorTag ( Console_Bold | Console_ForegroundBlue ) < < " Welcome to the Icinga 2 Setup Wizard! \n "
2014-10-22 19:26:31 +02:00
< < " \n "
< < " We'll guide you through all required configuration details. \n "
< < " \n "
2014-11-02 20:17:57 +01:00
< < " \n \n " < < ConsoleColorTag ( Console_Normal ) ;
2014-10-22 19:26:31 +02:00
//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
2015-03-27 14:11:21 +01:00
/* master or node setup */
2014-11-02 20:17:57 +01:00
std : : cout < < ConsoleColorTag ( Console_Bold ) < < " Please specify if this is a satellite setup "
< < " ('n' installs a master setup) " < < ConsoleColorTag ( Console_Normal ) < < " [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 */
2014-11-02 20:17:57 +01:00
std : : cout < < ConsoleColorTag ( Console_Bold ) < < " Please specifiy the common name (CN) " < < ConsoleColorTag ( Console_Normal ) < < " [ " < < 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 ;
2015-08-27 18:06:20 +02:00
cn = cn . Trim ( ) ;
2014-10-30 16:19:51 +01:00
2014-11-02 20:17:57 +01:00
std : : cout < < ConsoleColorTag ( Console_Bold ) < < " Please specifiy the local zone name " < < ConsoleColorTag ( Console_Normal ) < < " [ " < < cn < < " ]: " ;
2014-10-31 22:21:36 +01:00
std : : getline ( std : : cin , answer ) ;
boost : : algorithm : : to_lower ( answer ) ;
if ( answer . empty ( ) )
answer = cn ;
String local_zone = answer ;
2015-08-27 18:06:20 +02:00
local_zone = local_zone . Trim ( ) ;
2014-10-31 22:21:36 +01:00
2014-10-30 16:19:51 +01:00
std : : vector < std : : string > endpoints ;
String endpoint_buffer ;
2014-11-02 20:17:57 +01:00
std : : cout < < ConsoleColorTag ( Console_Bold ) < < " Please specify the master endpoint(s) this node should connect to: " < < ConsoleColorTag ( Console_Normal ) < < " \n " ;
2014-10-30 16:19:51 +01:00
String master_endpoint_name ;
wizard_endpoint_loop_start :
2014-11-13 15:25:15 +01:00
std : : cout < < ConsoleColorTag ( Console_Bold ) < < " Master Common Name " < < ConsoleColorTag ( Console_Normal ) < < " (CN from your master setup): " ;
2014-10-30 16:19:51 +01:00
std : : getline ( std : : cin , answer ) ;
boost : : algorithm : : to_lower ( answer ) ;
2015-02-11 15:47:45 +01:00
if ( answer . empty ( ) ) {
2014-10-30 16:19:51 +01:00
Log ( LogWarning , " cli " , " Master CN is required! Please retry. " ) ;
goto wizard_endpoint_loop_start ;
}
endpoint_buffer = answer ;
2015-08-27 18:06:20 +02:00
endpoint_buffer = endpoint_buffer . Trim ( ) ;
2014-10-30 16:19:51 +01:00
2015-04-27 11:10:49 +02:00
std : : cout < < " Do you want to establish a connection to the master " < < ConsoleColorTag ( Console_Bold ) < < " from this node? " < < ConsoleColorTag ( Console_Normal ) < < " [Y/n]: " ;
2014-10-30 16:19:51 +01:00
2015-04-20 10:51:18 +02:00
std : : getline ( std : : cin , answer ) ;
2014-10-30 16:19:51 +01:00
boost : : algorithm : : to_lower ( answer ) ;
2015-04-20 10:51:18 +02:00
choice = answer ;
if ( choice . Contains ( " n " ) ) {
Log ( LogWarning , " cli " , " Node to master connection setup skipped " ) ;
2015-04-27 11:10:49 +02:00
std : : cout < < " Connection setup skipped. Please configure your master to connect to this node. \n " ;
2015-04-20 10:51:18 +02:00
} else {
2015-04-30 12:52:35 +02:00
std : : cout < < ConsoleColorTag ( Console_Bold ) < < " Please fill out the master connection information: " < < ConsoleColorTag ( Console_Normal ) < < " \n " ;
2015-04-20 10:51:18 +02:00
std : : cout < < ConsoleColorTag ( Console_Bold ) < < " Master endpoint host " < < ConsoleColorTag ( Console_Normal ) < < " (Your master's IP address or FQDN): " ;
std : : getline ( std : : cin , answer ) ;
boost : : algorithm : : to_lower ( answer ) ;
2015-04-27 11:10:49 +02:00
if ( answer . empty ( ) ) {
Log ( LogWarning , " cli " , " Please enter the master's endpoint connection information. " ) ;
goto wizard_endpoint_loop_start ;
}
2014-10-30 16:19:51 +01:00
String tmp = answer ;
2015-08-27 18:06:20 +02:00
tmp = tmp . Trim ( ) ;
2014-10-30 16:19:51 +01:00
endpoint_buffer + = " , " + tmp ;
master_endpoint_name = tmp ; //store the endpoint name for later
2015-04-20 10:51:18 +02:00
std : : cout < < ConsoleColorTag ( Console_Bold ) < < " Master endpoint port " < < ConsoleColorTag ( Console_Normal ) < < " [5665]: " ;
2014-10-30 16:19:51 +01:00
2015-04-20 10:51:18 +02:00
std : : getline ( std : : cin , answer ) ;
boost : : algorithm : : to_lower ( answer ) ;
if ( ! answer . empty ( ) )
tmp = answer ;
else
tmp = " 5665 " ;
2014-10-30 16:19:51 +01:00
2015-08-27 18:06:20 +02:00
endpoint_buffer + = " , " + tmp . Trim ( ) ;
2014-10-30 16:19:51 +01:00
}
endpoints . push_back ( endpoint_buffer ) ;
2015-04-20 10:51:18 +02:00
std : : cout < < ConsoleColorTag ( Console_Bold ) < < " Add more master endpoints? " < < ConsoleColorTag ( Console_Normal ) < < " [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
2015-03-31 09:28:03 +02:00
if ( choice . Contains ( " y " ) )
2014-10-30 18:21:15 +01:00
goto wizard_endpoint_loop_start ;
2014-10-30 16:19:51 +01:00
2014-11-02 20:17:57 +01:00
std : : cout < < ConsoleColorTag ( Console_Bold ) < < " Please specify the master connection for CSR auto-signing " < < ConsoleColorTag ( Console_Normal ) < < " (defaults to master endpoint host): \n " ;
2014-10-30 16:19:51 +01:00
wizard_master_host :
2014-11-02 20:17:57 +01:00
std : : cout < < ConsoleColorTag ( Console_Bold ) < < " Host " < < ConsoleColorTag ( Console_Normal ) < < " [ " < < master_endpoint_name < < " ]: " ;
2014-10-30 16:19:51 +01:00
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 ;
2015-08-27 18:06:20 +02:00
master_host = master_host . Trim ( ) ;
2014-10-30 16:19:51 +01:00
2014-11-02 20:17:57 +01:00
std : : cout < < ConsoleColorTag ( Console_Bold ) < < " Port " < < ConsoleColorTag ( Console_Normal ) < < " [5665]: " ;
2014-10-30 16:19:51 +01:00
std : : getline ( std : : cin , answer ) ;
boost : : algorithm : : to_lower ( answer ) ;
if ( answer . empty ( ) )
answer = " 5665 " ;
String master_port = answer ;
2015-08-27 18:06:20 +02:00
master_port = master_port . Trim ( ) ;
2014-10-30 16:19:51 +01:00
2014-10-30 20:34:33 +01:00
/* workaround for fetching the master cert */
String pki_path = PkiUtility : : GetPkiPath ( ) ;
2015-08-24 15:11:49 +02:00
Utility : : MkDirP ( pki_path , 0700 ) ;
2014-10-30 20:34:33 +01:00
2014-12-14 11:33:45 +01:00
String user = ScriptGlobal : : Get ( " RunAsUser " ) ;
String group = ScriptGlobal : : 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! " ;
}
2015-08-24 15:11:49 +02:00
String node_cert = pki_path + " / " + cn + " .crt " ;
String node_key = pki_path + " / " + cn + " .key " ;
2015-02-07 20:44:25 +01:00
if ( Utility : : PathExists ( node_key ) )
2015-02-09 14:04:00 +01:00
NodeUtility : : CreateBackupFile ( node_key , true ) ;
2015-02-07 20:44:25 +01:00
if ( Utility : : PathExists ( node_cert ) )
2015-02-09 14:04:00 +01:00
NodeUtility : : CreateBackupFile ( node_cert ) ;
2015-02-07 20:44:25 +01:00
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 ;
}
2014-10-30 20:34:33 +01:00
/* fix permissions: root -> icinga daemon user */
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
//save-cert and store the master certificate somewhere
2015-10-29 08:11:23 +01:00
Log ( LogInformation , " cli " , " Generating self-signed certificate: " ) ;
2014-10-30 16:19:51 +01:00
2014-11-02 20:17:57 +01:00
Log ( LogInformation , " cli " )
< < " Fetching public certificate from master ( "
2014-10-30 16:19:51 +01:00
< < master_host < < " , " < < master_port < < " ): \n " ;
String trusted_cert = PkiUtility : : GetPkiPath ( ) + " /trusted-master.crt " ;
2015-02-07 20:44:25 +01:00
if ( Utility : : PathExists ( trusted_cert ) )
2015-02-09 14:04:00 +01:00
NodeUtility : : CreateBackupFile ( trusted_cert ) ;
2015-02-07 20:44:25 +01:00
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 ;
}
2014-11-02 20:17:57 +01:00
Log ( LogInformation , " cli " )
< < " Stored trusted master certificate in ' " < < trusted_cert < < " '. \n " ;
2014-10-30 16:19:51 +01:00
wizard_ticket :
2014-11-02 20:17:57 +01:00
std : : cout < < ConsoleColorTag ( Console_Bold ) < < " Please specify the request ticket generated on your Icinga 2 master. " < < ConsoleColorTag ( Console_Normal ) < < " \n "
2015-02-05 10:30:30 +01:00
< < " (Hint: # icinga2 pki ticket --cn ' " < < cn < < " '): " ;
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 ;
2015-08-27 18:06:20 +02:00
ticket = ticket . Trim ( ) ;
2014-10-30 16:19:51 +01:00
2014-11-02 20:17:57 +01:00
Log ( LogInformation , " cli " )
< < " Processing self-signed certificate request. Ticket ' " < < ticket < < " '. \n " ;
2014-10-30 16:19:51 +01:00
2014-11-13 15:25:15 +01:00
String target_ca = pki_path + " /ca.crt " ;
2015-02-07 20:44:25 +01:00
if ( Utility : : PathExists ( target_ca ) )
2015-02-09 14:04:00 +01:00
NodeUtility : : CreateBackupFile ( target_ca ) ;
2015-02-07 20:44:25 +01:00
if ( Utility : : PathExists ( node_cert ) )
2015-02-09 14:04:00 +01:00
NodeUtility : : CreateBackupFile ( node_cert ) ;
2015-02-07 20:44:25 +01:00
2014-11-13 15:25:15 +01:00
if ( PkiUtility : : RequestCertificate ( master_host , master_port , node_key , node_cert , target_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. " ;
2015-01-08 17:09:31 +01:00
goto wizard_ticket ;
2014-10-30 16:19:51 +01:00
}
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 */
2014-11-02 20:17:57 +01:00
std : : cout < < ConsoleColorTag ( Console_Bold ) < < " Please specify the API bind host/port " < < ConsoleColorTag ( Console_Normal ) < < " (optional): \n " ;
std : : cout < < ConsoleColorTag ( Console_Bold ) < < " Bind Host " < < ConsoleColorTag ( Console_Normal ) < < " []: " ;
2014-10-30 16:19:51 +01:00
std : : getline ( std : : cin , answer ) ;
boost : : algorithm : : to_lower ( answer ) ;
String bind_host = answer ;
2015-08-27 18:06:20 +02:00
bind_host = bind_host . Trim ( ) ;
2014-10-30 16:19:51 +01:00
std : : cout < < " Bind Port []: " ;
std : : getline ( std : : cin , answer ) ;
boost : : algorithm : : to_lower ( answer ) ;
String bind_port = answer ;
2015-08-27 18:06:20 +02:00
bind_port = bind_port . Trim ( ) ;
2014-10-30 16:19:51 +01:00
2015-03-27 14:11:21 +01:00
std : : cout < < ConsoleColorTag ( Console_Bold ) < < " Accept config from master? " < < ConsoleColorTag ( Console_Normal ) < < " [y/N]: " ;
std : : getline ( std : : cin , answer ) ;
boost : : algorithm : : to_lower ( answer ) ;
choice = answer ;
2015-03-31 09:28:03 +02:00
String accept_config = choice . Contains ( " y " ) ? " true " : " false " ;
2015-03-27 14:11:21 +01:00
std : : cout < < ConsoleColorTag ( Console_Bold ) < < " Accept commands from master? " < < ConsoleColorTag ( Console_Normal ) < < " [y/N]: " ;
std : : getline ( std : : cin , answer ) ;
boost : : algorithm : : to_lower ( answer ) ;
choice = answer ;
2015-03-31 09:28:03 +02:00
String accept_commands = choice . Contains ( " y " ) ? " true " : " false " ;
2015-03-27 14:11:21 +01:00
2014-11-02 20:17:57 +01:00
/* disable the notifications feature on client nodes */
Log ( LogInformation , " cli " , " Disabling the Notification feature. " ) ;
std : : vector < std : : string > disable ;
disable . push_back ( " notification " ) ;
FeatureUtility : : DisableFeatures ( disable ) ;
2014-11-02 19:38:35 +01:00
Log ( LogInformation , " cli " , " Enabling the Apilistener feature. " ) ;
2014-10-30 16:19:51 +01:00
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 "
2015-03-27 14:11:21 +01:00
< < " ca_path = SysconfDir + \" /icinga2/pki/ca.crt \" \n "
2015-06-18 19:13:10 +02:00
< < " \n "
2015-04-27 11:10:49 +02:00
< < " accept_config = " < < accept_config < < " \n "
< < " accept_commands = " < < accept_commands < < " \n " ;
2014-10-30 16:19:51 +01:00
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 */
2014-11-02 19:38:35 +01:00
Log ( LogInformation , " cli " , " Generating local zones.conf. " ) ;
2014-10-30 16:19:51 +01:00
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! " ;
}
2014-11-02 19:38:35 +01:00
Log ( LogInformation , " cli " , " Updating constants.conf. " ) ;
2014-10-30 16:19:51 +01:00
2014-11-02 19:38:35 +01:00
String constants_file = Application : : GetSysconfDir ( ) + " /icinga2/constants.conf " ;
NodeUtility : : CreateBackupFile ( constants_file ) ;
2014-10-30 16:19:51 +01:00
2014-10-31 11:38:16 +01:00
NodeUtility : : UpdateConstant ( " NodeName " , cn ) ;
2015-02-05 10:33:40 +01:00
NodeUtility : : UpdateConstant ( " ZoneName " , local_zone ) ;
2014-10-30 16:19:51 +01:00
} else {
/* master setup */
2014-11-02 20:17:57 +01:00
std : : cout < < ConsoleColorTag ( Console_Bold ) < < " Starting the Master setup routine... \n " ;
2014-10-30 16:19:51 +01:00
2015-06-25 13:28:16 +02:00
/* CN */
std : : cout < < ConsoleColorTag ( Console_Bold ) < < " Please specifiy the common name " < < ConsoleColorTag ( Console_Normal ) < < " (CN) [ " < < Utility : : GetFQDN ( ) < < " ]: " ;
std : : getline ( std : : cin , answer ) ;
boost : : algorithm : : to_lower ( answer ) ;
if ( answer . empty ( ) )
answer = Utility : : GetFQDN ( ) ;
String cn = answer ;
2015-08-27 18:06:20 +02:00
cn = cn . Trim ( ) ;
2015-06-25 13:28:16 +02:00
2015-07-08 15:32:47 +02:00
/* check whether the user wants to generate a new certificate or not */
String existing_path = PkiUtility : : GetPkiPath ( ) + " / " + cn + " .crt " ;
2014-10-30 16:19:51 +01:00
2015-07-08 15:32:47 +02:00
std : : cout < < ConsoleColorTag ( Console_Normal ) < < " Checking for existing certificates for common name ' " < < cn < < " '... \n " ;
if ( Utility : : PathExists ( existing_path ) ) {
std : : cout < < " Certificate ' " < < existing_path < < " ' for CN ' " < < cn < < " ' already existing. Skipping certificate generation. \n " ;
} else {
std : : cout < < " Certificates not yet generated. Running 'api setup' now. \n " ;
ApiSetupUtility : : SetupMasterCertificates ( cn ) ;
2014-10-30 20:34:33 +01:00
}
2014-10-30 16:19:51 +01:00
2015-07-08 15:32:47 +02:00
std : : cout < < ConsoleColorTag ( Console_Bold ) < < " Generating master configuration for Icinga 2. \n " < < ConsoleColorTag ( Console_Normal ) ;
2015-10-22 15:56:27 +02:00
ApiSetupUtility : : SetupMasterApiUser ( ) ;
2015-07-08 15:32:47 +02:00
if ( ! FeatureUtility : : CheckFeatureEnabled ( " api " ) )
2015-10-22 15:56:27 +02:00
ApiSetupUtility : : SetupMasterEnableApi ( ) ;
2015-07-08 15:32:47 +02:00
else
std : : cout < < " 'api' feature already enabled. \n " ;
2014-10-31 11:38:16 +01:00
NodeUtility : : GenerateNodeMasterIcingaConfig ( cn ) ;
2014-10-30 16:19:51 +01:00
/* apilistener config */
2014-11-02 20:17:57 +01:00
std : : cout < < ConsoleColorTag ( Console_Bold ) < < " Please specify the API bind host/port (optional): \n " ;
std : : cout < < ConsoleColorTag ( Console_Bold ) < < " Bind Host " < < ConsoleColorTag ( Console_Normal ) < < " []: " ;
2014-10-30 16:19:51 +01:00
std : : getline ( std : : cin , answer ) ;
boost : : algorithm : : to_lower ( answer ) ;
String bind_host = answer ;
2015-08-27 18:06:20 +02:00
bind_host = bind_host . Trim ( ) ;
2014-10-30 16:19:51 +01:00
2014-11-02 20:17:57 +01:00
std : : cout < < ConsoleColorTag ( Console_Bold ) < < " Bind Port " < < ConsoleColorTag ( Console_Normal ) < < " []: " ;
2014-10-30 16:19:51 +01:00
std : : getline ( std : : cin , answer ) ;
boost : : algorithm : : to_lower ( answer ) ;
String bind_port = answer ;
2015-08-27 18:06:20 +02:00
bind_port = bind_port . Trim ( ) ;
2014-10-30 16:19:51 +01:00
2015-06-25 10:41:23 +02:00
/* api feature is always enabled, check above */
2014-10-30 16:19:51 +01:00
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-11-02 17:45:51 +01:00
String constants_file = Application : : GetSysconfDir ( ) + " /icinga2/constants.conf " ;
NodeUtility : : CreateBackupFile ( constants_file ) ;
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
}
2014-11-02 17:45:51 +01:00
std : : cout < < " Done. \n \n " ;
std : : cout < < " Now restart your Icinga 2 daemon to finish the installation! \n \n " ;
2014-10-30 16:19:51 +01:00
2014-10-17 15:13:17 +02:00
return 0 ;
}