diff --git a/lib/base/utility.cpp b/lib/base/utility.cpp index a080b20aa..3bf58bf33 100644 --- a/lib/base/utility.cpp +++ b/lib/base/utility.cpp @@ -599,7 +599,7 @@ bool Utility::GlobRecursive(const String& path, const String& pattern, const boo } -bool Utility::MkDir(const String& path, int flags) +void Utility::MkDir(const String& path, int flags) { #ifndef _WIN32 if (mkdir(path.CStr(), flags) < 0 && errno != EEXIST) { @@ -610,22 +610,16 @@ bool Utility::MkDir(const String& path, int flags) << boost::errinfo_api_function("mkdir") << boost::errinfo_errno(errno)); } - - return true; } -bool Utility::MkDirP(const String& path, int flags) +void Utility::MkDirP(const String& path, int flags) { size_t pos = 0; - bool ret = true; - - while (ret && pos != String::NPos) { + while (pos != String::NPos) { pos = path.Find("/", pos + 1); - ret = MkDir(path.SubStr(0, pos), flags); + MkDir(path.SubStr(0, pos), flags); } - - return ret; } void Utility::RemoveDirRecursive(const String& path) diff --git a/lib/base/utility.hpp b/lib/base/utility.hpp index c1ceb273c..3ca6a4ad5 100644 --- a/lib/base/utility.hpp +++ b/lib/base/utility.hpp @@ -81,8 +81,8 @@ public: static bool Glob(const String& pathSpec, const boost::function& callback, int type = GlobFile | GlobDirectory); static bool GlobRecursive(const String& path, const String& pattern, const boost::function& callback, int type = GlobFile | GlobDirectory); - static bool MkDir(const String& path, int flags); - static bool MkDirP(const String& path, int flags); + static void MkDir(const String& path, int flags); + static void MkDirP(const String& path, int flags); static bool SetFileOwnership(const String& file, const String& user, const String& group); static void QueueAsyncCallback(const boost::function& callback, SchedulerPolicy policy = DefaultScheduler); diff --git a/lib/cli/apisetuputility.cpp b/lib/cli/apisetuputility.cpp index ef641a526..ca8649c3c 100644 --- a/lib/cli/apisetuputility.cpp +++ b/lib/cli/apisetuputility.cpp @@ -72,12 +72,7 @@ int ApiSetupUtility::SetupMasterCertificates(const String& cn) } String pki_path = PkiUtility::GetPkiPath(); - - if (!Utility::MkDirP(pki_path, 0700)) { - Log(LogCritical, "cli") - << "Could not create local pki directory '" << pki_path << "'."; - return 1; - } + Utility::MkDirP(pki_path, 0700); String user = ScriptGlobal::Get("RunAsUser"); String group = ScriptGlobal::Get("RunAsGroup"); diff --git a/lib/cli/nodesetupcommand.cpp b/lib/cli/nodesetupcommand.cpp index 9d6616baa..807f6fb2d 100644 --- a/lib/cli/nodesetupcommand.cpp +++ b/lib/cli/nodesetupcommand.cpp @@ -309,16 +309,7 @@ int NodeSetupCommand::SetupNode(const boost::program_options::variables_map& vm, /* pki request a signed certificate from the master */ String pki_path = PkiUtility::GetPkiPath(); - - String key = pki_path + "/" + cn + ".key"; - String cert = pki_path + "/" + cn + ".crt"; - String ca = pki_path + "/ca.crt"; - - if (!Utility::MkDirP(pki_path, 0700)) { - Log(LogCritical, "cli") - << "Could not create local pki directory '" << pki_path << "'."; - return 1; - } + Utility::MkDirP(pki_path, 0700); String user = ScriptGlobal::Get("RunAsUser"); String group = ScriptGlobal::Get("RunAsGroup"); @@ -328,6 +319,10 @@ int NodeSetupCommand::SetupNode(const boost::program_options::variables_map& vm, << "Cannot set ownership for user '" << user << "' group '" << group << "' on file '" << pki_path << "'. Verify it yourself!"; } + String key = pki_path + "/" + cn + ".key"; + String cert = pki_path + "/" + cn + ".crt"; + String ca = pki_path + "/ca.crt"; + if (Utility::PathExists(key)) NodeUtility::CreateBackupFile(key, true); if (Utility::PathExists(cert)) diff --git a/lib/cli/nodewizardcommand.cpp b/lib/cli/nodewizardcommand.cpp index db3b2194f..917d88576 100644 --- a/lib/cli/nodewizardcommand.cpp +++ b/lib/cli/nodewizardcommand.cpp @@ -243,14 +243,7 @@ wizard_master_host: /* workaround for fetching the master cert */ String pki_path = PkiUtility::GetPkiPath(); - String node_cert = pki_path + "/" + cn + ".crt"; - String node_key = pki_path + "/" + cn + ".key"; - - if (!Utility::MkDirP(pki_path, 0700)) { - Log(LogCritical, "cli") - << "Could not create local pki directory '" << pki_path << "'."; - return 1; - } + Utility::MkDirP(pki_path, 0700); String user = ScriptGlobal::Get("RunAsUser"); String group = ScriptGlobal::Get("RunAsGroup"); @@ -260,6 +253,9 @@ wizard_master_host: << "Cannot set ownership for user '" << user << "' group '" << group << "' on file '" << pki_path << "'. Verify it yourself!"; } + String node_cert = pki_path + "/" + cn + ".crt"; + String node_key = pki_path + "/" + cn + ".key"; + if (Utility::PathExists(node_key)) NodeUtility::CreateBackupFile(node_key, true); if (Utility::PathExists(node_cert)) diff --git a/lib/cli/pkiutility.cpp b/lib/cli/pkiutility.cpp index 5c49006d7..d40cdc2da 100644 --- a/lib/cli/pkiutility.cpp +++ b/lib/cli/pkiutility.cpp @@ -53,11 +53,7 @@ int PkiUtility::NewCa(void) return 1; } - if (!Utility::MkDirP(cadir, 0700)) { - Log(LogCritical, "base") - << "Could not create CA directory '" << cadir << "'."; - return 1; - } + Utility::MkDirP(cadir, 0700); MakeX509CSR("Icinga CA", cadir + "/ca.key", String(), cadir + "/ca.crt", cadir + "/serial.txt", true); diff --git a/lib/remote/apilistener-sync.cpp b/lib/remote/apilistener-sync.cpp index 95edd3ff7..7a998be48 100644 --- a/lib/remote/apilistener-sync.cpp +++ b/lib/remote/apilistener-sync.cpp @@ -137,15 +137,7 @@ void ApiListener::SyncZoneDir(const Zone::Ptr& zone) const Log(LogInformation, "ApiListener") << "Copying zone configuration files for zone '" << zone->GetName() << "' to '" << oldDir << "'."; - if (!Utility::MkDir(oldDir, 0700)) { - Log(LogCritical, "ApiListener") - << "mkdir() for path '" << oldDir << "' failed with error code " << errno << ", \"" << Utility::FormatErrorNumber(errno) << "\""; - - BOOST_THROW_EXCEPTION(posix_error() - << boost::errinfo_api_function("mkdir") - << boost::errinfo_errno(errno) - << boost::errinfo_file_name(oldDir)); - } + Utility::MkDir(oldDir, 0700); Dictionary::Ptr oldConfig = LoadConfigDir(oldDir); @@ -254,15 +246,7 @@ Value ApiListener::ConfigUpdateHandler(const MessageOrigin::Ptr& origin, const D String oldDir = Application::GetLocalStateDir() + "/lib/icinga2/api/zones/" + zone->GetName(); - if (!Utility::MkDir(oldDir, 0700)) { - Log(LogCritical, "ApiListener") - << "mkdir() for path '" << oldDir << "' failed with error code " << errno << ", \"" << Utility::FormatErrorNumber(errno) << "\""; - - BOOST_THROW_EXCEPTION(posix_error() - << boost::errinfo_api_function("mkdir") - << boost::errinfo_errno(errno) - << boost::errinfo_file_name(oldDir)); - } + Utility::MkDir(oldDir, 0700); Dictionary::Ptr newConfig = kv.second; Dictionary::Ptr oldConfig = LoadConfigDir(oldDir);