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