]> jfr.im git - solanum.git/blob - ircd/ircd.c
makerelease: fix DATECODE output
[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_conf.h"
57 #include "s_newconf.h"
58 #include "cache.h"
59 #include "monitor.h"
60 #include "patchlevel.h"
61 #include "serno.h"
62 #include "sslproc.h"
63 #include "wsproc.h"
64 #include "chmode.h"
65 #include "privilege.h"
66 #include "bandbi.h"
67 #include "authproc.h"
68 #include "operhash.h"
69
70 /* /quote set variables */
71 struct SetOptions GlobalSetOptions;
72
73 /* configuration set from ircd.conf */
74 struct config_file_entry ConfigFileEntry;
75 /* server info set from ircd.conf */
76 struct server_info ServerInfo;
77 /* admin info set from ircd.conf */
78 struct admin_info AdminInfo;
79
80 struct Counter Count;
81 struct ServerStatistics ServerStats;
82
83 int maxconnections;
84 struct Client me; /* That's me */
85 struct LocalUser meLocalUser; /* That's also part of me */
86
87 rb_dlink_list global_client_list;
88
89 /* unknown/client pointer lists */
90 rb_dlink_list unknown_list; /* unknown clients ON this server only */
91 rb_dlink_list lclient_list; /* local clients only ON this server */
92 rb_dlink_list serv_list; /* local servers to this server ONLY */
93 rb_dlink_list global_serv_list; /* global servers on the network */
94 rb_dlink_list local_oper_list; /* our opers, duplicated in lclient_list */
95 rb_dlink_list oper_list; /* network opers */
96
97 char * const *myargv;
98 volatile sig_atomic_t dorehash = false;
99 volatile sig_atomic_t dorehashbans = false;
100 volatile sig_atomic_t doremotd = false;
101 bool kline_queued = false;
102 bool server_state_foreground = false;
103 bool opers_see_all_users = false;
104 bool ircd_ssl_ok = false;
105 bool ircd_zlib_ok = true;
106
107 int testing_conf = 0;
108 time_t startup_time;
109
110 int default_server_capabs;
111
112 int splitmode;
113 int splitchecking;
114 int split_users;
115 int split_servers;
116 int eob_count;
117
118 const 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
137 const char *logFileName = NULL;
138 const char *pidFileName = NULL;
139
140 void
141 ircd_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);
163 close_logfiles();
164
165 unlink(pidFileName);
166 exit(0);
167 }
168
169 /*
170 * print_startup - print startup information
171 */
172 static void
173 print_startup(int pid)
174 {
175 int fd;
176
177 #ifndef _WIN32
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();
188 #endif
189 inotice("runtime path: %s", rb_path_to_self());
190 inotice("now running in %s mode from %s as pid %d ...",
191 !server_state_foreground ? "background" : "foreground",
192 ConfigFileEntry.dpath, pid);
193
194 #ifndef _WIN32
195 /* let the parent process know the initialization was successful
196 * -- jilles */
197 if (!server_state_foreground)
198 {
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();
206 }
207 if (dup2(1, 0) == -1)
208 abort();
209 if (dup2(1, 2) == -1)
210 abort();
211 #endif
212 }
213
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 */
221 static void
222 init_sys(void)
223 {
224 #if !defined(_WIN32) && defined(RLIMIT_NOFILE) && defined(HAVE_SYS_RESOURCE_H)
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");
233 fprintf(stderr, "ERROR: charybdis reserves %d FDs, shell limits must be above this\n", MAX_BUFFER);
234 exit(EXIT_FAILURE);
235 }
236 return;
237 }
238 #endif /* RLIMIT_FD_MAX */
239 maxconnections = MAXCONNECTIONS;
240 }
241
242 static int
243 make_daemon(void)
244 {
245 #ifndef _WIN32
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); */
280 #endif
281 return 0;
282 }
283
284 static int printVersion = 0;
285
286 struct lgetopt myopts[] = {
287 {"configfile", &ConfigFileEntry.configfile,
288 STRING, "File to use for ircd.conf"},
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
303 static void
304 check_rehash(void *unused)
305 {
306 /*
307 * Check to see whether we have to rehash the configuration ..
308 */
309 if(dorehash)
310 {
311 rehash(true);
312 dorehash = false;
313 }
314
315 if(dorehashbans)
316 {
317 rehash_bans();
318 dorehashbans = false;
319 }
320
321 if(doremotd)
322 {
323 sendto_realops_snomask(SNO_GENERAL, L_ALL,
324 "Got signal SIGUSR1, reloading ircd motd file");
325 cache_user_motd();
326 doremotd = false;
327 }
328 }
329
330 /*
331 * initalialize_global_set_options
332 *
333 * inputs - none
334 * output - none
335 * side effects - This sets all global set options needed
336 */
337 static void
338 initialize_global_set_options(void)
339 {
340 memset(&GlobalSetOptions, 0, sizeof(GlobalSetOptions));
341 /* memset( &ConfigFileEntry, 0, sizeof(ConfigFileEntry)); */
342
343 GlobalSetOptions.maxclients = ServerInfo.default_max_clients;
344
345 if(GlobalSetOptions.maxclients > (maxconnections - MAX_BUFFER) || (GlobalSetOptions.maxclients <= 0))
346 GlobalSetOptions.maxclients = maxconnections - MAX_BUFFER;
347
348 GlobalSetOptions.autoconn = 1;
349
350 GlobalSetOptions.spam_time = MIN_JOIN_LEAVE_TIME;
351 GlobalSetOptions.spam_num = MAX_JOIN_LEAVE_COUNT;
352
353 GlobalSetOptions.floodcount = ConfigFileEntry.default_floodcount;
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
365 GlobalSetOptions.ident_timeout = ConfigFileEntry.default_ident_timeout;
366
367 rb_strlcpy(GlobalSetOptions.operstring,
368 ConfigFileEntry.default_operstring,
369 sizeof(GlobalSetOptions.operstring));
370 rb_strlcpy(GlobalSetOptions.adminstring,
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 */
386 static void
387 initialize_server_capabs(void)
388 {
389 default_server_capabs &= ~CAP_ZIP;
390 }
391
392 /*
393 * relocate_paths
394 *
395 * inputs - none
396 * output - none
397 * side effects - items in ircd_paths[] array are relocated
398 */
399 #ifdef _WIN32
400 static void
401 relocate_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 }
470 #endif
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 */
479 static void
480 write_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
488 snprintf(buff, sizeof(buff), "%u\n", pid);
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 */
512 static void
513 check_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);
525 if(!rb_kill(pidfromfile, 0))
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 */
543 static void
544 setup_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
558 static void
559 ircd_log_cb(const char *str)
560 {
561 ilog(L_MAIN, "librb reports: %s", str);
562 }
563
564 static void
565 ircd_restart_cb(const char *str)
566 {
567 inotice("librb has called the restart callback: %s", str);
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 */
578 static void
579 ircd_die_cb(const char *str)
580 {
581 if(str != NULL)
582 {
583 /* Try to get the message out to currently logged in operators. */
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);
586 }
587 else
588 inotice("librb has called the die callback..aborting");
589
590 unlink(pidFileName);
591 exit(EXIT_FAILURE);
592 }
593
594 struct ev_entry *check_splitmode_ev = NULL;
595
596 static int
597 seed_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 }
611 close(fd);
612 }
613 return 0;
614 }
615
616 static void
617 seed_with_clock(void)
618 {
619 const struct timeval *tv;
620 rb_set_time();
621 tv = rb_current_time_tv();
622 srand(tv->tv_sec ^ (tv->tv_usec | (getpid() << 20)));
623 }
624
625 static void
626 seed_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
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 */
647 int
648 charybdis_main(int argc, char * const argv[])
649 {
650 int fd;
651
652 #ifndef _WIN32
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 }
659 #endif
660
661 #ifdef _WIN32
662 relocate_paths();
663 #endif
664
665 logFileName = ircd_paths[IRCD_PATH_IRCD_LOG];
666 pidFileName = ircd_paths[IRCD_PATH_IRCD_PID];
667
668 ConfigFileEntry.dpath = ircd_paths[IRCD_PATH_PREFIX];
669 ConfigFileEntry.configfile = ircd_paths[IRCD_PATH_IRCD_CONF]; /* Server configuration file */
670 ConfigFileEntry.connect_timeout = 30; /* Default to 30 */
671
672 init_sys();
673
674 umask(077); /* better safe than sorry --SRB */
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
685 rb_set_time();
686
687 /*
688 * Setup corefile size immediately after boot -kre
689 */
690 setup_corefile();
691
692 /* initialise operhash fairly early. */
693 init_operhash();
694
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
707 rb_dlinkAddTail(&me, &me.node, &global_client_list);
708
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));
713
714 if(printVersion)
715 {
716 printf("ircd: version %s(%s)\n", ircd_version, serno);
717 #ifdef CUSTOM_BRANDING
718 printf("ircd: based on %s-%s\n", PACKAGE_NAME, PACKAGE_VERSION);
719 #endif
720 printf("ircd: %s\n", rb_lib_version());
721 exit(EXIT_SUCCESS);
722 }
723
724 setup_signals();
725
726 if (testing_conf)
727 server_state_foreground = true;
728
729 #ifndef _WIN32
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);
739 #endif
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);
749 inotice("%s", rb_lib_version());
750 }
751
752 /* Init the event subsystem */
753 rb_lib_init(ircd_log_cb, ircd_restart_cb, ircd_die_cb, !server_state_foreground, maxconnections, DNODE_HEAP_SIZE, FD_HEAP_SIZE);
754 rb_linebuf_init(LINEBUF_HEAP_SIZE);
755
756 rb_init_prng(NULL, RB_PRNG_DEFAULT);
757
758 seed_random(NULL);
759
760 init_builtin_capabs();
761 default_server_capabs = CAP_MASK;
762
763 init_main_logfile();
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();
772 init_hook();
773 init_channels();
774 initclass();
775 whowas_init();
776 init_reject();
777 init_cache();
778 init_monitor();
779
780 construct_cflags_strings();
781
782 init_authd(); /* Start up authd. */
783 init_dns(); /* Start up DNS query system */
784
785 privilegeset_set_new("default", "", 0);
786
787 if (testing_conf)
788 fprintf(stderr, "\nBeginning config test\n");
789 read_conf_files(true); /* cold start init conf files */
790
791 mod_add_path(MODULE_DIR);
792 mod_add_path(MODULE_DIR "/autoload");
793 load_all_modules(1);
794 load_core_modules(1);
795
796 init_isupport();
797
798 init_bandb();
799 init_ssld();
800 init_wsockd();
801
802 rehash_bans();
803
804 initialize_server_capabs(); /* Set up default_server_capabs */
805 initialize_global_set_options();
806
807 if(ServerInfo.name == NULL)
808 {
809 ierror("no server name specified in serverinfo block.");
810 return -1;
811 }
812 rb_strlcpy(me.name, ServerInfo.name, sizeof(me.name));
813
814 if(ServerInfo.sid[0] == '\0')
815 {
816 ierror("no server sid specified in serverinfo block.");
817 return -2;
818 }
819 strcpy(me.id, ServerInfo.sid);
820 init_uid();
821
822 /* serverinfo{} description must exist. If not, error out. */
823 if(ServerInfo.description == NULL)
824 {
825 ierror("no server description specified in serverinfo block.");
826 return -3;
827 }
828 rb_strlcpy(me.info, ServerInfo.description, sizeof(me.info));
829
830 if(ServerInfo.ssl_cert != NULL)
831 {
832 /* just do the rb_setup_ssl_server to validate the config */
833 if(!rb_setup_ssl_server(ServerInfo.ssl_cert, ServerInfo.ssl_private_key, ServerInfo.ssl_dh_params, ServerInfo.ssl_cipher_list))
834 {
835 ilog(L_MAIN, "WARNING: Unable to setup SSL.");
836 ircd_ssl_ok = false;
837 }
838 else
839 ircd_ssl_ok = true;
840 }
841
842 if (testing_conf)
843 {
844 fprintf(stderr, "\nConfig testing complete.\n");
845 fflush(stderr);
846 return 0; /* Why? We want the launcher to exit out. */
847 }
848
849 me.from = &me;
850 me.servptr = &me;
851 SetMe(&me);
852 make_server(&me);
853 startup_time = rb_current_time();
854 add_to_client_hash(me.name, &me);
855 add_to_id_hash(me.id, &me);
856 me.serv->nameinfo = scache_connect(me.name, me.info, 0);
857
858 rb_dlinkAddAlloc(&me, &global_serv_list);
859
860 construct_umodebuf();
861
862 check_class();
863 write_pidfile(pidFileName);
864 load_help();
865 open_logfiles();
866
867 configure_authd();
868
869 ilog(L_MAIN, "Server Ready");
870
871 /* We want try_connections to be called as soon as possible now! -- adrian */
872 /* No, 'cause after a restart it would cause all sorts of nick collides */
873 /* um. by waiting even longer, that just means we have even *more*
874 * nick collisions. what a stupid idea. set an event for the IO loop --fl
875 */
876 rb_event_addish("try_connections", try_connections, NULL, STARTUP_CONNECTIONS_TIME);
877 rb_event_addonce("try_connections_startup", try_connections, NULL, 2);
878 rb_event_add("check_rehash", check_rehash, NULL, 3);
879 rb_event_addish("reseed_srand", seed_random, NULL, 300); /* reseed every 10 minutes */
880
881 if(splitmode)
882 check_splitmode_ev = rb_event_add("check_splitmode", check_splitmode, NULL, 5);
883
884 print_startup(getpid());
885
886 rb_lib_loop(0);
887
888 return 0;
889 }