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"
57 #include "s_newconf.h"
60 #include "patchlevel.h"
65 #include "privilege.h"
70 /* /quote set variables */
71 struct SetOptions GlobalSetOptions
;
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
;
81 struct ServerStatistics ServerStats
;
84 struct Client me
; /* That's me */
85 struct LocalUser meLocalUser
; /* That's also part of me */
87 rb_dlink_list global_client_list
;
89 /* unknown/client pointer lists */
90 rb_dlink_list unknown_list
; /* unknown clients ON this server only */
91 rb_dlink_list lclient_list
; /* local clients only ON this server */
92 rb_dlink_list serv_list
; /* local servers to this server ONLY */
93 rb_dlink_list global_serv_list
; /* global servers on the network */
94 rb_dlink_list local_oper_list
; /* our opers, duplicated in lclient_list */
95 rb_dlink_list oper_list
; /* network opers */
98 volatile sig_atomic_t dorehash
= false;
99 volatile sig_atomic_t dorehashbans
= false;
100 volatile sig_atomic_t doremotd
= false;
101 bool kline_queued
= false;
102 bool server_state_foreground
= false;
103 bool opers_see_all_users
= false;
104 bool ircd_ssl_ok
= false;
105 bool ircd_zlib_ok
= true;
107 int testing_conf
= 0;
110 int default_server_capabs
;
118 const char *ircd_paths
[IRCD_PATH_COUNT
] = {
119 [IRCD_PATH_PREFIX
] = DPATH
,
120 [IRCD_PATH_MODULES
] = MODPATH
,
121 [IRCD_PATH_AUTOLOAD_MODULES
] = AUTOMODPATH
,
122 [IRCD_PATH_ETC
] = ETCPATH
,
123 [IRCD_PATH_LOG
] = LOGPATH
,
124 [IRCD_PATH_USERHELP
] = UHPATH
,
125 [IRCD_PATH_OPERHELP
] = HPATH
,
126 [IRCD_PATH_IRCD_EXEC
] = SPATH
,
127 [IRCD_PATH_IRCD_CONF
] = CPATH
,
128 [IRCD_PATH_IRCD_MOTD
] = MPATH
,
129 [IRCD_PATH_IRCD_LOG
] = LPATH
,
130 [IRCD_PATH_IRCD_PID
] = PPATH
,
131 [IRCD_PATH_IRCD_OMOTD
] = OPATH
,
132 [IRCD_PATH_BANDB
] = DBPATH
,
133 [IRCD_PATH_BIN
] = BINPATH
,
134 [IRCD_PATH_LIBEXEC
] = PKGLIBEXECDIR
,
137 const char *logFileName
= NULL
;
138 const char *pidFileName
= NULL
;
141 ircd_shutdown(const char *reason
)
143 struct Client
*target_p
;
146 RB_DLINK_FOREACH(ptr
, lclient_list
.head
)
148 target_p
= ptr
->data
;
150 sendto_one(target_p
, ":%s NOTICE %s :Server Terminating. %s",
151 me
.name
, target_p
->name
, reason
);
154 RB_DLINK_FOREACH(ptr
, serv_list
.head
)
156 target_p
= ptr
->data
;
158 sendto_one(target_p
, ":%s ERROR :Terminated by %s",
162 ilog(L_MAIN
, "Server Terminating. %s", reason
);
170 * print_startup - print startup information
173 print_startup(int pid
)
179 fd
= open("/dev/null", O_RDWR
);
181 perror("open /dev/null");
189 inotice("runtime path: %s", rb_path_to_self());
190 inotice("now running in %s mode from %s as pid %d ...",
191 !server_state_foreground
? "background" : "foreground",
192 ConfigFileEntry
.dpath
, pid
);
195 /* let the parent process know the initialization was successful
197 if (!server_state_foreground
)
199 /* GCC complains on Linux if we don't check the value of write pedantically.
200 * Technically you're supposed to check the value, yes, but it probably can't fail.
201 * No, casting to void is of no use to shut the warning up. You HAVE to use the value.
204 if(write(0, ".", 1) < 1)
207 if (dup2(1, 0) == -1)
209 if (dup2(1, 2) == -1)
217 * inputs - boot_daemon flag
219 * side effects - if boot_daemon flag is not set, don't daemonize
224 #if !defined(_WIN32) && defined(RLIMIT_NOFILE) && defined(HAVE_SYS_RESOURCE_H)
227 if(!getrlimit(RLIMIT_NOFILE
, &limit
))
229 maxconnections
= limit
.rlim_cur
;
230 if(maxconnections
<= MAX_BUFFER
)
232 fprintf(stderr
, "ERROR: Shell FD limits are too low.\n");
233 fprintf(stderr
, "ERROR: charybdis reserves %d FDs, shell limits must be above this\n", MAX_BUFFER
);
238 #endif /* RLIMIT_FD_MAX */
239 maxconnections
= MAXCONNECTIONS
;
257 if((pid
= fork()) < 0)
265 /* Wait for initialization to finish, successfully or
266 * unsuccessfully. Until this point the child may still
267 * write to stdout/stderr.
269 if (read(pip
[0], &c
, 1) > 0)
284 static int printVersion
= 0;
286 struct lgetopt myopts
[] = {
287 {"configfile", &ConfigFileEntry
.configfile
,
288 STRING
, "File to use for ircd.conf"},
289 {"logfile", &logFileName
,
290 STRING
, "File to use for ircd.log"},
291 {"pidfile", &pidFileName
,
292 STRING
, "File to use for process ID"},
293 {"foreground", &server_state_foreground
,
294 YESNO
, "Run in foreground (don't detach)"},
295 {"version", &printVersion
,
296 YESNO
, "Print version and exit"},
297 {"conftest", &testing_conf
,
298 YESNO
, "Test the configuration files and exit"},
299 {"help", NULL
, USAGE
, "Print this text"},
300 {NULL
, NULL
, STRING
, NULL
},
304 check_rehash(void *unused
)
307 * Check to see whether we have to rehash the configuration ..
318 dorehashbans
= false;
323 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
324 "Got signal SIGUSR1, reloading ircd motd file");
331 * initalialize_global_set_options
335 * side effects - This sets all global set options needed
338 initialize_global_set_options(void)
340 memset(&GlobalSetOptions
, 0, sizeof(GlobalSetOptions
));
341 /* memset( &ConfigFileEntry, 0, sizeof(ConfigFileEntry)); */
343 GlobalSetOptions
.maxclients
= ServerInfo
.default_max_clients
;
345 if(GlobalSetOptions
.maxclients
> (maxconnections
- MAX_BUFFER
) || (GlobalSetOptions
.maxclients
<= 0))
346 GlobalSetOptions
.maxclients
= maxconnections
- MAX_BUFFER
;
348 GlobalSetOptions
.autoconn
= 1;
350 GlobalSetOptions
.spam_time
= MIN_JOIN_LEAVE_TIME
;
351 GlobalSetOptions
.spam_num
= MAX_JOIN_LEAVE_COUNT
;
353 GlobalSetOptions
.floodcount
= ConfigFileEntry
.default_floodcount
;
355 split_servers
= ConfigChannel
.default_split_server_count
;
356 split_users
= ConfigChannel
.default_split_user_count
;
358 if(split_users
&& split_servers
359 && (ConfigChannel
.no_create_on_split
|| ConfigChannel
.no_join_on_split
))
365 GlobalSetOptions
.ident_timeout
= ConfigFileEntry
.default_ident_timeout
;
367 rb_strlcpy(GlobalSetOptions
.operstring
,
368 ConfigFileEntry
.default_operstring
,
369 sizeof(GlobalSetOptions
.operstring
));
370 rb_strlcpy(GlobalSetOptions
.adminstring
,
371 ConfigFileEntry
.default_adminstring
,
372 sizeof(GlobalSetOptions
.adminstring
));
374 /* memset( &ConfigChannel, 0, sizeof(ConfigChannel)); */
376 /* End of global set options */
381 * initialize_server_capabs
387 initialize_server_capabs(void)
389 default_server_capabs
&= ~CAP_ZIP
;
397 * side effects - items in ircd_paths[] array are relocated
403 char prefix
[PATH_MAX
], workbuf
[PATH_MAX
];
406 rb_strlcpy(prefix
, rb_path_to_self(), sizeof prefix
);
408 ircd_paths
[IRCD_PATH_IRCD_EXEC
] = rb_strdup(prefix
);
410 /* if we're running from inside the source tree, we probably do not want to relocate any other paths */
411 if (strstr(prefix
, ".libs") != NULL
)
414 /* prefix = /home/kaniini/ircd/bin/ircd */
415 p
= strrchr(prefix
, RB_PATH_SEPARATOR
);
416 if (rb_unlikely(p
== NULL
))
420 /* prefix = /home/kaniini/ircd/bin */
421 p
= strrchr(prefix
, RB_PATH_SEPARATOR
);
422 if (rb_unlikely(p
== NULL
))
426 /* prefix = /home/kaniini/ircd */
427 ircd_paths
[IRCD_PATH_PREFIX
] = rb_strdup(prefix
);
429 /* now that we have our prefix, we can relocate the other paths... */
430 snprintf(workbuf
, sizeof workbuf
, "%s%cmodules", prefix
, RB_PATH_SEPARATOR
);
431 ircd_paths
[IRCD_PATH_MODULES
] = rb_strdup(workbuf
);
433 snprintf(workbuf
, sizeof workbuf
, "%s%cmodules%cautoload", prefix
, RB_PATH_SEPARATOR
, RB_PATH_SEPARATOR
);
434 ircd_paths
[IRCD_PATH_AUTOLOAD_MODULES
] = rb_strdup(workbuf
);
436 snprintf(workbuf
, sizeof workbuf
, "%s%cetc", prefix
, RB_PATH_SEPARATOR
);
437 ircd_paths
[IRCD_PATH_ETC
] = rb_strdup(workbuf
);
439 snprintf(workbuf
, sizeof workbuf
, "%s%clog", prefix
, RB_PATH_SEPARATOR
);
440 ircd_paths
[IRCD_PATH_LOG
] = rb_strdup(workbuf
);
442 snprintf(workbuf
, sizeof workbuf
, "%s%chelp%cusers", prefix
, RB_PATH_SEPARATOR
, RB_PATH_SEPARATOR
);
443 ircd_paths
[IRCD_PATH_USERHELP
] = rb_strdup(workbuf
);
445 snprintf(workbuf
, sizeof workbuf
, "%s%chelp%copers", prefix
, RB_PATH_SEPARATOR
, RB_PATH_SEPARATOR
);
446 ircd_paths
[IRCD_PATH_OPERHELP
] = rb_strdup(workbuf
);
448 snprintf(workbuf
, sizeof workbuf
, "%s%cetc%circd.conf", prefix
, RB_PATH_SEPARATOR
, RB_PATH_SEPARATOR
);
449 ircd_paths
[IRCD_PATH_IRCD_CONF
] = rb_strdup(workbuf
);
451 snprintf(workbuf
, sizeof workbuf
, "%s%cetc%circd.motd", prefix
, RB_PATH_SEPARATOR
, RB_PATH_SEPARATOR
);
452 ircd_paths
[IRCD_PATH_IRCD_MOTD
] = rb_strdup(workbuf
);
454 snprintf(workbuf
, sizeof workbuf
, "%s%cetc%copers.motd", prefix
, RB_PATH_SEPARATOR
, RB_PATH_SEPARATOR
);
455 ircd_paths
[IRCD_PATH_IRCD_OMOTD
] = rb_strdup(workbuf
);
457 snprintf(workbuf
, sizeof workbuf
, "%s%cetc%cban.db", prefix
, RB_PATH_SEPARATOR
, RB_PATH_SEPARATOR
);
458 ircd_paths
[IRCD_PATH_BANDB
] = rb_strdup(workbuf
);
460 snprintf(workbuf
, sizeof workbuf
, "%s%cetc%circd.pid", prefix
, RB_PATH_SEPARATOR
, RB_PATH_SEPARATOR
);
461 ircd_paths
[IRCD_PATH_IRCD_PID
] = rb_strdup(workbuf
);
463 snprintf(workbuf
, sizeof workbuf
, "%s%clogs%circd.log", prefix
, RB_PATH_SEPARATOR
, RB_PATH_SEPARATOR
);
464 ircd_paths
[IRCD_PATH_IRCD_LOG
] = rb_strdup(workbuf
);
466 snprintf(workbuf
, sizeof workbuf
, "%s%cbin", prefix
, RB_PATH_SEPARATOR
);
467 ircd_paths
[IRCD_PATH_BIN
] = rb_strdup(workbuf
);
468 ircd_paths
[IRCD_PATH_LIBEXEC
] = rb_strdup(workbuf
);
475 * inputs - filename+path of pid file
477 * side effects - write the pid of the ircd to filename
480 write_pidfile(const char *filename
)
484 if((fb
= fopen(filename
, "w")))
486 unsigned int pid
= (unsigned int) getpid();
488 snprintf(buff
, sizeof(buff
), "%u\n", pid
);
489 if((fputs(buff
, fb
) == -1))
491 ilog(L_MAIN
, "Error writing %u to pid file %s (%s)",
492 pid
, filename
, strerror(errno
));
499 ilog(L_MAIN
, "Error opening pid file %s", filename
);
506 * inputs - filename+path of pid file
508 * side effects - reads pid from pidfile and checks if ircd is in process
509 * list. if it is, gracefully exits
513 check_pidfile(const char *filename
)
519 /* Don't do logging here, since we don't have log() initialised */
520 if((fb
= fopen(filename
, "r")))
522 if(fgets(buff
, 20, fb
) != NULL
)
524 pidfromfile
= atoi(buff
);
525 if(!rb_kill(pidfromfile
, 0))
527 printf("ircd: daemon is already running\n");
540 * side effects - setups corefile to system limits.
546 #ifdef HAVE_SYS_RESOURCE_H
547 struct rlimit rlim
; /* resource limits */
549 /* Set corefilesize to maximum */
550 if(!getrlimit(RLIMIT_CORE
, &rlim
))
552 rlim
.rlim_cur
= rlim
.rlim_max
;
553 setrlimit(RLIMIT_CORE
, &rlim
);
559 ircd_log_cb(const char *str
)
561 ilog(L_MAIN
, "librb reports: %s", str
);
565 ircd_restart_cb(const char *str
)
567 inotice("librb has called the restart callback: %s", str
);
572 * Why EXIT_FAILURE here?
573 * Because if ircd_die_cb() is called it's because of a fatal
574 * error inside libcharybdis, and we don't know how to handle the
575 * exception, so it is logical to return a FAILURE exit code here.
579 ircd_die_cb(const char *str
)
583 /* Try to get the message out to currently logged in operators. */
584 sendto_realops_snomask(SNO_GENERAL
, L_NETWIDE
, "librb has called the die callback..aborting: %s", str
);
585 inotice("librb has called the die callback..aborting: %s", str
);
588 inotice("librb has called the die callback..aborting");
594 struct ev_entry
*check_splitmode_ev
= NULL
;
597 seed_with_urandom(void)
602 fd
= open("/dev/urandom", O_RDONLY
);
605 if(read(fd
, &seed
, sizeof(seed
)) == sizeof(seed
))
617 seed_with_clock(void)
619 const struct timeval
*tv
;
621 tv
= rb_current_time_tv();
622 srand(tv
->tv_sec
^ (tv
->tv_usec
| (getpid() << 20)));
626 seed_random(void *unused
)
629 if(rb_get_random(&seed
, sizeof(seed
)) == -1)
631 if(!seed_with_urandom())
641 * Initializes the IRCd.
643 * Inputs - number of commandline args, args themselves
645 * Side Effects - this is where the ircd gets going right now
648 charybdis_main(int argc
, char *argv
[])
653 /* Check to see if the user is running us as root, which is a nono */
656 fprintf(stderr
, "Don't run ircd as root!!!\n");
665 logFileName
= ircd_paths
[IRCD_PATH_IRCD_LOG
];
666 pidFileName
= ircd_paths
[IRCD_PATH_IRCD_PID
];
668 ConfigFileEntry
.dpath
= ircd_paths
[IRCD_PATH_PREFIX
];
669 ConfigFileEntry
.configfile
= ircd_paths
[IRCD_PATH_IRCD_CONF
]; /* Server configuration file */
670 ConfigFileEntry
.connect_timeout
= 30; /* Default to 30 */
674 umask(077); /* better safe than sorry --SRB */
677 parseargs(&argc
, &argv
, myopts
);
679 if(chdir(ConfigFileEntry
.dpath
))
681 fprintf(stderr
, "Unable to chdir to %s: %s\n", ConfigFileEntry
.dpath
, strerror(errno
));
688 * Setup corefile size immediately after boot -kre
692 /* initialise operhash fairly early. */
695 memset(&me
, 0, sizeof(me
));
696 memset(&meLocalUser
, 0, sizeof(meLocalUser
));
697 me
.localClient
= &meLocalUser
;
699 /* Make sure all lists are zeroed */
700 memset(&unknown_list
, 0, sizeof(unknown_list
));
701 memset(&lclient_list
, 0, sizeof(lclient_list
));
702 memset(&serv_list
, 0, sizeof(serv_list
));
703 memset(&global_serv_list
, 0, sizeof(global_serv_list
));
704 memset(&local_oper_list
, 0, sizeof(local_oper_list
));
705 memset(&oper_list
, 0, sizeof(oper_list
));
707 rb_dlinkAddTail(&me
, &me
.node
, &global_client_list
);
709 memset(&Count
, 0, sizeof(Count
));
710 memset(&ServerInfo
, 0, sizeof(ServerInfo
));
711 memset(&AdminInfo
, 0, sizeof(AdminInfo
));
712 memset(&ServerStats
, 0, sizeof(struct ServerStatistics
));
716 printf("ircd: version %s(%s)\n", ircd_version
, serno
);
717 #ifdef CUSTOM_BRANDING
718 printf("ircd: based on %s-%s\n", PACKAGE_NAME
, PACKAGE_VERSION
);
720 printf("ircd: %s\n", rb_lib_version());
727 server_state_foreground
= true;
730 /* Make sure fd 0, 1 and 2 are in use -- jilles */
733 fd
= open("/dev/null", O_RDWR
);
734 } while (fd
< 2 && fd
!= -1);
741 /* Check if there is pidfile and daemon already running */
744 check_pidfile(pidFileName
);
746 if(!server_state_foreground
)
748 inotice("starting %s ...", ircd_version
);
749 inotice("%s", rb_lib_version());
752 /* Init the event subsystem */
753 rb_lib_init(ircd_log_cb
, ircd_restart_cb
, ircd_die_cb
, !server_state_foreground
, maxconnections
, DNODE_HEAP_SIZE
, FD_HEAP_SIZE
);
754 rb_linebuf_init(LINEBUF_HEAP_SIZE
);
756 rb_init_prng(NULL
, RB_PRNG_DEFAULT
);
760 init_builtin_capabs();
761 default_server_capabs
= CAP_MASK
;
768 clear_scache_hash_table(); /* server cache name table */
780 construct_cflags_strings();
783 load_core_modules(1);
785 init_authd(); /* Start up authd. */
786 init_dns(); /* Start up DNS query system */
788 privilegeset_set_new("default", "", 0);
791 fprintf(stderr
, "\nBeginning config test\n");
792 read_conf_files(true); /* cold start init conf files */
794 mod_add_path(MODULE_DIR
);
795 mod_add_path(MODULE_DIR
"/autoload");
805 initialize_server_capabs(); /* Set up default_server_capabs */
806 initialize_global_set_options();
808 if(ServerInfo
.name
== NULL
)
810 ierror("no server name specified in serverinfo block.");
813 rb_strlcpy(me
.name
, ServerInfo
.name
, sizeof(me
.name
));
815 if(ServerInfo
.sid
[0] == '\0')
817 ierror("no server sid specified in serverinfo block.");
820 strcpy(me
.id
, ServerInfo
.sid
);
823 /* serverinfo{} description must exist. If not, error out. */
824 if(ServerInfo
.description
== NULL
)
826 ierror("no server description specified in serverinfo block.");
829 rb_strlcpy(me
.info
, ServerInfo
.description
, sizeof(me
.info
));
831 if(ServerInfo
.ssl_cert
!= NULL
&& ServerInfo
.ssl_private_key
!= NULL
)
833 /* just do the rb_setup_ssl_server to validate the config */
834 if(!rb_setup_ssl_server(ServerInfo
.ssl_cert
, ServerInfo
.ssl_private_key
, ServerInfo
.ssl_dh_params
, ServerInfo
.ssl_cipher_list
))
836 ilog(L_MAIN
, "WARNING: Unable to setup SSL.");
845 fprintf(stderr
, "\nConfig testing complete.\n");
847 return 0; /* Why? We want the launcher to exit out. */
854 startup_time
= rb_current_time();
855 add_to_client_hash(me
.name
, &me
);
856 add_to_id_hash(me
.id
, &me
);
857 me
.serv
->nameinfo
= scache_connect(me
.name
, me
.info
, 0);
859 rb_dlinkAddAlloc(&me
, &global_serv_list
);
861 construct_umodebuf();
864 write_pidfile(pidFileName
);
870 ilog(L_MAIN
, "Server Ready");
872 /* We want try_connections to be called as soon as possible now! -- adrian */
873 /* No, 'cause after a restart it would cause all sorts of nick collides */
874 /* um. by waiting even longer, that just means we have even *more*
875 * nick collisions. what a stupid idea. set an event for the IO loop --fl
877 rb_event_addish("try_connections", try_connections
, NULL
, STARTUP_CONNECTIONS_TIME
);
878 rb_event_addonce("try_connections_startup", try_connections
, NULL
, 2);
879 rb_event_add("check_rehash", check_rehash
, NULL
, 3);
880 rb_event_addish("reseed_srand", seed_random
, NULL
, 300); /* reseed every 10 minutes */
883 check_splitmode_ev
= rb_event_add("check_splitmode", check_splitmode
, NULL
, 5);
885 print_startup(getpid());