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