summaryrefslogtreecommitdiffstats
path: root/lib/cli/nodewizardcommand.cpp
blob: 3a3cd42bd60c374dc8bacd038fb9b9642c74139c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */

#include "cli/nodewizardcommand.hpp"
#include "cli/nodeutility.hpp"
#include "cli/featureutility.hpp"
#include "cli/apisetuputility.hpp"
#include "remote/apilistener.hpp"
#include "remote/pkiutility.hpp"
#include "base/atomic-file.hpp"
#include "base/logger.hpp"
#include "base/console.hpp"
#include "base/application.hpp"
#include "base/tlsutility.hpp"
#include "base/scriptglobal.hpp"
#include "base/exception.hpp"
#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;
namespace po = boost::program_options;

REGISTER_CLICOMMAND("node/wizard", NodeWizardCommand);

String NodeWizardCommand::GetDescription() const
{
	return "Wizard for Icinga 2 node setup.";
}

String NodeWizardCommand::GetShortDescription() const
{
	return "wizard for node setup";
}

ImpersonationLevel NodeWizardCommand::GetImpersonationLevel() const
{
	return ImpersonateIcinga;
}

int NodeWizardCommand::GetMaxArguments() const
{
	return -1;
}

void NodeWizardCommand::InitParameters(boost::program_options::options_description& visibleDesc,
	boost::program_options::options_description& hiddenDesc) const
{
	visibleDesc.add_options()
		("verbose", "increase log level");
}

/**
 * The entry point for the "node wizard" CLI command.
 *
 * @returns An exit status.
 */
int NodeWizardCommand::Run(const boost::program_options::variables_map& vm,
	const std::vector<std::string>& ap) const
{
	if (!vm.count("verbose"))
		Logger::SetConsoleLogSeverity(LogCritical);

	/*
	 * The wizard will get all information from the user,
	 * and then call all required functions.
	 */

	std::cout << ConsoleColorTag(Console_Bold | Console_ForegroundBlue)
		<< "Welcome to the Icinga 2 Setup Wizard!\n"
		<< "\n"
		<< "We will guide you through all required configuration details.\n"
		<< "\n"
		<< ConsoleColorTag(Console_Normal);

	/* 0. master or node setup?
	 * 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 /var/lib/icinga2/certs
	 * 9. enable ApiListener feature
	 * 10. generate zones.conf with endpoints and zone objects
	 * 11. set NodeName = cn and ZoneName in constants.conf
	 * 12. disable conf.d directory?
	 * 13. reload icinga2, or tell the user to
	 */

	std::string answer;
	/* master or satellite/agent setup */
	std::cout << ConsoleColorTag(Console_Bold)
		<< "Please specify if this is an agent/satellite setup "
		<< "('n' installs a master setup)" << ConsoleColorTag(Console_Normal)
		<< " [Y/n]: ";
	std::getline (std::cin, answer);

	boost::algorithm::to_lower(answer);

	String choice = answer;

	std::cout << "\n";

	int res = 0;

	if (choice.Contains("n"))
		res = MasterSetup();
	else
		res = AgentSatelliteSetup();

	if (res != 0)
		return res;

	std::cout << "\n";
	std::cout << ConsoleColorTag(Console_Bold | Console_ForegroundGreen)
		<< "Done.\n\n"
		<< ConsoleColorTag(Console_Normal);

	std::cout << ConsoleColorTag(Console_Bold | Console_ForegroundRed)
		<< "Now restart your Icinga 2 daemon to finish the installation!\n"
		<< ConsoleColorTag(Console_Normal);

	return 0;
}

int NodeWizardCommand::AgentSatelliteSetup() const
{
	std::string answer;
	String choice;
	bool connectToParent = false;

	std::cout << "Starting the Agent/Satellite setup routine...\n\n";

	/* CN */
	std::cout << ConsoleColorTag(Console_Bold)
		<< "Please specify the common name (CN)"
		<< ConsoleColorTag(Console_Normal)
		<< " [" << Utility::GetFQDN() << "]: ";

	std::getline(std::cin, answer);

	if (answer.empty())
		answer = Utility::GetFQDN();

	String cn = answer;
	cn = cn.Trim();

	std::vector<std::string> endpoints;

	String endpointBuffer;

	std::cout << ConsoleColorTag(Console_Bold)
		<< "\nPlease specify the parent endpoint(s) (master or satellite) where this node should connect to:"
		<< ConsoleColorTag(Console_Normal) << "\n";
	String parentEndpointName;

wizard_endpoint_loop_start:

	std::cout << ConsoleColorTag(Console_Bold)
		<< "Master/Satellite Common Name" << ConsoleColorTag(Console_Normal)
		<< " (CN from your master/satellite node): ";

	std::getline(std::cin, answer);

	if (answer.empty()) {
		Log(LogWarning, "cli", "Master/Satellite CN is required! Please retry.");
		goto wizard_endpoint_loop_start;
	}

	endpointBuffer = answer;
	endpointBuffer = endpointBuffer.Trim();

	std::cout << "\nDo you want to establish a connection to the parent node "
		<< ConsoleColorTag(Console_Bold) << "from this node?"
		<< ConsoleColorTag(Console_Normal) << " [Y/n]: ";

	std::getline (std::cin, answer);
	boost::algorithm::to_lower(answer);
	choice = answer;

	String parentEndpointPort = "5665";

	if (choice.Contains("n")) {
		connectToParent = false;

		Log(LogWarning, "cli", "Node to master/satellite connection setup skipped");
		std::cout << "Connection setup skipped. Please configure your parent node to\n"
			<< "connect to this node by setting the 'host' attribute for the node Endpoint object.\n";

	} else  {
		connectToParent = true;

		std::cout << ConsoleColorTag(Console_Bold)
			<< "Please specify the master/satellite connection information:"
			<< ConsoleColorTag(Console_Normal) << "\n"
			<< ConsoleColorTag(Console_Bold) << "Master/Satellite endpoint host"
			<< ConsoleColorTag(Console_Normal) << " (IP address or FQDN): ";

		std::getline(std::cin, answer);

		if (answer.empty()) {
			Log(LogWarning, "cli", "Please enter the parent endpoint (master/satellite) connection information.");
			goto wizard_endpoint_loop_start;
		}

		String tmp = answer;
		tmp = tmp.Trim();

		endpointBuffer += "," + tmp;
		parentEndpointName = tmp;

		std::cout << ConsoleColorTag(Console_Bold)
			<< "Master/Satellite endpoint port" << ConsoleColorTag(Console_Normal)
			<< " [" << parentEndpointPort << "]: ";

		std::getline(std::cin, answer);

		if (!answer.empty())
			parentEndpointPort = answer;

		endpointBuffer += "," + parentEndpointPort.Trim();
	}

	endpoints.push_back(endpointBuffer);

	std::cout << ConsoleColorTag(Console_Bold) << "\nAdd more master/satellite endpoints?"
		<< ConsoleColorTag(Console_Normal) << " [y/N]: ";
	std::getline (std::cin, answer);

	boost::algorithm::to_lower(answer);

	choice = answer;

	if (choice.Contains("y"))
		goto wizard_endpoint_loop_start;

	/* Extract parent node information. */
	String parentHost, parentPort;

	for (const String& endpoint : endpoints) {
		std::vector<String> tokens = endpoint.Split(",");

		if (tokens.size() > 1)
			parentHost = tokens[1];

		if (tokens.size() > 2)
			parentPort = tokens[2];
	}

	/* workaround for fetching the master cert */
	String certsDir = ApiListener::GetCertsDir();
	Utility::MkDirP(certsDir, 0700);

	String user = Configuration::RunAsUser;
	String group = Configuration::RunAsGroup;

	if (!Utility::SetFileOwnership(certsDir, user, group)) {
		Log(LogWarning, "cli")
			<< "Cannot set ownership for user '" << user
			<< "' group '" << group
			<< "' on file '" << certsDir << "'. Verify it yourself!";
	}

	String nodeCert = certsDir + "/" + cn + ".crt";
	String nodeKey = certsDir + "/" + cn + ".key";

	if (Utility::PathExists(nodeKey))
		NodeUtility::CreateBackupFile(nodeKey, true);
	if (Utility::PathExists(nodeCert))
		NodeUtility::CreateBackupFile(nodeCert);

	if (PkiUtility::NewCert(cn, nodeKey, Empty, nodeCert) > 0) {
		Log(LogCritical, "cli")
			<< "Failed to create new self-signed certificate for CN '"
			<< cn << "'. Please try again.";
		return 1;
	}

	/* fix permissions: root -> icinga daemon user */
	if (!Utility::SetFileOwnership(nodeKey, user, group)) {
		Log(LogWarning, "cli")
			<< "Cannot set ownership for user '" << user
			<< "' group '" << group
			<< "' on file '" << nodeKey << "'. Verify it yourself!";
	}

	std::shared_ptr<X509> trustedParentCert;

	/* Check whether we should connect to the parent node and present its trusted certificate. */
	if (connectToParent) {
		//save-cert and store the master certificate somewhere
		Log(LogInformation, "cli")
			<< "Fetching public certificate from master ("
			<< parentHost << ", " << parentPort << "):\n";

		trustedParentCert = PkiUtility::FetchCert(parentHost, parentPort);
		if (!trustedParentCert) {
			Log(LogCritical, "cli", "Peer did not present a valid certificate.");
			return 1;
		}

		std::cout << ConsoleColorTag(Console_Bold) << "Parent certificate information:\n"
			<< ConsoleColorTag(Console_Normal) << PkiUtility::GetCertificateInformation(trustedParentCert)
			<< ConsoleColorTag(Console_Bold) << "\nIs this information correct?"
			<< ConsoleColorTag(Console_Normal) << " [y/N]: ";

		std::getline (std::cin, answer);
		boost::algorithm::to_lower(answer);
		if (answer != "y") {
			Log(LogWarning, "cli", "Process aborted.");
			return 1;
		}

		Log(LogInformation, "cli", "Received trusted parent certificate.\n");
	}

wizard_ticket:
	String nodeCA = certsDir + "/ca.crt";
	String ticket;

	/* Check whether we can connect to the parent node and fetch the client and CA certificate. */
	if (connectToParent) {
		std::cout << ConsoleColorTag(Console_Bold)
			<< "\nPlease specify the request ticket generated on your Icinga 2 master "
			<< ConsoleColorTag(Console_Normal) << "(optional)"
			<< ConsoleColorTag(Console_Bold) << "."
			<< ConsoleColorTag(Console_Normal) << "\n"
			<< " (Hint: # icinga2 pki ticket --cn '" << cn << "'): ";

		std::getline(std::cin, answer);

		if (answer.empty()) {
			std::cout << ConsoleColorTag(Console_Bold) << "\n"
				<< "No ticket was specified. Please approve the certificate signing request manually\n"
				<< "on the master (see 'icinga2 ca list' and 'icinga2 ca sign --help' for details)."
				<< ConsoleColorTag(Console_Normal) << "\n";
		}

		ticket = answer;
		ticket = ticket.Trim();

		if (ticket.IsEmpty()) {
			Log(LogInformation, "cli")
				<< "Requesting certificate without a ticket.";
		} else {
			Log(LogInformation, "cli")
				<< "Requesting certificate with ticket '" << ticket << "'.";
		}

		if (Utility::PathExists(nodeCA))
			NodeUtility::CreateBackupFile(nodeCA);
		if (Utility::PathExists(nodeCert))
			NodeUtility::CreateBackupFile(nodeCert);

		if (PkiUtility::RequestCertificate(parentHost, parentPort, nodeKey,
			nodeCert, nodeCA, trustedParentCert, ticket) > 0) {
			Log(LogCritical, "cli")
				<< "Failed to fetch signed certificate from master '"
				<< parentHost << ", "
				<< parentPort << "'. Please try again.";
			goto wizard_ticket;
		}

		/* fix permissions (again) when updating the signed certificate */
		if (!Utility::SetFileOwnership(nodeCert, user, group)) {
			Log(LogWarning, "cli")
				<< "Cannot set ownership for user '" << user
				<< "' group '" << group << "' on file '"
				<< nodeCert << "'. Verify it yourself!";
		}
	} else {
		/* We cannot retrieve the parent certificate.
		 * Tell the user to manually copy the ca.crt file
		 * into DataDir + "/certs"
		 */

		std::cout <<  ConsoleColorTag(Console_Bold)
			<< "\nNo connection to the parent node was specified.\n\n"
			<< "Please copy the public CA certificate from your master/satellite\n"
			<< "into '" << nodeCA << "' before starting Icinga 2.\n"
			<< ConsoleColorTag(Console_Normal);

		if (Utility::PathExists(nodeCA)) {
			std::cout <<  ConsoleColorTag(Console_Bold)
				<< "\nFound public CA certificate in '" << nodeCA << "'.\n"
				<< "Please verify that it is the same as on your master/satellite.\n"
				<< ConsoleColorTag(Console_Normal);
		}

	}

	/* apilistener config */
	std::cout << ConsoleColorTag(Console_Bold)
		<< "Please specify the API bind host/port "
		<< ConsoleColorTag(Console_Normal) << "(optional)"
		<< ConsoleColorTag(Console_Bold) << ":\n";

	std::cout << ConsoleColorTag(Console_Bold)
		<< "Bind Host" << ConsoleColorTag(Console_Normal) << " []: ";

	std::getline(std::cin, answer);

	String bindHost = answer;
	bindHost = bindHost.Trim();

	std::cout << ConsoleColorTag(Console_Bold)
		<< "Bind Port" << ConsoleColorTag(Console_Normal) << " []: ";

	std::getline(std::cin, answer);

	String bindPort = answer;
	bindPort = bindPort.Trim();

	std::cout << ConsoleColorTag(Console_Bold) << "\n"
		<< "Accept config from parent node?" << ConsoleColorTag(Console_Normal)
		<< " [y/N]: ";
	std::getline(std::cin, answer);
	boost::algorithm::to_lower(answer);
	choice = answer;

	String acceptConfig = choice.Contains("y") ? "true" : "false";

	std::cout << ConsoleColorTag(Console_Bold)
		<< "Accept commands from parent node?" << ConsoleColorTag(Console_Normal)
		<< " [y/N]: ";
	std::getline(std::cin, answer);
	boost::algorithm::to_lower(answer);
	choice = answer;

	String acceptCommands = choice.Contains("y") ? "true" : "false";

	std::cout << "\n";

	std::cout << ConsoleColorTag(Console_Bold | Console_ForegroundGreen)
		<< "Reconfiguring Icinga...\n"
		<< ConsoleColorTag(Console_Normal);

	/* disable the notifications feature on agent/satellite nodes */
	Log(LogInformation, "cli", "Disabling the Notification feature.");

	FeatureUtility::DisableFeatures({ "notification" });

	Log(LogInformation, "cli", "Enabling the ApiListener feature.");

	FeatureUtility::EnableFeatures({ "api" });

	String apiConfPath = FeatureUtility::GetFeaturesAvailablePath() + "/api.conf";
	NodeUtility::CreateBackupFile(apiConfPath);

	AtomicFile fp (apiConfPath, 0644);

	fp << "/**\n"
		<< " * The API listener is used for distributed monitoring setups.\n"
		<< " */\n"
		<< "object ApiListener \"api\" {\n"
		<< "  accept_config = " << acceptConfig << "\n"
		<< "  accept_commands = " << acceptCommands << "\n";

	if (!bindHost.IsEmpty())
		fp << "  bind_host = \"" << bindHost << "\"\n";
	if (!bindPort.IsEmpty())
		fp << "  bind_port = " << bindPort << "\n";

	fp << "}\n";

	fp.Commit();

	/* Zones configuration. */
	Log(LogInformation, "cli", "Generating local zones.conf.");

	/* Setup command hardcodes this as FQDN */
	String endpointName = cn;

	/* Different local zone name. */
	std::cout << "\nLocal zone name [" + endpointName + "]: ";
	std::getline(std::cin, answer);

	if (answer.empty())
		answer = endpointName;

	String zoneName = answer;
	zoneName = zoneName.Trim();

	/* Different parent zone name. */
	std::cout << "Parent zone name [master]: ";
	std::getline(std::cin, answer);

	if (answer.empty())
		answer = "master";

	String parentZoneName = answer;
	parentZoneName = parentZoneName.Trim();

	/* Global zones. */
	std::vector<String> globalZones { "global-templates", "director-global" };

	std::cout << "\nDefault global zones: " << boost::algorithm::join(globalZones, " ");
	std::cout << "\nDo you want to specify additional global zones? [y/N]: ";

	std::getline(std::cin, answer);
	boost::algorithm::to_lower(answer);
	choice = answer;

wizard_global_zone_loop_start:
	if (choice.Contains("y")) {
		std::cout << "\nPlease specify the name of the global Zone: ";

		std::getline(std::cin, answer);

		if (answer.empty()) {
			std::cout << "\nName of the global Zone is required! Please retry.";
			goto wizard_global_zone_loop_start;
		}

		String globalZoneName = answer;
		globalZoneName = globalZoneName.Trim();

		if (std::find(globalZones.begin(), globalZones.end(), globalZoneName) != globalZones.end()) {
			std::cout << "The global zone '" << globalZoneName << "' is already specified."
				<< " Please retry.";
			goto wizard_global_zone_loop_start;
		}

		globalZones.push_back(globalZoneName);

		std::cout << "\nDo you want to specify another global zone? [y/N]: ";

		std::getline(std::cin, answer);
		boost::algorithm::to_lower(answer);
		choice = answer;

		if (choice.Contains("y"))
			goto wizard_global_zone_loop_start;
	} else
		Log(LogInformation, "cli", "No additional global Zones have been specified");

	/* Generate node configuration. */
	NodeUtility::GenerateNodeIcingaConfig(endpointName, zoneName, parentZoneName, endpoints, globalZones);

	if (endpointName != Utility::GetFQDN()) {
		Log(LogWarning, "cli")
			<< "CN/Endpoint name '" << endpointName << "' does not match the default FQDN '"
			<< Utility::GetFQDN() << "'. Requires update for NodeName constant in constants.conf!";
	}

	NodeUtility::UpdateConstant("NodeName", endpointName);
	NodeUtility::UpdateConstant("ZoneName", zoneName);

	if (!ticket.IsEmpty()) {
		String ticketPath = ApiListener::GetCertsDir() + "/ticket";
		AtomicFile af (ticketPath, 0600);

		if (!Utility::SetFileOwnership(af.GetTempFilename(), user, group)) {
			Log(LogWarning, "cli")
				<< "Cannot set ownership for user '" << user
				<< "' group '" << group
				<< "' on file '" << ticketPath << "'. Verify it yourself!";
		}

		af << ticket;
		af.Commit();
	}

	/* If no parent connection was made, the user must supply the ca.crt before restarting Icinga 2.*/
	if (!connectToParent) {
		Log(LogWarning, "cli")
			<< "No connection to the parent node was specified.\n\n"
			<< "Please copy the public CA certificate from your master/satellite\n"
			<< "into '" << nodeCA << "' before starting Icinga 2.\n";
	} else {
		Log(LogInformation, "cli", "Make sure to restart Icinga 2.");
	}

	/* Disable conf.d inclusion */
	std::cout << "\nDo you want to disable the inclusion of the conf.d directory [Y/n]: ";

	std::getline(std::cin, answer);
	boost::algorithm::to_lower(answer);
	choice = answer;

	if (choice.Contains("n"))
		Log(LogInformation, "cli")
			<< "conf.d directory has not been disabled.";
	else {
		std::cout << ConsoleColorTag(Console_Bold | Console_ForegroundGreen)
			<< "Disabling the inclusion of the conf.d directory...\n"
			<< ConsoleColorTag(Console_Normal);

		if (!NodeUtility::UpdateConfiguration("\"conf.d\"", false, true)) {
			std::cout << ConsoleColorTag(Console_Bold | Console_ForegroundRed)
				<< "Failed to disable the conf.d inclusion, it may already have been disabled.\n"
				<< ConsoleColorTag(Console_Normal);
		}

		/* Satellite/Agents should not include the api-users.conf file.
		 * The configuration should instead be managed via config sync or automation tools.
		 */
	}

	return 0;
}

int NodeWizardCommand::MasterSetup() const
{
	std::string answer;
	String choice;

	std::cout << ConsoleColorTag(Console_Bold) << "Starting the Master setup routine...\n\n";

	/* CN */
	std::cout << ConsoleColorTag(Console_Bold)
		<< "Please specify the common name" << ConsoleColorTag(Console_Normal)
		<< " (CN) [" << Utility::GetFQDN() << "]: ";

	std::getline(std::cin, answer);

	if (answer.empty())
		answer = Utility::GetFQDN();

	String cn = answer;
	cn = cn.Trim();

	std::cout << ConsoleColorTag(Console_Bold | Console_ForegroundGreen)
		<< "Reconfiguring Icinga...\n"
		<< ConsoleColorTag(Console_Normal);

	/* check whether the user wants to generate a new certificate or not */
	String existing_path = ApiListener::GetCertsDir() + "/" + cn + ".crt";

	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);
	}

	std::cout << ConsoleColorTag(Console_Bold)
		<< "Generating master configuration for Icinga 2.\n"
		<< ConsoleColorTag(Console_Normal);

	ApiSetupUtility::SetupMasterApiUser();

	if (!FeatureUtility::CheckFeatureEnabled("api"))
		ApiSetupUtility::SetupMasterEnableApi();
	else
		std::cout << "'api' feature already enabled.\n";

	/* Setup command hardcodes this as FQDN */
	String endpointName = cn;

	/* Different zone name. */
	std::cout << "\nMaster zone name [master]: ";
	std::getline(std::cin, answer);

	if (answer.empty())
		answer = "master";

	String zoneName = answer;
	zoneName = zoneName.Trim();

	/* Global zones. */
	std::vector<String> globalZones { "global-templates", "director-global" };

	std::cout << "\nDefault global zones: " << boost::algorithm::join(globalZones, " ");
	std::cout << "\nDo you want to specify additional global zones? [y/N]: ";

	std::getline(std::cin, answer);
	boost::algorithm::to_lower(answer);
	choice = answer;

wizard_global_zone_loop_start:
	if (choice.Contains("y")) {
		std::cout << "\nPlease specify the name of the global Zone: ";

		std::getline(std::cin, answer);

		if (answer.empty()) {
			std::cout << "\nName of the global Zone is required! Please retry.";
			goto wizard_global_zone_loop_start;
		}

		String globalZoneName = answer;
		globalZoneName = globalZoneName.Trim();

		if (std::find(globalZones.begin(), globalZones.end(), globalZoneName) != globalZones.end()) {
			std::cout << "The global zone '" << globalZoneName << "' is already specified."
				<< " Please retry.";
			goto wizard_global_zone_loop_start;
		}

		globalZones.push_back(globalZoneName);

		std::cout << "\nDo you want to specify another global zone? [y/N]: ";

		std::getline(std::cin, answer);
		boost::algorithm::to_lower(answer);
		choice = answer;

		if (choice.Contains("y"))
			goto wizard_global_zone_loop_start;
	} else
		Log(LogInformation, "cli", "No additional global Zones have been specified");

	/* Generate master configuration. */
	NodeUtility::GenerateNodeMasterIcingaConfig(endpointName, zoneName, globalZones);

	/* apilistener config */
	std::cout << ConsoleColorTag(Console_Bold)
		<< "Please specify the API bind host/port "
		<< ConsoleColorTag(Console_Normal) << "(optional)"
		<< ConsoleColorTag(Console_Bold) << ":\n";

	std::cout << ConsoleColorTag(Console_Bold)
		<< "Bind Host" << ConsoleColorTag(Console_Normal) << " []: ";

	std::getline(std::cin, answer);

	String bindHost = answer;
	bindHost = bindHost.Trim();

	std::cout << ConsoleColorTag(Console_Bold)
		<< "Bind Port" << ConsoleColorTag(Console_Normal) << " []: ";

	std::getline(std::cin, answer);

	String bindPort = answer;
	bindPort = bindPort.Trim();

	/* api feature is always enabled, check above */
	String apiConfPath = FeatureUtility::GetFeaturesAvailablePath() + "/api.conf";
	NodeUtility::CreateBackupFile(apiConfPath);

	AtomicFile fp (apiConfPath, 0644);

	fp << "/**\n"
		<< " * The API listener is used for distributed monitoring setups.\n"
		<< " */\n"
		<< "object ApiListener \"api\" {\n";

	if (!bindHost.IsEmpty())
		fp << "  bind_host = \"" << bindHost << "\"\n";
	if (!bindPort.IsEmpty())
		fp << "  bind_port = " << bindPort << "\n";

	fp << "\n"
		<< "  ticket_salt = TicketSalt\n"
		<< "}\n";

	fp.Commit();

	/* 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 an update for the NodeName constant in constants.conf!";
	}

	Log(LogInformation, "cli", "Updating constants.conf.");

	NodeUtility::CreateBackupFile(NodeUtility::GetConstantsConfPath());

	NodeUtility::UpdateConstant("NodeName", endpointName);
	NodeUtility::UpdateConstant("ZoneName", zoneName);

	String salt = RandomString(16);

	NodeUtility::UpdateConstant("TicketSalt", salt);

	/* Disable conf.d inclusion */
	std::cout << "\nDo you want to disable the inclusion of the conf.d directory [Y/n]: ";

	std::getline(std::cin, answer);
	boost::algorithm::to_lower(answer);
	choice = answer;

	if (choice.Contains("n"))
		Log(LogInformation, "cli")
			<< "conf.d directory has not been disabled.";
	else {
		std::cout << ConsoleColorTag(Console_Bold | Console_ForegroundGreen)
			<< "Disabling the inclusion of the conf.d directory...\n"
			<< ConsoleColorTag(Console_Normal);

		if (!NodeUtility::UpdateConfiguration("\"conf.d\"", false, true)) {
			std::cout << ConsoleColorTag(Console_Bold | Console_ForegroundRed)
				<< "Failed to disable the conf.d inclusion, it may already have been disabled.\n"
				<< ConsoleColorTag(Console_Normal);
		}

		/* Include api-users.conf */
		String apiUsersFilePath = Configuration::ConfigDir + "/conf.d/api-users.conf";

		std::cout << ConsoleColorTag(Console_Bold | Console_ForegroundGreen)
			<< "Checking if the api-users.conf file exists...\n"
			<< ConsoleColorTag(Console_Normal);

		if (Utility::PathExists(apiUsersFilePath)) {
			NodeUtility::UpdateConfiguration("\"conf.d/api-users.conf\"", true, false);
		} else {
			Log(LogWarning, "cli")
				<< "Included file '" << apiUsersFilePath << "' does not exist.";
		}
	}

	return 0;
}