Blob


1 /*
2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2003 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.71 2003/01/01 13:32:23 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 <sys/types.h>
28 #include <sys/stat.h>
29 #include <sys/wait.h>
30 #include <time.h>
31 #include <pwd.h>
32 #include <grp.h>
34 #include "resolve.h"
35 #include "conn.h"
36 #include "client.h"
37 #include "channel.h"
38 #include "conf.h"
39 #include "cvs-version.h"
40 #include "defines.h"
41 #include "lists.h"
42 #include "log.h"
43 #include "parse.h"
44 #include "irc.h"
46 #include "exp.h"
47 #include "ngircd.h"
50 LOCAL VOID Initialize_Signal_Handler PARAMS(( VOID ));
51 LOCAL VOID Signal_Handler PARAMS(( INT Signal ));
53 LOCAL VOID Show_Version PARAMS(( VOID ));
54 LOCAL VOID Show_Help PARAMS(( VOID ));
57 GLOBAL int
58 main( int argc, const char *argv[] )
59 {
60 struct passwd *pwd;
61 struct group *grp;
62 BOOLEAN ok, configtest = FALSE;
63 LONG pid, n;
64 INT i;
66 umask( 0077 );
68 NGIRCd_SignalQuit = NGIRCd_SignalRestart = NGIRCd_SignalRehash = FALSE;
69 NGIRCd_NoDaemon = NGIRCd_Passive = FALSE;
70 #ifdef DEBUG
71 NGIRCd_Debug = FALSE;
72 #endif
73 #ifdef SNIFFER
74 NGIRCd_Sniffer = FALSE;
75 #endif
76 strlcpy( NGIRCd_ConfFile, SYSCONFDIR, sizeof( NGIRCd_ConfFile ));
77 strlcat( NGIRCd_ConfFile, CONFIG_FILE, sizeof( NGIRCd_ConfFile ));
79 /* Kommandozeile parsen */
80 for( i = 1; i < argc; i++ )
81 {
82 ok = FALSE;
83 if(( argv[i][0] == '-' ) && ( argv[i][1] == '-' ))
84 {
85 /* Lange Option */
87 if( strcmp( argv[i], "--config" ) == 0 )
88 {
89 if( i + 1 < argc )
90 {
91 /* Ok, there's an parameter left */
92 strlcpy( NGIRCd_ConfFile, argv[i + 1], sizeof( NGIRCd_ConfFile ));
94 /* next parameter */
95 i++; ok = TRUE;
96 }
97 }
98 if( strcmp( argv[i], "--configtest" ) == 0 )
99 {
100 configtest = TRUE;
101 ok = TRUE;
103 #ifdef DEBUG
104 if( strcmp( argv[i], "--debug" ) == 0 )
106 NGIRCd_Debug = TRUE;
107 ok = TRUE;
109 #endif
110 if( strcmp( argv[i], "--help" ) == 0 )
112 Show_Version( );
113 puts( "" ); Show_Help( ); puts( "" );
114 exit( 1 );
116 if( strcmp( argv[i], "--nodaemon" ) == 0 )
118 NGIRCd_NoDaemon = TRUE;
119 ok = TRUE;
121 if( strcmp( argv[i], "--passive" ) == 0 )
123 NGIRCd_Passive = TRUE;
124 ok = TRUE;
126 #ifdef SNIFFER
127 if( strcmp( argv[i], "--sniffer" ) == 0 )
129 NGIRCd_Sniffer = TRUE;
130 ok = TRUE;
132 #endif
133 if( strcmp( argv[i], "--version" ) == 0 )
135 Show_Version( );
136 exit( 1 );
139 else if(( argv[i][0] == '-' ) && ( argv[i][1] != '-' ))
141 /* Kurze Option */
143 for( n = 1; n < (LONG)strlen( argv[i] ); n++ )
145 ok = FALSE;
146 #ifdef DEBUG
147 if( argv[i][n] == 'd' )
149 NGIRCd_Debug = TRUE;
150 ok = TRUE;
152 #endif
153 if( argv[i][n] == 'f' )
155 if(( ! argv[i][n + 1] ) && ( i + 1 < argc ))
157 /* Ok, next character is a blank */
158 strlcpy( NGIRCd_ConfFile, argv[i + 1], sizeof( NGIRCd_ConfFile ));
160 /* go to the following parameter */
161 i++; n = (LONG)strlen( argv[i] );
162 ok = TRUE;
165 if( argv[i][n] == 'n' )
167 NGIRCd_NoDaemon = TRUE;
168 ok = TRUE;
170 if( argv[i][n] == 'p' )
172 NGIRCd_Passive = TRUE;
173 ok = TRUE;
175 #ifdef SNIFFER
176 if( argv[i][n] == 's' )
178 NGIRCd_Sniffer = TRUE;
179 ok = TRUE;
181 #endif
183 if( ! ok )
185 printf( "%s: invalid option \"-%c\"!\n", PACKAGE, argv[i][n] );
186 printf( "Try \"%s --help\" for more information.\n", PACKAGE );
187 exit( 1 );
192 if( ! ok )
194 printf( "%s: invalid option \"%s\"!\n", PACKAGE, argv[i] );
195 printf( "Try \"%s --help\" for more information.\n", PACKAGE );
196 exit( 1 );
200 /* Debug-Level (fuer IRC-Befehl "VERSION") ermitteln */
201 strcpy( NGIRCd_DebugLevel, "" );
202 #ifdef DEBUG
203 if( NGIRCd_Debug ) strcpy( NGIRCd_DebugLevel, "1" );
204 #endif
205 #ifdef SNIFFER
206 if( NGIRCd_Sniffer )
208 NGIRCd_Debug = TRUE;
209 strcpy( NGIRCd_DebugLevel, "2" );
211 #endif
213 /* Soll nur die Konfigurations ueberprueft und ausgegeben werden? */
214 if( configtest )
216 Show_Version( ); puts( "" );
217 exit( Conf_Test( ));
220 while( ! NGIRCd_SignalQuit )
222 /* In der Regel wird ein Sub-Prozess ge-fork()'t, der
223 * nicht mehr mit dem Terminal verbunden ist. Mit der
224 * Option "--nodaemon" kann dies (z.B. zum Debuggen)
225 * verhindert werden. */
226 if( ! NGIRCd_NoDaemon )
228 /* Daemon im Hintergrund erzeugen */
229 pid = (LONG)fork( );
230 if( pid > 0 )
232 /* "alter" Prozess */
233 exit( 0 );
235 if( pid < 0 )
237 /* Fehler */
238 printf( "%s: Can't fork: %s!\nFatal error, exiting now ...\n", PACKAGE, strerror( errno ));
239 exit( 1 );
242 /* Child-Prozess initialisieren */
243 (VOID)setsid( );
244 chdir( "/" );
247 /* Globale Variablen initialisieren */
248 NGIRCd_Start = time( NULL );
249 (VOID)strftime( NGIRCd_StartStr, 64, "%a %b %d %Y at %H:%M:%S (%Z)", localtime( &NGIRCd_Start ));
250 NGIRCd_SignalRehash = FALSE;
251 NGIRCd_SignalRestart = FALSE;
252 NGIRCd_SignalQuit = FALSE;
254 /* Module initialisieren */
255 Log_Init( );
256 Resolve_Init( );
257 Conf_Init( );
258 Lists_Init( );
259 Channel_Init( );
260 Client_Init( );
261 Conn_Init( );
263 /* Wenn als root ausgefuehrt und eine andere UID
264 * konfiguriert ist, jetzt zu dieser wechseln */
265 if( getuid( ) == 0 )
267 if( Conf_GID != 0 )
269 /* Neue Group-ID setzen */
270 if( setgid( Conf_GID ) != 0 ) Log( LOG_ERR, "Can't change Group-ID to %u: %s", Conf_GID, strerror( errno ));
272 if( Conf_UID != 0 )
274 /* Neue User-ID setzen */
275 if( setuid( Conf_UID ) != 0 ) Log( LOG_ERR, "Can't change User-ID to %u: %s", Conf_UID, strerror( errno ));
279 /* User, Gruppe und Prozess-ID des Daemon ausgeben */
280 pwd = getpwuid( getuid( )); grp = getgrgid( getgid( ));
281 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( ), (LONG)getpid( ));
283 /* stderr in "Error-File" umlenken */
284 Log_InitErrorfile( );
286 /* Signal-Handler initialisieren */
287 Initialize_Signal_Handler( );
289 /* Protokoll- und Server-Identifikation erzeugen. Die vom ngIRCd
290 * beim PASS-Befehl verwendete Syntax sowie die erweiterten Flags
291 * sind in doc/Protocol.txt beschrieben. */
292 #ifdef IRCPLUS
293 sprintf( NGIRCd_ProtoID, "%s%s %s|%s:%s", PROTOVER, PROTOIRCPLUS, PACKAGE, VERSION, IRCPLUSFLAGS );
294 #ifdef USE_ZLIB
295 strcat( NGIRCd_ProtoID, "Z" );
296 #endif
297 if( Conf_OperCanMode ) strcat( NGIRCd_ProtoID, "o" );
298 #else
299 sprintf( NGIRCd_ProtoID, "%s%s %s|%s", PROTOVER, PROTOIRC, PACKAGE, VERSION );
300 #endif
301 strcat( NGIRCd_ProtoID, " P" );
302 #ifdef USE_ZLIB
303 strcat( NGIRCd_ProtoID, "Z" );
304 #endif
305 Log( LOG_DEBUG, "Protocol and server ID is \"%s\".", NGIRCd_ProtoID );
307 /* Vordefinierte Channels anlegen */
308 Channel_InitPredefined( );
310 /* Listen-Ports initialisieren */
311 if( Conn_InitListeners( ) < 1 )
313 Log( LOG_ALERT, "Server isn't listening on a single port!" );
314 Log( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE );
315 exit( 1 );
318 /* Hauptschleife */
319 Conn_Handler( );
321 /* Alles abmelden */
322 Conn_Exit( );
323 Client_Exit( );
324 Channel_Exit( );
325 Lists_Exit( );
326 Log_Exit( );
329 return 0;
330 } /* main */
333 GLOBAL CHAR *
334 NGIRCd_Version( VOID )
336 STATIC CHAR version[126];
338 #ifdef CVSDATE
339 sprintf( version, "%s %s(%s)-%s", PACKAGE, VERSION, CVSDATE, NGIRCd_VersionAddition( ));
340 #else
341 sprintf( version, "%s %s-%s", PACKAGE, VERSION, NGIRCd_VersionAddition( ));
342 #endif
343 return version;
344 } /* NGIRCd_Version */
347 GLOBAL CHAR *
348 NGIRCd_VersionAddition( VOID )
350 STATIC CHAR txt[64];
352 strcpy( txt, "" );
354 #ifdef USE_SYSLOG
355 if( txt[0] ) strcat( txt, "+" );
356 strcat( txt, "SYSLOG" );
357 #endif
358 #ifdef USE_ZLIB
359 if( txt[0] ) strcat( txt, "+" );
360 strcat( txt, "ZLIB" );
361 #endif
362 #ifdef DEBUG
363 if( txt[0] ) strcat( txt, "+" );
364 strcat( txt, "DEBUG" );
365 #endif
366 #ifdef SNIFFER
367 if( txt[0] ) strcat( txt, "+" );
368 strcat( txt, "SNIFFER" );
369 #endif
370 #ifdef STRICT_RFC
371 if( txt[0] ) strcat( txt, "+" );
372 strcat( txt, "RFC" );
373 #endif
374 #ifdef IRCPLUS
375 if( txt[0] ) strcat( txt, "+" );
376 strcat( txt, "IRCPLUS" );
377 #endif
379 if( txt[0] ) strlcat( txt, "-", sizeof( txt ));
380 strlcat( txt, TARGET_CPU, sizeof( txt ));
381 strlcat( txt, "/", sizeof( txt ));
382 strlcat( txt, TARGET_VENDOR, sizeof( txt ));
383 strlcat( txt, "/", sizeof( txt ));
384 strlcat( txt, TARGET_OS, sizeof( txt ));
386 return txt;
387 } /* NGIRCd_VersionAddition */
390 GLOBAL VOID
391 NGIRCd_Rehash( VOID )
393 CHAR old_name[CLIENT_ID_LEN];
395 Log( LOG_NOTICE|LOG_snotice, "Re-reading configuration NOW!" );
396 NGIRCd_SignalRehash = FALSE;
398 /* Alle Listen-Sockets schliessen */
399 Conn_ExitListeners( );
401 /* Alten Server-Namen merken */
402 assert( sizeof( old_name ) == sizeof( Conf_ServerName ));
403 strcpy( old_name, Conf_ServerName );
405 /* Konfiguration neu lesen ... */
406 Conf_Rehash( );
408 /* Alten Server-Namen wiederherstellen: dieser
409 * kann nicht zur Laufzeit geaendert werden ... */
410 if( strcmp( old_name, Conf_ServerName ) != 0 )
412 strcpy( Conf_ServerName, old_name );
413 Log( LOG_ERR, "Can't change \"ServerName\" on runtime! Ignored new name." );
416 /* neue pre-defined Channel anlegen: */
417 Channel_InitPredefined( );
419 /* Listen-Sockets neu anlegen: */
420 Conn_InitListeners( );
422 Log( LOG_NOTICE|LOG_snotice, "Re-reading of configuration done." );
423 } /* NGIRCd_Rehash */
426 LOCAL VOID
427 Initialize_Signal_Handler( VOID )
429 /* Signal-Handler initialisieren: einige Signale
430 * werden ignoriert, andere speziell behandelt. */
432 #ifdef HAVE_SIGACTION
433 /* sigaction() ist vorhanden */
435 struct sigaction saction;
437 /* Signal-Struktur initialisieren */
438 memset( &saction, 0, sizeof( saction ));
439 saction.sa_handler = Signal_Handler;
440 #ifdef SA_RESTART
441 saction.sa_flags |= SA_RESTART;
442 #endif
443 #ifdef SA_NOCLDWAIT
444 saction.sa_flags |= SA_NOCLDWAIT;
445 #endif
447 /* Signal-Handler einhaengen */
448 sigaction( SIGINT, &saction, NULL );
449 sigaction( SIGQUIT, &saction, NULL );
450 sigaction( SIGTERM, &saction, NULL);
451 sigaction( SIGHUP, &saction, NULL);
452 sigaction( SIGCHLD, &saction, NULL);
454 /* einige Signale ignorieren */
455 saction.sa_handler = SIG_IGN;
456 sigaction( SIGPIPE, &saction, NULL );
457 #else
458 /* kein sigaction() vorhanden */
460 /* Signal-Handler einhaengen */
461 signal( SIGINT, Signal_Handler );
462 signal( SIGQUIT, Signal_Handler );
463 signal( SIGTERM, Signal_Handler );
464 signal( SIGHUP, Signal_Handler );
465 signal( SIGCHLD, Signal_Handler );
467 /* einige Signale ignorieren */
468 signal( SIGPIPE, SIG_IGN );
469 #endif
470 } /* Initialize_Signal_Handler */
473 LOCAL VOID
474 Signal_Handler( INT Signal )
476 /* Signal-Handler. Dieser wird aufgerufen, wenn eines der Signale eintrifft,
477 * fuer das wir uns registriert haben (vgl. Initialize_Signal_Handler). Die
478 * Nummer des eingetroffenen Signals wird der Funktion uebergeben. */
480 switch( Signal )
482 case SIGTERM:
483 case SIGINT:
484 case SIGQUIT:
485 /* wir soll(t)en uns wohl beenden ... */
486 NGIRCd_SignalQuit = TRUE;
487 break;
488 case SIGHUP:
489 /* Konfiguration neu einlesen: */
490 NGIRCd_SignalRehash = TRUE;
491 break;
492 case SIGCHLD:
493 /* Child-Prozess wurde beendet. Zombies vermeiden: */
494 while( waitpid( -1, NULL, WNOHANG ) > 0);
495 break;
496 #ifdef DEBUG
497 default:
498 /* unbekanntes bzw. unbehandeltes Signal */
499 Log( LOG_DEBUG, "Got signal %d! Ignored.", Signal );
500 #endif
502 } /* Signal_Handler */
505 LOCAL VOID
506 Show_Version( VOID )
508 puts( NGIRCd_Version( ));
509 puts( "Copyright (c)2001-2003 by Alexander Barton (<alex@barton.de>)." );
510 puts( "Homepage: <http://arthur.ath.cx/~alex/ngircd/>\n" );
511 puts( "This is free software; see the source for copying conditions. There is NO" );
512 puts( "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE." );
513 } /* Show_Version */
516 LOCAL VOID
517 Show_Help( VOID )
519 #ifdef DEBUG
520 puts( " -d, --debug log extra debug messages" );
521 #endif
522 puts( " -f, --config <f> use file <f> as configuration file" );
523 puts( " -n, --nodaemon don't fork and don't detach from controlling terminal" );
524 puts( " -p, --passive disable automatic connections to other servers" );
525 #ifdef SNIFFER
526 puts( " -s, --sniffer enable network sniffer and display all IRC traffic" );
527 #endif
528 puts( " --configtest read, validate and display configuration; then exit" );
529 puts( " --version output version information and exit" );
530 puts( " --help display this help and exit" );
531 } /* Show_Help */
534 /* -eof- */