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