2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2010 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>
36 #if defined(DEBUG) && defined(HAVE_MTRACE)
48 #include "sighandlers.h"
53 #include "rendezvous.h"
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(( int fd ));
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;
88 #if defined(DEBUG) && defined(HAVE_MTRACE)
89 /* enable GNU libc memory tracing when running in debug mode
90 * and functionality available */
96 NGIRCd_SignalQuit = NGIRCd_SignalRestart = NGIRCd_SignalRehash = false;
97 NGIRCd_Passive = false;
102 NGIRCd_Sniffer = false;
104 strlcpy( NGIRCd_ConfFile, SYSCONFDIR, sizeof( NGIRCd_ConfFile ));
105 strlcat( NGIRCd_ConfFile, CONFIG_FILE, sizeof( NGIRCd_ConfFile ));
109 /* parse conmmand line */
110 for( i = 1; i < argc; i++ )
113 if(( argv[i][0] == '-' ) && ( argv[i][1] == '-' ))
116 if( strcmp( argv[i], "--config" ) == 0 )
120 /* Ok, there's an parameter left */
121 strlcpy( NGIRCd_ConfFile, argv[i + 1], sizeof( NGIRCd_ConfFile ));
127 if( strcmp( argv[i], "--configtest" ) == 0 )
133 if( strcmp( argv[i], "--debug" ) == 0 )
139 if( strcmp( argv[i], "--help" ) == 0 )
142 puts( "" ); Show_Help( ); puts( "" );
145 if( strcmp( argv[i], "--nodaemon" ) == 0 )
147 NGIRCd_NoDaemon = true;
150 if( strcmp( argv[i], "--passive" ) == 0 )
152 NGIRCd_Passive = true;
156 if( strcmp( argv[i], "--sniffer" ) == 0 )
158 NGIRCd_Sniffer = true;
162 if( strcmp( argv[i], "--version" ) == 0 )
168 else if(( argv[i][0] == '-' ) && ( argv[i][1] != '-' ))
171 for( n = 1; n < strlen( argv[i] ); n++ )
175 if (argv[i][n] == 'd') {
180 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] );
193 if (argv[i][n] == 'h') {
195 puts(""); Show_Help(); puts("");
199 if (argv[i][n] == 'n') {
200 NGIRCd_NoDaemon = true;
203 if (argv[i][n] == 'p') {
204 NGIRCd_Passive = true;
208 if (argv[i][n] == 's') {
209 NGIRCd_Sniffer = true;
213 if (argv[i][n] == 't') {
218 if (argv[i][n] == 'V') {
224 printf( "%s: invalid option \"-%c\"!\n", PACKAGE_NAME, argv[i][n] );
225 printf( "Try \"%s --help\" for more information.\n", PACKAGE_NAME );
233 printf( "%s: invalid option \"%s\"!\n", PACKAGE_NAME, argv[i] );
234 printf( "Try \"%s --help\" for more information.\n", PACKAGE_NAME );
239 /* Debug-Level (for IRCs "VERSION" command) */
240 NGIRCd_DebugLevel[0] = '\0';
242 if( NGIRCd_Debug ) strcpy( NGIRCd_DebugLevel, "1" );
248 strcpy( NGIRCd_DebugLevel, "2" );
254 Show_Version( ); puts( "" );
258 while( ! NGIRCd_SignalQuit )
260 /* Initialize global variables */
261 NGIRCd_Start = time( NULL );
262 (void)strftime( NGIRCd_StartStr, 64, "%a %b %d %Y at %H:%M:%S (%Z)", localtime( &NGIRCd_Start ));
264 NGIRCd_SignalRehash = false;
265 NGIRCd_SignalRestart = false;
266 NGIRCd_SignalQuit = false;
268 /* Initialize modules, part I */
269 Log_Init( ! NGIRCd_NoDaemon );
272 /* Initialize the "main program": chroot environment, user and
274 if (!NGIRCd_Init(NGIRCd_NoDaemon)) {
275 Log(LOG_ALERT, "Fatal: Initialization failed");
279 /* Initialize modules, part II: these functions are eventually
280 * called with already dropped privileges ... */
289 /* Redirect stderr handle to "error file" for debugging
290 * when not running in "no daemon" mode: */
291 if( ! NGIRCd_NoDaemon ) Log_InitErrorfile( );
293 if (!io_library_init(CONNECTION_POOL)) {
294 Log(LOG_ALERT, "Fatal: Cannot initialize IO routines: %s", strerror(errno));
298 if (!Signals_Init()) {
299 Log(LOG_ALERT, "Fatal: Could not set up signal handlers: %s", strerror(errno));
304 * create protocol and server identification.
305 * The syntax used by ngIRCd in PASS commands and the extended flags
306 * are described in doc/Protocol.txt
309 snprintf( NGIRCd_ProtoID, sizeof NGIRCd_ProtoID, "%s%s %s|%s:%s", PROTOVER, PROTOIRCPLUS, PACKAGE_NAME, PACKAGE_VERSION, IRCPLUSFLAGS );
311 strcat( NGIRCd_ProtoID, "Z" );
313 if( Conf_OperCanMode ) strcat( NGIRCd_ProtoID, "o" );
315 snprintf( NGIRCd_ProtoID, sizeof NGIRCd_ProtoID, "%s%s %s|%s", PROTOVER, PROTOIRC, PACKAGE_NAME, PACKAGE_VERSION );
317 strlcat( NGIRCd_ProtoID, " P", sizeof NGIRCd_ProtoID );
319 strlcat( NGIRCd_ProtoID, "Z", sizeof NGIRCd_ProtoID );
321 LogDebug("Protocol and server ID is \"%s\".", NGIRCd_ProtoID);
323 Channel_InitPredefined( );
325 if( Conn_InitListeners( ) < 1 )
327 Log( LOG_ALERT, "Server isn't listening on a single port!" );
328 Log( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
352 * Generate ngIRCd "version string".
353 * This string is generated once and then stored in NGIRCd_Version for
354 * further usage, for example by the IRC command VERSION and the --version
355 * command line switch.
360 NGIRCd_VersionAddition[0] = '\0';
363 strlcpy( NGIRCd_VersionAddition, "SYSLOG", sizeof NGIRCd_VersionAddition );
366 if( NGIRCd_VersionAddition[0] )
367 strlcat( NGIRCd_VersionAddition, "+", sizeof NGIRCd_VersionAddition );
368 strlcat( NGIRCd_VersionAddition, "ZLIB", sizeof NGIRCd_VersionAddition );
371 if ( NGIRCd_VersionAddition[0] ) strlcat( NGIRCd_VersionAddition, "+", sizeof NGIRCd_VersionAddition );
372 strlcat( NGIRCd_VersionAddition, "SSL", sizeof NGIRCd_VersionAddition );
375 if( NGIRCd_VersionAddition[0] )
376 strlcat( NGIRCd_VersionAddition, "+", sizeof NGIRCd_VersionAddition );
377 strlcat( NGIRCd_VersionAddition, "TCPWRAP", sizeof NGIRCd_VersionAddition );
380 if( NGIRCd_VersionAddition[0] )
381 strlcat( NGIRCd_VersionAddition, "+", sizeof NGIRCd_VersionAddition );
382 strlcat( NGIRCd_VersionAddition, "ZEROCONF", sizeof NGIRCd_VersionAddition );
385 if( NGIRCd_VersionAddition[0] )
386 strlcat( NGIRCd_VersionAddition, "+", sizeof NGIRCd_VersionAddition );
387 strlcat( NGIRCd_VersionAddition, "IDENT", sizeof NGIRCd_VersionAddition );
390 if (NGIRCd_VersionAddition[0])
391 strlcat(NGIRCd_VersionAddition, "+", sizeof NGIRCd_VersionAddition);
392 strlcat(NGIRCd_VersionAddition, "PAM", sizeof NGIRCd_VersionAddition);
395 if( NGIRCd_VersionAddition[0] )
396 strlcat( NGIRCd_VersionAddition, "+", sizeof NGIRCd_VersionAddition );
397 strlcat( NGIRCd_VersionAddition, "DEBUG", sizeof NGIRCd_VersionAddition );
400 if( NGIRCd_VersionAddition[0] )
401 strlcat( NGIRCd_VersionAddition, "+", sizeof NGIRCd_VersionAddition );
402 strlcat( NGIRCd_VersionAddition, "SNIFFER", sizeof NGIRCd_VersionAddition );
405 if( NGIRCd_VersionAddition[0] )
406 strlcat( NGIRCd_VersionAddition, "+", sizeof NGIRCd_VersionAddition );
407 strlcat( NGIRCd_VersionAddition, "RFC", sizeof NGIRCd_VersionAddition );
410 if( NGIRCd_VersionAddition[0] )
411 strlcat( NGIRCd_VersionAddition, "+", sizeof NGIRCd_VersionAddition );
412 strlcat( NGIRCd_VersionAddition, "IRCPLUS", sizeof NGIRCd_VersionAddition );
415 if (NGIRCd_VersionAddition[0])
416 strlcat(NGIRCd_VersionAddition, "+", sizeof(NGIRCd_VersionAddition));
417 strlcat(NGIRCd_VersionAddition, "IPv6", sizeof(NGIRCd_VersionAddition));
419 if( NGIRCd_VersionAddition[0] )
420 strlcat( NGIRCd_VersionAddition, "-", sizeof( NGIRCd_VersionAddition ));
422 strlcat( NGIRCd_VersionAddition, TARGET_CPU, sizeof( NGIRCd_VersionAddition ));
423 strlcat( NGIRCd_VersionAddition, "/", sizeof( NGIRCd_VersionAddition ));
424 strlcat( NGIRCd_VersionAddition, TARGET_VENDOR, sizeof( NGIRCd_VersionAddition ));
425 strlcat( NGIRCd_VersionAddition, "/", sizeof( NGIRCd_VersionAddition ));
426 strlcat( NGIRCd_VersionAddition, TARGET_OS, sizeof( NGIRCd_VersionAddition ));
428 snprintf(NGIRCd_Version, sizeof NGIRCd_Version, "%s %s-%s",
429 PACKAGE_NAME, PACKAGE_VERSION, NGIRCd_VersionAddition);
434 * Reload the server configuration file.
437 NGIRCd_Rehash( void )
439 char old_name[CLIENT_ID_LEN];
440 unsigned old_nicklen;
442 Log( LOG_NOTICE|LOG_snotice, "Re-reading configuration NOW!" );
443 NGIRCd_SignalRehash = false;
445 /* Remember old server name and nick name length */
446 strlcpy( old_name, Conf_ServerName, sizeof old_name );
447 old_nicklen = Conf_MaxNickLength;
449 /* Re-read configuration ... */
453 /* Close down all listening sockets */
454 Conn_ExitListeners( );
456 /* Recover old server name and nick name length: these values can't
457 * be changed during run-time */
458 if (strcmp(old_name, Conf_ServerName) != 0 ) {
459 strlcpy(Conf_ServerName, old_name, sizeof Conf_ServerName);
460 Log(LOG_ERR, "Can't change \"ServerName\" on runtime! Ignored new name.");
462 if (old_nicklen != Conf_MaxNickLength) {
463 Conf_MaxNickLength = old_nicklen;
464 Log(LOG_ERR, "Can't change \"MaxNickLength\" on runtime! Ignored new value.");
467 /* Create new pre-defined channels */
468 Channel_InitPredefined( );
470 if (!ConnSSL_InitLibrary())
471 Log(LOG_WARNING, "Re-Initializing SSL failed, using old keys");
473 /* Start listening on sockets */
474 Conn_InitListeners( );
476 /* Sync configuration with established connections */
477 Conn_SyncServerStruct( );
479 Log( LOG_NOTICE|LOG_snotice, "Re-reading of configuration done." );
480 } /* NGIRCd_Rehash */
484 * Display copyright and version information of ngIRCd on the console.
489 puts( NGIRCd_Version );
490 puts( "Copyright (c)2001-2010 Alexander Barton (<alex@barton.de>) and Contributors." );
491 puts( "Homepage: <http://ngircd.barton.de/>\n" );
492 puts( "This is free software; see the source for copying conditions. There is NO" );
493 puts( "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE." );
498 * Display a short help text on the console.
499 * This help depends on the configuration of the executable and only shows
500 * options that are actually enabled.
506 puts( " -d, --debug log extra debug messages" );
508 puts( " -f, --config <f> use file <f> as configuration file" );
509 puts( " -n, --nodaemon don't fork and don't detach from controlling terminal" );
510 puts( " -p, --passive disable automatic connections to other servers" );
512 puts( " -s, --sniffer enable network sniffer and display all IRC traffic" );
514 puts( " -t, --configtest read, validate and display configuration; then exit" );
515 puts( " -V, --version output version information and exit" );
516 puts( " -h, --help display this help and exit" );
521 * Delete the file containing the process ID (PID).
524 Pidfile_Delete( void )
526 /* Pidfile configured? */
527 if( ! Conf_PidFile[0] ) return;
530 Log( LOG_DEBUG, "Removing PID file (%s) ...", Conf_PidFile );
533 if( unlink( Conf_PidFile ))
534 Log( LOG_ERR, "Error unlinking PID file (%s): %s", Conf_PidFile, strerror( errno ));
535 } /* Pidfile_Delete */
539 * Create the file containing the process ID of ngIRCd ("PID file").
540 * @param pid The process ID to be stored in this file.
543 Pidfile_Create(pid_t pid)
549 /* Pidfile configured? */
550 if( ! Conf_PidFile[0] ) return;
553 Log( LOG_DEBUG, "Creating PID file (%s) ...", Conf_PidFile );
556 pidfd = open( Conf_PidFile, O_RDWR|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
558 Log( LOG_ERR, "Error writing PID file (%s): %s", Conf_PidFile, strerror( errno ));
562 len = snprintf(pidbuf, sizeof pidbuf, "%ld\n", (long)pid);
563 if (len < 0 || len >= (int)sizeof pidbuf) {
564 Log( LOG_ERR, "Error converting pid");
568 if (write(pidfd, pidbuf, (size_t)len) != (ssize_t)len)
569 Log( LOG_ERR, "Can't write PID file (%s): %s", Conf_PidFile, strerror( errno ));
571 if( close(pidfd) != 0 )
572 Log( LOG_ERR, "Error closing PID file (%s): %s", Conf_PidFile, strerror( errno ));
573 } /* Pidfile_Create */
577 * Redirect stdin, stdout and stderr to apropriate file handles.
580 Setup_FDStreams(int fd)
588 /* Create new stdin(0), stdout(1) and stderr(2) descriptors */
589 dup2( fd, 0 ); dup2( fd, 1 ); dup2( fd, 2 );
590 } /* Setup_FDStreams */
594 NGIRCd_getNobodyID(uid_t *uid, gid_t *gid )
600 * It can return EINVAL instead of EPERM
601 * so, if we are already unprivileged,
602 * use id of current user.
604 if (geteuid() && getuid()) {
611 pwd = getpwnam("nobody");
612 if (!pwd) return false;
614 if ( !pwd->pw_uid || !pwd->pw_gid)
626 NGIRCd_Init( bool NGIRCd_NoDaemon )
628 static bool initialized;
629 bool chrooted = false;
632 int real_errno, fd = -1;
638 if (!NGIRCd_NoDaemon) {
639 /* open /dev/null before chroot() */
640 fd = open( "/dev/null", O_RDWR);
642 Log(LOG_WARNING, "Could not open /dev/null: %s", strerror(errno));
645 if (!ConnSSL_InitLibrary())
647 "Warning: Error during SSL initialization, continuing ...");
649 if( Conf_Chroot[0] ) {
650 if( chdir( Conf_Chroot ) != 0 ) {
651 Log( LOG_ERR, "Can't chdir() in ChrootDir (%s): %s", Conf_Chroot, strerror( errno ));
655 if( chroot( Conf_Chroot ) != 0 ) {
656 if (errno != EPERM) {
657 Log( LOG_ERR, "Can't change root directory to \"%s\": %s",
658 Conf_Chroot, strerror( errno ));
663 Log( LOG_INFO, "Changed root and working directory to \"%s\".", Conf_Chroot );
668 Log(LOG_INFO, "ServerUID must not be 0, using \"nobody\" instead.", Conf_UID);
670 if (! NGIRCd_getNobodyID(&Conf_UID, &Conf_GID)) {
671 Log(LOG_WARNING, "Could not get user/group ID of user \"nobody\": %s",
672 errno ? strerror(errno) : "not found" );
677 if (getgid() != Conf_GID) {
678 /* Change group ID */
679 if (setgid(Conf_GID) != 0) {
681 Log( LOG_ERR, "Can't change group ID to %u: %s", Conf_GID, strerror( errno ));
682 if (real_errno != EPERM)
687 if (getuid() != Conf_UID) {
689 if (setuid(Conf_UID) != 0) {
691 Log(LOG_ERR, "Can't change user ID to %u: %s", Conf_UID, strerror(errno));
692 if (real_errno != EPERM)
699 /* Normally a child process is forked which isn't any longer
700 * connected to ther controlling terminal. Use "--nodaemon"
701 * to disable this "daemon mode" (useful for debugging). */
702 if ( ! NGIRCd_NoDaemon ) {
705 /* "Old" process: exit. */
710 fprintf( stderr, "%s: Can't fork: %s!\nFatal error, exiting now ...\n",
711 PACKAGE_NAME, strerror( errno ));
715 /* New child process */
719 setpgrp(0, getpid());
721 if (chdir( "/" ) != 0)
722 Log(LOG_ERR, "Can't change directory to '/': %s",
725 /* Detach stdin, stdout and stderr */
734 Pidfile_Create( pid );
736 /* Check UID/GID we are running as, can be different from values
737 * configured (e. g. if we were already started with a UID>0. */
741 pwd = getpwuid( Conf_UID );
742 grp = getgrgid( Conf_GID );
744 Log(LOG_INFO, "Running as user %s(%ld), group %s(%ld), with PID %ld.",
745 pwd ? pwd->pw_name : "unknown", (long)Conf_UID,
746 grp ? grp->gr_name : "unknown", (long)Conf_GID, (long)pid);
749 Log(LOG_INFO, "Running with root directory \"%s\".",
753 Log(LOG_INFO, "Not running with changed root directory.");
755 /* Change working directory to home directory of the user
756 * we are running as (only when running in daemon mode and not in chroot) */
759 if (!NGIRCd_NoDaemon ) {
760 if( chdir( pwd->pw_dir ) == 0 )
761 Log( LOG_DEBUG, "Changed working directory to \"%s\" ...", pwd->pw_dir );
763 Log( LOG_INFO, "Notice: Can't change working directory to \"%s\": %s",
764 pwd->pw_dir, strerror( errno ));
767 Log( LOG_ERR, "Can't get user informaton for UID %d!?", Conf_UID );