]> jfr.im git - irc/rqf/shadowircd.git/blob - src/ircd.c
Do not check floodcount if user is messaging self.
[irc/rqf/shadowircd.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 Client me; /* That's me */
84 struct LocalUser meLocalUser; /* That's also part of me */
85
86 rb_dlink_list global_client_list;
87
88 /* unknown/client pointer lists */
89 rb_dlink_list unknown_list; /* unknown clients ON this server only */
90 rb_dlink_list lclient_list; /* local clients only ON this server */
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 /* patricia */
123 rb_bh *prefix_heap;
124 rb_bh *node_heap;
125 rb_bh *patricia_heap;
126
127 rb_bh *linebuf_heap;
128
129 rb_bh *dnode_heap;
130
131 void
132 ircd_shutdown(const char *reason)
133 {
134 struct Client *target_p;
135 rb_dlink_node *ptr;
136
137 RB_DLINK_FOREACH(ptr, lclient_list.head)
138 {
139 target_p = ptr->data;
140
141 sendto_one(target_p, ":%s NOTICE %s :Server Terminating. %s",
142 me.name, target_p->name, reason);
143 }
144
145 RB_DLINK_FOREACH(ptr, serv_list.head)
146 {
147 target_p = ptr->data;
148
149 sendto_one(target_p, ":%s ERROR :Terminated by %s",
150 me.name, reason);
151 }
152
153 ilog(L_MAIN, "Server Terminating. %s", reason);
154 close_logfiles();
155
156 unlink(pidFileName);
157 exit(0);
158 }
159
160 /*
161 * print_startup - print startup information
162 */
163 static void
164 print_startup(int pid)
165 {
166 inotice("now running in %s mode from %s as pid %d ...",
167 !server_state_foreground ? "background" : "foreground",
168 ConfigFileEntry.dpath, pid);
169
170 /* let the parent process know the initialization was successful
171 * -- jilles */
172 if (!server_state_foreground)
173 write(0, ".", 1);
174 fclose(stdin);
175 fclose(stdout);
176 fclose(stderr);
177 open("/dev/null", O_RDWR);
178 dup2(0, 1);
179 dup2(0, 2);
180 }
181
182 static void
183 ircd_log_cb(const char *str)
184 {
185 ilog(L_MAIN, "%s", str);
186 }
187
188 static void
189 ircd_restart_cb(const char *str)
190 {
191 restart(str);
192 }
193
194 /*
195 * Why EXIT_FAILURE here?
196 * Because if ircd_die_cb() is called it's because of a fatal
197 * error inside libcharybdis, and we don't know how to handle the
198 * exception, so it is logical to return a FAILURE exit code here.
199 * --nenolod
200 */
201 static void
202 ircd_die_cb(const char *str)
203 {
204 if(str != NULL)
205 {
206 /* Try to get the message out to currently logged in operators. */
207 sendto_realops_snomask(SNO_GENERAL, L_NETWIDE, "Server panic! %s", str);
208 inotice("server panic: %s", str);
209 }
210
211 unlink(pidFileName);
212 exit(EXIT_FAILURE);
213 }
214
215 /*
216 * init_sys
217 *
218 * inputs - boot_daemon flag
219 * output - none
220 * side effects - if boot_daemon flag is not set, don't daemonize
221 */
222 static void
223 init_sys(void)
224 {
225 #if defined(RLIMIT_NOFILE) && defined(HAVE_SYS_RESOURCE_H)
226 struct rlimit limit;
227
228 if(!getrlimit(RLIMIT_NOFILE, &limit))
229 {
230 maxconnections = limit.rlim_cur;
231 if(maxconnections <= MAX_BUFFER)
232 {
233 fprintf(stderr, "ERROR: Shell FD limits are too low.\n");
234 fprintf(stderr, "ERROR: ircd-ratbox reserves %d FDs, shell limits must be above this\n", MAX_BUFFER);
235 exit(EXIT_FAILURE);
236 }
237 return;
238 }
239 #endif /* RLIMIT_FD_MAX */
240 maxconnections = MAXCONNECTIONS;
241 }
242
243 static int
244 make_daemon(void)
245 {
246 int pid;
247 int pip[2];
248 char c;
249
250 if (pipe(pip) < 0)
251 {
252 perror("pipe");
253 exit(EXIT_FAILURE);
254 }
255 dup2(pip[1], 0);
256 close(pip[1]);
257 if((pid = fork()) < 0)
258 {
259 perror("fork");
260 exit(EXIT_FAILURE);
261 }
262 else if(pid > 0)
263 {
264 close(0);
265 /* Wait for initialization to finish, successfully or
266 * unsuccessfully. Until this point the child may still
267 * write to stdout/stderr.
268 * -- jilles */
269 if (read(pip[0], &c, 1) > 0)
270 exit(EXIT_SUCCESS);
271 else
272 exit(EXIT_FAILURE);
273 }
274
275 close(pip[0]);
276 setsid();
277 /* fclose(stdin);
278 fclose(stdout);
279 fclose(stderr); */
280
281 return 0;
282 }
283
284 static int printVersion = 0;
285
286 struct lgetopt myopts[] = {
287 {"dlinefile", &ConfigFileEntry.dlinefile,
288 STRING, "File to use for dlines.conf"},
289 {"configfile", &ConfigFileEntry.configfile,
290 STRING, "File to use for ircd.conf"},
291 {"klinefile", &ConfigFileEntry.klinefile,
292 STRING, "File to use for kline.conf"},
293 {"xlinefile", &ConfigFileEntry.xlinefile,
294 STRING, "File to use for xline.conf"},
295 {"resvfile", &ConfigFileEntry.resvfile,
296 STRING, "File to use for resv.conf"},
297 {"logfile", &logFileName,
298 STRING, "File to use for ircd.log"},
299 {"pidfile", &pidFileName,
300 STRING, "File to use for process ID"},
301 {"foreground", &server_state_foreground,
302 YESNO, "Run in foreground (don't detach)"},
303 {"version", &printVersion,
304 YESNO, "Print version and exit"},
305 {"conftest", &testing_conf,
306 YESNO, "Test the configuration files and exit"},
307 {"help", NULL, USAGE, "Print this text"},
308 {NULL, NULL, STRING, NULL},
309 };
310
311 static void
312 check_rehash(void *unused)
313 {
314 /*
315 * Check to see whether we have to rehash the configuration ..
316 */
317 if(dorehash)
318 {
319 rehash(1);
320 dorehash = 0;
321 }
322
323 if(dorehashbans)
324 {
325 rehash_bans(1);
326 dorehashbans = 0;
327 }
328
329 if(doremotd)
330 {
331 sendto_realops_snomask(SNO_GENERAL, L_ALL,
332 "Got signal SIGUSR1, reloading ircd motd file");
333 cache_user_motd();
334 doremotd = 0;
335 }
336 }
337
338 /*
339 * initalialize_global_set_options
340 *
341 * inputs - none
342 * output - none
343 * side effects - This sets all global set options needed
344 */
345 static void
346 initialize_global_set_options(void)
347 {
348 memset(&GlobalSetOptions, 0, sizeof(GlobalSetOptions));
349 /* memset( &ConfigFileEntry, 0, sizeof(ConfigFileEntry)); */
350
351 GlobalSetOptions.maxclients = ServerInfo.default_max_clients;
352
353 if(GlobalSetOptions.maxclients > (maxconnections - MAX_BUFFER) || (GlobalSetOptions.maxclients <= 0))
354 GlobalSetOptions.maxclients = maxconnections - MAX_BUFFER;
355
356 GlobalSetOptions.autoconn = 1;
357
358 GlobalSetOptions.spam_time = MIN_JOIN_LEAVE_TIME;
359 GlobalSetOptions.spam_num = MAX_JOIN_LEAVE_COUNT;
360
361 if(ConfigFileEntry.default_floodcount)
362 GlobalSetOptions.floodcount = ConfigFileEntry.default_floodcount;
363 else
364 GlobalSetOptions.floodcount = 10;
365
366 split_servers = ConfigChannel.default_split_server_count;
367 split_users = ConfigChannel.default_split_user_count;
368
369 if(split_users && split_servers
370 && (ConfigChannel.no_create_on_split || ConfigChannel.no_join_on_split))
371 {
372 splitmode = 1;
373 splitchecking = 1;
374 }
375
376 GlobalSetOptions.ident_timeout = IDENT_TIMEOUT;
377
378 rb_strlcpy(GlobalSetOptions.operstring,
379 ConfigFileEntry.default_operstring,
380 sizeof(GlobalSetOptions.operstring));
381 rb_strlcpy(GlobalSetOptions.adminstring,
382 ConfigFileEntry.default_adminstring,
383 sizeof(GlobalSetOptions.adminstring));
384
385 /* memset( &ConfigChannel, 0, sizeof(ConfigChannel)); */
386
387 /* End of global set options */
388
389 }
390
391 /*
392 * initialize_server_capabs
393 *
394 * inputs - none
395 * output - none
396 */
397 static void
398 initialize_server_capabs(void)
399 {
400 default_server_capabs &= ~CAP_ZIP;
401 }
402
403
404 /*
405 * write_pidfile
406 *
407 * inputs - filename+path of pid file
408 * output - none
409 * side effects - write the pid of the ircd to filename
410 */
411 static void
412 write_pidfile(const char *filename)
413 {
414 FILE *fb;
415 char buff[32];
416 if((fb = fopen(filename, "w")))
417 {
418 unsigned int pid = (unsigned int) getpid();
419
420 rb_snprintf(buff, sizeof(buff), "%u\n", pid);
421 if((fputs(buff, fb) == -1))
422 {
423 ilog(L_MAIN, "Error writing %u to pid file %s (%s)",
424 pid, filename, strerror(errno));
425 }
426 fclose(fb);
427 return;
428 }
429 else
430 {
431 ilog(L_MAIN, "Error opening pid file %s", filename);
432 }
433 }
434
435 /*
436 * check_pidfile
437 *
438 * inputs - filename+path of pid file
439 * output - none
440 * side effects - reads pid from pidfile and checks if ircd is in process
441 * list. if it is, gracefully exits
442 * -kre
443 */
444 static void
445 check_pidfile(const char *filename)
446 {
447 FILE *fb;
448 char buff[32];
449 pid_t pidfromfile;
450
451 /* Don't do logging here, since we don't have log() initialised */
452 if((fb = fopen(filename, "r")))
453 {
454 if(fgets(buff, 20, fb) != NULL)
455 {
456 pidfromfile = atoi(buff);
457 if(!kill(pidfromfile, 0))
458 {
459 printf("ircd: daemon is already running\n");
460 exit(-1);
461 }
462 }
463 fclose(fb);
464 }
465 }
466
467 /*
468 * setup_corefile
469 *
470 * inputs - nothing
471 * output - nothing
472 * side effects - setups corefile to system limits.
473 * -kre
474 */
475 static void
476 setup_corefile(void)
477 {
478 #ifdef HAVE_SYS_RESOURCE_H
479 struct rlimit rlim; /* resource limits */
480
481 /* Set corefilesize to maximum */
482 if(!getrlimit(RLIMIT_CORE, &rlim))
483 {
484 rlim.rlim_cur = rlim.rlim_max;
485 setrlimit(RLIMIT_CORE, &rlim);
486 }
487 #endif
488 }
489
490 struct ev_entry *check_splitmode_ev = NULL;
491
492 static int
493 seed_with_urandom(void)
494 {
495 unsigned int seed;
496 int fd;
497
498 fd = open("/dev/urandom", O_RDONLY);
499 if(fd >= 0)
500 {
501 if(read(fd, &seed, sizeof(seed)) == sizeof(seed))
502 {
503 close(fd);
504 srand(seed);
505 return 1;
506 }
507 }
508 return 0;
509 }
510
511 static void
512 seed_with_clock(void)
513 {
514 const struct timeval *tv;
515 rb_set_time();
516 tv = rb_current_time_tv();
517 srand(tv->tv_sec ^ (tv->tv_usec | (getpid() << 20)));
518 }
519
520 static void
521 seed_random(void *unused)
522 {
523 unsigned int seed;
524 if(rb_get_random(&seed, sizeof(seed)) == -1)
525 {
526 if(!seed_with_urandom())
527 seed_with_clock();
528 return;
529 }
530 srand(seed);
531 }
532
533 /*
534 * main
535 *
536 * Initializes the IRCd.
537 *
538 * Inputs - number of commandline args, args themselves
539 * Outputs - none
540 * Side Effects - this is where the ircd gets going right now
541 */
542 int
543 main(int argc, char *argv[])
544 {
545 int fd;
546
547 /* Check to see if the user is running us as root, which is a nono */
548 if(geteuid() == 0)
549 {
550 fprintf(stderr, "Don't run ircd as root!!!\n");
551 return -1;
552 }
553
554 rb_set_time();
555
556 /*
557 * Setup corefile size immediately after boot -kre
558 */
559 setup_corefile();
560
561 memset(&me, 0, sizeof(me));
562 memset(&meLocalUser, 0, sizeof(meLocalUser));
563 me.localClient = &meLocalUser;
564
565 /* Make sure all lists are zeroed */
566 memset(&unknown_list, 0, sizeof(unknown_list));
567 memset(&lclient_list, 0, sizeof(lclient_list));
568 memset(&serv_list, 0, sizeof(serv_list));
569 memset(&global_serv_list, 0, sizeof(global_serv_list));
570 memset(&local_oper_list, 0, sizeof(local_oper_list));
571 memset(&oper_list, 0, sizeof(oper_list));
572
573 rb_dlinkAddTail(&me, &me.node, &global_client_list);
574
575 memset(&Count, 0, sizeof(Count));
576 memset(&ServerInfo, 0, sizeof(ServerInfo));
577 memset(&AdminInfo, 0, sizeof(AdminInfo));
578 memset(&ServerStats, 0, sizeof(struct ServerStatistics));
579
580 /* Initialise the channel capability usage counts... */
581 init_chcap_usage_counts();
582
583 ConfigFileEntry.dpath = DPATH;
584 ConfigFileEntry.configfile = CPATH; /* Server configuration file */
585 ConfigFileEntry.klinefile = KPATH; /* Server kline file */
586 ConfigFileEntry.dlinefile = DLPATH; /* dline file */
587 ConfigFileEntry.xlinefile = XPATH;
588 ConfigFileEntry.resvfile = RESVPATH;
589 ConfigFileEntry.connect_timeout = 30; /* Default to 30 */
590 myargv = argv;
591 umask(077); /* better safe than sorry --SRB */
592
593 parseargs(&argc, &argv, myopts);
594
595 if(printVersion)
596 {
597 printf("ircd: version %s(%s)\n", ircd_version, serno);
598 exit(EXIT_SUCCESS);
599 }
600
601 if(chdir(ConfigFileEntry.dpath))
602 {
603 fprintf(stderr, "Unable to chdir to %s: %s\n", ConfigFileEntry.dpath, strerror(errno));
604 exit(EXIT_FAILURE);
605 }
606
607 setup_signals();
608
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);
630 }
631
632 /* Init the event subsystem */
633 init_sys();
634 rb_lib_init(ircd_log_cb, ircd_restart_cb, ircd_die_cb, !server_state_foreground, maxconnections, DNODE_HEAP_SIZE, FD_HEAP_SIZE);
635 rb_linebuf_init(LINEBUF_HEAP_SIZE);
636
637 init_main_logfile();
638 newconf_init();
639 init_s_conf();
640 init_s_newconf();
641 init_hash();
642 clear_scache_hash_table(); /* server cache name table */
643 init_host_hash();
644 clear_hash_parse();
645 init_client();
646 init_hook();
647 init_channels();
648 initclass();
649 initwhowas();
650 init_reject();
651 init_cache();
652 init_monitor();
653 init_isupport();
654 load_all_modules(1);
655 #ifndef STATIC_MODULES
656 load_core_modules(1);
657 #endif
658 init_auth(); /* Initialise the auth code */
659 init_resolver(); /* Needs to be setup before the io loop */
660
661 if (testing_conf)
662 fprintf(stderr, "\nBeginning config test\n");
663 read_conf_files(YES); /* cold start init conf files */
664 rehash_bans(0);
665 #ifndef STATIC_MODULES
666
667 mod_add_path(MODULE_DIR);
668 mod_add_path(MODULE_DIR "/autoload");
669 #endif
670
671 init_ssld();
672
673 initialize_server_capabs(); /* Set up default_server_capabs */
674 initialize_global_set_options();
675
676 if(ServerInfo.name == NULL)
677 {
678 ierror("no server name specified in serverinfo block.");
679 return -1;
680 }
681 rb_strlcpy(me.name, ServerInfo.name, sizeof(me.name));
682
683 if(ServerInfo.sid[0] == '\0')
684 {
685 ierror("no server sid specified in serverinfo block.");
686 return -2;
687 }
688 strcpy(me.id, ServerInfo.sid);
689 init_uid();
690
691 /* serverinfo{} description must exist. If not, error out. */
692 if(ServerInfo.description == NULL)
693 {
694 ierror("no server description specified in serverinfo block.");
695 return -3;
696 }
697 rb_strlcpy(me.info, ServerInfo.description, sizeof(me.info));
698
699 if(ServerInfo.ssl_cert != NULL && ServerInfo.ssl_private_key != NULL)
700 {
701 /* just do the rb_setup_ssl_server to validate the config */
702 if(!rb_setup_ssl_server(ServerInfo.ssl_cert, ServerInfo.ssl_private_key, ServerInfo.ssl_dh_params))
703 {
704 ilog(L_MAIN, "WARNING: Unable to setup SSL.");
705 ssl_ok = 0;
706 }
707 else
708 ssl_ok = 1;
709 }
710
711 if (testing_conf)
712 {
713 fprintf(stderr, "\nConfig testing complete.\n");
714 fflush(stderr);
715 return 0; /* Why? We want the launcher to exit out. */
716 }
717
718 me.from = &me;
719 me.servptr = &me;
720 SetMe(&me);
721 make_server(&me);
722 startup_time = rb_current_time();
723 add_to_client_hash(me.name, &me);
724 add_to_id_hash(me.id, &me);
725 me.serv->nameinfo = scache_connect(me.name, me.info, 0);
726
727 rb_dlinkAddAlloc(&me, &global_serv_list);
728
729 construct_umodebuf();
730 construct_noparam_modes();
731
732 check_class();
733 write_pidfile(pidFileName);
734 load_help();
735 open_logfiles();
736
737 ilog(L_MAIN, "Server Ready");
738
739 /* We want try_connections to be called as soon as possible now! -- adrian */
740 /* No, 'cause after a restart it would cause all sorts of nick collides */
741 /* um. by waiting even longer, that just means we have even *more*
742 * nick collisions. what a stupid idea. set an event for the IO loop --fl
743 */
744 rb_event_addish("try_connections", try_connections, NULL, STARTUP_CONNECTIONS_TIME);
745 rb_event_addonce("try_connections_startup", try_connections, NULL, 2);
746 rb_event_add("check_rehash", check_rehash, NULL, 3);
747 rb_event_addish("reseed_srand", seed_random, NULL, 300); /* reseed every 10 minutes */
748
749 if(splitmode)
750 check_splitmode_ev = rb_event_add("check_splitmode", check_splitmode, NULL, 5);
751
752 print_startup(getpid());
753
754 rb_lib_loop(0);
755
756 return 0;
757 }