]>
Commit | Line | Data |
---|---|---|
212380e3 | 1 | /* |
6fce54ff | 2 | * charybdis: A slightly useful ircd. |
212380e3 AC |
3 | * ircd.c: Starts up and runs the ircd. |
4 | * | |
5 | * Copyright (C) 1990 Jarkko Oikarinen and University of Oulu, Co Center | |
6 | * Copyright (C) 1996-2002 Hybrid Development Team | |
6fce54ff | 7 | * Copyright (C) 2002-2008 ircd-ratbox development team |
adef4da1 | 8 | * Copyright (C) 2005-2013 charybdis development team |
212380e3 AC |
9 | * |
10 | * This program is free software; you can redistribute it and/or modify | |
11 | * it under the terms of the GNU General Public License as published by | |
12 | * the Free Software Foundation; either version 2 of the License, or | |
13 | * (at your option) any later version. | |
14 | * | |
15 | * This program is distributed in the hope that it will be useful, | |
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | * GNU General Public License for more details. | |
19 | * | |
20 | * You should have received a copy of the GNU General Public License | |
21 | * along with this program; if not, write to the Free Software | |
22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 | |
23 | * USA | |
212380e3 AC |
24 | */ |
25 | ||
fe037171 | 26 | #include "rb_lib.h" |
212380e3 AC |
27 | #include "stdinc.h" |
28 | #include "setup.h" | |
29 | #include "config.h" | |
212380e3 AC |
30 | #include "ircd.h" |
31 | #include "channel.h" | |
32 | #include "class.h" | |
33 | #include "client.h" | |
34 | #include "common.h" | |
212380e3 | 35 | #include "hash.h" |
4562c604 | 36 | #include "match.h" |
212380e3 | 37 | #include "ircd_signal.h" |
212380e3 AC |
38 | #include "msg.h" /* msgtab */ |
39 | #include "hostmask.h" | |
40 | #include "numeric.h" | |
41 | #include "parse.h" | |
212380e3 AC |
42 | #include "restart.h" |
43 | #include "s_auth.h" | |
212380e3 | 44 | #include "s_conf.h" |
4016731b | 45 | #include "logger.h" |
212380e3 AC |
46 | #include "s_serv.h" /* try_connections */ |
47 | #include "s_user.h" | |
48 | #include "s_stats.h" | |
49 | #include "scache.h" | |
50 | #include "send.h" | |
51 | #include "supported.h" | |
52 | #include "whowas.h" | |
53 | #include "modules.h" | |
212380e3 AC |
54 | #include "hook.h" |
55 | #include "ircd_getopt.h" | |
212380e3 | 56 | #include "newconf.h" |
212380e3 AC |
57 | #include "reject.h" |
58 | #include "s_conf.h" | |
59 | #include "s_newconf.h" | |
60 | #include "cache.h" | |
61 | #include "monitor.h" | |
212380e3 AC |
62 | #include "patchlevel.h" |
63 | #include "serno.h" | |
c6d72037 | 64 | #include "sslproc.h" |
efccc22c | 65 | #include "chmode.h" |
39a68b53 | 66 | #include "privilege.h" |
6eebc373 | 67 | #include "bandbi.h" |
6d063f05 | 68 | #include "authd.h" |
b02a913b | 69 | #include "operhash.h" |
212380e3 | 70 | |
8bd5767b | 71 | /* /quote set variables */ |
2af8c7ff | 72 | struct SetOptions GlobalSetOptions; |
8bd5767b JT |
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 */ | |
1aad9782 VY |
79 | struct admin_info AdminInfo; |
80 | ||
8bd5767b | 81 | struct Counter Count; |
47adde3d VY |
82 | struct ServerStatistics ServerStats; |
83 | ||
1aad9782 | 84 | int maxconnections; |
8bd5767b | 85 | struct Client me; /* That's me */ |
1aad9782 VY |
86 | struct LocalUser meLocalUser; /* That's also part of me */ |
87 | ||
3d3d1a95 | 88 | rb_dlink_list global_client_list; |
8bd5767b | 89 | |
3d3d1a95 | 90 | /* unknown/client pointer lists */ |
8bd5767b | 91 | rb_dlink_list unknown_list; /* unknown clients ON this server only */ |
3d3d1a95 | 92 | rb_dlink_list lclient_list; /* local clients only ON this server */ |
8bd5767b JT |
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 */ | |
b5b84cad VY |
96 | rb_dlink_list oper_list; /* network opers */ |
97 | ||
8bd5767b | 98 | const char *logFileName = LPATH; |
b5b84cad VY |
99 | const char *pidFileName = PPATH; |
100 | ||
1aad9782 | 101 | char **myargv; |
818c157a EM |
102 | bool dorehash = false; |
103 | bool dorehashbans = false; | |
104 | bool doremotd = false; | |
f66f0baa | 105 | bool kline_queued = false; |
8bd5767b JT |
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; | |
cd27d99c | 112 | time_t startup_time; |
8bd5767b | 113 | |
346fba92 | 114 | int default_server_capabs; |
8bd5767b | 115 | |
cd27d99c VY |
116 | int splitmode; |
117 | int splitchecking; | |
118 | int split_users; | |
119 | int split_servers; | |
120 | int eob_count; | |
c6d72037 | 121 | |
fd5af3d0 VY |
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); | |
ea82a3ca | 145 | close_logfiles(); |
fd5af3d0 VY |
146 | |
147 | unlink(pidFileName); | |
148 | exit(0); | |
149 | } | |
150 | ||
212380e3 AC |
151 | /* |
152 | * print_startup - print startup information | |
153 | */ | |
154 | static void | |
155 | print_startup(int pid) | |
156 | { | |
b45b2dae JT |
157 | int fd; |
158 | ||
159 | close(1); | |
160 | fd = open("/dev/null", O_RDWR); | |
161 | if (fd == -1) { | |
162 | perror("open /dev/null"); | |
163 | exit(EXIT_FAILURE); | |
164 | } | |
165 | if (fd == 0) | |
166 | fd = dup(fd); | |
167 | if (fd != 1) | |
168 | abort(); | |
169 | ||
212380e3 AC |
170 | inotice("now running in %s mode from %s as pid %d ...", |
171 | !server_state_foreground ? "background" : "foreground", | |
172 | ConfigFileEntry.dpath, pid); | |
173 | ||
174 | /* let the parent process know the initialization was successful | |
175 | * -- jilles */ | |
176 | if (!server_state_foreground) | |
177 | write(0, ".", 1); | |
b45b2dae JT |
178 | if (dup2(1, 0) == -1) |
179 | abort(); | |
180 | if (dup2(1, 2) == -1) | |
181 | abort(); | |
212380e3 AC |
182 | } |
183 | ||
212380e3 AC |
184 | /* |
185 | * init_sys | |
186 | * | |
187 | * inputs - boot_daemon flag | |
188 | * output - none | |
189 | * side effects - if boot_daemon flag is not set, don't daemonize | |
190 | */ | |
191 | static void | |
192 | init_sys(void) | |
193 | { | |
8bd5767b JT |
194 | #if defined(RLIMIT_NOFILE) && defined(HAVE_SYS_RESOURCE_H) |
195 | struct rlimit limit; | |
196 | ||
197 | if(!getrlimit(RLIMIT_NOFILE, &limit)) | |
198 | { | |
199 | maxconnections = limit.rlim_cur; | |
200 | if(maxconnections <= MAX_BUFFER) | |
201 | { | |
202 | fprintf(stderr, "ERROR: Shell FD limits are too low.\n"); | |
ec86417c | 203 | fprintf(stderr, "ERROR: charybdis reserves %d FDs, shell limits must be above this\n", MAX_BUFFER); |
8bd5767b JT |
204 | exit(EXIT_FAILURE); |
205 | } | |
206 | return; | |
207 | } | |
208 | #endif /* RLIMIT_FD_MAX */ | |
101db4c4 | 209 | maxconnections = MAXCONNECTIONS; |
212380e3 AC |
210 | } |
211 | ||
212 | static int | |
213 | make_daemon(void) | |
214 | { | |
215 | int pid; | |
216 | int pip[2]; | |
217 | char c; | |
218 | ||
219 | if (pipe(pip) < 0) | |
220 | { | |
221 | perror("pipe"); | |
222 | exit(EXIT_FAILURE); | |
223 | } | |
224 | dup2(pip[1], 0); | |
225 | close(pip[1]); | |
226 | if((pid = fork()) < 0) | |
227 | { | |
228 | perror("fork"); | |
229 | exit(EXIT_FAILURE); | |
230 | } | |
231 | else if(pid > 0) | |
232 | { | |
233 | close(0); | |
234 | /* Wait for initialization to finish, successfully or | |
235 | * unsuccessfully. Until this point the child may still | |
236 | * write to stdout/stderr. | |
237 | * -- jilles */ | |
238 | if (read(pip[0], &c, 1) > 0) | |
239 | exit(EXIT_SUCCESS); | |
240 | else | |
241 | exit(EXIT_FAILURE); | |
242 | } | |
243 | ||
244 | close(pip[0]); | |
245 | setsid(); | |
246 | /* fclose(stdin); | |
247 | fclose(stdout); | |
248 | fclose(stderr); */ | |
249 | ||
250 | return 0; | |
251 | } | |
252 | ||
253 | static int printVersion = 0; | |
254 | ||
255 | struct lgetopt myopts[] = { | |
212380e3 AC |
256 | {"configfile", &ConfigFileEntry.configfile, |
257 | STRING, "File to use for ircd.conf"}, | |
212380e3 AC |
258 | {"logfile", &logFileName, |
259 | STRING, "File to use for ircd.log"}, | |
260 | {"pidfile", &pidFileName, | |
261 | STRING, "File to use for process ID"}, | |
262 | {"foreground", &server_state_foreground, | |
263 | YESNO, "Run in foreground (don't detach)"}, | |
264 | {"version", &printVersion, | |
265 | YESNO, "Print version and exit"}, | |
266 | {"conftest", &testing_conf, | |
267 | YESNO, "Test the configuration files and exit"}, | |
268 | {"help", NULL, USAGE, "Print this text"}, | |
269 | {NULL, NULL, STRING, NULL}, | |
270 | }; | |
271 | ||
212380e3 AC |
272 | static void |
273 | check_rehash(void *unused) | |
274 | { | |
275 | /* | |
276 | * Check to see whether we have to rehash the configuration .. | |
277 | */ | |
278 | if(dorehash) | |
279 | { | |
280 | rehash(1); | |
818c157a | 281 | dorehash = false; |
212380e3 AC |
282 | } |
283 | ||
284 | if(dorehashbans) | |
285 | { | |
286 | rehash_bans(1); | |
818c157a | 287 | dorehashbans = false; |
212380e3 AC |
288 | } |
289 | ||
290 | if(doremotd) | |
291 | { | |
292 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
293 | "Got signal SIGUSR1, reloading ircd motd file"); | |
041d54ff | 294 | cache_user_motd(); |
818c157a | 295 | doremotd = false; |
212380e3 AC |
296 | } |
297 | } | |
298 | ||
212380e3 AC |
299 | /* |
300 | * initalialize_global_set_options | |
301 | * | |
302 | * inputs - none | |
303 | * output - none | |
55abcbb2 | 304 | * side effects - This sets all global set options needed |
212380e3 AC |
305 | */ |
306 | static void | |
307 | initialize_global_set_options(void) | |
308 | { | |
309 | memset(&GlobalSetOptions, 0, sizeof(GlobalSetOptions)); | |
310 | /* memset( &ConfigFileEntry, 0, sizeof(ConfigFileEntry)); */ | |
311 | ||
8bd5767b JT |
312 | GlobalSetOptions.maxclients = ServerInfo.default_max_clients; |
313 | ||
314 | if(GlobalSetOptions.maxclients > (maxconnections - MAX_BUFFER) || (GlobalSetOptions.maxclients <= 0)) | |
101db4c4 VY |
315 | GlobalSetOptions.maxclients = maxconnections - MAX_BUFFER; |
316 | ||
212380e3 AC |
317 | GlobalSetOptions.autoconn = 1; |
318 | ||
319 | GlobalSetOptions.spam_time = MIN_JOIN_LEAVE_TIME; | |
320 | GlobalSetOptions.spam_num = MAX_JOIN_LEAVE_COUNT; | |
321 | ||
7f3382fe | 322 | GlobalSetOptions.floodcount = ConfigFileEntry.default_floodcount; |
212380e3 AC |
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 | ||
7f3382fe | 334 | GlobalSetOptions.ident_timeout = ConfigFileEntry.default_ident_timeout; |
212380e3 | 335 | |
f427c8b0 | 336 | rb_strlcpy(GlobalSetOptions.operstring, |
212380e3 AC |
337 | ConfigFileEntry.default_operstring, |
338 | sizeof(GlobalSetOptions.operstring)); | |
f427c8b0 | 339 | rb_strlcpy(GlobalSetOptions.adminstring, |
212380e3 AC |
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 | ||
5203cba5 | 378 | snprintf(buff, sizeof(buff), "%u\n", pid); |
212380e3 AC |
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 | ||
6fce54ff VY |
448 | static void |
449 | ircd_log_cb(const char *str) | |
450 | { | |
fe037171 | 451 | ilog(L_MAIN, "librb reports: %s", str); |
6fce54ff VY |
452 | } |
453 | ||
454 | static void | |
455 | ircd_restart_cb(const char *str) | |
456 | { | |
fe037171 | 457 | inotice("librb has called the restart callback: %s", str); |
6fce54ff VY |
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. */ | |
fe037171 EM |
474 | sendto_realops_snomask(SNO_GENERAL, L_NETWIDE, "librb has called the die callback..aborting: %s", str); |
475 | inotice("librb has called the die callback..aborting: %s", str); | |
6fce54ff | 476 | } |
6643434b | 477 | else |
fe037171 | 478 | inotice("librb has called the die callback..aborting"); |
6fce54ff VY |
479 | |
480 | unlink(pidFileName); | |
481 | exit(EXIT_FAILURE); | |
482 | } | |
483 | ||
fa832850 AC |
484 | struct ev_entry *check_splitmode_ev = NULL; |
485 | ||
d80645d0 VY |
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 | } | |
5457b102 | 501 | close(fd); |
d80645d0 VY |
502 | } |
503 | return 0; | |
504 | } | |
505 | ||
506 | static void | |
507 | seed_with_clock(void) | |
508 | { | |
55abcbb2 | 509 | const struct timeval *tv; |
d80645d0 VY |
510 | rb_set_time(); |
511 | tv = rb_current_time_tv(); | |
512 | srand(tv->tv_sec ^ (tv->tv_usec | (getpid() << 20))); | |
513 | } | |
514 | ||
515 | static void | |
516 | seed_random(void *unused) | |
517 | { | |
518 | unsigned int seed; | |
519 | if(rb_get_random(&seed, sizeof(seed)) == -1) | |
520 | { | |
521 | if(!seed_with_urandom()) | |
522 | seed_with_clock(); | |
523 | return; | |
524 | } | |
525 | srand(seed); | |
526 | } | |
527 | ||
212380e3 AC |
528 | /* |
529 | * main | |
530 | * | |
531 | * Initializes the IRCd. | |
532 | * | |
533 | * Inputs - number of commandline args, args themselves | |
534 | * Outputs - none | |
535 | * Side Effects - this is where the ircd gets going right now | |
536 | */ | |
537 | int | |
bc38c72c | 538 | charybdis_main(int argc, char *argv[]) |
212380e3 AC |
539 | { |
540 | int fd; | |
541 | ||
542 | /* Check to see if the user is running us as root, which is a nono */ | |
543 | if(geteuid() == 0) | |
544 | { | |
545 | fprintf(stderr, "Don't run ircd as root!!!\n"); | |
546 | return -1; | |
547 | } | |
548 | ||
adc7be98 VY |
549 | init_sys(); |
550 | ||
befa24c8 JT |
551 | ConfigFileEntry.dpath = DPATH; |
552 | ConfigFileEntry.configfile = CPATH; /* Server configuration file */ | |
befa24c8 | 553 | ConfigFileEntry.connect_timeout = 30; /* Default to 30 */ |
346fba92 | 554 | |
befa24c8 | 555 | umask(077); /* better safe than sorry --SRB */ |
adc7be98 VY |
556 | |
557 | myargv = argv; | |
558 | parseargs(&argc, &argv, myopts); | |
559 | ||
560 | if(chdir(ConfigFileEntry.dpath)) | |
561 | { | |
562 | fprintf(stderr, "Unable to chdir to %s: %s\n", ConfigFileEntry.dpath, strerror(errno)); | |
563 | exit(EXIT_FAILURE); | |
564 | } | |
565 | ||
3d3d1a95 VY |
566 | rb_set_time(); |
567 | ||
212380e3 AC |
568 | /* |
569 | * Setup corefile size immediately after boot -kre | |
570 | */ | |
571 | setup_corefile(); | |
572 | ||
b02a913b AC |
573 | /* initialise operhash fairly early. */ |
574 | init_operhash(); | |
575 | ||
212380e3 AC |
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 | ||
330fc5c1 | 588 | rb_dlinkAddTail(&me, &me.node, &global_client_list); |
212380e3 | 589 | |
2af8c7ff VY |
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)); | |
212380e3 | 594 | |
212380e3 AC |
595 | if(printVersion) |
596 | { | |
b3803848 | 597 | printf("ircd: version %s(%s)\n", ircd_version, serno); |
8bd1c8a1 AC |
598 | #ifdef CUSTOM_BRANDING |
599 | printf("ircd: based on %s-%s\n", PACKAGE_NAME, PACKAGE_VERSION); | |
600 | #endif | |
d2b6b9f3 | 601 | printf("ircd: %s\n", rb_lib_version()); |
212380e3 AC |
602 | exit(EXIT_SUCCESS); |
603 | } | |
604 | ||
212380e3 AC |
605 | setup_signals(); |
606 | ||
212380e3 AC |
607 | if (testing_conf) |
608 | server_state_foreground = 1; | |
609 | ||
610 | /* Make sure fd 0, 1 and 2 are in use -- jilles */ | |
611 | do | |
612 | { | |
613 | fd = open("/dev/null", O_RDWR); | |
614 | } while (fd < 2 && fd != -1); | |
615 | if (fd > 2) | |
616 | close(fd); | |
617 | else if (fd == -1) | |
618 | exit(1); | |
619 | ||
620 | /* Check if there is pidfile and daemon already running */ | |
621 | if(!testing_conf) | |
622 | { | |
623 | check_pidfile(pidFileName); | |
624 | ||
625 | if(!server_state_foreground) | |
626 | make_daemon(); | |
627 | inotice("starting %s ...", ircd_version); | |
31c86ea8 | 628 | inotice("%s", rb_lib_version()); |
212380e3 AC |
629 | } |
630 | ||
631 | /* Init the event subsystem */ | |
0edb29fa | 632 | rb_lib_init(ircd_log_cb, ircd_restart_cb, ircd_die_cb, !server_state_foreground, maxconnections, DNODE_HEAP_SIZE, FD_HEAP_SIZE); |
150f35f7 | 633 | rb_linebuf_init(LINEBUF_HEAP_SIZE); |
212380e3 | 634 | |
e861902f | 635 | rb_init_prng(NULL, RB_PRNG_DEFAULT); |
37cd159e | 636 | |
e1db84d8 VY |
637 | seed_random(NULL); |
638 | ||
346fba92 AC |
639 | init_builtin_capabs(); |
640 | default_server_capabs = CAP_MASK; | |
641 | ||
212380e3 | 642 | init_main_logfile(); |
212380e3 AC |
643 | newconf_init(); |
644 | init_s_conf(); | |
645 | init_s_newconf(); | |
646 | init_hash(); | |
647 | clear_scache_hash_table(); /* server cache name table */ | |
648 | init_host_hash(); | |
649 | clear_hash_parse(); | |
650 | init_client(); | |
212380e3 AC |
651 | init_hook(); |
652 | init_channels(); | |
653 | initclass(); | |
b47f8a4f | 654 | whowas_init(); |
212380e3 AC |
655 | init_reject(); |
656 | init_cache(); | |
657 | init_monitor(); | |
030cdce7 | 658 | |
19716b9f | 659 | construct_cflags_strings(); |
030cdce7 | 660 | |
212380e3 | 661 | load_all_modules(1); |
212380e3 | 662 | load_core_modules(1); |
2a19fc3f | 663 | |
212380e3 | 664 | init_auth(); /* Initialise the auth code */ |
6d063f05 | 665 | init_authd(); /* Start up authd. */ |
8a26cd19 EM |
666 | init_nameserver_cache(); /* Get our nameserver cache for STATS a/A */ |
667 | ||
39a68b53 | 668 | privilegeset_set_new("default", "", 0); |
212380e3 AC |
669 | |
670 | if (testing_conf) | |
671 | fprintf(stderr, "\nBeginning config test\n"); | |
ea111ea5 | 672 | read_conf_files(true); /* cold start init conf files */ |
212380e3 | 673 | |
55abcbb2 KB |
674 | mod_add_path(MODULE_DIR); |
675 | mod_add_path(MODULE_DIR "/autoload"); | |
212380e3 | 676 | |
b583faf9 AC |
677 | init_isupport(); |
678 | ||
6eebc373 | 679 | init_bandb(); |
c6d72037 VY |
680 | init_ssld(); |
681 | ||
80c9ac51 AC |
682 | rehash_bans(0); |
683 | ||
212380e3 AC |
684 | initialize_server_capabs(); /* Set up default_server_capabs */ |
685 | initialize_global_set_options(); | |
686 | ||
687 | if(ServerInfo.name == NULL) | |
688 | { | |
689 | ierror("no server name specified in serverinfo block."); | |
690 | return -1; | |
691 | } | |
f427c8b0 | 692 | rb_strlcpy(me.name, ServerInfo.name, sizeof(me.name)); |
212380e3 AC |
693 | |
694 | if(ServerInfo.sid[0] == '\0') | |
695 | { | |
696 | ierror("no server sid specified in serverinfo block."); | |
697 | return -2; | |
698 | } | |
699 | strcpy(me.id, ServerInfo.sid); | |
700 | init_uid(); | |
701 | ||
702 | /* serverinfo{} description must exist. If not, error out. */ | |
703 | if(ServerInfo.description == NULL) | |
704 | { | |
705 | ierror("no server description specified in serverinfo block."); | |
706 | return -3; | |
707 | } | |
f427c8b0 | 708 | rb_strlcpy(me.info, ServerInfo.description, sizeof(me.info)); |
212380e3 | 709 | |
8bd5767b JT |
710 | if(ServerInfo.ssl_cert != NULL && ServerInfo.ssl_private_key != NULL) |
711 | { | |
712 | /* just do the rb_setup_ssl_server to validate the config */ | |
c1725bda | 713 | if(!rb_setup_ssl_server(ServerInfo.ssl_cert, ServerInfo.ssl_private_key, ServerInfo.ssl_dh_params, ServerInfo.ssl_cipher_list)) |
8bd5767b JT |
714 | { |
715 | ilog(L_MAIN, "WARNING: Unable to setup SSL."); | |
716 | ssl_ok = 0; | |
717 | } | |
718 | else | |
719 | ssl_ok = 1; | |
c6d72037 VY |
720 | } |
721 | ||
212380e3 AC |
722 | if (testing_conf) |
723 | { | |
724 | fprintf(stderr, "\nConfig testing complete.\n"); | |
725 | fflush(stderr); | |
726 | return 0; /* Why? We want the launcher to exit out. */ | |
727 | } | |
728 | ||
729 | me.from = &me; | |
730 | me.servptr = &me; | |
731 | SetMe(&me); | |
732 | make_server(&me); | |
e3354945 | 733 | startup_time = rb_current_time(); |
212380e3 AC |
734 | add_to_client_hash(me.name, &me); |
735 | add_to_id_hash(me.id, &me); | |
994544c2 | 736 | me.serv->nameinfo = scache_connect(me.name, me.info, 0); |
212380e3 | 737 | |
330fc5c1 | 738 | rb_dlinkAddAlloc(&me, &global_serv_list); |
212380e3 AC |
739 | |
740 | construct_umodebuf(); | |
741 | ||
742 | check_class(); | |
743 | write_pidfile(pidFileName); | |
744 | load_help(); | |
745 | open_logfiles(); | |
746 | ||
747 | ilog(L_MAIN, "Server Ready"); | |
748 | ||
212380e3 AC |
749 | /* We want try_connections to be called as soon as possible now! -- adrian */ |
750 | /* No, 'cause after a restart it would cause all sorts of nick collides */ | |
751 | /* um. by waiting even longer, that just means we have even *more* | |
752 | * nick collisions. what a stupid idea. set an event for the IO loop --fl | |
753 | */ | |
12aea5fe | 754 | rb_event_addish("try_connections", try_connections, NULL, STARTUP_CONNECTIONS_TIME); |
36645728 VY |
755 | rb_event_addonce("try_connections_startup", try_connections, NULL, 2); |
756 | rb_event_add("check_rehash", check_rehash, NULL, 3); | |
d80645d0 | 757 | rb_event_addish("reseed_srand", seed_random, NULL, 300); /* reseed every 10 minutes */ |
212380e3 | 758 | |
212380e3 | 759 | if(splitmode) |
36645728 | 760 | check_splitmode_ev = rb_event_add("check_splitmode", check_splitmode, NULL, 5); |
212380e3 | 761 | |
212380e3 AC |
762 | print_startup(getpid()); |
763 | ||
39944e1f | 764 | rb_lib_loop(0); |
212380e3 AC |
765 | |
766 | return 0; | |
767 | } |