]> jfr.im git - irc/rqf/shadowircd.git/blame - src/ircd.c
Restricted +O (operonly) channel mode to be set only by ircops
[irc/rqf/shadowircd.git] / src / ircd.c
CommitLineData
212380e3 1/*
b1d6fa8c 2 * charybdis: A slightly useful ircd.
212380e3 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
b1d6fa8c
VY
7 * Copyright (C) 2002-2008 ircd-ratbox development team
8 * Copyright (C) 2005-2008 charybdis development team
212380e3 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 *
b1d6fa8c 25 * $Id$
212380e3 26 */
27
7bab07d4 28#include "ratbox_lib.h"
212380e3 29#include "stdinc.h"
30#include "setup.h"
31#include "config.h"
212380e3 32#include "ircd.h"
33#include "channel.h"
34#include "class.h"
35#include "client.h"
36#include "common.h"
212380e3 37#include "hash.h"
13ae2f4b 38#include "match.h"
212380e3 39#include "ircd_signal.h"
212380e3 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"
212380e3 47#include "s_conf.h"
d3455e2c 48#include "logger.h"
212380e3 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"
212380e3 57#include "hook.h"
58#include "ircd_getopt.h"
212380e3 59#include "newconf.h"
212380e3 60#include "reject.h"
61#include "s_conf.h"
62#include "s_newconf.h"
63#include "cache.h"
64#include "monitor.h"
212380e3 65#include "patchlevel.h"
66#include "serno.h"
8db00894 67#include "sslproc.h"
75818939 68#include "chmode.h"
212380e3 69
b717a466 70/* /quote set variables */
7bab07d4 71struct SetOptions GlobalSetOptions;
b717a466
JT
72
73/* configuration set from ircd.conf */
74struct config_file_entry ConfigFileEntry;
75/* server info set from ircd.conf */
76struct server_info ServerInfo;
77/* admin info set from ircd.conf */
f0dd4837
VY
78struct admin_info AdminInfo;
79
b717a466 80struct Counter Count;
83251205
VY
81struct ServerStatistics ServerStats;
82
f0dd4837 83int maxconnections;
b717a466 84struct Client me; /* That's me */
f0dd4837
VY
85struct LocalUser meLocalUser; /* That's also part of me */
86
fd4535aa 87rb_dlink_list global_client_list;
b717a466 88
fd4535aa 89/* unknown/client pointer lists */
b717a466 90rb_dlink_list unknown_list; /* unknown clients ON this server only */
fd4535aa 91rb_dlink_list lclient_list; /* local clients only ON this server */
b717a466
JT
92rb_dlink_list serv_list; /* local servers to this server ONLY */
93rb_dlink_list global_serv_list; /* global servers on the network */
94rb_dlink_list local_oper_list; /* our opers, duplicated in lclient_list */
944de091
VY
95rb_dlink_list oper_list; /* network opers */
96
b717a466 97const char *logFileName = LPATH;
944de091
VY
98const char *pidFileName = PPATH;
99
f0dd4837 100char **myargv;
b717a466
JT
101int dorehash = 0;
102int dorehashbans = 0;
103int doremotd = 0;
104int kline_queued = 0;
105int server_state_foreground = 0;
106int opers_see_all_users = 0;
107int ssl_ok = 0;
108int zlib_ok = 1;
109
110int testing_conf = 0;
b5a93db8 111time_t startup_time;
b717a466 112
b5a93db8 113int default_server_capabs = CAP_MASK;
b717a466 114
b5a93db8
VY
115int splitmode;
116int splitchecking;
117int split_users;
118int split_servers;
119int eob_count;
8db00894 120
6972e25a
VY
121void
122ircd_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);
359dada2 144 close_logfiles();
6972e25a
VY
145
146 unlink(pidFileName);
147 exit(0);
148}
149
212380e3 150/*
151 * print_startup - print startup information
152 */
153static void
154print_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
212380e3 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 */
179static void
180init_sys(void)
181{
b717a466
JT
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 */
3fe90825 197 maxconnections = MAXCONNECTIONS;
212380e3 198}
199
200static int
201make_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
241static int printVersion = 0;
242
243struct 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
212380e3 268static void
269check_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");
a822ef29 290 cache_user_motd();
212380e3 291 doremotd = 0;
292 }
293}
294
212380e3 295/*
296 * initalialize_global_set_options
297 *
298 * inputs - none
299 * output - none
300 * side effects - This sets all global set options needed
301 */
302static void
303initialize_global_set_options(void)
304{
305 memset(&GlobalSetOptions, 0, sizeof(GlobalSetOptions));
306 /* memset( &ConfigFileEntry, 0, sizeof(ConfigFileEntry)); */
307
b717a466
JT
308 GlobalSetOptions.maxclients = ServerInfo.default_max_clients;
309
310 if(GlobalSetOptions.maxclients > (maxconnections - MAX_BUFFER) || (GlobalSetOptions.maxclients <= 0))
3fe90825
VY
311 GlobalSetOptions.maxclients = maxconnections - MAX_BUFFER;
312
212380e3 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
907468c4 335 rb_strlcpy(GlobalSetOptions.operstring,
212380e3 336 ConfigFileEntry.default_operstring,
337 sizeof(GlobalSetOptions.operstring));
907468c4 338 rb_strlcpy(GlobalSetOptions.adminstring,
212380e3 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 */
354static void
355initialize_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 */
368static void
369write_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
38e6acdd 377 rb_snprintf(buff, sizeof(buff), "%u\n", pid);
212380e3 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 */
401static void
402check_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 */
432static void
433setup_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
b1d6fa8c
VY
447static void
448ircd_log_cb(const char *str)
449{
450 ilog(L_MAIN, "%s", str);
451}
452
453static void
454ircd_restart_cb(const char *str)
455{
456 restart(str);
457}
458
459/*
460 * Why EXIT_FAILURE here?
461 * Because if ircd_die_cb() is called it's because of a fatal
462 * error inside libcharybdis, and we don't know how to handle the
463 * exception, so it is logical to return a FAILURE exit code here.
464 * --nenolod
465 */
466static void
467ircd_die_cb(const char *str)
468{
469 if(str != NULL)
470 {
471 /* Try to get the message out to currently logged in operators. */
472 sendto_realops_snomask(SNO_GENERAL, L_NETWIDE, "Server panic! %s", str);
473 inotice("server panic: %s", str);
474 }
475
476 unlink(pidFileName);
477 exit(EXIT_FAILURE);
478}
479
ccfe0e97
WP
480struct ev_entry *check_splitmode_ev = NULL;
481
8dd8b3e2
VY
482static int
483seed_with_urandom(void)
484{
485 unsigned int seed;
486 int fd;
487
488 fd = open("/dev/urandom", O_RDONLY);
489 if(fd >= 0)
490 {
491 if(read(fd, &seed, sizeof(seed)) == sizeof(seed))
492 {
493 close(fd);
494 srand(seed);
495 return 1;
496 }
497 }
498 return 0;
499}
500
501static void
502seed_with_clock(void)
503{
504 const struct timeval *tv;
505 rb_set_time();
506 tv = rb_current_time_tv();
507 srand(tv->tv_sec ^ (tv->tv_usec | (getpid() << 20)));
508}
509
510static void
511seed_random(void *unused)
512{
513 unsigned int seed;
514 if(rb_get_random(&seed, sizeof(seed)) == -1)
515 {
516 if(!seed_with_urandom())
517 seed_with_clock();
518 return;
519 }
520 srand(seed);
521}
522
212380e3 523/*
524 * main
525 *
526 * Initializes the IRCd.
527 *
528 * Inputs - number of commandline args, args themselves
529 * Outputs - none
530 * Side Effects - this is where the ircd gets going right now
531 */
532int
533main(int argc, char *argv[])
534{
535 int fd;
536
537 /* Check to see if the user is running us as root, which is a nono */
538 if(geteuid() == 0)
539 {
540 fprintf(stderr, "Don't run ircd as root!!!\n");
541 return -1;
542 }
543
9a7b24a6
VY
544 init_sys();
545
f43b492b
JT
546 ConfigFileEntry.dpath = DPATH;
547 ConfigFileEntry.configfile = CPATH; /* Server configuration file */
548 ConfigFileEntry.klinefile = KPATH; /* Server kline file */
549 ConfigFileEntry.dlinefile = DLPATH; /* dline file */
550 ConfigFileEntry.xlinefile = XPATH;
551 ConfigFileEntry.resvfile = RESVPATH;
552 ConfigFileEntry.connect_timeout = 30; /* Default to 30 */
553
554 umask(077); /* better safe than sorry --SRB */
9a7b24a6
VY
555
556 myargv = argv;
557 parseargs(&argc, &argv, myopts);
558
559 if(chdir(ConfigFileEntry.dpath))
560 {
561 fprintf(stderr, "Unable to chdir to %s: %s\n", ConfigFileEntry.dpath, strerror(errno));
562 exit(EXIT_FAILURE);
563 }
564
fd4535aa
VY
565 rb_set_time();
566
212380e3 567 /*
568 * Setup corefile size immediately after boot -kre
569 */
570 setup_corefile();
571
212380e3 572 memset(&me, 0, sizeof(me));
573 memset(&meLocalUser, 0, sizeof(meLocalUser));
574 me.localClient = &meLocalUser;
575
576 /* Make sure all lists are zeroed */
577 memset(&unknown_list, 0, sizeof(unknown_list));
578 memset(&lclient_list, 0, sizeof(lclient_list));
579 memset(&serv_list, 0, sizeof(serv_list));
580 memset(&global_serv_list, 0, sizeof(global_serv_list));
581 memset(&local_oper_list, 0, sizeof(local_oper_list));
582 memset(&oper_list, 0, sizeof(oper_list));
583
af81d5a0 584 rb_dlinkAddTail(&me, &me.node, &global_client_list);
212380e3 585
7bab07d4
VY
586 memset(&Count, 0, sizeof(Count));
587 memset(&ServerInfo, 0, sizeof(ServerInfo));
588 memset(&AdminInfo, 0, sizeof(AdminInfo));
589 memset(&ServerStats, 0, sizeof(struct ServerStatistics));
212380e3 590
591 /* Initialise the channel capability usage counts... */
592 init_chcap_usage_counts();
593
212380e3 594 if(printVersion)
595 {
7f980188 596 printf("ircd: version %s(%s)\n", ircd_version, serno);
212380e3 597 exit(EXIT_SUCCESS);
598 }
599
9a7b24a6 600
212380e3 601
602 setup_signals();
603
212380e3 604 if (testing_conf)
605 server_state_foreground = 1;
606
607 /* Make sure fd 0, 1 and 2 are in use -- jilles */
608 do
609 {
610 fd = open("/dev/null", O_RDWR);
611 } while (fd < 2 && fd != -1);
612 if (fd > 2)
613 close(fd);
614 else if (fd == -1)
615 exit(1);
616
617 /* Check if there is pidfile and daemon already running */
618 if(!testing_conf)
619 {
620 check_pidfile(pidFileName);
621
622 if(!server_state_foreground)
623 make_daemon();
624 inotice("starting %s ...", ircd_version);
625 }
626
627 /* Init the event subsystem */
15f1b609 628 rb_lib_init(ircd_log_cb, ircd_restart_cb, ircd_die_cb, !server_state_foreground, maxconnections, DNODE_HEAP_SIZE, FD_HEAP_SIZE);
348684d2 629 rb_linebuf_init(LINEBUF_HEAP_SIZE);
212380e3 630
631 init_main_logfile();
212380e3 632 newconf_init();
633 init_s_conf();
634 init_s_newconf();
635 init_hash();
636 clear_scache_hash_table(); /* server cache name table */
637 init_host_hash();
638 clear_hash_parse();
639 init_client();
212380e3 640 init_hook();
641 init_channels();
642 initclass();
643 initwhowas();
212380e3 644 init_reject();
645 init_cache();
646 init_monitor();
647 init_isupport();
648 load_all_modules(1);
649#ifndef STATIC_MODULES
650 load_core_modules(1);
651#endif
652 init_auth(); /* Initialise the auth code */
653 init_resolver(); /* Needs to be setup before the io loop */
654
655 if (testing_conf)
656 fprintf(stderr, "\nBeginning config test\n");
657 read_conf_files(YES); /* cold start init conf files */
658 rehash_bans(0);
659#ifndef STATIC_MODULES
660
661 mod_add_path(MODULE_DIR);
662 mod_add_path(MODULE_DIR "/autoload");
663#endif
664
8db00894
VY
665 init_ssld();
666
212380e3 667 initialize_server_capabs(); /* Set up default_server_capabs */
668 initialize_global_set_options();
669
670 if(ServerInfo.name == NULL)
671 {
672 ierror("no server name specified in serverinfo block.");
673 return -1;
674 }
907468c4 675 rb_strlcpy(me.name, ServerInfo.name, sizeof(me.name));
212380e3 676
677 if(ServerInfo.sid[0] == '\0')
678 {
679 ierror("no server sid specified in serverinfo block.");
680 return -2;
681 }
682 strcpy(me.id, ServerInfo.sid);
683 init_uid();
684
685 /* serverinfo{} description must exist. If not, error out. */
686 if(ServerInfo.description == NULL)
687 {
688 ierror("no server description specified in serverinfo block.");
689 return -3;
690 }
907468c4 691 rb_strlcpy(me.info, ServerInfo.description, sizeof(me.info));
212380e3 692
b717a466
JT
693 if(ServerInfo.ssl_cert != NULL && ServerInfo.ssl_private_key != NULL)
694 {
695 /* just do the rb_setup_ssl_server to validate the config */
696 if(!rb_setup_ssl_server(ServerInfo.ssl_cert, ServerInfo.ssl_private_key, ServerInfo.ssl_dh_params))
697 {
698 ilog(L_MAIN, "WARNING: Unable to setup SSL.");
699 ssl_ok = 0;
700 }
701 else
702 ssl_ok = 1;
8db00894
VY
703 }
704
212380e3 705 if (testing_conf)
706 {
707 fprintf(stderr, "\nConfig testing complete.\n");
708 fflush(stderr);
709 return 0; /* Why? We want the launcher to exit out. */
710 }
711
712 me.from = &me;
713 me.servptr = &me;
714 SetMe(&me);
715 make_server(&me);
9f6bbe3c 716 startup_time = rb_current_time();
212380e3 717 add_to_client_hash(me.name, &me);
718 add_to_id_hash(me.id, &me);
994544c2 719 me.serv->nameinfo = scache_connect(me.name, me.info, 0);
212380e3 720
af81d5a0 721 rb_dlinkAddAlloc(&me, &global_serv_list);
212380e3 722
723 construct_umodebuf();
75818939 724 construct_noparam_modes();
212380e3 725
726 check_class();
727 write_pidfile(pidFileName);
728 load_help();
729 open_logfiles();
730
731 ilog(L_MAIN, "Server Ready");
732
212380e3 733 /* We want try_connections to be called as soon as possible now! -- adrian */
734 /* No, 'cause after a restart it would cause all sorts of nick collides */
735 /* um. by waiting even longer, that just means we have even *more*
736 * nick collisions. what a stupid idea. set an event for the IO loop --fl
737 */
9e29fe51 738 rb_event_addish("try_connections", try_connections, NULL, STARTUP_CONNECTIONS_TIME);
42706093
VY
739 rb_event_addonce("try_connections_startup", try_connections, NULL, 2);
740 rb_event_add("check_rehash", check_rehash, NULL, 3);
8dd8b3e2 741 rb_event_addish("reseed_srand", seed_random, NULL, 300); /* reseed every 10 minutes */
212380e3 742
212380e3 743 if(splitmode)
42706093 744 check_splitmode_ev = rb_event_add("check_splitmode", check_splitmode, NULL, 5);
212380e3 745
212380e3 746 print_startup(getpid());
747
9b2fe0ba 748 rb_lib_loop(0);
212380e3 749
750 return 0;
751}