]> jfr.im git - solanum.git/blame - ircd/ircd.c
ircd: call relocate_paths() in all cases
[solanum.git] / ircd / ircd.c
CommitLineData
212380e3 1/*
6fce54ff 2 * charybdis: A slightly useful ircd.
212380e3
AC
3 * ircd.c: Starts up and runs the ircd.
4 *
5 * Copyright (C) 1990 Jarkko Oikarinen and University of Oulu, Co Center
6 * Copyright (C) 1996-2002 Hybrid Development Team
6fce54ff 7 * Copyright (C) 2002-2008 ircd-ratbox development team
adef4da1 8 * Copyright (C) 2005-2013 charybdis development team
212380e3
AC
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 * USA
212380e3
AC
24 */
25
fe037171 26#include "rb_lib.h"
212380e3
AC
27#include "stdinc.h"
28#include "setup.h"
9b8e9eb3 29#include "defaults.h"
212380e3
AC
30#include "ircd.h"
31#include "channel.h"
32#include "class.h"
33#include "client.h"
212380e3 34#include "hash.h"
4562c604 35#include "match.h"
212380e3 36#include "ircd_signal.h"
212380e3
AC
37#include "msg.h" /* msgtab */
38#include "hostmask.h"
39#include "numeric.h"
40#include "parse.h"
212380e3 41#include "restart.h"
212380e3 42#include "s_conf.h"
4016731b 43#include "logger.h"
212380e3
AC
44#include "s_serv.h" /* try_connections */
45#include "s_user.h"
46#include "s_stats.h"
47#include "scache.h"
48#include "send.h"
49#include "supported.h"
50#include "whowas.h"
51#include "modules.h"
212380e3
AC
52#include "hook.h"
53#include "ircd_getopt.h"
212380e3 54#include "newconf.h"
212380e3 55#include "reject.h"
212380e3
AC
56#include "s_newconf.h"
57#include "cache.h"
58#include "monitor.h"
212380e3
AC
59#include "patchlevel.h"
60#include "serno.h"
c6d72037 61#include "sslproc.h"
c53ca1e0 62#include "wsproc.h"
efccc22c 63#include "chmode.h"
39a68b53 64#include "privilege.h"
6eebc373 65#include "bandbi.h"
64fae260 66#include "authproc.h"
b02a913b 67#include "operhash.h"
212380e3 68
df3db5d9
AJ
69static void
70ircd_die_cb(const char *str) __attribute__((noreturn));
71
8bd5767b 72/* /quote set variables */
2af8c7ff 73struct SetOptions GlobalSetOptions;
8bd5767b
JT
74
75/* configuration set from ircd.conf */
76struct config_file_entry ConfigFileEntry;
77/* server info set from ircd.conf */
78struct server_info ServerInfo;
79/* admin info set from ircd.conf */
1aad9782
VY
80struct admin_info AdminInfo;
81
8bd5767b 82struct Counter Count;
47adde3d
VY
83struct ServerStatistics ServerStats;
84
1aad9782 85int maxconnections;
8bd5767b 86struct Client me; /* That's me */
1aad9782
VY
87struct LocalUser meLocalUser; /* That's also part of me */
88
3d3d1a95 89rb_dlink_list global_client_list;
8bd5767b 90
3d3d1a95 91/* unknown/client pointer lists */
8bd5767b 92rb_dlink_list unknown_list; /* unknown clients ON this server only */
3d3d1a95 93rb_dlink_list lclient_list; /* local clients only ON this server */
8bd5767b
JT
94rb_dlink_list serv_list; /* local servers to this server ONLY */
95rb_dlink_list global_serv_list; /* global servers on the network */
96rb_dlink_list local_oper_list; /* our opers, duplicated in lclient_list */
b5b84cad
VY
97rb_dlink_list oper_list; /* network opers */
98
5adde7a4 99char * const *myargv;
33d43d4f
MU
100volatile sig_atomic_t dorehash = false;
101volatile sig_atomic_t dorehashbans = false;
102volatile sig_atomic_t doremotd = false;
f66f0baa 103bool kline_queued = false;
503727d1
EM
104bool server_state_foreground = false;
105bool opers_see_all_users = false;
bfc44622
AC
106bool ircd_ssl_ok = false;
107bool ircd_zlib_ok = true;
8bd5767b
JT
108
109int testing_conf = 0;
cd27d99c 110time_t startup_time;
8bd5767b 111
346fba92 112int default_server_capabs;
8bd5767b 113
cd27d99c
VY
114int splitmode;
115int splitchecking;
116int split_users;
117int split_servers;
118int eob_count;
c6d72037 119
4d8cfacd
AC
120const char *ircd_paths[IRCD_PATH_COUNT] = {
121 [IRCD_PATH_PREFIX] = DPATH,
122 [IRCD_PATH_MODULES] = MODPATH,
123 [IRCD_PATH_AUTOLOAD_MODULES] = AUTOMODPATH,
124 [IRCD_PATH_ETC] = ETCPATH,
125 [IRCD_PATH_LOG] = LOGPATH,
126 [IRCD_PATH_USERHELP] = UHPATH,
127 [IRCD_PATH_OPERHELP] = HPATH,
128 [IRCD_PATH_IRCD_EXEC] = SPATH,
129 [IRCD_PATH_IRCD_CONF] = CPATH,
130 [IRCD_PATH_IRCD_MOTD] = MPATH,
131 [IRCD_PATH_IRCD_LOG] = LPATH,
132 [IRCD_PATH_IRCD_PID] = PPATH,
133 [IRCD_PATH_IRCD_OMOTD] = OPATH,
134 [IRCD_PATH_BANDB] = DBPATH,
135 [IRCD_PATH_BIN] = BINPATH,
136 [IRCD_PATH_LIBEXEC] = PKGLIBEXECDIR,
137};
138
e0e0c415
AC
139const char *ircd_pathnames[IRCD_PATH_COUNT] = {
140 [IRCD_PATH_PREFIX] = "prefix",
141 [IRCD_PATH_MODULES] = "modules",
142 [IRCD_PATH_AUTOLOAD_MODULES] = "autoload modules",
143 [IRCD_PATH_ETC] = "config",
144 [IRCD_PATH_LOG] = "log",
145 [IRCD_PATH_USERHELP] = "user help",
146 [IRCD_PATH_OPERHELP] = "oper help",
147 [IRCD_PATH_IRCD_EXEC] = "ircd binary",
148 [IRCD_PATH_IRCD_CONF] = "ircd.conf",
149 [IRCD_PATH_IRCD_MOTD] = "ircd.motd",
150 [IRCD_PATH_IRCD_LOG] = "ircd.log",
151 [IRCD_PATH_IRCD_PID] = "ircd.pid",
152 [IRCD_PATH_IRCD_OMOTD] = "oper motd",
153 [IRCD_PATH_BANDB] = "bandb",
154 [IRCD_PATH_BIN] = "binary dir",
155 [IRCD_PATH_LIBEXEC] = "libexec dir",
156};
157
2f545aad
AC
158const char *logFileName = NULL;
159const char *pidFileName = NULL;
4d8cfacd 160
fd5af3d0
VY
161void
162ircd_shutdown(const char *reason)
163{
164 struct Client *target_p;
165 rb_dlink_node *ptr;
166
167 RB_DLINK_FOREACH(ptr, lclient_list.head)
168 {
169 target_p = ptr->data;
170
171 sendto_one(target_p, ":%s NOTICE %s :Server Terminating. %s",
172 me.name, target_p->name, reason);
173 }
174
175 RB_DLINK_FOREACH(ptr, serv_list.head)
176 {
177 target_p = ptr->data;
178
179 sendto_one(target_p, ":%s ERROR :Terminated by %s",
180 me.name, reason);
181 }
182
183 ilog(L_MAIN, "Server Terminating. %s", reason);
ea82a3ca 184 close_logfiles();
fd5af3d0
VY
185
186 unlink(pidFileName);
187 exit(0);
188}
189
212380e3
AC
190/*
191 * print_startup - print startup information
192 */
193static void
194print_startup(int pid)
195{
b45b2dae
JT
196 int fd;
197
c1bfea73 198#ifndef _WIN32
b45b2dae
JT
199 close(1);
200 fd = open("/dev/null", O_RDWR);
201 if (fd == -1) {
202 perror("open /dev/null");
203 exit(EXIT_FAILURE);
204 }
205 if (fd == 0)
206 fd = dup(fd);
207 if (fd != 1)
208 abort();
c1bfea73 209#endif
212380e3
AC
210 inotice("now running in %s mode from %s as pid %d ...",
211 !server_state_foreground ? "background" : "foreground",
212 ConfigFileEntry.dpath, pid);
213
c1bfea73 214#ifndef _WIN32
212380e3
AC
215 /* let the parent process know the initialization was successful
216 * -- jilles */
217 if (!server_state_foreground)
c084fcaf 218 {
2ac4ba96
EM
219 /* GCC complains on Linux if we don't check the value of write pedantically.
220 * Technically you're supposed to check the value, yes, but it probably can't fail.
221 * No, casting to void is of no use to shut the warning up. You HAVE to use the value.
222 * --Elizfaox
223 */
224 if(write(0, ".", 1) < 1)
225 abort();
c084fcaf 226 }
b45b2dae
JT
227 if (dup2(1, 0) == -1)
228 abort();
229 if (dup2(1, 2) == -1)
230 abort();
c1bfea73 231#endif
212380e3
AC
232}
233
212380e3
AC
234/*
235 * init_sys
236 *
237 * inputs - boot_daemon flag
238 * output - none
239 * side effects - if boot_daemon flag is not set, don't daemonize
240 */
241static void
242init_sys(void)
243{
c1bfea73 244#if !defined(_WIN32) && defined(RLIMIT_NOFILE) && defined(HAVE_SYS_RESOURCE_H)
8bd5767b
JT
245 struct rlimit limit;
246
247 if(!getrlimit(RLIMIT_NOFILE, &limit))
248 {
249 maxconnections = limit.rlim_cur;
250 if(maxconnections <= MAX_BUFFER)
251 {
252 fprintf(stderr, "ERROR: Shell FD limits are too low.\n");
ec86417c 253 fprintf(stderr, "ERROR: charybdis reserves %d FDs, shell limits must be above this\n", MAX_BUFFER);
8bd5767b
JT
254 exit(EXIT_FAILURE);
255 }
256 return;
257 }
258#endif /* RLIMIT_FD_MAX */
101db4c4 259 maxconnections = MAXCONNECTIONS;
212380e3
AC
260}
261
262static int
263make_daemon(void)
264{
538d2089 265#ifndef _WIN32
212380e3
AC
266 int pid;
267 int pip[2];
268 char c;
269
270 if (pipe(pip) < 0)
271 {
272 perror("pipe");
273 exit(EXIT_FAILURE);
274 }
275 dup2(pip[1], 0);
276 close(pip[1]);
277 if((pid = fork()) < 0)
278 {
279 perror("fork");
280 exit(EXIT_FAILURE);
281 }
282 else if(pid > 0)
283 {
284 close(0);
285 /* Wait for initialization to finish, successfully or
286 * unsuccessfully. Until this point the child may still
287 * write to stdout/stderr.
288 * -- jilles */
289 if (read(pip[0], &c, 1) > 0)
290 exit(EXIT_SUCCESS);
291 else
292 exit(EXIT_FAILURE);
293 }
294
295 close(pip[0]);
296 setsid();
297/* fclose(stdin);
298 fclose(stdout);
299 fclose(stderr); */
538d2089 300#endif
212380e3
AC
301 return 0;
302}
303
304static int printVersion = 0;
305
306struct lgetopt myopts[] = {
212380e3
AC
307 {"configfile", &ConfigFileEntry.configfile,
308 STRING, "File to use for ircd.conf"},
212380e3
AC
309 {"logfile", &logFileName,
310 STRING, "File to use for ircd.log"},
311 {"pidfile", &pidFileName,
312 STRING, "File to use for process ID"},
313 {"foreground", &server_state_foreground,
314 YESNO, "Run in foreground (don't detach)"},
315 {"version", &printVersion,
316 YESNO, "Print version and exit"},
317 {"conftest", &testing_conf,
318 YESNO, "Test the configuration files and exit"},
319 {"help", NULL, USAGE, "Print this text"},
320 {NULL, NULL, STRING, NULL},
321};
322
212380e3
AC
323static void
324check_rehash(void *unused)
325{
326 /*
327 * Check to see whether we have to rehash the configuration ..
328 */
329 if(dorehash)
330 {
ab31d2b0 331 rehash(true);
1b916de5 332 dorehash = false;
212380e3
AC
333 }
334
335 if(dorehashbans)
336 {
ab31d2b0 337 rehash_bans();
1b916de5 338 dorehashbans = false;
212380e3
AC
339 }
340
341 if(doremotd)
342 {
343 sendto_realops_snomask(SNO_GENERAL, L_ALL,
344 "Got signal SIGUSR1, reloading ircd motd file");
041d54ff 345 cache_user_motd();
1b916de5 346 doremotd = false;
212380e3
AC
347 }
348}
349
212380e3
AC
350/*
351 * initalialize_global_set_options
352 *
353 * inputs - none
354 * output - none
55abcbb2 355 * side effects - This sets all global set options needed
212380e3
AC
356 */
357static void
358initialize_global_set_options(void)
359{
360 memset(&GlobalSetOptions, 0, sizeof(GlobalSetOptions));
361 /* memset( &ConfigFileEntry, 0, sizeof(ConfigFileEntry)); */
362
8bd5767b
JT
363 GlobalSetOptions.maxclients = ServerInfo.default_max_clients;
364
365 if(GlobalSetOptions.maxclients > (maxconnections - MAX_BUFFER) || (GlobalSetOptions.maxclients <= 0))
101db4c4
VY
366 GlobalSetOptions.maxclients = maxconnections - MAX_BUFFER;
367
212380e3
AC
368 GlobalSetOptions.autoconn = 1;
369
370 GlobalSetOptions.spam_time = MIN_JOIN_LEAVE_TIME;
371 GlobalSetOptions.spam_num = MAX_JOIN_LEAVE_COUNT;
372
7f3382fe 373 GlobalSetOptions.floodcount = ConfigFileEntry.default_floodcount;
212380e3
AC
374
375 split_servers = ConfigChannel.default_split_server_count;
376 split_users = ConfigChannel.default_split_user_count;
377
378 if(split_users && split_servers
379 && (ConfigChannel.no_create_on_split || ConfigChannel.no_join_on_split))
380 {
381 splitmode = 1;
382 splitchecking = 1;
383 }
384
7f3382fe 385 GlobalSetOptions.ident_timeout = ConfigFileEntry.default_ident_timeout;
212380e3 386
f427c8b0 387 rb_strlcpy(GlobalSetOptions.operstring,
212380e3
AC
388 ConfigFileEntry.default_operstring,
389 sizeof(GlobalSetOptions.operstring));
f427c8b0 390 rb_strlcpy(GlobalSetOptions.adminstring,
212380e3
AC
391 ConfigFileEntry.default_adminstring,
392 sizeof(GlobalSetOptions.adminstring));
393
394 /* memset( &ConfigChannel, 0, sizeof(ConfigChannel)); */
395
396 /* End of global set options */
397
398}
399
400/*
401 * initialize_server_capabs
402 *
403 * inputs - none
404 * output - none
405 */
406static void
407initialize_server_capabs(void)
408{
409 default_server_capabs &= ~CAP_ZIP;
410}
411
d6c17e5d
AC
412/*
413 * relocate_paths
414 *
415 * inputs - none
416 * output - none
417 * side effects - items in ircd_paths[] array are relocated
418 */
0aaa37f7 419#ifdef _WIN32
d6c17e5d
AC
420static void
421relocate_paths(void)
422{
423 char prefix[PATH_MAX], workbuf[PATH_MAX];
424 char *p;
425
426 rb_strlcpy(prefix, rb_path_to_self(), sizeof prefix);
427
428 ircd_paths[IRCD_PATH_IRCD_EXEC] = rb_strdup(prefix);
429
430 /* if we're running from inside the source tree, we probably do not want to relocate any other paths */
431 if (strstr(prefix, ".libs") != NULL)
432 return;
433
434 /* prefix = /home/kaniini/ircd/bin/ircd */
435 p = strrchr(prefix, RB_PATH_SEPARATOR);
436 if (rb_unlikely(p == NULL))
437 return;
438 *p = 0;
439
440 /* prefix = /home/kaniini/ircd/bin */
441 p = strrchr(prefix, RB_PATH_SEPARATOR);
442 if (rb_unlikely(p == NULL))
443 return;
444 *p = 0;
445
446 /* prefix = /home/kaniini/ircd */
447 ircd_paths[IRCD_PATH_PREFIX] = rb_strdup(prefix);
448
449 /* now that we have our prefix, we can relocate the other paths... */
450 snprintf(workbuf, sizeof workbuf, "%s%cmodules", prefix, RB_PATH_SEPARATOR);
451 ircd_paths[IRCD_PATH_MODULES] = rb_strdup(workbuf);
452
453 snprintf(workbuf, sizeof workbuf, "%s%cmodules%cautoload", prefix, RB_PATH_SEPARATOR, RB_PATH_SEPARATOR);
454 ircd_paths[IRCD_PATH_AUTOLOAD_MODULES] = rb_strdup(workbuf);
455
456 snprintf(workbuf, sizeof workbuf, "%s%cetc", prefix, RB_PATH_SEPARATOR);
457 ircd_paths[IRCD_PATH_ETC] = rb_strdup(workbuf);
458
459 snprintf(workbuf, sizeof workbuf, "%s%clog", prefix, RB_PATH_SEPARATOR);
460 ircd_paths[IRCD_PATH_LOG] = rb_strdup(workbuf);
461
462 snprintf(workbuf, sizeof workbuf, "%s%chelp%cusers", prefix, RB_PATH_SEPARATOR, RB_PATH_SEPARATOR);
463 ircd_paths[IRCD_PATH_USERHELP] = rb_strdup(workbuf);
464
465 snprintf(workbuf, sizeof workbuf, "%s%chelp%copers", prefix, RB_PATH_SEPARATOR, RB_PATH_SEPARATOR);
466 ircd_paths[IRCD_PATH_OPERHELP] = rb_strdup(workbuf);
467
468 snprintf(workbuf, sizeof workbuf, "%s%cetc%circd.conf", prefix, RB_PATH_SEPARATOR, RB_PATH_SEPARATOR);
469 ircd_paths[IRCD_PATH_IRCD_CONF] = rb_strdup(workbuf);
470
471 snprintf(workbuf, sizeof workbuf, "%s%cetc%circd.motd", prefix, RB_PATH_SEPARATOR, RB_PATH_SEPARATOR);
472 ircd_paths[IRCD_PATH_IRCD_MOTD] = rb_strdup(workbuf);
473
474 snprintf(workbuf, sizeof workbuf, "%s%cetc%copers.motd", prefix, RB_PATH_SEPARATOR, RB_PATH_SEPARATOR);
475 ircd_paths[IRCD_PATH_IRCD_OMOTD] = rb_strdup(workbuf);
476
477 snprintf(workbuf, sizeof workbuf, "%s%cetc%cban.db", prefix, RB_PATH_SEPARATOR, RB_PATH_SEPARATOR);
478 ircd_paths[IRCD_PATH_BANDB] = rb_strdup(workbuf);
479
480 snprintf(workbuf, sizeof workbuf, "%s%cetc%circd.pid", prefix, RB_PATH_SEPARATOR, RB_PATH_SEPARATOR);
481 ircd_paths[IRCD_PATH_IRCD_PID] = rb_strdup(workbuf);
482
483 snprintf(workbuf, sizeof workbuf, "%s%clogs%circd.log", prefix, RB_PATH_SEPARATOR, RB_PATH_SEPARATOR);
484 ircd_paths[IRCD_PATH_IRCD_LOG] = rb_strdup(workbuf);
485
486 snprintf(workbuf, sizeof workbuf, "%s%cbin", prefix, RB_PATH_SEPARATOR);
487 ircd_paths[IRCD_PATH_BIN] = rb_strdup(workbuf);
488 ircd_paths[IRCD_PATH_LIBEXEC] = rb_strdup(workbuf);
e0e0c415
AC
489
490 inotice("runtime paths:")
491 for (int i = 0; i < IRCD_PATH_COUNT; i++)
492 {
493 inotice(" %s: %s", ircd_pathnames[i], ircd_paths[i]);
494 }
d6c17e5d 495}
0aaa37f7 496#endif
212380e3
AC
497
498/*
499 * write_pidfile
500 *
501 * inputs - filename+path of pid file
502 * output - none
503 * side effects - write the pid of the ircd to filename
504 */
505static void
506write_pidfile(const char *filename)
507{
508 FILE *fb;
509 char buff[32];
510 if((fb = fopen(filename, "w")))
511 {
512 unsigned int pid = (unsigned int) getpid();
513
5203cba5 514 snprintf(buff, sizeof(buff), "%u\n", pid);
212380e3
AC
515 if((fputs(buff, fb) == -1))
516 {
517 ilog(L_MAIN, "Error writing %u to pid file %s (%s)",
518 pid, filename, strerror(errno));
519 }
520 fclose(fb);
521 return;
522 }
523 else
524 {
525 ilog(L_MAIN, "Error opening pid file %s", filename);
526 }
527}
528
529/*
530 * check_pidfile
531 *
532 * inputs - filename+path of pid file
533 * output - none
534 * side effects - reads pid from pidfile and checks if ircd is in process
535 * list. if it is, gracefully exits
536 * -kre
537 */
538static void
539check_pidfile(const char *filename)
540{
541 FILE *fb;
542 char buff[32];
543 pid_t pidfromfile;
544
545 /* Don't do logging here, since we don't have log() initialised */
546 if((fb = fopen(filename, "r")))
547 {
548 if(fgets(buff, 20, fb) != NULL)
549 {
550 pidfromfile = atoi(buff);
538d2089 551 if(!rb_kill(pidfromfile, 0))
212380e3
AC
552 {
553 printf("ircd: daemon is already running\n");
554 exit(-1);
555 }
556 }
557 fclose(fb);
558 }
559}
560
561/*
562 * setup_corefile
563 *
564 * inputs - nothing
565 * output - nothing
566 * side effects - setups corefile to system limits.
567 * -kre
568 */
569static void
570setup_corefile(void)
571{
572#ifdef HAVE_SYS_RESOURCE_H
573 struct rlimit rlim; /* resource limits */
574
575 /* Set corefilesize to maximum */
576 if(!getrlimit(RLIMIT_CORE, &rlim))
577 {
578 rlim.rlim_cur = rlim.rlim_max;
579 setrlimit(RLIMIT_CORE, &rlim);
580 }
581#endif
582}
583
6fce54ff
VY
584static void
585ircd_log_cb(const char *str)
586{
fe037171 587 ilog(L_MAIN, "librb reports: %s", str);
6fce54ff
VY
588}
589
590static void
591ircd_restart_cb(const char *str)
592{
fe037171 593 inotice("librb has called the restart callback: %s", str);
6fce54ff
VY
594 restart(str);
595}
596
597/*
598 * Why EXIT_FAILURE here?
599 * Because if ircd_die_cb() is called it's because of a fatal
600 * error inside libcharybdis, and we don't know how to handle the
601 * exception, so it is logical to return a FAILURE exit code here.
602 * --nenolod
603 */
604static void
605ircd_die_cb(const char *str)
606{
607 if(str != NULL)
608 {
609 /* Try to get the message out to currently logged in operators. */
fe037171
EM
610 sendto_realops_snomask(SNO_GENERAL, L_NETWIDE, "librb has called the die callback..aborting: %s", str);
611 inotice("librb has called the die callback..aborting: %s", str);
6fce54ff 612 }
6643434b 613 else
fe037171 614 inotice("librb has called the die callback..aborting");
6fce54ff
VY
615
616 unlink(pidFileName);
617 exit(EXIT_FAILURE);
618}
619
fa832850
AC
620struct ev_entry *check_splitmode_ev = NULL;
621
d80645d0
VY
622static int
623seed_with_urandom(void)
624{
625 unsigned int seed;
626 int fd;
627
628 fd = open("/dev/urandom", O_RDONLY);
629 if(fd >= 0)
630 {
631 if(read(fd, &seed, sizeof(seed)) == sizeof(seed))
632 {
633 close(fd);
634 srand(seed);
635 return 1;
636 }
5457b102 637 close(fd);
d80645d0
VY
638 }
639 return 0;
640}
641
642static void
643seed_with_clock(void)
644{
55abcbb2 645 const struct timeval *tv;
d80645d0
VY
646 rb_set_time();
647 tv = rb_current_time_tv();
648 srand(tv->tv_sec ^ (tv->tv_usec | (getpid() << 20)));
649}
650
651static void
652seed_random(void *unused)
653{
654 unsigned int seed;
655 if(rb_get_random(&seed, sizeof(seed)) == -1)
656 {
657 if(!seed_with_urandom())
658 seed_with_clock();
659 return;
660 }
661 srand(seed);
662}
663
212380e3
AC
664/*
665 * main
666 *
667 * Initializes the IRCd.
668 *
669 * Inputs - number of commandline args, args themselves
670 * Outputs - none
671 * Side Effects - this is where the ircd gets going right now
672 */
673int
5adde7a4 674charybdis_main(int argc, char * const argv[])
212380e3
AC
675{
676 int fd;
677
1ba9eba5 678#ifndef _WIN32
212380e3
AC
679 /* Check to see if the user is running us as root, which is a nono */
680 if(geteuid() == 0)
681 {
682 fprintf(stderr, "Don't run ircd as root!!!\n");
683 return -1;
684 }
1ba9eba5 685#endif
212380e3 686
d6c17e5d 687 relocate_paths();
adc7be98 688
2f545aad
AC
689 logFileName = ircd_paths[IRCD_PATH_IRCD_LOG];
690 pidFileName = ircd_paths[IRCD_PATH_IRCD_PID];
691
4d8cfacd
AC
692 ConfigFileEntry.dpath = ircd_paths[IRCD_PATH_PREFIX];
693 ConfigFileEntry.configfile = ircd_paths[IRCD_PATH_IRCD_CONF]; /* Server configuration file */
befa24c8 694 ConfigFileEntry.connect_timeout = 30; /* Default to 30 */
346fba92 695
d6c17e5d
AC
696 init_sys();
697
befa24c8 698 umask(077); /* better safe than sorry --SRB */
adc7be98
VY
699
700 myargv = argv;
701 parseargs(&argc, &argv, myopts);
702
703 if(chdir(ConfigFileEntry.dpath))
704 {
705 fprintf(stderr, "Unable to chdir to %s: %s\n", ConfigFileEntry.dpath, strerror(errno));
706 exit(EXIT_FAILURE);
707 }
708
3d3d1a95
VY
709 rb_set_time();
710
212380e3
AC
711 /*
712 * Setup corefile size immediately after boot -kre
713 */
714 setup_corefile();
715
b02a913b
AC
716 /* initialise operhash fairly early. */
717 init_operhash();
718
212380e3
AC
719 memset(&me, 0, sizeof(me));
720 memset(&meLocalUser, 0, sizeof(meLocalUser));
721 me.localClient = &meLocalUser;
722
723 /* Make sure all lists are zeroed */
724 memset(&unknown_list, 0, sizeof(unknown_list));
725 memset(&lclient_list, 0, sizeof(lclient_list));
726 memset(&serv_list, 0, sizeof(serv_list));
727 memset(&global_serv_list, 0, sizeof(global_serv_list));
728 memset(&local_oper_list, 0, sizeof(local_oper_list));
729 memset(&oper_list, 0, sizeof(oper_list));
730
330fc5c1 731 rb_dlinkAddTail(&me, &me.node, &global_client_list);
212380e3 732
2af8c7ff
VY
733 memset(&Count, 0, sizeof(Count));
734 memset(&ServerInfo, 0, sizeof(ServerInfo));
735 memset(&AdminInfo, 0, sizeof(AdminInfo));
736 memset(&ServerStats, 0, sizeof(struct ServerStatistics));
212380e3 737
212380e3
AC
738 if(printVersion)
739 {
b3803848 740 printf("ircd: version %s(%s)\n", ircd_version, serno);
8bd1c8a1
AC
741#ifdef CUSTOM_BRANDING
742 printf("ircd: based on %s-%s\n", PACKAGE_NAME, PACKAGE_VERSION);
743#endif
d2b6b9f3 744 printf("ircd: %s\n", rb_lib_version());
212380e3
AC
745 exit(EXIT_SUCCESS);
746 }
747
212380e3
AC
748 setup_signals();
749
212380e3 750 if (testing_conf)
503727d1 751 server_state_foreground = true;
212380e3 752
09f528c2 753#ifndef _WIN32
212380e3
AC
754 /* Make sure fd 0, 1 and 2 are in use -- jilles */
755 do
756 {
757 fd = open("/dev/null", O_RDWR);
758 } while (fd < 2 && fd != -1);
759 if (fd > 2)
760 close(fd);
761 else if (fd == -1)
762 exit(1);
09f528c2 763#endif
212380e3
AC
764
765 /* Check if there is pidfile and daemon already running */
766 if(!testing_conf)
767 {
768 check_pidfile(pidFileName);
769
770 if(!server_state_foreground)
771 make_daemon();
772 inotice("starting %s ...", ircd_version);
31c86ea8 773 inotice("%s", rb_lib_version());
212380e3
AC
774 }
775
776 /* Init the event subsystem */
0edb29fa 777 rb_lib_init(ircd_log_cb, ircd_restart_cb, ircd_die_cb, !server_state_foreground, maxconnections, DNODE_HEAP_SIZE, FD_HEAP_SIZE);
150f35f7 778 rb_linebuf_init(LINEBUF_HEAP_SIZE);
212380e3 779
e861902f 780 rb_init_prng(NULL, RB_PRNG_DEFAULT);
37cd159e 781
e1db84d8
VY
782 seed_random(NULL);
783
346fba92
AC
784 init_builtin_capabs();
785 default_server_capabs = CAP_MASK;
786
212380e3 787 init_main_logfile();
212380e3
AC
788 newconf_init();
789 init_s_conf();
790 init_s_newconf();
791 init_hash();
792 clear_scache_hash_table(); /* server cache name table */
793 init_host_hash();
794 clear_hash_parse();
795 init_client();
212380e3
AC
796 init_hook();
797 init_channels();
798 initclass();
b47f8a4f 799 whowas_init();
212380e3
AC
800 init_reject();
801 init_cache();
802 init_monitor();
030cdce7 803
19716b9f 804 construct_cflags_strings();
030cdce7 805
6d063f05 806 init_authd(); /* Start up authd. */
068c6c4a 807 init_dns(); /* Start up DNS query system */
8a26cd19 808
39a68b53 809 privilegeset_set_new("default", "", 0);
212380e3
AC
810
811 if (testing_conf)
812 fprintf(stderr, "\nBeginning config test\n");
ea111ea5 813 read_conf_files(true); /* cold start init conf files */
212380e3 814
55abcbb2
KB
815 mod_add_path(MODULE_DIR);
816 mod_add_path(MODULE_DIR "/autoload");
a19097ba
EM
817 load_all_modules(1);
818 load_core_modules(1);
212380e3 819
b583faf9
AC
820 init_isupport();
821
6eebc373 822 init_bandb();
c6d72037 823 init_ssld();
c53ca1e0 824 init_wsockd();
c6d72037 825
ab31d2b0 826 rehash_bans();
80c9ac51 827
212380e3
AC
828 initialize_server_capabs(); /* Set up default_server_capabs */
829 initialize_global_set_options();
830
831 if(ServerInfo.name == NULL)
832 {
833 ierror("no server name specified in serverinfo block.");
834 return -1;
835 }
f427c8b0 836 rb_strlcpy(me.name, ServerInfo.name, sizeof(me.name));
212380e3
AC
837
838 if(ServerInfo.sid[0] == '\0')
839 {
840 ierror("no server sid specified in serverinfo block.");
841 return -2;
842 }
0982871a 843 rb_strlcpy(me.id, ServerInfo.sid, sizeof(me.id));
212380e3
AC
844 init_uid();
845
846 /* serverinfo{} description must exist. If not, error out. */
847 if(ServerInfo.description == NULL)
848 {
849 ierror("no server description specified in serverinfo block.");
850 return -3;
851 }
f427c8b0 852 rb_strlcpy(me.info, ServerInfo.description, sizeof(me.info));
212380e3 853
f5960b83 854 if(ServerInfo.ssl_cert != NULL)
8bd5767b
JT
855 {
856 /* just do the rb_setup_ssl_server to validate the config */
c1725bda 857 if(!rb_setup_ssl_server(ServerInfo.ssl_cert, ServerInfo.ssl_private_key, ServerInfo.ssl_dh_params, ServerInfo.ssl_cipher_list))
8bd5767b
JT
858 {
859 ilog(L_MAIN, "WARNING: Unable to setup SSL.");
bfc44622 860 ircd_ssl_ok = false;
8bd5767b
JT
861 }
862 else
bfc44622 863 ircd_ssl_ok = true;
c6d72037
VY
864 }
865
212380e3
AC
866 if (testing_conf)
867 {
868 fprintf(stderr, "\nConfig testing complete.\n");
869 fflush(stderr);
870 return 0; /* Why? We want the launcher to exit out. */
871 }
872
873 me.from = &me;
874 me.servptr = &me;
875 SetMe(&me);
876 make_server(&me);
e3354945 877 startup_time = rb_current_time();
212380e3
AC
878 add_to_client_hash(me.name, &me);
879 add_to_id_hash(me.id, &me);
994544c2 880 me.serv->nameinfo = scache_connect(me.name, me.info, 0);
212380e3 881
330fc5c1 882 rb_dlinkAddAlloc(&me, &global_serv_list);
212380e3
AC
883
884 construct_umodebuf();
885
886 check_class();
887 write_pidfile(pidFileName);
888 load_help();
889 open_logfiles();
890
50808796
EM
891 configure_authd();
892
212380e3
AC
893 ilog(L_MAIN, "Server Ready");
894
212380e3
AC
895 /* We want try_connections to be called as soon as possible now! -- adrian */
896 /* No, 'cause after a restart it would cause all sorts of nick collides */
897 /* um. by waiting even longer, that just means we have even *more*
898 * nick collisions. what a stupid idea. set an event for the IO loop --fl
899 */
12aea5fe 900 rb_event_addish("try_connections", try_connections, NULL, STARTUP_CONNECTIONS_TIME);
36645728
VY
901 rb_event_addonce("try_connections_startup", try_connections, NULL, 2);
902 rb_event_add("check_rehash", check_rehash, NULL, 3);
d80645d0 903 rb_event_addish("reseed_srand", seed_random, NULL, 300); /* reseed every 10 minutes */
212380e3 904
212380e3 905 if(splitmode)
36645728 906 check_splitmode_ev = rb_event_add("check_splitmode", check_splitmode, NULL, 5);
212380e3 907
212380e3
AC
908 print_startup(getpid());
909
39944e1f 910 rb_lib_loop(0);
212380e3
AC
911
912 return 0;
913}