]>
Commit | Line | Data |
---|---|---|
212380e3 | 1 | /* |
6fce54ff | 2 | * charybdis: A slightly useful ircd. |
212380e3 AC |
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 | |
6fce54ff | 7 | * Copyright (C) 2002-2008 ircd-ratbox development team |
adef4da1 | 8 | * Copyright (C) 2005-2013 charybdis development team |
212380e3 AC |
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 | |
212380e3 AC |
24 | */ |
25 | ||
fe037171 | 26 | #include "rb_lib.h" |
212380e3 AC |
27 | #include "stdinc.h" |
28 | #include "setup.h" | |
9b8e9eb3 | 29 | #include "defaults.h" |
212380e3 AC |
30 | #include "ircd.h" |
31 | #include "channel.h" | |
32 | #include "class.h" | |
33 | #include "client.h" | |
212380e3 | 34 | #include "hash.h" |
4562c604 | 35 | #include "match.h" |
212380e3 | 36 | #include "ircd_signal.h" |
212380e3 AC |
37 | #include "msg.h" /* msgtab */ |
38 | #include "hostmask.h" | |
39 | #include "numeric.h" | |
40 | #include "parse.h" | |
212380e3 | 41 | #include "restart.h" |
212380e3 | 42 | #include "s_conf.h" |
4016731b | 43 | #include "logger.h" |
212380e3 AC |
44 | #include "s_serv.h" /* try_connections */ |
45 | #include "s_user.h" | |
46 | #include "s_stats.h" | |
47 | #include "scache.h" | |
48 | #include "send.h" | |
49 | #include "supported.h" | |
50 | #include "whowas.h" | |
51 | #include "modules.h" | |
212380e3 AC |
52 | #include "hook.h" |
53 | #include "ircd_getopt.h" | |
212380e3 | 54 | #include "newconf.h" |
212380e3 | 55 | #include "reject.h" |
212380e3 AC |
56 | #include "s_newconf.h" |
57 | #include "cache.h" | |
58 | #include "monitor.h" | |
212380e3 AC |
59 | #include "patchlevel.h" |
60 | #include "serno.h" | |
c6d72037 | 61 | #include "sslproc.h" |
c53ca1e0 | 62 | #include "wsproc.h" |
efccc22c | 63 | #include "chmode.h" |
39a68b53 | 64 | #include "privilege.h" |
6eebc373 | 65 | #include "bandbi.h" |
64fae260 | 66 | #include "authproc.h" |
b02a913b | 67 | #include "operhash.h" |
212380e3 | 68 | |
df3db5d9 AJ |
69 | static void |
70 | ircd_die_cb(const char *str) __attribute__((noreturn)); | |
71 | ||
8bd5767b | 72 | /* /quote set variables */ |
2af8c7ff | 73 | struct SetOptions GlobalSetOptions; |
8bd5767b JT |
74 | |
75 | /* configuration set from ircd.conf */ | |
76 | struct config_file_entry ConfigFileEntry; | |
77 | /* server info set from ircd.conf */ | |
78 | struct server_info ServerInfo; | |
79 | /* admin info set from ircd.conf */ | |
1aad9782 VY |
80 | struct admin_info AdminInfo; |
81 | ||
8bd5767b | 82 | struct Counter Count; |
47adde3d VY |
83 | struct ServerStatistics ServerStats; |
84 | ||
1aad9782 | 85 | int maxconnections; |
8bd5767b | 86 | struct Client me; /* That's me */ |
1aad9782 VY |
87 | struct LocalUser meLocalUser; /* That's also part of me */ |
88 | ||
3d3d1a95 | 89 | rb_dlink_list global_client_list; |
8bd5767b | 90 | |
3d3d1a95 | 91 | /* unknown/client pointer lists */ |
8bd5767b | 92 | rb_dlink_list unknown_list; /* unknown clients ON this server only */ |
3d3d1a95 | 93 | rb_dlink_list lclient_list; /* local clients only ON this server */ |
8bd5767b JT |
94 | rb_dlink_list serv_list; /* local servers to this server ONLY */ |
95 | rb_dlink_list global_serv_list; /* global servers on the network */ | |
96 | rb_dlink_list local_oper_list; /* our opers, duplicated in lclient_list */ | |
b5b84cad VY |
97 | rb_dlink_list oper_list; /* network opers */ |
98 | ||
5adde7a4 | 99 | char * const *myargv; |
33d43d4f MU |
100 | volatile sig_atomic_t dorehash = false; |
101 | volatile sig_atomic_t dorehashbans = false; | |
102 | volatile sig_atomic_t doremotd = false; | |
f66f0baa | 103 | bool kline_queued = false; |
503727d1 EM |
104 | bool server_state_foreground = false; |
105 | bool opers_see_all_users = false; | |
bfc44622 AC |
106 | bool ircd_ssl_ok = false; |
107 | bool ircd_zlib_ok = true; | |
8bd5767b JT |
108 | |
109 | int testing_conf = 0; | |
cd27d99c | 110 | time_t startup_time; |
8bd5767b | 111 | |
346fba92 | 112 | int default_server_capabs; |
8bd5767b | 113 | |
cd27d99c VY |
114 | int splitmode; |
115 | int splitchecking; | |
116 | int split_users; | |
117 | int split_servers; | |
118 | int eob_count; | |
c6d72037 | 119 | |
4d8cfacd AC |
120 | const char *ircd_paths[IRCD_PATH_COUNT] = { |
121 | [IRCD_PATH_PREFIX] = DPATH, | |
122 | [IRCD_PATH_MODULES] = MODPATH, | |
123 | [IRCD_PATH_AUTOLOAD_MODULES] = AUTOMODPATH, | |
124 | [IRCD_PATH_ETC] = ETCPATH, | |
125 | [IRCD_PATH_LOG] = LOGPATH, | |
126 | [IRCD_PATH_USERHELP] = UHPATH, | |
127 | [IRCD_PATH_OPERHELP] = HPATH, | |
128 | [IRCD_PATH_IRCD_EXEC] = SPATH, | |
129 | [IRCD_PATH_IRCD_CONF] = CPATH, | |
130 | [IRCD_PATH_IRCD_MOTD] = MPATH, | |
131 | [IRCD_PATH_IRCD_LOG] = LPATH, | |
132 | [IRCD_PATH_IRCD_PID] = PPATH, | |
133 | [IRCD_PATH_IRCD_OMOTD] = OPATH, | |
134 | [IRCD_PATH_BANDB] = DBPATH, | |
135 | [IRCD_PATH_BIN] = BINPATH, | |
136 | [IRCD_PATH_LIBEXEC] = PKGLIBEXECDIR, | |
137 | }; | |
138 | ||
e0e0c415 AC |
139 | const char *ircd_pathnames[IRCD_PATH_COUNT] = { |
140 | [IRCD_PATH_PREFIX] = "prefix", | |
141 | [IRCD_PATH_MODULES] = "modules", | |
142 | [IRCD_PATH_AUTOLOAD_MODULES] = "autoload modules", | |
143 | [IRCD_PATH_ETC] = "config", | |
144 | [IRCD_PATH_LOG] = "log", | |
145 | [IRCD_PATH_USERHELP] = "user help", | |
146 | [IRCD_PATH_OPERHELP] = "oper help", | |
147 | [IRCD_PATH_IRCD_EXEC] = "ircd binary", | |
148 | [IRCD_PATH_IRCD_CONF] = "ircd.conf", | |
149 | [IRCD_PATH_IRCD_MOTD] = "ircd.motd", | |
150 | [IRCD_PATH_IRCD_LOG] = "ircd.log", | |
151 | [IRCD_PATH_IRCD_PID] = "ircd.pid", | |
152 | [IRCD_PATH_IRCD_OMOTD] = "oper motd", | |
153 | [IRCD_PATH_BANDB] = "bandb", | |
154 | [IRCD_PATH_BIN] = "binary dir", | |
155 | [IRCD_PATH_LIBEXEC] = "libexec dir", | |
156 | }; | |
157 | ||
2f545aad AC |
158 | const char *logFileName = NULL; |
159 | const char *pidFileName = NULL; | |
4d8cfacd | 160 | |
fd5af3d0 VY |
161 | void |
162 | ircd_shutdown(const char *reason) | |
163 | { | |
164 | struct Client *target_p; | |
165 | rb_dlink_node *ptr; | |
166 | ||
167 | RB_DLINK_FOREACH(ptr, lclient_list.head) | |
168 | { | |
169 | target_p = ptr->data; | |
170 | ||
171 | sendto_one(target_p, ":%s NOTICE %s :Server Terminating. %s", | |
172 | me.name, target_p->name, reason); | |
173 | } | |
174 | ||
175 | RB_DLINK_FOREACH(ptr, serv_list.head) | |
176 | { | |
177 | target_p = ptr->data; | |
178 | ||
179 | sendto_one(target_p, ":%s ERROR :Terminated by %s", | |
180 | me.name, reason); | |
181 | } | |
182 | ||
183 | ilog(L_MAIN, "Server Terminating. %s", reason); | |
ea82a3ca | 184 | close_logfiles(); |
fd5af3d0 VY |
185 | |
186 | unlink(pidFileName); | |
187 | exit(0); | |
188 | } | |
189 | ||
212380e3 AC |
190 | /* |
191 | * init_sys | |
192 | * | |
193 | * inputs - boot_daemon flag | |
194 | * output - none | |
195 | * side effects - if boot_daemon flag is not set, don't daemonize | |
196 | */ | |
197 | static void | |
198 | init_sys(void) | |
199 | { | |
c1bfea73 | 200 | #if !defined(_WIN32) && defined(RLIMIT_NOFILE) && defined(HAVE_SYS_RESOURCE_H) |
8bd5767b JT |
201 | struct rlimit limit; |
202 | ||
203 | if(!getrlimit(RLIMIT_NOFILE, &limit)) | |
204 | { | |
205 | maxconnections = limit.rlim_cur; | |
206 | if(maxconnections <= MAX_BUFFER) | |
207 | { | |
208 | fprintf(stderr, "ERROR: Shell FD limits are too low.\n"); | |
ec86417c | 209 | fprintf(stderr, "ERROR: charybdis reserves %d FDs, shell limits must be above this\n", MAX_BUFFER); |
8bd5767b JT |
210 | exit(EXIT_FAILURE); |
211 | } | |
212 | return; | |
213 | } | |
214 | #endif /* RLIMIT_FD_MAX */ | |
101db4c4 | 215 | maxconnections = MAXCONNECTIONS; |
212380e3 AC |
216 | } |
217 | ||
0c433865 | 218 | #ifndef _WIN32 |
212380e3 AC |
219 | static int |
220 | make_daemon(void) | |
221 | { | |
0c433865 AJ |
222 | int pid, nullfd, fdx; |
223 | ||
224 | /* The below is approximately what daemon(1, 0) does, but | |
225 | we need control over the parent after forking to print | |
226 | the startup message -- Aaron */ | |
212380e3 | 227 | |
0c23c0b1 AJ |
228 | if((nullfd = open("/dev/null", O_RDWR)) < 0) |
229 | { | |
230 | perror("open /dev/null"); | |
231 | exit(EXIT_FAILURE); | |
232 | } | |
233 | ||
212380e3 AC |
234 | if((pid = fork()) < 0) |
235 | { | |
236 | perror("fork"); | |
237 | exit(EXIT_FAILURE); | |
238 | } | |
239 | else if(pid > 0) | |
240 | { | |
0c433865 AJ |
241 | inotice("now running in background mode from %s as pid %d ...", |
242 | ConfigFileEntry.dpath, pid); | |
243 | ||
244 | exit(EXIT_SUCCESS); | |
212380e3 AC |
245 | } |
246 | ||
0c433865 AJ |
247 | for(fdx = 0; fdx <= 2; fdx++) |
248 | if (fdx != nullfd) | |
249 | (void) dup2(nullfd, fdx); | |
250 | ||
251 | if(nullfd > 2) | |
252 | (void) close(nullfd); | |
253 | ||
254 | (void) setsid(); | |
255 | ||
212380e3 AC |
256 | return 0; |
257 | } | |
0c433865 | 258 | #endif |
212380e3 AC |
259 | |
260 | static int printVersion = 0; | |
261 | ||
262 | struct lgetopt myopts[] = { | |
212380e3 AC |
263 | {"configfile", &ConfigFileEntry.configfile, |
264 | STRING, "File to use for ircd.conf"}, | |
212380e3 AC |
265 | {"logfile", &logFileName, |
266 | STRING, "File to use for ircd.log"}, | |
267 | {"pidfile", &pidFileName, | |
268 | STRING, "File to use for process ID"}, | |
269 | {"foreground", &server_state_foreground, | |
270 | YESNO, "Run in foreground (don't detach)"}, | |
271 | {"version", &printVersion, | |
272 | YESNO, "Print version and exit"}, | |
273 | {"conftest", &testing_conf, | |
274 | YESNO, "Test the configuration files and exit"}, | |
275 | {"help", NULL, USAGE, "Print this text"}, | |
276 | {NULL, NULL, STRING, NULL}, | |
277 | }; | |
278 | ||
212380e3 AC |
279 | static void |
280 | check_rehash(void *unused) | |
281 | { | |
282 | /* | |
283 | * Check to see whether we have to rehash the configuration .. | |
284 | */ | |
285 | if(dorehash) | |
286 | { | |
ab31d2b0 | 287 | rehash(true); |
1b916de5 | 288 | dorehash = false; |
212380e3 AC |
289 | } |
290 | ||
291 | if(dorehashbans) | |
292 | { | |
ab31d2b0 | 293 | rehash_bans(); |
1b916de5 | 294 | dorehashbans = false; |
212380e3 AC |
295 | } |
296 | ||
297 | if(doremotd) | |
298 | { | |
299 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
300 | "Got signal SIGUSR1, reloading ircd motd file"); | |
041d54ff | 301 | cache_user_motd(); |
1b916de5 | 302 | doremotd = false; |
212380e3 AC |
303 | } |
304 | } | |
305 | ||
212380e3 AC |
306 | /* |
307 | * initalialize_global_set_options | |
308 | * | |
309 | * inputs - none | |
310 | * output - none | |
55abcbb2 | 311 | * side effects - This sets all global set options needed |
212380e3 AC |
312 | */ |
313 | static void | |
314 | initialize_global_set_options(void) | |
315 | { | |
316 | memset(&GlobalSetOptions, 0, sizeof(GlobalSetOptions)); | |
317 | /* memset( &ConfigFileEntry, 0, sizeof(ConfigFileEntry)); */ | |
318 | ||
8bd5767b JT |
319 | GlobalSetOptions.maxclients = ServerInfo.default_max_clients; |
320 | ||
321 | if(GlobalSetOptions.maxclients > (maxconnections - MAX_BUFFER) || (GlobalSetOptions.maxclients <= 0)) | |
101db4c4 VY |
322 | GlobalSetOptions.maxclients = maxconnections - MAX_BUFFER; |
323 | ||
212380e3 AC |
324 | GlobalSetOptions.autoconn = 1; |
325 | ||
326 | GlobalSetOptions.spam_time = MIN_JOIN_LEAVE_TIME; | |
327 | GlobalSetOptions.spam_num = MAX_JOIN_LEAVE_COUNT; | |
328 | ||
7f3382fe | 329 | GlobalSetOptions.floodcount = ConfigFileEntry.default_floodcount; |
212380e3 AC |
330 | |
331 | split_servers = ConfigChannel.default_split_server_count; | |
332 | split_users = ConfigChannel.default_split_user_count; | |
333 | ||
334 | if(split_users && split_servers | |
335 | && (ConfigChannel.no_create_on_split || ConfigChannel.no_join_on_split)) | |
336 | { | |
337 | splitmode = 1; | |
338 | splitchecking = 1; | |
339 | } | |
340 | ||
7f3382fe | 341 | GlobalSetOptions.ident_timeout = ConfigFileEntry.default_ident_timeout; |
212380e3 | 342 | |
f427c8b0 | 343 | rb_strlcpy(GlobalSetOptions.operstring, |
212380e3 AC |
344 | ConfigFileEntry.default_operstring, |
345 | sizeof(GlobalSetOptions.operstring)); | |
f427c8b0 | 346 | rb_strlcpy(GlobalSetOptions.adminstring, |
212380e3 AC |
347 | ConfigFileEntry.default_adminstring, |
348 | sizeof(GlobalSetOptions.adminstring)); | |
349 | ||
350 | /* memset( &ConfigChannel, 0, sizeof(ConfigChannel)); */ | |
351 | ||
352 | /* End of global set options */ | |
353 | ||
354 | } | |
355 | ||
356 | /* | |
357 | * initialize_server_capabs | |
358 | * | |
359 | * inputs - none | |
360 | * output - none | |
361 | */ | |
362 | static void | |
363 | initialize_server_capabs(void) | |
364 | { | |
365 | default_server_capabs &= ~CAP_ZIP; | |
366 | } | |
367 | ||
94555087 | 368 | #ifdef _WIN32 |
d6c17e5d AC |
369 | /* |
370 | * relocate_paths | |
371 | * | |
372 | * inputs - none | |
373 | * output - none | |
374 | * side effects - items in ircd_paths[] array are relocated | |
375 | */ | |
376 | static void | |
377 | relocate_paths(void) | |
378 | { | |
379 | char prefix[PATH_MAX], workbuf[PATH_MAX]; | |
380 | char *p; | |
381 | ||
382 | rb_strlcpy(prefix, rb_path_to_self(), sizeof prefix); | |
383 | ||
384 | ircd_paths[IRCD_PATH_IRCD_EXEC] = rb_strdup(prefix); | |
385 | ||
386 | /* if we're running from inside the source tree, we probably do not want to relocate any other paths */ | |
387 | if (strstr(prefix, ".libs") != NULL) | |
388 | return; | |
389 | ||
390 | /* prefix = /home/kaniini/ircd/bin/ircd */ | |
391 | p = strrchr(prefix, RB_PATH_SEPARATOR); | |
392 | if (rb_unlikely(p == NULL)) | |
393 | return; | |
394 | *p = 0; | |
395 | ||
396 | /* prefix = /home/kaniini/ircd/bin */ | |
397 | p = strrchr(prefix, RB_PATH_SEPARATOR); | |
398 | if (rb_unlikely(p == NULL)) | |
399 | return; | |
400 | *p = 0; | |
401 | ||
402 | /* prefix = /home/kaniini/ircd */ | |
403 | ircd_paths[IRCD_PATH_PREFIX] = rb_strdup(prefix); | |
404 | ||
405 | /* now that we have our prefix, we can relocate the other paths... */ | |
406 | snprintf(workbuf, sizeof workbuf, "%s%cmodules", prefix, RB_PATH_SEPARATOR); | |
407 | ircd_paths[IRCD_PATH_MODULES] = rb_strdup(workbuf); | |
408 | ||
409 | snprintf(workbuf, sizeof workbuf, "%s%cmodules%cautoload", prefix, RB_PATH_SEPARATOR, RB_PATH_SEPARATOR); | |
410 | ircd_paths[IRCD_PATH_AUTOLOAD_MODULES] = rb_strdup(workbuf); | |
411 | ||
412 | snprintf(workbuf, sizeof workbuf, "%s%cetc", prefix, RB_PATH_SEPARATOR); | |
413 | ircd_paths[IRCD_PATH_ETC] = rb_strdup(workbuf); | |
414 | ||
415 | snprintf(workbuf, sizeof workbuf, "%s%clog", prefix, RB_PATH_SEPARATOR); | |
416 | ircd_paths[IRCD_PATH_LOG] = rb_strdup(workbuf); | |
417 | ||
418 | snprintf(workbuf, sizeof workbuf, "%s%chelp%cusers", prefix, RB_PATH_SEPARATOR, RB_PATH_SEPARATOR); | |
419 | ircd_paths[IRCD_PATH_USERHELP] = rb_strdup(workbuf); | |
420 | ||
421 | snprintf(workbuf, sizeof workbuf, "%s%chelp%copers", prefix, RB_PATH_SEPARATOR, RB_PATH_SEPARATOR); | |
422 | ircd_paths[IRCD_PATH_OPERHELP] = rb_strdup(workbuf); | |
423 | ||
424 | snprintf(workbuf, sizeof workbuf, "%s%cetc%circd.conf", prefix, RB_PATH_SEPARATOR, RB_PATH_SEPARATOR); | |
425 | ircd_paths[IRCD_PATH_IRCD_CONF] = rb_strdup(workbuf); | |
426 | ||
427 | snprintf(workbuf, sizeof workbuf, "%s%cetc%circd.motd", prefix, RB_PATH_SEPARATOR, RB_PATH_SEPARATOR); | |
428 | ircd_paths[IRCD_PATH_IRCD_MOTD] = rb_strdup(workbuf); | |
429 | ||
430 | snprintf(workbuf, sizeof workbuf, "%s%cetc%copers.motd", prefix, RB_PATH_SEPARATOR, RB_PATH_SEPARATOR); | |
431 | ircd_paths[IRCD_PATH_IRCD_OMOTD] = rb_strdup(workbuf); | |
432 | ||
433 | snprintf(workbuf, sizeof workbuf, "%s%cetc%cban.db", prefix, RB_PATH_SEPARATOR, RB_PATH_SEPARATOR); | |
434 | ircd_paths[IRCD_PATH_BANDB] = rb_strdup(workbuf); | |
435 | ||
436 | snprintf(workbuf, sizeof workbuf, "%s%cetc%circd.pid", prefix, RB_PATH_SEPARATOR, RB_PATH_SEPARATOR); | |
437 | ircd_paths[IRCD_PATH_IRCD_PID] = rb_strdup(workbuf); | |
438 | ||
439 | snprintf(workbuf, sizeof workbuf, "%s%clogs%circd.log", prefix, RB_PATH_SEPARATOR, RB_PATH_SEPARATOR); | |
440 | ircd_paths[IRCD_PATH_IRCD_LOG] = rb_strdup(workbuf); | |
441 | ||
442 | snprintf(workbuf, sizeof workbuf, "%s%cbin", prefix, RB_PATH_SEPARATOR); | |
443 | ircd_paths[IRCD_PATH_BIN] = rb_strdup(workbuf); | |
444 | ircd_paths[IRCD_PATH_LIBEXEC] = rb_strdup(workbuf); | |
e0e0c415 | 445 | |
73b70ae8 | 446 | inotice("runtime paths:"); |
e0e0c415 AC |
447 | for (int i = 0; i < IRCD_PATH_COUNT; i++) |
448 | { | |
449 | inotice(" %s: %s", ircd_pathnames[i], ircd_paths[i]); | |
450 | } | |
d6c17e5d | 451 | } |
94555087 | 452 | #endif |
212380e3 AC |
453 | |
454 | /* | |
455 | * write_pidfile | |
456 | * | |
457 | * inputs - filename+path of pid file | |
458 | * output - none | |
459 | * side effects - write the pid of the ircd to filename | |
460 | */ | |
461 | static void | |
462 | write_pidfile(const char *filename) | |
463 | { | |
464 | FILE *fb; | |
465 | char buff[32]; | |
466 | if((fb = fopen(filename, "w"))) | |
467 | { | |
468 | unsigned int pid = (unsigned int) getpid(); | |
469 | ||
5203cba5 | 470 | snprintf(buff, sizeof(buff), "%u\n", pid); |
212380e3 AC |
471 | if((fputs(buff, fb) == -1)) |
472 | { | |
473 | ilog(L_MAIN, "Error writing %u to pid file %s (%s)", | |
474 | pid, filename, strerror(errno)); | |
475 | } | |
476 | fclose(fb); | |
477 | return; | |
478 | } | |
479 | else | |
480 | { | |
481 | ilog(L_MAIN, "Error opening pid file %s", filename); | |
482 | } | |
483 | } | |
484 | ||
485 | /* | |
486 | * check_pidfile | |
487 | * | |
488 | * inputs - filename+path of pid file | |
489 | * output - none | |
490 | * side effects - reads pid from pidfile and checks if ircd is in process | |
491 | * list. if it is, gracefully exits | |
492 | * -kre | |
493 | */ | |
494 | static void | |
495 | check_pidfile(const char *filename) | |
496 | { | |
497 | FILE *fb; | |
498 | char buff[32]; | |
499 | pid_t pidfromfile; | |
500 | ||
501 | /* Don't do logging here, since we don't have log() initialised */ | |
502 | if((fb = fopen(filename, "r"))) | |
503 | { | |
504 | if(fgets(buff, 20, fb) != NULL) | |
505 | { | |
506 | pidfromfile = atoi(buff); | |
538d2089 | 507 | if(!rb_kill(pidfromfile, 0)) |
212380e3 AC |
508 | { |
509 | printf("ircd: daemon is already running\n"); | |
510 | exit(-1); | |
511 | } | |
512 | } | |
513 | fclose(fb); | |
514 | } | |
515 | } | |
516 | ||
517 | /* | |
518 | * setup_corefile | |
519 | * | |
520 | * inputs - nothing | |
521 | * output - nothing | |
522 | * side effects - setups corefile to system limits. | |
523 | * -kre | |
524 | */ | |
525 | static void | |
526 | setup_corefile(void) | |
527 | { | |
528 | #ifdef HAVE_SYS_RESOURCE_H | |
529 | struct rlimit rlim; /* resource limits */ | |
530 | ||
531 | /* Set corefilesize to maximum */ | |
532 | if(!getrlimit(RLIMIT_CORE, &rlim)) | |
533 | { | |
534 | rlim.rlim_cur = rlim.rlim_max; | |
535 | setrlimit(RLIMIT_CORE, &rlim); | |
536 | } | |
537 | #endif | |
538 | } | |
539 | ||
6fce54ff VY |
540 | static void |
541 | ircd_log_cb(const char *str) | |
542 | { | |
fe037171 | 543 | ilog(L_MAIN, "librb reports: %s", str); |
6fce54ff VY |
544 | } |
545 | ||
546 | static void | |
547 | ircd_restart_cb(const char *str) | |
548 | { | |
fe037171 | 549 | inotice("librb has called the restart callback: %s", str); |
6fce54ff VY |
550 | restart(str); |
551 | } | |
552 | ||
553 | /* | |
554 | * Why EXIT_FAILURE here? | |
555 | * Because if ircd_die_cb() is called it's because of a fatal | |
556 | * error inside libcharybdis, and we don't know how to handle the | |
557 | * exception, so it is logical to return a FAILURE exit code here. | |
558 | * --nenolod | |
559 | */ | |
560 | static void | |
561 | ircd_die_cb(const char *str) | |
562 | { | |
563 | if(str != NULL) | |
564 | { | |
565 | /* Try to get the message out to currently logged in operators. */ | |
fe037171 EM |
566 | sendto_realops_snomask(SNO_GENERAL, L_NETWIDE, "librb has called the die callback..aborting: %s", str); |
567 | inotice("librb has called the die callback..aborting: %s", str); | |
6fce54ff | 568 | } |
6643434b | 569 | else |
fe037171 | 570 | inotice("librb has called the die callback..aborting"); |
6fce54ff VY |
571 | |
572 | unlink(pidFileName); | |
573 | exit(EXIT_FAILURE); | |
574 | } | |
575 | ||
fa832850 AC |
576 | struct ev_entry *check_splitmode_ev = NULL; |
577 | ||
d80645d0 VY |
578 | static int |
579 | seed_with_urandom(void) | |
580 | { | |
581 | unsigned int seed; | |
582 | int fd; | |
583 | ||
584 | fd = open("/dev/urandom", O_RDONLY); | |
585 | if(fd >= 0) | |
586 | { | |
587 | if(read(fd, &seed, sizeof(seed)) == sizeof(seed)) | |
588 | { | |
589 | close(fd); | |
590 | srand(seed); | |
591 | return 1; | |
592 | } | |
5457b102 | 593 | close(fd); |
d80645d0 VY |
594 | } |
595 | return 0; | |
596 | } | |
597 | ||
598 | static void | |
599 | seed_with_clock(void) | |
600 | { | |
55abcbb2 | 601 | const struct timeval *tv; |
d80645d0 VY |
602 | rb_set_time(); |
603 | tv = rb_current_time_tv(); | |
604 | srand(tv->tv_sec ^ (tv->tv_usec | (getpid() << 20))); | |
605 | } | |
606 | ||
607 | static void | |
608 | seed_random(void *unused) | |
609 | { | |
610 | unsigned int seed; | |
611 | if(rb_get_random(&seed, sizeof(seed)) == -1) | |
612 | { | |
613 | if(!seed_with_urandom()) | |
614 | seed_with_clock(); | |
615 | return; | |
616 | } | |
617 | srand(seed); | |
618 | } | |
619 | ||
212380e3 AC |
620 | /* |
621 | * main | |
622 | * | |
623 | * Initializes the IRCd. | |
624 | * | |
625 | * Inputs - number of commandline args, args themselves | |
626 | * Outputs - none | |
627 | * Side Effects - this is where the ircd gets going right now | |
628 | */ | |
629 | int | |
5adde7a4 | 630 | charybdis_main(int argc, char * const argv[]) |
212380e3 AC |
631 | { |
632 | int fd; | |
633 | ||
1ba9eba5 | 634 | #ifndef _WIN32 |
212380e3 AC |
635 | /* Check to see if the user is running us as root, which is a nono */ |
636 | if(geteuid() == 0) | |
637 | { | |
638 | fprintf(stderr, "Don't run ircd as root!!!\n"); | |
639 | return -1; | |
640 | } | |
1ba9eba5 | 641 | #endif |
212380e3 | 642 | |
94555087 | 643 | #ifdef _WIN32 |
d6c17e5d | 644 | relocate_paths(); |
94555087 | 645 | #endif |
adc7be98 | 646 | |
2f545aad AC |
647 | logFileName = ircd_paths[IRCD_PATH_IRCD_LOG]; |
648 | pidFileName = ircd_paths[IRCD_PATH_IRCD_PID]; | |
649 | ||
4d8cfacd AC |
650 | ConfigFileEntry.dpath = ircd_paths[IRCD_PATH_PREFIX]; |
651 | ConfigFileEntry.configfile = ircd_paths[IRCD_PATH_IRCD_CONF]; /* Server configuration file */ | |
befa24c8 | 652 | ConfigFileEntry.connect_timeout = 30; /* Default to 30 */ |
346fba92 | 653 | |
d6c17e5d AC |
654 | init_sys(); |
655 | ||
befa24c8 | 656 | umask(077); /* better safe than sorry --SRB */ |
adc7be98 VY |
657 | |
658 | myargv = argv; | |
659 | parseargs(&argc, &argv, myopts); | |
660 | ||
661 | if(chdir(ConfigFileEntry.dpath)) | |
662 | { | |
663 | fprintf(stderr, "Unable to chdir to %s: %s\n", ConfigFileEntry.dpath, strerror(errno)); | |
664 | exit(EXIT_FAILURE); | |
665 | } | |
666 | ||
3d3d1a95 VY |
667 | rb_set_time(); |
668 | ||
212380e3 AC |
669 | /* |
670 | * Setup corefile size immediately after boot -kre | |
671 | */ | |
672 | setup_corefile(); | |
673 | ||
b02a913b AC |
674 | /* initialise operhash fairly early. */ |
675 | init_operhash(); | |
676 | ||
212380e3 AC |
677 | memset(&me, 0, sizeof(me)); |
678 | memset(&meLocalUser, 0, sizeof(meLocalUser)); | |
679 | me.localClient = &meLocalUser; | |
680 | ||
681 | /* Make sure all lists are zeroed */ | |
682 | memset(&unknown_list, 0, sizeof(unknown_list)); | |
683 | memset(&lclient_list, 0, sizeof(lclient_list)); | |
684 | memset(&serv_list, 0, sizeof(serv_list)); | |
685 | memset(&global_serv_list, 0, sizeof(global_serv_list)); | |
686 | memset(&local_oper_list, 0, sizeof(local_oper_list)); | |
687 | memset(&oper_list, 0, sizeof(oper_list)); | |
688 | ||
330fc5c1 | 689 | rb_dlinkAddTail(&me, &me.node, &global_client_list); |
212380e3 | 690 | |
2af8c7ff VY |
691 | memset(&Count, 0, sizeof(Count)); |
692 | memset(&ServerInfo, 0, sizeof(ServerInfo)); | |
693 | memset(&AdminInfo, 0, sizeof(AdminInfo)); | |
694 | memset(&ServerStats, 0, sizeof(struct ServerStatistics)); | |
212380e3 | 695 | |
212380e3 AC |
696 | if(printVersion) |
697 | { | |
b3803848 | 698 | printf("ircd: version %s(%s)\n", ircd_version, serno); |
8bd1c8a1 AC |
699 | #ifdef CUSTOM_BRANDING |
700 | printf("ircd: based on %s-%s\n", PACKAGE_NAME, PACKAGE_VERSION); | |
701 | #endif | |
d2b6b9f3 | 702 | printf("ircd: %s\n", rb_lib_version()); |
212380e3 AC |
703 | exit(EXIT_SUCCESS); |
704 | } | |
705 | ||
212380e3 AC |
706 | setup_signals(); |
707 | ||
212380e3 | 708 | if (testing_conf) |
503727d1 | 709 | server_state_foreground = true; |
212380e3 | 710 | |
ef24ede3 AJ |
711 | #ifndef _WIN32 |
712 | /* Make sure fd 0, 1 and 2 are in use -- jilles */ | |
713 | do | |
714 | { | |
715 | fd = open("/dev/null", O_RDWR); | |
716 | } while (fd < 2 && fd != -1); | |
717 | if (fd > 2) | |
718 | close(fd); | |
719 | else if (fd == -1) | |
720 | exit(1); | |
721 | #endif | |
722 | ||
212380e3 AC |
723 | /* Check if there is pidfile and daemon already running */ |
724 | if(!testing_conf) | |
725 | { | |
726 | check_pidfile(pidFileName); | |
f4e9d915 | 727 | |
0942c1fc AJ |
728 | inotice("starting %s ...", ircd_version); |
729 | inotice("%s", rb_lib_version()); | |
730 | ||
f4e9d915 AJ |
731 | #ifndef _WIN32 |
732 | if(!server_state_foreground) | |
733 | make_daemon(); | |
734 | #endif | |
212380e3 AC |
735 | } |
736 | ||
737 | /* Init the event subsystem */ | |
0edb29fa | 738 | rb_lib_init(ircd_log_cb, ircd_restart_cb, ircd_die_cb, !server_state_foreground, maxconnections, DNODE_HEAP_SIZE, FD_HEAP_SIZE); |
150f35f7 | 739 | rb_linebuf_init(LINEBUF_HEAP_SIZE); |
212380e3 | 740 | |
e861902f | 741 | rb_init_prng(NULL, RB_PRNG_DEFAULT); |
37cd159e | 742 | |
e1db84d8 VY |
743 | seed_random(NULL); |
744 | ||
346fba92 AC |
745 | init_builtin_capabs(); |
746 | default_server_capabs = CAP_MASK; | |
747 | ||
212380e3 | 748 | init_main_logfile(); |
212380e3 AC |
749 | newconf_init(); |
750 | init_s_conf(); | |
751 | init_s_newconf(); | |
752 | init_hash(); | |
753 | clear_scache_hash_table(); /* server cache name table */ | |
754 | init_host_hash(); | |
755 | clear_hash_parse(); | |
756 | init_client(); | |
212380e3 AC |
757 | init_hook(); |
758 | init_channels(); | |
759 | initclass(); | |
b47f8a4f | 760 | whowas_init(); |
212380e3 AC |
761 | init_reject(); |
762 | init_cache(); | |
763 | init_monitor(); | |
030cdce7 | 764 | |
19716b9f | 765 | construct_cflags_strings(); |
030cdce7 | 766 | |
6d063f05 | 767 | init_authd(); /* Start up authd. */ |
068c6c4a | 768 | init_dns(); /* Start up DNS query system */ |
92dad483 | 769 | init_modules(); /* Start up modules system */ |
8a26cd19 | 770 | |
39a68b53 | 771 | privilegeset_set_new("default", "", 0); |
212380e3 AC |
772 | |
773 | if (testing_conf) | |
774 | fprintf(stderr, "\nBeginning config test\n"); | |
ea111ea5 | 775 | read_conf_files(true); /* cold start init conf files */ |
212380e3 | 776 | |
a19097ba EM |
777 | load_all_modules(1); |
778 | load_core_modules(1); | |
212380e3 | 779 | |
b583faf9 AC |
780 | init_isupport(); |
781 | ||
6eebc373 | 782 | init_bandb(); |
c6d72037 | 783 | init_ssld(); |
c53ca1e0 | 784 | init_wsockd(); |
c6d72037 | 785 | |
ab31d2b0 | 786 | rehash_bans(); |
80c9ac51 | 787 | |
212380e3 AC |
788 | initialize_server_capabs(); /* Set up default_server_capabs */ |
789 | initialize_global_set_options(); | |
790 | ||
791 | if(ServerInfo.name == NULL) | |
792 | { | |
793 | ierror("no server name specified in serverinfo block."); | |
794 | return -1; | |
795 | } | |
f427c8b0 | 796 | rb_strlcpy(me.name, ServerInfo.name, sizeof(me.name)); |
212380e3 AC |
797 | |
798 | if(ServerInfo.sid[0] == '\0') | |
799 | { | |
800 | ierror("no server sid specified in serverinfo block."); | |
801 | return -2; | |
802 | } | |
0982871a | 803 | rb_strlcpy(me.id, ServerInfo.sid, sizeof(me.id)); |
212380e3 AC |
804 | init_uid(); |
805 | ||
806 | /* serverinfo{} description must exist. If not, error out. */ | |
807 | if(ServerInfo.description == NULL) | |
808 | { | |
809 | ierror("no server description specified in serverinfo block."); | |
810 | return -3; | |
811 | } | |
f427c8b0 | 812 | rb_strlcpy(me.info, ServerInfo.description, sizeof(me.info)); |
212380e3 | 813 | |
f5960b83 | 814 | if(ServerInfo.ssl_cert != NULL) |
8bd5767b JT |
815 | { |
816 | /* just do the rb_setup_ssl_server to validate the config */ | |
c1725bda | 817 | if(!rb_setup_ssl_server(ServerInfo.ssl_cert, ServerInfo.ssl_private_key, ServerInfo.ssl_dh_params, ServerInfo.ssl_cipher_list)) |
8bd5767b JT |
818 | { |
819 | ilog(L_MAIN, "WARNING: Unable to setup SSL."); | |
bfc44622 | 820 | ircd_ssl_ok = false; |
8bd5767b JT |
821 | } |
822 | else | |
bfc44622 | 823 | ircd_ssl_ok = true; |
c6d72037 VY |
824 | } |
825 | ||
212380e3 AC |
826 | me.from = &me; |
827 | me.servptr = &me; | |
828 | SetMe(&me); | |
829 | make_server(&me); | |
e3354945 | 830 | startup_time = rb_current_time(); |
212380e3 AC |
831 | add_to_client_hash(me.name, &me); |
832 | add_to_id_hash(me.id, &me); | |
994544c2 | 833 | me.serv->nameinfo = scache_connect(me.name, me.info, 0); |
212380e3 | 834 | |
330fc5c1 | 835 | rb_dlinkAddAlloc(&me, &global_serv_list); |
212380e3 AC |
836 | |
837 | construct_umodebuf(); | |
838 | ||
d2b5f411 SA |
839 | if (testing_conf) |
840 | { | |
841 | fprintf(stderr, "\nConfig testing complete.\n"); | |
842 | fflush(stderr); | |
843 | return 0; /* Why? We want the launcher to exit out. */ | |
844 | } | |
845 | ||
212380e3 | 846 | check_class(); |
f4e9d915 | 847 | write_pidfile(pidFileName); |
212380e3 AC |
848 | load_help(); |
849 | open_logfiles(); | |
850 | ||
50808796 EM |
851 | configure_authd(); |
852 | ||
212380e3 AC |
853 | ilog(L_MAIN, "Server Ready"); |
854 | ||
212380e3 AC |
855 | /* We want try_connections to be called as soon as possible now! -- adrian */ |
856 | /* No, 'cause after a restart it would cause all sorts of nick collides */ | |
857 | /* um. by waiting even longer, that just means we have even *more* | |
858 | * nick collisions. what a stupid idea. set an event for the IO loop --fl | |
859 | */ | |
12aea5fe | 860 | rb_event_addish("try_connections", try_connections, NULL, STARTUP_CONNECTIONS_TIME); |
36645728 VY |
861 | rb_event_addonce("try_connections_startup", try_connections, NULL, 2); |
862 | rb_event_add("check_rehash", check_rehash, NULL, 3); | |
d80645d0 | 863 | rb_event_addish("reseed_srand", seed_random, NULL, 300); /* reseed every 10 minutes */ |
212380e3 | 864 | |
212380e3 | 865 | if(splitmode) |
36645728 | 866 | check_splitmode_ev = rb_event_add("check_splitmode", check_splitmode, NULL, 5); |
212380e3 | 867 | |
0c433865 AJ |
868 | if(server_state_foreground) |
869 | inotice("now running in foreground mode from %s as pid %d ...", | |
870 | ConfigFileEntry.dpath, getpid()); | |
212380e3 | 871 | |
39944e1f | 872 | rb_lib_loop(0); |
212380e3 AC |
873 | |
874 | return 0; | |
875 | } |