ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/branches/newio/src/ircd.c
Revision: 1303
Committed: Fri Mar 23 10:52:19 2012 UTC (12 years ago) by michael
Content type: text/x-csrc
Original Path: ircd-hybrid-8/src/ircd.c
File size: 17721 byte(s)
Log Message:
- Implement basic tls connections for server-server links

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

Properties

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