Warn when using -x with a non-DEBUGMODE ircd.
[ircu2.10.12-pk.git] / ircd / gline.c
index a0a1eb8b6ece485673dc1d7128c602a684760706..f7cb6192c79a61ce3de1cb600e3dd87da65e9281 100644 (file)
@@ -17,7 +17,7 @@
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  */
-/* @file
+/** @file
  * @brief Implementation of Gline manipulation functions.
  * @version $Id$
  */
 #include "msg.h"
 #include "numnicks.h"
 #include "numeric.h"
-#include "sys.h"    /* FALSE bleah */
 #include "whocmds.h"
 
-#include <assert.h>
+/* #include <assert.h> -- Now using assert in ircd_log.h */
 #include <string.h>
 #include <stdio.h>
 #include <stdlib.h>
-#include <arpa/inet.h> /* for inet_ntoa */
 
 #define CHECK_APPROVED    0    /**< Mask is acceptable */
 #define CHECK_OVERRIDABLE  1   /**< Mask is acceptable, but not by default */
@@ -70,6 +68,29 @@ struct Gline* GlobalGlineList  = 0;
 /** List of BadChan G-lines. */
 struct Gline* BadChanGlineList = 0;
 
+/** Iterate through \a list of G-lines.  Use this like a for loop,
+ * i.e., follow it with braces and use whatever you passed as \a gl
+ * as a single G-line to be acted upon.
+ *
+ * @param[in] list List of G-lines to iterate over.
+ * @param[in] gl Name of a struct Gline pointer variable that will be made to point to the G-lines in sequence.
+ * @param[in] next Name of a scratch struct Gline pointer variable.
+ */
+#define gliter(list, gl, next)                         \
+  /* Iterate through the G-lines in the list */                \
+  for ((gl) = (list); (gl); (gl) = (next))             \
+    /* Figure out the next pointer in list... */       \
+    if ((((next) = (gl)->gl_next) || 1) &&             \
+       /* Then see if it's expired */                  \
+       (gl)->gl_rexpire <= CurrentTime)                \
+      /* Record has expired, so free the G-line */     \
+      gline_free((gl));                                        \
+    /* See if we need to expire the G-line */          \
+    else if (((gl)->gl_expire > CurrentTime ||         \
+             ((gl)->gl_flags &= ~GLINE_ACTIVE)) && 0)  \
+      ; /* empty statement */                          \
+    else
+
 /** Find canonical user and host for a string.
  * If \a userhost starts with '$', assign \a userhost to *user_p and NULL to *host_p.
  * Otherwise, if \a userhost contains '@', assign the earlier part of it to *user_p and the rest to *host_p.
@@ -144,6 +165,7 @@ make_gline(char *user, char *host, char *reason, time_t expire, time_t lastmod,
 
   DupString(gline->gl_reason, reason); /* initialize gline... */
   gline->gl_expire = expire;
+  gline->gl_rexpire = expire;
   gline->gl_lastmod = lastmod;
   gline->gl_flags = flags & GLINE_MASK;
 
@@ -163,10 +185,8 @@ make_gline(char *user, char *host, char *reason, time_t expire, time_t lastmod,
     else
       gline->gl_host = NULL;
 
-    if (*user != '$' && ipmask_parse(host, &gline->gl_addr, &gline->gl_bits)) {
-      Debug((DEBUG_DEBUG,"IP gline: %s/%u", ircd_ntoa(&gline->gl_addr), gline->gl_bits));
+    if (*user != '$' && ipmask_parse(host, &gline->gl_addr, &gline->gl_bits))
       gline->gl_flags |= GLINE_IPMASK;
-    }
 
     if (after) {
       gline->gl_next = after->gl_next;
@@ -200,6 +220,9 @@ do_gline(struct Client *cptr, struct Client *sptr, struct Gline *gline)
   struct Client *acptr;
   int fd, retval = 0, tval;
 
+  if (feature_bool(FEAT_DISABLE_GLINES))
+    return 0; /* G-lines are disabled */
+
   if (GlineIsBadChan(gline)) /* no action taken on badchan glines */
     return 0;
   if (!GlineIsActive(gline)) /* no action taken on inactive glines */
@@ -225,11 +248,7 @@ do_gline(struct Client *cptr, struct Client *sptr, struct Gline *gline)
           continue;
 
         if (GlineIsIpMask(gline)) {
-#ifdef DEBUGMODE
-          char tbuf1[SOCKIPLEN], tbuf2[SOCKIPLEN];
-          Debug((DEBUG_DEBUG,"IP gline: %s %s/%u", ircd_ntoa_r(tbuf1, &cli_ip(cptr)), ircd_ntoa_r(tbuf2, &gline->gl_addr), gline->gl_bits));
-#endif
-          if (!ipmask_check(&cli_ip(cptr), &gline->gl_addr, gline->gl_bits))
+          if (!ipmask_check(&cli_ip(acptr), &gline->gl_addr, gline->gl_bits))
             continue;
         }
         else {
@@ -244,7 +263,7 @@ do_gline(struct Client *cptr, struct Client *sptr, struct Gline *gline)
 
       /* let the ops know about it */
       sendto_opmask_butone(0, SNO_GLINE, "G-line active for %s",
-                    get_client_name(acptr, TRUE));
+                           get_client_name(acptr, SHOW_IP));
 
       /* and get rid of him */
       if ((tval = exit_client_msg(cptr, acptr, &me, "G-lined (%s)",
@@ -293,7 +312,7 @@ gline_checkmask(char *mask)
         if (ipmask < 128)
           flags |= MASK_WILDS;
       } else {
-        if (dots != 3 || ipmask > 3)
+        if (dots != 3 || ipmask > 32)
           return CHECK_REJECTED;
         if (ipmask < 32)
          flags |= MASK_WILDS;
@@ -400,7 +419,12 @@ gline_add(struct Client *cptr, struct Client *sptr, char *userhost,
         /* uh, what to do here? */
         /* The answer, my dear Watson, is we throw a protocol_violation()
            -- hikari */
-        return protocol_violation(sptr,"%s has been smoking the sweet leaf and sent me a whacky gline",cli_name(sptr));
+        if (IsServer(cptr))
+          return protocol_violation(sptr,"%s has been smoking the sweet leaf and sent me a whacky gline",cli_name(sptr));
+        else {
+         sendto_opmask_butone(NULL, SNO_GLINE, "%s has been smoking the sweet leaf and sent me a whacky gline", cli_name(sptr));
+         return 0;
+        }
         break;
     }
      user = (*userhost =='$' ? userhost : userhost+2);
@@ -608,6 +632,7 @@ gline_deactivate(struct Client *cptr, struct Client *sptr, struct Gline *gline,
  * <dt>GLINE_ANY</dt><dd>Search both BadChans and user G-lines.</dd>
  * <dt>GLINE_BADCHAN</dt><dd>Search BadChans.</dd>
  * <dt>GLINE_GLOBAL</dt><dd>Only match global G-lines.</dd>
+ * <dt>GLINE_LOCAL</dt><dd>Only match local G-lines.</dd>
  * <dt>GLINE_LASTMOD</dt><dd>Only match G-lines with a last modification time.</dd>
  * <dt>GLINE_EXACT</dt><dd>Require an exact match of G-line mask.</dd>
  * <dt>anything else</dt><dd>Search user G-lines.</dd>
@@ -619,18 +644,15 @@ gline_deactivate(struct Client *cptr, struct Client *sptr, struct Gline *gline,
 struct Gline *
 gline_find(char *userhost, unsigned int flags)
 {
-  struct Gline *gline;
+  struct Gline *gline = 0;
   struct Gline *sgline;
   char *user, *host, *t_uh;
 
   if (flags & (GLINE_BADCHAN | GLINE_ANY)) {
-    for (gline = BadChanGlineList; gline; gline = sgline) {
-      sgline = gline->gl_next;
-
-      if (gline->gl_expire <= CurrentTime)
-       gline_free(gline);
-      else if ((flags & GLINE_GLOBAL && gline->gl_flags & GLINE_LOCAL) ||
-              (flags & GLINE_LASTMOD && !gline->gl_lastmod))
+    gliter(BadChanGlineList, gline, sgline) {
+      if ((flags & GLINE_GLOBAL && gline->gl_flags & GLINE_LOCAL) ||
+         (flags & GLINE_LOCAL && gline->gl_flags & GLINE_GLOBAL) ||
+         (flags & GLINE_LASTMOD && !gline->gl_lastmod))
        continue;
       else if ((flags & GLINE_EXACT ? ircd_strcmp(gline->gl_user, userhost) :
                match(gline->gl_user, userhost)) == 0)
@@ -643,31 +665,23 @@ gline_find(char *userhost, unsigned int flags)
     return 0;
 
   DupString(t_uh, userhost);
-  canon_userhost(t_uh, &user, &host, 0);
-
-  if (BadPtr(user))
-    return 0;
+  canon_userhost(t_uh, &user, &host, "*");
 
-  for (gline = GlobalGlineList; gline; gline = sgline) {
-    sgline = gline->gl_next;
-
-    if (gline->gl_expire <= CurrentTime)
-      gline_free(gline);
-    else if ((flags & GLINE_GLOBAL && gline->gl_flags & GLINE_LOCAL) ||
-            (flags & GLINE_LASTMOD && !gline->gl_lastmod))
+  gliter(GlobalGlineList, gline, sgline) {
+    if ((flags & GLINE_GLOBAL && gline->gl_flags & GLINE_LOCAL) ||
+       (flags & GLINE_LOCAL && gline->gl_flags & GLINE_GLOBAL) ||
+       (flags & GLINE_LASTMOD && !gline->gl_lastmod))
       continue;
     else if (flags & GLINE_EXACT) {
       if (((gline->gl_host && host && ircd_strcmp(gline->gl_host, host) == 0)
            || (!gline->gl_host && !host)) &&
-          ((!user && ircd_strcmp(gline->gl_user, "*") == 0) ||
-          ircd_strcmp(gline->gl_user, user) == 0))
+          (ircd_strcmp(gline->gl_user, user) == 0))
        break;
     } else {
-      if (((gline->gl_host && host && ircd_strcmp(gline->gl_host, host) == 0)
+      if (((gline->gl_host && host && match(gline->gl_host, host) == 0)
            || (!gline->gl_host && !host)) &&
-         ((!user && ircd_strcmp(gline->gl_user, "*") == 0) ||
-          match(gline->gl_user, user) == 0))
-      break;
+         (match(gline->gl_user, user) == 0))
+       break;
     }
   }
 
@@ -688,14 +702,7 @@ gline_lookup(struct Client *cptr, unsigned int flags)
   struct Gline *gline;
   struct Gline *sgline;
 
-  for (gline = GlobalGlineList; gline; gline = sgline) {
-    sgline = gline->gl_next;
-
-    if (gline->gl_expire <= CurrentTime) {
-      gline_free(gline);
-      continue;
-    }
-    
+  gliter(GlobalGlineList, gline, sgline) {
     if ((flags & GLINE_GLOBAL && gline->gl_flags & GLINE_LOCAL) ||
         (flags & GLINE_LASTMOD && !gline->gl_lastmod))
       continue;
@@ -704,19 +711,12 @@ gline_lookup(struct Client *cptr, unsigned int flags)
       Debug((DEBUG_DEBUG,"realname gline: '%s' '%s'",gline->gl_user,cli_info(cptr)));
       if (match(gline->gl_user+2, cli_info(cptr)) != 0)
         continue;
-      if (!GlineIsActive(gline))
-        continue;
-      return gline;
     }
     else {
       if (match(gline->gl_user, (cli_user(cptr))->username) != 0)
         continue;
 
       if (GlineIsIpMask(gline)) {
-#ifdef DEBUGMODE
-        char tbuf1[SOCKIPLEN], tbuf2[SOCKIPLEN];
-        Debug((DEBUG_DEBUG,"IP gline: %s %s/%u", ircd_ntoa_r(tbuf1, &cli_ip(cptr)), ircd_ntoa_r(tbuf2, &gline->gl_addr), gline->gl_bits));
-#endif
         if (!ipmask_check(&cli_ip(cptr), &gline->gl_addr, gline->gl_bits))
           continue;
       }
@@ -762,12 +762,8 @@ gline_burst(struct Client *cptr)
   struct Gline *gline;
   struct Gline *sgline;
 
-  for (gline = GlobalGlineList; gline; gline = sgline) { /* all glines */
-    sgline = gline->gl_next;
-
-    if (gline->gl_expire <= CurrentTime) /* expire any that need expiring */
-      gline_free(gline);
-    else if (!GlineIsLocal(gline) && gline->gl_lastmod)
+  gliter(GlobalGlineList, gline, sgline) {
+    if (!GlineIsLocal(gline) && gline->gl_lastmod)
       sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s%s%s %Tu %Tu :%s",
                    GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
                     gline->gl_host ? "@" : "",
@@ -776,12 +772,8 @@ gline_burst(struct Client *cptr)
                     gline->gl_reason);
   }
 
-  for (gline = BadChanGlineList; gline; gline = sgline) { /* all glines */
-    sgline = gline->gl_next;
-
-    if (gline->gl_expire <= CurrentTime) /* expire any that need expiring */
-      gline_free(gline);
-    else if (!GlineIsLocal(gline) && gline->gl_lastmod)
+  gliter(BadChanGlineList, gline, sgline) {
+    if (!GlineIsLocal(gline) && gline->gl_lastmod)
       sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s %Tu %Tu :%s",
                    GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
                    gline->gl_expire - CurrentTime, gline->gl_lastmod,
@@ -835,30 +827,20 @@ gline_list(struct Client *sptr, char *userhost)
               GlineIsLocal(gline) ? cli_name(&me) : "*",
               GlineIsActive(gline) ? '+' : '-', gline->gl_reason);
   } else {
-    for (gline = GlobalGlineList; gline; gline = sgline) {
-      sgline = gline->gl_next;
-
-      if (gline->gl_expire <= CurrentTime)
-       gline_free(gline);
-      else
-       send_reply(sptr, RPL_GLIST, gline->gl_user,
-                   gline->gl_host ? "@" : "",
-                   gline->gl_host ? gline->gl_host : "",
-                  gline->gl_expire + TSoffset,
-                  GlineIsLocal(gline) ? cli_name(&me) : "*",
-                  GlineIsActive(gline) ? '+' : '-', gline->gl_reason);
+    gliter(GlobalGlineList, gline, sgline) {
+      send_reply(sptr, RPL_GLIST, gline->gl_user,
+                gline->gl_host ? "@" : "",
+                gline->gl_host ? gline->gl_host : "",
+                gline->gl_expire + TSoffset,
+                GlineIsLocal(gline) ? cli_name(&me) : "*",
+                GlineIsActive(gline) ? '+' : '-', gline->gl_reason);
     }
 
-    for (gline = BadChanGlineList; gline; gline = sgline) {
-      sgline = gline->gl_next;
-
-      if (gline->gl_expire <= CurrentTime)
-       gline_free(gline);
-      else
-       send_reply(sptr, RPL_GLIST, gline->gl_user, "", "",
-                  gline->gl_expire + TSoffset,
-                  GlineIsLocal(gline) ? cli_name(&me) : "*",
-                  GlineIsActive(gline) ? '+' : '-', gline->gl_reason);
+    gliter(BadChanGlineList, gline, sgline) {
+      send_reply(sptr, RPL_GLIST, gline->gl_user, "", "",
+                gline->gl_expire + TSoffset,
+                GlineIsLocal(gline) ? cli_name(&me) : "*",
+                GlineIsActive(gline) ? '+' : '-', gline->gl_reason);
     }
   }
 
@@ -878,16 +860,13 @@ gline_stats(struct Client *sptr, const struct StatDesc *sd,
   struct Gline *gline;
   struct Gline *sgline;
 
-  for (gline = GlobalGlineList; gline; gline = sgline) {
-    sgline = gline->gl_next;
-
-    if (gline->gl_expire <= CurrentTime)
-      gline_free(gline);
-    else
-      send_reply(sptr, RPL_STATSGLINE, 'G', gline->gl_user,
-                 gline->gl_host ? "@" : "",
-                 gline->gl_host ? gline->gl_host : "",
-                gline->gl_expire + TSoffset, gline->gl_reason);
+  gliter(GlobalGlineList, gline, sgline) {
+    send_reply(sptr, RPL_STATSGLINE, 'G', gline->gl_user,
+              gline->gl_host ? "@" : "",
+              gline->gl_host ? gline->gl_host : "",
+              gline->gl_expire + TSoffset,
+              GlineIsActive(gline) ? '+' : '-',
+              gline->gl_reason);
   }
 }