]> code.citadel.org Git - citadel.git/blobdiff - citadel/msgbase.c
Attempting to fix a segfault generated in Removecontext()
[citadel.git] / citadel / msgbase.c
index 7b46dbee55872d098d1fdc2455e6c4dcfaee24e0..c252f5ff0d4b57f643c2dfd5046eae96dafda2a2 100644 (file)
@@ -593,56 +593,6 @@ 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.)
@@ -699,13 +649,6 @@ int CtdlForEachMessage(int mode, long ref, char *search_string,
         */
        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.
                 */
@@ -832,7 +775,21 @@ int CtdlForEachMessage(int mode, long ref, char *search_string,
                        }
                }
        if (need_to_free_re) regfree(&re);
-       free(msglist);
+
+       /*
+        * 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;
+       }
+       else {
+               free(msglist);
+       }
+
        return num_processed;
 }
 
@@ -1601,9 +1558,24 @@ 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 */
+
 
-       if (seenit_isthere(CC->cached_msglist, msgnum)) {
-               return om_ok;
+       /* 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;
+               }
        }
 
        return om_access_denied;
@@ -1660,21 +1632,28 @@ int CtdlOutputMsg(long msg_num,         /* message number (local) to fetch */
                return(r);
        }
 
+       /*
+        * Check to make sure the message is actually IN this room
+        */
        r = check_cached_msglist(msg_num);
+       if (r == om_access_denied) {
+               /* Not in the cache?  We get ONE shot to check it again. */
+               CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL, NULL, NULL);
+               r = check_cached_msglist(msg_num);
+       }
        if (r != om_ok) {
-               syslog(LOG_DEBUG, "Denying access to message %ld - not yet listed\n", msg_num);
+               syslog(LOG_DEBUG, "Security check fail: message %ld is not in %s\n",
+                       msg_num, CC->room.QRname
+               );
                if (do_proto) {
                        if (r == om_access_denied) {
-                               cprintf("%d Message %ld was not found in this room.\n",
-                                       ERROR + MESSAGE_NOT_FOUND,
+                               cprintf("%d message %ld was not found in this room\n",
+                                       ERROR + HIGHER_ACCESS_REQUIRED,
                                        msg_num
                                );
                        }
-                       else {
-                               cprintf("%d An unknown error has occurred.\n", ERROR);
-                       }
-               return(r);
                }
+               return(r);
        }
 
        /*
@@ -2819,7 +2798,11 @@ long send_message(struct CtdlMessage *msg) {
 
        /* Get a new message number */
        newmsgid = get_new_message_number();
-       snprintf(msgidbuf, sizeof msgidbuf, "%010ld@%s", newmsgid, config.c_fqdn);
+       snprintf(msgidbuf, sizeof msgidbuf, "%08lX-%08lX@%s",
+               (long unsigned int) time(NULL),
+               (long unsigned int) newmsgid,
+               config.c_fqdn
+       );
 
        /* Generate an ID if we don't have one already */
        if (msg->cm_fields['I']==NULL) {