#include <stdio.h>
+#include <string.h>
+#include "../lib/version.h"
#include "../control/control.h"
#include "../lib/irc_string.h"
+#include "../lib/strlfunc.h"
+#include "../core/nsmalloc.h"
+#include "../irc/irc.h"
+#include "../newsearch/newsearch.h"
+#include "../glines/glines.h"
#include "trusts.h"
+#include "newsearch/trusts_newsearch.h"
+
+MODULE_VERSION("");
-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);
+extern void printnick_channels(searchCtx *, nick *, nick *);
- if(!np)
- return;
+void calculatespaces(int spaces, int width, const char *str, char **_prebuf, char **_postbuf) {
+ static char prebuf[512], postbuf[512];
+ int spacelen;
- 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.");
- }
+ if(spaces + 5 >= sizeof(prebuf)) {
+ prebuf[0] = prebuf[1] = '\0';
} else {
- controlreply(np, "Error %d occured during migration, commands reregistered.", errcode);
- registercommands(0, NULL);
+ memset(prebuf, ' ', spaces);
+ prebuf[spaces] = '\0';
+ }
+
+ 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 int trusts_cmdmigrate(void *source, int cargc, char **cargv) {
- nick *sender = source;
- int ret;
+static void traverseandmark(unsigned int marker, trusthost *th, int markchildren) {
+ th->marker = marker;
+
+ if(markchildren) {
+ for(th=th->children;th;th=th->nextbychild) {
+ th->marker = marker;
+ traverseandmark(marker, th, markchildren);
+ }
+ }
+}
+
+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, int showchildren) {
+ trusthost *pth;
+ int parentcount = 0;
+
+ for(pth=th->parent;pth;pth=pth->parent) {
+ insertth(parents, pth);
+
+ pth->marker = marker;
+ }
+
+ if(parentcount == 0)
+ insertth(parents, th);
+
+ /* sadly we need to recurse down */
+ traverseandmark(marker, th, showchildren);
+}
+
+static void outputtree(nick *np, unsigned int marker, trustgroup *originalgroup, trusthost *th, int depth, int showchildren) {
+ const char *cidrstr;
+ char *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 = CIDRtostr(th->ip, th->bits);
+ calculatespaces(depth + 2, 30 + 1, cidrstr, &prespacebuf, &postspacebuf);
+
+ if(th->group == originalgroup) {
+ if(!showchildren && th->group == originalgroup && th->children)
+ prespacebuf[0] = '*';
+ else
+ prespacebuf[0] = ' ';
+
+ prespacebuf[1] = '>';
+
+ 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 %-21s %-15d /%-14d%s", prespacebuf, cidrstr, postspacebuf, th->count, th->maxusage, (th->count>0)?"(now)":((th->lastseen>0)?trusts_timetostr(th->lastseen):"(never)"), th->maxpernode, (irc_in_addr_is_ipv4(&th->ip))?(th->nodebits - 96):th->nodebits, parentbuf);
+
+ /* Make sure we're not seeing this subtree again. */
+ th->marker = -1;
+
+ for(th=th->children;th;th=th->nextbychild)
+ outputtree(np, marker, originalgroup, th, depth + 1, showchildren);
}
-static int trusts_cmdtrustlist(void *source, int cargc, char **cargv) {
- nick *sender = source;
- trustgroup *tg;
- trusthost *th;
- time_t t;
+static char *formatflags(int flags) {
+ static char buf[512];
- if(cargc < 1)
- return CMD_USAGE;
+ buf[0] = '\0';
- tg = tg_strtotg(cargv[0]);
- if(!tg) {
- controlreply(sender, "Couldn't find a trustgroup with that id.");
- return CMD_ERROR;
+ if(flags & TRUST_ENFORCE_IDENT)
+ strncat(buf, "enforcing ident", 512);
+
+ if(flags & TRUST_NO_CLEANUP) {
+ if(buf[0])
+ strncat(buf, ", ", 512);
+
+ strncat(buf, "exempt from cleanup", 512);
+ }
+
+ if(flags & TRUST_PROTECTED) {
+ if(buf[0])
+ strncat(buf, ", ", 512);
+
+ strncat(buf, "protected", 512);
}
- t = time(NULL);
+ if(flags & TRUST_RELIABLE_USERNAME) {
+ if(buf[0])
+ strncat(buf, ", ", 512);
+
+ strncat(buf, "reliable username", 512);
+ }
+
+ buf[512-1] = '\0';
+
+ return buf;
+}
+
+static char *formatlimit(unsigned int limit) {
+ static char buf[64];
+
+ if(limit)
+ snprintf(buf, sizeof(buf), "%u", limit);
+ else
+ strncpy(buf, "unlimited", sizeof(buf));
+
+ return buf;
+}
+
+static void displaygroup(nick *sender, trustgroup *tg, int showchildren) {
+ trusthost *th, **p2;
+ unsigned int marker;
+ array parents;
+ int i;
+ time_t t = getnettime();
/* abusing the ternary operator a bit :( */
controlreply(sender, "Name: : %s", tg->name->content);
- controlreply(sender, "Trusted for : %d", tg->trustedfor);
+ controlreply(sender, "Trusted for : %s", formatlimit(tg->trustedfor));
controlreply(sender, "Currently using : %d", tg->count);
- controlreply(sender, "Clients per user : %d (%senforcing ident)", tg->maxperident, tg->mode?"":"not ");
+ controlreply(sender, "Clients per user : %s", formatlimit(tg->maxperident));
+ controlreply(sender, "Flags : %s", formatflags(tg->flags));
controlreply(sender, "Contact: : %s", tg->contact->content);
- 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, "Expires in : %s", (tg->expires)?((tg->expires>t)?longtoduration(tg->expires - t, 2):"the past (will be removed during next cleanup)"):"never");
+ controlreply(sender, "Created 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)":((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, "---");
+ controlreply(sender, "Attributes: * (has hidden children, show with -v), > (belongs to this trust group)");
+ controlreply(sender, "Host Current Max Last seen Max per Node Node Mask 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, showchildren);
- controlreply(sender, "End of list.");
+ p2 = (trusthost **)(parents.content);
+ for(i=0;i<parents.cursi;i++)
+ outputtree(sender, marker, tg, p2[i], 0, showchildren);
- 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;
+ trusthost *th;
+ struct irc_in_addr ip;
+ unsigned char bits;
+ int showchildren;
- 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;
- }
+ if(strcmp(cargv[0], "-v") == 0) {
+ if(cargc < 2)
+ return CMD_USAGE;
- host = cargv[1];
- if(!trusts_str2cidr(host, &ip, &mask)) {
- controlreply(sender, "Invalid host.");
- return CMD_ERROR;
+ showchildren = 1;
+ name = cargv[1];
+ } else {
+ showchildren = 0;
+ 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, showchildren);
+ 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 */
+ if(ipmask_parse(name, &ip, &bits)) {
+ th = th_getbyhost(&ip);
- 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 */
+ if(!th) {
+ controlreply(sender, "Specified IP address is not trusted.");
+ return CMD_OK;
+ }
- 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.");
+ displaygroup(sender, th->group, showchildren);
+ return CMD_OK;
}
- if(superset || subset)
- controlreply(sender, "Adding anyway...");
- th = th_new(tg, host);
- if(!th) {
- controlreply(sender, "An error occured adding the host to the group.");
- return CMD_ERROR;
+ for(tg=tglist;tg;tg=tg->next) {
+ if(match(name, tg->name->content))
+ continue;
+
+ displaygroup(sender, tg, showchildren);
+ if(--remaining == 0) {
+ controlreply(sender, "Maximum number of matches reached.");
+ return CMD_OK;
+ }
+ found = 1;
}
- controlreply(sender, "Host added.");
- /* TODO: controlwall */
+ if(!found)
+ controlreply(sender, "No matches found.");
return CMD_OK;
}
-static int trusts_cmdtrustgroupadd(void *source, int cargc, char **cargv) {
+static int trusts_cmdtrustglinesuggest(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;
+ char mask[512];
+ char *p, *user, *host;
+ struct irc_in_addr ip;
+ unsigned char bits;
+ int count;
+ glinebuf gbuf;
+ char creator[32];
- if(cargc < 6)
+ if(cargc < 1)
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;
- }
+ strncpy(mask, cargv[0], sizeof(mask));
- howlong = durationtolong(cargv[2]);
- if((howlong <= 0) || (howlong > 365 * 86400 * 20)) {
- controlreply(sender, "Invalid duration supplied.");
- return CMD_ERROR;
- }
+ p = strchr(mask, '@');
- maxperident = strtoul(cargv[3], NULL, 10);
- if(!howmany || (maxperident > 1000)) {
- controlreply(sender, "Bad value for max per ident.");
- return CMD_ERROR;
- }
+ if(!p)
+ return CMD_USAGE;
- if(cargv[4][0] != '1' && cargv[4][0] != '0') {
- controlreply(sender, "Bad value for enforce ident (use 0 or 1).");
+ user = mask;
+ host = p + 1;
+ *p = '\0';
+
+ if(!ipmask_parse(host, &ip, &bits)) {
+ controlreply(sender, "Invalid CIDR.");
return CMD_ERROR;
}
- enforceident = cargv[4][0] == '1';
-
- contact = cargv[5];
- if(cargc < 7) {
- comment = "(no comment)";
- } else {
- comment = cargv[6];
- }
+ snprintf(creator, sizeof(creator), "#%s", sender->authname);
- /* don't allow #id or id forms */
- if((name[0] == '#') || strtoul(name, NULL, 10)) {
- controlreply(sender, "Invalid trustgroup name.");
- return CMD_ERROR;
- }
+ glinebufinit(&gbuf, 0);
+ glinebufaddbyip(&gbuf, user, &ip, 128, 0, creator, "Simulate", getnettime(), getnettime(), getnettime());
+ glinebufcounthits(&gbuf, &count, NULL);
+ glinebufspew(&gbuf, sender);
+ glinebufabort(&gbuf);
- tg = tg_strtotg(name);
- if(tg) {
- controlreply(sender, "A group with that name already exists");
- return CMD_ERROR;
- }
+ controlreply(sender, "Total hits: %d", count);
- snprintf(createdby, sizeof(createdby), "#%s", sender->authname);
+ return CMD_OK;
+}
- 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;
- }
+static int trusts_cmdtrustspew(void *source, int cargc, char **cargv) {
+ nick *sender = source;
+ searchASTExpr tree;
- controlreply(sender, "Group added.");
- /* TODO: controlwall */
+ if(cargc < 1)
+ return CMD_USAGE;
- return CMD_OK;
+ tree = NSASTNode(tgroup_parse, NSASTLiteral(cargv[0]));
+ return ast_nicksearch(&tree, controlreply, sender, NULL, printnick_channels, NULL, NULL, 2000);
}
static int commandsregistered;
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>");
+ registercontrolhelpcmd("trustlist", NO_OPER, 2, trusts_cmdtrustlist, "Usage: trustlist [-v] <#id|name|IP>\nShows trust data for the specified trust group.");
+ registercontrolhelpcmd("trustglinesuggest", NO_OPER, 1, trusts_cmdtrustglinesuggest, "Usage: trustglinesuggest <user@host>\nSuggests glines for the specified hostmask.");
+ registercontrolhelpcmd("trustspew", NO_OPER, 1, trusts_cmdtrustspew, "Usage: trustspew <#id|name>\nShows currently connected users for the specified trust group.");
}
static void deregistercommands(int hooknum, void *arg) {
return;
commandsregistered = 0;
- deregistercontrolcmd("trustmigrate", trusts_cmdmigrate);
deregistercontrolcmd("trustlist", trusts_cmdtrustlist);
- deregistercontrolcmd("trustgroupadd", trusts_cmdtrustgroupadd);
- deregistercontrolcmd("trustadd", trusts_cmdtrustadd);
+ deregistercontrolcmd("trustglinesuggest", trusts_cmdtrustglinesuggest);
+ deregistercontrolcmd("trustspew", trusts_cmdtrustspew);
}
void _init(void) {
deregisterhook(HOOK_TRUSTS_DB_LOADED, registercommands);
deregisterhook(HOOK_TRUSTS_DB_CLOSED, deregistercommands);
- trusts_migration_stop();
-
deregistercommands(0, NULL);
}