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