4690e7dd5c8d6c340781238d62294473fabd373e
[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  * 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.66 2002/12/26 13:17:57 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 #include "exp.h"
47 #include "ngircd.h"
48
49
50 LOCAL VOID Initialize_Signal_Handler PARAMS(( VOID ));
51 LOCAL VOID Signal_Handler PARAMS(( INT Signal ));
52
53 LOCAL VOID Show_Version PARAMS(( VOID ));
54 LOCAL VOID Show_Help PARAMS(( VOID ));
55
56
57 GLOBAL int
58 main( int argc, const char *argv[] )
59 {
60         struct passwd *pwd;
61         struct group *grp;
62         BOOLEAN ok, configtest = FALSE;
63         LONG pid, n;
64         INT i;
65
66         umask( 0077 );
67
68         NGIRCd_SignalQuit = NGIRCd_SignalRestart = NGIRCd_SignalRehash = FALSE;
69         NGIRCd_NoDaemon = NGIRCd_Passive = FALSE;
70 #ifdef DEBUG
71         NGIRCd_Debug = FALSE;
72 #endif
73 #ifdef SNIFFER
74         NGIRCd_Sniffer = FALSE;
75 #endif
76         strcpy( NGIRCd_ConfFile, SYSCONFDIR );
77         strcat( NGIRCd_ConfFile, CONFIG_FILE );
78
79         /* Kommandozeile parsen */
80         for( i = 1; i < argc; i++ )
81         {
82                 ok = FALSE;
83                 if(( argv[i][0] == '-' ) && ( argv[i][1] == '-' ))
84                 {
85                         /* Lange Option */
86
87                         if( strcmp( argv[i], "--config" ) == 0 )
88                         {
89                                 if( i + 1 < argc )
90                                 {
91                                         /* Ok, danach kommt noch ein Parameter */
92                                         strncpy( NGIRCd_ConfFile, argv[i + 1], FNAME_LEN - 1 );
93                                         NGIRCd_ConfFile[FNAME_LEN - 1] = '\0';
94
95                                         /* zum uebernaechsten Parameter */
96                                         i++; ok = TRUE;
97                                 }
98                         }
99                         if( strcmp( argv[i], "--configtest" ) == 0 )
100                         {
101                                 configtest = TRUE;
102                                 ok = TRUE;
103                         }
104 #ifdef DEBUG
105                         if( strcmp( argv[i], "--debug" ) == 0 )
106                         {
107                                 NGIRCd_Debug = TRUE;
108                                 ok = TRUE;
109                         }
110 #endif
111                         if( strcmp( argv[i], "--help" ) == 0 )
112                         {
113                                 Show_Version( );
114                                 puts( "" ); Show_Help( ); puts( "" );
115                                 exit( 1 );
116                         }
117                         if( strcmp( argv[i], "--nodaemon" ) == 0 )
118                         {
119                                 NGIRCd_NoDaemon = TRUE;
120                                 ok = TRUE;
121                         }
122                         if( strcmp( argv[i], "--passive" ) == 0 )
123                         {
124                                 NGIRCd_Passive = TRUE;
125                                 ok = TRUE;
126                         }
127 #ifdef SNIFFER
128                         if( strcmp( argv[i], "--sniffer" ) == 0 )
129                         {
130                                 NGIRCd_Sniffer = TRUE;
131                                 ok = TRUE;
132                         }
133 #endif
134                         if( strcmp( argv[i], "--version" ) == 0 )
135                         {
136                                 Show_Version( );
137                                 exit( 1 );
138                         }
139                 }
140                 else if(( argv[i][0] == '-' ) && ( argv[i][1] != '-' ))
141                 {
142                         /* Kurze Option */
143                         
144                         for( n = 1; n < (LONG)strlen( argv[i] ); n++ )
145                         {
146                                 ok = FALSE;
147 #ifdef DEBUG
148                                 if( argv[i][n] == 'd' )
149                                 {
150                                         NGIRCd_Debug = TRUE;
151                                         ok = TRUE;
152                                 }
153 #endif
154                                 if( argv[i][n] == 'f' )
155                                 {
156                                         if(( ! argv[i][n + 1] ) && ( i + 1 < argc ))
157                                         {
158                                                 /* Ok, danach kommt ein Leerzeichen */
159                                                 strncpy( NGIRCd_ConfFile, argv[i + 1], FNAME_LEN - 1 );
160                                                 NGIRCd_ConfFile[FNAME_LEN - 1] = '\0';
161
162                                                 /* zum uebernaechsten Parameter */
163                                                 i++; n = (LONG)strlen( argv[i] );
164                                                 ok = TRUE;
165                                         }
166                                 }
167                                 if( argv[i][n] == 'n' )
168                                 {
169                                         NGIRCd_NoDaemon = TRUE;
170                                         ok = TRUE;
171                                 }
172                                 if( argv[i][n] == 'p' )
173                                 {
174                                         NGIRCd_Passive = TRUE;
175                                         ok = TRUE;
176                                 }
177 #ifdef SNIFFER
178                                 if( argv[i][n] == 's' )
179                                 {
180                                         NGIRCd_Sniffer = TRUE;
181                                         ok = TRUE;
182                                 }
183 #endif
184
185                                 if( ! ok )
186                                 {
187                                         printf( "%s: invalid option \"-%c\"!\n", PACKAGE, argv[i][n] );
188                                         printf( "Try \"%s --help\" for more information.\n", PACKAGE );
189                                         exit( 1 );
190                                 }
191                         }
192
193                 }
194                 if( ! ok )
195                 {
196                         printf( "%s: invalid option \"%s\"!\n", PACKAGE, argv[i] );
197                         printf( "Try \"%s --help\" for more information.\n", PACKAGE );
198                         exit( 1 );
199                 }
200         }
201
202         /* Debug-Level (fuer IRC-Befehl "VERSION") ermitteln */
203         strcpy( NGIRCd_DebugLevel, "" );
204 #ifdef DEBUG
205         if( NGIRCd_Debug ) strcpy( NGIRCd_DebugLevel, "1" );
206 #endif
207 #ifdef SNIFFER
208         if( NGIRCd_Sniffer )
209         {
210                 NGIRCd_Debug = TRUE;
211                 strcpy( NGIRCd_DebugLevel, "2" );
212         }
213 #endif
214
215         /* Soll nur die Konfigurations ueberprueft und ausgegeben werden? */
216         if( configtest )
217         {
218                 Show_Version( ); puts( "" );
219                 exit( Conf_Test( ));
220         }
221         
222         while( ! NGIRCd_SignalQuit )
223         {
224                 /* In der Regel wird ein Sub-Prozess ge-fork()'t, der
225                  * nicht mehr mit dem Terminal verbunden ist. Mit der
226                  * Option "--nodaemon" kann dies (z.B. zum Debuggen)
227                  * verhindert werden. */
228                 if( ! NGIRCd_NoDaemon )
229                 {
230                         /* Daemon im Hintergrund erzeugen */
231                         pid = (LONG)fork( );
232                         if( pid > 0 )
233                         {
234                                 /* "alter" Prozess */
235                                 exit( 0 );
236                         }
237                         if( pid < 0 )
238                         {
239                                 /* Fehler */
240                                 printf( "%s: Can't fork: %s!\nFatal error, exiting now ...\n", PACKAGE, strerror( errno ));
241                                 exit( 1 );
242                         }
243
244                         /* Child-Prozess initialisieren */
245                         (VOID)setsid( );
246                         chdir( "/" );
247                 }
248         
249                 /* Globale Variablen initialisieren */
250                 NGIRCd_Start = time( NULL );
251                 (VOID)strftime( NGIRCd_StartStr, 64, "%a %b %d %Y at %H:%M:%S (%Z)", localtime( &NGIRCd_Start ));
252                 NGIRCd_SignalRehash = FALSE;
253                 NGIRCd_SignalRestart = FALSE;
254                 NGIRCd_SignalQuit = FALSE;
255
256                 /* Module initialisieren */
257                 Log_Init( );
258                 Resolve_Init( );
259                 Conf_Init( );
260                 Lists_Init( );
261                 Channel_Init( );
262                 Client_Init( );
263                 Conn_Init( );
264
265                 /* Wenn als root ausgefuehrt und eine andere UID
266                  * konfiguriert ist, jetzt zu dieser wechseln */
267                 if( getuid( ) == 0 )
268                 {
269                         if( Conf_GID != 0 )
270                         {
271                                 /* Neue Group-ID setzen */
272                                 if( setgid( Conf_GID ) != 0 ) Log( LOG_ERR, "Can't change Group-ID to %u: %s", Conf_GID, strerror( errno ));
273                         }
274                         if( Conf_UID != 0 )
275                         {
276                                 /* Neue User-ID setzen */
277                                 if( setuid( Conf_UID ) != 0 ) Log( LOG_ERR, "Can't change User-ID to %u: %s", Conf_UID, strerror( errno ));
278                         }
279                 }
280                 
281                 /* User, Gruppe und Prozess-ID des Daemon ausgeben */
282                 pwd = getpwuid( getuid( )); grp = getgrgid( getgid( ));
283                 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( ));
284
285                 /* stderr in "Error-File" umlenken */
286                 Log_InitErrorfile( );
287
288                 /* Signal-Handler initialisieren */
289                 Initialize_Signal_Handler( );
290
291                 /* Protokoll- und Server-Identifikation erzeugen. Die vom ngIRCd
292                  * beim PASS-Befehl verwendete Syntax sowie die erweiterten Flags
293                  * sind in doc/Protocol.txt beschrieben. */
294 #ifdef IRCPLUS
295                 sprintf( NGIRCd_ProtoID, "%s%s %s|%s:%s", PROTOVER, PROTOIRCPLUS, PACKAGE, VERSION, IRCPLUSFLAGS );
296 #ifdef USE_ZLIB
297                 strcat( NGIRCd_ProtoID, "Z" );
298 #endif
299                 if( Conf_OperCanMode ) strcat( NGIRCd_ProtoID, "o" );
300 #else
301                 sprintf( NGIRCd_ProtoID, "%s%s %s|%s", PROTOVER, PROTOIRC, PACKAGE, VERSION );
302 #endif
303                 strcat( NGIRCd_ProtoID, " P" );
304 #ifdef USE_ZLIB
305                 strcat( NGIRCd_ProtoID, "Z" );
306 #endif
307                 Log( LOG_DEBUG, "Protocol and server ID is \"%s\".", NGIRCd_ProtoID );
308
309                 /* Vordefinierte Channels anlegen */
310                 Channel_InitPredefined( );
311
312                 /* Listen-Ports initialisieren */
313                 if( Conn_InitListeners( ) < 1 )
314                 {
315                         Log( LOG_ALERT, "Server isn't listening on a single port!" );
316                         Log( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE );
317                         exit( 1 );
318                 }
319                 
320                 /* Hauptschleife */
321                 Conn_Handler( );
322
323                 /* Alles abmelden */
324                 Conn_Exit( );
325                 Client_Exit( );
326                 Channel_Exit( );
327                 Lists_Exit( );
328                 Log_Exit( );
329         }
330
331         return 0;
332 } /* main */
333
334
335 GLOBAL CHAR *
336 NGIRCd_Version( VOID )
337 {
338         STATIC CHAR version[126];
339         
340 #ifdef CVSDATE
341         sprintf( version, "%s %s(%s)-%s", PACKAGE, VERSION, CVSDATE, NGIRCd_VersionAddition( ));
342 #else
343         sprintf( version, "%s %s-%s", PACKAGE, VERSION, NGIRCd_VersionAddition( ));
344 #endif
345         return version;
346 } /* NGIRCd_Version */
347
348
349 GLOBAL CHAR *
350 NGIRCd_VersionAddition( VOID )
351 {
352         STATIC CHAR txt[64];
353
354         strcpy( txt, "" );
355
356 #ifdef USE_SYSLOG
357         if( txt[0] ) strcat( txt, "+" );
358         strcat( txt, "SYSLOG" );
359 #endif
360 #ifdef USE_ZLIB
361         if( txt[0] ) strcat( txt, "+" );
362         strcat( txt, "ZLIB" );
363 #endif
364 #ifdef DEBUG
365         if( txt[0] ) strcat( txt, "+" );
366         strcat( txt, "DEBUG" );
367 #endif
368 #ifdef SNIFFER
369         if( txt[0] ) strcat( txt, "+" );
370         strcat( txt, "SNIFFER" );
371 #endif
372 #ifdef STRICT_RFC
373         if( txt[0] ) strcat( txt, "+" );
374         strcat( txt, "RFC" );
375 #endif
376 #ifdef IRCPLUS
377         if( txt[0] ) strcat( txt, "+" );
378         strcat( txt, "IRCPLUS" );
379 #endif
380         
381         if( txt[0] ) strcat( txt, "-" );
382         strcat( txt, TARGET_CPU );
383         strcat( txt, "/" );
384         strcat( txt, TARGET_VENDOR );
385         strcat( txt, "/" );
386         strcat( txt, TARGET_OS );
387
388         return txt;
389 } /* NGIRCd_VersionAddition */
390
391
392 GLOBAL VOID
393 NGIRCd_Rehash( VOID )
394 {
395         CHAR old_name[CLIENT_ID_LEN];
396
397         Log( LOG_NOTICE|LOG_snotice, "Re-reading configuration NOW!" );
398         NGIRCd_SignalRehash = FALSE;
399
400         /* Alle Listen-Sockets schliessen */
401         Conn_ExitListeners( );
402
403         /* Alten Server-Namen merken */
404         strcpy( old_name, Conf_ServerName );
405
406         /* Konfiguration neu lesen ... */
407         Conf_Init( );
408
409         /* Alten Server-Namen wiederherstellen: dieser
410          * kann nicht zur Laufzeit geaendert werden ... */
411         if( strcmp( old_name, Conf_ServerName ) != 0 )
412         {
413                 strcpy( Conf_ServerName, old_name );
414                 Log( LOG_ERR, "Can't change \"ServerName\" on runtime! Ignored new name." );
415         }
416
417         /* neue pre-defined Channel anlegen: */
418         Channel_InitPredefined( );
419         
420         /* Listen-Sockets neu anlegen: */
421         Conn_InitListeners( );
422
423         Log( LOG_NOTICE|LOG_snotice, "Re-reading of configuration done." );
424 } /* NGIRCd_Rehash */
425
426
427 LOCAL VOID
428 Initialize_Signal_Handler( VOID )
429 {
430         /* Signal-Handler initialisieren: einige Signale
431          * werden ignoriert, andere speziell behandelt. */
432
433 #ifdef HAVE_SIGACTION
434         /* sigaction() ist vorhanden */
435
436         struct sigaction saction;
437
438         /* Signal-Struktur initialisieren */
439         memset( &saction, 0, sizeof( saction ));
440         saction.sa_handler = Signal_Handler;
441 #ifdef SA_RESTART
442         saction.sa_flags |= SA_RESTART;
443 #endif
444 #ifdef SA_NOCLDWAIT
445         saction.sa_flags |= SA_NOCLDWAIT;
446 #endif
447
448         /* Signal-Handler einhaengen */
449         sigaction( SIGINT, &saction, NULL );
450         sigaction( SIGQUIT, &saction, NULL );
451         sigaction( SIGTERM, &saction, NULL);
452         sigaction( SIGHUP, &saction, NULL);
453         sigaction( SIGCHLD, &saction, NULL);
454
455         /* einige Signale ignorieren */
456         saction.sa_handler = SIG_IGN;
457         sigaction( SIGPIPE, &saction, NULL );
458 #else
459         /* kein sigaction() vorhanden */
460
461         /* Signal-Handler einhaengen */
462         signal( SIGINT, Signal_Handler );
463         signal( SIGQUIT, Signal_Handler );
464         signal( SIGTERM, Signal_Handler );
465         signal( SIGHUP, Signal_Handler );
466         signal( SIGCHLD, Signal_Handler );
467
468         /* einige Signale ignorieren */
469         signal( SIGPIPE, SIG_IGN );
470 #endif
471 } /* Initialize_Signal_Handler */
472
473
474 LOCAL VOID
475 Signal_Handler( INT Signal )
476 {
477         /* Signal-Handler. Dieser wird aufgerufen, wenn eines der Signale eintrifft,
478          * fuer das wir uns registriert haben (vgl. Initialize_Signal_Handler). Die
479          * Nummer des eingetroffenen Signals wird der Funktion uebergeben. */
480
481         switch( Signal )
482         {
483                 case SIGTERM:
484                 case SIGINT:
485                 case SIGQUIT:
486                         /* wir soll(t)en uns wohl beenden ... */
487                         NGIRCd_SignalQuit = TRUE;
488                         break;
489                 case SIGHUP:
490                         /* Konfiguration neu einlesen: */
491                         NGIRCd_SignalRehash = TRUE;
492                         break;
493                 case SIGCHLD:
494                         /* Child-Prozess wurde beendet. Zombies vermeiden: */
495                         while( waitpid( -1, NULL, WNOHANG ) > 0);
496                         break;
497 #ifdef DEBUG
498                 default:
499                         /* unbekanntes bzw. unbehandeltes Signal */
500                         Log( LOG_DEBUG, "Got signal %d! Ignored.", Signal );
501 #endif
502         }
503 } /* Signal_Handler */
504
505
506 LOCAL VOID
507 Show_Version( VOID )
508 {
509         puts( NGIRCd_Version( ));
510         puts( "Copyright (c)2001,2002 by Alexander Barton (<alex@barton.de>)." );
511         puts( "Homepage: <http://arthur.ath.cx/~alex/ngircd/>\n" );
512         puts( "This is free software; see the source for copying conditions. There is NO" );
513         puts( "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE." );
514 } /* Show_Version */
515
516
517 LOCAL VOID
518 Show_Help( VOID )
519 {
520 #ifdef DEBUG
521         puts( "  -d, --debug        log extra debug messages" );
522 #endif
523         puts( "  -f, --config <f>   use file <f> as configuration file" );
524         puts( "  -n, --nodaemon     don't fork and don't detach from controlling terminal" );
525         puts( "  -p, --passive      disable automatic connections to other servers" );
526 #ifdef SNIFFER
527         puts( "  -s, --sniffer      enable network sniffer and display all IRC traffic" );
528 #endif
529         puts( "      --configtest   read, validate and display configuration; then exit" );
530         puts( "      --version      output version information and exit" );
531         puts( "      --help         display this help and exit" );
532 } /* Show_Help */
533
534
535 /* -eof- */