ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/ircd.c
Revision: 3274
Committed: Sun Apr 6 12:22:23 2014 UTC (12 years, 4 months ago) by michael
Content type: text/x-csrc
File size: 17210 byte(s)
Log Message:
- Clean up redundant/unused header includes

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

Properties

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