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