]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/ngircd.c
- new file header format.
[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";
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_Restart = FALSE;
69         NGIRCd_Quit = FALSE;
70         NGIRCd_NoDaemon = FALSE;
71         NGIRCd_Passive = FALSE;
72 #ifdef DEBUG
73         NGIRCd_Debug = FALSE;
74 #endif
75 #ifdef SNIFFER
76         NGIRCd_Sniffer = FALSE;
77 #endif
78         strcpy( NGIRCd_ConfFile, CONFIG_FILE );
79
80         /* Kommandozeile parsen */
81         for( i = 1; i < argc; i++ )
82         {
83                 ok = FALSE;
84                 if(( argv[i][0] == '-' ) && ( argv[i][1] == '-' ))
85                 {
86                         /* Lange Option */
87
88                         if( strcmp( argv[i], "--config" ) == 0 )
89                         {
90                                 if( i + 1 < argc )
91                                 {
92                                         /* Ok, danach kommt noch ein Parameter */
93                                         strncpy( NGIRCd_ConfFile, argv[i + 1], FNAME_LEN - 1 );
94                                         NGIRCd_ConfFile[FNAME_LEN - 1] = '\0';
95
96                                         /* zum uebernaechsten Parameter */
97                                         i++; ok = TRUE;
98                                 }
99                         }
100                         if( strcmp( argv[i], "--configtest" ) == 0 )
101                         {
102                                 configtest = TRUE;
103                                 ok = TRUE;
104                         }
105 #ifdef DEBUG
106                         if( strcmp( argv[i], "--debug" ) == 0 )
107                         {
108                                 NGIRCd_Debug = TRUE;
109                                 ok = TRUE;
110                         }
111 #endif
112                         if( strcmp( argv[i], "--help" ) == 0 )
113                         {
114                                 Show_Version( );
115                                 puts( "" ); Show_Help( ); puts( "" );
116                                 exit( 1 );
117                         }
118                         if( strcmp( argv[i], "--nodaemon" ) == 0 )
119                         {
120                                 NGIRCd_NoDaemon = TRUE;
121                                 ok = TRUE;
122                         }
123                         if( strcmp( argv[i], "--passive" ) == 0 )
124                         {
125                                 NGIRCd_Passive = TRUE;
126                                 ok = TRUE;
127                         }
128 #ifdef SNIFFER
129                         if( strcmp( argv[i], "--sniffer" ) == 0 )
130                         {
131                                 NGIRCd_Sniffer = TRUE;
132                                 ok = TRUE;
133                         }
134 #endif
135                         if( strcmp( argv[i], "--version" ) == 0 )
136                         {
137                                 Show_Version( );
138                                 exit( 1 );
139                         }
140                 }
141                 else if(( argv[i][0] == '-' ) && ( argv[i][1] != '-' ))
142                 {
143                         /* Kurze Option */
144                         
145                         for( n = 1; n < (LONG)strlen( argv[i] ); n++ )
146                         {
147                                 ok = FALSE;
148 #ifdef DEBUG
149                                 if( argv[i][n] == 'd' )
150                                 {
151                                         NGIRCd_Debug = TRUE;
152                                         ok = TRUE;
153                                 }
154 #endif
155                                 if( argv[i][n] == 'f' )
156                                 {
157                                         if(( ! argv[i][n + 1] ) && ( i + 1 < argc ))
158                                         {
159                                                 /* Ok, danach kommt ein Leerzeichen */
160                                                 strncpy( NGIRCd_ConfFile, argv[i + 1], FNAME_LEN - 1 );
161                                                 NGIRCd_ConfFile[FNAME_LEN - 1] = '\0';
162
163                                                 /* zum uebernaechsten Parameter */
164                                                 i++; n = (LONG)strlen( argv[i] );
165                                                 ok = TRUE;
166                                         }
167                                 }
168                                 if( argv[i][n] == 'n' )
169                                 {
170                                         NGIRCd_NoDaemon = TRUE;
171                                         ok = TRUE;
172                                 }
173                                 if( argv[i][n] == 'p' )
174                                 {
175                                         NGIRCd_Passive = TRUE;
176                                         ok = TRUE;
177                                 }
178 #ifdef SNIFFER
179                                 if( argv[i][n] == 's' )
180                                 {
181                                         NGIRCd_Sniffer = TRUE;
182                                         ok = TRUE;
183                                 }
184 #endif
185
186                                 if( ! ok )
187                                 {
188                                         printf( "%s: invalid option \"-%c\"!\n", PACKAGE, argv[i][n] );
189                                         printf( "Try \"%s --help\" for more information.\n", PACKAGE );
190                                         exit( 1 );
191                                 }
192                         }
193
194                 }
195                 if( ! ok )
196                 {
197                         printf( "%s: invalid option \"%s\"!\n", PACKAGE, argv[i] );
198                         printf( "Try \"%s --help\" for more information.\n", PACKAGE );
199                         exit( 1 );
200                 }
201         }
202
203         /* Debug-Level (fuer IRC-Befehl "VERSION") ermitteln */
204         strcpy( NGIRCd_DebugLevel, "" );
205 #ifdef DEBUG
206         if( NGIRCd_Debug ) strcpy( NGIRCd_DebugLevel, "1" );
207 #endif
208 #ifdef SNIFFER
209         if( NGIRCd_Sniffer )
210         {
211                 NGIRCd_Debug = TRUE;
212                 strcpy( NGIRCd_DebugLevel, "2" );
213         }
214 #endif
215
216         /* Soll nur die Konfigurations ueberprueft und ausgegeben werden? */
217         if( configtest )
218         {
219                 Show_Version( ); puts( "" );
220                 exit( Conf_Test( ));
221         }
222         
223         while( ! NGIRCd_Quit )
224         {
225                 /* In der Regel wird ein Sub-Prozess ge-fork()'t, der
226                  * nicht mehr mit dem Terminal verbunden ist. Mit der
227                  * Option "--nodaemon" kann dies (z.B. zum Debuggen)
228                  * verhindert werden. */
229                 if( ! NGIRCd_NoDaemon )
230                 {
231                         /* Daemon im Hintergrund erzeugen */
232                         pid = (LONG)fork( );
233                         if( pid > 0 )
234                         {
235                                 /* "alter" Prozess */
236                                 exit( 0 );
237                         }
238                         if( pid < 0 )
239                         {
240                                 /* Fehler */
241                                 printf( "%s: Can't fork: %s!\nFatal error, exiting now ...\n", PACKAGE, strerror( errno ));
242                                 exit( 1 );
243                         }
244
245                         /* Child-Prozess initialisieren */
246                         (VOID)setsid( );
247                         chdir( "/" );
248                 }
249         
250                 /* Globale Variablen initialisieren */
251                 NGIRCd_Start = time( NULL );
252                 (VOID)strftime( NGIRCd_StartStr, 64, "%a %b %d %Y at %H:%M:%S (%Z)", localtime( &NGIRCd_Start ));
253                 NGIRCd_Restart = FALSE;
254                 NGIRCd_Quit = 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         /* Alle Listen-Sockets schliessen */
398         Conn_ExitListeners( );
399
400         /* Alten Server-Namen merken */
401         strcpy( old_name, Conf_ServerName );
402
403         /* Konfiguration neu lesen ... */
404         Conf_Init( );
405
406         /* Alten Server-Namen wiederherstellen: dieser
407          * kann nicht zur Laufzeit geaendert werden ... */
408         if( strcmp( old_name, Conf_ServerName ) != 0 )
409         {
410                 strcpy( Conf_ServerName, old_name );
411                 Log( LOG_ERR, "Can't change \"ServerName\" on runtime! Ignored new name." );
412         }
413
414         /* neue pre-defined Channel anlegen: */
415         Channel_InitPredefined( );
416         
417         /* Listen-Sockets neu anlegen: */
418         Conn_InitListeners( );
419
420         Log( LOG_INFO, "Re-reading of configuration done." );
421 } /* NGIRCd_Rehash */
422
423
424 LOCAL VOID
425 Initialize_Signal_Handler( VOID )
426 {
427         /* Signal-Handler initialisieren: einige Signale
428          * werden ignoriert, andere speziell behandelt. */
429
430 #ifdef HAVE_SIGACTION
431         /* sigaction() ist vorhanden */
432
433         struct sigaction saction;
434
435         /* Signal-Struktur initialisieren */
436         memset( &saction, 0, sizeof( saction ));
437         saction.sa_handler = Signal_Handler;
438 #ifdef SA_RESTART
439         saction.sa_flags |= SA_RESTART;
440 #endif
441 #ifdef SA_NOCLDWAIT
442         saction.sa_flags |= SA_NOCLDWAIT;
443 #endif
444
445         /* Signal-Handler einhaengen */
446         sigaction( SIGINT, &saction, NULL );
447         sigaction( SIGQUIT, &saction, NULL );
448         sigaction( SIGTERM, &saction, NULL);
449         sigaction( SIGHUP, &saction, NULL);
450         sigaction( SIGCHLD, &saction, NULL);
451
452         /* einige Signale ignorieren */
453         saction.sa_handler = SIG_IGN;
454         sigaction( SIGPIPE, &saction, NULL );
455 #else
456         /* kein sigaction() vorhanden */
457
458         /* Signal-Handler einhaengen */
459         signal( SIGINT, Signal_Handler );
460         signal( SIGQUIT, Signal_Handler );
461         signal( SIGTERM, Signal_Handler );
462         signal( SIGHUP, Signal_Handler );
463         signal( SIGCHLD, Signal_Handler );
464
465         /* einige Signale ignorieren */
466         signal( SIGPIPE, SIG_IGN );
467 #endif
468 } /* Initialize_Signal_Handler */
469
470
471 LOCAL VOID
472 Signal_Handler( INT Signal )
473 {
474         /* Signal-Handler. Dieser wird aufgerufen, wenn eines der Signale eintrifft,
475          * fuer das wir uns registriert haben (vgl. Initialize_Signal_Handler). Die
476          * Nummer des eingetroffenen Signals wird der Funktion uebergeben. */
477
478         switch( Signal )
479         {
480                 case SIGTERM:
481                 case SIGINT:
482                 case SIGQUIT:
483                         /* wir soll(t)en uns wohl beenden ... */
484                         if( Signal == SIGTERM ) Log( LOG_WARNING|LOG_snotice, "Got TERM signal, terminating now ..." );
485                         else if( Signal == SIGINT ) Log( LOG_WARNING|LOG_snotice, "Got INT signal, terminating now ..." );
486                         else if( Signal == SIGQUIT ) Log( LOG_WARNING|LOG_snotice, "Got QUIT signal, terminating now ..." );
487                         NGIRCd_Quit = TRUE;
488                         break;
489                 case SIGHUP:
490                         /* Konfiguration neu einlesen: */
491                         Log( LOG_WARNING|LOG_snotice, "Got HUP signal, re-reading configuration ..." );
492                         NGIRCd_Rehash( );
493                         break;
494                 case SIGCHLD:
495                         /* Child-Prozess wurde beendet. Zombies vermeiden: */
496                         while( waitpid( -1, NULL, WNOHANG ) > 0);
497                         break;
498                 default:
499                         /* unbekanntes bzw. unbehandeltes Signal */
500                         Log( LOG_NOTICE, "Got signal %d! Ignored.", Signal );
501         }
502 } /* Signal_Handler */
503
504
505 LOCAL VOID
506 Show_Version( VOID )
507 {
508         puts( NGIRCd_Version( ));
509         puts( "Copyright (c)2001,2002 by Alexander Barton (<alex@barton.de>)." );
510         puts( "Homepage: <http://arthur.ath.cx/~alex/ngircd/>\n" );
511         puts( "This is free software; see the source for copying conditions. There is NO" );
512         puts( "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE." );
513 } /* Show_Version */
514
515
516 LOCAL VOID
517 Show_Help( VOID )
518 {
519 #ifdef DEBUG
520         puts( "  -d, --debug        log extra debug messages" );
521 #endif
522         puts( "  -f, --config <f>   use file <f> as configuration file" );
523         puts( "  -n, --nodaemon     don't fork and don't detach from controlling terminal" );
524         puts( "  -p, --passive      disable automatic connections to other servers" );
525 #ifdef SNIFFER
526         puts( "  -s, --sniffer      enable network sniffer and display all IRC traffic" );
527 #endif
528         puts( "      --configtest   read, validate and display configuration; then exit" );
529         puts( "      --version      output version information and exit" );
530         puts( "      --help         display this help and exit" );
531 } /* Show_Help */
532
533
534 /* -eof- */