]> arthur.barton.de Git - ngircd-alex.git/blobdiff - src/ngircd/ngircd.c
New "chroot" feature (from Benjamin Pineau), introducing new configuration
[ngircd-alex.git] / src / ngircd / ngircd.c
index 046ff959857295634ad163df308f3b7449ba74f6..8ced6e3f8903c8698e2aa505c8c558a15798b539 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * ngIRCd -- The Next Generation IRC Daemon
- * Copyright (c)2001-2003 by Alexander Barton (alex@barton.de)
+ * Copyright (c)2001-2004 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
@@ -14,7 +14,7 @@
 
 #include "portab.h"
 
-static char UNUSED id[] = "$Id: ngircd.c,v 1.76 2003/03/31 15:54:21 alex Exp $";
+static char UNUSED id[] = "$Id: ngircd.c,v 1.84 2004/05/07 11:19:21 alex Exp $";
 
 #include "imp.h"
 #include <assert.h>
@@ -24,10 +24,10 @@ static char UNUSED id[] = "$Id: ngircd.c,v 1.76 2003/03/31 15:54:21 alex Exp $";
 #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 <pwd.h>
 #include <grp.h>
 
@@ -183,6 +183,11 @@ main( int argc, const char *argv[] )
                                        ok = TRUE;
                                }
 #endif
+                               if( argv[i][n] == 't' )
+                               {
+                                       configtest = TRUE;
+                                       ok = TRUE;
+                               }
 
                                if( ! ok )
                                {
@@ -223,6 +228,38 @@ main( int argc, const char *argv[] )
        
        while( ! NGIRCd_SignalQuit )
        {
+               /* 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 ));
+               }
+
                /* 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)
@@ -247,18 +284,10 @@ main( int argc, const char *argv[] )
                        (VOID)setsid( );
                        chdir( "/" );
                }
-       
-               /* 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( );
+               /* Initialize modules, part II: these functions are eventually
+                * called with already dropped privileges ... */
                Resolve_Init( );
-               Conf_Init( );
                Lists_Init( );
                Channel_Init( );
                Client_Init( );
@@ -267,28 +296,15 @@ main( int argc, const char *argv[] )
 #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 */
+               /* 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( ), (LONG)getpid( ));
 
-               /* stderr in "Error-File" umlenken */
-               Log_InitErrorfile( );
+               /* Redirect stderr handle to "error file" for debugging.
+                * But don't try to write in the chroot jail, since it's more 
+                * secure to have a chroot dir not writable by the daemon.
+                */
+               if( ! Conf_Chroot[0] ) Log_InitErrorfile( );
 
                /* Signal-Handler initialisieren */
                Initialize_Signal_Handler( );
@@ -298,7 +314,7 @@ main( int argc, const char *argv[] )
                 * sind in doc/Protocol.txt beschrieben. */
 #ifdef IRCPLUS
                sprintf( NGIRCd_ProtoID, "%s%s %s|%s:%s", PROTOVER, PROTOIRCPLUS, PACKAGE_NAME, PACKAGE_VERSION, IRCPLUSFLAGS );
-#ifdef USE_ZLIB
+#ifdef ZLIB
                strcat( NGIRCd_ProtoID, "Z" );
 #endif
                if( Conf_OperCanMode ) strcat( NGIRCd_ProtoID, "o" );
@@ -306,7 +322,7 @@ main( int argc, const char *argv[] )
                sprintf( NGIRCd_ProtoID, "%s%s %s|%s", PROTOVER, PROTOIRC, PACKAGE_NAME, PACKAGE_VERSION );
 #endif
                strcat( NGIRCd_ProtoID, " P" );
-#ifdef USE_ZLIB
+#ifdef ZLIB
                strcat( NGIRCd_ProtoID, "Z" );
 #endif
                Log( LOG_DEBUG, "Protocol and server ID is \"%s\".", NGIRCd_ProtoID );
@@ -361,15 +377,15 @@ NGIRCd_VersionAddition( VOID )
 
        strcpy( txt, "" );
 
-#ifdef USE_SYSLOG
+#ifdef SYSLOG
        if( txt[0] ) strcat( txt, "+" );
        strcat( txt, "SYSLOG" );
 #endif
-#ifdef USE_ZLIB
+#ifdef ZLIB
        if( txt[0] ) strcat( txt, "+" );
        strcat( txt, "ZLIB" );
 #endif
-#ifdef USE_TCPWRAP
+#ifdef TCPWRAP
        if( txt[0] ) strcat( txt, "+" );
        strcat( txt, "TCPWRAP" );
 #endif
@@ -377,6 +393,10 @@ NGIRCd_VersionAddition( VOID )
        if( txt[0] ) strcat( txt, "+" );
        strcat( txt, "RENDEZVOUS" );
 #endif
+#ifdef IDENTAUTH
+       if( txt[0] ) strcat( txt, "+" );
+       strcat( txt, "IDENT" );
+#endif
 #ifdef DEBUG
        if( txt[0] ) strcat( txt, "+" );
        strcat( txt, "DEBUG" );
@@ -413,28 +433,26 @@ NGIRCd_Rehash( 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 */
-       assert( sizeof( old_name ) == sizeof( Conf_ServerName ));
+       /* 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 */
@@ -527,7 +545,7 @@ LOCAL VOID
 Show_Version( VOID )
 {
        puts( NGIRCd_Version( ));
-       puts( "Copyright (c)2001-2003 by Alexander Barton (<alex@barton.de>)." );
+       puts( "Copyright (c)2001-2004 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." );
@@ -546,7 +564,7 @@ Show_Help( VOID )
 #ifdef SNIFFER
        puts( "  -s, --sniffer      enable network sniffer and display all IRC traffic" );
 #endif
-       puts( "      --configtest   read, validate and display configuration; then exit" );
+       puts( "  -t, --configtest   read, validate and display configuration; then exit" );
        puts( "      --version      output version information and exit" );
        puts( "      --help         display this help and exit" );
 } /* Show_Help */