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