#include <stdio.h>
+#include <string.h>
#include "../control/control.h"
#include "../lib/irc_string.h"
+#include "../lib/strlfunc.h"
#include "trusts.h"
-int trusts_migration_start(TrustDBMigrationCallback, void *);
-void trusts_migration_stop(void);
static void registercommands(int, void *);
static void deregistercommands(int, void *);
-static void migrate_status(int errcode, void *tag) {
- long sender = (long)tag;
- nick *np = getnickbynumeric(sender);
+void calculatespaces(int spaces, int width, char *str, char **_prebuf, char **_postbuf) {
+ static char prebuf[512], postbuf[512];
+ int spacelen;
- if(!np)
- return;
+ if(spaces + 5 >= sizeof(prebuf)) {
+ prebuf[0] = prebuf[1] = '\0';
+ } else {
+ memset(prebuf, ' ', spaces);
+ prebuf[spaces] = '\0';
+ }
- if(!errcode || errcode == MIGRATION_LASTERROR) {
- if(!errcode) {
- controlreply(np, "Migration complete.");
- controlreply(np, "Attempting to reload database. . .");
- } else {
- controlreply(np, "An error occured after the database was unloaded, attempting reload. . .");
- }
- if(trusts_loaddb()) {
- controlreply(np, "Database reloaded successfully.");
- } else {
- controlreply(np, "An error occured, please reload the module manually.");
- }
+ spacelen = width - (strlen(str) + spaces);
+ if(spacelen <= 0 || spacelen + 5 >= sizeof(postbuf)) {
+ postbuf[0] = postbuf[1] = '\0';
} else {
- controlreply(np, "Error %d occured during migration, commands reregistered.", errcode);
- registercommands(0, NULL);
+ memset(postbuf, ' ', spacelen);
+ postbuf[spacelen] = '\0';
}
+
+ *_prebuf = prebuf;
+ *_postbuf = postbuf;
}
-static int trusts_cmdmigrate(void *source, int cargc, char **cargv) {
- nick *sender = source;
- int ret;
+static void traverseandmark(unsigned int marker, trusthost *th) {
+ th->marker = marker;
+
+ for(th=th->children;th;th=th->nextbychild) {
+ th->marker = marker;
+ traverseandmark(marker, th);
+ }
+}
+
+static void marktree(array *parents, unsigned int marker, trusthost *th) {
+ trusthost *pth;
+
+ for(pth=th->parent;pth;pth=pth->next) {
+ trusthost **p2 = (trusthost **)(parents->content);
+ int i;
+
+ /* this eliminates common subtrees */
+ for(i=0;i<parents->cursi;i++)
+ if(p2[i] == pth)
+ break;
+
+ if(i == parents->cursi) {
+ int pos = array_getfreeslot(parents);
+ ((trusthost **)(parents->content))[pos] = pth;
+ }
+
+ pth->marker = marker;
+ }
+
+ /* sadly we need to recurse down */
+ traverseandmark(marker, th);
+}
+
+static void outputtree(nick *np, unsigned int marker, trustgroup *originalgroup, trusthost *th, int depth) {
+ char *cidrstr, *prespacebuf, *postspacebuf, parentbuf[512];
- /* iffy but temporary */
- ret = trusts_migration_start(migrate_status, (void *)(sender->numeric));
- if(!ret) {
- controlreply(sender, "Migration started, commands deregistered.");
- deregistercommands(0, NULL);
+ if(th->marker != marker)
+ return;
+
+ cidrstr = trusts_cidr2str(th->ip, th->mask);
+ calculatespaces(depth + 1, 20 + 1, cidrstr, &prespacebuf, &postspacebuf);
+
+ if(th->group == originalgroup) {
+ prespacebuf[0] = '>';
+
+ parentbuf[0] = '\0';
} else {
- controlreply(sender, "Error %d starting migration.", ret);
+ /* show the ids of other groups */
+
+ snprintf(parentbuf, sizeof(parentbuf), "%-10d %s", th->group->id, th->group->name->content);
}
- return CMD_OK;
+ controlreply(np, "%s%s%s %-10d %-10d %-20s%s", prespacebuf, cidrstr, postspacebuf, th->count, th->maxusage, (th->count>0)?"(now)":((th->lastseen>0)?trusts_timetostr(th->lastseen):"(never)"), parentbuf);
+
+ for(th=th->children;th;th=th->nextbychild)
+ outputtree(np, marker, originalgroup, th, depth + 1);
}
static int trusts_cmdtrustlist(void *source, int cargc, char **cargv) {
nick *sender = source;
trustgroup *tg;
- trusthost *th;
+ trusthost *th, **p2;
time_t t;
+ unsigned int marker;
+ array parents;
+ int i;
if(cargc < 1)
return CMD_USAGE;
controlreply(sender, "Max usage : %d", tg->maxusage);
controlreply(sender, "Last max reset : %s", tg->lastmaxuserreset?trusts_timetostr(tg->lastmaxuserreset):"(never)");
- controlreply(sender, "Host Current Max Last seen");
+ controlreply(sender, "Host Current Max Last seen Group ID Group name");
+
+ marker = nextthmarker();
+ array_init(&parents, sizeof(trusthost *));
for(th=tg->hosts;th;th=th->next)
- controlreply(sender, " %-20s %-10d %-10d %s", trusts_cidr2str(th->ip, th->mask), th->count, th->maxusage, (th->count>0)?"(now)":((th->lastseen>0)?trusts_timetostr(th->lastseen):"(never)"));
+ marktree(&parents, marker, th);
+
+ p2 = (trusthost **)(parents.content);
+ for(i=0;i<parents.cursi;i++)
+ outputtree(sender, marker, tg, p2[i], 0);
+
+ array_free(&parents);
controlreply(sender, "End of list.");
nick *sender = source;
char *host;
uint32_t ip, mask;
- trusthost *th;
+ trusthost *th, *superset, *subset;
if(cargc < 2)
return CMD_USAGE;
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) {
return;
commandsregistered = 1;
- registercontrolhelpcmd("trustmigrate", NO_DEVELOPER, 0, trusts_cmdmigrate, "Usage: trustmigrate\nCopies trust data from O and reloads the database.");
registercontrolhelpcmd("trustlist", NO_OPER, 1, trusts_cmdtrustlist, "Usage: trustlist <#id|name|id>\nShows trust data for the specified trust group.");
registercontrolhelpcmd("trustgroupadd", NO_OPER, 6, trusts_cmdtrustgroupadd, "Usage: trustgroupadd <name> <howmany> <howlong> <maxperident> <enforceident> <contact> ?comment?");
registercontrolhelpcmd("trustadd", NO_OPER, 2, trusts_cmdtrustadd, "Usage: trustadd <#id|name|id> <host>");
return;
commandsregistered = 0;
- deregistercontrolcmd("trustmigrate", trusts_cmdmigrate);
deregistercontrolcmd("trustlist", trusts_cmdtrustlist);
deregistercontrolcmd("trustgroupadd", trusts_cmdtrustgroupadd);
deregistercontrolcmd("trustadd", trusts_cmdtrustadd);
deregisterhook(HOOK_TRUSTS_DB_LOADED, registercommands);
deregisterhook(HOOK_TRUSTS_DB_CLOSED, deregistercommands);
- trusts_migration_stop();
-
deregistercommands(0, NULL);
}