ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/ircd.c
Revision: 4620
Committed: Sun Sep 7 13:03:42 2014 UTC (10 years, 11 months ago) by michael
Content type: text/x-csrc
File size: 17004 byte(s)
Log Message:
- ircd.c: fixed compile warning

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

Properties

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