- NGIRCd_Reload() in NGIRCd_Rehash() umbenannt.
[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.61 2002/11/22 23:31:23 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                 if( Conf_OperCanMode ) strcat( NGIRCd_ProtoID, "o" );
298 #else
299                 sprintf( NGIRCd_ProtoID, "%s%s %s|%s", PROTOVER, PROTOIRC, PACKAGE, VERSION );
300 #endif
301                 strcat( NGIRCd_ProtoID, " P" );
302                 Log( LOG_DEBUG, "Protocol and server ID is \"%s\".", NGIRCd_ProtoID );
303
304                 /* Vordefinierte Channels anlegen */
305                 Channel_InitPredefined( );
306
307                 /* Listen-Ports initialisieren */
308                 if( Conn_InitListeners( ) < 1 )
309                 {
310                         Log( LOG_ALERT, "Server isn't listening on a single port!" );
311                         Log( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE );
312                         exit( 1 );
313                 }
314                 
315                 /* Hauptschleife */
316                 Conn_Handler( );
317
318                 /* Alles abmelden */
319                 Conn_Exit( );
320                 Client_Exit( );
321                 Channel_Exit( );
322                 Lists_Exit( );
323                 Log_Exit( );
324         }
325
326         return 0;
327 } /* main */
328
329
330 GLOBAL CHAR *
331 NGIRCd_Version( VOID )
332 {
333         STATIC CHAR version[126];
334         
335 #ifdef CVSDATE
336         sprintf( version, "%s %s(%s)-%s", PACKAGE, VERSION, CVSDATE, NGIRCd_VersionAddition( ));
337 #else
338         sprintf( version, "%s %s-%s", PACKAGE, VERSION, NGIRCd_VersionAddition( ));
339 #endif
340         return version;
341 } /* NGIRCd_Version */
342
343
344 GLOBAL CHAR *
345 NGIRCd_VersionAddition( VOID )
346 {
347         STATIC CHAR txt[64];
348
349         strcpy( txt, "" );
350
351 #ifdef USE_SYSLOG
352         if( txt[0] ) strcat( txt, "+" );
353         strcat( txt, "SYSLOG" );
354 #endif
355 #ifdef DEBUG
356         if( txt[0] ) strcat( txt, "+" );
357         strcat( txt, "DEBUG" );
358 #endif
359 #ifdef SNIFFER
360         if( txt[0] ) strcat( txt, "+" );
361         strcat( txt, "SNIFFER" );
362 #endif
363 #ifdef STRICT_RFC
364         if( txt[0] ) strcat( txt, "+" );
365         strcat( txt, "RFC" );
366 #endif
367 #ifdef IRCPLUS
368         if( txt[0] ) strcat( txt, "+" );
369         strcat( txt, "IRCPLUS" );
370 #endif
371         
372         if( txt[0] ) strcat( txt, "-" );
373         strcat( txt, TARGET_CPU );
374         strcat( txt, "/" );
375         strcat( txt, TARGET_VENDOR );
376         strcat( txt, "/" );
377         strcat( txt, TARGET_OS );
378
379         return txt;
380 } /* NGIRCd_VersionAddition */
381
382
383 GLOBAL VOID
384 NGIRCd_Rehash( VOID )
385 {
386         CHAR old_name[CLIENT_ID_LEN];
387         
388         /* Alle Listen-Sockets schliessen */
389         Conn_ExitListeners( );
390
391         /* Alten Server-Namen merken */
392         strcpy( old_name, Conf_ServerName );
393
394         /* Konfiguration neu lesen ... */
395         Conf_Init( );
396
397         /* Alten Server-Namen wiederherstellen: dieser
398          * kann nicht zur Laufzeit geaendert werden ... */
399         if( strcmp( old_name, Conf_ServerName ) != 0 )
400         {
401                 strcpy( Conf_ServerName, old_name );
402                 Log( LOG_ERR, "Can't change \"ServerName\" on runtime! Ignored new name." );
403         }
404
405         /* neue pre-defined Channel anlegen: */
406         Channel_InitPredefined( );
407         
408         /* Listen-Sockets neu anlegen: */
409         Conn_InitListeners( );
410
411         Log( LOG_INFO, "Re-reading of configuration done." );
412 } /* NGIRCd_Rehash */
413
414
415 LOCAL VOID
416 Initialize_Signal_Handler( VOID )
417 {
418         /* Signal-Handler initialisieren: einige Signale
419          * werden ignoriert, andere speziell behandelt. */
420
421 #ifdef HAVE_SIGACTION
422         /* sigaction() ist vorhanden */
423
424         struct sigaction saction;
425
426         /* Signal-Struktur initialisieren */
427         memset( &saction, 0, sizeof( saction ));
428         saction.sa_handler = Signal_Handler;
429 #ifdef SA_RESTART
430         saction.sa_flags |= SA_RESTART;
431 #endif
432 #ifdef SA_NOCLDWAIT
433         saction.sa_flags |= SA_NOCLDWAIT;
434 #endif
435
436         /* Signal-Handler einhaengen */
437         sigaction( SIGINT, &saction, NULL );
438         sigaction( SIGQUIT, &saction, NULL );
439         sigaction( SIGTERM, &saction, NULL);
440         sigaction( SIGHUP, &saction, NULL);
441         sigaction( SIGCHLD, &saction, NULL);
442
443         /* einige Signale ignorieren */
444         saction.sa_handler = SIG_IGN;
445         sigaction( SIGPIPE, &saction, NULL );
446 #else
447         /* kein sigaction() vorhanden */
448
449         /* Signal-Handler einhaengen */
450         signal( SIGINT, Signal_Handler );
451         signal( SIGQUIT, Signal_Handler );
452         signal( SIGTERM, Signal_Handler );
453         signal( SIGHUP, Signal_Handler );
454         signal( SIGCHLD, Signal_Handler );
455
456         /* einige Signale ignorieren */
457         signal( SIGPIPE, SIG_IGN );
458 #endif
459 } /* Initialize_Signal_Handler */
460
461
462 LOCAL VOID
463 Signal_Handler( INT Signal )
464 {
465         /* Signal-Handler. Dieser wird aufgerufen, wenn eines der Signale eintrifft,
466          * fuer das wir uns registriert haben (vgl. Initialize_Signal_Handler). Die
467          * Nummer des eingetroffenen Signals wird der Funktion uebergeben. */
468
469         switch( Signal )
470         {
471                 case SIGTERM:
472                 case SIGINT:
473                 case SIGQUIT:
474                         /* wir soll(t)en uns wohl beenden ... */
475                         if( Signal == SIGTERM ) Log( LOG_WARNING|LOG_snotice, "Got TERM signal, terminating now ..." );
476                         else if( Signal == SIGINT ) Log( LOG_WARNING|LOG_snotice, "Got INT signal, terminating now ..." );
477                         else if( Signal == SIGQUIT ) Log( LOG_WARNING|LOG_snotice, "Got QUIT signal, terminating now ..." );
478                         NGIRCd_Quit = TRUE;
479                         break;
480                 case SIGHUP:
481                         /* Konfiguration neu einlesen: */
482                         Log( LOG_WARNING|LOG_snotice, "Got HUP signal, re-reading configuration ..." );
483                         NGIRCd_Rehash( );
484                         break;
485                 case SIGCHLD:
486                         /* Child-Prozess wurde beendet. Zombies vermeiden: */
487                         while( waitpid( -1, NULL, WNOHANG ) > 0);
488                         break;
489                 default:
490                         /* unbekanntes bzw. unbehandeltes Signal */
491                         Log( LOG_NOTICE, "Got signal %d! Ignored.", Signal );
492         }
493 } /* Signal_Handler */
494
495
496 LOCAL VOID
497 Show_Version( VOID )
498 {
499         puts( NGIRCd_Version( ));
500         puts( "Copyright (c)2001,2002 by Alexander Barton (<alex@barton.de>)." );
501         puts( "Homepage: <http://arthur.ath.cx/~alex/ngircd/>\n" );
502         puts( "This is free software; see the source for copying conditions. There is NO" );
503         puts( "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE." );
504 } /* Show_Version */
505
506
507 LOCAL VOID
508 Show_Help( VOID )
509 {
510 #ifdef DEBUG
511         puts( "  -d, --debug        log extra debug messages" );
512 #endif
513         puts( "  -f, --config <f>   use file <f> as configuration file" );
514         puts( "  -n, --nodaemon     don't fork and don't detach from controlling terminal" );
515         puts( "  -p, --passive      disable automatic connections to other servers" );
516 #ifdef SNIFFER
517         puts( "  -s, --sniffer      enable network sniffer and display all IRC traffic" );
518 #endif
519         puts( "      --configtest   read, validate and display configuration; then exit" );
520         puts( "      --version      output version information and exit" );
521         puts( "      --help         display this help and exit" );
522 } /* Show_Help */
523
524
525 /* -eof- */