1 |
/* |
2 |
* ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd). |
3 |
* ircd.c: Starts up and runs the ircd. |
4 |
* |
5 |
* Copyright (C) 2002 by the past and present ircd coders, and others. |
6 |
* |
7 |
* This program is free software; you can redistribute it and/or modify |
8 |
* it under the terms of the GNU General Public License as published by |
9 |
* the Free Software Foundation; either version 2 of the License, or |
10 |
* (at your option) any later version. |
11 |
* |
12 |
* This program is distributed in the hope that it will be useful, |
13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
* GNU General Public License for more details. |
16 |
* |
17 |
* You should have received a copy of the GNU General Public License |
18 |
* along with this program; if not, write to the Free Software |
19 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
20 |
* USA |
21 |
* |
22 |
* $Id$ |
23 |
*/ |
24 |
|
25 |
#ifdef IN_IRCD |
26 |
#include "stdinc.h" |
27 |
#include "conf/conf.h" |
28 |
#include "s_user.h" |
29 |
#include "ircd.h" |
30 |
#include "channel.h" |
31 |
#include "channel_mode.h" |
32 |
#include "client.h" |
33 |
#include "common.h" |
34 |
#include "hash.h" |
35 |
#include "ircd_signal.h" |
36 |
#include "motd.h" |
37 |
#include "ircd_handler.h" |
38 |
#include "msg.h" /* msgtab */ |
39 |
#include "numeric.h" |
40 |
#include "packet.h" |
41 |
#include "parse.h" |
42 |
#include "restart.h" |
43 |
#include "s_auth.h" |
44 |
#include "parse_aline.h" |
45 |
#include "s_serv.h" |
46 |
#include "send.h" |
47 |
#include "whowas.h" |
48 |
#include "motd.h" |
49 |
#include "watch.h" |
50 |
#include "patchlevel.h" |
51 |
#include "serno.h" |
52 |
|
53 |
|
54 |
/* /quote set variables */ |
55 |
struct SetOptions GlobalSetOptions; |
56 |
struct ServerStatistics ServerStats; |
57 |
/* admin info set from ircd.conf */ |
58 |
struct Counter Count = { 0, 0, 0, 0, 0, 0, 0, 0 }; |
59 |
struct ServerState_t ServerState = { 0 }; |
60 |
struct Client me; /* That's me */ |
61 |
struct LocalUser meLocalUser; /* That's also part of me */ |
62 |
|
63 |
char **myargv; |
64 |
const char *ircd_version = PATCHLEVEL; |
65 |
const char *serno = SERIALNUM; |
66 |
char ircd_platform[IRCD_BUFSIZE]; |
67 |
|
68 |
int dorehash = 0; |
69 |
int doremotd = 0; |
70 |
|
71 |
/* Set to zero because it should be initialized later using |
72 |
* initialize_server_capabs |
73 |
*/ |
74 |
int default_server_capabs = 0; |
75 |
|
76 |
#ifdef HAVE_LIBCRYPTO |
77 |
int bio_spare_fd = -1; |
78 |
#endif |
79 |
|
80 |
int splitmode; |
81 |
int splitchecking; |
82 |
|
83 |
static dlink_node *fdlimit_hook; |
84 |
|
85 |
/* Do klines the same way hybrid-6 did them, i.e. at the |
86 |
* top of the next io_loop instead of in the same loop as |
87 |
* the klines are being applied. |
88 |
* |
89 |
* This should fix strange CPU starvation as very indirectly reported. |
90 |
* (Why do you people not email bug reports? WHY? WHY?) |
91 |
* |
92 |
* - Dianora |
93 |
*/ |
94 |
|
95 |
int rehashed_klines = 0; |
96 |
|
97 |
#ifndef _WIN32 |
98 |
/* |
99 |
* print_startup - print startup information |
100 |
*/ |
101 |
static void |
102 |
print_startup(int pid) |
103 |
{ |
104 |
printf("ircd: version %s\n", ircd_version); |
105 |
printf("ircd: pid %d\n", pid); |
106 |
printf("ircd: running in %s mode from %s\n", |
107 |
ServerState.foreground ? "foreground" : "background", DPATH); |
108 |
} |
109 |
|
110 |
static void |
111 |
make_daemon(void) |
112 |
{ |
113 |
int pid; |
114 |
|
115 |
if ((pid = fork()) < 0) |
116 |
{ |
117 |
perror("fork"); |
118 |
exit(EXIT_FAILURE); |
119 |
} |
120 |
else if (pid > 0) |
121 |
{ |
122 |
print_startup(pid); |
123 |
exit(EXIT_SUCCESS); |
124 |
} |
125 |
|
126 |
setsid(); |
127 |
} |
128 |
#endif |
129 |
|
130 |
static struct lgetopt myopts[] = { |
131 |
{"configfile", &ServerState.configfile, |
132 |
STRING, "File to use for ircd.conf"}, |
133 |
{"klinefile", &ServerState.klinefile, |
134 |
STRING, "File to use for kline.conf"}, |
135 |
{"rklinefile", &ServerState.rklinefile, |
136 |
STRING, "File to use for rkline.conf"}, |
137 |
{"dlinefile", &ServerState.dlinefile, |
138 |
STRING, "File to use for dline.conf"}, |
139 |
{"glinefile", &ServerState.glinefile, |
140 |
STRING, "File to use for gline.conf"}, |
141 |
{"xlinefile", &ServerState.xlinefile, |
142 |
STRING, "File to use for xline.conf"}, |
143 |
{"rxlinefile", &ServerState.rxlinefile, |
144 |
STRING, "File to use for rxline.conf"}, |
145 |
{"cresvfile", &ServerState.cresvfile, |
146 |
STRING, "File to use for cresv.conf"}, |
147 |
{"nresvfile", &ServerState.nresvfile, |
148 |
STRING, "File to use for nresv.conf"}, |
149 |
{"logfile", &ServerState.logfile, |
150 |
STRING, "File to use for ircd.log"}, |
151 |
{"pidfile", &ServerState.pidfile, |
152 |
STRING, "File to use for process ID"}, |
153 |
{"foreground", &ServerState.foreground, |
154 |
YESNO, "Run in foreground (don't detach)"}, |
155 |
{"version", &ServerState.printversion, |
156 |
YESNO, "Print version and exit"}, |
157 |
{"help", NULL, USAGE, "Print this text"}, |
158 |
{NULL, NULL, STRING, NULL}, |
159 |
}; |
160 |
|
161 |
static void |
162 |
io_loop(void) |
163 |
{ |
164 |
while (1 == 1) |
165 |
{ |
166 |
/* |
167 |
* Maybe we want a flags word? |
168 |
* ie. if (REHASHED_KLINES(global_flags)) |
169 |
* SET_REHASHED_KLINES(global_flags) |
170 |
* CLEAR_REHASHED_KLINES(global_flags) |
171 |
* |
172 |
* - Dianora |
173 |
*/ |
174 |
if (rehashed_klines) |
175 |
{ |
176 |
check_conf_klines(); |
177 |
rehashed_klines = 0; |
178 |
} |
179 |
|
180 |
if (listing_client_list.head) |
181 |
{ |
182 |
dlink_node *ptr = NULL, *ptr_next = NULL; |
183 |
DLINK_FOREACH_SAFE(ptr, ptr_next, listing_client_list.head) |
184 |
{ |
185 |
struct Client *client_p = ptr->data; |
186 |
assert(client_p->localClient->list_task); |
187 |
safe_list_channels(client_p, client_p->localClient->list_task, 0); |
188 |
} |
189 |
} |
190 |
|
191 |
/* Run pending events, then get the number of seconds to the next |
192 |
* event |
193 |
*/ |
194 |
while (eventNextTime() <= CurrentTime) |
195 |
eventRun(); |
196 |
|
197 |
comm_select(); |
198 |
exit_aborted_clients(); |
199 |
free_exited_clients(); |
200 |
send_queued_all(); |
201 |
|
202 |
// Check to see whether we have to rehash the configuration .. |
203 |
if (dorehash) |
204 |
{ |
205 |
sendto_realops_flags(UMODE_ALL, L_ALL, |
206 |
"Got signal SIGHUP, reloading ircd configuration"); |
207 |
read_conf_files(NO); |
208 |
dorehash = 0; |
209 |
} |
210 |
|
211 |
if (doremotd) |
212 |
{ |
213 |
read_message_file(&motd); |
214 |
sendto_realops_flags(UMODE_ALL, L_ALL, |
215 |
"Got signal SIGUSR1, reloading ircd MOTD file"); |
216 |
doremotd = 0; |
217 |
} |
218 |
} |
219 |
} |
220 |
|
221 |
/* initalialize_global_set_options() |
222 |
* |
223 |
* inputs - none |
224 |
* output - none |
225 |
* side effects - This sets all global set options needed |
226 |
* XXX to be removed with old s_conf.c |
227 |
*/ |
228 |
static void |
229 |
initialize_global_set_options(void) |
230 |
{ |
231 |
memset(&GlobalSetOptions, 0, sizeof(GlobalSetOptions)); |
232 |
|
233 |
GlobalSetOptions.autoconn = 1; |
234 |
GlobalSetOptions.spam_time = MIN_JOIN_LEAVE_TIME; |
235 |
GlobalSetOptions.spam_num = MAX_JOIN_LEAVE_COUNT; |
236 |
|
237 |
if (General.default_floodcount) |
238 |
GlobalSetOptions.floodcount = General.default_floodcount; |
239 |
else |
240 |
GlobalSetOptions.floodcount = 10; |
241 |
|
242 |
// XXX I have no idea what to try here - Dianora |
243 |
GlobalSetOptions.joinfloodcount = 16; |
244 |
GlobalSetOptions.joinfloodtime = 8; |
245 |
|
246 |
GlobalSetOptions.split_servers = Channel.default_split_server_count; |
247 |
GlobalSetOptions.split_users = Channel.default_split_user_count; |
248 |
|
249 |
if (GlobalSetOptions.split_users && GlobalSetOptions.split_servers && |
250 |
(Channel.no_create_on_split || Channel.no_join_on_split)) |
251 |
{ |
252 |
splitmode = 1; |
253 |
splitchecking = 1; |
254 |
} |
255 |
|
256 |
GlobalSetOptions.ident_timeout = IDENT_TIMEOUT; |
257 |
GlobalSetOptions.idletime = General.idletime; |
258 |
GlobalSetOptions.maxlisters = 10; /* XXX ya ya ya - db */ |
259 |
} |
260 |
|
261 |
/* write_pidfile() |
262 |
* |
263 |
* inputs - filename+path of pid file |
264 |
* output - NONE |
265 |
* side effects - write the pid of the ircd to filename |
266 |
*/ |
267 |
static void |
268 |
write_pidfile(const char *filename) |
269 |
{ |
270 |
FBFILE *fb; |
271 |
|
272 |
if ((fb = fbopen(filename, "w"))) |
273 |
{ |
274 |
char buff[32]; |
275 |
unsigned int pid = (unsigned int)getpid(); |
276 |
size_t nbytes = ircsprintf(buff, "%u\n", pid); |
277 |
|
278 |
if ((fbputs(buff, fb, nbytes) == -1)) |
279 |
ilog(L_ERROR, "Error writing %u to pid file %s (%s)", |
280 |
pid, filename, strerror(errno)); |
281 |
|
282 |
fbclose(fb); |
283 |
} |
284 |
else |
285 |
{ |
286 |
ilog(L_ERROR, "Error opening pid file %s", filename); |
287 |
} |
288 |
} |
289 |
|
290 |
/* check_pidfile() |
291 |
* |
292 |
* inputs - filename+path of pid file |
293 |
* output - none |
294 |
* side effects - reads pid from pidfile and checks if ircd is in process |
295 |
* list. if it is, gracefully exits |
296 |
* -kre |
297 |
*/ |
298 |
static void |
299 |
check_pidfile(const char *filename) |
300 |
{ |
301 |
#ifndef _WIN32 |
302 |
FBFILE *fb; |
303 |
char buff[32]; |
304 |
pid_t pidfromfile; |
305 |
|
306 |
// Don't do logging here, since we don't have log() initialised |
307 |
if ((fb = fbopen(filename, "r"))) |
308 |
{ |
309 |
if (fbgets(buff, 20, fb) == NULL) |
310 |
{ |
311 |
/* log(L_ERROR, "Error reading from pid file %s (%s)", filename, |
312 |
* strerror(errno)); |
313 |
*/ |
314 |
} |
315 |
else |
316 |
{ |
317 |
pidfromfile = atoi(buff); |
318 |
|
319 |
if (!kill(pidfromfile, 0)) |
320 |
{ |
321 |
// log(L_ERROR, "Server is already running"); |
322 |
printf("ircd: daemon is already running\n"); |
323 |
exit(-1); |
324 |
} |
325 |
} |
326 |
|
327 |
fbclose(fb); |
328 |
} |
329 |
else if (errno != ENOENT) |
330 |
{ |
331 |
// log(L_ERROR, "Error opening pid file %s", filename); |
332 |
} |
333 |
#endif |
334 |
} |
335 |
|
336 |
/* init_ssl() |
337 |
* |
338 |
* inputs - nothing |
339 |
* output - nothing |
340 |
* side effects - setups SSL context. |
341 |
*/ |
342 |
static void |
343 |
ssl_init(void) |
344 |
{ |
345 |
#ifdef HAVE_LIBCRYPTO |
346 |
SSL_library_init(); |
347 |
SSL_load_error_strings(); |
348 |
|
349 |
ServerInfo.ctx = SSL_CTX_new(SSLv23_server_method()); |
350 |
|
351 |
if (!ServerInfo.ctx) |
352 |
{ |
353 |
const char *s; |
354 |
|
355 |
fprintf(stderr, "ERROR: Could not initialize the SSL context -- %s\n", |
356 |
s = ERR_lib_error_string(ERR_get_error())); |
357 |
ilog(L_CRIT, "ERROR: Could not initialize the SSL context -- %s\n", s); |
358 |
} |
359 |
|
360 |
SSL_CTX_set_options(ServerInfo.ctx, SSL_OP_NO_SSLv2); |
361 |
SSL_CTX_set_options(ServerInfo.ctx, SSL_OP_TLS_ROLLBACK_BUG|SSL_OP_ALL); |
362 |
SSL_CTX_set_verify(ServerInfo.ctx, SSL_VERIFY_NONE, NULL); |
363 |
|
364 |
bio_spare_fd = save_spare_fd("SSL private key validation"); |
365 |
#endif /* HAVE_LIBCRYPTO */ |
366 |
} |
367 |
|
368 |
/* init_callbacks() |
369 |
* |
370 |
* inputs - nothing |
371 |
* output - nothing |
372 |
* side effects - setups standard hook points |
373 |
*/ |
374 |
static void |
375 |
init_callbacks(void) |
376 |
{ |
377 |
entering_umode_cb = register_callback("entering_umode", NULL); |
378 |
iorecv_cb = register_callback("iorecv", iorecv_default); |
379 |
iosend_cb = register_callback("iosend", iosend_default); |
380 |
iorecvctrl_cb = register_callback("iorecvctrl", NULL); |
381 |
iosendctrl_cb = register_callback("iosendctrl", NULL); |
382 |
authorize_client = register_callback("authorize_client", do_authorize_client); |
383 |
uid_get = register_callback("uid_get", do_uid_get); |
384 |
umode_cb = register_callback("changing_umode", change_simple_umode); |
385 |
} |
386 |
|
387 |
static void * |
388 |
changing_fdlimit(va_list args) |
389 |
{ |
390 |
int old_fdlimit = hard_fdlimit; |
391 |
int fdmax = va_arg(args, int); |
392 |
|
393 |
/* |
394 |
* allow MAXCLIENTS_MIN clients even at the cost of MAX_BUFFER and |
395 |
* some not really LEAKED_FDS |
396 |
*/ |
397 |
fdmax = LIBIO_MAX(fdmax, LEAKED_FDS + MAX_BUFFER + MAXCLIENTS_MIN); |
398 |
|
399 |
pass_callback(fdlimit_hook, fdmax); |
400 |
|
401 |
if (ServerInfo.max_clients > MAXCLIENTS_MAX) |
402 |
{ |
403 |
if (old_fdlimit != 0) |
404 |
sendto_realops_flags(UMODE_ALL, L_ALL, |
405 |
"HARD_FDLIMIT changed to %d, adjusting MAXCLIENTS to %d", |
406 |
hard_fdlimit, MAXCLIENTS_MAX); |
407 |
|
408 |
ServerInfo.max_clients = MAXCLIENTS_MAX; |
409 |
} |
410 |
|
411 |
return NULL; |
412 |
} |
413 |
|
414 |
EXTERN int |
415 |
main(int argc, char *argv[]) |
416 |
{ |
417 |
/* |
418 |
* Check to see if the user is running us as root, which is a nono |
419 |
*/ |
420 |
#ifndef _WIN32 |
421 |
if (geteuid() == 0) |
422 |
{ |
423 |
fprintf(stderr, "Don't run ircd as root!!!\n"); |
424 |
return 1; |
425 |
} |
426 |
|
427 |
// Setup corefile size immediately after boot -kre |
428 |
setup_corefile(); |
429 |
#endif |
430 |
|
431 |
memset(&ServerInfo, 0, sizeof(ServerInfo)); |
432 |
memset(&ServerStats, 0, sizeof(ServerStats)); |
433 |
|
434 |
ServerState.configfile = CPATH; // Server configuration file |
435 |
ServerState.klinefile = KPATH; // Server kline file |
436 |
ServerState.xlinefile = XPATH; // Server xline file |
437 |
ServerState.rxlinefile = RXPATH; // Server regex xline file |
438 |
ServerState.rklinefile = RKPATH; // Server regex kline file |
439 |
ServerState.dlinefile = DLPATH; // dline file |
440 |
ServerState.glinefile = GPATH; // gline log file |
441 |
ServerState.cresvfile = CRESVPATH; // channel resv file |
442 |
ServerState.nresvfile = NRESVPATH; // nick resv file |
443 |
myargv = argv; |
444 |
umask(077); // better safe than sorry --SRB |
445 |
|
446 |
parseargs(&argc, &argv, myopts); |
447 |
|
448 |
if (ServerState.printversion) |
449 |
{ |
450 |
printf("ircd: version %s\n", ircd_version); |
451 |
exit(EXIT_SUCCESS); |
452 |
} |
453 |
|
454 |
if (chdir(DPATH)) |
455 |
{ |
456 |
perror("chdir"); |
457 |
exit(EXIT_FAILURE); |
458 |
} |
459 |
|
460 |
ssl_init(); |
461 |
|
462 |
#ifndef _WIN32 |
463 |
if (!ServerState.foreground) |
464 |
make_daemon(); |
465 |
else |
466 |
print_startup(getpid()); |
467 |
#endif |
468 |
|
469 |
libio_init(!ServerState.foreground); |
470 |
outofmemory = ircd_outofmemory; |
471 |
fdlimit_hook = install_hook(fdlimit_cb, changing_fdlimit); |
472 |
|
473 |
check_pidfile(ServerState.pidfile); |
474 |
setup_signals(); |
475 |
libio_get_platform(ircd_platform, sizeof(ircd_platform)); |
476 |
init_log(ServerState.logfile); |
477 |
ServerState.can_use_v6 = check_can_use_v6(); |
478 |
|
479 |
memset(&me, 0, sizeof(me)); |
480 |
memset(&meLocalUser, 0, sizeof(meLocalUser)); |
481 |
me.localClient = &meLocalUser; |
482 |
me.from = me.servptr = &me; |
483 |
me.lasttime = me.since = me.firsttime = CurrentTime; |
484 |
SetMe(&me); |
485 |
make_server(&me); |
486 |
dlinkAdd(&me, &me.node, &global_client_list); |
487 |
dlinkAdd(&me, make_dlink_node(), &global_serv_list); |
488 |
|
489 |
init_callbacks(); |
490 |
init_motd(); |
491 |
init_isupport(); |
492 |
hash_init(); |
493 |
init_ip_hash_table(); // client host ip hash table |
494 |
clear_tree_parse(); |
495 |
init_client(); |
496 |
whowas_init(); |
497 |
watch_init(); |
498 |
init_auth(); |
499 |
channel_init(); |
500 |
init_channel_modes(); |
501 |
server_init(); |
502 |
init_conf(); |
503 |
read_conf_files(YES); // cold start init conf files |
504 |
|
505 |
hash_add_client(&me); |
506 |
if (me.id[0]) |
507 |
hash_add_id(&me); |
508 |
|
509 |
init_uid(); |
510 |
initialize_global_set_options(); |
511 |
|
512 |
/* |
513 |
* assemble_umode_buffer() has to be called after |
514 |
* reading conf/loading modules. |
515 |
*/ |
516 |
assemble_umode_buffer(); |
517 |
|
518 |
write_pidfile(ServerState.pidfile); |
519 |
|
520 |
ilog(L_NOTICE, "Server Ready"); |
521 |
|
522 |
// We want try_connections to be called as soon as possible now! -- adrian |
523 |
// No, 'cause after a restart it would cause all sorts of nick collides |
524 |
eventAddIsh("try_connections", try_connections, NULL, STARTUP_CONNECTIONS_TIME); |
525 |
|
526 |
eventAddIsh("collect_zipstats", collect_zipstats, NULL, ZIPSTATS_TIME); |
527 |
|
528 |
// Setup the timeout check. I'll shift it later :) -- adrian |
529 |
eventAddIsh("comm_checktimeouts", comm_checktimeouts, NULL, 1); |
530 |
|
531 |
io_loop(); |
532 |
return 0; |
533 |
} |
534 |
|
535 |
#else |
536 |
|
537 |
#include <windows.h> |
538 |
|
539 |
extern __declspec(dllimport) main(int, char **); |
540 |
|
541 |
/* |
542 |
* Initial entry point for Win32 GUI applications, called by the C runtime. |
543 |
* |
544 |
* It should be only a wrapper for main(), since when compiled as a console |
545 |
* application, main() is called instead. |
546 |
*/ |
547 |
int WINAPI |
548 |
WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, |
549 |
LPSTR lpCmdLine, int nCmdShow) |
550 |
{ |
551 |
/* Do we really need these pidfile, logfile etc arguments? |
552 |
* And we are not on a console, so -help or -foreground is meaningless. */ |
553 |
|
554 |
char *argv[2] = {"ircd", NULL}; |
555 |
|
556 |
return main(1, argv); |
557 |
} |
558 |
|
559 |
#endif |