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