Replaced cached_msglist array with a btree persistent through the session.
authorArt Cancro <ajc@citadel.org>
Thu, 27 Jan 2011 23:11:22 +0000 (18:11 -0500)
committerWilfried Goesgens <dothebart@citadel.org>
Sun, 4 Sep 2011 14:08:52 +0000 (14:08 +0000)
citadel/citserver.c
citadel/context.c
citadel/context.h
citadel/msgbase.c
citadel/msgbase.h
citadel/server.h
webcit/calendar.c

index 86bd0687c9fe4caea3a9ca93aebccca24d3179d2..3f9ac42ab5acd01ca8ac6a4b6bb7e726fa760b10 100644 (file)
@@ -907,7 +907,6 @@ void begin_session(CitContext *con)
        con->download_fp = NULL;
        con->upload_fp = NULL;
        con->cached_msglist = NULL;
-       con->cached_num_msgs = 0;
        con->FirstExpressMessage = NULL;
        time(&con->lastcmd);
        time(&con->lastidle);
index 2b1cce1b0d84a457157f55a3d88c590c496ae6f0..32b306bf92f82b53d6d4557e62c42db1de6206da 100644 (file)
@@ -369,7 +369,7 @@ void RemoveContext (CitContext *con)
        FreeStrBuf(&con->MigrateBuf);
        FreeStrBuf(&con->RecvBuf.Buf);
        if (con->cached_msglist) {
-               free(con->cached_msglist);
+               seenit_free(&con->cached_msglist);
        }
 
        syslog(LOG_DEBUG, "Done with RemoveContext()\n");
index 224b468f920f208bc2f18ce006db8eb791654f7b..f7f17b6245064625d0f96806ff03c46b7cb26891 100644 (file)
@@ -135,7 +135,7 @@ struct CitContext {
        void (*h_async_function) (void) ;       /* do async msgs function */
        void (*h_greeting_function) (void) ;    /* greeting function for session startup */
 
-       long *cached_msglist;                   /* results of the previous CtdlForEachMessage() */
+       struct seenit *cached_msglist;          /* results of the previous CtdlForEachMessage() */
        int cached_num_msgs;
 };
 
index 7f337d3ab0c3b04e98aaa7f9e2d2fdf85edc9031..7b46dbee55872d098d1fdc2455e6c4dcfaee24e0 100644 (file)
@@ -593,6 +593,56 @@ void CtdlSetSeen(long *target_msgnums, int num_target_msgnums,
 }
 
 
+
+/* store a value in the binary tree */
+void seenit_store(struct seenit **si, long msgnum) {
+       struct seenit *this_si;
+
+       if (*si == NULL) {                      /* store now */
+               *si = malloc(sizeof(struct seenit));
+               this_si = *si;
+               this_si->l = NULL;
+               this_si->r = NULL;
+               this_si->msgnum = msgnum;
+               return;
+       }
+
+       this_si = *si;
+       if (msgnum < this_si->msgnum) {
+               seenit_store(&this_si->l, msgnum);
+       }
+       else if (msgnum > this_si->msgnum) {
+               seenit_store(&this_si->r, msgnum);
+       }
+       else {
+               return;
+       }
+}
+
+
+/* search for a value in the binary tree */
+int seenit_isthere(struct seenit *si, long msgnum) {
+       if (!si) return(0);     /* not there */
+       if (msgnum < si->msgnum) return(seenit_isthere(si->l, msgnum));
+       if (msgnum > si->msgnum) return(seenit_isthere(si->r, msgnum));
+       return(1);              /* found it */
+}
+
+
+/* free the binary tree */
+void seenit_free(struct seenit **si) {
+       struct seenit *this_si = *si;
+       if (!this_si) return;
+       seenit_free(&this_si->l);
+       seenit_free(&this_si->r);
+       free(this_si);
+       *si = NULL;
+}
+
+
+
+
+
 /*
  * API function to perform an operation for each qualifying message in the
  * current room.  (Returns the number of messages processed.)
@@ -644,23 +694,18 @@ int CtdlForEachMessage(int mode, long ref, char *search_string,
        cdbfr->ptr = NULL;      /* clear this so that cdb_free() doesn't free it */
        cdb_free(cdbfr);        /* we own this memory now */
 
-       /*
-        * We cache the most recent msglist in order to do security checks later
-        */
-       if (CC->client_socket > 0) {
-               if (CC->cached_msglist != NULL) {
-                       free(CC->cached_msglist);
-               }
-       
-               CC->cached_msglist = msglist;
-               CC->cached_num_msgs = num_msgs;
-       }
-
        /*
         * Now begin the traversal.
         */
        if (num_msgs > 0) for (a = 0; a < num_msgs; ++a) {
 
+               /*
+                * cache the msgnums we've seen in order to perform security checks later
+                */
+               if (CC->client_socket > 0) {
+                       seenit_store(&CC->cached_msglist, msglist[a]);
+               }
+
                /* If the caller is looking for a specific MIME type, filter
                 * out all messages which are not of the type requested.
                 */
@@ -787,7 +832,7 @@ int CtdlForEachMessage(int mode, long ref, char *search_string,
                        }
                }
        if (need_to_free_re) regfree(&re);
-       if (CC->client_socket <= 0) free(msglist);
+       free(msglist);
        return num_processed;
 }
 
@@ -1556,24 +1601,9 @@ int check_cached_msglist(long msgnum) {
        if (!CC) return om_ok;                                          /* not a session */
        if (CC->client_socket <= 0) return om_ok;                       /* not a client session */
        if (CC->cached_msglist == NULL) return om_access_denied;        /* no msglist fetched */
-       if (CC->cached_num_msgs == 0) return om_access_denied;          /* nothing to check */
-
 
-       /* Do a binary search within the cached_msglist for the requested msgnum */
-       int min = 0;
-       int max = (CC->cached_num_msgs - 1);
-
-       while (max >= min) {
-               int middle = min + (max-min) / 2 ;
-               if (msgnum == CC->cached_msglist[middle]) {
-                       return om_ok;
-               }
-               if (msgnum > CC->cached_msglist[middle]) {
-                       min = middle + 1;
-               }
-               else {
-                       max = middle - 1;
-               }
+       if (seenit_isthere(CC->cached_msglist, msgnum)) {
+               return om_ok;
        }
 
        return om_access_denied;
@@ -1632,10 +1662,7 @@ int CtdlOutputMsg(long msg_num,          /* message number (local) to fetch */
 
        r = check_cached_msglist(msg_num);
        if (r != om_ok) {
-               syslog(LOG_DEBUG, "\033[31m SECURITY CHECK FAIL \033[0m\n");
-/*
- * FIXME enable this section when the security check yields no false positives
- *
+               syslog(LOG_DEBUG, "Denying access to message %ld - not yet listed\n", msg_num);
                if (do_proto) {
                        if (r == om_access_denied) {
                                cprintf("%d Message %ld was not found in this room.\n",
@@ -1648,7 +1675,6 @@ int CtdlOutputMsg(long msg_num,           /* message number (local) to fetch */
                        }
                return(r);
                }
-*/
        }
 
        /*
index 3171fbd753754612019287b9b690f8911af07c30..084e5189b171cf5c1baeb1d7b8aedbf417b61f49 100644 (file)
@@ -237,6 +237,11 @@ int CtdlIsMe(char *addr, int addr_buf_len);
 */
 void aide_message(char *text, char *subject) __attribute__ ((deprecated));
 
+void seenit_store(struct seenit **si, long msgnum);
+int seenit_isthere(struct seenit *si, long msgnum);
+void seenit_free(struct seenit **si);
+
+
 
 /* 
  * loading messages async via an FD: 
index 70770a15a931cbde9814401bcadf3b4cf969a330..b0a1d8655a9439c30c89483fdd146990c8c9f0dd 100644 (file)
@@ -268,6 +268,14 @@ struct UseTable {
 };
 
 
+/* "seenit" is a simple binary tree storing the message pointers we've seen */
+struct seenit {
+       struct seenit *l;
+       struct seenit *r;
+       long msgnum;
+};
+
+
 
 /* Preferred field order                                                       */
 /*               **********                    Important fields                */
index fe81b959796c3af249dec7c885e3504fa01c5785..273df3bc83e0949b4ffeca5687dc5db334c2ab57 100644 (file)
@@ -806,7 +806,9 @@ void display_edit_event(void) {
 }
 
 /*
- * save an edited event
+ * Save an edited event.  If there is an older version of this event in the store,
+ * we first load it into memory and then apply the changes, so that any fields which
+ * Citael does not know how to handle will be preserved as-is.
  */
 void save_event(void) {
        long msgnum = 0L;