]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/ngircd.c
New configuration variable "PidFile", section "[Global]": if defined,
[ngircd-alex.git] / src / ngircd / ngircd.c
1 /*
2  * ngIRCd -- The Next Generation IRC Daemon
3  * Copyright (c)2001-2005 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.88 2005/02/04 14:24:21 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 <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>
33
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"
45
46 #ifdef RENDEZVOUS
47 #include "rendezvous.h"
48 #endif
49
50 #include "exp.h"
51 #include "ngircd.h"
52
53
54 LOCAL VOID Initialize_Signal_Handler PARAMS(( VOID ));
55 LOCAL VOID Signal_Handler PARAMS(( INT Signal ));
56
57 LOCAL VOID Show_Version PARAMS(( VOID ));
58 LOCAL VOID Show_Help PARAMS(( VOID ));
59
60 LOCAL VOID Pidfile_Create PARAMS(( LONG ));
61 LOCAL VOID Pidfile_Delete PARAMS(( VOID ));
62
63
64 GLOBAL int
65 main( int argc, const char *argv[] )
66 {
67         struct passwd *pwd;
68         struct group *grp;
69         BOOLEAN ok, configtest = FALSE;
70         LONG pid, n;
71         INT i;
72
73         umask( 0077 );
74
75         NGIRCd_SignalQuit = NGIRCd_SignalRestart = NGIRCd_SignalRehash = FALSE;
76         NGIRCd_NoDaemon = NGIRCd_Passive = FALSE;
77 #ifdef DEBUG
78         NGIRCd_Debug = FALSE;
79 #endif
80 #ifdef SNIFFER
81         NGIRCd_Sniffer = FALSE;
82 #endif
83         strlcpy( NGIRCd_ConfFile, SYSCONFDIR, sizeof( NGIRCd_ConfFile ));
84         strlcat( NGIRCd_ConfFile, CONFIG_FILE, sizeof( NGIRCd_ConfFile ));
85
86         /* Kommandozeile parsen */
87         for( i = 1; i < argc; i++ )
88         {
89                 ok = FALSE;
90                 if(( argv[i][0] == '-' ) && ( argv[i][1] == '-' ))
91                 {
92                         /* Lange Option */
93
94                         if( strcmp( argv[i], "--config" ) == 0 )
95                         {
96                                 if( i + 1 < argc )
97                                 {
98                                         /* Ok, there's an parameter left */
99                                         strlcpy( NGIRCd_ConfFile, argv[i + 1], sizeof( NGIRCd_ConfFile ));
100
101                                         /* next parameter */
102                                         i++; ok = TRUE;
103                                 }
104                         }
105                         if( strcmp( argv[i], "--configtest" ) == 0 )
106                         {
107                                 configtest = TRUE;
108                                 ok = TRUE;
109                         }
110 #ifdef DEBUG
111                         if( strcmp( argv[i], "--debug" ) == 0 )
112                         {
113                                 NGIRCd_Debug = TRUE;
114                                 ok = TRUE;
115                         }
116 #endif
117                         if( strcmp( argv[i], "--help" ) == 0 )
118                         {
119                                 Show_Version( );
120                                 puts( "" ); Show_Help( ); puts( "" );
121                                 exit( 1 );
122                         }
123                         if( strcmp( argv[i], "--nodaemon" ) == 0 )
124                         {
125                                 NGIRCd_NoDaemon = TRUE;
126                                 ok = TRUE;
127                         }
128                         if( strcmp( argv[i], "--passive" ) == 0 )
129                         {
130                                 NGIRCd_Passive = TRUE;
131                                 ok = TRUE;
132                         }
133 #ifdef SNIFFER
134                         if( strcmp( argv[i], "--sniffer" ) == 0 )
135                         {
136                                 NGIRCd_Sniffer = TRUE;
137                                 ok = TRUE;
138                         }
139 #endif
140                         if( strcmp( argv[i], "--version" ) == 0 )
141                         {
142                                 Show_Version( );
143                                 exit( 1 );
144                         }
145                 }
146                 else if(( argv[i][0] == '-' ) && ( argv[i][1] != '-' ))
147                 {
148                         /* Kurze Option */
149                         
150                         for( n = 1; n < (LONG)strlen( argv[i] ); n++ )
151                         {
152                                 ok = FALSE;
153 #ifdef DEBUG
154                                 if( argv[i][n] == 'd' )
155                                 {
156                                         NGIRCd_Debug = TRUE;
157                                         ok = TRUE;
158                                 }
159 #endif
160                                 if( argv[i][n] == 'f' )
161                                 {
162                                         if(( ! argv[i][n + 1] ) && ( i + 1 < argc ))
163                                         {
164                                                 /* Ok, next character is a blank */
165                                                 strlcpy( NGIRCd_ConfFile, argv[i + 1], sizeof( NGIRCd_ConfFile ));
166
167                                                 /* go to the following parameter */
168                                                 i++; n = (LONG)strlen( argv[i] );
169                                                 ok = TRUE;
170                                         }
171                                 }
172                                 if( argv[i][n] == 'n' )
173                                 {
174                                         NGIRCd_NoDaemon = TRUE;
175                                         ok = TRUE;
176                                 }
177                                 if( argv[i][n] == 'p' )
178                                 {
179                                         NGIRCd_Passive = TRUE;
180                                         ok = TRUE;
181                                 }
182 #ifdef SNIFFER
183                                 if( argv[i][n] == 's' )
184                                 {
185                                         NGIRCd_Sniffer = TRUE;
186                                         ok = TRUE;
187                                 }
188 #endif
189                                 if( argv[i][n] == 't' )
190                                 {
191                                         configtest = TRUE;
192                                         ok = TRUE;
193                                 }
194
195                                 if( ! ok )
196                                 {
197                                         printf( "%s: invalid option \"-%c\"!\n", PACKAGE_NAME, argv[i][n] );
198                                         printf( "Try \"%s --help\" for more information.\n", PACKAGE_NAME );
199                                         exit( 1 );
200                                 }
201                         }
202
203                 }
204                 if( ! ok )
205                 {
206                         printf( "%s: invalid option \"%s\"!\n", PACKAGE_NAME, argv[i] );
207                         printf( "Try \"%s --help\" for more information.\n", PACKAGE_NAME );
208                         exit( 1 );
209                 }
210         }
211
212         /* Debug-Level (fuer IRC-Befehl "VERSION") ermitteln */
213         strcpy( NGIRCd_DebugLevel, "" );
214 #ifdef DEBUG
215         if( NGIRCd_Debug ) strcpy( NGIRCd_DebugLevel, "1" );
216 #endif
217 #ifdef SNIFFER
218         if( NGIRCd_Sniffer )
219         {
220                 NGIRCd_Debug = TRUE;
221                 strcpy( NGIRCd_DebugLevel, "2" );
222         }
223 #endif
224
225         /* Soll nur die Konfigurations ueberprueft und ausgegeben werden? */
226         if( configtest )
227         {
228                 Show_Version( ); puts( "" );
229                 exit( Conf_Test( ));
230         }
231         
232         while( ! NGIRCd_SignalQuit )
233         {
234                 /* Initialize global variables */
235                 NGIRCd_Start = time( NULL );
236                 (VOID)strftime( NGIRCd_StartStr, 64, "%a %b %d %Y at %H:%M:%S (%Z)", localtime( &NGIRCd_Start ));
237
238                 NGIRCd_SignalRehash = FALSE;
239                 NGIRCd_SignalRestart = FALSE;
240                 NGIRCd_SignalQuit = FALSE;
241
242                 /* Initialize modules, part I */
243                 Log_Init( );
244                 Conf_Init( );
245
246                 if( Conf_Chroot[0] )
247                 {
248                         /* Chroot */
249                         if( chdir( Conf_Chroot ) != 0 ) Log( LOG_ERR, "Can't chdir() in ChrootDir (%s): %s", Conf_Chroot, strerror( errno ));
250
251                         if( chroot( Conf_Chroot ) != 0 ) Log( LOG_ERR, "Can't change root directory to \"%s\": %s", Conf_Chroot, strerror( errno ));
252                         else Log( LOG_INFO, "Changed root and working directory to \"%s\".", Conf_Chroot );
253                 }
254
255                 if( Conf_GID != 0 )
256                 {
257                         /* Set new group ID */
258                         if( setgid( Conf_GID ) != 0 ) Log( LOG_ERR, "Can't change group ID to %u: %s", Conf_GID, strerror( errno ));
259                 }
260                 if( Conf_UID != 0 )
261                 {
262                         /* Set new user ID */
263                         if( setuid( Conf_UID ) != 0 ) Log( LOG_ERR, "Can't change user ID to %u: %s", Conf_UID, strerror( errno ));
264                 }
265
266                 /* In der Regel wird ein Sub-Prozess ge-fork()'t, der
267                  * nicht mehr mit dem Terminal verbunden ist. Mit der
268                  * Option "--nodaemon" kann dies (z.B. zum Debuggen)
269                  * verhindert werden. */
270                 if( ! NGIRCd_NoDaemon )
271                 {
272                         /* Daemon im Hintergrund erzeugen */
273                         pid = (LONG)fork( );
274                         if( pid > 0 )
275                         {
276                                 /* "alter" Prozess */
277                                 exit( 0 );
278                         }
279                         if( pid < 0 )
280                         {
281                                 /* Fehler */
282                                 printf( "%s: Can't fork: %s!\nFatal error, exiting now ...\n", PACKAGE_NAME, strerror( errno ));
283                                 exit( 1 );
284                         }
285
286                         /* Child-Prozess initialisieren */
287                         (VOID)setsid( );
288                         chdir( "/" );
289                 }
290
291                 /* Create PID file */
292                 pid = (LONG) getpid( );
293                 Pidfile_Create( pid );
294
295                 /* Show user, group, and PID of the running daemon */
296                 pwd = getpwuid( getuid( )); grp = getgrgid( getgid( ));
297                 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( ), pid);
298
299                 /* Change working directory to home directory of the user
300                  * we are running as (when not running chroot()'ed!) */
301                 if( Conf_UID != 0 && ! Conf_Chroot[0] )
302                 {
303                         struct passwd *pwd;
304
305                         pwd = getpwuid( Conf_UID );
306                         if( pwd != NULL )
307                         {
308                                 if( chdir( pwd->pw_dir ) == 0 ) Log( LOG_DEBUG, "Changed working directory to \"%s\" ...", pwd->pw_dir );
309                                 else Log( LOG_ERR, "Can't change working directory to \"%s\": %s", pwd->pw_dir, strerror( errno ));
310                         }
311                         else Log( LOG_ERR, "Can't get user informaton for UID %d!?", Conf_UID );
312                 }
313
314                 /* Initialize modules, part II: these functions are eventually
315                  * called with already dropped privileges ... */
316                 Resolve_Init( );
317                 Lists_Init( );
318                 Channel_Init( );
319                 Client_Init( );
320 #ifdef RENDEZVOUS
321                 Rendezvous_Init( );
322 #endif
323                 Conn_Init( );
324
325                 /* Redirect stderr handle to "error file" for debugging.
326                  * But don't try to write in the chroot jail, since it's more 
327                  * secure to have a chroot dir not writable by the daemon.
328                  */
329                 if( ! Conf_Chroot[0] ) Log_InitErrorfile( );
330
331                 /* Signal-Handler initialisieren */
332                 Initialize_Signal_Handler( );
333
334                 /* Protokoll- und Server-Identifikation erzeugen. Die vom ngIRCd
335                  * beim PASS-Befehl verwendete Syntax sowie die erweiterten Flags
336                  * sind in doc/Protocol.txt beschrieben. */
337 #ifdef IRCPLUS
338                 sprintf( NGIRCd_ProtoID, "%s%s %s|%s:%s", PROTOVER, PROTOIRCPLUS, PACKAGE_NAME, PACKAGE_VERSION, IRCPLUSFLAGS );
339 #ifdef ZLIB
340                 strcat( NGIRCd_ProtoID, "Z" );
341 #endif
342                 if( Conf_OperCanMode ) strcat( NGIRCd_ProtoID, "o" );
343 #else
344                 sprintf( NGIRCd_ProtoID, "%s%s %s|%s", PROTOVER, PROTOIRC, PACKAGE_NAME, PACKAGE_VERSION );
345 #endif
346                 strcat( NGIRCd_ProtoID, " P" );
347 #ifdef ZLIB
348                 strcat( NGIRCd_ProtoID, "Z" );
349 #endif
350                 Log( LOG_DEBUG, "Protocol and server ID is \"%s\".", NGIRCd_ProtoID );
351
352                 /* Vordefinierte Channels anlegen */
353                 Channel_InitPredefined( );
354
355                 /* Listen-Ports initialisieren */
356                 if( Conn_InitListeners( ) < 1 )
357                 {
358                         Log( LOG_ALERT, "Server isn't listening on a single port!" );
359                         Log( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
360                         Pidfile_Delete( );
361                         exit( 1 );
362                 }
363                 
364                 /* Hauptschleife */
365                 Conn_Handler( );
366
367                 /* Alles abmelden */
368                 Conn_Exit( );
369 #ifdef RENDEZVOUS
370                 Rendezvous_Exit( );
371 #endif
372                 Client_Exit( );
373                 Channel_Exit( );
374                 Lists_Exit( );
375                 Log_Exit( );
376
377                 Pidfile_Delete( );
378         }
379
380         return 0;
381 } /* main */
382
383
384 GLOBAL CHAR *
385 NGIRCd_Version( VOID )
386 {
387         STATIC CHAR version[126];
388         
389 #ifdef CVSDATE
390         sprintf( version, "%s %s(%s)-%s", PACKAGE_NAME, PACKAGE_VERSION, CVSDATE, NGIRCd_VersionAddition( ));
391 #else
392         sprintf( version, "%s %s-%s", PACKAGE_NAME, PACKAGE_VERSION, NGIRCd_VersionAddition( ));
393 #endif
394         return version;
395 } /* NGIRCd_Version */
396
397
398 GLOBAL CHAR *
399 NGIRCd_VersionAddition( VOID )
400 {
401         STATIC CHAR txt[200];
402
403         strcpy( txt, "" );
404
405 #ifdef SYSLOG
406         if( txt[0] ) strcat( txt, "+" );
407         strcat( txt, "SYSLOG" );
408 #endif
409 #ifdef ZLIB
410         if( txt[0] ) strcat( txt, "+" );
411         strcat( txt, "ZLIB" );
412 #endif
413 #ifdef TCPWRAP
414         if( txt[0] ) strcat( txt, "+" );
415         strcat( txt, "TCPWRAP" );
416 #endif
417 #ifdef RENDEZVOUS
418         if( txt[0] ) strcat( txt, "+" );
419         strcat( txt, "RENDEZVOUS" );
420 #endif
421 #ifdef IDENTAUTH
422         if( txt[0] ) strcat( txt, "+" );
423         strcat( txt, "IDENT" );
424 #endif
425 #ifdef DEBUG
426         if( txt[0] ) strcat( txt, "+" );
427         strcat( txt, "DEBUG" );
428 #endif
429 #ifdef SNIFFER
430         if( txt[0] ) strcat( txt, "+" );
431         strcat( txt, "SNIFFER" );
432 #endif
433 #ifdef STRICT_RFC
434         if( txt[0] ) strcat( txt, "+" );
435         strcat( txt, "RFC" );
436 #endif
437 #ifdef IRCPLUS
438         if( txt[0] ) strcat( txt, "+" );
439         strcat( txt, "IRCPLUS" );
440 #endif
441         
442         if( txt[0] ) strlcat( txt, "-", sizeof( txt ));
443         strlcat( txt, TARGET_CPU, sizeof( txt ));
444         strlcat( txt, "/", sizeof( txt ));
445         strlcat( txt, TARGET_VENDOR, sizeof( txt ));
446         strlcat( txt, "/", sizeof( txt ));
447         strlcat( txt, TARGET_OS, sizeof( txt ));
448
449         return txt;
450 } /* NGIRCd_VersionAddition */
451
452
453 GLOBAL VOID
454 NGIRCd_Rehash( VOID )
455 {
456         CHAR old_name[CLIENT_ID_LEN];
457
458         Log( LOG_NOTICE|LOG_snotice, "Re-reading configuration NOW!" );
459         NGIRCd_SignalRehash = FALSE;
460
461         /* Close down all listening sockets */
462         Conn_ExitListeners( );
463
464         /* Remember old server name */
465         strcpy( old_name, Conf_ServerName );
466
467         /* Re-read configuration ... */
468         Conf_Rehash( );
469
470         /* Recover old server name: it can't be changed during run-time */
471         if( strcmp( old_name, Conf_ServerName ) != 0 )
472         {
473                 strcpy( Conf_ServerName, old_name );
474                 Log( LOG_ERR, "Can't change \"ServerName\" on runtime! Ignored new name." );
475         }
476
477         /* Create new pre-defined channels */
478         Channel_InitPredefined( );
479         
480         /* Start listening on sockets */
481         Conn_InitListeners( );
482
483         /* Sync configuration with established connections */
484         Conn_SyncServerStruct( );
485
486         Log( LOG_NOTICE|LOG_snotice, "Re-reading of configuration done." );
487 } /* NGIRCd_Rehash */
488
489
490 LOCAL VOID
491 Initialize_Signal_Handler( VOID )
492 {
493         /* Signal-Handler initialisieren: einige Signale
494          * werden ignoriert, andere speziell behandelt. */
495
496 #ifdef HAVE_SIGACTION
497         /* sigaction() ist vorhanden */
498
499         struct sigaction saction;
500
501         /* Signal-Struktur initialisieren */
502         memset( &saction, 0, sizeof( saction ));
503         saction.sa_handler = Signal_Handler;
504 #ifdef SA_RESTART
505         saction.sa_flags |= SA_RESTART;
506 #endif
507 #ifdef SA_NOCLDWAIT
508         saction.sa_flags |= SA_NOCLDWAIT;
509 #endif
510
511         /* Signal-Handler einhaengen */
512         sigaction( SIGINT, &saction, NULL );
513         sigaction( SIGQUIT, &saction, NULL );
514         sigaction( SIGTERM, &saction, NULL);
515         sigaction( SIGHUP, &saction, NULL);
516         sigaction( SIGCHLD, &saction, NULL);
517
518         /* einige Signale ignorieren */
519         saction.sa_handler = SIG_IGN;
520         sigaction( SIGPIPE, &saction, NULL );
521 #else
522         /* kein sigaction() vorhanden */
523
524         /* Signal-Handler einhaengen */
525         signal( SIGINT, Signal_Handler );
526         signal( SIGQUIT, Signal_Handler );
527         signal( SIGTERM, Signal_Handler );
528         signal( SIGHUP, Signal_Handler );
529         signal( SIGCHLD, Signal_Handler );
530
531         /* einige Signale ignorieren */
532         signal( SIGPIPE, SIG_IGN );
533 #endif
534 } /* Initialize_Signal_Handler */
535
536
537 LOCAL VOID
538 Signal_Handler( INT Signal )
539 {
540         /* Signal-Handler. Dieser wird aufgerufen, wenn eines der Signale eintrifft,
541          * fuer das wir uns registriert haben (vgl. Initialize_Signal_Handler). Die
542          * Nummer des eingetroffenen Signals wird der Funktion uebergeben. */
543
544         switch( Signal )
545         {
546                 case SIGTERM:
547                 case SIGINT:
548                 case SIGQUIT:
549                         /* wir soll(t)en uns wohl beenden ... */
550                         NGIRCd_SignalQuit = TRUE;
551                         break;
552                 case SIGHUP:
553                         /* Konfiguration neu einlesen: */
554                         NGIRCd_SignalRehash = TRUE;
555                         break;
556                 case SIGCHLD:
557                         /* Child-Prozess wurde beendet. Zombies vermeiden: */
558                         while( waitpid( -1, NULL, WNOHANG ) > 0);
559                         break;
560 #ifdef DEBUG
561                 default:
562                         /* unbekanntes bzw. unbehandeltes Signal */
563                         Log( LOG_DEBUG, "Got signal %d! Ignored.", Signal );
564 #endif
565         }
566 } /* Signal_Handler */
567
568
569 LOCAL VOID
570 Show_Version( VOID )
571 {
572         puts( NGIRCd_Version( ));
573         puts( "Copyright (c)2001-2005 by Alexander Barton (<alex@barton.de>)." );
574         puts( "Homepage: <http://arthur.ath.cx/~alex/ngircd/>\n" );
575         puts( "This is free software; see the source for copying conditions. There is NO" );
576         puts( "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE." );
577 } /* Show_Version */
578
579
580 LOCAL VOID
581 Show_Help( VOID )
582 {
583 #ifdef DEBUG
584         puts( "  -d, --debug        log extra debug messages" );
585 #endif
586         puts( "  -f, --config <f>   use file <f> as configuration file" );
587         puts( "  -n, --nodaemon     don't fork and don't detach from controlling terminal" );
588         puts( "  -p, --passive      disable automatic connections to other servers" );
589 #ifdef SNIFFER
590         puts( "  -s, --sniffer      enable network sniffer and display all IRC traffic" );
591 #endif
592         puts( "  -t, --configtest   read, validate and display configuration; then exit" );
593         puts( "      --version      output version information and exit" );
594         puts( "      --help         display this help and exit" );
595 } /* Show_Help */
596
597
598 LOCAL VOID
599 Pidfile_Delete( VOID )
600 {
601         /* Pidfile configured? */
602         if( ! Conf_PidFile[0] ) return;
603
604 #ifdef DEBUG
605         Log( LOG_DEBUG, "Removing PID file (%s) ...", Conf_PidFile );
606 #endif
607
608         if( unlink( Conf_PidFile ))
609                 Log( LOG_ERR, "Error unlinking PID file (%s): %s", Conf_PidFile, strerror( errno ));
610 } /* Pidfile_Delete */
611
612
613 LOCAL VOID
614 Pidfile_Create( LONG pid )
615 {
616         FILE *pidf;
617
618         /* Pidfile configured? */
619         if( ! Conf_PidFile[0] ) return;
620
621         pidf = fopen( Conf_PidFile, "w" );
622
623 #ifdef DEBUG
624         Log( LOG_DEBUG, "Creating PID file (%s) ...", Conf_PidFile );
625 #endif
626
627         if( ! pidf )
628         {
629                 Log( LOG_ERR, "Error writing PID file (%s): %s", Conf_PidFile, strerror( errno ));
630                 return;
631         }
632
633         if( fprintf( pidf, "%ld\n", pid ) < 0 )
634                 Log( LOG_ERR, "Can't write PID file (%s): %s", Conf_PidFile, strerror( errno ));
635
636         if( fclose(pidf) != 0 )
637                 Log( LOG_ERR, "Error closing PID file (%s): %s", Conf_PidFile, strerror( errno ));
638 } /* Pidfile_Create */
639
640
641 /* -eof- */