]> jfr.im git - solanum.git/blob - ircd/ircd.c
ircd: relocate_paths() back on windows only now
[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 static void
70 ircd_die_cb(const char *str) __attribute__((noreturn));
71
72 /* /quote set variables */
73 struct SetOptions GlobalSetOptions;
74
75 /* configuration set from ircd.conf */
76 struct config_file_entry ConfigFileEntry;
77 /* server info set from ircd.conf */
78 struct server_info ServerInfo;
79 /* admin info set from ircd.conf */
80 struct admin_info AdminInfo;
81
82 struct Counter Count;
83 struct ServerStatistics ServerStats;
84
85 int maxconnections;
86 struct Client me; /* That's me */
87 struct LocalUser meLocalUser; /* That's also part of me */
88
89 rb_dlink_list global_client_list;
90
91 /* unknown/client pointer lists */
92 rb_dlink_list unknown_list; /* unknown clients ON this server only */
93 rb_dlink_list lclient_list; /* local clients only ON this server */
94 rb_dlink_list serv_list; /* local servers to this server ONLY */
95 rb_dlink_list global_serv_list; /* global servers on the network */
96 rb_dlink_list local_oper_list; /* our opers, duplicated in lclient_list */
97 rb_dlink_list oper_list; /* network opers */
98
99 char * const *myargv;
100 volatile sig_atomic_t dorehash = false;
101 volatile sig_atomic_t dorehashbans = false;
102 volatile sig_atomic_t doremotd = false;
103 bool kline_queued = false;
104 bool server_state_foreground = false;
105 bool opers_see_all_users = false;
106 bool ircd_ssl_ok = false;
107 bool ircd_zlib_ok = true;
108
109 int testing_conf = 0;
110 time_t startup_time;
111
112 int default_server_capabs;
113
114 int splitmode;
115 int splitchecking;
116 int split_users;
117 int split_servers;
118 int eob_count;
119
120 const 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
139 const 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
158 const char *logFileName = NULL;
159 const char *pidFileName = NULL;
160
161 void
162 ircd_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);
184 close_logfiles();
185
186 unlink(pidFileName);
187 exit(0);
188 }
189
190 /*
191 * print_startup - print startup information
192 */
193 static void
194 print_startup(int pid)
195 {
196 int fd;
197
198 #ifndef _WIN32
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();
209 #endif
210 inotice("now running in %s mode from %s as pid %d ...",
211 !server_state_foreground ? "background" : "foreground",
212 ConfigFileEntry.dpath, pid);
213
214 #ifndef _WIN32
215 /* let the parent process know the initialization was successful
216 * -- jilles */
217 if (!server_state_foreground)
218 {
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();
226 }
227 if (dup2(1, 0) == -1)
228 abort();
229 if (dup2(1, 2) == -1)
230 abort();
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 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); */
300 #endif
301 return 0;
302 }
303
304 static int printVersion = 0;
305
306 struct lgetopt myopts[] = {
307 {"configfile", &ConfigFileEntry.configfile,
308 STRING, "File to use for ircd.conf"},
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
323 static void
324 check_rehash(void *unused)
325 {
326 /*
327 * Check to see whether we have to rehash the configuration ..
328 */
329 if(dorehash)
330 {
331 rehash(true);
332 dorehash = false;
333 }
334
335 if(dorehashbans)
336 {
337 rehash_bans();
338 dorehashbans = false;
339 }
340
341 if(doremotd)
342 {
343 sendto_realops_snomask(SNO_GENERAL, L_ALL,
344 "Got signal SIGUSR1, reloading ircd motd file");
345 cache_user_motd();
346 doremotd = false;
347 }
348 }
349
350 /*
351 * initalialize_global_set_options
352 *
353 * inputs - none
354 * output - none
355 * side effects - This sets all global set options needed
356 */
357 static void
358 initialize_global_set_options(void)
359 {
360 memset(&GlobalSetOptions, 0, sizeof(GlobalSetOptions));
361 /* memset( &ConfigFileEntry, 0, sizeof(ConfigFileEntry)); */
362
363 GlobalSetOptions.maxclients = ServerInfo.default_max_clients;
364
365 if(GlobalSetOptions.maxclients > (maxconnections - MAX_BUFFER) || (GlobalSetOptions.maxclients <= 0))
366 GlobalSetOptions.maxclients = maxconnections - MAX_BUFFER;
367
368 GlobalSetOptions.autoconn = 1;
369
370 GlobalSetOptions.spam_time = MIN_JOIN_LEAVE_TIME;
371 GlobalSetOptions.spam_num = MAX_JOIN_LEAVE_COUNT;
372
373 GlobalSetOptions.floodcount = ConfigFileEntry.default_floodcount;
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
385 GlobalSetOptions.ident_timeout = ConfigFileEntry.default_ident_timeout;
386
387 rb_strlcpy(GlobalSetOptions.operstring,
388 ConfigFileEntry.default_operstring,
389 sizeof(GlobalSetOptions.operstring));
390 rb_strlcpy(GlobalSetOptions.adminstring,
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 */
406 static void
407 initialize_server_capabs(void)
408 {
409 default_server_capabs &= ~CAP_ZIP;
410 }
411
412 #ifdef _WIN32
413 /*
414 * relocate_paths
415 *
416 * inputs - none
417 * output - none
418 * side effects - items in ircd_paths[] array are relocated
419 */
420 static void
421 relocate_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);
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 }
495 }
496 #endif
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 */
505 static void
506 write_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
514 snprintf(buff, sizeof(buff), "%u\n", pid);
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 */
538 static void
539 check_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);
551 if(!rb_kill(pidfromfile, 0))
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 */
569 static void
570 setup_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
584 static void
585 ircd_log_cb(const char *str)
586 {
587 ilog(L_MAIN, "librb reports: %s", str);
588 }
589
590 static void
591 ircd_restart_cb(const char *str)
592 {
593 inotice("librb has called the restart callback: %s", str);
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 */
604 static void
605 ircd_die_cb(const char *str)
606 {
607 if(str != NULL)
608 {
609 /* Try to get the message out to currently logged in operators. */
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);
612 }
613 else
614 inotice("librb has called the die callback..aborting");
615
616 unlink(pidFileName);
617 exit(EXIT_FAILURE);
618 }
619
620 struct ev_entry *check_splitmode_ev = NULL;
621
622 static int
623 seed_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 }
637 close(fd);
638 }
639 return 0;
640 }
641
642 static void
643 seed_with_clock(void)
644 {
645 const struct timeval *tv;
646 rb_set_time();
647 tv = rb_current_time_tv();
648 srand(tv->tv_sec ^ (tv->tv_usec | (getpid() << 20)));
649 }
650
651 static void
652 seed_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
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 */
673 int
674 charybdis_main(int argc, char * const argv[])
675 {
676 int fd;
677
678 #ifndef _WIN32
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 }
685 #endif
686
687 #ifdef _WIN32
688 relocate_paths();
689 #endif
690
691 logFileName = ircd_paths[IRCD_PATH_IRCD_LOG];
692 pidFileName = ircd_paths[IRCD_PATH_IRCD_PID];
693
694 ConfigFileEntry.dpath = ircd_paths[IRCD_PATH_PREFIX];
695 ConfigFileEntry.configfile = ircd_paths[IRCD_PATH_IRCD_CONF]; /* Server configuration file */
696 ConfigFileEntry.connect_timeout = 30; /* Default to 30 */
697
698 init_sys();
699
700 umask(077); /* better safe than sorry --SRB */
701
702 myargv = argv;
703 parseargs(&argc, &argv, myopts);
704
705 if(chdir(ConfigFileEntry.dpath))
706 {
707 fprintf(stderr, "Unable to chdir to %s: %s\n", ConfigFileEntry.dpath, strerror(errno));
708 exit(EXIT_FAILURE);
709 }
710
711 rb_set_time();
712
713 /*
714 * Setup corefile size immediately after boot -kre
715 */
716 setup_corefile();
717
718 /* initialise operhash fairly early. */
719 init_operhash();
720
721 memset(&me, 0, sizeof(me));
722 memset(&meLocalUser, 0, sizeof(meLocalUser));
723 me.localClient = &meLocalUser;
724
725 /* Make sure all lists are zeroed */
726 memset(&unknown_list, 0, sizeof(unknown_list));
727 memset(&lclient_list, 0, sizeof(lclient_list));
728 memset(&serv_list, 0, sizeof(serv_list));
729 memset(&global_serv_list, 0, sizeof(global_serv_list));
730 memset(&local_oper_list, 0, sizeof(local_oper_list));
731 memset(&oper_list, 0, sizeof(oper_list));
732
733 rb_dlinkAddTail(&me, &me.node, &global_client_list);
734
735 memset(&Count, 0, sizeof(Count));
736 memset(&ServerInfo, 0, sizeof(ServerInfo));
737 memset(&AdminInfo, 0, sizeof(AdminInfo));
738 memset(&ServerStats, 0, sizeof(struct ServerStatistics));
739
740 if(printVersion)
741 {
742 printf("ircd: version %s(%s)\n", ircd_version, serno);
743 #ifdef CUSTOM_BRANDING
744 printf("ircd: based on %s-%s\n", PACKAGE_NAME, PACKAGE_VERSION);
745 #endif
746 printf("ircd: %s\n", rb_lib_version());
747 exit(EXIT_SUCCESS);
748 }
749
750 setup_signals();
751
752 if (testing_conf)
753 server_state_foreground = true;
754
755 #ifndef _WIN32
756 /* Make sure fd 0, 1 and 2 are in use -- jilles */
757 do
758 {
759 fd = open("/dev/null", O_RDWR);
760 } while (fd < 2 && fd != -1);
761 if (fd > 2)
762 close(fd);
763 else if (fd == -1)
764 exit(1);
765 #endif
766
767 /* Check if there is pidfile and daemon already running */
768 if(!testing_conf)
769 {
770 check_pidfile(pidFileName);
771
772 if(!server_state_foreground)
773 make_daemon();
774 inotice("starting %s ...", ircd_version);
775 inotice("%s", rb_lib_version());
776 }
777
778 /* Init the event subsystem */
779 rb_lib_init(ircd_log_cb, ircd_restart_cb, ircd_die_cb, !server_state_foreground, maxconnections, DNODE_HEAP_SIZE, FD_HEAP_SIZE);
780 rb_linebuf_init(LINEBUF_HEAP_SIZE);
781
782 rb_init_prng(NULL, RB_PRNG_DEFAULT);
783
784 seed_random(NULL);
785
786 init_builtin_capabs();
787 default_server_capabs = CAP_MASK;
788
789 init_main_logfile();
790 newconf_init();
791 init_s_conf();
792 init_s_newconf();
793 init_hash();
794 clear_scache_hash_table(); /* server cache name table */
795 init_host_hash();
796 clear_hash_parse();
797 init_client();
798 init_hook();
799 init_channels();
800 initclass();
801 whowas_init();
802 init_reject();
803 init_cache();
804 init_monitor();
805
806 construct_cflags_strings();
807
808 init_authd(); /* Start up authd. */
809 init_dns(); /* Start up DNS query system */
810 init_modules(); /* Start up modules system */
811
812 privilegeset_set_new("default", "", 0);
813
814 if (testing_conf)
815 fprintf(stderr, "\nBeginning config test\n");
816 read_conf_files(true); /* cold start init conf files */
817
818 load_all_modules(1);
819 load_core_modules(1);
820
821 init_isupport();
822
823 init_bandb();
824 init_ssld();
825 init_wsockd();
826
827 rehash_bans();
828
829 initialize_server_capabs(); /* Set up default_server_capabs */
830 initialize_global_set_options();
831
832 if(ServerInfo.name == NULL)
833 {
834 ierror("no server name specified in serverinfo block.");
835 return -1;
836 }
837 rb_strlcpy(me.name, ServerInfo.name, sizeof(me.name));
838
839 if(ServerInfo.sid[0] == '\0')
840 {
841 ierror("no server sid specified in serverinfo block.");
842 return -2;
843 }
844 rb_strlcpy(me.id, ServerInfo.sid, sizeof(me.id));
845 init_uid();
846
847 /* serverinfo{} description must exist. If not, error out. */
848 if(ServerInfo.description == NULL)
849 {
850 ierror("no server description specified in serverinfo block.");
851 return -3;
852 }
853 rb_strlcpy(me.info, ServerInfo.description, sizeof(me.info));
854
855 if(ServerInfo.ssl_cert != NULL)
856 {
857 /* just do the rb_setup_ssl_server to validate the config */
858 if(!rb_setup_ssl_server(ServerInfo.ssl_cert, ServerInfo.ssl_private_key, ServerInfo.ssl_dh_params, ServerInfo.ssl_cipher_list))
859 {
860 ilog(L_MAIN, "WARNING: Unable to setup SSL.");
861 ircd_ssl_ok = false;
862 }
863 else
864 ircd_ssl_ok = true;
865 }
866
867 if (testing_conf)
868 {
869 fprintf(stderr, "\nConfig testing complete.\n");
870 fflush(stderr);
871 return 0; /* Why? We want the launcher to exit out. */
872 }
873
874 me.from = &me;
875 me.servptr = &me;
876 SetMe(&me);
877 make_server(&me);
878 startup_time = rb_current_time();
879 add_to_client_hash(me.name, &me);
880 add_to_id_hash(me.id, &me);
881 me.serv->nameinfo = scache_connect(me.name, me.info, 0);
882
883 rb_dlinkAddAlloc(&me, &global_serv_list);
884
885 construct_umodebuf();
886
887 check_class();
888 write_pidfile(pidFileName);
889 load_help();
890 open_logfiles();
891
892 configure_authd();
893
894 ilog(L_MAIN, "Server Ready");
895
896 /* We want try_connections to be called as soon as possible now! -- adrian */
897 /* No, 'cause after a restart it would cause all sorts of nick collides */
898 /* um. by waiting even longer, that just means we have even *more*
899 * nick collisions. what a stupid idea. set an event for the IO loop --fl
900 */
901 rb_event_addish("try_connections", try_connections, NULL, STARTUP_CONNECTIONS_TIME);
902 rb_event_addonce("try_connections_startup", try_connections, NULL, 2);
903 rb_event_add("check_rehash", check_rehash, NULL, 3);
904 rb_event_addish("reseed_srand", seed_random, NULL, 300); /* reseed every 10 minutes */
905
906 if(splitmode)
907 check_splitmode_ev = rb_event_add("check_splitmode", check_splitmode, NULL, 5);
908
909 print_startup(getpid());
910
911 rb_lib_loop(0);
912
913 return 0;
914 }