]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/ngircd.c
- die "umask" des Servers wird nun auf 077 gesetzt;
[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.48 2002/06/02 17:01:21 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.", (INT32)getuid( ), (INT32)getgid( ));
279
280                 Log_InitErrorfile( );
281
282                 /* Signal-Handler initialisieren */
283                 Initialize_Signal_Handler( );
284
285                 /* Listen-Ports initialisieren */
286                 Initialize_Listen_Ports( );
287
288                 /* Hauptschleife */
289                 Conn_Handler( );
290
291                 /* Alles abmelden */
292                 Conn_Exit( );
293                 Client_Exit( );
294                 Channel_Exit( );
295                 Lists_Exit( );
296                 Log_Exit( );
297         }
298
299         return 0;
300 } /* main */
301
302
303 GLOBAL CHAR *
304 NGIRCd_Version( VOID )
305 {
306         STATIC CHAR version[126];
307
308         sprintf( version, "%s version %s-%s", PACKAGE, VERSION, NGIRCd_VersionAddition( ));
309         return version;
310 } /* NGIRCd_Version */
311
312
313 GLOBAL CHAR *
314 NGIRCd_VersionAddition( VOID )
315 {
316         STATIC CHAR txt[64];
317
318         strcpy( txt, "" );
319
320 #ifdef USE_SYSLOG
321         if( txt[0] ) strcat( txt, "+" );
322         strcat( txt, "SYSLOG" );
323 #endif
324 #ifdef REGEX
325         if( txt[0] ) strcat( txt, "+" );
326         strcat( txt, "REGEX" );
327 #endif
328 #ifdef STRICT_RFC
329         if( txt[0] ) strcat( txt, "+" );
330         strcat( txt, "RFC" );
331 #endif
332 #ifdef DEBUG
333         if( txt[0] ) strcat( txt, "+" );
334         strcat( txt, "DEBUG" );
335 #endif
336 #ifdef SNIFFER
337         if( txt[0] ) strcat( txt, "+" );
338         strcat( txt, "SNIFFER" );
339 #endif
340
341         if( txt[0] ) strcat( txt, "-" );
342 #ifdef PROTOTYPES
343         strcat( txt, TARGET_CPU"/"TARGET_VENDOR"/"TARGET_OS );
344 #else
345         strcat( txt, TARGET_CPU );
346         strcat( txt, "/" );
347         strcat( txt, TARGET_VENDOR );
348         strcat( txt, "/" );
349         strcat( txt, TARGET_OS );
350 #endif  
351
352         return txt;
353 } /* NGIRCd_VersionAddition */
354
355
356 LOCAL VOID
357 Initialize_Signal_Handler( VOID )
358 {
359         /* Signal-Handler initialisieren: einige Signale
360          * werden ignoriert, andere speziell behandelt. */
361
362 #ifdef HAVE_SIGACTION
363         /* sigaction() ist vorhanden */
364
365         struct sigaction saction;
366
367         /* Signal-Struktur initialisieren */
368         memset( &saction, 0, sizeof( saction ));
369         saction.sa_handler = Signal_Handler;
370 #ifdef SA_RESTART
371         saction.sa_flags |= SA_RESTART;
372 #endif
373 #ifdef SA_NOCLDWAIT
374         saction.sa_flags |= SA_NOCLDWAIT;
375 #endif
376
377         /* Signal-Handler einhaengen */
378         sigaction( SIGINT, &saction, NULL );
379         sigaction( SIGQUIT, &saction, NULL );
380         sigaction( SIGTERM, &saction, NULL);
381         sigaction( SIGHUP, &saction, NULL);
382         sigaction( SIGCHLD, &saction, NULL);
383
384         /* einige Signale ignorieren */
385         saction.sa_handler = SIG_IGN;
386         sigaction( SIGPIPE, &saction, NULL );
387 #else
388         /* kein sigaction() vorhanden */
389
390         /* Signal-Handler einhaengen */
391         signal( SIGINT, Signal_Handler );
392         signal( SIGQUIT, Signal_Handler );
393         signal( SIGTERM, Signal_Handler );
394         signal( SIGHUP, Signal_Handler );
395         signal( SIGCHLD, Signal_Handler );
396
397         /* einige Signale ignorieren */
398         signal( SIGPIPE, SIG_IGN );
399 #endif
400 } /* Initialize_Signal_Handler */
401
402
403 LOCAL VOID
404 Signal_Handler( INT Signal )
405 {
406         /* Signal-Handler. Dieser wird aufgerufen, wenn eines der Signale eintrifft,
407          * fuer das wir uns registriert haben (vgl. Initialize_Signal_Handler). Die
408          * Nummer des eingetroffenen Signals wird der Funktion uebergeben. */
409
410         switch( Signal )
411         {
412                 case SIGTERM:
413                 case SIGINT:
414                 case SIGQUIT:
415                         /* wir soll(t)en uns wohl beenden ... */
416                         if( Signal == SIGTERM ) Log( LOG_WARNING, "Got TERM signal, terminating now ..." );
417                         else if( Signal == SIGINT ) Log( LOG_WARNING, "Got INT signal, terminating now ..." );
418                         else if( Signal == SIGQUIT ) Log( LOG_WARNING, "Got QUIT signal, terminating now ..." );
419                         NGIRCd_Quit = TRUE;
420                         break;
421                 case SIGHUP:
422                         /* neu starten */
423                         Log( LOG_WARNING, "Got HUP signal, restarting now ..." );
424                         NGIRCd_Restart = TRUE;
425                         break;
426                 case SIGCHLD:
427                         /* Child-Prozess wurde beendet. Zombies vermeiden: */
428                         while( waitpid( -1, NULL, WNOHANG ) > 0);
429                         break;
430                 default:
431                         /* unbekanntes bzw. unbehandeltes Signal */
432                         Log( LOG_NOTICE, "Got signal %d! Ignored.", Signal );
433         }
434 } /* Signal_Handler */
435
436
437 LOCAL VOID
438 Initialize_Listen_Ports( VOID )
439 {
440         /* Ports, auf denen der Server Verbindungen entgegennehmen
441          * soll, initialisieren */
442         
443         UINT created, i;
444
445         created = 0;
446         for( i = 0; i < Conf_ListenPorts_Count; i++ )
447         {
448                 if( Conn_NewListener( Conf_ListenPorts[i] )) created++;
449                 else Log( LOG_ERR, "Can't listen on port %u!", Conf_ListenPorts[i] );
450         }
451
452         if( created < 1 )
453         {
454                 Log( LOG_ALERT, "Server isn't listening on a single port!" );
455                 Log( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE );
456                 exit( 1 );
457         }
458 } /* Initialize_Listen_Ports */
459
460
461 LOCAL VOID
462 Show_Version( VOID )
463 {
464         puts( NGIRCd_Version( ));
465         puts( "Copyright (c)2001,2002 by Alexander Barton (<alex@barton.de>)." );
466         puts( "Homepage: <http://arthur.ath.cx/~alex/ngircd/>\n" );
467         puts( "This is free software; see the source for copying conditions. There is NO" );
468         puts( "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE." );
469 } /* Show_Version */
470
471
472 LOCAL VOID
473 Show_Help( VOID )
474 {
475 #ifdef DEBUG
476         puts( "  -d, --debug        log extra debug messages" );
477 #endif
478         puts( "  -f, --config <f>   use file <f> as configuration file" );
479         puts( "  -n, --nodaemon     don't fork and don't detatch from controlling terminal" );
480         puts( "  -p, --passive      disable automatic connections to other servers" );
481 #ifdef SNIFFER
482         puts( "  -s, --sniffer      enable network sniffer and display all IRC traffic" );
483 #endif
484         puts( "      --configtest   read, validate and display configuration; then exit" );
485         puts( "      --version      output version information and exit" );
486         puts( "      --help         display this help and exit" );
487 } /* Show_Help */
488
489
490 /* -eof- */