1 |
/* |
2 |
* ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd) |
3 |
* |
4 |
* Copyright (c) 1997-2017 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 "auth.h" |
48 |
#include "s_bsd.h" |
49 |
#include "log.h" |
50 |
#include "server.h" |
51 |
#include "send.h" |
52 |
#include "whowas.h" |
53 |
#include "modules.h" |
54 |
#include "memory.h" |
55 |
#include "mempool.h" |
56 |
#include "ircd_getopt.h" |
57 |
#include "watch.h" |
58 |
#include "conf_db.h" |
59 |
#include "conf_class.h" |
60 |
#include "ipcache.h" |
61 |
#include "isupport.h" |
62 |
#include "userhost.h" |
63 |
|
64 |
|
65 |
#ifdef HAVE_LIBGEOIP |
66 |
GeoIP *GeoIPv4_ctx; |
67 |
GeoIP *GeoIPv6_ctx; |
68 |
#endif |
69 |
|
70 |
struct SetOptions GlobalSetOptions; /* /quote set variables */ |
71 |
struct Counter Count; |
72 |
struct ServerState_t server_state; |
73 |
struct ServerStatistics ServerStats; |
74 |
struct ServerTime SystemTime; |
75 |
struct Connection meConnection; /* That's also part of me */ |
76 |
struct Client me = { .connection = &meConnection }; /* That's me */ |
77 |
|
78 |
char **myargv; |
79 |
const char *logFileName = LPATH; |
80 |
const char *pidFileName = PPATH; |
81 |
|
82 |
unsigned int dorehash; |
83 |
unsigned int doremotd; |
84 |
|
85 |
static int printVersion; |
86 |
|
87 |
static struct lgetopt myopts[] = |
88 |
{ |
89 |
{ "configfile", &ConfigGeneral.configfile, |
90 |
STRING, "File to use for ircd.conf" }, |
91 |
{ "klinefile", &ConfigGeneral.klinefile, |
92 |
STRING, "File to use for kline database" }, |
93 |
{ "dlinefile", &ConfigGeneral.dlinefile, |
94 |
STRING, "File to use for dline database" }, |
95 |
{ "xlinefile", &ConfigGeneral.xlinefile, |
96 |
STRING, "File to use for xline database" }, |
97 |
{ "resvfile", &ConfigGeneral.resvfile, |
98 |
STRING, "File to use for resv database" }, |
99 |
{ "logfile", &logFileName, |
100 |
STRING, "File to use for ircd.log" }, |
101 |
{ "pidfile", &pidFileName, |
102 |
STRING, "File to use for process ID" }, |
103 |
{ "foreground", &server_state.foreground, |
104 |
YESNO, "Run in foreground (don't detach)" }, |
105 |
{ "version", &printVersion, |
106 |
YESNO, "Print version and exit" }, |
107 |
{ "help", NULL, USAGE, "Print this text" }, |
108 |
{ NULL, NULL, STRING, NULL }, |
109 |
}; |
110 |
|
111 |
static struct event event_cleanup_tklines = |
112 |
{ |
113 |
.name = "cleanup_tklines", |
114 |
.handler = cleanup_tklines, |
115 |
.when = CLEANUP_TKLINES_TIME |
116 |
}; |
117 |
|
118 |
static struct event event_try_connections = |
119 |
{ |
120 |
.name = "try_connections", |
121 |
.handler = try_connections, |
122 |
.when = STARTUP_CONNECTIONS_TIME |
123 |
}; |
124 |
|
125 |
static struct event event_comm_checktimeouts = |
126 |
{ |
127 |
.name = "comm_checktimeouts", |
128 |
.handler = comm_checktimeouts, |
129 |
.when = 1 |
130 |
}; |
131 |
|
132 |
static struct event event_save_all_databases = |
133 |
{ |
134 |
.name = "save_all_databases", |
135 |
.handler = save_all_databases, |
136 |
.when = DATABASE_UPDATE_TIMEOUT |
137 |
}; |
138 |
|
139 |
struct event event_write_links_file = |
140 |
{ |
141 |
.name = "write_links_file", |
142 |
.handler = write_links_file, |
143 |
}; |
144 |
|
145 |
|
146 |
void |
147 |
set_time(void) |
148 |
{ |
149 |
struct timeval newtime = { .tv_sec = 0, .tv_usec = 0 }; |
150 |
|
151 |
if (gettimeofday(&newtime, NULL) == -1) |
152 |
{ |
153 |
char buf[IRCD_BUFSIZE]; |
154 |
|
155 |
snprintf(buf, sizeof(buf), "Clock failure, TS can be corrupted: %s", |
156 |
strerror(errno)); |
157 |
server_die(buf, SERVER_SHUTDOWN); |
158 |
} |
159 |
|
160 |
if ((uintmax_t)newtime.tv_sec < CurrentTime) |
161 |
{ |
162 |
ilog(LOG_TYPE_IRCD, "System clock is running backwards - (%ju < %ju)", |
163 |
(uintmax_t)newtime.tv_sec, CurrentTime); |
164 |
sendto_realops_flags(UMODE_DEBUG, L_ALL, SEND_NOTICE, |
165 |
"System clock is running backwards - (%ju < %ju)", |
166 |
(uintmax_t)newtime.tv_sec, CurrentTime); |
167 |
event_set_back_events(CurrentTime - (uintmax_t)newtime.tv_sec); |
168 |
} |
169 |
|
170 |
SystemTime.tv_sec = newtime.tv_sec; |
171 |
SystemTime.tv_usec = newtime.tv_usec; |
172 |
} |
173 |
|
174 |
static void |
175 |
io_loop(void) |
176 |
{ |
177 |
while (1) |
178 |
{ |
179 |
if (listing_client_list.head) |
180 |
{ |
181 |
dlink_node *node = NULL, *node_next = NULL; |
182 |
DLINK_FOREACH_SAFE(node, node_next, listing_client_list.head) |
183 |
safe_list_channels(node->data, 0); |
184 |
} |
185 |
|
186 |
/* Run pending events */ |
187 |
event_run(); |
188 |
|
189 |
comm_select(); |
190 |
exit_aborted_clients(); |
191 |
free_exited_clients(); |
192 |
|
193 |
/* Check to see whether we have to rehash the configuration. */ |
194 |
if (dorehash) |
195 |
{ |
196 |
conf_rehash(1); |
197 |
dorehash = 0; |
198 |
} |
199 |
|
200 |
if (doremotd) |
201 |
{ |
202 |
motd_recache(); |
203 |
sendto_realops_flags(UMODE_SERVNOTICE, L_ALL, SEND_NOTICE, |
204 |
"Got signal SIGUSR1, reloading motd file(s)"); |
205 |
doremotd = 0; |
206 |
} |
207 |
} |
208 |
} |
209 |
|
210 |
/* initalialize_global_set_options() |
211 |
* |
212 |
* inputs - none |
213 |
* output - none |
214 |
* side effects - This sets all global set options needed |
215 |
*/ |
216 |
static void |
217 |
initialize_global_set_options(void) |
218 |
{ |
219 |
GlobalSetOptions.maxclients = ConfigServerInfo.default_max_clients; |
220 |
GlobalSetOptions.autoconn = 1; |
221 |
GlobalSetOptions.spam_time = MIN_JOIN_LEAVE_TIME; |
222 |
GlobalSetOptions.spam_num = MAX_JOIN_LEAVE_COUNT; |
223 |
GlobalSetOptions.floodcount = ConfigGeneral.default_floodcount; |
224 |
GlobalSetOptions.floodtime = ConfigGeneral.default_floodtime; |
225 |
GlobalSetOptions.joinfloodcount = ConfigChannel.default_join_flood_count; |
226 |
GlobalSetOptions.joinfloodtime = ConfigChannel.default_join_flood_time; |
227 |
GlobalSetOptions.ident_timeout = IDENT_TIMEOUT; |
228 |
} |
229 |
|
230 |
/* write_pidfile() |
231 |
* |
232 |
* inputs - filename+path of pid file |
233 |
* output - NONE |
234 |
* side effects - write the pid of the ircd to filename |
235 |
*/ |
236 |
static void |
237 |
write_pidfile(const char *filename) |
238 |
{ |
239 |
FILE *fb; |
240 |
|
241 |
if ((fb = fopen(filename, "w"))) |
242 |
{ |
243 |
char buf[IRCD_BUFSIZE]; |
244 |
unsigned int pid = (unsigned int)getpid(); |
245 |
|
246 |
snprintf(buf, sizeof(buf), "%u\n", pid); |
247 |
|
248 |
if (fputs(buf, fb) == -1) |
249 |
ilog(LOG_TYPE_IRCD, "Error writing to pid file %s: %s", |
250 |
filename, strerror(errno)); |
251 |
|
252 |
fclose(fb); |
253 |
} |
254 |
else |
255 |
ilog(LOG_TYPE_IRCD, "Error opening pid file %s: %s", |
256 |
filename, strerror(errno)); |
257 |
} |
258 |
|
259 |
/* check_pidfile() |
260 |
* |
261 |
* inputs - filename+path of pid file |
262 |
* output - none |
263 |
* side effects - reads pid from pidfile and checks if ircd is in process |
264 |
* list. if it is, gracefully exits |
265 |
* -kre |
266 |
*/ |
267 |
static void |
268 |
check_pidfile(const char *filename) |
269 |
{ |
270 |
FILE *fb; |
271 |
char buf[IRCD_BUFSIZE]; |
272 |
|
273 |
if ((fb = fopen(filename, "r"))) |
274 |
{ |
275 |
if (!fgets(buf, 20, fb)) |
276 |
ilog(LOG_TYPE_IRCD, "Error reading from pid file %s: %s", |
277 |
filename, strerror(errno)); |
278 |
else |
279 |
{ |
280 |
pid_t pid = atoi(buf); |
281 |
|
282 |
if (!kill(pid, 0)) |
283 |
{ |
284 |
/* log(L_ERROR, "Server is already running"); */ |
285 |
printf("ircd: daemon is already running\n"); |
286 |
exit(EXIT_FAILURE); |
287 |
} |
288 |
} |
289 |
|
290 |
fclose(fb); |
291 |
} |
292 |
else if (errno != ENOENT) |
293 |
ilog(LOG_TYPE_IRCD, "Error opening pid file %s: %s", |
294 |
filename, strerror(errno)); |
295 |
} |
296 |
|
297 |
/* setup_corefile() |
298 |
* |
299 |
* inputs - nothing |
300 |
* output - nothing |
301 |
* side effects - setups corefile to system limits. |
302 |
* -kre |
303 |
*/ |
304 |
static void |
305 |
setup_corefile(void) |
306 |
{ |
307 |
struct rlimit rlim; /* resource limits */ |
308 |
|
309 |
/* Set corefilesize to maximum */ |
310 |
if (!getrlimit(RLIMIT_CORE, &rlim)) |
311 |
{ |
312 |
rlim.rlim_cur = rlim.rlim_max; |
313 |
setrlimit(RLIMIT_CORE, &rlim); |
314 |
} |
315 |
} |
316 |
|
317 |
/* |
318 |
* print_startup - print startup information |
319 |
*/ |
320 |
static void |
321 |
print_startup(int pid) |
322 |
{ |
323 |
printf("ircd: version %s(%s)\n", ircd_version, serno); |
324 |
printf("ircd: pid %d\n", pid); |
325 |
printf("ircd: running in %s mode from %s\n", !server_state.foreground ? "background" |
326 |
: "foreground", ConfigGeneral.dpath); |
327 |
} |
328 |
|
329 |
static void |
330 |
make_daemon(void) |
331 |
{ |
332 |
int pid; |
333 |
|
334 |
if ((pid = fork()) < 0) |
335 |
{ |
336 |
perror("fork"); |
337 |
exit(EXIT_FAILURE); |
338 |
} |
339 |
else if (pid > 0) |
340 |
{ |
341 |
print_startup(pid); |
342 |
exit(EXIT_SUCCESS); |
343 |
} |
344 |
|
345 |
setsid(); |
346 |
} |
347 |
|
348 |
int |
349 |
main(int argc, char *argv[]) |
350 |
{ |
351 |
/* Check to see if the user is running us as root, which is a nono */ |
352 |
if (!geteuid()) |
353 |
{ |
354 |
fprintf(stderr, "ERROR: This server won't run as root/superuser\n"); |
355 |
return -1; |
356 |
} |
357 |
|
358 |
/* Setup corefile size immediately after boot -kre */ |
359 |
setup_corefile(); |
360 |
|
361 |
/* Save server boot time right away, so getrusage works correctly */ |
362 |
set_time(); |
363 |
|
364 |
/* It's not random, but it ought to be a little harder to guess */ |
365 |
init_genrand(SystemTime.tv_sec ^ (SystemTime.tv_usec | (getpid() << 20))); |
366 |
|
367 |
ConfigGeneral.dpath = DPATH; |
368 |
ConfigGeneral.spath = SPATH; |
369 |
ConfigGeneral.mpath = MPATH; |
370 |
ConfigGeneral.configfile = CPATH; /* Server configuration file */ |
371 |
ConfigGeneral.klinefile = KPATH; /* Server kline file */ |
372 |
ConfigGeneral.xlinefile = XPATH; /* Server xline file */ |
373 |
ConfigGeneral.dlinefile = DLPATH; /* dline file */ |
374 |
ConfigGeneral.resvfile = RESVPATH; /* resv file */ |
375 |
|
376 |
myargv = argv; |
377 |
umask(077); /* umask 077: u=rwx,g=,o= */ |
378 |
|
379 |
parseargs(&argc, &argv, myopts); |
380 |
|
381 |
if (printVersion) |
382 |
{ |
383 |
printf("ircd: version %s(%s)\n", ircd_version, serno); |
384 |
exit(EXIT_SUCCESS); |
385 |
} |
386 |
|
387 |
if (chdir(ConfigGeneral.dpath)) |
388 |
{ |
389 |
perror("chdir"); |
390 |
exit(EXIT_FAILURE); |
391 |
} |
392 |
|
393 |
if (!server_state.foreground) |
394 |
{ |
395 |
make_daemon(); |
396 |
close_standard_fds(); /* this needs to be before netio_init()! */ |
397 |
} |
398 |
else |
399 |
print_startup(getpid()); |
400 |
|
401 |
setup_signals(); |
402 |
|
403 |
/* We need this to initialise the fd array before anything else */ |
404 |
fdlist_init(); |
405 |
log_set_file(LOG_TYPE_IRCD, 0, logFileName); |
406 |
|
407 |
netio_init(); /* This needs to be setup early ! -- adrian */ |
408 |
tls_init(); |
409 |
|
410 |
/* Check if there is pidfile and daemon already running */ |
411 |
check_pidfile(pidFileName); |
412 |
|
413 |
mp_pool_init(); |
414 |
init_dlink_nodes(); |
415 |
isupport_init(); |
416 |
dbuf_init(); |
417 |
hash_init(); |
418 |
userhost_init(); |
419 |
ipcache_init(); |
420 |
client_init(); |
421 |
class_init(); |
422 |
whowas_init(); |
423 |
watch_init(); |
424 |
auth_init(); /* Initialise the auth code */ |
425 |
resolver_init(); /* Needs to be setup before the io loop */ |
426 |
modules_init(); |
427 |
read_conf_files(1); /* cold start init conf files */ |
428 |
server_capab_init(); /* Set up default_server_capabs */ |
429 |
initialize_global_set_options(); /* Has to be called after read_conf_files() */ |
430 |
channel_init(); |
431 |
channel_mode_init(); |
432 |
read_links_file(); |
433 |
motd_init(); |
434 |
user_modes_init(); |
435 |
|
436 |
if (EmptyString(ConfigServerInfo.name)) |
437 |
{ |
438 |
ilog(LOG_TYPE_IRCD, "ERROR: No server name specified in serverinfo block."); |
439 |
exit(EXIT_FAILURE); |
440 |
} |
441 |
|
442 |
strlcpy(me.name, ConfigServerInfo.name, sizeof(me.name)); |
443 |
|
444 |
/* serverinfo {} description must exist. If not, error out.*/ |
445 |
if (EmptyString(ConfigServerInfo.description)) |
446 |
{ |
447 |
ilog(LOG_TYPE_IRCD, "ERROR: No server description specified in serverinfo block."); |
448 |
exit(EXIT_FAILURE); |
449 |
} |
450 |
|
451 |
strlcpy(me.info, ConfigServerInfo.description, sizeof(me.info)); |
452 |
|
453 |
if (EmptyString(ConfigServerInfo.sid)) |
454 |
{ |
455 |
ilog(LOG_TYPE_IRCD, "Generating server ID"); |
456 |
generate_sid(); |
457 |
} |
458 |
else |
459 |
strlcpy(me.id, ConfigServerInfo.sid, sizeof(me.id)); |
460 |
|
461 |
init_uid(); |
462 |
|
463 |
me.from = &me; |
464 |
me.servptr = &me; |
465 |
me.connection->lasttime = CurrentTime; |
466 |
me.connection->since = CurrentTime; |
467 |
me.connection->firsttime = CurrentTime; |
468 |
|
469 |
SetMe(&me); |
470 |
make_server(&me); |
471 |
|
472 |
hash_add_id(&me); |
473 |
hash_add_client(&me); |
474 |
|
475 |
dlinkAdd(&me, &me.node, &global_server_list); |
476 |
|
477 |
load_kline_database(ConfigGeneral.klinefile); |
478 |
load_dline_database(ConfigGeneral.dlinefile); |
479 |
load_xline_database(ConfigGeneral.xlinefile); |
480 |
load_resv_database(ConfigGeneral.resvfile); |
481 |
|
482 |
load_all_modules(1); |
483 |
load_conf_modules(); |
484 |
load_core_modules(1); |
485 |
|
486 |
write_pidfile(pidFileName); |
487 |
|
488 |
event_addish(&event_cleanup_tklines, NULL); |
489 |
|
490 |
/* We want try_connections to be called as soon as possible now! -- adrian */ |
491 |
/* No, 'cause after a restart it would cause all sorts of nick collides */ |
492 |
event_addish(&event_try_connections, NULL); |
493 |
|
494 |
/* Setup the timeout check. I'll shift it later :) -- adrian */ |
495 |
event_add(&event_comm_checktimeouts, NULL); |
496 |
|
497 |
event_addish(&event_save_all_databases, NULL); |
498 |
|
499 |
if (ConfigServerHide.flatten_links_delay && event_write_links_file.active == 0) |
500 |
{ |
501 |
event_write_links_file.when = ConfigServerHide.flatten_links_delay; |
502 |
event_add(&event_write_links_file, NULL); |
503 |
} |
504 |
|
505 |
ilog(LOG_TYPE_IRCD, "Server Ready"); |
506 |
io_loop(); |
507 |
|
508 |
return 0; |
509 |
} |