2001-12-11 22:53:04 +01:00
|
|
|
/*
|
|
|
|
* ngIRCd -- The Next Generation IRC Daemon
|
2002-01-02 03:44:36 +01:00
|
|
|
* Copyright (c)2001,2002 by Alexander Barton (alex@barton.de)
|
2001-12-11 22:53:04 +01:00
|
|
|
*
|
|
|
|
* 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
|
2001-12-31 03:18:51 +01:00
|
|
|
* der an ngIRCd beteiligten Autoren finden Sie in der Datei AUTHORS.
|
2001-12-11 22:53:04 +01:00
|
|
|
*
|
2002-01-11 15:45:18 +01:00
|
|
|
* $Id: ngircd.c,v 1.18 2002/01/11 14:45:18 alex Exp $
|
2001-12-11 22:53:04 +01:00
|
|
|
*
|
|
|
|
* ngircd.c: Hier beginnt alles ;-)
|
|
|
|
*
|
|
|
|
* $Log: ngircd.c,v $
|
2002-01-11 15:45:18 +01:00
|
|
|
* Revision 1.18 2002/01/11 14:45:18 alex
|
|
|
|
* - Kommandozeilen-Parser implementiert: Debug- und No-Daemon-Modus, Hilfe.
|
|
|
|
*
|
2002-01-02 03:51:16 +01:00
|
|
|
* Revision 1.17 2002/01/02 02:51:16 alex
|
|
|
|
* - Signal-Handler fuer SIGCHLD: so sollten Zombies nicht mehr vorkommen.
|
2002-01-02 03:44:36 +01:00
|
|
|
*
|
2001-12-31 03:18:51 +01:00
|
|
|
* Revision 1.15 2001/12/31 02:18:51 alex
|
|
|
|
* - viele neue Befehle (WHOIS, ISON, OPER, DIE, RESTART),
|
|
|
|
* - neuen Header "defines.h" mit (fast) allen Konstanten.
|
|
|
|
* - Code Cleanups und viele "kleine" Aenderungen & Bugfixes.
|
|
|
|
*
|
2001-12-30 20:26:11 +01:00
|
|
|
* Revision 1.14 2001/12/30 19:26:12 alex
|
|
|
|
* - Unterstuetzung fuer die Konfigurationsdatei eingebaut.
|
|
|
|
*
|
2001-12-30 12:42:00 +01:00
|
|
|
* Revision 1.13 2001/12/30 11:42:00 alex
|
|
|
|
* - der Server meldet nun eine ordentliche "Start-Zeit".
|
|
|
|
*
|
2001-12-29 04:07:36 +01:00
|
|
|
* Revision 1.12 2001/12/29 03:07:36 alex
|
|
|
|
* - einige Loglevel geaendert.
|
|
|
|
*
|
2001-12-26 15:45:37 +01:00
|
|
|
* Revision 1.11 2001/12/26 14:45:37 alex
|
|
|
|
* - "Code Cleanups".
|
|
|
|
*
|
2001-12-24 02:34:38 +01:00
|
|
|
* Revision 1.10 2001/12/24 01:34:38 alex
|
|
|
|
* - Signal-Handler aufgeraeumt; u.a. SIGPIPE wird nun korrekt ignoriert.
|
|
|
|
*
|
2001-12-21 23:24:50 +01:00
|
|
|
* Revision 1.9 2001/12/21 22:24:50 alex
|
|
|
|
* - neues Modul "parse" wird initialisiert und abgemeldet.
|
|
|
|
*
|
2001-12-14 09:15:26 +01:00
|
|
|
* Revision 1.8 2001/12/14 08:15:26 alex
|
|
|
|
* - neue Module (irc, client, channel) werden an- und abgemeldet.
|
|
|
|
* - zweiter Listen-Socket wird zu Testzwecken konfiguriert.
|
|
|
|
*
|
2001-12-13 02:31:46 +01:00
|
|
|
* Revision 1.7 2001/12/13 01:31:46 alex
|
|
|
|
* - Conn_Handler() wird nun mit einem Timeout aufgerufen.
|
|
|
|
*
|
2001-12-13 00:30:42 +01:00
|
|
|
* Revision 1.6 2001/12/12 23:30:42 alex
|
|
|
|
* - Log-Meldungen an syslog angepasst.
|
|
|
|
* - NGIRCd_Quit ist nun das Flag zum Beenden des ngircd.
|
|
|
|
*
|
2001-12-12 18:21:21 +01:00
|
|
|
* Revision 1.5 2001/12/12 17:21:21 alex
|
|
|
|
* - mehr Unterfunktionen eingebaut, Modul besser strukturiert & dokumentiert.
|
|
|
|
* - Anpassungen an neue Module.
|
|
|
|
*
|
2001-12-12 02:58:52 +01:00
|
|
|
* Revision 1.4 2001/12/12 01:58:53 alex
|
|
|
|
* - Test auf socklen_t verbessert.
|
|
|
|
*
|
2001-12-12 02:40:39 +01:00
|
|
|
* Revision 1.3 2001/12/12 01:40:39 alex
|
|
|
|
* - ein paar mehr Kommentare; Variablennamen verstaendlicher gemacht.
|
|
|
|
* - fehlenden Header <arpa/inet.h> ergaenz.
|
|
|
|
* - SIGINT und SIGQUIT werden nun ebenfalls behandelt.
|
|
|
|
*
|
2001-12-11 23:04:21 +01:00
|
|
|
* Revision 1.2 2001/12/11 22:04:21 alex
|
|
|
|
* - Test auf stdint.h (HAVE_STDINT_H) hinzugefuegt.
|
|
|
|
*
|
|
|
|
* Revision 1.1.1.1 2001/12/11 21:53:04 alex
|
2001-12-21 23:24:50 +01:00
|
|
|
* - Imported sources to CVS.
|
2001-12-11 22:53:04 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
2001-12-12 02:40:39 +01:00
|
|
|
#define PORTAB_CHECK_TYPES /* Prueffunktion einbinden, s.u. */
|
2001-12-11 22:53:04 +01:00
|
|
|
|
|
|
|
|
|
|
|
#include <portab.h>
|
|
|
|
#include "global.h"
|
|
|
|
|
|
|
|
#include <imp.h>
|
2001-12-11 23:04:21 +01:00
|
|
|
|
2001-12-11 22:53:04 +01:00
|
|
|
#include <assert.h>
|
|
|
|
#include <stdio.h>
|
2001-12-12 18:21:21 +01:00
|
|
|
#include <signal.h>
|
2002-01-11 15:45:18 +01:00
|
|
|
#include <string.h>
|
2002-01-02 03:44:36 +01:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
2001-12-30 12:42:00 +01:00
|
|
|
#include <time.h>
|
2001-12-12 18:21:21 +01:00
|
|
|
|
2001-12-14 09:15:26 +01:00
|
|
|
#include "channel.h"
|
|
|
|
#include "client.h"
|
2001-12-12 18:21:21 +01:00
|
|
|
#include "conf.h"
|
|
|
|
#include "conn.h"
|
2001-12-14 09:15:26 +01:00
|
|
|
#include "irc.h"
|
2001-12-11 22:53:04 +01:00
|
|
|
#include "log.h"
|
2001-12-21 23:24:50 +01:00
|
|
|
#include "parse.h"
|
2001-12-11 22:53:04 +01:00
|
|
|
|
|
|
|
#include <exp.h>
|
|
|
|
#include "ngircd.h"
|
|
|
|
|
|
|
|
|
2001-12-12 18:21:21 +01:00
|
|
|
LOCAL VOID Initialize_Signal_Handler( VOID );
|
2001-12-11 22:53:04 +01:00
|
|
|
LOCAL VOID Signal_Handler( INT Signal );
|
|
|
|
|
2001-12-30 20:26:11 +01:00
|
|
|
LOCAL VOID Initialize_Listen_Ports( VOID );
|
|
|
|
|
2002-01-11 15:45:18 +01:00
|
|
|
LOCAL VOID Show_Version( VOID );
|
|
|
|
LOCAL VOID Show_Help( VOID );
|
|
|
|
|
2001-12-11 22:53:04 +01:00
|
|
|
|
|
|
|
GLOBAL INT main( INT argc, CONST CHAR *argv[] )
|
|
|
|
{
|
2002-01-11 15:45:18 +01:00
|
|
|
BOOLEAN ok;
|
|
|
|
INT i, n;
|
|
|
|
|
2001-12-12 18:21:21 +01:00
|
|
|
/* Datentypen der portab-Library ueberpruefen */
|
2001-12-11 22:53:04 +01:00
|
|
|
portab_check_types( );
|
|
|
|
|
2002-01-11 15:45:18 +01:00
|
|
|
NGIRCd_Restart = FALSE;
|
|
|
|
NGIRCd_Quit = FALSE;
|
|
|
|
NGIRCd_NoDaemon = FALSE;
|
|
|
|
#ifdef DEBUG
|
|
|
|
NGIRCd_Debug = FALSE;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Kommandozeile parsen */
|
|
|
|
for( i = 1; i < argc; i++ )
|
|
|
|
{
|
|
|
|
ok = FALSE;
|
|
|
|
if(( argv[i][0] == '-' ) && ( argv[i][1] == '-' ))
|
|
|
|
{
|
|
|
|
/* Lange Option */
|
|
|
|
|
|
|
|
if( strcmp( argv[i], "--help" ) == 0 )
|
|
|
|
{
|
|
|
|
Show_Version( ); puts( "" );
|
|
|
|
Show_Help( ); puts( "" );
|
|
|
|
exit( 1 );
|
|
|
|
}
|
|
|
|
if( strcmp( argv[i], "--version" ) == 0 )
|
|
|
|
{
|
|
|
|
Show_Version( );
|
|
|
|
exit( 1 );
|
|
|
|
}
|
|
|
|
#ifdef DEBUG
|
|
|
|
if( strcmp( argv[i], "--debug" ) == 0 )
|
|
|
|
{
|
|
|
|
NGIRCd_Debug = TRUE;
|
|
|
|
ok = TRUE;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if( strcmp( argv[i], "--nodaemon" ) == 0 )
|
|
|
|
{
|
|
|
|
NGIRCd_NoDaemon = TRUE;
|
|
|
|
ok = TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(( argv[i][0] == '-' ) && ( argv[i][1] != '-' ))
|
|
|
|
{
|
|
|
|
/* Kurze Option */
|
|
|
|
|
|
|
|
for( n = 1; n < strlen( argv[i] ); n++ )
|
|
|
|
{
|
|
|
|
ok = FALSE;
|
|
|
|
#ifdef DEBUG
|
|
|
|
if( argv[i][n] == 'd' )
|
|
|
|
{
|
|
|
|
NGIRCd_Debug = TRUE;
|
|
|
|
ok = TRUE;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if( argv[i][n] == 'n' )
|
|
|
|
{
|
|
|
|
NGIRCd_NoDaemon = TRUE;
|
|
|
|
ok = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( ! ok )
|
|
|
|
{
|
|
|
|
printf( PACKAGE": invalid option \"-%c\"!\n", argv[i][n] );
|
|
|
|
puts( "Try \""PACKAGE" --help\" for more information." );
|
|
|
|
exit( 1 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
if( ! ok )
|
|
|
|
{
|
|
|
|
printf( PACKAGE": invalid option \"%s\"!\n", argv[i] );
|
|
|
|
puts( "Try \""PACKAGE" --help\" for more information." );
|
|
|
|
exit( 1 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-12-13 00:30:42 +01:00
|
|
|
while( ! NGIRCd_Quit )
|
2001-12-11 22:53:04 +01:00
|
|
|
{
|
2001-12-31 03:18:51 +01:00
|
|
|
/* Globale Variablen initialisieren */
|
|
|
|
NGIRCd_Start = time( NULL );
|
|
|
|
strftime( NGIRCd_StartStr, 64, "%a %b %d %Y at %H:%M:%S (%Z)", localtime( &NGIRCd_Start ));
|
|
|
|
NGIRCd_Restart = FALSE;
|
|
|
|
NGIRCd_Quit = FALSE;
|
|
|
|
|
|
|
|
/* Module initialisieren */
|
|
|
|
Log_Init( );
|
|
|
|
Conf_Init( );
|
|
|
|
Parse_Init( );
|
|
|
|
IRC_Init( );
|
|
|
|
Channel_Init( );
|
|
|
|
Client_Init( );
|
|
|
|
Conn_Init( );
|
|
|
|
|
|
|
|
/* Signal-Handler initialisieren */
|
|
|
|
Initialize_Signal_Handler( );
|
|
|
|
|
|
|
|
/* Listen-Ports initialisieren */
|
|
|
|
Initialize_Listen_Ports( );
|
|
|
|
|
|
|
|
/* Hauptschleife */
|
|
|
|
while( TRUE )
|
|
|
|
{
|
|
|
|
if( NGIRCd_Quit || NGIRCd_Restart ) break;
|
|
|
|
Conn_Handler( 5 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Alles abmelden */
|
|
|
|
Conn_Exit( );
|
|
|
|
Client_Exit( );
|
|
|
|
Channel_Exit( );
|
|
|
|
IRC_Exit( );
|
|
|
|
Parse_Exit( );
|
|
|
|
Conf_Exit( );
|
|
|
|
Log_Exit( );
|
|
|
|
}
|
2001-12-11 22:53:04 +01:00
|
|
|
return 0;
|
|
|
|
} /* main */
|
|
|
|
|
|
|
|
|
2002-01-11 15:45:18 +01:00
|
|
|
GLOBAL CHAR *NGIRCd_Version( VOID )
|
|
|
|
{
|
|
|
|
STATIC CHAR version[126];
|
|
|
|
CHAR txt[64];
|
|
|
|
|
|
|
|
strcpy( txt, "" );
|
|
|
|
|
|
|
|
#ifdef USE_SYSLOG
|
|
|
|
if( txt[0] ) strcat( txt, "+" );
|
|
|
|
else strcat( txt, "-" );
|
|
|
|
strcat( txt, "SYSLOG" );
|
|
|
|
#endif
|
|
|
|
#ifdef STRICT_RFC
|
|
|
|
if( txt[0] ) strcat( txt, "+" );
|
|
|
|
else strcat( txt, "-" );
|
|
|
|
strcat( txt, "RFC" );
|
|
|
|
#endif
|
|
|
|
#ifdef DEBUG
|
|
|
|
if( txt[0] ) strcat( txt, "+" );
|
|
|
|
else strcat( txt, "-" );
|
|
|
|
strcat( txt, "DEBUG" );
|
|
|
|
#endif
|
|
|
|
#ifdef SNIFFER
|
|
|
|
if( txt[0] ) strcat( txt, "+" );
|
|
|
|
else strcat( txt, "-" );
|
|
|
|
strcat( txt, "SNIFFER" );
|
|
|
|
#endif
|
|
|
|
|
|
|
|
sprintf( version, PACKAGE" version "VERSION"%s-"P_OSNAME"/"P_ARCHNAME, txt );
|
|
|
|
return version;
|
|
|
|
} /* NGIRCd_Version */
|
|
|
|
|
|
|
|
|
2001-12-12 18:21:21 +01:00
|
|
|
LOCAL VOID Initialize_Signal_Handler( VOID )
|
|
|
|
{
|
2001-12-26 15:45:37 +01:00
|
|
|
/* Signal-Handler initialisieren: einige Signale
|
|
|
|
* werden ignoriert, andere speziell behandelt. */
|
2001-12-12 18:21:21 +01:00
|
|
|
|
|
|
|
struct sigaction saction;
|
|
|
|
|
|
|
|
/* Signal-Struktur initialisieren */
|
|
|
|
memset( &saction, 0, sizeof( saction ));
|
|
|
|
|
|
|
|
/* Signal-Handler einhaengen */
|
2001-12-24 02:34:38 +01:00
|
|
|
saction.sa_handler = Signal_Handler;
|
2001-12-12 18:21:21 +01:00
|
|
|
sigaction( SIGINT, &saction, NULL );
|
|
|
|
sigaction( SIGQUIT, &saction, NULL );
|
|
|
|
sigaction( SIGTERM, &saction, NULL);
|
2002-01-02 03:44:36 +01:00
|
|
|
sigaction( SIGCHLD, &saction, NULL);
|
2001-12-24 02:34:38 +01:00
|
|
|
|
|
|
|
/* einige Signale ignorieren */
|
|
|
|
saction.sa_handler = SIG_IGN;
|
|
|
|
sigaction( SIGPIPE, &saction, NULL );
|
2001-12-12 18:21:21 +01:00
|
|
|
} /* Initialize_Signal_Handler */
|
|
|
|
|
|
|
|
|
2001-12-11 22:53:04 +01:00
|
|
|
LOCAL VOID Signal_Handler( INT Signal )
|
|
|
|
{
|
2001-12-12 18:21:21 +01:00
|
|
|
/* Signal-Handler. Dieser wird aufgerufen, wenn eines der Signale eintrifft,
|
|
|
|
* fuer das wir uns registriert haben (vgl. Initialize_Signal_Handler). Die
|
|
|
|
* Nummer des eingetroffenen Signals wird der Funktion uebergeben. */
|
|
|
|
|
2001-12-11 22:53:04 +01:00
|
|
|
switch( Signal )
|
|
|
|
{
|
|
|
|
case SIGTERM:
|
2001-12-12 02:40:39 +01:00
|
|
|
case SIGINT:
|
|
|
|
case SIGQUIT:
|
2001-12-12 18:21:21 +01:00
|
|
|
/* wir soll(t)en uns wohl beenden ... */
|
2001-12-29 04:07:36 +01:00
|
|
|
Log( LOG_WARNING, "Got signal %d, terminating now ...", Signal );
|
2001-12-13 00:30:42 +01:00
|
|
|
NGIRCd_Quit = TRUE;
|
2001-12-11 22:53:04 +01:00
|
|
|
break;
|
2002-01-02 03:44:36 +01:00
|
|
|
case SIGCHLD:
|
|
|
|
/* Child-Prozess wurde beendet. Zombies vermeiden: */
|
|
|
|
while( waitpid( -1, NULL, WNOHANG ) > 0);
|
|
|
|
break;
|
2001-12-11 22:53:04 +01:00
|
|
|
default:
|
2001-12-12 18:21:21 +01:00
|
|
|
/* unbekanntes bzw. unbehandeltes Signal */
|
2001-12-13 00:30:42 +01:00
|
|
|
Log( LOG_NOTICE, "Got signal %d! Ignored.", Signal );
|
2001-12-11 22:53:04 +01:00
|
|
|
}
|
|
|
|
} /* Signal_Handler */
|
|
|
|
|
|
|
|
|
2001-12-30 20:26:11 +01:00
|
|
|
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 */
|
|
|
|
|
2002-01-11 15:45:18 +01:00
|
|
|
|
|
|
|
LOCAL VOID Show_Version( VOID )
|
|
|
|
{
|
|
|
|
puts( NGIRCd_Version( ));
|
|
|
|
puts( "Copyright (c)2001,2002 by Alexander Barton (alex@barton.de).\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 )
|
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
|
|
|
puts( " -d, --debug log extra debug messages" );
|
|
|
|
#endif
|
|
|
|
puts( " -n, --nodaemon don't fork and don't detatch from controlling terminal" );
|
|
|
|
puts( " --version display this help and exit" );
|
|
|
|
puts( " --help output version information and exit" );
|
|
|
|
} /* Show_Help */
|
|
|
|
|
|
|
|
|
2001-12-11 22:53:04 +01:00
|
|
|
/* -eof- */
|