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