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