Synchronize server connections with server configurations after reloading the configu...
[ngircd-alex.git] / src / ngircd / ngircd.c
1 /*
2  * ngIRCd -- The Next Generation IRC Daemon
3  * Copyright (c)2001-2003 by Alexander Barton (alex@barton.de)
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * Please read the file COPYING, README and AUTHORS for more information.
10  *
11  * Main program -- main()
12  */
13
14
15 #include "portab.h"
16
17 static char UNUSED id[] = "$Id: ngircd.c,v 1.75 2003/03/27 01:22:44 alex Exp $";
18
19 #include "imp.h"
20 #include <assert.h>
21 #include <errno.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <signal.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <sys/wait.h>
30 #include <time.h>
31 #include <pwd.h>
32 #include <grp.h>
33
34 #include "resolve.h"
35 #include "conn.h"
36 #include "client.h"
37 #include "channel.h"
38 #include "conf.h"
39 #include "cvs-version.h"
40 #include "defines.h"
41 #include "lists.h"
42 #include "log.h"
43 #include "parse.h"
44 #include "irc.h"
45
46 #ifdef RENDEZVOUS
47 #include "rendezvous.h"
48 #endif
49
50 #include "exp.h"
51 #include "ngircd.h"
52
53
54 LOCAL VOID Initialize_Signal_Handler PARAMS(( VOID ));
55 LOCAL VOID Signal_Handler PARAMS(( INT Signal ));
56
57 LOCAL VOID Show_Version PARAMS(( VOID ));
58 LOCAL VOID Show_Help PARAMS(( VOID ));
59
60
61 GLOBAL int
62 main( int argc, const char *argv[] )
63 {
64         struct passwd *pwd;
65         struct group *grp;
66         BOOLEAN ok, configtest = FALSE;
67         LONG pid, n;
68         INT i;
69
70         umask( 0077 );
71
72         NGIRCd_SignalQuit = NGIRCd_SignalRestart = NGIRCd_SignalRehash = FALSE;
73         NGIRCd_NoDaemon = NGIRCd_Passive = FALSE;
74 #ifdef DEBUG
75         NGIRCd_Debug = FALSE;
76 #endif
77 #ifdef SNIFFER
78         NGIRCd_Sniffer = FALSE;
79 #endif
80         strlcpy( NGIRCd_ConfFile, SYSCONFDIR, sizeof( NGIRCd_ConfFile ));
81         strlcat( NGIRCd_ConfFile, CONFIG_FILE, sizeof( NGIRCd_ConfFile ));
82
83         /* Kommandozeile parsen */
84         for( i = 1; i < argc; i++ )
85         {
86                 ok = FALSE;
87                 if(( argv[i][0] == '-' ) && ( argv[i][1] == '-' ))
88                 {
89                         /* Lange Option */
90
91                         if( strcmp( argv[i], "--config" ) == 0 )
92                         {
93                                 if( i + 1 < argc )
94                                 {
95                                         /* Ok, there's an parameter left */
96                                         strlcpy( NGIRCd_ConfFile, argv[i + 1], sizeof( NGIRCd_ConfFile ));
97
98                                         /* next parameter */
99                                         i++; ok = TRUE;
100                                 }
101                         }
102                         if( strcmp( argv[i], "--configtest" ) == 0 )
103                         {
104                                 configtest = TRUE;
105                                 ok = TRUE;
106                         }
107 #ifdef DEBUG
108                         if( strcmp( argv[i], "--debug" ) == 0 )
109                         {
110                                 NGIRCd_Debug = TRUE;
111                                 ok = TRUE;
112                         }
113 #endif
114                         if( strcmp( argv[i], "--help" ) == 0 )
115                         {
116                                 Show_Version( );
117                                 puts( "" ); Show_Help( ); puts( "" );
118                                 exit( 1 );
119                         }
120                         if( strcmp( argv[i], "--nodaemon" ) == 0 )
121                         {
122                                 NGIRCd_NoDaemon = TRUE;
123                                 ok = TRUE;
124                         }
125                         if( strcmp( argv[i], "--passive" ) == 0 )
126                         {
127                                 NGIRCd_Passive = TRUE;
128                                 ok = TRUE;
129                         }
130 #ifdef SNIFFER
131                         if( strcmp( argv[i], "--sniffer" ) == 0 )
132                         {
133                                 NGIRCd_Sniffer = TRUE;
134                                 ok = TRUE;
135                         }
136 #endif
137                         if( strcmp( argv[i], "--version" ) == 0 )
138                         {
139                                 Show_Version( );
140                                 exit( 1 );
141                         }
142                 }
143                 else if(( argv[i][0] == '-' ) && ( argv[i][1] != '-' ))
144                 {
145                         /* Kurze Option */
146                         
147                         for( n = 1; n < (LONG)strlen( argv[i] ); n++ )
148                         {
149                                 ok = FALSE;
150 #ifdef DEBUG
151                                 if( argv[i][n] == 'd' )
152                                 {
153                                         NGIRCd_Debug = TRUE;
154                                         ok = TRUE;
155                                 }
156 #endif
157                                 if( argv[i][n] == 'f' )
158                                 {
159                                         if(( ! argv[i][n + 1] ) && ( i + 1 < argc ))
160                                         {
161                                                 /* Ok, next character is a blank */
162                                                 strlcpy( NGIRCd_ConfFile, argv[i + 1], sizeof( NGIRCd_ConfFile ));
163
164                                                 /* go to the following 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_SignalQuit )
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_SignalRehash = FALSE;
255                 NGIRCd_SignalRestart = FALSE;
256                 NGIRCd_SignalQuit = FALSE;
257
258                 /* Module initialisieren */
259                 Log_Init( );
260                 Resolve_Init( );
261                 Conf_Init( );
262                 Lists_Init( );
263                 Channel_Init( );
264                 Client_Init( );
265 #ifdef RENDEZVOUS
266                 Rendezvous_Init( );
267 #endif
268                 Conn_Init( );
269
270                 /* Wenn als root ausgefuehrt und eine andere UID
271                  * konfiguriert ist, jetzt zu dieser wechseln */
272                 if( getuid( ) == 0 )
273                 {
274                         if( Conf_GID != 0 )
275                         {
276                                 /* Neue Group-ID setzen */
277                                 if( setgid( Conf_GID ) != 0 ) Log( LOG_ERR, "Can't change Group-ID to %u: %s", Conf_GID, strerror( errno ));
278                         }
279                         if( Conf_UID != 0 )
280                         {
281                                 /* Neue User-ID setzen */
282                                 if( setuid( Conf_UID ) != 0 ) Log( LOG_ERR, "Can't change User-ID to %u: %s", Conf_UID, strerror( errno ));
283                         }
284                 }
285                 
286                 /* User, Gruppe und Prozess-ID des Daemon ausgeben */
287                 pwd = getpwuid( getuid( )); grp = getgrgid( getgid( ));
288                 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( ));
289
290                 /* stderr in "Error-File" umlenken */
291                 Log_InitErrorfile( );
292
293                 /* Signal-Handler initialisieren */
294                 Initialize_Signal_Handler( );
295
296                 /* Protokoll- und Server-Identifikation erzeugen. Die vom ngIRCd
297                  * beim PASS-Befehl verwendete Syntax sowie die erweiterten Flags
298                  * sind in doc/Protocol.txt beschrieben. */
299 #ifdef IRCPLUS
300                 sprintf( NGIRCd_ProtoID, "%s%s %s|%s:%s", PROTOVER, PROTOIRCPLUS, PACKAGE, VERSION, IRCPLUSFLAGS );
301 #ifdef USE_ZLIB
302                 strcat( NGIRCd_ProtoID, "Z" );
303 #endif
304                 if( Conf_OperCanMode ) strcat( NGIRCd_ProtoID, "o" );
305 #else
306                 sprintf( NGIRCd_ProtoID, "%s%s %s|%s", PROTOVER, PROTOIRC, PACKAGE, VERSION );
307 #endif
308                 strcat( NGIRCd_ProtoID, " P" );
309 #ifdef USE_ZLIB
310                 strcat( NGIRCd_ProtoID, "Z" );
311 #endif
312                 Log( LOG_DEBUG, "Protocol and server ID is \"%s\".", NGIRCd_ProtoID );
313
314                 /* Vordefinierte Channels anlegen */
315                 Channel_InitPredefined( );
316
317                 /* Listen-Ports initialisieren */
318                 if( Conn_InitListeners( ) < 1 )
319                 {
320                         Log( LOG_ALERT, "Server isn't listening on a single port!" );
321                         Log( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE );
322                         exit( 1 );
323                 }
324                 
325                 /* Hauptschleife */
326                 Conn_Handler( );
327
328                 /* Alles abmelden */
329                 Conn_Exit( );
330 #ifdef RENDEZVOUS
331                 Rendezvous_Exit( );
332 #endif
333                 Client_Exit( );
334                 Channel_Exit( );
335                 Lists_Exit( );
336                 Log_Exit( );
337         }
338
339         return 0;
340 } /* main */
341
342
343 GLOBAL CHAR *
344 NGIRCd_Version( VOID )
345 {
346         STATIC CHAR version[126];
347         
348 #ifdef CVSDATE
349         sprintf( version, "%s %s(%s)-%s", PACKAGE, VERSION, CVSDATE, NGIRCd_VersionAddition( ));
350 #else
351         sprintf( version, "%s %s-%s", PACKAGE, VERSION, NGIRCd_VersionAddition( ));
352 #endif
353         return version;
354 } /* NGIRCd_Version */
355
356
357 GLOBAL CHAR *
358 NGIRCd_VersionAddition( VOID )
359 {
360         STATIC CHAR txt[200];
361
362         strcpy( txt, "" );
363
364 #ifdef USE_SYSLOG
365         if( txt[0] ) strcat( txt, "+" );
366         strcat( txt, "SYSLOG" );
367 #endif
368 #ifdef USE_ZLIB
369         if( txt[0] ) strcat( txt, "+" );
370         strcat( txt, "ZLIB" );
371 #endif
372 #ifdef USE_TCPWRAP
373         if( txt[0] ) strcat( txt, "+" );
374         strcat( txt, "TCPWRAP" );
375 #endif
376 #ifdef RENDEZVOUS
377         if( txt[0] ) strcat( txt, "+" );
378         strcat( txt, "RENDEZVOUS" );
379 #endif
380 #ifdef DEBUG
381         if( txt[0] ) strcat( txt, "+" );
382         strcat( txt, "DEBUG" );
383 #endif
384 #ifdef SNIFFER
385         if( txt[0] ) strcat( txt, "+" );
386         strcat( txt, "SNIFFER" );
387 #endif
388 #ifdef STRICT_RFC
389         if( txt[0] ) strcat( txt, "+" );
390         strcat( txt, "RFC" );
391 #endif
392 #ifdef IRCPLUS
393         if( txt[0] ) strcat( txt, "+" );
394         strcat( txt, "IRCPLUS" );
395 #endif
396         
397         if( txt[0] ) strlcat( txt, "-", sizeof( txt ));
398         strlcat( txt, TARGET_CPU, sizeof( txt ));
399         strlcat( txt, "/", sizeof( txt ));
400         strlcat( txt, TARGET_VENDOR, sizeof( txt ));
401         strlcat( txt, "/", sizeof( txt ));
402         strlcat( txt, TARGET_OS, sizeof( txt ));
403
404         return txt;
405 } /* NGIRCd_VersionAddition */
406
407
408 GLOBAL VOID
409 NGIRCd_Rehash( VOID )
410 {
411         CHAR old_name[CLIENT_ID_LEN];
412
413         Log( LOG_NOTICE|LOG_snotice, "Re-reading configuration NOW!" );
414         NGIRCd_SignalRehash = FALSE;
415
416         /* Alle Listen-Sockets schliessen */
417         Conn_ExitListeners( );
418
419         /* Alten Server-Namen merken */
420         assert( sizeof( old_name ) == sizeof( Conf_ServerName ));
421         strcpy( old_name, Conf_ServerName );
422
423         /* Konfiguration neu lesen ... */
424         Conf_Rehash( );
425
426         /* Alten Server-Namen wiederherstellen: dieser
427          * kann nicht zur Laufzeit geaendert werden ... */
428         if( strcmp( old_name, Conf_ServerName ) != 0 )
429         {
430                 strcpy( Conf_ServerName, old_name );
431                 Log( LOG_ERR, "Can't change \"ServerName\" on runtime! Ignored new name." );
432         }
433
434         /* neue pre-defined Channel anlegen: */
435         Channel_InitPredefined( );
436         
437         /* Listen-Sockets neu anlegen: */
438         Conn_InitListeners( );
439
440         /* Sync configuration with established connections */
441         Conn_SyncServerStruct( );
442
443         Log( LOG_NOTICE|LOG_snotice, "Re-reading of configuration done." );
444 } /* NGIRCd_Rehash */
445
446
447 LOCAL VOID
448 Initialize_Signal_Handler( VOID )
449 {
450         /* Signal-Handler initialisieren: einige Signale
451          * werden ignoriert, andere speziell behandelt. */
452
453 #ifdef HAVE_SIGACTION
454         /* sigaction() ist vorhanden */
455
456         struct sigaction saction;
457
458         /* Signal-Struktur initialisieren */
459         memset( &saction, 0, sizeof( saction ));
460         saction.sa_handler = Signal_Handler;
461 #ifdef SA_RESTART
462         saction.sa_flags |= SA_RESTART;
463 #endif
464 #ifdef SA_NOCLDWAIT
465         saction.sa_flags |= SA_NOCLDWAIT;
466 #endif
467
468         /* Signal-Handler einhaengen */
469         sigaction( SIGINT, &saction, NULL );
470         sigaction( SIGQUIT, &saction, NULL );
471         sigaction( SIGTERM, &saction, NULL);
472         sigaction( SIGHUP, &saction, NULL);
473         sigaction( SIGCHLD, &saction, NULL);
474
475         /* einige Signale ignorieren */
476         saction.sa_handler = SIG_IGN;
477         sigaction( SIGPIPE, &saction, NULL );
478 #else
479         /* kein sigaction() vorhanden */
480
481         /* Signal-Handler einhaengen */
482         signal( SIGINT, Signal_Handler );
483         signal( SIGQUIT, Signal_Handler );
484         signal( SIGTERM, Signal_Handler );
485         signal( SIGHUP, Signal_Handler );
486         signal( SIGCHLD, Signal_Handler );
487
488         /* einige Signale ignorieren */
489         signal( SIGPIPE, SIG_IGN );
490 #endif
491 } /* Initialize_Signal_Handler */
492
493
494 LOCAL VOID
495 Signal_Handler( INT Signal )
496 {
497         /* Signal-Handler. Dieser wird aufgerufen, wenn eines der Signale eintrifft,
498          * fuer das wir uns registriert haben (vgl. Initialize_Signal_Handler). Die
499          * Nummer des eingetroffenen Signals wird der Funktion uebergeben. */
500
501         switch( Signal )
502         {
503                 case SIGTERM:
504                 case SIGINT:
505                 case SIGQUIT:
506                         /* wir soll(t)en uns wohl beenden ... */
507                         NGIRCd_SignalQuit = TRUE;
508                         break;
509                 case SIGHUP:
510                         /* Konfiguration neu einlesen: */
511                         NGIRCd_SignalRehash = TRUE;
512                         break;
513                 case SIGCHLD:
514                         /* Child-Prozess wurde beendet. Zombies vermeiden: */
515                         while( waitpid( -1, NULL, WNOHANG ) > 0);
516                         break;
517 #ifdef DEBUG
518                 default:
519                         /* unbekanntes bzw. unbehandeltes Signal */
520                         Log( LOG_DEBUG, "Got signal %d! Ignored.", Signal );
521 #endif
522         }
523 } /* Signal_Handler */
524
525
526 LOCAL VOID
527 Show_Version( VOID )
528 {
529         puts( NGIRCd_Version( ));
530         puts( "Copyright (c)2001-2003 by Alexander Barton (<alex@barton.de>)." );
531         puts( "Homepage: <http://arthur.ath.cx/~alex/ngircd/>\n" );
532         puts( "This is free software; see the source for copying conditions. There is NO" );
533         puts( "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE." );
534 } /* Show_Version */
535
536
537 LOCAL VOID
538 Show_Help( VOID )
539 {
540 #ifdef DEBUG
541         puts( "  -d, --debug        log extra debug messages" );
542 #endif
543         puts( "  -f, --config <f>   use file <f> as configuration file" );
544         puts( "  -n, --nodaemon     don't fork and don't detach from controlling terminal" );
545         puts( "  -p, --passive      disable automatic connections to other servers" );
546 #ifdef SNIFFER
547         puts( "  -s, --sniffer      enable network sniffer and display all IRC traffic" );
548 #endif
549         puts( "      --configtest   read, validate and display configuration; then exit" );
550         puts( "      --version      output version information and exit" );
551         puts( "      --help         display this help and exit" );
552 } /* Show_Help */
553
554
555 /* -eof- */