2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2008 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.
17 * The main program, including the C function main() which is called
18 * by the loader of the operating system.
30 #include <sys/types.h>
43 #include "cvs-version.h"
50 #include "rendezvous.h"
57 static void Initialize_Signal_Handler PARAMS(( void ));
58 static void Signal_Handler PARAMS(( int Signal ));
60 static void Show_Version PARAMS(( void ));
61 static void Show_Help PARAMS(( void ));
63 static void Pidfile_Create PARAMS(( pid_t pid ));
64 static void Pidfile_Delete PARAMS(( void ));
66 static void Fill_Version PARAMS(( void ));
68 static void Setup_FDStreams PARAMS(( void ));
70 static bool NGIRCd_Init PARAMS(( bool ));
73 * The main() function of ngIRCd.
74 * Here all starts: this function is called by the operating system loader,
75 * it is the first portion of code executed of ngIRCd.
76 * @param argc The number of arguments passed to ngIRCd on the command line.
77 * @param argv An array containing all the arguments passed to ngIRCd.
78 * @return Global exit code of ngIRCd, zero on success.
81 main( int argc, const char *argv[] )
83 bool ok, configtest = false;
84 bool NGIRCd_NoDaemon = false;
90 NGIRCd_SignalQuit = NGIRCd_SignalRestart = NGIRCd_SignalRehash = false;
91 NGIRCd_Passive = false;
96 NGIRCd_Sniffer = false;
98 strlcpy( NGIRCd_ConfFile, SYSCONFDIR, sizeof( NGIRCd_ConfFile ));
99 strlcat( NGIRCd_ConfFile, CONFIG_FILE, sizeof( NGIRCd_ConfFile ));
103 /* Kommandozeile parsen */
104 for( i = 1; i < argc; i++ )
107 if(( argv[i][0] == '-' ) && ( argv[i][1] == '-' ))
111 if( strcmp( argv[i], "--config" ) == 0 )
115 /* Ok, there's an parameter left */
116 strlcpy( NGIRCd_ConfFile, argv[i + 1], sizeof( NGIRCd_ConfFile ));
122 if( strcmp( argv[i], "--configtest" ) == 0 )
128 if( strcmp( argv[i], "--debug" ) == 0 )
134 if( strcmp( argv[i], "--help" ) == 0 )
137 puts( "" ); Show_Help( ); puts( "" );
140 if( strcmp( argv[i], "--nodaemon" ) == 0 )
142 NGIRCd_NoDaemon = true;
145 if( strcmp( argv[i], "--passive" ) == 0 )
147 NGIRCd_Passive = true;
151 if( strcmp( argv[i], "--sniffer" ) == 0 )
153 NGIRCd_Sniffer = true;
157 if( strcmp( argv[i], "--version" ) == 0 )
163 else if(( argv[i][0] == '-' ) && ( argv[i][1] != '-' ))
166 for( n = 1; n < strlen( argv[i] ); n++ )
170 if( argv[i][n] == 'd' )
176 if( argv[i][n] == 'f' )
178 if(( ! argv[i][n + 1] ) && ( i + 1 < argc ))
180 /* Ok, next character is a blank */
181 strlcpy( NGIRCd_ConfFile, argv[i + 1], sizeof( NGIRCd_ConfFile ));
183 /* go to the following parameter */
185 n = strlen( argv[i] );
189 if( argv[i][n] == 'n' )
191 NGIRCd_NoDaemon = true;
194 if( argv[i][n] == 'p' )
196 NGIRCd_Passive = true;
200 if( argv[i][n] == 's' )
202 NGIRCd_Sniffer = true;
206 if( argv[i][n] == 't' )
214 printf( "%s: invalid option \"-%c\"!\n", PACKAGE_NAME, argv[i][n] );
215 printf( "Try \"%s --help\" for more information.\n", PACKAGE_NAME );
223 printf( "%s: invalid option \"%s\"!\n", PACKAGE_NAME, argv[i] );
224 printf( "Try \"%s --help\" for more information.\n", PACKAGE_NAME );
229 /* Debug-Level (fuer IRC-Befehl "VERSION") ermitteln */
230 NGIRCd_DebugLevel[0] = '\0';
232 if( NGIRCd_Debug ) strcpy( NGIRCd_DebugLevel, "1" );
238 strcpy( NGIRCd_DebugLevel, "2" );
242 /* Soll nur die Konfigurations ueberprueft und ausgegeben werden? */
245 Show_Version( ); puts( "" );
249 while( ! NGIRCd_SignalQuit )
251 /* Initialize global variables */
252 NGIRCd_Start = time( NULL );
253 (void)strftime( NGIRCd_StartStr, 64, "%a %b %d %Y at %H:%M:%S (%Z)", localtime( &NGIRCd_Start ));
255 NGIRCd_SignalRehash = false;
256 NGIRCd_SignalRestart = false;
257 NGIRCd_SignalQuit = false;
259 /* Initialize modules, part I */
260 Log_Init( ! NGIRCd_NoDaemon );
263 /* Initialize the "main program": chroot environment, user and
265 if (!NGIRCd_Init(NGIRCd_NoDaemon)) {
266 Log(LOG_ALERT, "Fatal: Initialization failed");
270 /* Initialize modules, part II: these functions are eventually
271 * called with already dropped privileges ... */
280 /* Redirect stderr handle to "error file" for debugging
281 * when not running in "no daemon" mode: */
282 if( ! NGIRCd_NoDaemon ) Log_InitErrorfile( );
285 /* Signal-Handler initialisieren */
286 Initialize_Signal_Handler( );
288 /* Protokoll- und Server-Identifikation erzeugen. Die vom ngIRCd
289 * beim PASS-Befehl verwendete Syntax sowie die erweiterten Flags
290 * sind in doc/Protocol.txt beschrieben. */
292 snprintf( NGIRCd_ProtoID, sizeof NGIRCd_ProtoID, "%s%s %s|%s:%s", PROTOVER, PROTOIRCPLUS, PACKAGE_NAME, PACKAGE_VERSION, IRCPLUSFLAGS );
294 strcat( NGIRCd_ProtoID, "Z" );
296 if( Conf_OperCanMode ) strcat( NGIRCd_ProtoID, "o" );
298 snprintf( NGIRCd_ProtoID, sizeof NGIRCd_ProtoID, "%s%s %s|%s", PROTOVER, PROTOIRC, PACKAGE_NAME, PACKAGE_VERSION );
300 strlcat( NGIRCd_ProtoID, " P", sizeof NGIRCd_ProtoID );
302 strlcat( NGIRCd_ProtoID, "Z", sizeof NGIRCd_ProtoID );
304 Log( LOG_DEBUG, "Protocol and server ID is \"%s\".", NGIRCd_ProtoID );
306 /* Vordefinierte Channels anlegen */
307 Channel_InitPredefined( );
309 /* Listen-Ports initialisieren */
310 if( Conn_InitListeners( ) < 1 )
312 Log( LOG_ALERT, "Server isn't listening on a single port!" );
313 Log( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
337 * Generate ngIRCd "version string".
338 * This string is generated once and then stored in NGIRCd_Version for
339 * further usage, for example by the IRC command VERSION and the --version
340 * command line switch.
345 NGIRCd_VersionAddition[0] = '\0';
348 strlcpy( NGIRCd_VersionAddition, "SYSLOG", sizeof NGIRCd_VersionAddition );
351 if( NGIRCd_VersionAddition[0] )
352 strlcat( NGIRCd_VersionAddition, "+", sizeof NGIRCd_VersionAddition );
354 strlcat( NGIRCd_VersionAddition, "ZLIB", sizeof NGIRCd_VersionAddition );
357 if( NGIRCd_VersionAddition[0] )
358 strlcat( NGIRCd_VersionAddition, "+", sizeof NGIRCd_VersionAddition );
360 strlcat( NGIRCd_VersionAddition, "TCPWRAP", sizeof NGIRCd_VersionAddition );
363 if( NGIRCd_VersionAddition[0] )
364 strlcat( NGIRCd_VersionAddition, "+", sizeof NGIRCd_VersionAddition );
366 strlcat( NGIRCd_VersionAddition, "ZEROCONF", sizeof NGIRCd_VersionAddition );
369 if( NGIRCd_VersionAddition[0] )
370 strlcat( NGIRCd_VersionAddition, "+", sizeof NGIRCd_VersionAddition );
372 strlcat( NGIRCd_VersionAddition, "IDENT", sizeof NGIRCd_VersionAddition );
375 if( NGIRCd_VersionAddition[0] )
376 strlcat( NGIRCd_VersionAddition, "+", sizeof NGIRCd_VersionAddition );
378 strlcat( NGIRCd_VersionAddition, "DEBUG", sizeof NGIRCd_VersionAddition );
381 if( NGIRCd_VersionAddition[0] )
382 strlcat( NGIRCd_VersionAddition, "+", sizeof NGIRCd_VersionAddition );
384 strlcat( NGIRCd_VersionAddition, "SNIFFER", sizeof NGIRCd_VersionAddition );
387 if( NGIRCd_VersionAddition[0] )
388 strlcat( NGIRCd_VersionAddition, "+", sizeof NGIRCd_VersionAddition );
390 strlcat( NGIRCd_VersionAddition, "RFC", sizeof NGIRCd_VersionAddition );
393 if( NGIRCd_VersionAddition[0] )
394 strlcat( NGIRCd_VersionAddition, "+", sizeof NGIRCd_VersionAddition );
396 strlcat( NGIRCd_VersionAddition, "IRCPLUS", sizeof NGIRCd_VersionAddition );
399 if (NGIRCd_VersionAddition[0])
400 strlcat(NGIRCd_VersionAddition, "+", sizeof(NGIRCd_VersionAddition));
402 strlcat(NGIRCd_VersionAddition, "IPv6", sizeof(NGIRCd_VersionAddition));
404 if( NGIRCd_VersionAddition[0] )
405 strlcat( NGIRCd_VersionAddition, "-", sizeof( NGIRCd_VersionAddition ));
407 strlcat( NGIRCd_VersionAddition, TARGET_CPU, sizeof( NGIRCd_VersionAddition ));
408 strlcat( NGIRCd_VersionAddition, "/", sizeof( NGIRCd_VersionAddition ));
409 strlcat( NGIRCd_VersionAddition, TARGET_VENDOR, sizeof( NGIRCd_VersionAddition ));
410 strlcat( NGIRCd_VersionAddition, "/", sizeof( NGIRCd_VersionAddition ));
411 strlcat( NGIRCd_VersionAddition, TARGET_OS, sizeof( NGIRCd_VersionAddition ));
414 snprintf( NGIRCd_Version, sizeof NGIRCd_Version,"%s %s(%s)-%s", PACKAGE_NAME, PACKAGE_VERSION, CVSDATE, NGIRCd_VersionAddition);
416 snprintf( NGIRCd_Version, sizeof NGIRCd_Version, "%s %s-%s", PACKAGE_NAME, PACKAGE_VERSION, NGIRCd_VersionAddition);
422 * Reload the server configuration file.
425 NGIRCd_Rehash( void )
427 char old_name[CLIENT_ID_LEN];
428 unsigned old_nicklen;
430 Log( LOG_NOTICE|LOG_snotice, "Re-reading configuration NOW!" );
431 NGIRCd_SignalRehash = false;
433 /* Remember old server name and nick name length */
434 strlcpy( old_name, Conf_ServerName, sizeof old_name );
435 old_nicklen = Conf_MaxNickLength;
437 /* Re-read configuration ... */
441 /* Close down all listening sockets */
442 Conn_ExitListeners( );
444 /* Recover old server name and nick name length: these values can't
445 * be changed during run-time */
446 if (strcmp(old_name, Conf_ServerName) != 0 ) {
447 strlcpy(Conf_ServerName, old_name, sizeof Conf_ServerName);
448 Log(LOG_ERR, "Can't change \"ServerName\" on runtime! Ignored new name.");
450 if (old_nicklen != Conf_MaxNickLength) {
451 Conf_MaxNickLength = old_nicklen;
452 Log(LOG_ERR, "Can't change \"MaxNickLength\" on runtime! Ignored new value.");
455 /* Create new pre-defined channels */
456 Channel_InitPredefined( );
458 /* Start listening on sockets */
459 Conn_InitListeners( );
461 /* Sync configuration with established connections */
462 Conn_SyncServerStruct( );
464 Log( LOG_NOTICE|LOG_snotice, "Re-reading of configuration done." );
465 } /* NGIRCd_Rehash */
469 * Initialize the signal handler.
472 Initialize_Signal_Handler( void )
474 /* Signal-Handler initialisieren: einige Signale
475 * werden ignoriert, andere speziell behandelt. */
477 #ifdef HAVE_SIGACTION
478 /* sigaction() ist vorhanden */
480 struct sigaction saction;
482 /* Signal-Struktur initialisieren */
483 memset( &saction, 0, sizeof( saction ));
484 saction.sa_handler = Signal_Handler;
486 saction.sa_flags |= SA_RESTART;
489 saction.sa_flags |= SA_NOCLDWAIT;
492 /* Signal-Handler einhaengen */
493 sigaction(SIGINT, &saction, NULL);
494 sigaction(SIGQUIT, &saction, NULL);
495 sigaction(SIGTERM, &saction, NULL);
496 sigaction(SIGHUP, &saction, NULL);
497 sigaction(SIGCHLD, &saction, NULL);
499 /* einige Signale ignorieren */
500 saction.sa_handler = SIG_IGN;
501 sigaction(SIGPIPE, &saction, NULL);
503 /* kein sigaction() vorhanden */
505 /* Signal-Handler einhaengen */
506 signal(SIGINT, Signal_Handler);
507 signal(SIGQUIT, Signal_Handler);
508 signal(SIGTERM, Signal_Handler);
509 signal(SIGHUP, Signal_Handler);
510 signal(SIGCHLD, Signal_Handler);
512 /* einige Signale ignorieren */
513 signal(SIGPIPE, SIG_IGN);
515 } /* Initialize_Signal_Handler */
519 * Signal handler of ngIRCd.
520 * This function is called whenever ngIRCd catches a signal sent by the
521 * user and/or the system to it. For example SIGTERM and SIGHUP.
522 * @param Signal Number of the signal to handle.
525 Signal_Handler( int Signal )
532 /* wir soll(t)en uns wohl beenden ... */
533 NGIRCd_SignalQuit = true;
536 /* Konfiguration neu einlesen: */
537 NGIRCd_SignalRehash = true;
540 /* Child-Prozess wurde beendet. Zombies vermeiden: */
541 while( waitpid( -1, NULL, WNOHANG ) > 0);
545 /* unbekanntes bzw. unbehandeltes Signal */
546 Log( LOG_DEBUG, "Got signal %d! Ignored.", Signal );
549 } /* Signal_Handler */
553 * Display copyright and version information of ngIRCd on the console.
558 puts( NGIRCd_Version );
559 puts( "Copyright (c)2001-2008 Alexander Barton (<alex@barton.de>) and Contributors." );
560 puts( "Homepage: <http://ngircd.barton.de/>\n" );
561 puts( "This is free software; see the source for copying conditions. There is NO" );
562 puts( "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE." );
567 * Display a short help text on the console.
568 * This help depends on the configuration of the executable and only shows
569 * options that are actually enabled.
575 puts( " -d, --debug log extra debug messages" );
577 puts( " -f, --config <f> use file <f> as configuration file" );
578 puts( " -n, --nodaemon don't fork and don't detach from controlling terminal" );
579 puts( " -p, --passive disable automatic connections to other servers" );
581 puts( " -s, --sniffer enable network sniffer and display all IRC traffic" );
583 puts( " -t, --configtest read, validate and display configuration; then exit" );
584 puts( " --version output version information and exit" );
585 puts( " --help display this help and exit" );
590 * Delete the file containing the process ID (PID).
593 Pidfile_Delete( void )
595 /* Pidfile configured? */
596 if( ! Conf_PidFile[0] ) return;
599 Log( LOG_DEBUG, "Removing PID file (%s) ...", Conf_PidFile );
602 if( unlink( Conf_PidFile ))
603 Log( LOG_ERR, "Error unlinking PID file (%s): %s", Conf_PidFile, strerror( errno ));
604 } /* Pidfile_Delete */
608 * Create the file containing the process ID of ngIRCd ("PID file").
609 * @param pid The process ID to be stored in this file.
612 Pidfile_Create(pid_t pid)
618 /* Pidfile configured? */
619 if( ! Conf_PidFile[0] ) return;
622 Log( LOG_DEBUG, "Creating PID file (%s) ...", Conf_PidFile );
625 pidfd = open( Conf_PidFile, O_RDWR|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
627 Log( LOG_ERR, "Error writing PID file (%s): %s", Conf_PidFile, strerror( errno ));
631 len = snprintf(pidbuf, sizeof pidbuf, "%ld\n", (long)pid);
632 if (len < 0 || len >= (int)sizeof pidbuf) {
633 Log( LOG_ERR, "Error converting pid");
637 if (write(pidfd, pidbuf, (size_t)len) != (ssize_t)len)
638 Log( LOG_ERR, "Can't write PID file (%s): %s", Conf_PidFile, strerror( errno ));
640 if( close(pidfd) != 0 )
641 Log( LOG_ERR, "Error closing PID file (%s): %s", Conf_PidFile, strerror( errno ));
642 } /* Pidfile_Create */
646 * Redirect stdin, stdout and stderr to apropriate file handles.
649 Setup_FDStreams( void )
653 /* Test if we can open /dev/null for reading and writing. If not
654 * we are most probably chrooted already and the server has been
655 * restarted. So we simply don't try to redirect stdXXX ... */
656 fd = open( "/dev/null", O_RDWR );
658 Log(LOG_WARNING, "Could not open /dev/null: %s", strerror(errno));
665 /* Create new stdin(0), stdout(1) and stderr(2) descriptors */
666 dup2( fd, 0 ); dup2( fd, 1 ); dup2( fd, 2 );
668 /* Close newly opened file descriptor if not stdin/out/err */
669 if( fd > 2 ) close( fd );
670 } /* Setup_FDStreams */
674 NGIRCd_getNobodyID(uid_t *uid, gid_t *gid )
678 pwd = getpwnam("nobody");
679 if (!pwd) return false;
681 if ( !pwd->pw_uid || !pwd->pw_gid)
693 NGIRCd_Init( bool NGIRCd_NoDaemon )
695 static bool initialized;
696 bool chrooted = false;
705 if( Conf_Chroot[0] ) {
706 if( chdir( Conf_Chroot ) != 0 ) {
707 Log( LOG_ERR, "Can't chdir() in ChrootDir (%s): %s", Conf_Chroot, strerror( errno ));
711 if( chroot( Conf_Chroot ) != 0 ) {
712 if (errno != EPERM) {
713 Log( LOG_ERR, "Can't change root directory to \"%s\": %s",
714 Conf_Chroot, strerror( errno ));
720 Log( LOG_INFO, "Changed root and working directory to \"%s\".", Conf_Chroot );
725 Log(LOG_INFO, "ServerUID must not be 0, using \"nobody\" instead.", Conf_UID);
727 if (! NGIRCd_getNobodyID(&Conf_UID, &Conf_GID)) {
728 Log(LOG_WARNING, "Could not get user/group ID of user \"nobody\": %s",
729 errno ? strerror(errno) : "not found" );
734 if (getgid() != Conf_GID) {
735 /* Change group ID */
736 if (setgid(Conf_GID) != 0) {
738 Log( LOG_ERR, "Can't change group ID to %u: %s", Conf_GID, strerror( errno ));
739 if (real_errno != EPERM)
744 if (getuid() != Conf_UID) {
746 if (setuid(Conf_UID) != 0) {
748 Log(LOG_ERR, "Can't change user ID to %u: %s", Conf_UID, strerror(errno));
749 if (real_errno != EPERM)
756 /* Normally a child process is forked which isn't any longer
757 * connected to ther controlling terminal. Use "--nodaemon"
758 * to disable this "daemon mode" (useful for debugging). */
759 if ( ! NGIRCd_NoDaemon ) {
762 /* "Old" process: exit. */
767 fprintf( stderr, "%s: Can't fork: %s!\nFatal error, exiting now ...\n",
768 PACKAGE_NAME, strerror( errno ));
772 /* New child process */
776 /* Detach stdin, stdout and stderr */
781 Pidfile_Create( pid );
783 /* Check UID/GID we are running as, can be different from values
784 * configured (e. g. if we were already started with a UID>0. */
788 pwd = getpwuid( Conf_UID );
789 grp = getgrgid( Conf_GID );
791 Log( LOG_INFO, "Running as user %s(%ld), group %s(%ld), with PID %ld.",
792 pwd ? pwd->pw_name : "unknown", Conf_UID,
793 grp ? grp->gr_name : "unknown", Conf_GID, pid);
796 Log( LOG_INFO, "Running chrooted, chrootdir \"%s\".", Conf_Chroot );
799 Log( LOG_INFO, "Not running chrooted." );
802 /* Change working directory to home directory of the user
803 * we are running as (only when running in daemon mode and not in chroot) */
806 if (!NGIRCd_NoDaemon ) {
807 if( chdir( pwd->pw_dir ) == 0 )
808 Log( LOG_DEBUG, "Changed working directory to \"%s\" ...", pwd->pw_dir );
810 Log( LOG_INFO, "Notice: Can't change working directory to \"%s\": %s",
811 pwd->pw_dir, strerror( errno ));
814 Log( LOG_ERR, "Can't get user informaton for UID %d!?", Conf_UID );