2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2011 Alexander Barton (alex@barton.de) and Contributors.
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.
16 * The main program, including the C function main() which is called
17 * by the loader of the operating system.
29 #include <sys/types.h>
35 #if defined(DEBUG) && defined(HAVE_MTRACE)
47 #include "sighandlers.h"
55 static void Show_Version PARAMS(( void ));
56 static void Show_Help PARAMS(( void ));
58 static void Pidfile_Create PARAMS(( pid_t pid ));
59 static void Pidfile_Delete PARAMS(( void ));
61 static void Fill_Version PARAMS(( void ));
63 static void Setup_FDStreams PARAMS(( int fd ));
65 static bool NGIRCd_Init PARAMS(( bool ));
69 * The main() function of ngIRCd.
71 * Here all starts: this function is called by the operating system loader,
72 * it is the first portion of code executed of ngIRCd.
74 * @param argc The number of arguments passed to ngIRCd on the command line.
75 * @param argv An array containing all the arguments passed to ngIRCd.
76 * @return Global exit code of ngIRCd, zero on success.
79 main( int argc, const char *argv[] )
81 bool ok, configtest = false;
82 bool NGIRCd_NoDaemon = false;
86 #if defined(DEBUG) && defined(HAVE_MTRACE)
87 /* enable GNU libc memory tracing when running in debug mode
88 * and functionality available */
94 NGIRCd_SignalQuit = NGIRCd_SignalRestart = false;
95 NGIRCd_Passive = false;
100 NGIRCd_Sniffer = false;
102 strlcpy( NGIRCd_ConfFile, SYSCONFDIR, sizeof( NGIRCd_ConfFile ));
103 strlcat( NGIRCd_ConfFile, CONFIG_FILE, sizeof( NGIRCd_ConfFile ));
107 /* parse conmmand line */
108 for( i = 1; i < argc; i++ )
111 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') {
178 if (argv[i][n] == 'f') {
179 if(( ! argv[i][n + 1] ) && ( i + 1 < argc ))
181 /* Ok, next character is a blank */
182 strlcpy( NGIRCd_ConfFile, argv[i + 1], sizeof( NGIRCd_ConfFile ));
184 /* go to the following parameter */
186 n = strlen( argv[i] );
191 if (argv[i][n] == 'h') {
193 puts(""); Show_Help(); puts("");
197 if (argv[i][n] == 'n') {
198 NGIRCd_NoDaemon = true;
201 if (argv[i][n] == 'p') {
202 NGIRCd_Passive = true;
206 if (argv[i][n] == 's') {
207 NGIRCd_Sniffer = true;
211 if (argv[i][n] == 't') {
216 if (argv[i][n] == 'V') {
222 printf( "%s: invalid option \"-%c\"!\n", PACKAGE_NAME, argv[i][n] );
223 printf( "Try \"%s --help\" for more information.\n", PACKAGE_NAME );
231 printf( "%s: invalid option \"%s\"!\n", PACKAGE_NAME, argv[i] );
232 printf( "Try \"%s --help\" for more information.\n", PACKAGE_NAME );
237 /* Debug level for "VERSION" command */
238 NGIRCd_DebugLevel[0] = '\0';
240 if( NGIRCd_Debug ) strcpy( NGIRCd_DebugLevel, "1" );
246 strcpy( NGIRCd_DebugLevel, "2" );
252 Show_Version( ); puts( "" );
256 while( ! NGIRCd_SignalQuit )
258 /* Initialize global variables */
259 NGIRCd_Start = time( NULL );
260 (void)strftime( NGIRCd_StartStr, 64, "%a %b %d %Y at %H:%M:%S (%Z)", localtime( &NGIRCd_Start ));
262 NGIRCd_SignalRestart = false;
263 NGIRCd_SignalQuit = false;
265 /* Initialize modules, part I */
266 Log_Init( ! NGIRCd_NoDaemon );
269 /* Initialize the "main program": chroot environment, user and
271 if (!NGIRCd_Init(NGIRCd_NoDaemon)) {
272 Log(LOG_ALERT, "Fatal: Initialization failed");
276 /* Initialize modules, part II: these functions are eventually
277 * called with already dropped privileges ... */
282 if (!io_library_init(CONNECTION_POOL)) {
283 Log(LOG_ALERT, "Fatal: Cannot initialize IO routines: %s", strerror(errno));
287 if (!Signals_Init()) {
288 Log(LOG_ALERT, "Fatal: Could not set up signal handlers: %s", strerror(errno));
292 /* Create protocol and server identification. The syntax
293 * used by ngIRCd in PASS commands and the known "extended
294 * flags" are described in doc/Protocol.txt. */
296 snprintf( NGIRCd_ProtoID, sizeof NGIRCd_ProtoID, "%s%s %s|%s:%s", PROTOVER, PROTOIRCPLUS, PACKAGE_NAME, PACKAGE_VERSION, IRCPLUSFLAGS );
298 strcat( NGIRCd_ProtoID, "Z" );
300 if( Conf_OperCanMode ) strcat( NGIRCd_ProtoID, "o" );
302 snprintf( NGIRCd_ProtoID, sizeof NGIRCd_ProtoID, "%s%s %s|%s", PROTOVER, PROTOIRC, PACKAGE_NAME, PACKAGE_VERSION );
304 strlcat( NGIRCd_ProtoID, " P", sizeof NGIRCd_ProtoID );
306 strlcat( NGIRCd_ProtoID, "Z", sizeof NGIRCd_ProtoID );
308 LogDebug("Protocol and server ID is \"%s\".", NGIRCd_ProtoID);
310 Channel_InitPredefined( );
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 );
335 * Generate ngIRCd "version strings".
337 * The ngIRCd version information is generated once and then stored in the
338 * NGIRCd_Version and NGIRCd_VersionAddition string variables for further
339 * usage, for example by the IRC command "VERSION" and the --version command
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 );
353 strlcat( NGIRCd_VersionAddition, "ZLIB", sizeof NGIRCd_VersionAddition );
356 if ( NGIRCd_VersionAddition[0] ) strlcat( NGIRCd_VersionAddition, "+", sizeof NGIRCd_VersionAddition );
357 strlcat( NGIRCd_VersionAddition, "SSL", sizeof NGIRCd_VersionAddition );
360 if( NGIRCd_VersionAddition[0] )
361 strlcat( NGIRCd_VersionAddition, "+", sizeof NGIRCd_VersionAddition );
362 strlcat( NGIRCd_VersionAddition, "TCPWRAP", sizeof NGIRCd_VersionAddition );
365 if( NGIRCd_VersionAddition[0] )
366 strlcat( NGIRCd_VersionAddition, "+", sizeof NGIRCd_VersionAddition );
367 strlcat( NGIRCd_VersionAddition, "IDENT", sizeof NGIRCd_VersionAddition );
370 if (NGIRCd_VersionAddition[0])
371 strlcat(NGIRCd_VersionAddition, "+", sizeof NGIRCd_VersionAddition);
372 strlcat(NGIRCd_VersionAddition, "PAM", sizeof NGIRCd_VersionAddition);
375 if( NGIRCd_VersionAddition[0] )
376 strlcat( NGIRCd_VersionAddition, "+", sizeof NGIRCd_VersionAddition );
377 strlcat( NGIRCd_VersionAddition, "DEBUG", sizeof NGIRCd_VersionAddition );
380 if( NGIRCd_VersionAddition[0] )
381 strlcat( NGIRCd_VersionAddition, "+", sizeof NGIRCd_VersionAddition );
382 strlcat( NGIRCd_VersionAddition, "SNIFFER", sizeof NGIRCd_VersionAddition );
385 if( NGIRCd_VersionAddition[0] )
386 strlcat( NGIRCd_VersionAddition, "+", sizeof NGIRCd_VersionAddition );
387 strlcat( NGIRCd_VersionAddition, "RFC", sizeof NGIRCd_VersionAddition );
390 if( NGIRCd_VersionAddition[0] )
391 strlcat( NGIRCd_VersionAddition, "+", sizeof NGIRCd_VersionAddition );
392 strlcat( NGIRCd_VersionAddition, "IRCPLUS", sizeof NGIRCd_VersionAddition );
395 if (NGIRCd_VersionAddition[0])
396 strlcat(NGIRCd_VersionAddition, "+", sizeof(NGIRCd_VersionAddition));
397 strlcat(NGIRCd_VersionAddition, "IPv6", sizeof(NGIRCd_VersionAddition));
399 if( NGIRCd_VersionAddition[0] )
400 strlcat( NGIRCd_VersionAddition, "-", sizeof( NGIRCd_VersionAddition ));
402 strlcat( NGIRCd_VersionAddition, TARGET_CPU, sizeof( NGIRCd_VersionAddition ));
403 strlcat( NGIRCd_VersionAddition, "/", sizeof( NGIRCd_VersionAddition ));
404 strlcat( NGIRCd_VersionAddition, TARGET_VENDOR, sizeof( NGIRCd_VersionAddition ));
405 strlcat( NGIRCd_VersionAddition, "/", sizeof( NGIRCd_VersionAddition ));
406 strlcat( NGIRCd_VersionAddition, TARGET_OS, sizeof( NGIRCd_VersionAddition ));
408 snprintf(NGIRCd_Version, sizeof NGIRCd_Version, "%s %s-%s",
409 PACKAGE_NAME, PACKAGE_VERSION, NGIRCd_VersionAddition);
414 * Display copyright and version information of ngIRCd on the console.
419 puts( NGIRCd_Version );
420 puts( "Copyright (c)2001-2011 Alexander Barton (<alex@barton.de>) and Contributors." );
421 puts( "Homepage: <http://ngircd.barton.de/>\n" );
422 puts( "This is free software; see the source for copying conditions. There is NO" );
423 puts( "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE." );
428 * Display a short help text on the console.
429 * This help depends on the configuration of the executable and only shows
430 * options that are actually enabled.
436 puts( " -d, --debug log extra debug messages" );
438 puts( " -f, --config <f> use file <f> as configuration file" );
439 puts( " -n, --nodaemon don't fork and don't detach from controlling terminal" );
440 puts( " -p, --passive disable automatic connections to other servers" );
442 puts( " -s, --sniffer enable network sniffer and display all IRC traffic" );
444 puts( " -t, --configtest read, validate and display configuration; then exit" );
445 puts( " -V, --version output version information and exit" );
446 puts( " -h, --help display this help and exit" );
451 * Delete the file containing the process ID (PID).
454 Pidfile_Delete( void )
456 /* Pidfile configured? */
457 if( ! Conf_PidFile[0] ) return;
460 Log( LOG_DEBUG, "Removing PID file (%s) ...", Conf_PidFile );
463 if( unlink( Conf_PidFile ))
464 Log( LOG_ERR, "Error unlinking PID file (%s): %s", Conf_PidFile, strerror( errno ));
465 } /* Pidfile_Delete */
469 * Create the file containing the process ID of ngIRCd ("PID file").
471 * @param pid The process ID to be stored in this file.
474 Pidfile_Create(pid_t pid)
480 /* Pidfile configured? */
481 if( ! Conf_PidFile[0] ) return;
484 Log( LOG_DEBUG, "Creating PID file (%s) ...", Conf_PidFile );
487 pidfd = open( Conf_PidFile, O_RDWR|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
489 Log( LOG_ERR, "Error writing PID file (%s): %s", Conf_PidFile, strerror( errno ));
493 len = snprintf(pidbuf, sizeof pidbuf, "%ld\n", (long)pid);
494 if (len < 0 || len >= (int)sizeof pidbuf) {
495 Log( LOG_ERR, "Error converting pid");
499 if (write(pidfd, pidbuf, (size_t)len) != (ssize_t)len)
500 Log( LOG_ERR, "Can't write PID file (%s): %s", Conf_PidFile, strerror( errno ));
502 if( close(pidfd) != 0 )
503 Log( LOG_ERR, "Error closing PID file (%s): %s", Conf_PidFile, strerror( errno ));
504 } /* Pidfile_Create */
508 * Redirect stdin, stdout and stderr to apropriate file handles.
510 * @param fd The file handle stdin, stdout and stderr should be redirected to.
513 Setup_FDStreams(int fd)
521 /* Create new stdin(0), stdout(1) and stderr(2) descriptors */
522 dup2( fd, 0 ); dup2( fd, 1 ); dup2( fd, 2 );
523 } /* Setup_FDStreams */
527 * Get user and group ID of unprivileged "nobody" user.
530 * @param gid Group ID
531 * @return true on success.
534 NGIRCd_getNobodyID(uid_t *uid, gid_t *gid )
540 * It can return EINVAL instead of EPERM
541 * so, if we are already unprivileged,
542 * use id of current user.
544 if (geteuid() && getuid()) {
551 pwd = getpwnam("nobody");
552 if (!pwd) return false;
554 if ( !pwd->pw_uid || !pwd->pw_gid)
562 } /* NGIRCd_getNobodyID */
566 * Initialize ngIRCd daemon.
568 * @param NGIRCd_NoDaemon Set to true if ngIRCd should run in the
569 * foreground and not as a daemon.
570 * @return true on success.
573 NGIRCd_Init( bool NGIRCd_NoDaemon )
575 static bool initialized;
576 bool chrooted = false;
579 int real_errno, fd = -1;
585 if (!NGIRCd_NoDaemon) {
586 /* open /dev/null before chroot() */
587 fd = open( "/dev/null", O_RDWR);
589 Log(LOG_WARNING, "Could not open /dev/null: %s", strerror(errno));
592 if (!ConnSSL_InitLibrary())
594 "Warning: Error during SSL initialization, continuing ...");
596 if( Conf_Chroot[0] ) {
597 if( chdir( Conf_Chroot ) != 0 ) {
598 Log( LOG_ERR, "Can't chdir() in ChrootDir (%s): %s", Conf_Chroot, strerror( errno ));
602 if( chroot( Conf_Chroot ) != 0 ) {
603 if (errno != EPERM) {
604 Log( LOG_ERR, "Can't change root directory to \"%s\": %s",
605 Conf_Chroot, strerror( errno ));
610 Log( LOG_INFO, "Changed root and working directory to \"%s\".", Conf_Chroot );
615 Log(LOG_INFO, "ServerUID must not be 0, using \"nobody\" instead.", Conf_UID);
617 if (! NGIRCd_getNobodyID(&Conf_UID, &Conf_GID)) {
618 Log(LOG_WARNING, "Could not get user/group ID of user \"nobody\": %s",
619 errno ? strerror(errno) : "not found" );
624 if (getgid() != Conf_GID) {
625 /* Change group ID */
626 if (setgid(Conf_GID) != 0) {
628 Log( LOG_ERR, "Can't change group ID to %u: %s", Conf_GID, strerror( errno ));
629 if (real_errno != EPERM)
634 if (getuid() != Conf_UID) {
636 if (setuid(Conf_UID) != 0) {
638 Log(LOG_ERR, "Can't change user ID to %u: %s", Conf_UID, strerror(errno));
639 if (real_errno != EPERM)
646 /* Normally a child process is forked which isn't any longer
647 * connected to ther controlling terminal. Use "--nodaemon"
648 * to disable this "daemon mode" (useful for debugging). */
649 if ( ! NGIRCd_NoDaemon ) {
652 /* "Old" process: exit. */
657 fprintf( stderr, "%s: Can't fork: %s!\nFatal error, exiting now ...\n",
658 PACKAGE_NAME, strerror( errno ));
662 /* New child process */
666 setpgrp(0, getpid());
668 if (chdir( "/" ) != 0)
669 Log(LOG_ERR, "Can't change directory to '/': %s",
672 /* Detach stdin, stdout and stderr */
681 Pidfile_Create( pid );
683 /* Check UID/GID we are running as, can be different from values
684 * configured (e. g. if we were already started with a UID>0. */
688 pwd = getpwuid( Conf_UID );
689 grp = getgrgid( Conf_GID );
691 Log(LOG_INFO, "Running as user %s(%ld), group %s(%ld), with PID %ld.",
692 pwd ? pwd->pw_name : "unknown", (long)Conf_UID,
693 grp ? grp->gr_name : "unknown", (long)Conf_GID, (long)pid);
696 Log(LOG_INFO, "Running with root directory \"%s\".",
700 Log(LOG_INFO, "Not running with changed root directory.");
702 /* Change working directory to home directory of the user
703 * we are running as (only when running in daemon mode and not in chroot) */
706 if (!NGIRCd_NoDaemon ) {
707 if( chdir( pwd->pw_dir ) == 0 )
708 Log( LOG_DEBUG, "Changed working directory to \"%s\" ...", pwd->pw_dir );
710 Log( LOG_INFO, "Notice: Can't change working directory to \"%s\": %s",
711 pwd->pw_dir, strerror( errno ));
714 Log( LOG_ERR, "Can't get user informaton for UID %d!?", Conf_UID );