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