]> jfr.im git - irc/quakenet/newserv.git/blobdiff - trusts/trusts_commands.c
Merge default.
[irc/quakenet/newserv.git] / trusts / trusts_commands.c
index fa51c17e53b2c16c5e0d736545e5c3e7c3cfda79..baa7f1544aa03c47a9214dc02828e42a1402bbf4 100644 (file)
@@ -1,20 +1,24 @@
 #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("");
+
 static void registercommands(int, void *);
 static void deregistercommands(int, void *);
 
 extern void printnick_channels(searchCtx *, nick *, nick *);
 
-void calculatespaces(int spaces, int width, char *str, char **_prebuf, char **_postbuf) {
+void calculatespaces(int spaces, int width, const char *str, char **_prebuf, char **_postbuf) {
   static char prebuf[512], postbuf[512];
   int spacelen;
 
@@ -37,12 +41,14 @@ void calculatespaces(int spaces, int width, char *str, char **_prebuf, char **_p
   *_postbuf = postbuf;
 }
 
-static void traverseandmark(unsigned int marker, trusthost *th) {
+static void traverseandmark(unsigned int marker, trusthost *th, int markchildren) {
   th->marker = marker;
 
-  for(th=th->children;th;th=th->nextbychild) {
-    th->marker = marker;
-    traverseandmark(marker, th);
+  if(markchildren) {
+    for(th=th->children;th;th=th->nextbychild) {
+      th->marker = marker;
+      traverseandmark(marker, th, markchildren);
+    }
   }
 }
 
@@ -61,11 +67,11 @@ static void insertth(array *parents, trusthost *th) {
   }
 }
 
-static void marktree(array *parents, unsigned int marker, trusthost *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->next) {
+  for(pth=th->parent;pth;pth=pth->parent) {
     insertth(parents, pth);
 
     pth->marker = marker;
@@ -75,20 +81,26 @@ static void marktree(array *parents, unsigned int marker, trusthost *th) {
     insertth(parents, th);
 
   /* sadly we need to recurse down */
-  traverseandmark(marker, th);
+  traverseandmark(marker, th, showchildren);
 }
 
-static void outputtree(nick *np, unsigned int marker, trustgroup *originalgroup, trusthost *th, int depth) {
-  char *cidrstr, *prespacebuf, *postspacebuf, parentbuf[512];
+static void outputtree(nick *np, unsigned int marker, trustgroup *originalgroup, trusthost *th, int depth, int showchildren) {
+  const char *cidrstr;
+  char *prespacebuf, *postspacebuf, parentbuf[512];
 
   if(th->marker != marker)
     return;
 
-  cidrstr = trusts_cidr2str(&th->ip, th->bits);
-  calculatespaces(depth + 1, 30 + 1, cidrstr, &prespacebuf, &postspacebuf);
+  cidrstr = CIDRtostr(th->ip, th->bits);
+  calculatespaces(depth + 2, 30 + 1, cidrstr, &prespacebuf, &postspacebuf);
 
   if(th->group == originalgroup) {
-    prespacebuf[0] = '>';
+    if(!showchildren && th->group == originalgroup && th->children)
+      prespacebuf[0] = '*';
+    else
+      prespacebuf[0] = ' ';
+
+    prespacebuf[1] = '>';
 
     parentbuf[0] = '\0';
   } else {
@@ -103,10 +115,55 @@ static void outputtree(nick *np, unsigned int marker, trustgroup *originalgroup,
   th->marker = -1;
 
   for(th=th->children;th;th=th->nextbychild)
-    outputtree(np, marker, originalgroup, th, depth + 1);
+    outputtree(np, marker, originalgroup, th, depth + 1, showchildren);
+}
+
+static char *formatflags(int flags) {
+  static char buf[512];
+
+  buf[0] = '\0';
+
+  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);
+  }
+
+  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) {
+static void displaygroup(nick *sender, trustgroup *tg, int showchildren) {
   trusthost *th, **p2;
   unsigned int marker;
   array parents;
@@ -115,29 +172,32 @@ static void displaygroup(nick *sender, trustgroup *tg) {
 
   /* 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)?((tg->expires>t)?longtoduration(tg->expires - t, 2):"(the past -- BUG)"):"never");
-  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->lastmaxusereset?trusts_timetostr(tg->lastmaxusereset):"(never)");
 
+  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)
-    marktree(&parents, marker, th);
+    marktree(&parents, marker, th, showchildren);
 
   p2 = (trusthost **)(parents.content);
   for(i=0;i<parents.cursi;i++)
-    outputtree(sender, marker, tg, p2[i], 0);
+    outputtree(sender, marker, tg, p2[i], 0, showchildren);
 
   array_free(&parents);
 
@@ -152,16 +212,26 @@ static int trusts_cmdtrustlist(void *source, int cargc, char **cargv) {
   trusthost *th;
   struct irc_in_addr ip;
   unsigned char bits;
+  int showchildren;
 
   if(cargc < 1)
     return CMD_USAGE;
 
-  name = cargv[0];
+  if(strcmp(cargv[0], "-v") == 0) {
+    if(cargc < 2)
+      return CMD_USAGE;
+
+    showchildren = 1;
+    name = cargv[1];
+  } else {
+    showchildren = 0;
+    name = cargv[0];
+  }
 
   tg = tg_strtotg(name);
 
   if(tg) {
-    displaygroup(sender, tg);
+    displaygroup(sender, tg, showchildren);
     return CMD_OK;
   }
 
@@ -173,7 +243,7 @@ static int trusts_cmdtrustlist(void *source, int cargc, char **cargv) {
       return CMD_OK;
     }
 
-    displaygroup(sender, th->group);
+    displaygroup(sender, th->group, showchildren);
     return CMD_OK;
   }
 
@@ -181,7 +251,7 @@ static int trusts_cmdtrustlist(void *source, int cargc, char **cargv) {
     if(match(name, tg->name->content))
       continue;
 
-    displaygroup(sender, tg);
+    displaygroup(sender, tg, showchildren);
     if(--remaining == 0) {
       controlreply(sender, "Maximum number of matches reached.");
       return CMD_OK;
@@ -195,130 +265,44 @@ static int trusts_cmdtrustlist(void *source, int cargc, char **cargv) {
   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 trusts_cmdtrustgline(void *source, int cargc, char **cargv) {
-  trustgroup *tg;
+static int trusts_cmdtrustglinesuggest(void *source, int cargc, char **cargv) {
   nick *sender = source;
-  char *user, *reason;
-  int duration, count;
+  char mask[512];
+  char *p, *user, *host;
+  struct irc_in_addr ip;
+  unsigned char bits;
+  int count;
+  glinebuf gbuf;
+  char creator[32];
 
-  if(cargc < 4)
+  if(cargc < 1)
     return CMD_USAGE;
 
-  tg = tg_strtotg(cargv[0]);
-  if(!tg) {
-    controlreply(sender, "Couldn't look up trustgroup.");
-    return CMD_ERROR;
-  }
-
-  user = cargv[1];
-
-  duration = durationtolong(cargv[2]);
-  if((duration <= 0) || (duration > MAXDURATION)) {
-    controlreply(sender, "Invalid duration supplied.");
-    return CMD_ERROR;
-  }
-
-  reason = cargv[3];
-
-  count = trustgline(tg, user, duration, reason);
-
-  controlwall(NO_OPER, NL_GLINES|NL_TRUSTS, "%s TRUSTGLINE'd user '%s' on group '%s', %d gline(s) set.", controlid(sender), user, tg->name->content, count);
-  controlreply(sender, "Done. %d gline(s) set.", count);
+  strncpy(mask, cargv[0], sizeof(mask));
 
-  return CMD_OK;
-}
+  p = strchr(mask, '@');
 
-static int trusts_cmdtrustungline(void *source, int cargc, char **cargv) {
-  trustgroup *tg;
-  nick *sender = source;
-  char *user;
-  int count;
-
-  if(cargc < 2)
+  if(!p)
     return CMD_USAGE;
 
-  tg = tg_strtotg(cargv[0]);
-  if(!tg) {
-    controlreply(sender, "Couldn't look up trustgroup.");
+  user = mask;
+  host = p + 1;
+  *p = '\0';
+
+  if(!ipmask_parse(host, &ip, &bits)) {
+    controlreply(sender, "Invalid CIDR.");
     return CMD_ERROR;
   }
 
-  user = cargv[1];
+  snprintf(creator, sizeof(creator), "#%s", sender->authname);
 
-  count = trustungline(tg, user, 0, "Deactivated.");
+  glinebufinit(&gbuf, 0);
+  glinebufaddbyip(&gbuf, user, &ip, 128, 0, creator, "Simulate", getnettime(), getnettime(), getnettime());
+  glinebufcounthits(&gbuf, &count, NULL);
+  glinebufspew(&gbuf, sender);
+  glinebufabort(&gbuf);
 
-  controlwall(NO_OPER, NL_GLINES|NL_TRUSTS, "%s TRUSTUNGLINE'd user '%s' on group '%s', %d gline(s) removed.", controlid(sender), user, tg->name->content, count);
-  controlreply(sender, "Done. %d gline(s) removed.", count);
+  controlreply(sender, "Total hits: %d", count);
 
   return CMD_OK;
 }
@@ -341,10 +325,8 @@ static void registercommands(int hooknum, void *arg) {
     return;
   commandsregistered = 1;
 
-  registercontrolhelpcmd("trustlist", NO_OPER, 1, trusts_cmdtrustlist, "Usage: trustlist <#id|name|IP>\nShows trust data for the specified trust group.");
-  registercontrolhelpcmd("trustdump", NO_OPER, 2, trusts_cmdtrustdump, "Usage: trustdump <#id> <number>");
-  registercontrolhelpcmd("trustgline", NO_OPER, 4, trusts_cmdtrustgline, "Usage: trustgline <#id|name> <user> <duration> <reason>\nGlines a user on all hosts of a trust group.");
-  registercontrolhelpcmd("trustungline", NO_OPER, 4, trusts_cmdtrustungline, "Usage: trustungline <#id|name> <user>\nUnglines a user on all hosts of a trust group.");
+  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.");
 }
 
@@ -354,9 +336,7 @@ static void deregistercommands(int hooknum, void *arg) {
   commandsregistered = 0;
 
   deregistercontrolcmd("trustlist", trusts_cmdtrustlist);
-  deregistercontrolcmd("trustdump", trusts_cmdtrustdump);
-  deregistercontrolcmd("trustgline", trusts_cmdtrustgline);
-  deregistercontrolcmd("trustungline", trusts_cmdtrustungline);
+  deregistercontrolcmd("trustglinesuggest", trusts_cmdtrustglinesuggest);
   deregistercontrolcmd("trustspew", trusts_cmdtrustspew);
 }