ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/ircd.c
Revision: 2914
Committed: Fri Jan 24 19:45:36 2014 UTC (11 years, 7 months ago) by michael
Content type: text/x-csrc
File size: 17207 byte(s)
Log Message:
- Fixed bug where some irc clients could not reconnect
  via ssl as pointed out by gp

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

Properties

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