#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"
static void registercommands(int, void *);
static void deregistercommands(int, void *);
-static int trusts_cmdtrustlist(void *source, int cargc, char **cargv) {
- nick *sender = source;
- trustgroup *tg;
- trusthost *th;
- time_t t;
+void calculatespaces(int spaces, int width, char *str, char **_prebuf, char **_postbuf) {
+ static char prebuf[512], postbuf[512];
+ int spacelen;
- if(cargc < 1)
- return CMD_USAGE;
+ if(spaces + 5 >= sizeof(prebuf)) {
+ prebuf[0] = prebuf[1] = '\0';
+ } else {
+ memset(prebuf, ' ', spaces);
+ prebuf[spaces] = '\0';
+ }
- tg = tg_strtotg(cargv[0]);
- if(!tg) {
- controlreply(sender, "Couldn't find a trustgroup with that id.");
- return CMD_ERROR;
+ spacelen = width - (strlen(str) + spaces);
+ if(spacelen <= 0 || spacelen + 5 >= sizeof(postbuf)) {
+ postbuf[0] = postbuf[1] = '\0';
+ } else {
+ memset(postbuf, ' ', spacelen);
+ postbuf[spacelen] = '\0';
+ }
+
+ *_prebuf = prebuf;
+ *_postbuf = postbuf;
+}
+
+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 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 void marktree(array *parents, unsigned int marker, trusthost *th) {
+ trusthost *pth;
+ int parentcount = 0;
+
+ for(pth=th->parent;pth;pth=pth->next) {
+ insertth(parents, pth);
+
+ pth->marker = marker;
}
- t = time(NULL);
+ 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);
+ }
+
+ 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, "ID: : %u", tg->id);
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 Group ID Group name");
- controlreply(sender, "Host Current Max Last seen");
+ 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);
- controlreply(sender, "End of list.");
+ p2 = (trusthost **)(parents.content);
+ for(i=0;i<parents.cursi;i++)
+ outputtree(sender, marker, tg, p2[i], 0);
- return CMD_OK;
+ array_free(&parents);
+
+ controlreply(sender, "End of list.");
}
-static int trusts_cmdtrustadd(void *source, int cargc, char **cargv) {
- trustgroup *tg;
+static int trusts_cmdtrustlist(void *source, int cargc, char **cargv) {
nick *sender = source;
- char *host;
- uint32_t ip, mask;
- trusthost *th, *superset, *subset;
+ trustgroup *tg = NULL;
+ int found = 0, remaining = 50;
+ char *name;
- if(cargc < 2)
+ if(cargc < 1)
return CMD_USAGE;
- tg = tg_strtotg(cargv[0]);
- if(!tg) {
- controlreply(sender, "Couldn't look up trustgroup.");
- return CMD_ERROR;
- }
-
- host = cargv[1];
- if(!trusts_str2cidr(host, &ip, &mask)) {
- controlreply(sender, "Invalid host.");
- return CMD_ERROR;
- }
+ name = cargv[0];
- /* 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;
- }
- }
+ tg = tg_strtotg(name);
- if(th_getbyhostandmask(ip, mask)) {
- controlreply(sender, "This host already exists in another group with the same mask.");
- return CMD_ERROR;
+ if(tg) {
+ displaygroup(sender, tg);
+ return CMD_OK;
}
- /* 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 */
+ for(tg=tglist;tg;tg=tg->next) {
+ if(match(name, tg->name->content))
+ continue;
- controlreply(sender, "Warning: this host already exists in another group, but this new host will override it as it has a smaller prefix.");
+ displaygroup(sender, tg);
+ if(--remaining == 0) {
+ controlreply(sender, "Maximum number of matches reached.");
+ return CMD_OK;
+ }
+ found = 1;
}
- 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...");
+ if(!found)
+ controlreply(sender, "No matches found.");
- th = th_new(tg, host);
- if(!th) {
- controlreply(sender, "An error occured adding the host to the group.");
- return CMD_ERROR;
- }
+ return CMD_OK;
+}
- controlreply(sender, "Host added.");
- /* TODO: controlwall */
+static int comparetgs(const void *_a, const void *_b) {
+ const trustgroup *a = _a;
+ const trustgroup *b = _b;
- return CMD_OK;
+ if(a->id > b->id)
+ return 1;
+ if(a->id < b-> id)
+ return -1;
+ return 0;
}
-static int trusts_cmdtrustgroupadd(void *source, int cargc, char **cargv) {
- nick *sender = source;
- char *name, *contact, *comment, createdby[ACCOUNTLEN + 2];
- unsigned int howmany, maxperident, enforceident;
- time_t howlong;
- trustgroup *tg;
+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(cargc < 6)
+ if((argc < 2) || (argv[0][0] != '#'))
return CMD_USAGE;
- name = cargv[0];
- howmany = strtoul(cargv[1], NULL, 10);
- if(!howmany || (howmany > 50000)) {
- controlreply(sender, "Bad value maximum number of clients.");
- return CMD_ERROR;
- }
+ wanted = atoi(&argv[0][1]);
+ max = atoi(argv[1]);
- howlong = durationtolong(cargv[2]);
- if((howlong <= 0) || (howlong > 365 * 86400 * 20)) {
- controlreply(sender, "Invalid duration supplied.");
- return CMD_ERROR;
+ for(maxid=totalcount=0,tg=tglist;tg;tg=tg->next) {
+ if(totalcount == 0 || tg->id > maxid)
+ maxid = tg->id;
+
+ totalcount++;
}
- maxperident = strtoul(cargv[3], NULL, 10);
- if(!howmany || (maxperident > 1000)) {
- controlreply(sender, "Bad value for max per ident.");
- return CMD_ERROR;
+ if(maxid > totalcount) {
+ controlreply(np, "Start ID cannot exceed current maximum group ID (#%u)", maxid);
+ return CMD_OK;
}
- if(cargv[4][0] != '1' && cargv[4][0] != '0') {
- controlreply(sender, "Bad value for enforce ident (use 0 or 1).");
+ atg = nsmalloc(POOL_TRUSTS, sizeof(trusthost *) * totalcount);
+ if(!atg) {
+ controlreply(np, "Memory error.");
return CMD_ERROR;
}
- enforceident = cargv[4][0] == '1';
- contact = cargv[5];
+ for(i=0,tg=tglist;i<totalcount&&th;tg=tg->next,i++)
+ atg[i] = tg;
- if(cargc < 7) {
- comment = "(no comment)";
- } else {
- comment = cargv[6];
- }
+ qsort(atg, totalcount, sizeof(trustgroup *), comparetgs);
- /* don't allow #id or id forms */
- if((name[0] == '#') || strtoul(name, NULL, 10)) {
- controlreply(sender, "Invalid trustgroup name.");
- return CMD_ERROR;
- }
+ for(i=0;i<totalcount;i++)
+ if(atg[i]->id >= wanted)
+ break;
- tg = tg_strtotg(name);
- if(tg) {
- controlreply(sender, "A group with that name already exists");
- return CMD_ERROR;
- }
+ for(groupcount=linecount=0;i<totalcount;i++) {
+ linecount++;
+ groupcount++;
- snprintf(createdby, sizeof(createdby), "#%s", sender->authname);
+ controlreply(np, "G,%s", dumptg(atg[i], 1));
- tg = tg_new(name, howmany, enforceident, maxperident, howlong + time(NULL), createdby, contact, comment);
- if(!tg) {
- controlreply(sender, "An error occured adding the trustgroup.");
- return CMD_ERROR;
- }
+ for(th=atg[i]->hosts;th;th=th->next) {
+ linecount++;
+ controlreply(np, "H,%s", dumpth(th, 1));
+ }
- controlreply(sender, "Group added.");
- /* TODO: controlwall */
+ if(--max == 0)
+ break;
+ }
+ nsfree(POOL_TRUSTS, atg);
+ controlreply(np, "End of list, %u groups and %u lines returned.", groupcount, linecount);
return CMD_OK;
}
commandsregistered = 1;
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>");
+ registercontrolhelpcmd("trustdump", NO_OPER, 2, trusts_cmdtrustdump, "Usage: trustdump <#id> <number>");
}
static void deregistercommands(int hooknum, void *arg) {
commandsregistered = 0;
deregistercontrolcmd("trustlist", trusts_cmdtrustlist);
- deregistercontrolcmd("trustgroupadd", trusts_cmdtrustgroupadd);
- deregistercontrolcmd("trustadd", trusts_cmdtrustadd);
+ deregistercontrolcmd("trustdump", trusts_cmdtrustdump);
}
void _init(void) {