Blob


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 */
15 #include "portab.h"
17 static char UNUSED id[] = "$Id: ngircd.c,v 1.86 2004/09/04 20:28:51 alex Exp $";
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>
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"
46 #ifdef RENDEZVOUS
47 #include "rendezvous.h"
48 #endif
50 #include "exp.h"
51 #include "ngircd.h"
54 LOCAL VOID Initialize_Signal_Handler PARAMS(( VOID ));
55 LOCAL VOID Signal_Handler PARAMS(( INT Signal ));
57 LOCAL VOID Show_Version PARAMS(( VOID ));
58 LOCAL VOID Show_Help PARAMS(( VOID ));
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;
70 umask( 0077 );
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 ));
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 */
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 ));
98 /* next parameter */
99 i++; ok = TRUE;
102 if( strcmp( argv[i], "--configtest" ) == 0 )
104 configtest = TRUE;
105 ok = TRUE;
107 #ifdef DEBUG
108 if( strcmp( argv[i], "--debug" ) == 0 )
110 NGIRCd_Debug = TRUE;
111 ok = TRUE;
113 #endif
114 if( strcmp( argv[i], "--help" ) == 0 )
116 Show_Version( );
117 puts( "" ); Show_Help( ); puts( "" );
118 exit( 1 );
120 if( strcmp( argv[i], "--nodaemon" ) == 0 )
122 NGIRCd_NoDaemon = TRUE;
123 ok = TRUE;
125 if( strcmp( argv[i], "--passive" ) == 0 )
127 NGIRCd_Passive = TRUE;
128 ok = TRUE;
130 #ifdef SNIFFER
131 if( strcmp( argv[i], "--sniffer" ) == 0 )
133 NGIRCd_Sniffer = TRUE;
134 ok = TRUE;
136 #endif
137 if( strcmp( argv[i], "--version" ) == 0 )
139 Show_Version( );
140 exit( 1 );
143 else if(( argv[i][0] == '-' ) && ( argv[i][1] != '-' ))
145 /* Kurze Option */
147 for( n = 1; n < (LONG)strlen( argv[i] ); n++ )
149 ok = FALSE;
150 #ifdef DEBUG
151 if( argv[i][n] == 'd' )
153 NGIRCd_Debug = TRUE;
154 ok = TRUE;
156 #endif
157 if( argv[i][n] == 'f' )
159 if(( ! argv[i][n + 1] ) && ( i + 1 < argc ))
161 /* Ok, next character is a blank */
162 strlcpy( NGIRCd_ConfFile, argv[i + 1], sizeof( NGIRCd_ConfFile ));
164 /* go to the following parameter */
165 i++; n = (LONG)strlen( argv[i] );
166 ok = TRUE;
169 if( argv[i][n] == 'n' )
171 NGIRCd_NoDaemon = TRUE;
172 ok = TRUE;
174 if( argv[i][n] == 'p' )
176 NGIRCd_Passive = TRUE;
177 ok = TRUE;
179 #ifdef SNIFFER
180 if( argv[i][n] == 's' )
182 NGIRCd_Sniffer = TRUE;
183 ok = TRUE;
185 #endif
186 if( argv[i][n] == 't' )
188 configtest = TRUE;
189 ok = TRUE;
192 if( ! ok )
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 );
201 if( ! ok )
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 );
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 )
217 NGIRCd_Debug = TRUE;
218 strcpy( NGIRCd_DebugLevel, "2" );
220 #endif
222 /* Soll nur die Konfigurations ueberprueft und ausgegeben werden? */
223 if( configtest )
225 Show_Version( ); puts( "" );
226 exit( Conf_Test( ));
229 while( ! NGIRCd_SignalQuit )
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 ));
235 NGIRCd_SignalRehash = FALSE;
236 NGIRCd_SignalRestart = FALSE;
237 NGIRCd_SignalQuit = FALSE;
239 /* Initialize modules, part I */
240 Log_Init( );
241 Conf_Init( );
243 if( Conf_Chroot[0] )
245 /* Chroot */
246 if( chdir( Conf_Chroot ) != 0 ) Log( LOG_ERR, "Can't chdir() in ChrootDir (%s): %s", Conf_Chroot, strerror( errno ));
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 );
252 if( Conf_GID != 0 )
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 ));
257 if( Conf_UID != 0 )
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 ));
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 )
269 /* Daemon im Hintergrund erzeugen */
270 pid = (LONG)fork( );
271 if( pid > 0 )
273 /* "alter" Prozess */
274 exit( 0 );
276 if( pid < 0 )
278 /* Fehler */
279 printf( "%s: Can't fork: %s!\nFatal error, exiting now ...\n", PACKAGE_NAME, strerror( errno ));
280 exit( 1 );
283 /* Child-Prozess initialisieren */
284 (VOID)setsid( );
285 chdir( "/" );
288 /* Show user, group, and PID of the running daemon */
289 pwd = getpwuid( getuid( )); grp = getgrgid( getgid( ));
290 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( ));
292 /* Change working directory to home directory of the user
293 * we are running as (when not running chroot()'ed!) */
294 if( Conf_UID != 0 && ! Conf_Chroot[0] )
296 struct passwd *pwd;
298 pwd = getpwuid( Conf_UID );
299 if( pwd != NULL )
301 if( chdir( pwd->pw_dir ) == 0 ) Log( LOG_DEBUG, "Changed working directory to \"%s\" ...", pwd->pw_dir );
302 else Log( LOG_ERR, "Can't change working directory to \"%s\": %s", pwd->pw_dir, strerror( errno ));
304 else Log( LOG_ERR, "Can't get user informaton for UID %d!?", Conf_UID );
307 /* Initialize modules, part II: these functions are eventually
308 * called with already dropped privileges ... */
309 Resolve_Init( );
310 Lists_Init( );
311 Channel_Init( );
312 Client_Init( );
313 #ifdef RENDEZVOUS
314 Rendezvous_Init( );
315 #endif
316 Conn_Init( );
318 /* Redirect stderr handle to "error file" for debugging.
319 * But don't try to write in the chroot jail, since it's more
320 * secure to have a chroot dir not writable by the daemon.
321 */
322 if( ! Conf_Chroot[0] ) Log_InitErrorfile( );
324 /* Signal-Handler initialisieren */
325 Initialize_Signal_Handler( );
327 /* Protokoll- und Server-Identifikation erzeugen. Die vom ngIRCd
328 * beim PASS-Befehl verwendete Syntax sowie die erweiterten Flags
329 * sind in doc/Protocol.txt beschrieben. */
330 #ifdef IRCPLUS
331 sprintf( NGIRCd_ProtoID, "%s%s %s|%s:%s", PROTOVER, PROTOIRCPLUS, PACKAGE_NAME, PACKAGE_VERSION, IRCPLUSFLAGS );
332 #ifdef ZLIB
333 strcat( NGIRCd_ProtoID, "Z" );
334 #endif
335 if( Conf_OperCanMode ) strcat( NGIRCd_ProtoID, "o" );
336 #else
337 sprintf( NGIRCd_ProtoID, "%s%s %s|%s", PROTOVER, PROTOIRC, PACKAGE_NAME, PACKAGE_VERSION );
338 #endif
339 strcat( NGIRCd_ProtoID, " P" );
340 #ifdef ZLIB
341 strcat( NGIRCd_ProtoID, "Z" );
342 #endif
343 Log( LOG_DEBUG, "Protocol and server ID is \"%s\".", NGIRCd_ProtoID );
345 /* Vordefinierte Channels anlegen */
346 Channel_InitPredefined( );
348 /* Listen-Ports initialisieren */
349 if( Conn_InitListeners( ) < 1 )
351 Log( LOG_ALERT, "Server isn't listening on a single port!" );
352 Log( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
353 exit( 1 );
356 /* Hauptschleife */
357 Conn_Handler( );
359 /* Alles abmelden */
360 Conn_Exit( );
361 #ifdef RENDEZVOUS
362 Rendezvous_Exit( );
363 #endif
364 Client_Exit( );
365 Channel_Exit( );
366 Lists_Exit( );
367 Log_Exit( );
370 return 0;
371 } /* main */
374 GLOBAL CHAR *
375 NGIRCd_Version( VOID )
377 STATIC CHAR version[126];
379 #ifdef CVSDATE
380 sprintf( version, "%s %s(%s)-%s", PACKAGE_NAME, PACKAGE_VERSION, CVSDATE, NGIRCd_VersionAddition( ));
381 #else
382 sprintf( version, "%s %s-%s", PACKAGE_NAME, PACKAGE_VERSION, NGIRCd_VersionAddition( ));
383 #endif
384 return version;
385 } /* NGIRCd_Version */
388 GLOBAL CHAR *
389 NGIRCd_VersionAddition( VOID )
391 STATIC CHAR txt[200];
393 strcpy( txt, "" );
395 #ifdef SYSLOG
396 if( txt[0] ) strcat( txt, "+" );
397 strcat( txt, "SYSLOG" );
398 #endif
399 #ifdef ZLIB
400 if( txt[0] ) strcat( txt, "+" );
401 strcat( txt, "ZLIB" );
402 #endif
403 #ifdef TCPWRAP
404 if( txt[0] ) strcat( txt, "+" );
405 strcat( txt, "TCPWRAP" );
406 #endif
407 #ifdef RENDEZVOUS
408 if( txt[0] ) strcat( txt, "+" );
409 strcat( txt, "RENDEZVOUS" );
410 #endif
411 #ifdef IDENTAUTH
412 if( txt[0] ) strcat( txt, "+" );
413 strcat( txt, "IDENT" );
414 #endif
415 #ifdef DEBUG
416 if( txt[0] ) strcat( txt, "+" );
417 strcat( txt, "DEBUG" );
418 #endif
419 #ifdef SNIFFER
420 if( txt[0] ) strcat( txt, "+" );
421 strcat( txt, "SNIFFER" );
422 #endif
423 #ifdef STRICT_RFC
424 if( txt[0] ) strcat( txt, "+" );
425 strcat( txt, "RFC" );
426 #endif
427 #ifdef IRCPLUS
428 if( txt[0] ) strcat( txt, "+" );
429 strcat( txt, "IRCPLUS" );
430 #endif
432 if( txt[0] ) strlcat( txt, "-", sizeof( txt ));
433 strlcat( txt, TARGET_CPU, sizeof( txt ));
434 strlcat( txt, "/", sizeof( txt ));
435 strlcat( txt, TARGET_VENDOR, sizeof( txt ));
436 strlcat( txt, "/", sizeof( txt ));
437 strlcat( txt, TARGET_OS, sizeof( txt ));
439 return txt;
440 } /* NGIRCd_VersionAddition */
443 GLOBAL VOID
444 NGIRCd_Rehash( VOID )
446 CHAR old_name[CLIENT_ID_LEN];
448 Log( LOG_NOTICE|LOG_snotice, "Re-reading configuration NOW!" );
449 NGIRCd_SignalRehash = FALSE;
451 /* Close down all listening sockets */
452 Conn_ExitListeners( );
454 /* Remember old server name */
455 strcpy( old_name, Conf_ServerName );
457 /* Re-read configuration ... */
458 Conf_Rehash( );
460 /* Recover old server name: it can't be changed during run-time */
461 if( strcmp( old_name, Conf_ServerName ) != 0 )
463 strcpy( Conf_ServerName, old_name );
464 Log( LOG_ERR, "Can't change \"ServerName\" on runtime! Ignored new name." );
467 /* Create new pre-defined channels */
468 Channel_InitPredefined( );
470 /* Start listening on sockets */
471 Conn_InitListeners( );
473 /* Sync configuration with established connections */
474 Conn_SyncServerStruct( );
476 Log( LOG_NOTICE|LOG_snotice, "Re-reading of configuration done." );
477 } /* NGIRCd_Rehash */
480 LOCAL VOID
481 Initialize_Signal_Handler( VOID )
483 /* Signal-Handler initialisieren: einige Signale
484 * werden ignoriert, andere speziell behandelt. */
486 #ifdef HAVE_SIGACTION
487 /* sigaction() ist vorhanden */
489 struct sigaction saction;
491 /* Signal-Struktur initialisieren */
492 memset( &saction, 0, sizeof( saction ));
493 saction.sa_handler = Signal_Handler;
494 #ifdef SA_RESTART
495 saction.sa_flags |= SA_RESTART;
496 #endif
497 #ifdef SA_NOCLDWAIT
498 saction.sa_flags |= SA_NOCLDWAIT;
499 #endif
501 /* Signal-Handler einhaengen */
502 sigaction( SIGINT, &saction, NULL );
503 sigaction( SIGQUIT, &saction, NULL );
504 sigaction( SIGTERM, &saction, NULL);
505 sigaction( SIGHUP, &saction, NULL);
506 sigaction( SIGCHLD, &saction, NULL);
508 /* einige Signale ignorieren */
509 saction.sa_handler = SIG_IGN;
510 sigaction( SIGPIPE, &saction, NULL );
511 #else
512 /* kein sigaction() vorhanden */
514 /* Signal-Handler einhaengen */
515 signal( SIGINT, Signal_Handler );
516 signal( SIGQUIT, Signal_Handler );
517 signal( SIGTERM, Signal_Handler );
518 signal( SIGHUP, Signal_Handler );
519 signal( SIGCHLD, Signal_Handler );
521 /* einige Signale ignorieren */
522 signal( SIGPIPE, SIG_IGN );
523 #endif
524 } /* Initialize_Signal_Handler */
527 LOCAL VOID
528 Signal_Handler( INT Signal )
530 /* Signal-Handler. Dieser wird aufgerufen, wenn eines der Signale eintrifft,
531 * fuer das wir uns registriert haben (vgl. Initialize_Signal_Handler). Die
532 * Nummer des eingetroffenen Signals wird der Funktion uebergeben. */
534 switch( Signal )
536 case SIGTERM:
537 case SIGINT:
538 case SIGQUIT:
539 /* wir soll(t)en uns wohl beenden ... */
540 NGIRCd_SignalQuit = TRUE;
541 break;
542 case SIGHUP:
543 /* Konfiguration neu einlesen: */
544 NGIRCd_SignalRehash = TRUE;
545 break;
546 case SIGCHLD:
547 /* Child-Prozess wurde beendet. Zombies vermeiden: */
548 while( waitpid( -1, NULL, WNOHANG ) > 0);
549 break;
550 #ifdef DEBUG
551 default:
552 /* unbekanntes bzw. unbehandeltes Signal */
553 Log( LOG_DEBUG, "Got signal %d! Ignored.", Signal );
554 #endif
556 } /* Signal_Handler */
559 LOCAL VOID
560 Show_Version( VOID )
562 puts( NGIRCd_Version( ));
563 puts( "Copyright (c)2001-2004 by Alexander Barton (<alex@barton.de>)." );
564 puts( "Homepage: <http://arthur.ath.cx/~alex/ngircd/>\n" );
565 puts( "This is free software; see the source for copying conditions. There is NO" );
566 puts( "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE." );
567 } /* Show_Version */
570 LOCAL VOID
571 Show_Help( VOID )
573 #ifdef DEBUG
574 puts( " -d, --debug log extra debug messages" );
575 #endif
576 puts( " -f, --config <f> use file <f> as configuration file" );
577 puts( " -n, --nodaemon don't fork and don't detach from controlling terminal" );
578 puts( " -p, --passive disable automatic connections to other servers" );
579 #ifdef SNIFFER
580 puts( " -s, --sniffer enable network sniffer and display all IRC traffic" );
581 #endif
582 puts( " -t, --configtest read, validate and display configuration; then exit" );
583 puts( " --version output version information and exit" );
584 puts( " --help display this help and exit" );
585 } /* Show_Help */
588 /* -eof- */