]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/ngircd.c
- stdlib.h wird nun includiert.
[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.43 2002/04/04 13:03:55 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 STRICT_RFC
315         if( txt[0] ) strcat( txt, "+" );
316         strcat( txt, "RFC" );
317 #endif
318 #ifdef DEBUG
319         if( txt[0] ) strcat( txt, "+" );
320         strcat( txt, "DEBUG" );
321 #endif
322 #ifdef SNIFFER
323         if( txt[0] ) strcat( txt, "+" );
324         strcat( txt, "SNIFFER" );
325 #endif
326
327         if( txt[0] ) strcat( txt, "-" );
328         strcat( txt, TARGET_CPU"/"TARGET_VENDOR"/"TARGET_OS );
329
330         return txt;
331 } /* NGIRCd_VersionAddition */
332
333
334 LOCAL VOID Initialize_Signal_Handler( VOID )
335 {
336         /* Signal-Handler initialisieren: einige Signale
337          * werden ignoriert, andere speziell behandelt. */
338
339 #ifdef HAVE_SIGACTION
340         /* sigaction() ist vorhanden */
341
342         struct sigaction saction;
343
344         /* Signal-Struktur initialisieren */
345         memset( &saction, 0, sizeof( saction ));
346         saction.sa_handler = Signal_Handler;
347 #ifdef SA_RESTART
348         saction.sa_flags |= SA_RESTART;
349 #endif
350 #ifdef SA_NOCLDWAIT
351         saction.sa_flags |= SA_NOCLDWAIT;
352 #endif
353
354         /* Signal-Handler einhaengen */
355         sigaction( SIGINT, &saction, NULL );
356         sigaction( SIGQUIT, &saction, NULL );
357         sigaction( SIGTERM, &saction, NULL);
358         sigaction( SIGHUP, &saction, NULL);
359         sigaction( SIGCHLD, &saction, NULL);
360
361         /* einige Signale ignorieren */
362         saction.sa_handler = SIG_IGN;
363         sigaction( SIGPIPE, &saction, NULL );
364 #else
365         /* kein sigaction() vorhanden */
366
367         /* Signal-Handler einhaengen */
368         signal( SIGINT, Signal_Handler );
369         signal( SIGQUIT, Signal_Handler );
370         signal( SIGTERM, Signal_Handler );
371         signal( SIGHUP, Signal_Handler );
372         signal( SIGCHLD, Signal_Handler );
373
374         /* einige Signale ignorieren */
375         signal( SIGPIPE, SIG_IGN );
376 #endif
377 } /* Initialize_Signal_Handler */
378
379
380 LOCAL VOID Signal_Handler( INT Signal )
381 {
382         /* Signal-Handler. Dieser wird aufgerufen, wenn eines der Signale eintrifft,
383          * fuer das wir uns registriert haben (vgl. Initialize_Signal_Handler). Die
384          * Nummer des eingetroffenen Signals wird der Funktion uebergeben. */
385
386         switch( Signal )
387         {
388                 case SIGTERM:
389                 case SIGINT:
390                 case SIGQUIT:
391                         /* wir soll(t)en uns wohl beenden ... */
392                         if( Signal == SIGTERM ) Log( LOG_WARNING, "Got TERM signal, terminating now ..." );
393                         else if( Signal == SIGINT ) Log( LOG_WARNING, "Got INT signal, terminating now ..." );
394                         else if( Signal == SIGQUIT ) Log( LOG_WARNING, "Got QUIT signal, terminating now ..." );
395                         NGIRCd_Quit = TRUE;
396                         break;
397                 case SIGHUP:
398                         /* neu starten */
399                         Log( LOG_WARNING, "Got HUP signal, restarting now ..." );
400                         NGIRCd_Restart = TRUE;
401                         break;
402                 case SIGCHLD:
403                         /* Child-Prozess wurde beendet. Zombies vermeiden: */
404                         while( waitpid( -1, NULL, WNOHANG ) > 0);
405                         break;
406                 default:
407                         /* unbekanntes bzw. unbehandeltes Signal */
408                         Log( LOG_NOTICE, "Got signal %d! Ignored.", Signal );
409         }
410 } /* Signal_Handler */
411
412
413 LOCAL VOID Initialize_Listen_Ports( VOID )
414 {
415         /* Ports, auf denen der Server Verbindungen entgegennehmen
416          * soll, initialisieren */
417         
418         UINT created, i;
419
420         created = 0;
421         for( i = 0; i < Conf_ListenPorts_Count; i++ )
422         {
423                 if( Conn_NewListener( Conf_ListenPorts[i] )) created++;
424                 else Log( LOG_ERR, "Can't listen on port %u!", Conf_ListenPorts[i] );
425         }
426
427         if( created < 1 )
428         {
429                 Log( LOG_ALERT, "Server isn't listening on a single port!" );
430                 Log( LOG_ALERT, PACKAGE" exiting due to fatal errors!" );
431                 exit( 1 );
432         }
433 } /* Initialize_Listen_Ports */
434
435
436 LOCAL VOID Show_Version( VOID )
437 {
438         puts( NGIRCd_Version( ));
439         puts( "Copyright (c)2001,2002 by Alexander Barton (<alex@barton.de>)." );
440         puts( "Homepage: <http://arthur.ath.cx/~alex/ngircd/>\n" );
441         puts( "This is free software; see the source for copying conditions. There is NO" );
442         puts( "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE." );
443 } /* Show_Version */
444
445
446 LOCAL VOID Show_Help( VOID )
447 {
448 #ifdef DEBUG
449         puts( "  -d, --debug        log extra debug messages" );
450 #endif
451         puts( "  -f, --config <f>   use file <f> as configuration file" );
452         puts( "  -n, --nodaemon     don't fork and don't detatch from controlling terminal" );
453         puts( "  -p, --passive      disable automatic connections to other servers" );
454 #ifdef SNIFFER
455         puts( "  -s, --sniffer      enable network sniffer and display all IRC traffic" );
456 #endif
457         puts( "      --configtest   read, validate and display configuration; then exit" );
458         puts( "      --version      output version information and exit" );
459         puts( "      --help         display this help and exit" );
460 } /* Show_Help */
461
462
463 /* -eof- */