]> jfr.im git - irc/quakenet/newserv.git/blobdiff - trusts/data.c
Implement --help parameter.
[irc/quakenet/newserv.git] / trusts / data.c
index c0784394e96fcd78284c01de09bd84e3625d0395..f4c49a6a316894c446bf18f29ab9861b91882c9e 100644 (file)
@@ -13,6 +13,8 @@ trustgroup *tglist;
 void th_dbupdatecounts(trusthost *);
 void tg_dbupdatecounts(trustgroup *);
 
+static trusthost *th_getnextchildbyhost(trusthost *, trusthost *);
+
 void trusts_freeall(void) {
   trustgroup *tg, *ntg;
   trusthost *th, *nth;
@@ -25,7 +27,7 @@ void trusts_freeall(void) {
       th_free(th);
     }
 
-    tg_free(tg);
+    tg_free(tg, 1);
   }
 
   tglist = NULL;
@@ -42,38 +44,67 @@ trustgroup *tg_getbyid(unsigned int id) {
 }
 
 void th_free(trusthost *th) {
+  triggerhook(HOOK_TRUSTS_LOSTHOST, th);
+
   nsfree(POOL_TRUSTS, th);
 }
 
-trusthost *th_add(trustgroup *tg, unsigned int id, char *host, unsigned int maxusage, time_t lastseen) {
-  u_int32_t ip, mask;
+static void th_updatechildren(trusthost *th) {
+  trusthost *nth = NULL;
+
+  th->children = NULL;
+
+  for(;;) {
+    nth = th_getnextchildbyhost(th, nth);
+    if(!nth)
+      break;
+
+    nth->nextbychild = th->children;
+    th->children = nth;
+  }
+}
+
+void th_linktree(void) {
+  trustgroup *tg;
   trusthost *th;
 
-  if(!trusts_str2cidr(host, &ip, &mask))
-    return NULL;
+  /* ugh */
+  for(tg=tglist;tg;tg=tg->next)
+    for(th=tg->hosts;th;th=th->next)
+      th->parent = th_getsmallestsupersetbyhost(th->ip, th->mask);
+
+  for(tg=tglist;tg;tg=tg->next)
+    for(th=tg->hosts;th;th=th->next)
+      if(th->parent)
+        th_updatechildren(th->parent);
+}
+
+trusthost *th_add(trusthost *ith) {
+  trusthost *th;
 
   th = nsmalloc(POOL_TRUSTS, sizeof(trusthost));
   if(!th)
     return NULL;
 
-  th->id = id;
-  th->maxusage = maxusage;
-  th->lastseen = lastseen;
-  th->ip = ip;
-  th->mask = mask;
+  memcpy(th, ith, sizeof(trusthost));
 
   th->users = NULL;
-  th->group = tg;
   th->count = 0;
 
-  th->next = tg->hosts;
-  tg->hosts = th;
+  th->parent = NULL;
+  th->children = NULL;
+
+  th->marker = 0;
+
+  th->next = th->group->hosts;
+  th->group->hosts = th;
 
   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);
@@ -82,30 +113,24 @@ void tg_free(trustgroup *tg) {
   nsfree(POOL_TRUSTS, tg);
 }
 
-trustgroup *tg_add(unsigned int id, char *name, unsigned int trustedfor, int mode, unsigned int maxperident, unsigned int maxusage, time_t expires, time_t lastseen, time_t lastmaxuserreset, char *createdby, char *contact, char *comment) {
+trustgroup *tg_add(trustgroup *itg) {
   trustgroup *tg = nsmalloc(POOL_TRUSTS, sizeof(trustgroup));
   if(!tg)
     return NULL;
 
-  tg->name = getsstring(name, TRUSTNAMELEN);
-  tg->createdby = getsstring(createdby, NICKLEN);
-  tg->contact = getsstring(contact, CONTACTLEN);
-  tg->comment = getsstring(comment, COMMENTLEN);
+  memcpy(tg, itg, sizeof(trustgroup));
+
+  tg->name = getsstring(tg->name->content, TRUSTNAMELEN);
+  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;
   }
 
-  tg->id = id;
-  tg->trustedfor = trustedfor;
-  tg->mode = mode;
-  tg->maxperident = maxperident;
-  tg->maxusage = maxusage;
-  tg->expires = expires;
-  tg->lastseen = lastseen;
-  tg->lastmaxuserreset = lastmaxuserreset;
   tg->hosts = NULL;
-
+  tg->marker = 0;
   tg->count = 0;
 
   memset(tg->exts, 0, sizeof(tg->exts));
@@ -183,12 +208,46 @@ trusthost *th_getsubsetbyhost(uint32_t ip, uint32_t mask) {
   return NULL;
 }
 
+/* NOT reentrant obviously */
+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)
+        break;
+    }
+
+    /* INVARIANT: tg => th */
+    if(!tg)
+      return NULL;
+
+    if(th->parent == orig)
+      return th;
+  }
+
+  for(;;) {
+    if(th->next) {
+      th = th->next;
+    } else {
+      if(!th->group->next)
+        return NULL;
+      th = th->group->next->hosts;
+    }
+
+    if(th->parent == orig)
+      return 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 trusts_flush(void) {
+void trusts_flush(void (*thflush)(trusthost *), void (*tgflush)(trustgroup *)) {
   trustgroup *tg;
   trusthost *th;
   time_t t = time(NULL);
@@ -197,13 +256,13 @@ void trusts_flush(void) {
     if(tg->count > 0)
       tg->lastseen = t;
 
-    tg_dbupdatecounts(tg);
+    tgflush(tg);
 
     for(th=tg->hosts;th;th=th->next) {
       if(th->count > 0)
         th->lastseen = t;
 
-      th_dbupdatecounts(th);
+      thflush(th);
     }
   }
 }
@@ -214,26 +273,16 @@ 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(!strcmp(name, tg->name->content))
       return tg;
 
   return NULL;
@@ -322,3 +371,82 @@ void th_adjusthosts(trusthost *th, trusthost *superset, trusthost *subset) {
           trusts_newnick(np, 1);
   }
 }
+
+unsigned int nexttgmarker(void) {
+  static unsigned int tgmarker = 0;
+  trustgroup *tg;
+
+  tgmarker++;
+  if(!tgmarker) {
+    /* If we wrapped to zero, zap the marker on all groups */
+    for(tg=tglist;tg;tg=tg->next)
+      tg->marker=0;
+
+    tgmarker++;
+  }
+
+  return tgmarker;
+}
+
+unsigned int nextthmarker(void) {
+  static unsigned int thmarker = 0;
+  trustgroup *tg;
+  trusthost *th;
+
+  thmarker++;
+  if(!thmarker) {
+    /* If we wrapped to zero, zap the marker on all hosts */
+    for(tg=tglist;tg;tg=tg->next)
+      for(th=tg->hosts;th;th=th->next)
+        th->marker=0;
+
+    thmarker++;
+  }
+
+  return thmarker;
+}
+
+trusthost *th_getbyid(unsigned int id) {
+  trustgroup *tg;
+  trusthost *th;
+
+  for(tg=tglist;tg;tg=tg->next)
+    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.mode = newtg->mode;
+  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;
+}