]>
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 | |
24 | * | |
6fce54ff | 25 | * $Id$ |
212380e3 AC |
26 | */ |
27 | ||
2af8c7ff | 28 | #include "ratbox_lib.h" |
212380e3 AC |
29 | #include "stdinc.h" |
30 | #include "setup.h" | |
31 | #include "config.h" | |
212380e3 AC |
32 | #include "ircd.h" |
33 | #include "channel.h" | |
34 | #include "class.h" | |
35 | #include "client.h" | |
36 | #include "common.h" | |
212380e3 | 37 | #include "hash.h" |
4562c604 | 38 | #include "match.h" |
212380e3 | 39 | #include "ircd_signal.h" |
212380e3 AC |
40 | #include "msg.h" /* msgtab */ |
41 | #include "hostmask.h" | |
42 | #include "numeric.h" | |
43 | #include "parse.h" | |
212380e3 AC |
44 | #include "restart.h" |
45 | #include "s_auth.h" | |
212380e3 | 46 | #include "s_conf.h" |
4016731b | 47 | #include "logger.h" |
212380e3 AC |
48 | #include "s_serv.h" /* try_connections */ |
49 | #include "s_user.h" | |
50 | #include "s_stats.h" | |
51 | #include "scache.h" | |
52 | #include "send.h" | |
53 | #include "supported.h" | |
54 | #include "whowas.h" | |
55 | #include "modules.h" | |
212380e3 AC |
56 | #include "hook.h" |
57 | #include "ircd_getopt.h" | |
212380e3 | 58 | #include "newconf.h" |
212380e3 AC |
59 | #include "reject.h" |
60 | #include "s_conf.h" | |
61 | #include "s_newconf.h" | |
62 | #include "cache.h" | |
63 | #include "monitor.h" | |
212380e3 AC |
64 | #include "patchlevel.h" |
65 | #include "serno.h" | |
c6d72037 | 66 | #include "sslproc.h" |
efccc22c | 67 | #include "chmode.h" |
39a68b53 | 68 | #include "privilege.h" |
6eebc373 | 69 | #include "bandbi.h" |
6d063f05 | 70 | #include "authd.h" |
b02a913b | 71 | #include "operhash.h" |
212380e3 | 72 | |
8bd5767b | 73 | /* /quote set variables */ |
2af8c7ff | 74 | struct SetOptions GlobalSetOptions; |
8bd5767b JT |
75 | |
76 | /* configuration set from ircd.conf */ | |
77 | struct config_file_entry ConfigFileEntry; | |
78 | /* server info set from ircd.conf */ | |
79 | struct server_info ServerInfo; | |
80 | /* admin info set from ircd.conf */ | |
1aad9782 VY |
81 | struct admin_info AdminInfo; |
82 | ||
8bd5767b | 83 | struct Counter Count; |
47adde3d VY |
84 | struct ServerStatistics ServerStats; |
85 | ||
1aad9782 | 86 | int maxconnections; |
8bd5767b | 87 | struct Client me; /* That's me */ |
1aad9782 VY |
88 | struct LocalUser meLocalUser; /* That's also part of me */ |
89 | ||
3d3d1a95 | 90 | rb_dlink_list global_client_list; |
8bd5767b | 91 | |
3d3d1a95 | 92 | /* unknown/client pointer lists */ |
8bd5767b | 93 | rb_dlink_list unknown_list; /* unknown clients ON this server only */ |
3d3d1a95 | 94 | rb_dlink_list lclient_list; /* local clients only ON this server */ |
8bd5767b JT |
95 | rb_dlink_list serv_list; /* local servers to this server ONLY */ |
96 | rb_dlink_list global_serv_list; /* global servers on the network */ | |
97 | rb_dlink_list local_oper_list; /* our opers, duplicated in lclient_list */ | |
b5b84cad VY |
98 | rb_dlink_list oper_list; /* network opers */ |
99 | ||
8bd5767b | 100 | const char *logFileName = LPATH; |
b5b84cad VY |
101 | const char *pidFileName = PPATH; |
102 | ||
1aad9782 | 103 | char **myargv; |
8bd5767b JT |
104 | int dorehash = 0; |
105 | int dorehashbans = 0; | |
106 | int doremotd = 0; | |
107 | int kline_queued = 0; | |
108 | int server_state_foreground = 0; | |
109 | int opers_see_all_users = 0; | |
110 | int ssl_ok = 0; | |
111 | int zlib_ok = 1; | |
112 | ||
113 | int testing_conf = 0; | |
cd27d99c | 114 | time_t startup_time; |
8bd5767b | 115 | |
346fba92 | 116 | int default_server_capabs; |
8bd5767b | 117 | |
cd27d99c VY |
118 | int splitmode; |
119 | int splitchecking; | |
120 | int split_users; | |
121 | int split_servers; | |
122 | int eob_count; | |
c6d72037 | 123 | |
fd5af3d0 VY |
124 | void |
125 | ircd_shutdown(const char *reason) | |
126 | { | |
127 | struct Client *target_p; | |
128 | rb_dlink_node *ptr; | |
129 | ||
130 | RB_DLINK_FOREACH(ptr, lclient_list.head) | |
131 | { | |
132 | target_p = ptr->data; | |
133 | ||
134 | sendto_one(target_p, ":%s NOTICE %s :Server Terminating. %s", | |
135 | me.name, target_p->name, reason); | |
136 | } | |
137 | ||
138 | RB_DLINK_FOREACH(ptr, serv_list.head) | |
139 | { | |
140 | target_p = ptr->data; | |
141 | ||
142 | sendto_one(target_p, ":%s ERROR :Terminated by %s", | |
143 | me.name, reason); | |
144 | } | |
145 | ||
146 | ilog(L_MAIN, "Server Terminating. %s", reason); | |
ea82a3ca | 147 | close_logfiles(); |
fd5af3d0 VY |
148 | |
149 | unlink(pidFileName); | |
150 | exit(0); | |
151 | } | |
152 | ||
212380e3 AC |
153 | /* |
154 | * print_startup - print startup information | |
155 | */ | |
156 | static void | |
157 | print_startup(int pid) | |
158 | { | |
b45b2dae JT |
159 | int fd; |
160 | ||
161 | close(1); | |
162 | fd = open("/dev/null", O_RDWR); | |
163 | if (fd == -1) { | |
164 | perror("open /dev/null"); | |
165 | exit(EXIT_FAILURE); | |
166 | } | |
167 | if (fd == 0) | |
168 | fd = dup(fd); | |
169 | if (fd != 1) | |
170 | abort(); | |
171 | ||
212380e3 AC |
172 | inotice("now running in %s mode from %s as pid %d ...", |
173 | !server_state_foreground ? "background" : "foreground", | |
174 | ConfigFileEntry.dpath, pid); | |
175 | ||
176 | /* let the parent process know the initialization was successful | |
177 | * -- jilles */ | |
178 | if (!server_state_foreground) | |
179 | write(0, ".", 1); | |
b45b2dae JT |
180 | if (dup2(1, 0) == -1) |
181 | abort(); | |
182 | if (dup2(1, 2) == -1) | |
183 | abort(); | |
212380e3 AC |
184 | } |
185 | ||
212380e3 AC |
186 | /* |
187 | * init_sys | |
188 | * | |
189 | * inputs - boot_daemon flag | |
190 | * output - none | |
191 | * side effects - if boot_daemon flag is not set, don't daemonize | |
192 | */ | |
193 | static void | |
194 | init_sys(void) | |
195 | { | |
8bd5767b JT |
196 | #if defined(RLIMIT_NOFILE) && defined(HAVE_SYS_RESOURCE_H) |
197 | struct rlimit limit; | |
198 | ||
199 | if(!getrlimit(RLIMIT_NOFILE, &limit)) | |
200 | { | |
201 | maxconnections = limit.rlim_cur; | |
202 | if(maxconnections <= MAX_BUFFER) | |
203 | { | |
204 | fprintf(stderr, "ERROR: Shell FD limits are too low.\n"); | |
ec86417c | 205 | fprintf(stderr, "ERROR: charybdis reserves %d FDs, shell limits must be above this\n", MAX_BUFFER); |
8bd5767b JT |
206 | exit(EXIT_FAILURE); |
207 | } | |
208 | return; | |
209 | } | |
210 | #endif /* RLIMIT_FD_MAX */ | |
101db4c4 | 211 | maxconnections = MAXCONNECTIONS; |
212380e3 AC |
212 | } |
213 | ||
214 | static int | |
215 | make_daemon(void) | |
216 | { | |
217 | int pid; | |
218 | int pip[2]; | |
219 | char c; | |
220 | ||
221 | if (pipe(pip) < 0) | |
222 | { | |
223 | perror("pipe"); | |
224 | exit(EXIT_FAILURE); | |
225 | } | |
226 | dup2(pip[1], 0); | |
227 | close(pip[1]); | |
228 | if((pid = fork()) < 0) | |
229 | { | |
230 | perror("fork"); | |
231 | exit(EXIT_FAILURE); | |
232 | } | |
233 | else if(pid > 0) | |
234 | { | |
235 | close(0); | |
236 | /* Wait for initialization to finish, successfully or | |
237 | * unsuccessfully. Until this point the child may still | |
238 | * write to stdout/stderr. | |
239 | * -- jilles */ | |
240 | if (read(pip[0], &c, 1) > 0) | |
241 | exit(EXIT_SUCCESS); | |
242 | else | |
243 | exit(EXIT_FAILURE); | |
244 | } | |
245 | ||
246 | close(pip[0]); | |
247 | setsid(); | |
248 | /* fclose(stdin); | |
249 | fclose(stdout); | |
250 | fclose(stderr); */ | |
251 | ||
252 | return 0; | |
253 | } | |
254 | ||
255 | static int printVersion = 0; | |
256 | ||
257 | struct lgetopt myopts[] = { | |
212380e3 AC |
258 | {"configfile", &ConfigFileEntry.configfile, |
259 | STRING, "File to use for ircd.conf"}, | |
212380e3 AC |
260 | {"logfile", &logFileName, |
261 | STRING, "File to use for ircd.log"}, | |
262 | {"pidfile", &pidFileName, | |
263 | STRING, "File to use for process ID"}, | |
264 | {"foreground", &server_state_foreground, | |
265 | YESNO, "Run in foreground (don't detach)"}, | |
266 | {"version", &printVersion, | |
267 | YESNO, "Print version and exit"}, | |
268 | {"conftest", &testing_conf, | |
269 | YESNO, "Test the configuration files and exit"}, | |
270 | {"help", NULL, USAGE, "Print this text"}, | |
271 | {NULL, NULL, STRING, NULL}, | |
272 | }; | |
273 | ||
212380e3 AC |
274 | static void |
275 | check_rehash(void *unused) | |
276 | { | |
277 | /* | |
278 | * Check to see whether we have to rehash the configuration .. | |
279 | */ | |
280 | if(dorehash) | |
281 | { | |
282 | rehash(1); | |
283 | dorehash = 0; | |
284 | } | |
285 | ||
286 | if(dorehashbans) | |
287 | { | |
288 | rehash_bans(1); | |
289 | dorehashbans = 0; | |
290 | } | |
291 | ||
292 | if(doremotd) | |
293 | { | |
294 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
295 | "Got signal SIGUSR1, reloading ircd motd file"); | |
041d54ff | 296 | cache_user_motd(); |
212380e3 AC |
297 | doremotd = 0; |
298 | } | |
299 | } | |
300 | ||
212380e3 AC |
301 | /* |
302 | * initalialize_global_set_options | |
303 | * | |
304 | * inputs - none | |
305 | * output - none | |
55abcbb2 | 306 | * side effects - This sets all global set options needed |
212380e3 AC |
307 | */ |
308 | static void | |
309 | initialize_global_set_options(void) | |
310 | { | |
311 | memset(&GlobalSetOptions, 0, sizeof(GlobalSetOptions)); | |
312 | /* memset( &ConfigFileEntry, 0, sizeof(ConfigFileEntry)); */ | |
313 | ||
8bd5767b JT |
314 | GlobalSetOptions.maxclients = ServerInfo.default_max_clients; |
315 | ||
316 | if(GlobalSetOptions.maxclients > (maxconnections - MAX_BUFFER) || (GlobalSetOptions.maxclients <= 0)) | |
101db4c4 VY |
317 | GlobalSetOptions.maxclients = maxconnections - MAX_BUFFER; |
318 | ||
212380e3 AC |
319 | GlobalSetOptions.autoconn = 1; |
320 | ||
321 | GlobalSetOptions.spam_time = MIN_JOIN_LEAVE_TIME; | |
322 | GlobalSetOptions.spam_num = MAX_JOIN_LEAVE_COUNT; | |
323 | ||
7f3382fe | 324 | GlobalSetOptions.floodcount = ConfigFileEntry.default_floodcount; |
212380e3 AC |
325 | |
326 | split_servers = ConfigChannel.default_split_server_count; | |
327 | split_users = ConfigChannel.default_split_user_count; | |
328 | ||
329 | if(split_users && split_servers | |
330 | && (ConfigChannel.no_create_on_split || ConfigChannel.no_join_on_split)) | |
331 | { | |
332 | splitmode = 1; | |
333 | splitchecking = 1; | |
334 | } | |
335 | ||
7f3382fe | 336 | GlobalSetOptions.ident_timeout = ConfigFileEntry.default_ident_timeout; |
212380e3 | 337 | |
f427c8b0 | 338 | rb_strlcpy(GlobalSetOptions.operstring, |
212380e3 AC |
339 | ConfigFileEntry.default_operstring, |
340 | sizeof(GlobalSetOptions.operstring)); | |
f427c8b0 | 341 | rb_strlcpy(GlobalSetOptions.adminstring, |
212380e3 AC |
342 | ConfigFileEntry.default_adminstring, |
343 | sizeof(GlobalSetOptions.adminstring)); | |
344 | ||
345 | /* memset( &ConfigChannel, 0, sizeof(ConfigChannel)); */ | |
346 | ||
347 | /* End of global set options */ | |
348 | ||
349 | } | |
350 | ||
351 | /* | |
352 | * initialize_server_capabs | |
353 | * | |
354 | * inputs - none | |
355 | * output - none | |
356 | */ | |
357 | static void | |
358 | initialize_server_capabs(void) | |
359 | { | |
360 | default_server_capabs &= ~CAP_ZIP; | |
361 | } | |
362 | ||
363 | ||
364 | /* | |
365 | * write_pidfile | |
366 | * | |
367 | * inputs - filename+path of pid file | |
368 | * output - none | |
369 | * side effects - write the pid of the ircd to filename | |
370 | */ | |
371 | static void | |
372 | write_pidfile(const char *filename) | |
373 | { | |
374 | FILE *fb; | |
375 | char buff[32]; | |
376 | if((fb = fopen(filename, "w"))) | |
377 | { | |
378 | unsigned int pid = (unsigned int) getpid(); | |
379 | ||
5203cba5 | 380 | snprintf(buff, sizeof(buff), "%u\n", pid); |
212380e3 AC |
381 | if((fputs(buff, fb) == -1)) |
382 | { | |
383 | ilog(L_MAIN, "Error writing %u to pid file %s (%s)", | |
384 | pid, filename, strerror(errno)); | |
385 | } | |
386 | fclose(fb); | |
387 | return; | |
388 | } | |
389 | else | |
390 | { | |
391 | ilog(L_MAIN, "Error opening pid file %s", filename); | |
392 | } | |
393 | } | |
394 | ||
395 | /* | |
396 | * check_pidfile | |
397 | * | |
398 | * inputs - filename+path of pid file | |
399 | * output - none | |
400 | * side effects - reads pid from pidfile and checks if ircd is in process | |
401 | * list. if it is, gracefully exits | |
402 | * -kre | |
403 | */ | |
404 | static void | |
405 | check_pidfile(const char *filename) | |
406 | { | |
407 | FILE *fb; | |
408 | char buff[32]; | |
409 | pid_t pidfromfile; | |
410 | ||
411 | /* Don't do logging here, since we don't have log() initialised */ | |
412 | if((fb = fopen(filename, "r"))) | |
413 | { | |
414 | if(fgets(buff, 20, fb) != NULL) | |
415 | { | |
416 | pidfromfile = atoi(buff); | |
417 | if(!kill(pidfromfile, 0)) | |
418 | { | |
419 | printf("ircd: daemon is already running\n"); | |
420 | exit(-1); | |
421 | } | |
422 | } | |
423 | fclose(fb); | |
424 | } | |
425 | } | |
426 | ||
427 | /* | |
428 | * setup_corefile | |
429 | * | |
430 | * inputs - nothing | |
431 | * output - nothing | |
432 | * side effects - setups corefile to system limits. | |
433 | * -kre | |
434 | */ | |
435 | static void | |
436 | setup_corefile(void) | |
437 | { | |
438 | #ifdef HAVE_SYS_RESOURCE_H | |
439 | struct rlimit rlim; /* resource limits */ | |
440 | ||
441 | /* Set corefilesize to maximum */ | |
442 | if(!getrlimit(RLIMIT_CORE, &rlim)) | |
443 | { | |
444 | rlim.rlim_cur = rlim.rlim_max; | |
445 | setrlimit(RLIMIT_CORE, &rlim); | |
446 | } | |
447 | #endif | |
448 | } | |
449 | ||
6fce54ff VY |
450 | static void |
451 | ircd_log_cb(const char *str) | |
452 | { | |
6643434b | 453 | ilog(L_MAIN, "libratbox reports: %s", str); |
6fce54ff VY |
454 | } |
455 | ||
456 | static void | |
457 | ircd_restart_cb(const char *str) | |
458 | { | |
b1daa815 | 459 | inotice("libratbox has called the restart callback: %s", str); |
6fce54ff VY |
460 | restart(str); |
461 | } | |
462 | ||
463 | /* | |
464 | * Why EXIT_FAILURE here? | |
465 | * Because if ircd_die_cb() is called it's because of a fatal | |
466 | * error inside libcharybdis, and we don't know how to handle the | |
467 | * exception, so it is logical to return a FAILURE exit code here. | |
468 | * --nenolod | |
469 | */ | |
470 | static void | |
471 | ircd_die_cb(const char *str) | |
472 | { | |
473 | if(str != NULL) | |
474 | { | |
475 | /* Try to get the message out to currently logged in operators. */ | |
6643434b VY |
476 | sendto_realops_snomask(SNO_GENERAL, L_NETWIDE, "libratbox has called the die callback..aborting: %s", str); |
477 | inotice("libratbox has called the die callback..aborting: %s", str); | |
6fce54ff | 478 | } |
6643434b | 479 | else |
b1daa815 | 480 | inotice("libratbox has called the die callback..aborting"); |
6fce54ff VY |
481 | |
482 | unlink(pidFileName); | |
483 | exit(EXIT_FAILURE); | |
484 | } | |
485 | ||
fa832850 AC |
486 | struct ev_entry *check_splitmode_ev = NULL; |
487 | ||
d80645d0 VY |
488 | static int |
489 | seed_with_urandom(void) | |
490 | { | |
491 | unsigned int seed; | |
492 | int fd; | |
493 | ||
494 | fd = open("/dev/urandom", O_RDONLY); | |
495 | if(fd >= 0) | |
496 | { | |
497 | if(read(fd, &seed, sizeof(seed)) == sizeof(seed)) | |
498 | { | |
499 | close(fd); | |
500 | srand(seed); | |
501 | return 1; | |
502 | } | |
5457b102 | 503 | close(fd); |
d80645d0 VY |
504 | } |
505 | return 0; | |
506 | } | |
507 | ||
508 | static void | |
509 | seed_with_clock(void) | |
510 | { | |
55abcbb2 | 511 | const struct timeval *tv; |
d80645d0 VY |
512 | rb_set_time(); |
513 | tv = rb_current_time_tv(); | |
514 | srand(tv->tv_sec ^ (tv->tv_usec | (getpid() << 20))); | |
515 | } | |
516 | ||
517 | static void | |
518 | seed_random(void *unused) | |
519 | { | |
520 | unsigned int seed; | |
521 | if(rb_get_random(&seed, sizeof(seed)) == -1) | |
522 | { | |
523 | if(!seed_with_urandom()) | |
524 | seed_with_clock(); | |
525 | return; | |
526 | } | |
527 | srand(seed); | |
528 | } | |
529 | ||
212380e3 AC |
530 | /* |
531 | * main | |
532 | * | |
533 | * Initializes the IRCd. | |
534 | * | |
535 | * Inputs - number of commandline args, args themselves | |
536 | * Outputs - none | |
537 | * Side Effects - this is where the ircd gets going right now | |
538 | */ | |
539 | int | |
bc38c72c | 540 | charybdis_main(int argc, char *argv[]) |
212380e3 AC |
541 | { |
542 | int fd; | |
543 | ||
544 | /* Check to see if the user is running us as root, which is a nono */ | |
545 | if(geteuid() == 0) | |
546 | { | |
547 | fprintf(stderr, "Don't run ircd as root!!!\n"); | |
548 | return -1; | |
549 | } | |
550 | ||
adc7be98 VY |
551 | init_sys(); |
552 | ||
befa24c8 JT |
553 | ConfigFileEntry.dpath = DPATH; |
554 | ConfigFileEntry.configfile = CPATH; /* Server configuration file */ | |
befa24c8 | 555 | ConfigFileEntry.connect_timeout = 30; /* Default to 30 */ |
346fba92 | 556 | |
befa24c8 | 557 | umask(077); /* better safe than sorry --SRB */ |
adc7be98 VY |
558 | |
559 | myargv = argv; | |
560 | parseargs(&argc, &argv, myopts); | |
561 | ||
562 | if(chdir(ConfigFileEntry.dpath)) | |
563 | { | |
564 | fprintf(stderr, "Unable to chdir to %s: %s\n", ConfigFileEntry.dpath, strerror(errno)); | |
565 | exit(EXIT_FAILURE); | |
566 | } | |
567 | ||
3d3d1a95 VY |
568 | rb_set_time(); |
569 | ||
212380e3 AC |
570 | /* |
571 | * Setup corefile size immediately after boot -kre | |
572 | */ | |
573 | setup_corefile(); | |
574 | ||
b02a913b AC |
575 | /* initialise operhash fairly early. */ |
576 | init_operhash(); | |
577 | ||
212380e3 AC |
578 | memset(&me, 0, sizeof(me)); |
579 | memset(&meLocalUser, 0, sizeof(meLocalUser)); | |
580 | me.localClient = &meLocalUser; | |
581 | ||
582 | /* Make sure all lists are zeroed */ | |
583 | memset(&unknown_list, 0, sizeof(unknown_list)); | |
584 | memset(&lclient_list, 0, sizeof(lclient_list)); | |
585 | memset(&serv_list, 0, sizeof(serv_list)); | |
586 | memset(&global_serv_list, 0, sizeof(global_serv_list)); | |
587 | memset(&local_oper_list, 0, sizeof(local_oper_list)); | |
588 | memset(&oper_list, 0, sizeof(oper_list)); | |
589 | ||
330fc5c1 | 590 | rb_dlinkAddTail(&me, &me.node, &global_client_list); |
212380e3 | 591 | |
2af8c7ff VY |
592 | memset(&Count, 0, sizeof(Count)); |
593 | memset(&ServerInfo, 0, sizeof(ServerInfo)); | |
594 | memset(&AdminInfo, 0, sizeof(AdminInfo)); | |
595 | memset(&ServerStats, 0, sizeof(struct ServerStatistics)); | |
212380e3 | 596 | |
212380e3 AC |
597 | if(printVersion) |
598 | { | |
b3803848 | 599 | printf("ircd: version %s(%s)\n", ircd_version, serno); |
8bd1c8a1 AC |
600 | #ifdef CUSTOM_BRANDING |
601 | printf("ircd: based on %s-%s\n", PACKAGE_NAME, PACKAGE_VERSION); | |
602 | #endif | |
d2b6b9f3 | 603 | printf("ircd: %s\n", rb_lib_version()); |
212380e3 AC |
604 | exit(EXIT_SUCCESS); |
605 | } | |
606 | ||
212380e3 AC |
607 | setup_signals(); |
608 | ||
212380e3 AC |
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); | |
31c86ea8 | 630 | inotice("%s", rb_lib_version()); |
212380e3 AC |
631 | } |
632 | ||
633 | /* Init the event subsystem */ | |
0edb29fa | 634 | rb_lib_init(ircd_log_cb, ircd_restart_cb, ircd_die_cb, !server_state_foreground, maxconnections, DNODE_HEAP_SIZE, FD_HEAP_SIZE); |
150f35f7 | 635 | rb_linebuf_init(LINEBUF_HEAP_SIZE); |
212380e3 | 636 | |
e861902f | 637 | rb_init_prng(NULL, RB_PRNG_DEFAULT); |
37cd159e | 638 | |
e1db84d8 VY |
639 | seed_random(NULL); |
640 | ||
346fba92 AC |
641 | init_builtin_capabs(); |
642 | default_server_capabs = CAP_MASK; | |
643 | ||
212380e3 | 644 | init_main_logfile(); |
212380e3 AC |
645 | newconf_init(); |
646 | init_s_conf(); | |
647 | init_s_newconf(); | |
648 | init_hash(); | |
649 | clear_scache_hash_table(); /* server cache name table */ | |
650 | init_host_hash(); | |
651 | clear_hash_parse(); | |
652 | init_client(); | |
212380e3 AC |
653 | init_hook(); |
654 | init_channels(); | |
655 | initclass(); | |
b47f8a4f | 656 | whowas_init(); |
212380e3 AC |
657 | init_reject(); |
658 | init_cache(); | |
659 | init_monitor(); | |
030cdce7 | 660 | |
19716b9f | 661 | construct_cflags_strings(); |
030cdce7 | 662 | |
212380e3 | 663 | load_all_modules(1); |
212380e3 | 664 | load_core_modules(1); |
2a19fc3f | 665 | |
212380e3 | 666 | init_auth(); /* Initialise the auth code */ |
6d063f05 | 667 | init_authd(); /* Start up authd. */ |
39a68b53 | 668 | privilegeset_set_new("default", "", 0); |
212380e3 AC |
669 | |
670 | if (testing_conf) | |
671 | fprintf(stderr, "\nBeginning config test\n"); | |
672 | read_conf_files(YES); /* 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 | } |