ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/ircd.c
Revision: 6735
Committed: Fri Nov 6 19:16:48 2015 UTC (9 years, 9 months ago) by michael
Content type: text/x-csrc
File size: 15612 byte(s)
Log Message:
- ircd.c: move code

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 5347 * Copyright (c) 1997-2015 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 2980 ilog(LOG_TYPE_IRCD, "System clock is running backwards - (%lu < %lu)",
161     (unsigned long)newtime.tv_sec, (unsigned long)CurrentTime);
162     sendto_realops_flags(UMODE_DEBUG, L_ALL, SEND_NOTICE,
163     "System clock is running backwards - (%lu < %lu)",
164     (unsigned long)newtime.tv_sec,
165     (unsigned long)CurrentTime);
166 michael 6370 event_set_back_events(CurrentTime - newtime.tv_sec);
167 adx 30 }
168    
169 michael 6481 SystemTime.tv_sec = newtime.tv_sec;
170 adx 30 SystemTime.tv_usec = newtime.tv_usec;
171     }
172    
173     static void
174     io_loop(void)
175     {
176 michael 3215 while (1)
177 adx 30 {
178     if (listing_client_list.head)
179     {
180 michael 4815 dlink_node *node = NULL, *node_next = NULL;
181     DLINK_FOREACH_SAFE(node, node_next, listing_client_list.head)
182     safe_list_channels(node->data, 0);
183 adx 30 }
184    
185 michael 4094 /* Run pending events */
186     event_run();
187 adx 30
188     comm_select();
189     exit_aborted_clients();
190     free_exited_clients();
191    
192 michael 6735 /* Check to see whether we have to rehash the configuration. */
193 adx 30 if (dorehash)
194     {
195 michael 4982 conf_rehash(1);
196 adx 30 dorehash = 0;
197     }
198 michael 3215
199 adx 30 if (doremotd)
200     {
201 michael 2150 motd_recache();
202 michael 6318 sendto_realops_flags(UMODE_SERVNOTICE, L_ALL, SEND_NOTICE,
203 michael 3065 "Got signal SIGUSR1, reloading motd file(s)");
204 adx 30 doremotd = 0;
205     }
206     }
207     }
208    
209     /* initalialize_global_set_options()
210     *
211     * inputs - none
212     * output - none
213 michael 2916 * side effects - This sets all global set options needed
214 adx 30 */
215     static void
216     initialize_global_set_options(void)
217     {
218 michael 5489 GlobalSetOptions.maxclients = ConfigServerInfo.default_max_clients;
219 michael 5499 GlobalSetOptions.autoconn = 1;
220 adx 30 GlobalSetOptions.spam_time = MIN_JOIN_LEAVE_TIME;
221 michael 5499 GlobalSetOptions.spam_num = MAX_JOIN_LEAVE_COUNT;
222     GlobalSetOptions.floodcount = ConfigGeneral.default_floodcount;
223 michael 5489 GlobalSetOptions.joinfloodcount = ConfigChannel.default_join_flood_count;
224     GlobalSetOptions.joinfloodtime = ConfigChannel.default_join_flood_time;
225 adx 30 GlobalSetOptions.ident_timeout = IDENT_TIMEOUT;
226     }
227    
228     /* initialize_server_capabs()
229     *
230     * inputs - none
231     * output - none
232     */
233     static void
234     initialize_server_capabs(void)
235     {
236 michael 6354 add_capability("QS", CAPAB_QS);
237     add_capability("EOB", CAPAB_EOB);
238     add_capability("CLUSTER", CAPAB_CLUSTER);
239     add_capability("SVS", CAPAB_SVS);
240     add_capability("CHW", CAPAB_CHW);
241     add_capability("HOPS", CAPAB_HOPS);
242 adx 30 }
243    
244     /* write_pidfile()
245     *
246     * inputs - filename+path of pid file
247     * output - NONE
248     * side effects - write the pid of the ircd to filename
249     */
250     static void
251     write_pidfile(const char *filename)
252     {
253 michael 1325 FILE *fb;
254 adx 30
255 michael 1325 if ((fb = fopen(filename, "w")))
256 adx 30 {
257 michael 6470 char buf[IRCD_BUFSIZE];
258 adx 30 unsigned int pid = (unsigned int)getpid();
259    
260 michael 6470 snprintf(buf, sizeof(buf), "%u\n", pid);
261 michael 1325
262 michael 6470 if (fputs(buf, fb) == -1)
263 michael 5737 ilog(LOG_TYPE_IRCD, "Error writing to pid file %s: %s",
264     filename, strerror(errno));
265 adx 30
266 michael 1325 fclose(fb);
267 adx 30 }
268     else
269 michael 5566 ilog(LOG_TYPE_IRCD, "Error opening pid file %s: %s",
270 michael 4748 filename, strerror(errno));
271 adx 30 }
272    
273     /* check_pidfile()
274     *
275     * inputs - filename+path of pid file
276     * output - none
277     * side effects - reads pid from pidfile and checks if ircd is in process
278     * list. if it is, gracefully exits
279     * -kre
280     */
281     static void
282     check_pidfile(const char *filename)
283     {
284 michael 1325 FILE *fb;
285 michael 6470 char buf[IRCD_BUFSIZE];
286 adx 30
287 michael 1325 if ((fb = fopen(filename, "r")))
288 adx 30 {
289 michael 6470 if (!fgets(buf, 20, fb))
290 michael 6260 ilog(LOG_TYPE_IRCD, "Error reading from pid file %s: %s",
291     filename, strerror(errno));
292 adx 30 else
293     {
294 michael 6481 pid_t pid = atoi(buf);
295 adx 30
296 michael 6481 if (!kill(pid, 0))
297 adx 30 {
298     /* log(L_ERROR, "Server is already running"); */
299     printf("ircd: daemon is already running\n");
300 michael 6646 exit(EXIT_FAILURE);
301 adx 30 }
302     }
303    
304 michael 1325 fclose(fb);
305 adx 30 }
306     else if (errno != ENOENT)
307 michael 6260 ilog(LOG_TYPE_IRCD, "Error opening pid file %s: %s",
308     filename, strerror(errno));
309 adx 30 }
310    
311     /* setup_corefile()
312     *
313     * inputs - nothing
314     * output - nothing
315     * side effects - setups corefile to system limits.
316     * -kre
317     */
318     static void
319     setup_corefile(void)
320     {
321     #ifdef HAVE_SYS_RESOURCE_H
322     struct rlimit rlim; /* resource limits */
323    
324     /* Set corefilesize to maximum */
325     if (!getrlimit(RLIMIT_CORE, &rlim))
326     {
327     rlim.rlim_cur = rlim.rlim_max;
328     setrlimit(RLIMIT_CORE, &rlim);
329     }
330     #endif
331     }
332    
333 michael 2228 #ifdef HAVE_LIBCRYPTO
334     static int
335     always_accept_verify_cb(int preverify_ok, X509_STORE_CTX *x509_ctx)
336     {
337     return 1;
338     }
339     #endif
340    
341 michael 6481 /* ssl_init()
342 adx 30 *
343     * inputs - nothing
344     * output - nothing
345     * side effects - setups SSL context.
346     */
347     static void
348 michael 1798 ssl_init(void)
349 adx 30 {
350     #ifdef HAVE_LIBCRYPTO
351     SSL_load_error_strings();
352     SSLeay_add_ssl_algorithms();
353    
354 michael 4733 if (!(ConfigServerInfo.server_ctx = SSL_CTX_new(SSLv23_server_method())))
355 adx 30 {
356 michael 4129 const char *s = ERR_lib_error_string(ERR_get_error());
357 adx 30
358 michael 4129 fprintf(stderr, "ERROR: Could not initialize the SSL Server context -- %s\n", s);
359 michael 4763 ilog(LOG_TYPE_IRCD, "ERROR: Could not initialize the SSL Server context -- %s", s);
360 michael 4497 exit(EXIT_FAILURE);
361 michael 4751 return; /* Not reached */
362 adx 30 }
363    
364 michael 4730 SSL_CTX_set_options(ConfigServerInfo.server_ctx, SSL_OP_NO_SSLv2|SSL_OP_NO_SSLv3|SSL_OP_NO_TICKET);
365 michael 4593 SSL_CTX_set_options(ConfigServerInfo.server_ctx, SSL_OP_SINGLE_DH_USE|SSL_OP_CIPHER_SERVER_PREFERENCE);
366 michael 4340 SSL_CTX_set_verify(ConfigServerInfo.server_ctx, SSL_VERIFY_PEER|SSL_VERIFY_CLIENT_ONCE,
367 michael 2228 always_accept_verify_cb);
368 michael 4730 SSL_CTX_set_session_cache_mode(ConfigServerInfo.server_ctx, SSL_SESS_CACHE_OFF);
369 michael 4892 SSL_CTX_set_cipher_list(ConfigServerInfo.server_ctx, "EECDH+HIGH:EDH+HIGH:HIGH:!aNULL");
370 adx 30
371 michael 4744 #if OPENSSL_VERSION_NUMBER >= 0x009080FFL && !defined(OPENSSL_NO_ECDH)
372 michael 4070 {
373     EC_KEY *key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
374    
375     if (key)
376     {
377 michael 4340 SSL_CTX_set_tmp_ecdh(ConfigServerInfo.server_ctx, key);
378 michael 4070 EC_KEY_free(key);
379     }
380     }
381    
382 michael 4340 SSL_CTX_set_options(ConfigServerInfo.server_ctx, SSL_OP_SINGLE_ECDH_USE);
383 michael 4070 #endif
384    
385 michael 4733 if (!(ConfigServerInfo.client_ctx = SSL_CTX_new(SSLv23_client_method())))
386 michael 1303 {
387 michael 4129 const char *s = ERR_lib_error_string(ERR_get_error());
388 michael 1303
389 michael 4129 fprintf(stderr, "ERROR: Could not initialize the SSL Client context -- %s\n", s);
390 michael 4763 ilog(LOG_TYPE_IRCD, "ERROR: Could not initialize the SSL Client context -- %s", s);
391 michael 4497 exit(EXIT_FAILURE);
392 michael 4751 return; /* Not reached */
393 michael 1303 }
394    
395 michael 4730 SSL_CTX_set_options(ConfigServerInfo.client_ctx, SSL_OP_NO_SSLv2|SSL_OP_NO_SSLv3|SSL_OP_NO_TICKET);
396 michael 4340 SSL_CTX_set_options(ConfigServerInfo.client_ctx, SSL_OP_SINGLE_DH_USE);
397     SSL_CTX_set_verify(ConfigServerInfo.client_ctx, SSL_VERIFY_PEER|SSL_VERIFY_CLIENT_ONCE,
398 michael 2228 always_accept_verify_cb);
399 michael 4730 SSL_CTX_set_session_cache_mode(ConfigServerInfo.client_ctx, SSL_SESS_CACHE_OFF);
400 adx 30 #endif /* HAVE_LIBCRYPTO */
401     }
402    
403 michael 6735 /*
404     * print_startup - print startup information
405     */
406     static void
407     print_startup(int pid)
408     {
409     printf("ircd: version %s(%s)\n", ircd_version, serno);
410     printf("ircd: pid %d\n", pid);
411     printf("ircd: running in %s mode from %s\n", !server_state.foreground ? "background"
412     : "foreground", ConfigGeneral.dpath);
413     }
414    
415     static void
416     make_daemon(void)
417     {
418     int pid;
419    
420     if ((pid = fork()) < 0)
421     {
422     perror("fork");
423     exit(EXIT_FAILURE);
424     }
425     else if (pid > 0)
426     {
427     print_startup(pid);
428     exit(EXIT_SUCCESS);
429     }
430    
431     setsid();
432     }
433    
434 adx 30 int
435     main(int argc, char *argv[])
436     {
437 michael 2253 /* Check to see if the user is running us as root, which is a nono */
438 michael 4733 if (!geteuid())
439 adx 30 {
440 michael 3525 fprintf(stderr, "ERROR: This server won't run as root/superuser\n");
441 michael 982 return -1;
442 adx 30 }
443    
444     /* Setup corefile size immediately after boot -kre */
445     setup_corefile();
446    
447 michael 5545 /* Save server boot time right away, so getrusage works correctly */
448 adx 30 set_time();
449    
450 michael 5545 /* It's not random, but it ought to be a little harder to guess */
451 michael 982 init_genrand(SystemTime.tv_sec ^ (SystemTime.tv_usec | (getpid() << 20)));
452    
453 michael 4340 ConfigGeneral.dpath = DPATH;
454     ConfigGeneral.spath = SPATH;
455     ConfigGeneral.mpath = MPATH;
456     ConfigGeneral.configfile = CPATH; /* Server configuration file */
457     ConfigGeneral.klinefile = KPATH; /* Server kline file */
458     ConfigGeneral.xlinefile = XPATH; /* Server xline file */
459     ConfigGeneral.dlinefile = DLPATH; /* dline file */
460     ConfigGeneral.resvfile = RESVPATH; /* resv file */
461 michael 1702
462 adx 30 myargv = argv;
463 michael 5723 umask(077); /* umask 077: u=rwx,g=,o= */
464 adx 30
465     parseargs(&argc, &argv, myopts);
466    
467     if (printVersion)
468     {
469 michael 2646 printf("ircd: version %s(%s)\n", ircd_version, serno);
470 adx 30 exit(EXIT_SUCCESS);
471     }
472    
473 michael 4340 if (chdir(ConfigGeneral.dpath))
474 adx 30 {
475     perror("chdir");
476     exit(EXIT_FAILURE);
477     }
478    
479 michael 1798 ssl_init();
480 adx 30
481     if (!server_state.foreground)
482     {
483     make_daemon();
484     close_standard_fds(); /* this needs to be before init_netio()! */
485     }
486     else
487     print_startup(getpid());
488    
489     setup_signals();
490    
491     /* We need this to initialise the fd array before anything else */
492     fdlist_init();
493 michael 1831 log_set_file(LOG_TYPE_IRCD, 0, logFileName);
494 michael 4415
495 michael 2632 init_netio(); /* This needs to be setup early ! -- adrian */
496 michael 2253
497 adx 30 /* Check if there is pidfile and daemon already running */
498     check_pidfile(pidFileName);
499    
500 michael 1654 mp_pool_init();
501 adx 30 init_dlink_nodes();
502 michael 6185 isupport_init();
503 adx 30 dbuf_init();
504 michael 1798 hash_init();
505 michael 6393 userhost_init();
506 michael 4319 ipcache_init();
507 michael 1798 client_init();
508 michael 1632 class_init();
509 michael 1358 whowas_init();
510 michael 876 watch_init();
511 michael 1798 auth_init(); /* Initialise the auth code */
512 michael 998 init_resolver(); /* Needs to be setup before the io loop */
513 michael 1404 modules_init();
514 adx 30 read_conf_files(1); /* cold start init conf files */
515     initialize_server_capabs(); /* Set up default_server_capabs */
516 michael 5489 initialize_global_set_options(); /* Has to be called after read_conf_files() */
517 michael 1798 channel_init();
518 michael 2216 read_links_file();
519 michael 2150 motd_init();
520 michael 6189 user_modes_init();
521 michael 1858 #ifdef HAVE_LIBGEOIP
522     geoip_ctx = GeoIP_new(GEOIP_MEMORY_CACHE);
523     #endif
524 adx 30
525 michael 4340 if (EmptyString(ConfigServerInfo.name))
526 michael 1115 {
527 michael 1247 ilog(LOG_TYPE_IRCD, "ERROR: No server name specified in serverinfo block.");
528 michael 1115 exit(EXIT_FAILURE);
529     }
530    
531 michael 4340 strlcpy(me.name, ConfigServerInfo.name, sizeof(me.name));
532 adx 30
533 michael 6481 /* serverinfo {} description must exist. If not, error out.*/
534 michael 4340 if (EmptyString(ConfigServerInfo.description))
535 adx 30 {
536 michael 1247 ilog(LOG_TYPE_IRCD, "ERROR: No server description specified in serverinfo block.");
537 adx 30 exit(EXIT_FAILURE);
538     }
539 michael 885
540 michael 4340 strlcpy(me.info, ConfigServerInfo.description, sizeof(me.info));
541 adx 30
542 michael 6156 if (EmptyString(ConfigServerInfo.sid))
543     {
544     ilog(LOG_TYPE_IRCD, "Generating server ID");
545     generate_sid();
546     }
547     else
548     strlcpy(me.id, ConfigServerInfo.sid, sizeof(me.id));
549    
550 michael 6464 init_uid();
551    
552 michael 5545 me.from = &me;
553     me.servptr = &me;
554     me.connection->lasttime = CurrentTime;
555     me.connection->since = CurrentTime;
556 michael 4588 me.connection->firsttime = CurrentTime;
557 adx 30
558     SetMe(&me);
559     make_server(&me);
560    
561 michael 1115 hash_add_id(&me);
562 adx 30 hash_add_client(&me);
563 michael 2916
564 michael 4209 dlinkAdd(&me, make_dlink_node(), &global_server_list);
565 michael 6464 dlinkAdd(&me, &me.node, &global_client_list);
566 adx 30
567 michael 1622 load_kline_database();
568     load_dline_database();
569     load_xline_database();
570     load_resv_database();
571    
572 adx 30 load_all_modules(1);
573     load_conf_modules();
574     load_core_modules(1);
575 michael 1115
576 adx 30 write_pidfile(pidFileName);
577    
578 michael 4094 event_addish(&event_cleanup_tklines, NULL);
579 adx 30
580     /* We want try_connections to be called as soon as possible now! -- adrian */
581     /* No, 'cause after a restart it would cause all sorts of nick collides */
582 michael 4094 event_addish(&event_try_connections, NULL);
583 adx 30
584     /* Setup the timeout check. I'll shift it later :) -- adrian */
585 michael 4399 event_add(&event_comm_checktimeouts, NULL);
586 adx 30
587 michael 4094 event_addish(&event_save_all_databases, NULL);
588 michael 1625
589 michael 6642 if (ConfigServerHide.flatten_links_delay && event_write_links_file.active == 0)
590 michael 4094 {
591 michael 6597 event_write_links_file.when = ConfigServerHide.flatten_links_delay;
592 michael 6636 event_add(&event_write_links_file, NULL);
593 michael 4094 }
594 adx 30
595 michael 6464 ilog(LOG_TYPE_IRCD, "Server Ready");
596 adx 30 io_loop();
597 michael 6464
598 michael 885 return 0;
599 adx 30 }

Properties

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