Explorar o código

* More cookie work, needs re-use checking still

svn: 1256
Bryan Drewery %!s(int64=22) %!d(string=hai) anos
pai
achega
544892066e
Modificáronse 3 ficheiros con 130 adicións e 40 borrados
  1. 103 15
      src/mod/irc.mod/irc.c
  2. 3 1
      src/mod/irc.mod/irc.h
  3. 24 24
      src/mod/irc.mod/mode.c

+ 103 - 15
src/mod/irc.mod/irc.c

@@ -102,28 +102,116 @@ dprintf(DP_HELP, "PRIVMSG %s :cap flood.\n", chan->dname);
 
 }
 
+/* this is to become the NEW makeplaincookie */
+static char *salt2 = NULL;
 
+static char *
+makecookie(char *chn, char *bnick)
+{
+  char *buf = NULL, randstring[5] = "", ts[11] = "", *chname = NULL, *hash = NULL, tohash[50] = "";
+
+  if (!salt2)
+    salt2 = strdup(SALT2);
+
+  chname = strdup(chn);
+
+  make_rand_str(randstring, 4);
+
+  /* &ts[4] is now last 6 digits of time */
+  sprintf(ts, "%010li", now + timesync);
+  
+  /* Only use first 3 chars of chan */
+  if (strlen(chname) > 2)
+    chname[3] = 0;
+  strtoupper(chname);
+
+  sprintf(tohash, "%c%s%s%s%s%c", salt2[0], bnick, chname, &ts[4], randstring, salt2[15]);
+  hash = MD5(tohash);
+  buf = calloc(1, 20);
+  sprintf(buf, "%c%c%c!%s@%s", hash[8], hash[16], hash[18], randstring, ts);
+  /* sprintf(buf, "%c/%s!%s@%X", hash[16], randstring, ts, BIT31); */
+  free(chname);
+  return buf;
+}
 
-void
-makeopline(struct chanset_t *chan, char *nick, char *buf)
+static int
+checkcookie(char *chn, char *bnick, char *cookie)
 {
-  char plaincookie[20] = "", enccookie[48] = "", *p = NULL, nck[20] = "", key[200] = "";
-  memberlist *m = NULL;
+  char randstring[5] = "", ts[11] = "", *chname = NULL, *hash = NULL, tohash[50] = "", *p = NULL;
+  time_t optime = 0;
+  int goodhash = 0, goodts = 0, goodobscure = 0;
+  if (!salt2)
+    salt2 = strdup(SALT2);
+
+  chname = strdup(chn);
+  p = cookie;
+  p += 4; /* has! */
+  strncpyz(randstring, p, sizeof(randstring));
+  p += 5; /* rand@ */
+  /* &ts[4] is now last 6 digits of time */
+  strncpyz(ts, p, sizeof(ts));
+  optime = atol(ts);
+
+  /* Only use first 3 chars of chan */
+  if (strlen(chname) > 2)
+    chname[3] = 0;
+  strtoupper(chname);
+  putlog(LOG_MISC, "*", "randstring: %s ts: %s", randstring, ts);
+  /* hash!rand@ts */
+  sprintf(tohash, "%c%s%s%s%s%c", salt2[0], bnick, chname, &ts[4], randstring, salt2[15]);
+  putlog(LOG_MISC, "*", "tohash: %s", tohash);
+  hash = MD5(tohash);
+  if (hash[8] == cookie[0] && hash[16] == cookie[1] && hash[18] == cookie[2])
+    goodhash = 1;
+  else
+    return 1;
+  putlog(LOG_MISC, "*", "slack: %d", ((now + timesync) - optime));
+  if (((now + timesync) - optime) < 300)
+    goodts = 1;
+  else
+    return 2;
+  goodobscure = 1;
+  if (goodhash && goodts && goodobscure)
+    putlog(LOG_MISC, "*", "GOOD COOKIE!");
 
-  if ((m = ismember(chan, nick)))
-    strcpy(nck, m->nick);
+  return 0;
+}
+
+/*
+   plain cookie:
+   Last 6 digits of time
+   Last 5 chars of nick
+   Last 4 regular chars of chan
+ */
+static void 
+makeplaincookie(char *chname, char *nick, char *buf)
+{
+  char work[256] = "", work2[256] = "";
+  int i, n;
+
+  sprintf(work, "%010li", (now + timesync));
+  strcpy(buf, (char *) &work[4]);
+  work[0] = 0;
+  if (strlen(nick) < 5)
+    while (strlen(work) + strlen(nick) < 5)
+      strcat(work, " ");
   else
-    strcpy(nck, nick);
-  makeplaincookie(chan->dname, nck, plaincookie);
-  strcpy(key, botname);
-  strcat(key, SALT2);
-/*  putlog(LOG_DEBUG, "*", "Encrypting opline for %s with cookie %s and key %s", nck, plaincookie, key); */
-  p = encrypt_string(key, plaincookie);
-  strcpy(enccookie, p);
-  free(p);
-  sprintf(buf, "MODE %s +o-b %s *!*@<%s>\n", chan->name, nck, enccookie);
+    strcpy(work, (char *) &nick[strlen(nick) - 5]);
+  strcat(buf, work);
+
+  n = 3;
+  for (i = strlen(chname) - 1; (i >= 0) && (n >= 0); i--)
+    if (((unsigned char) chname[i] < 128) && ((unsigned char) chname[i] > 32)) {
+      work2[n] = tolower(chname[i]);
+      n--;
+    }
+  while (n >= 0)
+    work2[n--] = ' ';
+  work2[4] = 0;
+  strcat(buf, work2);
 }
 
+
 /*
    opreq = o #chan nick
    inreq = i #chan nick uhost 

+ 3 - 1
src/mod/irc.mod/irc.h

@@ -21,7 +21,9 @@
 
 
 static int check_bind_pubc(char *, char *, char *, struct userrec *, char *, char *);
-static void makeopline(struct chanset_t *, char *, char *);
+static char *makecookie(char *, char *);
+static int checkcookie(char *, char *, char *);
+static void makeplaincookie(char *, char *, char *);
 static int me_voice(struct chanset_t *);
 static int any_ops(struct chanset_t *);
 static char *getchanmode(struct chanset_t *);

+ 24 - 24
src/mod/irc.mod/mode.c

@@ -30,8 +30,8 @@ do_op(char *nick, struct chanset_t *chan, int delay, int force)
     return 0;
 
   if (channel_fastop(chan) || channel_take(chan)) {
-    add_mode(chan, '+', 'o', nick);
-//    add_cookie(chan, nick);
+//    add_mode(chan, '+', 'o', nick);
+    add_cookie(chan, nick);
   } else {
     add_cookie(chan, nick);
   }
@@ -57,7 +57,6 @@ flush_cookies(struct chanset_t *chan, int pri)
       *p++ = 'o';
       postsize -= egg_strcatn(post, chan->ccmode[i].op, sizeof(post));
       postsize -= egg_strcatn(post, " ", sizeof(post));
-
       free(chan->ccmode[i].op), chan->ccmode[i].op = NULL;
     }
   }
@@ -70,6 +69,7 @@ flush_cookies(struct chanset_t *chan, int pri)
   *p = 0;
 
   if (post[0]) {
+    char *cookie = NULL;
     /* remove the trailing space... */
     size_t myindex = (sizeof(post) - 1) - postsize;
 
@@ -79,14 +79,10 @@ flush_cookies(struct chanset_t *chan, int pri)
     egg_strcatn(out, " ", sizeof(out));
     egg_strcatn(out, post, sizeof(out));
     egg_strcatn(out, " ", sizeof(out));
-    egg_strcatn(out, "*!*@NEW.MULTI.BOT.COOKIE.OPS.COMING.SOON", sizeof(out));
-/*    char *tmp = NULL;
 
-    tmp = calloc(1, strlen(chan->name) + 200);
-    makeopline(chan, nick, tmp);
-    dprintf(DP_MODE, tmp);
-    free(tmp);
-*/
+    cookie = makecookie(chan->dname, botname);
+    egg_strcatn(out, cookie, sizeof(out));
+    free(cookie);
   }
   if (out[0]) {
     if (pri == QUICK) {
@@ -338,7 +334,6 @@ real_add_mode(struct chanset_t *chan, const char plus, const char mode, const ch
     }
 
     /* op-type mode change */
-
     /* for cookie ops, use ccmode instead of cmode */
     if (cookie) {
       for (i = 0; i < (modesperline - 1); i++)
@@ -417,6 +412,14 @@ real_add_mode(struct chanset_t *chan, const char plus, const char mode, const ch
     modes--;
   if (modes < 1)
     flush_mode(chan, NORMAL);   /* Full buffer! Flush modes. */
+ 
+  /* flush full cookie queue */
+  modes = modesperline - 1;
+  for (i = 0; i < (modesperline - 1); i++)
+    if (chan->ccmode[i].op)
+      modes--;
+  if (modes < 1)
+    flush_cookies(chan, NORMAL);
 }
 
 /*
@@ -984,22 +987,19 @@ gotmode(char *from, char *msg)
               if (unbans != 1 || (strncmp(modes[modecnt - 1], "-b", 2))) {
                 isbadop = 1;
               } else {
-                char enccookie[25] = "", plaincookie[25] = "", key[NICKLEN + 20] = "", goodcookie[25] = "";
-
-                /* -b *!*@[...] */
-                strncpyz(enccookie, (char *) &(modes[modecnt - 1][8]), sizeof(enccookie));
-                p = enccookie + strlen(enccookie) - 1;
-                strcpy(key, nick);
-                strcat(key, SALT2);
-                p = decrypt_string(key, enccookie);
-                strncpyz(plaincookie, p, sizeof(plaincookie));
-                free(p);
+                char cookie[20] = "", *goodcookie = NULL;
+
+                /* -b hash!rand@time */
+                strncpyz(cookie, (char *) &(modes[modecnt - 1][3]), sizeof(cookie));
+                checkcookie(chan->dname, nick, cookie);
+
                 /*
                  * last 6 digits of time
                  * last 5 chars of nick
                  * last 5 regular chars of chan
                  */
-                makeplaincookie(chan->dname, (char *) (modes[0] + 3), goodcookie);	/*[0]+3 is first +o nick */
+/*
+                makeplaincookie(chan->dname, (char *) (modes[0] + 3), goodcookie);	
                 if (strncmp((char *) &plaincookie[6], (char *) &goodcookie[6], 5))
                   isbadop = 2;
                 else if (strncmp((char *) &plaincookie[11], (char *) &goodcookie[11], 5))
@@ -1008,18 +1008,18 @@ gotmode(char *from, char *msg)
                   char ltmp[20] = "";
                   time_t optime, off;
 
-                  /* this makes NO sense, optime should just be ltmp[4]-... but its not... ??? */
                   sprintf(ltmp, "%010li", now + timesync);
                   strncpyz((char *) &ltmp[4], plaincookie, 7);
                   optime = atol(ltmp);
                   off = (now + timesync - optime);
 
                   if (chan->cookie_time_slack && (abs(off) > chan->cookie_time_slack)) {
-                    /* isbadop = 4; */
+                    // isbadop = 4;
                     putlog(LOG_DEBUG, "*", "%s opped with bad ts (not punishing.): %li was off by %li", nick,
                            optime, off);
                   }
                 }
+*/
               }
               if (isbadop) {
                 char trg[NICKLEN] = "";