2014-10-06 14:21:18 +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-13 13:43:05 +02:00
|
|
|
#include "cli/pkinewcacommand.hpp"
|
2014-10-06 14:21:18 +02:00
|
|
|
#include "base/logger_fwd.hpp"
|
|
|
|
#include "base/clicommand.hpp"
|
2014-10-13 13:43:05 +02:00
|
|
|
#include "base/application.hpp"
|
|
|
|
#include "base/tlsutility.hpp"
|
|
|
|
#include <fstream>
|
2014-10-06 14:21:18 +02:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2014-10-13 13:43:05 +02:00
|
|
|
REGISTER_CLICOMMAND("pki/new-ca", PKINewCACommand);
|
2014-10-06 14:21:18 +02:00
|
|
|
|
2014-10-13 13:43:05 +02:00
|
|
|
String PKINewCACommand::GetDescription(void) const
|
2014-10-06 14:21:18 +02:00
|
|
|
{
|
|
|
|
return "Sets up a new Certificate Authority.";
|
|
|
|
}
|
|
|
|
|
2014-10-13 13:43:05 +02:00
|
|
|
String PKINewCACommand::GetShortDescription(void) const
|
2014-10-06 14:21:18 +02:00
|
|
|
{
|
|
|
|
return "sets up a new CA";
|
|
|
|
}
|
|
|
|
|
2014-10-13 13:43:05 +02:00
|
|
|
void PKINewCACommand::InitParameters(boost::program_options::options_description& visibleDesc,
|
2014-10-14 16:45:00 +02:00
|
|
|
boost::program_options::options_description& hiddenDesc,
|
|
|
|
ArgumentCompletionDescription& argCompletionDesc) const
|
2014-10-06 14:21:18 +02:00
|
|
|
{
|
|
|
|
/* Command doesn't support any parameters. */
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-10-13 13:43:05 +02:00
|
|
|
* The entry point for the "pki new-ca" CLI command.
|
2014-10-06 14:21:18 +02:00
|
|
|
*
|
|
|
|
* @returns An exit status.
|
|
|
|
*/
|
2014-10-13 18:07:52 +02:00
|
|
|
int PKINewCACommand::Run(const boost::program_options::variables_map& vm, const std::vector<std::string>& ap) const
|
2014-10-06 14:21:18 +02:00
|
|
|
{
|
2014-10-13 13:43:05 +02:00
|
|
|
String cadir = Application::GetLocalStateDir() + "/lib/icinga2/ca";
|
|
|
|
|
|
|
|
if (Utility::PathExists(cadir)) {
|
|
|
|
Log(LogCritical, "base", "CA directory '" + cadir + "' already exists.");
|
|
|
|
return 1;
|
|
|
|
}
|
2014-10-13 18:07:52 +02:00
|
|
|
|
2014-10-13 13:43:05 +02:00
|
|
|
if (!Utility::MkDirP(cadir, 0700)) {
|
|
|
|
Log(LogCritical, "base", "Could not create CA directory '" + cadir + "'.");
|
|
|
|
return 1;
|
|
|
|
}
|
2014-10-13 18:07:52 +02:00
|
|
|
|
2014-10-13 13:58:18 +02:00
|
|
|
MakeX509CSR("Icinga CA", cadir + "/ca.key", String(), cadir + "/ca.crt", true);
|
2014-10-13 18:07:52 +02:00
|
|
|
|
2014-10-13 13:43:05 +02:00
|
|
|
String serialpath = cadir + "/serial.txt";
|
|
|
|
|
|
|
|
Log(LogInformation, "cli", "Initializing serial file in '" + serialpath + "'.");
|
2014-10-13 18:07:52 +02:00
|
|
|
|
2014-10-13 13:43:05 +02:00
|
|
|
std::ofstream fp;
|
|
|
|
fp.open(serialpath.CStr());
|
|
|
|
fp << "01";
|
|
|
|
fp.close();
|
2014-10-06 14:21:18 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|