ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/ircd.c
Revision: 6375
Committed: Fri Aug 21 10:34:16 2015 UTC (10 years ago) by michael
Content type: text/x-csrc
File size: 15766 byte(s)
Log Message:
- Remove splitmode checking

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

Properties

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