Make ircd.conf accept "*" as vhost like .11 did. Fix comment in channel.c.
[ircu2.10.12-pk.git] / ircd / channel.c
index db4e7f9a51a60a3fc7519d19f335e0b309d34dfa..81be0d993346796f09e823b4b51ce458a5b1c4bc 100644 (file)
@@ -67,6 +67,10 @@ static unsigned int membershipAllocCount;
 static struct Membership* membershipFreeList;
 /** Freelist for struct Ban*'s */
 static struct Ban* free_bans;
+/** Number of ban structures allocated. */
+static size_t bans_alloc;
+/** Number of ban structures in use. */
+static size_t bans_inuse;
 
 #if !defined(NDEBUG)
 /** return the length (>=0) of a chain of links.
@@ -91,10 +95,8 @@ static void
 set_ban_mask(struct Ban *ban, const char *banstr)
 {
   char *sep;
-  MyFree(ban->banstr);
-  if (!banstr)
-    return;
-  DupString(ban->banstr, banstr);
+  assert(banstr != NULL);
+  ircd_strncpy(ban->banstr, banstr, sizeof(ban->banstr) - 1);
   sep = strrchr(banstr, '@');
   if (sep) {
     ban->nu_len = sep - banstr;
@@ -117,6 +119,9 @@ make_ban(const char *banstr)
   }
   else if (!(ban = MyMalloc(sizeof(*ban))))
     return NULL;
+  else
+    bans_alloc++;
+  bans_inuse++;
   memset(ban, 0, sizeof(*ban));
   set_ban_mask(ban, banstr);
   return ban;
@@ -128,10 +133,22 @@ make_ban(const char *banstr)
 void
 free_ban(struct Ban *ban)
 {
-  MyFree(ban->who);
-  MyFree(ban->banstr);
   ban->next = free_bans;
   free_bans = ban;
+  bans_inuse--;
+}
+
+/** Report ban usage to \a cptr.
+ * @param[in] cptr Client requesting information.
+ */
+void bans_send_meminfo(struct Client *cptr)
+{
+  struct Ban *ban;
+  size_t num_free;
+  for (num_free = 0, ban = free_bans; ban; ban = ban->next)
+    num_free++;
+  send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Bans: inuse %zu(%zu) free %zu alloc %zu",
+            bans_inuse, bans_inuse * sizeof(*ban), num_free, bans_alloc);
 }
 
 /** return the struct Membership* that represents a client on a channel
@@ -241,24 +258,15 @@ int sub1_from_channel(struct Channel* chptr)
 
   chptr->users = 0;
 
-  /*
-   * Also channels without Apass set need to be kept alive,
-   * otherwise Bad Guys(tm) would be able to takeover
-   * existing channels too easily, and then set an Apass!
-   * However, if a channel without Apass becomes empty
-   * then we try to be kind to them and remove possible
-   * limiting modes.
+  /* There is a semantics problem here: Assuming no fragments across a
+   * split, a channel without Apass could be maliciously destroyed and
+   * recreated, and someone could set apass on the new instance.
+   *
+   * This could be fixed by preserving the empty non-Apass channel for
+   * the same time as if it had an Apass (but removing +i and +l), and
+   * reopping the first user to rejoin.  However, preventing net rides
+   * requires a backwards-incompatible protocol change..
    */
-  chptr->mode.mode &= ~MODE_INVITEONLY;
-  chptr->mode.limit = 0;
-  /*
-   * We do NOT reset a possible key or bans because when
-   * the 'channel owners' can't get in because of a key
-   * or ban then apparently there was a fight/takeover
-   * on the channel and we want them to contact IRC opers
-   * who then will educate them on the use of Apass/upass.
-   */
-
   if (!chptr->mode.apass[0])         /* If no Apass, destroy now. */
     destruct_channel(chptr);
   else if (TStime() - chptr->creationtime < 172800)    /* Channel younger than 48 hours? */
@@ -655,6 +663,9 @@ int member_can_send_to_channel(struct Membership* member, int reveal)
    */
   if (member->channel->mode.mode & MODE_MODERATED)
     return 0;
+  /* If only logged in users may join and you're not one, you can't speak. */
+  if (member->channel->mode.mode & MODE_REGONLY && !IsAccount(member->user))
+    return 0;
   /*
    * If you're banned then you can't speak either.
    * but because of the amount of CPU time that is_banned chews
@@ -1268,7 +1279,8 @@ void clean_channelname(char *cn)
   int i;
 
   for (i = 0; cn[i]; i++) {
-    if (i >= CHANNELLEN || !IsChannelChar(cn[i])) {
+    if (i >= IRCD_MIN(CHANNELLEN, feature_int(FEAT_CHANNELLEN))
+        || !IsChannelChar(cn[i])) {
       cn[i] = '\0';
       return;
     }
@@ -2484,6 +2496,11 @@ mode_parse_upass(struct ParseState *state, int *flag_p)
       send_reply(state->sptr, ERR_UPASSNOTSET, state->chptr->chname, state->chptr->chname);
       return;
     }
+    /* cannot set a +U password that is the same as +A */
+    if (state->dir == MODE_ADD && !ircd_strcmp(state->chptr->mode.apass, t_str)) {
+      send_reply(state->sptr, ERR_UPASS_SAME_APASS, state->chptr->chname);
+      return;
+    }
     /* can't add a upass if one is set, nor can one remove the wrong upass */
     if ((state->dir == MODE_ADD && *state->chptr->mode.upass) ||
        (state->dir == MODE_DEL &&
@@ -2726,8 +2743,6 @@ int apply_ban(struct Ban **banlist, struct Ban *newban, int do_free)
     /* If no matches were found, fail. */
     if (do_free)
       free_ban(newban);
-    else
-      MyFree(newban->banstr);
     return 3;
   }
   if (do_free)
@@ -2787,10 +2802,9 @@ mode_parse_ban(struct ParseState *state, int *flag_p)
   newban = state->banlist + (state->numbans++);
   newban->next = 0;
   newban->flags = ((state->dir == MODE_ADD) ? BAN_ADD : BAN_DEL)
-      | (*flag_p == 'b' ? 0 : BAN_EXCEPTION);
-  newban->banstr = NULL;
+      | (*flag_p == MODE_BAN ? 0 : BAN_EXCEPTION);
   set_ban_mask(newban, collapse(pretty_mask(t_str)));
-  newban->who = cli_name(state->sptr);
+  ircd_strncpy(newban->who, cli_name(state->sptr), HOSTLEN);
   newban->when = TStime();
   apply_ban(&state->chptr->banlist, newban, 0);
 }
@@ -2822,12 +2836,12 @@ mode_process_bans(struct ParseState *state)
       count--;
       len -= banlen;
 
-      MyFree(ban->banstr);
-
       continue;
     } else if (ban->flags & BAN_DEL) { /* Deleted a ban? */
+      char *bandup;
+      DupString(bandup, ban->banstr);
       modebuf_mode_string(state->mbuf, MODE_DEL | MODE_BAN,
-                         ban->banstr, 1);
+                         bandup, 1);
 
       if (state->flags & MODE_PARSE_SET) { /* Ok, make it take effect */
        if (prevban) /* clip it out of the list... */
@@ -2837,9 +2851,6 @@ mode_process_bans(struct ParseState *state)
 
        count--;
        len -= banlen;
-
-        ban->banstr = NULL; /* modebuf_mode_string() gave ownership of
-                             * the ban string to state->mbuf */
         free_ban(ban);
 
        changed++;
@@ -2857,7 +2868,6 @@ mode_process_bans(struct ParseState *state)
          !(state->flags & MODE_PARSE_BOUNCE)) {
        count--;
        len -= banlen;
-        MyFree(ban->banstr);
       } else {
        if (state->flags & MODE_PARSE_SET && MyUser(state->sptr) &&
            (len > (feature_int(FEAT_AVBANLEN) * feature_int(FEAT_MAXBANS)) ||
@@ -2866,15 +2876,16 @@ mode_process_bans(struct ParseState *state)
                     ban->banstr);
          count--;
          len -= banlen;
-          MyFree(ban->banstr);
        } else {
+          char *bandup;
          /* add the ban to the buffer */
+          DupString(bandup, ban->banstr);
          modebuf_mode_string(state->mbuf, MODE_ADD | MODE_BAN,
-                             ban->banstr, 1);
+                             bandup, 1);
 
          if (state->flags & MODE_PARSE_SET) { /* create a new ban */
            newban = make_ban(ban->banstr);
-           DupString(newban->who, ban->who);
+            strcpy(newban->who, ban->who);
            newban->when = ban->when;
            newban->flags = ban->flags & BAN_IPMASK;
 
@@ -2999,11 +3010,11 @@ mode_process_clients(struct ParseState *state)
          continue;
         }
 
-        if (feature_bool(FEAT_OPLEVELS)) {
        /* don't allow to deop members with an op level that is <= our own level */
        if (state->sptr != state->cli_change[i].client          /* but allow to deop oneself */
-               && state->member
-               && OpLevel(member) <= OpLevel(state->member)) {
+            && state->chptr->mode.apass[0]
+            && state->member
+            && OpLevel(member) <= OpLevel(state->member)) {
            int equal = (OpLevel(member) == OpLevel(state->member));
            send_reply(state->sptr, ERR_NOTLOWEROPLEVEL,
                       cli_name(state->cli_change[i].client),
@@ -3012,20 +3023,24 @@ mode_process_clients(struct ParseState *state)
                       "deop", equal ? "the same" : "a higher");
          continue;
        }
-      }
     }
     }
 
     /* set op-level of member being opped */
     if ((state->cli_change[i].flag & (MODE_ADD | MODE_CHANOP)) ==
        (MODE_ADD | MODE_CHANOP)) {
-      /* If on a channel with upass set, someone with level x gives ops to someone else,
-         then that person gets level x-1.  On other channels, where upass is not set,
-        the level stays the same. */
-      int level_increment = *state->chptr->mode.upass ? 1 : 0;
-      /* Someone being opped by a server gets op-level 0 */
-      int old_level = (state->member == NULL) ? -level_increment : OpLevel(state->member);
-      SetOpLevel(member, old_level == MAXOPLEVEL ? MAXOPLEVEL : (old_level + level_increment));
+      /* If being opped by an outsider, get oplevel 0 for an apass
+       *   channel, else MAXOPLEVEL.
+       * Otherwise, if not an apass channel, or state->member has
+       *   MAXOPLEVEL, get oplevel MAXOPLEVEL.
+       * Otherwise, get state->member's oplevel+1.
+       */
+      if (!state->member)
+        SetOpLevel(member, state->chptr->mode.apass[0] ? 0 : MAXOPLEVEL);
+      else if (!state->chptr->mode.apass[0] || OpLevel(state->member) == MAXOPLEVEL)
+        SetOpLevel(member, MAXOPLEVEL);
+      else
+        SetOpLevel(member, OpLevel(state->member) + 1);
     }
 
     /* actually effect the change */
@@ -3148,7 +3163,7 @@ mode_parse(struct ModeBuf *mbuf, struct Client *cptr, struct Client *sptr,
 
   for (i = 0; i < MAXPARA; i++) { /* initialize ops/voices arrays */
     state.banlist[i].next = 0;
-    state.banlist[i].who = 0;
+    state.banlist[i].who[0] = '\0';
     state.banlist[i].when = 0;
     state.banlist[i].flags = 0;
     state.cli_change[i].flag = 0;
@@ -3368,24 +3383,34 @@ joinbuf_join(struct JoinBuf *jbuf, struct Channel *chan, unsigned int flags)
        is_local) /* got to remove user here */
       remove_user_from_channel(jbuf->jb_source, chan);
   } else {
+    int oplevel = !chan->mode.apass[0] ? MAXOPLEVEL
+        : (flags & CHFL_CHANNEL_MANAGER) ? 0
+        : 1;
     /* Add user to channel */
     if ((chan->mode.mode & MODE_DELJOINS) && !(flags & CHFL_VOICED_OR_OPPED))
-      add_user_to_channel(chan, jbuf->jb_source, flags | CHFL_DELAYED, 0);
+      add_user_to_channel(chan, jbuf->jb_source, flags | CHFL_DELAYED, oplevel);
     else
-      add_user_to_channel(chan, jbuf->jb_source, flags, 0);
+      add_user_to_channel(chan, jbuf->jb_source, flags, oplevel);
 
     /* send notification to all servers */
     if (jbuf->jb_type != JOINBUF_TYPE_CREATE && !is_local)
-      sendcmdto_serv_butone(jbuf->jb_source, CMD_JOIN, jbuf->jb_connect,
-                           "%H %Tu", chan, chan->creationtime);
+    {
+      if (flags & CHFL_CHANOP)
+        sendcmdto_serv_butone(jbuf->jb_source, CMD_JOIN, jbuf->jb_connect,
+                              "%u:%H %Tu", oplevel, chan, chan->creationtime);
+      else
+        sendcmdto_serv_butone(jbuf->jb_source, CMD_JOIN, jbuf->jb_connect,
+                              "%H %Tu", chan, chan->creationtime);
+    }
 
     if (!((chan->mode.mode & MODE_DELJOINS) && !(flags & CHFL_VOICED_OR_OPPED))) {
       /* Send the notification to the channel */
       sendcmdto_channel_butserv_butone(jbuf->jb_source, CMD_JOIN, chan, NULL, 0, "%H", chan);
 
       /* send an op, too, if needed */
-      if (!MyUser(jbuf->jb_source) && jbuf->jb_type == JOINBUF_TYPE_CREATE)
-       sendcmdto_channel_butserv_butone(jbuf->jb_source, CMD_MODE, chan, NULL, 0, "%H +o %C",
+      if (flags & CHFL_CHANOP && (oplevel < MAXOPLEVEL || !MyUser(jbuf->jb_source)))
+       sendcmdto_channel_butserv_butone((chan->mode.apass[0] ? &me : jbuf->jb_source),
+                                         CMD_MODE, chan, NULL, 0, "%H +o %C",
                                         chan, jbuf->jb_source);
     } else if (MyUser(jbuf->jb_source))
       sendcmdto_one(jbuf->jb_source, CMD_JOIN, jbuf->jb_source, ":%H", chan);
@@ -3469,7 +3494,8 @@ int IsInvited(struct Client* cptr, const void* chptr)
 
 /* RevealDelayedJoin: sends a join for a hidden user */
 
-void RevealDelayedJoin(struct Membership *member) {
+void RevealDelayedJoin(struct Membership *member)
+{
   ClearDelayedJoin(member);
   sendcmdto_channel_butserv_butone(member->user, CMD_JOIN, member->channel, member->user, 0, ":%H",
                                    member->channel);
@@ -3478,14 +3504,15 @@ void RevealDelayedJoin(struct Membership *member) {
 
 /* CheckDelayedJoins: checks and clear +d if necessary */
 
-void CheckDelayedJoins(struct Channel *chan) {
+void CheckDelayedJoins(struct Channel *chan)
+{
   struct Membership *memb2;
-  
+
   if (chan->mode.mode & MODE_WASDELJOINS) {
     for (memb2=chan->members;memb2;memb2=memb2->next_member)
       if (IsDelayedJoin(memb2))
         break;
-    
+
     if (!memb2) {
       /* clear +d */
       chan->mode.mode &= ~MODE_WASDELJOINS;