Author: Kev <klmitch@mit.edu>
[ircu2.10.12-pk.git] / ircd / gline.c
index bdc50dfe9b0b8ae88e0f5e6115f4ba5ef6e370ef..7ed3f1cfcab2b8de4eb5cadf91e401af81d14b3d 100644 (file)
  *
  * $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_policy.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 "send.h"
 #include "struct.h"
 #include "numnicks.h"
 #include "numeric.h"
 #include "sys.h"    /* FALSE bleah */
+#include "whocmds.h"
 
 #include <assert.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 */
 
 struct Gline* GlobalGlineList  = 0;
 struct Gline* BadChanGlineList = 0;
@@ -59,14 +83,12 @@ canon_userhost(char *userhost, char **user_p, char **host_p, char *def_user)
 }
 
 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;
@@ -97,7 +119,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 */
@@ -109,8 +131,25 @@ make_gline(char *userhost, char *reason, time_t expire, time_t lastmod,
     DupString(gline->gl_user, user); /* remember them... */
     DupString(gline->gl_host, host);
 
-    if (check_if_ipmask(host)) /* mark if it's an IP mask */
+    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));
       gline->gl_flags |= GLINE_IPMASK;
+    }
 
     if (after) {
       gline->gl_next = after->gl_next;
@@ -144,51 +183,124 @@ 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 ((GlineIsIpMask(gline) ? match(gline->gl_host, acptr->sock_ip) :
-          match(gline->gl_host, acptr->sockhost)) == 0 &&
-         (!acptr->user->username ||
-          match(gline->gl_user, acptr->user->username) == 0)) {
-       /* ok, here's one that got G-lined */
-       sendto_one(acptr, ":%s %d %s :*** %s.", me.name, ERR_YOUREBANNEDCREEP,
-                  acptr->name, gline->gl_reason);
-
-       /* let the ops know about it */
-       sendto_op_mask(SNO_GLINE, "G-line active for %s",
-                      get_client_name(acptr, FALSE));
-
-       /* and get rid of him */
-       if ((tval = exit_client_msg(cptr, acptr, &me, "G-lined (%s)",
-                                   gline->gl_reason)))
-         retval = tval; /* retain killed status */
+       
+      if (cli_user(acptr)->username && 
+          match (gline->gl_user, (cli_user(acptr))->username) != 0)
+               continue;
+          
+      if (GlineIsIpMask(gline)) {
+        Debug((DEBUG_DEBUG,"IP gline: %08x %08x/%i",(cli_ip(cptr)).s_addr,gline->ipnum.s_addr,gline->bits));
+        if (((cli_ip(acptr)).s_addr & NETMASK(gline->bits)) != gline->ipnum.s_addr)
+          continue;
       }
+      else {
+        if (match(gline->gl_host, cli_sockhost(acptr)) != 0)
+          continue;
+      }
+
+      /* ok, here's one that got G-lined */
+      send_reply(acptr, SND_EXPLICIT | ERR_YOUREBANNEDCREEP, ":%s",
+          gline->gl_reason);
+
+      /* let the ops know about it */
+      sendto_opmask_butone(0, SNO_GLINE, "G-line active for %s",
+                    get_client_name(acptr, TRUE));
+
+      /* and get rid of him */
+      if ((tval = exit_client_msg(cptr, acptr, &me, "G-lined (%s)",
+          gline->gl_reason)))
+        retval = tval; /* retain killed status */
     }
   }
-
   return retval;
 }
 
-static void
-propagate_gline(struct Client *cptr, struct Client *sptr, struct Gline *gline)
+/*
+ * This routine 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.
+ */
+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 */
+      ipmask = strtoul(++mask, &mask, 10);
+
+      if (*mask || dots != 3 || ipmask > 32 || /* sanity-check to date */
+         (flags & (MASK_WILDS | MASK_IP)) != MASK_IP)
+       return CHECK_REJECTED; /* how strange... */
+
+      if (ipmask < 32) /* it's a masked address; mark wilds */
+       flags |= MASK_WILDS;
+
+      flags |= MASK_HALT; /* Halt the ipmask calculation */
+
+      break; /* get out of the loop */
+    } else if (!IsDigit(*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;
+}
+
+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(cptr, CMD_GLINE, sptr, "* %c%s%s%s %Tu %Tu :%s",
-                         GlineIsActive(gline) ? '+' : '-', gline->gl_user,
+    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_expire - CurrentTime, gline->gl_lastmod,
                          gline->gl_reason);
   else
-    sendcmdto_serv_butone(cptr, CMD_GLINE, sptr, "* %c%s%s%s %Tu :%s",
-                         GlineIsActive(gline) ? '+' : '-', gline->gl_user,
-                         GlineIsBadChan(gline) ? "" : "@",
+    sendcmdto_serv_butone(sptr, CMD_GLINE, cptr,
+                         (GlineIsRemActive(gline) ?
+                          "* +%s%s%s %Tu :%s" : "* -%s%s%s"),
+                         gline->gl_user, GlineIsBadChan(gline) ? "" : "@",
                          GlineIsBadChan(gline) ? "" : gline->gl_host,
                          gline->gl_expire - CurrentTime, gline->gl_reason);
+
+  return 0;
 }
 
 int 
@@ -196,79 +308,97 @@ 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);
 
+  /* 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 */
+      ) {
+    if ((flags & GLINE_LOCAL) && !HasPriv(sptr, PRIV_LOCAL_BADCHAN))
+      return send_reply(sptr, ERR_NOPRIVILEGES);
+
+    flags |= GLINE_BADCHAN;
+# ifndef NO_OLD_GLINE
+    if (userhost[2] == '#' || userhost[2] == '&' || userhost[2] == '+')
+      user = userhost + 2;
+    else
+# endif /* OLD_GLINE */
+      user = userhost;
+    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.
    */
   if (!(flags & GLINE_FORCE) && (expire <= 0 || expire > GLINE_MAX_EXPIRE)) {
     if (!IsServer(sptr) && MyConnect(sptr))
-      send_error_to_client(sptr, ERR_BADEXPIRE, expire);
+      send_reply(sptr, ERR_BADEXPIRE, expire);
     return 0;
   }
 
   expire += CurrentTime; /* convert from lifetime to timestamp */
 
-  /* NO_OLD_GLINE allows *@#channel to work correctly */
-#ifdef BADCHAN
-  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;
-  }
-#endif /* BADCHAN */
-
   /* Inform ops... */
-  sendto_op_mask(SNO_GLINE, "%s adding %s %s for %s, expiring at "
-                TIME_T_FMT ": %s",
-                IsServer(sptr) ? sptr->name : sptr->user->server->name,
-                flags & GLINE_LOCAL ? "local" : "global",
-                flags & GLINE_BADCHAN ? "BADCHAN" : "GLINE", userhost,
-                expire + TSoffset, reason);
-
-#ifdef GPATH
+  sendto_opmask_butone(0, SNO_GLINE, "%s adding %s %s for %s%s%s, expiring at "
+                      "%Tu: %s",
+#ifdef HEAD_IN_SAND_SNOTICES
+                      cli_name(sptr),
+#else
+                      IsServer(sptr) ? cli_name(sptr) :
+                      cli_name((cli_user(sptr))->server),
+#endif
+                      flags & GLINE_LOCAL ? "local" : "global",
+                      flags & GLINE_BADCHAN ? "BADCHAN" : "GLINE", user,
+                      flags & GLINE_BADCHAN ? "" : "@",
+                      flags & GLINE_BADCHAN ? "" : host,
+                      expire + TSoffset, reason);
+
   /* and log it */
-  if (IsServer(sptr))
-    write_log(GPATH, "# " TIME_T_FMT " %s adding %s %s for %s, expiring at "
-             TIME_T_FMT ": %s\n", TStime(), sptr->name,
-             flags & GLINE_LOCAL ? "local" : "global",
-             flags & GLINE_BADCHAN ? "BADCHAN" : "GLINE", userhost,
-             expire + TSoffset, reason);
-  else
-    write_log(GPATH, "# " TIME_T_FMT " %s!%s@%s adding %s %s for %s, "
-             "expiring at " TIME_T_FMT ": %s\n", TStime(), sptr->name,
-             sptr->user->username, sptr->user->host,
-             flags & GLINE_LOCAL ? "local" : "global",
-             flags & GLINE_BADCHAN ? "BADCHAN" : "GLINE", userhost,
-             expire + TSoffset, reason);
-#endif /* GPATH */
+  log_write(LS_GLINE, L_INFO, LOG_NOSNOTICE,
+           "%#C adding %s %s for %s, expiring at %Tu: %s", sptr,
+           flags & GLINE_LOCAL ? "local" : "global",
+           flags & GLINE_BADCHAN ? "BADCHAN" : "GLINE", userhost,
+           expire + TSoffset, reason);
 
   /* 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);
+  gline_propagate(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 */
-
   return do_gline(cptr, sptr, agline); /* knock off users if necessary */
 }
 
@@ -276,44 +406,51 @@ int
 gline_activate(struct Client *cptr, struct Client *sptr, struct Gline *gline,
               time_t lastmod, unsigned int flags)
 {
+  unsigned int saveflags = 0;
+
   assert(0 != gline);
-  assert(!GlineIsLocal(gline));
 
-  gline->gl_flags |= GLINE_ACTIVE;
+  saveflags = gline->gl_flags;
 
-  if (gline->gl_lastmod >= lastmod) /* force lastmod to increase */
-    gline->gl_lastmod++;
-  else
-    gline->gl_lastmod = lastmod;
+  if (flags & GLINE_LOCAL)
+    gline->gl_flags &= ~GLINE_LDEACT;
+  else {
+    gline->gl_flags |= GLINE_ACTIVE;
 
-  /* Inform ops and log it */
-  sendto_op_mask(SNO_GLINE, "%s activating global %s for %s%s%s, expiring at "
-                TIME_T_FMT ": %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);
+    if (gline->gl_lastmod) {
+      if (gline->gl_lastmod >= lastmod) /* force lastmod to increase */
+       gline->gl_lastmod++;
+      else
+       gline->gl_lastmod = lastmod;
+    }
+  }
 
-#ifdef GPATH
-  if (IsServer(sptr))
-    write_log(GPATH, "# " TIME_T_FMT " %s activating global %s for %s%s%s, "
-             "expiring at " TIME_T_FMT ": %s\n", TStime(), sptr->name,
-             GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
-             gline->gl_user, GlineIsBadChan(gline) ? "" : "@",
-             GlineIsBadChan(gline) ? "" : gline->gl_host,
-             gline->gl_expire + TSoffset, gline->gl_reason);
-  else
-    write_log(GPATH, "# " TIME_T_FMT " %s!%s@%s activating %s for "
-             "%s%s%s, expiring at " TIME_T_FMT ": %s\n", TStime(), sptr->name,
-             sptr->user->username, sptr->user->host,
-             GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
-             gline->gl_user, GlineIsBadChan(gline) ? "" : "@",
-             GlineIsBadChan(gline) ? "" : gline->gl_host,
-             gline->gl_expire + TSoffset, gline->gl_reason);
-#endif /* GPATH */
+  if ((saveflags & GLINE_ACTMASK) == GLINE_ACTIVE)
+    return 0; /* was active to begin with */
 
-  propagate_gline(cptr, sptr, gline);
+  /* Inform ops and log it */
+  sendto_opmask_butone(0, SNO_GLINE, "%s activating global %s for %s%s%s, "
+                      "expiring at %Tu: %s",
+#ifdef HEAD_IN_SAND_SNOTICES
+                      cli_name(sptr),
+#else
+                      IsServer(sptr) ? cli_name(sptr) :
+                      cli_name((cli_user(sptr))->server),
+#endif
+                      GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
+                      gline->gl_user, GlineIsBadChan(gline) ? "" : "@",
+                      GlineIsBadChan(gline) ? "" : 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,
+           GlineIsBadChan(gline) ? "" : "@",
+           GlineIsBadChan(gline) ? "" : gline->gl_host,
+           gline->gl_expire + TSoffset, gline->gl_reason);
+
+  if (!(flags & GLINE_LOCAL)) /* don't propagate local changes */
+    gline_propagate(cptr, sptr, gline);
 
   return GlineIsBadChan(gline) ? 0 : do_gline(cptr, sptr, gline);
 }
@@ -322,52 +459,65 @@ int
 gline_deactivate(struct Client *cptr, struct Client *sptr, struct Gline *gline,
                 time_t lastmod, unsigned int flags)
 {
+  unsigned int saveflags = 0;
+  char *msg;
+
   assert(0 != gline);
 
-  if (!GlineIsLocal(gline)) {
-    gline->gl_flags &= ~GLINE_ACTIVE;
+  saveflags = gline->gl_flags;
 
-    if (gline->gl_lastmod >= lastmod)
-      gline->gl_lastmod++;
-    else
-      gline->gl_lastmod = lastmod;
-  }
+  if (GlineIsLocal(gline))
+    msg = "removing local";
+  else if (!gline->gl_lastmod && !(flags & GLINE_LOCAL)) {
+    msg = "removing global";
+    gline->gl_flags &= ~GLINE_ACTIVE; /* propagate a -<mask> */
+  } else {
+    msg = "deactivating global";
 
-  /* Inform ops and log it */
-  sendto_op_mask(SNO_GLINE, "%s %s %s for %s%s%s, expiring at "
-                TIME_T_FMT ": %s",
-                IsServer(sptr) ? sptr->name : sptr->user->server->name,
-                GlineIsLocal(gline) ? "removing local" :
-                "deactivating global",
-                GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
-                gline->gl_user, GlineIsBadChan(gline) ? "" : "@",
-                GlineIsBadChan(gline) ? "" : gline->gl_host,
-                gline->gl_expire + TSoffset, gline->gl_reason);
+    if (flags & GLINE_LOCAL)
+      gline->gl_flags |= GLINE_LDEACT;
+    else {
+      gline->gl_flags &= ~GLINE_ACTIVE;
 
-#ifdef GPATH
-  if (IsServer(sptr))
-    write_log(GPATH, "# " TIME_T_FMT " %s %s %s for %s%s%s, "
-             "expiring at " TIME_T_FMT ": %s\n", TStime(), sptr->name,
-             GlineIsLocal(gline) ? "removing local" : "deactivating global",
-             GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
-             gline->gl_user, GlineIsBadChan(gline) ? "" : "@",
-             GlineIsBadChan(gline) ? "" : gline->gl_host,
-             gline->gl_expire + TSoffset, gline->gl_reason);
-  else
-    write_log(GPATH, "# " TIME_T_FMT " %s!%s@%s %s %s for "
-             "%s%s%s, expiring at " TIME_T_FMT ": %s\n", TStime(), sptr->name,
-             sptr->user->username, sptr->user->host,
-             GlineIsLocal(gline) ? "removing local" : "deactivating global",
-             GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
-             gline->gl_user, GlineIsBadChan(gline) ? "" : "@",
-             GlineIsBadChan(gline) ? "" : gline->gl_host,
-             gline->gl_expire + TSoffset, gline->gl_reason);
-#endif /* GPATH */
+      if (gline->gl_lastmod) {
+       if (gline->gl_lastmod >= lastmod)
+         gline->gl_lastmod++;
+       else
+         gline->gl_lastmod = lastmod;
+      }
+    }
 
-  if (GlineIsLocal(gline))
-    gline_free(gline);
-  else
-    propagate_gline(cptr, sptr, gline);
+    if ((saveflags & GLINE_ACTMASK) != GLINE_ACTIVE)
+      return 0; /* was inactive to begin with */
+  }
+
+  /* Inform ops and log it */
+  sendto_opmask_butone(0, SNO_GLINE, "%s %s %s for %s%s%s, expiring at %Tu: "
+                      "%s",
+#ifdef HEAD_IN_SAND_SNOTICES
+                      cli_name(sptr),
+#else
+                      IsServer(sptr) ? cli_name(sptr) :
+                      cli_name((cli_user(sptr))->server),
+#endif
+                      msg, GlineIsBadChan(gline) ? "BADCHAN" : "GLINE",
+                      gline->gl_user, GlineIsBadChan(gline) ? "" : "@",
+                      GlineIsBadChan(gline) ? "" : gline->gl_host,
+                      gline->gl_expire + TSoffset, gline->gl_reason);
+
+  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,
+           GlineIsBadChan(gline) ? "" : "@",
+           GlineIsBadChan(gline) ? "" : gline->gl_host,
+           gline->gl_expire + TSoffset, gline->gl_reason);
+
+  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); /* get rid of it */
 
   return 0;
 }
@@ -385,6 +535,9 @@ gline_find(char *userhost, unsigned int flags)
 
       if (gline->gl_expire <= CurrentTime)
        gline_free(gline);
+      else if ((flags & GLINE_GLOBAL && gline->gl_flags & GLINE_LOCAL) ||
+              (flags & GLINE_LASTMOD && !gline->gl_lastmod))
+       continue;
       else if ((flags & GLINE_EXACT ? ircd_strcmp(gline->gl_user, userhost) :
                match(gline->gl_user, userhost)) == 0)
        return gline;
@@ -407,6 +560,9 @@ gline_find(char *userhost, unsigned int flags)
 
     if (gline->gl_expire <= CurrentTime)
       gline_free(gline);
+    else if ((flags & GLINE_GLOBAL && gline->gl_flags & GLINE_LOCAL) ||
+            (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) ||
@@ -426,7 +582,7 @@ gline_find(char *userhost, unsigned int flags)
 }
 
 struct Gline *
-gline_lookup(struct Client *cptr)
+gline_lookup(struct Client *cptr, unsigned int flags)
 {
   struct Gline *gline;
   struct Gline *sgline;
@@ -434,15 +590,32 @@ gline_lookup(struct Client *cptr)
   for (gline = GlobalGlineList; gline; gline = sgline) {
     sgline = gline->gl_next;
 
-    if (gline->gl_expire <= CurrentTime)
+    if (gline->gl_expire <= CurrentTime) {
       gline_free(gline);
-    else if ((GlineIsIpMask(gline) ?
-             match(gline->gl_host, ircd_ntoa((const char *)&cptr->ip)) :
-             match(gline->gl_host, cptr->user->host)) == 0 &&
-            match(gline->gl_user, cptr->user->username) == 0)
-      return gline;
+      continue;
+    }
+    
+    if ((flags & GLINE_GLOBAL && gline->gl_flags & GLINE_LOCAL) ||
+            (flags & GLINE_LASTMOD && !gline->gl_lastmod))
+      continue;
+     
+    if (match(gline->gl_user, (cli_user(cptr))->username) != 0)
+      continue;
+        
+    if (GlineIsIpMask(gline)) {
+      Debug((DEBUG_DEBUG,"IP gline: %08x %08x/%i",(cli_ip(cptr)).s_addr,gline->ipnum.s_addr,gline->bits));
+      if (((cli_ip(cptr)).s_addr & NETMASK(gline->bits)) != gline->ipnum.s_addr)
+        continue;
+    }
+    else {
+      if (match(gline->gl_host, (cli_user(cptr))->host) != 0) 
+        continue;
+    }
+    return gline;
   }
-
+  /*
+   * No Glines matched
+   */
   return 0;
 }
 
@@ -474,8 +647,8 @@ 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(cptr, CMD_GLINE, &me, "* %c%s@%s %Tu %Tu :%s",
-                   GlineIsActive(gline) ? '+' : '-', gline->gl_user,
+      sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s@%s %Tu %Tu :%s",
+                   GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
                    gline->gl_host, gline->gl_expire - CurrentTime,
                    gline->gl_lastmod, gline->gl_reason);
   }
@@ -486,8 +659,8 @@ 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(cptr, CMD_GLINE, &me, "* %c%s %Tu %Tu :%s",
-                   GlineIsActive(gline) ? '+' : '-', gline->gl_user,
+      sendcmdto_one(&me, CMD_GLINE, cptr, "* %c%s %Tu %Tu :%s",
+                   GlineIsRemActive(gline) ? '+' : '-', gline->gl_user,
                    gline->gl_expire - CurrentTime, gline->gl_lastmod,
                    gline->gl_reason);
   }
@@ -499,8 +672,8 @@ gline_resend(struct Client *cptr, struct Gline *gline)
   if (GlineIsLocal(gline) || !gline->gl_lastmod)
     return 0;
 
-  sendcmdto_one(cptr, CMD_GLINE, &me, "* %c%s%s%s %Tu %Tu :%s",
-               GlineIsActive(gline) ? '+' : '-', gline->gl_user,
+  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_expire - CurrentTime, gline->gl_lastmod,
@@ -517,14 +690,14 @@ gline_list(struct Client *sptr, char *userhost)
 
   if (userhost) {
     if (!(gline = gline_find(userhost, GLINE_ANY))) /* no such gline */
-      return send_error_to_client(sptr, ERR_NOSUCHGLINE, userhost);
+      return send_reply(sptr, ERR_NOSUCHGLINE, userhost);
 
     /* send gline information along */
-    sendto_one(sptr, rpl_str(RPL_GLIST), me.name, sptr->name, gline->gl_user,
+    send_reply(sptr, RPL_GLIST, gline->gl_user,
               GlineIsBadChan(gline) ? "" : "@",
               GlineIsBadChan(gline) ? "" : 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) {
@@ -533,10 +706,9 @@ gline_list(struct Client *sptr, char *userhost)
       if (gline->gl_expire <= CurrentTime)
        gline_free(gline);
       else
-       sendto_one(sptr, rpl_str(RPL_GLIST), me.name, sptr->name,
-                  gline->gl_user, "@", gline->gl_host,
+       send_reply(sptr, RPL_GLIST, gline->gl_user, "@", gline->gl_host,
                   gline->gl_expire + TSoffset,
-                  GlineIsLocal(gline) ? me.name : "*",
+                  GlineIsLocal(gline) ? cli_name(&me) : "*",
                   GlineIsActive(gline) ? '+' : '-', gline->gl_reason);
     }
 
@@ -546,16 +718,15 @@ gline_list(struct Client *sptr, char *userhost)
       if (gline->gl_expire <= CurrentTime)
        gline_free(gline);
       else
-       sendto_one(sptr, rpl_str(RPL_GLIST), me.name, sptr->name,
-                  gline->gl_user, "", "", gline->gl_expire + TSoffset,
-                  GlineIsLocal(gline) ? me.name : "*",
+       send_reply(sptr, RPL_GLIST, gline->gl_user, "", "",
+                  gline->gl_expire + TSoffset,
+                  GlineIsLocal(gline) ? cli_name(&me) : "*",
                   GlineIsActive(gline) ? '+' : '-', gline->gl_reason);
     }
   }
 
   /* end of gline information */
-  sendto_one(sptr, rpl_str(RPL_ENDOFGLIST), me.name, sptr->name);
-  return 0;
+  return send_reply(sptr, RPL_ENDOFGLIST);
 }
 
 void
@@ -570,8 +741,7 @@ gline_stats(struct Client *sptr)
     if (gline->gl_expire <= CurrentTime)
       gline_free(gline);
     else
-      sendto_one(sptr, rpl_str(RPL_STATSGLINE), me.name, sptr->name, 'G',
-                gline->gl_user, gline->gl_host, gline->gl_expire + TSoffset,
-                gline->gl_reason);
+      send_reply(sptr, RPL_STATSGLINE, 'G', gline->gl_user, gline->gl_host,
+                gline->gl_expire + TSoffset, gline->gl_reason);
   }
 }