]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/ngircd.c
- Added optional support for Rendezvous.
[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.72 2003/02/23 12:04:05 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[64];
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 DEBUG
373         if( txt[0] ) strcat( txt, "+" );
374         strcat( txt, "DEBUG" );
375 #endif
376 #ifdef SNIFFER
377         if( txt[0] ) strcat( txt, "+" );
378         strcat( txt, "SNIFFER" );
379 #endif
380 #ifdef STRICT_RFC
381         if( txt[0] ) strcat( txt, "+" );
382         strcat( txt, "RFC" );
383 #endif
384 #ifdef IRCPLUS
385         if( txt[0] ) strcat( txt, "+" );
386         strcat( txt, "IRCPLUS" );
387 #endif
388 #ifdef RENDEZVOUS
389         if( txt[0] ) strcat( txt, "+" );
390         strcat( txt, "RENDEZVOUS" );
391 #endif
392         
393         if( txt[0] ) strlcat( txt, "-", sizeof( txt ));
394         strlcat( txt, TARGET_CPU, sizeof( txt ));
395         strlcat( txt, "/", sizeof( txt ));
396         strlcat( txt, TARGET_VENDOR, sizeof( txt ));
397         strlcat( txt, "/", sizeof( txt ));
398         strlcat( txt, TARGET_OS, sizeof( txt ));
399
400         return txt;
401 } /* NGIRCd_VersionAddition */
402
403
404 GLOBAL VOID
405 NGIRCd_Rehash( VOID )
406 {
407         CHAR old_name[CLIENT_ID_LEN];
408
409         Log( LOG_NOTICE|LOG_snotice, "Re-reading configuration NOW!" );
410         NGIRCd_SignalRehash = FALSE;
411
412         /* Alle Listen-Sockets schliessen */
413         Conn_ExitListeners( );
414
415         /* Alten Server-Namen merken */
416         assert( sizeof( old_name ) == sizeof( Conf_ServerName ));
417         strcpy( old_name, Conf_ServerName );
418
419         /* Konfiguration neu lesen ... */
420         Conf_Rehash( );
421
422         /* Alten Server-Namen wiederherstellen: dieser
423          * kann nicht zur Laufzeit geaendert werden ... */
424         if( strcmp( old_name, Conf_ServerName ) != 0 )
425         {
426                 strcpy( Conf_ServerName, old_name );
427                 Log( LOG_ERR, "Can't change \"ServerName\" on runtime! Ignored new name." );
428         }
429
430         /* neue pre-defined Channel anlegen: */
431         Channel_InitPredefined( );
432         
433         /* Listen-Sockets neu anlegen: */
434         Conn_InitListeners( );
435
436         Log( LOG_NOTICE|LOG_snotice, "Re-reading of configuration done." );
437 } /* NGIRCd_Rehash */
438
439
440 LOCAL VOID
441 Initialize_Signal_Handler( VOID )
442 {
443         /* Signal-Handler initialisieren: einige Signale
444          * werden ignoriert, andere speziell behandelt. */
445
446 #ifdef HAVE_SIGACTION
447         /* sigaction() ist vorhanden */
448
449         struct sigaction saction;
450
451         /* Signal-Struktur initialisieren */
452         memset( &saction, 0, sizeof( saction ));
453         saction.sa_handler = Signal_Handler;
454 #ifdef SA_RESTART
455         saction.sa_flags |= SA_RESTART;
456 #endif
457 #ifdef SA_NOCLDWAIT
458         saction.sa_flags |= SA_NOCLDWAIT;
459 #endif
460
461         /* Signal-Handler einhaengen */
462         sigaction( SIGINT, &saction, NULL );
463         sigaction( SIGQUIT, &saction, NULL );
464         sigaction( SIGTERM, &saction, NULL);
465         sigaction( SIGHUP, &saction, NULL);
466         sigaction( SIGCHLD, &saction, NULL);
467
468         /* einige Signale ignorieren */
469         saction.sa_handler = SIG_IGN;
470         sigaction( SIGPIPE, &saction, NULL );
471 #else
472         /* kein sigaction() vorhanden */
473
474         /* Signal-Handler einhaengen */
475         signal( SIGINT, Signal_Handler );
476         signal( SIGQUIT, Signal_Handler );
477         signal( SIGTERM, Signal_Handler );
478         signal( SIGHUP, Signal_Handler );
479         signal( SIGCHLD, Signal_Handler );
480
481         /* einige Signale ignorieren */
482         signal( SIGPIPE, SIG_IGN );
483 #endif
484 } /* Initialize_Signal_Handler */
485
486
487 LOCAL VOID
488 Signal_Handler( INT Signal )
489 {
490         /* Signal-Handler. Dieser wird aufgerufen, wenn eines der Signale eintrifft,
491          * fuer das wir uns registriert haben (vgl. Initialize_Signal_Handler). Die
492          * Nummer des eingetroffenen Signals wird der Funktion uebergeben. */
493
494         switch( Signal )
495         {
496                 case SIGTERM:
497                 case SIGINT:
498                 case SIGQUIT:
499                         /* wir soll(t)en uns wohl beenden ... */
500                         NGIRCd_SignalQuit = TRUE;
501                         break;
502                 case SIGHUP:
503                         /* Konfiguration neu einlesen: */
504                         NGIRCd_SignalRehash = TRUE;
505                         break;
506                 case SIGCHLD:
507                         /* Child-Prozess wurde beendet. Zombies vermeiden: */
508                         while( waitpid( -1, NULL, WNOHANG ) > 0);
509                         break;
510 #ifdef DEBUG
511                 default:
512                         /* unbekanntes bzw. unbehandeltes Signal */
513                         Log( LOG_DEBUG, "Got signal %d! Ignored.", Signal );
514 #endif
515         }
516 } /* Signal_Handler */
517
518
519 LOCAL VOID
520 Show_Version( VOID )
521 {
522         puts( NGIRCd_Version( ));
523         puts( "Copyright (c)2001-2003 by Alexander Barton (<alex@barton.de>)." );
524         puts( "Homepage: <http://arthur.ath.cx/~alex/ngircd/>\n" );
525         puts( "This is free software; see the source for copying conditions. There is NO" );
526         puts( "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE." );
527 } /* Show_Version */
528
529
530 LOCAL VOID
531 Show_Help( VOID )
532 {
533 #ifdef DEBUG
534         puts( "  -d, --debug        log extra debug messages" );
535 #endif
536         puts( "  -f, --config <f>   use file <f> as configuration file" );
537         puts( "  -n, --nodaemon     don't fork and don't detach from controlling terminal" );
538         puts( "  -p, --passive      disable automatic connections to other servers" );
539 #ifdef SNIFFER
540         puts( "  -s, --sniffer      enable network sniffer and display all IRC traffic" );
541 #endif
542         puts( "      --configtest   read, validate and display configuration; then exit" );
543         puts( "      --version      output version information and exit" );
544         puts( "      --help         display this help and exit" );
545 } /* Show_Help */
546
547
548 /* -eof- */