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