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

@ -602,12 +602,12 @@ 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
NGIRCd_Init( bool NGIRCd_NoDaemon ) NGIRCd_Init(bool NGIRCd_NoDaemon)
{ {
static bool initialized; static bool initialized;
bool chrooted = false; bool chrooted = false;
@ -623,57 +623,70 @@ 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 ...");
if( Conf_Chroot[0] ) { /* Change root */
if( chdir( Conf_Chroot ) != 0 ) { if (Conf_Chroot[0]) {
Log( LOG_ERR, "Can't chdir() in ChrootDir (%s): %s", Conf_Chroot, strerror( errno )); if (chdir(Conf_Chroot) != 0) {
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,
Conf_Chroot, strerror( errno )); "Can't change root directory to \"%s\": %s",
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,
errno ? strerror(errno) : "not found" ); "Could not get user/group ID of user \"nobody\": %s",
errno ? strerror(errno) : "not found" );
goto out; goto out;
} }
} }
/* Change group ID */
if (getgid() != Conf_GID) { if (getgid() != Conf_GID) {
/* Change group ID */
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;
} }
} }
/* Change user ID */
if (getuid() != Conf_UID) { if (getuid() != Conf_UID) {
/* Change user ID */
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",
if (real_errno != EPERM) Conf_UID, strerror(errno));
if (real_errno != EPERM)
goto out; goto out;
} }
} }
@ -683,26 +696,27 @@ NGIRCd_Init( bool NGIRCd_NoDaemon )
/* Normally a child process is forked which isn't any longer /* Normally a child process is forked which isn't any longer
* connected to ther controlling terminal. Use "--nodaemon" * connected to ther controlling terminal. Use "--nodaemon"
* to disable this "daemon mode" (useful for debugging). */ * to disable this "daemon mode" (useful for debugging). */
if ( ! NGIRCd_NoDaemon ) { if (!NGIRCd_NoDaemon) {
pid = fork( ); pid = fork();
if( pid > 0 ) { if (pid > 0) {
/* "Old" process: exit. */ /* "Old" process: exit. */
exit( 0 ); exit(0);
} }
if( pid < 0 ) { if (pid < 0) {
/* Error!? */ /* Error!? */
fprintf( stderr, "%s: Can't fork: %s!\nFatal error, exiting now ...\n", fprintf(stderr,
PACKAGE_NAME, strerror( errno )); "%s: Can't fork: %s!\nFatal error, exiting now ...\n",
exit( 1 ); PACKAGE_NAME, strerror(errno));
exit(1);
} }
/* New child process */ /* New child process */
#ifndef NeXT #ifndef NeXT
(void)setsid( ); (void)setsid();
#else #else
setpgrp(0, getpid()); setpgrp(0, getpid());
#endif #endif
if (chdir( "/" ) != 0) if (chdir("/") != 0)
Log(LOG_ERR, "Can't change directory to '/': %s", Log(LOG_ERR, "Can't change directory to '/': %s",
strerror(errno)); strerror(errno));
@ -713,19 +727,19 @@ NGIRCd_Init( bool NGIRCd_NoDaemon )
} }
pid = getpid(); pid = getpid();
Pidfile_Create( pid ); Pidfile_Create(pid);
/* Check UID/GID we are running as, can be different from values /* Check UID/GID we are running as, can be different from values
* configured (e. g. if we were already started with a UID>0. */ * configured (e. g. if we were already started with a UID>0. */
Conf_UID = getuid(); Conf_UID = getuid();
Conf_GID = getgid(); Conf_GID = getgid();
pwd = getpwuid( Conf_UID ); pwd = getpwuid(Conf_UID);
grp = getgrgid( Conf_GID ); grp = getgrgid(Conf_GID);
Log(LOG_INFO, "Running as user %s(%ld), group %s(%ld), with PID %ld.", Log(LOG_INFO, "Running as user %s(%ld), group %s(%ld), with PID %ld.",
pwd ? pwd->pw_name : "unknown", (long)Conf_UID, pwd ? pwd->pw_name : "unknown", (long)Conf_UID,
grp ? grp->gr_name : "unknown", (long)Conf_GID, (long)pid); grp ? grp->gr_name : "unknown", (long)Conf_GID, (long)pid);
if (chrooted) { if (chrooted) {
Log(LOG_INFO, "Running with root directory \"%s\".", Log(LOG_INFO, "Running with root directory \"%s\".",
@ -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,
Log( LOG_DEBUG, "Changed working directory to \"%s\" ...", pwd->pw_dir ); "Changed working directory to \"%s\" ...",
else pwd->pw_dir);
Log( LOG_INFO, "Notice: Can't change working directory to \"%s\": %s", else
pwd->pw_dir, strerror( errno )); Log(LOG_INFO,
} "Notice: Can't change working directory to \"%s\": %s",
} else { pwd->pw_dir, strerror(errno));
Log( LOG_ERR, "Can't get user informaton for UID %d!?", Conf_UID ); } else
} Log(LOG_ERR, "Can't get user informaton for UID %d!?", Conf_UID);
return true; return true;
out: out: