Blame


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