Blob


1 /*
2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001,2002 by Alexander Barton (alex@barton.de)
4 *
5 * Dieses Programm ist freie Software. Sie koennen es unter den Bedingungen
6 * der GNU General Public License (GPL), wie von der Free Software Foundation
7 * herausgegeben, weitergeben und/oder modifizieren, entweder unter Version 2
8 * der Lizenz oder (wenn Sie es wuenschen) jeder spaeteren Version.
9 * Naehere Informationen entnehmen Sie bitter der Datei COPYING. Eine Liste
10 * der an ngIRCd beteiligten Autoren finden Sie in der Datei AUTHORS.
11 *
12 * $Id: ngircd.c,v 1.54 2002/09/07 17:57:17 alex Exp $
13 *
14 * ngircd.c: Hier beginnt alles ;-)
15 */
18 #include "portab.h"
20 #include "imp.h"
21 #include <assert.h>
22 #include <errno.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <signal.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <sys/wait.h>
31 #include <time.h>
33 #include "resolve.h"
34 #include "conn.h"
35 #include "client.h"
36 #include "channel.h"
37 #include "conf.h"
38 #include "defines.h"
39 #include "lists.h"
40 #include "log.h"
41 #include "parse.h"
42 #include "irc.h"
44 #include "exp.h"
45 #include "ngircd.h"
48 LOCAL VOID Initialize_Signal_Handler PARAMS(( VOID ));
49 LOCAL VOID Signal_Handler PARAMS(( INT Signal ));
51 LOCAL VOID Initialize_Listen_Ports PARAMS(( VOID ));
53 LOCAL VOID Show_Version PARAMS(( VOID ));
54 LOCAL VOID Show_Help PARAMS(( VOID ));
57 GLOBAL int
58 main( int argc, const char *argv[] )
59 {
60 BOOLEAN ok, configtest = FALSE;
61 INT32 pid, n;
62 INT i;
64 umask( 0077 );
66 NGIRCd_Restart = FALSE;
67 NGIRCd_Quit = FALSE;
68 NGIRCd_NoDaemon = FALSE;
69 NGIRCd_Passive = FALSE;
70 #ifdef DEBUG
71 NGIRCd_Debug = FALSE;
72 #endif
73 #ifdef SNIFFER
74 NGIRCd_Sniffer = FALSE;
75 #endif
76 strcpy( NGIRCd_ConfFile, CONFIG_FILE );
78 /* Kommandozeile parsen */
79 for( i = 1; i < argc; i++ )
80 {
81 ok = FALSE;
82 if(( argv[i][0] == '-' ) && ( argv[i][1] == '-' ))
83 {
84 /* Lange Option */
86 if( strcmp( argv[i], "--config" ) == 0 )
87 {
88 if( i + 1 < argc )
89 {
90 /* Ok, danach kommt noch ein Parameter */
91 strncpy( NGIRCd_ConfFile, argv[i + 1], FNAME_LEN - 1 );
92 NGIRCd_ConfFile[FNAME_LEN - 1] = '\0';
94 /* zum uebernaechsten Parameter */
95 i++; ok = TRUE;
96 }
97 }
98 if( strcmp( argv[i], "--configtest" ) == 0 )
99 {
100 configtest = TRUE;
101 ok = TRUE;
103 #ifdef DEBUG
104 if( strcmp( argv[i], "--debug" ) == 0 )
106 NGIRCd_Debug = TRUE;
107 ok = TRUE;
109 #endif
110 if( strcmp( argv[i], "--help" ) == 0 )
112 Show_Version( );
113 puts( "" ); Show_Help( ); puts( "" );
114 exit( 1 );
116 if( strcmp( argv[i], "--nodaemon" ) == 0 )
118 NGIRCd_NoDaemon = TRUE;
119 ok = TRUE;
121 if( strcmp( argv[i], "--passive" ) == 0 )
123 NGIRCd_Passive = TRUE;
124 ok = TRUE;
126 #ifdef SNIFFER
127 if( strcmp( argv[i], "--sniffer" ) == 0 )
129 NGIRCd_Sniffer = TRUE;
130 ok = TRUE;
132 #endif
133 if( strcmp( argv[i], "--version" ) == 0 )
135 Show_Version( );
136 exit( 1 );
139 else if(( argv[i][0] == '-' ) && ( argv[i][1] != '-' ))
141 /* Kurze Option */
143 for( n = 1; n < (INT32)strlen( argv[i] ); n++ )
145 ok = FALSE;
146 #ifdef DEBUG
147 if( argv[i][n] == 'd' )
149 NGIRCd_Debug = TRUE;
150 ok = TRUE;
152 #endif
153 if( argv[i][n] == 'f' )
155 if(( ! argv[i][n + 1] ) && ( i + 1 < argc ))
157 /* Ok, danach kommt ein Leerzeichen */
158 strncpy( NGIRCd_ConfFile, argv[i + 1], FNAME_LEN - 1 );
159 NGIRCd_ConfFile[FNAME_LEN - 1] = '\0';
161 /* zum uebernaechsten Parameter */
162 i++; n = strlen( argv[i] );
163 ok = TRUE;
166 if( argv[i][n] == 'n' )
168 NGIRCd_NoDaemon = TRUE;
169 ok = TRUE;
171 if( argv[i][n] == 'p' )
173 NGIRCd_Passive = TRUE;
174 ok = TRUE;
176 #ifdef SNIFFER
177 if( argv[i][n] == 's' )
179 NGIRCd_Sniffer = TRUE;
180 ok = TRUE;
182 #endif
184 if( ! ok )
186 printf( "%s: invalid option \"-%c\"!\n", PACKAGE, argv[i][n] );
187 printf( "Try \"%s --help\" for more information.\n", PACKAGE );
188 exit( 1 );
193 if( ! ok )
195 printf( "%s: invalid option \"%s\"!\n", PACKAGE, argv[i] );
196 printf( "Try \"%s --help\" for more information.\n", PACKAGE );
197 exit( 1 );
201 /* Debug-Level (fuer IRC-Befehl "VERSION") ermitteln */
202 strcpy( NGIRCd_DebugLevel, "" );
203 #ifdef DEBUG
204 if( NGIRCd_Debug ) strcpy( NGIRCd_DebugLevel, "1" );
205 #endif
206 #ifdef SNIFFER
207 if( NGIRCd_Sniffer )
209 NGIRCd_Debug = TRUE;
210 strcpy( NGIRCd_DebugLevel, "2" );
212 #endif
214 /* Soll nur die Konfigurations ueberprueft und ausgegeben werden? */
215 if( configtest )
217 Show_Version( ); puts( "" );
218 exit( Conf_Test( ));
221 while( ! NGIRCd_Quit )
223 /* In der Regel wird ein Sub-Prozess ge-fork()'t, der
224 * nicht mehr mit dem Terminal verbunden ist. Mit der
225 * Option "--nodaemon" kann dies (z.B. zum Debuggen)
226 * verhindert werden. */
227 if( ! NGIRCd_NoDaemon )
229 /* Daemon im Hintergrund erzeugen */
230 pid = (INT32)fork( );
231 if( pid > 0 )
233 /* "alter" Prozess */
234 exit( 0 );
236 if( pid < 0 )
238 /* Fehler */
239 printf( "%s: Can't fork: %s!\nFatal error, exiting now ...\n", PACKAGE, strerror( errno ));
240 exit( 1 );
243 /* Child-Prozess initialisieren */
244 (VOID)setsid( );
245 chdir( "/" );
248 /* Globale Variablen initialisieren */
249 NGIRCd_Start = time( NULL );
250 (VOID)strftime( NGIRCd_StartStr, 64, "%a %b %d %Y at %H:%M:%S (%Z)", localtime( &NGIRCd_Start ));
251 NGIRCd_Restart = FALSE;
252 NGIRCd_Quit = FALSE;
254 /* Module initialisieren */
255 Log_Init( );
256 Resolve_Init( );
257 Conf_Init( );
258 Lists_Init( );
259 Channel_Init( );
260 Client_Init( );
261 Conn_Init( );
263 /* Wenn als root ausgefuehrt und eine andere UID
264 * konfiguriert ist, jetzt zu dieser wechseln */
265 if( getuid( ) == 0 )
267 if( Conf_GID != 0 )
269 /* Neue Group-ID setzen */
270 if( setgid( Conf_GID ) != 0 ) Log( LOG_ERR, "Can't change Group-ID to %u: %s", Conf_GID, strerror( errno ));
272 if( Conf_UID != 0 )
274 /* Neue User-ID setzen */
275 if( setuid( Conf_UID ) != 0 ) Log( LOG_ERR, "Can't change User-ID to %u: %s", Conf_UID, strerror( errno ));
278 Log( LOG_INFO, "Running as user %ld, group %ld, with PID %ld.", (INT32)getuid( ), (INT32)getgid( ), (INT32)getpid( ));
280 Log_InitErrorfile( );
282 /* Signal-Handler initialisieren */
283 Initialize_Signal_Handler( );
285 /* Protokoll- und Server-Identifikation erzeugen. Die vom ngIRCd
286 * beim PASS-Befehl verwendete Syntax sowie die erweiterten Flags
287 * sind in doc/Protocol.txt beschrieben. */
288 #ifdef IRCPLUS
289 sprintf( NGIRCd_ProtoID, "%s%s %s|%s:%s", PROTOVER, PROTOIRCPLUS, PACKAGE, VERSION, IRCPLUSFLAGS );
290 if( Conf_OperCanMode ) strcat( NGIRCd_ProtoID, "o" );
291 #else
292 sprintf( NGIRCd_ProtoID, "%s%s %s|%s", PROTOVER, PROTOIRC, PACKAGE, VERSION );
293 #endif
294 strcat( NGIRCd_ProtoID, " P" );
295 Log( LOG_DEBUG, "Protocol and server ID is \"%s\".", NGIRCd_ProtoID );
297 /* Vordefinierte Channels anlegen */
298 Channel_InitPredefined( );
300 /* Listen-Ports initialisieren */
301 Initialize_Listen_Ports( );
303 /* Hauptschleife */
304 Conn_Handler( );
306 /* Alles abmelden */
307 Conn_Exit( );
308 Client_Exit( );
309 Channel_Exit( );
310 Lists_Exit( );
311 Log_Exit( );
314 return 0;
315 } /* main */
318 GLOBAL CHAR *
319 NGIRCd_Version( VOID )
321 STATIC CHAR version[126];
323 sprintf( version, "%s %s-%s", PACKAGE, VERSION, NGIRCd_VersionAddition( ));
324 return version;
325 } /* NGIRCd_Version */
328 GLOBAL CHAR *
329 NGIRCd_VersionAddition( VOID )
331 STATIC CHAR txt[64];
333 strcpy( txt, "" );
335 #ifdef USE_SYSLOG
336 if( txt[0] ) strcat( txt, "+" );
337 strcat( txt, "SYSLOG" );
338 #endif
339 #ifdef DEBUG
340 if( txt[0] ) strcat( txt, "+" );
341 strcat( txt, "DEBUG" );
342 #endif
343 #ifdef SNIFFER
344 if( txt[0] ) strcat( txt, "+" );
345 strcat( txt, "SNIFFER" );
346 #endif
347 #ifdef STRICT_RFC
348 if( txt[0] ) strcat( txt, "+" );
349 strcat( txt, "RFC" );
350 #endif
351 #ifdef IRCPLUS
352 if( txt[0] ) strcat( txt, "+" );
353 strcat( txt, "IRCPLUS" );
354 #endif
356 if( txt[0] ) strcat( txt, "-" );
357 strcat( txt, TARGET_CPU );
358 strcat( txt, "/" );
359 strcat( txt, TARGET_VENDOR );
360 strcat( txt, "/" );
361 strcat( txt, TARGET_OS );
363 return txt;
364 } /* NGIRCd_VersionAddition */
367 LOCAL VOID
368 Initialize_Signal_Handler( VOID )
370 /* Signal-Handler initialisieren: einige Signale
371 * werden ignoriert, andere speziell behandelt. */
373 #ifdef HAVE_SIGACTION
374 /* sigaction() ist vorhanden */
376 struct sigaction saction;
378 /* Signal-Struktur initialisieren */
379 memset( &saction, 0, sizeof( saction ));
380 saction.sa_handler = Signal_Handler;
381 #ifdef SA_RESTART
382 saction.sa_flags |= SA_RESTART;
383 #endif
384 #ifdef SA_NOCLDWAIT
385 saction.sa_flags |= SA_NOCLDWAIT;
386 #endif
388 /* Signal-Handler einhaengen */
389 sigaction( SIGINT, &saction, NULL );
390 sigaction( SIGQUIT, &saction, NULL );
391 sigaction( SIGTERM, &saction, NULL);
392 sigaction( SIGHUP, &saction, NULL);
393 sigaction( SIGCHLD, &saction, NULL);
395 /* einige Signale ignorieren */
396 saction.sa_handler = SIG_IGN;
397 sigaction( SIGPIPE, &saction, NULL );
398 #else
399 /* kein sigaction() vorhanden */
401 /* Signal-Handler einhaengen */
402 signal( SIGINT, Signal_Handler );
403 signal( SIGQUIT, Signal_Handler );
404 signal( SIGTERM, Signal_Handler );
405 signal( SIGHUP, Signal_Handler );
406 signal( SIGCHLD, Signal_Handler );
408 /* einige Signale ignorieren */
409 signal( SIGPIPE, SIG_IGN );
410 #endif
411 } /* Initialize_Signal_Handler */
414 LOCAL VOID
415 Signal_Handler( INT Signal )
417 /* Signal-Handler. Dieser wird aufgerufen, wenn eines der Signale eintrifft,
418 * fuer das wir uns registriert haben (vgl. Initialize_Signal_Handler). Die
419 * Nummer des eingetroffenen Signals wird der Funktion uebergeben. */
421 switch( Signal )
423 case SIGTERM:
424 case SIGINT:
425 case SIGQUIT:
426 /* wir soll(t)en uns wohl beenden ... */
427 if( Signal == SIGTERM ) Log( LOG_WARNING, "Got TERM signal, terminating now ..." );
428 else if( Signal == SIGINT ) Log( LOG_WARNING, "Got INT signal, terminating now ..." );
429 else if( Signal == SIGQUIT ) Log( LOG_WARNING, "Got QUIT signal, terminating now ..." );
430 NGIRCd_Quit = TRUE;
431 break;
432 case SIGHUP:
433 /* neu starten */
434 Log( LOG_WARNING, "Got HUP signal, restarting now ..." );
435 NGIRCd_Restart = TRUE;
436 break;
437 case SIGCHLD:
438 /* Child-Prozess wurde beendet. Zombies vermeiden: */
439 while( waitpid( -1, NULL, WNOHANG ) > 0);
440 break;
441 default:
442 /* unbekanntes bzw. unbehandeltes Signal */
443 Log( LOG_NOTICE, "Got signal %d! Ignored.", Signal );
445 } /* Signal_Handler */
448 LOCAL VOID
449 Initialize_Listen_Ports( VOID )
451 /* Ports, auf denen der Server Verbindungen entgegennehmen
452 * soll, initialisieren */
454 UINT created, i;
456 created = 0;
457 for( i = 0; i < Conf_ListenPorts_Count; i++ )
459 if( Conn_NewListener( Conf_ListenPorts[i] )) created++;
460 else Log( LOG_ERR, "Can't listen on port %u!", Conf_ListenPorts[i] );
463 if( created < 1 )
465 Log( LOG_ALERT, "Server isn't listening on a single port!" );
466 Log( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE );
467 exit( 1 );
469 } /* Initialize_Listen_Ports */
472 LOCAL VOID
473 Show_Version( VOID )
475 puts( NGIRCd_Version( ));
476 puts( "Copyright (c)2001,2002 by Alexander Barton (<alex@barton.de>)." );
477 puts( "Homepage: <http://arthur.ath.cx/~alex/ngircd/>\n" );
478 puts( "This is free software; see the source for copying conditions. There is NO" );
479 puts( "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE." );
480 } /* Show_Version */
483 LOCAL VOID
484 Show_Help( VOID )
486 #ifdef DEBUG
487 puts( " -d, --debug log extra debug messages" );
488 #endif
489 puts( " -f, --config <f> use file <f> as configuration file" );
490 puts( " -n, --nodaemon don't fork and don't detatch from controlling terminal" );
491 puts( " -p, --passive disable automatic connections to other servers" );
492 #ifdef SNIFFER
493 puts( " -s, --sniffer enable network sniffer and display all IRC traffic" );
494 #endif
495 puts( " --configtest read, validate and display configuration; then exit" );
496 puts( " --version output version information and exit" );
497 puts( " --help display this help and exit" );
498 } /* Show_Help */
501 /* -eof- */