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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1402 - (show annotations)
Wed May 9 18:42:45 2012 UTC (8 years, 1 month ago) by michael
File MIME type: text/x-chdr
File size: 17546 byte(s)
- /info no longer sends information about the platform ircd is running on
- remove get_ircd_platform() function
- configure: --enable-efnet switch is no longer required

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 "conf.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 "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 = { .use_logging = 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
86 int dorehash = 0;
87 int doremotd = 0;
88
89 /* Set to zero because it should be initialized later using
90 * initialize_server_capabs
91 */
92 int default_server_capabs = 0;
93 unsigned int splitmode;
94 unsigned int splitchecking;
95 unsigned int split_users;
96 unsigned int split_servers;
97
98 /* Do klines the same way hybrid-6 did them, i.e. at the
99 * top of the next io_loop instead of in the same loop as
100 * the klines are being applied.
101 *
102 * This should fix strange CPU starvation as very indirectly reported.
103 * (Why do you people not email bug reports? WHY? WHY?)
104 *
105 * - Dianora
106 */
107
108 int rehashed_klines = 0;
109
110
111 /*
112 * print_startup - print startup information
113 */
114 static void
115 print_startup(int pid)
116 {
117 printf("ircd: version %s\n", ircd_version);
118 printf("ircd: pid %d\n", pid);
119 printf("ircd: running in %s mode from %s\n", !server_state.foreground ? "background"
120 : "foreground", ConfigFileEntry.dpath);
121 }
122
123 static void
124 make_daemon(void)
125 {
126 int pid;
127
128 if ((pid = fork()) < 0)
129 {
130 perror("fork");
131 exit(EXIT_FAILURE);
132 }
133 else if (pid > 0)
134 {
135 print_startup(pid);
136 exit(EXIT_SUCCESS);
137 }
138
139 setsid();
140 }
141
142 static int printVersion = 0;
143
144 static struct lgetopt myopts[] = {
145 {"dlinefile", &ConfigFileEntry.dlinefile,
146 STRING, "File to use for dline.conf"},
147 {"configfile", &ConfigFileEntry.configfile,
148 STRING, "File to use for ircd.conf"},
149 {"klinefile", &ConfigFileEntry.klinefile,
150 STRING, "File to use for kline.conf"},
151 {"xlinefile", &ConfigFileEntry.xlinefile,
152 STRING, "File to use for xline.conf"},
153 {"logfile", &logFileName,
154 STRING, "File to use for ircd.log"},
155 {"pidfile", &pidFileName,
156 STRING, "File to use for process ID"},
157 {"foreground", &server_state.foreground,
158 YESNO, "Run in foreground (don't detach)"},
159 {"version", &printVersion,
160 YESNO, "Print version and exit"},
161 {"help", NULL, USAGE, "Print this text"},
162 {NULL, NULL, STRING, NULL},
163 };
164
165 void
166 set_time(void)
167 {
168 static char to_send[200];
169 struct timeval newtime;
170 newtime.tv_sec = 0;
171 newtime.tv_usec = 0;
172
173 if (gettimeofday(&newtime, NULL) == -1)
174 {
175 ilog(LOG_TYPE_IRCD, "Clock Failure (%s), TS can be corrupted",
176 strerror(errno));
177 sendto_realops_flags(UMODE_ALL, L_ALL,
178 "Clock Failure (%s), TS can be corrupted",
179 strerror(errno));
180 restart("Clock Failure");
181 }
182
183 if (newtime.tv_sec < CurrentTime)
184 {
185 snprintf(to_send, sizeof(to_send),
186 "System clock is running backwards - (%lu < %lu)",
187 (unsigned long)newtime.tv_sec, (unsigned long)CurrentTime);
188 report_error(L_ALL, to_send, me.name, 0);
189 set_back_events(CurrentTime - newtime.tv_sec);
190 }
191
192 SystemTime.tv_sec = newtime.tv_sec;
193 SystemTime.tv_usec = newtime.tv_usec;
194 }
195
196 static void
197 io_loop(void)
198 {
199 while (1 == 1)
200 {
201 /*
202 * Maybe we want a flags word?
203 * ie. if (REHASHED_KLINES(global_flags))
204 * SET_REHASHED_KLINES(global_flags)
205 * CLEAR_REHASHED_KLINES(global_flags)
206 *
207 * - Dianora
208 */
209 if (rehashed_klines)
210 {
211 check_conf_klines();
212 rehashed_klines = 0;
213 }
214
215 if (listing_client_list.head)
216 {
217 dlink_node *ptr = NULL, *ptr_next = NULL;
218 DLINK_FOREACH_SAFE(ptr, ptr_next, listing_client_list.head)
219 {
220 struct Client *client_p = ptr->data;
221 assert(client_p->localClient->list_task);
222 safe_list_channels(client_p, client_p->localClient->list_task, 0);
223 }
224 }
225
226 /* Run pending events, then get the number of seconds to the next
227 * event
228 */
229 while (eventNextTime() <= CurrentTime)
230 eventRun();
231
232 comm_select();
233 exit_aborted_clients();
234 free_exited_clients();
235 send_queued_all();
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 read_message_file(&ConfigFileEntry.motd);
246 sendto_realops_flags(UMODE_ALL, L_ALL,
247 "Got signal SIGUSR1, reloading ircd motd file");
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_message_files()
292 *
293 * inputs - none
294 * output - none
295 * side effects - Set up all message files needed, motd etc.
296 */
297 static void
298 initialize_message_files(void)
299 {
300 init_message_file(USER_MOTD, MPATH, &ConfigFileEntry.motd);
301 init_message_file(OPER_MOTD, OPATH, &ConfigFileEntry.opermotd);
302 init_message_file(USER_LINKS, LIPATH, &ConfigFileEntry.linksfile);
303
304 read_message_file(&ConfigFileEntry.motd);
305 read_message_file(&ConfigFileEntry.opermotd);
306 read_message_file(&ConfigFileEntry.linksfile);
307
308 init_isupport();
309 }
310
311 /* initialize_server_capabs()
312 *
313 * inputs - none
314 * output - none
315 */
316 static void
317 initialize_server_capabs(void)
318 {
319 add_capability("QS", CAP_QS, 1);
320 add_capability("EOB", CAP_EOB, 1);
321 add_capability("TS6", CAP_TS6, 0);
322 add_capability("CLUSTER", CAP_CLUSTER, 1);
323 add_capability("SVS", CAP_SVS, 1);
324 #ifdef HALFOPS
325 add_capability("HOPS", CAP_HOPS, 1);
326 #endif
327 }
328
329 /* write_pidfile()
330 *
331 * inputs - filename+path of pid file
332 * output - NONE
333 * side effects - write the pid of the ircd to filename
334 */
335 static void
336 write_pidfile(const char *filename)
337 {
338 FILE *fb;
339
340 if ((fb = fopen(filename, "w")))
341 {
342 char buff[32];
343 unsigned int pid = (unsigned int)getpid();
344
345 snprintf(buff, sizeof(buff), "%u\n", pid);
346
347 if ((fputs(buff, fb) == -1))
348 ilog(LOG_TYPE_IRCD, "Error writing %u to pid file %s (%s)",
349 pid, filename, strerror(errno));
350
351 fclose(fb);
352 }
353 else
354 {
355 ilog(LOG_TYPE_IRCD, "Error opening pid file %s", filename);
356 }
357 }
358
359 /* check_pidfile()
360 *
361 * inputs - filename+path of pid file
362 * output - none
363 * side effects - reads pid from pidfile and checks if ircd is in process
364 * list. if it is, gracefully exits
365 * -kre
366 */
367 static void
368 check_pidfile(const char *filename)
369 {
370 FILE *fb;
371 char buff[32];
372 pid_t pidfromfile;
373
374 /* Don't do logging here, since we don't have log() initialised */
375 if ((fb = fopen(filename, "r")))
376 {
377 if (fgets(buff, 20, fb) == NULL)
378 {
379 /* log(L_ERROR, "Error reading from pid file %s (%s)", filename,
380 * strerror(errno));
381 */
382 }
383 else
384 {
385 pidfromfile = atoi(buff);
386
387 if (!kill(pidfromfile, 0))
388 {
389 /* log(L_ERROR, "Server is already running"); */
390 printf("ircd: daemon is already running\n");
391 exit(-1);
392 }
393 }
394
395 fclose(fb);
396 }
397 else if (errno != ENOENT)
398 {
399 /* log(L_ERROR, "Error opening pid file %s", filename); */
400 }
401 }
402
403 /* setup_corefile()
404 *
405 * inputs - nothing
406 * output - nothing
407 * side effects - setups corefile to system limits.
408 * -kre
409 */
410 static void
411 setup_corefile(void)
412 {
413 #ifdef HAVE_SYS_RESOURCE_H
414 struct rlimit rlim; /* resource limits */
415
416 /* Set corefilesize to maximum */
417 if (!getrlimit(RLIMIT_CORE, &rlim))
418 {
419 rlim.rlim_cur = rlim.rlim_max;
420 setrlimit(RLIMIT_CORE, &rlim);
421 }
422 #endif
423 }
424
425 /* init_ssl()
426 *
427 * inputs - nothing
428 * output - nothing
429 * side effects - setups SSL context.
430 */
431 static void
432 init_ssl(void)
433 {
434 #ifdef HAVE_LIBCRYPTO
435 SSL_load_error_strings();
436 SSLeay_add_ssl_algorithms();
437
438 if ((ServerInfo.server_ctx = SSL_CTX_new(SSLv23_server_method())) == NULL)
439 {
440 const char *s;
441
442 fprintf(stderr, "ERROR: Could not initialize the SSL Server context -- %s\n",
443 s = ERR_lib_error_string(ERR_get_error()));
444 ilog(LOG_TYPE_IRCD, "ERROR: Could not initialize the SSL Server context -- %s\n", s);
445 }
446
447 SSL_CTX_set_options(ServerInfo.server_ctx, SSL_OP_NO_SSLv2|SSL_OP_NO_SSLv3|SSL_OP_NO_TLSv1);
448 SSL_CTX_set_options(ServerInfo.server_ctx, SSL_OP_TLS_ROLLBACK_BUG|SSL_OP_ALL);
449 SSL_CTX_set_verify(ServerInfo.server_ctx, SSL_VERIFY_NONE, NULL);
450
451 if ((ServerInfo.client_ctx = SSL_CTX_new(SSLv23_client_method())) == NULL)
452 {
453 const char *s;
454
455 fprintf(stderr, "ERROR: Could not initialize the SSL Client context -- %s\n",
456 s = ERR_lib_error_string(ERR_get_error()));
457 ilog(LOG_TYPE_IRCD, "ERROR: Could not initialize the SSL Client context -- %s\n", s);
458 }
459
460 SSL_CTX_set_options(ServerInfo.client_ctx, SSL_OP_NO_SSLv2|SSL_OP_NO_SSLv3|SSL_OP_NO_TLSv1);
461 SSL_CTX_set_options(ServerInfo.client_ctx, SSL_OP_TLS_ROLLBACK_BUG|SSL_OP_ALL);
462 SSL_CTX_set_verify(ServerInfo.client_ctx, SSL_VERIFY_NONE, NULL);
463 #endif /* HAVE_LIBCRYPTO */
464 }
465
466 /* init_callbacks()
467 *
468 * inputs - nothing
469 * output - nothing
470 * side effects - setups standard hook points
471 */
472 static void
473 init_callbacks(void)
474 {
475 iorecv_cb = register_callback("iorecv", iorecv_default);
476 iosend_cb = register_callback("iosend", iosend_default);
477 }
478
479 int
480 main(int argc, char *argv[])
481 {
482 /* Check to see if the user is running
483 * us as root, which is a nono
484 */
485 if (geteuid() == 0)
486 {
487 fprintf(stderr, "Don't run ircd as root!!!\n");
488 return -1;
489 }
490
491 /* Setup corefile size immediately after boot -kre */
492 setup_corefile();
493
494 /* save server boot time right away, so getrusage works correctly */
495 set_time();
496
497 /* It ain't random, but it ought to be a little harder to guess */
498 init_genrand(SystemTime.tv_sec ^ (SystemTime.tv_usec | (getpid() << 20)));
499
500 me.localClient = &meLocalUser;
501 dlinkAdd(&me, &me.node, &global_client_list); /* Pointer to beginning
502 of Client list */
503 /* Initialise the channel capability usage counts... */
504 init_chcap_usage_counts();
505
506 ConfigFileEntry.dpath = DPATH;
507 ConfigFileEntry.configfile = CPATH; /* Server configuration file */
508 ConfigFileEntry.klinefile = KPATH; /* Server kline file */
509 ConfigFileEntry.xlinefile = XPATH; /* Server xline file */
510 ConfigFileEntry.rxlinefile = RXPATH; /* Server regex xline file */
511 ConfigFileEntry.rklinefile = RKPATH; /* Server regex kline file */
512 ConfigFileEntry.dlinefile = DLPATH; /* dline file */
513 ConfigFileEntry.glinefile = GPATH; /* gline log file */
514 ConfigFileEntry.cresvfile = CRESVPATH; /* channel resv file */
515 ConfigFileEntry.nresvfile = NRESVPATH; /* nick resv file */
516 myargv = argv;
517 umask(077); /* better safe than sorry --SRB */
518
519 parseargs(&argc, &argv, myopts);
520
521 if (printVersion)
522 {
523 printf("ircd: version %s\n", ircd_version);
524 exit(EXIT_SUCCESS);
525 }
526
527 if (chdir(ConfigFileEntry.dpath))
528 {
529 perror("chdir");
530 exit(EXIT_FAILURE);
531 }
532
533 init_ssl();
534
535 if (!server_state.foreground)
536 {
537 make_daemon();
538 close_standard_fds(); /* this needs to be before init_netio()! */
539 }
540 else
541 print_startup(getpid());
542
543 setup_signals();
544
545 /* Init the event subsystem */
546 eventInit();
547 /* We need this to initialise the fd array before anything else */
548 fdlist_init();
549 log_add_file(LOG_TYPE_IRCD, 0, logFileName);
550 check_can_use_v6();
551 init_comm(); /* This needs to be setup early ! -- adrian */
552 /* Check if there is pidfile and daemon already running */
553 check_pidfile(pidFileName);
554
555 initBlockHeap();
556 init_dlink_nodes();
557 init_callbacks();
558 initialize_message_files();
559 dbuf_init();
560 init_hash();
561 init_ip_hash_table(); /* client host ip hash table */
562 init_host_hash(); /* Host-hashtable. */
563 init_client();
564 init_class();
565 whowas_init();
566 watch_init();
567 init_auth(); /* Initialise the auth code */
568 init_resolver(); /* Needs to be setup before the io loop */
569 read_conf_files(1); /* cold start init conf files */
570 init_uid();
571 initialize_server_capabs(); /* Set up default_server_capabs */
572 initialize_global_set_options();
573 init_channels();
574
575 if (EmptyString(ServerInfo.sid))
576 {
577 ilog(LOG_TYPE_IRCD, "ERROR: No server id specified in serverinfo block.");
578 exit(EXIT_FAILURE);
579 }
580
581 strlcpy(me.id, ServerInfo.sid, sizeof(me.id));
582
583 if (EmptyString(ServerInfo.name))
584 {
585 ilog(LOG_TYPE_IRCD, "ERROR: No server name specified in serverinfo block.");
586 exit(EXIT_FAILURE);
587 }
588
589 strlcpy(me.name, ServerInfo.name, sizeof(me.name));
590
591 /* serverinfo{} description must exist. If not, error out.*/
592 if (EmptyString(ServerInfo.description))
593 {
594 ilog(LOG_TYPE_IRCD, "ERROR: No server description specified in serverinfo block.");
595 exit(EXIT_FAILURE);
596 }
597
598 strlcpy(me.info, ServerInfo.description, sizeof(me.info));
599
600 me.from = &me;
601 me.servptr = &me;
602 me.localClient->lasttime = CurrentTime;
603 me.localClient->since = CurrentTime;
604 me.localClient->firsttime = CurrentTime;
605
606 SetMe(&me);
607 make_server(&me);
608
609 hash_add_id(&me);
610 hash_add_client(&me);
611
612 /* add ourselves to global_serv_list */
613 dlinkAdd(&me, make_dlink_node(), &global_serv_list);
614
615 if (chdir(MODPATH))
616 {
617 ilog(LOG_TYPE_IRCD, "Could not load core modules. Terminating!");
618 exit(EXIT_FAILURE);
619 }
620
621 load_all_modules(1);
622 load_conf_modules();
623 load_core_modules(1);
624
625 /* Go back to DPATH after checking to see if we can chdir to MODPATH */
626 if (chdir(ConfigFileEntry.dpath))
627 {
628 perror("chdir");
629 exit(EXIT_FAILURE);
630 }
631
632 /*
633 * assemble_umode_buffer() has to be called after
634 * reading conf/loading modules.
635 */
636 assemble_umode_buffer();
637
638 write_pidfile(pidFileName);
639
640 ilog(LOG_TYPE_IRCD, "Server Ready");
641
642 eventAddIsh("cleanup_glines", cleanup_glines, NULL, CLEANUP_GLINES_TIME);
643 eventAddIsh("cleanup_tklines", cleanup_tklines, NULL, CLEANUP_TKLINES_TIME);
644
645 /* We want try_connections to be called as soon as possible now! -- adrian */
646 /* No, 'cause after a restart it would cause all sorts of nick collides */
647 eventAddIsh("try_connections", try_connections, NULL, STARTUP_CONNECTIONS_TIME);
648
649 /* Setup the timeout check. I'll shift it later :) -- adrian */
650 eventAddIsh("comm_checktimeouts", comm_checktimeouts, NULL, 1);
651
652 if (ConfigServerHide.links_delay > 0)
653 eventAddIsh("write_links_file", write_links_file, NULL, ConfigServerHide.links_delay);
654 else
655 ConfigServerHide.links_disabled = 1;
656
657 if (splitmode)
658 eventAddIsh("check_splitmode", check_splitmode, NULL, 60);
659
660 io_loop();
661 return 0;
662 }

Properties

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

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