ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/ircd.c
Revision: 1176
Committed: Sun Aug 14 11:24:24 2011 UTC (14 years ago) by michael
Content type: text/x-csrc
Original Path: ircd-hybrid-8/src/ircd.c
File size: 17425 byte(s)
Log Message:
- remove idle-time klines
- rename LocalUser.last to LocalUser.last_privmsg
- m_message.c: reset source_p->last_privmsg even if a client is messaging itself


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

Properties

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