ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/branches/8.2.x/src/ircd.c
(Generate patch)

Comparing:
ircd-hybrid/trunk/src/ircd.c (file contents), Revision 2632 by michael, Sun Dec 8 18:33:48 2013 UTC vs.
ircd-hybrid/branches/8.2.x/src/ircd.c (file contents), Revision 4198 by michael, Thu Jul 10 19:52:27 2014 UTC

# Line 1 | Line 1
1   /*
2 < *  ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd).
3 < *  ircd.c: Starts up and runs the ircd.
2 > *  ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd)
3   *
4 < *  Copyright (C) 2002 by the past and present ircd coders, and others.
4 > *  Copyright (c) 1997-2014 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
# Line 18 | Line 17
17   *  along with this program; if not, write to the Free Software
18   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
19   *  USA
20 < *
21 < *  $Id$
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 "s_user.h"
28 > #include "user.h"
29   #include "list.h"
30   #include "ircd.h"
31   #include "channel.h"
30 #include "channel_mode.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 "s_gline.h"
38 > #include "gline.h"
39   #include "motd.h"
40   #include "conf.h"
41   #include "hostmask.h"
41 #include "numeric.h"
42 #include "packet.h"
42   #include "parse.h"
43 < #include "irc_res.h"
43 > #include "res.h"
44   #include "restart.h"
45   #include "rng_mt.h"
46 < #include "s_auth.h"
46 > #include "auth.h"
47   #include "s_bsd.h"
48   #include "log.h"
49 < #include "s_misc.h"
51 < #include "s_serv.h"      /* try_connections */
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"
57 #include "hook.h"
55   #include "ircd_getopt.h"
59 #include "supported.h"
56   #include "watch.h"
57   #include "conf_db.h"
58   #include "conf_class.h"
# Line 65 | Line 61
61   #ifdef HAVE_LIBGEOIP
62   GeoIP *geoip_ctx;
63   #endif
68 /* /quote set variables */
69 struct SetOptions GlobalSetOptions;
64  
71 /* configuration set from ircd.conf */
72 struct config_file_entry ConfigFileEntry;
73 /* server info set from ircd.conf */
74 struct server_info ServerInfo;
75 /* admin info set from ircd.conf */
76 struct admin_info AdminInfo = { NULL, NULL, NULL };
77 struct Counter Count = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
78 struct ServerState_t server_state = { 0 };
79 struct logging_entry ConfigLoggingEntry = { .use_logging = 1 };
65   struct ServerStatistics ServerStats;
66   struct timeval SystemTime;
67   struct Client me;             /* That's me */
# Line 93 | Line 78 | int doremotd = 0;
78   /* Set to zero because it should be initialized later using
79   * initialize_server_capabs
80   */
81 < int default_server_capabs = 0;
81 > unsigned int default_server_capabs;
82   unsigned int splitmode;
83   unsigned int splitchecking;
84   unsigned int split_users;
85   unsigned int split_servers;
86  
87 < /* Do klines the same way hybrid-6 did them, i.e. at the
88 < * top of the next io_loop instead of in the same loop as
89 < * the klines are being applied.
90 < *
91 < * This should fix strange CPU starvation as very indirectly reported.
92 < * (Why do you people not email bug reports? WHY? WHY?)
108 < *
109 < * - Dianora
110 < */
87 > static struct event event_cleanup_glines =
88 > {
89 >  .name = "cleanup_glines",
90 >  .handler = cleanup_glines,
91 >  .when = CLEANUP_GLINES_TIME
92 > };
93  
94 < int rehashed_klines = 0;
94 > static struct event event_cleanup_tklines =
95 > {
96 >  .name = "cleanup_tklines",
97 >  .handler = cleanup_tklines,
98 >  .when = CLEANUP_TKLINES_TIME
99 > };
100 >
101 > static struct event event_try_connections =
102 > {
103 >  .name = "try_connections",
104 >  .handler = try_connections,
105 >  .when = STARTUP_CONNECTIONS_TIME
106 > };
107 >
108 > static struct event event_comm_checktimeouts =
109 > {
110 >  .name = "comm_checktimeouts",
111 >  .handler = comm_checktimeouts,
112 >  .when = 1
113 > };
114 >
115 > static struct event event_save_all_databases =
116 > {
117 >  .name = "save_all_databases",
118 >  .handler = save_all_databases,
119 >  .when = DATABASE_UPDATE_TIMEOUT
120 > };
121 >
122 > struct event event_write_links_file =
123 > {
124 >  .name = "write_links_file",
125 >  .handler = write_links_file,
126 > };
127  
128  
129   /*
# Line 118 | Line 132 | int rehashed_klines = 0;
132   static void
133   print_startup(int pid)
134   {
135 <  printf("ircd: version %s\n", ircd_version);
135 >  printf("ircd: version %s(%s)\n", ircd_version, serno);
136    printf("ircd: pid %d\n", pid);
137    printf("ircd: running in %s mode from %s\n", !server_state.foreground ? "background"
138           : "foreground", ConfigFileEntry.dpath);
# Line 145 | Line 159 | make_daemon(void)
159  
160   static int printVersion = 0;
161  
162 < static struct lgetopt myopts[] = {
163 <  {"configfile", &ConfigFileEntry.configfile,
162 > static struct lgetopt myopts[] =
163 > {
164 >  {"configfile", &ConfigFileEntry.configfile,
165     STRING, "File to use for ircd.conf"},
166    {"glinefile",  &ConfigFileEntry.glinefile,
167     STRING, "File to use for gline database"},
168 <  {"klinefile",  &ConfigFileEntry.klinefile,
168 >  {"klinefile",  &ConfigFileEntry.klinefile,
169     STRING, "File to use for kline database"},
170    {"dlinefile",  &ConfigFileEntry.dlinefile,
171     STRING, "File to use for dline database"},
172 <  {"xlinefile",  &ConfigFileEntry.xlinefile,
172 >  {"xlinefile",  &ConfigFileEntry.xlinefile,
173     STRING, "File to use for xline database"},
174    {"resvfile",  &ConfigFileEntry.resvfile,
175     STRING, "File to use for resv database"},
176 <  {"logfile",    &logFileName,
176 >  {"logfile",    &logFileName,
177     STRING, "File to use for ircd.log"},
178    {"pidfile",    &pidFileName,
179     STRING, "File to use for process ID"},
180 <  {"foreground", &server_state.foreground,
180 >  {"foreground", &server_state.foreground,
181     YESNO, "Run in foreground (don't detach)"},
182 <  {"version",    &printVersion,
182 >  {"version",    &printVersion,
183     YESNO, "Print version and exit"},
184    {"help", NULL, USAGE, "Print this text"},
185    {NULL, NULL, STRING, NULL},
# Line 173 | Line 188 | static struct lgetopt myopts[] = {
188   void
189   set_time(void)
190   {
191 <  static char to_send[200];
177 <  struct timeval newtime;
178 <  newtime.tv_sec  = 0;
179 <  newtime.tv_usec = 0;
191 >  struct timeval newtime = { .tv_sec = 0, .tv_usec = 0 };
192  
193    if (gettimeofday(&newtime, NULL) == -1)
194    {
# Line 185 | Line 197 | set_time(void)
197      sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
198                           "Clock Failure (%s), TS can be corrupted",
199                           strerror(errno));
200 <    restart("Clock Failure");
200 >    server_die("Clock Failure", 1);
201    }
202  
203    if (newtime.tv_sec < CurrentTime)
204    {
205 <    snprintf(to_send, sizeof(to_send),
206 <             "System clock is running backwards - (%lu < %lu)",
207 <             (unsigned long)newtime.tv_sec, (unsigned long)CurrentTime);
208 <    report_error(L_ALL, to_send, me.name, 0);
205 >    ilog(LOG_TYPE_IRCD, "System clock is running backwards - (%lu < %lu)",
206 >         (unsigned long)newtime.tv_sec, (unsigned long)CurrentTime);
207 >    sendto_realops_flags(UMODE_DEBUG, L_ALL, SEND_NOTICE,
208 >                         "System clock is running backwards - (%lu < %lu)",
209 >                         (unsigned long)newtime.tv_sec,
210 >                         (unsigned long)CurrentTime);
211      set_back_events(CurrentTime - newtime.tv_sec);
212    }
213  
# Line 204 | Line 218 | set_time(void)
218   static void
219   io_loop(void)
220   {
221 <  while (1 == 1)
221 >  while (1)
222    {
209    /*
210     * Maybe we want a flags word?
211     * ie. if (REHASHED_KLINES(global_flags))
212     * SET_REHASHED_KLINES(global_flags)
213     * CLEAR_REHASHED_KLINES(global_flags)
214     *
215     * - Dianora
216     */
217    if (rehashed_klines)
218    {
219      check_conf_klines();
220      rehashed_klines = 0;
221    }
222
223      if (listing_client_list.head)
224      {
225        dlink_node *ptr = NULL, *ptr_next = NULL;
226        DLINK_FOREACH_SAFE(ptr, ptr_next, listing_client_list.head)
227 <      {
228 <        struct Client *client_p = ptr->data;
229 <        assert(client_p->localClient->list_task);
230 <        safe_list_channels(client_p, client_p->localClient->list_task, 0);
231 <      }
227 >        safe_list_channels(ptr->data, 0);
228      }
229  
230 <    /* Run pending events, then get the number of seconds to the next
231 <     * event
236 <     */
237 <    while (eventNextTime() <= CurrentTime)
238 <      eventRun();
230 >    /* Run pending events */
231 >    event_run();
232  
233      comm_select();
234      exit_aborted_clients();
235      free_exited_clients();
243    send_queued_all();
236  
237      /* Check to see whether we have to rehash the configuration .. */
238      if (dorehash)
# Line 248 | Line 240 | io_loop(void)
240        rehash(1);
241        dorehash = 0;
242      }
243 +
244      if (doremotd)
245      {
246        motd_recache();
247        sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
248 <                           "Got signal SIGUSR1, reloading motd files");
248 >                           "Got signal SIGUSR1, reloading motd file(s)");
249        doremotd = 0;
250      }
251    }
# Line 262 | Line 255 | io_loop(void)
255   *
256   * inputs       - none
257   * output       - none
258 < * side effects - This sets all global set options needed
258 > * side effects - This sets all global set options needed
259   */
260   static void
261   initialize_global_set_options(void)
262   {
270  memset(&GlobalSetOptions, 0, sizeof(GlobalSetOptions));
271
263    GlobalSetOptions.autoconn  = 1;
264    GlobalSetOptions.spam_time = MIN_JOIN_LEAVE_TIME;
265    GlobalSetOptions.spam_num  = MAX_JOIN_LEAVE_COUNT;
# Line 293 | Line 284 | initialize_global_set_options(void)
284    }
285  
286    GlobalSetOptions.ident_timeout = IDENT_TIMEOUT;
296  /* End of global set options */
287   }
288  
289   /* initialize_server_capabs()
# Line 308 | Line 298 | initialize_server_capabs(void)
298    add_capability("EOB", CAP_EOB, 1);
299    add_capability("TS6", CAP_TS6, 0);
300    add_capability("CLUSTER", CAP_CLUSTER, 1);
311 //  add_capability("FAKEHOST", CAP_FAKEHOST, 1);
301    add_capability("SVS", CAP_SVS, 1);
302 < #ifdef HALFOPS
302 >  add_capability("CHW", CAP_CHW, 1);
303    add_capability("HOPS", CAP_HOPS, 1);
315 #endif
304   }
305  
306   /* write_pidfile()
# Line 328 | Line 316 | write_pidfile(const char *filename)
316  
317    if ((fb = fopen(filename, "w")))
318    {
319 <    char buff[32];
319 >    char buff[IRCD_BUFSIZE];
320      unsigned int pid = (unsigned int)getpid();
321  
322      snprintf(buff, sizeof(buff), "%u\n", pid);
# Line 357 | Line 345 | static void
345   check_pidfile(const char *filename)
346   {
347    FILE *fb;
348 <  char buff[32];
348 >  char buff[IRCD_BUFSIZE];
349    pid_t pidfromfile;
350  
351    /* Don't do logging here, since we don't have log() initialised */
# Line 429 | Line 417 | static void
417   ssl_init(void)
418   {
419   #ifdef HAVE_LIBCRYPTO
420 +  const unsigned char session_id[] = "ircd-hybrid";
421 +
422    SSL_load_error_strings();
423    SSLeay_add_ssl_algorithms();
424  
425    if ((ServerInfo.server_ctx = SSL_CTX_new(SSLv23_server_method())) == NULL)
426    {
427 <    const char *s;
427 >    const char *s = ERR_lib_error_string(ERR_get_error());
428  
429 <    fprintf(stderr, "ERROR: Could not initialize the SSL Server context -- %s\n",
440 <            s = ERR_lib_error_string(ERR_get_error()));
429 >    fprintf(stderr, "ERROR: Could not initialize the SSL Server context -- %s\n", s);
430      ilog(LOG_TYPE_IRCD, "ERROR: Could not initialize the SSL Server context -- %s\n", s);
431    }
432  
433 <  SSL_CTX_set_options(ServerInfo.server_ctx, SSL_OP_NO_SSLv2|SSL_OP_NO_SSLv3|SSL_OP_NO_TLSv1);
434 <  SSL_CTX_set_options(ServerInfo.server_ctx, SSL_OP_TLS_ROLLBACK_BUG|SSL_OP_ALL);
433 >  SSL_CTX_set_options(ServerInfo.server_ctx, SSL_OP_NO_SSLv2|SSL_OP_NO_SSLv3);
434 >  SSL_CTX_set_options(ServerInfo.server_ctx, SSL_OP_SINGLE_DH_USE);
435    SSL_CTX_set_verify(ServerInfo.server_ctx, SSL_VERIFY_PEER|SSL_VERIFY_CLIENT_ONCE,
436                       always_accept_verify_cb);
437 +  SSL_CTX_set_session_id_context(ServerInfo.server_ctx, session_id, sizeof(session_id) - 1);
438 +
439 + #if OPENSSL_VERSION_NUMBER >= 0x1000005FL && !defined(OPENSSL_NO_ECDH)
440 +  {
441 +    EC_KEY *key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
442 +
443 +    if (key)
444 +    {
445 +      SSL_CTX_set_tmp_ecdh(ServerInfo.server_ctx, key);
446 +      EC_KEY_free(key);
447 +    }
448 +  }
449 +
450 +  SSL_CTX_set_options(ServerInfo.server_ctx, SSL_OP_SINGLE_ECDH_USE);
451 + #endif
452  
453    if ((ServerInfo.client_ctx = SSL_CTX_new(SSLv23_client_method())) == NULL)
454    {
455 <    const char *s;
455 >    const char *s = ERR_lib_error_string(ERR_get_error());
456  
457 <    fprintf(stderr, "ERROR: Could not initialize the SSL Client context -- %s\n",
454 <            s = ERR_lib_error_string(ERR_get_error()));
457 >    fprintf(stderr, "ERROR: Could not initialize the SSL Client context -- %s\n", s);
458      ilog(LOG_TYPE_IRCD, "ERROR: Could not initialize the SSL Client context -- %s\n", s);
459    }
460  
461 <  SSL_CTX_set_options(ServerInfo.client_ctx, SSL_OP_NO_SSLv2|SSL_OP_NO_SSLv3|SSL_OP_NO_TLSv1);
462 <  SSL_CTX_set_options(ServerInfo.client_ctx, SSL_OP_TLS_ROLLBACK_BUG|SSL_OP_ALL);
461 >  SSL_CTX_set_options(ServerInfo.client_ctx, SSL_OP_NO_SSLv2|SSL_OP_NO_SSLv3);
462 >  SSL_CTX_set_options(ServerInfo.client_ctx, SSL_OP_SINGLE_DH_USE);
463    SSL_CTX_set_verify(ServerInfo.client_ctx, SSL_VERIFY_PEER|SSL_VERIFY_CLIENT_ONCE,
464                       always_accept_verify_cb);
465   #endif /* HAVE_LIBCRYPTO */
# Line 468 | Line 471 | main(int argc, char *argv[])
471    /* Check to see if the user is running us as root, which is a nono */
472    if (geteuid() == 0)
473    {
474 <    fprintf(stderr, "Don't run ircd as root!!!\n");
474 >    fprintf(stderr, "ERROR: This server won't run as root/superuser\n");
475      return -1;
476    }
477  
# Line 484 | Line 487 | main(int argc, char *argv[])
487    me.localClient = &meLocalUser;
488    dlinkAdd(&me, &me.node, &global_client_list);  /* Pointer to beginning
489                                                     of Client list */
490 <  /* Initialise the channel capability usage counts... */
488 <  init_chcap_usage_counts();
489 <
490 >  ConfigLoggingEntry.use_logging = 1;
491    ConfigFileEntry.dpath      = DPATH;
492 +  ConfigFileEntry.spath      = SPATH;
493 +  ConfigFileEntry.mpath      = MPATH;
494    ConfigFileEntry.configfile = CPATH;    /* Server configuration file */
495    ConfigFileEntry.klinefile  = KPATH;    /* Server kline file         */
496    ConfigFileEntry.glinefile  = GPATH;    /* Server gline file         */
# Line 502 | Line 505 | main(int argc, char *argv[])
505  
506    if (printVersion)
507    {
508 <    printf("ircd: version %s\n", ircd_version);
508 >    printf("ircd: version %s(%s)\n", ircd_version, serno);
509      exit(EXIT_SUCCESS);
510    }
511  
# Line 524 | Line 527 | main(int argc, char *argv[])
527  
528    setup_signals();
529  
527  /* Init the event subsystem */
528  eventInit();
529
530    /* We need this to initialise the fd array before anything else */
531    fdlist_init();
532    log_set_file(LOG_TYPE_IRCD, 0, logFileName);
# Line 597 | Line 597 | main(int argc, char *argv[])
597  
598    hash_add_id(&me);
599    hash_add_client(&me);
600 <  
600 >
601    /* add ourselves to global_serv_list */
602    dlinkAdd(&me, make_dlink_node(), &global_serv_list);
603  
# Line 634 | Line 634 | main(int argc, char *argv[])
634  
635    ilog(LOG_TYPE_IRCD, "Server Ready");
636  
637 <  eventAddIsh("cleanup_glines", cleanup_glines, NULL, CLEANUP_GLINES_TIME);
638 <  eventAddIsh("cleanup_tklines", cleanup_tklines, NULL, CLEANUP_TKLINES_TIME);
637 >  event_addish(&event_cleanup_glines, NULL);
638 >  event_addish(&event_cleanup_tklines, NULL);
639  
640    /* We want try_connections to be called as soon as possible now! -- adrian */
641    /* No, 'cause after a restart it would cause all sorts of nick collides */
642 <  eventAddIsh("try_connections", try_connections, NULL, STARTUP_CONNECTIONS_TIME);
642 >  event_addish(&event_try_connections, NULL);
643  
644    /* Setup the timeout check. I'll shift it later :)  -- adrian */
645 <  eventAddIsh("comm_checktimeouts", comm_checktimeouts, NULL, 1);
645 >  event_addish(&event_comm_checktimeouts, NULL);
646  
647 <  eventAddIsh("save_all_databases", save_all_databases, NULL, DATABASE_UPDATE_TIMEOUT);
647 >  event_addish(&event_save_all_databases, NULL);
648  
649    if (ConfigServerHide.links_delay > 0)
650 <    eventAddIsh("write_links_file", write_links_file, NULL, ConfigServerHide.links_delay);
650 >  {
651 >    event_write_links_file.when = ConfigServerHide.links_delay;
652 >    event_addish(&event_write_links_file, NULL);
653 >  }
654    else
655      ConfigServerHide.links_disabled = 1;
656  
657    if (splitmode)
658 <    eventAddIsh("check_splitmode", check_splitmode, NULL, 60);
658 >    event_addish(&splitmode_event, NULL);
659  
660    io_loop();
661    return 0;

Diff Legend

Removed lines
+ Added lines
< Changed lines (old)
> Changed lines (new)