summaryrefslogtreecommitdiff
path: root/src/inspircd.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/inspircd.cpp')
-rw-r--r--src/inspircd.cpp101
1 files changed, 53 insertions, 48 deletions
diff --git a/src/inspircd.cpp b/src/inspircd.cpp
index 8803a9e54..7dcaeb9b6 100644
--- a/src/inspircd.cpp
+++ b/src/inspircd.cpp
@@ -87,6 +87,56 @@ namespace
delete p;
}
+ // Drops to the unprivileged user/group specified in <security:runas{user,group}>.
+ void DropRoot()
+ {
+#ifndef _WIN32
+ ConfigTag* security = ServerInstance->Config->ConfValue("security");
+
+ const std::string SetGroup = security->getString("runasgroup");
+ if (!SetGroup.empty())
+ {
+ errno = 0;
+ if (setgroups(0, NULL) == -1)
+ {
+ ServerInstance->Logs->Log("STARTUP", LOG_DEFAULT, "setgroups() failed (wtf?): %s", strerror(errno));
+ exit(EXIT_STATUS_CONFIG);
+ }
+
+ struct group* g = getgrnam(SetGroup.c_str());
+ if (!g)
+ {
+ ServerInstance->Logs->Log("STARTUP", LOG_DEFAULT, "getgrnam(%s) failed (wrong group?): %s", SetGroup.c_str(), strerror(errno));
+ exit(EXIT_STATUS_CONFIG);
+ }
+
+ if (setgid(g->gr_gid) == -1)
+ {
+ ServerInstance->Logs->Log("STARTUP", LOG_DEFAULT, "setgid(%d) failed (wrong group?): %s", g->gr_gid, strerror(errno));
+ exit(EXIT_STATUS_CONFIG);
+ }
+ }
+
+ const std::string SetUser = security->getString("runasuser");
+ if (!SetUser.empty())
+ {
+ errno = 0;
+ struct passwd* u = getpwnam(SetUser.c_str());
+ if (!u)
+ {
+ ServerInstance->Logs->Log("STARTUP", LOG_DEFAULT, "getpwnam(%s) failed (wrong user?): %s", SetUser.c_str(), strerror(errno));
+ exit(EXIT_STATUS_CONFIG);
+ }
+
+ if (setuid(u->pw_uid) == -1)
+ {
+ ServerInstance->Logs->Log("STARTUP", LOG_DEFAULT, "setuid(%d) failed (wrong user?): %s", u->pw_uid, strerror(errno));
+ exit(EXIT_STATUS_CONFIG);
+ }
+ }
+#endif
+ }
+
// Seeds the random number generator if applicable.
void SeedRng(timespec ts)
{
@@ -506,55 +556,10 @@ InspIRCd::InspIRCd(int argc, char** argv)
QueryPerformanceFrequency(&stats.QPFrequency);
#endif
- Logs->Log("STARTUP", LOG_DEFAULT, "Startup complete as '%s'[%s], %lu max open sockets", Config->ServerName.c_str(),Config->GetSID().c_str(), SocketEngine::GetMaxFds());
-
-#ifndef _WIN32
- ConfigTag* security = Config->ConfValue("security");
-
- const std::string SetGroup = security->getString("runasgroup");
- if (!SetGroup.empty())
- {
- errno = 0;
- if (setgroups(0, NULL) == -1)
- {
- this->Logs->Log("STARTUP", LOG_DEFAULT, "setgroups() failed (wtf?): %s", strerror(errno));
- exit(EXIT_STATUS_CONFIG);
- }
-
- struct group* g = getgrnam(SetGroup.c_str());
- if (!g)
- {
- this->Logs->Log("STARTUP", LOG_DEFAULT, "getgrnam(%s) failed (wrong group?): %s", SetGroup.c_str(), strerror(errno));
- exit(EXIT_STATUS_CONFIG);
- }
-
- if (setgid(g->gr_gid) == -1)
- {
- this->Logs->Log("STARTUP", LOG_DEFAULT, "setgid(%d) failed (wrong group?): %s", g->gr_gid, strerror(errno));
- exit(EXIT_STATUS_CONFIG);
- }
- }
-
- const std::string SetUser = security->getString("runasuser");
- if (!SetUser.empty())
- {
- errno = 0;
- struct passwd* u = getpwnam(SetUser.c_str());
- if (!u)
- {
- this->Logs->Log("STARTUP", LOG_DEFAULT, "getpwnam(%s) failed (wrong user?): %s", SetUser.c_str(), strerror(errno));
- exit(EXIT_STATUS_CONFIG);
- }
+ WritePID(Config->PID);
+ DropRoot();
- if (setuid(u->pw_uid) == -1)
- {
- this->Logs->Log("STARTUP", LOG_DEFAULT, "setuid(%d) failed (wrong user?): %s", u->pw_uid, strerror(errno));
- exit(EXIT_STATUS_CONFIG);
- }
- }
-
- this->WritePID(Config->PID);
-#endif
+ Logs->Log("STARTUP", LOG_DEFAULT, "Startup complete as '%s'[%s], %lu max open sockets", Config->ServerName.c_str(),Config->GetSID().c_str(), SocketEngine::GetMaxFds());
}
void InspIRCd::UpdateTime()