Rip out clean_channelname() and reject invalid names where appropriate.
[ircu2.10.12-pk.git] / ircd / m_join.c
index 437a52f2bbb58be6ee6009f3049427898c268d38..2a58a37fa1aa423432400203e614bafee6982b36 100644 (file)
  * $Id$
  */
 
-/*
- * m_functions execute protocol messages on this server:
- *
- *    cptr    is always NON-NULL, pointing to a *LOCAL* client
- *            structure (with an open socket connected!). This
- *            identifies the physical socket where the message
- *            originated (or which caused the m_function to be
- *            executed--some m_functions may call others...).
- *
- *    sptr    is the source of the message, defined by the
- *            prefix part of the message if present. If not
- *            or prefix not found, then sptr==cptr.
- *
- *            (!IsServer(cptr)) => (cptr == sptr), because
- *            prefixes are taken *only* from servers...
- *
- *            (IsServer(cptr))
- *                    (sptr == cptr) => the message didn't
- *                    have the prefix.
- *
- *                    (sptr != cptr && IsServer(sptr) means
- *                    the prefix specified servername. (?)
- *
- *                    (sptr != cptr && !IsServer(sptr) means
- *                    that message originated from a remote
- *                    user (not local).
- *
- *            combining
- *
- *            (!IsServer(sptr)) means that, sptr can safely
- *            taken as defining the target structure of the
- *            message in this server.
- *
- *    *Always* true (if 'parse' and others are working correct):
- *
- *    1)      sptr->from == cptr  (note: cptr->from == cptr)
- *
- *    2)      MyConnect(sptr) <=> sptr == cptr (e.g. sptr
- *            *cannot* be a local connection, unless it's
- *            actually cptr!). [MyConnect(x) should probably
- *            be defined as (x == x->from) --msa ]
- *
- *    parc    number of variable parameter strings (if zero,
- *            parv is allowed to be NULL)
- *
- *    parv    a NULL terminated list of parameter pointers,
- *
- *                    parv[0], sender (prefix string), if not present
- *                            this points to an empty string.
- *                    parv[1]...parv[parc-1]
- *                            pointers to additional parameters
- *                    parv[parc] == NULL, *always*
- *
- *            note:   it is guaranteed that parv[0]..parv[parc-1] are all
- *                    non-NULL pointers.
- */
-#if 0
-/*
- * No need to include handlers.h here the signatures must match
- * and we don't need to force a rebuild of all the handlers everytime
- * we add a new one to the list. --Bleep
- */
-#include "handlers.h"
-#endif /* 0 */
+#include "config.h"
+
 #include "channel.h"
 #include "client.h"
 #include "gline.h"
 #include "hash.h"
 #include "ircd.h"
 #include "ircd_chattr.h"
+#include "ircd_features.h"
+#include "ircd_log.h"
 #include "ircd_reply.h"
 #include "ircd_string.h"
 #include "msg.h"
 #include "s_user.h"
 #include "send.h"
 
-#include <assert.h>
+/* #include <assert.h> -- Now using assert in ircd_log.h */
 #include <stdlib.h>
 #include <string.h>
 
-/*
- * Helper function to find last 0 in a comma-separated list of
- * channel names.
+/** Searches for and handles a 0 in a join list.
+ * @param[in] cptr Client that sent us the message.
+ * @param[in] sptr Original source of message.
+ * @param[in] chanlist List of channels to join.
+ * @return First token in \a chanlist after the final 0 entry, which
+ * may be its nul terminator (if the final entry is a 0 entry).
  */
 static char *
-last0(char *chanlist)
+last0(struct Client *cptr, struct Client *sptr, char *chanlist)
 {
   char *p;
+  int join0 = 0;
 
   for (p = chanlist; p[0]; p++) /* find last "JOIN 0" */
     if (p[0] == '0' && (p[1] == ',' || p[1] == '\0' || !IsChannelChar(p[1]))) {
-      chanlist = p; /* we'll start parsing here */
-
-      if (!p[1]) /* hit the end */
-       break;
-
-      p++;
+      if (*++p == ',')
+        p++;
+      chanlist = p;
+      join0 = 1;
     } else {
       while (p[0] != ',' && p[0] != '\0') /* skip past channel name */
        p++;
@@ -131,41 +73,33 @@ last0(char *chanlist)
        break;
     }
 
-  return chanlist;
-}
-
-/*
- * Helper function to perform a JOIN 0 if needed; returns 0 if channel
- * name is not 0, else removes user from all channels and returns 1.
- */
-static int
-join0(struct JoinBuf *join, struct Client *cptr, struct Client *sptr,
-      char *chan)
-{
-  struct Membership *member;
-  struct JoinBuf part;
+  if (join0) {
+    struct JoinBuf part;
+    struct Membership *member;
 
-  /* is it a JOIN 0? */
-  if (chan[0] != '0' || chan[1] != '\0')
-    return 0;
-  
-  joinbuf_join(join, 0, 0); /* join special channel 0 */
+    joinbuf_init(&part, sptr, cptr, JOINBUF_TYPE_PARTALL,
+                 "Left all channels", 0);
 
-  /* leave all channels */
-  joinbuf_init(&part, sptr, cptr, JOINBUF_TYPE_PARTALL,
-              "Left all channels", 0);
+    joinbuf_join(&part, 0, 0);
 
-  while ((member = cli_user(sptr)->channel))
-    joinbuf_join(&part, member->channel,
-                IsZombie(member) ? CHFL_ZOMBIE : 0);
+    while ((member = cli_user(sptr)->channel))
+      joinbuf_join(&part, member->channel,
+                   IsZombie(member) ? CHFL_ZOMBIE :
+                   IsDelayedJoin(member) ? CHFL_DELAYED :
+                   0);
 
-  joinbuf_flush(&part);
+    joinbuf_flush(&part);
+  }
 
-  return 1;
+  return chanlist;
 }
 
-/*
- * m_join - generic message handler
+/** Handle a JOIN message from a client connection.
+ * See @ref m_functions for discussion of the arguments.
+ * @param[in] cptr Client that sent us the message.
+ * @param[in] sptr Original source of message.
+ * @param[in] parc Number of arguments.
+ * @param[in] parv Argument vector.
  */
 int m_join(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
 {
@@ -173,8 +107,6 @@ int m_join(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
   struct JoinBuf join;
   struct JoinBuf create;
   struct Gline *gline;
-  unsigned int flags = 0;
-  int i;
   char *p = 0;
   char *chanlist;
   char *name;
@@ -186,22 +118,36 @@ int m_join(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
   joinbuf_init(&join, sptr, cptr, JOINBUF_TYPE_JOIN, 0, 0);
   joinbuf_init(&create, sptr, cptr, JOINBUF_TYPE_CREATE, 0, TStime());
 
-  chanlist = last0(parv[1]); /* find last "JOIN 0" */
+  chanlist = last0(cptr, sptr, parv[1]); /* find last "JOIN 0" */
 
   keys = parv[2]; /* remember where keys are */
 
   for (name = ircd_strtok(&p, chanlist, ","); name;
        name = ircd_strtok(&p, 0, ",")) {
-    clean_channelname(name);
+    char *key = 0;
 
-    if (join0(&join, cptr, sptr, name)) /* did client do a JOIN 0? */
-      continue;
+    /* If we have any more keys, take the first for this channel. */
+    if (!BadPtr(keys)
+        && (keys = strchr(key = keys, ',')))
+      *keys++ = '\0';
 
-    if (!IsChannelName(name)) { /* bad channel name */
+    /* Empty keys are the same as no keys. */
+    if (key && !key[0])
+      key = 0;
+
+    if (!IsChannelName(name) || !strIsIrcCh(name))
+    {
+      /* bad channel name */
       send_reply(sptr, ERR_NOSUCHCHANNEL, name);
       continue;
     }
 
+    if (cli_user(sptr)->joined >= feature_int(FEAT_MAXCHANNELSPERUSER)
+       && !HasPriv(sptr, PRIV_CHAN_LIMIT)) {
+      send_reply(sptr, ERR_TOOMANYCHANNELS, name);
+      break; /* no point processing the other channels */
+    }
+
     /* BADCHANed channel */
     if ((gline = gline_find(name, GLINE_BADCHAN | GLINE_EXACT)) &&
        GlineIsActive(gline) && !IsAnOper(sptr)) {
@@ -209,61 +155,87 @@ int m_join(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
       continue;
     }
 
-    if ((chptr = FindChannel(name))) {
-      if (find_member_link(chptr, sptr))
-       continue; /* already on channel */
+    if (!(chptr = FindChannel(name))) {
+      if ((name[0] == '&') && !feature_bool(FEAT_LOCAL_CHANNELS)) {
+        send_reply(sptr, ERR_NOSUCHCHANNEL, name);
+        continue;
+      }
 
-      flags = CHFL_DEOPPED;
-    } else
-      flags = IsModelessChannel(name) ? CHFL_DEOPPED : CHFL_CHANOP;
+      if (!(chptr = get_channel(sptr, name, CGT_CREATE)))
+        continue;
 
-    if (cli_user(sptr)->joined >= MAXCHANNELSPERUSER &&
-       !HasPriv(sptr, PRIV_CHAN_LIMIT)) {
-      send_reply(sptr, ERR_TOOMANYCHANNELS, chptr ? chptr->chname : name);
-      break; /* no point processing the other channels */
-    }
+      /* Try to add the new channel as a recent target for the user. */
+      if (check_target_limit(sptr, chptr, chptr->chname, 1)) {
+        chptr->members = 0;
+        destruct_channel(chptr);
+        continue;
+      }
 
-    if (chptr) {
-      if (check_target_limit(sptr, chptr, chptr->chname, 0))
-       continue; /* exceeded target limit */
-      else if ((i = can_join(sptr, chptr, keys))) {
-       if (i > MAGIC_OPER_OVERRIDE) { /* oper overrode mode */
-         switch (i - MAGIC_OPER_OVERRIDE) {
-         case ERR_CHANNELISFULL: /* figure out which mode */
-           i = 'l';
-           break;
-
-         case ERR_INVITEONLYCHAN:
-           i = 'i';
-           break;
-
-         case ERR_BANNEDFROMCHAN:
-           i = 'b';
-           break;
-
-         case ERR_BADCHANNELKEY:
-           i = 'k';
-           break;
-         }
-
-         /* send accountability notice */
-         sendto_opmask_butone(0, SNO_HACK4, "OPER JOIN: %C JOIN %H "
-                              "(overriding +%c)", sptr, chptr, i);
-       } else {
-         send_reply(sptr, i, chptr->chname);
-         continue;
-       }
-      } /* else if ((i = can_join(sptr, chptr, keys))) { */
+      joinbuf_join(&create, chptr, CHFL_CHANOP | CHFL_CHANNEL_MANAGER);
+    } else if (find_member_link(chptr, sptr)) {
+      continue; /* already on channel */
+    } else if (check_target_limit(sptr, chptr, chptr->chname, 0)) {
+      continue;
+    } else {
+      int flags = CHFL_DEOPPED;
+      int err = 0;
+
+      /* Check target change limits. */
+
+      /* Check Apass/Upass -- since we only ever look at a single
+       * "key" per channel now, this hampers brute force attacks. */
+      if (key && !strcmp(key, chptr->mode.apass))
+        flags = CHFL_CHANOP | CHFL_CHANNEL_MANAGER;
+      else if (key && !strcmp(key, chptr->mode.upass))
+        flags = CHFL_CHANOP;
+      else if (IsInvited(sptr, chptr)) {
+        /* Invites bypass these other checks. */
+      } else if (chptr->mode.mode & MODE_INVITEONLY)
+        err = ERR_INVITEONLYCHAN;
+      else if (chptr->mode.limit && (chptr->users >= chptr->mode.limit))
+        err = ERR_CHANNELISFULL;
+      else if ((chptr->mode.mode & MODE_REGONLY) && !IsAccount(sptr))
+        err = ERR_NEEDREGGEDNICK;
+      else if (find_ban(sptr, chptr->banlist))
+        err = ERR_BANNEDFROMCHAN;
+      else if (*chptr->mode.key && (!key || strcmp(key, chptr->mode.key)))
+        err = ERR_BADCHANNELKEY;
+
+      /* An oper with WALK_LCHAN privilege can join a local channel
+       * he otherwise could not join by using "OVERRIDE" as the key.
+       * This will generate a HACK(4) notice, but fails if the oper
+       * could normally join the channel. */
+      if (IsLocalChannel(chptr->chname)
+          && HasPriv(sptr, PRIV_WALK_LCHAN)
+          && !(flags & CHFL_CHANOP)
+          && key && !strcmp(key, "OVERRIDE")
+          && strcmp(chptr->mode.key, "OVERRIDE"))
+      {
+        switch (err) {
+        case 0:
+          send_reply(sptr, ERR_DONTCHEAT, chptr->chname);
+          continue;
+        case ERR_INVITEONLYCHAN: err = 'i'; break;
+        case ERR_CHANNELISFULL:  err = 'l'; break;
+        case ERR_BANNEDFROMCHAN: err = 'b'; break;
+        case ERR_BADCHANNELKEY:  err = 'k'; break;
+        case ERR_NEEDREGGEDNICK: err = 'r'; break;
+        default: err = '?'; break;
+        }
+        /* send accountability notice */
+        sendto_opmask_butone(0, SNO_HACK4, "OPER JOIN: %C JOIN %H "
+                             "(overriding +%c)", sptr, chptr, err);
+        err = 0;
+      }
+
+      /* Is there some reason the user may not join? */
+      if (err) {
+        send_reply(sptr, err, chptr->chname);
+        continue;
+      }
 
       joinbuf_join(&join, chptr, flags);
-    } else if (!(chptr = get_channel(sptr, name, CGT_CREATE)))
-      continue; /* couldn't get channel */
-    else if (check_target_limit(sptr, chptr, chptr->chname, 1)) {
-      /* Note: check_target_limit will only ever return 0 here */
-      sub1_from_channel(chptr); /* created it... */
-      continue;
-    } else
-      joinbuf_join(&create, chptr, flags);
+    }
 
     del_invite(sptr, chptr);
 
@@ -282,22 +254,32 @@ int m_join(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
   return 0;
 }
 
-/*
- * ms_join - server message handler
+/** Handle a JOIN message from a server connection.
+ * See @ref m_functions for discussion of the arguments.
+ * @param[in] cptr Client that sent us the message.
+ * @param[in] sptr Original source of message.
+ * @param[in] parc Number of arguments.
+ * @param[in] parv Argument vector.
  */
 int ms_join(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
 {
   struct Membership *member;
   struct Channel *chptr;
   struct JoinBuf join;
-  unsigned int flags = 0;
+  unsigned int flags;
   time_t creation = 0;
   char *p = 0;
   char *chanlist;
   char *name;
 
-  if (IsServer(sptr)) {
-    return protocol_violation(sptr,"%s tried to JOIN a channel, duh!", cli_name(sptr));
+  if (IsServer(sptr))
+  {
+    return protocol_violation(cptr,
+                              "%s tried to JOIN %s, duh!",
+                              cli_name(sptr),
+                              (parc < 2 || *parv[1] == '\0') ? "a channel" :
+                                                               parv[1]
+                              );
   }
 
   if (parc < 2 || *parv[1] == '\0')
@@ -308,43 +290,62 @@ int ms_join(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
 
   joinbuf_init(&join, sptr, cptr, JOINBUF_TYPE_JOIN, 0, 0);
 
-  chanlist = last0(parv[1]); /* find last "JOIN 0" */
+  chanlist = last0(cptr, sptr, parv[1]); /* find last "JOIN 0" */
 
   for (name = ircd_strtok(&p, chanlist, ","); name;
        name = ircd_strtok(&p, 0, ",")) {
-    clean_channelname(name);
 
-    if (join0(&join, cptr, sptr, name)) /* did client do a JOIN 0? */
-      continue;
+    if (name[0] == '0' && name[1] == ':')
+    {
+      flags = CHFL_CHANOP | CHFL_CHANNEL_MANAGER;
+      name += 2;
+    }
+    else if (name[0] == '1' && name[1] == ':')
+    {
+      flags = CHFL_CHANOP;
+      name += 2;
+    }
+    else
+      flags = CHFL_DEOPPED;
 
-    if (IsLocalChannel(name) || !IsChannelName(name)) {
-      protocol_violation(sptr,"%s tried to join %s",cli_name(cptr),name);
+    if (IsLocalChannel(name) || !IsChannelName(name))
+    {
+      protocol_violation(cptr, "%s tried to join %s", cli_name(sptr), name);
       continue;
     }
 
-    if (!(chptr = FindChannel(name))) {
+    if (!(chptr = FindChannel(name)))
+    {
       /* No channel exists, so create one */
-      if (!(chptr = get_channel(sptr, name, CGT_CREATE))) {
+      if (!(chptr = get_channel(sptr, name, CGT_CREATE)))
+      {
         protocol_violation(sptr,"couldn't get channel %s for %s",
                           name,cli_name(sptr));
        continue;
       }
-      flags = CHFL_DEOPPED | ((cli_flags(sptr) & FLAGS_TS8) ? CHFL_SERVOPOK : 0);
+      flags |= HasFlag(sptr, FLAG_TS8) ? CHFL_SERVOPOK : 0;
 
-      /* when the network is 2.10.11+ then remove MAGIC_REMOTE_JOIN_TS */ 
+      /* when the network is 2.10.11+ then remove MAGIC_REMOTE_JOIN_TS */
       chptr->creationtime = creation ? creation : MAGIC_REMOTE_JOIN_TS;
     }
     else { /* We have a valid channel? */
-      if ((member = find_member_link(chptr, sptr))) {
+      if ((member = find_member_link(chptr, sptr)))
+      {
+       /* It is impossible to get here --Run */
        if (!IsZombie(member)) /* already on channel */
          continue;
 
        flags = member->status & (CHFL_DEOPPED | CHFL_SERVOPOK);
        remove_user_from_channel(sptr, chptr);
        chptr = FindChannel(name);
-      } else
-       flags = CHFL_DEOPPED | ((cli_flags(sptr) & FLAGS_TS8) ? CHFL_SERVOPOK : 0);
-    } 
+      }
+      else
+        flags |= HasFlag(sptr, FLAG_TS8) ? CHFL_SERVOPOK : 0;
+      /* Always copy the timestamp when it is older, that is the only way to
+         ensure network-wide synchronization of creation times. */
+      if (creation && creation < chptr->creationtime)
+       chptr->creationtime = creation;
+    }
 
     joinbuf_join(&join, chptr, flags);
   }