]> jfr.im git - irc/quakenet/newserv.git/blobdiff - trusts/formats.c
CHANSERV: tell user when they can't attempts to auth any more, and drop max attempts...
[irc/quakenet/newserv.git] / trusts / formats.c
index a3397df726aa65d29fd8acc4ebf5b780c81ae529..35bee39d80179f44f5e12452dc0bb813d8fb44e3 100644 (file)
 #include <stdio.h>
 #include <stdint.h>
 #include <time.h>
+#include <string.h>
+#include <stdlib.h>
+#include "../lib/strlfunc.h"
+#include "../irc/irc.h"
+#include "trusts.h"
 
-int trusts_parsecidr(const char *host, uint32_t *ip, short *mask) {
-  unsigned int octet1 = 0, octet2 = 0, octet3 = 0, octet4 = 0, umask = 32;
+char *trusts_timetostr(time_t t) {
+  static char buf[100];
 
-  if(sscanf(host, "%u.%u.%u.%u/%u", &octet1, &octet2, &octet3, &octet4, &umask) != 5)
-    if(sscanf(host, "%u.%u.%u/%u", &octet1, &octet2, &octet3, &umask) != 4)
-      if(sscanf(host, "%u.%u/%u", &octet1, &octet2, &umask) != 3)
-        if(sscanf(host, "%u/%u", &octet1, &umask) != 2)
-          if(sscanf(host, "%u.%u.%u.%u", &octet1, &octet2, &octet3, &octet4) != 4)
-            return 0;
+  strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", localtime(&t));
 
-  if(octet1 > 255 || octet2 > 255 || octet3 > 255 || octet4 > 255 || umask > 32)
-    return 0;
+  return buf;
+}
 
-  *ip = (octet1 << 24) | (octet2 << 16) | (octet3 << 8) | octet4;
-  *mask = umask;
+char *dumpth(trusthost *th, int oformat) {
+  static char buf[512];
 
-  return 1;
+  if(oformat) {
+    snprintf(buf, sizeof(buf), "#%u,%s,%u,%u,%jd", th->group->id, CIDRtostr(th->ip, th->bits), th->count, th->maxusage, (intmax_t)th->lastseen);
+  } else {
+    snprintf(buf, sizeof(buf), "%u,%s,%u,%u,%jd,%jd,%u,%u", th->group->id, CIDRtostr(th->ip, th->bits), th->id, th->maxusage, (intmax_t)th->lastseen, (intmax_t)th->created, th->maxpernode, th->nodebits);
+  }
+
+  return buf;
+}
+
+char *dumptg(trustgroup *tg, int oformat) {
+  static char buf[512];
+
+  if(oformat) {
+    snprintf(buf, sizeof(buf), "#%u,%s,%u,%u,%d,%u,%u,%jd,%jd,%jd,%s,%s,%s", tg->id, tg->name->content, tg->count, tg->trustedfor, tg->flags & TRUST_ENFORCE_IDENT, tg->maxperident, tg->maxusage, (intmax_t)tg->expires, (intmax_t)tg->lastseen, (intmax_t)tg->lastmaxusereset, tg->createdby->content, tg->contact->content, tg->comment->content);
+  } else {
+    snprintf(buf, sizeof(buf), "%u,%s,%u,%d,%u,%u,%jd,%jd,%jd,%s,%s,%s", tg->id, tg->name->content, tg->trustedfor, tg->flags, tg->maxperident, tg->maxusage, (intmax_t)tg->expires, (intmax_t)tg->lastseen, (intmax_t)tg->lastmaxusereset, tg->createdby->content, tg->contact->content, tg->comment->content);
+  }
+
+  return buf;
 }
 
-/* returns mask pre-anded */
-int trusts_str2cidr(const char *host, uint32_t *ip, uint32_t *mask) {
-  uint32_t result;
-  short smask;
+int parsetg(char *buf, trustgroup *tg, int oformat) {
+  char *line, *createdby, *contact, *comment, *name, *id;
+  unsigned long expires, lastseen, lastmaxusereset;
+  char xbuf[1024];
+  int pos;
+
+/* #id,ticket35153,14,20,1,1,17,1879854575,1222639249,0,nterfacer,Qwhois&2120764,Non-Commercial Bouncer (Created by: doomie)
+      ,name       ,current
+                     ,trustedfor
+                        ,flags
+                          ,maxperident
+                            ,maxusage
+                               ,expires  ,lastseen   ,lastmaxusereset
+                                                       ,createdby,contact       ,comment
+*/
+  int r;
+
+  strlcpy(xbuf, buf, sizeof(xbuf));
+  line = xbuf;
+
+  if(!*line)
+    return 0;
+
+  if(oformat) {
+    if(line[0] != '#')
+      return 0;
+    line++;
+  }
+
+  id = line;
+  line = strchr(xbuf, ',');
+  if(!line)
+    return 0;
+  *line++ = '\0';
+
+  if(oformat && (id[0] == '#'))
+    id++;
 
-  if(!trusts_parsecidr(host, &result, &smask))
+  tg->id = strtoul(id, NULL, 10);
+  if(!tg->id)
     return 0;
 
-  if(smask == 0) {
-    *mask = 0;
+  name = line;
+  line = strchr(line, ',');
+  if(!line)
+    return 0;
+  *line++ = '\0';
+
+  if(oformat) {
+    r = sscanf(line, "%*u,%u,%u,%u,%u,%lu,%lu,%lu,%n",
+               /*current, */ &tg->trustedfor, &tg->flags, &tg->maxperident,
+               &tg->maxusage, &expires, &lastseen, &lastmaxusereset, &pos);
+
+    if(tg->maxperident > 0)
+      tg->flags |= TRUST_RELIABLE_USERNAME;
   } else {
-    *mask = 0xffffffff << (32 - smask);
+    r = sscanf(line, "%u,%u,%u,%u,%lu,%lu,%lu,%n",
+               &tg->trustedfor, &tg->flags, &tg->maxperident,
+               &tg->maxusage, &expires, &lastseen, &lastmaxusereset, &pos);
+  }
+  if(r != 7)
+    return 0;
+
+  tg->expires = (time_t)expires;
+  tg->lastseen = (time_t)lastseen;
+  tg->lastmaxusereset = (time_t)lastmaxusereset;
+
+  createdby = &line[pos];
+  contact = strchr(createdby, ',');
+  if(!contact)
+    return 0;
+  *contact++ = '\0';
+
+  comment = strchr(contact, ',');
+  if(!comment)
+    return 0;
+  *comment++ = '\0';
+
+  tg->name = getsstring(name, TRUSTNAMELEN);
+  tg->createdby = getsstring(createdby, CREATEDBYLEN);
+  tg->comment = getsstring(comment, COMMENTLEN);
+  tg->contact = getsstring(contact, CONTACTLEN);
+  if(!tg->name || !tg->createdby || !tg->comment || !tg->contact) {
+    freesstring(tg->name);
+    freesstring(tg->createdby);
+    freesstring(tg->comment);
+    freesstring(tg->contact);
+    return 0;
   }
-  *ip = result & *mask;
 
   return 1;
 }
 
-char *trusts_cidr2str(uint32_t ip, uint32_t mask) {
-  static char buf[100];
-  char maskbuf[10];
+int parseth(char *line, trusthost *th, unsigned int *tgid, int oformat) {
+  unsigned long lastseen, created;
+  int maxpernode, nodebits;
+  char *ip, xbuf[1024], *id;
 
-  if(mask != 0) {
-    /* count number of trailing zeros */
-    float f = (float)(mask & -mask);
+/* #id,192.168.2.128/26,20,23,1222732944
+       ip                ,cur,max,lastseen */
 
-    mask = 32 - ((*(unsigned int *)&f >> 23) - 0x7f);
-  }
+  strlcpy(xbuf, line, sizeof(xbuf));
+  id = line = xbuf;
+
+  line = strchr(line, ',');
+  if(!line)
+    return 0;
+  *line++ = '\0';
+
+  if(oformat && (id[0] == '#'))
+    id++;
+
+  *tgid = strtoul(id, NULL, 10);
+  if(!*tgid)
+    return 0;
 
-  if(mask < 32) {
-    snprintf(maskbuf, sizeof(maskbuf), "/%u", mask);
+  ip = line;
+  line = strchr(line, ',');
+  if(!line)
+    return 0;
+  *line++ = '\0';
+
+  if(!ipmask_parse(ip, &th->ip, &th->bits))
+    return 0;
+
+  if(oformat) {
+    if(sscanf(line, "%*u,%u,%lu", /*current, */&th->maxusage, &lastseen) != 2)
+      return 0;
+    created = getnettime();
+    maxpernode = 0;
+    nodebits = 128;
   } else {
-    maskbuf[0] = '\0';
+    if(sscanf(line, "%u,%u,%lu,%lu,%d,%d", &th->id, &th->maxusage, &lastseen, &created, &maxpernode, &nodebits) != 6)
+      return 0;
   }
 
-  snprintf(buf, sizeof(buf), "%u.%u.%u.%u%s", (ip >> 24) & 0xff, (ip >> 16) & 0xff, (ip >> 8) & 0xff, ip & 0xff, maskbuf);
+  th->lastseen = (time_t)lastseen;
+  th->created = (time_t)created;
+  th->maxpernode = maxpernode;
+  th->nodebits = nodebits;
 
-  return buf;
+  return 1;
 }
 
-char *trusts_timetostr(time_t t) {
-  static char buf[100];
+char *rtrim(char *buf) {
+  static char obuf[1024];
+  size_t len = strlcpy(obuf, buf, sizeof(obuf));
 
-  strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", localtime(&t));
+  if((len < sizeof(obuf)) && (len > 0)) {
+    int i;
+    for(i=len-1;i>=0;i--) {
+      if(obuf[i] != ' ')
+        break;
 
-  return buf;
-}
+      obuf[i] = '\0';
+    }
+  }
 
+  return obuf;
+}