Moved to new module init structure.
[citadel.git] / citadel / serv_imap.c
index 452ddefcb84dc7b0a8ef7689a28878bdca9258ea..be4132ea3cf4698ee7df433de28484e165872535 100644 (file)
@@ -2,7 +2,7 @@
  * $Id$ 
  *
  * IMAP server for the Citadel system
- * Copyright (C) 2000-2006 by Art Cancro and others.
+ * Copyright (C) 2000-2007 by Art Cancro and others.
  * This code is released under the terms of the GNU General Public License.
  *
  * WARNING: the IMAP protocol is badly designed.  No implementation of it
 #include <limits.h>
 #include "citadel.h"
 #include "server.h"
-#include "sysdep_decls.h"
 #include "citserver.h"
 #include "support.h"
 #include "config.h"
-#include "serv_extensions.h"
 #include "room_ops.h"
 #include "user_ops.h"
 #include "policy.h"
 #include "internet_addressing.h"
 #include "serv_imap.h"
 #include "imap_tools.h"
+#include "imap_list.h"
 #include "imap_fetch.h"
 #include "imap_search.h"
 #include "imap_store.h"
+#include "imap_acl.h"
+#include "imap_metadata.h"
 #include "imap_misc.h"
 
 #ifdef HAVE_OPENSSL
 #include "serv_crypto.h"
 #endif
 
+
+#include "ctdl_module.h"
+
+
 /* imap_rename() uses this struct containing list of rooms to rename */
 struct irl {
        struct irl *next;
@@ -434,10 +439,19 @@ void imap_cleanup_function(void)
  * output this stuff in other places as well)
  */
 void imap_output_capability_string(void) {
-       cprintf("CAPABILITY IMAP4REV1 NAMESPACE ID AUTH=LOGIN");
+       cprintf("CAPABILITY IMAP4REV1 NAMESPACE ID ACL AUTH=PLAIN AUTH=LOGIN");
+
 #ifdef HAVE_OPENSSL
        if (!CC->redirect_ssl) cprintf(" STARTTLS");
 #endif
+
+       /* We are building a partial implementation of METADATA for the sole purpose
+        * of interoperating with the ical/vcard version of the Bynari Insight Connector.
+        * If you were expecting something else, comment out one or both of these
+        * extension advertisements.
+        */
+       cprintf(" METADATA");
+       /* cprintf(" LIST-EXTENDED"); */
 }
 
 /*
@@ -510,7 +524,7 @@ void imap_login(int num_parms, char *parms[])
                return;
        }
 
-       if (CtdlLoginExistingUser(parms[2]) == login_ok) {
+       if (CtdlLoginExistingUser(NULL, parms[2]) == login_ok) {
                if (CtdlTryPassword(parms[3]) == pass_ok) {
                        cprintf("%s OK [", parms[0]);
                        imap_output_capability_string();
@@ -549,18 +563,58 @@ void imap_authenticate(int num_parms, char *parms[])
                return;
        }
 
+       if (!strcasecmp(parms[2], "PLAIN")) {
+               // CtdlEncodeBase64(buf, "Username:", 9);
+               // cprintf("+ %s\r\n", buf);
+               cprintf("+ \r\n");
+               IMAP->authstate = imap_as_expecting_plainauth;
+               strcpy(IMAP->authseq, parms[0]);
+               return;
+       }
+
        else {
                cprintf("%s NO AUTHENTICATE %s failed\r\n",
                        parms[0], parms[1]);
        }
 }
 
+void imap_auth_plain(char *cmd)
+{
+       char decoded_authstring[1024];
+       char ident[256];
+       char user[256];
+       char pass[256];
+       int result;
+
+       CtdlDecodeBase64(decoded_authstring, cmd, strlen(cmd));
+       safestrncpy(ident, decoded_authstring, sizeof ident);
+       safestrncpy(user, &decoded_authstring[strlen(ident) + 1], sizeof user);
+       safestrncpy(pass, &decoded_authstring[strlen(ident) + strlen(user) + 2], sizeof pass);
+
+       IMAP->authstate = imap_as_normal;
+
+       if (strlen(ident) > 0) {
+               result = CtdlLoginExistingUser(user, ident);
+       }
+       else {
+               result = CtdlLoginExistingUser(NULL, user);
+       }
+
+       if (result == login_ok) {
+               if (CtdlTryPassword(pass) == pass_ok) {
+                       cprintf("%s OK authentication succeeded\r\n", IMAP->authseq);
+                       return;
+               }
+       }
+       cprintf("%s NO authentication failed\r\n", IMAP->authseq);
+}
+
 void imap_auth_login_user(char *cmd)
 {
        char buf[SIZ];
 
        CtdlDecodeBase64(buf, cmd, SIZ);
-       CtdlLoginExistingUser(buf);
+       CtdlLoginExistingUser(NULL, buf);
        CtdlEncodeBase64(buf, "Password:", 9);
        cprintf("+ %s\r\n", buf);
        IMAP->authstate = imap_as_expecting_password;
@@ -573,8 +627,7 @@ void imap_auth_login_pass(char *cmd)
 
        CtdlDecodeBase64(buf, cmd, SIZ);
        if (CtdlTryPassword(buf) == pass_ok) {
-               cprintf("%s OK authentication succeeded\r\n",
-                       IMAP->authseq);
+               cprintf("%s OK authentication succeeded\r\n", IMAP->authseq);
        } else {
                cprintf("%s NO authentication failed\r\n", IMAP->authseq);
        }
@@ -727,7 +780,7 @@ int imap_do_expunge(void)
                        }
                }
                if (num_delmsgs > 0) {
-                       CtdlDeleteMessages(CC->room.QRname, delmsgs, num_delmsgs, "", 1);
+                       CtdlDeleteMessages(CC->room.QRname, delmsgs, num_delmsgs, "");
                }
                num_expunged += num_delmsgs;
                free(delmsgs);
@@ -813,133 +866,6 @@ void imap_namespace(int num_parms, char *parms[])
 
 
 
-/*
- * Used by LIST and LSUB to show the floors in the listing
- */
-void imap_list_floors(char *cmd, char *pattern)
-{
-       int i;
-       struct floor *fl;
-
-       for (i = 0; i < MAXFLOORS; ++i) {
-               fl = cgetfloor(i);
-               if (fl->f_flags & F_INUSE) {
-                       if (imap_mailbox_matches_pattern
-                           (pattern, fl->f_name)) {
-                               cprintf("* %s (\\NoSelect) \"/\" ", cmd);
-                               imap_strout(fl->f_name);
-                               cprintf("\r\n");
-                       }
-               }
-       }
-}
-
-
-
-/*
- * Back end for imap_lsub()
- *
- * 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 ctdlroom *qrbuf, void *data)
-{
-       char buf[SIZ];
-       int ra;
-       char *pattern;
-
-       pattern = (char *) data;
-
-       /* Only list rooms to which the user has access!! */
-       CtdlRoomAccess(qrbuf, &CC->user, &ra, NULL);
-       if (ra & UA_KNOWN) {
-               imap_mailboxname(buf, sizeof buf, qrbuf);
-               if (imap_mailbox_matches_pattern(pattern, buf)) {
-                       cprintf("* LSUB () \"/\" ");
-                       imap_strout(buf);
-                       cprintf("\r\n");
-               }
-       }
-}
-
-
-/*
- * Implements the LSUB command
- */
-void imap_lsub(int num_parms, char *parms[])
-{
-       char pattern[SIZ];
-       if (num_parms < 4) {
-               cprintf("%s BAD arguments invalid\r\n", parms[0]);
-               return;
-       }
-       snprintf(pattern, sizeof pattern, "%s%s", parms[2], parms[3]);
-
-       if (strlen(parms[3]) == 0) {
-               cprintf("* LIST (\\Noselect) \"/\" \"\"\r\n");
-       }
-
-       else {
-               imap_list_floors("LSUB", pattern);
-               ForEachRoom(imap_lsub_listroom, pattern);
-       }
-
-       cprintf("%s OK LSUB completed\r\n", parms[0]);
-}
-
-
-
-/*
- * Back end for imap_list()
- */
-void imap_list_listroom(struct ctdlroom *qrbuf, void *data)
-{
-       char buf[SIZ];
-       int ra;
-       char *pattern;
-
-       pattern = (char *) data;
-
-       /* Only list rooms to which the user has access!! */
-       CtdlRoomAccess(qrbuf, &CC->user, &ra, NULL);
-       if ((ra & UA_KNOWN)
-           || ((ra & UA_GOTOALLOWED) && (ra & UA_ZAPPED))) {
-               imap_mailboxname(buf, sizeof buf, qrbuf);
-               if (imap_mailbox_matches_pattern(pattern, buf)) {
-                       cprintf("* LIST () \"/\" ");
-                       imap_strout(buf);
-                       cprintf("\r\n");
-               }
-       }
-}
-
-
-/*
- * Implements the LIST command
- */
-void imap_list(int num_parms, char *parms[])
-{
-       char pattern[SIZ];
-       if (num_parms < 4) {
-               cprintf("%s BAD arguments invalid\r\n", parms[0]);
-               return;
-       }
-       snprintf(pattern, sizeof pattern, "%s%s", parms[2], parms[3]);
-
-       if (strlen(parms[3]) == 0) {
-               cprintf("* LIST (\\Noselect) \"/\" \"\"\r\n");
-       }
-
-       else {
-               imap_list_floors("LIST", pattern);
-               ForEachRoom(imap_list_listroom, pattern);
-       }
-
-       cprintf("%s OK LIST completed\r\n", parms[0]);
-}
-
-
-
 /*
  * Implements the CREATE command
  *
@@ -991,9 +917,8 @@ void imap_create(int num_parms, char *parms[])
 
        ret = create_room(roomname, newroomtype, "", floornum, 1, 0, newroomview);
        if (ret == 0) {
-               cprintf
-                   ("%s NO Mailbox already exists, or create failed\r\n",
-                    parms[0]);
+               /*** DO NOT CHANGE THIS ERROR MESSAGE IN ANY WAY!  BYNARI CONNECTOR DEPENDS ON IT! ***/
+               cprintf("%s NO Mailbox already exists, or create failed\r\n", parms[0]);
        } else {
                cprintf("%s OK CREATE completed\r\n", parms[0]);
        }
@@ -1374,8 +1299,7 @@ void imap_rename(int num_parms, char *parms[])
                                           irl->irl_newfloor);
                        if (r != crr_ok) {
                                /* FIXME handle error returns better */
-                               lprintf(CTDL_ERR, "CtdlRenameRoom() error %d\n",
-                                       r);
+                               lprintf(CTDL_ERR, "CtdlRenameRoom() error %d\n", r);
                        }
                        irlp = irl;
                        irl = irl->next;
@@ -1397,7 +1321,10 @@ void imap_command_loop(void)
        char cmdbuf[SIZ];
        char *parms[SIZ];
        int num_parms;
+       struct timeval tv1, tv2;
+       suseconds_t total_time = 0;
 
+       gettimeofday(&tv1, NULL);
        CC->lastcmd = time(NULL);
        memset(cmdbuf, 0, sizeof cmdbuf);       /* Clear it, just in case */
        flush_output();
@@ -1410,6 +1337,9 @@ void imap_command_loop(void)
        if (IMAP->authstate == imap_as_expecting_password) {
                lprintf(CTDL_INFO, "IMAP: <password>\n");
        }
+       else if (IMAP->authstate == imap_as_expecting_plainauth) {
+               lprintf(CTDL_INFO, "IMAP: <plain_auth>\n");
+       }
        else if (bmstrcasestr(cmdbuf, " LOGIN ")) {
                lprintf(CTDL_INFO, "IMAP: LOGIN...\n");
        }
@@ -1432,6 +1362,10 @@ void imap_command_loop(void)
                imap_auth_login_user(cmdbuf);
                return;
        }
+       if (IMAP->authstate == imap_as_expecting_plainauth) {
+               imap_auth_plain(cmdbuf);
+               return;
+       }
        if (IMAP->authstate == imap_as_expecting_password) {
                imap_auth_login_pass(cmdbuf);
                return;
@@ -1464,7 +1398,7 @@ void imap_command_loop(void)
 
        else if ((!strcasecmp(parms[1], "NOOP"))
                 || (!strcasecmp(parms[1], "CHECK"))) {
-               cprintf("%s OK This command successfully did nothing.\r\n",
+               cprintf("%s OK No operation\r\n",
                        parms[0]);
        }
 
@@ -1478,7 +1412,7 @@ void imap_command_loop(void)
                        imap_do_expunge();      /* yes, we auto-expunge */
                }
                cprintf("* BYE %s logging out\r\n", config.c_fqdn);
-               cprintf("%s OK thank you for using Citadel IMAP\r\n",
+               cprintf("%s OK Citadel IMAP session ended.\r\n",
                        parms[0]);
                CC->kill_me = 1;
                return;
@@ -1515,7 +1449,7 @@ void imap_command_loop(void)
        }
 
        else if (!strcasecmp(parms[1], "LSUB")) {
-               imap_lsub(num_parms, parms);
+               imap_list(num_parms, parms);
        }
 
        else if (!strcasecmp(parms[1], "LIST")) {
@@ -1554,6 +1488,34 @@ void imap_command_loop(void)
                imap_namespace(num_parms, parms);
        }
 
+       else if (!strcasecmp(parms[1], "SETACL")) {
+               imap_setacl(num_parms, parms);
+       }
+
+       else if (!strcasecmp(parms[1], "DELETEACL")) {
+               imap_deleteacl(num_parms, parms);
+       }
+
+       else if (!strcasecmp(parms[1], "GETACL")) {
+               imap_getacl(num_parms, parms);
+       }
+
+       else if (!strcasecmp(parms[1], "LISTRIGHTS")) {
+               imap_listrights(num_parms, parms);
+       }
+
+       else if (!strcasecmp(parms[1], "MYRIGHTS")) {
+               imap_myrights(num_parms, parms);
+       }
+
+       else if (!strcasecmp(parms[1], "GETMETADATA")) {
+               imap_getmetadata(num_parms, parms);
+       }
+
+       else if (!strcasecmp(parms[1], "SETMETADATA")) {
+               imap_setmetadata(num_parms, parms);
+       }
+
        else if (IMAP->selected == 0) {
                cprintf("%s BAD no folder selected\r\n", parms[0]);
        }
@@ -1614,14 +1576,20 @@ void imap_command_loop(void)
 
        /* If the client transmitted a message we can free it now */
        imap_free_transmitted_message();
-}
 
+       gettimeofday(&tv2, NULL);
+       total_time = (tv2.tv_usec + (tv2.tv_sec * 1000000)) - (tv1.tv_usec + (tv1.tv_sec * 1000000));
+       lprintf(CTDL_DEBUG, "IMAP command completed in %ld.%ld seconds\n",
+               (total_time / 1000000),
+               (total_time % 1000000)
+       );
+}
 
 
 /*
  * This function is called to register the IMAP extension with Citadel.
  */
-char *serv_imap_init(void)
+CTDL_MODULE_INIT(imap)
 {
        CtdlRegisterServiceHook(config.c_imap_port,
                                NULL, imap_greeting, imap_command_loop, NULL);
@@ -1630,5 +1598,7 @@ char *serv_imap_init(void)
                                NULL, imaps_greeting, imap_command_loop, NULL);
 #endif
        CtdlRegisterSessionHook(imap_cleanup_function, EVT_STOP);
+
+       /* return our Subversion id for the Log */
        return "$Id$";
 }