]> jfr.im git - irc/quakenet/newserv.git/blobdiff - trusts/trusts_management.c
Merge throttle.
[irc/quakenet/newserv.git] / trusts / trusts_management.c
index dfd82be5f3cf740881b85164efcbb8230d3c0495..4520fb5af633f09ff8db9a03bc1d3d0d6e2c89ca 100644 (file)
@@ -8,8 +8,8 @@
 #include "../core/schedule.h"
 #include "../irc/irc.h"
 #include "../lib/stringbuf.h"
-#include "../noperserv/noperserv.h"
-#include "../noperserv/noperserv_policy.h"
+#include "../control/control.h"
+#include "../control/control_policy.h"
 #include "trusts.h"
 
 MODULE_VERSION("");
@@ -56,7 +56,7 @@ static int trusts_cmdtrustadd(void *source, int cargc, char **cargv) {
   if (!noperserv_policy_command_permitted(NO_DEVELOPER, sender)) {
     int minbits = irc_in_addr_is_ipv4(&ip)?TRUST_MIN_UNPRIVILEGED_BITS_IPV4:TRUST_MIN_UNPRIVILEGED_BITS_IPV6;
     if(bits < minbits) {
-      controlreply(sender, "You don't have the necessary privileges to add a subnet larger than /%d.", irc_in_addr_is_ipv4(&ip)?(minbits-96):minbits);
+      controlreply(sender, "You don't have the necessary privileges to add a subnet larger than /%d.", irc_bitlen(&ip, minbits));
       return CMD_ERROR;
     }
 
@@ -275,7 +275,7 @@ static int trusts_cmdtrustdel(void *source, int cargc, char **cargv) {
   if (!noperserv_policy_command_permitted(NO_DEVELOPER, sender)) {
     int minbits = irc_in_addr_is_ipv4(&ip)?TRUST_MIN_UNPRIVILEGED_BITS_IPV4:TRUST_MIN_UNPRIVILEGED_BITS_IPV6;
     if(bits < minbits) {
-      controlreply(sender, "You don't have the necessary privileges to remove a subnet larger than /%d.", irc_in_addr_is_ipv4(&ip)?(minbits-96):minbits);
+      controlreply(sender, "You don't have the necessary privileges to remove a subnet larger than /%d.", irc_bitlen(&ip, minbits));
       return CMD_ERROR;
     }
 
@@ -401,6 +401,20 @@ static int modifyreliableusername(void *arg, char *num, nick *source, int overri
   return 1;
 }
 
+static int modifyunthrottle(void *arg, char *num, nick *source, int override) {
+  trustgroup *tg = arg;
+
+  if(num[0] == '1') {
+    tg->flags |= TRUST_UNTHROTTLE;
+  } else if(num[0] == '0') {
+    tg->flags &= ~TRUST_UNTHROTTLE;
+  } else {
+    return 0;
+  }
+
+  return 1;
+}
+
 static int modifyexpires(void *arg, char *expires, nick *source, int override) {
   trustgroup *tg = arg;
   int howlong = durationtolong(expires);
@@ -487,11 +501,16 @@ static int modifynodebits(void *arg, char *num, nick *source, int override) {
   if(irc_in_addr_is_ipv4(&th->ip))
     nodebits += 96;
 
+  if(nodebits > 128) {
+    controlreply(source, "Node bits is invalid.");
+    return 0;
+  }
+
   if(!override) {
     int minbits = irc_in_addr_is_ipv4(&th->ip)?TRUST_MIN_UNPRIVILEGED_NODEBITS_IPV4:TRUST_MIN_UNPRIVILEGED_NODEBITS_IPV6;
 
     if(nodebits < minbits) {
-      controlreply(source, "You don't have the necessary privileges to set node bits to a subnet larger than /%d.", irc_in_addr_is_ipv4(&th->ip)?(minbits-96):minbits);
+      controlreply(source, "You don't have the necessary privileges to set node bits to a subnet larger than /%d.", irc_bitlen(&th->ip, minbits));
       return 0;
     }
   }
@@ -514,9 +533,8 @@ static struct trustmodification *trusthostmods;
 static int trusts_cmdtrustgroupmodify(void *source, int cargc, char **cargv) {
   trustgroup *tg;
   nick *sender = source;
-  char *what, *to, validfields[512];
+  char *what, *to;
   int i, override;
-  StringBuf b;
 
   if(cargc < 3)
     return CMD_USAGE;
@@ -538,7 +556,6 @@ static int trusts_cmdtrustgroupmodify(void *source, int cargc, char **cargv) {
     return CMD_ERROR;
   }
 
-  sbinit(&b, validfields, sizeof(validfields));
   for(i=0;i<trustgroupmods_a.cursi;i++) {
     if(!strcmp(what, trustgroupmods[i].name)) {
       if(!(trustgroupmods[i].fn)(tg, to, sender, override)) {
@@ -547,17 +564,10 @@ static int trusts_cmdtrustgroupmodify(void *source, int cargc, char **cargv) {
       }
       break;
     }
-
-    if(i > 0)
-      sbaddstr(&b, ", ");
-    sbaddstr(&b, trustgroupmods[i].name);
   }
 
-  if(i == trustgroupmods_a.cursi) {
-    sbterminate(&b);
-    controlreply(sender, "No such field, valid fields are: %s", validfields);
-    return CMD_ERROR;
-  }
+  if(i == trustgroupmods_a.cursi)
+    return CMD_USAGE;
 
   triggerhook(HOOK_TRUSTS_MODIFYGROUP, tg);
   tg_update(tg);
@@ -573,9 +583,8 @@ static int trusts_cmdtrusthostmodify(void *source, int cargc, char **cargv) {
   trustgroup *tg;
   trusthost *th;
   nick *sender = source;
-  char *what, *to, validfields[512];
+  char *what, *to;
   int i, override;
-  StringBuf b;
   struct irc_in_addr ip;
   unsigned char bits;
 
@@ -597,7 +606,7 @@ static int trusts_cmdtrusthostmodify(void *source, int cargc, char **cargv) {
   if (!noperserv_policy_command_permitted(NO_DEVELOPER, sender)) {
     int minbits = irc_in_addr_is_ipv4(&ip)?TRUST_MIN_UNPRIVILEGED_BITS_IPV4:TRUST_MIN_UNPRIVILEGED_BITS_IPV6;
     if(bits < minbits) {
-      controlreply(sender, "You don't have the necessary privileges to modify a subnet larger than /%d.", irc_in_addr_is_ipv4(&ip)?(minbits-96):minbits);
+      controlreply(sender, "You don't have the necessary privileges to modify a subnet larger than /%d.", irc_bitlen(&ip, minbits));
       return CMD_ERROR;
     }
 
@@ -619,7 +628,6 @@ static int trusts_cmdtrusthostmodify(void *source, int cargc, char **cargv) {
 
   override = noperserv_policy_command_permitted(NO_DEVELOPER, sender);
 
-  sbinit(&b, validfields, sizeof(validfields));
   for(i=0;i<trusthostmods_a.cursi;i++) {
     if(!strcmp(what, trusthostmods[i].name)) {
       if(!(trusthostmods[i].fn)(th, to, sender, override)) {
@@ -628,24 +636,17 @@ static int trusts_cmdtrusthostmodify(void *source, int cargc, char **cargv) {
       }
       break;
     }
-
-    if(i > 0)
-      sbaddstr(&b, ", ");
-    sbaddstr(&b, trusthostmods[i].name);
   }
 
-  if(i == trusthostmods_a.cursi) {
-    sbterminate(&b);
-    controlreply(sender, "No such field, valid fields are: %s", validfields);
-    return CMD_ERROR;
-  }
+  if(i == trusthostmods_a.cursi)
+    return CMD_USAGE;
 
   triggerhook(HOOK_TRUSTS_MODIFYHOST, th);
   th_update(th);
   controlreply(sender, "Host modified.");
 
-  controlwall(NO_OPER, NL_TRUSTS, "%s TRUSTMODIFIED'ed host '%s' in group '%s' (field: %s, value: %s)", controlid(sender), trusts_cidr2str(&ip, bits), tg->name->content, what, to);
-  trustlog(tg, sender->authname, "Modified %s for host '%s': %s", what, trusts_cidr2str(&ip, bits), to);
+  controlwall(NO_OPER, NL_TRUSTS, "%s TRUSTMODIFIED'ed host '%s' in group '%s' (field: %s, value: %s)", controlid(sender), CIDRtostr(ip, bits), tg->name->content, what, to);
+  trustlog(tg, sender->authname, "Modified %s for host '%s': %s", what, CIDRtostr(ip, bits), to);
 
   return CMD_OK;
 }
@@ -741,6 +742,11 @@ static int trusts_cmdtrustcleanup(void *source, int cargc, char **cargv) {
 static int commandsregistered;
 
 static void registercommands(int hooknum, void *arg) {
+  static char tgmhelp[512], thmhelp[512];
+  char validfields[512];
+  StringBuf b;
+  int i;
+
   if(commandsregistered)
     return;
   commandsregistered = 1;
@@ -749,8 +755,29 @@ static void registercommands(int hooknum, void *arg) {
   registercontrolhelpcmd("trustadd", NO_OPER, 2, trusts_cmdtrustadd, "Usage: trustadd <#id|name|id> <host>");
   registercontrolhelpcmd("trustgroupdel", NO_OPER, 1, trusts_cmdtrustgroupdel, "Usage: trustgroupdel <#id|name|id>");
   registercontrolhelpcmd("trustdel", NO_OPER, 2, trusts_cmdtrustdel, "Usage: trustdel <#id|name|id> <ip/mask>");
-  registercontrolhelpcmd("trustgroupmodify", NO_OPER, 3, trusts_cmdtrustgroupmodify, "Usage: trustgroupmodify <#id|name|id> <field> <new value>");
-  registercontrolhelpcmd("trusthostmodify", NO_OPER, 4, trusts_cmdtrusthostmodify, "Usage: trusthostmodify <#id|name|id> <host> <field> <new value>");
+
+  sbinit(&b, validfields, sizeof(validfields));
+  for(i=0;i<trustgroupmods_a.cursi;i++) {
+    if(i > 0)
+      sbaddstr(&b, ", ");
+    sbaddstr(&b, trustgroupmods[i].name);
+  }
+  sbterminate(&b);
+
+  snprintf(tgmhelp, sizeof(tgmhelp), "Usage: trustgroupmodify <#id|name|id> <field> <new value>\nModifies a trust group.\nValid fields: %s", validfields);
+  registercontrolhelpcmd("trustgroupmodify", NO_OPER, 3, trusts_cmdtrustgroupmodify, tgmhelp);
+
+  sbinit(&b, validfields, sizeof(validfields));
+  for(i=0;i<trusthostmods_a.cursi;i++) {
+    if(i > 0)
+      sbaddstr(&b, ", ");
+    sbaddstr(&b, trusthostmods[i].name);
+  }
+  sbterminate(&b);
+
+  snprintf(thmhelp, sizeof(thmhelp), "Usage: trusthostmodify <#id|name|id> <host> <field> <new value>\nModifies a trust host\nValid fields: %s", validfields);
+  registercontrolhelpcmd("trusthostmodify", NO_OPER, 4, trusts_cmdtrusthostmodify, thmhelp);
+
   registercontrolhelpcmd("trustlog", NO_OPER, 2, trusts_cmdtrustlog, "Usage: trustlog <#id|name> ?limit?\nShows log for the specified trust group.");
   registercontrolhelpcmd("trustloggrep", NO_OPER, 2, trusts_cmdtrustloggrep, "Usage trustloggrep <pattern> ?limit?\nShows maching log entries.");
   registercontrolhelpcmd("trustcomment", NO_OPER, 2, trusts_cmdtrustcomment, "Usage: trustcomment <#id|name> <comment>\nLogs a comment for a trust.");
@@ -790,6 +817,7 @@ static void setupmods(void) {
   MSGROUP(trustedfor);
   MSGROUP(cleanup);
   MSGROUP(protected);
+  MSGROUP(unthrottle);
 
   MSHOST(maxpernode);
   MSHOST(nodebits);
@@ -838,15 +866,16 @@ static void cleanuptrusts(void *arg) {
     }
 
     for(i=0;i<expiredths.cursi;i++) {
-      char *cidrstr;
+      const char *cidrstr;
 
       th = ((trusthost **)(expiredths.content))[i];
       triggerhook(HOOK_TRUSTS_DELHOST, th);
-      th_delete(th);
 
-      cidrstr = trusts_cidr2str(&th->ip, th->bits);
+      cidrstr = CIDRtostr(th->ip, th->bits);
       trustlog(tg, "cleanuptrusts", "Removed host '%s' because it was unused for %d days.", cidrstr, CLEANUP_TH_INACTIVE);
 
+      th_delete(th);
+
       thcount++;
     }