ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/ircd.c
Revision: 1718
Committed: Thu Dec 27 19:03:00 2012 UTC (12 years, 8 months ago) by michael
Content type: text/x-csrc
File size: 17724 byte(s)
Log Message:
- src/ircd.c: minor updates to myopts[] commandline options

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

Properties

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