]>
Commit | Line | Data |
---|---|---|
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-2008 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 | * $Id$ | |
26 | */ | |
27 | ||
28 | #include "ratbox_lib.h" | |
29 | #include "stdinc.h" | |
30 | #include "setup.h" | |
31 | #include "config.h" | |
32 | #include "ircd.h" | |
33 | #include "channel.h" | |
34 | #include "class.h" | |
35 | #include "client.h" | |
36 | #include "common.h" | |
37 | #include "hash.h" | |
38 | #include "match.h" | |
39 | #include "ircd_signal.h" | |
40 | #include "msg.h" /* msgtab */ | |
41 | #include "hostmask.h" | |
42 | #include "numeric.h" | |
43 | #include "parse.h" | |
44 | #include "res.h" | |
45 | #include "restart.h" | |
46 | #include "s_auth.h" | |
47 | #include "s_conf.h" | |
48 | #include "logger.h" | |
49 | #include "s_serv.h" /* try_connections */ | |
50 | #include "s_user.h" | |
51 | #include "s_stats.h" | |
52 | #include "scache.h" | |
53 | #include "send.h" | |
54 | #include "supported.h" | |
55 | #include "whowas.h" | |
56 | #include "modules.h" | |
57 | #include "hook.h" | |
58 | #include "ircd_getopt.h" | |
59 | #include "newconf.h" | |
60 | #include "reject.h" | |
61 | #include "s_conf.h" | |
62 | #include "s_newconf.h" | |
63 | #include "cache.h" | |
64 | #include "monitor.h" | |
65 | #include "patchlevel.h" | |
66 | #include "serno.h" | |
67 | #include "sslproc.h" | |
68 | #include "chmode.h" | |
69 | #include "privilege.h" | |
70 | ||
71 | /* /quote set variables */ | |
72 | struct SetOptions GlobalSetOptions; | |
73 | ||
74 | /* configuration set from ircd.conf */ | |
75 | struct config_file_entry ConfigFileEntry; | |
76 | /* server info set from ircd.conf */ | |
77 | struct server_info ServerInfo; | |
78 | /* admin info set from ircd.conf */ | |
79 | struct admin_info AdminInfo; | |
80 | ||
81 | struct Counter Count; | |
82 | struct ServerStatistics ServerStats; | |
83 | ||
84 | int maxconnections; | |
85 | struct Client me; /* That's me */ | |
86 | struct LocalUser meLocalUser; /* That's also part of me */ | |
87 | ||
88 | rb_dlink_list global_client_list; | |
89 | ||
90 | /* unknown/client pointer lists */ | |
91 | rb_dlink_list unknown_list; /* unknown clients ON this server only */ | |
92 | rb_dlink_list lclient_list; /* local clients only ON this server */ | |
93 | rb_dlink_list serv_list; /* local servers to this server ONLY */ | |
94 | rb_dlink_list global_serv_list; /* global servers on the network */ | |
95 | rb_dlink_list local_oper_list; /* our opers, duplicated in lclient_list */ | |
96 | rb_dlink_list oper_list; /* network opers */ | |
97 | ||
98 | const char *logFileName = LPATH; | |
99 | const char *pidFileName = PPATH; | |
100 | ||
101 | char **myargv; | |
102 | int dorehash = 0; | |
103 | int dorehashbans = 0; | |
104 | int doremotd = 0; | |
105 | int kline_queued = 0; | |
106 | int server_state_foreground = 0; | |
107 | int opers_see_all_users = 0; | |
108 | int ssl_ok = 0; | |
109 | int zlib_ok = 1; | |
110 | ||
111 | int testing_conf = 0; | |
112 | time_t startup_time; | |
113 | ||
114 | int default_server_capabs = CAP_MASK; | |
115 | ||
116 | int splitmode; | |
117 | int splitchecking; | |
118 | int split_users; | |
119 | int split_servers; | |
120 | int eob_count; | |
121 | ||
122 | void | |
123 | ircd_shutdown(const char *reason) | |
124 | { | |
125 | struct Client *target_p; | |
126 | rb_dlink_node *ptr; | |
127 | ||
128 | RB_DLINK_FOREACH(ptr, lclient_list.head) | |
129 | { | |
130 | target_p = ptr->data; | |
131 | ||
132 | sendto_one(target_p, ":%s NOTICE %s :Server Terminating. %s", | |
133 | me.name, target_p->name, reason); | |
134 | } | |
135 | ||
136 | RB_DLINK_FOREACH(ptr, serv_list.head) | |
137 | { | |
138 | target_p = ptr->data; | |
139 | ||
140 | sendto_one(target_p, ":%s ERROR :Terminated by %s", | |
141 | me.name, reason); | |
142 | } | |
143 | ||
144 | ilog(L_MAIN, "Server Terminating. %s", reason); | |
145 | close_logfiles(); | |
146 | ||
147 | unlink(pidFileName); | |
148 | exit(0); | |
149 | } | |
150 | ||
151 | /* | |
152 | * print_startup - print startup information | |
153 | */ | |
154 | static void | |
155 | print_startup(int pid) | |
156 | { | |
157 | inotice("now running in %s mode from %s as pid %d ...", | |
158 | !server_state_foreground ? "background" : "foreground", | |
159 | ConfigFileEntry.dpath, pid); | |
160 | ||
161 | /* let the parent process know the initialization was successful | |
162 | * -- jilles */ | |
163 | if (!server_state_foreground) | |
164 | write(0, ".", 1); | |
165 | fclose(stdin); | |
166 | fclose(stdout); | |
167 | fclose(stderr); | |
168 | open("/dev/null", O_RDWR); | |
169 | dup2(0, 1); | |
170 | dup2(0, 2); | |
171 | } | |
172 | ||
173 | /* | |
174 | * init_sys | |
175 | * | |
176 | * inputs - boot_daemon flag | |
177 | * output - none | |
178 | * side effects - if boot_daemon flag is not set, don't daemonize | |
179 | */ | |
180 | static void | |
181 | init_sys(void) | |
182 | { | |
183 | #if defined(RLIMIT_NOFILE) && defined(HAVE_SYS_RESOURCE_H) | |
184 | struct rlimit limit; | |
185 | ||
186 | if(!getrlimit(RLIMIT_NOFILE, &limit)) | |
187 | { | |
188 | maxconnections = limit.rlim_cur; | |
189 | if(maxconnections <= MAX_BUFFER) | |
190 | { | |
191 | fprintf(stderr, "ERROR: Shell FD limits are too low.\n"); | |
192 | fprintf(stderr, "ERROR: charybdis reserves %d FDs, shell limits must be above this\n", MAX_BUFFER); | |
193 | exit(EXIT_FAILURE); | |
194 | } | |
195 | return; | |
196 | } | |
197 | #endif /* RLIMIT_FD_MAX */ | |
198 | maxconnections = MAXCONNECTIONS; | |
199 | } | |
200 | ||
201 | static int | |
202 | make_daemon(void) | |
203 | { | |
204 | int pid; | |
205 | int pip[2]; | |
206 | char c; | |
207 | ||
208 | if (pipe(pip) < 0) | |
209 | { | |
210 | perror("pipe"); | |
211 | exit(EXIT_FAILURE); | |
212 | } | |
213 | dup2(pip[1], 0); | |
214 | close(pip[1]); | |
215 | if((pid = fork()) < 0) | |
216 | { | |
217 | perror("fork"); | |
218 | exit(EXIT_FAILURE); | |
219 | } | |
220 | else if(pid > 0) | |
221 | { | |
222 | close(0); | |
223 | /* Wait for initialization to finish, successfully or | |
224 | * unsuccessfully. Until this point the child may still | |
225 | * write to stdout/stderr. | |
226 | * -- jilles */ | |
227 | if (read(pip[0], &c, 1) > 0) | |
228 | exit(EXIT_SUCCESS); | |
229 | else | |
230 | exit(EXIT_FAILURE); | |
231 | } | |
232 | ||
233 | close(pip[0]); | |
234 | setsid(); | |
235 | /* fclose(stdin); | |
236 | fclose(stdout); | |
237 | fclose(stderr); */ | |
238 | ||
239 | return 0; | |
240 | } | |
241 | ||
242 | static int printVersion = 0; | |
243 | ||
244 | struct lgetopt myopts[] = { | |
245 | {"dlinefile", &ConfigFileEntry.dlinefile, | |
246 | STRING, "File to use for dlines.conf"}, | |
247 | {"configfile", &ConfigFileEntry.configfile, | |
248 | STRING, "File to use for ircd.conf"}, | |
249 | {"klinefile", &ConfigFileEntry.klinefile, | |
250 | STRING, "File to use for kline.conf"}, | |
251 | {"xlinefile", &ConfigFileEntry.xlinefile, | |
252 | STRING, "File to use for xline.conf"}, | |
253 | {"resvfile", &ConfigFileEntry.resvfile, | |
254 | STRING, "File to use for resv.conf"}, | |
255 | {"logfile", &logFileName, | |
256 | STRING, "File to use for ircd.log"}, | |
257 | {"pidfile", &pidFileName, | |
258 | STRING, "File to use for process ID"}, | |
259 | {"foreground", &server_state_foreground, | |
260 | YESNO, "Run in foreground (don't detach)"}, | |
261 | {"version", &printVersion, | |
262 | YESNO, "Print version and exit"}, | |
263 | {"conftest", &testing_conf, | |
264 | YESNO, "Test the configuration files and exit"}, | |
265 | {"help", NULL, USAGE, "Print this text"}, | |
266 | {NULL, NULL, STRING, NULL}, | |
267 | }; | |
268 | ||
269 | static void | |
270 | check_rehash(void *unused) | |
271 | { | |
272 | /* | |
273 | * Check to see whether we have to rehash the configuration .. | |
274 | */ | |
275 | if(dorehash) | |
276 | { | |
277 | rehash(1); | |
278 | dorehash = 0; | |
279 | } | |
280 | ||
281 | if(dorehashbans) | |
282 | { | |
283 | rehash_bans(1); | |
284 | dorehashbans = 0; | |
285 | } | |
286 | ||
287 | if(doremotd) | |
288 | { | |
289 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
290 | "Got signal SIGUSR1, reloading ircd motd file"); | |
291 | cache_user_motd(); | |
292 | doremotd = 0; | |
293 | } | |
294 | } | |
295 | ||
296 | /* | |
297 | * initalialize_global_set_options | |
298 | * | |
299 | * inputs - none | |
300 | * output - none | |
301 | * side effects - This sets all global set options needed | |
302 | */ | |
303 | static void | |
304 | initialize_global_set_options(void) | |
305 | { | |
306 | memset(&GlobalSetOptions, 0, sizeof(GlobalSetOptions)); | |
307 | /* memset( &ConfigFileEntry, 0, sizeof(ConfigFileEntry)); */ | |
308 | ||
309 | GlobalSetOptions.maxclients = ServerInfo.default_max_clients; | |
310 | ||
311 | if(GlobalSetOptions.maxclients > (maxconnections - MAX_BUFFER) || (GlobalSetOptions.maxclients <= 0)) | |
312 | GlobalSetOptions.maxclients = maxconnections - MAX_BUFFER; | |
313 | ||
314 | GlobalSetOptions.autoconn = 1; | |
315 | ||
316 | GlobalSetOptions.spam_time = MIN_JOIN_LEAVE_TIME; | |
317 | GlobalSetOptions.spam_num = MAX_JOIN_LEAVE_COUNT; | |
318 | ||
319 | if(ConfigFileEntry.default_floodcount) | |
320 | GlobalSetOptions.floodcount = ConfigFileEntry.default_floodcount; | |
321 | else | |
322 | GlobalSetOptions.floodcount = 10; | |
323 | ||
324 | split_servers = ConfigChannel.default_split_server_count; | |
325 | split_users = ConfigChannel.default_split_user_count; | |
326 | ||
327 | if(split_users && split_servers | |
328 | && (ConfigChannel.no_create_on_split || ConfigChannel.no_join_on_split)) | |
329 | { | |
330 | splitmode = 1; | |
331 | splitchecking = 1; | |
332 | } | |
333 | ||
334 | GlobalSetOptions.ident_timeout = IDENT_TIMEOUT; | |
335 | ||
336 | rb_strlcpy(GlobalSetOptions.operstring, | |
337 | ConfigFileEntry.default_operstring, | |
338 | sizeof(GlobalSetOptions.operstring)); | |
339 | rb_strlcpy(GlobalSetOptions.adminstring, | |
340 | ConfigFileEntry.default_adminstring, | |
341 | sizeof(GlobalSetOptions.adminstring)); | |
342 | ||
343 | /* memset( &ConfigChannel, 0, sizeof(ConfigChannel)); */ | |
344 | ||
345 | /* End of global set options */ | |
346 | ||
347 | } | |
348 | ||
349 | /* | |
350 | * initialize_server_capabs | |
351 | * | |
352 | * inputs - none | |
353 | * output - none | |
354 | */ | |
355 | static void | |
356 | initialize_server_capabs(void) | |
357 | { | |
358 | default_server_capabs &= ~CAP_ZIP; | |
359 | } | |
360 | ||
361 | ||
362 | /* | |
363 | * write_pidfile | |
364 | * | |
365 | * inputs - filename+path of pid file | |
366 | * output - none | |
367 | * side effects - write the pid of the ircd to filename | |
368 | */ | |
369 | static void | |
370 | write_pidfile(const char *filename) | |
371 | { | |
372 | FILE *fb; | |
373 | char buff[32]; | |
374 | if((fb = fopen(filename, "w"))) | |
375 | { | |
376 | unsigned int pid = (unsigned int) getpid(); | |
377 | ||
378 | rb_snprintf(buff, sizeof(buff), "%u\n", pid); | |
379 | if((fputs(buff, fb) == -1)) | |
380 | { | |
381 | ilog(L_MAIN, "Error writing %u to pid file %s (%s)", | |
382 | pid, filename, strerror(errno)); | |
383 | } | |
384 | fclose(fb); | |
385 | return; | |
386 | } | |
387 | else | |
388 | { | |
389 | ilog(L_MAIN, "Error opening pid file %s", filename); | |
390 | } | |
391 | } | |
392 | ||
393 | /* | |
394 | * check_pidfile | |
395 | * | |
396 | * inputs - filename+path of pid file | |
397 | * output - none | |
398 | * side effects - reads pid from pidfile and checks if ircd is in process | |
399 | * list. if it is, gracefully exits | |
400 | * -kre | |
401 | */ | |
402 | static void | |
403 | check_pidfile(const char *filename) | |
404 | { | |
405 | FILE *fb; | |
406 | char buff[32]; | |
407 | pid_t pidfromfile; | |
408 | ||
409 | /* Don't do logging here, since we don't have log() initialised */ | |
410 | if((fb = fopen(filename, "r"))) | |
411 | { | |
412 | if(fgets(buff, 20, fb) != NULL) | |
413 | { | |
414 | pidfromfile = atoi(buff); | |
415 | if(!kill(pidfromfile, 0)) | |
416 | { | |
417 | printf("ircd: daemon is already running\n"); | |
418 | exit(-1); | |
419 | } | |
420 | } | |
421 | fclose(fb); | |
422 | } | |
423 | } | |
424 | ||
425 | /* | |
426 | * setup_corefile | |
427 | * | |
428 | * inputs - nothing | |
429 | * output - nothing | |
430 | * side effects - setups corefile to system limits. | |
431 | * -kre | |
432 | */ | |
433 | static void | |
434 | setup_corefile(void) | |
435 | { | |
436 | #ifdef HAVE_SYS_RESOURCE_H | |
437 | struct rlimit rlim; /* resource limits */ | |
438 | ||
439 | /* Set corefilesize to maximum */ | |
440 | if(!getrlimit(RLIMIT_CORE, &rlim)) | |
441 | { | |
442 | rlim.rlim_cur = rlim.rlim_max; | |
443 | setrlimit(RLIMIT_CORE, &rlim); | |
444 | } | |
445 | #endif | |
446 | } | |
447 | ||
448 | static void | |
449 | ircd_log_cb(const char *str) | |
450 | { | |
451 | ilog(L_MAIN, "libratbox reports: %s", str); | |
452 | } | |
453 | ||
454 | static void | |
455 | ircd_restart_cb(const char *str) | |
456 | { | |
457 | inotice("libratbox has called the restart callback: %s", str); | |
458 | restart(str); | |
459 | } | |
460 | ||
461 | /* | |
462 | * Why EXIT_FAILURE here? | |
463 | * Because if ircd_die_cb() is called it's because of a fatal | |
464 | * error inside libcharybdis, and we don't know how to handle the | |
465 | * exception, so it is logical to return a FAILURE exit code here. | |
466 | * --nenolod | |
467 | */ | |
468 | static void | |
469 | ircd_die_cb(const char *str) | |
470 | { | |
471 | if(str != NULL) | |
472 | { | |
473 | /* Try to get the message out to currently logged in operators. */ | |
474 | sendto_realops_snomask(SNO_GENERAL, L_NETWIDE, "libratbox has called the die callback..aborting: %s", str); | |
475 | inotice("libratbox has called the die callback..aborting: %s", str); | |
476 | } | |
477 | else | |
478 | inotice("libratbox has called the die callback..aborting"); | |
479 | ||
480 | unlink(pidFileName); | |
481 | exit(EXIT_FAILURE); | |
482 | } | |
483 | ||
484 | struct ev_entry *check_splitmode_ev = NULL; | |
485 | ||
486 | static int | |
487 | seed_with_urandom(void) | |
488 | { | |
489 | unsigned int seed; | |
490 | int fd; | |
491 | ||
492 | fd = open("/dev/urandom", O_RDONLY); | |
493 | if(fd >= 0) | |
494 | { | |
495 | if(read(fd, &seed, sizeof(seed)) == sizeof(seed)) | |
496 | { | |
497 | close(fd); | |
498 | srand(seed); | |
499 | return 1; | |
500 | } | |
501 | } | |
502 | return 0; | |
503 | } | |
504 | ||
505 | static void | |
506 | seed_with_clock(void) | |
507 | { | |
508 | const struct timeval *tv; | |
509 | rb_set_time(); | |
510 | tv = rb_current_time_tv(); | |
511 | srand(tv->tv_sec ^ (tv->tv_usec | (getpid() << 20))); | |
512 | } | |
513 | ||
514 | static void | |
515 | seed_random(void *unused) | |
516 | { | |
517 | unsigned int seed; | |
518 | if(rb_get_random(&seed, sizeof(seed)) == -1) | |
519 | { | |
520 | if(!seed_with_urandom()) | |
521 | seed_with_clock(); | |
522 | return; | |
523 | } | |
524 | srand(seed); | |
525 | } | |
526 | ||
527 | /* | |
528 | * main | |
529 | * | |
530 | * Initializes the IRCd. | |
531 | * | |
532 | * Inputs - number of commandline args, args themselves | |
533 | * Outputs - none | |
534 | * Side Effects - this is where the ircd gets going right now | |
535 | */ | |
536 | int | |
537 | main(int argc, char *argv[]) | |
538 | { | |
539 | int fd; | |
540 | ||
541 | /* Check to see if the user is running us as root, which is a nono */ | |
542 | if(geteuid() == 0) | |
543 | { | |
544 | fprintf(stderr, "Don't run ircd as root!!!\n"); | |
545 | return -1; | |
546 | } | |
547 | ||
548 | init_sys(); | |
549 | ||
550 | ConfigFileEntry.dpath = DPATH; | |
551 | ConfigFileEntry.configfile = CPATH; /* Server configuration file */ | |
552 | ConfigFileEntry.klinefile = KPATH; /* Server kline file */ | |
553 | ConfigFileEntry.dlinefile = DLPATH; /* dline file */ | |
554 | ConfigFileEntry.xlinefile = XPATH; | |
555 | ConfigFileEntry.resvfile = RESVPATH; | |
556 | ConfigFileEntry.connect_timeout = 30; /* Default to 30 */ | |
557 | ||
558 | umask(077); /* better safe than sorry --SRB */ | |
559 | ||
560 | myargv = argv; | |
561 | parseargs(&argc, &argv, myopts); | |
562 | ||
563 | if(chdir(ConfigFileEntry.dpath)) | |
564 | { | |
565 | fprintf(stderr, "Unable to chdir to %s: %s\n", ConfigFileEntry.dpath, strerror(errno)); | |
566 | exit(EXIT_FAILURE); | |
567 | } | |
568 | ||
569 | rb_set_time(); | |
570 | ||
571 | /* | |
572 | * Setup corefile size immediately after boot -kre | |
573 | */ | |
574 | setup_corefile(); | |
575 | ||
576 | memset(&me, 0, sizeof(me)); | |
577 | memset(&meLocalUser, 0, sizeof(meLocalUser)); | |
578 | me.localClient = &meLocalUser; | |
579 | ||
580 | /* Make sure all lists are zeroed */ | |
581 | memset(&unknown_list, 0, sizeof(unknown_list)); | |
582 | memset(&lclient_list, 0, sizeof(lclient_list)); | |
583 | memset(&serv_list, 0, sizeof(serv_list)); | |
584 | memset(&global_serv_list, 0, sizeof(global_serv_list)); | |
585 | memset(&local_oper_list, 0, sizeof(local_oper_list)); | |
586 | memset(&oper_list, 0, sizeof(oper_list)); | |
587 | ||
588 | rb_dlinkAddTail(&me, &me.node, &global_client_list); | |
589 | ||
590 | memset(&Count, 0, sizeof(Count)); | |
591 | memset(&ServerInfo, 0, sizeof(ServerInfo)); | |
592 | memset(&AdminInfo, 0, sizeof(AdminInfo)); | |
593 | memset(&ServerStats, 0, sizeof(struct ServerStatistics)); | |
594 | ||
595 | /* Initialise the channel capability usage counts... */ | |
596 | init_chcap_usage_counts(); | |
597 | ||
598 | if(printVersion) | |
599 | { | |
600 | printf("ircd: version %s(%s)\n", ircd_version, serno); | |
601 | printf("ircd: %s\n", rb_lib_version()); | |
602 | exit(EXIT_SUCCESS); | |
603 | } | |
604 | ||
605 | ||
606 | ||
607 | setup_signals(); | |
608 | ||
609 | if (testing_conf) | |
610 | server_state_foreground = 1; | |
611 | ||
612 | /* Make sure fd 0, 1 and 2 are in use -- jilles */ | |
613 | do | |
614 | { | |
615 | fd = open("/dev/null", O_RDWR); | |
616 | } while (fd < 2 && fd != -1); | |
617 | if (fd > 2) | |
618 | close(fd); | |
619 | else if (fd == -1) | |
620 | exit(1); | |
621 | ||
622 | /* Check if there is pidfile and daemon already running */ | |
623 | if(!testing_conf) | |
624 | { | |
625 | check_pidfile(pidFileName); | |
626 | ||
627 | if(!server_state_foreground) | |
628 | make_daemon(); | |
629 | inotice("starting %s ...", ircd_version); | |
630 | inotice("%s", rb_lib_version()); | |
631 | } | |
632 | ||
633 | /* Init the event subsystem */ | |
634 | rb_lib_init(ircd_log_cb, ircd_restart_cb, ircd_die_cb, !server_state_foreground, maxconnections, DNODE_HEAP_SIZE, FD_HEAP_SIZE); | |
635 | rb_linebuf_init(LINEBUF_HEAP_SIZE); | |
636 | ||
637 | if(ConfigFileEntry.use_egd && (ConfigFileEntry.egdpool_path != NULL)) | |
638 | { | |
639 | rb_init_prng(ConfigFileEntry.egdpool_path, RB_PRNG_EGD); | |
640 | } else | |
641 | rb_init_prng(NULL, RB_PRNG_DEFAULT); | |
642 | ||
643 | seed_random(NULL); | |
644 | ||
645 | init_main_logfile(); | |
646 | newconf_init(); | |
647 | init_s_conf(); | |
648 | init_s_newconf(); | |
649 | init_hash(); | |
650 | clear_scache_hash_table(); /* server cache name table */ | |
651 | init_host_hash(); | |
652 | clear_hash_parse(); | |
653 | init_client(); | |
654 | init_hook(); | |
655 | init_channels(); | |
656 | initclass(); | |
657 | initwhowas(); | |
658 | init_reject(); | |
659 | init_cache(); | |
660 | init_monitor(); | |
661 | init_isupport(); | |
662 | load_all_modules(1); | |
663 | #ifndef STATIC_MODULES | |
664 | load_core_modules(1); | |
665 | #endif | |
666 | init_auth(); /* Initialise the auth code */ | |
667 | init_resolver(); /* Needs to be setup before the io loop */ | |
668 | privilegeset_set_new("default", "", 0); | |
669 | ||
670 | if (testing_conf) | |
671 | fprintf(stderr, "\nBeginning config test\n"); | |
672 | read_conf_files(YES); /* cold start init conf files */ | |
673 | rehash_bans(0); | |
674 | #ifndef STATIC_MODULES | |
675 | ||
676 | mod_add_path(MODULE_DIR); | |
677 | mod_add_path(MODULE_DIR "/autoload"); | |
678 | #endif | |
679 | ||
680 | init_ssld(); | |
681 | ||
682 | initialize_server_capabs(); /* Set up default_server_capabs */ | |
683 | initialize_global_set_options(); | |
684 | ||
685 | if(ServerInfo.name == NULL) | |
686 | { | |
687 | ierror("no server name specified in serverinfo block."); | |
688 | return -1; | |
689 | } | |
690 | rb_strlcpy(me.name, ServerInfo.name, sizeof(me.name)); | |
691 | ||
692 | if(ServerInfo.sid[0] == '\0') | |
693 | { | |
694 | ierror("no server sid specified in serverinfo block."); | |
695 | return -2; | |
696 | } | |
697 | strcpy(me.id, ServerInfo.sid); | |
698 | init_uid(); | |
699 | ||
700 | /* serverinfo{} description must exist. If not, error out. */ | |
701 | if(ServerInfo.description == NULL) | |
702 | { | |
703 | ierror("no server description specified in serverinfo block."); | |
704 | return -3; | |
705 | } | |
706 | rb_strlcpy(me.info, ServerInfo.description, sizeof(me.info)); | |
707 | ||
708 | if(ServerInfo.ssl_cert != NULL && ServerInfo.ssl_private_key != NULL) | |
709 | { | |
710 | /* just do the rb_setup_ssl_server to validate the config */ | |
711 | if(!rb_setup_ssl_server(ServerInfo.ssl_cert, ServerInfo.ssl_private_key, ServerInfo.ssl_dh_params)) | |
712 | { | |
713 | ilog(L_MAIN, "WARNING: Unable to setup SSL."); | |
714 | ssl_ok = 0; | |
715 | } | |
716 | else | |
717 | ssl_ok = 1; | |
718 | } | |
719 | ||
720 | if (testing_conf) | |
721 | { | |
722 | fprintf(stderr, "\nConfig testing complete.\n"); | |
723 | fflush(stderr); | |
724 | return 0; /* Why? We want the launcher to exit out. */ | |
725 | } | |
726 | ||
727 | me.from = &me; | |
728 | me.servptr = &me; | |
729 | SetMe(&me); | |
730 | make_server(&me); | |
731 | startup_time = rb_current_time(); | |
732 | add_to_client_hash(me.name, &me); | |
733 | add_to_id_hash(me.id, &me); | |
734 | me.serv->nameinfo = scache_connect(me.name, me.info, 0); | |
735 | ||
736 | rb_dlinkAddAlloc(&me, &global_serv_list); | |
737 | ||
738 | construct_umodebuf(); | |
739 | construct_noparam_modes(); | |
740 | ||
741 | check_class(); | |
742 | write_pidfile(pidFileName); | |
743 | load_help(); | |
744 | open_logfiles(); | |
745 | ||
746 | ilog(L_MAIN, "Server Ready"); | |
747 | ||
748 | /* We want try_connections to be called as soon as possible now! -- adrian */ | |
749 | /* No, 'cause after a restart it would cause all sorts of nick collides */ | |
750 | /* um. by waiting even longer, that just means we have even *more* | |
751 | * nick collisions. what a stupid idea. set an event for the IO loop --fl | |
752 | */ | |
753 | rb_event_addish("try_connections", try_connections, NULL, STARTUP_CONNECTIONS_TIME); | |
754 | rb_event_addonce("try_connections_startup", try_connections, NULL, 2); | |
755 | rb_event_add("check_rehash", check_rehash, NULL, 3); | |
756 | rb_event_addish("reseed_srand", seed_random, NULL, 300); /* reseed every 10 minutes */ | |
757 | ||
758 | if(splitmode) | |
759 | check_splitmode_ev = rb_event_add("check_splitmode", check_splitmode, NULL, 5); | |
760 | ||
761 | print_startup(getpid()); | |
762 | ||
763 | rb_lib_loop(0); | |
764 | ||
765 | return 0; | |
766 | } |