diff --git a/config.h.cmake b/config.h.cmake index 1903b287c..7529f05ab 100644 --- a/config.h.cmake +++ b/config.h.cmake @@ -20,7 +20,7 @@ #define ICINGA_INCLUDECONFDIR "${CMAKE_INSTALL_FULL_DATADIR}/icinga2/include" #define ICINGA_USER "${ICINGA2_USER}" #define ICINGA_GROUP "${ICINGA2_GROUP}" - +#define ICINGA_SYSCONFIGFILE "${ICINGA2_SYSCONFIGFILE}" #define ICINGA_BUILD_HOST_NAME "${ICINGA2_BUILD_HOST_NAME}" #define ICINGA_BUILD_COMPILER_NAME "${ICINGA2_BUILD_COMPILER_NAME}" #define ICINGA_BUILD_COMPILER_VERSION "${ICINGA2_BUILD_COMPILER_VERSION}" diff --git a/etc/icinga2/init.conf.cmake b/etc/icinga2/init.conf.cmake index 9f57bca82..22406a998 100644 --- a/etc/icinga2/init.conf.cmake +++ b/etc/icinga2/init.conf.cmake @@ -3,5 +3,3 @@ * configuration file (icinga2.conf) is processed. */ -const RunAsUser = "@ICINGA2_USER@" -const RunAsGroup = "@ICINGA2_GROUP@" diff --git a/icinga-app/icinga.cpp b/icinga-app/icinga.cpp index f5665c983..368906876 100644 --- a/icinga-app/icinga.cpp +++ b/icinga-app/icinga.cpp @@ -142,6 +142,7 @@ int Main(void) #endif /* _WIN32 */ Application::DeclarePrefixDir(ICINGA_PREFIX); + Application::DeclareSysconfigFile(ICINGA_SYSCONFIGFILE); Application::DeclareSysconfDir(ICINGA_SYSCONFDIR); Application::DeclareRunDir(ICINGA_RUNDIR); Application::DeclareLocalStateDir(ICINGA_LOCALSTATEDIR); @@ -152,8 +153,17 @@ int Main(void) #endif /* _WIN32 */ Application::DeclareZonesDir(Application::GetSysconfDir() + "/icinga2/zones.d"); - Application::DeclareRunAsUser(ICINGA_USER); - Application::DeclareRunAsGroup(ICINGA_GROUP); + + String icinga_user = Utility::GetFromSysconfig("ICINGA2_USER"); + if (icinga_user.IsEmpty()) + icinga_user = ICINGA_USER; + + String icinga_group = Utility::GetFromSysconfig("ICINGA2_GROUP"); + if (icinga_group.IsEmpty()) + icinga_group = ICINGA_GROUP; + + Application::DeclareRunAsUser(icinga_user); + Application::DeclareRunAsGroup(icinga_group); #ifdef __linux__ Application::DeclareRLimitFiles(Application::GetDefaultRLimitFiles()); Application::DeclareRLimitProcesses(Application::GetDefaultRLimitProcesses()); diff --git a/lib/base/application.cpp b/lib/base/application.cpp index ce4982e2a..70b2c40e9 100644 --- a/lib/base/application.cpp +++ b/lib/base/application.cpp @@ -1286,6 +1286,27 @@ void Application::DeclareStatePath(const String& path) ScriptGlobal::Set("StatePath", path); } +/** + * Retrives the path of the sysconfig file. + * + * @returns The path. + */ +String Application::GetSysconfigFile(void) +{ + return ScriptGlobal::Get("SysconfigFile"); +} + +/** + * Sets the path of the sysconfig file. + * + * @param path The new path. + */ +void Application::DeclareSysconfigFile(const String& path) +{ + if (!ScriptGlobal::Exists("SysconfigFile")) + ScriptGlobal::Set("SysconfigFile", path); +} + /** * Retrieves the path for the modified attributes file. * diff --git a/lib/base/application.hpp b/lib/base/application.hpp index bd04bbd02..05b2e5d91 100644 --- a/lib/base/application.hpp +++ b/lib/base/application.hpp @@ -105,6 +105,9 @@ public: static String GetIncludeConfDir(void); static void DeclareIncludeConfDir(const String& path); + static String GetSysconfigFile(void); + static void DeclareSysconfigFile(const String& path); + static String GetStatePath(void); static void DeclareStatePath(const String& path); diff --git a/lib/base/utility.cpp b/lib/base/utility.cpp index 836393b03..9aaddb8ba 100644 --- a/lib/base/utility.cpp +++ b/lib/base/utility.cpp @@ -1950,3 +1950,35 @@ String Utility::GetIcingaDataPath(void) } #endif /* _WIN32 */ + +String Utility::GetFromSysconfig(const String& env) +{ +#ifndef _WIN32 + String sysconf = Application::GetSysconfigFile(); + if (sysconf.IsEmpty()) + return ""; + + String cmdInner = ". " + EscapeShellArg(sysconf) + " 2>&1 >/dev/null;echo \"$" + env + "\""; + String cmd = "sh -c " + EscapeShellArg(cmdInner); + + FILE *fp = popen(cmd.CStr(), "r"); + + if (!fp) + return ""; + + char line[1024]; + String out; + + if (fgets(line, sizeof(line), fp)) + out = line; + else + return ""; + + pclose(fp); + + return out.Trim(); +#else + //TODO: Figure out how to do this on windows + return ""; +#endif /* _WIN32 */ +} diff --git a/lib/base/utility.hpp b/lib/base/utility.hpp index 3e19301f0..08c1f1999 100644 --- a/lib/base/utility.hpp +++ b/lib/base/utility.hpp @@ -148,6 +148,8 @@ public: static String GetIcingaDataPath(void); #endif /* _WIN32 */ + static String GetFromSysconfig(const String& env); + #ifdef I2_DEBUG static void SetTime(double); static void IncrementTime(double);