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