<< "Dumping program state to file '" << filename << "'";
std::fstream fp;
- String tempFilename = Utility::CreateTempFile(filename + ".XXXXXX", fp);
+ String tempFilename = Utility::CreateTempFile(filename + ".XXXXXX", 0600, fp);
if (!fp)
BOOST_THROW_EXCEPTION(std::runtime_error("Could not open '" + tempFilename + "' file"));
<< "Dumping variables to file '" << filename << "'";
std::fstream fp;
- String tempFilename = Utility::CreateTempFile(filename + ".XXXXXX", fp);
+ String tempFilename = Utility::CreateTempFile(filename + ".XXXXXX", 0600, fp);
if (!fp)
BOOST_THROW_EXCEPTION(std::runtime_error("Could not open '" + tempFilename + "' file"));
}
-void Utility::MkDir(const String& path, int flags)
+void Utility::MkDir(const String& path, int mode)
{
#ifndef _WIN32
- if (mkdir(path.CStr(), flags) < 0 && errno != EEXIST) {
+ if (mkdir(path.CStr(), mode) < 0 && errno != EEXIST) {
#else /*_ WIN32 */
if (mkdir(path.CStr()) < 0 && errno != EEXIST) {
#endif /* _WIN32 */
return JsonDecode(json);
}
-void Utility::SaveJsonFile(const String& path, const Value& value)
+void Utility::SaveJsonFile(const String& path, int mode, const Value& value)
{
std::fstream fp;
- String tempFilename = Utility::CreateTempFile(path + ".XXXXXX", fp);
+ String tempFilename = Utility::CreateTempFile(path + ".XXXXXX", mode, fp);
fp.exceptions(std::ofstream::failbit | std::ofstream::badbit);
fp << JsonEncode(value);
return output;
}
-String Utility::CreateTempFile(const String& path, std::fstream& fp)
+String Utility::CreateTempFile(const String& path, int mode, std::fstream& fp)
{
std::vector<char> targetPath(path.Begin(), path.End());
targetPath.push_back('\0');
close(fd);
- return String(targetPath.begin(), targetPath.end() - 1);
+ String resultPath = String(targetPath.begin(), targetPath.end() - 1);
+
+ if (chmod(resultPath.CStr(), mode) < 0) {
+ BOOST_THROW_EXCEPTION(posix_error()
+ << boost::errinfo_api_function("chmod")
+ << boost::errinfo_errno(errno)
+ << boost::errinfo_file_name(resultPath));
+ }
+
+ return resultPath;
}
#ifdef _WIN32
static void CopyFile(const String& source, const String& target);
static Value LoadJsonFile(const String& path);
- static void SaveJsonFile(const String& path, const Value& value);
+ static void SaveJsonFile(const String& path, int mode, const Value& value);
static String GetPlatformKernel(void);
static String GetPlatformKernelVersion(void);
static String ValidateUTF8(const String& input);
- static String CreateTempFile(const String& path, std::fstream& fp);
+ static String CreateTempFile(const String& path, int mode, std::fstream& fp);
private:
Utility(void);
NodeUtility::CreateBackupFile(apiUsersPath);
std::fstream fp;
- String tempFilename = Utility::CreateTempFile(apiUsersPath + ".XXXXXX", fp);
+ String tempFilename = Utility::CreateTempFile(apiUsersPath + ".XXXXXX", 0640, fp);
fp << "/**\n"
<< " * The APIUser objects are used for authentication against the API.\n"
NodeUtility::CreateBackupFile(apipath);
std::fstream fp;
- String tempApiPath = Utility::CreateTempFile(apipath + ".XXXXXX", fp);
+ String tempApiPath = Utility::CreateTempFile(apipath + ".XXXXXX", 0640, fp);
fp << "/**\n"
<< " * The API listener is used for distributed monitoring setups.\n"
NodeUtility::CreateBackupFile(apipath);
std::fstream fp;
- String tempApiPath = Utility::CreateTempFile(apipath + ".XXXXXX", fp);
+ String tempApiPath = Utility::CreateTempFile(apipath + ".XXXXXX", 0640, fp);
fp << "/**\n"
<< " * The API listener is used for distributed monitoring setups.\n"
/* store the new inventory for next run */
NodeUtility::CreateRepositoryPath();
- Utility::SaveJsonFile(inventory_path, inventory);
+ Utility::SaveJsonFile(inventory_path, 0600, inventory);
std::cout << "Make sure to reload Icinga 2 for these changes to take effect." << std::endl;
node->Set("repository", Empty);
CreateRepositoryPath();
- Utility::SaveJsonFile(path, node);
+ Utility::SaveJsonFile(path, 0600, node);
}
void NodeUtility::AddNodeSettings(const String& name, const String& host,
settings->Set("log_duration", log_duration);
CreateRepositoryPath();
- Utility::SaveJsonFile(GetNodeSettingsFile(name), settings);
+ Utility::SaveJsonFile(GetNodeSettingsFile(name), 0600, settings);
}
void NodeUtility::RemoveNode(const String& name)
}
std::fstream fp;
- String tempFilename = Utility::CreateTempFile(filename + ".XXXXXX", fp);
+ String tempFilename = Utility::CreateTempFile(filename + ".XXXXXX", 0640, fp);
fp << "/*\n";
fp << " * Generated by Icinga 2 node setup commands\n";
String list_path = GetBlackAndWhiteListPath(type);
CreateRepositoryPath();
- Utility::SaveJsonFile(list_path, lists);
+ Utility::SaveJsonFile(list_path, 0600, lists);
return 0;
}
String list_path = GetBlackAndWhiteListPath(type);
CreateRepositoryPath();
- Utility::SaveJsonFile(list_path, lists);
+ Utility::SaveJsonFile(list_path, 0600, lists);
return 0;
}
std::ifstream ifp(constantsFile.CStr());
std::fstream ofp;
- String tempFile = Utility::CreateTempFile(constantsFile + ".XXXXXX", ofp);
+ String tempFile = Utility::CreateTempFile(constantsFile + ".XXXXXX", 0640, ofp);
bool found = false;
NodeUtility::CreateBackupFile(apipath);
std::fstream fp;
- String tempApiPath = Utility::CreateTempFile(apipath + ".XXXXXX", fp);
+ String tempApiPath = Utility::CreateTempFile(apipath + ".XXXXXX", 0640, fp);
fp << "/**\n"
<< " * The API listener is used for distributed monitoring setups.\n"
std::fstream fp;
- String tempApiPath = Utility::CreateTempFile(apipath + ".XXXXXX", fp);
+ String tempApiPath = Utility::CreateTempFile(apipath + ".XXXXXX", 0640, fp);
fp << "/**\n"
<< " * The API listener is used for distributed monitoring setups.\n"
CreateRepositoryPath(Utility::DirName(path));
std::fstream fp;
- String tempFilename = Utility::CreateTempFile(path + ".XXXXXX", fp);
+ String tempFilename = Utility::CreateTempFile(path + ".XXXXXX", 0600, fp);
fp << JsonEncode(item);
fp.close();
CreateRepositoryPath(Utility::DirName(path));
std::fstream fp;
- String tempFilename = Utility::CreateTempFile(path + ".XXXXXX", fp);
+ String tempFilename = Utility::CreateTempFile(path + ".XXXXXX", 0640, fp);
SerializeObject(fp, name, type, item);
fp << std::endl;
String objectsPath = GetObjectsPath();
std::fstream objectfp;
- String tempObjectsPath = Utility::CreateTempFile(objectsPath + ".XXXXXX", objectfp);
+ String tempObjectsPath = Utility::CreateTempFile(objectsPath + ".XXXXXX", 0640, objectfp);
objectfp << std::fixed;
String statusPath = GetStatusPath();
std::fstream statusfp;
- String tempStatusPath = Utility::CreateTempFile(statusPath + ".XXXXXX", statusfp);
+ String tempStatusPath = Utility::CreateTempFile(statusPath + ".XXXXXX", 0640, statusfp);
statusfp << std::fixed;
m_ObjectsPath = filename;
std::fstream *fp = new std::fstream();
- m_ObjectsTempFile = Utility::CreateTempFile(filename + ".XXXXXX", *fp);
+ m_ObjectsTempFile = Utility::CreateTempFile(filename + ".XXXXXX", 0600, *fp);
if (!*fp)
BOOST_THROW_EXCEPTION(std::runtime_error("Could not open '" + m_ObjectsTempFile + "' file"));
String repositoryFile = GetRepositoryDir() + SHA256(params->Get("endpoint")) + ".repo";
std::fstream fp;
- String tempRepositoryFile = Utility::CreateTempFile(repositoryFile + ".XXXXXX", fp);
+ String tempRepositoryFile = Utility::CreateTempFile(repositoryFile + ".XXXXXX", 0640, fp);
fp << JsonEncode(params);
fp.close();
String path = GetModAttrPath();
std::fstream fp;
- String tempFilename = Utility::CreateTempFile(path + ".XXXXXX", fp);
+ String tempFilename = Utility::CreateTempFile(path + ".XXXXXX", 0640, fp);
ConfigObject::Ptr previousObject;
ConfigObject::DumpModifiedAttributes(boost::bind(&PersistModAttrHelper, boost::ref(fp), boost::ref(previousObject), _1, _2, _3));