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