1 |
/* |
2 |
* ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd) |
3 |
* |
4 |
* Copyright (c) 1997-2021 ircd-hybrid development team |
5 |
* |
6 |
* This program is free software; you can redistribute it and/or modify |
7 |
* it under the terms of the GNU General Public License as published by |
8 |
* the Free Software Foundation; either version 2 of the License, or |
9 |
* (at your option) any later version. |
10 |
* |
11 |
* This program is distributed in the hope that it will be useful, |
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
* GNU General Public License for more details. |
15 |
* |
16 |
* You should have received a copy of the GNU General Public License |
17 |
* along with this program; if not, write to the Free Software |
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 |
19 |
* USA |
20 |
*/ |
21 |
|
22 |
/*! \file ircd.c |
23 |
* \brief Starts up and runs the ircd. |
24 |
* \version $Id$ |
25 |
*/ |
26 |
|
27 |
#include "stdinc.h" |
28 |
#include "user.h" |
29 |
#include "list.h" |
30 |
#include "ircd.h" |
31 |
#include "channel.h" |
32 |
#include "channel_mode.h" |
33 |
#include "client.h" |
34 |
#include "event.h" |
35 |
#include "fdlist.h" |
36 |
#include "hash.h" |
37 |
#include "id.h" |
38 |
#include "irc_string.h" |
39 |
#include "ircd_signal.h" |
40 |
#include "motd.h" |
41 |
#include "conf.h" |
42 |
#include "hostmask.h" |
43 |
#include "parse.h" |
44 |
#include "res.h" |
45 |
#include "restart.h" |
46 |
#include "rng_mt.h" |
47 |
#include "s_bsd.h" |
48 |
#include "log.h" |
49 |
#include "server.h" |
50 |
#include "server_capab.h" |
51 |
#include "send.h" |
52 |
#include "modules.h" |
53 |
#include "ircd_getopt.h" |
54 |
#include "conf_db.h" |
55 |
#include "conf_class.h" |
56 |
#include "ipcache.h" |
57 |
#include "isupport.h" |
58 |
#include "patchlevel.h" |
59 |
#include "extban.h" |
60 |
|
61 |
|
62 |
struct SetOptions GlobalSetOptions; /* /quote set variables */ |
63 |
struct Counter Count; |
64 |
struct ServerState_t server_state; |
65 |
struct ServerStatistics ServerStats; |
66 |
struct Connection meConnection; /* That's also part of me */ |
67 |
struct Client me = { .connection = &meConnection }; /* That's me */ |
68 |
|
69 |
char **myargv; |
70 |
const char *logFileName = LPATH; |
71 |
const char *pidFileName = PPATH; |
72 |
|
73 |
bool dorehash; |
74 |
bool doremotd; |
75 |
|
76 |
static bool printVersion; |
77 |
|
78 |
static struct lgetopt myopts[] = |
79 |
{ |
80 |
{ "configfile", &ConfigGeneral.configfile, |
81 |
STRING, "File to use for ircd.conf" }, |
82 |
{ "klinefile", &ConfigGeneral.klinefile, |
83 |
STRING, "File to use for kline database" }, |
84 |
{ "dlinefile", &ConfigGeneral.dlinefile, |
85 |
STRING, "File to use for dline database" }, |
86 |
{ "xlinefile", &ConfigGeneral.xlinefile, |
87 |
STRING, "File to use for xline database" }, |
88 |
{ "resvfile", &ConfigGeneral.resvfile, |
89 |
STRING, "File to use for resv database" }, |
90 |
{ "logfile", &logFileName, |
91 |
STRING, "File to use for ircd.log" }, |
92 |
{ "pidfile", &pidFileName, |
93 |
STRING, "File to use for process ID" }, |
94 |
{ "foreground", &server_state.foreground, |
95 |
BOOLEAN, "Run in foreground (don't detach)" }, |
96 |
{ "version", &printVersion, |
97 |
BOOLEAN, "Print version and exit" }, |
98 |
{ "help", NULL, USAGE, "Print this text" }, |
99 |
{ NULL, NULL, STRING, NULL }, |
100 |
}; |
101 |
|
102 |
static struct event event_cleanup_tklines = |
103 |
{ |
104 |
.name = "cleanup_tklines", |
105 |
.handler = cleanup_tklines, |
106 |
.when = CLEANUP_TKLINES_TIME |
107 |
}; |
108 |
|
109 |
static struct event event_try_connections = |
110 |
{ |
111 |
.name = "try_connections", |
112 |
.handler = try_connections, |
113 |
.when = 15 |
114 |
}; |
115 |
|
116 |
static struct event event_comm_checktimeouts = |
117 |
{ |
118 |
.name = "comm_checktimeouts", |
119 |
.handler = comm_checktimeouts, |
120 |
.when = 1 |
121 |
}; |
122 |
|
123 |
static struct event event_save_all_databases = |
124 |
{ |
125 |
.name = "save_all_databases", |
126 |
.handler = save_all_databases, |
127 |
.when = DATABASE_UPDATE_TIMEOUT |
128 |
}; |
129 |
|
130 |
struct event event_write_links_file = |
131 |
{ |
132 |
.name = "write_links_file", |
133 |
.handler = write_links_file, |
134 |
}; |
135 |
|
136 |
|
137 |
static void |
138 |
io_loop(void) |
139 |
{ |
140 |
while (true) |
141 |
{ |
142 |
if (listing_client_list.head) |
143 |
{ |
144 |
dlink_node *node = NULL, *node_next = NULL; |
145 |
DLINK_FOREACH_SAFE(node, node_next, listing_client_list.head) |
146 |
safe_list_channels(node->data, false); |
147 |
} |
148 |
|
149 |
/* Run pending events */ |
150 |
event_run(); |
151 |
|
152 |
comm_select(); |
153 |
exit_aborted_clients(); |
154 |
free_exited_clients(); |
155 |
|
156 |
/* Check to see whether we have to rehash the configuration. */ |
157 |
if (dorehash == true) |
158 |
{ |
159 |
conf_rehash(true); |
160 |
dorehash = false; |
161 |
} |
162 |
|
163 |
if (doremotd == true) |
164 |
{ |
165 |
motd_recache(); |
166 |
sendto_realops_flags(UMODE_SERVNOTICE, L_ALL, SEND_NOTICE, |
167 |
"Got signal SIGUSR1, reloading motd file(s)"); |
168 |
doremotd = false; |
169 |
} |
170 |
} |
171 |
} |
172 |
|
173 |
/* initalialize_global_set_options() |
174 |
* |
175 |
* inputs - none |
176 |
* output - none |
177 |
* side effects - This sets all global set options needed |
178 |
*/ |
179 |
static void |
180 |
initialize_global_set_options(void) |
181 |
{ |
182 |
GlobalSetOptions.maxclients = ConfigServerInfo.default_max_clients; |
183 |
GlobalSetOptions.autoconn = true; |
184 |
GlobalSetOptions.spam_time = MIN_JOIN_LEAVE_TIME; |
185 |
GlobalSetOptions.spam_num = MAX_JOIN_LEAVE_COUNT; |
186 |
GlobalSetOptions.floodcount = ConfigGeneral.default_floodcount; |
187 |
GlobalSetOptions.floodtime = ConfigGeneral.default_floodtime; |
188 |
GlobalSetOptions.joinfloodcount = ConfigChannel.default_join_flood_count; |
189 |
GlobalSetOptions.joinfloodtime = ConfigChannel.default_join_flood_time; |
190 |
} |
191 |
|
192 |
/* write_pidfile() |
193 |
* |
194 |
* inputs - filename+path of pid file |
195 |
* output - NONE |
196 |
* side effects - write the pid of the ircd to filename |
197 |
*/ |
198 |
static void |
199 |
write_pidfile(const char *filename) |
200 |
{ |
201 |
FILE *fb; |
202 |
|
203 |
if ((fb = fopen(filename, "w"))) |
204 |
{ |
205 |
char buf[IRCD_BUFSIZE]; |
206 |
unsigned int pid = (unsigned int)getpid(); |
207 |
|
208 |
snprintf(buf, sizeof(buf), "%u\n", pid); |
209 |
|
210 |
if (fputs(buf, fb) == -1) |
211 |
ilog(LOG_TYPE_IRCD, "Error writing to pid file %s: %s", |
212 |
filename, strerror(errno)); |
213 |
|
214 |
fclose(fb); |
215 |
} |
216 |
else |
217 |
ilog(LOG_TYPE_IRCD, "Error opening pid file %s: %s", |
218 |
filename, strerror(errno)); |
219 |
} |
220 |
|
221 |
/* check_pidfile() |
222 |
* |
223 |
* inputs - filename+path of pid file |
224 |
* output - none |
225 |
* side effects - reads pid from pidfile and checks if ircd is in process |
226 |
* list. if it is, gracefully exits |
227 |
* -kre |
228 |
*/ |
229 |
static void |
230 |
check_pidfile(const char *filename) |
231 |
{ |
232 |
FILE *fb; |
233 |
char buf[IRCD_BUFSIZE]; |
234 |
|
235 |
if ((fb = fopen(filename, "r"))) |
236 |
{ |
237 |
if (fgets(buf, 20, fb) == NULL) |
238 |
ilog(LOG_TYPE_IRCD, "Error reading from pid file %s: %s", |
239 |
filename, strerror(errno)); |
240 |
else |
241 |
{ |
242 |
pid_t pid = atoi(buf); |
243 |
|
244 |
if (kill(pid, 0) == 0) |
245 |
{ |
246 |
/* log(L_ERROR, "Server is already running"); */ |
247 |
printf("ircd: daemon is already running\n"); |
248 |
exit(EXIT_FAILURE); |
249 |
} |
250 |
} |
251 |
|
252 |
fclose(fb); |
253 |
} |
254 |
else if (errno != ENOENT) |
255 |
ilog(LOG_TYPE_IRCD, "Error opening pid file %s: %s", |
256 |
filename, strerror(errno)); |
257 |
} |
258 |
|
259 |
/* setup_corefile() |
260 |
* |
261 |
* inputs - nothing |
262 |
* output - nothing |
263 |
* side effects - setups corefile to system limits. |
264 |
* -kre |
265 |
*/ |
266 |
static void |
267 |
setup_corefile(void) |
268 |
{ |
269 |
struct rlimit rlim; /* resource limits */ |
270 |
|
271 |
/* Set corefilesize to maximum */ |
272 |
if (getrlimit(RLIMIT_CORE, &rlim) == 0) |
273 |
{ |
274 |
rlim.rlim_cur = rlim.rlim_max; |
275 |
setrlimit(RLIMIT_CORE, &rlim); |
276 |
} |
277 |
} |
278 |
|
279 |
static void |
280 |
setup_fdlimit(void) |
281 |
{ |
282 |
struct rlimit rlim; /* resource limits */ |
283 |
|
284 |
if (getrlimit(RLIMIT_NOFILE, &rlim)) |
285 |
{ |
286 |
fprintf(stderr, "getrlimit: couldn't get maximum number of file descriptors: %s\n", |
287 |
strerror(errno)); |
288 |
exit(EXIT_FAILURE); |
289 |
} |
290 |
|
291 |
#ifdef __APPLE__ |
292 |
if (rlim.rlim_max > OPEN_MAX) |
293 |
rlim.rlim_max = OPEN_MAX; |
294 |
#endif |
295 |
if (rlim.rlim_max > 0xFFFF) |
296 |
rlim.rlim_max = 0xFFFF; |
297 |
rlim.rlim_cur = rlim.rlim_max; |
298 |
|
299 |
if (setrlimit(RLIMIT_NOFILE, &rlim) == 0) |
300 |
hard_fdlimit = rlim.rlim_cur; |
301 |
else |
302 |
{ |
303 |
fprintf(stderr, "setrlimit: couldn't set maximum number of file descriptors: %s\n", |
304 |
strerror(errno)); |
305 |
exit(EXIT_FAILURE); |
306 |
} |
307 |
} |
308 |
|
309 |
/* |
310 |
* print_startup - print startup information |
311 |
*/ |
312 |
static void |
313 |
print_startup(int pid) |
314 |
{ |
315 |
printf("ircd: version %s\n", PATCHLEVEL); |
316 |
printf("ircd: pid %d\n", pid); |
317 |
printf("ircd: running in %s mode from %s\n", !server_state.foreground ? "background" |
318 |
: "foreground", ConfigGeneral.dpath); |
319 |
} |
320 |
|
321 |
static void |
322 |
make_daemon(void) |
323 |
{ |
324 |
int pid = fork(); |
325 |
|
326 |
if (pid < 0) |
327 |
{ |
328 |
perror("fork"); |
329 |
exit(EXIT_FAILURE); |
330 |
} |
331 |
else if (pid > 0) |
332 |
{ |
333 |
print_startup(pid); |
334 |
exit(EXIT_SUCCESS); |
335 |
} |
336 |
|
337 |
setsid(); |
338 |
} |
339 |
|
340 |
int |
341 |
main(int argc, char *argv[]) |
342 |
{ |
343 |
/* Check to see if the user is running us as root, which is a nono */ |
344 |
if (geteuid() == 0) |
345 |
{ |
346 |
fprintf(stderr, "ERROR: This server won't run as root/superuser\n"); |
347 |
return -1; |
348 |
} |
349 |
|
350 |
/* Setup corefile size immediately after boot -kre */ |
351 |
setup_corefile(); |
352 |
|
353 |
setup_fdlimit(); |
354 |
|
355 |
/* Save server boot time right away, so getrusage works correctly */ |
356 |
event_time_set(); |
357 |
|
358 |
/* It's not random, but it ought to be a little harder to guess */ |
359 |
uint32_t seed = (uint32_t)(event_base->time.sec_real ^ |
360 |
(event_base->time.sec_monotonic | (getpid() << 16))); |
361 |
init_genrand(seed); |
362 |
|
363 |
ConfigGeneral.dpath = DPATH; |
364 |
ConfigGeneral.spath = SPATH; |
365 |
ConfigGeneral.mpath = MPATH; |
366 |
ConfigGeneral.configfile = CPATH; /* Server configuration file */ |
367 |
ConfigGeneral.klinefile = KPATH; /* Server kline file */ |
368 |
ConfigGeneral.xlinefile = XPATH; /* Server xline file */ |
369 |
ConfigGeneral.dlinefile = DLPATH; /* dline file */ |
370 |
ConfigGeneral.resvfile = RESVPATH; /* resv file */ |
371 |
|
372 |
myargv = argv; |
373 |
umask(077); /* umask 077: u=rwx,g=,o= */ |
374 |
|
375 |
parseargs(&argc, &argv, myopts); |
376 |
|
377 |
if (printVersion == true) |
378 |
{ |
379 |
printf("ircd: version %s\n", PATCHLEVEL); |
380 |
exit(EXIT_SUCCESS); |
381 |
} |
382 |
|
383 |
if (chdir(ConfigGeneral.dpath)) |
384 |
{ |
385 |
perror("chdir"); |
386 |
exit(EXIT_FAILURE); |
387 |
} |
388 |
|
389 |
if (server_state.foreground == false) |
390 |
{ |
391 |
make_daemon(); |
392 |
close_standard_fds(); /* this needs to be before comm_select_init()! */ |
393 |
} |
394 |
else |
395 |
print_startup(getpid()); |
396 |
|
397 |
setup_signals(); |
398 |
|
399 |
/* We need this to initialise the fd array before anything else */ |
400 |
fdlist_init(); |
401 |
log_set_file(LOG_TYPE_IRCD, 0, logFileName); |
402 |
|
403 |
comm_select_init(); /* This needs to be setup early ! -- adrian */ |
404 |
tls_init(); |
405 |
|
406 |
/* Check if there is pidfile and daemon already running */ |
407 |
check_pidfile(pidFileName); |
408 |
|
409 |
isupport_init(); |
410 |
ipcache_init(); |
411 |
client_init(); |
412 |
class_init(); |
413 |
resolver_init(); /* Needs to be setup before the io loop */ |
414 |
modules_init(); |
415 |
conf_read_files(true); /* cold start init conf files */ |
416 |
capab_init(); /* Set up default_server_capabs */ |
417 |
initialize_global_set_options(); /* Has to be called after conf_read_files() */ |
418 |
channel_mode_init(); |
419 |
extban_init(); |
420 |
read_links_file(); |
421 |
motd_init(); |
422 |
user_modes_init(); |
423 |
|
424 |
if (EmptyString(ConfigServerInfo.name)) |
425 |
{ |
426 |
ilog(LOG_TYPE_IRCD, "ERROR: No server name specified in serverinfo block."); |
427 |
exit(EXIT_FAILURE); |
428 |
} |
429 |
|
430 |
strlcpy(me.name, ConfigServerInfo.name, sizeof(me.name)); |
431 |
|
432 |
/* serverinfo {} description must exist. If not, error out.*/ |
433 |
if (EmptyString(ConfigServerInfo.description)) |
434 |
{ |
435 |
ilog(LOG_TYPE_IRCD, "ERROR: No server description specified in serverinfo block."); |
436 |
exit(EXIT_FAILURE); |
437 |
} |
438 |
|
439 |
strlcpy(me.info, ConfigServerInfo.description, sizeof(me.info)); |
440 |
|
441 |
if (EmptyString(ConfigServerInfo.sid)) |
442 |
{ |
443 |
ilog(LOG_TYPE_IRCD, "Generating server ID"); |
444 |
generate_sid(); |
445 |
} |
446 |
else |
447 |
strlcpy(me.id, ConfigServerInfo.sid, sizeof(me.id)); |
448 |
|
449 |
init_uid(); |
450 |
|
451 |
me.from = &me; |
452 |
me.servptr = &me; |
453 |
me.connection->created_real = event_base->time.sec_real; |
454 |
me.connection->created_monotonic = event_base->time.sec_monotonic; |
455 |
|
456 |
SetMe(&me); |
457 |
server_make(&me); |
458 |
|
459 |
hash_add_id(&me); |
460 |
hash_add_client(&me); |
461 |
|
462 |
dlinkAdd(&me, &me.node, &global_server_list); |
463 |
|
464 |
load_kline_database(ConfigGeneral.klinefile); |
465 |
load_dline_database(ConfigGeneral.dlinefile); |
466 |
load_xline_database(ConfigGeneral.xlinefile); |
467 |
load_resv_database(ConfigGeneral.resvfile); |
468 |
|
469 |
load_all_modules(true); |
470 |
load_conf_modules(); |
471 |
load_core_modules(true); |
472 |
|
473 |
write_pidfile(pidFileName); |
474 |
|
475 |
event_addish(&event_cleanup_tklines, NULL); |
476 |
|
477 |
/* We want try_connections to be called as soon as possible now! -- adrian */ |
478 |
/* No, 'cause after a restart it would cause all sorts of nick collides */ |
479 |
event_addish(&event_try_connections, NULL); |
480 |
|
481 |
/* Setup the timeout check. I'll shift it later :) -- adrian */ |
482 |
event_add(&event_comm_checktimeouts, NULL); |
483 |
|
484 |
event_addish(&event_save_all_databases, NULL); |
485 |
|
486 |
if (ConfigServerHide.flatten_links_delay && event_write_links_file.active == false) |
487 |
{ |
488 |
event_write_links_file.when = ConfigServerHide.flatten_links_delay; |
489 |
event_add(&event_write_links_file, NULL); |
490 |
} |
491 |
|
492 |
ilog(LOG_TYPE_IRCD, "Server ready. Running version: %s", PATCHLEVEL); |
493 |
io_loop(); |
494 |
|
495 |
return 0; |
496 |
} |