Blame


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