ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/ircd.c
Revision: 4094
Committed: Sun Jun 29 11:48:50 2014 UTC (9 years, 9 months ago) by michael
Content type: text/x-csrc
File size: 17409 byte(s)
Log Message:
- Merged Adam's event system rewrite

File Contents

# Content
1 /*
2 * ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd)
3 *
4 * Copyright (c) 1997-2014 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307
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 "client.h"
33 #include "event.h"
34 #include "fdlist.h"
35 #include "hash.h"
36 #include "irc_string.h"
37 #include "ircd_signal.h"
38 #include "gline.h"
39 #include "motd.h"
40 #include "conf.h"
41 #include "hostmask.h"
42 #include "parse.h"
43 #include "res.h"
44 #include "restart.h"
45 #include "rng_mt.h"
46 #include "auth.h"
47 #include "s_bsd.h"
48 #include "log.h"
49 #include "server.h" /* try_connections */
50 #include "send.h"
51 #include "whowas.h"
52 #include "modules.h"
53 #include "memory.h"
54 #include "mempool.h"
55 #include "ircd_getopt.h"
56 #include "supported.h"
57 #include "watch.h"
58 #include "conf_db.h"
59 #include "conf_class.h"
60
61
62 #ifdef HAVE_LIBGEOIP
63 GeoIP *geoip_ctx;
64 #endif
65
66 /* /quote set variables */
67 struct SetOptions GlobalSetOptions;
68 struct Counter Count;
69 struct ServerState_t server_state;
70 struct ServerStatistics ServerStats;
71 struct timeval SystemTime;
72 struct Client me; /* That's me */
73 struct LocalUser meLocalUser; /* That's also part of me */
74
75 const char *logFileName = LPATH;
76 const char *pidFileName = PPATH;
77
78 char **myargv;
79
80 int dorehash = 0;
81 int doremotd = 0;
82
83 /* Set to zero because it should be initialized later using
84 * initialize_server_capabs
85 */
86 unsigned int default_server_capabs;
87 unsigned int splitmode;
88 unsigned int splitchecking;
89 unsigned int split_users;
90 unsigned int split_servers;
91
92 static struct event event_cleanup_glines =
93 {
94 .name = "cleanup_glines",
95 .handler = cleanup_glines,
96 .when = CLEANUP_GLINES_TIME
97 };
98
99 static struct event event_cleanup_tklines =
100 {
101 .name = "cleanup_tklines",
102 .handler = cleanup_tklines,
103 .when = CLEANUP_TKLINES_TIME
104 };
105
106 static struct event event_try_connections =
107 {
108 .name = "try_connections",
109 .handler = try_connections,
110 .when = STARTUP_CONNECTIONS_TIME
111 };
112
113 static struct event event_comm_checktimeouts =
114 {
115 .name = "comm_checktimeouts",
116 .handler = comm_checktimeouts,
117 .when = 1
118 };
119
120 static struct event event_save_all_databases =
121 {
122 .name = "save_all_databases",
123 .handler = save_all_databases,
124 .when = DATABASE_UPDATE_TIMEOUT
125 };
126
127 struct event event_write_links_file =
128 {
129 .name = "write_links_file",
130 .handler = write_links_file,
131 };
132
133 struct event event_check_splitmode =
134 {
135 .name = "check_splitmode",
136 .handler = check_splitmode,
137 .when = 60
138 };
139
140 /*
141 * print_startup - print startup information
142 */
143 static void
144 print_startup(int pid)
145 {
146 printf("ircd: version %s(%s)\n", ircd_version, serno);
147 printf("ircd: pid %d\n", pid);
148 printf("ircd: running in %s mode from %s\n", !server_state.foreground ? "background"
149 : "foreground", ConfigFileEntry.dpath);
150 }
151
152 static void
153 make_daemon(void)
154 {
155 int pid;
156
157 if ((pid = fork()) < 0)
158 {
159 perror("fork");
160 exit(EXIT_FAILURE);
161 }
162 else if (pid > 0)
163 {
164 print_startup(pid);
165 exit(EXIT_SUCCESS);
166 }
167
168 setsid();
169 }
170
171 static int printVersion = 0;
172
173 static struct lgetopt myopts[] =
174 {
175 {"configfile", &ConfigFileEntry.configfile,
176 STRING, "File to use for ircd.conf"},
177 {"glinefile", &ConfigFileEntry.glinefile,
178 STRING, "File to use for gline database"},
179 {"klinefile", &ConfigFileEntry.klinefile,
180 STRING, "File to use for kline database"},
181 {"dlinefile", &ConfigFileEntry.dlinefile,
182 STRING, "File to use for dline database"},
183 {"xlinefile", &ConfigFileEntry.xlinefile,
184 STRING, "File to use for xline database"},
185 {"resvfile", &ConfigFileEntry.resvfile,
186 STRING, "File to use for resv database"},
187 {"logfile", &logFileName,
188 STRING, "File to use for ircd.log"},
189 {"pidfile", &pidFileName,
190 STRING, "File to use for process ID"},
191 {"foreground", &server_state.foreground,
192 YESNO, "Run in foreground (don't detach)"},
193 {"version", &printVersion,
194 YESNO, "Print version and exit"},
195 {"help", NULL, USAGE, "Print this text"},
196 {NULL, NULL, STRING, NULL},
197 };
198
199 void
200 set_time(void)
201 {
202 struct timeval newtime = { .tv_sec = 0, .tv_usec = 0 };
203
204 if (gettimeofday(&newtime, NULL) == -1)
205 {
206 ilog(LOG_TYPE_IRCD, "Clock Failure (%s), TS can be corrupted",
207 strerror(errno));
208 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
209 "Clock Failure (%s), TS can be corrupted",
210 strerror(errno));
211 server_die("Clock Failure", 1);
212 }
213
214 if (newtime.tv_sec < CurrentTime)
215 {
216 ilog(LOG_TYPE_IRCD, "System clock is running backwards - (%lu < %lu)",
217 (unsigned long)newtime.tv_sec, (unsigned long)CurrentTime);
218 sendto_realops_flags(UMODE_DEBUG, L_ALL, SEND_NOTICE,
219 "System clock is running backwards - (%lu < %lu)",
220 (unsigned long)newtime.tv_sec,
221 (unsigned long)CurrentTime);
222 set_back_events(CurrentTime - newtime.tv_sec);
223 }
224
225 SystemTime.tv_sec = newtime.tv_sec;
226 SystemTime.tv_usec = newtime.tv_usec;
227 }
228
229 static void
230 io_loop(void)
231 {
232 while (1)
233 {
234 if (listing_client_list.head)
235 {
236 dlink_node *ptr = NULL, *ptr_next = NULL;
237 DLINK_FOREACH_SAFE(ptr, ptr_next, listing_client_list.head)
238 safe_list_channels(ptr->data, 0);
239 }
240
241 /* Run pending events */
242 event_run();
243
244 comm_select();
245 exit_aborted_clients();
246 free_exited_clients();
247
248 /* Check to see whether we have to rehash the configuration .. */
249 if (dorehash)
250 {
251 rehash(1);
252 dorehash = 0;
253 }
254
255 if (doremotd)
256 {
257 motd_recache();
258 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
259 "Got signal SIGUSR1, reloading motd file(s)");
260 doremotd = 0;
261 }
262 }
263 }
264
265 /* initalialize_global_set_options()
266 *
267 * inputs - none
268 * output - none
269 * side effects - This sets all global set options needed
270 */
271 static void
272 initialize_global_set_options(void)
273 {
274 memset(&GlobalSetOptions, 0, sizeof(GlobalSetOptions));
275
276 GlobalSetOptions.autoconn = 1;
277 GlobalSetOptions.spam_time = MIN_JOIN_LEAVE_TIME;
278 GlobalSetOptions.spam_num = MAX_JOIN_LEAVE_COUNT;
279
280 if (ConfigFileEntry.default_floodcount)
281 GlobalSetOptions.floodcount = ConfigFileEntry.default_floodcount;
282 else
283 GlobalSetOptions.floodcount = 10;
284
285 /* XXX I have no idea what to try here - Dianora */
286 GlobalSetOptions.joinfloodcount = 16;
287 GlobalSetOptions.joinfloodtime = 8;
288
289 split_servers = ConfigChannel.default_split_server_count;
290 split_users = ConfigChannel.default_split_user_count;
291
292 if (split_users && split_servers && (ConfigChannel.no_create_on_split ||
293 ConfigChannel.no_join_on_split))
294 {
295 splitmode = 1;
296 splitchecking = 1;
297 }
298
299 GlobalSetOptions.ident_timeout = IDENT_TIMEOUT;
300 /* End of global set options */
301 }
302
303 /* initialize_server_capabs()
304 *
305 * inputs - none
306 * output - none
307 */
308 static void
309 initialize_server_capabs(void)
310 {
311 add_capability("QS", CAP_QS, 1);
312 add_capability("EOB", CAP_EOB, 1);
313 add_capability("TS6", CAP_TS6, 0);
314 add_capability("CLUSTER", CAP_CLUSTER, 1);
315 add_capability("SVS", CAP_SVS, 1);
316 add_capability("CHW", CAP_CHW, 1);
317 add_capability("HOPS", CAP_HOPS, 1);
318 }
319
320 /* write_pidfile()
321 *
322 * inputs - filename+path of pid file
323 * output - NONE
324 * side effects - write the pid of the ircd to filename
325 */
326 static void
327 write_pidfile(const char *filename)
328 {
329 FILE *fb;
330
331 if ((fb = fopen(filename, "w")))
332 {
333 char buff[IRCD_BUFSIZE];
334 unsigned int pid = (unsigned int)getpid();
335
336 snprintf(buff, sizeof(buff), "%u\n", pid);
337
338 if ((fputs(buff, fb) == -1))
339 ilog(LOG_TYPE_IRCD, "Error writing %u to pid file %s (%s)",
340 pid, filename, strerror(errno));
341
342 fclose(fb);
343 }
344 else
345 {
346 ilog(LOG_TYPE_IRCD, "Error opening pid file %s", filename);
347 }
348 }
349
350 /* check_pidfile()
351 *
352 * inputs - filename+path of pid file
353 * output - none
354 * side effects - reads pid from pidfile and checks if ircd is in process
355 * list. if it is, gracefully exits
356 * -kre
357 */
358 static void
359 check_pidfile(const char *filename)
360 {
361 FILE *fb;
362 char buff[IRCD_BUFSIZE];
363 pid_t pidfromfile;
364
365 /* Don't do logging here, since we don't have log() initialised */
366 if ((fb = fopen(filename, "r")))
367 {
368 if (fgets(buff, 20, fb) == NULL)
369 {
370 /* log(L_ERROR, "Error reading from pid file %s (%s)", filename,
371 * strerror(errno));
372 */
373 }
374 else
375 {
376 pidfromfile = atoi(buff);
377
378 if (!kill(pidfromfile, 0))
379 {
380 /* log(L_ERROR, "Server is already running"); */
381 printf("ircd: daemon is already running\n");
382 exit(-1);
383 }
384 }
385
386 fclose(fb);
387 }
388 else if (errno != ENOENT)
389 {
390 /* log(L_ERROR, "Error opening pid file %s", filename); */
391 }
392 }
393
394 /* setup_corefile()
395 *
396 * inputs - nothing
397 * output - nothing
398 * side effects - setups corefile to system limits.
399 * -kre
400 */
401 static void
402 setup_corefile(void)
403 {
404 #ifdef HAVE_SYS_RESOURCE_H
405 struct rlimit rlim; /* resource limits */
406
407 /* Set corefilesize to maximum */
408 if (!getrlimit(RLIMIT_CORE, &rlim))
409 {
410 rlim.rlim_cur = rlim.rlim_max;
411 setrlimit(RLIMIT_CORE, &rlim);
412 }
413 #endif
414 }
415
416 #ifdef HAVE_LIBCRYPTO
417 static int
418 always_accept_verify_cb(int preverify_ok, X509_STORE_CTX *x509_ctx)
419 {
420 return 1;
421 }
422 #endif
423
424 /* init_ssl()
425 *
426 * inputs - nothing
427 * output - nothing
428 * side effects - setups SSL context.
429 */
430 static void
431 ssl_init(void)
432 {
433 #ifdef HAVE_LIBCRYPTO
434 const unsigned char session_id[] = "ircd-hybrid";
435
436 SSL_load_error_strings();
437 SSLeay_add_ssl_algorithms();
438
439 if ((ServerInfo.server_ctx = SSL_CTX_new(SSLv23_server_method())) == NULL)
440 {
441 const char *s;
442
443 fprintf(stderr, "ERROR: Could not initialize the SSL Server context -- %s\n",
444 s = ERR_lib_error_string(ERR_get_error()));
445 ilog(LOG_TYPE_IRCD, "ERROR: Could not initialize the SSL Server context -- %s\n", s);
446 }
447
448 SSL_CTX_set_options(ServerInfo.server_ctx, SSL_OP_NO_SSLv2|SSL_OP_NO_SSLv3|SSL_OP_NO_TLSv1);
449 SSL_CTX_set_options(ServerInfo.server_ctx, SSL_OP_TLS_ROLLBACK_BUG);
450 SSL_CTX_set_verify(ServerInfo.server_ctx, SSL_VERIFY_PEER|SSL_VERIFY_CLIENT_ONCE,
451 always_accept_verify_cb);
452 SSL_CTX_set_session_id_context(ServerInfo.server_ctx, session_id, sizeof(session_id) - 1);
453
454 #if OPENSSL_VERSION_NUMBER >= 0x1000005FL && !defined(OPENSSL_NO_ECDH)
455 {
456 EC_KEY *key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
457
458 if (key)
459 {
460 SSL_CTX_set_tmp_ecdh(ServerInfo.server_ctx, key);
461 EC_KEY_free(key);
462 }
463 }
464
465 SSL_CTX_set_options(ServerInfo.server_ctx, SSL_OP_SINGLE_ECDH_USE);
466 #endif
467
468 if ((ServerInfo.client_ctx = SSL_CTX_new(SSLv23_client_method())) == NULL)
469 {
470 const char *s;
471
472 fprintf(stderr, "ERROR: Could not initialize the SSL Client context -- %s\n",
473 s = ERR_lib_error_string(ERR_get_error()));
474 ilog(LOG_TYPE_IRCD, "ERROR: Could not initialize the SSL Client context -- %s\n", s);
475 }
476
477 SSL_CTX_set_options(ServerInfo.client_ctx, SSL_OP_NO_SSLv2|SSL_OP_NO_SSLv3|SSL_OP_NO_TLSv1);
478 SSL_CTX_set_options(ServerInfo.client_ctx, SSL_OP_TLS_ROLLBACK_BUG);
479 SSL_CTX_set_verify(ServerInfo.client_ctx, SSL_VERIFY_PEER|SSL_VERIFY_CLIENT_ONCE,
480 always_accept_verify_cb);
481 #endif /* HAVE_LIBCRYPTO */
482 }
483
484 int
485 main(int argc, char *argv[])
486 {
487 /* Check to see if the user is running us as root, which is a nono */
488 if (geteuid() == 0)
489 {
490 fprintf(stderr, "ERROR: This server won't run as root/superuser\n");
491 return -1;
492 }
493
494 /* Setup corefile size immediately after boot -kre */
495 setup_corefile();
496
497 /* save server boot time right away, so getrusage works correctly */
498 set_time();
499
500 /* It ain't random, but it ought to be a little harder to guess */
501 init_genrand(SystemTime.tv_sec ^ (SystemTime.tv_usec | (getpid() << 20)));
502
503 me.localClient = &meLocalUser;
504 dlinkAdd(&me, &me.node, &global_client_list); /* Pointer to beginning
505 of Client list */
506 ConfigLoggingEntry.use_logging = 1;
507 ConfigFileEntry.dpath = DPATH;
508 ConfigFileEntry.spath = SPATH;
509 ConfigFileEntry.mpath = MPATH;
510 ConfigFileEntry.configfile = CPATH; /* Server configuration file */
511 ConfigFileEntry.klinefile = KPATH; /* Server kline file */
512 ConfigFileEntry.glinefile = GPATH; /* Server gline file */
513 ConfigFileEntry.xlinefile = XPATH; /* Server xline file */
514 ConfigFileEntry.dlinefile = DLPATH; /* dline file */
515 ConfigFileEntry.resvfile = RESVPATH; /* resv file */
516
517 myargv = argv;
518 umask(077); /* better safe than sorry --SRB */
519
520 parseargs(&argc, &argv, myopts);
521
522 if (printVersion)
523 {
524 printf("ircd: version %s(%s)\n", ircd_version, serno);
525 exit(EXIT_SUCCESS);
526 }
527
528 if (chdir(ConfigFileEntry.dpath))
529 {
530 perror("chdir");
531 exit(EXIT_FAILURE);
532 }
533
534 ssl_init();
535
536 if (!server_state.foreground)
537 {
538 make_daemon();
539 close_standard_fds(); /* this needs to be before init_netio()! */
540 }
541 else
542 print_startup(getpid());
543
544 setup_signals();
545
546 /* We need this to initialise the fd array before anything else */
547 fdlist_init();
548 log_set_file(LOG_TYPE_IRCD, 0, logFileName);
549 check_can_use_v6();
550 init_netio(); /* This needs to be setup early ! -- adrian */
551
552 /* Check if there is pidfile and daemon already running */
553 check_pidfile(pidFileName);
554
555 mp_pool_init();
556 init_dlink_nodes();
557 init_isupport();
558 dbuf_init();
559 hash_init();
560 init_ip_hash_table(); /* client host ip hash table */
561 init_host_hash(); /* Host-hashtable. */
562 client_init();
563 class_init();
564 whowas_init();
565 watch_init();
566 auth_init(); /* Initialise the auth code */
567 init_resolver(); /* Needs to be setup before the io loop */
568 modules_init();
569 read_conf_files(1); /* cold start init conf files */
570 init_uid();
571 initialize_server_capabs(); /* Set up default_server_capabs */
572 initialize_global_set_options();
573 channel_init();
574 read_links_file();
575 motd_init();
576 #ifdef HAVE_LIBGEOIP
577 geoip_ctx = GeoIP_new(GEOIP_MEMORY_CACHE);
578 #endif
579
580 if (EmptyString(ServerInfo.sid))
581 {
582 ilog(LOG_TYPE_IRCD, "ERROR: No server id specified in serverinfo block.");
583 exit(EXIT_FAILURE);
584 }
585
586 strlcpy(me.id, ServerInfo.sid, sizeof(me.id));
587
588 if (EmptyString(ServerInfo.name))
589 {
590 ilog(LOG_TYPE_IRCD, "ERROR: No server name specified in serverinfo block.");
591 exit(EXIT_FAILURE);
592 }
593
594 strlcpy(me.name, ServerInfo.name, sizeof(me.name));
595
596 /* serverinfo{} description must exist. If not, error out.*/
597 if (EmptyString(ServerInfo.description))
598 {
599 ilog(LOG_TYPE_IRCD, "ERROR: No server description specified in serverinfo block.");
600 exit(EXIT_FAILURE);
601 }
602
603 strlcpy(me.info, ServerInfo.description, sizeof(me.info));
604
605 me.from = &me;
606 me.servptr = &me;
607 me.localClient->lasttime = CurrentTime;
608 me.localClient->since = CurrentTime;
609 me.localClient->firsttime = CurrentTime;
610
611 SetMe(&me);
612 make_server(&me);
613
614 hash_add_id(&me);
615 hash_add_client(&me);
616
617 /* add ourselves to global_serv_list */
618 dlinkAdd(&me, make_dlink_node(), &global_serv_list);
619
620 load_kline_database();
621 load_dline_database();
622 load_gline_database();
623 load_xline_database();
624 load_resv_database();
625
626 if (chdir(MODPATH))
627 {
628 ilog(LOG_TYPE_IRCD, "Could not load core modules. Terminating!");
629 exit(EXIT_FAILURE);
630 }
631
632 load_all_modules(1);
633 load_conf_modules();
634 load_core_modules(1);
635
636 /* Go back to DPATH after checking to see if we can chdir to MODPATH */
637 if (chdir(ConfigFileEntry.dpath))
638 {
639 perror("chdir");
640 exit(EXIT_FAILURE);
641 }
642
643 /*
644 * assemble_umode_buffer() has to be called after
645 * reading conf/loading modules.
646 */
647 assemble_umode_buffer();
648
649 write_pidfile(pidFileName);
650
651 ilog(LOG_TYPE_IRCD, "Server Ready");
652
653 event_addish(&event_cleanup_glines, NULL);
654 event_addish(&event_cleanup_tklines, NULL);
655
656 /* We want try_connections to be called as soon as possible now! -- adrian */
657 /* No, 'cause after a restart it would cause all sorts of nick collides */
658 event_addish(&event_try_connections, NULL);
659
660 /* Setup the timeout check. I'll shift it later :) -- adrian */
661 event_addish(&event_comm_checktimeouts, NULL);
662
663 event_addish(&event_save_all_databases, NULL);
664
665 if (ConfigServerHide.links_delay > 0)
666 {
667 event_write_links_file.when = ConfigServerHide.links_delay;
668 event_addish(&event_write_links_file, NULL);
669 }
670 else
671 ConfigServerHide.links_disabled = 1;
672
673 if (splitmode)
674 event_addish(&event_check_splitmode, NULL);
675
676 io_loop();
677 return 0;
678 }

Properties

Name Value
svn:eol-style native
svn:keywords Id Revision