Blame


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