ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/ircd.c
Revision: 6802
Committed: Wed Nov 18 11:25:16 2015 UTC (8 years, 5 months ago) by michael
Content type: text/x-csrc
File size: 15527 byte(s)
Log Message:
- ircd.c: style correction

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

Properties

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