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