1 |
|
/* |
2 |
< |
* ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd). |
3 |
< |
* ircd.c: Starts up and runs the ircd. |
2 |
> |
* ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd) |
3 |
|
* |
4 |
< |
* Copyright (C) 2002 by the past and present ircd coders, and others. |
4 |
> |
* Copyright (c) 1997-2014 ircd-hybrid development team |
5 |
|
* |
6 |
|
* This program is free software; you can redistribute it and/or modify |
7 |
|
* it under the terms of the GNU General Public License as published by |
17 |
|
* along with this program; if not, write to the Free Software |
18 |
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
19 |
|
* USA |
20 |
< |
* |
21 |
< |
* $Id$ |
20 |
> |
*/ |
21 |
> |
|
22 |
> |
/*! \file ircd.c |
23 |
> |
* \brief Starts up and runs the ircd. |
24 |
> |
* \version $Id$ |
25 |
|
*/ |
26 |
|
|
27 |
|
#include "stdinc.h" |
38 |
|
#include "ircd_signal.h" |
39 |
|
#include "s_gline.h" |
40 |
|
#include "motd.h" |
41 |
+ |
#include "conf.h" |
42 |
|
#include "hostmask.h" |
43 |
|
#include "numeric.h" |
44 |
|
#include "packet.h" |
48 |
|
#include "rng_mt.h" |
49 |
|
#include "s_auth.h" |
50 |
|
#include "s_bsd.h" |
51 |
< |
#include "s_conf.h" |
49 |
< |
#include "s_log.h" |
51 |
> |
#include "log.h" |
52 |
|
#include "s_misc.h" |
53 |
|
#include "s_serv.h" /* try_connections */ |
54 |
|
#include "send.h" |
55 |
|
#include "whowas.h" |
56 |
|
#include "modules.h" |
57 |
|
#include "memory.h" |
58 |
+ |
#include "mempool.h" |
59 |
|
#include "hook.h" |
60 |
|
#include "ircd_getopt.h" |
58 |
– |
#include "balloc.h" |
59 |
– |
#include "motd.h" |
61 |
|
#include "supported.h" |
62 |
|
#include "watch.h" |
63 |
+ |
#include "conf_db.h" |
64 |
+ |
#include "conf_class.h" |
65 |
+ |
|
66 |
|
|
67 |
+ |
#ifdef HAVE_LIBGEOIP |
68 |
+ |
GeoIP *geoip_ctx; |
69 |
+ |
#endif |
70 |
|
|
71 |
|
/* /quote set variables */ |
72 |
|
struct SetOptions GlobalSetOptions; |
73 |
< |
|
74 |
< |
/* configuration set from ircd.conf */ |
68 |
< |
struct config_file_entry ConfigFileEntry; |
69 |
< |
/* server info set from ircd.conf */ |
70 |
< |
struct server_info ServerInfo; |
71 |
< |
/* admin info set from ircd.conf */ |
72 |
< |
struct admin_info AdminInfo = { NULL, NULL, NULL }; |
73 |
< |
struct Counter Count = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; |
74 |
< |
struct ServerState_t server_state = { 0 }; |
75 |
< |
struct logging_entry ConfigLoggingEntry = { 1, 1 }; |
73 |
> |
struct Counter Count; |
74 |
> |
struct ServerState_t server_state; |
75 |
|
struct ServerStatistics ServerStats; |
76 |
|
struct timeval SystemTime; |
77 |
|
struct Client me; /* That's me */ |
81 |
|
const char *pidFileName = PPATH; |
82 |
|
|
83 |
|
char **myargv; |
85 |
– |
char ircd_platform[PLATFORMLEN]; |
84 |
|
|
85 |
|
int dorehash = 0; |
86 |
|
int doremotd = 0; |
113 |
|
static void |
114 |
|
print_startup(int pid) |
115 |
|
{ |
116 |
< |
printf("ircd: version %s\n", ircd_version); |
116 |
> |
printf("ircd: version %s(%s)\n", ircd_version, serno); |
117 |
|
printf("ircd: pid %d\n", pid); |
118 |
|
printf("ircd: running in %s mode from %s\n", !server_state.foreground ? "background" |
119 |
|
: "foreground", ConfigFileEntry.dpath); |
140 |
|
|
141 |
|
static int printVersion = 0; |
142 |
|
|
143 |
< |
struct lgetopt myopts[] = { |
144 |
< |
{"dlinefile", &ConfigFileEntry.dlinefile, |
147 |
< |
STRING, "File to use for dline.conf"}, |
148 |
< |
{"configfile", &ConfigFileEntry.configfile, |
143 |
> |
static struct lgetopt myopts[] = { |
144 |
> |
{"configfile", &ConfigFileEntry.configfile, |
145 |
|
STRING, "File to use for ircd.conf"}, |
146 |
< |
{"klinefile", &ConfigFileEntry.klinefile, |
147 |
< |
STRING, "File to use for kline.conf"}, |
148 |
< |
{"xlinefile", &ConfigFileEntry.xlinefile, |
149 |
< |
STRING, "File to use for xline.conf"}, |
150 |
< |
{"logfile", &logFileName, |
146 |
> |
{"glinefile", &ConfigFileEntry.glinefile, |
147 |
> |
STRING, "File to use for gline database"}, |
148 |
> |
{"klinefile", &ConfigFileEntry.klinefile, |
149 |
> |
STRING, "File to use for kline database"}, |
150 |
> |
{"dlinefile", &ConfigFileEntry.dlinefile, |
151 |
> |
STRING, "File to use for dline database"}, |
152 |
> |
{"xlinefile", &ConfigFileEntry.xlinefile, |
153 |
> |
STRING, "File to use for xline database"}, |
154 |
> |
{"resvfile", &ConfigFileEntry.resvfile, |
155 |
> |
STRING, "File to use for resv database"}, |
156 |
> |
{"logfile", &logFileName, |
157 |
|
STRING, "File to use for ircd.log"}, |
158 |
|
{"pidfile", &pidFileName, |
159 |
|
STRING, "File to use for process ID"}, |
160 |
< |
{"foreground", &server_state.foreground, |
160 |
> |
{"foreground", &server_state.foreground, |
161 |
|
YESNO, "Run in foreground (don't detach)"}, |
162 |
< |
{"version", &printVersion, |
162 |
> |
{"version", &printVersion, |
163 |
|
YESNO, "Print version and exit"}, |
164 |
|
{"help", NULL, USAGE, "Print this text"}, |
165 |
|
{NULL, NULL, STRING, NULL}, |
168 |
|
void |
169 |
|
set_time(void) |
170 |
|
{ |
171 |
< |
static char to_send[200]; |
171 |
> |
static char to_send[IRCD_BUFSIZE]; |
172 |
|
struct timeval newtime; |
173 |
|
newtime.tv_sec = 0; |
174 |
|
newtime.tv_usec = 0; |
177 |
|
{ |
178 |
|
ilog(LOG_TYPE_IRCD, "Clock Failure (%s), TS can be corrupted", |
179 |
|
strerror(errno)); |
180 |
< |
sendto_realops_flags(UMODE_ALL, L_ALL, |
180 |
> |
sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE, |
181 |
|
"Clock Failure (%s), TS can be corrupted", |
182 |
|
strerror(errno)); |
183 |
|
restart("Clock Failure"); |
203 |
|
{ |
204 |
|
/* |
205 |
|
* Maybe we want a flags word? |
206 |
< |
* ie. if (REHASHED_KLINES(global_flags)) |
206 |
> |
* ie. if (REHASHED_KLINES(global_flags)) |
207 |
|
* SET_REHASHED_KLINES(global_flags) |
208 |
|
* CLEAR_REHASHED_KLINES(global_flags) |
209 |
|
* |
235 |
|
comm_select(); |
236 |
|
exit_aborted_clients(); |
237 |
|
free_exited_clients(); |
236 |
– |
send_queued_all(); |
238 |
|
|
239 |
|
/* Check to see whether we have to rehash the configuration .. */ |
240 |
|
if (dorehash) |
244 |
|
} |
245 |
|
if (doremotd) |
246 |
|
{ |
247 |
< |
read_message_file(&ConfigFileEntry.motd); |
248 |
< |
sendto_realops_flags(UMODE_ALL, L_ALL, |
249 |
< |
"Got signal SIGUSR1, reloading ircd motd file"); |
247 |
> |
motd_recache(); |
248 |
> |
sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE, |
249 |
> |
"Got signal SIGUSR1, reloading motd files"); |
250 |
|
doremotd = 0; |
251 |
|
} |
252 |
|
} |
256 |
|
* |
257 |
|
* inputs - none |
258 |
|
* output - none |
259 |
< |
* side effects - This sets all global set options needed |
259 |
> |
* side effects - This sets all global set options needed |
260 |
|
*/ |
261 |
|
static void |
262 |
|
initialize_global_set_options(void) |
290 |
|
/* End of global set options */ |
291 |
|
} |
292 |
|
|
292 |
– |
/* initialize_message_files() |
293 |
– |
* |
294 |
– |
* inputs - none |
295 |
– |
* output - none |
296 |
– |
* side effects - Set up all message files needed, motd etc. |
297 |
– |
*/ |
298 |
– |
static void |
299 |
– |
initialize_message_files(void) |
300 |
– |
{ |
301 |
– |
init_message_file(USER_MOTD, MPATH, &ConfigFileEntry.motd); |
302 |
– |
init_message_file(OPER_MOTD, OPATH, &ConfigFileEntry.opermotd); |
303 |
– |
init_message_file(USER_LINKS, LIPATH, &ConfigFileEntry.linksfile); |
304 |
– |
|
305 |
– |
read_message_file(&ConfigFileEntry.motd); |
306 |
– |
read_message_file(&ConfigFileEntry.opermotd); |
307 |
– |
read_message_file(&ConfigFileEntry.linksfile); |
308 |
– |
|
309 |
– |
init_isupport(); |
310 |
– |
} |
311 |
– |
|
293 |
|
/* initialize_server_capabs() |
294 |
|
* |
295 |
|
* inputs - none |
302 |
|
add_capability("EOB", CAP_EOB, 1); |
303 |
|
add_capability("TS6", CAP_TS6, 0); |
304 |
|
add_capability("CLUSTER", CAP_CLUSTER, 1); |
305 |
+ |
// add_capability("FAKEHOST", CAP_FAKEHOST, 1); |
306 |
|
add_capability("SVS", CAP_SVS, 1); |
307 |
|
#ifdef HALFOPS |
308 |
|
add_capability("HOPS", CAP_HOPS, 1); |
318 |
|
static void |
319 |
|
write_pidfile(const char *filename) |
320 |
|
{ |
321 |
< |
FBFILE *fb; |
321 |
> |
FILE *fb; |
322 |
|
|
323 |
< |
if ((fb = fbopen(filename, "w"))) |
323 |
> |
if ((fb = fopen(filename, "w"))) |
324 |
|
{ |
325 |
< |
char buff[32]; |
325 |
> |
char buff[IRCD_BUFSIZE]; |
326 |
|
unsigned int pid = (unsigned int)getpid(); |
345 |
– |
size_t nbytes = snprintf(buff, sizeof(buff), "%u\n", pid); |
327 |
|
|
328 |
< |
if ((fbputs(buff, fb, nbytes) == -1)) |
328 |
> |
snprintf(buff, sizeof(buff), "%u\n", pid); |
329 |
> |
|
330 |
> |
if ((fputs(buff, fb) == -1)) |
331 |
|
ilog(LOG_TYPE_IRCD, "Error writing %u to pid file %s (%s)", |
332 |
|
pid, filename, strerror(errno)); |
333 |
|
|
334 |
< |
fbclose(fb); |
352 |
< |
return; |
334 |
> |
fclose(fb); |
335 |
|
} |
336 |
|
else |
337 |
|
{ |
350 |
|
static void |
351 |
|
check_pidfile(const char *filename) |
352 |
|
{ |
353 |
< |
FBFILE *fb; |
354 |
< |
char buff[32]; |
353 |
> |
FILE *fb; |
354 |
> |
char buff[IRCD_BUFSIZE]; |
355 |
|
pid_t pidfromfile; |
356 |
|
|
357 |
|
/* Don't do logging here, since we don't have log() initialised */ |
358 |
< |
if ((fb = fbopen(filename, "r"))) |
358 |
> |
if ((fb = fopen(filename, "r"))) |
359 |
|
{ |
360 |
< |
if (fbgets(buff, 20, fb) == NULL) |
360 |
> |
if (fgets(buff, 20, fb) == NULL) |
361 |
|
{ |
362 |
|
/* log(L_ERROR, "Error reading from pid file %s (%s)", filename, |
363 |
|
* strerror(errno)); |
375 |
|
} |
376 |
|
} |
377 |
|
|
378 |
< |
fbclose(fb); |
378 |
> |
fclose(fb); |
379 |
|
} |
380 |
|
else if (errno != ENOENT) |
381 |
|
{ |
405 |
|
#endif |
406 |
|
} |
407 |
|
|
408 |
+ |
#ifdef HAVE_LIBCRYPTO |
409 |
+ |
static int |
410 |
+ |
always_accept_verify_cb(int preverify_ok, X509_STORE_CTX *x509_ctx) |
411 |
+ |
{ |
412 |
+ |
return 1; |
413 |
+ |
} |
414 |
+ |
#endif |
415 |
+ |
|
416 |
|
/* init_ssl() |
417 |
|
* |
418 |
|
* inputs - nothing |
420 |
|
* side effects - setups SSL context. |
421 |
|
*/ |
422 |
|
static void |
423 |
< |
init_ssl(void) |
423 |
> |
ssl_init(void) |
424 |
|
{ |
425 |
|
#ifdef HAVE_LIBCRYPTO |
426 |
+ |
const unsigned char session_id[] = "ircd-hybrid"; |
427 |
+ |
|
428 |
|
SSL_load_error_strings(); |
429 |
|
SSLeay_add_ssl_algorithms(); |
430 |
|
|
437 |
|
ilog(LOG_TYPE_IRCD, "ERROR: Could not initialize the SSL Server context -- %s\n", s); |
438 |
|
} |
439 |
|
|
440 |
< |
SSL_CTX_set_options(ServerInfo.server_ctx, SSL_OP_NO_SSLv2); |
440 |
> |
SSL_CTX_set_options(ServerInfo.server_ctx, SSL_OP_NO_SSLv2|SSL_OP_NO_SSLv3|SSL_OP_NO_TLSv1); |
441 |
|
SSL_CTX_set_options(ServerInfo.server_ctx, SSL_OP_TLS_ROLLBACK_BUG|SSL_OP_ALL); |
442 |
< |
SSL_CTX_set_verify(ServerInfo.server_ctx, SSL_VERIFY_NONE, NULL); |
442 |
> |
SSL_CTX_set_verify(ServerInfo.server_ctx, SSL_VERIFY_PEER|SSL_VERIFY_CLIENT_ONCE, |
443 |
> |
always_accept_verify_cb); |
444 |
> |
SSL_CTX_set_session_id_context(ServerInfo.server_ctx, session_id, sizeof(session_id) - 1); |
445 |
|
|
446 |
|
if ((ServerInfo.client_ctx = SSL_CTX_new(SSLv23_client_method())) == NULL) |
447 |
|
{ |
452 |
|
ilog(LOG_TYPE_IRCD, "ERROR: Could not initialize the SSL Client context -- %s\n", s); |
453 |
|
} |
454 |
|
|
455 |
< |
SSL_CTX_set_options(ServerInfo.client_ctx, SSL_OP_NO_SSLv2); |
455 |
> |
SSL_CTX_set_options(ServerInfo.client_ctx, SSL_OP_NO_SSLv2|SSL_OP_NO_SSLv3|SSL_OP_NO_TLSv1); |
456 |
|
SSL_CTX_set_options(ServerInfo.client_ctx, SSL_OP_TLS_ROLLBACK_BUG|SSL_OP_ALL); |
457 |
< |
SSL_CTX_set_verify(ServerInfo.client_ctx, SSL_VERIFY_NONE, NULL); |
457 |
> |
SSL_CTX_set_verify(ServerInfo.client_ctx, SSL_VERIFY_PEER|SSL_VERIFY_CLIENT_ONCE, |
458 |
> |
always_accept_verify_cb); |
459 |
|
#endif /* HAVE_LIBCRYPTO */ |
460 |
|
} |
461 |
|
|
467 |
– |
/* init_callbacks() |
468 |
– |
* |
469 |
– |
* inputs - nothing |
470 |
– |
* output - nothing |
471 |
– |
* side effects - setups standard hook points |
472 |
– |
*/ |
473 |
– |
static void |
474 |
– |
init_callbacks(void) |
475 |
– |
{ |
476 |
– |
iorecv_cb = register_callback("iorecv", iorecv_default); |
477 |
– |
iosend_cb = register_callback("iosend", iosend_default); |
478 |
– |
} |
479 |
– |
|
462 |
|
int |
463 |
|
main(int argc, char *argv[]) |
464 |
|
{ |
465 |
< |
/* Check to see if the user is running |
484 |
< |
* us as root, which is a nono |
485 |
< |
*/ |
465 |
> |
/* Check to see if the user is running us as root, which is a nono */ |
466 |
|
if (geteuid() == 0) |
467 |
|
{ |
468 |
|
fprintf(stderr, "Don't run ircd as root!!!\n"); |
479 |
|
init_genrand(SystemTime.tv_sec ^ (SystemTime.tv_usec | (getpid() << 20))); |
480 |
|
|
481 |
|
me.localClient = &meLocalUser; |
482 |
< |
dlinkAdd(&me, &me.node, &global_client_list); /* Pointer to beginning |
482 |
> |
dlinkAdd(&me, &me.node, &global_client_list); /* Pointer to beginning |
483 |
|
of Client list */ |
484 |
|
/* Initialise the channel capability usage counts... */ |
485 |
|
init_chcap_usage_counts(); |
486 |
|
|
487 |
|
ConfigFileEntry.dpath = DPATH; |
488 |
< |
ConfigFileEntry.configfile = CPATH; /* Server configuration file */ |
489 |
< |
ConfigFileEntry.klinefile = KPATH; /* Server kline file */ |
490 |
< |
ConfigFileEntry.xlinefile = XPATH; /* Server xline file */ |
491 |
< |
ConfigFileEntry.rxlinefile = RXPATH; /* Server regex xline file */ |
492 |
< |
ConfigFileEntry.rklinefile = RKPATH; /* Server regex kline file */ |
493 |
< |
ConfigFileEntry.dlinefile = DLPATH; /* dline file */ |
494 |
< |
ConfigFileEntry.glinefile = GPATH; /* gline log file */ |
515 |
< |
ConfigFileEntry.cresvfile = CRESVPATH; /* channel resv file */ |
516 |
< |
ConfigFileEntry.nresvfile = NRESVPATH; /* nick resv file */ |
488 |
> |
ConfigFileEntry.configfile = CPATH; /* Server configuration file */ |
489 |
> |
ConfigFileEntry.klinefile = KPATH; /* Server kline file */ |
490 |
> |
ConfigFileEntry.glinefile = GPATH; /* Server gline file */ |
491 |
> |
ConfigFileEntry.xlinefile = XPATH; /* Server xline file */ |
492 |
> |
ConfigFileEntry.dlinefile = DLPATH; /* dline file */ |
493 |
> |
ConfigFileEntry.resvfile = RESVPATH; /* resv file */ |
494 |
> |
|
495 |
|
myargv = argv; |
496 |
|
umask(077); /* better safe than sorry --SRB */ |
497 |
|
|
499 |
|
|
500 |
|
if (printVersion) |
501 |
|
{ |
502 |
< |
printf("ircd: version %s\n", ircd_version); |
502 |
> |
printf("ircd: version %s(%s)\n", ircd_version, serno); |
503 |
|
exit(EXIT_SUCCESS); |
504 |
|
} |
505 |
|
|
509 |
|
exit(EXIT_FAILURE); |
510 |
|
} |
511 |
|
|
512 |
< |
init_ssl(); |
512 |
> |
ssl_init(); |
513 |
|
|
514 |
|
if (!server_state.foreground) |
515 |
|
{ |
521 |
|
|
522 |
|
setup_signals(); |
523 |
|
|
546 |
– |
get_ircd_platform(ircd_platform); |
547 |
– |
|
524 |
|
/* Init the event subsystem */ |
525 |
|
eventInit(); |
526 |
+ |
|
527 |
|
/* We need this to initialise the fd array before anything else */ |
528 |
|
fdlist_init(); |
529 |
< |
log_add_file(LOG_TYPE_IRCD, 0, logFileName); |
529 |
> |
log_set_file(LOG_TYPE_IRCD, 0, logFileName); |
530 |
|
check_can_use_v6(); |
531 |
< |
init_comm(); /* This needs to be setup early ! -- adrian */ |
531 |
> |
init_netio(); /* This needs to be setup early ! -- adrian */ |
532 |
> |
|
533 |
|
/* Check if there is pidfile and daemon already running */ |
534 |
|
check_pidfile(pidFileName); |
535 |
|
|
536 |
< |
initBlockHeap(); |
536 |
> |
mp_pool_init(); |
537 |
|
init_dlink_nodes(); |
538 |
< |
init_callbacks(); |
561 |
< |
initialize_message_files(); |
538 |
> |
init_isupport(); |
539 |
|
dbuf_init(); |
540 |
< |
init_hash(); |
540 |
> |
hash_init(); |
541 |
|
init_ip_hash_table(); /* client host ip hash table */ |
542 |
|
init_host_hash(); /* Host-hashtable. */ |
543 |
< |
clear_tree_parse(); |
544 |
< |
init_client(); |
545 |
< |
init_class(); |
569 |
< |
init_whowas(); |
543 |
> |
client_init(); |
544 |
> |
class_init(); |
545 |
> |
whowas_init(); |
546 |
|
watch_init(); |
547 |
< |
init_auth(); /* Initialise the auth code */ |
547 |
> |
auth_init(); /* Initialise the auth code */ |
548 |
|
init_resolver(); /* Needs to be setup before the io loop */ |
549 |
+ |
modules_init(); |
550 |
|
read_conf_files(1); /* cold start init conf files */ |
551 |
|
init_uid(); |
552 |
|
initialize_server_capabs(); /* Set up default_server_capabs */ |
553 |
|
initialize_global_set_options(); |
554 |
< |
init_channels(); |
554 |
> |
channel_init(); |
555 |
> |
read_links_file(); |
556 |
> |
motd_init(); |
557 |
> |
#ifdef HAVE_LIBGEOIP |
558 |
> |
geoip_ctx = GeoIP_new(GEOIP_MEMORY_CACHE); |
559 |
> |
#endif |
560 |
|
|
561 |
|
if (EmptyString(ServerInfo.sid)) |
562 |
|
{ |
594 |
|
|
595 |
|
hash_add_id(&me); |
596 |
|
hash_add_client(&me); |
597 |
< |
|
597 |
> |
|
598 |
|
/* add ourselves to global_serv_list */ |
599 |
|
dlinkAdd(&me, make_dlink_node(), &global_serv_list); |
600 |
|
|
601 |
+ |
load_kline_database(); |
602 |
+ |
load_dline_database(); |
603 |
+ |
load_gline_database(); |
604 |
+ |
load_xline_database(); |
605 |
+ |
load_resv_database(); |
606 |
+ |
|
607 |
|
if (chdir(MODPATH)) |
608 |
|
{ |
609 |
|
ilog(LOG_TYPE_IRCD, "Could not load core modules. Terminating!"); |
641 |
|
/* Setup the timeout check. I'll shift it later :) -- adrian */ |
642 |
|
eventAddIsh("comm_checktimeouts", comm_checktimeouts, NULL, 1); |
643 |
|
|
644 |
+ |
eventAddIsh("save_all_databases", save_all_databases, NULL, DATABASE_UPDATE_TIMEOUT); |
645 |
+ |
|
646 |
|
if (ConfigServerHide.links_delay > 0) |
647 |
|
eventAddIsh("write_links_file", write_links_file, NULL, ConfigServerHide.links_delay); |
648 |
|
else |