ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/ircd.c
Revision: 7006
Committed: Fri Jan 1 00:07:54 2016 UTC (9 years, 7 months ago) by michael
Content type: text/x-csrc
File size: 15618 byte(s)
Log Message:
- Update copyright years

File Contents

# User Rev Content
1 adx 30 /*
2 michael 2916 * ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd)
3 adx 30 *
4 michael 7006 * Copyright (c) 1997-2016 ircd-hybrid development team
5 adx 30 *
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 michael 4565 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
19 adx 30 * USA
20     */
21    
22 michael 2916 /*! \file ircd.c
23     * \brief Starts up and runs the ircd.
24     * \version $Id$
25     */
26    
27 adx 30 #include "stdinc.h"
28 michael 3347 #include "user.h"
29 michael 1011 #include "list.h"
30 adx 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 michael 6161 #include "id.h"
37 adx 30 #include "irc_string.h"
38     #include "ircd_signal.h"
39     #include "motd.h"
40 michael 1632 #include "conf.h"
41 adx 30 #include "hostmask.h"
42     #include "parse.h"
43 michael 3322 #include "res.h"
44 adx 30 #include "restart.h"
45 michael 982 #include "rng_mt.h"
46 michael 3324 #include "auth.h"
47 adx 30 #include "s_bsd.h"
48 michael 1309 #include "log.h"
49 michael 6481 #include "server.h"
50 adx 30 #include "send.h"
51     #include "whowas.h"
52     #include "modules.h"
53     #include "memory.h"
54 michael 1654 #include "mempool.h"
55 adx 30 #include "ircd_getopt.h"
56 michael 876 #include "watch.h"
57 michael 1622 #include "conf_db.h"
58 michael 1632 #include "conf_class.h"
59 michael 4325 #include "ipcache.h"
60 michael 6185 #include "isupport.h"
61 michael 6393 #include "userhost.h"
62 adx 30
63 michael 1858
64     #ifdef HAVE_LIBGEOIP
65     GeoIP *geoip_ctx;
66     #endif
67 michael 2872
68 michael 5737 struct SetOptions GlobalSetOptions; /* /quote set variables */
69 michael 5602 struct Counter Count;
70     struct ServerState_t server_state;
71     struct ServerStatistics ServerStats;
72 adx 30 struct timeval SystemTime;
73 michael 5737 struct Connection meConnection; /* That's also part of me */
74 michael 5470 struct Client me = { .connection = &meConnection }; /* That's me */
75 adx 30
76 michael 5460 char **myargv;
77 adx 30 const char *logFileName = LPATH;
78     const char *pidFileName = PPATH;
79    
80 michael 5460 unsigned int dorehash;
81     unsigned int doremotd;
82 adx 30
83 michael 6735 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 michael 4094 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 adx 30 void
145     set_time(void)
146     {
147 michael 2978 struct timeval newtime = { .tv_sec = 0, .tv_usec = 0 };
148 adx 30
149     if (gettimeofday(&newtime, NULL) == -1)
150     {
151 michael 6482 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 adx 30 }
157    
158     if (newtime.tv_sec < CurrentTime)
159     {
160 michael 6782 ilog(LOG_TYPE_IRCD, "System clock is running backwards - (%ju < %ju)",
161     newtime.tv_sec, CurrentTime);
162 michael 2980 sendto_realops_flags(UMODE_DEBUG, L_ALL, SEND_NOTICE,
163 michael 6782 "System clock is running backwards - (%ju < %ju)",
164 michael 6802 newtime.tv_sec, CurrentTime);
165 michael 6370 event_set_back_events(CurrentTime - newtime.tv_sec);
166 adx 30 }
167    
168 michael 6481 SystemTime.tv_sec = newtime.tv_sec;
169 adx 30 SystemTime.tv_usec = newtime.tv_usec;
170     }
171    
172     static void
173     io_loop(void)
174     {
175 michael 3215 while (1)
176 adx 30 {
177     if (listing_client_list.head)
178     {
179 michael 4815 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 adx 30 }
183    
184 michael 4094 /* Run pending events */
185     event_run();
186 adx 30
187     comm_select();
188     exit_aborted_clients();
189     free_exited_clients();
190    
191 michael 6735 /* Check to see whether we have to rehash the configuration. */
192 adx 30 if (dorehash)
193     {
194 michael 4982 conf_rehash(1);
195 adx 30 dorehash = 0;
196     }
197 michael 3215
198 adx 30 if (doremotd)
199     {
200 michael 2150 motd_recache();
201 michael 6318 sendto_realops_flags(UMODE_SERVNOTICE, L_ALL, SEND_NOTICE,
202 michael 3065 "Got signal SIGUSR1, reloading motd file(s)");
203 adx 30 doremotd = 0;
204     }
205     }
206     }
207    
208     /* initalialize_global_set_options()
209     *
210     * inputs - none
211     * output - none
212 michael 2916 * side effects - This sets all global set options needed
213 adx 30 */
214     static void
215     initialize_global_set_options(void)
216     {
217 michael 5489 GlobalSetOptions.maxclients = ConfigServerInfo.default_max_clients;
218 michael 5499 GlobalSetOptions.autoconn = 1;
219 adx 30 GlobalSetOptions.spam_time = MIN_JOIN_LEAVE_TIME;
220 michael 5499 GlobalSetOptions.spam_num = MAX_JOIN_LEAVE_COUNT;
221     GlobalSetOptions.floodcount = ConfigGeneral.default_floodcount;
222 michael 5489 GlobalSetOptions.joinfloodcount = ConfigChannel.default_join_flood_count;
223     GlobalSetOptions.joinfloodtime = ConfigChannel.default_join_flood_time;
224 adx 30 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 michael 6354 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 adx 30 }
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 michael 1325 FILE *fb;
253 adx 30
254 michael 1325 if ((fb = fopen(filename, "w")))
255 adx 30 {
256 michael 6470 char buf[IRCD_BUFSIZE];
257 adx 30 unsigned int pid = (unsigned int)getpid();
258    
259 michael 6470 snprintf(buf, sizeof(buf), "%u\n", pid);
260 michael 1325
261 michael 6470 if (fputs(buf, fb) == -1)
262 michael 5737 ilog(LOG_TYPE_IRCD, "Error writing to pid file %s: %s",
263     filename, strerror(errno));
264 adx 30
265 michael 1325 fclose(fb);
266 adx 30 }
267     else
268 michael 5566 ilog(LOG_TYPE_IRCD, "Error opening pid file %s: %s",
269 michael 4748 filename, strerror(errno));
270 adx 30 }
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 michael 1325 FILE *fb;
284 michael 6470 char buf[IRCD_BUFSIZE];
285 adx 30
286 michael 1325 if ((fb = fopen(filename, "r")))
287 adx 30 {
288 michael 6470 if (!fgets(buf, 20, fb))
289 michael 6260 ilog(LOG_TYPE_IRCD, "Error reading from pid file %s: %s",
290     filename, strerror(errno));
291 adx 30 else
292     {
293 michael 6481 pid_t pid = atoi(buf);
294 adx 30
295 michael 6481 if (!kill(pid, 0))
296 adx 30 {
297     /* log(L_ERROR, "Server is already running"); */
298     printf("ircd: daemon is already running\n");
299 michael 6646 exit(EXIT_FAILURE);
300 adx 30 }
301     }
302    
303 michael 1325 fclose(fb);
304 adx 30 }
305     else if (errno != ENOENT)
306 michael 6260 ilog(LOG_TYPE_IRCD, "Error opening pid file %s: %s",
307     filename, strerror(errno));
308 adx 30 }
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 michael 2228 #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 michael 6481 /* ssl_init()
341 adx 30 *
342     * inputs - nothing
343     * output - nothing
344     * side effects - setups SSL context.
345     */
346     static void
347 michael 1798 ssl_init(void)
348 adx 30 {
349     #ifdef HAVE_LIBCRYPTO
350     SSL_load_error_strings();
351     SSLeay_add_ssl_algorithms();
352    
353 michael 4733 if (!(ConfigServerInfo.server_ctx = SSL_CTX_new(SSLv23_server_method())))
354 adx 30 {
355 michael 4129 const char *s = ERR_lib_error_string(ERR_get_error());
356 adx 30
357 michael 4129 fprintf(stderr, "ERROR: Could not initialize the SSL Server context -- %s\n", s);
358 michael 4763 ilog(LOG_TYPE_IRCD, "ERROR: Could not initialize the SSL Server context -- %s", s);
359 michael 4497 exit(EXIT_FAILURE);
360 michael 4751 return; /* Not reached */
361 adx 30 }
362    
363 michael 4730 SSL_CTX_set_options(ConfigServerInfo.server_ctx, SSL_OP_NO_SSLv2|SSL_OP_NO_SSLv3|SSL_OP_NO_TICKET);
364 michael 4593 SSL_CTX_set_options(ConfigServerInfo.server_ctx, SSL_OP_SINGLE_DH_USE|SSL_OP_CIPHER_SERVER_PREFERENCE);
365 michael 4340 SSL_CTX_set_verify(ConfigServerInfo.server_ctx, SSL_VERIFY_PEER|SSL_VERIFY_CLIENT_ONCE,
366 michael 2228 always_accept_verify_cb);
367 michael 4730 SSL_CTX_set_session_cache_mode(ConfigServerInfo.server_ctx, SSL_SESS_CACHE_OFF);
368 michael 4892 SSL_CTX_set_cipher_list(ConfigServerInfo.server_ctx, "EECDH+HIGH:EDH+HIGH:HIGH:!aNULL");
369 adx 30
370 michael 4744 #if OPENSSL_VERSION_NUMBER >= 0x009080FFL && !defined(OPENSSL_NO_ECDH)
371 michael 4070 {
372     EC_KEY *key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
373    
374     if (key)
375     {
376 michael 4340 SSL_CTX_set_tmp_ecdh(ConfigServerInfo.server_ctx, key);
377 michael 4070 EC_KEY_free(key);
378     }
379     }
380    
381 michael 4340 SSL_CTX_set_options(ConfigServerInfo.server_ctx, SSL_OP_SINGLE_ECDH_USE);
382 michael 4070 #endif
383    
384 michael 4733 if (!(ConfigServerInfo.client_ctx = SSL_CTX_new(SSLv23_client_method())))
385 michael 1303 {
386 michael 4129 const char *s = ERR_lib_error_string(ERR_get_error());
387 michael 1303
388 michael 4129 fprintf(stderr, "ERROR: Could not initialize the SSL Client context -- %s\n", s);
389 michael 4763 ilog(LOG_TYPE_IRCD, "ERROR: Could not initialize the SSL Client context -- %s", s);
390 michael 4497 exit(EXIT_FAILURE);
391 michael 4751 return; /* Not reached */
392 michael 1303 }
393    
394 michael 4730 SSL_CTX_set_options(ConfigServerInfo.client_ctx, SSL_OP_NO_SSLv2|SSL_OP_NO_SSLv3|SSL_OP_NO_TICKET);
395 michael 4340 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 michael 2228 always_accept_verify_cb);
398 michael 4730 SSL_CTX_set_session_cache_mode(ConfigServerInfo.client_ctx, SSL_SESS_CACHE_OFF);
399 adx 30 #endif /* HAVE_LIBCRYPTO */
400     }
401    
402 michael 6735 /*
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 adx 30 int
434     main(int argc, char *argv[])
435     {
436 michael 2253 /* Check to see if the user is running us as root, which is a nono */
437 michael 4733 if (!geteuid())
438 adx 30 {
439 michael 3525 fprintf(stderr, "ERROR: This server won't run as root/superuser\n");
440 michael 982 return -1;
441 adx 30 }
442    
443     /* Setup corefile size immediately after boot -kre */
444     setup_corefile();
445    
446 michael 5545 /* Save server boot time right away, so getrusage works correctly */
447 adx 30 set_time();
448    
449 michael 5545 /* It's not random, but it ought to be a little harder to guess */
450 michael 982 init_genrand(SystemTime.tv_sec ^ (SystemTime.tv_usec | (getpid() << 20)));
451    
452 michael 4340 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 michael 1702
461 adx 30 myargv = argv;
462 michael 5723 umask(077); /* umask 077: u=rwx,g=,o= */
463 adx 30
464     parseargs(&argc, &argv, myopts);
465    
466     if (printVersion)
467     {
468 michael 2646 printf("ircd: version %s(%s)\n", ircd_version, serno);
469 adx 30 exit(EXIT_SUCCESS);
470     }
471    
472 michael 4340 if (chdir(ConfigGeneral.dpath))
473 adx 30 {
474     perror("chdir");
475     exit(EXIT_FAILURE);
476     }
477    
478 michael 1798 ssl_init();
479 adx 30
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 michael 1831 log_set_file(LOG_TYPE_IRCD, 0, logFileName);
493 michael 4415
494 michael 2632 init_netio(); /* This needs to be setup early ! -- adrian */
495 michael 2253
496 adx 30 /* Check if there is pidfile and daemon already running */
497     check_pidfile(pidFileName);
498    
499 michael 1654 mp_pool_init();
500 adx 30 init_dlink_nodes();
501 michael 6185 isupport_init();
502 adx 30 dbuf_init();
503 michael 1798 hash_init();
504 michael 6393 userhost_init();
505 michael 4319 ipcache_init();
506 michael 1798 client_init();
507 michael 1632 class_init();
508 michael 1358 whowas_init();
509 michael 876 watch_init();
510 michael 1798 auth_init(); /* Initialise the auth code */
511 michael 998 init_resolver(); /* Needs to be setup before the io loop */
512 michael 1404 modules_init();
513 adx 30 read_conf_files(1); /* cold start init conf files */
514     initialize_server_capabs(); /* Set up default_server_capabs */
515 michael 5489 initialize_global_set_options(); /* Has to be called after read_conf_files() */
516 michael 1798 channel_init();
517 michael 2216 read_links_file();
518 michael 2150 motd_init();
519 michael 6189 user_modes_init();
520 michael 1858 #ifdef HAVE_LIBGEOIP
521     geoip_ctx = GeoIP_new(GEOIP_MEMORY_CACHE);
522     #endif
523 adx 30
524 michael 4340 if (EmptyString(ConfigServerInfo.name))
525 michael 1115 {
526 michael 1247 ilog(LOG_TYPE_IRCD, "ERROR: No server name specified in serverinfo block.");
527 michael 1115 exit(EXIT_FAILURE);
528     }
529    
530 michael 4340 strlcpy(me.name, ConfigServerInfo.name, sizeof(me.name));
531 adx 30
532 michael 6481 /* serverinfo {} description must exist. If not, error out.*/
533 michael 4340 if (EmptyString(ConfigServerInfo.description))
534 adx 30 {
535 michael 1247 ilog(LOG_TYPE_IRCD, "ERROR: No server description specified in serverinfo block.");
536 adx 30 exit(EXIT_FAILURE);
537     }
538 michael 885
539 michael 4340 strlcpy(me.info, ConfigServerInfo.description, sizeof(me.info));
540 adx 30
541 michael 6156 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 michael 6464 init_uid();
550    
551 michael 5545 me.from = &me;
552     me.servptr = &me;
553     me.connection->lasttime = CurrentTime;
554     me.connection->since = CurrentTime;
555 michael 4588 me.connection->firsttime = CurrentTime;
556 adx 30
557     SetMe(&me);
558     make_server(&me);
559    
560 michael 1115 hash_add_id(&me);
561 adx 30 hash_add_client(&me);
562 michael 2916
563 michael 4209 dlinkAdd(&me, make_dlink_node(), &global_server_list);
564 michael 6464 dlinkAdd(&me, &me.node, &global_client_list);
565 adx 30
566 michael 6928 load_kline_database(ConfigGeneral.klinefile);
567     load_dline_database(ConfigGeneral.dlinefile);
568     load_xline_database(ConfigGeneral.xlinefile);
569     load_resv_database(ConfigGeneral.resvfile);
570 michael 1622
571 adx 30 load_all_modules(1);
572     load_conf_modules();
573     load_core_modules(1);
574 michael 1115
575 adx 30 write_pidfile(pidFileName);
576    
577 michael 4094 event_addish(&event_cleanup_tklines, NULL);
578 adx 30
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 michael 4094 event_addish(&event_try_connections, NULL);
582 adx 30
583     /* Setup the timeout check. I'll shift it later :) -- adrian */
584 michael 4399 event_add(&event_comm_checktimeouts, NULL);
585 adx 30
586 michael 4094 event_addish(&event_save_all_databases, NULL);
587 michael 1625
588 michael 6642 if (ConfigServerHide.flatten_links_delay && event_write_links_file.active == 0)
589 michael 4094 {
590 michael 6597 event_write_links_file.when = ConfigServerHide.flatten_links_delay;
591 michael 6636 event_add(&event_write_links_file, NULL);
592 michael 4094 }
593 adx 30
594 michael 6464 ilog(LOG_TYPE_IRCD, "Server Ready");
595 adx 30 io_loop();
596 michael 6464
597 michael 885 return 0;
598 adx 30 }

Properties

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