If a server sends us a new, expired G-line, create it deactivated (fixes SF #2840365).
[ircu2.10.12-pk.git] / ircd / gline.c
index f294d99f456f07694a4c5fd5e22324339c299ebd..dfbf77d2394fdc38e946d9423fa68ba267edb20e 100644 (file)
@@ -349,26 +349,35 @@ gline_propagate(struct Client *cptr, struct Client *sptr, struct Gline *gline)
 
 /** Count number of users who match \a mask.
  * @param[in] mask user\@host or user\@ip mask to check.
+ * @param[in] flags Bitmask possibly containing the value GLINE_LOCAL, to limit searches to this server.
  * @return Count of matching users.
  */
 static int
-count_users(char *mask)
+count_users(char *mask, int flags)
 {
+  struct irc_in_addr ipmask;
   struct Client *acptr;
   int count = 0;
+  int ipmask_valid;
   char namebuf[USERLEN + HOSTLEN + 2];
   char ipbuf[USERLEN + SOCKIPLEN + 2];
+  unsigned char ipmask_len;
 
+  ipmask_valid = ipmask_parse(mask, &ipmask, &ipmask_len);
   for (acptr = GlobalClientList; acptr; acptr = cli_next(acptr)) {
     if (!IsUser(acptr))
       continue;
+    if ((flags & GLINE_LOCAL) && !MyConnect(acptr))
+      continue;
 
     ircd_snprintf(0, namebuf, sizeof(namebuf), "%s@%s",
                  cli_user(acptr)->username, cli_user(acptr)->host);
     ircd_snprintf(0, ipbuf, sizeof(ipbuf), "%s@%s", cli_user(acptr)->username,
                  ircd_ntoa(&cli_ip(acptr)));
 
-    if (!match(mask, namebuf) || !match(mask, ipbuf))
+    if (!match(mask, namebuf)
+        || !match(mask, ipbuf)
+        || (ipmask_valid && ipmask_check(&cli_ip(acptr), &ipmask, ipmask_len)))
       count++;
   }
 
@@ -429,6 +438,8 @@ gline_add(struct Client *cptr, struct Client *sptr, char *userhost,
 
   assert(0 != userhost);
   assert(0 != reason);
+  assert(((flags & (GLINE_GLOBAL | GLINE_LOCAL)) == GLINE_GLOBAL) ||
+         ((flags & (GLINE_GLOBAL | GLINE_LOCAL)) == GLINE_LOCAL));
 
   Debug((DEBUG_DEBUG, "gline_add(\"%s\", \"%s\", \"%s\", \"%s\", %Tu, %Tu "
         "%Tu, 0x%04x)", cli_name(cptr), cli_name(sptr), userhost, reason,
@@ -477,7 +488,7 @@ gline_add(struct Client *cptr, struct Client *sptr, char *userhost,
        break;
       }
 
-      if ((tmp = count_users(uhmask)) >=
+      if ((tmp = count_users(uhmask, flags)) >=
          feature_int(FEAT_GLINEMAXUSERCOUNT) && !(flags & GLINE_OPERFORCE))
        return send_reply(sptr, ERR_TOOMANYUSERS, tmp);
     }
@@ -492,6 +503,9 @@ gline_add(struct Client *cptr, struct Client *sptr, char *userhost,
     if (!IsServer(sptr) && MyConnect(sptr))
       send_reply(sptr, ERR_BADEXPIRE, expire);
     return 0;
+  } else if (expire <= CurrentTime) {
+    /* This expired G-line was forced to be added, so mark it inactive. */
+    flags &= ~GLINE_ACTIVE;
   }
 
   if (!lifetime) /* no lifetime set, use expiration time */
@@ -501,11 +515,12 @@ gline_add(struct Client *cptr, struct Client *sptr, char *userhost,
 
   /* Inform ops... */
   sendto_opmask_butone(0, ircd_strncmp(reason, "AUTO", 4) ? SNO_GLINE :
-                       SNO_AUTO, "%s adding %s %s for %s%s%s, expiring at "
+                       SNO_AUTO, "%s adding %s%s %s for %s%s%s, expiring at "
                        "%Tu: %s",
                        (feature_bool(FEAT_HIS_SNOTICES) || IsServer(sptr)) ?
                          cli_name(sptr) :
                          cli_name((cli_user(sptr))->server),
+                       (flags & GLINE_ACTIVE) ? "" : "deactivated ",
                       (flags & GLINE_LOCAL) ? "local" : "global",
                       (flags & GLINE_BADCHAN) ? "BADCHAN" : "GLINE", user,
                       (flags & (GLINE_BADCHAN|GLINE_REALNAME)) ? "" : "@",
@@ -918,8 +933,7 @@ gline_find(char *userhost, unsigned int flags)
 
   if (flags & (GLINE_BADCHAN | GLINE_ANY)) {
     gliter(BadChanGlineList, gline, sgline) {
-      if ((flags & GLINE_GLOBAL && gline->gl_flags & GLINE_LOCAL) ||
-         (flags & GLINE_LOCAL && gline->gl_flags & GLINE_GLOBAL) ||
+        if ((flags & (GlineIsLocal(gline) ? GLINE_GLOBAL : GLINE_LOCAL)) ||
          (flags & GLINE_LASTMOD && !gline->gl_lastmod))
        continue;
       else if ((flags & GLINE_EXACT ? ircd_strcmp(gline->gl_user, userhost) :
@@ -936,8 +950,7 @@ gline_find(char *userhost, unsigned int flags)
   canon_userhost(t_uh, &user, &host, "*");
 
   gliter(GlobalGlineList, gline, sgline) {
-    if ((flags & GLINE_GLOBAL && gline->gl_flags & GLINE_LOCAL) ||
-       (flags & GLINE_LOCAL && gline->gl_flags & GLINE_GLOBAL) ||
+    if ((flags & (GlineIsLocal(gline) ? GLINE_GLOBAL : GLINE_LOCAL)) ||
        (flags & GLINE_LASTMOD && !gline->gl_lastmod))
       continue;
     else if (flags & GLINE_EXACT) {