]> code.citadel.org Git - citadel.git/blobdiff - citadel/euidindex.c
Updating cmd_euid() to use the CtdlForEachMessage() API fixes the security check...
[citadel.git] / citadel / euidindex.c
index ce9ebd350d1f22ae4b14772c7570b675d0b704e0..a1d5ec960c69424968bb80098197cfd1e7079bd8 100644 (file)
@@ -1,8 +1,5 @@
 /*
- * $Id$
- *
  * Index messages by EUID per room.
- *
  */
 
 #include "sysdep.h"
@@ -44,6 +41,8 @@
 #include "control.h"
 #include "euidindex.h"
 
+#include "ctdl_module.h"
+
 /*
  * The structure of an euidindex record *key* is:
  *
@@ -70,6 +69,7 @@ int DoesThisRoomNeedEuidIndexing(struct ctdlroom *qrbuf) {
                case VIEW_BBS:          return(0);
                case VIEW_MAILBOX:      return(0);
                case VIEW_ADDRESSBOOK:  return(1);
+               case VIEW_DRAFTS:       return(0);
                case VIEW_CALENDAR:     return(1);
                case VIEW_TASKS:        return(1);
                case VIEW_NOTES:        return(1);
@@ -89,12 +89,16 @@ int DoesThisRoomNeedEuidIndexing(struct ctdlroom *qrbuf) {
  * its message number.
  */
 long locate_message_by_euid(char *euid, struct ctdlroom *qrbuf) {
+       return CtdlLocateMessageByEuid (euid, qrbuf);
+}
+
+long CtdlLocateMessageByEuid(char *euid, struct ctdlroom *qrbuf) {
        char *key;
        int key_len;
        struct cdbdata *cdb_euid;
        long msgnum = (-1L);
 
-       CtdlLogPrintf(CTDL_DEBUG, "Searching for EUID <%s> in <%s>\n", euid, qrbuf->QRname);
+       syslog(LOG_DEBUG, "Searching for EUID <%s> in <%s>\n", euid, qrbuf->QRname);
 
        key_len = strlen(euid) + sizeof(long) + 1;
        key = malloc(key_len);
@@ -114,7 +118,7 @@ long locate_message_by_euid(char *euid, struct ctdlroom *qrbuf) {
                memcpy(&msgnum, cdb_euid->ptr, sizeof(long));
                cdb_free(cdb_euid);
        }
-       CtdlLogPrintf(CTDL_DEBUG, "returning msgnum = %ld\n", msgnum);
+       syslog(LOG_DEBUG, "returning msgnum = %ld\n", msgnum);
        return(msgnum);
 }
 
@@ -129,7 +133,7 @@ void index_message_by_euid(char *euid, struct ctdlroom *qrbuf, long msgnum) {
        char *data;
        int data_len;
 
-       CtdlLogPrintf(CTDL_DEBUG, "Indexing message #%ld <%s> in <%s>\n", msgnum, euid, qrbuf->QRname);
+       syslog(LOG_DEBUG, "Indexing message #%ld <%s> in <%s>\n", msgnum, euid, qrbuf->QRname);
 
        key_len = strlen(euid) + sizeof(long) + 1;
        key = malloc(key_len);
@@ -169,7 +173,7 @@ void rebuild_euid_index_for_room(struct ctdlroom *qrbuf, void *data) {
        struct RoomProcList *ptr;
        struct ctdlroom qr;
 
-       /* Lazy programming here.  Call this function as a ForEachRoom backend
+       /* Lazy programming here.  Call this function as a CtdlForEachRoom backend
         * in order to queue up the room names, or call it with a null room
         * to make it do the processing.
         */
@@ -185,12 +189,12 @@ void rebuild_euid_index_for_room(struct ctdlroom *qrbuf, void *data) {
        }
 
        while (rplist != NULL) {
-               if (getroom(&qr, rplist->name) == 0) {
+               if (CtdlGetRoom(&qr, rplist->name) == 0) {
                        if (DoesThisRoomNeedEuidIndexing(&qr)) {
-                               CtdlLogPrintf(CTDL_DEBUG,
+                               syslog(LOG_DEBUG,
                                        "Rebuilding EUID index for <%s>\n",
                                        rplist->name);
-                               usergoto(rplist->name, 0, 0, NULL, NULL);
+                               CtdlUserGoto(rplist->name, 0, 0, NULL, NULL);
                                CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL,
                                        rebuild_euid_index_for_msg, NULL);
                        }
@@ -207,47 +211,60 @@ void rebuild_euid_index_for_room(struct ctdlroom *qrbuf, void *data) {
  */
 void rebuild_euid_index(void) {
        cdb_trunc(CDB_EUIDINDEX);               /* delete the old indices */
-       ForEachRoom(rebuild_euid_index_for_room, NULL); /* enumerate rm names */
+       CtdlForEachRoom(rebuild_euid_index_for_room, NULL);     /* enumerate rm names */
        rebuild_euid_index_for_room(NULL, NULL);        /* and index them */
 }
 
 
 
+struct euid_callback {
+       long msgnum;
+       int found_it;
+};
+
+/*
+ * callback for cmd_euid
+ */
+void euid_is_msg_in_room(long msgnum, void *userdata) {
+       struct euid_callback *ec = (struct euid_callback *) userdata;
+
+       if (msgnum == ec->msgnum) ec->found_it = 1;
+}
+
+
 /*
  * Server command to fetch a message number given an euid.
  */
 void cmd_euid(char *cmdbuf) {
        char euid[256];
        long msgnum;
-        struct cdbdata *cdbfr;
-        long *msglist = NULL;
-        int num_msgs = 0;
-       int i;
+       struct euid_callback ec;
 
-       if (CtdlAccessCheck(ac_logged_in)) return;
+       if (CtdlAccessCheck(ac_logged_in_or_guest)) return;
 
        extract_token(euid, cmdbuf, 0, '|', sizeof euid);
-       msgnum = locate_message_by_euid(euid, &CC->room);
+       msgnum = CtdlLocateMessageByEuid(euid, &CC->room);
        if (msgnum <= 0L) {
                cprintf("%d not found\n", ERROR + MESSAGE_NOT_FOUND);
                return;
        }
 
-        cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
-       if (cdbfr != NULL) {
-                num_msgs = cdbfr->len / sizeof(long);
-                msglist = (long *) cdbfr->ptr;
-                for (i = 0; i < num_msgs; ++i) {
-                        if (msglist[i] == msgnum) {
-                               cdb_free(cdbfr);
-                               cprintf("%d %ld\n", CIT_OK, msgnum);
-                               return;
-                       }
-               }
-                cdb_free(cdbfr);
-       }
+       ec.msgnum = msgnum;
+       ec.found_it = 0;
+       CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL, euid_is_msg_in_room, (void *)&ec);
 
+       if (ec.found_it) {
+               cprintf("%d %ld\n", CIT_OK, msgnum);
+               return;
+       }
        cprintf("%d not found\n", ERROR + MESSAGE_NOT_FOUND);
 }
 
-
+CTDL_MODULE_INIT(euidindex)
+{
+       if (!threading) {
+               CtdlRegisterProtoHook(cmd_euid, "EUID", "Fetch the msgnum associated with an EUID");
+       }
+       /* return our Subversion id for the Log */
+       return "euidindex";
+}