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