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