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.102 2005/06/24 19:55:10 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 ));
72 LOCAL bool NGIRCd_Init PARAMS(( bool ));
75 * The main() function of ngIRCd.
76 * Here all starts: this function is called by the operating system loader,
77 * it is the first portion of code executed of ngIRCd.
78 * @param argc The number of arguments passed to ngIRCd on the command line.
79 * @param argv An array containing all the arguments passed to ngIRCd.
80 * @return Global exit code of ngIRCd, zero on success.
83 main( int argc, const char *argv[] )
85 bool ok, configtest = false;
86 bool NGIRCd_NoDaemon = false;
92 NGIRCd_SignalQuit = NGIRCd_SignalRestart = NGIRCd_SignalRehash = false;
93 NGIRCd_Passive = false;
98 NGIRCd_Sniffer = false;
100 strlcpy( NGIRCd_ConfFile, SYSCONFDIR, sizeof( NGIRCd_ConfFile ));
101 strlcat( NGIRCd_ConfFile, CONFIG_FILE, sizeof( NGIRCd_ConfFile ));
105 /* Kommandozeile parsen */
106 for( i = 1; i < argc; i++ )
109 if(( argv[i][0] == '-' ) && ( argv[i][1] == '-' ))
113 if( strcmp( argv[i], "--config" ) == 0 )
117 /* Ok, there's an parameter left */
118 strlcpy( NGIRCd_ConfFile, argv[i + 1], sizeof( NGIRCd_ConfFile ));
124 if( strcmp( argv[i], "--configtest" ) == 0 )
130 if( strcmp( argv[i], "--debug" ) == 0 )
136 if( strcmp( argv[i], "--help" ) == 0 )
139 puts( "" ); Show_Help( ); puts( "" );
142 if( strcmp( argv[i], "--nodaemon" ) == 0 )
144 NGIRCd_NoDaemon = true;
147 if( strcmp( argv[i], "--passive" ) == 0 )
149 NGIRCd_Passive = true;
153 if( strcmp( argv[i], "--sniffer" ) == 0 )
155 NGIRCd_Sniffer = true;
159 if( strcmp( argv[i], "--version" ) == 0 )
165 else if(( argv[i][0] == '-' ) && ( argv[i][1] != '-' ))
168 for( n = 1; n < strlen( argv[i] ); n++ )
172 if( argv[i][n] == 'd' )
178 if( argv[i][n] == 'f' )
180 if(( ! argv[i][n + 1] ) && ( i + 1 < argc ))
182 /* Ok, next character is a blank */
183 strlcpy( NGIRCd_ConfFile, argv[i + 1], sizeof( NGIRCd_ConfFile ));
185 /* go to the following parameter */
187 n = strlen( argv[i] );
191 if( argv[i][n] == 'n' )
193 NGIRCd_NoDaemon = true;
196 if( argv[i][n] == 'p' )
198 NGIRCd_Passive = true;
202 if( argv[i][n] == 's' )
204 NGIRCd_Sniffer = true;
208 if( argv[i][n] == 't' )
216 printf( "%s: invalid option \"-%c\"!\n", PACKAGE_NAME, argv[i][n] );
217 printf( "Try \"%s --help\" for more information.\n", PACKAGE_NAME );
225 printf( "%s: invalid option \"%s\"!\n", PACKAGE_NAME, argv[i] );
226 printf( "Try \"%s --help\" for more information.\n", PACKAGE_NAME );
231 /* Debug-Level (fuer IRC-Befehl "VERSION") ermitteln */
232 NGIRCd_DebugLevel[0] = '\0';
234 if( NGIRCd_Debug ) strcpy( NGIRCd_DebugLevel, "1" );
240 strcpy( NGIRCd_DebugLevel, "2" );
244 /* Soll nur die Konfigurations ueberprueft und ausgegeben werden? */
247 Show_Version( ); puts( "" );
251 while( ! NGIRCd_SignalQuit )
253 /* Initialize global variables */
254 NGIRCd_Start = time( NULL );
255 (void)strftime( NGIRCd_StartStr, 64, "%a %b %d %Y at %H:%M:%S (%Z)", localtime( &NGIRCd_Start ));
257 NGIRCd_SignalRehash = false;
258 NGIRCd_SignalRestart = false;
259 NGIRCd_SignalQuit = false;
261 /* Initialize modules, part I */
262 Log_Init( ! NGIRCd_NoDaemon );
265 if (!NGIRCd_Init( NGIRCd_NoDaemon )) {
266 Log(LOG_WARNING, "Fatal: Initialization failed");
270 /* Initialize modules, part II: these functions are eventually
271 * called with already dropped privileges ... */
282 /* Redirect stderr handle to "error file" for debugging
283 * when not running in "no daemon" mode: */
284 if( ! NGIRCd_NoDaemon ) Log_InitErrorfile( );
287 /* Signal-Handler initialisieren */
288 Initialize_Signal_Handler( );
290 /* Protokoll- und Server-Identifikation erzeugen. Die vom ngIRCd
291 * beim PASS-Befehl verwendete Syntax sowie die erweiterten Flags
292 * sind in doc/Protocol.txt beschrieben. */
294 snprintf( NGIRCd_ProtoID, sizeof NGIRCd_ProtoID, "%s%s %s|%s:%s", PROTOVER, PROTOIRCPLUS, PACKAGE_NAME, PACKAGE_VERSION, IRCPLUSFLAGS );
296 strcat( NGIRCd_ProtoID, "Z" );
298 if( Conf_OperCanMode ) strcat( NGIRCd_ProtoID, "o" );
300 snprintf( NGIRCd_ProtoID, sizeof NGIRCd_ProtoID, "%s%s %s|%s", PROTOVER, PROTOIRC, PACKAGE_NAME, PACKAGE_VERSION );
302 strlcat( NGIRCd_ProtoID, " P", sizeof NGIRCd_ProtoID );
304 strlcat( NGIRCd_ProtoID, "Z", sizeof NGIRCd_ProtoID );
306 Log( LOG_DEBUG, "Protocol and server ID is \"%s\".", NGIRCd_ProtoID );
308 /* Vordefinierte Channels anlegen */
309 Channel_InitPredefined( );
311 /* Listen-Ports initialisieren */
312 if( Conn_InitListeners( ) < 1 )
314 Log( LOG_ALERT, "Server isn't listening on a single port!" );
315 Log( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
340 * Generate ngIRCd "version string".
341 * This string is generated once and then stored in NGIRCd_Version for
342 * further usage, for example by the IRC command VERSION and the --version
343 * command line switch.
348 NGIRCd_VersionAddition[0] = '\0';
351 strlcpy( NGIRCd_VersionAddition, "SYSLOG", sizeof NGIRCd_VersionAddition );
354 if( NGIRCd_VersionAddition[0] )
355 strlcat( NGIRCd_VersionAddition, "+", sizeof NGIRCd_VersionAddition );
357 strlcat( NGIRCd_VersionAddition, "ZLIB", sizeof NGIRCd_VersionAddition );
360 if( NGIRCd_VersionAddition[0] )
361 strlcat( NGIRCd_VersionAddition, "+", sizeof NGIRCd_VersionAddition );
363 strlcat( NGIRCd_VersionAddition, "TCPWRAP", sizeof NGIRCd_VersionAddition );
366 if( NGIRCd_VersionAddition[0] )
367 strlcat( NGIRCd_VersionAddition, "+", sizeof NGIRCd_VersionAddition );
369 strlcat( NGIRCd_VersionAddition, "RENDEZVOUS", sizeof NGIRCd_VersionAddition );
372 if( NGIRCd_VersionAddition[0] )
373 strlcat( NGIRCd_VersionAddition, "+", sizeof NGIRCd_VersionAddition );
375 strlcat( NGIRCd_VersionAddition, "IDENT", sizeof NGIRCd_VersionAddition );
378 if( NGIRCd_VersionAddition[0] )
379 strlcat( NGIRCd_VersionAddition, "+", sizeof NGIRCd_VersionAddition );
381 strlcat( NGIRCd_VersionAddition, "DEBUG", sizeof NGIRCd_VersionAddition );
384 if( NGIRCd_VersionAddition[0] )
385 strlcat( NGIRCd_VersionAddition, "+", sizeof NGIRCd_VersionAddition );
387 strlcat( NGIRCd_VersionAddition, "SNIFFER", sizeof NGIRCd_VersionAddition );
390 if( NGIRCd_VersionAddition[0] )
391 strlcat( NGIRCd_VersionAddition, "+", sizeof NGIRCd_VersionAddition );
393 strlcat( NGIRCd_VersionAddition, "RFC", sizeof NGIRCd_VersionAddition );
396 if( NGIRCd_VersionAddition[0] )
397 strlcat( NGIRCd_VersionAddition, "+", sizeof NGIRCd_VersionAddition );
399 strlcat( NGIRCd_VersionAddition, "IRCPLUS", sizeof NGIRCd_VersionAddition );
402 if( NGIRCd_VersionAddition[0] )
403 strlcat( NGIRCd_VersionAddition, "-", sizeof( NGIRCd_VersionAddition ));
405 strlcat( NGIRCd_VersionAddition, TARGET_CPU, sizeof( NGIRCd_VersionAddition ));
406 strlcat( NGIRCd_VersionAddition, "/", sizeof( NGIRCd_VersionAddition ));
407 strlcat( NGIRCd_VersionAddition, TARGET_VENDOR, sizeof( NGIRCd_VersionAddition ));
408 strlcat( NGIRCd_VersionAddition, "/", sizeof( NGIRCd_VersionAddition ));
409 strlcat( NGIRCd_VersionAddition, TARGET_OS, sizeof( NGIRCd_VersionAddition ));
412 snprintf( NGIRCd_Version, sizeof NGIRCd_Version,"%s %s(%s)-%s", PACKAGE_NAME, PACKAGE_VERSION, CVSDATE, NGIRCd_VersionAddition);
414 snprintf( NGIRCd_Version, sizeof NGIRCd_Version, "%s %s-%s", PACKAGE_NAME, PACKAGE_VERSION, NGIRCd_VersionAddition);
420 * Reload the server configuration file.
423 NGIRCd_Rehash( void )
425 char old_name[CLIENT_ID_LEN];
427 Log( LOG_NOTICE|LOG_snotice, "Re-reading configuration NOW!" );
428 NGIRCd_SignalRehash = false;
430 /* Close down all listening sockets */
431 Conn_ExitListeners( );
433 /* Remember old server name */
434 strlcpy( old_name, Conf_ServerName, sizeof old_name );
436 /* Re-read configuration ... */
439 /* Recover old server name: it can't be changed during run-time */
440 if( strcmp( old_name, Conf_ServerName ) != 0 )
442 strcpy( Conf_ServerName, old_name );
443 Log( LOG_ERR, "Can't change \"ServerName\" on runtime! Ignored new name." );
446 /* Create new pre-defined channels */
447 Channel_InitPredefined( );
449 /* Start listening on sockets */
450 Conn_InitListeners( );
452 /* Sync configuration with established connections */
453 Conn_SyncServerStruct( );
455 Log( LOG_NOTICE|LOG_snotice, "Re-reading of configuration done." );
456 } /* NGIRCd_Rehash */
460 * Initialize the signal handler.
463 Initialize_Signal_Handler( void )
465 /* Signal-Handler initialisieren: einige Signale
466 * werden ignoriert, andere speziell behandelt. */
468 #ifdef HAVE_SIGACTION
469 /* sigaction() ist vorhanden */
471 struct sigaction saction;
473 /* Signal-Struktur initialisieren */
474 memset( &saction, 0, sizeof( saction ));
475 saction.sa_handler = Signal_Handler;
477 saction.sa_flags |= SA_RESTART;
480 saction.sa_flags |= SA_NOCLDWAIT;
483 /* Signal-Handler einhaengen */
484 sigaction( SIGINT, &saction, NULL );
485 sigaction( SIGQUIT, &saction, NULL );
486 sigaction( SIGTERM, &saction, NULL);
487 sigaction( SIGHUP, &saction, NULL);
488 sigaction( SIGCHLD, &saction, NULL);
490 /* einige Signale ignorieren */
491 saction.sa_handler = SIG_IGN;
492 sigaction( SIGPIPE, &saction, NULL );
494 /* kein sigaction() vorhanden */
496 /* Signal-Handler einhaengen */
497 signal( SIGINT, Signal_Handler );
498 signal( SIGQUIT, Signal_Handler );
499 signal( SIGTERM, Signal_Handler );
500 signal( SIGHUP, Signal_Handler );
501 signal( SIGCHLD, Signal_Handler );
503 /* einige Signale ignorieren */
504 signal( SIGPIPE, SIG_IGN );
506 } /* Initialize_Signal_Handler */
510 * Signal handler of ngIRCd.
511 * This function is called whenever ngIRCd catches a signal sent by the
512 * user and/or the system to it. For example SIGTERM and SIGHUP.
513 * @param Signal Number of the signal to handle.
516 Signal_Handler( int Signal )
523 /* wir soll(t)en uns wohl beenden ... */
524 NGIRCd_SignalQuit = true;
527 /* Konfiguration neu einlesen: */
528 NGIRCd_SignalRehash = true;
531 /* Child-Prozess wurde beendet. Zombies vermeiden: */
532 while( waitpid( -1, NULL, WNOHANG ) > 0);
536 /* unbekanntes bzw. unbehandeltes Signal */
537 Log( LOG_DEBUG, "Got signal %d! Ignored.", Signal );
540 } /* Signal_Handler */
544 * Display copyright and version information of ngIRCd on the console.
549 puts( NGIRCd_Version );
550 puts( "Copyright (c)2001-2005 by Alexander Barton (<alex@barton.de>)." );
551 puts( "Homepage: <http://arthur.ath.cx/~alex/ngircd/>\n" );
552 puts( "This is free software; see the source for copying conditions. There is NO" );
553 puts( "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE." );
558 * Display a short help text on the console.
559 * This help depends on the configuration of the executable and only shows
560 * options that are actually enabled.
566 puts( " -d, --debug log extra debug messages" );
568 puts( " -f, --config <f> use file <f> as configuration file" );
569 puts( " -n, --nodaemon don't fork and don't detach from controlling terminal" );
570 puts( " -p, --passive disable automatic connections to other servers" );
572 puts( " -s, --sniffer enable network sniffer and display all IRC traffic" );
574 puts( " -t, --configtest read, validate and display configuration; then exit" );
575 puts( " --version output version information and exit" );
576 puts( " --help display this help and exit" );
581 * Delete the file containing the process ID (PID).
584 Pidfile_Delete( void )
586 /* Pidfile configured? */
587 if( ! Conf_PidFile[0] ) return;
590 Log( LOG_DEBUG, "Removing PID file (%s) ...", Conf_PidFile );
593 if( unlink( Conf_PidFile ))
594 Log( LOG_ERR, "Error unlinking PID file (%s): %s", Conf_PidFile, strerror( errno ));
595 } /* Pidfile_Delete */
599 * Create the file containing the process ID of ngIRCd ("PID file").
600 * @param pid The process ID to be stored in this file.
603 Pidfile_Create( long pid )
609 /* Pidfile configured? */
610 if( ! Conf_PidFile[0] ) return;
613 Log( LOG_DEBUG, "Creating PID file (%s) ...", Conf_PidFile );
616 pidfd = open( Conf_PidFile, O_RDWR|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
618 Log( LOG_ERR, "Error writing PID file (%s): %s", Conf_PidFile, strerror( errno ));
622 len = snprintf( pidbuf, sizeof pidbuf, "%ld\n", pid );
623 if (len < 0|| len < (int)sizeof pid) {
624 Log( LOG_ERR, "Error converting pid");
628 if( write( pidfd, pidbuf, len) != len)
629 Log( LOG_ERR, "Can't write PID file (%s): %s", Conf_PidFile, strerror( errno ));
631 if( close(pidfd) != 0 )
632 Log( LOG_ERR, "Error closing PID file (%s): %s", Conf_PidFile, strerror( errno ));
633 } /* Pidfile_Create */
637 * Redirect stdin, stdout and stderr to apropriate file handles.
640 Setup_FDStreams( void )
644 /* Test if we can open /dev/null for reading and writing. If not
645 * we are most probably chrooted already and the server has been
646 * restarted. So we simply don't try to redirect stdXXX ... */
647 fd = open( "/dev/null", O_RDWR );
649 Log(LOG_WARNING, "Could not open /dev/null: %s", strerror(errno));
656 /* Create new stdin(0), stdout(1) and stderr(2) descriptors */
657 dup2( fd, 0 ); dup2( fd, 1 ); dup2( fd, 2 );
659 /* Close newly opened file descriptor if not stdin/out/err */
660 if( fd > 2 ) close( fd );
661 } /* Setup_FDStreams */
665 NGIRCd_getNobodyID(unsigned int *uid, unsigned int *gid )
669 pwd = getpwnam("nobody");
670 if (!pwd) return false;
672 if ( !pwd->pw_uid || !pwd->pw_gid)
684 NGIRCd_Init( bool NGIRCd_NoDaemon )
686 static bool initialized;
687 bool chrooted = false;
696 if( Conf_Chroot[0] ) {
697 if( chdir( Conf_Chroot ) != 0 ) {
698 Log( LOG_ERR, "Can't chdir() in ChrootDir (%s): %s", Conf_Chroot, strerror( errno ));
702 if( chroot( Conf_Chroot ) != 0 ) {
703 if (errno != EPERM) {
704 Log( LOG_ERR, "Can't change root directory to \"%s\": %s",
705 Conf_Chroot, strerror( errno ));
711 Log( LOG_INFO, "Changed root and working directory to \"%s\".", Conf_Chroot );
715 if ( Conf_UID == 0 ) {
716 Log( LOG_INFO, "Conf_UID must not be 0, switching to user nobody", Conf_UID );
718 if (!NGIRCd_getNobodyID(&Conf_UID, &Conf_GID )) {
719 Log( LOG_WARNING, "Could not get uid/gid of user nobody: %s",
720 errno ? strerror(errno) : "not found" );
725 if( setgid( Conf_GID ) != 0 ) {
727 Log( LOG_ERR, "Can't change group ID to %u: %s", Conf_GID, strerror( errno ));
728 if (real_errno != EPERM)
732 if( setuid( Conf_UID ) != 0 ) {
734 Log( LOG_ERR, "Can't change user ID to %u: %s", Conf_UID, strerror( errno ));
735 if (real_errno != EPERM)
741 /* Normally a child process is forked which isn't any longer
742 * connected to ther controlling terminal. Use "--nodaemon"
743 * to disable this "daemon mode" (useful for debugging). */
744 if ( ! NGIRCd_NoDaemon ) {
747 /* "Old" process: exit. */
752 fprintf( stderr, "%s: Can't fork: %s!\nFatal error, exiting now ...\n",
753 PACKAGE_NAME, strerror( errno ));
757 /* New child process */
761 /* Detach stdin, stdout and stderr */
766 Pidfile_Create( pid );
768 /* check uid we are running as, can be different from values configured (e.g. if we were already
769 started with a uid > 0 */
773 assert( Conf_GID > 0);
774 assert( Conf_UID > 0);
776 pwd = getpwuid( Conf_UID );
777 grp = getgrgid( Conf_GID );
778 Log( LOG_INFO, "Running as user %s(%ld), group %s(%ld), with PID %ld.",
779 pwd ? pwd->pw_name : "unknown", Conf_UID,
780 grp ? grp->gr_name : "unknown", Conf_GID, pid);
783 Log( LOG_INFO, "Running chrooted, chrootdir \"%s\".", Conf_Chroot );
786 Log( LOG_INFO, "Not running chrooted." );
789 /* Change working directory to home directory of the user
790 * we are running as (only when running in daemon mode and not in chroot) */
793 if (!NGIRCd_NoDaemon ) {
794 if( chdir( pwd->pw_dir ) == 0 )
795 Log( LOG_DEBUG, "Changed working directory to \"%s\" ...", pwd->pw_dir );
797 Log( LOG_ERR, "Can't change working directory to \"%s\": %s",
798 pwd->pw_dir, strerror( errno ));
801 Log( LOG_ERR, "Can't get user informaton for UID %d!?", Conf_UID );