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