From: Gunnar Beutner Date: Sun, 15 Sep 2013 08:32:55 +0000 (+0200) Subject: Set group before user. X-Git-Tag: v0.0.3~511 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=aba260e695423dd2133b14340504fdf398711127;p=icinga2 Set group before user. --- diff --git a/icinga-app/icinga.cpp b/icinga-app/icinga.cpp index 734a19d93..90efd84d3 100644 --- a/icinga-app/icinga.cpp +++ b/icinga-app/icinga.cpp @@ -249,40 +249,40 @@ int main(int argc, char **argv) po::notify(g_AppParams); #ifndef _WIN32 - if (g_AppParams.count("user")) { - String user = g_AppParams["user"].as(); + if (g_AppParams.count("group")) { + String group = g_AppParams["group"].as(); errno = 0; - struct passwd *pw = getpwnam(user.CStr()); + struct group *gr = getgrnam(group.CStr()); - if (!pw) { + if (!gr) { BOOST_THROW_EXCEPTION(posix_error() << boost::errinfo_api_function("getpwnam") << boost::errinfo_errno(errno)); } - if (setuid(pw->pw_uid) < 0) { + if (setgid(gr->gr_gid) < 0) { BOOST_THROW_EXCEPTION(posix_error() - << boost::errinfo_api_function("setuid") + << boost::errinfo_api_function("setgid") << boost::errinfo_errno(errno)); } } - if (g_AppParams.count("group")) { - String group = g_AppParams["group"].as(); + if (g_AppParams.count("user")) { + String user = g_AppParams["user"].as(); errno = 0; - struct group *gr = getgrnam(group.CStr()); + struct passwd *pw = getpwnam(user.CStr()); - if (!gr) { + if (!pw) { BOOST_THROW_EXCEPTION(posix_error() << boost::errinfo_api_function("getpwnam") << boost::errinfo_errno(errno)); } - if (setgid(gr->gr_gid) < 0) { + if (setuid(pw->pw_uid) < 0) { BOOST_THROW_EXCEPTION(posix_error() - << boost::errinfo_api_function("setgid") + << boost::errinfo_api_function("setuid") << boost::errinfo_errno(errno)); } }