2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2004 by Alexander Barton (alex@barton.de)
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 * Please read the file COPYING, README and AUTHORS for more information.
11 * Main program -- main()
17 static char UNUSED id[] = "$Id: ngircd.c,v 1.84 2004/05/07 11:19:21 alex Exp $";
28 #include <sys/types.h>
39 #include "cvs-version.h"
47 #include "rendezvous.h"
54 LOCAL VOID Initialize_Signal_Handler PARAMS(( VOID ));
55 LOCAL VOID Signal_Handler PARAMS(( INT Signal ));
57 LOCAL VOID Show_Version PARAMS(( VOID ));
58 LOCAL VOID Show_Help PARAMS(( VOID ));
62 main( int argc, const char *argv[] )
66 BOOLEAN ok, configtest = FALSE;
72 NGIRCd_SignalQuit = NGIRCd_SignalRestart = NGIRCd_SignalRehash = FALSE;
73 NGIRCd_NoDaemon = NGIRCd_Passive = FALSE;
78 NGIRCd_Sniffer = FALSE;
80 strlcpy( NGIRCd_ConfFile, SYSCONFDIR, sizeof( NGIRCd_ConfFile ));
81 strlcat( NGIRCd_ConfFile, CONFIG_FILE, sizeof( NGIRCd_ConfFile ));
83 /* Kommandozeile parsen */
84 for( i = 1; i < argc; i++ )
87 if(( argv[i][0] == '-' ) && ( argv[i][1] == '-' ))
91 if( strcmp( argv[i], "--config" ) == 0 )
95 /* Ok, there's an parameter left */
96 strlcpy( NGIRCd_ConfFile, argv[i + 1], sizeof( NGIRCd_ConfFile ));
102 if( strcmp( argv[i], "--configtest" ) == 0 )
108 if( strcmp( argv[i], "--debug" ) == 0 )
114 if( strcmp( argv[i], "--help" ) == 0 )
117 puts( "" ); Show_Help( ); puts( "" );
120 if( strcmp( argv[i], "--nodaemon" ) == 0 )
122 NGIRCd_NoDaemon = TRUE;
125 if( strcmp( argv[i], "--passive" ) == 0 )
127 NGIRCd_Passive = TRUE;
131 if( strcmp( argv[i], "--sniffer" ) == 0 )
133 NGIRCd_Sniffer = TRUE;
137 if( strcmp( argv[i], "--version" ) == 0 )
143 else if(( argv[i][0] == '-' ) && ( argv[i][1] != '-' ))
147 for( n = 1; n < (LONG)strlen( argv[i] ); n++ )
151 if( argv[i][n] == 'd' )
157 if( argv[i][n] == 'f' )
159 if(( ! argv[i][n + 1] ) && ( i + 1 < argc ))
161 /* Ok, next character is a blank */
162 strlcpy( NGIRCd_ConfFile, argv[i + 1], sizeof( NGIRCd_ConfFile ));
164 /* go to the following parameter */
165 i++; n = (LONG)strlen( argv[i] );
169 if( argv[i][n] == 'n' )
171 NGIRCd_NoDaemon = TRUE;
174 if( argv[i][n] == 'p' )
176 NGIRCd_Passive = TRUE;
180 if( argv[i][n] == 's' )
182 NGIRCd_Sniffer = TRUE;
186 if( argv[i][n] == 't' )
194 printf( "%s: invalid option \"-%c\"!\n", PACKAGE_NAME, argv[i][n] );
195 printf( "Try \"%s --help\" for more information.\n", PACKAGE_NAME );
203 printf( "%s: invalid option \"%s\"!\n", PACKAGE_NAME, argv[i] );
204 printf( "Try \"%s --help\" for more information.\n", PACKAGE_NAME );
209 /* Debug-Level (fuer IRC-Befehl "VERSION") ermitteln */
210 strcpy( NGIRCd_DebugLevel, "" );
212 if( NGIRCd_Debug ) strcpy( NGIRCd_DebugLevel, "1" );
218 strcpy( NGIRCd_DebugLevel, "2" );
222 /* Soll nur die Konfigurations ueberprueft und ausgegeben werden? */
225 Show_Version( ); puts( "" );
229 while( ! NGIRCd_SignalQuit )
231 /* Initialize global variables */
232 NGIRCd_Start = time( NULL );
233 (VOID)strftime( NGIRCd_StartStr, 64, "%a %b %d %Y at %H:%M:%S (%Z)", localtime( &NGIRCd_Start ));
235 NGIRCd_SignalRehash = FALSE;
236 NGIRCd_SignalRestart = FALSE;
237 NGIRCd_SignalQuit = FALSE;
239 /* Initialize modules, part I */
246 if( chdir( Conf_Chroot ) != 0 ) Log( LOG_ERR, "Can't chdir() in ChrootDir (%s): %s", Conf_Chroot, strerror( errno ));
248 if( chroot( Conf_Chroot ) != 0 ) Log( LOG_ERR, "Can't change root directory to \"%s\": %s", Conf_Chroot, strerror( errno ));
249 else Log( LOG_INFO, "Changed root and working directory to \"%s\".", Conf_Chroot );
254 /* Set new group ID */
255 if( setgid( Conf_GID ) != 0 ) Log( LOG_ERR, "Can't change group ID to %u: %s", Conf_GID, strerror( errno ));
259 /* Set new user ID */
260 if( setuid( Conf_UID ) != 0 ) Log( LOG_ERR, "Can't change user ID to %u: %s", Conf_UID, strerror( errno ));
263 /* In der Regel wird ein Sub-Prozess ge-fork()'t, der
264 * nicht mehr mit dem Terminal verbunden ist. Mit der
265 * Option "--nodaemon" kann dies (z.B. zum Debuggen)
266 * verhindert werden. */
267 if( ! NGIRCd_NoDaemon )
269 /* Daemon im Hintergrund erzeugen */
273 /* "alter" Prozess */
279 printf( "%s: Can't fork: %s!\nFatal error, exiting now ...\n", PACKAGE_NAME, strerror( errno ));
283 /* Child-Prozess initialisieren */
288 /* Initialize modules, part II: these functions are eventually
289 * called with already dropped privileges ... */
299 /* Show user, group, and PID of the running daemon */
300 pwd = getpwuid( getuid( )); grp = getgrgid( getgid( ));
301 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( ));
303 /* Redirect stderr handle to "error file" for debugging.
304 * But don't try to write in the chroot jail, since it's more
305 * secure to have a chroot dir not writable by the daemon.
307 if( ! Conf_Chroot[0] ) Log_InitErrorfile( );
309 /* Signal-Handler initialisieren */
310 Initialize_Signal_Handler( );
312 /* Protokoll- und Server-Identifikation erzeugen. Die vom ngIRCd
313 * beim PASS-Befehl verwendete Syntax sowie die erweiterten Flags
314 * sind in doc/Protocol.txt beschrieben. */
316 sprintf( NGIRCd_ProtoID, "%s%s %s|%s:%s", PROTOVER, PROTOIRCPLUS, PACKAGE_NAME, PACKAGE_VERSION, IRCPLUSFLAGS );
318 strcat( NGIRCd_ProtoID, "Z" );
320 if( Conf_OperCanMode ) strcat( NGIRCd_ProtoID, "o" );
322 sprintf( NGIRCd_ProtoID, "%s%s %s|%s", PROTOVER, PROTOIRC, PACKAGE_NAME, PACKAGE_VERSION );
324 strcat( NGIRCd_ProtoID, " P" );
326 strcat( NGIRCd_ProtoID, "Z" );
328 Log( LOG_DEBUG, "Protocol and server ID is \"%s\".", NGIRCd_ProtoID );
330 /* Vordefinierte Channels anlegen */
331 Channel_InitPredefined( );
333 /* Listen-Ports initialisieren */
334 if( Conn_InitListeners( ) < 1 )
336 Log( LOG_ALERT, "Server isn't listening on a single port!" );
337 Log( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
360 NGIRCd_Version( VOID )
362 STATIC CHAR version[126];
365 sprintf( version, "%s %s(%s)-%s", PACKAGE_NAME, PACKAGE_VERSION, CVSDATE, NGIRCd_VersionAddition( ));
367 sprintf( version, "%s %s-%s", PACKAGE_NAME, PACKAGE_VERSION, NGIRCd_VersionAddition( ));
370 } /* NGIRCd_Version */
374 NGIRCd_VersionAddition( VOID )
376 STATIC CHAR txt[200];
381 if( txt[0] ) strcat( txt, "+" );
382 strcat( txt, "SYSLOG" );
385 if( txt[0] ) strcat( txt, "+" );
386 strcat( txt, "ZLIB" );
389 if( txt[0] ) strcat( txt, "+" );
390 strcat( txt, "TCPWRAP" );
393 if( txt[0] ) strcat( txt, "+" );
394 strcat( txt, "RENDEZVOUS" );
397 if( txt[0] ) strcat( txt, "+" );
398 strcat( txt, "IDENT" );
401 if( txt[0] ) strcat( txt, "+" );
402 strcat( txt, "DEBUG" );
405 if( txt[0] ) strcat( txt, "+" );
406 strcat( txt, "SNIFFER" );
409 if( txt[0] ) strcat( txt, "+" );
410 strcat( txt, "RFC" );
413 if( txt[0] ) strcat( txt, "+" );
414 strcat( txt, "IRCPLUS" );
417 if( txt[0] ) strlcat( txt, "-", sizeof( txt ));
418 strlcat( txt, TARGET_CPU, sizeof( txt ));
419 strlcat( txt, "/", sizeof( txt ));
420 strlcat( txt, TARGET_VENDOR, sizeof( txt ));
421 strlcat( txt, "/", sizeof( txt ));
422 strlcat( txt, TARGET_OS, sizeof( txt ));
425 } /* NGIRCd_VersionAddition */
429 NGIRCd_Rehash( VOID )
431 CHAR old_name[CLIENT_ID_LEN];
433 Log( LOG_NOTICE|LOG_snotice, "Re-reading configuration NOW!" );
434 NGIRCd_SignalRehash = FALSE;
436 /* Close down all listening sockets */
437 Conn_ExitListeners( );
439 /* Remember old server name */
440 strcpy( old_name, Conf_ServerName );
442 /* Re-read configuration ... */
445 /* Recover old server name: it can't be changed during run-time */
446 if( strcmp( old_name, Conf_ServerName ) != 0 )
448 strcpy( Conf_ServerName, old_name );
449 Log( LOG_ERR, "Can't change \"ServerName\" on runtime! Ignored new name." );
452 /* Create new pre-defined channels */
453 Channel_InitPredefined( );
455 /* Start listening on sockets */
456 Conn_InitListeners( );
458 /* Sync configuration with established connections */
459 Conn_SyncServerStruct( );
461 Log( LOG_NOTICE|LOG_snotice, "Re-reading of configuration done." );
462 } /* NGIRCd_Rehash */
466 Initialize_Signal_Handler( VOID )
468 /* Signal-Handler initialisieren: einige Signale
469 * werden ignoriert, andere speziell behandelt. */
471 #ifdef HAVE_SIGACTION
472 /* sigaction() ist vorhanden */
474 struct sigaction saction;
476 /* Signal-Struktur initialisieren */
477 memset( &saction, 0, sizeof( saction ));
478 saction.sa_handler = Signal_Handler;
480 saction.sa_flags |= SA_RESTART;
483 saction.sa_flags |= SA_NOCLDWAIT;
486 /* Signal-Handler einhaengen */
487 sigaction( SIGINT, &saction, NULL );
488 sigaction( SIGQUIT, &saction, NULL );
489 sigaction( SIGTERM, &saction, NULL);
490 sigaction( SIGHUP, &saction, NULL);
491 sigaction( SIGCHLD, &saction, NULL);
493 /* einige Signale ignorieren */
494 saction.sa_handler = SIG_IGN;
495 sigaction( SIGPIPE, &saction, NULL );
497 /* kein sigaction() vorhanden */
499 /* Signal-Handler einhaengen */
500 signal( SIGINT, Signal_Handler );
501 signal( SIGQUIT, Signal_Handler );
502 signal( SIGTERM, Signal_Handler );
503 signal( SIGHUP, Signal_Handler );
504 signal( SIGCHLD, Signal_Handler );
506 /* einige Signale ignorieren */
507 signal( SIGPIPE, SIG_IGN );
509 } /* Initialize_Signal_Handler */
513 Signal_Handler( INT Signal )
515 /* Signal-Handler. Dieser wird aufgerufen, wenn eines der Signale eintrifft,
516 * fuer das wir uns registriert haben (vgl. Initialize_Signal_Handler). Die
517 * Nummer des eingetroffenen Signals wird der Funktion uebergeben. */
524 /* wir soll(t)en uns wohl beenden ... */
525 NGIRCd_SignalQuit = TRUE;
528 /* Konfiguration neu einlesen: */
529 NGIRCd_SignalRehash = TRUE;
532 /* Child-Prozess wurde beendet. Zombies vermeiden: */
533 while( waitpid( -1, NULL, WNOHANG ) > 0);
537 /* unbekanntes bzw. unbehandeltes Signal */
538 Log( LOG_DEBUG, "Got signal %d! Ignored.", Signal );
541 } /* Signal_Handler */
547 puts( NGIRCd_Version( ));
548 puts( "Copyright (c)2001-2004 by Alexander Barton (<alex@barton.de>)." );
549 puts( "Homepage: <http://arthur.ath.cx/~alex/ngircd/>\n" );
550 puts( "This is free software; see the source for copying conditions. There is NO" );
551 puts( "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE." );
559 puts( " -d, --debug log extra debug messages" );
561 puts( " -f, --config <f> use file <f> as configuration file" );
562 puts( " -n, --nodaemon don't fork and don't detach from controlling terminal" );
563 puts( " -p, --passive disable automatic connections to other servers" );
565 puts( " -s, --sniffer enable network sniffer and display all IRC traffic" );
567 puts( " -t, --configtest read, validate and display configuration; then exit" );
568 puts( " --version output version information and exit" );
569 puts( " --help display this help and exit" );