added basic ssl support to ircu
[ircu2.10.12-pk.git] / ircd / s_serv.c
index 4cb3459cc6c9ca9354e6e5116f2048c420f2343d..abcf6aa55b66825e333e7f8c26fa9c7368d1807f 100644 (file)
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- *
- * $Id$
  */
+/** @file
+ * @brief Miscellaneous server support functions.
+ * @version $Id$
+ */
+#include "config.h"
+
 #include "s_serv.h"
 #include "IPcheck.h"
 #include "channel.h"
 #include "client.h"
-#include "crule.h"
 #include "gline.h"
 #include "hash.h"
 #include "ircd.h"
 #include "ircd_alloc.h"
+#include "ircd_log.h"
 #include "ircd_reply.h"
 #include "ircd_string.h"
-#include "ircd_xopen.h"
+#include "ircd_snprintf.h"
+#include "ircd_crypt.h"
 #include "jupe.h"
 #include "list.h"
-#include "msg.h"
 #include "match.h"
+#include "msg.h"
+#include "msgq.h"
 #include "numeric.h"
 #include "numnicks.h"
 #include "parse.h"
 #include "s_misc.h"
 #include "s_user.h"
 #include "send.h"
-#include "sprintf_irc.h"
 #include "struct.h"
 #include "sys.h"
 #include "userload.h"
 
-#include <assert.h>
+/* #include <assert.h> -- Now using assert in ircd_log.h */
 #include <stdlib.h>
 #include <string.h>
 
+/** Maximum connection count since last restart. */
 unsigned int max_connection_count = 0;
+/** Maximum (local) client count since last restart. */
 unsigned int max_client_count = 0;
 
-int exit_new_server(struct Client* cptr, struct Client* sptr,
-                    const char* host, time_t timestamp, const char* fmt, ...)
+/** Squit a new (pre-burst) server.
+ * @param cptr Local client that tried to introduce the server.
+ * @param sptr Server to disconnect.
+ * @param host Name of server being disconnected.
+ * @param timestamp Link time of server being disconnected.
+ * @param pattern Format string for squit message.
+ * @return CPTR_KILLED if cptr == sptr, else 0.
+ */
+int exit_new_server(struct Client *cptr, struct Client *sptr, const char *host,
+                    time_t timestamp, const char *pattern, ...)
 {
-  va_list vl;
-  char *buf =
-      (char*) MyMalloc(strlen(me.name) + strlen(host) + 22 + strlen(fmt));
-  assert(0 != buf);
-  va_start(vl, fmt);
+  struct VarData vd;
+  int retval = 0;
+
+  vd.vd_format = pattern;
+  va_start(vd.vd_args, pattern);
+
   if (!IsServer(sptr))
-    return vexit_client_msg(cptr, cptr, &me, fmt, vl);
-  sprintf_irc(buf, ":%s " TOK_SQUIT " %s " TIME_T_FMT " :", me.name, host, timestamp);
-  strcat(buf, fmt);
-  vsendto_one(cptr, buf, vl);
-  va_end(vl);
-  MyFree(buf);
-  return 0;
+    retval = vexit_client_msg(cptr, cptr, &me, pattern, vd.vd_args);
+  else
+    sendcmdto_one(&me, CMD_SQUIT, cptr, "%s %Tu :%v", host, timestamp, &vd);
+
+  va_end(vd.vd_args);
+
+  return retval;
 }
 
+/** Indicate whether \a a is between \a b and #me (that is, \a b would
+ * be killed if \a a squits).
+ * @param a A server that may be between us and \a b.
+ * @param b A client that may be on the far side of \a a.
+ * @return Non-zero if \a a is between \a b and #me.
+ */
 int a_kills_b_too(struct Client *a, struct Client *b)
 {
-  for (; b != a && b != &me; b = b->serv->up);
+  for (; b != a && b != &me; b = cli_serv(b)->up);
   return (a == b ? 1 : 0);
 }
 
-/*
- * server_estab
- *
- * May only be called after a SERVER was received from cptr,
- * and thus make_server was called, and serv->prot set. --Run
+/** Handle a connection that has sent a valid PASS and SERVER.
+ * @param cptr New peer server.
+ * @param aconf Connect block for \a cptr.
+ * @return Zero.
  */
 int server_estab(struct Client *cptr, struct ConfItem *aconf)
 {
   struct Client* acptr = 0;
   const char*    inpath;
-  int split,     i;
+  int            i;
 
   assert(0 != cptr);
-  assert(0 != cptr->local);
+  assert(0 != cli_local(cptr));
 
-  split = (0 != ircd_strcmp(cptr->name, cptr->sockhost)
-      &&   0 != ircd_strncmp(cptr->info, "JUPE", 4));
-  inpath = cptr->name;
+  inpath = cli_name(cptr);
 
   if (IsUnknown(cptr)) {
     if (aconf->passwd[0])
-      sendto_one(cptr, "PASS :%s", aconf->passwd);
+      sendrawto_one(cptr, MSG_PASS " :%s", aconf->passwd);
     /*
      *  Pass my info to the new server
      */
-    sendto_one(cptr, "SERVER %s 1 " TIME_T_FMT " " TIME_T_FMT " J%s %s%s :%s",
-        me.name, me.serv->timestamp, cptr->serv->timestamp,
-        MAJOR_PROTOCOL, NumServCap(&me),
-        (me.info[0]) ? (me.info) : "IRCers United");
-    /*
-     * Don't charge this IP# for connecting
-     * XXX - if this comes from a server port, it will not have been added
-     * to the IP check registry, see add_connection in s_bsd.c 
-     */
-    IPcheck_connect_fail(cptr->ip);
+    sendrawto_one(cptr, MSG_SERVER " %s 1 %Tu %Tu J%s %s%s +%s6 :%s",
+                 cli_name(&me), cli_serv(&me)->timestamp,
+                 cli_serv(cptr)->timestamp, MAJOR_PROTOCOL, NumServCap(&me),
+                 feature_bool(FEAT_HUB) ? "h" : "",
+                 *(cli_info(&me)) ? cli_info(&me) : "IRCers United");
   }
 
-  det_confs_butmask(cptr, CONF_LEAF | CONF_HUB | CONF_SERVER | CONF_UWORLD);
+  det_confs_butmask(cptr, CONF_SERVER | CONF_UWORLD);
 
   if (!IsHandshake(cptr))
     hAddClient(cptr);
   SetServer(cptr);
-  cptr->handler = SERVER_HANDLER;
+  cli_handler(cptr) = SERVER_HANDLER;
   Count_unknownbecomesserver(UserStats);
-
-  release_dns_reply(cptr);
-
   SetBurst(cptr);
 
-  nextping = CurrentTime;
+/*    nextping = CurrentTime; */
 
   /*
    * NOTE: check for acptr->user == cptr->serv->user is necessary to insure
    * that we got the same one... bleah
    */
-  if (cptr->serv->user && *cptr->serv->by &&
-      (acptr = findNUser(cptr->serv->by))) {
-    if (acptr->user == cptr->serv->user) {
-      if (MyUser(acptr))
-        sendto_one(acptr, ":%s NOTICE %s :Link with %s established.",
-                   me.name, acptr->name, inpath);
-      else
-        sendto_one(acptr, "%s NOTICE %s%s :Link with %s established.",
-                   NumServ(&me), NumNick(acptr), inpath);
+  if (cli_serv(cptr)->user && *(cli_serv(cptr))->by &&
+      (acptr = findNUser(cli_serv(cptr)->by))) {
+    if (cli_user(acptr) == cli_serv(cptr)->user) {
+      sendcmdto_one(&me, CMD_NOTICE, acptr, "%C :Link with %s established.",
+                    acptr, inpath);
     }
     else {
       /*
        * if not the same client, set by to empty string
        */
       acptr = 0;
-      *cptr->serv->by = '\0';
+      *(cli_serv(cptr))->by = '\0';
     }
   }
 
-  sendto_lops_butone(acptr, "Link with %s established.", inpath);
-  cptr->serv->up = &me;
-  cptr->serv->updown = add_dlink(&me.serv->down, cptr);
-  sendto_op_mask(SNO_NETWORK, "Net junction: %s %s", me.name, cptr->name);
+  sendto_opmask_butone(acptr, SNO_OLDSNO, "Link with %s established.", inpath);
+  cli_serv(cptr)->up = &me;
+  cli_serv(cptr)->updown = add_dlink(&(cli_serv(&me))->down, cptr);
+  sendto_opmask_butone(0, SNO_NETWORK, "Net junction: %s %s", cli_name(&me),
+                       cli_name(cptr));
   SetJunction(cptr);
   /*
    * Old sendto_serv_but_one() call removed because we now
@@ -173,30 +181,28 @@ int server_estab(struct Client *cptr, struct ConfItem *aconf)
     if (!(acptr = LocalClientArray[i]) || !IsServer(acptr) ||
         acptr == cptr || IsMe(acptr))
       continue;
-    if (!match(me.name, cptr->name))
+    if (!match(cli_name(&me), cli_name(cptr)))
       continue;
-    if (split)
-    {
-        sendto_one(acptr, "%s " TOK_SERVER " %s 2 0 " TIME_T_FMT " %s%u %s%s 0 :%s",
-            NumServ(&me), cptr->name, cptr->serv->timestamp,
-            (Protocol(cptr) > 9) ? "J" : "J0", Protocol(cptr),
-            NumServCap(cptr), cptr->info);
-    }
-    else
-    {
-        sendto_one(acptr, "%s " TOK_SERVER " %s 2 0 " TIME_T_FMT " %s%u %s%s 0 :%s",
-            NumServ(&me), cptr->name, cptr->serv->timestamp,
-            (Protocol(cptr) > 9) ? "J" : "J0", Protocol(cptr),
-            NumServCap(cptr), cptr->info);
-    }
+    sendcmdto_one(&me, CMD_SERVER, acptr,
+                 "%s 2 0 %Tu J%02u %s%s +%s%s%s :%s", cli_name(cptr),
+                 cli_serv(cptr)->timestamp, Protocol(cptr), NumServCap(cptr),
+                 IsHub(cptr) ? "h" : "", IsService(cptr) ? "s" : "",
+                 IsIPv6(cptr) ? "6" : "", cli_info(cptr));
   }
 
+  /* Send these as early as possible so that glined users/juped servers can
+   * be removed from the network while the remote server is still chewing
+   * our burst.
+   */
+  gline_burst(cptr);
+  jupe_burst(cptr);
+
   /*
    * Pass on my client information to the new server
    *
    * First, pass only servers (idea is that if the link gets
-   * cancelled beacause the server was already there,
-   * there are no NICK's to be cancelled...). Of course,
+   * canceled because the server was already there,
+   * there are no NICK's to be canceled...). Of course,
    * if cancellation occurs, all this info is sent anyway,
    * and I guess the link dies when a read is attempted...? --msa
    *
@@ -206,9 +212,9 @@ int server_estab(struct Client *cptr, struct ConfItem *aconf)
    * a race condition, not the normal way of operation...
    */
 
-  for (acptr = &me; acptr; acptr = acptr->prev) {
+  for (acptr = &me; acptr; acptr = cli_prev(acptr)) {
     /* acptr->from == acptr for acptr == cptr */
-    if (acptr->from == cptr)
+    if (cli_from(acptr) == cptr)
       continue;
     if (IsServer(acptr)) {
       const char* protocol_str;
@@ -218,49 +224,33 @@ int server_estab(struct Client *cptr, struct ConfItem *aconf)
       else
         protocol_str = IsBurst(acptr) ? "J0" : "P0";
 
-      if (0 == match(me.name, acptr->name))
+      if (0 == match(cli_name(&me), cli_name(acptr)))
         continue;
-      split = (MyConnect(acptr) && 
-               0 != ircd_strcmp(acptr->name, acptr->sockhost) &&
-               0 != ircd_strncmp(acptr->info, "JUPE", 4));
-      if (split)
-      {
-          sendto_one(cptr,
-              "%s " TOK_SERVER " %s %d 0 " TIME_T_FMT " %s%u %s%s 0 :%s",
-              NumServ(acptr->serv->up), acptr->name,
-              acptr->hopcount + 1, acptr->serv->timestamp,
-              protocol_str, Protocol(acptr),
-              NumServCap(acptr), acptr->info);
-      }
-      else
-      {
-          sendto_one(cptr,
-              "%s " TOK_SERVER " %s %d 0 " TIME_T_FMT " %s%u %s%s 0 :%s",
-              NumServ(acptr->serv->up), acptr->name,
-              acptr->hopcount + 1, acptr->serv->timestamp,
-              protocol_str, Protocol(acptr), 
-              NumServCap(acptr), acptr->info);
-      }
+      sendcmdto_one(cli_serv(acptr)->up, CMD_SERVER, cptr,
+                   "%s %d 0 %Tu %s%u %s%s +%s%s%s :%s", cli_name(acptr),
+                   cli_hopcount(acptr) + 1, cli_serv(acptr)->timestamp,
+                   protocol_str, Protocol(acptr), NumServCap(acptr),
+                   IsHub(acptr) ? "h" : "", IsService(acptr) ? "s" : "",
+                   IsIPv6(acptr) ? "6" : "", cli_info(acptr));
     }
   }
 
-  for (acptr = &me; acptr; acptr = acptr->prev)
+  for (acptr = &me; acptr; acptr = cli_prev(acptr))
   {
     /* acptr->from == acptr for acptr == cptr */
-    if (acptr->from == cptr)
+    if (cli_from(acptr) == cptr)
       continue;
     if (IsUser(acptr))
     {
-      char xxx_buf[8];
+      char xxx_buf[25];
       char *s = umode_str(acptr);
-      sendto_one(cptr, *s ?
-            "%s N %s %d " TIME_T_FMT " %s %s +%s %s %s%s :%s" :
-            "%s N %s %d " TIME_T_FMT " %s %s %s%s %s%s :%s",
-            NumServ(acptr->user->server),
-            acptr->name, acptr->hopcount + 1, acptr->lastnick,
-            acptr->user->username, acptr->user->host,
-            s, inttobase64(xxx_buf, ntohl(acptr->ip.s_addr), 6),
-            NumNick(acptr), acptr->info);
+      sendcmdto_one(cli_user(acptr)->server, CMD_NICK, cptr,
+                   "%s %d %Tu %s %s %s%s%s%s %s%s :%s",
+                   cli_name(acptr), cli_hopcount(acptr) + 1, cli_lastnick(acptr),
+                   cli_user(acptr)->username, cli_user(acptr)->realhost,
+                   *s ? "+" : "", s, *s ? " " : "",
+                   iptobase64(xxx_buf, &cli_ip(acptr), sizeof(xxx_buf), IsIPv6(cptr)),
+                   NumNick(acptr), cli_info(acptr));
     }
   }
   /*
@@ -272,9 +262,7 @@ int server_estab(struct Client *cptr, struct ConfItem *aconf)
     for (chptr = GlobalChannelList; chptr; chptr = chptr->next)
       send_channel_modes(cptr, chptr);
   }
-  jupe_burst(cptr);
-  gline_burst(cptr);
-  sendto_one(cptr, "%s EB", NumServ(&me));
+  sendcmdto_one(&me, CMD_END_OF_BURST, cptr, "");
   return 0;
 }