2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2005 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.95 2005/03/19 18:43:49 fw Exp $";
28 #include <sys/types.h>
41 #include "cvs-version.h"
48 #include "rendezvous.h"
55 LOCAL void Initialize_Signal_Handler PARAMS(( void ));
56 LOCAL void Signal_Handler PARAMS(( int Signal ));
58 LOCAL void Show_Version PARAMS(( void ));
59 LOCAL void Show_Help PARAMS(( void ));
61 LOCAL void Pidfile_Create PARAMS(( long ));
62 LOCAL void Pidfile_Delete PARAMS(( void ));
64 LOCAL void Fill_Version PARAMS(( void ));
66 LOCAL void Setup_FDStreams PARAMS(( void ));
70 main( int argc, const char *argv[] )
74 bool ok, configtest = false;
81 NGIRCd_SignalQuit = NGIRCd_SignalRestart = NGIRCd_SignalRehash = false;
82 NGIRCd_NoDaemon = NGIRCd_Passive = false;
87 NGIRCd_Sniffer = false;
89 strlcpy( NGIRCd_ConfFile, SYSCONFDIR, sizeof( NGIRCd_ConfFile ));
90 strlcat( NGIRCd_ConfFile, CONFIG_FILE, sizeof( NGIRCd_ConfFile ));
94 /* Kommandozeile parsen */
95 for( i = 1; i < argc; i++ )
98 if(( argv[i][0] == '-' ) && ( argv[i][1] == '-' ))
102 if( strcmp( argv[i], "--config" ) == 0 )
106 /* Ok, there's an parameter left */
107 strlcpy( NGIRCd_ConfFile, argv[i + 1], sizeof( NGIRCd_ConfFile ));
113 if( strcmp( argv[i], "--configtest" ) == 0 )
119 if( strcmp( argv[i], "--debug" ) == 0 )
125 if( strcmp( argv[i], "--help" ) == 0 )
128 puts( "" ); Show_Help( ); puts( "" );
131 if( strcmp( argv[i], "--nodaemon" ) == 0 )
133 NGIRCd_NoDaemon = true;
136 if( strcmp( argv[i], "--passive" ) == 0 )
138 NGIRCd_Passive = true;
142 if( strcmp( argv[i], "--sniffer" ) == 0 )
144 NGIRCd_Sniffer = true;
148 if( strcmp( argv[i], "--version" ) == 0 )
154 else if(( argv[i][0] == '-' ) && ( argv[i][1] != '-' ))
157 for( n = 1; n < strlen( argv[i] ); n++ )
161 if( argv[i][n] == 'd' )
167 if( argv[i][n] == 'f' )
169 if(( ! argv[i][n + 1] ) && ( i + 1 < argc ))
171 /* Ok, next character is a blank */
172 strlcpy( NGIRCd_ConfFile, argv[i + 1], sizeof( NGIRCd_ConfFile ));
174 /* go to the following parameter */
176 n = strlen( argv[i] );
180 if( argv[i][n] == 'n' )
182 NGIRCd_NoDaemon = true;
185 if( argv[i][n] == 'p' )
187 NGIRCd_Passive = true;
191 if( argv[i][n] == 's' )
193 NGIRCd_Sniffer = true;
197 if( argv[i][n] == 't' )
205 printf( "%s: invalid option \"-%c\"!\n", PACKAGE_NAME, argv[i][n] );
206 printf( "Try \"%s --help\" for more information.\n", PACKAGE_NAME );
214 printf( "%s: invalid option \"%s\"!\n", PACKAGE_NAME, argv[i] );
215 printf( "Try \"%s --help\" for more information.\n", PACKAGE_NAME );
220 /* Debug-Level (fuer IRC-Befehl "VERSION") ermitteln */
221 NGIRCd_DebugLevel[0] = '\0';
223 if( NGIRCd_Debug ) strcpy( NGIRCd_DebugLevel, "1" );
229 strcpy( NGIRCd_DebugLevel, "2" );
233 /* Soll nur die Konfigurations ueberprueft und ausgegeben werden? */
236 Show_Version( ); puts( "" );
240 while( ! NGIRCd_SignalQuit )
242 /* Initialize global variables */
243 NGIRCd_Start = time( NULL );
244 (void)strftime( NGIRCd_StartStr, 64, "%a %b %d %Y at %H:%M:%S (%Z)", localtime( &NGIRCd_Start ));
246 NGIRCd_SignalRehash = false;
247 NGIRCd_SignalRestart = false;
248 NGIRCd_SignalQuit = false;
250 /* Initialize modules, part I */
257 if( chdir( Conf_Chroot ) != 0 ) Log( LOG_ERR, "Can't chdir() in ChrootDir (%s): %s", Conf_Chroot, strerror( errno ));
259 if( chroot( Conf_Chroot ) != 0 ) Log( LOG_ERR, "Can't change root directory to \"%s\": %s", Conf_Chroot, strerror( errno ));
260 else Log( LOG_INFO, "Changed root and working directory to \"%s\".", Conf_Chroot );
265 /* Set new group ID */
266 if( setgid( Conf_GID ) != 0 ) Log( LOG_ERR, "Can't change group ID to %u: %s", Conf_GID, strerror( errno ));
270 /* Set new user ID */
271 if( setuid( Conf_UID ) != 0 ) Log( LOG_ERR, "Can't change user ID to %u: %s", Conf_UID, strerror( errno ));
274 /* Normally a child process is forked which isn't any longer
275 * connected to ther controlling terminal. Use "--nodaemon"
276 * to disable this "daemon mode" (useful for debugging). */
277 if( ! NGIRCd_NoDaemon )
279 /* fork child process */
283 /* "Old" process: exit. */
289 fprintf( stderr, "%s: Can't fork: %s!\nFatal error, exiting now ...\n", PACKAGE_NAME, strerror( errno ));
293 /* New child process */
297 /* Detach stdin, stdout and stderr */
301 /* Create PID file */
302 pid = (long) getpid( );
303 Pidfile_Create( pid );
305 /* Show user, group, and PID of the running daemon */
306 pwd = getpwuid( getuid( )); grp = getgrgid( getgid( ));
307 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( ), pid);
309 /* Change working directory to home directory of the user
310 * we are running as (when not running chroot()'ed!) */
311 if( Conf_UID != 0 && ! Conf_Chroot[0] )
315 pwd = getpwuid( Conf_UID );
318 if( chdir( pwd->pw_dir ) == 0 ) Log( LOG_DEBUG, "Changed working directory to \"%s\" ...", pwd->pw_dir );
319 else Log( LOG_ERR, "Can't change working directory to \"%s\": %s", pwd->pw_dir, strerror( errno ));
321 else Log( LOG_ERR, "Can't get user informaton for UID %d!?", Conf_UID );
324 /* Initialize modules, part II: these functions are eventually
325 * called with already dropped privileges ... */
336 /* Redirect stderr handle to "error file" for debugging
337 * when not running in "no daemon" mode: */
338 if( ! NGIRCd_NoDaemon ) Log_InitErrorfile( );
341 /* Signal-Handler initialisieren */
342 Initialize_Signal_Handler( );
344 /* Protokoll- und Server-Identifikation erzeugen. Die vom ngIRCd
345 * beim PASS-Befehl verwendete Syntax sowie die erweiterten Flags
346 * sind in doc/Protocol.txt beschrieben. */
348 sprintf( NGIRCd_ProtoID, "%s%s %s|%s:%s", PROTOVER, PROTOIRCPLUS, PACKAGE_NAME, PACKAGE_VERSION, IRCPLUSFLAGS );
350 strcat( NGIRCd_ProtoID, "Z" );
352 if( Conf_OperCanMode ) strcat( NGIRCd_ProtoID, "o" );
354 sprintf( NGIRCd_ProtoID, "%s%s %s|%s", PROTOVER, PROTOIRC, PACKAGE_NAME, PACKAGE_VERSION );
356 strcat( NGIRCd_ProtoID, " P" );
358 strcat( NGIRCd_ProtoID, "Z" );
360 Log( LOG_DEBUG, "Protocol and server ID is \"%s\".", NGIRCd_ProtoID );
362 /* Vordefinierte Channels anlegen */
363 Channel_InitPredefined( );
365 /* Listen-Ports initialisieren */
366 if( Conn_InitListeners( ) < 1 )
368 Log( LOG_ALERT, "Server isn't listening on a single port!" );
369 Log( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
397 NGIRCd_VersionAddition[0] = '\0';
400 strcpy( NGIRCd_VersionAddition, "SYSLOG" );
403 if( NGIRCd_VersionAddition[0] ) strcat( NGIRCd_VersionAddition, "+" );
404 strcat( NGIRCd_VersionAddition, "ZLIB" );
407 if( NGIRCd_VersionAddition[0] ) strcat( NGIRCd_VersionAddition, "+" );
408 strcat( NGIRCd_VersionAddition, "TCPWRAP" );
411 if( NGIRCd_VersionAddition[0] ) strcat( NGIRCd_VersionAddition, "+" );
412 strcat( NGIRCd_VersionAddition, "RENDEZVOUS" );
415 if( NGIRCd_VersionAddition[0] ) strcat( NGIRCd_VersionAddition, "+" );
416 strcat( NGIRCd_VersionAddition, "IDENT" );
419 if( NGIRCd_VersionAddition[0] ) strcat( NGIRCd_VersionAddition, "+" );
420 strcat( NGIRCd_VersionAddition, "DEBUG" );
423 if( NGIRCd_VersionAddition[0] ) strcat( NGIRCd_VersionAddition, "+" );
424 strcat( NGIRCd_VersionAddition, "SNIFFER" );
427 if( NGIRCd_VersionAddition[0] ) strcat( NGIRCd_VersionAddition, "+" );
428 strcat( NGIRCd_VersionAddition, "RFC" );
431 if( NGIRCd_VersionAddition[0] ) strcat( NGIRCd_VersionAddition, "+" );
432 strcat( NGIRCd_VersionAddition, "IRCPLUS" );
435 if( NGIRCd_VersionAddition[0] ) strlcat( NGIRCd_VersionAddition, "-", sizeof( NGIRCd_VersionAddition ));
436 strlcat( NGIRCd_VersionAddition, TARGET_CPU, sizeof( NGIRCd_VersionAddition ));
437 strlcat( NGIRCd_VersionAddition, "/", sizeof( NGIRCd_VersionAddition ));
438 strlcat( NGIRCd_VersionAddition, TARGET_VENDOR, sizeof( NGIRCd_VersionAddition ));
439 strlcat( NGIRCd_VersionAddition, "/", sizeof( NGIRCd_VersionAddition ));
440 strlcat( NGIRCd_VersionAddition, TARGET_OS, sizeof( NGIRCd_VersionAddition ));
443 snprintf( NGIRCd_Version, sizeof NGIRCd_Version,"%s %s(%s)-%s", PACKAGE_NAME, PACKAGE_VERSION, CVSDATE, NGIRCd_VersionAddition);
445 snprintf( NGIRCd_Version, sizeof NGIRCd_Version, "%s %s-%s", PACKAGE_NAME, PACKAGE_VERSION, NGIRCd_VersionAddition);
451 NGIRCd_Rehash( void )
453 char old_name[CLIENT_ID_LEN];
455 Log( LOG_NOTICE|LOG_snotice, "Re-reading configuration NOW!" );
456 NGIRCd_SignalRehash = false;
458 /* Close down all listening sockets */
459 Conn_ExitListeners( );
461 /* Remember old server name */
462 strcpy( old_name, Conf_ServerName );
464 /* Re-read configuration ... */
467 /* Recover old server name: it can't be changed during run-time */
468 if( strcmp( old_name, Conf_ServerName ) != 0 )
470 strcpy( Conf_ServerName, old_name );
471 Log( LOG_ERR, "Can't change \"ServerName\" on runtime! Ignored new name." );
474 /* Create new pre-defined channels */
475 Channel_InitPredefined( );
477 /* Start listening on sockets */
478 Conn_InitListeners( );
480 /* Sync configuration with established connections */
481 Conn_SyncServerStruct( );
483 Log( LOG_NOTICE|LOG_snotice, "Re-reading of configuration done." );
484 } /* NGIRCd_Rehash */
488 Initialize_Signal_Handler( void )
490 /* Signal-Handler initialisieren: einige Signale
491 * werden ignoriert, andere speziell behandelt. */
493 #ifdef HAVE_SIGACTION
494 /* sigaction() ist vorhanden */
496 struct sigaction saction;
498 /* Signal-Struktur initialisieren */
499 memset( &saction, 0, sizeof( saction ));
500 saction.sa_handler = Signal_Handler;
502 saction.sa_flags |= SA_RESTART;
505 saction.sa_flags |= SA_NOCLDWAIT;
508 /* Signal-Handler einhaengen */
509 sigaction( SIGINT, &saction, NULL );
510 sigaction( SIGQUIT, &saction, NULL );
511 sigaction( SIGTERM, &saction, NULL);
512 sigaction( SIGHUP, &saction, NULL);
513 sigaction( SIGCHLD, &saction, NULL);
515 /* einige Signale ignorieren */
516 saction.sa_handler = SIG_IGN;
517 sigaction( SIGPIPE, &saction, NULL );
519 /* kein sigaction() vorhanden */
521 /* Signal-Handler einhaengen */
522 signal( SIGINT, Signal_Handler );
523 signal( SIGQUIT, Signal_Handler );
524 signal( SIGTERM, Signal_Handler );
525 signal( SIGHUP, Signal_Handler );
526 signal( SIGCHLD, Signal_Handler );
528 /* einige Signale ignorieren */
529 signal( SIGPIPE, SIG_IGN );
531 } /* Initialize_Signal_Handler */
535 Signal_Handler( int Signal )
537 /* Signal-Handler. Dieser wird aufgerufen, wenn eines der Signale eintrifft,
538 * fuer das wir uns registriert haben (vgl. Initialize_Signal_Handler). Die
539 * Nummer des eingetroffenen Signals wird der Funktion uebergeben. */
546 /* wir soll(t)en uns wohl beenden ... */
547 NGIRCd_SignalQuit = true;
550 /* Konfiguration neu einlesen: */
551 NGIRCd_SignalRehash = true;
554 /* Child-Prozess wurde beendet. Zombies vermeiden: */
555 while( waitpid( -1, NULL, WNOHANG ) > 0);
559 /* unbekanntes bzw. unbehandeltes Signal */
560 Log( LOG_DEBUG, "Got signal %d! Ignored.", Signal );
563 } /* Signal_Handler */
569 puts( NGIRCd_Version );
570 puts( "Copyright (c)2001-2005 by Alexander Barton (<alex@barton.de>)." );
571 puts( "Homepage: <http://arthur.ath.cx/~alex/ngircd/>\n" );
572 puts( "This is free software; see the source for copying conditions. There is NO" );
573 puts( "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE." );
581 puts( " -d, --debug log extra debug messages" );
583 puts( " -f, --config <f> use file <f> as configuration file" );
584 puts( " -n, --nodaemon don't fork and don't detach from controlling terminal" );
585 puts( " -p, --passive disable automatic connections to other servers" );
587 puts( " -s, --sniffer enable network sniffer and display all IRC traffic" );
589 puts( " -t, --configtest read, validate and display configuration; then exit" );
590 puts( " --version output version information and exit" );
591 puts( " --help display this help and exit" );
596 Pidfile_Delete( void )
598 /* Pidfile configured? */
599 if( ! Conf_PidFile[0] ) return;
602 Log( LOG_DEBUG, "Removing PID file (%s) ...", Conf_PidFile );
605 if( unlink( Conf_PidFile ))
606 Log( LOG_ERR, "Error unlinking PID file (%s): %s", Conf_PidFile, strerror( errno ));
607 } /* Pidfile_Delete */
611 Pidfile_Create( long pid )
615 /* Pidfile configured? */
616 if( ! Conf_PidFile[0] ) return;
619 Log( LOG_DEBUG, "Creating PID file (%s) ...", Conf_PidFile );
622 pidf = fopen( Conf_PidFile, "w" );
626 Log( LOG_ERR, "Error writing PID file (%s): %s", Conf_PidFile, strerror( errno ));
630 if( fprintf( pidf, "%ld\n", pid ) < 0 )
631 Log( LOG_ERR, "Can't write PID file (%s): %s", Conf_PidFile, strerror( errno ));
633 if( fclose(pidf) != 0 )
634 Log( LOG_ERR, "Error closing PID file (%s): %s", Conf_PidFile, strerror( errno ));
635 } /* Pidfile_Create */
639 Setup_FDStreams( void )
643 /* Test if we can open /dev/null for reading and writing. If not
644 * we are most probably chrooted already and the server has been
645 * restarted. So we simply don't try to redirect stdXXX ... */
646 fd = open( "/dev/null", O_RDWR );
647 if ( fd < 0 ) return;
649 /* Close "old" stdin/out/err descriptors */
650 close( 0 ); close( 1 ); close( 2 );
652 /* Create new stdin(0), stdout(1) and stderr(2) descriptors */
653 dup2( fd, 0 ); dup2( fd, 1 ); dup2( fd, 2 );
655 /* Close newly opened file descriptor if not stdin/out/err */
656 if( fd > 2 ) close( fd );
657 } /* Setup_FDStreams */