| 1 |
/* |
| 2 |
* ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd) |
| 3 |
* |
| 4 |
* Copyright (c) 1997-2018 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 |
| 8 |
* the Free Software Foundation; either version 2 of the License, or |
| 9 |
* (at your option) any later version. |
| 10 |
* |
| 11 |
* This program is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
* GNU General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU General Public License |
| 17 |
* along with this program; if not, write to the Free Software |
| 18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 |
| 19 |
* USA |
| 20 |
*/ |
| 21 |
|
| 22 |
/*! \file ircd.c |
| 23 |
* \brief Starts up and runs the ircd. |
| 24 |
* \version $Id$ |
| 25 |
*/ |
| 26 |
|
| 27 |
#include "stdinc.h" |
| 28 |
#include "user.h" |
| 29 |
#include "list.h" |
| 30 |
#include "ircd.h" |
| 31 |
#include "channel.h" |
| 32 |
#include "channel_mode.h" |
| 33 |
#include "client.h" |
| 34 |
#include "event.h" |
| 35 |
#include "fdlist.h" |
| 36 |
#include "hash.h" |
| 37 |
#include "id.h" |
| 38 |
#include "irc_string.h" |
| 39 |
#include "ircd_signal.h" |
| 40 |
#include "motd.h" |
| 41 |
#include "conf.h" |
| 42 |
#include "hostmask.h" |
| 43 |
#include "parse.h" |
| 44 |
#include "res.h" |
| 45 |
#include "restart.h" |
| 46 |
#include "rng_mt.h" |
| 47 |
#include "auth.h" |
| 48 |
#include "s_bsd.h" |
| 49 |
#include "log.h" |
| 50 |
#include "server.h" |
| 51 |
#include "server_capab.h" |
| 52 |
#include "send.h" |
| 53 |
#include "modules.h" |
| 54 |
#include "memory.h" |
| 55 |
#include "ircd_getopt.h" |
| 56 |
#include "conf_db.h" |
| 57 |
#include "conf_class.h" |
| 58 |
#include "ipcache.h" |
| 59 |
#include "isupport.h" |
| 60 |
|
| 61 |
|
| 62 |
struct SetOptions GlobalSetOptions; /* /quote set variables */ |
| 63 |
struct Counter Count; |
| 64 |
struct ServerState_t server_state; |
| 65 |
struct ServerStatistics ServerStats; |
| 66 |
struct ServerTime SystemTime; |
| 67 |
struct Connection meConnection; /* That's also part of me */ |
| 68 |
struct Client me = { .connection = &meConnection }; /* That's me */ |
| 69 |
|
| 70 |
char **myargv; |
| 71 |
const char *logFileName = LPATH; |
| 72 |
const char *pidFileName = PPATH; |
| 73 |
|
| 74 |
bool dorehash; |
| 75 |
bool doremotd; |
| 76 |
|
| 77 |
static bool printVersion; |
| 78 |
|
| 79 |
static struct lgetopt myopts[] = |
| 80 |
{ |
| 81 |
{ "configfile", &ConfigGeneral.configfile, |
| 82 |
STRING, "File to use for ircd.conf" }, |
| 83 |
{ "klinefile", &ConfigGeneral.klinefile, |
| 84 |
STRING, "File to use for kline database" }, |
| 85 |
{ "dlinefile", &ConfigGeneral.dlinefile, |
| 86 |
STRING, "File to use for dline database" }, |
| 87 |
{ "xlinefile", &ConfigGeneral.xlinefile, |
| 88 |
STRING, "File to use for xline database" }, |
| 89 |
{ "resvfile", &ConfigGeneral.resvfile, |
| 90 |
STRING, "File to use for resv database" }, |
| 91 |
{ "logfile", &logFileName, |
| 92 |
STRING, "File to use for ircd.log" }, |
| 93 |
{ "pidfile", &pidFileName, |
| 94 |
STRING, "File to use for process ID" }, |
| 95 |
{ "foreground", &server_state.foreground, |
| 96 |
YESNO, "Run in foreground (don't detach)" }, |
| 97 |
{ "version", &printVersion, |
| 98 |
YESNO, "Print version and exit" }, |
| 99 |
{ "help", NULL, USAGE, "Print this text" }, |
| 100 |
{ NULL, NULL, STRING, NULL }, |
| 101 |
}; |
| 102 |
|
| 103 |
static struct event event_cleanup_tklines = |
| 104 |
{ |
| 105 |
.name = "cleanup_tklines", |
| 106 |
.handler = cleanup_tklines, |
| 107 |
.when = CLEANUP_TKLINES_TIME |
| 108 |
}; |
| 109 |
|
| 110 |
static struct event event_try_connections = |
| 111 |
{ |
| 112 |
.name = "try_connections", |
| 113 |
.handler = try_connections, |
| 114 |
.when = STARTUP_CONNECTIONS_TIME |
| 115 |
}; |
| 116 |
|
| 117 |
static struct event event_comm_checktimeouts = |
| 118 |
{ |
| 119 |
.name = "comm_checktimeouts", |
| 120 |
.handler = comm_checktimeouts, |
| 121 |
.when = 1 |
| 122 |
}; |
| 123 |
|
| 124 |
static struct event event_save_all_databases = |
| 125 |
{ |
| 126 |
.name = "save_all_databases", |
| 127 |
.handler = save_all_databases, |
| 128 |
.when = DATABASE_UPDATE_TIMEOUT |
| 129 |
}; |
| 130 |
|
| 131 |
struct event event_write_links_file = |
| 132 |
{ |
| 133 |
.name = "write_links_file", |
| 134 |
.handler = write_links_file, |
| 135 |
}; |
| 136 |
|
| 137 |
|
| 138 |
void |
| 139 |
set_time(void) |
| 140 |
{ |
| 141 |
struct timeval newtime = { .tv_sec = 0, .tv_usec = 0 }; |
| 142 |
|
| 143 |
if (gettimeofday(&newtime, NULL) == -1) |
| 144 |
{ |
| 145 |
char buf[IRCD_BUFSIZE]; |
| 146 |
|
| 147 |
snprintf(buf, sizeof(buf), "Clock failure, TS can be corrupted: %s", |
| 148 |
strerror(errno)); |
| 149 |
server_die(buf, SERVER_SHUTDOWN); |
| 150 |
} |
| 151 |
|
| 152 |
if ((uintmax_t)newtime.tv_sec < CurrentTime) |
| 153 |
{ |
| 154 |
ilog(LOG_TYPE_IRCD, "System clock is running backwards - (%ju < %ju)", |
| 155 |
(uintmax_t)newtime.tv_sec, CurrentTime); |
| 156 |
sendto_realops_flags(UMODE_DEBUG, L_ALL, SEND_NOTICE, |
| 157 |
"System clock is running backwards - (%ju < %ju)", |
| 158 |
(uintmax_t)newtime.tv_sec, CurrentTime); |
| 159 |
event_set_back_events(CurrentTime - (uintmax_t)newtime.tv_sec); |
| 160 |
} |
| 161 |
|
| 162 |
SystemTime.tv_sec = newtime.tv_sec; |
| 163 |
SystemTime.tv_usec = newtime.tv_usec; |
| 164 |
} |
| 165 |
|
| 166 |
static void |
| 167 |
io_loop(void) |
| 168 |
{ |
| 169 |
while (1) |
| 170 |
{ |
| 171 |
if (listing_client_list.head) |
| 172 |
{ |
| 173 |
dlink_node *node = NULL, *node_next = NULL; |
| 174 |
DLINK_FOREACH_SAFE(node, node_next, listing_client_list.head) |
| 175 |
safe_list_channels(node->data, false); |
| 176 |
} |
| 177 |
|
| 178 |
/* Run pending events */ |
| 179 |
event_run(); |
| 180 |
|
| 181 |
comm_select(); |
| 182 |
exit_aborted_clients(); |
| 183 |
free_exited_clients(); |
| 184 |
|
| 185 |
/* Check to see whether we have to rehash the configuration. */ |
| 186 |
if (dorehash == true) |
| 187 |
{ |
| 188 |
conf_rehash(true); |
| 189 |
dorehash = false; |
| 190 |
} |
| 191 |
|
| 192 |
if (doremotd == true) |
| 193 |
{ |
| 194 |
motd_recache(); |
| 195 |
sendto_realops_flags(UMODE_SERVNOTICE, L_ALL, SEND_NOTICE, |
| 196 |
"Got signal SIGUSR1, reloading motd file(s)"); |
| 197 |
doremotd = false; |
| 198 |
} |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
/* initalialize_global_set_options() |
| 203 |
* |
| 204 |
* inputs - none |
| 205 |
* output - none |
| 206 |
* side effects - This sets all global set options needed |
| 207 |
*/ |
| 208 |
static void |
| 209 |
initialize_global_set_options(void) |
| 210 |
{ |
| 211 |
GlobalSetOptions.maxclients = ConfigServerInfo.default_max_clients; |
| 212 |
GlobalSetOptions.autoconn = 1; |
| 213 |
GlobalSetOptions.spam_time = MIN_JOIN_LEAVE_TIME; |
| 214 |
GlobalSetOptions.spam_num = MAX_JOIN_LEAVE_COUNT; |
| 215 |
GlobalSetOptions.floodcount = ConfigGeneral.default_floodcount; |
| 216 |
GlobalSetOptions.floodtime = ConfigGeneral.default_floodtime; |
| 217 |
GlobalSetOptions.joinfloodcount = ConfigChannel.default_join_flood_count; |
| 218 |
GlobalSetOptions.joinfloodtime = ConfigChannel.default_join_flood_time; |
| 219 |
GlobalSetOptions.ident_timeout = IDENT_TIMEOUT; |
| 220 |
} |
| 221 |
|
| 222 |
/* write_pidfile() |
| 223 |
* |
| 224 |
* inputs - filename+path of pid file |
| 225 |
* output - NONE |
| 226 |
* side effects - write the pid of the ircd to filename |
| 227 |
*/ |
| 228 |
static void |
| 229 |
write_pidfile(const char *filename) |
| 230 |
{ |
| 231 |
FILE *fb; |
| 232 |
|
| 233 |
if ((fb = fopen(filename, "w"))) |
| 234 |
{ |
| 235 |
char buf[IRCD_BUFSIZE]; |
| 236 |
unsigned int pid = (unsigned int)getpid(); |
| 237 |
|
| 238 |
snprintf(buf, sizeof(buf), "%u\n", pid); |
| 239 |
|
| 240 |
if (fputs(buf, fb) == -1) |
| 241 |
ilog(LOG_TYPE_IRCD, "Error writing to pid file %s: %s", |
| 242 |
filename, strerror(errno)); |
| 243 |
|
| 244 |
fclose(fb); |
| 245 |
} |
| 246 |
else |
| 247 |
ilog(LOG_TYPE_IRCD, "Error opening pid file %s: %s", |
| 248 |
filename, strerror(errno)); |
| 249 |
} |
| 250 |
|
| 251 |
/* check_pidfile() |
| 252 |
* |
| 253 |
* inputs - filename+path of pid file |
| 254 |
* output - none |
| 255 |
* side effects - reads pid from pidfile and checks if ircd is in process |
| 256 |
* list. if it is, gracefully exits |
| 257 |
* -kre |
| 258 |
*/ |
| 259 |
static void |
| 260 |
check_pidfile(const char *filename) |
| 261 |
{ |
| 262 |
FILE *fb; |
| 263 |
char buf[IRCD_BUFSIZE]; |
| 264 |
|
| 265 |
if ((fb = fopen(filename, "r"))) |
| 266 |
{ |
| 267 |
if (!fgets(buf, 20, fb)) |
| 268 |
ilog(LOG_TYPE_IRCD, "Error reading from pid file %s: %s", |
| 269 |
filename, strerror(errno)); |
| 270 |
else |
| 271 |
{ |
| 272 |
pid_t pid = atoi(buf); |
| 273 |
|
| 274 |
if (!kill(pid, 0)) |
| 275 |
{ |
| 276 |
/* log(L_ERROR, "Server is already running"); */ |
| 277 |
printf("ircd: daemon is already running\n"); |
| 278 |
exit(EXIT_FAILURE); |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
fclose(fb); |
| 283 |
} |
| 284 |
else if (errno != ENOENT) |
| 285 |
ilog(LOG_TYPE_IRCD, "Error opening pid file %s: %s", |
| 286 |
filename, strerror(errno)); |
| 287 |
} |
| 288 |
|
| 289 |
/* setup_corefile() |
| 290 |
* |
| 291 |
* inputs - nothing |
| 292 |
* output - nothing |
| 293 |
* side effects - setups corefile to system limits. |
| 294 |
* -kre |
| 295 |
*/ |
| 296 |
static void |
| 297 |
setup_corefile(void) |
| 298 |
{ |
| 299 |
struct rlimit rlim; /* resource limits */ |
| 300 |
|
| 301 |
/* Set corefilesize to maximum */ |
| 302 |
if (!getrlimit(RLIMIT_CORE, &rlim)) |
| 303 |
{ |
| 304 |
rlim.rlim_cur = rlim.rlim_max; |
| 305 |
setrlimit(RLIMIT_CORE, &rlim); |
| 306 |
} |
| 307 |
} |
| 308 |
|
| 309 |
/* |
| 310 |
* print_startup - print startup information |
| 311 |
*/ |
| 312 |
static void |
| 313 |
print_startup(int pid) |
| 314 |
{ |
| 315 |
printf("ircd: version %s(%s)\n", ircd_version, serno); |
| 316 |
printf("ircd: pid %d\n", pid); |
| 317 |
printf("ircd: running in %s mode from %s\n", !server_state.foreground ? "background" |
| 318 |
: "foreground", ConfigGeneral.dpath); |
| 319 |
} |
| 320 |
|
| 321 |
static void |
| 322 |
make_daemon(void) |
| 323 |
{ |
| 324 |
int pid; |
| 325 |
|
| 326 |
if ((pid = fork()) < 0) |
| 327 |
{ |
| 328 |
perror("fork"); |
| 329 |
exit(EXIT_FAILURE); |
| 330 |
} |
| 331 |
else if (pid > 0) |
| 332 |
{ |
| 333 |
print_startup(pid); |
| 334 |
exit(EXIT_SUCCESS); |
| 335 |
} |
| 336 |
|
| 337 |
setsid(); |
| 338 |
} |
| 339 |
|
| 340 |
int |
| 341 |
main(int argc, char *argv[]) |
| 342 |
{ |
| 343 |
/* Check to see if the user is running us as root, which is a nono */ |
| 344 |
if (!geteuid()) |
| 345 |
{ |
| 346 |
fprintf(stderr, "ERROR: This server won't run as root/superuser\n"); |
| 347 |
return -1; |
| 348 |
} |
| 349 |
|
| 350 |
/* Setup corefile size immediately after boot -kre */ |
| 351 |
setup_corefile(); |
| 352 |
|
| 353 |
/* Save server boot time right away, so getrusage works correctly */ |
| 354 |
set_time(); |
| 355 |
|
| 356 |
/* It's not random, but it ought to be a little harder to guess */ |
| 357 |
init_genrand(SystemTime.tv_sec ^ (SystemTime.tv_usec | (getpid() << 20))); |
| 358 |
|
| 359 |
ConfigGeneral.dpath = DPATH; |
| 360 |
ConfigGeneral.spath = SPATH; |
| 361 |
ConfigGeneral.mpath = MPATH; |
| 362 |
ConfigGeneral.configfile = CPATH; /* Server configuration file */ |
| 363 |
ConfigGeneral.klinefile = KPATH; /* Server kline file */ |
| 364 |
ConfigGeneral.xlinefile = XPATH; /* Server xline file */ |
| 365 |
ConfigGeneral.dlinefile = DLPATH; /* dline file */ |
| 366 |
ConfigGeneral.resvfile = RESVPATH; /* resv file */ |
| 367 |
|
| 368 |
myargv = argv; |
| 369 |
umask(077); /* umask 077: u=rwx,g=,o= */ |
| 370 |
|
| 371 |
parseargs(&argc, &argv, myopts); |
| 372 |
|
| 373 |
if (printVersion == true) |
| 374 |
{ |
| 375 |
printf("ircd: version %s(%s)\n", ircd_version, serno); |
| 376 |
exit(EXIT_SUCCESS); |
| 377 |
} |
| 378 |
|
| 379 |
if (chdir(ConfigGeneral.dpath)) |
| 380 |
{ |
| 381 |
perror("chdir"); |
| 382 |
exit(EXIT_FAILURE); |
| 383 |
} |
| 384 |
|
| 385 |
if (server_state.foreground == false) |
| 386 |
{ |
| 387 |
make_daemon(); |
| 388 |
close_standard_fds(); /* this needs to be before comm_select_init()! */ |
| 389 |
} |
| 390 |
else |
| 391 |
print_startup(getpid()); |
| 392 |
|
| 393 |
setup_signals(); |
| 394 |
|
| 395 |
/* We need this to initialise the fd array before anything else */ |
| 396 |
fdlist_init(); |
| 397 |
log_set_file(LOG_TYPE_IRCD, 0, logFileName); |
| 398 |
|
| 399 |
comm_select_init(); /* This needs to be setup early ! -- adrian */ |
| 400 |
tls_init(); |
| 401 |
|
| 402 |
/* Check if there is pidfile and daemon already running */ |
| 403 |
check_pidfile(pidFileName); |
| 404 |
|
| 405 |
isupport_init(); |
| 406 |
hash_init(); |
| 407 |
ipcache_init(); |
| 408 |
client_init(); |
| 409 |
class_init(); |
| 410 |
auth_init(); /* Initialise the auth code */ |
| 411 |
resolver_init(); /* Needs to be setup before the io loop */ |
| 412 |
modules_init(); |
| 413 |
read_conf_files(true); /* cold start init conf files */ |
| 414 |
capab_init(); /* Set up default_server_capabs */ |
| 415 |
initialize_global_set_options(); /* Has to be called after read_conf_files() */ |
| 416 |
channel_mode_init(); |
| 417 |
read_links_file(); |
| 418 |
motd_init(); |
| 419 |
user_modes_init(); |
| 420 |
|
| 421 |
if (EmptyString(ConfigServerInfo.name)) |
| 422 |
{ |
| 423 |
ilog(LOG_TYPE_IRCD, "ERROR: No server name specified in serverinfo block."); |
| 424 |
exit(EXIT_FAILURE); |
| 425 |
} |
| 426 |
|
| 427 |
strlcpy(me.name, ConfigServerInfo.name, sizeof(me.name)); |
| 428 |
|
| 429 |
/* serverinfo {} description must exist. If not, error out.*/ |
| 430 |
if (EmptyString(ConfigServerInfo.description)) |
| 431 |
{ |
| 432 |
ilog(LOG_TYPE_IRCD, "ERROR: No server description specified in serverinfo block."); |
| 433 |
exit(EXIT_FAILURE); |
| 434 |
} |
| 435 |
|
| 436 |
strlcpy(me.info, ConfigServerInfo.description, sizeof(me.info)); |
| 437 |
|
| 438 |
if (EmptyString(ConfigServerInfo.sid)) |
| 439 |
{ |
| 440 |
ilog(LOG_TYPE_IRCD, "Generating server ID"); |
| 441 |
generate_sid(); |
| 442 |
} |
| 443 |
else |
| 444 |
strlcpy(me.id, ConfigServerInfo.sid, sizeof(me.id)); |
| 445 |
|
| 446 |
init_uid(); |
| 447 |
|
| 448 |
me.from = &me; |
| 449 |
me.servptr = &me; |
| 450 |
me.connection->lasttime = CurrentTime; |
| 451 |
me.connection->since = CurrentTime; |
| 452 |
me.connection->firsttime = CurrentTime; |
| 453 |
|
| 454 |
SetMe(&me); |
| 455 |
server_make(&me); |
| 456 |
|
| 457 |
hash_add_id(&me); |
| 458 |
hash_add_client(&me); |
| 459 |
|
| 460 |
dlinkAdd(&me, &me.node, &global_server_list); |
| 461 |
|
| 462 |
load_kline_database(ConfigGeneral.klinefile); |
| 463 |
load_dline_database(ConfigGeneral.dlinefile); |
| 464 |
load_xline_database(ConfigGeneral.xlinefile); |
| 465 |
load_resv_database(ConfigGeneral.resvfile); |
| 466 |
|
| 467 |
load_all_modules(true); |
| 468 |
load_conf_modules(); |
| 469 |
load_core_modules(true); |
| 470 |
|
| 471 |
write_pidfile(pidFileName); |
| 472 |
|
| 473 |
event_addish(&event_cleanup_tklines, NULL); |
| 474 |
|
| 475 |
/* We want try_connections to be called as soon as possible now! -- adrian */ |
| 476 |
/* No, 'cause after a restart it would cause all sorts of nick collides */ |
| 477 |
event_addish(&event_try_connections, NULL); |
| 478 |
|
| 479 |
/* Setup the timeout check. I'll shift it later :) -- adrian */ |
| 480 |
event_add(&event_comm_checktimeouts, NULL); |
| 481 |
|
| 482 |
event_addish(&event_save_all_databases, NULL); |
| 483 |
|
| 484 |
if (ConfigServerHide.flatten_links_delay && event_write_links_file.active == false) |
| 485 |
{ |
| 486 |
event_write_links_file.when = ConfigServerHide.flatten_links_delay; |
| 487 |
event_add(&event_write_links_file, NULL); |
| 488 |
} |
| 489 |
|
| 490 |
ilog(LOG_TYPE_IRCD, "Server ready. Running version: %s(%s)", ircd_version, serno); |
| 491 |
io_loop(); |
| 492 |
|
| 493 |
return 0; |
| 494 |
} |