Author: Kev <klmitch@mit.edu>
[ircu2.10.12-pk.git] / ircd / gline.c
index bc18f39ce2c49eb9671364535e1122bab176f4f7..68b7a9e83b59bb1efbedc118f9b763161de64fd9 100644 (file)
@@ -5,7 +5,7 @@
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 1, or (at your option)
+ * the Free Software Foundation; either version 2, or (at your option)
  * any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * 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 Implementation of Gline manipulation functions.
+ * @version $Id$
+ */
+#include "config.h"
+
 #include "gline.h"
 #include "client.h"
 #include "ircd.h"
 #include "ircd_alloc.h"
+#include "ircd_features.h"
+#include "ircd_log.h"
 #include "ircd_reply.h"
+#include "ircd_snprintf.h"
 #include "ircd_string.h"
 #include "match.h"
 #include "numeric.h"
 #include "s_bsd.h"
 #include "s_debug.h"
 #include "s_misc.h"
+#include "s_stats.h"
 #include "send.h"
 #include "struct.h"
-#include "support.h"
 #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 */
+#define CHECK_REJECTED    2    /**< Mask is totally unacceptable */
+
+#define MASK_WILD_0    0x01    /**< Wildcards in the last position */
+#define MASK_WILD_1    0x02    /**< Wildcards in the next-to-last position */
+
+#define MASK_WILD_MASK 0x03    /**< Mask out the positional wildcards */
+
+#define MASK_WILDS     0x10    /**< Mask contains wildcards */
+#define MASK_IP                0x20    /**< Mask is an IP address */
+#define MASK_HALT      0x40    /**< Finished processing mask */
+
+/** List of user G-lines. */
 struct Gline* GlobalGlineList  = 0;
+/** List of BadChan G-lines. */
 struct Gline* BadChanGlineList = 0;
 
+/** 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.
+ * Otherwise, assign \a def_user to *user_p and \a userhost to *host_p.
+ *
+ * @param[in] userhost Input string from user.
+ * @param[out] user_p Gets pointer to user (or channel/realname) part of hostmask.
+ * @param[out] host_p Gets point to host part of hostmask (may be assigned NULL).
+ * @param[in] def_user Default value for user part.
+ */
 static void
 canon_userhost(char *userhost, char **user_p, char **host_p, char *def_user)
 {
   char *tmp;
 
+  if (*userhost == '$') {
+    *user_p = userhost;
+    *host_p = NULL;
+    return;
+  }
+
   if (!(tmp = strchr(userhost, '@'))) {
     *user_p = def_user;
     *host_p = userhost;
@@ -61,32 +101,40 @@ canon_userhost(char *userhost, char **user_p, char **host_p, char *def_user)
   }
 }
 
+/** Create a Gline structure.
+ * @param[in] user User part of mask.
+ * @param[in] host Host part of mask (NULL if not applicable).
+ * @param[in] reason Reason for G-line.
+ * @param[in] expire Expiration timestamp.
+ * @param[in] lastmod Last modification timestamp.
+ * @param[in] flags Bitwise combination of GLINE_* bits.
+ * @return Newly allocated G-line.
+ */
 static struct Gline *
-make_gline(char *userhost, char *reason, time_t expire, time_t lastmod,
+make_gline(char *user, char *host, char *reason, time_t expire, time_t lastmod,
           unsigned int flags)
 {
   struct Gline *gline, *sgline, *after = 0;
-  char *user, *host;
 
   if (!(flags & GLINE_BADCHAN)) { /* search for overlapping glines first */
-    canon_userhost(userhost, &user, &host, "*"); /* find user and host */
 
     for (gline = GlobalGlineList; gline; gline = sgline) {
       sgline = gline->gl_next;
 
       if (gline->gl_expire <= CurrentTime)
        gline_free(gline);
-      else if ((gline->gl_flags & GLINE_LOCAL) != (flags & GLINE_LOCAL))
+      else if (((gline->gl_flags & GLINE_LOCAL) != (flags & GLINE_LOCAL)) ||
+               (gline->gl_host && !host) || (!gline->gl_host && host))
        continue;
-      else if (!mmatch(gline->gl_user, user) && /* gline contains new mask */
-              !mmatch(gline->gl_host, host)) {
+      else if (!mmatch(gline->gl_user, user) /* gline contains new mask */
+              && (gline->gl_host == NULL || !mmatch(gline->gl_host, host))) {
        if (expire <= gline->gl_expire) /* will expire before wider gline */
          return 0;
        else
          after = gline; /* stick new gline after this one */
-      } else if (!mmatch(user, gline->gl_user) && /* new mask contains gline */
-                !mmatch(host, gline->gl_host) &&
-                gline->gl_expire <= expire) /* gline expires before new one */
+      } else if (!mmatch(user, gline->gl_user) /* new mask contains gline */
+                && (gline->gl_host==NULL || !mmatch(host, gline->gl_host)) 
+                && gline->gl_expire <= expire) /* old expires before new */
        gline_free(gline); /* save some memory */
     }
   }
@@ -100,7 +148,7 @@ make_gline(char *userhost, char *reason, time_t expire, time_t lastmod,
   gline->gl_flags = flags & GLINE_MASK;
 
   if (flags & GLINE_BADCHAN) { /* set a BADCHAN gline */
-    DupString(gline->gl_user, userhost); /* first, remember channel */
+    DupString(gline->gl_user, user); /* first, remember channel */
     gline->gl_host = 0;
 
     gline->gl_next = BadChanGlineList; /* then link it into list */
@@ -110,25 +158,13 @@ make_gline(char *userhost, char *reason, time_t expire, time_t lastmod,
     BadChanGlineList = gline;
   } else {
     DupString(gline->gl_user, user); /* remember them... */
-    DupString(gline->gl_host, host);
-
-    if (check_if_ipmask(host)) { /* mark if it's an IP mask */
-      int class;
-      char ipname[16];
-      int ad[4] = { 0 };
-      int bits2 = 0;
-       
-      class = sscanf(host,"%d.%d.%d.%d/%d",
-                     &ad[0],&ad[1],&ad[2],&ad[3], &bits2);
-      if (class!=5) {
-        gline->bits=class*8;
-      }
-      else {
-        gline->bits=bits2;
-      }
-      sprintf_irc(ipname,"%d.%d.%d.%d",ad[0],ad[1],ad[2],ad[3]);
-      gline->ipnum.s_addr = inet_addr(ipname);
-      Debug((DEBUG_DEBUG,"IP gline: %08x/%i",gline->ipnum.s_addr,gline->bits));
+    if (*user != '$')
+      DupString(gline->gl_host, host);
+    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));
       gline->gl_flags |= GLINE_IPMASK;
     }
 
@@ -150,12 +186,22 @@ make_gline(char *userhost, char *reason, time_t expire, time_t lastmod,
   return gline;
 }
 
+/** Check local clients against a new G-line.
+ * If the G-line is inactive or a badchan, return immediately.
+ * Otherwise, if any users match it, disconnect them.
+ * @param[in] cptr Peer connect that sent the G-line.
+ * @param[in] sptr Client that originated the G-line.
+ * @param[in] gline New G-line to check.
+ * @return Zero, unless \a sptr G-lined himself, in which case CPTR_KILLED.
+ */
 static int
 do_gline(struct Client *cptr, struct Client *sptr, struct Gline *gline)
 {
   struct Client *acptr;
   int fd, retval = 0, tval;
 
+  if (GlineIsBadChan(gline)) /* no action taken on badchan glines */
+    return 0;
   if (!GlineIsActive(gline)) /* no action taken on inactive glines */
     return 0;
 
@@ -164,21 +210,32 @@ do_gline(struct Client *cptr, struct Client *sptr, struct Gline *gline)
      * get the users!
      */
     if ((acptr = LocalClientArray[fd])) {
-      if (!acptr->user)
+      if (!cli_user(acptr))
        continue;
-       
-      if (acptr->user->username && 
-          match(gline->gl_user, acptr->user->username) == 0)
-          continue;
-          
-      if (GlineIsIpMask(gline)) {
-        Debug((DEBUG_DEBUG,"IP gline: %08x %08x/%i",cptr->ip.s_addr,gline->ipnum.s_addr,gline->bits));
-        if ((cptr->ip.s_addr & NETMASK(gline->bits)) != gline->ipnum.s_addr)
-          continue;
-      }
-      else {
-        if (match(gline->gl_host, acptr->sock_ip) != 0)
+
+      if (GlineIsRealName(gline)) { /* Realname Gline */
+       Debug((DEBUG_DEBUG,"Realname Gline: %s %s",(cli_info(acptr)),
+                                       gline->gl_user+2));
+        if (match(gline->gl_user+2, cli_info(acptr)) != 0)
+            continue;
+        Debug((DEBUG_DEBUG,"Matched!"));
+      } else { /* Host/IP gline */
+        if (cli_user(acptr)->username &&
+            match(gline->gl_user, (cli_user(acptr))->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;
+        }
+        else {
+          if (match(gline->gl_host, cli_sockhost(acptr)) != 0)
+            continue;
+        }
       }
 
       /* ok, here's one that got G-lined */
@@ -187,7 +244,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, FALSE));
+                    get_client_name(acptr, TRUE));
 
       /* and get rid of him */
       if ((tval = exit_client_msg(cptr, acptr, &me, "G-lined (%s)",
@@ -198,36 +255,178 @@ do_gline(struct Client *cptr, struct Client *sptr, struct Gline *gline)
   return retval;
 }
 
-static void
-propagate_gline(struct Client *cptr, struct Client *sptr, struct Gline *gline)
+/**
+ * Implements the mask checking applied to local G-lines.
+ * Basically, host masks must have a minimum of two non-wild domain
+ * fields, and IP masks must have a minimum of 16 bits.  If the mask
+ * has even one wild-card, OVERRIDABLE is returned, assuming the other
+ * check doesn't fail.
+ * @param[in] mask G-line mask to check.
+ * @return One of CHECK_REJECTED, CHECK_OVERRIDABLE, or CHECK_APPROVED.
+ */
+static int
+gline_checkmask(char *mask)
+{
+  unsigned int flags = MASK_IP;
+  unsigned int dots = 0;
+  unsigned int ipmask = 0;
+
+  for (; *mask; mask++) { /* go through given mask */
+    if (*mask == '.') { /* it's a separator; advance positional wilds */
+      flags = (flags & ~MASK_WILD_MASK) | ((flags << 1) & MASK_WILD_MASK);
+      dots++;
+
+      if ((flags & (MASK_IP | MASK_WILDS)) == MASK_IP)
+       ipmask += 8; /* It's an IP with no wilds, count bits */
+    } else if (*mask == '*' || *mask == '?')
+      flags |= MASK_WILD_0 | MASK_WILDS; /* found a wildcard */
+    else if (*mask == '/') { /* n.n.n.n/n notation; parse bit specifier */
+      ++mask;
+      ipmask = strtoul(mask, &mask, 10);
+
+      /* sanity-check to date */
+      if (*mask || (flags & (MASK_WILDS | MASK_IP)) != MASK_IP)
+       return CHECK_REJECTED;
+      if (!dots) {
+        if (ipmask > 128)
+          return CHECK_REJECTED;
+        if (ipmask < 128)
+          flags |= MASK_WILDS;
+      } else {
+        if (dots != 3 || ipmask > 3)
+          return CHECK_REJECTED;
+        if (ipmask < 32)
+         flags |= MASK_WILDS;
+      }
+
+      flags |= MASK_HALT; /* Halt the ipmask calculation */
+      break; /* get out of the loop */
+    } else if (!IsIP6Char(*mask)) {
+      flags &= ~MASK_IP; /* not an IP anymore! */
+      ipmask = 0;
+    }
+  }
+
+  /* Sanity-check quads */
+  if (dots > 3 || (!(flags & MASK_WILDS) && dots < 3)) {
+    flags &= ~MASK_IP;
+    ipmask = 0;
+  }
+
+  /* update bit count if necessary */
+  if ((flags & (MASK_IP | MASK_WILDS | MASK_HALT)) == MASK_IP)
+    ipmask += 8;
+
+  /* Check to see that it's not too wide of a mask */
+  if (flags & MASK_WILDS &&
+      ((!(flags & MASK_IP) && (dots < 2 || flags & MASK_WILD_MASK)) ||
+       (flags & MASK_IP && ipmask < 16)))
+    return CHECK_REJECTED; /* to wide, reject */
+
+  /* Ok, it's approved; require override if it has wildcards, though */
+  return flags & MASK_WILDS ? CHECK_OVERRIDABLE : CHECK_APPROVED;
+}
+
+/** Forward a G-line to other servers.
+ * @param[in] cptr Client that sent us the G-line.
+ * @param[in] sptr Client that originated the G-line.
+ * @param[in] gline G-line to forward.
+ * @return Zero.
+ */
+int
+gline_propagate(struct Client *cptr, struct Client *sptr, struct Gline *gline)
 {
   if (GlineIsLocal(gline) || (IsUser(sptr) && !gline->gl_lastmod))
-    return;
+    return 0;
 
   if (gline->gl_lastmod)
     sendcmdto_serv_butone(sptr, CMD_GLINE, cptr, "* %c%s%s%s %Tu %Tu :%s",
                          GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
-                         GlineIsBadChan(gline) ? "" : "@",
-                         GlineIsBadChan(gline) ? "" : gline->gl_host,
+                         gline->gl_host ? "@" : "",
+                         gline->gl_host ? gline->gl_host : "",
                          gline->gl_expire - CurrentTime, gline->gl_lastmod,
                          gline->gl_reason);
   else
-    sendcmdto_serv_butone(sptr, CMD_GLINE, cptr, "* %c%s%s%s %Tu :%s",
-                         GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
-                         GlineIsBadChan(gline) ? "" : "@",
-                         GlineIsBadChan(gline) ? "" : gline->gl_host,
+    sendcmdto_serv_butone(sptr, CMD_GLINE, cptr,
+                         (GlineIsRemActive(gline) ?
+                          "* +%s%s%s %Tu :%s" : "* -%s%s%s"),
+                         gline->gl_user, 
+                         gline->gl_host ? "@" : "",
+                         gline->gl_host ? gline->gl_host : "",
                          gline->gl_expire - CurrentTime, gline->gl_reason);
+
+  return 0;
 }
 
-int 
+/** Create a new G-line and add it to global lists.
+ * \a userhost may be in one of four forms:
+ * \li A channel name, to add a BadChan.
+ * \li A string starting with $R and followed by a mask to match against their realname.
+ * \li A user\@IP mask (user\@ part optional) to create an IP-based ban.
+ * \li A user\@host mask (user\@ part optional) to create a hostname ban.
+ *
+ * @param[in] cptr Client that sent us the G-line.
+ * @param[in] sptr Client that originated the G-line.
+ * @param[in] userhost Text mask for the G-line.
+ * @param[in] reason Reason for G-line.
+ * @param[in] expire Duration of G-line in seconds.
+ * @param[in] lastmod Last modification time of G-line.
+ * @param[in] flags Bitwise combination of GLINE_* flags.
+ * @return Zero or CPTR_KILLED, depending on whether \a sptr is suicidal.
+ */
+int
 gline_add(struct Client *cptr, struct Client *sptr, char *userhost,
          char *reason, time_t expire, time_t lastmod, unsigned int flags)
 {
   struct Gline *agline;
+  char uhmask[USERLEN + HOSTLEN + 2];
+  char *user, *host;
+  int tmp;
 
   assert(0 != userhost);
   assert(0 != reason);
 
+  if (*userhost == '#' || *userhost == '&') {
+    if ((flags & GLINE_LOCAL) && !HasPriv(sptr, PRIV_LOCAL_BADCHAN))
+      return send_reply(sptr, ERR_NOPRIVILEGES);
+
+    flags |= GLINE_BADCHAN;
+    user = userhost;
+    host = 0;
+  } else if (*userhost == '$') {
+    switch (*userhost == '$' ? userhost[1] : userhost[3]) {
+      case 'R': flags |= GLINE_REALNAME; break;
+      default:
+        /* 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));
+        break;
+    }
+     user = (*userhost =='$' ? userhost : userhost+2);
+     host = 0;
+  } else {
+    canon_userhost(userhost, &user, &host, "*");
+    if (sizeof(uhmask) <
+       ircd_snprintf(0, uhmask, sizeof(uhmask), "%s@%s", user, host))
+      return send_reply(sptr, ERR_LONGMASK);
+    else if (MyUser(sptr) || (IsUser(sptr) && flags & GLINE_LOCAL)) {
+      switch (gline_checkmask(host)) {
+      case CHECK_OVERRIDABLE: /* oper overrided restriction */
+       if (flags & GLINE_OPERFORCE)
+         break;
+       /*FALLTHROUGH*/
+      case CHECK_REJECTED:
+       return send_reply(sptr, ERR_MASKTOOWIDE, uhmask);
+       break;
+      }
+
+      if ((tmp = count_users(uhmask)) >=
+         feature_int(FEAT_GLINEMAXUSERCOUNT) && !(flags & GLINE_OPERFORCE))
+       return send_reply(sptr, ERR_TOOMANYUSERS, tmp);
+    }
+  }
+
   /*
    * You cannot set a negative (or zero) expire time, nor can you set an
    * expiration time for greater than GLINE_MAX_EXPIRE.
@@ -240,55 +439,47 @@ gline_add(struct Client *cptr, struct Client *sptr, char *userhost,
 
   expire += CurrentTime; /* convert from lifetime to timestamp */
 
-  /* NO_OLD_GLINE allows *@#channel to work correctly */
-  if (*userhost == '#' || *userhost == '&' || *userhost == '+'
-# ifndef NO_OLD_GLINE
-      || userhost[2] == '#' || userhost[2] == '&' || userhost[2] == '+'
-# endif /* OLD_GLINE */
-      ) {
-# ifndef LOCAL_BADCHAN
-    if (flags & GLINE_LOCAL)
-      return 0;
-# endif
-    flags |= GLINE_BADCHAN;
-  }
-
   /* Inform ops... */
-  sendto_opmask_butone(0, SNO_GLINE, "%s adding %s %s for %s, expiring at "
-                      "%Tu: %s",
-                      IsServer(sptr) ? sptr->name : sptr->user->server->name,
-                      flags & GLINE_LOCAL ? "local" : "global",
-                      flags & GLINE_BADCHAN ? "BADCHAN" : "GLINE", userhost,
+  sendto_opmask_butone(0, ircd_strncmp(reason, "AUTO", 4) ? SNO_GLINE :
+                       SNO_AUTO, "%s adding %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_LOCAL) ? "local" : "global",
+                      (flags & GLINE_BADCHAN) ? "BADCHAN" : "GLINE", user,
+                      (flags & (GLINE_BADCHAN|GLINE_REALNAME)) ? "" : "@",
+                      (flags & (GLINE_BADCHAN|GLINE_REALNAME)) ? "" : host,
                       expire + TSoffset, reason);
 
-#ifdef GPATH
   /* and log it */
-  write_log(GPATH, "# %Tu %C adding %s %s for %s, expiring at %Tu: %s\n",
-           TStime(), sptr, flags & GLINE_LOCAL ? "local" : "global",
-           flags & GLINE_BADCHAN ? "BADCHAN" : "GLINE", userhost,
+  log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
+           "%#C adding %s %s for %s%s%s, expiring at %Tu: %s", sptr,
+           flags & GLINE_LOCAL ? "local" : "global",
+           flags & GLINE_BADCHAN ? "BADCHAN" : "GLINE", user,
+           flags & (GLINE_BADCHAN|GLINE_REALNAME) ? "" : "@",
+           flags & (GLINE_BADCHAN|GLINE_REALNAME) ? "" : host,
            expire + TSoffset, reason);
-#endif /* GPATH */
 
   /* make the gline */
-  agline = make_gline(userhost, reason, expire, lastmod, flags);
+  agline = make_gline(userhost, reason, expire, lastmod, flags);
 
   if (!agline) /* if it overlapped, silently return */
     return 0;
 
-  propagate_gline(cptr, sptr, agline);
-
-  if (GlineIsBadChan(agline))
-    return 0;
-
-#ifdef GPATH
-  /* this can be inserted into the conf */
-  write_log(GPATH, "%c:%s:%s:%s\n", GlineIsIpMask(agline) ? 'k' : 'K',
-           GlineHost(agline), GlineReason(agline), GlineUser(agline));
-#endif /* GPATH */
+  gline_propagate(cptr, sptr, agline);
 
   return do_gline(cptr, sptr, agline); /* knock off users if necessary */
 }
 
+/** Activate a currently inactive G-line.
+ * @param[in] cptr Peer that told us to activate the G-line.
+ * @param[in] sptr Client that originally thought it was a good idea.
+ * @param[in] gline G-line to activate.
+ * @param[in] lastmod New value for last modification timestamp.
+ * @param[in] flags 0 if the activation should be propagated, GLINE_LOCAL if not.
+ * @return Zero, unless \a sptr had a death wish (in which case CPTR_KILLED).
+ */
 int
 gline_activate(struct Client *cptr, struct Client *sptr, struct Gline *gline,
               time_t lastmod, unsigned int flags)
@@ -304,10 +495,12 @@ gline_activate(struct Client *cptr, struct Client *sptr, struct Gline *gline,
   else {
     gline->gl_flags |= GLINE_ACTIVE;
 
-    if (gline->gl_lastmod >= lastmod) /* force lastmod to increase */
-      gline->gl_lastmod++;
-    else
-      gline->gl_lastmod = lastmod;
+    if (gline->gl_lastmod) {
+      if (gline->gl_lastmod >= lastmod) /* force lastmod to increase */
+       gline->gl_lastmod++;
+      else
+       gline->gl_lastmod = lastmod;
+    }
   }
 
   if ((saveflags & GLINE_ACTMASK) == GLINE_ACTIVE)
@@ -315,28 +508,36 @@ gline_activate(struct Client *cptr, struct Client *sptr, struct Gline *gline,
 
   /* Inform ops and log it */
   sendto_opmask_butone(0, SNO_GLINE, "%s activating global %s for %s%s%s, "
-                      "expiring at %Tu: %s",
-                      IsServer(sptr) ? sptr->name : sptr->user->server->name,
-                      GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
-                      gline->gl_user, GlineIsBadChan(gline) ? "" : "@",
-                      GlineIsBadChan(gline) ? "" : gline->gl_host,
-                      gline->gl_expire + TSoffset, gline->gl_reason);
-
-#ifdef GPATH
-  write_log(GPATH, "# %Tu %C activating global %s for %s%s%s, expiring at "
-           "%Tu: %s\n", TStime(), sptr,
-           GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
-           gline->gl_user, GlineIsBadChan(gline) ? "" : "@",
-           GlineIsBadChan(gline) ? "" : gline->gl_host,
+                       "expiring at %Tu: %s",
+                       (feature_bool(FEAT_HIS_SNOTICES) || IsServer(sptr)) ?
+                         cli_name(sptr) :
+                         cli_name((cli_user(sptr))->server),
+                       GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
+                       gline->gl_user, gline->gl_host ? "@" : "",
+                       gline->gl_host ? gline->gl_host : "",
+                       gline->gl_expire + TSoffset, gline->gl_reason);
+  
+  log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
+           "%#C activating global %s for %s%s%s, expiring at %Tu: %s", sptr,
+           GlineIsBadChan(gline) ? "BADCHAN" : "GLINE", gline->gl_user,
+           gline->gl_host ? "@" : "",
+           gline->gl_host ? gline->gl_host : "",
            gline->gl_expire + TSoffset, gline->gl_reason);
-#endif /* GPATH */
 
   if (!(flags & GLINE_LOCAL)) /* don't propagate local changes */
-    propagate_gline(cptr, sptr, gline);
+    gline_propagate(cptr, sptr, gline);
 
-  return GlineIsBadChan(gline) ? 0 : do_gline(cptr, sptr, gline);
+  return do_gline(cptr, sptr, gline);
 }
 
+/** Deactivate a G-line.
+ * @param[in] cptr Peer that gave us the message.
+ * @param[in] sptr Client that initiated the deactivation.
+ * @param[in] gline G-line to deactivate.
+ * @param[in] lastmod New value for G-line last modification timestamp.
+ * @param[in] flags GLINE_LOCAL to only deactivate locally, 0 to propagate.
+ * @return Zero.
+ */
 int
 gline_deactivate(struct Client *cptr, struct Client *sptr, struct Gline *gline,
                 time_t lastmod, unsigned int flags)
@@ -350,9 +551,10 @@ gline_deactivate(struct Client *cptr, struct Client *sptr, struct Gline *gline,
 
   if (GlineIsLocal(gline))
     msg = "removing local";
-  else if (!gline->gl_lastmod && !(flags & GLINE_LOCAL))
+  else if (!gline->gl_lastmod && !(flags & GLINE_LOCAL)) {
     msg = "removing global";
-  else {
+    gline->gl_flags &= ~GLINE_ACTIVE; /* propagate a -<mask> */
+  } else {
     msg = "deactivating global";
 
     if (flags & GLINE_LOCAL)
@@ -360,10 +562,12 @@ gline_deactivate(struct Client *cptr, struct Client *sptr, struct Gline *gline,
     else {
       gline->gl_flags &= ~GLINE_ACTIVE;
 
-      if (gline->gl_lastmod >= lastmod)
-       gline->gl_lastmod++;
-      else
-       gline->gl_lastmod = lastmod;
+      if (gline->gl_lastmod) {
+       if (gline->gl_lastmod >= lastmod)
+         gline->gl_lastmod++;
+       else
+         gline->gl_lastmod = lastmod;
+      }
     }
 
     if ((saveflags & GLINE_ACTMASK) != GLINE_ACTIVE)
@@ -373,28 +577,45 @@ gline_deactivate(struct Client *cptr, struct Client *sptr, struct Gline *gline,
   /* Inform ops and log it */
   sendto_opmask_butone(0, SNO_GLINE, "%s %s %s for %s%s%s, expiring at %Tu: "
                       "%s",
-                      IsServer(sptr) ? sptr->name : sptr->user->server->name,
+                       (feature_bool(FEAT_HIS_SNOTICES) || IsServer(sptr)) ?
+                         cli_name(sptr) :
+                         cli_name((cli_user(sptr))->server),
                       msg, GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
-                      gline->gl_user, GlineIsBadChan(gline) ? "" : "@",
-                      GlineIsBadChan(gline) ? "" : gline->gl_host,
+                      gline->gl_user, gline->gl_host ? "@" : "",
+                       gline->gl_host ? gline->gl_host : "",
                       gline->gl_expire + TSoffset, gline->gl_reason);
 
-#ifdef GPATH
-  write_log(GPATH, "# %Tu %C %s %s for %s%s%s, expiring at %Tu: %s\n",
-           TStime(), sptr, msg, GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
-           gline->gl_user, GlineIsBadChan(gline) ? "" : "@",
-           GlineIsBadChan(gline) ? "" : gline->gl_host,
+  log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
+           "%#C %s %s for %s%s%s, expiring at %Tu: %s", sptr, msg,
+           GlineIsBadChan(gline) ? "BADCHAN" : "GLINE", gline->gl_user,
+           gline->gl_host ? "@" : "",
+           gline->gl_host ? gline->gl_host : "",
            gline->gl_expire + TSoffset, gline->gl_reason);
-#endif /* GPATH */
 
+  if (!(flags & GLINE_LOCAL)) /* don't propagate local changes */
+    gline_propagate(cptr, sptr, gline);
+
+  /* if it's a local gline or a Uworld gline (and not locally deactivated).. */
   if (GlineIsLocal(gline) || (!gline->gl_lastmod && !(flags & GLINE_LOCAL)))
-    gline_free(gline);
-  else if (!(flags & GLINE_LOCAL)) /* don't propagate local changes */
-    propagate_gline(cptr, sptr, gline);
+    gline_free(gline); /* get rid of it */
 
   return 0;
 }
 
+/** Find a G-line for a particular mask, guided by certain flags.
+ * Certain bits in \a flags are interpreted specially:
+ * <dl>
+ * <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_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>
+ * </dl>
+ * @param[in] userhost Mask to search for.
+ * @param[in] flags Bitwise combination of GLINE_* flags.
+ * @return First matching G-line, or NULL if none are found.
+ */
 struct Gline *
 gline_find(char *userhost, unsigned int flags)
 {
@@ -418,16 +639,15 @@ gline_find(char *userhost, unsigned int flags)
   }
 
   if ((flags & (GLINE_BADCHAN | GLINE_ANY)) == GLINE_BADCHAN ||
-      *userhost == '#' || *userhost == '&' || *userhost == '+'
-#ifndef NO_OLD_GLINE
-      || userhost[2] == '#' || userhost[2] == '&' || userhost[2] == '+'
-#endif /* NO_OLD_GLINE */
-      )
+      *userhost == '#' || *userhost == '&')
     return 0;
 
   DupString(t_uh, userhost);
   canon_userhost(t_uh, &user, &host, 0);
 
+  if (BadPtr(user))
+    return 0;
+
   for (gline = GlobalGlineList; gline; gline = sgline) {
     sgline = gline->gl_next;
 
@@ -437,12 +657,14 @@ gline_find(char *userhost, unsigned int flags)
             (flags & GLINE_LASTMOD && !gline->gl_lastmod))
       continue;
     else if (flags & GLINE_EXACT) {
-      if (ircd_strcmp(gline->gl_host, host) == 0 &&
-         ((!user && ircd_strcmp(gline->gl_user, "*") == 0) ||
+      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))
        break;
     } else {
-      if (match(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;
@@ -454,6 +676,12 @@ gline_find(char *userhost, unsigned int flags)
   return gline;
 }
 
+/** Find a matching G-line for a user.
+ * @param[in] cptr Client to compare against.
+ * @param[in] flags Bitwise combination of GLINE_GLOBAL and/or
+ * GLINE_LASTMOD to limit matches.
+ * @return Matching G-line, or NULL if none are found.
+ */
 struct Gline *
 gline_lookup(struct Client *cptr, unsigned int flags)
 {
@@ -469,22 +697,36 @@ gline_lookup(struct Client *cptr, unsigned int flags)
     }
     
     if ((flags & GLINE_GLOBAL && gline->gl_flags & GLINE_LOCAL) ||
-            (flags & GLINE_LASTMOD && !gline->gl_lastmod))
+        (flags & GLINE_LASTMOD && !gline->gl_lastmod))
       continue;
-     
-    if (match(gline->gl_user, cptr->user->username) != 0)
-      continue;
-        
-    if (GlineIsIpMask(gline)) {
-      Debug((DEBUG_DEBUG,"IP gline: %08x %08x/%i",cptr->ip.s_addr,gline->ipnum.s_addr,gline->bits));
-      if ((cptr->ip.s_addr & NETMASK(gline->bits)) != gline->ipnum.s_addr)
+
+    if (GlineIsRealName(gline)) {
+      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_host, cptr->user->host) != 0) 
+      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;
+      }
+      else {
+        if (match(gline->gl_host, (cli_user(cptr))->realhost) != 0)
+          continue;
+      }
     }
-    return gline;
+    if (GlineIsActive(gline))
+      return gline;
   }
   /*
    * No Glines matched
@@ -492,6 +734,9 @@ gline_lookup(struct Client *cptr, unsigned int flags)
   return 0;
 }
 
+/** Delink and free a G-line.
+ * @param[in] gline G-line to free.
+ */
 void
 gline_free(struct Gline *gline)
 {
@@ -508,6 +753,9 @@ gline_free(struct Gline *gline)
   MyFree(gline);
 }
 
+/** Burst all known global G-lines to another server.
+ * @param[in] cptr Destination of burst.
+ */
 void
 gline_burst(struct Client *cptr)
 {
@@ -520,10 +768,12 @@ gline_burst(struct Client *cptr)
     if (gline->gl_expire <= CurrentTime) /* expire any that need expiring */
       gline_free(gline);
     else if (!GlineIsLocal(gline) && gline->gl_lastmod)
-      sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s@%s %Tu %Tu :%s",
+      sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s%s%s %Tu %Tu :%s",
                    GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
-                   gline->gl_host, gline->gl_expire - CurrentTime,
-                   gline->gl_lastmod, gline->gl_reason);
+                    gline->gl_host ? "@" : "",
+                    gline->gl_host ? gline->gl_host : "",
+                   gline->gl_expire - CurrentTime, gline->gl_lastmod,
+                    gline->gl_reason);
   }
 
   for (gline = BadChanGlineList; gline; gline = sgline) { /* all glines */
@@ -539,6 +789,11 @@ gline_burst(struct Client *cptr)
   }
 }
 
+/** Send a G-line to another server.
+ * @param[in] cptr Who to inform of the G-line.
+ * @param[in] gline G-line to send.
+ * @return Zero.
+ */
 int
 gline_resend(struct Client *cptr, struct Gline *gline)
 {
@@ -547,14 +802,21 @@ gline_resend(struct Client *cptr, struct Gline *gline)
 
   sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s%s%s %Tu %Tu :%s",
                GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
-               GlineIsBadChan(gline) ? "" : "@",
-               GlineIsBadChan(gline) ? "" : gline->gl_host,
+               gline->gl_host ? "@" : "",
+                gline->gl_host ? gline->gl_host : "",
                gline->gl_expire - CurrentTime, gline->gl_lastmod,
                gline->gl_reason);
 
   return 0;
 }
 
+/** Display one or all G-lines to a user.
+ * If \a userhost is not NULL, only send the first matching G-line.
+ * Otherwise send the whole list.
+ * @param[in] sptr User asking for G-line list.
+ * @param[in] userhost G-line mask to search for (or NULL).
+ * @return Zero.
+ */
 int
 gline_list(struct Client *sptr, char *userhost)
 {
@@ -567,10 +829,10 @@ gline_list(struct Client *sptr, char *userhost)
 
     /* send gline information along */
     send_reply(sptr, RPL_GLIST, gline->gl_user,
-              GlineIsBadChan(gline) ? "" : "@",
-              GlineIsBadChan(gline) ? "" : gline->gl_host,
+               gline->gl_host ? "@" : "",
+               gline->gl_host ? gline->gl_host : "",
               gline->gl_expire + TSoffset,
-              GlineIsLocal(gline) ? me.name : "*",
+              GlineIsLocal(gline) ? cli_name(&me) : "*",
               GlineIsActive(gline) ? '+' : '-', gline->gl_reason);
   } else {
     for (gline = GlobalGlineList; gline; gline = sgline) {
@@ -579,9 +841,11 @@ gline_list(struct Client *sptr, char *userhost)
       if (gline->gl_expire <= CurrentTime)
        gline_free(gline);
       else
-       send_reply(sptr, RPL_GLIST, gline->gl_user, "@", gline->gl_host,
+       send_reply(sptr, RPL_GLIST, gline->gl_user,
+                   gline->gl_host ? "@" : "",
+                   gline->gl_host ? gline->gl_host : "",
                   gline->gl_expire + TSoffset,
-                  GlineIsLocal(gline) ? me.name : "*",
+                  GlineIsLocal(gline) ? cli_name(&me) : "*",
                   GlineIsActive(gline) ? '+' : '-', gline->gl_reason);
     }
 
@@ -593,7 +857,7 @@ gline_list(struct Client *sptr, char *userhost)
       else
        send_reply(sptr, RPL_GLIST, gline->gl_user, "", "",
                   gline->gl_expire + TSoffset,
-                  GlineIsLocal(gline) ? me.name : "*",
+                  GlineIsLocal(gline) ? cli_name(&me) : "*",
                   GlineIsActive(gline) ? '+' : '-', gline->gl_reason);
     }
   }
@@ -602,8 +866,14 @@ gline_list(struct Client *sptr, char *userhost)
   return send_reply(sptr, RPL_ENDOFGLIST);
 }
 
+/** Statistics callback to list G-lines.
+ * @param[in] sptr Client requesting statistics.
+ * @param[in] sd Stats descriptor for request (ignored).
+ * @param[in] param Extra parameter from user (ignored).
+ */
 void
-gline_stats(struct Client *sptr)
+gline_stats(struct Client *sptr, const struct StatDesc *sd,
+            char *param)
 {
   struct Gline *gline;
   struct Gline *sgline;
@@ -614,7 +884,30 @@ gline_stats(struct Client *sptr)
     if (gline->gl_expire <= CurrentTime)
       gline_free(gline);
     else
-      send_reply(sptr, RPL_STATSGLINE, 'G', gline->gl_user, gline->gl_host,
+      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);
   }
 }
+
+/** Calculate memory used by G-lines.
+ * @param[out] gl_size Number of bytes used by G-lines.
+ * @return Number of G-lines in use.
+ */
+int
+gline_memory_count(size_t *gl_size)
+{
+  struct Gline *gline;
+  unsigned int gl = 0;
+
+  for (gline = GlobalGlineList; gline; gline = gline->gl_next)
+  {
+    gl++;
+    *gl_size += sizeof(struct Gline);
+    *gl_size += gline->gl_user ? (strlen(gline->gl_user) + 1) : 0;
+    *gl_size += gline->gl_host ? (strlen(gline->gl_host) + 1) : 0;
+    *gl_size += gline->gl_reason ? (strlen(gline->gl_reason) + 1) : 0;
+  }
+  return gl;
+}