]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/ngircd.c
184746be2cef0a2831d99fa5ac5115bce1946017
[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.45 2002/05/18 12:20: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/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 )
202         {
203                 NGIRCd_Debug = TRUE;
204                 strcpy( NGIRCd_DebugLevel, "2" );
205         }
206 #endif
207
208         /* Soll nur die Konfigurations ueberprueft und ausgegeben werden? */
209         if( configtest )
210         {
211                 Show_Version( ); puts( "" );
212                 exit( Conf_Test( ));
213         }
214         
215         while( ! NGIRCd_Quit )
216         {
217                 /* In der Regel wird ein Sub-Prozess ge-fork()'t, der
218                  * nicht mehr mit dem Terminal verbunden ist. Mit der
219                  * Option "--nodaemon" kann dies (z.B. zum Debuggen)
220                  * verhindert werden. */
221                 if( ! NGIRCd_NoDaemon )
222                 {
223                         /* Daemon im Hintergrund erzeugen */
224                         pid = (INT32)fork( );
225                         if( pid > 0 )
226                         {
227                                 /* "alter" Prozess */
228                                 exit( 0 );
229                         }
230                         if( pid < 0 )
231                         {
232                                 /* Fehler */
233                                 printf( PACKAGE": Can't fork: %s!\nFatal error, exiting now ...", strerror( errno ));
234                                 exit( 1 );
235                         }
236
237                         /* Child-Prozess initialisieren */
238                         (VOID)setsid( );
239                         chdir( "/" );
240                 }
241         
242                 /* Globale Variablen initialisieren */
243                 NGIRCd_Start = time( NULL );
244                 (VOID)strftime( NGIRCd_StartStr, 64, "%a %b %d %Y at %H:%M:%S (%Z)", localtime( &NGIRCd_Start ));
245                 NGIRCd_Restart = FALSE;
246                 NGIRCd_Quit = FALSE;
247
248                 /* Module initialisieren */
249                 Log_Init( );
250                 Conf_Init( );
251                 Channel_Init( );
252                 Client_Init( );
253                 Conn_Init( );
254
255                 /* Wenn als root ausgefuehrt und eine andere UID
256                  * konfiguriert ist, jetzt zu dieser wechseln */
257                 if( getuid( ) == 0 )
258                 {
259                         if( Conf_GID != 0 )
260                         {
261                                 /* Neue Group-ID setzen */
262                                 if( setgid( Conf_GID ) != 0 ) Log( LOG_ERR, "Can't change Group-ID to %u: %s", Conf_GID, strerror( errno ));
263                         }
264                         if( Conf_UID != 0 )
265                         {
266                                 /* Neue User-ID setzen */
267                                 if( setuid( Conf_UID ) != 0 ) Log( LOG_ERR, "Can't change User-ID to %u: %s", Conf_UID, strerror( errno ));
268                         }
269                 }
270                 Log( LOG_INFO, "Running as user %ld, group %ld.", (INT32)getuid( ), (INT32)getgid( ));
271
272                 Log_InitErrorfile( );
273
274                 /* Signal-Handler initialisieren */
275                 Initialize_Signal_Handler( );
276
277                 /* Listen-Ports initialisieren */
278                 Initialize_Listen_Ports( );
279
280                 /* Hauptschleife */
281                 while( TRUE )
282                 {
283                         if( NGIRCd_Quit || NGIRCd_Restart ) break;
284                         Conn_Handler( 5 );
285                 }
286
287                 /* Alles abmelden */
288                 Conn_Exit( );
289                 Client_Exit( );
290                 Channel_Exit( );
291                 Conf_Exit( );
292                 Log_Exit( );
293         }
294
295         return 0;
296 } /* main */
297
298
299 GLOBAL CHAR *NGIRCd_Version( VOID )
300 {
301         STATIC CHAR version[126];
302
303         sprintf( version, PACKAGE" version "VERSION"-%s", NGIRCd_VersionAddition( ));
304         return version;
305 } /* NGIRCd_Version */
306
307
308 GLOBAL CHAR *NGIRCd_VersionAddition( VOID )
309 {
310         STATIC CHAR txt[64];
311
312         strcpy( txt, "" );
313
314 #ifdef USE_SYSLOG
315         if( txt[0] ) strcat( txt, "+" );
316         strcat( txt, "SYSLOG" );
317 #endif
318 #ifdef REGEX
319         if( txt[0] ) strcat( txt, "+" );
320         strcat( txt, "REGEX" );
321 #endif
322 #ifdef STRICT_RFC
323         if( txt[0] ) strcat( txt, "+" );
324         strcat( txt, "RFC" );
325 #endif
326 #ifdef DEBUG
327         if( txt[0] ) strcat( txt, "+" );
328         strcat( txt, "DEBUG" );
329 #endif
330 #ifdef SNIFFER
331         if( txt[0] ) strcat( txt, "+" );
332         strcat( txt, "SNIFFER" );
333 #endif
334
335         if( txt[0] ) strcat( txt, "-" );
336         strcat( txt, TARGET_CPU"/"TARGET_VENDOR"/"TARGET_OS );
337
338         return txt;
339 } /* NGIRCd_VersionAddition */
340
341
342 LOCAL VOID Initialize_Signal_Handler( VOID )
343 {
344         /* Signal-Handler initialisieren: einige Signale
345          * werden ignoriert, andere speziell behandelt. */
346
347 #ifdef HAVE_SIGACTION
348         /* sigaction() ist vorhanden */
349
350         struct sigaction saction;
351
352         /* Signal-Struktur initialisieren */
353         memset( &saction, 0, sizeof( saction ));
354         saction.sa_handler = Signal_Handler;
355 #ifdef SA_RESTART
356         saction.sa_flags |= SA_RESTART;
357 #endif
358 #ifdef SA_NOCLDWAIT
359         saction.sa_flags |= SA_NOCLDWAIT;
360 #endif
361
362         /* Signal-Handler einhaengen */
363         sigaction( SIGINT, &saction, NULL );
364         sigaction( SIGQUIT, &saction, NULL );
365         sigaction( SIGTERM, &saction, NULL);
366         sigaction( SIGHUP, &saction, NULL);
367         sigaction( SIGCHLD, &saction, NULL);
368
369         /* einige Signale ignorieren */
370         saction.sa_handler = SIG_IGN;
371         sigaction( SIGPIPE, &saction, NULL );
372 #else
373         /* kein sigaction() vorhanden */
374
375         /* Signal-Handler einhaengen */
376         signal( SIGINT, Signal_Handler );
377         signal( SIGQUIT, Signal_Handler );
378         signal( SIGTERM, Signal_Handler );
379         signal( SIGHUP, Signal_Handler );
380         signal( SIGCHLD, Signal_Handler );
381
382         /* einige Signale ignorieren */
383         signal( SIGPIPE, SIG_IGN );
384 #endif
385 } /* Initialize_Signal_Handler */
386
387
388 LOCAL VOID Signal_Handler( INT Signal )
389 {
390         /* Signal-Handler. Dieser wird aufgerufen, wenn eines der Signale eintrifft,
391          * fuer das wir uns registriert haben (vgl. Initialize_Signal_Handler). Die
392          * Nummer des eingetroffenen Signals wird der Funktion uebergeben. */
393
394         switch( Signal )
395         {
396                 case SIGTERM:
397                 case SIGINT:
398                 case SIGQUIT:
399                         /* wir soll(t)en uns wohl beenden ... */
400                         if( Signal == SIGTERM ) Log( LOG_WARNING, "Got TERM signal, terminating now ..." );
401                         else if( Signal == SIGINT ) Log( LOG_WARNING, "Got INT signal, terminating now ..." );
402                         else if( Signal == SIGQUIT ) Log( LOG_WARNING, "Got QUIT signal, terminating now ..." );
403                         NGIRCd_Quit = TRUE;
404                         break;
405                 case SIGHUP:
406                         /* neu starten */
407                         Log( LOG_WARNING, "Got HUP signal, restarting now ..." );
408                         NGIRCd_Restart = TRUE;
409                         break;
410                 case SIGCHLD:
411                         /* Child-Prozess wurde beendet. Zombies vermeiden: */
412                         while( waitpid( -1, NULL, WNOHANG ) > 0);
413                         break;
414                 default:
415                         /* unbekanntes bzw. unbehandeltes Signal */
416                         Log( LOG_NOTICE, "Got signal %d! Ignored.", Signal );
417         }
418 } /* Signal_Handler */
419
420
421 LOCAL VOID Initialize_Listen_Ports( VOID )
422 {
423         /* Ports, auf denen der Server Verbindungen entgegennehmen
424          * soll, initialisieren */
425         
426         UINT created, i;
427
428         created = 0;
429         for( i = 0; i < Conf_ListenPorts_Count; i++ )
430         {
431                 if( Conn_NewListener( Conf_ListenPorts[i] )) created++;
432                 else Log( LOG_ERR, "Can't listen on port %u!", Conf_ListenPorts[i] );
433         }
434
435         if( created < 1 )
436         {
437                 Log( LOG_ALERT, "Server isn't listening on a single port!" );
438                 Log( LOG_ALERT, PACKAGE" exiting due to fatal errors!" );
439                 exit( 1 );
440         }
441 } /* Initialize_Listen_Ports */
442
443
444 LOCAL VOID Show_Version( VOID )
445 {
446         puts( NGIRCd_Version( ));
447         puts( "Copyright (c)2001,2002 by Alexander Barton (<alex@barton.de>)." );
448         puts( "Homepage: <http://arthur.ath.cx/~alex/ngircd/>\n" );
449         puts( "This is free software; see the source for copying conditions. There is NO" );
450         puts( "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE." );
451 } /* Show_Version */
452
453
454 LOCAL VOID Show_Help( VOID )
455 {
456 #ifdef DEBUG
457         puts( "  -d, --debug        log extra debug messages" );
458 #endif
459         puts( "  -f, --config <f>   use file <f> as configuration file" );
460         puts( "  -n, --nodaemon     don't fork and don't detatch from controlling terminal" );
461         puts( "  -p, --passive      disable automatic connections to other servers" );
462 #ifdef SNIFFER
463         puts( "  -s, --sniffer      enable network sniffer and display all IRC traffic" );
464 #endif
465         puts( "      --configtest   read, validate and display configuration; then exit" );
466         puts( "      --version      output version information and exit" );
467         puts( "      --help         display this help and exit" );
468 } /* Show_Help */
469
470
471 /* -eof- */