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