]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/ngircd.c
Adjusted copyright notice to include 2004.
[ngircd-alex.git] / src / ngircd / ngircd.c
1 /*
2  * ngIRCd -- The Next Generation IRC Daemon
3  * Copyright (c)2001-2004 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.81 2004/01/02 19:23:30 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                                 if( argv[i][n] == 't' )
187                                 {
188                                         configtest = TRUE;
189                                         ok = TRUE;
190                                 }
191
192                                 if( ! ok )
193                                 {
194                                         printf( "%s: invalid option \"-%c\"!\n", PACKAGE_NAME, argv[i][n] );
195                                         printf( "Try \"%s --help\" for more information.\n", PACKAGE_NAME );
196                                         exit( 1 );
197                                 }
198                         }
199
200                 }
201                 if( ! ok )
202                 {
203                         printf( "%s: invalid option \"%s\"!\n", PACKAGE_NAME, argv[i] );
204                         printf( "Try \"%s --help\" for more information.\n", PACKAGE_NAME );
205                         exit( 1 );
206                 }
207         }
208
209         /* Debug-Level (fuer IRC-Befehl "VERSION") ermitteln */
210         strcpy( NGIRCd_DebugLevel, "" );
211 #ifdef DEBUG
212         if( NGIRCd_Debug ) strcpy( NGIRCd_DebugLevel, "1" );
213 #endif
214 #ifdef SNIFFER
215         if( NGIRCd_Sniffer )
216         {
217                 NGIRCd_Debug = TRUE;
218                 strcpy( NGIRCd_DebugLevel, "2" );
219         }
220 #endif
221
222         /* Soll nur die Konfigurations ueberprueft und ausgegeben werden? */
223         if( configtest )
224         {
225                 Show_Version( ); puts( "" );
226                 exit( Conf_Test( ));
227         }
228         
229         while( ! NGIRCd_SignalQuit )
230         {
231                 /* In der Regel wird ein Sub-Prozess ge-fork()'t, der
232                  * nicht mehr mit dem Terminal verbunden ist. Mit der
233                  * Option "--nodaemon" kann dies (z.B. zum Debuggen)
234                  * verhindert werden. */
235                 if( ! NGIRCd_NoDaemon )
236                 {
237                         /* Daemon im Hintergrund erzeugen */
238                         pid = (LONG)fork( );
239                         if( pid > 0 )
240                         {
241                                 /* "alter" Prozess */
242                                 exit( 0 );
243                         }
244                         if( pid < 0 )
245                         {
246                                 /* Fehler */
247                                 printf( "%s: Can't fork: %s!\nFatal error, exiting now ...\n", PACKAGE_NAME, strerror( errno ));
248                                 exit( 1 );
249                         }
250
251                         /* Child-Prozess initialisieren */
252                         (VOID)setsid( );
253                         chdir( "/" );
254                 }
255         
256                 /* Globale Variablen initialisieren */
257                 NGIRCd_Start = time( NULL );
258                 (VOID)strftime( NGIRCd_StartStr, 64, "%a %b %d %Y at %H:%M:%S (%Z)", localtime( &NGIRCd_Start ));
259                 NGIRCd_SignalRehash = FALSE;
260                 NGIRCd_SignalRestart = FALSE;
261                 NGIRCd_SignalQuit = FALSE;
262
263                 /* Module initialisieren */
264                 Log_Init( );
265                 Resolve_Init( );
266                 Conf_Init( );
267                 Lists_Init( );
268                 Channel_Init( );
269                 Client_Init( );
270 #ifdef RENDEZVOUS
271                 Rendezvous_Init( );
272 #endif
273                 Conn_Init( );
274
275                 /* Wenn als root ausgefuehrt und eine andere UID
276                  * konfiguriert ist, jetzt zu dieser wechseln */
277                 if( getuid( ) == 0 )
278                 {
279                         if( Conf_GID != 0 )
280                         {
281                                 /* Neue Group-ID setzen */
282                                 if( setgid( Conf_GID ) != 0 ) Log( LOG_ERR, "Can't change Group-ID to %u: %s", Conf_GID, strerror( errno ));
283                         }
284                         if( Conf_UID != 0 )
285                         {
286                                 /* Neue User-ID setzen */
287                                 if( setuid( Conf_UID ) != 0 ) Log( LOG_ERR, "Can't change User-ID to %u: %s", Conf_UID, strerror( errno ));
288                         }
289                 }
290                 
291                 /* User, Gruppe und Prozess-ID des Daemon ausgeben */
292                 pwd = getpwuid( getuid( )); grp = getgrgid( getgid( ));
293                 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( ));
294
295                 /* stderr in "Error-File" umlenken */
296                 Log_InitErrorfile( );
297
298                 /* Signal-Handler initialisieren */
299                 Initialize_Signal_Handler( );
300
301                 /* Protokoll- und Server-Identifikation erzeugen. Die vom ngIRCd
302                  * beim PASS-Befehl verwendete Syntax sowie die erweiterten Flags
303                  * sind in doc/Protocol.txt beschrieben. */
304 #ifdef IRCPLUS
305                 sprintf( NGIRCd_ProtoID, "%s%s %s|%s:%s", PROTOVER, PROTOIRCPLUS, PACKAGE_NAME, PACKAGE_VERSION, IRCPLUSFLAGS );
306 #ifdef ZLIB
307                 strcat( NGIRCd_ProtoID, "Z" );
308 #endif
309                 if( Conf_OperCanMode ) strcat( NGIRCd_ProtoID, "o" );
310 #else
311                 sprintf( NGIRCd_ProtoID, "%s%s %s|%s", PROTOVER, PROTOIRC, PACKAGE_NAME, PACKAGE_VERSION );
312 #endif
313                 strcat( NGIRCd_ProtoID, " P" );
314 #ifdef ZLIB
315                 strcat( NGIRCd_ProtoID, "Z" );
316 #endif
317                 Log( LOG_DEBUG, "Protocol and server ID is \"%s\".", NGIRCd_ProtoID );
318
319                 /* Vordefinierte Channels anlegen */
320                 Channel_InitPredefined( );
321
322                 /* Listen-Ports initialisieren */
323                 if( Conn_InitListeners( ) < 1 )
324                 {
325                         Log( LOG_ALERT, "Server isn't listening on a single port!" );
326                         Log( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
327                         exit( 1 );
328                 }
329                 
330                 /* Hauptschleife */
331                 Conn_Handler( );
332
333                 /* Alles abmelden */
334                 Conn_Exit( );
335 #ifdef RENDEZVOUS
336                 Rendezvous_Exit( );
337 #endif
338                 Client_Exit( );
339                 Channel_Exit( );
340                 Lists_Exit( );
341                 Log_Exit( );
342         }
343
344         return 0;
345 } /* main */
346
347
348 GLOBAL CHAR *
349 NGIRCd_Version( VOID )
350 {
351         STATIC CHAR version[126];
352         
353 #ifdef CVSDATE
354         sprintf( version, "%s %s(%s)-%s", PACKAGE_NAME, PACKAGE_VERSION, CVSDATE, NGIRCd_VersionAddition( ));
355 #else
356         sprintf( version, "%s %s-%s", PACKAGE_NAME, PACKAGE_VERSION, NGIRCd_VersionAddition( ));
357 #endif
358         return version;
359 } /* NGIRCd_Version */
360
361
362 GLOBAL CHAR *
363 NGIRCd_VersionAddition( VOID )
364 {
365         STATIC CHAR txt[200];
366
367         strcpy( txt, "" );
368
369 #ifdef SYSLOG
370         if( txt[0] ) strcat( txt, "+" );
371         strcat( txt, "SYSLOG" );
372 #endif
373 #ifdef ZLIB
374         if( txt[0] ) strcat( txt, "+" );
375         strcat( txt, "ZLIB" );
376 #endif
377 #ifdef TCPWRAP
378         if( txt[0] ) strcat( txt, "+" );
379         strcat( txt, "TCPWRAP" );
380 #endif
381 #ifdef RENDEZVOUS
382         if( txt[0] ) strcat( txt, "+" );
383         strcat( txt, "RENDEZVOUS" );
384 #endif
385 #ifdef IDENTAUTH
386         if( txt[0] ) strcat( txt, "+" );
387         strcat( txt, "IDENT" );
388 #endif
389 #ifdef DEBUG
390         if( txt[0] ) strcat( txt, "+" );
391         strcat( txt, "DEBUG" );
392 #endif
393 #ifdef SNIFFER
394         if( txt[0] ) strcat( txt, "+" );
395         strcat( txt, "SNIFFER" );
396 #endif
397 #ifdef STRICT_RFC
398         if( txt[0] ) strcat( txt, "+" );
399         strcat( txt, "RFC" );
400 #endif
401 #ifdef IRCPLUS
402         if( txt[0] ) strcat( txt, "+" );
403         strcat( txt, "IRCPLUS" );
404 #endif
405         
406         if( txt[0] ) strlcat( txt, "-", sizeof( txt ));
407         strlcat( txt, TARGET_CPU, sizeof( txt ));
408         strlcat( txt, "/", sizeof( txt ));
409         strlcat( txt, TARGET_VENDOR, sizeof( txt ));
410         strlcat( txt, "/", sizeof( txt ));
411         strlcat( txt, TARGET_OS, sizeof( txt ));
412
413         return txt;
414 } /* NGIRCd_VersionAddition */
415
416
417 GLOBAL VOID
418 NGIRCd_Rehash( VOID )
419 {
420         CHAR old_name[CLIENT_ID_LEN];
421
422         Log( LOG_NOTICE|LOG_snotice, "Re-reading configuration NOW!" );
423         NGIRCd_SignalRehash = FALSE;
424
425         /* Alle Listen-Sockets schliessen */
426         Conn_ExitListeners( );
427
428         /* Alten Server-Namen merken */
429 #ifdef DEBUG
430         assert( sizeof( old_name ) == sizeof( Conf_ServerName ));
431 #endif
432         strcpy( old_name, Conf_ServerName );
433
434         /* Konfiguration neu lesen ... */
435         Conf_Rehash( );
436
437         /* Alten Server-Namen wiederherstellen: dieser
438          * kann nicht zur Laufzeit geaendert werden ... */
439         if( strcmp( old_name, Conf_ServerName ) != 0 )
440         {
441                 strcpy( Conf_ServerName, old_name );
442                 Log( LOG_ERR, "Can't change \"ServerName\" on runtime! Ignored new name." );
443         }
444
445         /* neue pre-defined Channel anlegen: */
446         Channel_InitPredefined( );
447         
448         /* Listen-Sockets neu anlegen: */
449         Conn_InitListeners( );
450
451         /* Sync configuration with established connections */
452         Conn_SyncServerStruct( );
453
454         Log( LOG_NOTICE|LOG_snotice, "Re-reading of configuration done." );
455 } /* NGIRCd_Rehash */
456
457
458 LOCAL VOID
459 Initialize_Signal_Handler( VOID )
460 {
461         /* Signal-Handler initialisieren: einige Signale
462          * werden ignoriert, andere speziell behandelt. */
463
464 #ifdef HAVE_SIGACTION
465         /* sigaction() ist vorhanden */
466
467         struct sigaction saction;
468
469         /* Signal-Struktur initialisieren */
470         memset( &saction, 0, sizeof( saction ));
471         saction.sa_handler = Signal_Handler;
472 #ifdef SA_RESTART
473         saction.sa_flags |= SA_RESTART;
474 #endif
475 #ifdef SA_NOCLDWAIT
476         saction.sa_flags |= SA_NOCLDWAIT;
477 #endif
478
479         /* Signal-Handler einhaengen */
480         sigaction( SIGINT, &saction, NULL );
481         sigaction( SIGQUIT, &saction, NULL );
482         sigaction( SIGTERM, &saction, NULL);
483         sigaction( SIGHUP, &saction, NULL);
484         sigaction( SIGCHLD, &saction, NULL);
485
486         /* einige Signale ignorieren */
487         saction.sa_handler = SIG_IGN;
488         sigaction( SIGPIPE, &saction, NULL );
489 #else
490         /* kein sigaction() vorhanden */
491
492         /* Signal-Handler einhaengen */
493         signal( SIGINT, Signal_Handler );
494         signal( SIGQUIT, Signal_Handler );
495         signal( SIGTERM, Signal_Handler );
496         signal( SIGHUP, Signal_Handler );
497         signal( SIGCHLD, Signal_Handler );
498
499         /* einige Signale ignorieren */
500         signal( SIGPIPE, SIG_IGN );
501 #endif
502 } /* Initialize_Signal_Handler */
503
504
505 LOCAL VOID
506 Signal_Handler( INT Signal )
507 {
508         /* Signal-Handler. Dieser wird aufgerufen, wenn eines der Signale eintrifft,
509          * fuer das wir uns registriert haben (vgl. Initialize_Signal_Handler). Die
510          * Nummer des eingetroffenen Signals wird der Funktion uebergeben. */
511
512         switch( Signal )
513         {
514                 case SIGTERM:
515                 case SIGINT:
516                 case SIGQUIT:
517                         /* wir soll(t)en uns wohl beenden ... */
518                         NGIRCd_SignalQuit = TRUE;
519                         break;
520                 case SIGHUP:
521                         /* Konfiguration neu einlesen: */
522                         NGIRCd_SignalRehash = TRUE;
523                         break;
524                 case SIGCHLD:
525                         /* Child-Prozess wurde beendet. Zombies vermeiden: */
526                         while( waitpid( -1, NULL, WNOHANG ) > 0);
527                         break;
528 #ifdef DEBUG
529                 default:
530                         /* unbekanntes bzw. unbehandeltes Signal */
531                         Log( LOG_DEBUG, "Got signal %d! Ignored.", Signal );
532 #endif
533         }
534 } /* Signal_Handler */
535
536
537 LOCAL VOID
538 Show_Version( VOID )
539 {
540         puts( NGIRCd_Version( ));
541         puts( "Copyright (c)2001-2004 by Alexander Barton (<alex@barton.de>)." );
542         puts( "Homepage: <http://arthur.ath.cx/~alex/ngircd/>\n" );
543         puts( "This is free software; see the source for copying conditions. There is NO" );
544         puts( "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE." );
545 } /* Show_Version */
546
547
548 LOCAL VOID
549 Show_Help( VOID )
550 {
551 #ifdef DEBUG
552         puts( "  -d, --debug        log extra debug messages" );
553 #endif
554         puts( "  -f, --config <f>   use file <f> as configuration file" );
555         puts( "  -n, --nodaemon     don't fork and don't detach from controlling terminal" );
556         puts( "  -p, --passive      disable automatic connections to other servers" );
557 #ifdef SNIFFER
558         puts( "  -s, --sniffer      enable network sniffer and display all IRC traffic" );
559 #endif
560         puts( "  -t, --configtest   read, validate and display configuration; then exit" );
561         puts( "      --version      output version information and exit" );
562         puts( "      --help         display this help and exit" );
563 } /* Show_Help */
564
565
566 /* -eof- */