ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid-7.2/src/ircd.c
Revision: 967
Committed: Sun Aug 2 18:05:28 2009 UTC (14 years, 7 months ago) by michael
Content type: text/x-csrc
File size: 18088 byte(s)
Log Message:
- added ssl_server_protocol configuration option to servinfo{}.
  valid flags are 'sslv3' and 'tlsv1'

File Contents

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

Properties

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