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