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