]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/ngircd.c
- der Daemon kann nun seine UID und GID wechseln.
[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  * Dieses Programm ist freie Software. Sie koennen es unter den Bedingungen
6  * der GNU General Public License (GPL), wie von der Free Software Foundation
7  * herausgegeben, weitergeben und/oder modifizieren, entweder unter Version 2
8  * der Lizenz oder (wenn Sie es wuenschen) jeder spaeteren Version.
9  * Naehere Informationen entnehmen Sie bitter der Datei COPYING. Eine Liste
10  * der an ngIRCd beteiligten Autoren finden Sie in der Datei AUTHORS.
11  *
12  * $Id: ngircd.c,v 1.39 2002/03/29 22:56:40 alex Exp $
13  *
14  * ngircd.c: Hier beginnt alles ;-)
15  */
16
17
18 #include "portab.h"
19
20 #include "imp.h"
21 #include <assert.h>
22 #include <errno.h>
23 #include <stdio.h>
24 #include <signal.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <sys/types.h>
28 #include <sys/wait.h>
29 #include <time.h>
30
31 #include "channel.h"
32 #include "client.h"
33 #include "conf.h"
34 #include "conn.h"
35 #include "defines.h"
36 #include "irc.h"
37 #include "log.h"
38 #include "parse.h"
39
40 #include "exp.h"
41 #include "ngircd.h"
42
43
44 LOCAL VOID Initialize_Signal_Handler( VOID );
45 LOCAL VOID Signal_Handler( INT Signal );
46
47 LOCAL VOID Initialize_Listen_Ports( VOID );
48
49 LOCAL VOID Show_Version( VOID );
50 LOCAL VOID Show_Help( VOID );
51
52
53 GLOBAL int main( int argc, const char *argv[] )
54 {
55         BOOLEAN ok, configtest = FALSE;
56         INT32 pid, n;
57         INT i;
58
59         NGIRCd_Restart = FALSE;
60         NGIRCd_Quit = FALSE;
61         NGIRCd_NoDaemon = FALSE;
62         NGIRCd_Passive = FALSE;
63 #ifdef DEBUG
64         NGIRCd_Debug = FALSE;
65 #endif
66 #ifdef SNIFFER
67         NGIRCd_Sniffer = FALSE;
68 #endif
69         strcpy( NGIRCd_ConfFile, CONFIG_FILE );
70
71         /* Kommandozeile parsen */
72         for( i = 1; i < argc; i++ )
73         {
74                 ok = FALSE;
75                 if(( argv[i][0] == '-' ) && ( argv[i][1] == '-' ))
76                 {
77                         /* Lange Option */
78
79                         if( strcmp( argv[i], "--config" ) == 0 )
80                         {
81                                 if( i + 1 < argc )
82                                 {
83                                         /* Ok, danach kommt noch ein Parameter */
84                                         strncpy( NGIRCd_ConfFile, argv[i + 1], FNAME_LEN - 1 );
85                                         NGIRCd_ConfFile[FNAME_LEN - 1] = '\0';
86
87                                         /* zum uebernaechsten Parameter */
88                                         i++; ok = TRUE;
89                                 }
90                         }
91                         if( strcmp( argv[i], "--configtest" ) == 0 )
92                         {
93                                 configtest = TRUE;
94                                 ok = TRUE;
95                         }
96 #ifdef DEBUG
97                         if( strcmp( argv[i], "--debug" ) == 0 )
98                         {
99                                 NGIRCd_Debug = TRUE;
100                                 ok = TRUE;
101                         }
102 #endif
103                         if( strcmp( argv[i], "--help" ) == 0 )
104                         {
105                                 Show_Version( );
106                                 puts( "" ); Show_Help( ); puts( "" );
107                                 exit( 1 );
108                         }
109                         if( strcmp( argv[i], "--nodaemon" ) == 0 )
110                         {
111                                 NGIRCd_NoDaemon = TRUE;
112                                 ok = TRUE;
113                         }
114                         if( strcmp( argv[i], "--passive" ) == 0 )
115                         {
116                                 NGIRCd_Passive = TRUE;
117                                 ok = TRUE;
118                         }
119 #ifdef SNIFFER
120                         if( strcmp( argv[i], "--sniffer" ) == 0 )
121                         {
122                                 NGIRCd_Sniffer = TRUE;
123                                 ok = TRUE;
124                         }
125 #endif
126                         if( strcmp( argv[i], "--version" ) == 0 )
127                         {
128                                 Show_Version( );
129                                 exit( 1 );
130                         }
131                 }
132                 else if(( argv[i][0] == '-' ) && ( argv[i][1] != '-' ))
133                 {
134                         /* Kurze Option */
135                         
136                         for( n = 1; n < (INT32)strlen( argv[i] ); n++ )
137                         {
138                                 ok = FALSE;
139 #ifdef DEBUG
140                                 if( argv[i][n] == 'd' )
141                                 {
142                                         NGIRCd_Debug = TRUE;
143                                         ok = TRUE;
144                                 }
145 #endif
146                                 if( argv[i][n] == 'f' )
147                                 {
148                                         if(( ! argv[i][n+i] ) && ( i + 1 < argc ))
149                                         {
150                                                 /* Ok, danach kommt ein Leerzeichen */
151                                                 strncpy( NGIRCd_ConfFile, argv[i + 1], FNAME_LEN - 1 );
152                                                 NGIRCd_ConfFile[FNAME_LEN - 1] = '\0';
153
154                                                 /* zum uebernaechsten Parameter */
155                                                 i++; n = strlen( argv[i] );
156                                                 ok = TRUE;
157                                         }
158                                 }
159                                 if( argv[i][n] == 'n' )
160                                 {
161                                         NGIRCd_NoDaemon = TRUE;
162                                         ok = TRUE;
163                                 }
164                                 if( argv[i][n] == 'p' )
165                                 {
166                                         NGIRCd_Passive = TRUE;
167                                         ok = TRUE;
168                                 }
169 #ifdef SNIFFER
170                                 if( argv[i][n] == 's' )
171                                 {
172                                         NGIRCd_Sniffer = TRUE;
173                                         ok = TRUE;
174                                 }
175 #endif
176
177                                 if( ! ok )
178                                 {
179                                         printf( PACKAGE": invalid option \"-%c\"!\n", argv[i][n] );
180                                         puts( "Try \""PACKAGE" --help\" for more information." );
181                                         exit( 1 );
182                                 }
183                         }
184
185                 }
186                 if( ! ok )
187                 {
188                         printf( PACKAGE": invalid option \"%s\"!\n", argv[i] );
189                         puts( "Try \""PACKAGE" --help\" for more information." );
190                         exit( 1 );
191                 }
192         }
193
194         /* Debug-Level (fuer IRC-Befehl "VERSION") ermitteln */
195         strcpy( NGIRCd_DebugLevel, "" );
196 #ifdef DEBUG
197         if( NGIRCd_Debug ) strcpy( NGIRCd_DebugLevel, "1" );
198 #endif
199 #ifdef SNIFFER
200         if( NGIRCd_Sniffer ) strcpy( NGIRCd_DebugLevel, "2" );
201 #endif
202
203         /* Soll nur die Konfigurations ueberprueft und ausgegeben werden? */
204         if( configtest )
205         {
206                 Show_Version( ); puts( "" );
207                 exit( Conf_Test( ));
208         }
209         
210         while( ! NGIRCd_Quit )
211         {
212                 /* In der Regel wird ein Sub-Prozess ge-fork()'t, der
213                  * nicht mehr mit dem Terminal verbunden ist. Mit der
214                  * Option "--nodaemon" kann dies (z.B. zum Debuggen)
215                  * verhindert werden. */
216                 if( ! NGIRCd_NoDaemon )
217                 {
218                         /* Daemon im Hintergrund erzeugen */
219                         pid = (INT32)fork( );
220                         if( pid > 0 )
221                         {
222                                 /* "alter" Prozess */
223                                 exit( 0 );
224                         }
225                         if( pid < 0 )
226                         {
227                                 /* Fehler */
228                                 printf( PACKAGE": Can't fork: %s!\nFatal error, exiting now ...", strerror( errno ));
229                                 exit( 1 );
230                         }
231
232                         /* Child-Prozess initialisieren */
233                         (VOID)setsid( );
234                         chdir( "/" );
235                 }
236         
237                 /* Globale Variablen initialisieren */
238                 NGIRCd_Start = time( NULL );
239                 (VOID)strftime( NGIRCd_StartStr, 64, "%a %b %d %Y at %H:%M:%S (%Z)", localtime( &NGIRCd_Start ));
240                 NGIRCd_Restart = FALSE;
241                 NGIRCd_Quit = FALSE;
242
243                 /* Module initialisieren */
244                 Log_Init( );
245                 Conf_Init( );
246                 Channel_Init( );
247                 Client_Init( );
248                 Conn_Init( );
249
250                 /* Wenn als root ausgefuehrt und eine andere UID
251                  * konfiguriert ist, jetzt zu dieser wechseln */
252                 if( getuid( ) != 0 )
253                 {
254                         if( Conf_GID > 0 )
255                         {
256                                 /* Neue Group-ID setzen */
257                                 if( setgid( Conf_GID ) != 0 ) Log( LOG_ERR, "Can't change Group-ID to %u: %s", Conf_GID, strerror( errno ));
258                         }
259                         if( Conf_UID > 0 )
260                         {
261                                 /* Neue User-ID setzen */
262                                 if( setgid( Conf_UID ) != 0 ) Log( LOG_ERR, "Can't change User-ID to %u: %s", Conf_UID, strerror( errno ));
263                         }
264                 }
265                 Log( LOG_INFO, "Running as user %ld, group %ld.", (INT32)getuid( ), (INT32)getgid( ));
266
267                 /* Signal-Handler initialisieren */
268                 Initialize_Signal_Handler( );
269
270                 /* Listen-Ports initialisieren */
271                 Initialize_Listen_Ports( );
272
273                 /* Hauptschleife */
274                 while( TRUE )
275                 {
276                         if( NGIRCd_Quit || NGIRCd_Restart ) break;
277                         Conn_Handler( 5 );
278                 }
279
280                 /* Alles abmelden */
281                 Conn_Exit( );
282                 Client_Exit( );
283                 Channel_Exit( );
284                 Conf_Exit( );
285                 Log_Exit( );
286         }
287
288         return 0;
289 } /* main */
290
291
292 GLOBAL CHAR *NGIRCd_Version( VOID )
293 {
294         STATIC CHAR version[126];
295
296         sprintf( version, PACKAGE" version "VERSION"-%s", NGIRCd_VersionAddition( ));
297         return version;
298 } /* NGIRCd_Version */
299
300
301 GLOBAL CHAR *NGIRCd_VersionAddition( VOID )
302 {
303         STATIC CHAR txt[64];
304
305         strcpy( txt, "" );
306
307 #ifdef USE_SYSLOG
308         if( txt[0] ) strcat( txt, "+" );
309         strcat( txt, "SYSLOG" );
310 #endif
311 #ifdef STRICT_RFC
312         if( txt[0] ) strcat( txt, "+" );
313         strcat( txt, "RFC" );
314 #endif
315 #ifdef DEBUG
316         if( txt[0] ) strcat( txt, "+" );
317         strcat( txt, "DEBUG" );
318 #endif
319 #ifdef SNIFFER
320         if( txt[0] ) strcat( txt, "+" );
321         strcat( txt, "SNIFFER" );
322 #endif
323
324         if( txt[0] ) strcat( txt, "-" );
325         strcat( txt, TARGET_CPU"/"TARGET_VENDOR"/"TARGET_OS );
326
327         return txt;
328 } /* NGIRCd_VersionAddition */
329
330
331 LOCAL VOID Initialize_Signal_Handler( VOID )
332 {
333         /* Signal-Handler initialisieren: einige Signale
334          * werden ignoriert, andere speziell behandelt. */
335
336 #ifdef HAVE_SIGACTION
337         /* sigaction() ist vorhanden */
338
339         struct sigaction saction;
340
341         /* Signal-Struktur initialisieren */
342         memset( &saction, 0, sizeof( saction ));
343         saction.sa_handler = Signal_Handler;
344 #ifdef SA_RESTART
345         saction.sa_flags |= SA_RESTART;
346 #endif
347 #ifdef SA_NOCLDWAIT
348         saction.sa_flags |= SA_NOCLDWAIT;
349 #endif
350
351         /* Signal-Handler einhaengen */
352         sigaction( SIGINT, &saction, NULL );
353         sigaction( SIGQUIT, &saction, NULL );
354         sigaction( SIGTERM, &saction, NULL);
355         sigaction( SIGHUP, &saction, NULL);
356         sigaction( SIGCHLD, &saction, NULL);
357
358         /* einige Signale ignorieren */
359         saction.sa_handler = SIG_IGN;
360         sigaction( SIGPIPE, &saction, NULL );
361 #else
362         /* kein sigaction() vorhanden */
363
364         /* Signal-Handler einhaengen */
365         signal( SIGINT, Signal_Handler );
366         signal( SIGQUIT, Signal_Handler );
367         signal( SIGTERM, Signal_Handler );
368         signal( SIGHUP, Signal_Handler );
369         signal( SIGCHLD, Signal_Handler );
370
371         /* einige Signale ignorieren */
372         signal( SIGPIPE, SIG_IGN );
373 #endif
374 } /* Initialize_Signal_Handler */
375
376
377 LOCAL VOID Signal_Handler( INT Signal )
378 {
379         /* Signal-Handler. Dieser wird aufgerufen, wenn eines der Signale eintrifft,
380          * fuer das wir uns registriert haben (vgl. Initialize_Signal_Handler). Die
381          * Nummer des eingetroffenen Signals wird der Funktion uebergeben. */
382
383         switch( Signal )
384         {
385                 case SIGTERM:
386                 case SIGINT:
387                 case SIGQUIT:
388                         /* wir soll(t)en uns wohl beenden ... */
389                         if( Signal == SIGTERM ) Log( LOG_WARNING, "Got TERM signal, terminating now ..." );
390                         else if( Signal == SIGINT ) Log( LOG_WARNING, "Got INT signal, terminating now ..." );
391                         else if( Signal == SIGQUIT ) Log( LOG_WARNING, "Got QUIT signal, terminating now ..." );
392                         NGIRCd_Quit = TRUE;
393                         break;
394                 case SIGHUP:
395                         /* neu starten */
396                         Log( LOG_WARNING, "Got HUP signal, restarting now ..." );
397                         NGIRCd_Restart = TRUE;
398                         break;
399                 case SIGCHLD:
400                         /* Child-Prozess wurde beendet. Zombies vermeiden: */
401                         while( waitpid( -1, NULL, WNOHANG ) > 0);
402                         break;
403                 default:
404                         /* unbekanntes bzw. unbehandeltes Signal */
405                         Log( LOG_NOTICE, "Got signal %d! Ignored.", Signal );
406         }
407 } /* Signal_Handler */
408
409
410 LOCAL VOID Initialize_Listen_Ports( VOID )
411 {
412         /* Ports, auf denen der Server Verbindungen entgegennehmen
413          * soll, initialisieren */
414         
415         UINT created, i;
416
417         created = 0;
418         for( i = 0; i < Conf_ListenPorts_Count; i++ )
419         {
420                 if( Conn_NewListener( Conf_ListenPorts[i] )) created++;
421                 else Log( LOG_ERR, "Can't listen on port %u!", Conf_ListenPorts[i] );
422         }
423
424         if( created < 1 )
425         {
426                 Log( LOG_ALERT, "Server isn't listening on a single port!" );
427                 Log( LOG_ALERT, PACKAGE" exiting due to fatal errors!" );
428                 exit( 1 );
429         }
430 } /* Initialize_Listen_Ports */
431
432
433 LOCAL VOID Show_Version( VOID )
434 {
435         puts( NGIRCd_Version( ));
436         puts( "Copyright (c)2001,2002 by Alexander Barton (alex@barton.de).\n" );
437         puts( "This is free software; see the source for copying conditions. There is NO" );
438         puts( "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE." );
439 } /* Show_Version */
440
441
442 LOCAL VOID Show_Help( VOID )
443 {
444 #ifdef DEBUG
445         puts( "  -d, --debug        log extra debug messages" );
446 #endif
447         puts( "  -f, --config <f>   use file <f> as configuration file" );
448         puts( "  -n, --nodaemon     don't fork and don't detatch from controlling terminal" );
449         puts( "  -p, --passive      disable automatic connections to other servers" );
450 #ifdef SNIFFER
451         puts( "  -s, --sniffer      enable network sniffer and display all IRC traffic" );
452 #endif
453         puts( "      --configtest   read, validate and display configuration; then exit" );
454         puts( "      --version      output version information and exit" );
455         puts( "      --help         display this help and exit" );
456 } /* Show_Help */
457
458
459 /* -eof- */