ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/ircd.c
Revision: 5805
Committed: Tue Apr 21 21:49:58 2015 UTC (10 years, 4 months ago) by michael
Content type: text/x-csrc
File size: 16254 byte(s)
Log Message:
- Removed glines
- Added kline_min_cidr, kline_min_cidr6, dline_min_cidr and
  dline_min_cidr6 configuration options

File Contents

# Content
1 /*
2 * ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd)
3 *
4 * Copyright (c) 1997-2015 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
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 "user.h"
29 #include "list.h"
30 #include "ircd.h"
31 #include "channel.h"
32 #include "client.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 "motd.h"
39 #include "conf.h"
40 #include "hostmask.h"
41 #include "parse.h"
42 #include "res.h"
43 #include "restart.h"
44 #include "rng_mt.h"
45 #include "auth.h"
46 #include "s_bsd.h"
47 #include "log.h"
48 #include "server.h" /* try_connections */
49 #include "send.h"
50 #include "whowas.h"
51 #include "modules.h"
52 #include "memory.h"
53 #include "mempool.h"
54 #include "ircd_getopt.h"
55 #include "watch.h"
56 #include "conf_db.h"
57 #include "conf_class.h"
58 #include "ipcache.h"
59
60
61 #ifdef HAVE_LIBGEOIP
62 GeoIP *geoip_ctx;
63 #endif
64
65 struct SetOptions GlobalSetOptions; /* /quote set variables */
66 struct Counter Count;
67 struct ServerState_t server_state;
68 struct ServerStatistics ServerStats;
69 struct timeval SystemTime;
70 struct Connection meConnection; /* That's also part of me */
71 struct Client me = { .connection = &meConnection }; /* That's me */
72
73 char **myargv;
74 const char *logFileName = LPATH;
75 const char *pidFileName = PPATH;
76
77 unsigned int dorehash;
78 unsigned int doremotd;
79 unsigned int splitmode;
80 unsigned int splitchecking;
81 unsigned int split_users;
82 unsigned int split_servers;
83
84 static struct event event_cleanup_tklines =
85 {
86 .name = "cleanup_tklines",
87 .handler = cleanup_tklines,
88 .when = CLEANUP_TKLINES_TIME
89 };
90
91 static struct event event_try_connections =
92 {
93 .name = "try_connections",
94 .handler = try_connections,
95 .when = STARTUP_CONNECTIONS_TIME
96 };
97
98 static struct event event_comm_checktimeouts =
99 {
100 .name = "comm_checktimeouts",
101 .handler = comm_checktimeouts,
102 .when = 1
103 };
104
105 static struct event event_save_all_databases =
106 {
107 .name = "save_all_databases",
108 .handler = save_all_databases,
109 .when = DATABASE_UPDATE_TIMEOUT
110 };
111
112 struct event event_write_links_file =
113 {
114 .name = "write_links_file",
115 .handler = write_links_file,
116 };
117
118
119 /*
120 * print_startup - print startup information
121 */
122 static void
123 print_startup(int pid)
124 {
125 printf("ircd: version %s(%s)\n", ircd_version, serno);
126 printf("ircd: pid %d\n", pid);
127 printf("ircd: running in %s mode from %s\n", !server_state.foreground ? "background"
128 : "foreground", ConfigGeneral.dpath);
129 }
130
131 static void
132 make_daemon(void)
133 {
134 int pid;
135
136 if ((pid = fork()) < 0)
137 {
138 perror("fork");
139 exit(EXIT_FAILURE);
140 }
141 else if (pid > 0)
142 {
143 print_startup(pid);
144 exit(EXIT_SUCCESS);
145 }
146
147 setsid();
148 }
149
150 static int printVersion = 0;
151
152 static struct lgetopt myopts[] =
153 {
154 {"configfile", &ConfigGeneral.configfile,
155 STRING, "File to use for ircd.conf"},
156 {"klinefile", &ConfigGeneral.klinefile,
157 STRING, "File to use for kline database"},
158 {"dlinefile", &ConfigGeneral.dlinefile,
159 STRING, "File to use for dline database"},
160 {"xlinefile", &ConfigGeneral.xlinefile,
161 STRING, "File to use for xline database"},
162 {"resvfile", &ConfigGeneral.resvfile,
163 STRING, "File to use for resv database"},
164 {"logfile", &logFileName,
165 STRING, "File to use for ircd.log"},
166 {"pidfile", &pidFileName,
167 STRING, "File to use for process ID"},
168 {"foreground", &server_state.foreground,
169 YESNO, "Run in foreground (don't detach)"},
170 {"version", &printVersion,
171 YESNO, "Print version and exit"},
172 {"help", NULL, USAGE, "Print this text"},
173 {NULL, NULL, STRING, NULL},
174 };
175
176 void
177 set_time(void)
178 {
179 struct timeval newtime = { .tv_sec = 0, .tv_usec = 0 };
180
181 if (gettimeofday(&newtime, NULL) == -1)
182 {
183 ilog(LOG_TYPE_IRCD, "Clock Failure (%s), TS can be corrupted",
184 strerror(errno));
185 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
186 "Clock Failure (%s), TS can be corrupted",
187 strerror(errno));
188 server_die("Clock Failure", 1);
189 }
190
191 if (newtime.tv_sec < CurrentTime)
192 {
193 ilog(LOG_TYPE_IRCD, "System clock is running backwards - (%lu < %lu)",
194 (unsigned long)newtime.tv_sec, (unsigned long)CurrentTime);
195 sendto_realops_flags(UMODE_DEBUG, L_ALL, SEND_NOTICE,
196 "System clock is running backwards - (%lu < %lu)",
197 (unsigned long)newtime.tv_sec,
198 (unsigned long)CurrentTime);
199 set_back_events(CurrentTime - newtime.tv_sec);
200 }
201
202 SystemTime.tv_sec = newtime.tv_sec;
203 SystemTime.tv_usec = newtime.tv_usec;
204 }
205
206 static void
207 io_loop(void)
208 {
209 while (1)
210 {
211 if (listing_client_list.head)
212 {
213 dlink_node *node = NULL, *node_next = NULL;
214 DLINK_FOREACH_SAFE(node, node_next, listing_client_list.head)
215 safe_list_channels(node->data, 0);
216 }
217
218 /* Run pending events */
219 event_run();
220
221 comm_select();
222 exit_aborted_clients();
223 free_exited_clients();
224
225 /* Check to see whether we have to rehash the configuration .. */
226 if (dorehash)
227 {
228 conf_rehash(1);
229 dorehash = 0;
230 }
231
232 if (doremotd)
233 {
234 motd_recache();
235 sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
236 "Got signal SIGUSR1, reloading motd file(s)");
237 doremotd = 0;
238 }
239 }
240 }
241
242 /* initalialize_global_set_options()
243 *
244 * inputs - none
245 * output - none
246 * side effects - This sets all global set options needed
247 */
248 static void
249 initialize_global_set_options(void)
250 {
251 GlobalSetOptions.maxclients = ConfigServerInfo.default_max_clients;
252 GlobalSetOptions.autoconn = 1;
253 GlobalSetOptions.spam_time = MIN_JOIN_LEAVE_TIME;
254 GlobalSetOptions.spam_num = MAX_JOIN_LEAVE_COUNT;
255 GlobalSetOptions.floodcount = ConfigGeneral.default_floodcount;
256 GlobalSetOptions.joinfloodcount = ConfigChannel.default_join_flood_count;
257 GlobalSetOptions.joinfloodtime = ConfigChannel.default_join_flood_time;
258
259 split_servers = ConfigChannel.default_split_server_count;
260 split_users = ConfigChannel.default_split_user_count;
261
262 if (split_users && split_servers && (ConfigChannel.no_create_on_split ||
263 ConfigChannel.no_join_on_split))
264 {
265 splitmode = 1;
266 splitchecking = 1;
267 }
268
269 GlobalSetOptions.ident_timeout = IDENT_TIMEOUT;
270 }
271
272 /* initialize_server_capabs()
273 *
274 * inputs - none
275 * output - none
276 */
277 static void
278 initialize_server_capabs(void)
279 {
280 add_capability("QS", CAP_QS);
281 add_capability("EOB", CAP_EOB);
282 add_capability("CLUSTER", CAP_CLUSTER);
283 add_capability("SVS", CAP_SVS);
284 add_capability("CHW", CAP_CHW);
285 add_capability("HOPS", CAP_HOPS);
286 }
287
288 /* write_pidfile()
289 *
290 * inputs - filename+path of pid file
291 * output - NONE
292 * side effects - write the pid of the ircd to filename
293 */
294 static void
295 write_pidfile(const char *filename)
296 {
297 FILE *fb;
298
299 if ((fb = fopen(filename, "w")))
300 {
301 char buff[IRCD_BUFSIZE];
302 unsigned int pid = (unsigned int)getpid();
303
304 snprintf(buff, sizeof(buff), "%u\n", pid);
305
306 if (fputs(buff, fb) == -1)
307 ilog(LOG_TYPE_IRCD, "Error writing to pid file %s: %s",
308 filename, strerror(errno));
309
310 fclose(fb);
311 }
312 else
313 ilog(LOG_TYPE_IRCD, "Error opening pid file %s: %s",
314 filename, strerror(errno));
315 }
316
317 /* check_pidfile()
318 *
319 * inputs - filename+path of pid file
320 * output - none
321 * side effects - reads pid from pidfile and checks if ircd is in process
322 * list. if it is, gracefully exits
323 * -kre
324 */
325 static void
326 check_pidfile(const char *filename)
327 {
328 FILE *fb;
329 char buff[IRCD_BUFSIZE];
330 pid_t pidfromfile;
331
332 /* Don't do logging here, since we don't have log() initialised */
333 if ((fb = fopen(filename, "r")))
334 {
335 if (!fgets(buff, 20, fb))
336 {
337 /* log(L_ERROR, "Error reading from pid file %s (%s)", filename,
338 * strerror(errno));
339 */
340 }
341 else
342 {
343 pidfromfile = atoi(buff);
344
345 if (!kill(pidfromfile, 0))
346 {
347 /* log(L_ERROR, "Server is already running"); */
348 printf("ircd: daemon is already running\n");
349 exit(-1);
350 }
351 }
352
353 fclose(fb);
354 }
355 else if (errno != ENOENT)
356 {
357 /* log(L_ERROR, "Error opening pid file %s", filename); */
358 }
359 }
360
361 /* setup_corefile()
362 *
363 * inputs - nothing
364 * output - nothing
365 * side effects - setups corefile to system limits.
366 * -kre
367 */
368 static void
369 setup_corefile(void)
370 {
371 #ifdef HAVE_SYS_RESOURCE_H
372 struct rlimit rlim; /* resource limits */
373
374 /* Set corefilesize to maximum */
375 if (!getrlimit(RLIMIT_CORE, &rlim))
376 {
377 rlim.rlim_cur = rlim.rlim_max;
378 setrlimit(RLIMIT_CORE, &rlim);
379 }
380 #endif
381 }
382
383 #ifdef HAVE_LIBCRYPTO
384 static int
385 always_accept_verify_cb(int preverify_ok, X509_STORE_CTX *x509_ctx)
386 {
387 return 1;
388 }
389 #endif
390
391 /* init_ssl()
392 *
393 * inputs - nothing
394 * output - nothing
395 * side effects - setups SSL context.
396 */
397 static void
398 ssl_init(void)
399 {
400 #ifdef HAVE_LIBCRYPTO
401 SSL_load_error_strings();
402 SSLeay_add_ssl_algorithms();
403
404 if (!(ConfigServerInfo.server_ctx = SSL_CTX_new(SSLv23_server_method())))
405 {
406 const char *s = ERR_lib_error_string(ERR_get_error());
407
408 fprintf(stderr, "ERROR: Could not initialize the SSL Server context -- %s\n", s);
409 ilog(LOG_TYPE_IRCD, "ERROR: Could not initialize the SSL Server context -- %s", s);
410 exit(EXIT_FAILURE);
411 return; /* Not reached */
412 }
413
414 SSL_CTX_set_options(ConfigServerInfo.server_ctx, SSL_OP_NO_SSLv2|SSL_OP_NO_SSLv3|SSL_OP_NO_TICKET);
415 SSL_CTX_set_options(ConfigServerInfo.server_ctx, SSL_OP_SINGLE_DH_USE|SSL_OP_CIPHER_SERVER_PREFERENCE);
416 SSL_CTX_set_verify(ConfigServerInfo.server_ctx, SSL_VERIFY_PEER|SSL_VERIFY_CLIENT_ONCE,
417 always_accept_verify_cb);
418 SSL_CTX_set_session_cache_mode(ConfigServerInfo.server_ctx, SSL_SESS_CACHE_OFF);
419 SSL_CTX_set_cipher_list(ConfigServerInfo.server_ctx, "EECDH+HIGH:EDH+HIGH:HIGH:!aNULL");
420
421 #if OPENSSL_VERSION_NUMBER >= 0x009080FFL && !defined(OPENSSL_NO_ECDH)
422 {
423 EC_KEY *key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
424
425 if (key)
426 {
427 SSL_CTX_set_tmp_ecdh(ConfigServerInfo.server_ctx, key);
428 EC_KEY_free(key);
429 }
430 }
431
432 SSL_CTX_set_options(ConfigServerInfo.server_ctx, SSL_OP_SINGLE_ECDH_USE);
433 #endif
434
435 if (!(ConfigServerInfo.client_ctx = SSL_CTX_new(SSLv23_client_method())))
436 {
437 const char *s = ERR_lib_error_string(ERR_get_error());
438
439 fprintf(stderr, "ERROR: Could not initialize the SSL Client context -- %s\n", s);
440 ilog(LOG_TYPE_IRCD, "ERROR: Could not initialize the SSL Client context -- %s", s);
441 exit(EXIT_FAILURE);
442 return; /* Not reached */
443 }
444
445 SSL_CTX_set_options(ConfigServerInfo.client_ctx, SSL_OP_NO_SSLv2|SSL_OP_NO_SSLv3|SSL_OP_NO_TICKET);
446 SSL_CTX_set_options(ConfigServerInfo.client_ctx, SSL_OP_SINGLE_DH_USE);
447 SSL_CTX_set_verify(ConfigServerInfo.client_ctx, SSL_VERIFY_PEER|SSL_VERIFY_CLIENT_ONCE,
448 always_accept_verify_cb);
449 SSL_CTX_set_session_cache_mode(ConfigServerInfo.client_ctx, SSL_SESS_CACHE_OFF);
450 #endif /* HAVE_LIBCRYPTO */
451 }
452
453 int
454 main(int argc, char *argv[])
455 {
456 /* Check to see if the user is running us as root, which is a nono */
457 if (!geteuid())
458 {
459 fprintf(stderr, "ERROR: This server won't run as root/superuser\n");
460 return -1;
461 }
462
463 /* Setup corefile size immediately after boot -kre */
464 setup_corefile();
465
466 /* Save server boot time right away, so getrusage works correctly */
467 set_time();
468
469 /* It's not random, but it ought to be a little harder to guess */
470 init_genrand(SystemTime.tv_sec ^ (SystemTime.tv_usec | (getpid() << 20)));
471
472 dlinkAdd(&me, &me.node, &global_client_list);
473
474 ConfigGeneral.dpath = DPATH;
475 ConfigGeneral.spath = SPATH;
476 ConfigGeneral.mpath = MPATH;
477 ConfigGeneral.configfile = CPATH; /* Server configuration file */
478 ConfigGeneral.klinefile = KPATH; /* Server kline file */
479 ConfigGeneral.xlinefile = XPATH; /* Server xline file */
480 ConfigGeneral.dlinefile = DLPATH; /* dline file */
481 ConfigGeneral.resvfile = RESVPATH; /* resv file */
482
483 myargv = argv;
484 umask(077); /* umask 077: u=rwx,g=,o= */
485
486 parseargs(&argc, &argv, myopts);
487
488 if (printVersion)
489 {
490 printf("ircd: version %s(%s)\n", ircd_version, serno);
491 exit(EXIT_SUCCESS);
492 }
493
494 if (chdir(ConfigGeneral.dpath))
495 {
496 perror("chdir");
497 exit(EXIT_FAILURE);
498 }
499
500 ssl_init();
501
502 if (!server_state.foreground)
503 {
504 make_daemon();
505 close_standard_fds(); /* this needs to be before init_netio()! */
506 }
507 else
508 print_startup(getpid());
509
510 setup_signals();
511
512 /* We need this to initialise the fd array before anything else */
513 fdlist_init();
514 log_set_file(LOG_TYPE_IRCD, 0, logFileName);
515
516 init_netio(); /* This needs to be setup early ! -- adrian */
517
518 /* Check if there is pidfile and daemon already running */
519 check_pidfile(pidFileName);
520
521 mp_pool_init();
522 init_dlink_nodes();
523 init_isupport();
524 dbuf_init();
525 hash_init();
526 ipcache_init();
527 client_init();
528 class_init();
529 whowas_init();
530 watch_init();
531 auth_init(); /* Initialise the auth code */
532 init_resolver(); /* Needs to be setup before the io loop */
533 modules_init();
534 read_conf_files(1); /* cold start init conf files */
535 init_uid();
536 initialize_server_capabs(); /* Set up default_server_capabs */
537 initialize_global_set_options(); /* Has to be called after read_conf_files() */
538 channel_init();
539 read_links_file();
540 motd_init();
541 user_usermodes_init();
542 #ifdef HAVE_LIBGEOIP
543 geoip_ctx = GeoIP_new(GEOIP_MEMORY_CACHE);
544 #endif
545
546 if (EmptyString(ConfigServerInfo.sid))
547 {
548 ilog(LOG_TYPE_IRCD, "ERROR: No server id specified in serverinfo block.");
549 exit(EXIT_FAILURE);
550 }
551
552 strlcpy(me.id, ConfigServerInfo.sid, sizeof(me.id));
553
554 if (EmptyString(ConfigServerInfo.name))
555 {
556 ilog(LOG_TYPE_IRCD, "ERROR: No server name specified in serverinfo block.");
557 exit(EXIT_FAILURE);
558 }
559
560 strlcpy(me.name, ConfigServerInfo.name, sizeof(me.name));
561
562 /* serverinfo{} description must exist. If not, error out.*/
563 if (EmptyString(ConfigServerInfo.description))
564 {
565 ilog(LOG_TYPE_IRCD, "ERROR: No server description specified in serverinfo block.");
566 exit(EXIT_FAILURE);
567 }
568
569 strlcpy(me.info, ConfigServerInfo.description, sizeof(me.info));
570
571 me.from = &me;
572 me.servptr = &me;
573 me.connection->lasttime = CurrentTime;
574 me.connection->since = CurrentTime;
575 me.connection->firsttime = CurrentTime;
576
577 SetMe(&me);
578 make_server(&me);
579
580 hash_add_id(&me);
581 hash_add_client(&me);
582
583 dlinkAdd(&me, make_dlink_node(), &global_server_list);
584
585 load_kline_database();
586 load_dline_database();
587 load_xline_database();
588 load_resv_database();
589
590 load_all_modules(1);
591 load_conf_modules();
592 load_core_modules(1);
593
594 write_pidfile(pidFileName);
595
596 ilog(LOG_TYPE_IRCD, "Server Ready");
597
598 event_addish(&event_cleanup_tklines, NULL);
599
600 /* We want try_connections to be called as soon as possible now! -- adrian */
601 /* No, 'cause after a restart it would cause all sorts of nick collides */
602 event_addish(&event_try_connections, NULL);
603
604 /* Setup the timeout check. I'll shift it later :) -- adrian */
605 event_add(&event_comm_checktimeouts, NULL);
606
607 event_addish(&event_save_all_databases, NULL);
608
609 if (ConfigServerHide.links_delay > 0)
610 {
611 event_write_links_file.when = ConfigServerHide.links_delay;
612 event_addish(&event_write_links_file, NULL);
613 }
614 else
615 ConfigServerHide.links_disabled = 1;
616
617 if (splitmode)
618 event_addish(&splitmode_event, NULL);
619
620 io_loop();
621 return 0;
622 }

Properties

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