]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/ngircd.c
Fixed resolver when using IDENT lookups, cleaned up code.
[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.85 2004/05/11 00:01:11 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 <time.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <sys/wait.h>
31 #include <pwd.h>
32 #include <grp.h>
33
34 #include "defines.h"
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 "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                 /* Initialize global variables */
232                 NGIRCd_Start = time( NULL );
233                 (VOID)strftime( NGIRCd_StartStr, 64, "%a %b %d %Y at %H:%M:%S (%Z)", localtime( &NGIRCd_Start ));
234
235                 NGIRCd_SignalRehash = FALSE;
236                 NGIRCd_SignalRestart = FALSE;
237                 NGIRCd_SignalQuit = FALSE;
238
239                 /* Initialize modules, part I */
240                 Log_Init( );
241                 Conf_Init( );
242
243                 if( Conf_Chroot[0] )
244                 {
245                         /* Chroot */
246                         if( chdir( Conf_Chroot ) != 0 ) Log( LOG_ERR, "Can't chdir() in ChrootDir (%s): %s", Conf_Chroot, strerror( errno ));
247
248                         if( chroot( Conf_Chroot ) != 0 ) Log( LOG_ERR, "Can't change root directory to \"%s\": %s", Conf_Chroot, strerror( errno ));
249                         else Log( LOG_INFO, "Changed root and working directory to \"%s\".", Conf_Chroot );
250                 }
251
252                 if( Conf_GID != 0 )
253                 {
254                         /* Set new group ID */
255                         if( setgid( Conf_GID ) != 0 ) Log( LOG_ERR, "Can't change group ID to %u: %s", Conf_GID, strerror( errno ));
256                 }
257                 if( Conf_UID != 0 )
258                 {
259                         /* Set new user ID */
260                         if( setuid( Conf_UID ) != 0 ) Log( LOG_ERR, "Can't change user ID to %u: %s", Conf_UID, strerror( errno ));
261                 }
262
263                 /* In der Regel wird ein Sub-Prozess ge-fork()'t, der
264                  * nicht mehr mit dem Terminal verbunden ist. Mit der
265                  * Option "--nodaemon" kann dies (z.B. zum Debuggen)
266                  * verhindert werden. */
267                 if( ! NGIRCd_NoDaemon )
268                 {
269                         /* Daemon im Hintergrund erzeugen */
270                         pid = (LONG)fork( );
271                         if( pid > 0 )
272                         {
273                                 /* "alter" Prozess */
274                                 exit( 0 );
275                         }
276                         if( pid < 0 )
277                         {
278                                 /* Fehler */
279                                 printf( "%s: Can't fork: %s!\nFatal error, exiting now ...\n", PACKAGE_NAME, strerror( errno ));
280                                 exit( 1 );
281                         }
282
283                         /* Child-Prozess initialisieren */
284                         (VOID)setsid( );
285                         chdir( "/" );
286                 }
287
288                 /* Initialize modules, part II: these functions are eventually
289                  * called with already dropped privileges ... */
290                 Resolve_Init( );
291                 Lists_Init( );
292                 Channel_Init( );
293                 Client_Init( );
294 #ifdef RENDEZVOUS
295                 Rendezvous_Init( );
296 #endif
297                 Conn_Init( );
298
299                 /* Show user, group, and PID of the running daemon */
300                 pwd = getpwuid( getuid( )); grp = getgrgid( getgid( ));
301                 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( ));
302
303                 /* Redirect stderr handle to "error file" for debugging.
304                  * But don't try to write in the chroot jail, since it's more 
305                  * secure to have a chroot dir not writable by the daemon.
306                  */
307                 if( ! Conf_Chroot[0] ) Log_InitErrorfile( );
308
309                 /* Signal-Handler initialisieren */
310                 Initialize_Signal_Handler( );
311
312                 /* Protokoll- und Server-Identifikation erzeugen. Die vom ngIRCd
313                  * beim PASS-Befehl verwendete Syntax sowie die erweiterten Flags
314                  * sind in doc/Protocol.txt beschrieben. */
315 #ifdef IRCPLUS
316                 sprintf( NGIRCd_ProtoID, "%s%s %s|%s:%s", PROTOVER, PROTOIRCPLUS, PACKAGE_NAME, PACKAGE_VERSION, IRCPLUSFLAGS );
317 #ifdef ZLIB
318                 strcat( NGIRCd_ProtoID, "Z" );
319 #endif
320                 if( Conf_OperCanMode ) strcat( NGIRCd_ProtoID, "o" );
321 #else
322                 sprintf( NGIRCd_ProtoID, "%s%s %s|%s", PROTOVER, PROTOIRC, PACKAGE_NAME, PACKAGE_VERSION );
323 #endif
324                 strcat( NGIRCd_ProtoID, " P" );
325 #ifdef ZLIB
326                 strcat( NGIRCd_ProtoID, "Z" );
327 #endif
328                 Log( LOG_DEBUG, "Protocol and server ID is \"%s\".", NGIRCd_ProtoID );
329
330                 /* Vordefinierte Channels anlegen */
331                 Channel_InitPredefined( );
332
333                 /* Listen-Ports initialisieren */
334                 if( Conn_InitListeners( ) < 1 )
335                 {
336                         Log( LOG_ALERT, "Server isn't listening on a single port!" );
337                         Log( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
338                         exit( 1 );
339                 }
340                 
341                 /* Hauptschleife */
342                 Conn_Handler( );
343
344                 /* Alles abmelden */
345                 Conn_Exit( );
346 #ifdef RENDEZVOUS
347                 Rendezvous_Exit( );
348 #endif
349                 Client_Exit( );
350                 Channel_Exit( );
351                 Lists_Exit( );
352                 Log_Exit( );
353         }
354
355         return 0;
356 } /* main */
357
358
359 GLOBAL CHAR *
360 NGIRCd_Version( VOID )
361 {
362         STATIC CHAR version[126];
363         
364 #ifdef CVSDATE
365         sprintf( version, "%s %s(%s)-%s", PACKAGE_NAME, PACKAGE_VERSION, CVSDATE, NGIRCd_VersionAddition( ));
366 #else
367         sprintf( version, "%s %s-%s", PACKAGE_NAME, PACKAGE_VERSION, NGIRCd_VersionAddition( ));
368 #endif
369         return version;
370 } /* NGIRCd_Version */
371
372
373 GLOBAL CHAR *
374 NGIRCd_VersionAddition( VOID )
375 {
376         STATIC CHAR txt[200];
377
378         strcpy( txt, "" );
379
380 #ifdef SYSLOG
381         if( txt[0] ) strcat( txt, "+" );
382         strcat( txt, "SYSLOG" );
383 #endif
384 #ifdef ZLIB
385         if( txt[0] ) strcat( txt, "+" );
386         strcat( txt, "ZLIB" );
387 #endif
388 #ifdef TCPWRAP
389         if( txt[0] ) strcat( txt, "+" );
390         strcat( txt, "TCPWRAP" );
391 #endif
392 #ifdef RENDEZVOUS
393         if( txt[0] ) strcat( txt, "+" );
394         strcat( txt, "RENDEZVOUS" );
395 #endif
396 #ifdef IDENTAUTH
397         if( txt[0] ) strcat( txt, "+" );
398         strcat( txt, "IDENT" );
399 #endif
400 #ifdef DEBUG
401         if( txt[0] ) strcat( txt, "+" );
402         strcat( txt, "DEBUG" );
403 #endif
404 #ifdef SNIFFER
405         if( txt[0] ) strcat( txt, "+" );
406         strcat( txt, "SNIFFER" );
407 #endif
408 #ifdef STRICT_RFC
409         if( txt[0] ) strcat( txt, "+" );
410         strcat( txt, "RFC" );
411 #endif
412 #ifdef IRCPLUS
413         if( txt[0] ) strcat( txt, "+" );
414         strcat( txt, "IRCPLUS" );
415 #endif
416         
417         if( txt[0] ) strlcat( txt, "-", sizeof( txt ));
418         strlcat( txt, TARGET_CPU, sizeof( txt ));
419         strlcat( txt, "/", sizeof( txt ));
420         strlcat( txt, TARGET_VENDOR, sizeof( txt ));
421         strlcat( txt, "/", sizeof( txt ));
422         strlcat( txt, TARGET_OS, sizeof( txt ));
423
424         return txt;
425 } /* NGIRCd_VersionAddition */
426
427
428 GLOBAL VOID
429 NGIRCd_Rehash( VOID )
430 {
431         CHAR old_name[CLIENT_ID_LEN];
432
433         Log( LOG_NOTICE|LOG_snotice, "Re-reading configuration NOW!" );
434         NGIRCd_SignalRehash = FALSE;
435
436         /* Close down all listening sockets */
437         Conn_ExitListeners( );
438
439         /* Remember old server name */
440         strcpy( old_name, Conf_ServerName );
441
442         /* Re-read configuration ... */
443         Conf_Rehash( );
444
445         /* Recover old server name: it can't be changed during run-time */
446         if( strcmp( old_name, Conf_ServerName ) != 0 )
447         {
448                 strcpy( Conf_ServerName, old_name );
449                 Log( LOG_ERR, "Can't change \"ServerName\" on runtime! Ignored new name." );
450         }
451
452         /* Create new pre-defined channels */
453         Channel_InitPredefined( );
454         
455         /* Start listening on sockets */
456         Conn_InitListeners( );
457
458         /* Sync configuration with established connections */
459         Conn_SyncServerStruct( );
460
461         Log( LOG_NOTICE|LOG_snotice, "Re-reading of configuration done." );
462 } /* NGIRCd_Rehash */
463
464
465 LOCAL VOID
466 Initialize_Signal_Handler( VOID )
467 {
468         /* Signal-Handler initialisieren: einige Signale
469          * werden ignoriert, andere speziell behandelt. */
470
471 #ifdef HAVE_SIGACTION
472         /* sigaction() ist vorhanden */
473
474         struct sigaction saction;
475
476         /* Signal-Struktur initialisieren */
477         memset( &saction, 0, sizeof( saction ));
478         saction.sa_handler = Signal_Handler;
479 #ifdef SA_RESTART
480         saction.sa_flags |= SA_RESTART;
481 #endif
482 #ifdef SA_NOCLDWAIT
483         saction.sa_flags |= SA_NOCLDWAIT;
484 #endif
485
486         /* Signal-Handler einhaengen */
487         sigaction( SIGINT, &saction, NULL );
488         sigaction( SIGQUIT, &saction, NULL );
489         sigaction( SIGTERM, &saction, NULL);
490         sigaction( SIGHUP, &saction, NULL);
491         sigaction( SIGCHLD, &saction, NULL);
492
493         /* einige Signale ignorieren */
494         saction.sa_handler = SIG_IGN;
495         sigaction( SIGPIPE, &saction, NULL );
496 #else
497         /* kein sigaction() vorhanden */
498
499         /* Signal-Handler einhaengen */
500         signal( SIGINT, Signal_Handler );
501         signal( SIGQUIT, Signal_Handler );
502         signal( SIGTERM, Signal_Handler );
503         signal( SIGHUP, Signal_Handler );
504         signal( SIGCHLD, Signal_Handler );
505
506         /* einige Signale ignorieren */
507         signal( SIGPIPE, SIG_IGN );
508 #endif
509 } /* Initialize_Signal_Handler */
510
511
512 LOCAL VOID
513 Signal_Handler( INT Signal )
514 {
515         /* Signal-Handler. Dieser wird aufgerufen, wenn eines der Signale eintrifft,
516          * fuer das wir uns registriert haben (vgl. Initialize_Signal_Handler). Die
517          * Nummer des eingetroffenen Signals wird der Funktion uebergeben. */
518
519         switch( Signal )
520         {
521                 case SIGTERM:
522                 case SIGINT:
523                 case SIGQUIT:
524                         /* wir soll(t)en uns wohl beenden ... */
525                         NGIRCd_SignalQuit = TRUE;
526                         break;
527                 case SIGHUP:
528                         /* Konfiguration neu einlesen: */
529                         NGIRCd_SignalRehash = TRUE;
530                         break;
531                 case SIGCHLD:
532                         /* Child-Prozess wurde beendet. Zombies vermeiden: */
533                         while( waitpid( -1, NULL, WNOHANG ) > 0);
534                         break;
535 #ifdef DEBUG
536                 default:
537                         /* unbekanntes bzw. unbehandeltes Signal */
538                         Log( LOG_DEBUG, "Got signal %d! Ignored.", Signal );
539 #endif
540         }
541 } /* Signal_Handler */
542
543
544 LOCAL VOID
545 Show_Version( VOID )
546 {
547         puts( NGIRCd_Version( ));
548         puts( "Copyright (c)2001-2004 by Alexander Barton (<alex@barton.de>)." );
549         puts( "Homepage: <http://arthur.ath.cx/~alex/ngircd/>\n" );
550         puts( "This is free software; see the source for copying conditions. There is NO" );
551         puts( "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE." );
552 } /* Show_Version */
553
554
555 LOCAL VOID
556 Show_Help( VOID )
557 {
558 #ifdef DEBUG
559         puts( "  -d, --debug        log extra debug messages" );
560 #endif
561         puts( "  -f, --config <f>   use file <f> as configuration file" );
562         puts( "  -n, --nodaemon     don't fork and don't detach from controlling terminal" );
563         puts( "  -p, --passive      disable automatic connections to other servers" );
564 #ifdef SNIFFER
565         puts( "  -s, --sniffer      enable network sniffer and display all IRC traffic" );
566 #endif
567         puts( "  -t, --configtest   read, validate and display configuration; then exit" );
568         puts( "      --version      output version information and exit" );
569         puts( "      --help         display this help and exit" );
570 } /* Show_Help */
571
572
573 /* -eof- */