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

Comparing:
ircd-hybrid-7.2/src/ircd.c (file contents), Revision 1013 by michael, Sun Oct 18 14:26:49 2009 UTC vs.
ircd-hybrid/trunk/src/ircd.c (file contents), Revision 6735 by michael, Fri Nov 6 19:16:48 2015 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-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
# Line 16 | Line 15
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307
18 > *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
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"
32 – #include "common.h"
33   #include "event.h"
34   #include "fdlist.h"
35   #include "hash.h"
36 + #include "id.h"
37   #include "irc_string.h"
37 – #include "sprintf_irc.h"
38   #include "ircd_signal.h"
39 – #include "s_gline.h"
39   #include "motd.h"
40 < #include "ircd_handler.h"
42 < #include "msg.h"         /* msgtab */
40 > #include "conf.h"
41   #include "hostmask.h"
44 – #include "numeric.h"
45 – #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 "s_conf.h"
49 < #include "s_log.h"
54 < #include "s_misc.h"
55 < #include "s_serv.h"      /* try_connections */
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 "hook.h"
54 > #include "mempool.h"
55   #include "ircd_getopt.h"
62 – #include "balloc.h"
63 – #include "motd.h"
64 – #include "supported.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  
67 – /* Try and find the correct name to use with getrlimit() for setting the max.
68 – * number of files allowed to be open by this process.
69 – */
63  
64 < /* /quote set variables */
65 < struct SetOptions GlobalSetOptions;
64 > #ifdef HAVE_LIBGEOIP
65 > GeoIP *geoip_ctx;
66 > #endif
67  
68 < /* configuration set from ircd.conf */
69 < struct config_file_entry ConfigFileEntry;
70 < /* server info set from ircd.conf */
77 < struct server_info ServerInfo;
78 < /* admin info set from ircd.conf */
79 < struct admin_info AdminInfo = { NULL, NULL, NULL };
80 < struct Counter Count = { 0, 0, 0, 0, 0, 0, 0, 0 };
81 < struct ServerState_t server_state = { 0 };
82 < struct logging_entry ConfigLoggingEntry = { 1, {0}, {0}, {0}, {0}, {0}, {0}, {0}, {0} };
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 Client me;             /* That's me */
74 < struct LocalUser meLocalUser; /* That's also part of me */
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 < char **myargv;
81 < char ircd_platform[PLATFORMLEN];
93 <
94 < int dorehash = 0;
95 < int doremotd = 0;
96 <
97 < /* Set to zero because it should be initialized later using
98 < * initialize_server_capabs
99 < */
100 < int default_server_capabs = 0;
101 <
102 < #ifdef HAVE_LIBCRYPTO
103 < int bio_spare_fd = -1;
104 < #endif
105 <
106 < unsigned int splitmode;
107 < unsigned int splitchecking;
108 < unsigned int split_users;
109 < unsigned int split_servers;
110 <
111 < /* Do klines the same way hybrid-6 did them, i.e. at the
112 < * top of the next io_loop instead of in the same loop as
113 < * the klines are being applied.
114 < *
115 < * This should fix strange CPU starvation as very indirectly reported.
116 < * (Why do you people not email bug reports? WHY? WHY?)
117 < *
118 < * - Dianora
119 < */
80 > unsigned int dorehash;
81 > unsigned int doremotd;
82  
83 < int rehashed_klines = 0;
83 > static int printVersion;
84  
85 <
124 < /*
125 < * print_startup - print startup information
126 < */
127 < static void
128 < print_startup(int pid)
85 > static struct lgetopt myopts[] =
86   {
87 <  printf("ircd: version %s\n", ircd_version);
88 <  printf("ircd: pid %d\n", pid);
89 <  printf("ircd: running in %s mode from %s\n", !server_state.foreground ? "background"
90 <         : "foreground", ConfigFileEntry.dpath);
91 < }
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 void
137 < make_daemon(void)
109 > static struct event event_cleanup_tklines =
110   {
111 <  int pid;
111 >  .name = "cleanup_tklines",
112 >  .handler = cleanup_tklines,
113 >  .when = CLEANUP_TKLINES_TIME
114 > };
115  
116 <  if ((pid = fork()) < 0)
117 <  {
118 <    perror("fork");
119 <    exit(EXIT_FAILURE);
120 <  }
121 <  else if (pid > 0)
147 <  {
148 <    print_startup(pid);
149 <    exit(EXIT_SUCCESS);
150 <  }
116 > static struct event event_try_connections =
117 > {
118 >  .name = "try_connections",
119 >  .handler = try_connections,
120 >  .when = STARTUP_CONNECTIONS_TIME
121 > };
122  
123 <  setsid();
124 < }
123 > static struct event event_comm_checktimeouts =
124 > {
125 >  .name = "comm_checktimeouts",
126 >  .handler = comm_checktimeouts,
127 >  .when = 1
128 > };
129  
130 < static int printVersion = 0;
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 lgetopt myopts[] = {
138 <  {"dlinefile",  &ConfigFileEntry.dlinefile,
139 <   STRING, "File to use for dline.conf"},
140 <  {"configfile", &ConfigFileEntry.configfile,
161 <   STRING, "File to use for ircd.conf"},
162 <  {"klinefile",  &ConfigFileEntry.klinefile,
163 <   STRING, "File to use for kline.conf"},
164 <  {"xlinefile",  &ConfigFileEntry.xlinefile,
165 <   STRING, "File to use for xline.conf"},
166 <  {"logfile",    &logFileName,
167 <   STRING, "File to use for ircd.log"},
168 <  {"pidfile",    &pidFileName,
169 <   STRING, "File to use for process ID"},
170 <  {"foreground", &server_state.foreground,
171 <   YESNO, "Run in foreground (don't detach)"},
172 <  {"version",    &printVersion,
173 <   YESNO, "Print version and exit"},
174 <  {"help", NULL, USAGE, "Print this text"},
175 <  {NULL, NULL, STRING, NULL},
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 <  static char to_send[200];
182 <  struct timeval newtime;
183 <  newtime.tv_sec  = 0;
184 <  newtime.tv_usec = 0;
147 >  struct timeval newtime = { .tv_sec = 0, .tv_usec = 0 };
148  
149    if (gettimeofday(&newtime, NULL) == -1)
150    {
151 <    ilog(L_ERROR, "Clock Failure (%s), TS can be corrupted",
152 <         strerror(errno));
153 <    sendto_realops_flags(UMODE_ALL, L_ALL,
154 <                         "Clock Failure (%s), TS can be corrupted",
155 <                         strerror(errno));
193 <    restart("Clock Failure");
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 <    ircsprintf(to_send, "System clock is running backwards - (%lu < %lu)",
161 <               (unsigned long)newtime.tv_sec, (unsigned long)CurrentTime);
162 <    report_error(L_ALL, to_send, me.name, 0);
163 <    set_back_events(CurrentTime - newtime.tv_sec);
160 >    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 >    event_set_back_events(CurrentTime - newtime.tv_sec);
167    }
168  
169 <  SystemTime.tv_sec  = newtime.tv_sec;
169 >  SystemTime.tv_sec = newtime.tv_sec;
170    SystemTime.tv_usec = newtime.tv_usec;
171   }
172  
173   static void
174   io_loop(void)
175   {
176 <  while (1 == 1)
176 >  while (1)
177    {
213 –    /*
214 –     * Maybe we want a flags word?
215 –     * ie. if (REHASHED_KLINES(global_flags))
216 –     * SET_REHASHED_KLINES(global_flags)
217 –     * CLEAR_REHASHED_KLINES(global_flags)
218 –     *
219 –     * - Dianora
220 –     */
221 –    if (rehashed_klines)
222 –    {
223 –      check_conf_klines();
224 –      rehashed_klines = 0;
225 –    }
226 –
178      if (listing_client_list.head)
179      {
180 <      dlink_node *ptr = NULL, *ptr_next = NULL;
181 <      DLINK_FOREACH_SAFE(ptr, ptr_next, listing_client_list.head)
182 <      {
232 <        struct Client *client_p = ptr->data;
233 <        assert(client_p->localClient->list_task);
234 <        safe_list_channels(client_p, client_p->localClient->list_task, 0);
235 <      }
180 >      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      }
184  
185 <    /* Run pending events, then get the number of seconds to the next
186 <     * event
240 <     */
241 <    while (eventNextTime() <= CurrentTime)
242 <      eventRun();
185 >    /* Run pending events */
186 >    event_run();
187  
188      comm_select();
189      exit_aborted_clients();
190      free_exited_clients();
247 –    send_queued_all();
191  
192 <    /* Check to see whether we have to rehash the configuration .. */
192 >    /* Check to see whether we have to rehash the configuration. */
193      if (dorehash)
194      {
195 <      rehash(1);
195 >      conf_rehash(1);
196        dorehash = 0;
197      }
198 +
199      if (doremotd)
200      {
201 <      read_message_file(&ConfigFileEntry.motd);
202 <      sendto_realops_flags(UMODE_ALL, L_ALL,
203 <                           "Got signal SIGUSR1, reloading ircd motd file");
201 >      motd_recache();
202 >      sendto_realops_flags(UMODE_SERVNOTICE, L_ALL, SEND_NOTICE,
203 >                           "Got signal SIGUSR1, reloading motd file(s)");
204        doremotd = 0;
205      }
206    }
# Line 266 | Line 210 | io_loop(void)
210   *
211   * inputs       - none
212   * output       - none
213 < * side effects - This sets all global set options needed
213 > * side effects - This sets all global set options needed
214   */
215   static void
216   initialize_global_set_options(void)
217   {
218 <  memset(&GlobalSetOptions, 0, sizeof(GlobalSetOptions));
219 <
276 <  GlobalSetOptions.autoconn  = 1;
218 >  GlobalSetOptions.maxclients = ConfigServerInfo.default_max_clients;
219 >  GlobalSetOptions.autoconn = 1;
220    GlobalSetOptions.spam_time = MIN_JOIN_LEAVE_TIME;
221 <  GlobalSetOptions.spam_num  = MAX_JOIN_LEAVE_COUNT;
222 <
223 <  if (ConfigFileEntry.default_floodcount)
224 <    GlobalSetOptions.floodcount = ConfigFileEntry.default_floodcount;
282 <  else
283 <    GlobalSetOptions.floodcount = 10;
284 <
285 <  /* XXX I have no idea what to try here - Dianora */
286 <  GlobalSetOptions.joinfloodcount = 16;
287 <  GlobalSetOptions.joinfloodtime = 8;
288 <
289 <  split_servers = ConfigChannel.default_split_server_count;
290 <  split_users   = ConfigChannel.default_split_user_count;
291 <
292 <  if (split_users && split_servers && (ConfigChannel.no_create_on_split ||
293 <                                       ConfigChannel.no_join_on_split))
294 <  {
295 <    splitmode     = 1;
296 <    splitchecking = 1;
297 <  }
298 <
221 >  GlobalSetOptions.spam_num = MAX_JOIN_LEAVE_COUNT;
222 >  GlobalSetOptions.floodcount = ConfigGeneral.default_floodcount;
223 >  GlobalSetOptions.joinfloodcount = ConfigChannel.default_join_flood_count;
224 >  GlobalSetOptions.joinfloodtime = ConfigChannel.default_join_flood_time;
225    GlobalSetOptions.ident_timeout = IDENT_TIMEOUT;
300 –  GlobalSetOptions.idletime = ConfigFileEntry.idletime;
301 –  /* End of global set options */
302 – }
303 –
304 – /* initialize_message_files()
305 – *
306 – * inputs       - none
307 – * output       - none
308 – * side effects - Set up all message files needed, motd etc.
309 – */
310 – static void
311 – initialize_message_files(void)
312 – {
313 –  init_message_file(USER_MOTD, MPATH, &ConfigFileEntry.motd);
314 –  init_message_file(OPER_MOTD, OPATH, &ConfigFileEntry.opermotd);
315 –  init_message_file(USER_LINKS, LIPATH, &ConfigFileEntry.linksfile);
316 –
317 –  read_message_file(&ConfigFileEntry.motd);
318 –  read_message_file(&ConfigFileEntry.opermotd);
319 –  read_message_file(&ConfigFileEntry.linksfile);
320 –
321 –  init_isupport();
226   }
227  
228   /* initialize_server_capabs()
# Line 329 | Line 233 | initialize_message_files(void)
233   static void
234   initialize_server_capabs(void)
235   {
236 <  add_capability("QS", CAP_QS, 1);
237 <  add_capability("EOB", CAP_EOB, 1);
238 <
239 <  if (ServerInfo.sid != NULL)   /* only enable TS6 if we have an SID */
240 <    add_capability("TS6", CAP_TS6, 0);
241 <
338 <  add_capability("ZIP", CAP_ZIP, 0);
339 <  add_capability("CLUSTER", CAP_CLUSTER, 1);
340 < #ifdef HALFOPS
341 <  add_capability("HOPS", CAP_HOPS, 1);
342 < #endif
236 >  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   }
243  
244   /* write_pidfile()
# Line 351 | Line 250 | initialize_server_capabs(void)
250   static void
251   write_pidfile(const char *filename)
252   {
253 <  FBFILE *fb;
253 >  FILE *fb;
254  
255 <  if ((fb = fbopen(filename, "w")))
255 >  if ((fb = fopen(filename, "w")))
256    {
257 <    char buff[32];
257 >    char buf[IRCD_BUFSIZE];
258      unsigned int pid = (unsigned int)getpid();
360 –    size_t nbytes = ircsprintf(buff, "%u\n", pid);
259  
260 <    if ((fbputs(buff, fb, nbytes) == -1))
261 <      ilog(L_ERROR, "Error writing %u to pid file %s (%s)",
262 <           pid, filename, strerror(errno));
260 >    snprintf(buf, sizeof(buf), "%u\n", pid);
261 >
262 >    if (fputs(buf, fb) == -1)
263 >      ilog(LOG_TYPE_IRCD, "Error writing to pid file %s: %s",
264 >           filename, strerror(errno));
265  
266 <    fbclose(fb);
367 <    return;
266 >    fclose(fb);
267    }
268    else
269 <  {
270 <    ilog(L_ERROR, "Error opening pid file %s", filename);
372 <  }
269 >    ilog(LOG_TYPE_IRCD, "Error opening pid file %s: %s",
270 >         filename, strerror(errno));
271   }
272  
273   /* check_pidfile()
# Line 383 | Line 281 | write_pidfile(const char *filename)
281   static void
282   check_pidfile(const char *filename)
283   {
284 <  FBFILE *fb;
285 <  char buff[32];
388 <  pid_t pidfromfile;
284 >  FILE *fb;
285 >  char buf[IRCD_BUFSIZE];
286  
287 <  /* Don't do logging here, since we don't have log() initialised */
391 <  if ((fb = fbopen(filename, "r")))
287 >  if ((fb = fopen(filename, "r")))
288    {
289 <    if (fbgets(buff, 20, fb) == NULL)
290 <    {
291 <      /* log(L_ERROR, "Error reading from pid file %s (%s)", filename,
396 <       * strerror(errno));
397 <       */
398 <    }
289 >    if (!fgets(buf, 20, fb))
290 >      ilog(LOG_TYPE_IRCD, "Error reading from pid file %s: %s",
291 >           filename, strerror(errno));
292      else
293      {
294 <      pidfromfile = atoi(buff);
294 >      pid_t pid = atoi(buf);
295  
296 <      if (!kill(pidfromfile, 0))
296 >      if (!kill(pid, 0))
297        {
298          /* log(L_ERROR, "Server is already running"); */
299          printf("ircd: daemon is already running\n");
300 <        exit(-1);
300 >        exit(EXIT_FAILURE);
301        }
302      }
303  
304 <    fbclose(fb);
304 >    fclose(fb);
305    }
306    else if (errno != ENOENT)
307 <  {
308 <    /* log(L_ERROR, "Error opening pid file %s", filename); */
416 <  }
307 >    ilog(LOG_TYPE_IRCD, "Error opening pid file %s: %s",
308 >         filename, strerror(errno));
309   }
310  
311   /* setup_corefile()
# Line 438 | Line 330 | setup_corefile(void)
330   #endif
331   }
332  
333 < /* init_ssl()
333 > #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 > /* ssl_init()
342   *
343   * inputs       - nothing
344   * output       - nothing
345   * side effects - setups SSL context.
346   */
347   static void
348 < init_ssl(void)
348 > ssl_init(void)
349   {
350   #ifdef HAVE_LIBCRYPTO
351    SSL_load_error_strings();
352    SSLeay_add_ssl_algorithms();
353  
354 <  if ((ServerInfo.server_ctx = SSL_CTX_new(SSLv23_server_method())) == NULL)
354 >  if (!(ConfigServerInfo.server_ctx = SSL_CTX_new(SSLv23_server_method())))
355    {
356 <    const char *s;
356 >    const char *s = ERR_lib_error_string(ERR_get_error());
357  
358 <    fprintf(stderr, "ERROR: Could not initialize the SSL context -- %s\n",
359 <            s = ERR_lib_error_string(ERR_get_error()));
360 <    ilog(L_CRIT, "ERROR: Could not initialize the SSL context -- %s\n", s);
358 >    fprintf(stderr, "ERROR: Could not initialize the SSL Server context -- %s\n", s);
359 >    ilog(LOG_TYPE_IRCD, "ERROR: Could not initialize the SSL Server context -- %s", s);
360 >    exit(EXIT_FAILURE);
361 >    return;  /* Not reached */
362    }
363  
364 <  SSL_CTX_set_options(ServerInfo.server_ctx, SSL_OP_NO_SSLv2);
365 <  SSL_CTX_set_options(ServerInfo.server_ctx, SSL_OP_TLS_ROLLBACK_BUG|SSL_OP_ALL);
366 <  SSL_CTX_set_verify(ServerInfo.server_ctx, SSL_VERIFY_NONE, NULL);
364 >  SSL_CTX_set_options(ConfigServerInfo.server_ctx, SSL_OP_NO_SSLv2|SSL_OP_NO_SSLv3|SSL_OP_NO_TICKET);
365 >  SSL_CTX_set_options(ConfigServerInfo.server_ctx, SSL_OP_SINGLE_DH_USE|SSL_OP_CIPHER_SERVER_PREFERENCE);
366 >  SSL_CTX_set_verify(ConfigServerInfo.server_ctx, SSL_VERIFY_PEER|SSL_VERIFY_CLIENT_ONCE,
367 >                     always_accept_verify_cb);
368 >  SSL_CTX_set_session_cache_mode(ConfigServerInfo.server_ctx, SSL_SESS_CACHE_OFF);
369 >  SSL_CTX_set_cipher_list(ConfigServerInfo.server_ctx, "EECDH+HIGH:EDH+HIGH:HIGH:!aNULL");
370  
371 <  bio_spare_fd = save_spare_fd("SSL private key validation");
371 > #if OPENSSL_VERSION_NUMBER >= 0x009080FFL && !defined(OPENSSL_NO_ECDH)
372 >  {
373 >    EC_KEY *key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
374 >
375 >    if (key)
376 >    {
377 >      SSL_CTX_set_tmp_ecdh(ConfigServerInfo.server_ctx, key);
378 >      EC_KEY_free(key);
379 >    }
380 >  }
381 >
382 >  SSL_CTX_set_options(ConfigServerInfo.server_ctx, SSL_OP_SINGLE_ECDH_USE);
383 > #endif
384 >
385 >  if (!(ConfigServerInfo.client_ctx = SSL_CTX_new(SSLv23_client_method())))
386 >  {
387 >    const char *s = ERR_lib_error_string(ERR_get_error());
388 >
389 >    fprintf(stderr, "ERROR: Could not initialize the SSL Client context -- %s\n", s);
390 >    ilog(LOG_TYPE_IRCD, "ERROR: Could not initialize the SSL Client context -- %s", s);
391 >    exit(EXIT_FAILURE);
392 >    return;  /* Not reached */
393 >  }
394 >
395 >  SSL_CTX_set_options(ConfigServerInfo.client_ctx, SSL_OP_NO_SSLv2|SSL_OP_NO_SSLv3|SSL_OP_NO_TICKET);
396 >  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 >                     always_accept_verify_cb);
399 >  SSL_CTX_set_session_cache_mode(ConfigServerInfo.client_ctx, SSL_SESS_CACHE_OFF);
400   #endif /* HAVE_LIBCRYPTO */
401   }
402  
403 < /* init_callbacks()
404 < *
473 < * inputs       - nothing
474 < * output       - nothing
475 < * side effects - setups standard hook points
403 > /*
404 > * print_startup - print startup information
405   */
406   static void
407 < init_callbacks(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 <  iorecv_cb = register_callback("iorecv", iorecv_default);
419 <  iosend_cb = register_callback("iosend", iosend_default);
420 <  iorecvctrl_cb = register_callback("iorecvctrl", NULL);
421 <  iosendctrl_cb = register_callback("iosendctrl", NULL);
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   int
435   main(int argc, char *argv[])
436   {
437 <  /* Check to see if the user is running
438 <   * us as root, which is a nono
491 <   */
492 <  if (geteuid() == 0)
437 >  /* Check to see if the user is running us as root, which is a nono */
438 >  if (!geteuid())
439    {
440 <    fprintf(stderr, "Don't run ircd as root!!!\n");
440 >    fprintf(stderr, "ERROR: This server won't run as root/superuser\n");
441      return -1;
442    }
443  
444    /* Setup corefile size immediately after boot -kre */
445    setup_corefile();
446  
447 <  /* save server boot time right away, so getrusage works correctly */
447 >  /* Save server boot time right away, so getrusage works correctly */
448    set_time();
449  
450 <  /* It ain't random, but it ought to be a little harder to guess */
450 >  /* It's not random, but it ought to be a little harder to guess */
451    init_genrand(SystemTime.tv_sec ^ (SystemTime.tv_usec | (getpid() << 20)));
452  
453 <  memset(&me, 0, sizeof(me));
454 <  memset(&meLocalUser, 0, sizeof(meLocalUser));
455 <  me.localClient = &meLocalUser;
456 <  dlinkAdd(&me, &me.node, &global_client_list); /* Pointer to beginning
457 <                                                   of Client list */
458 <
459 <  memset(&ServerInfo, 0, sizeof(ServerInfo));
460 <  memset(&ServerStats, 0, sizeof(ServerStats));
461 <
516 <  /* Initialise the channel capability usage counts... */
517 <  init_chcap_usage_counts();
518 <
519 <  ConfigFileEntry.dpath      = DPATH;
520 <  ConfigFileEntry.configfile = CPATH;  /* Server configuration file */
521 <  ConfigFileEntry.klinefile  = KPATH;  /* Server kline file         */
522 <  ConfigFileEntry.xlinefile  = XPATH;  /* Server xline file         */
523 <  ConfigFileEntry.rxlinefile = RXPATH; /* Server regex xline file   */
524 <  ConfigFileEntry.rklinefile = RKPATH; /* Server regex kline file   */
525 <  ConfigFileEntry.dlinefile  = DLPATH; /* dline file                */
526 <  ConfigFileEntry.glinefile  = GPATH;  /* gline log file            */
527 <  ConfigFileEntry.cresvfile  = CRESVPATH; /* channel resv file      */
528 <  ConfigFileEntry.nresvfile  = NRESVPATH; /* nick resv file         */
453 >  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 >
462    myargv = argv;
463 <  umask(077);                /* better safe than sorry --SRB */
463 >  umask(077);  /* umask 077: u=rwx,g=,o= */
464  
465    parseargs(&argc, &argv, myopts);
466  
467    if (printVersion)
468    {
469 <    printf("ircd: version %s\n", ircd_version);
469 >    printf("ircd: version %s(%s)\n", ircd_version, serno);
470      exit(EXIT_SUCCESS);
471    }
472  
473 <  if (chdir(ConfigFileEntry.dpath))
473 >  if (chdir(ConfigGeneral.dpath))
474    {
475      perror("chdir");
476      exit(EXIT_FAILURE);
477    }
478  
479 <  init_ssl();
479 >  ssl_init();
480  
481    if (!server_state.foreground)
482    {
# Line 555 | Line 488 | main(int argc, char *argv[])
488  
489    setup_signals();
490  
558 –  get_ircd_platform(ircd_platform);
559 –
560 –  /* Init the event subsystem */
561 –  eventInit();
491    /* We need this to initialise the fd array before anything else */
492    fdlist_init();
493 <  init_log(logFileName);
494 <  check_can_use_v6();
495 <  init_comm();         /* This needs to be setup early ! -- adrian */
493 >  log_set_file(LOG_TYPE_IRCD, 0, logFileName);
494 >
495 >  init_netio();         /* This needs to be setup early ! -- adrian */
496 >
497    /* Check if there is pidfile and daemon already running */
498    check_pidfile(pidFileName);
499  
500 <  initBlockHeap();
500 >  mp_pool_init();
501    init_dlink_nodes();
502 <  init_callbacks();
573 <  initialize_message_files();
502 >  isupport_init();
503    dbuf_init();
504 <  init_hash();
505 <  init_ip_hash_table();      /* client host ip hash table */
506 <  init_host_hash();          /* Host-hashtable. */
507 <  clear_tree_parse();
508 <  init_client();
509 <  init_class();
581 <  init_whowas();
504 >  hash_init();
505 >  userhost_init();
506 >  ipcache_init();
507 >  client_init();
508 >  class_init();
509 >  whowas_init();
510    watch_init();
511 <  init_auth();          /* Initialise the auth code */
511 >  auth_init();          /* Initialise the auth code */
512    init_resolver();      /* Needs to be setup before the io loop */
513 +  modules_init();
514    read_conf_files(1);   /* cold start init conf files */
586 –  me.id[0] = '\0';
587 –  init_uid();
515    initialize_server_capabs();   /* Set up default_server_capabs */
516 <  initialize_global_set_options();
517 <  init_channels();
516 >  initialize_global_set_options();  /* Has to be called after read_conf_files() */
517 >  channel_init();
518 >  read_links_file();
519 >  motd_init();
520 >  user_modes_init();
521 > #ifdef HAVE_LIBGEOIP
522 >  geoip_ctx = GeoIP_new(GEOIP_MEMORY_CACHE);
523 > #endif
524  
525 <  if (ServerInfo.name == NULL)
525 >  if (EmptyString(ConfigServerInfo.name))
526    {
527 <    ilog(L_CRIT, "No server name specified in serverinfo block.");
527 >    ilog(LOG_TYPE_IRCD, "ERROR: No server name specified in serverinfo block.");
528      exit(EXIT_FAILURE);
529    }
530  
531 <  strlcpy(me.name, ServerInfo.name, sizeof(me.name));
531 >  strlcpy(me.name, ConfigServerInfo.name, sizeof(me.name));
532  
533 <  /* serverinfo{} description must exist.  If not, error out.*/
534 <  if (ServerInfo.description == NULL)
533 >  /* serverinfo {} description must exist.  If not, error out.*/
534 >  if (EmptyString(ConfigServerInfo.description))
535    {
536 <    ilog(L_CRIT,
604 <      "ERROR: No server description specified in serverinfo block.");
536 >    ilog(LOG_TYPE_IRCD, "ERROR: No server description specified in serverinfo block.");
537      exit(EXIT_FAILURE);
538    }
539  
540 <  strlcpy(me.info, ServerInfo.description, sizeof(me.info));
540 >  strlcpy(me.info, ConfigServerInfo.description, sizeof(me.info));
541 >
542 >  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 >  init_uid();
551  
552 <  me.from    = &me;
552 >  me.from = &me;
553    me.servptr = &me;
554 +  me.connection->lasttime = CurrentTime;
555 +  me.connection->since = CurrentTime;
556 +  me.connection->firsttime = CurrentTime;
557  
558    SetMe(&me);
559    make_server(&me);
560  
561 <  me.lasttime = me.since = me.firsttime = CurrentTime;
561 >  hash_add_id(&me);
562    hash_add_client(&me);
618 –  
619 –  /* add ourselves to global_serv_list */
620 –  dlinkAdd(&me, make_dlink_node(), &global_serv_list);
563  
564 <  check_class();
564 >  dlinkAdd(&me, make_dlink_node(), &global_server_list);
565 >  dlinkAdd(&me, &me.node, &global_client_list);
566  
567 < #ifndef STATIC_MODULES
568 <  if (chdir(MODPATH))
569 <  {
570 <    ilog (L_CRIT, "Could not load core modules. Terminating!");
628 <    exit(EXIT_FAILURE);
629 <  }
567 >  load_kline_database();
568 >  load_dline_database();
569 >  load_xline_database();
570 >  load_resv_database();
571  
572    load_all_modules(1);
573    load_conf_modules();
574    load_core_modules(1);
634 –  /* Go back to DPATH after checking to see if we can chdir to MODPATH */
635 –  chdir(ConfigFileEntry.dpath);
636 – #else
637 –  load_all_modules(1);
638 – #endif
639 –  /*
640 –   * assemble_umode_buffer() has to be called after
641 –   * reading conf/loading modules.
642 –   */
643 –  assemble_umode_buffer();
575  
576    write_pidfile(pidFileName);
577  
578 <  ilog(L_NOTICE, "Server Ready");
648 <
649 <  eventAddIsh("cleanup_glines", cleanup_glines, NULL, CLEANUP_GLINES_TIME);
650 <  eventAddIsh("cleanup_tklines", cleanup_tklines, NULL, CLEANUP_TKLINES_TIME);
578 >  event_addish(&event_cleanup_tklines, NULL);
579  
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 <  eventAddIsh("try_connections", try_connections, NULL, STARTUP_CONNECTIONS_TIME);
655 <
656 <  eventAddIsh("collect_zipstats", collect_zipstats, NULL, ZIPSTATS_TIME);
582 >  event_addish(&event_try_connections, NULL);
583  
584    /* Setup the timeout check. I'll shift it later :)  -- adrian */
585 <  eventAddIsh("comm_checktimeouts", comm_checktimeouts, NULL, 1);
585 >  event_add(&event_comm_checktimeouts, NULL);
586  
587 <  if (ConfigServerHide.links_delay > 0)
662 <    eventAddIsh("write_links_file", write_links_file, NULL, ConfigServerHide.links_delay);
663 <  else
664 <    ConfigServerHide.links_disabled = 1;
587 >  event_addish(&event_save_all_databases, NULL);
588  
589 <  if (splitmode)
590 <    eventAddIsh("check_splitmode", check_splitmode, NULL, 60);
589 >  if (ConfigServerHide.flatten_links_delay && event_write_links_file.active == 0)
590 >  {
591 >    event_write_links_file.when = ConfigServerHide.flatten_links_delay;
592 >    event_add(&event_write_links_file, NULL);
593 >  }
594  
595 +  ilog(LOG_TYPE_IRCD, "Server Ready");
596    io_loop();
597 +
598    return 0;
599   }

Diff Legend

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