+#include <stdio.h>
+#include <string.h>
#include "../control/control.h"
#include "../lib/irc_string.h"
+#include "../lib/strlfunc.h"
+#include "../core/nsmalloc.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;
- /* iffy but temporary */
- ret = trusts_migration_start(migrate_status, (void *)(sender->numeric));
- if(!ret) {
- controlreply(sender, "Migration started, commands deregistered.");
- deregistercommands(0, NULL);
- } else {
- controlreply(sender, "Error %d starting migration.", ret);
+ for(th=th->children;th;th=th->nextbychild) {
+ th->marker = marker;
+ traverseandmark(marker, th);
}
+}
- return CMD_OK;
+static void insertth(array *parents, trusthost *th) {
+ int i;
+ trusthost **p2 = (trusthost **)(parents->content);
+
+ /* this eliminates common subtrees */
+ for(i=0;i<parents->cursi;i++)
+ if(p2[i] == th)
+ break;
+
+ if(i == parents->cursi) {
+ int pos = array_getfreeslot(parents);
+ ((trusthost **)(parents->content))[pos] = th;
+ }
}
-static int trusts_cmdtrustlist(void *source, int cargc, char **cargv) {
- nick *sender = source;
- trustgroup *tg;
- trusthost *th;
- time_t t;
+static void marktree(array *parents, unsigned int marker, trusthost *th) {
+ trusthost *pth;
+ int parentcount = 0;
- if(cargc < 1)
- return CMD_USAGE;
+ for(pth=th->parent;pth;pth=pth->next) {
+ insertth(parents, pth);
- tg = tg_strtotg(cargv[0]);
- if(!tg) {
- controlreply(sender, "Couldn't find a trustgroup with that id.");
- return CMD_ERROR;
+ pth->marker = marker;
+ }
+
+ if(parentcount == 0)
+ insertth(parents, th);
+
+ /* 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];
+
+ 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 {
+ /* show the ids of other groups */
+
+ snprintf(parentbuf, sizeof(parentbuf), "%-10d %s", th->group->id, th->group->name->content);
}
- t = time(NULL);
+ controlreply(np, "%s%s%s %-10d %-10d %-21s%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 void displaygroup(nick *sender, trustgroup *tg) {
+ trusthost *th, **p2;
+ unsigned int marker;
+ array parents;
+ int i;
+ time_t t = time(NULL);
+
+ /* abusing the ternary operator a bit :( */
controlreply(sender, "Name: : %s", tg->name->content);
controlreply(sender, "Trusted for : %d", tg->trustedfor);
controlreply(sender, "Currently using : %d", tg->count);
controlreply(sender, "Clients per user : %d (%senforcing ident)", tg->maxperident, tg->mode?"":"not ");
controlreply(sender, "Contact: : %s", tg->contact->content);
- controlreply(sender, "Expires in : %s", (tg->expires>t)?longtoduration(tg->expires - t, 2):"(in the past)");
+ controlreply(sender, "Expires in : %s", (tg->expires>t)?longtoduration(tg->expires - t, 2):"(the past -- BUG)");
controlreply(sender, "Last changed by : %s", tg->createdby->content);
controlreply(sender, "Comment: : %s", tg->comment->content);
controlreply(sender, "ID: : %u", tg->id);
- controlreply(sender, "Last used : %s", (tg->count>0)?"(now)":trusts_timetostr(tg->lastseen));
+ controlreply(sender, "Last used : %s", (tg->count>0)?"(now)":((tg->lastseen>0)?trusts_timetostr(tg->lastseen):"(never)"));
controlreply(sender, "Max usage : %d", tg->maxusage);
- controlreply(sender, "Last max reset : %s", tg->lastmaxuserreset?trusts_timetostr(tg->lastmaxuserreset):"(never)");
+ controlreply(sender, "Last max reset : %s", tg->lastmaxusereset?trusts_timetostr(tg->lastmaxusereset):"(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)":trusts_timetostr(th->lastseen));
+ 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.");
+}
+
+static int trusts_cmdtrustlist(void *source, int cargc, char **cargv) {
+ nick *sender = source;
+ trustgroup *tg = NULL;
+ int found = 0, remaining = 50;
+ char *name;
+
+ if(cargc < 1)
+ return CMD_USAGE;
+
+ name = cargv[0];
+
+ tg = tg_strtotg(name);
+
+ if(tg) {
+ displaygroup(sender, tg);
+ return CMD_OK;
+ }
+
+ for(tg=tglist;tg;tg=tg->next) {
+ if(match(name, tg->name->content))
+ continue;
+
+ displaygroup(sender, tg);
+ if(--remaining == 0) {
+ controlreply(sender, "Maximum number of matches reached.");
+ return CMD_OK;
+ }
+ found = 1;
+ }
+
+ if(!found)
+ controlreply(sender, "No matches found.");
return CMD_OK;
}
+static int comparetgs(const void *_a, const void *_b) {
+ const trustgroup *a = _a;
+ const trustgroup *b = _b;
+
+ if(a->id > b->id)
+ return 1;
+ if(a->id < b-> id)
+ return -1;
+ return 0;
+}
+
+static int trusts_cmdtrustdump(void *source, int argc, char **argv) {
+ trusthost *th;
+ trustgroup *tg, **atg;
+ unsigned int wanted, max, maxid, totalcount, i, groupcount, linecount;
+ nick *np = source;
+
+ if((argc < 2) || (argv[0][0] != '#'))
+ return CMD_USAGE;
+
+ wanted = atoi(&argv[0][1]);
+ max = atoi(argv[1]);
+
+ for(maxid=totalcount=0,tg=tglist;tg;tg=tg->next) {
+ if(totalcount == 0 || tg->id > maxid)
+ maxid = tg->id;
+
+ totalcount++;
+ }
+
+ if(maxid > totalcount) {
+ controlreply(np, "Start ID cannot exceed current maximum group ID (#%u)", maxid);
+ return CMD_OK;
+ }
+
+ atg = nsmalloc(POOL_TRUSTS, sizeof(trusthost *) * totalcount);
+ if(!atg) {
+ controlreply(np, "Memory error.");
+ return CMD_ERROR;
+ }
+
+ for(i=0,tg=tglist;i<totalcount&&tg;tg=tg->next,i++)
+ atg[i] = tg;
+
+ qsort(atg, totalcount, sizeof(trustgroup *), comparetgs);
+
+ for(i=0;i<totalcount;i++)
+ if(atg[i]->id >= wanted)
+ break;
+
+ for(groupcount=linecount=0;i<totalcount;i++) {
+ linecount++;
+ groupcount++;
+
+ controlreply(np, "G,%s", dumptg(atg[i], 1));
+
+ for(th=atg[i]->hosts;th;th=th->next) {
+ linecount++;
+ controlreply(np, "H,%s", dumpth(th, 1));
+ }
+
+ if(--max == 0)
+ break;
+ }
+ nsfree(POOL_TRUSTS, atg);
+
+ controlreply(np, "End of list, %u groups and %u lines returned.", groupcount, linecount);
+ return CMD_OK;
+}
+
static int commandsregistered;
static void registercommands(int hooknum, void *arg) {
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("trustdump", NO_OPER, 2, trusts_cmdtrustdump, "Usage: trustdump <#id> <number>");
}
static void deregistercommands(int hooknum, void *arg) {
return;
commandsregistered = 0;
- deregistercontrolcmd("trustmigrate", trusts_cmdmigrate);
deregistercontrolcmd("trustlist", trusts_cmdtrustlist);
+ deregistercontrolcmd("trustdump", trusts_cmdtrustdump);
}
void _init(void) {
deregisterhook(HOOK_TRUSTS_DB_LOADED, registercommands);
deregisterhook(HOOK_TRUSTS_DB_CLOSED, deregistercommands);
- trusts_migration_stop();
-
deregistercommands(0, NULL);
}