]> jfr.im git - irc/quakenet/newserv.git/commitdiff
Add subset/set checking into trustadd, now handles moving of users between trusts...
authorChris Porter <redacted>
Fri, 3 Oct 2008 05:21:49 +0000 (06:21 +0100)
committerChris Porter <redacted>
Fri, 3 Oct 2008 05:21:49 +0000 (06:21 +0100)
Counts don't currently contain those of subsetted trustgroups, or display them.
Also need more extensive testing.
TRUSTS_NEWNICK/LOSTNICK have a move parameter now, for when they shouldn't enforce things like glines.

core/hooks.h
trusts/data.c
trusts/db.c
trusts/events.c
trusts/trusts.h
trusts/trusts_commands.c
trusts/trusts_policy.c

index 85025c5c886e4efdcb81feebd77b7866cb0b0aa8..ab6021d99fcc033c5fb6c6e53f1dbeb4e35bed96 100644 (file)
@@ -77,8 +77,8 @@
 
 #define HOOK_TRUSTS_DB_CLOSED      900 /* No arg */
 #define HOOK_TRUSTS_DB_LOADED      901 /* No arg */
-#define HOOK_TRUSTS_NEWNICK        902 /* Argument is nick* */
-#define HOOK_TRUSTS_LOSTNICK       903 /* Argument is nick* */
+#define HOOK_TRUSTS_NEWNICK        902 /* Argument is void*[2] (nick*, long) */
+#define HOOK_TRUSTS_LOSTNICK       903 /* Argument is void*[2] (nick*, long) */
 #define HOOK_TRUSTS_NEWGROUP       904 /* Argument is trustgroup* */
 #define HOOK_TRUSTS_LOSTGROUP      905 /* Argument is trustgroup* */
 
index 75cd6e6855605506a1c38f306a57c062213eb05e..c0784394e96fcd78284c01de09bd84e3625d0395 100644 (file)
@@ -1,5 +1,6 @@
 #include <stdlib.h>
 #include <string.h>
+#include <stdio.h>
 
 #include "../lib/sstring.h"
 #include "../core/hooks.h"
@@ -136,19 +137,57 @@ trusthost *th_getbyhost(uint32_t ip) {
   return result;
 }
 
-/* should this return the largest, smallest or any match? currently the latter */
-trusthost *th_getsupersetbyhost(uint32_t ip, uint32_t mask) {
+trusthost *th_getbyhostandmask(uint32_t ip, uint32_t mask) {
   trustgroup *tg;
   trusthost *th;
 
   for(tg=tglist;tg;tg=tg->next)
     for(th=tg->hosts;th;th=th->next)
-      if((th->ip & mask) == ip)
+      if((th->ip == ip) && (th->mask == mask))
         return th;
 
   return NULL;
 }
 
+/* returns the ip with the smallest prefix that is still a superset of the given host */
+trusthost *th_getsmallestsupersetbyhost(uint32_t ip, uint32_t mask) {
+  trustgroup *tg;
+  trusthost *th, *result = NULL;
+  uint32_t smask;
+
+  for(tg=tglist;tg;tg=tg->next) {
+    for(th=tg->hosts;th;th=th->next) {
+      if(th->ip == (ip & th->mask)) {
+        if((th->mask < mask) && (!result || (th->mask > smask))) {
+          smask = th->mask;
+          result = th;
+        }
+      }
+    }
+  }
+
+  return result;
+}
+
+/* returns the first ip that is a subset it comes across */
+trusthost *th_getsubsetbyhost(uint32_t ip, uint32_t mask) {
+  trustgroup *tg;
+  trusthost *th;
+
+  for(tg=tglist;tg;tg=tg->next)
+    for(th=tg->hosts;th;th=th->next)
+      if((th->ip & mask) == ip)
+        if(th->mask > mask)
+          return th;
+
+  return NULL;
+}
+
+void th_getsuperandsubsets(uint32_t ip, uint32_t mask, trusthost **superset, trusthost **subset) {
+  *superset = th_getsmallestsupersetbyhost(ip, mask);
+  *subset = th_getsubsetbyhost(ip, mask);
+}
+
 void trusts_flush(void) {
   trustgroup *tg;
   trusthost *th;
@@ -199,3 +238,87 @@ trustgroup *tg_strtotg(char *name) {
 
   return NULL;
 }
+
+void th_adjusthosts(trusthost *th, trusthost *superset, trusthost *subset) {
+  /*
+   * First and foremost, CIDR doesn't allow hosts to cross boundaries, i.e. everything with a smaller prefix
+   * is entirely contained with the prefix that is one smaller.
+   * e.g. 0.0.0.0/23, 0.0.0.128/23, you can't have a single prefix for 0.0.0.64-0.0.0.192, instead
+   * you have two, 0.0.0.64/26 and 0.0.0.128/26.
+   *
+   * This makes the code MUCH easier as the entire thing is one huge set/tree.
+   *
+   * Four cases here:
+   * 1: host isn't covered by any existing hosts.
+   * 2: host is covered by a less specific one only, e.g. adding 0.0.0.1/32, while 0.0.0.0/24 already exists.
+   * 3: host is covered by a more specific one only, e.g. adding 0.0.0.0/24 while 0.0.0.1/32 already exists
+   *    (note there might be more than one more specific host, e.g. 0.0.0.1/32 and 0.0.0.2/32).
+   * 4: covered by more and less specific cases, e.g. adding 0.0.0.0/24 to: { 0.0.0.1/32, 0.0.0.2/32, 0.0.0.0/16 }.
+   *
+   * CASE 1
+   * ------
+   *
+   * !superset && !subset
+   *
+   * Scan through the host hash and add any clients which match our host, this is exactly the same as case 3
+   * but without needing to check (though checking doesn't hurt), so we'll just use the code for that.
+   *
+   * CASE 2
+   * ------
+   *
+   * superset && !subset
+   *
+   * We have the less specific host in 'superset', we know it is the only one so pull out clients in it's
+   * ->users list matching our new host.
+   * No need to look for extra hosts in the main nick hash as they're all covered already.
+   *
+   * CASE 3
+   * ------
+   *
+   * !superset && subset
+   *
+   * We have one host in 'subset', but there might be more than one, we don't care though!
+   * We can scan the entire host hash and pull out any hosts that match us and don't have
+   * a trust group already, this ignores any with a more specific prefix.
+   *
+   * CASE 4
+   * ------
+   *
+   * superset && subset
+   *
+   * Here we first fix up the ones less specific then us, so we just perform what we did for case 2,
+   * then we perform what we did for case 3.
+   *
+   * So in summary:
+   *   CASE 1: DO 3
+   *   CASE 2: (work)
+   *   CASE 3: (work)
+   *   CASE 4: DO 2; DO 3
+   * Or:
+   *   if(2 || 4)     : DO 2
+   *   if(1 || 3 || 4): DO 3
+   */
+
+  /* we let the compiler do the boolean minimisation for clarity reasons */
+
+  if((superset && !subset) || (superset && subset)) { /* cases 2 and 4 */
+    nick *np, *nnp;
+    for(np=superset->users;np;np=nnp) {
+      nnp = nextbytrust(np);
+      if((irc_in_addr_v4_to_int(&np->p_ipaddr) & th->mask) == th->ip) {
+        trusts_lostnick(np, 1);
+        trusts_newnick(np, 1);
+      }
+    }
+  }
+
+  if((!superset && !subset) || (!superset && subset) || (superset && subset)) { /* cases 1, 3 and 4 */
+    nick *np;
+    int i;
+
+    for(i=0;i<NICKHASHSIZE;i++)
+      for(np=nicktable[i];np;np=np->next)
+        if(!gettrusthost(np) && ((irc_in_addr_v4_to_int(&np->p_ipaddr) & th->mask) == th->ip))
+          trusts_newnick(np, 1);
+  }
+}
index 69d71697e3eb4ead25663b1fff0a80f76bd2549a..a740a616e4d2f8528f1d288a44a813575bc447e3 100644 (file)
@@ -228,7 +228,16 @@ void tg_dbupdatecounts(trustgroup *tg) {
 }
 
 trusthost *th_new(trustgroup *tg, char *host) {
-  trusthost *th = th_add(tg, thmaxid + 1, host, 0, 0);
+  trusthost *th, *superset, *subset;
+  u_int32_t ip, mask;
+
+  /* ugh */
+  if(!trusts_str2cidr(host, &ip, &mask))
+    return NULL;
+
+  th_getsuperandsubsets(ip, mask, &superset, &subset);
+
+  th = th_add(tg, thmaxid + 1, host, 0, 0);
   if(!th)
     return NULL;
 
@@ -239,7 +248,7 @@ trusthost *th_new(trustgroup *tg, char *host) {
     "Tuusut", "hosts", th->id, tg->id, trusts_cidr2str(th->ip, th->mask), th->maxusage, th->lastseen
   );
 
-  /* fix up stuff in memory */
+  th_adjusthosts(th, subset, superset);
 
   return th;
 }
index d14c885df10f14a2fd79ca456f739b222ef9d44c..b16d55706228c86f445d9a172433f262aa82f4a5 100644 (file)
@@ -3,10 +3,10 @@
 
 static void __counthandler(int hooknum, void *arg);
 
-static void __newnick(int hooknum, void *arg) {
-  nick *sender = arg;
+void trusts_newnick(nick *sender, int moving) {
   uint32_t host;
   trusthost *th;
+  void *arg[2];
 
   host = irc_in_addr_v4_to_int(&sender->p_ipaddr);
   th = th_getbyhost(host);
@@ -22,18 +22,29 @@ static void __newnick(int hooknum, void *arg) {
 
   /* sucks we have to do this, at least until we get priority hooks */
   __counthandler(HOOK_TRUSTS_NEWNICK, sender);
-  triggerhook(HOOK_TRUSTS_NEWNICK, sender);
+
+  arg[0] = sender;
+  arg[1] = (void *)(long)moving;
+  triggerhook(HOOK_TRUSTS_NEWNICK, arg);
 }
 
-static void __lostnick(int hooknum, void *arg) {
-  nick *sender = arg, *np, *lp;
+static void __newnick(int hooknum, void *arg) {
+  trusts_newnick(arg, 0);
+}
+
+void trusts_lostnick(nick *sender, int moving) {
+  nick *np, *lp;
   trusthost *th = gettrusthost(sender);
+  void *arg[2];
 
   if(!th)
     return;
 
   __counthandler(HOOK_TRUSTS_LOSTNICK, sender);
-  triggerhook(HOOK_TRUSTS_LOSTNICK, sender);
+
+  arg[0] = sender;
+  arg[1] = (void *)(long)moving;
+  triggerhook(HOOK_TRUSTS_LOSTNICK, arg);
 
   /*
    * we need to erase this nick from the trusthost list
@@ -54,6 +65,10 @@ static void __lostnick(int hooknum, void *arg) {
   }
 }
 
+static void __lostnick(int hooknum, void *arg) {
+  trusts_lostnick(arg, 0);
+}
+
 static void __counthandler(int hooknum, void *arg) {
   time_t t = time(NULL);
   trusthost *th = gettrusthost((nick *)arg);
index 9fd4cd81e8848f065e8df9a0039546271c7a7876..c596b080e058e42c9fce4d81956a8adc458be946 100644 (file)
@@ -87,8 +87,12 @@ trusthost *th_add(trustgroup *, unsigned int, char *, unsigned int, time_t);
 void tg_free(trustgroup *);
 trustgroup *tg_add(unsigned int, char *, unsigned int, int, unsigned int, unsigned int, time_t, time_t, time_t, char *, char *, char *);
 trusthost *th_getbyhost(uint32_t);
-trusthost *th_getsupersetbyhost(uint32_t, uint32_t);
-trustgroup *tg_strtotg(char *name);
+trusthost *th_getbyhostandmask(uint32_t, uint32_t);
+trusthost *th_getsmallestsupersetbyhost(uint32_t, uint32_t);
+trustgroup *tg_strtotg(char *);
+void th_adjusthosts(trusthost *th, trusthost *, trusthost *);
+void th_getsuperandsubsets(uint32_t, uint32_t, trusthost **, trusthost **);
+trusthost *th_getsubsetbyhost(uint32_t, uint32_t);
 
 /* migration.c */
 typedef void (*TrustMigrationGroup)(void *, unsigned int, char *, unsigned int, unsigned int, unsigned int, unsigned int, time_t, time_t, time_t, char *, char *, char *);
@@ -108,4 +112,8 @@ typedef struct trustmigration {
 /* db-migration.c */
 typedef void (*TrustDBMigrationCallback)(int, void *);
 
+/* events.c */
+void trusts_newnick(nick *, int);
+void trusts_lostnick(nick *, int);
+
 #endif
index e71bdf16f1ee6031e962158ffc2e1d8dd93bb9a3..d403f94cfaaba46ac4058c0a5ac6875fa58f4991 100644 (file)
@@ -95,7 +95,7 @@ static int trusts_cmdtrustadd(void *source, int cargc, char **cargv) {
   nick *sender = source;
   char *host;
   uint32_t ip, mask;
-  trusthost *th;
+  trusthost *th, *superset, *subset;
 
   if(cargc < 2)
     return CMD_USAGE;
@@ -112,25 +112,41 @@ static int trusts_cmdtrustadd(void *source, int cargc, char **cargv) {
     return CMD_ERROR;
   }
 
-  th = th_getbyhost(ip);
-  if(th) {
-    if(mask == th->mask) {
-      controlreply(sender, "This host already exists with the same mask.");
+  /* OKAY! Lots of checking here!
+   *
+   * Need to check:
+   *   - host isn't already covered by given group (reject if it is)
+   *   - host doesn't already exist exactly already (reject if it does)
+   *   - host is more specific than an existing one (warn if it is, fix up later)
+   *   - host is less specific than an existing one (warn if it is, don't need to do anything special)
+   */
+
+  for(th=tg->hosts;th;th=th->next) {
+    if(th->ip == (ip & th->mask)) {
+      controlreply(sender, "This host (or part of it) is already covered in the given group.");
       return CMD_ERROR;
     }
-    if(mask > th->mask) {
-      /* this mask is a closer fit */
+  }
 
-      controlreply(sender, "Warning: this host will override another (%s), as it has smaller prefix (group: %s).", trusts_cidr2str(th->ip, th->mask), th->group->name->content);
-      controlreply(sender, "Adding anyway...");
-    }
-  } else {
-    th = th_getsupersetbyhost(ip, mask);
-    if(th) {
-      controlreply(sender, "Warning: this host is already covered by a smaller prefix (%s), which will remain part of that group: %s", trusts_cidr2str(th->ip, th->mask), th->group->name->content);
-      controlreply(sender, "Adding anyway...");
-    }
+  if(th_getbyhostandmask(ip, mask)) {
+    controlreply(sender, "This host already exists in another group with the same mask.");
+    return CMD_ERROR;
+  }
+
+  /* this function will set both to NULL if it's equal, hence the check above */
+  th_getsuperandsubsets(ip, mask, &superset, &subset);
+  if(superset) {
+    /* a superset exists for us, we will be more specific than one existing host */
+
+    controlreply(sender, "Warning: this host already exists in another group, but this new host will override it as it has a smaller prefix.");
+  }
+  if(subset) {
+    /* a subset of us exists, we will be less specific than some existing hosts */
+
+    controlreply(sender, "Warning: this host already exists in at least one other group, the new host has a larger prefix and therefore will not override those hosts.");
   }
+  if(superset || subset)
+    controlreply(sender, "Adding anyway...");
 
   th = th_new(tg, host);
   if(!th) {
index 607fa97fd3bb46c93cb664da8bb0a8e53033e146..737101a657cc43b3f7e15b30b0970c44ad971c09 100644 (file)
@@ -5,10 +5,15 @@
 static int countext;
 
 static void policycheck(int hooknum, void *arg) {
-  nick *np = arg;
+  void **args = arg;
+  nick *np = args[0];
+  long moving = (long)args[1];
   trusthost *th = gettrusthost(np);
   trustgroup *tg = th->group;
 
+  if(moving)
+    return;
+
   /*
    * the purpose of this logic is to avoid spam like this:
    * WARNING: tgX exceeded limit: 11 connected vs 10 max