ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/ircd.c
Revision: 3267
Committed: Sat Apr 5 19:16:06 2014 UTC (11 years, 4 months ago) by michael
Content type: text/x-csrc
File size: 17315 byte(s)
Log Message:
- Re-add CHW capability to cope nice with rb

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

Properties

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