2 * charybdis: A slightly useful ircd.
3 * ircd.c: Starts up and runs the ircd.
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
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.
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.
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
36 #include "ircd_signal.h"
37 #include "msg.h" /* msgtab */
44 #include "s_serv.h" /* try_connections */
49 #include "supported.h"
53 #include "ircd_getopt.h"
56 #include "s_newconf.h"
59 #include "patchlevel.h"
64 #include "privilege.h"
69 /* /quote set variables */
70 struct SetOptions GlobalSetOptions
;
72 /* configuration set from ircd.conf */
73 struct config_file_entry ConfigFileEntry
;
74 /* server info set from ircd.conf */
75 struct server_info ServerInfo
;
76 /* admin info set from ircd.conf */
77 struct admin_info AdminInfo
;
80 struct ServerStatistics ServerStats
;
83 struct Client me
; /* That's me */
84 struct LocalUser meLocalUser
; /* That's also part of me */
86 rb_dlink_list global_client_list
;
88 /* unknown/client pointer lists */
89 rb_dlink_list unknown_list
; /* unknown clients ON this server only */
90 rb_dlink_list lclient_list
; /* local clients only ON this server */
91 rb_dlink_list serv_list
; /* local servers to this server ONLY */
92 rb_dlink_list global_serv_list
; /* global servers on the network */
93 rb_dlink_list local_oper_list
; /* our opers, duplicated in lclient_list */
94 rb_dlink_list oper_list
; /* network opers */
97 volatile sig_atomic_t dorehash
= false;
98 volatile sig_atomic_t dorehashbans
= false;
99 volatile sig_atomic_t doremotd
= false;
100 bool kline_queued
= false;
101 bool server_state_foreground
= false;
102 bool opers_see_all_users
= false;
103 bool ircd_ssl_ok
= false;
104 bool ircd_zlib_ok
= true;
106 int testing_conf
= 0;
109 int default_server_capabs
;
117 const char *ircd_paths
[IRCD_PATH_COUNT
] = {
118 [IRCD_PATH_PREFIX
] = DPATH
,
119 [IRCD_PATH_MODULES
] = MODPATH
,
120 [IRCD_PATH_AUTOLOAD_MODULES
] = AUTOMODPATH
,
121 [IRCD_PATH_ETC
] = ETCPATH
,
122 [IRCD_PATH_LOG
] = LOGPATH
,
123 [IRCD_PATH_USERHELP
] = UHPATH
,
124 [IRCD_PATH_OPERHELP
] = HPATH
,
125 [IRCD_PATH_IRCD_EXEC
] = SPATH
,
126 [IRCD_PATH_IRCD_CONF
] = CPATH
,
127 [IRCD_PATH_IRCD_MOTD
] = MPATH
,
128 [IRCD_PATH_IRCD_LOG
] = LPATH
,
129 [IRCD_PATH_IRCD_PID
] = PPATH
,
130 [IRCD_PATH_IRCD_OMOTD
] = OPATH
,
131 [IRCD_PATH_BANDB
] = DBPATH
,
132 [IRCD_PATH_BIN
] = BINPATH
,
133 [IRCD_PATH_LIBEXEC
] = PKGLIBEXECDIR
,
136 const char *logFileName
= NULL
;
137 const char *pidFileName
= NULL
;
140 ircd_shutdown(const char *reason
)
142 struct Client
*target_p
;
145 RB_DLINK_FOREACH(ptr
, lclient_list
.head
)
147 target_p
= ptr
->data
;
149 sendto_one(target_p
, ":%s NOTICE %s :Server Terminating. %s",
150 me
.name
, target_p
->name
, reason
);
153 RB_DLINK_FOREACH(ptr
, serv_list
.head
)
155 target_p
= ptr
->data
;
157 sendto_one(target_p
, ":%s ERROR :Terminated by %s",
161 ilog(L_MAIN
, "Server Terminating. %s", reason
);
169 * print_startup - print startup information
172 print_startup(int pid
)
178 fd
= open("/dev/null", O_RDWR
);
180 perror("open /dev/null");
188 inotice("runtime path: %s", rb_path_to_self());
189 inotice("now running in %s mode from %s as pid %d ...",
190 !server_state_foreground
? "background" : "foreground",
191 ConfigFileEntry
.dpath
, pid
);
194 /* let the parent process know the initialization was successful
196 if (!server_state_foreground
)
198 /* GCC complains on Linux if we don't check the value of write pedantically.
199 * Technically you're supposed to check the value, yes, but it probably can't fail.
200 * No, casting to void is of no use to shut the warning up. You HAVE to use the value.
203 if(write(0, ".", 1) < 1)
206 if (dup2(1, 0) == -1)
208 if (dup2(1, 2) == -1)
216 * inputs - boot_daemon flag
218 * side effects - if boot_daemon flag is not set, don't daemonize
223 #if !defined(_WIN32) && defined(RLIMIT_NOFILE) && defined(HAVE_SYS_RESOURCE_H)
226 if(!getrlimit(RLIMIT_NOFILE
, &limit
))
228 maxconnections
= limit
.rlim_cur
;
229 if(maxconnections
<= MAX_BUFFER
)
231 fprintf(stderr
, "ERROR: Shell FD limits are too low.\n");
232 fprintf(stderr
, "ERROR: charybdis reserves %d FDs, shell limits must be above this\n", MAX_BUFFER
);
237 #endif /* RLIMIT_FD_MAX */
238 maxconnections
= MAXCONNECTIONS
;
256 if((pid
= fork()) < 0)
264 /* Wait for initialization to finish, successfully or
265 * unsuccessfully. Until this point the child may still
266 * write to stdout/stderr.
268 if (read(pip
[0], &c
, 1) > 0)
283 static int printVersion
= 0;
285 struct lgetopt myopts
[] = {
286 {"configfile", &ConfigFileEntry
.configfile
,
287 STRING
, "File to use for ircd.conf"},
288 {"logfile", &logFileName
,
289 STRING
, "File to use for ircd.log"},
290 {"pidfile", &pidFileName
,
291 STRING
, "File to use for process ID"},
292 {"foreground", &server_state_foreground
,
293 YESNO
, "Run in foreground (don't detach)"},
294 {"version", &printVersion
,
295 YESNO
, "Print version and exit"},
296 {"conftest", &testing_conf
,
297 YESNO
, "Test the configuration files and exit"},
298 {"help", NULL
, USAGE
, "Print this text"},
299 {NULL
, NULL
, STRING
, NULL
},
303 check_rehash(void *unused
)
306 * Check to see whether we have to rehash the configuration ..
317 dorehashbans
= false;
322 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
323 "Got signal SIGUSR1, reloading ircd motd file");
330 * initalialize_global_set_options
334 * side effects - This sets all global set options needed
337 initialize_global_set_options(void)
339 memset(&GlobalSetOptions
, 0, sizeof(GlobalSetOptions
));
340 /* memset( &ConfigFileEntry, 0, sizeof(ConfigFileEntry)); */
342 GlobalSetOptions
.maxclients
= ServerInfo
.default_max_clients
;
344 if(GlobalSetOptions
.maxclients
> (maxconnections
- MAX_BUFFER
) || (GlobalSetOptions
.maxclients
<= 0))
345 GlobalSetOptions
.maxclients
= maxconnections
- MAX_BUFFER
;
347 GlobalSetOptions
.autoconn
= 1;
349 GlobalSetOptions
.spam_time
= MIN_JOIN_LEAVE_TIME
;
350 GlobalSetOptions
.spam_num
= MAX_JOIN_LEAVE_COUNT
;
352 GlobalSetOptions
.floodcount
= ConfigFileEntry
.default_floodcount
;
354 split_servers
= ConfigChannel
.default_split_server_count
;
355 split_users
= ConfigChannel
.default_split_user_count
;
357 if(split_users
&& split_servers
358 && (ConfigChannel
.no_create_on_split
|| ConfigChannel
.no_join_on_split
))
364 GlobalSetOptions
.ident_timeout
= ConfigFileEntry
.default_ident_timeout
;
366 rb_strlcpy(GlobalSetOptions
.operstring
,
367 ConfigFileEntry
.default_operstring
,
368 sizeof(GlobalSetOptions
.operstring
));
369 rb_strlcpy(GlobalSetOptions
.adminstring
,
370 ConfigFileEntry
.default_adminstring
,
371 sizeof(GlobalSetOptions
.adminstring
));
373 /* memset( &ConfigChannel, 0, sizeof(ConfigChannel)); */
375 /* End of global set options */
380 * initialize_server_capabs
386 initialize_server_capabs(void)
388 default_server_capabs
&= ~CAP_ZIP
;
396 * side effects - items in ircd_paths[] array are relocated
402 char prefix
[PATH_MAX
], workbuf
[PATH_MAX
];
405 rb_strlcpy(prefix
, rb_path_to_self(), sizeof prefix
);
407 ircd_paths
[IRCD_PATH_IRCD_EXEC
] = rb_strdup(prefix
);
409 /* if we're running from inside the source tree, we probably do not want to relocate any other paths */
410 if (strstr(prefix
, ".libs") != NULL
)
413 /* prefix = /home/kaniini/ircd/bin/ircd */
414 p
= strrchr(prefix
, RB_PATH_SEPARATOR
);
415 if (rb_unlikely(p
== NULL
))
419 /* prefix = /home/kaniini/ircd/bin */
420 p
= strrchr(prefix
, RB_PATH_SEPARATOR
);
421 if (rb_unlikely(p
== NULL
))
425 /* prefix = /home/kaniini/ircd */
426 ircd_paths
[IRCD_PATH_PREFIX
] = rb_strdup(prefix
);
428 /* now that we have our prefix, we can relocate the other paths... */
429 snprintf(workbuf
, sizeof workbuf
, "%s%cmodules", prefix
, RB_PATH_SEPARATOR
);
430 ircd_paths
[IRCD_PATH_MODULES
] = rb_strdup(workbuf
);
432 snprintf(workbuf
, sizeof workbuf
, "%s%cmodules%cautoload", prefix
, RB_PATH_SEPARATOR
, RB_PATH_SEPARATOR
);
433 ircd_paths
[IRCD_PATH_AUTOLOAD_MODULES
] = rb_strdup(workbuf
);
435 snprintf(workbuf
, sizeof workbuf
, "%s%cetc", prefix
, RB_PATH_SEPARATOR
);
436 ircd_paths
[IRCD_PATH_ETC
] = rb_strdup(workbuf
);
438 snprintf(workbuf
, sizeof workbuf
, "%s%clog", prefix
, RB_PATH_SEPARATOR
);
439 ircd_paths
[IRCD_PATH_LOG
] = rb_strdup(workbuf
);
441 snprintf(workbuf
, sizeof workbuf
, "%s%chelp%cusers", prefix
, RB_PATH_SEPARATOR
, RB_PATH_SEPARATOR
);
442 ircd_paths
[IRCD_PATH_USERHELP
] = rb_strdup(workbuf
);
444 snprintf(workbuf
, sizeof workbuf
, "%s%chelp%copers", prefix
, RB_PATH_SEPARATOR
, RB_PATH_SEPARATOR
);
445 ircd_paths
[IRCD_PATH_OPERHELP
] = rb_strdup(workbuf
);
447 snprintf(workbuf
, sizeof workbuf
, "%s%cetc%circd.conf", prefix
, RB_PATH_SEPARATOR
, RB_PATH_SEPARATOR
);
448 ircd_paths
[IRCD_PATH_IRCD_CONF
] = rb_strdup(workbuf
);
450 snprintf(workbuf
, sizeof workbuf
, "%s%cetc%circd.motd", prefix
, RB_PATH_SEPARATOR
, RB_PATH_SEPARATOR
);
451 ircd_paths
[IRCD_PATH_IRCD_MOTD
] = rb_strdup(workbuf
);
453 snprintf(workbuf
, sizeof workbuf
, "%s%cetc%copers.motd", prefix
, RB_PATH_SEPARATOR
, RB_PATH_SEPARATOR
);
454 ircd_paths
[IRCD_PATH_IRCD_OMOTD
] = rb_strdup(workbuf
);
456 snprintf(workbuf
, sizeof workbuf
, "%s%cetc%cban.db", prefix
, RB_PATH_SEPARATOR
, RB_PATH_SEPARATOR
);
457 ircd_paths
[IRCD_PATH_BANDB
] = rb_strdup(workbuf
);
459 snprintf(workbuf
, sizeof workbuf
, "%s%cetc%circd.pid", prefix
, RB_PATH_SEPARATOR
, RB_PATH_SEPARATOR
);
460 ircd_paths
[IRCD_PATH_IRCD_PID
] = rb_strdup(workbuf
);
462 snprintf(workbuf
, sizeof workbuf
, "%s%clogs%circd.log", prefix
, RB_PATH_SEPARATOR
, RB_PATH_SEPARATOR
);
463 ircd_paths
[IRCD_PATH_IRCD_LOG
] = rb_strdup(workbuf
);
465 snprintf(workbuf
, sizeof workbuf
, "%s%cbin", prefix
, RB_PATH_SEPARATOR
);
466 ircd_paths
[IRCD_PATH_BIN
] = rb_strdup(workbuf
);
467 ircd_paths
[IRCD_PATH_LIBEXEC
] = rb_strdup(workbuf
);
474 * inputs - filename+path of pid file
476 * side effects - write the pid of the ircd to filename
479 write_pidfile(const char *filename
)
483 if((fb
= fopen(filename
, "w")))
485 unsigned int pid
= (unsigned int) getpid();
487 snprintf(buff
, sizeof(buff
), "%u\n", pid
);
488 if((fputs(buff
, fb
) == -1))
490 ilog(L_MAIN
, "Error writing %u to pid file %s (%s)",
491 pid
, filename
, strerror(errno
));
498 ilog(L_MAIN
, "Error opening pid file %s", filename
);
505 * inputs - filename+path of pid file
507 * side effects - reads pid from pidfile and checks if ircd is in process
508 * list. if it is, gracefully exits
512 check_pidfile(const char *filename
)
518 /* Don't do logging here, since we don't have log() initialised */
519 if((fb
= fopen(filename
, "r")))
521 if(fgets(buff
, 20, fb
) != NULL
)
523 pidfromfile
= atoi(buff
);
524 if(!rb_kill(pidfromfile
, 0))
526 printf("ircd: daemon is already running\n");
539 * side effects - setups corefile to system limits.
545 #ifdef HAVE_SYS_RESOURCE_H
546 struct rlimit rlim
; /* resource limits */
548 /* Set corefilesize to maximum */
549 if(!getrlimit(RLIMIT_CORE
, &rlim
))
551 rlim
.rlim_cur
= rlim
.rlim_max
;
552 setrlimit(RLIMIT_CORE
, &rlim
);
558 ircd_log_cb(const char *str
)
560 ilog(L_MAIN
, "librb reports: %s", str
);
564 ircd_restart_cb(const char *str
)
566 inotice("librb has called the restart callback: %s", str
);
571 * Why EXIT_FAILURE here?
572 * Because if ircd_die_cb() is called it's because of a fatal
573 * error inside libcharybdis, and we don't know how to handle the
574 * exception, so it is logical to return a FAILURE exit code here.
578 ircd_die_cb(const char *str
)
582 /* Try to get the message out to currently logged in operators. */
583 sendto_realops_snomask(SNO_GENERAL
, L_NETWIDE
, "librb has called the die callback..aborting: %s", str
);
584 inotice("librb has called the die callback..aborting: %s", str
);
587 inotice("librb has called the die callback..aborting");
593 struct ev_entry
*check_splitmode_ev
= NULL
;
596 seed_with_urandom(void)
601 fd
= open("/dev/urandom", O_RDONLY
);
604 if(read(fd
, &seed
, sizeof(seed
)) == sizeof(seed
))
616 seed_with_clock(void)
618 const struct timeval
*tv
;
620 tv
= rb_current_time_tv();
621 srand(tv
->tv_sec
^ (tv
->tv_usec
| (getpid() << 20)));
625 seed_random(void *unused
)
628 if(rb_get_random(&seed
, sizeof(seed
)) == -1)
630 if(!seed_with_urandom())
640 * Initializes the IRCd.
642 * Inputs - number of commandline args, args themselves
644 * Side Effects - this is where the ircd gets going right now
647 charybdis_main(int argc
, char * const argv
[])
652 /* Check to see if the user is running us as root, which is a nono */
655 fprintf(stderr
, "Don't run ircd as root!!!\n");
664 logFileName
= ircd_paths
[IRCD_PATH_IRCD_LOG
];
665 pidFileName
= ircd_paths
[IRCD_PATH_IRCD_PID
];
667 ConfigFileEntry
.dpath
= ircd_paths
[IRCD_PATH_PREFIX
];
668 ConfigFileEntry
.configfile
= ircd_paths
[IRCD_PATH_IRCD_CONF
]; /* Server configuration file */
669 ConfigFileEntry
.connect_timeout
= 30; /* Default to 30 */
673 umask(077); /* better safe than sorry --SRB */
676 parseargs(&argc
, &argv
, myopts
);
678 if(chdir(ConfigFileEntry
.dpath
))
680 fprintf(stderr
, "Unable to chdir to %s: %s\n", ConfigFileEntry
.dpath
, strerror(errno
));
687 * Setup corefile size immediately after boot -kre
691 /* initialise operhash fairly early. */
694 memset(&me
, 0, sizeof(me
));
695 memset(&meLocalUser
, 0, sizeof(meLocalUser
));
696 me
.localClient
= &meLocalUser
;
698 /* Make sure all lists are zeroed */
699 memset(&unknown_list
, 0, sizeof(unknown_list
));
700 memset(&lclient_list
, 0, sizeof(lclient_list
));
701 memset(&serv_list
, 0, sizeof(serv_list
));
702 memset(&global_serv_list
, 0, sizeof(global_serv_list
));
703 memset(&local_oper_list
, 0, sizeof(local_oper_list
));
704 memset(&oper_list
, 0, sizeof(oper_list
));
706 rb_dlinkAddTail(&me
, &me
.node
, &global_client_list
);
708 memset(&Count
, 0, sizeof(Count
));
709 memset(&ServerInfo
, 0, sizeof(ServerInfo
));
710 memset(&AdminInfo
, 0, sizeof(AdminInfo
));
711 memset(&ServerStats
, 0, sizeof(struct ServerStatistics
));
715 printf("ircd: version %s(%s)\n", ircd_version
, serno
);
716 #ifdef CUSTOM_BRANDING
717 printf("ircd: based on %s-%s\n", PACKAGE_NAME
, PACKAGE_VERSION
);
719 printf("ircd: %s\n", rb_lib_version());
726 server_state_foreground
= true;
729 /* Make sure fd 0, 1 and 2 are in use -- jilles */
732 fd
= open("/dev/null", O_RDWR
);
733 } while (fd
< 2 && fd
!= -1);
740 /* Check if there is pidfile and daemon already running */
743 check_pidfile(pidFileName
);
745 if(!server_state_foreground
)
747 inotice("starting %s ...", ircd_version
);
748 inotice("%s", rb_lib_version());
751 /* Init the event subsystem */
752 rb_lib_init(ircd_log_cb
, ircd_restart_cb
, ircd_die_cb
, !server_state_foreground
, maxconnections
, DNODE_HEAP_SIZE
, FD_HEAP_SIZE
);
753 rb_linebuf_init(LINEBUF_HEAP_SIZE
);
755 rb_init_prng(NULL
, RB_PRNG_DEFAULT
);
759 init_builtin_capabs();
760 default_server_capabs
= CAP_MASK
;
767 clear_scache_hash_table(); /* server cache name table */
779 construct_cflags_strings();
781 init_authd(); /* Start up authd. */
782 init_dns(); /* Start up DNS query system */
784 privilegeset_set_new("default", "", 0);
787 fprintf(stderr
, "\nBeginning config test\n");
788 read_conf_files(true); /* cold start init conf files */
790 mod_add_path(MODULE_DIR
);
791 mod_add_path(MODULE_DIR
"/autoload");
793 load_core_modules(1);
803 initialize_server_capabs(); /* Set up default_server_capabs */
804 initialize_global_set_options();
806 if(ServerInfo
.name
== NULL
)
808 ierror("no server name specified in serverinfo block.");
811 rb_strlcpy(me
.name
, ServerInfo
.name
, sizeof(me
.name
));
813 if(ServerInfo
.sid
[0] == '\0')
815 ierror("no server sid specified in serverinfo block.");
818 rb_strlcpy(me
.id
, ServerInfo
.sid
, sizeof(me
.id
));
821 /* serverinfo{} description must exist. If not, error out. */
822 if(ServerInfo
.description
== NULL
)
824 ierror("no server description specified in serverinfo block.");
827 rb_strlcpy(me
.info
, ServerInfo
.description
, sizeof(me
.info
));
829 if(ServerInfo
.ssl_cert
!= NULL
)
831 /* just do the rb_setup_ssl_server to validate the config */
832 if(!rb_setup_ssl_server(ServerInfo
.ssl_cert
, ServerInfo
.ssl_private_key
, ServerInfo
.ssl_dh_params
, ServerInfo
.ssl_cipher_list
))
834 ilog(L_MAIN
, "WARNING: Unable to setup SSL.");
843 fprintf(stderr
, "\nConfig testing complete.\n");
845 return 0; /* Why? We want the launcher to exit out. */
852 startup_time
= rb_current_time();
853 add_to_client_hash(me
.name
, &me
);
854 add_to_id_hash(me
.id
, &me
);
855 me
.serv
->nameinfo
= scache_connect(me
.name
, me
.info
, 0);
857 rb_dlinkAddAlloc(&me
, &global_serv_list
);
859 construct_umodebuf();
862 write_pidfile(pidFileName
);
868 ilog(L_MAIN
, "Server Ready");
870 /* We want try_connections to be called as soon as possible now! -- adrian */
871 /* No, 'cause after a restart it would cause all sorts of nick collides */
872 /* um. by waiting even longer, that just means we have even *more*
873 * nick collisions. what a stupid idea. set an event for the IO loop --fl
875 rb_event_addish("try_connections", try_connections
, NULL
, STARTUP_CONNECTIONS_TIME
);
876 rb_event_addonce("try_connections_startup", try_connections
, NULL
, 2);
877 rb_event_add("check_rehash", check_rehash
, NULL
, 3);
878 rb_event_addish("reseed_srand", seed_random
, NULL
, 300); /* reseed every 10 minutes */
881 check_splitmode_ev
= rb_event_add("check_splitmode", check_splitmode
, NULL
, 5);
883 print_startup(getpid());