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.
15 static char UNUSED id[] = "$Id: ngircd.c,v 1.96 2005/06/01 21:52:18 alex Exp $";
19 * The main program, including the C function main() which is called
20 * by the loader of the operating system.
32 #include <sys/types.h>
45 #include "cvs-version.h"
52 #include "rendezvous.h"
59 LOCAL void Initialize_Signal_Handler PARAMS(( void ));
60 LOCAL void Signal_Handler PARAMS(( int Signal ));
62 LOCAL void Show_Version PARAMS(( void ));
63 LOCAL void Show_Help PARAMS(( void ));
65 LOCAL void Pidfile_Create PARAMS(( long ));
66 LOCAL void Pidfile_Delete PARAMS(( void ));
68 LOCAL void Fill_Version PARAMS(( void ));
70 LOCAL void Setup_FDStreams PARAMS(( void ));
74 * The main() function of ngIRCd.
75 * Here all starts: this function is called by the operating system loader,
76 * it is the first portion of code executed of ngIRCd.
77 * @param argc The number of arguments passed to ngIRCd on the command line.
78 * @param argv An array containing all the arguments passed to ngIRCd.
79 * @return Global exit code of ngIRCd, zero on success.
82 main( int argc, const char *argv[] )
86 bool ok, configtest = false;
93 NGIRCd_SignalQuit = NGIRCd_SignalRestart = NGIRCd_SignalRehash = false;
94 NGIRCd_NoDaemon = NGIRCd_Passive = false;
99 NGIRCd_Sniffer = false;
101 strlcpy( NGIRCd_ConfFile, SYSCONFDIR, sizeof( NGIRCd_ConfFile ));
102 strlcat( NGIRCd_ConfFile, CONFIG_FILE, sizeof( NGIRCd_ConfFile ));
106 /* Kommandozeile parsen */
107 for( i = 1; i < argc; i++ )
110 if(( argv[i][0] == '-' ) && ( argv[i][1] == '-' ))
114 if( strcmp( argv[i], "--config" ) == 0 )
118 /* Ok, there's an parameter left */
119 strlcpy( NGIRCd_ConfFile, argv[i + 1], sizeof( NGIRCd_ConfFile ));
125 if( strcmp( argv[i], "--configtest" ) == 0 )
131 if( strcmp( argv[i], "--debug" ) == 0 )
137 if( strcmp( argv[i], "--help" ) == 0 )
140 puts( "" ); Show_Help( ); puts( "" );
143 if( strcmp( argv[i], "--nodaemon" ) == 0 )
145 NGIRCd_NoDaemon = true;
148 if( strcmp( argv[i], "--passive" ) == 0 )
150 NGIRCd_Passive = true;
154 if( strcmp( argv[i], "--sniffer" ) == 0 )
156 NGIRCd_Sniffer = true;
160 if( strcmp( argv[i], "--version" ) == 0 )
166 else if(( argv[i][0] == '-' ) && ( argv[i][1] != '-' ))
169 for( n = 1; n < strlen( argv[i] ); n++ )
173 if( argv[i][n] == 'd' )
179 if( argv[i][n] == 'f' )
181 if(( ! argv[i][n + 1] ) && ( i + 1 < argc ))
183 /* Ok, next character is a blank */
184 strlcpy( NGIRCd_ConfFile, argv[i + 1], sizeof( NGIRCd_ConfFile ));
186 /* go to the following parameter */
188 n = strlen( argv[i] );
192 if( argv[i][n] == 'n' )
194 NGIRCd_NoDaemon = true;
197 if( argv[i][n] == 'p' )
199 NGIRCd_Passive = true;
203 if( argv[i][n] == 's' )
205 NGIRCd_Sniffer = true;
209 if( argv[i][n] == 't' )
217 printf( "%s: invalid option \"-%c\"!\n", PACKAGE_NAME, argv[i][n] );
218 printf( "Try \"%s --help\" for more information.\n", PACKAGE_NAME );
226 printf( "%s: invalid option \"%s\"!\n", PACKAGE_NAME, argv[i] );
227 printf( "Try \"%s --help\" for more information.\n", PACKAGE_NAME );
232 /* Debug-Level (fuer IRC-Befehl "VERSION") ermitteln */
233 NGIRCd_DebugLevel[0] = '\0';
235 if( NGIRCd_Debug ) strcpy( NGIRCd_DebugLevel, "1" );
241 strcpy( NGIRCd_DebugLevel, "2" );
245 /* Soll nur die Konfigurations ueberprueft und ausgegeben werden? */
248 Show_Version( ); puts( "" );
252 while( ! NGIRCd_SignalQuit )
254 /* Initialize global variables */
255 NGIRCd_Start = time( NULL );
256 (void)strftime( NGIRCd_StartStr, 64, "%a %b %d %Y at %H:%M:%S (%Z)", localtime( &NGIRCd_Start ));
258 NGIRCd_SignalRehash = false;
259 NGIRCd_SignalRestart = false;
260 NGIRCd_SignalQuit = false;
262 /* Initialize modules, part I */
269 if( chdir( Conf_Chroot ) != 0 ) Log( LOG_ERR, "Can't chdir() in ChrootDir (%s): %s", Conf_Chroot, strerror( errno ));
271 if( chroot( Conf_Chroot ) != 0 ) Log( LOG_ERR, "Can't change root directory to \"%s\": %s", Conf_Chroot, strerror( errno ));
272 else Log( LOG_INFO, "Changed root and working directory to \"%s\".", Conf_Chroot );
277 /* Set new group ID */
278 if( setgid( Conf_GID ) != 0 ) Log( LOG_ERR, "Can't change group ID to %u: %s", Conf_GID, strerror( errno ));
282 /* Set new user ID */
283 if( setuid( Conf_UID ) != 0 ) Log( LOG_ERR, "Can't change user ID to %u: %s", Conf_UID, strerror( errno ));
286 /* Normally a child process is forked which isn't any longer
287 * connected to ther controlling terminal. Use "--nodaemon"
288 * to disable this "daemon mode" (useful for debugging). */
289 if( ! NGIRCd_NoDaemon )
291 /* fork child process */
295 /* "Old" process: exit. */
301 fprintf( stderr, "%s: Can't fork: %s!\nFatal error, exiting now ...\n", PACKAGE_NAME, strerror( errno ));
305 /* New child process */
309 /* Detach stdin, stdout and stderr */
313 /* Create PID file */
314 pid = (long) getpid( );
315 Pidfile_Create( pid );
317 /* Show user, group, and PID of the running daemon */
318 pwd = getpwuid( getuid( )); grp = getgrgid( getgid( ));
319 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);
321 /* Change working directory to home directory of the user
322 * we are running as (when not running chroot()'ed!) */
323 if( Conf_UID != 0 && ! Conf_Chroot[0] )
327 pwd = getpwuid( Conf_UID );
330 if( chdir( pwd->pw_dir ) == 0 ) Log( LOG_DEBUG, "Changed working directory to \"%s\" ...", pwd->pw_dir );
331 else Log( LOG_ERR, "Can't change working directory to \"%s\": %s", pwd->pw_dir, strerror( errno ));
333 else Log( LOG_ERR, "Can't get user informaton for UID %d!?", Conf_UID );
336 /* Initialize modules, part II: these functions are eventually
337 * called with already dropped privileges ... */
348 /* Redirect stderr handle to "error file" for debugging
349 * when not running in "no daemon" mode: */
350 if( ! NGIRCd_NoDaemon ) Log_InitErrorfile( );
353 /* Signal-Handler initialisieren */
354 Initialize_Signal_Handler( );
356 /* Protokoll- und Server-Identifikation erzeugen. Die vom ngIRCd
357 * beim PASS-Befehl verwendete Syntax sowie die erweiterten Flags
358 * sind in doc/Protocol.txt beschrieben. */
360 sprintf( NGIRCd_ProtoID, "%s%s %s|%s:%s", PROTOVER, PROTOIRCPLUS, PACKAGE_NAME, PACKAGE_VERSION, IRCPLUSFLAGS );
362 strcat( NGIRCd_ProtoID, "Z" );
364 if( Conf_OperCanMode ) strcat( NGIRCd_ProtoID, "o" );
366 sprintf( NGIRCd_ProtoID, "%s%s %s|%s", PROTOVER, PROTOIRC, PACKAGE_NAME, PACKAGE_VERSION );
368 strcat( NGIRCd_ProtoID, " P" );
370 strcat( NGIRCd_ProtoID, "Z" );
372 Log( LOG_DEBUG, "Protocol and server ID is \"%s\".", NGIRCd_ProtoID );
374 /* Vordefinierte Channels anlegen */
375 Channel_InitPredefined( );
377 /* Listen-Ports initialisieren */
378 if( Conn_InitListeners( ) < 1 )
380 Log( LOG_ALERT, "Server isn't listening on a single port!" );
381 Log( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
407 * Generate ngIRCd "version string".
408 * This string is generated once and then stored in NGIRCd_Version for
409 * further usage, for example by the IRC command VERSION and the --version
410 * command line switch.
415 NGIRCd_VersionAddition[0] = '\0';
418 strcpy( NGIRCd_VersionAddition, "SYSLOG" );
421 if( NGIRCd_VersionAddition[0] ) strcat( NGIRCd_VersionAddition, "+" );
422 strcat( NGIRCd_VersionAddition, "ZLIB" );
425 if( NGIRCd_VersionAddition[0] ) strcat( NGIRCd_VersionAddition, "+" );
426 strcat( NGIRCd_VersionAddition, "TCPWRAP" );
429 if( NGIRCd_VersionAddition[0] ) strcat( NGIRCd_VersionAddition, "+" );
430 strcat( NGIRCd_VersionAddition, "RENDEZVOUS" );
433 if( NGIRCd_VersionAddition[0] ) strcat( NGIRCd_VersionAddition, "+" );
434 strcat( NGIRCd_VersionAddition, "IDENT" );
437 if( NGIRCd_VersionAddition[0] ) strcat( NGIRCd_VersionAddition, "+" );
438 strcat( NGIRCd_VersionAddition, "DEBUG" );
441 if( NGIRCd_VersionAddition[0] ) strcat( NGIRCd_VersionAddition, "+" );
442 strcat( NGIRCd_VersionAddition, "SNIFFER" );
445 if( NGIRCd_VersionAddition[0] ) strcat( NGIRCd_VersionAddition, "+" );
446 strcat( NGIRCd_VersionAddition, "RFC" );
449 if( NGIRCd_VersionAddition[0] ) strcat( NGIRCd_VersionAddition, "+" );
450 strcat( NGIRCd_VersionAddition, "IRCPLUS" );
453 if( NGIRCd_VersionAddition[0] ) strlcat( NGIRCd_VersionAddition, "-", sizeof( NGIRCd_VersionAddition ));
454 strlcat( NGIRCd_VersionAddition, TARGET_CPU, sizeof( NGIRCd_VersionAddition ));
455 strlcat( NGIRCd_VersionAddition, "/", sizeof( NGIRCd_VersionAddition ));
456 strlcat( NGIRCd_VersionAddition, TARGET_VENDOR, sizeof( NGIRCd_VersionAddition ));
457 strlcat( NGIRCd_VersionAddition, "/", sizeof( NGIRCd_VersionAddition ));
458 strlcat( NGIRCd_VersionAddition, TARGET_OS, sizeof( NGIRCd_VersionAddition ));
461 snprintf( NGIRCd_Version, sizeof NGIRCd_Version,"%s %s(%s)-%s", PACKAGE_NAME, PACKAGE_VERSION, CVSDATE, NGIRCd_VersionAddition);
463 snprintf( NGIRCd_Version, sizeof NGIRCd_Version, "%s %s-%s", PACKAGE_NAME, PACKAGE_VERSION, NGIRCd_VersionAddition);
469 * Reload the server configuration file.
472 NGIRCd_Rehash( void )
474 char old_name[CLIENT_ID_LEN];
476 Log( LOG_NOTICE|LOG_snotice, "Re-reading configuration NOW!" );
477 NGIRCd_SignalRehash = false;
479 /* Close down all listening sockets */
480 Conn_ExitListeners( );
482 /* Remember old server name */
483 strcpy( old_name, Conf_ServerName );
485 /* Re-read configuration ... */
488 /* Recover old server name: it can't be changed during run-time */
489 if( strcmp( old_name, Conf_ServerName ) != 0 )
491 strcpy( Conf_ServerName, old_name );
492 Log( LOG_ERR, "Can't change \"ServerName\" on runtime! Ignored new name." );
495 /* Create new pre-defined channels */
496 Channel_InitPredefined( );
498 /* Start listening on sockets */
499 Conn_InitListeners( );
501 /* Sync configuration with established connections */
502 Conn_SyncServerStruct( );
504 Log( LOG_NOTICE|LOG_snotice, "Re-reading of configuration done." );
505 } /* NGIRCd_Rehash */
509 * Initialize the signal handler.
512 Initialize_Signal_Handler( void )
514 /* Signal-Handler initialisieren: einige Signale
515 * werden ignoriert, andere speziell behandelt. */
517 #ifdef HAVE_SIGACTION
518 /* sigaction() ist vorhanden */
520 struct sigaction saction;
522 /* Signal-Struktur initialisieren */
523 memset( &saction, 0, sizeof( saction ));
524 saction.sa_handler = Signal_Handler;
526 saction.sa_flags |= SA_RESTART;
529 saction.sa_flags |= SA_NOCLDWAIT;
532 /* Signal-Handler einhaengen */
533 sigaction( SIGINT, &saction, NULL );
534 sigaction( SIGQUIT, &saction, NULL );
535 sigaction( SIGTERM, &saction, NULL);
536 sigaction( SIGHUP, &saction, NULL);
537 sigaction( SIGCHLD, &saction, NULL);
539 /* einige Signale ignorieren */
540 saction.sa_handler = SIG_IGN;
541 sigaction( SIGPIPE, &saction, NULL );
543 /* kein sigaction() vorhanden */
545 /* Signal-Handler einhaengen */
546 signal( SIGINT, Signal_Handler );
547 signal( SIGQUIT, Signal_Handler );
548 signal( SIGTERM, Signal_Handler );
549 signal( SIGHUP, Signal_Handler );
550 signal( SIGCHLD, Signal_Handler );
552 /* einige Signale ignorieren */
553 signal( SIGPIPE, SIG_IGN );
555 } /* Initialize_Signal_Handler */
559 * Signal handler of ngIRCd.
560 * This function is called whenever ngIRCd catches a signal sent by the
561 * user and/or the system to it. For example SIGTERM and SIGHUP.
562 * @param Signal Number of the signal to handle.
565 Signal_Handler( int Signal )
572 /* wir soll(t)en uns wohl beenden ... */
573 NGIRCd_SignalQuit = true;
576 /* Konfiguration neu einlesen: */
577 NGIRCd_SignalRehash = true;
580 /* Child-Prozess wurde beendet. Zombies vermeiden: */
581 while( waitpid( -1, NULL, WNOHANG ) > 0);
585 /* unbekanntes bzw. unbehandeltes Signal */
586 Log( LOG_DEBUG, "Got signal %d! Ignored.", Signal );
589 } /* Signal_Handler */
593 * Display copyright and version information of ngIRCd on the console.
598 puts( NGIRCd_Version );
599 puts( "Copyright (c)2001-2005 by Alexander Barton (<alex@barton.de>)." );
600 puts( "Homepage: <http://arthur.ath.cx/~alex/ngircd/>\n" );
601 puts( "This is free software; see the source for copying conditions. There is NO" );
602 puts( "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE." );
607 * Display a short help text on the console.
608 * This help depends on the configuration of the executable and only shows
609 * options that are actually enabled.
615 puts( " -d, --debug log extra debug messages" );
617 puts( " -f, --config <f> use file <f> as configuration file" );
618 puts( " -n, --nodaemon don't fork and don't detach from controlling terminal" );
619 puts( " -p, --passive disable automatic connections to other servers" );
621 puts( " -s, --sniffer enable network sniffer and display all IRC traffic" );
623 puts( " -t, --configtest read, validate and display configuration; then exit" );
624 puts( " --version output version information and exit" );
625 puts( " --help display this help and exit" );
630 * Delete the file containing the process ID (PID).
633 Pidfile_Delete( void )
635 /* Pidfile configured? */
636 if( ! Conf_PidFile[0] ) return;
639 Log( LOG_DEBUG, "Removing PID file (%s) ...", Conf_PidFile );
642 if( unlink( Conf_PidFile ))
643 Log( LOG_ERR, "Error unlinking PID file (%s): %s", Conf_PidFile, strerror( errno ));
644 } /* Pidfile_Delete */
648 * Create the file containing the process ID of ngIRCd ("PID file").
649 * @param pid The process ID to be stored in this file.
652 Pidfile_Create( long pid )
656 /* Pidfile configured? */
657 if( ! Conf_PidFile[0] ) return;
660 Log( LOG_DEBUG, "Creating PID file (%s) ...", Conf_PidFile );
663 pidf = fopen( Conf_PidFile, "w" );
667 Log( LOG_ERR, "Error writing PID file (%s): %s", Conf_PidFile, strerror( errno ));
671 if( fprintf( pidf, "%ld\n", pid ) < 0 )
672 Log( LOG_ERR, "Can't write PID file (%s): %s", Conf_PidFile, strerror( errno ));
674 if( fclose(pidf) != 0 )
675 Log( LOG_ERR, "Error closing PID file (%s): %s", Conf_PidFile, strerror( errno ));
676 } /* Pidfile_Create */
680 * Redirect stdin, stdout and stderr to apropriate file handles.
683 Setup_FDStreams( void )
687 /* Test if we can open /dev/null for reading and writing. If not
688 * we are most probably chrooted already and the server has been
689 * restarted. So we simply don't try to redirect stdXXX ... */
690 fd = open( "/dev/null", O_RDWR );
691 if ( fd < 0 ) return;
693 /* Close "old" stdin/out/err descriptors */
694 close( 0 ); close( 1 ); close( 2 );
696 /* Create new stdin(0), stdout(1) and stderr(2) descriptors */
697 dup2( fd, 0 ); dup2( fd, 1 ); dup2( fd, 2 );
699 /* Close newly opened file descriptor if not stdin/out/err */
700 if( fd > 2 ) close( fd );
701 } /* Setup_FDStreams */