]> jfr.im git - irc/quakenet/newserv.git/blobdiff - trusts/data.c
GLINES: fix null pointer deref in trustgline / trustungline
[irc/quakenet/newserv.git] / trusts / data.c
index 23aaaa76a91138b13f013665641954888873eab8..3e8427fb82799a75fe0f937c996e7a8afe485633 100644 (file)
@@ -1,11 +1,13 @@
 #include <stdlib.h>
 #include <string.h>
+#include <strings.h>
 #include <stdio.h>
 
 #include "../lib/sstring.h"
 #include "../core/hooks.h"
 #include "../core/nsmalloc.h"
 #include "../lib/irc_string.h"
+#include "../irc/irc.h"
 #include "trusts.h"
 
 trustgroup *tglist;
@@ -27,7 +29,7 @@ void trusts_freeall(void) {
       th_free(th);
     }
 
-    tg_free(tg);
+    tg_free(tg, 1);
   }
 
   tglist = NULL;
@@ -44,6 +46,8 @@ trustgroup *tg_getbyid(unsigned int id) {
 }
 
 void th_free(trusthost *th) {
+  triggerhook(HOOK_TRUSTS_LOSTHOST, th);
+
   nsfree(POOL_TRUSTS, th);
 }
 
@@ -69,7 +73,7 @@ void th_linktree(void) {
   /* ugh */
   for(tg=tglist;tg;tg=tg->next)
     for(th=tg->hosts;th;th=th->next)
-      th->parent = th_getsmallestsupersetbyhost(th->ip, th->mask);
+      th->parent = th_getsmallestsupersetbyhost(&th->ip, th->bits);
 
   for(tg=tglist;tg;tg=tg->next)
     for(th=tg->hosts;th;th=th->next)
@@ -100,8 +104,9 @@ trusthost *th_add(trusthost *ith) {
   return th;
 }
 
-void tg_free(trustgroup *tg) {
-  triggerhook(HOOK_TRUSTS_LOSTGROUP, tg);
+void tg_free(trustgroup *tg, int created) {
+  if(created)
+    triggerhook(HOOK_TRUSTS_LOSTGROUP, tg);
 
   freesstring(tg->name);
   freesstring(tg->createdby);
@@ -118,11 +123,11 @@ trustgroup *tg_add(trustgroup *itg) {
   memcpy(tg, itg, sizeof(trustgroup));
 
   tg->name = getsstring(tg->name->content, TRUSTNAMELEN);
-  tg->createdby = getsstring(tg->createdby->content, NICKLEN);
+  tg->createdby = getsstring(tg->createdby->content, CREATEDBYLEN);
   tg->contact = getsstring(tg->contact->content, CONTACTLEN);
   tg->comment = getsstring(tg->comment->content, COMMENTLEN);
   if(!tg->name || !tg->createdby || !tg->contact || !tg->comment) {
-    tg_free(tg);
+    tg_free(tg, 0);
     return NULL;
   }
 
@@ -140,16 +145,16 @@ trustgroup *tg_add(trustgroup *itg) {
   return tg;
 }
 
-trusthost *th_getbyhost(uint32_t ip) {
+trusthost *th_getbyhost(struct irc_in_addr *ip) {
   trustgroup *tg;
   trusthost *th, *result = NULL;
-  uint32_t mask;
+  uint32_t bits;
 
   for(tg=tglist;tg;tg=tg->next) {
     for(th=tg->hosts;th;th=th->next) {
-      if((ip & th->mask) == th->ip) {
-        if(!result || (th->mask > mask)) {
-          mask = th->mask;
+      if(ipmask_check(ip, &th->ip, th->bits)) {
+        if(!result || (th->bits > bits)) {
+          bits = th->bits;
           result = th;
         }
       }
@@ -159,29 +164,29 @@ trusthost *th_getbyhost(uint32_t ip) {
   return result;
 }
 
-trusthost *th_getbyhostandmask(uint32_t ip, uint32_t mask) {
+trusthost *th_getbyhostandmask(struct irc_in_addr *ip, uint32_t bits) {
   trustgroup *tg;
   trusthost *th;
 
   for(tg=tglist;tg;tg=tg->next)
     for(th=tg->hosts;th;th=th->next)
-      if((th->ip == ip) && (th->mask == mask))
+      if(ipmask_check(ip, &th->ip, 128) && th->bits == bits)
         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) {
+trusthost *th_getsmallestsupersetbyhost(struct irc_in_addr *ip, uint32_t bits) {
   trustgroup *tg;
   trusthost *th, *result = NULL;
-  uint32_t smask;
+  uint32_t sbits;
 
   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;
+      if(ipmask_check(ip, &th->ip, th->bits)) {
+        if((th->bits < bits) && (!result || (th->bits > sbits))) {
+          sbits = th->bits;
           result = th;
         }
       }
@@ -192,14 +197,14 @@ trusthost *th_getsmallestsupersetbyhost(uint32_t ip, uint32_t mask) {
 }
 
 /* returns the first ip that is a subset it comes across */
-trusthost *th_getsubsetbyhost(uint32_t ip, uint32_t mask) {
+trusthost *th_getsubsetbyhost(struct irc_in_addr *ip, uint32_t bits) {
   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)
+      if(ipmask_check(ip, &th->ip, th->bits))
+        if(th->bits > bits)
           return th;
 
   return NULL;
@@ -210,7 +215,6 @@ static trusthost *th_getnextchildbyhost(trusthost *orig, trusthost *th) {
   if(!th) {
     trustgroup *tg;
 
-    tg = tglist;
     for(tg=tglist;tg;tg=tg->next) {
       th = tg->hosts;
       if(th)
@@ -229,9 +233,16 @@ static trusthost *th_getnextchildbyhost(trusthost *orig, trusthost *th) {
     if(th->next) {
       th = th->next;
     } else {
-      if(!th->group->next)
+      trustgroup *tg = th->group;
+
+      do {
+        tg = tg->next;
+      } while (tg && !tg->hosts);
+
+      if(!tg)
         return NULL;
-      th = th->group->next->hosts;
+
+      th = tg->hosts;
     }
 
     if(th->parent == orig)
@@ -239,15 +250,15 @@ static trusthost *th_getnextchildbyhost(trusthost *orig, trusthost *th) {
   }
 }
 
-void th_getsuperandsubsets(uint32_t ip, uint32_t mask, trusthost **superset, trusthost **subset) {
-  *superset = th_getsmallestsupersetbyhost(ip, mask);
-  *subset = th_getsubsetbyhost(ip, mask);
+void th_getsuperandsubsets(struct irc_in_addr *ip, uint32_t bits, trusthost **superset, trusthost **subset) {
+  *superset = th_getsmallestsupersetbyhost(ip, bits);
+  *subset = th_getsubsetbyhost(ip, bits);
 }
 
 void trusts_flush(void (*thflush)(trusthost *), void (*tgflush)(trustgroup *)) {
   trustgroup *tg;
   trusthost *th;
-  time_t t = time(NULL);
+  time_t t = getnettime();
 
   for(tg=tglist;tg;tg=tg->next) {
     if(tg->count > 0)
@@ -270,32 +281,24 @@ trustgroup *tg_strtotg(char *name) {
 
   /* legacy format */
   if(name[0] == '#') {
-    id = strtoul(&name[1], NULL, 10);
-    if(!id)
+    char *endp;
+    id = strtoul(&name[1], &endp, 10);
+    if(!id || *endp)
       return NULL;
 
-    for(tg=tglist;tg;tg=tg->next)
-      if(tg->id == id)
-        return tg;
+    return tg_getbyid(id);
   }
 
   for(tg=tglist;tg;tg=tg->next)
-    if(!match(name, tg->name->content))
-      return tg;
-
-  id = strtoul(name, NULL, 10);
-  if(!id)
-    return NULL;
-
-  /* legacy format */
-  for(tg=tglist;tg;tg=tg->next)
-    if(tg->id == id)
+    if(!strcasecmp(name, tg->name->content))
       return tg;
 
   return NULL;
 }
 
 void th_adjusthosts(trusthost *th, trusthost *superset, trusthost *subset) {
+  struct irc_in_addr ipaddress_canonical;
+
   /*
    * 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.
@@ -361,7 +364,8 @@ void th_adjusthosts(trusthost *th, trusthost *superset, trusthost *subset) {
     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) {
+      ip_canonicalize_tunnel(&ipaddress_canonical, &np->ipaddress);
+      if(ipmask_check(&ipaddress_canonical, &th->ip, th->bits)) {
         trusts_lostnick(np, 1);
         trusts_newnick(np, 1);
       }
@@ -372,10 +376,13 @@ void th_adjusthosts(trusthost *th, trusthost *superset, trusthost *subset) {
     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))
+    for(i=0;i<NICKHASHSIZE;i++) {
+      for(np=nicktable[i];np;np=np->next) {
+        ip_canonicalize_tunnel(&ipaddress_canonical, &np->ipaddress);
+        if(!gettrusthost(np) && ipmask_check(&ipaddress_canonical, &th->ip, th->bits))
           trusts_newnick(np, 1);
+      }
+    }
   }
 }
 
@@ -413,13 +420,55 @@ unsigned int nextthmarker(void) {
   return thmarker;
 }
 
-trustgroup *tg_inttotg(unsigned int id) {
+trusthost *th_getbyid(unsigned int id) {
   trustgroup *tg;
+  trusthost *th;
 
   for(tg=tglist;tg;tg=tg->next)
-    if(tg->id == id)
-      return tg;
+    for(th=tg->hosts;th;th=th->next)
+      if(th->id == id)
+        return th;
 
   return NULL;
 }
 
+int tg_modify(trustgroup *oldtg, trustgroup *newtg) {
+  trustgroup vnewtg;
+
+  memcpy(&vnewtg, oldtg, sizeof(trustgroup));
+
+  /* unfortunately we can't just memcpy the new one over */
+
+  vnewtg.name = getsstring(newtg->name->content, TRUSTNAMELEN);
+  vnewtg.createdby = getsstring(newtg->createdby->content, CREATEDBYLEN);
+  vnewtg.contact = getsstring(newtg->contact->content, CONTACTLEN);
+  vnewtg.comment = getsstring(newtg->comment->content, COMMENTLEN);
+  if(!vnewtg.name || !vnewtg.createdby || !vnewtg.contact || !vnewtg.comment) {
+    freesstring(vnewtg.name);
+    freesstring(vnewtg.createdby);
+    freesstring(vnewtg.contact);
+    freesstring(vnewtg.comment);
+    return 0;
+  }
+
+  /* id remains the same, count/hosts/marker/next/exts are ignored */
+  vnewtg.trustedfor = newtg->trustedfor;
+  vnewtg.flags = newtg->flags;
+  vnewtg.maxperident = newtg->maxperident;
+  vnewtg.maxusage = newtg->maxusage;
+  vnewtg.expires = newtg->expires;
+  vnewtg.lastseen = newtg->lastseen;
+  vnewtg.lastmaxusereset = newtg->lastmaxusereset;
+
+  memcpy(oldtg, &vnewtg, sizeof(trustgroup));
+
+  return 1;
+}
+
+int th_modify(trusthost *oldth, trusthost *newth) {
+  oldth->maxpernode = newth->maxpernode;
+  oldth->nodebits = newth->nodebits;
+
+  return 1;
+}
+