NGIRCd_Init(): Code cleanup

This commit is contained in:
Alexander Barton 2012-01-03 19:34:54 +01:00
parent 9069380ddf
commit e4006a93e3
1 changed files with 65 additions and 48 deletions

View File

@ -603,7 +603,7 @@ Random_Init(void)
* Initialize ngIRCd daemon.
*
* @param NGIRCd_NoDaemon Set to true if ngIRCd should run in the
* foreground and not as a daemon.
* foreground (and not as a daemon).
* @return true on success.
*/
static bool
@ -623,56 +623,69 @@ NGIRCd_Init( bool NGIRCd_NoDaemon )
/* open /dev/null before chroot() */
fd = open( "/dev/null", O_RDWR);
if (fd < 0)
Log(LOG_WARNING, "Could not open /dev/null: %s", strerror(errno));
Log(LOG_WARNING, "Could not open /dev/null: %s",
strerror(errno));
}
/* SSL initialization */
if (!ConnSSL_InitLibrary())
Log(LOG_WARNING,
"Warning: Error during SSL initialization, continuing ...");
/* Change root */
if (Conf_Chroot[0]) {
if (chdir(Conf_Chroot) != 0) {
Log( LOG_ERR, "Can't chdir() in ChrootDir (%s): %s", Conf_Chroot, strerror( errno ));
Log(LOG_ERR, "Can't chdir() in ChrootDir (%s): %s",
Conf_Chroot, strerror(errno));
goto out;
}
if (chroot(Conf_Chroot) != 0) {
if (errno != EPERM) {
Log( LOG_ERR, "Can't change root directory to \"%s\": %s",
Log(LOG_ERR,
"Can't change root directory to \"%s\": %s",
Conf_Chroot, strerror(errno));
goto out;
}
} else {
chrooted = true;
Log( LOG_INFO, "Changed root and working directory to \"%s\".", Conf_Chroot );
Log(LOG_INFO,
"Changed root and working directory to \"%s\".",
Conf_Chroot);
}
}
/* Check user ID */
if (Conf_UID == 0) {
Log(LOG_INFO, "ServerUID must not be 0, using \"nobody\" instead.", Conf_UID);
Log(LOG_INFO,
"ServerUID must not be 0, using \"nobody\" instead.",
Conf_UID);
if (!NGIRCd_getNobodyID(&Conf_UID, &Conf_GID)) {
Log(LOG_WARNING, "Could not get user/group ID of user \"nobody\": %s",
Log(LOG_WARNING,
"Could not get user/group ID of user \"nobody\": %s",
errno ? strerror(errno) : "not found" );
goto out;
}
}
if (getgid() != Conf_GID) {
/* Change group ID */
if (getgid() != Conf_GID) {
if (setgid(Conf_GID) != 0) {
real_errno = errno;
Log( LOG_ERR, "Can't change group ID to %u: %s", Conf_GID, strerror( errno ));
Log(LOG_ERR, "Can't change group ID to %u: %s",
Conf_GID, strerror(errno));
if (real_errno != EPERM)
goto out;
}
}
if (getuid() != Conf_UID) {
/* Change user ID */
if (getuid() != Conf_UID) {
if (setuid(Conf_UID) != 0) {
real_errno = errno;
Log(LOG_ERR, "Can't change user ID to %u: %s", Conf_UID, strerror(errno));
Log(LOG_ERR, "Can't change user ID to %u: %s",
Conf_UID, strerror(errno));
if (real_errno != EPERM)
goto out;
}
@ -691,7 +704,8 @@ NGIRCd_Init( bool NGIRCd_NoDaemon )
}
if (pid < 0) {
/* Error!? */
fprintf( stderr, "%s: Can't fork: %s!\nFatal error, exiting now ...\n",
fprintf(stderr,
"%s: Can't fork: %s!\nFatal error, exiting now ...\n",
PACKAGE_NAME, strerror(errno));
exit(1);
}
@ -734,20 +748,23 @@ NGIRCd_Init( bool NGIRCd_NoDaemon )
} else
Log(LOG_INFO, "Not running with changed root directory.");
/* Change working directory to home directory of the user
* we are running as (only when running in daemon mode and not in chroot) */
/* Change working directory to home directory of the user we are
* running as (only when running in daemon mode and not in chroot) */
if (NGIRCd_NoDaemon)
return true;
if (pwd) {
if (!NGIRCd_NoDaemon ) {
if (chdir(pwd->pw_dir) == 0)
Log( LOG_DEBUG, "Changed working directory to \"%s\" ...", pwd->pw_dir );
Log(LOG_DEBUG,
"Changed working directory to \"%s\" ...",
pwd->pw_dir);
else
Log( LOG_INFO, "Notice: Can't change working directory to \"%s\": %s",
Log(LOG_INFO,
"Notice: Can't change working directory to \"%s\": %s",
pwd->pw_dir, strerror(errno));
}
} else {
} else
Log(LOG_ERR, "Can't get user informaton for UID %d!?", Conf_UID);
}
return true;
out: