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 *
11 * Main program -- main()
12 */
15 #include "portab.h"
17 static char UNUSED id[] = "$Id: ngircd.c,v 1.94 2005/02/11 13:52:37 alex Exp $";
19 #include "imp.h"
20 #include <assert.h>
21 #include <errno.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <signal.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <time.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <sys/wait.h>
31 #include <fcntl.h>
32 #include <pwd.h>
33 #include <grp.h>
35 #include "defines.h"
36 #include "resolve.h"
37 #include "conn.h"
38 #include "client.h"
39 #include "channel.h"
40 #include "conf.h"
41 #include "cvs-version.h"
42 #include "lists.h"
43 #include "log.h"
44 #include "parse.h"
45 #include "irc.h"
47 #ifdef RENDEZVOUS
48 #include "rendezvous.h"
49 #endif
51 #include "exp.h"
52 #include "ngircd.h"
55 LOCAL VOID Initialize_Signal_Handler PARAMS(( VOID ));
56 LOCAL VOID Signal_Handler PARAMS(( INT Signal ));
58 LOCAL VOID Show_Version PARAMS(( VOID ));
59 LOCAL VOID Show_Help PARAMS(( VOID ));
61 LOCAL VOID Pidfile_Create PARAMS(( LONG ));
62 LOCAL VOID Pidfile_Delete PARAMS(( VOID ));
64 LOCAL VOID Fill_Version PARAMS(( VOID ));
66 LOCAL VOID Setup_FDStreams PARAMS(( VOID ));
69 GLOBAL int
70 main( int argc, const char *argv[] )
71 {
72 struct passwd *pwd;
73 struct group *grp;
74 BOOLEAN ok, configtest = FALSE;
75 LONG pid, n;
76 INT i;
78 umask( 0077 );
80 NGIRCd_SignalQuit = NGIRCd_SignalRestart = NGIRCd_SignalRehash = FALSE;
81 NGIRCd_NoDaemon = NGIRCd_Passive = FALSE;
82 #ifdef DEBUG
83 NGIRCd_Debug = FALSE;
84 #endif
85 #ifdef SNIFFER
86 NGIRCd_Sniffer = FALSE;
87 #endif
88 strlcpy( NGIRCd_ConfFile, SYSCONFDIR, sizeof( NGIRCd_ConfFile ));
89 strlcat( NGIRCd_ConfFile, CONFIG_FILE, sizeof( NGIRCd_ConfFile ));
91 Fill_Version( );
93 /* Kommandozeile parsen */
94 for( i = 1; i < argc; i++ )
95 {
96 ok = FALSE;
97 if(( argv[i][0] == '-' ) && ( argv[i][1] == '-' ))
98 {
99 /* Lange Option */
101 if( strcmp( argv[i], "--config" ) == 0 )
103 if( i + 1 < argc )
105 /* Ok, there's an parameter left */
106 strlcpy( NGIRCd_ConfFile, argv[i + 1], sizeof( NGIRCd_ConfFile ));
108 /* next parameter */
109 i++; ok = TRUE;
112 if( strcmp( argv[i], "--configtest" ) == 0 )
114 configtest = TRUE;
115 ok = TRUE;
117 #ifdef DEBUG
118 if( strcmp( argv[i], "--debug" ) == 0 )
120 NGIRCd_Debug = TRUE;
121 ok = TRUE;
123 #endif
124 if( strcmp( argv[i], "--help" ) == 0 )
126 Show_Version( );
127 puts( "" ); Show_Help( ); puts( "" );
128 exit( 1 );
130 if( strcmp( argv[i], "--nodaemon" ) == 0 )
132 NGIRCd_NoDaemon = TRUE;
133 ok = TRUE;
135 if( strcmp( argv[i], "--passive" ) == 0 )
137 NGIRCd_Passive = TRUE;
138 ok = TRUE;
140 #ifdef SNIFFER
141 if( strcmp( argv[i], "--sniffer" ) == 0 )
143 NGIRCd_Sniffer = TRUE;
144 ok = TRUE;
146 #endif
147 if( strcmp( argv[i], "--version" ) == 0 )
149 Show_Version( );
150 exit( 1 );
153 else if(( argv[i][0] == '-' ) && ( argv[i][1] != '-' ))
155 /* Kurze Option */
157 for( n = 1; n < (LONG)strlen( argv[i] ); n++ )
159 ok = FALSE;
160 #ifdef DEBUG
161 if( argv[i][n] == 'd' )
163 NGIRCd_Debug = TRUE;
164 ok = TRUE;
166 #endif
167 if( argv[i][n] == 'f' )
169 if(( ! argv[i][n + 1] ) && ( i + 1 < argc ))
171 /* Ok, next character is a blank */
172 strlcpy( NGIRCd_ConfFile, argv[i + 1], sizeof( NGIRCd_ConfFile ));
174 /* go to the following parameter */
175 i++; n = (LONG)strlen( argv[i] );
176 ok = TRUE;
179 if( argv[i][n] == 'n' )
181 NGIRCd_NoDaemon = TRUE;
182 ok = TRUE;
184 if( argv[i][n] == 'p' )
186 NGIRCd_Passive = TRUE;
187 ok = TRUE;
189 #ifdef SNIFFER
190 if( argv[i][n] == 's' )
192 NGIRCd_Sniffer = TRUE;
193 ok = TRUE;
195 #endif
196 if( argv[i][n] == 't' )
198 configtest = TRUE;
199 ok = TRUE;
202 if( ! ok )
204 printf( "%s: invalid option \"-%c\"!\n", PACKAGE_NAME, argv[i][n] );
205 printf( "Try \"%s --help\" for more information.\n", PACKAGE_NAME );
206 exit( 1 );
211 if( ! ok )
213 printf( "%s: invalid option \"%s\"!\n", PACKAGE_NAME, argv[i] );
214 printf( "Try \"%s --help\" for more information.\n", PACKAGE_NAME );
215 exit( 1 );
219 /* Debug-Level (fuer IRC-Befehl "VERSION") ermitteln */
220 NGIRCd_DebugLevel[0] = '\0';
221 #ifdef DEBUG
222 if( NGIRCd_Debug ) strcpy( NGIRCd_DebugLevel, "1" );
223 #endif
224 #ifdef SNIFFER
225 if( NGIRCd_Sniffer )
227 NGIRCd_Debug = TRUE;
228 strcpy( NGIRCd_DebugLevel, "2" );
230 #endif
232 /* Soll nur die Konfigurations ueberprueft und ausgegeben werden? */
233 if( configtest )
235 Show_Version( ); puts( "" );
236 exit( Conf_Test( ));
239 while( ! NGIRCd_SignalQuit )
241 /* Initialize global variables */
242 NGIRCd_Start = time( NULL );
243 (VOID)strftime( NGIRCd_StartStr, 64, "%a %b %d %Y at %H:%M:%S (%Z)", localtime( &NGIRCd_Start ));
245 NGIRCd_SignalRehash = FALSE;
246 NGIRCd_SignalRestart = FALSE;
247 NGIRCd_SignalQuit = FALSE;
249 /* Initialize modules, part I */
250 Log_Init( );
251 Conf_Init( );
253 if( Conf_Chroot[0] )
255 /* Chroot */
256 if( chdir( Conf_Chroot ) != 0 ) Log( LOG_ERR, "Can't chdir() in ChrootDir (%s): %s", Conf_Chroot, strerror( errno ));
258 if( chroot( Conf_Chroot ) != 0 ) Log( LOG_ERR, "Can't change root directory to \"%s\": %s", Conf_Chroot, strerror( errno ));
259 else Log( LOG_INFO, "Changed root and working directory to \"%s\".", Conf_Chroot );
262 if( Conf_GID != 0 )
264 /* Set new group ID */
265 if( setgid( Conf_GID ) != 0 ) Log( LOG_ERR, "Can't change group ID to %u: %s", Conf_GID, strerror( errno ));
267 if( Conf_UID != 0 )
269 /* Set new user ID */
270 if( setuid( Conf_UID ) != 0 ) Log( LOG_ERR, "Can't change user ID to %u: %s", Conf_UID, strerror( errno ));
273 /* Normally a child process is forked which isn't any longer
274 * connected to ther controlling terminal. Use "--nodaemon"
275 * to disable this "daemon mode" (useful for debugging). */
276 if( ! NGIRCd_NoDaemon )
278 /* fork child process */
279 pid = (LONG)fork( );
280 if( pid > 0 )
282 /* "Old" process: exit. */
283 exit( 0 );
285 if( pid < 0 )
287 /* Error!? */
288 fprintf( stderr, "%s: Can't fork: %s!\nFatal error, exiting now ...\n", PACKAGE_NAME, strerror( errno ));
289 exit( 1 );
292 /* New child process */
293 (VOID)setsid( );
294 chdir( "/" );
296 /* Detach stdin, stdout and stderr */
297 (VOID)Setup_FDStreams( );
300 /* Create PID file */
301 pid = (LONG) getpid( );
302 Pidfile_Create( pid );
304 /* Show user, group, and PID of the running daemon */
305 pwd = getpwuid( getuid( )); grp = getgrgid( getgid( ));
306 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);
308 /* Change working directory to home directory of the user
309 * we are running as (when not running chroot()'ed!) */
310 if( Conf_UID != 0 && ! Conf_Chroot[0] )
312 struct passwd *pwd;
314 pwd = getpwuid( Conf_UID );
315 if( pwd != NULL )
317 if( chdir( pwd->pw_dir ) == 0 ) Log( LOG_DEBUG, "Changed working directory to \"%s\" ...", pwd->pw_dir );
318 else Log( LOG_ERR, "Can't change working directory to \"%s\": %s", pwd->pw_dir, strerror( errno ));
320 else Log( LOG_ERR, "Can't get user informaton for UID %d!?", Conf_UID );
323 /* Initialize modules, part II: these functions are eventually
324 * called with already dropped privileges ... */
325 Resolve_Init( );
326 Lists_Init( );
327 Channel_Init( );
328 Client_Init( );
329 #ifdef RENDEZVOUS
330 Rendezvous_Init( );
331 #endif
332 Conn_Init( );
334 #ifdef DEBUG
335 /* Redirect stderr handle to "error file" for debugging
336 * when not running in "no daemon" mode: */
337 if( ! NGIRCd_NoDaemon ) Log_InitErrorfile( );
338 #endif
340 /* Signal-Handler initialisieren */
341 Initialize_Signal_Handler( );
343 /* Protokoll- und Server-Identifikation erzeugen. Die vom ngIRCd
344 * beim PASS-Befehl verwendete Syntax sowie die erweiterten Flags
345 * sind in doc/Protocol.txt beschrieben. */
346 #ifdef IRCPLUS
347 sprintf( NGIRCd_ProtoID, "%s%s %s|%s:%s", PROTOVER, PROTOIRCPLUS, PACKAGE_NAME, PACKAGE_VERSION, IRCPLUSFLAGS );
348 #ifdef ZLIB
349 strcat( NGIRCd_ProtoID, "Z" );
350 #endif
351 if( Conf_OperCanMode ) strcat( NGIRCd_ProtoID, "o" );
352 #else
353 sprintf( NGIRCd_ProtoID, "%s%s %s|%s", PROTOVER, PROTOIRC, PACKAGE_NAME, PACKAGE_VERSION );
354 #endif
355 strcat( NGIRCd_ProtoID, " P" );
356 #ifdef ZLIB
357 strcat( NGIRCd_ProtoID, "Z" );
358 #endif
359 Log( LOG_DEBUG, "Protocol and server ID is \"%s\".", NGIRCd_ProtoID );
361 /* Vordefinierte Channels anlegen */
362 Channel_InitPredefined( );
364 /* Listen-Ports initialisieren */
365 if( Conn_InitListeners( ) < 1 )
367 Log( LOG_ALERT, "Server isn't listening on a single port!" );
368 Log( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
369 Pidfile_Delete( );
370 exit( 1 );
373 /* Hauptschleife */
374 Conn_Handler( );
376 /* Alles abmelden */
377 Conn_Exit( );
378 #ifdef RENDEZVOUS
379 Rendezvous_Exit( );
380 #endif
381 Client_Exit( );
382 Channel_Exit( );
383 Lists_Exit( );
384 Log_Exit( );
386 Pidfile_Delete( );
389 return 0;
390 } /* main */
393 LOCAL VOID
394 Fill_Version( VOID )
396 NGIRCd_VersionAddition[0] = '\0';
398 #ifdef SYSLOG
399 strcpy( NGIRCd_VersionAddition, "SYSLOG" );
400 #endif
401 #ifdef ZLIB
402 if( NGIRCd_VersionAddition[0] ) strcat( NGIRCd_VersionAddition, "+" );
403 strcat( NGIRCd_VersionAddition, "ZLIB" );
404 #endif
405 #ifdef TCPWRAP
406 if( NGIRCd_VersionAddition[0] ) strcat( NGIRCd_VersionAddition, "+" );
407 strcat( NGIRCd_VersionAddition, "TCPWRAP" );
408 #endif
409 #ifdef RENDEZVOUS
410 if( NGIRCd_VersionAddition[0] ) strcat( NGIRCd_VersionAddition, "+" );
411 strcat( NGIRCd_VersionAddition, "RENDEZVOUS" );
412 #endif
413 #ifdef IDENTAUTH
414 if( NGIRCd_VersionAddition[0] ) strcat( NGIRCd_VersionAddition, "+" );
415 strcat( NGIRCd_VersionAddition, "IDENT" );
416 #endif
417 #ifdef DEBUG
418 if( NGIRCd_VersionAddition[0] ) strcat( NGIRCd_VersionAddition, "+" );
419 strcat( NGIRCd_VersionAddition, "DEBUG" );
420 #endif
421 #ifdef SNIFFER
422 if( NGIRCd_VersionAddition[0] ) strcat( NGIRCd_VersionAddition, "+" );
423 strcat( NGIRCd_VersionAddition, "SNIFFER" );
424 #endif
425 #ifdef STRICT_RFC
426 if( NGIRCd_VersionAddition[0] ) strcat( NGIRCd_VersionAddition, "+" );
427 strcat( NGIRCd_VersionAddition, "RFC" );
428 #endif
429 #ifdef IRCPLUS
430 if( NGIRCd_VersionAddition[0] ) strcat( NGIRCd_VersionAddition, "+" );
431 strcat( NGIRCd_VersionAddition, "IRCPLUS" );
432 #endif
434 if( NGIRCd_VersionAddition[0] ) strlcat( NGIRCd_VersionAddition, "-", sizeof( NGIRCd_VersionAddition ));
435 strlcat( NGIRCd_VersionAddition, TARGET_CPU, sizeof( NGIRCd_VersionAddition ));
436 strlcat( NGIRCd_VersionAddition, "/", sizeof( NGIRCd_VersionAddition ));
437 strlcat( NGIRCd_VersionAddition, TARGET_VENDOR, sizeof( NGIRCd_VersionAddition ));
438 strlcat( NGIRCd_VersionAddition, "/", sizeof( NGIRCd_VersionAddition ));
439 strlcat( NGIRCd_VersionAddition, TARGET_OS, sizeof( NGIRCd_VersionAddition ));
441 #ifdef CVSDATE
442 snprintf( NGIRCd_Version, sizeof NGIRCd_Version,"%s %s(%s)-%s", PACKAGE_NAME, PACKAGE_VERSION, CVSDATE, NGIRCd_VersionAddition);
443 #else
444 snprintf( NGIRCd_Version, sizeof NGIRCd_Version, "%s %s-%s", PACKAGE_NAME, PACKAGE_VERSION, NGIRCd_VersionAddition);
445 #endif
446 } /* Fill_Version */
449 GLOBAL VOID
450 NGIRCd_Rehash( VOID )
452 CHAR old_name[CLIENT_ID_LEN];
454 Log( LOG_NOTICE|LOG_snotice, "Re-reading configuration NOW!" );
455 NGIRCd_SignalRehash = FALSE;
457 /* Close down all listening sockets */
458 Conn_ExitListeners( );
460 /* Remember old server name */
461 strcpy( old_name, Conf_ServerName );
463 /* Re-read configuration ... */
464 Conf_Rehash( );
466 /* Recover old server name: it can't be changed during run-time */
467 if( strcmp( old_name, Conf_ServerName ) != 0 )
469 strcpy( Conf_ServerName, old_name );
470 Log( LOG_ERR, "Can't change \"ServerName\" on runtime! Ignored new name." );
473 /* Create new pre-defined channels */
474 Channel_InitPredefined( );
476 /* Start listening on sockets */
477 Conn_InitListeners( );
479 /* Sync configuration with established connections */
480 Conn_SyncServerStruct( );
482 Log( LOG_NOTICE|LOG_snotice, "Re-reading of configuration done." );
483 } /* NGIRCd_Rehash */
486 LOCAL VOID
487 Initialize_Signal_Handler( VOID )
489 /* Signal-Handler initialisieren: einige Signale
490 * werden ignoriert, andere speziell behandelt. */
492 #ifdef HAVE_SIGACTION
493 /* sigaction() ist vorhanden */
495 struct sigaction saction;
497 /* Signal-Struktur initialisieren */
498 memset( &saction, 0, sizeof( saction ));
499 saction.sa_handler = Signal_Handler;
500 #ifdef SA_RESTART
501 saction.sa_flags |= SA_RESTART;
502 #endif
503 #ifdef SA_NOCLDWAIT
504 saction.sa_flags |= SA_NOCLDWAIT;
505 #endif
507 /* Signal-Handler einhaengen */
508 sigaction( SIGINT, &saction, NULL );
509 sigaction( SIGQUIT, &saction, NULL );
510 sigaction( SIGTERM, &saction, NULL);
511 sigaction( SIGHUP, &saction, NULL);
512 sigaction( SIGCHLD, &saction, NULL);
514 /* einige Signale ignorieren */
515 saction.sa_handler = SIG_IGN;
516 sigaction( SIGPIPE, &saction, NULL );
517 #else
518 /* kein sigaction() vorhanden */
520 /* Signal-Handler einhaengen */
521 signal( SIGINT, Signal_Handler );
522 signal( SIGQUIT, Signal_Handler );
523 signal( SIGTERM, Signal_Handler );
524 signal( SIGHUP, Signal_Handler );
525 signal( SIGCHLD, Signal_Handler );
527 /* einige Signale ignorieren */
528 signal( SIGPIPE, SIG_IGN );
529 #endif
530 } /* Initialize_Signal_Handler */
533 LOCAL VOID
534 Signal_Handler( INT Signal )
536 /* Signal-Handler. Dieser wird aufgerufen, wenn eines der Signale eintrifft,
537 * fuer das wir uns registriert haben (vgl. Initialize_Signal_Handler). Die
538 * Nummer des eingetroffenen Signals wird der Funktion uebergeben. */
540 switch( Signal )
542 case SIGTERM:
543 case SIGINT:
544 case SIGQUIT:
545 /* wir soll(t)en uns wohl beenden ... */
546 NGIRCd_SignalQuit = TRUE;
547 break;
548 case SIGHUP:
549 /* Konfiguration neu einlesen: */
550 NGIRCd_SignalRehash = TRUE;
551 break;
552 case SIGCHLD:
553 /* Child-Prozess wurde beendet. Zombies vermeiden: */
554 while( waitpid( -1, NULL, WNOHANG ) > 0);
555 break;
556 #ifdef DEBUG
557 default:
558 /* unbekanntes bzw. unbehandeltes Signal */
559 Log( LOG_DEBUG, "Got signal %d! Ignored.", Signal );
560 #endif
562 } /* Signal_Handler */
565 LOCAL VOID
566 Show_Version( VOID )
568 puts( NGIRCd_Version );
569 puts( "Copyright (c)2001-2005 by Alexander Barton (<alex@barton.de>)." );
570 puts( "Homepage: <http://arthur.ath.cx/~alex/ngircd/>\n" );
571 puts( "This is free software; see the source for copying conditions. There is NO" );
572 puts( "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE." );
573 } /* Show_Version */
576 LOCAL VOID
577 Show_Help( VOID )
579 #ifdef DEBUG
580 puts( " -d, --debug log extra debug messages" );
581 #endif
582 puts( " -f, --config <f> use file <f> as configuration file" );
583 puts( " -n, --nodaemon don't fork and don't detach from controlling terminal" );
584 puts( " -p, --passive disable automatic connections to other servers" );
585 #ifdef SNIFFER
586 puts( " -s, --sniffer enable network sniffer and display all IRC traffic" );
587 #endif
588 puts( " -t, --configtest read, validate and display configuration; then exit" );
589 puts( " --version output version information and exit" );
590 puts( " --help display this help and exit" );
591 } /* Show_Help */
594 LOCAL VOID
595 Pidfile_Delete( VOID )
597 /* Pidfile configured? */
598 if( ! Conf_PidFile[0] ) return;
600 #ifdef DEBUG
601 Log( LOG_DEBUG, "Removing PID file (%s) ...", Conf_PidFile );
602 #endif
604 if( unlink( Conf_PidFile ))
605 Log( LOG_ERR, "Error unlinking PID file (%s): %s", Conf_PidFile, strerror( errno ));
606 } /* Pidfile_Delete */
609 LOCAL VOID
610 Pidfile_Create( LONG pid )
612 FILE *pidf;
614 /* Pidfile configured? */
615 if( ! Conf_PidFile[0] ) return;
617 #ifdef DEBUG
618 Log( LOG_DEBUG, "Creating PID file (%s) ...", Conf_PidFile );
619 #endif
621 pidf = fopen( Conf_PidFile, "w" );
623 if( ! pidf )
625 Log( LOG_ERR, "Error writing PID file (%s): %s", Conf_PidFile, strerror( errno ));
626 return;
629 if( fprintf( pidf, "%ld\n", pid ) < 0 )
630 Log( LOG_ERR, "Can't write PID file (%s): %s", Conf_PidFile, strerror( errno ));
632 if( fclose(pidf) != 0 )
633 Log( LOG_ERR, "Error closing PID file (%s): %s", Conf_PidFile, strerror( errno ));
634 } /* Pidfile_Create */
637 LOCAL VOID
638 Setup_FDStreams( VOID )
640 INT fd;
642 /* Test if we can open /dev/null for reading and writing. If not
643 * we are most probably chrooted already and the server has been
644 * restarted. So we simply don't try to redirect stdXXX ... */
645 fd = open( "/dev/null", O_RDWR );
646 if ( fd < 0 ) return;
648 /* Close "old" stdin/out/err descriptors */
649 close( 0 ); close( 1 ); close( 2 );
651 /* Create new stdin(0), stdout(1) and stderr(2) descriptors */
652 dup2( fd, 0 ); dup2( fd, 1 ); dup2( fd, 2 );
654 /* Close newly opened file descriptor if not stdin/out/err */
655 if( fd > 2 ) close( fd );
656 } /* Setup_FDStreams */
659 /* -eof- */