/*
* ngIRCd -- The Next Generation IRC Daemon
- * Copyright (c)2001-2003 by Alexander Barton (alex@barton.de)
+ * Copyright (c)2001-2005 by Alexander Barton (alex@barton.de)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
#include "portab.h"
-static char UNUSED id[] = "$Id: ngircd.c,v 1.80 2003/12/29 14:53:26 alex Exp $";
+static char UNUSED id[] = "$Id: ngircd.c,v 1.94 2005/02/11 13:52:37 alex Exp $";
#include "imp.h"
#include <assert.h>
#include <signal.h>
#include <string.h>
#include <unistd.h>
+#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
-#include <time.h>
+#include <fcntl.h>
#include <pwd.h>
#include <grp.h>
+#include "defines.h"
#include "resolve.h"
#include "conn.h"
#include "client.h"
#include "channel.h"
#include "conf.h"
#include "cvs-version.h"
-#include "defines.h"
#include "lists.h"
#include "log.h"
#include "parse.h"
LOCAL VOID Show_Version PARAMS(( VOID ));
LOCAL VOID Show_Help PARAMS(( VOID ));
+LOCAL VOID Pidfile_Create PARAMS(( LONG ));
+LOCAL VOID Pidfile_Delete PARAMS(( VOID ));
+
+LOCAL VOID Fill_Version PARAMS(( VOID ));
+
+LOCAL VOID Setup_FDStreams PARAMS(( VOID ));
+
GLOBAL int
main( int argc, const char *argv[] )
strlcpy( NGIRCd_ConfFile, SYSCONFDIR, sizeof( NGIRCd_ConfFile ));
strlcat( NGIRCd_ConfFile, CONFIG_FILE, sizeof( NGIRCd_ConfFile ));
+ Fill_Version( );
+
/* Kommandozeile parsen */
for( i = 1; i < argc; i++ )
{
}
/* Debug-Level (fuer IRC-Befehl "VERSION") ermitteln */
- strcpy( NGIRCd_DebugLevel, "" );
+ NGIRCd_DebugLevel[0] = '\0';
#ifdef DEBUG
if( NGIRCd_Debug ) strcpy( NGIRCd_DebugLevel, "1" );
#endif
while( ! NGIRCd_SignalQuit )
{
- /* In der Regel wird ein Sub-Prozess ge-fork()'t, der
- * nicht mehr mit dem Terminal verbunden ist. Mit der
- * Option "--nodaemon" kann dies (z.B. zum Debuggen)
- * verhindert werden. */
+ /* Initialize global variables */
+ NGIRCd_Start = time( NULL );
+ (VOID)strftime( NGIRCd_StartStr, 64, "%a %b %d %Y at %H:%M:%S (%Z)", localtime( &NGIRCd_Start ));
+
+ NGIRCd_SignalRehash = FALSE;
+ NGIRCd_SignalRestart = FALSE;
+ NGIRCd_SignalQuit = FALSE;
+
+ /* Initialize modules, part I */
+ Log_Init( );
+ Conf_Init( );
+
+ if( Conf_Chroot[0] )
+ {
+ /* Chroot */
+ if( chdir( Conf_Chroot ) != 0 ) Log( LOG_ERR, "Can't chdir() in ChrootDir (%s): %s", Conf_Chroot, strerror( errno ));
+
+ if( chroot( Conf_Chroot ) != 0 ) Log( LOG_ERR, "Can't change root directory to \"%s\": %s", Conf_Chroot, strerror( errno ));
+ else Log( LOG_INFO, "Changed root and working directory to \"%s\".", Conf_Chroot );
+ }
+
+ if( Conf_GID != 0 )
+ {
+ /* Set new group ID */
+ if( setgid( Conf_GID ) != 0 ) Log( LOG_ERR, "Can't change group ID to %u: %s", Conf_GID, strerror( errno ));
+ }
+ if( Conf_UID != 0 )
+ {
+ /* Set new user ID */
+ if( setuid( Conf_UID ) != 0 ) Log( LOG_ERR, "Can't change user ID to %u: %s", Conf_UID, strerror( errno ));
+ }
+
+ /* Normally a child process is forked which isn't any longer
+ * connected to ther controlling terminal. Use "--nodaemon"
+ * to disable this "daemon mode" (useful for debugging). */
if( ! NGIRCd_NoDaemon )
{
- /* Daemon im Hintergrund erzeugen */
+ /* fork child process */
pid = (LONG)fork( );
if( pid > 0 )
{
- /* "alter" Prozess */
+ /* "Old" process: exit. */
exit( 0 );
}
if( pid < 0 )
{
- /* Fehler */
- printf( "%s: Can't fork: %s!\nFatal error, exiting now ...\n", PACKAGE_NAME, strerror( errno ));
+ /* Error!? */
+ fprintf( stderr, "%s: Can't fork: %s!\nFatal error, exiting now ...\n", PACKAGE_NAME, strerror( errno ));
exit( 1 );
}
- /* Child-Prozess initialisieren */
+ /* New child process */
(VOID)setsid( );
chdir( "/" );
+
+ /* Detach stdin, stdout and stderr */
+ (VOID)Setup_FDStreams( );
}
-
- /* Globale Variablen initialisieren */
- NGIRCd_Start = time( NULL );
- (VOID)strftime( NGIRCd_StartStr, 64, "%a %b %d %Y at %H:%M:%S (%Z)", localtime( &NGIRCd_Start ));
- NGIRCd_SignalRehash = FALSE;
- NGIRCd_SignalRestart = FALSE;
- NGIRCd_SignalQuit = FALSE;
- /* Module initialisieren */
- Log_Init( );
+ /* Create PID file */
+ pid = (LONG) getpid( );
+ Pidfile_Create( pid );
+
+ /* Show user, group, and PID of the running daemon */
+ pwd = getpwuid( getuid( )); grp = getgrgid( getgid( ));
+ Log( LOG_INFO, "Running as user %s(%ld), group %s(%ld), with PID %ld.", pwd ? pwd->pw_name : "unknown", (LONG)getuid( ), grp ? grp->gr_name : "unknown", (LONG)getgid( ), pid);
+
+ /* Change working directory to home directory of the user
+ * we are running as (when not running chroot()'ed!) */
+ if( Conf_UID != 0 && ! Conf_Chroot[0] )
+ {
+ struct passwd *pwd;
+
+ pwd = getpwuid( Conf_UID );
+ if( pwd != NULL )
+ {
+ if( chdir( pwd->pw_dir ) == 0 ) Log( LOG_DEBUG, "Changed working directory to \"%s\" ...", pwd->pw_dir );
+ else Log( LOG_ERR, "Can't change working directory to \"%s\": %s", pwd->pw_dir, strerror( errno ));
+ }
+ else Log( LOG_ERR, "Can't get user informaton for UID %d!?", Conf_UID );
+ }
+
+ /* Initialize modules, part II: these functions are eventually
+ * called with already dropped privileges ... */
Resolve_Init( );
- Conf_Init( );
Lists_Init( );
Channel_Init( );
Client_Init( );
#endif
Conn_Init( );
- /* Wenn als root ausgefuehrt und eine andere UID
- * konfiguriert ist, jetzt zu dieser wechseln */
- if( getuid( ) == 0 )
- {
- if( Conf_GID != 0 )
- {
- /* Neue Group-ID setzen */
- if( setgid( Conf_GID ) != 0 ) Log( LOG_ERR, "Can't change Group-ID to %u: %s", Conf_GID, strerror( errno ));
- }
- if( Conf_UID != 0 )
- {
- /* Neue User-ID setzen */
- if( setuid( Conf_UID ) != 0 ) Log( LOG_ERR, "Can't change User-ID to %u: %s", Conf_UID, strerror( errno ));
- }
- }
-
- /* User, Gruppe und Prozess-ID des Daemon ausgeben */
- pwd = getpwuid( getuid( )); grp = getgrgid( getgid( ));
- Log( LOG_INFO, "Running as user %s(%ld), group %s(%ld), with PID %ld.", pwd ? pwd->pw_name : "unknown", (LONG)getuid( ), grp ? grp->gr_name : "unknown", (LONG)getgid( ), (LONG)getpid( ));
-
- /* stderr in "Error-File" umlenken */
- Log_InitErrorfile( );
+#ifdef DEBUG
+ /* Redirect stderr handle to "error file" for debugging
+ * when not running in "no daemon" mode: */
+ if( ! NGIRCd_NoDaemon ) Log_InitErrorfile( );
+#endif
/* Signal-Handler initialisieren */
Initialize_Signal_Handler( );
{
Log( LOG_ALERT, "Server isn't listening on a single port!" );
Log( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
+ Pidfile_Delete( );
exit( 1 );
}
Channel_Exit( );
Lists_Exit( );
Log_Exit( );
+
+ Pidfile_Delete( );
}
return 0;
} /* main */
-GLOBAL CHAR *
-NGIRCd_Version( VOID )
-{
- STATIC CHAR version[126];
-
-#ifdef CVSDATE
- sprintf( version, "%s %s(%s)-%s", PACKAGE_NAME, PACKAGE_VERSION, CVSDATE, NGIRCd_VersionAddition( ));
-#else
- sprintf( version, "%s %s-%s", PACKAGE_NAME, PACKAGE_VERSION, NGIRCd_VersionAddition( ));
-#endif
- return version;
-} /* NGIRCd_Version */
-
-
-GLOBAL CHAR *
-NGIRCd_VersionAddition( VOID )
+LOCAL VOID
+Fill_Version( VOID )
{
- STATIC CHAR txt[200];
-
- strcpy( txt, "" );
+ NGIRCd_VersionAddition[0] = '\0';
#ifdef SYSLOG
- if( txt[0] ) strcat( txt, "+" );
- strcat( txt, "SYSLOG" );
+ strcpy( NGIRCd_VersionAddition, "SYSLOG" );
#endif
#ifdef ZLIB
- if( txt[0] ) strcat( txt, "+" );
- strcat( txt, "ZLIB" );
+ if( NGIRCd_VersionAddition[0] ) strcat( NGIRCd_VersionAddition, "+" );
+ strcat( NGIRCd_VersionAddition, "ZLIB" );
#endif
#ifdef TCPWRAP
- if( txt[0] ) strcat( txt, "+" );
- strcat( txt, "TCPWRAP" );
+ if( NGIRCd_VersionAddition[0] ) strcat( NGIRCd_VersionAddition, "+" );
+ strcat( NGIRCd_VersionAddition, "TCPWRAP" );
#endif
#ifdef RENDEZVOUS
- if( txt[0] ) strcat( txt, "+" );
- strcat( txt, "RENDEZVOUS" );
+ if( NGIRCd_VersionAddition[0] ) strcat( NGIRCd_VersionAddition, "+" );
+ strcat( NGIRCd_VersionAddition, "RENDEZVOUS" );
#endif
#ifdef IDENTAUTH
- if( txt[0] ) strcat( txt, "+" );
- strcat( txt, "IDENT" );
+ if( NGIRCd_VersionAddition[0] ) strcat( NGIRCd_VersionAddition, "+" );
+ strcat( NGIRCd_VersionAddition, "IDENT" );
#endif
#ifdef DEBUG
- if( txt[0] ) strcat( txt, "+" );
- strcat( txt, "DEBUG" );
+ if( NGIRCd_VersionAddition[0] ) strcat( NGIRCd_VersionAddition, "+" );
+ strcat( NGIRCd_VersionAddition, "DEBUG" );
#endif
#ifdef SNIFFER
- if( txt[0] ) strcat( txt, "+" );
- strcat( txt, "SNIFFER" );
+ if( NGIRCd_VersionAddition[0] ) strcat( NGIRCd_VersionAddition, "+" );
+ strcat( NGIRCd_VersionAddition, "SNIFFER" );
#endif
#ifdef STRICT_RFC
- if( txt[0] ) strcat( txt, "+" );
- strcat( txt, "RFC" );
+ if( NGIRCd_VersionAddition[0] ) strcat( NGIRCd_VersionAddition, "+" );
+ strcat( NGIRCd_VersionAddition, "RFC" );
#endif
#ifdef IRCPLUS
- if( txt[0] ) strcat( txt, "+" );
- strcat( txt, "IRCPLUS" );
+ if( NGIRCd_VersionAddition[0] ) strcat( NGIRCd_VersionAddition, "+" );
+ strcat( NGIRCd_VersionAddition, "IRCPLUS" );
#endif
-
- if( txt[0] ) strlcat( txt, "-", sizeof( txt ));
- strlcat( txt, TARGET_CPU, sizeof( txt ));
- strlcat( txt, "/", sizeof( txt ));
- strlcat( txt, TARGET_VENDOR, sizeof( txt ));
- strlcat( txt, "/", sizeof( txt ));
- strlcat( txt, TARGET_OS, sizeof( txt ));
- return txt;
-} /* NGIRCd_VersionAddition */
+ if( NGIRCd_VersionAddition[0] ) strlcat( NGIRCd_VersionAddition, "-", sizeof( NGIRCd_VersionAddition ));
+ strlcat( NGIRCd_VersionAddition, TARGET_CPU, sizeof( NGIRCd_VersionAddition ));
+ strlcat( NGIRCd_VersionAddition, "/", sizeof( NGIRCd_VersionAddition ));
+ strlcat( NGIRCd_VersionAddition, TARGET_VENDOR, sizeof( NGIRCd_VersionAddition ));
+ strlcat( NGIRCd_VersionAddition, "/", sizeof( NGIRCd_VersionAddition ));
+ strlcat( NGIRCd_VersionAddition, TARGET_OS, sizeof( NGIRCd_VersionAddition ));
+
+#ifdef CVSDATE
+ snprintf( NGIRCd_Version, sizeof NGIRCd_Version,"%s %s(%s)-%s", PACKAGE_NAME, PACKAGE_VERSION, CVSDATE, NGIRCd_VersionAddition);
+#else
+ snprintf( NGIRCd_Version, sizeof NGIRCd_Version, "%s %s-%s", PACKAGE_NAME, PACKAGE_VERSION, NGIRCd_VersionAddition);
+#endif
+} /* Fill_Version */
GLOBAL VOID
Log( LOG_NOTICE|LOG_snotice, "Re-reading configuration NOW!" );
NGIRCd_SignalRehash = FALSE;
- /* Alle Listen-Sockets schliessen */
+ /* Close down all listening sockets */
Conn_ExitListeners( );
- /* Alten Server-Namen merken */
-#ifdef DEBUG
- assert( sizeof( old_name ) == sizeof( Conf_ServerName ));
-#endif
+ /* Remember old server name */
strcpy( old_name, Conf_ServerName );
- /* Konfiguration neu lesen ... */
+ /* Re-read configuration ... */
Conf_Rehash( );
- /* Alten Server-Namen wiederherstellen: dieser
- * kann nicht zur Laufzeit geaendert werden ... */
+ /* Recover old server name: it can't be changed during run-time */
if( strcmp( old_name, Conf_ServerName ) != 0 )
{
strcpy( Conf_ServerName, old_name );
Log( LOG_ERR, "Can't change \"ServerName\" on runtime! Ignored new name." );
}
- /* neue pre-defined Channel anlegen: */
+ /* Create new pre-defined channels */
Channel_InitPredefined( );
- /* Listen-Sockets neu anlegen: */
+ /* Start listening on sockets */
Conn_InitListeners( );
/* Sync configuration with established connections */
LOCAL VOID
Show_Version( VOID )
{
- puts( NGIRCd_Version( ));
- puts( "Copyright (c)2001-2003 by Alexander Barton (<alex@barton.de>)." );
+ puts( NGIRCd_Version );
+ puts( "Copyright (c)2001-2005 by Alexander Barton (<alex@barton.de>)." );
puts( "Homepage: <http://arthur.ath.cx/~alex/ngircd/>\n" );
puts( "This is free software; see the source for copying conditions. There is NO" );
puts( "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE." );
puts( " -d, --debug log extra debug messages" );
#endif
puts( " -f, --config <f> use file <f> as configuration file" );
- puts( " -n, --nodaemon don't fork and don't detach from controlling terminal" );
- puts( " -p, --passive disable automatic connections to other servers" );
+ puts( " -n, --nodaemon don't fork and don't detach from controlling terminal" );
+ puts( " -p, --passive disable automatic connections to other servers" );
#ifdef SNIFFER
puts( " -s, --sniffer enable network sniffer and display all IRC traffic" );
#endif
} /* Show_Help */
+LOCAL VOID
+Pidfile_Delete( VOID )
+{
+ /* Pidfile configured? */
+ if( ! Conf_PidFile[0] ) return;
+
+#ifdef DEBUG
+ Log( LOG_DEBUG, "Removing PID file (%s) ...", Conf_PidFile );
+#endif
+
+ if( unlink( Conf_PidFile ))
+ Log( LOG_ERR, "Error unlinking PID file (%s): %s", Conf_PidFile, strerror( errno ));
+} /* Pidfile_Delete */
+
+
+LOCAL VOID
+Pidfile_Create( LONG pid )
+{
+ FILE *pidf;
+
+ /* Pidfile configured? */
+ if( ! Conf_PidFile[0] ) return;
+
+#ifdef DEBUG
+ Log( LOG_DEBUG, "Creating PID file (%s) ...", Conf_PidFile );
+#endif
+
+ pidf = fopen( Conf_PidFile, "w" );
+
+ if( ! pidf )
+ {
+ Log( LOG_ERR, "Error writing PID file (%s): %s", Conf_PidFile, strerror( errno ));
+ return;
+ }
+
+ if( fprintf( pidf, "%ld\n", pid ) < 0 )
+ Log( LOG_ERR, "Can't write PID file (%s): %s", Conf_PidFile, strerror( errno ));
+
+ if( fclose(pidf) != 0 )
+ Log( LOG_ERR, "Error closing PID file (%s): %s", Conf_PidFile, strerror( errno ));
+} /* Pidfile_Create */
+
+
+LOCAL VOID
+Setup_FDStreams( VOID )
+{
+ INT fd;
+
+ /* Test if we can open /dev/null for reading and writing. If not
+ * we are most probably chrooted already and the server has been
+ * restarted. So we simply don't try to redirect stdXXX ... */
+ fd = open( "/dev/null", O_RDWR );
+ if ( fd < 0 ) return;
+
+ /* Close "old" stdin/out/err descriptors */
+ close( 0 ); close( 1 ); close( 2 );
+
+ /* Create new stdin(0), stdout(1) and stderr(2) descriptors */
+ dup2( fd, 0 ); dup2( fd, 1 ); dup2( fd, 2 );
+
+ /* Close newly opened file descriptor if not stdin/out/err */
+ if( fd > 2 ) close( fd );
+} /* Setup_FDStreams */
+
+
/* -eof- */