Fix warnings all over citserver; handle function replies; remove unused code.
[citadel.git] / citadel / msgbase.c
index 5ec0801d20038b45cd3f2b3ebc768081bb99452b..7eb321905315c9b2313e81cf0639729cb8891ebe 100644 (file)
@@ -1,9 +1,9 @@
 /*
  * Implements the message store.
  *
- * Copyright (c) 1987-2010 by the citadel.org team
+ * Copyright (c) 1987-2011 by the citadel.org team
  *
- * This program is free software; you can redistribute it and/or modify
+ * This program is open source software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 3 of the License, or
  * (at your option) any later version.
@@ -15,7 +15,7 @@
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  */
 
 #include "sysdep.h"
@@ -633,14 +633,16 @@ int CtdlForEachMessage(int mode, long ref, char *search_string,
 
        /* Load the message list */
        cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
-       if (cdbfr != NULL) {
-               msglist = (long *) cdbfr->ptr;
-               num_msgs = cdbfr->len / sizeof(long);
-       } else {
+       if (cdbfr == NULL) {
                if (need_to_free_re) regfree(&re);
                return 0;       /* No messages at all?  No further action. */
        }
 
+       msglist = (long *) cdbfr->ptr;
+       num_msgs = cdbfr->len / sizeof(long);
+
+       cdbfr->ptr = NULL;      /* clear this so that cdb_free() doesn't free it */
+       cdb_free(cdbfr);        /* we own this memory now */
 
        /*
         * Now begin the traversal.
@@ -772,8 +774,22 @@ int CtdlForEachMessage(int mode, long ref, char *search_string,
                                ++num_processed;
                        }
                }
-       cdb_free(cdbfr);        /* Clean up */
        if (need_to_free_re) regfree(&re);
+
+       /*
+        * 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;
 }
 
@@ -1090,6 +1106,10 @@ void mime_download(char *name, char *filename, char *partnum, char *disp,
                        return;
        
                rv = fwrite(content, length, 1, CC->download_fp);
+               if (rv == -1) {
+                       syslog(LOG_EMERG, "mime_download(): Couldn't write: %s\n",
+                              strerror(errno));
+               }
                fflush(CC->download_fp);
                rewind(CC->download_fp);
        
@@ -1125,49 +1145,6 @@ void mime_spew_section(char *name, char *filename, char *partnum, char *disp,
        }
 }
 
-#ifdef MESSAGE_IN_ROOM
-/*
- * Check if a message is in the current room.
- * This is used by CtdlFetchMessage to prevent random picking
- * of messages from users private rooms
- *
- * The message list should probably be cached against the CC->room
- */
-int CtdlMessageInRoom(long msgnum)
-{
-       visit vbuf;
-       struct cdbdata *cdbfr;
-
-       /* Learn about the user and room in question */
-       CtdlGetUser(&CC->user, CC->curr_user);
-       CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
-
-       /* Load the message list */
-       cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
-       if (cdbfr != NULL) {
-               long *msglist = NULL;
-               int num_msgs = 0;
-               int i;
-               int r = 0;
-               
-               msglist = (long *) cdbfr->ptr;
-               num_msgs = cdbfr->len / sizeof(long);
-
-               /* search for message msgnum */
-               for (i=0; i<num_msgs; i++) {
-                       if (msglist[i] == msgnum) {
-                               r = 1;
-                               break;
-                       }       
-               }
-
-               cdb_free(cdbfr);
-               return r;
-       } else {
-               return 0;
-       }
-}
-#endif
 
 /*
  * Load a message from disk into memory.
@@ -1186,14 +1163,6 @@ struct CtdlMessage *CtdlFetchMessage(long msgnum, int with_body)
        cit_uint8_t field_header;
 
        syslog(LOG_DEBUG, "CtdlFetchMessage(%ld, %d)\n", msgnum, with_body);
-
-#ifdef MESSAGE_IN_ROOM
-       if (!CtdlMessageInRoom(msgnum)) {
-               syslog(LOG_DEBUG, "Message %ld not in current room\n", msgnum);
-               return NULL;
-       }
-#endif
-
        dmsgtext = cdb_fetch(CDB_MSGMAIN, &msgnum, sizeof(long));
        if (dmsgtext == NULL) {
                return NULL;
@@ -1583,6 +1552,40 @@ void extract_encapsulated_message(char *name, char *filename, char *partnum, cha
 }
 
 
+/*
+ * Determine whether the specified message exists in the cached_msglist
+ * (This is a security check)
+ */
+int check_cached_msglist(long msgnum) {
+
+       /* cases in which we skip the check */
+       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;
+               }
+       }
+
+       return om_access_denied;
+}
+
+
 /* 
  * Determine whether the currently logged in session has permission to read
  * messages in the current room.
@@ -1633,14 +1636,29 @@ int CtdlOutputMsg(long msg_num,         /* message number (local) to fetch */
                return(r);
        }
 
-#ifdef MESSAGE_IN_ROOM
-       if (!CtdlMessageInRoom(msg_num)) {
-               syslog(LOG_DEBUG, "Message %ld not in current room\n", msg_num);
-               if (do_proto) cprintf("%d Can't locate msg %ld in room\n",
-                       ERROR + MESSAGE_NOT_FOUND, msg_num);
-               return(om_no_such_msg);
+       /*
+        * 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, "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 + HIGHER_ACCESS_REQUIRED,
+                                       msg_num
+                               );
+                       }
+               }
+               return(r);
        }
-#endif
 
        /*
         * Fetch the message from disk.  If we're in HEADERS_FAST mode,
@@ -1941,6 +1959,7 @@ void OutputRFC822MsgHeaders(
        int i, j, k;
        char *mptr = NULL;
        char *mpptr = NULL;
+       char *hptr;
 
        for (i = 0; i < 256; ++i) {
                if (TheMessage->cm_fields[i]) {
@@ -1966,7 +1985,11 @@ void OutputRFC822MsgHeaders(
                        else if (i == 'V') {
                                if ((flags & QP_EADDR) != 0) 
                                        mptr = qp_encode_email_addrs(mptr);
-                               cprintf("Envelope-To: %s%s", mptr, nl);
+                               hptr = mptr;
+                               while ((*hptr != '\0') && isspace(*hptr))
+                                       hptr ++;
+                               if (!IsEmptyStr(hptr))
+                                       cprintf("Envelope-To: %s%s", hptr, nl);
                        }
                        else if (i == 'U') {
                                cprintf("Subject: %s%s", mptr, nl);
@@ -2019,7 +2042,11 @@ void OutputRFC822MsgHeaders(
                                }
                        }
                        else if (i == 'K') {
-                               cprintf("Reply-To: <%s>%s", mptr, nl);
+                               hptr = mptr;
+                               while ((*hptr != '\0') && isspace(*hptr))
+                                       hptr ++;
+                               if (!IsEmptyStr(hptr))
+                                       cprintf("Reply-To: %s%s", mptr, nl);
                        }
                        if (mptr != mpptr)
                                free (mptr);
@@ -2045,7 +2072,6 @@ void Dump_RFC822HeadersBody(
        int outlen = 0;
        int nllen = strlen(nl);
        char *mptr;
-       int rc;
 
        mptr = TheMessage->cm_fields['M'];
 
@@ -2105,7 +2131,7 @@ void Dump_RFC822HeadersBody(
                }
        }
        if (outlen > 0) {
-               rc = client_write(outbuf, outlen);
+               client_write(outbuf, outlen);
                outlen = 0;
        }
 }
@@ -2784,7 +2810,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) {
@@ -2907,7 +2937,6 @@ void serialize_message(struct ser_ret *ret,               /* return values */
 void dump_message(struct CtdlMessage *msg,     /* unserialized msg */
                  long Siz)                     /* how many chars ? */
 {
-       size_t wlen;
        int i;
        static char *forder = FORDER;
        char *buf;
@@ -2922,8 +2951,6 @@ void dump_message(struct CtdlMessage *msg,        /* unserialized msg */
 
        buf = (char*) malloc (Siz + 1);
 
-       wlen = 3;
-       
        for (i=0; i<26; ++i) if (msg->cm_fields[(int)forder[i]] != NULL) {
                        snprintf (buf, Siz, " msg[%c] = %s ...\n", (char) forder[i], 
                                   msg->cm_fields[(int)forder[i]]);
@@ -3281,6 +3308,10 @@ long CtdlSubmitMsg(struct CtdlMessage *msg,      /* message to save */
                        network_fp = fopen(submit_filename, "wb+");
                        if (network_fp != NULL) {
                                rv = fwrite(smr.ser, smr.len, 1, network_fp);
+                               if (rv == -1) {
+                                       syslog(LOG_EMERG, "CtdlSubmitMsg(): Couldn't write network spool file: %s\n",
+                                              strerror(errno));
+                               }
                                fclose(network_fp);
                        }
                        free(smr.ser);
@@ -3921,30 +3952,36 @@ int CtdlDoIHavePermissionToPostInThisRoom(
 
        if ((CC->user.axlevel < AxProbU)
            && ((CC->room.QRflags & QR_MAILBOX) == 0)) {
-               snprintf(errmsgbuf, n, "Need to be validated to enter "
-                               "(except in %s> to sysop)", MAILROOM);
+               snprintf(errmsgbuf, n, "Need to be validated to enter (except in %s> to sysop)", MAILROOM);
                return (ERROR + HIGHER_ACCESS_REQUIRED);
        }
 
        CtdlRoomAccess(&CC->room, &CC->user, &ra, NULL);
 
-       if ( (!(ra & UA_POSTALLOWED)) && (ra & UA_REPLYALLOWED) && (!is_reply) ) {
+       if (ra & UA_POSTALLOWED) {
+               strcpy(errmsgbuf, "OK to post or reply here");
+               return(0);
+       }
+
+       if ( (ra & UA_REPLYALLOWED) && (is_reply) ) {
                /*
                 * To be thorough, we ought to check to see if the message they are
                 * replying to is actually a valid one in this room, but unless this
                 * actually becomes a problem we'll go with high performance instead.
                 */
-               snprintf(errmsgbuf, n, "You may only reply to existing messages here.");
-               return (ERROR + HIGHER_ACCESS_REQUIRED);
+               strcpy(errmsgbuf, "OK to reply here");
+               return(0);
        }
 
-       else if (!(ra & UA_POSTALLOWED)) {
-               snprintf(errmsgbuf, n, "Higher access is required to post in this room.");
+       if ( (ra & UA_REPLYALLOWED) && (!is_reply) ) {
+               /* Clarify what happened with a better error message */
+               snprintf(errmsgbuf, n, "You may only reply to existing messages here.");
                return (ERROR + HIGHER_ACCESS_REQUIRED);
        }
 
-       strcpy(errmsgbuf, "Ok");
-       return(0);
+       snprintf(errmsgbuf, n, "Higher access is required to post in this room.");
+       return (ERROR + HIGHER_ACCESS_REQUIRED);
+
 }
 
 
@@ -4935,6 +4972,11 @@ void AdjRefCount(long msgnum, int incr)
        new_arcq.arcq_msgnum = msgnum;
        new_arcq.arcq_delta = incr;
        rv = fwrite(&new_arcq, sizeof(struct arcq), 1, arcfp);
+       if (rv == -1) {
+               syslog(LOG_EMERG, "Couldn't write Refcount Queue File %s: %s\n",
+                      file_arcq,
+                      strerror(errno));
+       }
        fflush(arcfp);
 
        return;