/[svn]/ircd-hybrid/trunk/src/ircd.c
ViewVC logotype

Contents of /ircd-hybrid/trunk/src/ircd.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1702 - (show annotations)
Thu Dec 20 15:49:20 2012 UTC (7 years, 9 months ago) by michael
File MIME type: text/x-chdr
File size: 17534 byte(s)
- /info now also shows pathnames of g-/x-line database files

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

Properties

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

svnadmin@ircd-hybrid.org
ViewVC Help
Powered by ViewVC 1.1.28