]> arthur.barton.de Git - ngircd-alex.git/blobdiff - src/ngircd/ngircd.c
Enlarged buffer for version string ;-)
[ngircd-alex.git] / src / ngircd / ngircd.c
index 4291c79f9d1a9ca53cb4778ef0923e2f06180358..1166c7b74881ade1349e508fc1cb578b04c534d8 100644 (file)
@@ -1,72 +1,84 @@
 /*
  * ngIRCd -- The Next Generation IRC Daemon
- * Copyright (c)2001,2002 by Alexander Barton (alex@barton.de)
+ * Copyright (c)2001-2003 by Alexander Barton (alex@barton.de)
  *
- * Dieses Programm ist freie Software. Sie koennen es unter den Bedingungen
- * der GNU General Public License (GPL), wie von der Free Software Foundation
- * herausgegeben, weitergeben und/oder modifizieren, entweder unter Version 2
- * der Lizenz oder (wenn Sie es wuenschen) jeder spaeteren Version.
- * Naehere Informationen entnehmen Sie bitter der Datei COPYING. Eine Liste
- * der an ngIRCd beteiligten Autoren finden Sie in der Datei AUTHORS.
+ * 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
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ * Please read the file COPYING, README and AUTHORS for more information.
  *
- * $Id: ngircd.c,v 1.36 2002/03/27 16:41:25 alex Exp $
- *
- * ngircd.c: Hier beginnt alles ;-)
+ * Main program -- main()
  */
 
 
 #include "portab.h"
 
+static char UNUSED id[] = "$Id: ngircd.c,v 1.74 2003/03/07 14:50:13 alex Exp $";
+
 #include "imp.h"
 #include <assert.h>
 #include <errno.h>
 #include <stdio.h>
+#include <stdlib.h>
 #include <signal.h>
 #include <string.h>
 #include <unistd.h>
 #include <sys/types.h>
+#include <sys/stat.h>
 #include <sys/wait.h>
 #include <time.h>
+#include <pwd.h>
+#include <grp.h>
 
-#include "channel.h"
+#include "resolve.h"
+#include "conn.h"
 #include "client.h"
+#include "channel.h"
 #include "conf.h"
-#include "conn.h"
+#include "cvs-version.h"
 #include "defines.h"
-#include "irc.h"
+#include "lists.h"
 #include "log.h"
 #include "parse.h"
+#include "irc.h"
+
+#ifdef RENDEZVOUS
+#include "rendezvous.h"
+#endif
 
 #include "exp.h"
 #include "ngircd.h"
 
 
-LOCAL VOID Initialize_Signal_Handler( VOID );
-LOCAL VOID Signal_Handler( INT Signal );
-
-LOCAL VOID Initialize_Listen_Ports( VOID );
+LOCAL VOID Initialize_Signal_Handler PARAMS(( VOID ));
+LOCAL VOID Signal_Handler PARAMS(( INT Signal ));
 
-LOCAL VOID Show_Version( VOID );
-LOCAL VOID Show_Help( VOID );
+LOCAL VOID Show_Version PARAMS(( VOID ));
+LOCAL VOID Show_Help PARAMS(( VOID ));
 
 
-GLOBAL int main( int argc, const char *argv[] )
+GLOBAL int
+main( int argc, const char *argv[] )
 {
+       struct passwd *pwd;
+       struct group *grp;
        BOOLEAN ok, configtest = FALSE;
-       INT32 pid, n;
+       LONG pid, n;
        INT i;
 
-       NGIRCd_Restart = FALSE;
-       NGIRCd_Quit = FALSE;
-       NGIRCd_NoDaemon = FALSE;
-       NGIRCd_Passive = FALSE;
+       umask( 0077 );
+
+       NGIRCd_SignalQuit = NGIRCd_SignalRestart = NGIRCd_SignalRehash = FALSE;
+       NGIRCd_NoDaemon = NGIRCd_Passive = FALSE;
 #ifdef DEBUG
        NGIRCd_Debug = FALSE;
 #endif
 #ifdef SNIFFER
        NGIRCd_Sniffer = FALSE;
 #endif
-       strcpy( NGIRCd_ConfFile, CONFIG_FILE );
+       strlcpy( NGIRCd_ConfFile, SYSCONFDIR, sizeof( NGIRCd_ConfFile ));
+       strlcat( NGIRCd_ConfFile, CONFIG_FILE, sizeof( NGIRCd_ConfFile ));
 
        /* Kommandozeile parsen */
        for( i = 1; i < argc; i++ )
@@ -76,6 +88,17 @@ GLOBAL int main( int argc, const char *argv[] )
                {
                        /* Lange Option */
 
+                       if( strcmp( argv[i], "--config" ) == 0 )
+                       {
+                               if( i + 1 < argc )
+                               {
+                                       /* Ok, there's an parameter left */
+                                       strlcpy( NGIRCd_ConfFile, argv[i + 1], sizeof( NGIRCd_ConfFile ));
+
+                                       /* next parameter */
+                                       i++; ok = TRUE;
+                               }
+                       }
                        if( strcmp( argv[i], "--configtest" ) == 0 )
                        {
                                configtest = TRUE;
@@ -121,7 +144,7 @@ GLOBAL int main( int argc, const char *argv[] )
                {
                        /* Kurze Option */
                        
-                       for( n = 1; n < (INT32)strlen( argv[i] ); n++ )
+                       for( n = 1; n < (LONG)strlen( argv[i] ); n++ )
                        {
                                ok = FALSE;
 #ifdef DEBUG
@@ -131,6 +154,18 @@ GLOBAL int main( int argc, const char *argv[] )
                                        ok = TRUE;
                                }
 #endif
+                               if( argv[i][n] == 'f' )
+                               {
+                                       if(( ! argv[i][n + 1] ) && ( i + 1 < argc ))
+                                       {
+                                               /* Ok, next character is a blank */
+                                               strlcpy( NGIRCd_ConfFile, argv[i + 1], sizeof( NGIRCd_ConfFile ));
+
+                                               /* go to the following parameter */
+                                               i++; n = (LONG)strlen( argv[i] );
+                                               ok = TRUE;
+                                       }
+                               }
                                if( argv[i][n] == 'n' )
                                {
                                        NGIRCd_NoDaemon = TRUE;
@@ -151,8 +186,8 @@ GLOBAL int main( int argc, const char *argv[] )
 
                                if( ! ok )
                                {
-                                       printf( PACKAGE": invalid option \"-%c\"!\n", argv[i][n] );
-                                       puts( "Try \""PACKAGE" --help\" for more information." );
+                                       printf( "%s: invalid option \"-%c\"!\n", PACKAGE, argv[i][n] );
+                                       printf( "Try \"%s --help\" for more information.\n", PACKAGE );
                                        exit( 1 );
                                }
                        }
@@ -160,8 +195,8 @@ GLOBAL int main( int argc, const char *argv[] )
                }
                if( ! ok )
                {
-                       printf( PACKAGE": invalid option \"%s\"!\n", argv[i] );
-                       puts( "Try \""PACKAGE" --help\" for more information." );
+                       printf( "%s: invalid option \"%s\"!\n", PACKAGE, argv[i] );
+                       printf( "Try \"%s --help\" for more information.\n", PACKAGE );
                        exit( 1 );
                }
        }
@@ -172,7 +207,11 @@ GLOBAL int main( int argc, const char *argv[] )
        if( NGIRCd_Debug ) strcpy( NGIRCd_DebugLevel, "1" );
 #endif
 #ifdef SNIFFER
-       if( NGIRCd_Sniffer ) strcpy( NGIRCd_DebugLevel, "2" );
+       if( NGIRCd_Sniffer )
+       {
+               NGIRCd_Debug = TRUE;
+               strcpy( NGIRCd_DebugLevel, "2" );
+       }
 #endif
 
        /* Soll nur die Konfigurations ueberprueft und ausgegeben werden? */
@@ -182,7 +221,7 @@ GLOBAL int main( int argc, const char *argv[] )
                exit( Conf_Test( ));
        }
        
-       while( ! NGIRCd_Quit )
+       while( ! NGIRCd_SignalQuit )
        {
                /* In der Regel wird ein Sub-Prozess ge-fork()'t, der
                 * nicht mehr mit dem Terminal verbunden ist. Mit der
@@ -191,7 +230,7 @@ GLOBAL int main( int argc, const char *argv[] )
                if( ! NGIRCd_NoDaemon )
                {
                        /* Daemon im Hintergrund erzeugen */
-                       pid = (INT32)fork( );
+                       pid = (LONG)fork( );
                        if( pid > 0 )
                        {
                                /* "alter" Prozess */
@@ -200,7 +239,7 @@ GLOBAL int main( int argc, const char *argv[] )
                        if( pid < 0 )
                        {
                                /* Fehler */
-                               printf( PACKAGE": Can't fork: %s!\nFatal error, exiting now ...", strerror( errno ));
+                               printf( "%s: Can't fork: %s!\nFatal error, exiting now ...\n", PACKAGE, strerror( errno ));
                                exit( 1 );
                        }
 
@@ -212,58 +251,113 @@ GLOBAL int main( int argc, const char *argv[] )
                /* 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_Restart = FALSE;
-               NGIRCd_Quit = FALSE;
+               NGIRCd_SignalRehash = FALSE;
+               NGIRCd_SignalRestart = FALSE;
+               NGIRCd_SignalQuit = FALSE;
 
                /* Module initialisieren */
                Log_Init( );
+               Resolve_Init( );
                Conf_Init( );
+               Lists_Init( );
                Channel_Init( );
                Client_Init( );
+#ifdef RENDEZVOUS
+               Rendezvous_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( );
+
                /* Signal-Handler initialisieren */
                Initialize_Signal_Handler( );
 
-               /* Listen-Ports initialisieren */
-               Initialize_Listen_Ports( );
+               /* Protokoll- und Server-Identifikation erzeugen. Die vom ngIRCd
+                * beim PASS-Befehl verwendete Syntax sowie die erweiterten Flags
+                * sind in doc/Protocol.txt beschrieben. */
+#ifdef IRCPLUS
+               sprintf( NGIRCd_ProtoID, "%s%s %s|%s:%s", PROTOVER, PROTOIRCPLUS, PACKAGE, VERSION, IRCPLUSFLAGS );
+#ifdef USE_ZLIB
+               strcat( NGIRCd_ProtoID, "Z" );
+#endif
+               if( Conf_OperCanMode ) strcat( NGIRCd_ProtoID, "o" );
+#else
+               sprintf( NGIRCd_ProtoID, "%s%s %s|%s", PROTOVER, PROTOIRC, PACKAGE, VERSION );
+#endif
+               strcat( NGIRCd_ProtoID, " P" );
+#ifdef USE_ZLIB
+               strcat( NGIRCd_ProtoID, "Z" );
+#endif
+               Log( LOG_DEBUG, "Protocol and server ID is \"%s\".", NGIRCd_ProtoID );
 
-               /* Hauptschleife */
-               while( TRUE )
+               /* Vordefinierte Channels anlegen */
+               Channel_InitPredefined( );
+
+               /* Listen-Ports initialisieren */
+               if( Conn_InitListeners( ) < 1 )
                {
-                       if( NGIRCd_Quit || NGIRCd_Restart ) break;
-                       Conn_Handler( 5 );
+                       Log( LOG_ALERT, "Server isn't listening on a single port!" );
+                       Log( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE );
+                       exit( 1 );
                }
+               
+               /* Hauptschleife */
+               Conn_Handler( );
 
                /* Alles abmelden */
                Conn_Exit( );
+#ifdef RENDEZVOUS
+               Rendezvous_Exit( );
+#endif
                Client_Exit( );
                Channel_Exit( );
-               Conf_Exit( );
+               Lists_Exit( );
                Log_Exit( );
        }
 
-#ifndef DEBUG
-       /* aufraeumen */
-       if( unlink( ERROR_FILE ) != 0 ) Log( LOG_ERR, "Can't delete \""ERROR_FILE"\": %s", strerror( errno ));
-#endif
-
        return 0;
 } /* main */
 
 
-GLOBAL CHAR *NGIRCd_Version( VOID )
+GLOBAL CHAR *
+NGIRCd_Version( VOID )
 {
        STATIC CHAR version[126];
-
-       sprintf( version, PACKAGE" version "VERSION"-%s", NGIRCd_VersionAddition( ));
+       
+#ifdef CVSDATE
+       sprintf( version, "%s %s(%s)-%s", PACKAGE, VERSION, CVSDATE, NGIRCd_VersionAddition( ));
+#else
+       sprintf( version, "%s %s-%s", PACKAGE, VERSION, NGIRCd_VersionAddition( ));
+#endif
        return version;
 } /* NGIRCd_Version */
 
 
-GLOBAL CHAR *NGIRCd_VersionAddition( VOID )
+GLOBAL CHAR *
+NGIRCd_VersionAddition( VOID )
 {
-       STATIC CHAR txt[64];
+       STATIC CHAR txt[200];
 
        strcpy( txt, "" );
 
@@ -271,9 +365,17 @@ GLOBAL CHAR *NGIRCd_VersionAddition( VOID )
        if( txt[0] ) strcat( txt, "+" );
        strcat( txt, "SYSLOG" );
 #endif
-#ifdef STRICT_RFC
+#ifdef USE_ZLIB
        if( txt[0] ) strcat( txt, "+" );
-       strcat( txt, "RFC" );
+       strcat( txt, "ZLIB" );
+#endif
+#ifdef USE_TCPWRAP
+       if( txt[0] ) strcat( txt, "+" );
+       strcat( txt, "TCPWRAP" );
+#endif
+#ifdef RENDEZVOUS
+       if( txt[0] ) strcat( txt, "+" );
+       strcat( txt, "RENDEZVOUS" );
 #endif
 #ifdef DEBUG
        if( txt[0] ) strcat( txt, "+" );
@@ -283,15 +385,64 @@ GLOBAL CHAR *NGIRCd_VersionAddition( VOID )
        if( txt[0] ) strcat( txt, "+" );
        strcat( txt, "SNIFFER" );
 #endif
-
-       if( txt[0] ) strcat( txt, "-" );
-       strcat( txt, TARGET_CPU"/"TARGET_VENDOR"/"TARGET_OS );
+#ifdef STRICT_RFC
+       if( txt[0] ) strcat( txt, "+" );
+       strcat( txt, "RFC" );
+#endif
+#ifdef IRCPLUS
+       if( txt[0] ) strcat( txt, "+" );
+       strcat( txt, "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 */
 
 
-LOCAL VOID Initialize_Signal_Handler( VOID )
+GLOBAL VOID
+NGIRCd_Rehash( VOID )
+{
+       CHAR old_name[CLIENT_ID_LEN];
+
+       Log( LOG_NOTICE|LOG_snotice, "Re-reading configuration NOW!" );
+       NGIRCd_SignalRehash = FALSE;
+
+       /* Alle Listen-Sockets schliessen */
+       Conn_ExitListeners( );
+
+       /* Alten Server-Namen merken */
+       assert( sizeof( old_name ) == sizeof( Conf_ServerName ));
+       strcpy( old_name, Conf_ServerName );
+
+       /* Konfiguration neu lesen ... */
+       Conf_Rehash( );
+
+       /* Alten Server-Namen wiederherstellen: dieser
+        * kann nicht zur Laufzeit geaendert werden ... */
+       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: */
+       Channel_InitPredefined( );
+       
+       /* Listen-Sockets neu anlegen: */
+       Conn_InitListeners( );
+
+       Log( LOG_NOTICE|LOG_snotice, "Re-reading of configuration done." );
+} /* NGIRCd_Rehash */
+
+
+LOCAL VOID
+Initialize_Signal_Handler( VOID )
 {
        /* Signal-Handler initialisieren: einige Signale
         * werden ignoriert, andere speziell behandelt. */
@@ -337,7 +488,8 @@ LOCAL VOID Initialize_Signal_Handler( VOID )
 } /* Initialize_Signal_Handler */
 
 
-LOCAL VOID Signal_Handler( INT Signal )
+LOCAL VOID
+Signal_Handler( INT Signal )
 {
        /* Signal-Handler. Dieser wird aufgerufen, wenn eines der Signale eintrifft,
         * fuer das wir uns registriert haben (vgl. Initialize_Signal_Handler). Die
@@ -349,72 +501,51 @@ LOCAL VOID Signal_Handler( INT Signal )
                case SIGINT:
                case SIGQUIT:
                        /* wir soll(t)en uns wohl beenden ... */
-                       if( Signal == SIGTERM ) Log( LOG_WARNING, "Got TERM signal, terminating now ..." );
-                       else if( Signal == SIGINT ) Log( LOG_WARNING, "Got INT signal, terminating now ..." );
-                       else if( Signal == SIGQUIT ) Log( LOG_WARNING, "Got QUIT signal, terminating now ..." );
-                       NGIRCd_Quit = TRUE;
+                       NGIRCd_SignalQuit = TRUE;
                        break;
                case SIGHUP:
-                       /* neu starten */
-                       Log( LOG_WARNING, "Got HUP signal, restarting now ..." );
-                       NGIRCd_Restart = TRUE;
+                       /* Konfiguration neu einlesen: */
+                       NGIRCd_SignalRehash = TRUE;
                        break;
                case SIGCHLD:
                        /* Child-Prozess wurde beendet. Zombies vermeiden: */
                        while( waitpid( -1, NULL, WNOHANG ) > 0);
                        break;
+#ifdef DEBUG
                default:
                        /* unbekanntes bzw. unbehandeltes Signal */
-                       Log( LOG_NOTICE, "Got signal %d! Ignored.", Signal );
+                       Log( LOG_DEBUG, "Got signal %d! Ignored.", Signal );
+#endif
        }
 } /* Signal_Handler */
 
 
-LOCAL VOID Initialize_Listen_Ports( VOID )
-{
-       /* Ports, auf denen der Server Verbindungen entgegennehmen
-        * soll, initialisieren */
-       
-       INT created, i;
-
-       created = 0;
-       for( i = 0; i < Conf_ListenPorts_Count; i++ )
-       {
-               if( Conn_NewListener( Conf_ListenPorts[i] )) created++;
-               else Log( LOG_ERR, "Can't listen on port %d!", Conf_ListenPorts[i] );
-       }
-
-       if( created < 1 )
-       {
-               Log( LOG_ALERT, "Server isn't listening on a single port!" );
-               Log( LOG_ALERT, PACKAGE" exiting due to fatal errors!" );
-               exit( 1 );
-       }
-} /* Initialize_Listen_Ports */
-
-
-LOCAL VOID Show_Version( VOID )
+LOCAL VOID
+Show_Version( VOID )
 {
        puts( NGIRCd_Version( ));
-       puts( "Copyright (c)2001,2002 by Alexander Barton (alex@barton.de).\n" );
+       puts( "Copyright (c)2001-2003 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." );
 } /* Show_Version */
 
 
-LOCAL VOID Show_Help( VOID )
+LOCAL VOID
+Show_Help( VOID )
 {
 #ifdef DEBUG
-       puts( "  -d, --debug       log extra debug messages" );
+       puts( "  -d, --debug        log extra debug messages" );
 #endif
-        puts( "  -n, --nodaemon    don't fork and don't detatch from controlling terminal" );
-        puts( "  -p, --passive     disable automatic connections to other servers" );
+       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" );
 #ifdef SNIFFER
-       puts( "  -s, --sniffer     enable network sniffer and display all IRC traffic" );
+       puts( "  -s, --sniffer      enable network sniffer and display all IRC traffic" );
 #endif
-       puts( "      --configtest  read, validate and display configuration; then exit" );
-       puts( "      --version     output version information and exit" );
-       puts( "      --help        display this help and exit" );
+       puts( "      --configtest   read, validate and display configuration; then exit" );
+       puts( "      --version      output version information and exit" );
+       puts( "      --help         display this help and exit" );
 } /* Show_Help */