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