ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/src/ircd.c
Revision: 502
Committed: Fri Mar 3 19:49:25 2006 UTC (18 years, 1 month ago) by michael
Content type: text/x-csrc
File size: 18588 byte(s)
Log Message:
- Implemented CAP command handler based uppon ircu's m_cap()
- Added somewhat outdated draft-mitchell-irc-capabilities-01.txt until
  I get the latest version from kev.
- Added "multi-prefix" cap so clients supporting "multi-prefix"
  may recieve multi prefixed NAMES replies, e.g. @%+nick1 @+nick2 ..
- Fixed "make clean" for src/conf/

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 #ifdef IN_IRCD
26 #include "stdinc.h"
27 #include "s_user.h"
28 #include "ircd.h"
29 #include "channel.h"
30 #include "channel_mode.h"
31 #include "client.h"
32 #include "common.h"
33 #include "hash.h"
34 #include "ircd_signal.h"
35 #include "s_gline.h"
36 #include "motd.h"
37 #include "ircd_handler.h"
38 #include "msg.h" /* msgtab */
39 #include "hostmask.h"
40 #include "numeric.h"
41 #include "packet.h"
42 #include "parse.h"
43 #include "restart.h"
44 #include "s_auth.h"
45 #include "s_conf.h"
46 #include "parse_aline.h"
47 #include "s_serv.h"
48 #include "s_stats.h"
49 #include "send.h"
50 #include "whowas.h"
51 #include "conf/modules.h"
52 #include "ircd_getopt.h"
53 #include "motd.h"
54 #include "supported.h"
55 #include "watch.h"
56
57 /* Try and find the correct name to use with getrlimit() for setting the max.
58 * number of files allowed to be open by this process.
59 */
60
61 /* /quote set variables */
62 struct SetOptions GlobalSetOptions;
63
64 /* configuration set from ircd.conf */
65 struct config_file_entry ConfigFileEntry;
66 /* server info set from ircd.conf */
67 struct server_info ServerInfo;
68 /* admin info set from ircd.conf */
69 struct admin_info AdminInfo = { NULL, NULL, NULL };
70 struct Counter Count = { 0, 0, 0, 0, 0, 0, 0, 0 };
71 struct ServerState_t server_state = { 0 };
72 struct logging_entry ConfigLoggingEntry = { 1, {0}, {0}, {0}, {0}, {0}, {0}, {0}, {0} };
73 struct Client me; /* That's me */
74 struct LocalUser meLocalUser; /* That's also part of me */
75 unsigned long connect_id = 0; /* unique connect ID */
76
77 static unsigned long initialVMTop = 0; /* top of virtual memory at init */
78 const char *logFileName = LPATH;
79 const char *pidFileName = PPATH;
80
81 char **myargv;
82 char ircd_platform[PLATFORMLEN];
83
84 int dorehash = 0;
85 int doremotd = 0;
86
87 /* Set to zero because it should be initialized later using
88 * initialize_server_capabs
89 */
90 int default_server_capabs = 0;
91
92 #ifdef HAVE_LIBCRYPTO
93 int bio_spare_fd = -1;
94 #endif
95
96 int splitmode;
97 int splitchecking;
98 int split_users;
99 unsigned int split_servers;
100
101 static dlink_node *fdlimit_hook;
102
103 /* Do klines the same way hybrid-6 did them, i.e. at the
104 * top of the next io_loop instead of in the same loop as
105 * the klines are being applied.
106 *
107 * This should fix strange CPU starvation as very indirectly reported.
108 * (Why do you people not email bug reports? WHY? WHY?)
109 *
110 * - Dianora
111 */
112
113 int rehashed_klines = 0;
114
115 /*
116 * get_vm_top - get the operating systems notion of the resident set size
117 */
118 #ifndef _WIN32
119 static unsigned long
120 get_vm_top(void)
121 {
122 /*
123 * NOTE: sbrk is not part of the ANSI C library or the POSIX.1 standard
124 * however it seems that everyone defines it. Calling sbrk with a 0
125 * argument will return a pointer to the top of the process virtual
126 * memory without changing the process size, so this call should be
127 * reasonably safe (sbrk returns the new value for the top of memory).
128 * This code relies on the notion that the address returned will be an
129 * offset from 0 (NULL), so the result of sbrk is cast to a size_t and
130 * returned. We really shouldn't be using it here but...
131 */
132
133 void *vptr = sbrk(0);
134 return((unsigned long)vptr);
135 }
136
137 /*
138 * print_startup - print startup information
139 */
140 static void
141 print_startup(int pid)
142 {
143 printf("ircd: version %s\n", ircd_version);
144 printf("ircd: pid %d\n", pid);
145 printf("ircd: running in %s mode from %s\n", !server_state.foreground ? "background"
146 : "foreground", ConfigFileEntry.dpath);
147 }
148
149 static void
150 make_daemon(void)
151 {
152 int pid;
153
154 if ((pid = fork()) < 0)
155 {
156 perror("fork");
157 exit(EXIT_FAILURE);
158 }
159 else if (pid > 0)
160 {
161 print_startup(pid);
162 exit(EXIT_SUCCESS);
163 }
164
165 setsid();
166 }
167 #endif
168
169 /*
170 * get_maxrss - get the operating systems notion of the resident set size
171 */
172 unsigned long
173 get_maxrss(void)
174 {
175 #ifdef _WIN32
176 return (0); /* FIXME */
177 #else
178 return (get_vm_top() - initialVMTop);
179 #endif
180 }
181
182 static int printVersion = 0;
183
184 struct lgetopt myopts[] = {
185 {"dlinefile", &ConfigFileEntry.dlinefile,
186 STRING, "File to use for dline.conf"},
187 {"configfile", &ConfigFileEntry.configfile,
188 STRING, "File to use for ircd.conf"},
189 {"klinefile", &ConfigFileEntry.klinefile,
190 STRING, "File to use for kline.conf"},
191 {"xlinefile", &ConfigFileEntry.xlinefile,
192 STRING, "File to use for xline.conf"},
193 {"logfile", &logFileName,
194 STRING, "File to use for ircd.log"},
195 {"pidfile", &pidFileName,
196 STRING, "File to use for process ID"},
197 {"foreground", &server_state.foreground,
198 YESNO, "Run in foreground (don't detach)"},
199 {"version", &printVersion,
200 YESNO, "Print version and exit"},
201 {"help", NULL, USAGE, "Print this text"},
202 {NULL, NULL, STRING, NULL},
203 };
204
205 static void
206 io_loop(void)
207 {
208 while (1 == 1)
209 {
210 /*
211 * Maybe we want a flags word?
212 * ie. if (REHASHED_KLINES(global_flags))
213 * SET_REHASHED_KLINES(global_flags)
214 * CLEAR_REHASHED_KLINES(global_flags)
215 *
216 * - Dianora
217 */
218 if (rehashed_klines)
219 {
220 check_conf_klines();
221 rehashed_klines = 0;
222 }
223
224 if (listing_client_list.head)
225 {
226 dlink_node *ptr = NULL, *ptr_next = NULL;
227 DLINK_FOREACH_SAFE(ptr, ptr_next, listing_client_list.head)
228 {
229 struct Client *client_p = ptr->data;
230 assert(client_p->localClient->list_task);
231 safe_list_channels(client_p, client_p->localClient->list_task, 0);
232 }
233 }
234
235 /* Run pending events, then get the number of seconds to the next
236 * event
237 */
238 while (eventNextTime() <= CurrentTime)
239 eventRun();
240
241 comm_select();
242 exit_aborted_clients();
243 free_exited_clients();
244 send_queued_all();
245
246 /* Check to see whether we have to rehash the configuration .. */
247 if (dorehash)
248 {
249 rehash(1);
250 dorehash = 0;
251 }
252
253 if (doremotd)
254 {
255 read_message_file(&ConfigFileEntry.motd);
256 sendto_realops_flags(UMODE_ALL, L_ALL,
257 "Got signal SIGUSR1, reloading ircd motd file");
258 doremotd = 0;
259 }
260 }
261 }
262
263 /* initalialize_global_set_options()
264 *
265 * inputs - none
266 * output - none
267 * side effects - This sets all global set options needed
268 * XXX to be removed with old s_conf.c
269 */
270 static void
271 initialize_global_set_options(void)
272 {
273 memset(&GlobalSetOptions, 0, sizeof(GlobalSetOptions));
274
275 GlobalSetOptions.autoconn = 1;
276 GlobalSetOptions.spam_time = MIN_JOIN_LEAVE_TIME;
277 GlobalSetOptions.spam_num = MAX_JOIN_LEAVE_COUNT;
278
279 if (ConfigFileEntry.default_floodcount)
280 GlobalSetOptions.floodcount = ConfigFileEntry.default_floodcount;
281 else
282 GlobalSetOptions.floodcount = 10;
283
284 /* XXX I have no idea what to try here - Dianora */
285 GlobalSetOptions.joinfloodcount = 16;
286 GlobalSetOptions.joinfloodtime = 8;
287
288 split_servers = ConfigChannel.default_split_server_count;
289 split_users = ConfigChannel.default_split_user_count;
290
291 if (split_users && split_servers && (ConfigChannel.no_create_on_split ||
292 ConfigChannel.no_join_on_split))
293 {
294 splitmode = 1;
295 splitchecking = 1;
296 }
297
298 GlobalSetOptions.ident_timeout = IDENT_TIMEOUT;
299 GlobalSetOptions.idletime = ConfigFileEntry.idletime;
300 GlobalSetOptions.maxlisters = 10; /* XXX ya ya ya - db */
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("HUB", CAP_HUB, 0);
334 add_capability("TS6", CAP_TS6, 0);
335 add_capability("ZIP", CAP_ZIP, 0);
336 add_capability("CLUSTER", CAP_CLUSTER, 1);
337 #ifdef HALFOPS
338 add_capability("HOPS", CAP_HOPS, 1);
339 #endif
340 }
341
342 /* write_pidfile()
343 *
344 * inputs - filename+path of pid file
345 * output - NONE
346 * side effects - write the pid of the ircd to filename
347 */
348 static void
349 write_pidfile(const char *filename)
350 {
351 FBFILE *fb;
352
353 if ((fb = fbopen(filename, "w")))
354 {
355 char buff[32];
356 unsigned int pid = (unsigned int)getpid();
357 size_t nbytes = ircsprintf(buff, "%u\n", pid);
358
359 if ((fbputs(buff, fb, nbytes) == -1))
360 ilog(L_ERROR, "Error writing %u to pid file %s (%s)",
361 pid, filename, strerror(errno));
362
363 fbclose(fb);
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 #ifndef _WIN32
383 FBFILE *fb;
384 char buff[32];
385 pid_t pidfromfile;
386
387 /* Don't do logging here, since we don't have log() initialised */
388 if ((fb = fbopen(filename, "r")))
389 {
390 if (fbgets(buff, 20, fb) == NULL)
391 {
392 /* log(L_ERROR, "Error reading from pid file %s (%s)", filename,
393 * strerror(errno));
394 */
395 }
396 else
397 {
398 pidfromfile = atoi(buff);
399
400 if (!kill(pidfromfile, 0))
401 {
402 /* log(L_ERROR, "Server is already running"); */
403 printf("ircd: daemon is already running\n");
404 exit(-1);
405 }
406 }
407
408 fbclose(fb);
409 }
410 else if (errno != ENOENT)
411 {
412 /* log(L_ERROR, "Error opening pid file %s", filename); */
413 }
414 #endif
415 }
416
417 /* setup_corefile()
418 *
419 * inputs - nothing
420 * output - nothing
421 * side effects - setups corefile to system limits.
422 * -kre
423 */
424 static void
425 setup_corefile(void)
426 {
427 #ifdef HAVE_SYS_RESOURCE_H
428 struct rlimit rlim; /* resource limits */
429
430 /* Set corefilesize to maximum */
431 if (!getrlimit(RLIMIT_CORE, &rlim))
432 {
433 rlim.rlim_cur = rlim.rlim_max;
434 setrlimit(RLIMIT_CORE, &rlim);
435 }
436 #endif
437 }
438
439 /* init_ssl()
440 *
441 * inputs - nothing
442 * output - nothing
443 * side effects - setups SSL context.
444 */
445 static void
446 init_ssl(void)
447 {
448 #ifdef HAVE_LIBCRYPTO
449 SSL_library_init();
450 SSL_load_error_strings();
451
452 ServerInfo.ctx = SSL_CTX_new(SSLv23_server_method());
453
454 if (!ServerInfo.ctx)
455 {
456 const char *s;
457
458 fprintf(stderr, "ERROR: Could not initialize the SSL context -- %s\n",
459 s = ERR_lib_error_string(ERR_get_error()));
460 ilog(L_CRIT, "ERROR: Could not initialize the SSL context -- %s\n", s);
461 }
462
463 SSL_CTX_set_options(ServerInfo.ctx, SSL_OP_NO_SSLv2);
464 SSL_CTX_set_options(ServerInfo.ctx, SSL_OP_TLS_ROLLBACK_BUG|SSL_OP_ALL);
465 SSL_CTX_set_verify(ServerInfo.ctx, SSL_VERIFY_NONE, NULL);
466
467 bio_spare_fd = save_spare_fd("SSL private key validation");
468 #endif /* HAVE_LIBCRYPTO */
469 }
470
471 /* init_callbacks()
472 *
473 * inputs - nothing
474 * output - nothing
475 * side effects - setups standard hook points
476 */
477 static void
478 init_callbacks(void)
479 {
480 entering_umode_cb = register_callback("entering_umode", NULL);
481 iorecv_cb = register_callback("iorecv", iorecv_default);
482 iosend_cb = register_callback("iosend", iosend_default);
483 iorecvctrl_cb = register_callback("iorecvctrl", NULL);
484 iosendctrl_cb = register_callback("iosendctrl", NULL);
485 uid_get_cb = register_callback("uid_get", uid_get);
486 umode_cb = register_callback("changing_umode", change_simple_umode);
487 }
488
489 static void *
490 changing_fdlimit(va_list args)
491 {
492 int old_fdlimit = hard_fdlimit;
493 int fdmax = va_arg(args, int);
494
495 /* allow MAXCLIENTS_MIN clients even at the cost of MAX_BUFFER and
496 * some not really LEAKED_FDS */
497 fdmax = IRCD_MAX(fdmax, LEAKED_FDS + MAX_BUFFER + MAXCLIENTS_MIN);
498
499 pass_callback(fdlimit_hook, fdmax);
500
501 if (ServerInfo.max_clients > MAXCLIENTS_MAX)
502 {
503 if (old_fdlimit != 0)
504 sendto_realops_flags(UMODE_ALL, L_ALL,
505 "HARD_FDLIMIT changed to %d, adjusting MAXCLIENTS to %d",
506 hard_fdlimit, MAXCLIENTS_MAX);
507
508 ServerInfo.max_clients = MAXCLIENTS_MAX;
509 }
510
511 return NULL;
512 }
513
514 EXTERN int
515 main(int argc, char *argv[])
516 {
517 /* Check to see if the user is running
518 * us as root, which is a nono
519 */
520 #ifndef _WIN32
521 if (geteuid() == 0)
522 {
523 fprintf(stderr, "Don't run ircd as root!!!\n");
524 return 1;
525 }
526
527 /* Setup corefile size immediately after boot -kre */
528 setup_corefile();
529
530 /* set initialVMTop before we allocate any memory */
531 initialVMTop = get_vm_top();
532 #endif
533
534 memset(&ServerInfo, 0, sizeof(ServerInfo));
535
536 ConfigFileEntry.dpath = DPATH;
537 ConfigFileEntry.configfile = CPATH; /* Server configuration file */
538 ConfigFileEntry.klinefile = KPATH; /* Server kline file */
539 ConfigFileEntry.xlinefile = XPATH; /* Server xline file */
540 ConfigFileEntry.rxlinefile = RXPATH; /* Server regex xline file */
541 ConfigFileEntry.rklinefile = RKPATH; /* Server regex kline file */
542 ConfigFileEntry.dlinefile = DLPATH; /* dline file */
543 ConfigFileEntry.glinefile = GPATH; /* gline log file */
544 ConfigFileEntry.cresvfile = CRESVPATH; /* channel resv file */
545 ConfigFileEntry.nresvfile = NRESVPATH; /* nick resv file */
546 myargv = argv;
547 umask(077); /* better safe than sorry --SRB */
548
549 parseargs(&argc, &argv, myopts);
550
551 if (printVersion)
552 {
553 printf("ircd: version %s\n", ircd_version);
554 exit(EXIT_SUCCESS);
555 }
556
557 if (chdir(ConfigFileEntry.dpath))
558 {
559 perror("chdir");
560 exit(EXIT_FAILURE);
561 }
562
563 init_ssl();
564
565 #ifndef _WIN32
566 if (!server_state.foreground)
567 make_daemon();
568 else
569 print_startup(getpid());
570 #endif
571
572 libio_init(!server_state.foreground);
573 outofmemory = ircd_outofmemory;
574 fdlimit_hook = install_hook(fdlimit_cb, changing_fdlimit);
575
576 check_pidfile(pidFileName);
577 setup_signals();
578 get_ircd_platform(ircd_platform);
579 init_log(logFileName);
580 ServerInfo.can_use_v6 = check_can_use_v6();
581
582 /* make_dlink_node() cannot be called until after libio_init() */
583 memset(&me, 0, sizeof(me));
584 memset(&meLocalUser, 0, sizeof(meLocalUser));
585 me.localClient = &meLocalUser;
586 me.from = me.servptr = &me;
587 me.lasttime = me.since = me.firsttime = CurrentTime;
588
589 SetMe(&me);
590 make_server(&me);
591 dlinkAdd(&me, &me.node, &global_client_list);
592 dlinkAdd(&me, make_dlink_node(), &global_serv_list);
593
594 init_callbacks();
595 initialize_message_files();
596 init_hash();
597 init_ip_hash_table(); /* client host ip hash table */
598 init_host_hash(); /* Host-hashtable. */
599 clear_tree_parse();
600 init_client();
601 init_class();
602 init_whowas();
603 init_stats();
604 init_auth(); /* Initialise the auth code */
605 init_channels();
606 init_channel_modes();
607 initialize_server_capabs(); /* Set up default_server_capabs */
608 ilog(L_NOTICE, "%s %s", IRCD_PREFIX, AUTOMODPATH);
609 read_conf_files(1); /* cold start init conf files */
610 check_class();
611 init_watch();
612
613 if (ServerInfo.name == NULL)
614 {
615 ilog(L_CRIT, "No server name specified in serverinfo block.");
616 exit(EXIT_FAILURE);
617 }
618
619 strlcpy(me.name, ServerInfo.name, sizeof(me.name));
620
621 /* serverinfo{} description must exist. If not, error out.*/
622 if (ServerInfo.description == NULL)
623 {
624 ilog(L_CRIT,
625 "ERROR: No server description specified in serverinfo block.");
626 exit(EXIT_FAILURE);
627 }
628
629 strlcpy(me.info, ServerInfo.description, sizeof(me.info));
630
631 hash_add_client(&me);
632
633 init_uid(); /* XXX move this one up after inculcating new conf system */
634 initialize_global_set_options(); /* and this one is going to be deleted */
635
636 #ifndef STATIC_MODULES
637 if (chdir(MODPATH))
638 {
639 ilog (L_CRIT, "Could not load core modules. Terminating!");
640 exit(EXIT_FAILURE);
641 }
642
643 boot_modules(1);
644
645 /* Go back to DPATH after checking to see if we can chdir to MODPATH */
646 chdir(ConfigFileEntry.dpath);
647 #else
648 load_all_modules(1);
649 #endif
650 /*
651 * assemble_umode_buffer() has to be called after
652 * reading conf/loading modules.
653 */
654 assemble_umode_buffer();
655
656 write_pidfile(pidFileName);
657
658 ilog(L_NOTICE, "Server Ready");
659
660 eventAddIsh("cleanup_glines", cleanup_glines, NULL, CLEANUP_GLINES_TIME);
661 eventAddIsh("cleanup_tklines", cleanup_tklines, NULL, CLEANUP_TKLINES_TIME);
662
663 /* We want try_connections to be called as soon as possible now! -- adrian */
664 /* No, 'cause after a restart it would cause all sorts of nick collides */
665 eventAddIsh("try_connections", try_connections, NULL, STARTUP_CONNECTIONS_TIME);
666
667 eventAddIsh("collect_zipstats", collect_zipstats, NULL, ZIPSTATS_TIME);
668
669 /* Setup the timeout check. I'll shift it later :) -- adrian */
670 eventAddIsh("comm_checktimeouts", comm_checktimeouts, NULL, 1);
671
672 /* XXX to be removed with old s_conf.c [superseded] */
673 if (ConfigServerHide.links_delay > 0)
674 eventAddIsh("write_links_file", write_links_file, NULL, ConfigServerHide.links_delay);
675 else
676 ConfigServerHide.links_disabled = 1;
677
678 if (splitmode) /* XXX */
679 eventAddIsh("check_splitmode", check_splitmode, NULL, 60);
680
681 io_loop();
682 return 0;
683 }
684
685 #else
686
687 #include <windows.h>
688
689 extern __declspec(dllimport) main(int, char **);
690
691 /*
692 * Initial entry point for Win32 GUI applications, called by the C runtime.
693 *
694 * It should be only a wrapper for main(), since when compiled as a console
695 * application, main() is called instead.
696 */
697 int WINAPI
698 WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
699 LPSTR lpCmdLine, int nCmdShow)
700 {
701 /* Do we really need these pidfile, logfile etc arguments?
702 * And we are not on a console, so -help or -foreground is meaningless. */
703
704 char *argv[2] = {"ircd", NULL};
705
706 return main(1, argv);
707 }
708
709 #endif

Properties

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