NGIRCd_Init(): Code cleanup

This commit is contained in:
Alexander Barton 2012-01-03 19:34:54 +01:00
parent 9069380ddf
commit e4006a93e3

View File

@ -603,7 +603,7 @@ Random_Init(void)
* Initialize ngIRCd daemon. * Initialize ngIRCd daemon.
* *
* @param NGIRCd_NoDaemon Set to true if ngIRCd should run in the * @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. * @return true on success.
*/ */
static bool static bool
@ -623,56 +623,69 @@ NGIRCd_Init( bool NGIRCd_NoDaemon )
/* open /dev/null before chroot() */ /* open /dev/null before chroot() */
fd = open( "/dev/null", O_RDWR); fd = open( "/dev/null", O_RDWR);
if (fd < 0) 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()) if (!ConnSSL_InitLibrary())
Log(LOG_WARNING, Log(LOG_WARNING,
"Warning: Error during SSL initialization, continuing ..."); "Warning: Error during SSL initialization, continuing ...");
/* Change root */
if (Conf_Chroot[0]) { if (Conf_Chroot[0]) {
if (chdir(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; goto out;
} }
if (chroot(Conf_Chroot) != 0) { if (chroot(Conf_Chroot) != 0) {
if (errno != EPERM) { 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)); Conf_Chroot, strerror(errno));
goto out; goto out;
} }
} else { } else {
chrooted = true; 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) { 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)) { 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" ); errno ? strerror(errno) : "not found" );
goto out; goto out;
} }
} }
if (getgid() != Conf_GID) {
/* Change group ID */ /* Change group ID */
if (getgid() != Conf_GID) {
if (setgid(Conf_GID) != 0) { if (setgid(Conf_GID) != 0) {
real_errno = errno; 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) if (real_errno != EPERM)
goto out; goto out;
} }
} }
if (getuid() != Conf_UID) {
/* Change user ID */ /* Change user ID */
if (getuid() != Conf_UID) {
if (setuid(Conf_UID) != 0) { if (setuid(Conf_UID) != 0) {
real_errno = errno; 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) if (real_errno != EPERM)
goto out; goto out;
} }
@ -691,7 +704,8 @@ NGIRCd_Init( bool NGIRCd_NoDaemon )
} }
if (pid < 0) { if (pid < 0) {
/* Error!? */ /* 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)); PACKAGE_NAME, strerror(errno));
exit(1); exit(1);
} }
@ -734,20 +748,23 @@ NGIRCd_Init( bool NGIRCd_NoDaemon )
} else } else
Log(LOG_INFO, "Not running with changed root directory."); Log(LOG_INFO, "Not running with changed root directory.");
/* Change working directory to home directory of the user /* Change working directory to home directory of the user we are
* we are running as (only when running in daemon mode and not in chroot) */ * running as (only when running in daemon mode and not in chroot) */
if (NGIRCd_NoDaemon)
return true;
if (pwd) { if (pwd) {
if (!NGIRCd_NoDaemon ) {
if (chdir(pwd->pw_dir) == 0) 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 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)); pwd->pw_dir, strerror(errno));
} } else
} else {
Log(LOG_ERR, "Can't get user informaton for UID %d!?", Conf_UID); Log(LOG_ERR, "Can't get user informaton for UID %d!?", Conf_UID);
}
return true; return true;
out: out: