]> jfr.im git - irc/quakenet/newserv.git/blobdiff - trusts/trusts_db.c
Make log table work with PostgreqSQL.
[irc/quakenet/newserv.git] / trusts / trusts_db.c
index 1ad9f17e98b2a2fd907c9fd0aed93b06f828a674..ed457f18d374d8c94f7fda10c240f4c74d6b80a5 100644 (file)
@@ -1,12 +1,16 @@
 #include <stdio.h>
 #include <stdarg.h>
+#include "../lib/version.h"
 #include "../dbapi2/dbapi2.h"
 #include "../core/error.h"
 #include "../core/hooks.h"
 #include "../core/schedule.h"
 #include "../control/control.h"
+#include "../irc/irc.h"
 #include "trusts.h"
 
+MODULE_VERSION("");
+
 DBAPIConn *trustsdb;
 static int tgmaxid, thmaxid;
 static int loaderror;
@@ -33,15 +37,15 @@ void createtrusttables(int mode) {
   }
 
   trustsdb->createtable(trustsdb, NULL, NULL,
-    "CREATE TABLE ? (id INT PRIMARY KEY, name VARCHAR(?), trustedfor INT, mode INT, maxperident INT, maxusage INT, expires INT, lastseen INT, lastmaxusereset INT, createdby VARCHAR(?), contact VARCHAR(?), comment VARCHAR(?))",
+    "CREATE TABLE ? (id INT PRIMARY KEY, name VARCHAR(?), trustedfor INT, flags INT, maxperident INT, maxusage INT, expires INT, lastseen INT, lastmaxusereset INT, createdby VARCHAR(?), contact VARCHAR(?), comment VARCHAR(?))",
     "Tdddd", groups, TRUSTNAMELEN, CREATEDBYLEN, CONTACTLEN, COMMENTLEN
   );
 
   /* I'd like multiple keys here but that's not gonna happen on a cross-database platform :( */
-  trustsdb->createtable(trustsdb, NULL, NULL, "CREATE TABLE ? (id INT PRIMARY KEY, groupid INT, host VARCHAR(?), maxusage INT, created INT, lastseen INT)", "Td", hosts, TRUSTHOSTLEN);
+  trustsdb->createtable(trustsdb, NULL, NULL, "CREATE TABLE ? (id INT PRIMARY KEY, groupid INT, host VARCHAR(?), maxusage INT, created INT, lastseen INT, maxpernode INT, nodebits INT)", "Td", hosts, TRUSTHOSTLEN);
 
   trustsdb->createtable(trustsdb, NULL, NULL,
-    "CREATE TABLE ? (id INT PRIMARY KEY, groupid INT, groupname VARCHAR(?), ts INT, username VARCHAR(?), message VARCHAR(?))",
+    "CREATE TABLE ? (groupid INT, groupname VARCHAR(?), ts INT, username VARCHAR(?), message VARCHAR(?))",
     "Tddd", "log", TRUSTNAMELEN, CREATEDBYLEN, TRUSTLOGLEN
   );
 }
@@ -80,7 +84,7 @@ static void loadhosts_data(const DBAPIResult *result, void *tag) {
     return;
   }
 
-  if(result->fields != 6) {
+  if(result->fields != 8) {
     Error("trusts", ERR_ERROR, "Wrong number of fields in hosts table.");
     loaderror = 1;
 
@@ -106,7 +110,7 @@ static void loadhosts_data(const DBAPIResult *result, void *tag) {
     }
 
     host = result->get(result, 2);
-    if(!trusts_str2cidr(host, &th.ip, &th.mask)) {
+    if(!ipmask_parse(host, &th.ip, &th.bits)) {
       Error("trusts", ERR_WARNING, "Error parsing cidr for host: %s", host);
       continue;
     }
@@ -114,6 +118,8 @@ static void loadhosts_data(const DBAPIResult *result, void *tag) {
     th.maxusage = strtoul(result->get(result, 3), NULL, 10);
     th.created = (time_t)strtoul(result->get(result, 4), NULL, 10);
     th.lastseen = (time_t)strtoul(result->get(result, 5), NULL, 10);
+    th.maxpernode = strtol(result->get(result, 6), NULL, 10);
+    th.nodebits = strtol(result->get(result, 7), NULL, 10);
 
     if(!th_add(&th))
       Error("trusts", ERR_WARNING, "Error adding host to trust %d: %s", groupid, host);
@@ -159,7 +165,7 @@ static void loadgroups_data(const DBAPIResult *result, void *tag) {
 
     tg.name = getsstring(rtrim(result->get(result, 1)), TRUSTNAMELEN);
     tg.trustedfor = strtoul(result->get(result, 2), NULL, 10);
-    tg.mode = atoi(result->get(result, 3));
+    tg.flags = atoi(result->get(result, 3));
     tg.maxperident = strtoul(result->get(result, 4), NULL, 10);
     tg.maxusage = strtoul(result->get(result, 5), NULL, 10);
     tg.expires = (time_t)strtoul(result->get(result, 6), NULL, 10);
@@ -256,7 +262,7 @@ trusthost *th_copy(trusthost *ith) {
 
   trustsdb_insertth("hosts", th, th->group->id);
 
-  th_getsuperandsubsets(ith->ip, ith->mask, &superset, &subset);
+  th_getsuperandsubsets(&ith->ip, ith->bits, &superset, &subset);
   th_adjusthosts(th, superset, subset);
   th_linktree();
 
@@ -266,15 +272,18 @@ trusthost *th_copy(trusthost *ith) {
 trusthost *th_new(trustgroup *tg, char *host) {
   trusthost *th, nth;
 
-  if(!trusts_str2cidr(host, &nth.ip, &nth.mask))
+  if(!ipmask_parse(host, &nth.ip, &nth.bits))
     return NULL;
 
   nth.group = tg;
   nth.id = thmaxid + 1;
-  nth.created = time(NULL);
+  nth.created = getnettime();
   nth.lastseen = 0;
   nth.maxusage = 0;
 
+  nth.maxpernode = 0;
+  nth.nodebits = (irc_in_addr_is_ipv4(&nth.ip))?128:64;
+
   th = th_copy(&nth);
   if(!th)
     return NULL;
@@ -312,22 +321,22 @@ trustgroup *tg_new(trustgroup *itg) {
 
 void trustsdb_insertth(char *table, trusthost *th, unsigned int groupid) {
   trustsdb->squery(trustsdb,
-    "INSERT INTO ? (id, groupid, host, maxusage, created, lastseen) VALUES (?, ?, ?, ?, ?, ?)",
-    "Tuusuut", table, th->id, groupid, trusts_cidr2str(th->ip, th->mask), th->maxusage, th->created, th->lastseen
+    "INSERT INTO ? (id, groupid, host, maxusage, created, lastseen, maxpernode, nodebits) VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
+    "Tuusuutuu", table, th->id, groupid, trusts_cidr2str(&th->ip, th->bits), th->maxusage, th->created, th->lastseen, th->maxpernode, th->nodebits
   );
 }
 
 void trustsdb_inserttg(char *table, trustgroup *tg) {
   trustsdb->squery(trustsdb,
-    "INSERT INTO ? (id, name, trustedfor, mode, maxperident, maxusage, expires, lastseen, lastmaxusereset, createdby, contact, comment) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
-    "Tusuuuutttsss", table, tg->id, tg->name->content, tg->trustedfor, tg->mode, tg->maxperident, tg->maxusage, tg->expires, tg->lastseen, tg->lastmaxusereset, tg->createdby->content, tg->contact->content, tg->comment->content
+    "INSERT INTO ? (id, name, trustedfor, flags, maxperident, maxusage, expires, lastseen, lastmaxusereset, createdby, contact, comment) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
+    "Tusuuuutttsss", table, tg->id, tg->name->content, tg->trustedfor, tg->flags, tg->maxperident, tg->maxusage, tg->expires, tg->lastseen, tg->lastmaxusereset, tg->createdby->content, tg->contact->content, tg->comment->content
   );
 }
 
 void tg_update(trustgroup *tg) {
   trustsdb->squery(trustsdb,
-    "UPDATE ? SET name = ?, trustedfor = ?, maxperident = ?, maxusage = ?, expires = ?, lastseen = ?, lastmaxusereset = ?, createdby = ?, contact = ?, comment = ? WHERE id = ?",
-    "Tsuuuutttsssu", "groups", tg->name->content, tg->trustedfor, tg->mode, tg->maxperident, tg->maxusage, tg->expires, tg->lastseen, tg->lastmaxusereset, tg->createdby->content, tg->contact->content, tg->comment->content, tg->id
+    "UPDATE ? SET name = ?, trustedfor = ?, flags = ?, maxperident = ?, maxusage = ?, expires = ?, lastseen = ?, lastmaxusereset = ?, createdby = ?, contact = ?, comment = ? WHERE id = ?",
+    "Tsuuuutttsssu", "groups", tg->name->content, tg->trustedfor, tg->flags, tg->maxperident, tg->maxusage, tg->expires, tg->lastseen, tg->lastmaxusereset, tg->createdby->content, tg->contact->content, tg->comment->content, tg->id
   );
 }
 
@@ -351,6 +360,13 @@ void tg_delete(trustgroup *tg) {
   tg_free(tg, 1);
 }
 
+void th_update(trusthost *th) {
+  trustsdb->squery(trustsdb,
+    "UPDATE ? SET maxpernode = ?, nodebits = ? WHERE id = ?",
+    "Tuuu", "hosts", th->maxpernode, th->nodebits, th->id
+  );
+}
+
 void trustsdb_deleteth(char *table, trusthost *th) {
   trustsdb->squery(trustsdb,
     "DELETE FROM ? WHERE id = ?",
@@ -368,11 +384,27 @@ void th_delete(trusthost *th) {
     }
   }
 
+  if(th->parent) {
+    for(pnext=&(th->parent->children);*pnext;pnext=&((*pnext)->nextbychild)) {
+      if(*pnext == th) {
+        *pnext = th->nextbychild;
+        break;
+      }
+    }
+  }
+
   for(np=th->users;np;np=nextbytrust(np))
     settrusthost(np, NULL);
 
   th->group->count -= th->count;
 
+  if(th->parent) {
+    th->parent->count += th->count;
+
+    if(th->lastseen > th->parent->lastseen)
+      th->parent->lastseen = th->lastseen;
+  }
+
   trustsdb_deleteth("hosts", th);
   th_free(th);