]> code.citadel.org Git - citadel.git/blobdiff - citadel/serv_imap.c
* Renamed "struct user" to "struct ctdluser"
[citadel.git] / citadel / serv_imap.c
index 5d9e119ef34228e3193696590fbc2242727dc41a..07c289219ad64abeea1eb17450fd15427559fd41 100644 (file)
@@ -11,7 +11,6 @@
  *
  * WARNING: Mark Crispin is an idiot.  IMAP is the most brain-damaged protocol
  * you will ever have the profound lack of pleasure to encounter.
- * 
  */
 
 #include "sysdep.h"
 #include <ctype.h>
 #include <string.h>
 #include <limits.h>
+
+#ifdef HAVE_OPENSSL
+#include <openssl/ssl.h>
+#include <openssl/err.h>
+#include <openssl/rand.h>
+#endif
+
 #include "citadel.h"
 #include "server.h"
 #include "sysdep_decls.h"
 #include "citserver.h"
 #include "support.h"
 #include "config.h"
-#include "dynloader.h"
+#include "serv_extensions.h"
 #include "room_ops.h"
 #include "user_ops.h"
 #include "policy.h"
 #include "imap_store.h"
 #include "imap_misc.h"
 
+#ifdef HAVE_OPENSSL
+#include "serv_crypto.h"
+#endif
+
+/* imap_rename() uses this struct containing list of rooms to rename */
+struct irl {
+       struct irl *next;
+       char irl_oldroom[ROOMNAMELEN];
+       char irl_newroom[ROOMNAMELEN];
+       int irl_newfloor;
+};
+
+/* Data which is passed between imap_rename() and imap_rename_backend() */
+struct irlparms { 
+       char *oldname;
+       char *newname;
+       struct irl **irl;
+};
 
 long SYM_IMAP;
 
@@ -99,7 +123,7 @@ void imap_set_seen_flags(void) {
        struct visit vbuf;
        int i;
 
-       CtdlGetRelationship(&vbuf, &CC->usersupp, &CC->quickroom);
+       CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
        if (IMAP->num_msgs > 0) {
                for (i=0; i<IMAP->num_msgs; ++i) {
                        if (is_msg_in_mset(vbuf.v_seen, IMAP->msgids[i])) {
@@ -171,14 +195,31 @@ void imap_rescan_msgids(void) {
 
        int original_num_msgs = 0;
        long original_highest = 0L;
-       int i;
-       int count;
+       int i, j;
+       int message_still_exists;
+       struct cdbdata *cdbfr;
+       long *msglist = NULL;
+       int num_msgs = 0;
+
 
        if (IMAP->selected == 0) {
                lprintf(5, "imap_load_msgids() can't run; no room selected\n");
                return;
        }
 
+       /* Load the *current* message list from disk, so we can compare it
+        * to what we have in memory.
+        */
+       cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
+       if (cdbfr != NULL) {
+               msglist = mallok(cdbfr->len);
+               memcpy(msglist, cdbfr->ptr, cdbfr->len);
+               num_msgs = cdbfr->len / sizeof(long);
+               cdb_free(cdbfr);
+       }
+       else {
+               num_msgs = 0;
+       }
 
        /*
         * Check to see if any of the messages we know about have been expunged
@@ -186,10 +227,14 @@ void imap_rescan_msgids(void) {
        if (IMAP->num_msgs > 0)
         for (i=0; i<IMAP->num_msgs; ++i) {
 
-               count = CtdlForEachMessage(MSGS_EQ, IMAP->msgids[i],
-                       NULL, NULL, NULL, NULL);
+               message_still_exists = 0;
+               if (num_msgs > 0) for (j = 0; j < num_msgs; ++j) {
+                       if (msglist[j] == IMAP->msgids[i]) {
+                               message_still_exists = 1;
+                       }
+               }
 
-               if (count == 0) {
+               if (message_still_exists == 0) {
                        cprintf("* %d EXPUNGE\r\n", i+1);
 
                        /* Here's some nice stupid nonsense.  When a message
@@ -221,9 +266,11 @@ void imap_rescan_msgids(void) {
        /*
         * Now peruse the room for *new* messages only.
         */
-       CtdlForEachMessage(MSGS_GT, original_highest, NULL, NULL,
-               imap_add_single_msgid, NULL);
-
+       if (num_msgs > 0) for (j=0; j<num_msgs; ++j) {
+               if (msglist[j] > original_highest) {
+                       imap_add_single_msgid(msglist[j], NULL);
+               }
+       }
        imap_set_seen_flags();
 
        /*
@@ -233,6 +280,7 @@ void imap_rescan_msgids(void) {
                cprintf("* %d EXISTS\r\n", IMAP->num_msgs);
        }
 
+       if (num_msgs != 0) phree(msglist);
 }
 
 
@@ -299,7 +347,7 @@ void imap_authenticate(int num_parms, char *parms[]) {
        }
 
        if (!strcasecmp(parms[2], "LOGIN")) {
-               encode_base64(buf, "Username:");
+               CtdlEncodeBase64(buf, "Username:", 9);
                cprintf("+ %s\r\n", buf);
                IMAP->authstate = imap_as_expecting_username;
                strcpy(IMAP->authseq, parms[0]);
@@ -317,7 +365,7 @@ void imap_auth_login_user(char *cmd) {
 
        CtdlDecodeBase64(buf, cmd, SIZ);
        CtdlLoginExistingUser(buf);
-       encode_base64(buf, "Password:");
+       CtdlEncodeBase64(buf, "Password:", 9);
        cprintf("+ %s\r\n", buf);
        IMAP->authstate = imap_as_expecting_password;
        return;
@@ -343,11 +391,68 @@ void imap_auth_login_pass(char *cmd) {
  * implements the CAPABILITY command
  */
 void imap_capability(int num_parms, char *parms[]) {
-       cprintf("* CAPABILITY IMAP4 IMAP4REV1 AUTH=LOGIN\r\n");
+       cprintf("* CAPABILITY IMAP4 IMAP4REV1 AUTH=LOGIN");
+#ifdef HAVE_OPENSSL
+       cprintf(" STARTTLS");
+#endif
+       cprintf("\r\n");
        cprintf("%s OK CAPABILITY completed\r\n", parms[0]);
 }
 
 
+/*
+ * implements the STARTTLS command
+ */
+#ifdef HAVE_OPENSSL
+void imap_starttls(int num_parms, char *parms[]) {
+       int retval, bits, alg_bits;
+
+       if (!ssl_ctx) {
+               cprintf("%s NO No SSL_CTX available\r\n", parms[0]);
+               return;
+       }
+       if (!(CC->ssl = SSL_new(ssl_ctx))) {
+               lprintf(2, "SSL_new failed: %s\n",
+                               ERR_reason_error_string(ERR_peek_error()));
+               cprintf("%s NO SSL_new: %s\r\n", parms[0],
+                               ERR_reason_error_string(ERR_get_error()));
+               return;
+       }
+       if (!(SSL_set_fd(CC->ssl, CC->client_socket))) {
+               lprintf(2, "SSL_set_fd failed: %s\n",
+                               ERR_reason_error_string(ERR_peek_error()));
+               SSL_free(CC->ssl);
+               CC->ssl = NULL;
+               cprintf("%s NO SSL_set_fd: %s\r\n", parms[0],
+                               ERR_reason_error_string(ERR_get_error()));
+               return;
+       }
+       cprintf("%s OK begin TLS negotiation now\r\n", parms[0]);
+       retval = SSL_accept(CC->ssl);
+       if (retval < 1) {
+               /*
+                * Can't notify the client of an error here; they will
+                * discover the problem at the SSL layer and should
+                * revert to unencrypted communications.
+                */
+               long errval;
+
+               errval = SSL_get_error(CC->ssl, retval);
+               lprintf(2, "SSL_accept failed: %s\n",
+                               ERR_reason_error_string(ERR_get_error()));
+               SSL_free(CC->ssl);
+               CC->ssl = NULL;
+               return;
+       }
+       BIO_set_close(CC->ssl->rbio, BIO_NOCLOSE);
+       bits = SSL_CIPHER_get_bits(SSL_get_current_cipher(CC->ssl), &alg_bits);
+       lprintf(3, "SSL/TLS using %s on %s (%d of %d bits)\n",
+                       SSL_CIPHER_get_name(SSL_get_current_cipher(CC->ssl)),
+                       SSL_CIPHER_get_version(SSL_get_current_cipher(CC->ssl)),
+                       bits, alg_bits);
+       CC->redirect_ssl = 1;
+}
+#endif
 
 
 
@@ -360,7 +465,7 @@ void imap_select(int num_parms, char *parms[]) {
        int c = 0;
        int ok = 0;
        int ra = 0;
-       struct quickroom QRscratch;
+       struct ctdlroom QRscratch;
        int msgs, new;
        int floornum;
        int roomflags;
@@ -382,7 +487,7 @@ void imap_select(int num_parms, char *parms[]) {
         /* Then try a mailbox name match */
         if (c != 0) {
                 MailboxName(augmented_roomname, sizeof augmented_roomname,
-                           &CC->usersupp, towhere);
+                           &CC->user, towhere);
                 c = getroom(&QRscratch, augmented_roomname);
                 if (c == 0)
                         strcpy(towhere, augmented_roomname);
@@ -391,7 +496,7 @@ void imap_select(int num_parms, char *parms[]) {
        /* If the room exists, check security/access */
         if (c == 0) {
                 /* See if there is an existing user/room relationship */
-                ra = CtdlRoomAccess(&QRscratch, &CC->usersupp);
+                ra = CtdlRoomAccess(&QRscratch, &CC->user);
 
                 /* normal clients have to pass through security */
                 if (ra & UA_KNOWN) {
@@ -411,7 +516,7 @@ void imap_select(int num_parms, char *parms[]) {
         * usergoto() formally takes us to the desired room, happily returning
         * the number of messages and number of new messages.
         */
-       memcpy(&CC->quickroom, &QRscratch, sizeof(struct quickroom));
+       memcpy(&CC->room, &QRscratch, sizeof(struct ctdlroom));
        usergoto(NULL, 0, 0, &msgs, &new);
        IMAP->selected = 1;
 
@@ -447,9 +552,13 @@ int imap_do_expunge(void) {
 
        if (IMAP->num_msgs > 0) for (i=0; i<IMAP->num_msgs; ++i) {
                if (IMAP->flags[i] & IMAP_DELETED) {
-                       CtdlDeleteMessages(CC->quickroom.QRname,
+                       CtdlDeleteMessages(CC->room.QRname,
                                        IMAP->msgids[i], "");
                        ++num_expunged;
+                       lprintf(9, "%ld ... deleted\n", IMAP->msgids[i]);
+               }
+               else {
+                       lprintf(9, "%ld ... not deleted\n", IMAP->msgids[i]);
                }
        }
 
@@ -466,7 +575,8 @@ int imap_do_expunge(void) {
  */
 void imap_expunge(int num_parms, char *parms[]) {
        int num_expunged = 0;
-       imap_do_expunge();
+
+       num_expunged = imap_do_expunge();
        cprintf("%s OK expunged %d messages.\r\n", parms[0], num_expunged);
 }
 
@@ -515,7 +625,7 @@ void imap_list_floors(char *cmd, char *pattern) {
  * IMAP "subscribed folder" is equivocated to Citadel "known rooms."  This
  * may or may not be the desired behavior in the future.
  */
-void imap_lsub_listroom(struct quickroom *qrbuf, void *data) {
+void imap_lsub_listroom(struct ctdlroom *qrbuf, void *data) {
        char buf[SIZ];
        int ra;
        char *pattern;
@@ -523,7 +633,7 @@ void imap_lsub_listroom(struct quickroom *qrbuf, void *data) {
        pattern = (char *)data;
 
        /* Only list rooms to which the user has access!! */
-       ra = CtdlRoomAccess(qrbuf, &CC->usersupp);
+       ra = CtdlRoomAccess(qrbuf, &CC->user);
        if (ra & UA_KNOWN) {
                imap_mailboxname(buf, sizeof buf, qrbuf);
                if (imap_mailbox_matches_pattern(pattern, buf)) {
@@ -563,7 +673,7 @@ void imap_lsub(int num_parms, char *parms[]) {
 /*
  * Back end for imap_list()
  */
-void imap_list_listroom(struct quickroom *qrbuf, void *data) {
+void imap_list_listroom(struct ctdlroom *qrbuf, void *data) {
        char buf[SIZ];
        int ra;
        char *pattern;
@@ -571,7 +681,7 @@ void imap_list_listroom(struct quickroom *qrbuf, void *data) {
        pattern = (char *)data;
 
        /* Only list rooms to which the user has access!! */
-       ra = CtdlRoomAccess(qrbuf, &CC->usersupp);
+       ra = CtdlRoomAccess(qrbuf, &CC->user);
        if ( (ra & UA_KNOWN) 
          || ((ra & UA_GOTOALLOWED) && (ra & UA_ZAPPED))) {
                imap_mailboxname(buf, sizeof buf, qrbuf);
@@ -620,6 +730,11 @@ void imap_create(int num_parms, char *parms[]) {
        int flags;
        int newroomtype;
 
+       if (strchr(parms[2], '\\') != NULL) {
+               cprintf("%s NO Invalid character in folder name\r\n", parms[0]);
+               return;
+       }
+
        ret = imap_roomname(roomname, sizeof roomname, parms[2]);
        if (ret < 0) {
                cprintf("%s NO Invalid mailbox name or location\r\n",
@@ -658,7 +773,7 @@ int imap_grabroom(char *returned_roomname, char *foldername) {
        char augmented_roomname[ROOMNAMELEN];
        char roomname[ROOMNAMELEN];
        int c;
-       struct quickroom QRscratch;
+       struct ctdlroom QRscratch;
        int ra;
        int ok = 0;
 
@@ -673,7 +788,7 @@ int imap_grabroom(char *returned_roomname, char *foldername) {
         /* Then try a mailbox name match */
         if (c != 0) {
                 MailboxName(augmented_roomname, sizeof augmented_roomname,
-                           &CC->usersupp, roomname);
+                           &CC->user, roomname);
                 c = getroom(&QRscratch, augmented_roomname);
                 if (c == 0)
                         strcpy(roomname, augmented_roomname);
@@ -682,7 +797,7 @@ int imap_grabroom(char *returned_roomname, char *foldername) {
        /* If the room exists, check security/access */
         if (c == 0) {
                 /* See if there is an existing user/room relationship */
-                ra = CtdlRoomAccess(&QRscratch, &CC->usersupp);
+                ra = CtdlRoomAccess(&QRscratch, &CC->user);
 
                 /* normal clients have to pass through security */
                 if (ra & UA_KNOWN) {
@@ -726,7 +841,7 @@ void imap_status(int num_parms, char *parms[]) {
         * folder is selected, save its name so we can return there!!!!!)
         */
        if (IMAP->selected) {
-               strcpy(savedroom, CC->quickroom.QRname);
+               strcpy(savedroom, CC->room.QRname);
        }
        usergoto(roomname, 0, 0, &msgs, &new);
 
@@ -736,7 +851,7 @@ void imap_status(int num_parms, char *parms[]) {
         * names and simply spew all possible data items.  It's far easier to
         * code and probably saves us some processing time too.
         */
-       imap_mailboxname(buf, sizeof buf, &CC->quickroom);
+       imap_mailboxname(buf, sizeof buf, &CC->room);
        cprintf("* STATUS ");
        imap_strout(buf);
        cprintf(" (MESSAGES %d ", msgs);
@@ -783,7 +898,7 @@ void imap_subscribe(int num_parms, char *parms[]) {
         * we're looking for.
         */
        if (IMAP->selected) {
-               strcpy(savedroom, CC->quickroom.QRname);
+               strcpy(savedroom, CC->room.QRname);
        }
        usergoto(roomname, 0, 0, &msgs, &new);
 
@@ -820,7 +935,7 @@ void imap_unsubscribe(int num_parms, char *parms[]) {
         * usergoto() formally takes us to the desired room.
         */
        if (IMAP->selected) {
-               strcpy(savedroom, CC->quickroom.QRname);
+               strcpy(savedroom, CC->room.QRname);
        }
        usergoto(roomname, 0, 0, &msgs, &new);
 
@@ -869,16 +984,16 @@ void imap_delete(int num_parms, char *parms[]) {
         * folder is selected, save its name so we can return there!!!!!)
         */
        if (IMAP->selected) {
-               strcpy(savedroom, CC->quickroom.QRname);
+               strcpy(savedroom, CC->room.QRname);
        }
        usergoto(roomname, 0, 0, &msgs, &new);
 
        /*
         * Now delete the room.
         */
-       if (CtdlDoIHavePermissionToDeleteThisRoom(&CC->quickroom)) {
+       if (CtdlDoIHavePermissionToDeleteThisRoom(&CC->room)) {
                cprintf("%s OK DELETE completed\r\n", parms[0]);
-               delete_room(&CC->quickroom);
+               delete_room(&CC->room);
        }
        else {
                cprintf("%s NO Can't delete this folder.\r\n", parms[0]);
@@ -894,6 +1009,43 @@ void imap_delete(int num_parms, char *parms[]) {
 }
 
 
+/*
+ * Back end function for imap_rename()
+ */
+void imap_rename_backend(struct ctdlroom *qrbuf, void *data) {
+       char foldername[SIZ];
+       char newfoldername[SIZ];
+       char newroomname[ROOMNAMELEN];
+       int newfloor = 0;
+       struct irl *irlp = NULL;        /* scratch pointer */
+       struct irlparms *irlparms;
+
+       irlparms = (struct irlparms *)data;
+       imap_mailboxname(foldername, sizeof foldername, qrbuf);
+
+       /* Rename subfolders */
+       if ( (!strncasecmp(foldername, irlparms->oldname,
+          strlen(irlparms->oldname))
+          && (foldername[strlen(irlparms->oldname)] == '|')) ) {
+
+               sprintf(newfoldername, "%s|%s",
+                       irlparms->newname,
+                       &foldername[strlen(irlparms->oldname)+1]
+               );
+
+               newfloor = imap_roomname(newroomname,
+                       sizeof newroomname, newfoldername) & 0xFF;
+
+               irlp = (struct irl *) mallok(sizeof(struct irl));
+               strcpy(irlp->irl_newroom, newroomname);
+               strcpy(irlp->irl_oldroom, qrbuf->QRname);
+               irlp->irl_newfloor = newfloor;
+               irlp->next = *(irlparms->irl);
+               *(irlparms->irl) = irlp;
+       }
+}
+       
+
 /*
  * Implements the RENAME command
  *
@@ -904,49 +1056,15 @@ void imap_rename(int num_parms, char *parms[]) {
        int oldr, newr;
        int new_floor;
        int r;
-
-       /* struct containing list of rooms to rename */
-       struct irl {
-               struct irl *next;
-               char irl_oldroom[ROOMNAMELEN];
-               char irl_newroom[ROOMNAMELEN];
-               int irl_newfloor;
-       };
        struct irl *irl = NULL;         /* the list */
        struct irl *irlp = NULL;        /* scratch pointer */
+       struct irlparms irlparms;
 
-       /*
-        * Back end function for imap_rename()
-        */
-       void imap_rename_backend(struct quickroom *qrbuf, void *data) {
-               char foldername[SIZ];
-               char newfoldername[SIZ];
-               char newroomname[ROOMNAMELEN];
-               int newfloor;
-       
-               imap_mailboxname(foldername, sizeof foldername, qrbuf);
-       
-               if ( (!strncasecmp(foldername, parms[2], strlen(parms[2]))
-                  && (foldername[strlen(parms[2])] == '|')) ) {
-       
-                       sprintf(newfoldername, "%s|%s",
-                               parms[3],
-                               &foldername[strlen(parms[2])+1]
-                       );
-       
-                       newfloor = imap_roomname(newroomname,
-                               sizeof newroomname, newfoldername) & 0xFF;
-
-                       irlp = (struct irl *) mallok(sizeof(struct irl));
-                       strcpy(irlp->irl_newroom, newroomname);
-                       strcpy(irlp->irl_oldroom, qrbuf->QRname);
-                       irlp->irl_newfloor = newfloor;
-                       irlp->next = irl;
-                       irl = irlp;
-
-               }
+       if (strchr(parms[3], '\\') != NULL) {
+               cprintf("%s NO Invalid character in folder name\r\n", parms[0]);
+               return;
        }
-       
+
        oldr = imap_roomname(old_room, sizeof old_room, parms[2]);
        newr = imap_roomname(new_room, sizeof new_room, parms[3]);
        new_floor = (newr & 0xFF);
@@ -991,7 +1109,10 @@ void imap_rename(int num_parms, char *parms[]) {
 
        /* Otherwise, do the subfolders.  Build a list of rooms to rename... */
        else {
-               ForEachRoom(imap_rename_backend, NULL);
+               irlparms.oldname = parms[2];
+               irlparms.newname = parms[3];
+               irlparms.irl = &irl;
+               ForEachRoom(imap_rename_backend, (void *)&irlparms);
 
                /* ... and now rename them. */
                while (irl != NULL) {
@@ -1099,6 +1220,12 @@ void imap_command_loop(void) {
                imap_capability(num_parms, parms);
        }
 
+#ifdef HAVE_OPENSSL
+       else if (!strcasecmp(parms[1], "STARTTLS")) {
+               imap_starttls(num_parms, parms);
+       }
+#endif
+
        else if (!CC->logged_in) {
                cprintf("%s BAD Not logged in.\r\n", parms[0]);
        }
@@ -1214,10 +1341,9 @@ void imap_command_loop(void) {
 
 
 /*
- * This function is called by dynloader.c to register the IMAP module
- * with the Citadel server.
+ * This function is called to register the IMAP extension with Citadel.
  */
-char *Dynamic_Module_Init(void)
+char *serv_imap_init(void)
 {
        SYM_IMAP = CtdlGetDynamicSymbol();
        CtdlRegisterServiceHook(config.c_imap_port,