]>
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 | * print_startup - print startup information | |
192 | */ | |
193 | static void | |
194 | print_startup(int pid) | |
195 | { | |
b45b2dae JT |
196 | int fd; |
197 | ||
c1bfea73 | 198 | #ifndef _WIN32 |
b45b2dae JT |
199 | close(1); |
200 | fd = open("/dev/null", O_RDWR); | |
201 | if (fd == -1) { | |
202 | perror("open /dev/null"); | |
203 | exit(EXIT_FAILURE); | |
204 | } | |
205 | if (fd == 0) | |
206 | fd = dup(fd); | |
207 | if (fd != 1) | |
208 | abort(); | |
c1bfea73 | 209 | #endif |
212380e3 AC |
210 | inotice("now running in %s mode from %s as pid %d ...", |
211 | !server_state_foreground ? "background" : "foreground", | |
212 | ConfigFileEntry.dpath, pid); | |
213 | ||
c1bfea73 | 214 | #ifndef _WIN32 |
212380e3 AC |
215 | /* let the parent process know the initialization was successful |
216 | * -- jilles */ | |
217 | if (!server_state_foreground) | |
c084fcaf | 218 | { |
2ac4ba96 EM |
219 | /* GCC complains on Linux if we don't check the value of write pedantically. |
220 | * Technically you're supposed to check the value, yes, but it probably can't fail. | |
221 | * No, casting to void is of no use to shut the warning up. You HAVE to use the value. | |
222 | * --Elizfaox | |
223 | */ | |
224 | if(write(0, ".", 1) < 1) | |
225 | abort(); | |
c084fcaf | 226 | } |
b45b2dae JT |
227 | if (dup2(1, 0) == -1) |
228 | abort(); | |
229 | if (dup2(1, 2) == -1) | |
230 | abort(); | |
c1bfea73 | 231 | #endif |
212380e3 AC |
232 | } |
233 | ||
212380e3 AC |
234 | /* |
235 | * init_sys | |
236 | * | |
237 | * inputs - boot_daemon flag | |
238 | * output - none | |
239 | * side effects - if boot_daemon flag is not set, don't daemonize | |
240 | */ | |
241 | static void | |
242 | init_sys(void) | |
243 | { | |
c1bfea73 | 244 | #if !defined(_WIN32) && defined(RLIMIT_NOFILE) && defined(HAVE_SYS_RESOURCE_H) |
8bd5767b JT |
245 | struct rlimit limit; |
246 | ||
247 | if(!getrlimit(RLIMIT_NOFILE, &limit)) | |
248 | { | |
249 | maxconnections = limit.rlim_cur; | |
250 | if(maxconnections <= MAX_BUFFER) | |
251 | { | |
252 | fprintf(stderr, "ERROR: Shell FD limits are too low.\n"); | |
ec86417c | 253 | fprintf(stderr, "ERROR: charybdis reserves %d FDs, shell limits must be above this\n", MAX_BUFFER); |
8bd5767b JT |
254 | exit(EXIT_FAILURE); |
255 | } | |
256 | return; | |
257 | } | |
258 | #endif /* RLIMIT_FD_MAX */ | |
101db4c4 | 259 | maxconnections = MAXCONNECTIONS; |
212380e3 AC |
260 | } |
261 | ||
262 | static int | |
263 | make_daemon(void) | |
264 | { | |
538d2089 | 265 | #ifndef _WIN32 |
212380e3 AC |
266 | int pid; |
267 | int pip[2]; | |
268 | char c; | |
269 | ||
270 | if (pipe(pip) < 0) | |
271 | { | |
272 | perror("pipe"); | |
273 | exit(EXIT_FAILURE); | |
274 | } | |
275 | dup2(pip[1], 0); | |
276 | close(pip[1]); | |
277 | if((pid = fork()) < 0) | |
278 | { | |
279 | perror("fork"); | |
280 | exit(EXIT_FAILURE); | |
281 | } | |
282 | else if(pid > 0) | |
283 | { | |
284 | close(0); | |
285 | /* Wait for initialization to finish, successfully or | |
286 | * unsuccessfully. Until this point the child may still | |
287 | * write to stdout/stderr. | |
288 | * -- jilles */ | |
289 | if (read(pip[0], &c, 1) > 0) | |
290 | exit(EXIT_SUCCESS); | |
291 | else | |
292 | exit(EXIT_FAILURE); | |
293 | } | |
294 | ||
295 | close(pip[0]); | |
296 | setsid(); | |
297 | /* fclose(stdin); | |
298 | fclose(stdout); | |
299 | fclose(stderr); */ | |
538d2089 | 300 | #endif |
212380e3 AC |
301 | return 0; |
302 | } | |
303 | ||
304 | static int printVersion = 0; | |
305 | ||
306 | struct lgetopt myopts[] = { | |
212380e3 AC |
307 | {"configfile", &ConfigFileEntry.configfile, |
308 | STRING, "File to use for ircd.conf"}, | |
212380e3 AC |
309 | {"logfile", &logFileName, |
310 | STRING, "File to use for ircd.log"}, | |
311 | {"pidfile", &pidFileName, | |
312 | STRING, "File to use for process ID"}, | |
313 | {"foreground", &server_state_foreground, | |
314 | YESNO, "Run in foreground (don't detach)"}, | |
315 | {"version", &printVersion, | |
316 | YESNO, "Print version and exit"}, | |
317 | {"conftest", &testing_conf, | |
318 | YESNO, "Test the configuration files and exit"}, | |
319 | {"help", NULL, USAGE, "Print this text"}, | |
320 | {NULL, NULL, STRING, NULL}, | |
321 | }; | |
322 | ||
212380e3 AC |
323 | static void |
324 | check_rehash(void *unused) | |
325 | { | |
326 | /* | |
327 | * Check to see whether we have to rehash the configuration .. | |
328 | */ | |
329 | if(dorehash) | |
330 | { | |
ab31d2b0 | 331 | rehash(true); |
1b916de5 | 332 | dorehash = false; |
212380e3 AC |
333 | } |
334 | ||
335 | if(dorehashbans) | |
336 | { | |
ab31d2b0 | 337 | rehash_bans(); |
1b916de5 | 338 | dorehashbans = false; |
212380e3 AC |
339 | } |
340 | ||
341 | if(doremotd) | |
342 | { | |
343 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
344 | "Got signal SIGUSR1, reloading ircd motd file"); | |
041d54ff | 345 | cache_user_motd(); |
1b916de5 | 346 | doremotd = false; |
212380e3 AC |
347 | } |
348 | } | |
349 | ||
212380e3 AC |
350 | /* |
351 | * initalialize_global_set_options | |
352 | * | |
353 | * inputs - none | |
354 | * output - none | |
55abcbb2 | 355 | * side effects - This sets all global set options needed |
212380e3 AC |
356 | */ |
357 | static void | |
358 | initialize_global_set_options(void) | |
359 | { | |
360 | memset(&GlobalSetOptions, 0, sizeof(GlobalSetOptions)); | |
361 | /* memset( &ConfigFileEntry, 0, sizeof(ConfigFileEntry)); */ | |
362 | ||
8bd5767b JT |
363 | GlobalSetOptions.maxclients = ServerInfo.default_max_clients; |
364 | ||
365 | if(GlobalSetOptions.maxclients > (maxconnections - MAX_BUFFER) || (GlobalSetOptions.maxclients <= 0)) | |
101db4c4 VY |
366 | GlobalSetOptions.maxclients = maxconnections - MAX_BUFFER; |
367 | ||
212380e3 AC |
368 | GlobalSetOptions.autoconn = 1; |
369 | ||
370 | GlobalSetOptions.spam_time = MIN_JOIN_LEAVE_TIME; | |
371 | GlobalSetOptions.spam_num = MAX_JOIN_LEAVE_COUNT; | |
372 | ||
7f3382fe | 373 | GlobalSetOptions.floodcount = ConfigFileEntry.default_floodcount; |
212380e3 AC |
374 | |
375 | split_servers = ConfigChannel.default_split_server_count; | |
376 | split_users = ConfigChannel.default_split_user_count; | |
377 | ||
378 | if(split_users && split_servers | |
379 | && (ConfigChannel.no_create_on_split || ConfigChannel.no_join_on_split)) | |
380 | { | |
381 | splitmode = 1; | |
382 | splitchecking = 1; | |
383 | } | |
384 | ||
7f3382fe | 385 | GlobalSetOptions.ident_timeout = ConfigFileEntry.default_ident_timeout; |
212380e3 | 386 | |
f427c8b0 | 387 | rb_strlcpy(GlobalSetOptions.operstring, |
212380e3 AC |
388 | ConfigFileEntry.default_operstring, |
389 | sizeof(GlobalSetOptions.operstring)); | |
f427c8b0 | 390 | rb_strlcpy(GlobalSetOptions.adminstring, |
212380e3 AC |
391 | ConfigFileEntry.default_adminstring, |
392 | sizeof(GlobalSetOptions.adminstring)); | |
393 | ||
394 | /* memset( &ConfigChannel, 0, sizeof(ConfigChannel)); */ | |
395 | ||
396 | /* End of global set options */ | |
397 | ||
398 | } | |
399 | ||
400 | /* | |
401 | * initialize_server_capabs | |
402 | * | |
403 | * inputs - none | |
404 | * output - none | |
405 | */ | |
406 | static void | |
407 | initialize_server_capabs(void) | |
408 | { | |
409 | default_server_capabs &= ~CAP_ZIP; | |
410 | } | |
411 | ||
94555087 | 412 | #ifdef _WIN32 |
d6c17e5d AC |
413 | /* |
414 | * relocate_paths | |
415 | * | |
416 | * inputs - none | |
417 | * output - none | |
418 | * side effects - items in ircd_paths[] array are relocated | |
419 | */ | |
420 | static void | |
421 | relocate_paths(void) | |
422 | { | |
423 | char prefix[PATH_MAX], workbuf[PATH_MAX]; | |
424 | char *p; | |
425 | ||
426 | rb_strlcpy(prefix, rb_path_to_self(), sizeof prefix); | |
427 | ||
428 | ircd_paths[IRCD_PATH_IRCD_EXEC] = rb_strdup(prefix); | |
429 | ||
430 | /* if we're running from inside the source tree, we probably do not want to relocate any other paths */ | |
431 | if (strstr(prefix, ".libs") != NULL) | |
432 | return; | |
433 | ||
434 | /* prefix = /home/kaniini/ircd/bin/ircd */ | |
435 | p = strrchr(prefix, RB_PATH_SEPARATOR); | |
436 | if (rb_unlikely(p == NULL)) | |
437 | return; | |
438 | *p = 0; | |
439 | ||
440 | /* prefix = /home/kaniini/ircd/bin */ | |
441 | p = strrchr(prefix, RB_PATH_SEPARATOR); | |
442 | if (rb_unlikely(p == NULL)) | |
443 | return; | |
444 | *p = 0; | |
445 | ||
446 | /* prefix = /home/kaniini/ircd */ | |
447 | ircd_paths[IRCD_PATH_PREFIX] = rb_strdup(prefix); | |
448 | ||
449 | /* now that we have our prefix, we can relocate the other paths... */ | |
450 | snprintf(workbuf, sizeof workbuf, "%s%cmodules", prefix, RB_PATH_SEPARATOR); | |
451 | ircd_paths[IRCD_PATH_MODULES] = rb_strdup(workbuf); | |
452 | ||
453 | snprintf(workbuf, sizeof workbuf, "%s%cmodules%cautoload", prefix, RB_PATH_SEPARATOR, RB_PATH_SEPARATOR); | |
454 | ircd_paths[IRCD_PATH_AUTOLOAD_MODULES] = rb_strdup(workbuf); | |
455 | ||
456 | snprintf(workbuf, sizeof workbuf, "%s%cetc", prefix, RB_PATH_SEPARATOR); | |
457 | ircd_paths[IRCD_PATH_ETC] = rb_strdup(workbuf); | |
458 | ||
459 | snprintf(workbuf, sizeof workbuf, "%s%clog", prefix, RB_PATH_SEPARATOR); | |
460 | ircd_paths[IRCD_PATH_LOG] = rb_strdup(workbuf); | |
461 | ||
462 | snprintf(workbuf, sizeof workbuf, "%s%chelp%cusers", prefix, RB_PATH_SEPARATOR, RB_PATH_SEPARATOR); | |
463 | ircd_paths[IRCD_PATH_USERHELP] = rb_strdup(workbuf); | |
464 | ||
465 | snprintf(workbuf, sizeof workbuf, "%s%chelp%copers", prefix, RB_PATH_SEPARATOR, RB_PATH_SEPARATOR); | |
466 | ircd_paths[IRCD_PATH_OPERHELP] = rb_strdup(workbuf); | |
467 | ||
468 | snprintf(workbuf, sizeof workbuf, "%s%cetc%circd.conf", prefix, RB_PATH_SEPARATOR, RB_PATH_SEPARATOR); | |
469 | ircd_paths[IRCD_PATH_IRCD_CONF] = rb_strdup(workbuf); | |
470 | ||
471 | snprintf(workbuf, sizeof workbuf, "%s%cetc%circd.motd", prefix, RB_PATH_SEPARATOR, RB_PATH_SEPARATOR); | |
472 | ircd_paths[IRCD_PATH_IRCD_MOTD] = rb_strdup(workbuf); | |
473 | ||
474 | snprintf(workbuf, sizeof workbuf, "%s%cetc%copers.motd", prefix, RB_PATH_SEPARATOR, RB_PATH_SEPARATOR); | |
475 | ircd_paths[IRCD_PATH_IRCD_OMOTD] = rb_strdup(workbuf); | |
476 | ||
477 | snprintf(workbuf, sizeof workbuf, "%s%cetc%cban.db", prefix, RB_PATH_SEPARATOR, RB_PATH_SEPARATOR); | |
478 | ircd_paths[IRCD_PATH_BANDB] = rb_strdup(workbuf); | |
479 | ||
480 | snprintf(workbuf, sizeof workbuf, "%s%cetc%circd.pid", prefix, RB_PATH_SEPARATOR, RB_PATH_SEPARATOR); | |
481 | ircd_paths[IRCD_PATH_IRCD_PID] = rb_strdup(workbuf); | |
482 | ||
483 | snprintf(workbuf, sizeof workbuf, "%s%clogs%circd.log", prefix, RB_PATH_SEPARATOR, RB_PATH_SEPARATOR); | |
484 | ircd_paths[IRCD_PATH_IRCD_LOG] = rb_strdup(workbuf); | |
485 | ||
486 | snprintf(workbuf, sizeof workbuf, "%s%cbin", prefix, RB_PATH_SEPARATOR); | |
487 | ircd_paths[IRCD_PATH_BIN] = rb_strdup(workbuf); | |
488 | ircd_paths[IRCD_PATH_LIBEXEC] = rb_strdup(workbuf); | |
e0e0c415 | 489 | |
73b70ae8 | 490 | inotice("runtime paths:"); |
e0e0c415 AC |
491 | for (int i = 0; i < IRCD_PATH_COUNT; i++) |
492 | { | |
493 | inotice(" %s: %s", ircd_pathnames[i], ircd_paths[i]); | |
494 | } | |
d6c17e5d | 495 | } |
94555087 | 496 | #endif |
212380e3 AC |
497 | |
498 | /* | |
499 | * write_pidfile | |
500 | * | |
501 | * inputs - filename+path of pid file | |
502 | * output - none | |
503 | * side effects - write the pid of the ircd to filename | |
504 | */ | |
505 | static void | |
506 | write_pidfile(const char *filename) | |
507 | { | |
508 | FILE *fb; | |
509 | char buff[32]; | |
510 | if((fb = fopen(filename, "w"))) | |
511 | { | |
512 | unsigned int pid = (unsigned int) getpid(); | |
513 | ||
5203cba5 | 514 | snprintf(buff, sizeof(buff), "%u\n", pid); |
212380e3 AC |
515 | if((fputs(buff, fb) == -1)) |
516 | { | |
517 | ilog(L_MAIN, "Error writing %u to pid file %s (%s)", | |
518 | pid, filename, strerror(errno)); | |
519 | } | |
520 | fclose(fb); | |
521 | return; | |
522 | } | |
523 | else | |
524 | { | |
525 | ilog(L_MAIN, "Error opening pid file %s", filename); | |
526 | } | |
527 | } | |
528 | ||
529 | /* | |
530 | * check_pidfile | |
531 | * | |
532 | * inputs - filename+path of pid file | |
533 | * output - none | |
534 | * side effects - reads pid from pidfile and checks if ircd is in process | |
535 | * list. if it is, gracefully exits | |
536 | * -kre | |
537 | */ | |
538 | static void | |
539 | check_pidfile(const char *filename) | |
540 | { | |
541 | FILE *fb; | |
542 | char buff[32]; | |
543 | pid_t pidfromfile; | |
544 | ||
545 | /* Don't do logging here, since we don't have log() initialised */ | |
546 | if((fb = fopen(filename, "r"))) | |
547 | { | |
548 | if(fgets(buff, 20, fb) != NULL) | |
549 | { | |
550 | pidfromfile = atoi(buff); | |
538d2089 | 551 | if(!rb_kill(pidfromfile, 0)) |
212380e3 AC |
552 | { |
553 | printf("ircd: daemon is already running\n"); | |
554 | exit(-1); | |
555 | } | |
556 | } | |
557 | fclose(fb); | |
558 | } | |
559 | } | |
560 | ||
561 | /* | |
562 | * setup_corefile | |
563 | * | |
564 | * inputs - nothing | |
565 | * output - nothing | |
566 | * side effects - setups corefile to system limits. | |
567 | * -kre | |
568 | */ | |
569 | static void | |
570 | setup_corefile(void) | |
571 | { | |
572 | #ifdef HAVE_SYS_RESOURCE_H | |
573 | struct rlimit rlim; /* resource limits */ | |
574 | ||
575 | /* Set corefilesize to maximum */ | |
576 | if(!getrlimit(RLIMIT_CORE, &rlim)) | |
577 | { | |
578 | rlim.rlim_cur = rlim.rlim_max; | |
579 | setrlimit(RLIMIT_CORE, &rlim); | |
580 | } | |
581 | #endif | |
582 | } | |
583 | ||
6fce54ff VY |
584 | static void |
585 | ircd_log_cb(const char *str) | |
586 | { | |
fe037171 | 587 | ilog(L_MAIN, "librb reports: %s", str); |
6fce54ff VY |
588 | } |
589 | ||
590 | static void | |
591 | ircd_restart_cb(const char *str) | |
592 | { | |
fe037171 | 593 | inotice("librb has called the restart callback: %s", str); |
6fce54ff VY |
594 | restart(str); |
595 | } | |
596 | ||
597 | /* | |
598 | * Why EXIT_FAILURE here? | |
599 | * Because if ircd_die_cb() is called it's because of a fatal | |
600 | * error inside libcharybdis, and we don't know how to handle the | |
601 | * exception, so it is logical to return a FAILURE exit code here. | |
602 | * --nenolod | |
603 | */ | |
604 | static void | |
605 | ircd_die_cb(const char *str) | |
606 | { | |
607 | if(str != NULL) | |
608 | { | |
609 | /* Try to get the message out to currently logged in operators. */ | |
fe037171 EM |
610 | sendto_realops_snomask(SNO_GENERAL, L_NETWIDE, "librb has called the die callback..aborting: %s", str); |
611 | inotice("librb has called the die callback..aborting: %s", str); | |
6fce54ff | 612 | } |
6643434b | 613 | else |
fe037171 | 614 | inotice("librb has called the die callback..aborting"); |
6fce54ff VY |
615 | |
616 | unlink(pidFileName); | |
617 | exit(EXIT_FAILURE); | |
618 | } | |
619 | ||
fa832850 AC |
620 | struct ev_entry *check_splitmode_ev = NULL; |
621 | ||
d80645d0 VY |
622 | static int |
623 | seed_with_urandom(void) | |
624 | { | |
625 | unsigned int seed; | |
626 | int fd; | |
627 | ||
628 | fd = open("/dev/urandom", O_RDONLY); | |
629 | if(fd >= 0) | |
630 | { | |
631 | if(read(fd, &seed, sizeof(seed)) == sizeof(seed)) | |
632 | { | |
633 | close(fd); | |
634 | srand(seed); | |
635 | return 1; | |
636 | } | |
5457b102 | 637 | close(fd); |
d80645d0 VY |
638 | } |
639 | return 0; | |
640 | } | |
641 | ||
642 | static void | |
643 | seed_with_clock(void) | |
644 | { | |
55abcbb2 | 645 | const struct timeval *tv; |
d80645d0 VY |
646 | rb_set_time(); |
647 | tv = rb_current_time_tv(); | |
648 | srand(tv->tv_sec ^ (tv->tv_usec | (getpid() << 20))); | |
649 | } | |
650 | ||
651 | static void | |
652 | seed_random(void *unused) | |
653 | { | |
654 | unsigned int seed; | |
655 | if(rb_get_random(&seed, sizeof(seed)) == -1) | |
656 | { | |
657 | if(!seed_with_urandom()) | |
658 | seed_with_clock(); | |
659 | return; | |
660 | } | |
661 | srand(seed); | |
662 | } | |
663 | ||
212380e3 AC |
664 | /* |
665 | * main | |
666 | * | |
667 | * Initializes the IRCd. | |
668 | * | |
669 | * Inputs - number of commandline args, args themselves | |
670 | * Outputs - none | |
671 | * Side Effects - this is where the ircd gets going right now | |
672 | */ | |
673 | int | |
5adde7a4 | 674 | charybdis_main(int argc, char * const argv[]) |
212380e3 AC |
675 | { |
676 | int fd; | |
677 | ||
1ba9eba5 | 678 | #ifndef _WIN32 |
212380e3 AC |
679 | /* Check to see if the user is running us as root, which is a nono */ |
680 | if(geteuid() == 0) | |
681 | { | |
682 | fprintf(stderr, "Don't run ircd as root!!!\n"); | |
683 | return -1; | |
684 | } | |
1ba9eba5 | 685 | #endif |
212380e3 | 686 | |
94555087 | 687 | #ifdef _WIN32 |
d6c17e5d | 688 | relocate_paths(); |
94555087 | 689 | #endif |
adc7be98 | 690 | |
2f545aad AC |
691 | logFileName = ircd_paths[IRCD_PATH_IRCD_LOG]; |
692 | pidFileName = ircd_paths[IRCD_PATH_IRCD_PID]; | |
693 | ||
4d8cfacd AC |
694 | ConfigFileEntry.dpath = ircd_paths[IRCD_PATH_PREFIX]; |
695 | ConfigFileEntry.configfile = ircd_paths[IRCD_PATH_IRCD_CONF]; /* Server configuration file */ | |
befa24c8 | 696 | ConfigFileEntry.connect_timeout = 30; /* Default to 30 */ |
346fba92 | 697 | |
d6c17e5d AC |
698 | init_sys(); |
699 | ||
befa24c8 | 700 | umask(077); /* better safe than sorry --SRB */ |
adc7be98 VY |
701 | |
702 | myargv = argv; | |
703 | parseargs(&argc, &argv, myopts); | |
704 | ||
705 | if(chdir(ConfigFileEntry.dpath)) | |
706 | { | |
707 | fprintf(stderr, "Unable to chdir to %s: %s\n", ConfigFileEntry.dpath, strerror(errno)); | |
708 | exit(EXIT_FAILURE); | |
709 | } | |
710 | ||
3d3d1a95 VY |
711 | rb_set_time(); |
712 | ||
212380e3 AC |
713 | /* |
714 | * Setup corefile size immediately after boot -kre | |
715 | */ | |
716 | setup_corefile(); | |
717 | ||
b02a913b AC |
718 | /* initialise operhash fairly early. */ |
719 | init_operhash(); | |
720 | ||
212380e3 AC |
721 | memset(&me, 0, sizeof(me)); |
722 | memset(&meLocalUser, 0, sizeof(meLocalUser)); | |
723 | me.localClient = &meLocalUser; | |
724 | ||
725 | /* Make sure all lists are zeroed */ | |
726 | memset(&unknown_list, 0, sizeof(unknown_list)); | |
727 | memset(&lclient_list, 0, sizeof(lclient_list)); | |
728 | memset(&serv_list, 0, sizeof(serv_list)); | |
729 | memset(&global_serv_list, 0, sizeof(global_serv_list)); | |
730 | memset(&local_oper_list, 0, sizeof(local_oper_list)); | |
731 | memset(&oper_list, 0, sizeof(oper_list)); | |
732 | ||
330fc5c1 | 733 | rb_dlinkAddTail(&me, &me.node, &global_client_list); |
212380e3 | 734 | |
2af8c7ff VY |
735 | memset(&Count, 0, sizeof(Count)); |
736 | memset(&ServerInfo, 0, sizeof(ServerInfo)); | |
737 | memset(&AdminInfo, 0, sizeof(AdminInfo)); | |
738 | memset(&ServerStats, 0, sizeof(struct ServerStatistics)); | |
212380e3 | 739 | |
212380e3 AC |
740 | if(printVersion) |
741 | { | |
b3803848 | 742 | printf("ircd: version %s(%s)\n", ircd_version, serno); |
8bd1c8a1 AC |
743 | #ifdef CUSTOM_BRANDING |
744 | printf("ircd: based on %s-%s\n", PACKAGE_NAME, PACKAGE_VERSION); | |
745 | #endif | |
d2b6b9f3 | 746 | printf("ircd: %s\n", rb_lib_version()); |
212380e3 AC |
747 | exit(EXIT_SUCCESS); |
748 | } | |
749 | ||
212380e3 AC |
750 | setup_signals(); |
751 | ||
212380e3 | 752 | if (testing_conf) |
503727d1 | 753 | server_state_foreground = true; |
212380e3 | 754 | |
09f528c2 | 755 | #ifndef _WIN32 |
212380e3 AC |
756 | /* Make sure fd 0, 1 and 2 are in use -- jilles */ |
757 | do | |
758 | { | |
759 | fd = open("/dev/null", O_RDWR); | |
760 | } while (fd < 2 && fd != -1); | |
761 | if (fd > 2) | |
762 | close(fd); | |
763 | else if (fd == -1) | |
764 | exit(1); | |
09f528c2 | 765 | #endif |
212380e3 AC |
766 | |
767 | /* Check if there is pidfile and daemon already running */ | |
768 | if(!testing_conf) | |
769 | { | |
770 | check_pidfile(pidFileName); | |
771 | ||
772 | if(!server_state_foreground) | |
773 | make_daemon(); | |
774 | inotice("starting %s ...", ircd_version); | |
31c86ea8 | 775 | inotice("%s", rb_lib_version()); |
212380e3 AC |
776 | } |
777 | ||
778 | /* Init the event subsystem */ | |
0edb29fa | 779 | rb_lib_init(ircd_log_cb, ircd_restart_cb, ircd_die_cb, !server_state_foreground, maxconnections, DNODE_HEAP_SIZE, FD_HEAP_SIZE); |
150f35f7 | 780 | rb_linebuf_init(LINEBUF_HEAP_SIZE); |
212380e3 | 781 | |
e861902f | 782 | rb_init_prng(NULL, RB_PRNG_DEFAULT); |
37cd159e | 783 | |
e1db84d8 VY |
784 | seed_random(NULL); |
785 | ||
346fba92 AC |
786 | init_builtin_capabs(); |
787 | default_server_capabs = CAP_MASK; | |
788 | ||
212380e3 | 789 | init_main_logfile(); |
212380e3 AC |
790 | newconf_init(); |
791 | init_s_conf(); | |
792 | init_s_newconf(); | |
793 | init_hash(); | |
794 | clear_scache_hash_table(); /* server cache name table */ | |
795 | init_host_hash(); | |
796 | clear_hash_parse(); | |
797 | init_client(); | |
212380e3 AC |
798 | init_hook(); |
799 | init_channels(); | |
800 | initclass(); | |
b47f8a4f | 801 | whowas_init(); |
212380e3 AC |
802 | init_reject(); |
803 | init_cache(); | |
804 | init_monitor(); | |
030cdce7 | 805 | |
19716b9f | 806 | construct_cflags_strings(); |
030cdce7 | 807 | |
6d063f05 | 808 | init_authd(); /* Start up authd. */ |
068c6c4a | 809 | init_dns(); /* Start up DNS query system */ |
92dad483 | 810 | init_modules(); /* Start up modules system */ |
8a26cd19 | 811 | |
39a68b53 | 812 | privilegeset_set_new("default", "", 0); |
212380e3 AC |
813 | |
814 | if (testing_conf) | |
815 | fprintf(stderr, "\nBeginning config test\n"); | |
ea111ea5 | 816 | read_conf_files(true); /* cold start init conf files */ |
212380e3 | 817 | |
a19097ba EM |
818 | load_all_modules(1); |
819 | load_core_modules(1); | |
212380e3 | 820 | |
b583faf9 AC |
821 | init_isupport(); |
822 | ||
6eebc373 | 823 | init_bandb(); |
c6d72037 | 824 | init_ssld(); |
c53ca1e0 | 825 | init_wsockd(); |
c6d72037 | 826 | |
ab31d2b0 | 827 | rehash_bans(); |
80c9ac51 | 828 | |
212380e3 AC |
829 | initialize_server_capabs(); /* Set up default_server_capabs */ |
830 | initialize_global_set_options(); | |
831 | ||
832 | if(ServerInfo.name == NULL) | |
833 | { | |
834 | ierror("no server name specified in serverinfo block."); | |
835 | return -1; | |
836 | } | |
f427c8b0 | 837 | rb_strlcpy(me.name, ServerInfo.name, sizeof(me.name)); |
212380e3 AC |
838 | |
839 | if(ServerInfo.sid[0] == '\0') | |
840 | { | |
841 | ierror("no server sid specified in serverinfo block."); | |
842 | return -2; | |
843 | } | |
0982871a | 844 | rb_strlcpy(me.id, ServerInfo.sid, sizeof(me.id)); |
212380e3 AC |
845 | init_uid(); |
846 | ||
847 | /* serverinfo{} description must exist. If not, error out. */ | |
848 | if(ServerInfo.description == NULL) | |
849 | { | |
850 | ierror("no server description specified in serverinfo block."); | |
851 | return -3; | |
852 | } | |
f427c8b0 | 853 | rb_strlcpy(me.info, ServerInfo.description, sizeof(me.info)); |
212380e3 | 854 | |
f5960b83 | 855 | if(ServerInfo.ssl_cert != NULL) |
8bd5767b JT |
856 | { |
857 | /* just do the rb_setup_ssl_server to validate the config */ | |
c1725bda | 858 | if(!rb_setup_ssl_server(ServerInfo.ssl_cert, ServerInfo.ssl_private_key, ServerInfo.ssl_dh_params, ServerInfo.ssl_cipher_list)) |
8bd5767b JT |
859 | { |
860 | ilog(L_MAIN, "WARNING: Unable to setup SSL."); | |
bfc44622 | 861 | ircd_ssl_ok = false; |
8bd5767b JT |
862 | } |
863 | else | |
bfc44622 | 864 | ircd_ssl_ok = true; |
c6d72037 VY |
865 | } |
866 | ||
212380e3 AC |
867 | if (testing_conf) |
868 | { | |
869 | fprintf(stderr, "\nConfig testing complete.\n"); | |
870 | fflush(stderr); | |
871 | return 0; /* Why? We want the launcher to exit out. */ | |
872 | } | |
873 | ||
874 | me.from = &me; | |
875 | me.servptr = &me; | |
876 | SetMe(&me); | |
877 | make_server(&me); | |
e3354945 | 878 | startup_time = rb_current_time(); |
212380e3 AC |
879 | add_to_client_hash(me.name, &me); |
880 | add_to_id_hash(me.id, &me); | |
994544c2 | 881 | me.serv->nameinfo = scache_connect(me.name, me.info, 0); |
212380e3 | 882 | |
330fc5c1 | 883 | rb_dlinkAddAlloc(&me, &global_serv_list); |
212380e3 AC |
884 | |
885 | construct_umodebuf(); | |
886 | ||
887 | check_class(); | |
888 | write_pidfile(pidFileName); | |
889 | load_help(); | |
890 | open_logfiles(); | |
891 | ||
50808796 EM |
892 | configure_authd(); |
893 | ||
212380e3 AC |
894 | ilog(L_MAIN, "Server Ready"); |
895 | ||
212380e3 AC |
896 | /* We want try_connections to be called as soon as possible now! -- adrian */ |
897 | /* No, 'cause after a restart it would cause all sorts of nick collides */ | |
898 | /* um. by waiting even longer, that just means we have even *more* | |
899 | * nick collisions. what a stupid idea. set an event for the IO loop --fl | |
900 | */ | |
12aea5fe | 901 | rb_event_addish("try_connections", try_connections, NULL, STARTUP_CONNECTIONS_TIME); |
36645728 VY |
902 | rb_event_addonce("try_connections_startup", try_connections, NULL, 2); |
903 | rb_event_add("check_rehash", check_rehash, NULL, 3); | |
d80645d0 | 904 | rb_event_addish("reseed_srand", seed_random, NULL, 300); /* reseed every 10 minutes */ |
212380e3 | 905 | |
212380e3 | 906 | if(splitmode) |
36645728 | 907 | check_splitmode_ev = rb_event_add("check_splitmode", check_splitmode, NULL, 5); |
212380e3 | 908 | |
212380e3 AC |
909 | print_startup(getpid()); |
910 | ||
39944e1f | 911 | rb_lib_loop(0); |
212380e3 AC |
912 | |
913 | return 0; | |
914 | } |