Blob


1 /*
2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2005 by Alexander Barton (alex@barton.de)
4 *
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.
10 */
13 #include "portab.h"
15 static char UNUSED id[] = "$Id: ngircd.c,v 1.96 2005/06/01 21:52:18 alex Exp $";
17 /**
18 * @file
19 * The main program, including the C function main() which is called
20 * by the loader of the operating system.
21 */
23 #include "imp.h"
24 #include <assert.h>
25 #include <errno.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <signal.h>
29 #include <string.h>
30 #include <unistd.h>
31 #include <time.h>
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <sys/wait.h>
35 #include <fcntl.h>
36 #include <pwd.h>
37 #include <grp.h>
39 #include "defines.h"
40 #include "resolve.h"
41 #include "conn.h"
42 #include "client.h"
43 #include "channel.h"
44 #include "conf.h"
45 #include "cvs-version.h"
46 #include "lists.h"
47 #include "log.h"
48 #include "parse.h"
49 #include "irc.h"
51 #ifdef RENDEZVOUS
52 #include "rendezvous.h"
53 #endif
55 #include "exp.h"
56 #include "ngircd.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 ));
73 /**
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.
80 */
81 GLOBAL int
82 main( int argc, const char *argv[] )
83 {
84 struct passwd *pwd;
85 struct group *grp;
86 bool ok, configtest = false;
87 long pid;
88 int i;
89 size_t n;
91 umask( 0077 );
93 NGIRCd_SignalQuit = NGIRCd_SignalRestart = NGIRCd_SignalRehash = false;
94 NGIRCd_NoDaemon = NGIRCd_Passive = false;
95 #ifdef DEBUG
96 NGIRCd_Debug = false;
97 #endif
98 #ifdef SNIFFER
99 NGIRCd_Sniffer = false;
100 #endif
101 strlcpy( NGIRCd_ConfFile, SYSCONFDIR, sizeof( NGIRCd_ConfFile ));
102 strlcat( NGIRCd_ConfFile, CONFIG_FILE, sizeof( NGIRCd_ConfFile ));
104 Fill_Version( );
106 /* Kommandozeile parsen */
107 for( i = 1; i < argc; i++ )
109 ok = false;
110 if(( argv[i][0] == '-' ) && ( argv[i][1] == '-' ))
112 /* Lange Option */
114 if( strcmp( argv[i], "--config" ) == 0 )
116 if( i + 1 < argc )
118 /* Ok, there's an parameter left */
119 strlcpy( NGIRCd_ConfFile, argv[i + 1], sizeof( NGIRCd_ConfFile ));
121 /* next parameter */
122 i++; ok = true;
125 if( strcmp( argv[i], "--configtest" ) == 0 )
127 configtest = true;
128 ok = true;
130 #ifdef DEBUG
131 if( strcmp( argv[i], "--debug" ) == 0 )
133 NGIRCd_Debug = true;
134 ok = true;
136 #endif
137 if( strcmp( argv[i], "--help" ) == 0 )
139 Show_Version( );
140 puts( "" ); Show_Help( ); puts( "" );
141 exit( 1 );
143 if( strcmp( argv[i], "--nodaemon" ) == 0 )
145 NGIRCd_NoDaemon = true;
146 ok = true;
148 if( strcmp( argv[i], "--passive" ) == 0 )
150 NGIRCd_Passive = true;
151 ok = true;
153 #ifdef SNIFFER
154 if( strcmp( argv[i], "--sniffer" ) == 0 )
156 NGIRCd_Sniffer = true;
157 ok = true;
159 #endif
160 if( strcmp( argv[i], "--version" ) == 0 )
162 Show_Version( );
163 exit( 1 );
166 else if(( argv[i][0] == '-' ) && ( argv[i][1] != '-' ))
168 /* Kurze Option */
169 for( n = 1; n < strlen( argv[i] ); n++ )
171 ok = false;
172 #ifdef DEBUG
173 if( argv[i][n] == 'd' )
175 NGIRCd_Debug = true;
176 ok = true;
178 #endif
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 */
187 i++;
188 n = strlen( argv[i] );
189 ok = true;
192 if( argv[i][n] == 'n' )
194 NGIRCd_NoDaemon = true;
195 ok = true;
197 if( argv[i][n] == 'p' )
199 NGIRCd_Passive = true;
200 ok = true;
202 #ifdef SNIFFER
203 if( argv[i][n] == 's' )
205 NGIRCd_Sniffer = true;
206 ok = true;
208 #endif
209 if( argv[i][n] == 't' )
211 configtest = true;
212 ok = true;
215 if( ! ok )
217 printf( "%s: invalid option \"-%c\"!\n", PACKAGE_NAME, argv[i][n] );
218 printf( "Try \"%s --help\" for more information.\n", PACKAGE_NAME );
219 exit( 1 );
224 if( ! ok )
226 printf( "%s: invalid option \"%s\"!\n", PACKAGE_NAME, argv[i] );
227 printf( "Try \"%s --help\" for more information.\n", PACKAGE_NAME );
228 exit( 1 );
232 /* Debug-Level (fuer IRC-Befehl "VERSION") ermitteln */
233 NGIRCd_DebugLevel[0] = '\0';
234 #ifdef DEBUG
235 if( NGIRCd_Debug ) strcpy( NGIRCd_DebugLevel, "1" );
236 #endif
237 #ifdef SNIFFER
238 if( NGIRCd_Sniffer )
240 NGIRCd_Debug = true;
241 strcpy( NGIRCd_DebugLevel, "2" );
243 #endif
245 /* Soll nur die Konfigurations ueberprueft und ausgegeben werden? */
246 if( configtest )
248 Show_Version( ); puts( "" );
249 exit( Conf_Test( ));
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 */
263 Log_Init( );
264 Conf_Init( );
266 if( Conf_Chroot[0] )
268 /* Chroot */
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 );
275 if( Conf_GID != 0 )
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 ));
280 if( Conf_UID != 0 )
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 */
292 pid = (long)fork( );
293 if( pid > 0 )
295 /* "Old" process: exit. */
296 exit( 0 );
298 if( pid < 0 )
300 /* Error!? */
301 fprintf( stderr, "%s: Can't fork: %s!\nFatal error, exiting now ...\n", PACKAGE_NAME, strerror( errno ));
302 exit( 1 );
305 /* New child process */
306 (void)setsid( );
307 chdir( "/" );
309 /* Detach stdin, stdout and stderr */
310 Setup_FDStreams( );
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] )
325 struct passwd *pwd;
327 pwd = getpwuid( Conf_UID );
328 if( pwd != NULL )
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 ... */
338 Resolve_Init( );
339 Lists_Init( );
340 Channel_Init( );
341 Client_Init( );
342 #ifdef RENDEZVOUS
343 Rendezvous_Init( );
344 #endif
345 Conn_Init( );
347 #ifdef DEBUG
348 /* Redirect stderr handle to "error file" for debugging
349 * when not running in "no daemon" mode: */
350 if( ! NGIRCd_NoDaemon ) Log_InitErrorfile( );
351 #endif
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. */
359 #ifdef IRCPLUS
360 sprintf( NGIRCd_ProtoID, "%s%s %s|%s:%s", PROTOVER, PROTOIRCPLUS, PACKAGE_NAME, PACKAGE_VERSION, IRCPLUSFLAGS );
361 #ifdef ZLIB
362 strcat( NGIRCd_ProtoID, "Z" );
363 #endif
364 if( Conf_OperCanMode ) strcat( NGIRCd_ProtoID, "o" );
365 #else
366 sprintf( NGIRCd_ProtoID, "%s%s %s|%s", PROTOVER, PROTOIRC, PACKAGE_NAME, PACKAGE_VERSION );
367 #endif
368 strcat( NGIRCd_ProtoID, " P" );
369 #ifdef ZLIB
370 strcat( NGIRCd_ProtoID, "Z" );
371 #endif
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 );
382 Pidfile_Delete( );
383 exit( 1 );
386 /* Hauptschleife */
387 Conn_Handler( );
389 /* Alles abmelden */
390 Conn_Exit( );
391 #ifdef RENDEZVOUS
392 Rendezvous_Exit( );
393 #endif
394 Client_Exit( );
395 Channel_Exit( );
396 Lists_Exit( );
397 Log_Exit( );
399 Pidfile_Delete( );
402 return 0;
403 } /* main */
406 /**
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.
411 */
412 LOCAL void
413 Fill_Version( void )
415 NGIRCd_VersionAddition[0] = '\0';
417 #ifdef SYSLOG
418 strcpy( NGIRCd_VersionAddition, "SYSLOG" );
419 #endif
420 #ifdef ZLIB
421 if( NGIRCd_VersionAddition[0] ) strcat( NGIRCd_VersionAddition, "+" );
422 strcat( NGIRCd_VersionAddition, "ZLIB" );
423 #endif
424 #ifdef TCPWRAP
425 if( NGIRCd_VersionAddition[0] ) strcat( NGIRCd_VersionAddition, "+" );
426 strcat( NGIRCd_VersionAddition, "TCPWRAP" );
427 #endif
428 #ifdef RENDEZVOUS
429 if( NGIRCd_VersionAddition[0] ) strcat( NGIRCd_VersionAddition, "+" );
430 strcat( NGIRCd_VersionAddition, "RENDEZVOUS" );
431 #endif
432 #ifdef IDENTAUTH
433 if( NGIRCd_VersionAddition[0] ) strcat( NGIRCd_VersionAddition, "+" );
434 strcat( NGIRCd_VersionAddition, "IDENT" );
435 #endif
436 #ifdef DEBUG
437 if( NGIRCd_VersionAddition[0] ) strcat( NGIRCd_VersionAddition, "+" );
438 strcat( NGIRCd_VersionAddition, "DEBUG" );
439 #endif
440 #ifdef SNIFFER
441 if( NGIRCd_VersionAddition[0] ) strcat( NGIRCd_VersionAddition, "+" );
442 strcat( NGIRCd_VersionAddition, "SNIFFER" );
443 #endif
444 #ifdef STRICT_RFC
445 if( NGIRCd_VersionAddition[0] ) strcat( NGIRCd_VersionAddition, "+" );
446 strcat( NGIRCd_VersionAddition, "RFC" );
447 #endif
448 #ifdef IRCPLUS
449 if( NGIRCd_VersionAddition[0] ) strcat( NGIRCd_VersionAddition, "+" );
450 strcat( NGIRCd_VersionAddition, "IRCPLUS" );
451 #endif
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 ));
460 #ifdef CVSDATE
461 snprintf( NGIRCd_Version, sizeof NGIRCd_Version,"%s %s(%s)-%s", PACKAGE_NAME, PACKAGE_VERSION, CVSDATE, NGIRCd_VersionAddition);
462 #else
463 snprintf( NGIRCd_Version, sizeof NGIRCd_Version, "%s %s-%s", PACKAGE_NAME, PACKAGE_VERSION, NGIRCd_VersionAddition);
464 #endif
465 } /* Fill_Version */
468 /**
469 * Reload the server configuration file.
470 */
471 GLOBAL void
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 ... */
486 Conf_Rehash( );
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 */
508 /**
509 * Initialize the signal handler.
510 */
511 LOCAL void
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;
525 #ifdef SA_RESTART
526 saction.sa_flags |= SA_RESTART;
527 #endif
528 #ifdef SA_NOCLDWAIT
529 saction.sa_flags |= SA_NOCLDWAIT;
530 #endif
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 );
542 #else
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 );
554 #endif
555 } /* Initialize_Signal_Handler */
558 /**
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.
563 */
564 LOCAL void
565 Signal_Handler( int Signal )
567 switch( Signal )
569 case SIGTERM:
570 case SIGINT:
571 case SIGQUIT:
572 /* wir soll(t)en uns wohl beenden ... */
573 NGIRCd_SignalQuit = true;
574 break;
575 case SIGHUP:
576 /* Konfiguration neu einlesen: */
577 NGIRCd_SignalRehash = true;
578 break;
579 case SIGCHLD:
580 /* Child-Prozess wurde beendet. Zombies vermeiden: */
581 while( waitpid( -1, NULL, WNOHANG ) > 0);
582 break;
583 #ifdef DEBUG
584 default:
585 /* unbekanntes bzw. unbehandeltes Signal */
586 Log( LOG_DEBUG, "Got signal %d! Ignored.", Signal );
587 #endif
589 } /* Signal_Handler */
592 /**
593 * Display copyright and version information of ngIRCd on the console.
594 */
595 LOCAL void
596 Show_Version( void )
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." );
603 } /* Show_Version */
606 /**
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.
610 */
611 LOCAL void
612 Show_Help( void )
614 #ifdef DEBUG
615 puts( " -d, --debug log extra debug messages" );
616 #endif
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" );
620 #ifdef SNIFFER
621 puts( " -s, --sniffer enable network sniffer and display all IRC traffic" );
622 #endif
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" );
626 } /* Show_Help */
629 /**
630 * Delete the file containing the process ID (PID).
631 */
632 LOCAL void
633 Pidfile_Delete( void )
635 /* Pidfile configured? */
636 if( ! Conf_PidFile[0] ) return;
638 #ifdef DEBUG
639 Log( LOG_DEBUG, "Removing PID file (%s) ...", Conf_PidFile );
640 #endif
642 if( unlink( Conf_PidFile ))
643 Log( LOG_ERR, "Error unlinking PID file (%s): %s", Conf_PidFile, strerror( errno ));
644 } /* Pidfile_Delete */
647 /**
648 * Create the file containing the process ID of ngIRCd ("PID file").
649 * @param pid The process ID to be stored in this file.
650 */
651 LOCAL void
652 Pidfile_Create( long pid )
654 FILE *pidf;
656 /* Pidfile configured? */
657 if( ! Conf_PidFile[0] ) return;
659 #ifdef DEBUG
660 Log( LOG_DEBUG, "Creating PID file (%s) ...", Conf_PidFile );
661 #endif
663 pidf = fopen( Conf_PidFile, "w" );
665 if( ! pidf )
667 Log( LOG_ERR, "Error writing PID file (%s): %s", Conf_PidFile, strerror( errno ));
668 return;
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 */
679 /**
680 * Redirect stdin, stdout and stderr to apropriate file handles.
681 */
682 LOCAL void
683 Setup_FDStreams( void )
685 int fd;
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 */
704 /* -eof- */