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