]> jfr.im git - irc/rqf/shadowircd.git/blob - src/ircd.c
Add is_halfop() and is_owner() and start using them in the new functions.
[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 #include "privilege.h"
70 #include "bandbi.h"
71
72 /* /quote set variables */
73 struct SetOptions GlobalSetOptions;
74
75 /* configuration set from ircd.conf */
76 struct config_file_entry ConfigFileEntry;
77 /* server info set from ircd.conf */
78 struct server_info ServerInfo;
79 /* admin info set from ircd.conf */
80 struct admin_info AdminInfo;
81
82 struct Counter Count;
83 struct ServerStatistics ServerStats;
84
85 int maxconnections;
86 struct Client me; /* That's me */
87 struct LocalUser meLocalUser; /* That's also part of me */
88
89 rb_dlink_list global_client_list;
90
91 /* unknown/client pointer lists */
92 rb_dlink_list unknown_list; /* unknown clients ON this server only */
93 rb_dlink_list lclient_list; /* local clients only ON this server */
94 rb_dlink_list serv_list; /* local servers to this server ONLY */
95 rb_dlink_list global_serv_list; /* global servers on the network */
96 rb_dlink_list local_oper_list; /* our opers, duplicated in lclient_list */
97 rb_dlink_list oper_list; /* network opers */
98
99 const char *logFileName = LPATH;
100 const char *pidFileName = PPATH;
101
102 char **myargv;
103 int dorehash = 0;
104 int dorehashbans = 0;
105 int doremotd = 0;
106 int kline_queued = 0;
107 int server_state_foreground = 0;
108 int opers_see_all_users = 0;
109 int ssl_ok = 0;
110 int zlib_ok = 1;
111
112 int testing_conf = 0;
113 time_t startup_time;
114
115 int default_server_capabs = CAP_MASK;
116
117 int splitmode;
118 int splitchecking;
119 int split_users;
120 int split_servers;
121 int eob_count;
122
123 void
124 ircd_shutdown(const char *reason)
125 {
126 struct Client *target_p;
127 rb_dlink_node *ptr;
128
129 RB_DLINK_FOREACH(ptr, lclient_list.head)
130 {
131 target_p = ptr->data;
132
133 sendto_one(target_p, ":%s NOTICE %s :Server Terminating. %s",
134 me.name, target_p->name, reason);
135 }
136
137 RB_DLINK_FOREACH(ptr, serv_list.head)
138 {
139 target_p = ptr->data;
140
141 sendto_one(target_p, ":%s ERROR :Terminated by %s",
142 me.name, reason);
143 }
144
145 ilog(L_MAIN, "Server Terminating. %s", reason);
146 close_logfiles();
147
148 unlink(pidFileName);
149 exit(0);
150 }
151
152 /*
153 * print_startup - print startup information
154 */
155 static void
156 print_startup(int pid)
157 {
158 inotice("now running in %s mode from %s as pid %d ...",
159 !server_state_foreground ? "background" : "foreground",
160 ConfigFileEntry.dpath, pid);
161
162 /* let the parent process know the initialization was successful
163 * -- jilles */
164 if (!server_state_foreground)
165 write(0, ".", 1);
166 fclose(stdin);
167 fclose(stdout);
168 fclose(stderr);
169 open("/dev/null", O_RDWR);
170 dup2(0, 1);
171 dup2(0, 2);
172 }
173
174 /*
175 * init_sys
176 *
177 * inputs - boot_daemon flag
178 * output - none
179 * side effects - if boot_daemon flag is not set, don't daemonize
180 */
181 static void
182 init_sys(void)
183 {
184 #if defined(RLIMIT_NOFILE) && defined(HAVE_SYS_RESOURCE_H)
185 struct rlimit limit;
186
187 if(!getrlimit(RLIMIT_NOFILE, &limit))
188 {
189 maxconnections = limit.rlim_cur;
190 if(maxconnections <= MAX_BUFFER)
191 {
192 fprintf(stderr, "ERROR: Shell FD limits are too low.\n");
193 fprintf(stderr, "ERROR: charybdis reserves %d FDs, shell limits must be above this\n", MAX_BUFFER);
194 exit(EXIT_FAILURE);
195 }
196 return;
197 }
198 #endif /* RLIMIT_FD_MAX */
199 maxconnections = MAXCONNECTIONS;
200 }
201
202 static int
203 make_daemon(void)
204 {
205 int pid;
206 int pip[2];
207 char c;
208
209 if (pipe(pip) < 0)
210 {
211 perror("pipe");
212 exit(EXIT_FAILURE);
213 }
214 dup2(pip[1], 0);
215 close(pip[1]);
216 if((pid = fork()) < 0)
217 {
218 perror("fork");
219 exit(EXIT_FAILURE);
220 }
221 else if(pid > 0)
222 {
223 close(0);
224 /* Wait for initialization to finish, successfully or
225 * unsuccessfully. Until this point the child may still
226 * write to stdout/stderr.
227 * -- jilles */
228 if (read(pip[0], &c, 1) > 0)
229 exit(EXIT_SUCCESS);
230 else
231 exit(EXIT_FAILURE);
232 }
233
234 close(pip[0]);
235 setsid();
236 /* fclose(stdin);
237 fclose(stdout);
238 fclose(stderr); */
239
240 return 0;
241 }
242
243 static int printVersion = 0;
244
245 struct lgetopt myopts[] = {
246 {"configfile", &ConfigFileEntry.configfile,
247 STRING, "File to use for ircd.conf"},
248 {"logfile", &logFileName,
249 STRING, "File to use for ircd.log"},
250 {"pidfile", &pidFileName,
251 STRING, "File to use for process ID"},
252 {"foreground", &server_state_foreground,
253 YESNO, "Run in foreground (don't detach)"},
254 {"version", &printVersion,
255 YESNO, "Print version and exit"},
256 {"conftest", &testing_conf,
257 YESNO, "Test the configuration files and exit"},
258 {"help", NULL, USAGE, "Print this text"},
259 {NULL, NULL, STRING, NULL},
260 };
261
262 static void
263 check_rehash(void *unused)
264 {
265 /*
266 * Check to see whether we have to rehash the configuration ..
267 */
268 if(dorehash)
269 {
270 rehash(1);
271 dorehash = 0;
272 }
273
274 if(dorehashbans)
275 {
276 rehash_bans(1);
277 dorehashbans = 0;
278 }
279
280 if(doremotd)
281 {
282 sendto_realops_snomask(SNO_GENERAL, L_ALL,
283 "Got signal SIGUSR1, reloading ircd motd file");
284 cache_user_motd();
285 doremotd = 0;
286 }
287 }
288
289 /*
290 * initalialize_global_set_options
291 *
292 * inputs - none
293 * output - none
294 * side effects - This sets all global set options needed
295 */
296 static void
297 initialize_global_set_options(void)
298 {
299 memset(&GlobalSetOptions, 0, sizeof(GlobalSetOptions));
300 /* memset( &ConfigFileEntry, 0, sizeof(ConfigFileEntry)); */
301
302 GlobalSetOptions.maxclients = ServerInfo.default_max_clients;
303
304 if(GlobalSetOptions.maxclients > (maxconnections - MAX_BUFFER) || (GlobalSetOptions.maxclients <= 0))
305 GlobalSetOptions.maxclients = maxconnections - MAX_BUFFER;
306
307 GlobalSetOptions.autoconn = 1;
308
309 GlobalSetOptions.spam_time = MIN_JOIN_LEAVE_TIME;
310 GlobalSetOptions.spam_num = MAX_JOIN_LEAVE_COUNT;
311
312 if(ConfigFileEntry.default_floodcount)
313 GlobalSetOptions.floodcount = ConfigFileEntry.default_floodcount;
314 else
315 GlobalSetOptions.floodcount = 10;
316
317 split_servers = ConfigChannel.default_split_server_count;
318 split_users = ConfigChannel.default_split_user_count;
319
320 if(split_users && split_servers
321 && (ConfigChannel.no_create_on_split || ConfigChannel.no_join_on_split))
322 {
323 splitmode = 1;
324 splitchecking = 1;
325 }
326
327 GlobalSetOptions.ident_timeout = IDENT_TIMEOUT;
328
329 rb_strlcpy(GlobalSetOptions.operstring,
330 ConfigFileEntry.default_operstring,
331 sizeof(GlobalSetOptions.operstring));
332 rb_strlcpy(GlobalSetOptions.adminstring,
333 ConfigFileEntry.default_adminstring,
334 sizeof(GlobalSetOptions.adminstring));
335
336 /* memset( &ConfigChannel, 0, sizeof(ConfigChannel)); */
337
338 /* End of global set options */
339
340 }
341
342 /*
343 * initialize_server_capabs
344 *
345 * inputs - none
346 * output - none
347 */
348 static void
349 initialize_server_capabs(void)
350 {
351 default_server_capabs &= ~CAP_ZIP;
352 }
353
354
355 /*
356 * write_pidfile
357 *
358 * inputs - filename+path of pid file
359 * output - none
360 * side effects - write the pid of the ircd to filename
361 */
362 static void
363 write_pidfile(const char *filename)
364 {
365 FILE *fb;
366 char buff[32];
367 if((fb = fopen(filename, "w")))
368 {
369 unsigned int pid = (unsigned int) getpid();
370
371 rb_snprintf(buff, sizeof(buff), "%u\n", pid);
372 if((fputs(buff, fb) == -1))
373 {
374 ilog(L_MAIN, "Error writing %u to pid file %s (%s)",
375 pid, filename, strerror(errno));
376 }
377 fclose(fb);
378 return;
379 }
380 else
381 {
382 ilog(L_MAIN, "Error opening pid file %s", filename);
383 }
384 }
385
386 /*
387 * check_pidfile
388 *
389 * inputs - filename+path of pid file
390 * output - none
391 * side effects - reads pid from pidfile and checks if ircd is in process
392 * list. if it is, gracefully exits
393 * -kre
394 */
395 static void
396 check_pidfile(const char *filename)
397 {
398 FILE *fb;
399 char buff[32];
400 pid_t pidfromfile;
401
402 /* Don't do logging here, since we don't have log() initialised */
403 if((fb = fopen(filename, "r")))
404 {
405 if(fgets(buff, 20, fb) != NULL)
406 {
407 pidfromfile = atoi(buff);
408 if(!kill(pidfromfile, 0))
409 {
410 printf("ircd: daemon is already running\n");
411 exit(-1);
412 }
413 }
414 fclose(fb);
415 }
416 }
417
418 /*
419 * setup_corefile
420 *
421 * inputs - nothing
422 * output - nothing
423 * side effects - setups corefile to system limits.
424 * -kre
425 */
426 static void
427 setup_corefile(void)
428 {
429 #ifdef HAVE_SYS_RESOURCE_H
430 struct rlimit rlim; /* resource limits */
431
432 /* Set corefilesize to maximum */
433 if(!getrlimit(RLIMIT_CORE, &rlim))
434 {
435 rlim.rlim_cur = rlim.rlim_max;
436 setrlimit(RLIMIT_CORE, &rlim);
437 }
438 #endif
439 }
440
441 static void
442 ircd_log_cb(const char *str)
443 {
444 ilog(L_MAIN, "libratbox reports: %s", str);
445 }
446
447 static void
448 ircd_restart_cb(const char *str)
449 {
450 inotice("libratbox has called the restart callback: %s", str);
451 restart(str);
452 }
453
454 /*
455 * Why EXIT_FAILURE here?
456 * Because if ircd_die_cb() is called it's because of a fatal
457 * error inside libcharybdis, and we don't know how to handle the
458 * exception, so it is logical to return a FAILURE exit code here.
459 * --nenolod
460 */
461 static void
462 ircd_die_cb(const char *str)
463 {
464 if(str != NULL)
465 {
466 /* Try to get the message out to currently logged in operators. */
467 sendto_realops_snomask(SNO_GENERAL, L_NETWIDE, "libratbox has called the die callback..aborting: %s", str);
468 inotice("libratbox has called the die callback..aborting: %s", str);
469 }
470 else
471 inotice("libratbox has called the die callback..aborting");
472
473 unlink(pidFileName);
474 exit(EXIT_FAILURE);
475 }
476
477 struct ev_entry *check_splitmode_ev = NULL;
478
479 static int
480 seed_with_urandom(void)
481 {
482 unsigned int seed;
483 int fd;
484
485 fd = open("/dev/urandom", O_RDONLY);
486 if(fd >= 0)
487 {
488 if(read(fd, &seed, sizeof(seed)) == sizeof(seed))
489 {
490 close(fd);
491 srand(seed);
492 return 1;
493 }
494 }
495 return 0;
496 }
497
498 static void
499 seed_with_clock(void)
500 {
501 const struct timeval *tv;
502 rb_set_time();
503 tv = rb_current_time_tv();
504 srand(tv->tv_sec ^ (tv->tv_usec | (getpid() << 20)));
505 }
506
507 static void
508 seed_random(void *unused)
509 {
510 unsigned int seed;
511 if(rb_get_random(&seed, sizeof(seed)) == -1)
512 {
513 if(!seed_with_urandom())
514 seed_with_clock();
515 return;
516 }
517 srand(seed);
518 }
519
520 /*
521 * main
522 *
523 * Initializes the IRCd.
524 *
525 * Inputs - number of commandline args, args themselves
526 * Outputs - none
527 * Side Effects - this is where the ircd gets going right now
528 */
529 int
530 main(int argc, char *argv[])
531 {
532 int fd;
533
534 /* Check to see if the user is running us as root, which is a nono */
535 if(geteuid() == 0)
536 {
537 fprintf(stderr, "Don't run ircd as root!!!\n");
538 return -1;
539 }
540
541 init_sys();
542
543 ConfigFileEntry.dpath = DPATH;
544 ConfigFileEntry.configfile = CPATH; /* Server configuration file */
545 ConfigFileEntry.connect_timeout = 30; /* Default to 30 */
546
547 umask(077); /* better safe than sorry --SRB */
548
549 myargv = argv;
550 parseargs(&argc, &argv, myopts);
551
552 if(chdir(ConfigFileEntry.dpath))
553 {
554 fprintf(stderr, "Unable to chdir to %s: %s\n", ConfigFileEntry.dpath, strerror(errno));
555 exit(EXIT_FAILURE);
556 }
557
558 rb_set_time();
559
560 /*
561 * Setup corefile size immediately after boot -kre
562 */
563 setup_corefile();
564
565 memset(&me, 0, sizeof(me));
566 memset(&meLocalUser, 0, sizeof(meLocalUser));
567 me.localClient = &meLocalUser;
568
569 /* Make sure all lists are zeroed */
570 memset(&unknown_list, 0, sizeof(unknown_list));
571 memset(&lclient_list, 0, sizeof(lclient_list));
572 memset(&serv_list, 0, sizeof(serv_list));
573 memset(&global_serv_list, 0, sizeof(global_serv_list));
574 memset(&local_oper_list, 0, sizeof(local_oper_list));
575 memset(&oper_list, 0, sizeof(oper_list));
576
577 rb_dlinkAddTail(&me, &me.node, &global_client_list);
578
579 memset(&Count, 0, sizeof(Count));
580 memset(&ServerInfo, 0, sizeof(ServerInfo));
581 memset(&AdminInfo, 0, sizeof(AdminInfo));
582 memset(&ServerStats, 0, sizeof(struct ServerStatistics));
583
584 /* Initialise the channel capability usage counts... */
585 init_chcap_usage_counts();
586
587 if(printVersion)
588 {
589 printf("ircd: version %s(%s)\n", ircd_version, serno);
590 printf("ircd: %s\n", rb_lib_version());
591 exit(EXIT_SUCCESS);
592 }
593
594
595
596 setup_signals();
597
598 if (testing_conf)
599 server_state_foreground = 1;
600
601 /* Make sure fd 0, 1 and 2 are in use -- jilles */
602 do
603 {
604 fd = open("/dev/null", O_RDWR);
605 } while (fd < 2 && fd != -1);
606 if (fd > 2)
607 close(fd);
608 else if (fd == -1)
609 exit(1);
610
611 /* Check if there is pidfile and daemon already running */
612 if(!testing_conf)
613 {
614 check_pidfile(pidFileName);
615
616 if(!server_state_foreground)
617 make_daemon();
618 inotice("starting %s ...", ircd_version);
619 inotice("%s", rb_lib_version());
620 }
621
622 /* Init the event subsystem */
623 rb_lib_init(ircd_log_cb, ircd_restart_cb, ircd_die_cb, !server_state_foreground, maxconnections, DNODE_HEAP_SIZE, FD_HEAP_SIZE);
624 rb_linebuf_init(LINEBUF_HEAP_SIZE);
625
626 if(ConfigFileEntry.use_egd && (ConfigFileEntry.egdpool_path != NULL))
627 {
628 rb_init_prng(ConfigFileEntry.egdpool_path, RB_PRNG_EGD);
629 } else
630 rb_init_prng(NULL, RB_PRNG_DEFAULT);
631
632 seed_random(NULL);
633
634 init_main_logfile();
635 newconf_init();
636 init_s_conf();
637 init_s_newconf();
638 init_hash();
639 clear_scache_hash_table(); /* server cache name table */
640 init_host_hash();
641 clear_hash_parse();
642 init_client();
643 init_hook();
644 init_channels();
645 initclass();
646 initwhowas();
647 init_reject();
648 init_cache();
649 init_monitor();
650 init_isupport();
651 load_all_modules(1);
652 #ifndef STATIC_MODULES
653 load_core_modules(1);
654 #endif
655 init_auth(); /* Initialise the auth code */
656 init_resolver(); /* Needs to be setup before the io loop */
657 privilegeset_set_new("default", "", 0);
658
659 if (testing_conf)
660 fprintf(stderr, "\nBeginning config test\n");
661 read_conf_files(YES); /* cold start init conf files */
662 #ifndef STATIC_MODULES
663
664 mod_add_path(MODULE_DIR);
665 mod_add_path(MODULE_DIR "/autoload");
666 #endif
667
668 init_bandb();
669 init_ssld();
670
671 rehash_bans(0);
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 }