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