- Rechtschreibfehler korrigiert ;-)
[ngircd-alex.git] / src / ngircd / ngircd.c
1 /*
2  * ngIRCd -- The Next Generation IRC Daemon
3  * Copyright (c)2001,2002 by Alexander Barton (alex@barton.de)
4  *
5  * Dieses Programm ist freie Software. Sie koennen es unter den Bedingungen
6  * der GNU General Public License (GPL), wie von der Free Software Foundation
7  * herausgegeben, weitergeben und/oder modifizieren, entweder unter Version 2
8  * der Lizenz oder (wenn Sie es wuenschen) jeder spaeteren Version.
9  * Naehere Informationen entnehmen Sie bitter der Datei COPYING. Eine Liste
10  * der an ngIRCd beteiligten Autoren finden Sie in der Datei AUTHORS.
11  *
12  * $Id: ngircd.c,v 1.59 2002/11/18 18:49:34 alex Exp $
13  *
14  * ngircd.c: Hier beginnt alles ;-)
15  */
16
17
18 #include "portab.h"
19
20 #include "imp.h"
21 #include <assert.h>
22 #include <errno.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <signal.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <sys/wait.h>
31 #include <time.h>
32 #include <pwd.h>
33 #include <grp.h>
34
35 #include "resolve.h"
36 #include "conn.h"
37 #include "client.h"
38 #include "channel.h"
39 #include "conf.h"
40 #include "cvs-version.h"
41 #include "defines.h"
42 #include "lists.h"
43 #include "log.h"
44 #include "parse.h"
45 #include "irc.h"
46
47 #include "exp.h"
48 #include "ngircd.h"
49
50
51 LOCAL VOID Initialize_Signal_Handler PARAMS(( VOID ));
52 LOCAL VOID Signal_Handler PARAMS(( INT Signal ));
53
54 LOCAL VOID Initialize_Listen_Ports PARAMS(( VOID ));
55
56 LOCAL VOID Show_Version PARAMS(( VOID ));
57 LOCAL VOID Show_Help PARAMS(( VOID ));
58
59
60 GLOBAL int
61 main( int argc, const char *argv[] )
62 {
63         struct passwd *pwd;
64         struct group *grp;
65         BOOLEAN ok, configtest = FALSE;
66         LONG pid, n;
67         INT i;
68
69         umask( 0077 );
70
71         NGIRCd_Restart = FALSE;
72         NGIRCd_Quit = FALSE;
73         NGIRCd_NoDaemon = FALSE;
74         NGIRCd_Passive = FALSE;
75 #ifdef DEBUG
76         NGIRCd_Debug = FALSE;
77 #endif
78 #ifdef SNIFFER
79         NGIRCd_Sniffer = FALSE;
80 #endif
81         strcpy( NGIRCd_ConfFile, CONFIG_FILE );
82
83         /* Kommandozeile parsen */
84         for( i = 1; i < argc; i++ )
85         {
86                 ok = FALSE;
87                 if(( argv[i][0] == '-' ) && ( argv[i][1] == '-' ))
88                 {
89                         /* Lange Option */
90
91                         if( strcmp( argv[i], "--config" ) == 0 )
92                         {
93                                 if( i + 1 < argc )
94                                 {
95                                         /* Ok, danach kommt noch ein Parameter */
96                                         strncpy( NGIRCd_ConfFile, argv[i + 1], FNAME_LEN - 1 );
97                                         NGIRCd_ConfFile[FNAME_LEN - 1] = '\0';
98
99                                         /* zum uebernaechsten Parameter */
100                                         i++; ok = TRUE;
101                                 }
102                         }
103                         if( strcmp( argv[i], "--configtest" ) == 0 )
104                         {
105                                 configtest = TRUE;
106                                 ok = TRUE;
107                         }
108 #ifdef DEBUG
109                         if( strcmp( argv[i], "--debug" ) == 0 )
110                         {
111                                 NGIRCd_Debug = TRUE;
112                                 ok = TRUE;
113                         }
114 #endif
115                         if( strcmp( argv[i], "--help" ) == 0 )
116                         {
117                                 Show_Version( );
118                                 puts( "" ); Show_Help( ); puts( "" );
119                                 exit( 1 );
120                         }
121                         if( strcmp( argv[i], "--nodaemon" ) == 0 )
122                         {
123                                 NGIRCd_NoDaemon = TRUE;
124                                 ok = TRUE;
125                         }
126                         if( strcmp( argv[i], "--passive" ) == 0 )
127                         {
128                                 NGIRCd_Passive = TRUE;
129                                 ok = TRUE;
130                         }
131 #ifdef SNIFFER
132                         if( strcmp( argv[i], "--sniffer" ) == 0 )
133                         {
134                                 NGIRCd_Sniffer = TRUE;
135                                 ok = TRUE;
136                         }
137 #endif
138                         if( strcmp( argv[i], "--version" ) == 0 )
139                         {
140                                 Show_Version( );
141                                 exit( 1 );
142                         }
143                 }
144                 else if(( argv[i][0] == '-' ) && ( argv[i][1] != '-' ))
145                 {
146                         /* Kurze Option */
147                         
148                         for( n = 1; n < (LONG)strlen( argv[i] ); n++ )
149                         {
150                                 ok = FALSE;
151 #ifdef DEBUG
152                                 if( argv[i][n] == 'd' )
153                                 {
154                                         NGIRCd_Debug = TRUE;
155                                         ok = TRUE;
156                                 }
157 #endif
158                                 if( argv[i][n] == 'f' )
159                                 {
160                                         if(( ! argv[i][n + 1] ) && ( i + 1 < argc ))
161                                         {
162                                                 /* Ok, danach kommt ein Leerzeichen */
163                                                 strncpy( NGIRCd_ConfFile, argv[i + 1], FNAME_LEN - 1 );
164                                                 NGIRCd_ConfFile[FNAME_LEN - 1] = '\0';
165
166                                                 /* zum uebernaechsten Parameter */
167                                                 i++; n = (LONG)strlen( argv[i] );
168                                                 ok = TRUE;
169                                         }
170                                 }
171                                 if( argv[i][n] == 'n' )
172                                 {
173                                         NGIRCd_NoDaemon = TRUE;
174                                         ok = TRUE;
175                                 }
176                                 if( argv[i][n] == 'p' )
177                                 {
178                                         NGIRCd_Passive = TRUE;
179                                         ok = TRUE;
180                                 }
181 #ifdef SNIFFER
182                                 if( argv[i][n] == 's' )
183                                 {
184                                         NGIRCd_Sniffer = TRUE;
185                                         ok = TRUE;
186                                 }
187 #endif
188
189                                 if( ! ok )
190                                 {
191                                         printf( "%s: invalid option \"-%c\"!\n", PACKAGE, argv[i][n] );
192                                         printf( "Try \"%s --help\" for more information.\n", PACKAGE );
193                                         exit( 1 );
194                                 }
195                         }
196
197                 }
198                 if( ! ok )
199                 {
200                         printf( "%s: invalid option \"%s\"!\n", PACKAGE, argv[i] );
201                         printf( "Try \"%s --help\" for more information.\n", PACKAGE );
202                         exit( 1 );
203                 }
204         }
205
206         /* Debug-Level (fuer IRC-Befehl "VERSION") ermitteln */
207         strcpy( NGIRCd_DebugLevel, "" );
208 #ifdef DEBUG
209         if( NGIRCd_Debug ) strcpy( NGIRCd_DebugLevel, "1" );
210 #endif
211 #ifdef SNIFFER
212         if( NGIRCd_Sniffer )
213         {
214                 NGIRCd_Debug = TRUE;
215                 strcpy( NGIRCd_DebugLevel, "2" );
216         }
217 #endif
218
219         /* Soll nur die Konfigurations ueberprueft und ausgegeben werden? */
220         if( configtest )
221         {
222                 Show_Version( ); puts( "" );
223                 exit( Conf_Test( ));
224         }
225         
226         while( ! NGIRCd_Quit )
227         {
228                 /* In der Regel wird ein Sub-Prozess ge-fork()'t, der
229                  * nicht mehr mit dem Terminal verbunden ist. Mit der
230                  * Option "--nodaemon" kann dies (z.B. zum Debuggen)
231                  * verhindert werden. */
232                 if( ! NGIRCd_NoDaemon )
233                 {
234                         /* Daemon im Hintergrund erzeugen */
235                         pid = (LONG)fork( );
236                         if( pid > 0 )
237                         {
238                                 /* "alter" Prozess */
239                                 exit( 0 );
240                         }
241                         if( pid < 0 )
242                         {
243                                 /* Fehler */
244                                 printf( "%s: Can't fork: %s!\nFatal error, exiting now ...\n", PACKAGE, strerror( errno ));
245                                 exit( 1 );
246                         }
247
248                         /* Child-Prozess initialisieren */
249                         (VOID)setsid( );
250                         chdir( "/" );
251                 }
252         
253                 /* Globale Variablen initialisieren */
254                 NGIRCd_Start = time( NULL );
255                 (VOID)strftime( NGIRCd_StartStr, 64, "%a %b %d %Y at %H:%M:%S (%Z)", localtime( &NGIRCd_Start ));
256                 NGIRCd_Restart = FALSE;
257                 NGIRCd_Quit = FALSE;
258
259                 /* Module initialisieren */
260                 Log_Init( );
261                 Resolve_Init( );
262                 Conf_Init( );
263                 Lists_Init( );
264                 Channel_Init( );
265                 Client_Init( );
266                 Conn_Init( );
267
268                 /* Wenn als root ausgefuehrt und eine andere UID
269                  * konfiguriert ist, jetzt zu dieser wechseln */
270                 if( getuid( ) == 0 )
271                 {
272                         if( Conf_GID != 0 )
273                         {
274                                 /* Neue Group-ID setzen */
275                                 if( setgid( Conf_GID ) != 0 ) Log( LOG_ERR, "Can't change Group-ID to %u: %s", Conf_GID, strerror( errno ));
276                         }
277                         if( Conf_UID != 0 )
278                         {
279                                 /* Neue User-ID setzen */
280                                 if( setuid( Conf_UID ) != 0 ) Log( LOG_ERR, "Can't change User-ID to %u: %s", Conf_UID, strerror( errno ));
281                         }
282                 }
283                 
284                 /* User, Gruppe und Prozess-ID des Daemon ausgeben */
285                 pwd = getpwuid( getuid( )); grp = getgrgid( getgid( ));
286                 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( ));
287
288                 /* stderr in "Error-File" umlenken */
289                 Log_InitErrorfile( );
290
291                 /* Signal-Handler initialisieren */
292                 Initialize_Signal_Handler( );
293
294                 /* Protokoll- und Server-Identifikation erzeugen. Die vom ngIRCd
295                  * beim PASS-Befehl verwendete Syntax sowie die erweiterten Flags
296                  * sind in doc/Protocol.txt beschrieben. */
297 #ifdef IRCPLUS
298                 sprintf( NGIRCd_ProtoID, "%s%s %s|%s:%s", PROTOVER, PROTOIRCPLUS, PACKAGE, VERSION, IRCPLUSFLAGS );
299                 if( Conf_OperCanMode ) strcat( NGIRCd_ProtoID, "o" );
300 #else
301                 sprintf( NGIRCd_ProtoID, "%s%s %s|%s", PROTOVER, PROTOIRC, PACKAGE, VERSION );
302 #endif
303                 strcat( NGIRCd_ProtoID, " P" );
304                 Log( LOG_DEBUG, "Protocol and server ID is \"%s\".", NGIRCd_ProtoID );
305
306                 /* Vordefinierte Channels anlegen */
307                 Channel_InitPredefined( );
308
309                 /* Listen-Ports initialisieren */
310                 Initialize_Listen_Ports( );
311
312                 /* Hauptschleife */
313                 Conn_Handler( );
314
315                 /* Alles abmelden */
316                 Conn_Exit( );
317                 Client_Exit( );
318                 Channel_Exit( );
319                 Lists_Exit( );
320                 Log_Exit( );
321         }
322
323         return 0;
324 } /* main */
325
326
327 GLOBAL CHAR *
328 NGIRCd_Version( VOID )
329 {
330         STATIC CHAR version[126];
331         
332 #ifdef CVSDATE
333         sprintf( version, "%s %s(%s)-%s", PACKAGE, VERSION, CVSDATE, NGIRCd_VersionAddition( ));
334 #else
335         sprintf( version, "%s %s-%s", PACKAGE, VERSION, NGIRCd_VersionAddition( ));
336 #endif
337         return version;
338 } /* NGIRCd_Version */
339
340
341 GLOBAL CHAR *
342 NGIRCd_VersionAddition( VOID )
343 {
344         STATIC CHAR txt[64];
345
346         strcpy( txt, "" );
347
348 #ifdef USE_SYSLOG
349         if( txt[0] ) strcat( txt, "+" );
350         strcat( txt, "SYSLOG" );
351 #endif
352 #ifdef DEBUG
353         if( txt[0] ) strcat( txt, "+" );
354         strcat( txt, "DEBUG" );
355 #endif
356 #ifdef SNIFFER
357         if( txt[0] ) strcat( txt, "+" );
358         strcat( txt, "SNIFFER" );
359 #endif
360 #ifdef STRICT_RFC
361         if( txt[0] ) strcat( txt, "+" );
362         strcat( txt, "RFC" );
363 #endif
364 #ifdef IRCPLUS
365         if( txt[0] ) strcat( txt, "+" );
366         strcat( txt, "IRCPLUS" );
367 #endif
368         
369         if( txt[0] ) strcat( txt, "-" );
370         strcat( txt, TARGET_CPU );
371         strcat( txt, "/" );
372         strcat( txt, TARGET_VENDOR );
373         strcat( txt, "/" );
374         strcat( txt, TARGET_OS );
375
376         return txt;
377 } /* NGIRCd_VersionAddition */
378
379
380 LOCAL VOID
381 Initialize_Signal_Handler( VOID )
382 {
383         /* Signal-Handler initialisieren: einige Signale
384          * werden ignoriert, andere speziell behandelt. */
385
386 #ifdef HAVE_SIGACTION
387         /* sigaction() ist vorhanden */
388
389         struct sigaction saction;
390
391         /* Signal-Struktur initialisieren */
392         memset( &saction, 0, sizeof( saction ));
393         saction.sa_handler = Signal_Handler;
394 #ifdef SA_RESTART
395         saction.sa_flags |= SA_RESTART;
396 #endif
397 #ifdef SA_NOCLDWAIT
398         saction.sa_flags |= SA_NOCLDWAIT;
399 #endif
400
401         /* Signal-Handler einhaengen */
402         sigaction( SIGINT, &saction, NULL );
403         sigaction( SIGQUIT, &saction, NULL );
404         sigaction( SIGTERM, &saction, NULL);
405         sigaction( SIGHUP, &saction, NULL);
406         sigaction( SIGCHLD, &saction, NULL);
407
408         /* einige Signale ignorieren */
409         saction.sa_handler = SIG_IGN;
410         sigaction( SIGPIPE, &saction, NULL );
411 #else
412         /* kein sigaction() vorhanden */
413
414         /* Signal-Handler einhaengen */
415         signal( SIGINT, Signal_Handler );
416         signal( SIGQUIT, Signal_Handler );
417         signal( SIGTERM, Signal_Handler );
418         signal( SIGHUP, Signal_Handler );
419         signal( SIGCHLD, Signal_Handler );
420
421         /* einige Signale ignorieren */
422         signal( SIGPIPE, SIG_IGN );
423 #endif
424 } /* Initialize_Signal_Handler */
425
426
427 LOCAL VOID
428 Signal_Handler( INT Signal )
429 {
430         /* Signal-Handler. Dieser wird aufgerufen, wenn eines der Signale eintrifft,
431          * fuer das wir uns registriert haben (vgl. Initialize_Signal_Handler). Die
432          * Nummer des eingetroffenen Signals wird der Funktion uebergeben. */
433
434         switch( Signal )
435         {
436                 case SIGTERM:
437                 case SIGINT:
438                 case SIGQUIT:
439                         /* wir soll(t)en uns wohl beenden ... */
440                         if( Signal == SIGTERM ) Log( LOG_WARNING, "Got TERM signal, terminating now ..." );
441                         else if( Signal == SIGINT ) Log( LOG_WARNING, "Got INT signal, terminating now ..." );
442                         else if( Signal == SIGQUIT ) Log( LOG_WARNING, "Got QUIT signal, terminating now ..." );
443                         NGIRCd_Quit = TRUE;
444                         break;
445                 case SIGHUP:
446                         /* neu starten */
447                         Log( LOG_WARNING, "Got HUP signal, restarting now ..." );
448                         NGIRCd_Restart = TRUE;
449                         break;
450                 case SIGCHLD:
451                         /* Child-Prozess wurde beendet. Zombies vermeiden: */
452                         while( waitpid( -1, NULL, WNOHANG ) > 0);
453                         break;
454                 default:
455                         /* unbekanntes bzw. unbehandeltes Signal */
456                         Log( LOG_NOTICE, "Got signal %d! Ignored.", Signal );
457         }
458 } /* Signal_Handler */
459
460
461 LOCAL VOID
462 Initialize_Listen_Ports( VOID )
463 {
464         /* Ports, auf denen der Server Verbindungen entgegennehmen
465          * soll, initialisieren */
466         
467         INT created, i;
468
469         created = 0;
470         for( i = 0; i < Conf_ListenPorts_Count; i++ )
471         {
472                 if( Conn_NewListener( Conf_ListenPorts[i] )) created++;
473                 else Log( LOG_ERR, "Can't listen on port %u!", Conf_ListenPorts[i] );
474         }
475
476         if( created < 1 )
477         {
478                 Log( LOG_ALERT, "Server isn't listening on a single port!" );
479                 Log( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE );
480                 exit( 1 );
481         }
482 } /* Initialize_Listen_Ports */
483
484
485 LOCAL VOID
486 Show_Version( VOID )
487 {
488         puts( NGIRCd_Version( ));
489         puts( "Copyright (c)2001,2002 by Alexander Barton (<alex@barton.de>)." );
490         puts( "Homepage: <http://arthur.ath.cx/~alex/ngircd/>\n" );
491         puts( "This is free software; see the source for copying conditions. There is NO" );
492         puts( "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE." );
493 } /* Show_Version */
494
495
496 LOCAL VOID
497 Show_Help( VOID )
498 {
499 #ifdef DEBUG
500         puts( "  -d, --debug        log extra debug messages" );
501 #endif
502         puts( "  -f, --config <f>   use file <f> as configuration file" );
503         puts( "  -n, --nodaemon     don't fork and don't detach from controlling terminal" );
504         puts( "  -p, --passive      disable automatic connections to other servers" );
505 #ifdef SNIFFER
506         puts( "  -s, --sniffer      enable network sniffer and display all IRC traffic" );
507 #endif
508         puts( "      --configtest   read, validate and display configuration; then exit" );
509         puts( "      --version      output version information and exit" );
510         puts( "      --help         display this help and exit" );
511 } /* Show_Help */
512
513
514 /* -eof- */