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