]> code.citadel.org Git - citadel.git/blobdiff - citadel/server/modules/imap/serv_imap.c
Where gettimeofday() is required, include both time.h and sys/time.h
[citadel.git] / citadel / server / modules / imap / serv_imap.c
index 43d1eff7ac8fa9ce23fb633f77dacf803c013ffb..d55971bb5f76de414b88cd4122b3bf9ad3ea2d8a 100644 (file)
@@ -1,21 +1,9 @@
 // IMAP server for the Citadel system
 //
-// Copyright (C) 2000-2022 by Art Cancro and others.
-// This code is released under the terms of the GNU General Public License.
+// Copyright (c) 1987-2023 by the citadel.org team
 //
-// WARNING: the IMAP protocol is badly designed.  No implementation of it
-// is perfect.  Indeed, with so much gratuitous complexity, *all* IMAP
-// implementations have bugs.
-//
-// 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.
-//
-// This program is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-// GNU General Public License for more details.
+// This program is open source software.  Use, duplication, or disclosure
+// is subject to the terms of the GNU General Public License, version 3.
 
 #include "../../sysdep.h"
 #include <stdlib.h>
 #include <errno.h>
 #include <sys/types.h>
 #include <time.h>
+#include <sys/time.h>
 #include <sys/wait.h>
 #include <ctype.h>
 #include <string.h>
 #include <limits.h>
 #include <libcitadel.h>
-#include "../../citadel.h"
+#include "../../citadel_defs.h"
 #include "../../server.h"
 #include "../../citserver.h"
 #include "../../support.h"
 #include "../../config.h"
 #include "../../user_ops.h"
+#include "../../room_ops.h"
 #include "../../database.h"
 #include "../../msgbase.h"
 #include "../../internet_addressing.h"
 
 #include "../../ctdl_module.h"
 HashList *ImapCmds = NULL;
-void registerImapCMD(const char *First, long FLen, 
-                    const char *Second, long SLen,
-                    imap_handler H,
-                    int Flags)
-{
+void registerImapCMD(const char *First, long FLen, const char *Second, long SLen, imap_handler H, int Flags) {
        imap_handler_hook *h;
 
        h = (imap_handler_hook*) malloc(sizeof(imap_handler_hook));
@@ -78,11 +64,9 @@ void registerImapCMD(const char *First, long FLen,
 }
 
 
-const imap_handler_hook *imap_lookup(int num_parms, ConstStr *Params)
-{
-       struct CitContext *CCC = CC;
+const imap_handler_hook *imap_lookup(int num_parms, ConstStr *Params) {
        void *v;
-       citimap *Imap = CCCIMAP;
+       citimap *Imap = IMAP;
 
        if (num_parms < 1)
                return NULL;
@@ -92,15 +76,13 @@ const imap_handler_hook *imap_lookup(int num_parms, ConstStr *Params)
        StrBufUpCase(Imap->Reply);
 
        syslog(LOG_DEBUG, "---- Looking up [%s] -----", ChrPtr(Imap->Reply));
-       if (GetHash(ImapCmds, SKEY(Imap->Reply), &v))
-       {
+       if (GetHash(ImapCmds, SKEY(Imap->Reply), &v)) {
                syslog(LOG_DEBUG, "Found."); 
                FlushStrBuf(Imap->Reply);
                return (imap_handler_hook *) v;
        }
 
-       if (num_parms == 1)
-       {
+       if (num_parms == 1) {
                syslog(LOG_DEBUG, "NOT Found."); 
                FlushStrBuf(Imap->Reply);
                return NULL;
@@ -109,8 +91,7 @@ const imap_handler_hook *imap_lookup(int num_parms, ConstStr *Params)
        syslog(LOG_DEBUG, "---- Looking up [%s] -----", ChrPtr(Imap->Reply));
        StrBufAppendBufPlain(Imap->Reply, CKEY(Params[2]), 0);
        StrBufUpCase(Imap->Reply);
-       if (GetHash(ImapCmds, SKEY(Imap->Reply), &v))
-       {
+       if (GetHash(ImapCmds, SKEY(Imap->Reply), &v)) {
                syslog(LOG_DEBUG, "Found."); 
                FlushStrBuf(Imap->Reply);
                return (imap_handler_hook *) v;
@@ -120,6 +101,7 @@ const imap_handler_hook *imap_lookup(int num_parms, ConstStr *Params)
                return NULL;
 }
 
+
 /* imap_rename() uses this struct containing list of rooms to rename */
 struct irl {
        struct irl *next;
@@ -128,6 +110,7 @@ struct irl {
        int irl_newfloor;
 };
 
+
 /* Data which is passed between imap_rename() and imap_rename_backend() */
 typedef struct __irlparms {
        const char *oldname;
@@ -135,14 +118,13 @@ typedef struct __irlparms {
        const char *newname;
        long newnamelen;
        struct irl **irl;
-}irlparms;
+} irlparms;
 
 
 /*
  * If there is a message ID map in memory, free it
  */
-void imap_free_msgids(void)
-{
+void imap_free_msgids(void) {
        citimap *Imap = IMAP;
        if (Imap->msgids != NULL) {
                free(Imap->msgids);
@@ -178,14 +160,14 @@ void imap_free_transmitted_message(void) {
  */
 void imap_set_seen_flags(int first_msg) {
        citimap *Imap = IMAP;
-       visit vbuf;
+       struct visit vbuf;
        int i;
        int num_sets;
        int s;
        char setstr[64], lostr[64], histr[64];
        long lo, hi;
 
-       if (Imap->num_msgs < 1) return;
+       if (Imap->num_msgs < 0) return;
        CtdlGetRelationship(&vbuf, &CC->user, &CC->room);
 
        for (i = first_msg; i < Imap->num_msgs; ++i) {
@@ -251,7 +233,6 @@ void imap_set_seen_flags(int first_msg) {
 }
 
 
-
 /*
  * Back end for imap_load_msgids()
  *
@@ -273,14 +254,11 @@ void imap_add_single_msgid(long msgnum, void *userdata) {
 }
 
 
-
 /*
  * Set up a message ID map for the current room (folder)
  */
 void imap_load_msgids(void) {
-       struct CitContext *CCC = CC;
-       struct cdbdata *cdbfr;
-       citimap *Imap = CCCIMAP;
+       citimap *Imap = IMAP;
 
        if (Imap->selected == 0) {
                syslog(LOG_ERR, "imap_load_msgids() can't run; no room selected");
@@ -290,21 +268,13 @@ void imap_load_msgids(void) {
        imap_free_msgids();     /* If there was already a map, free it */
 
        /* Load the message list */
-       cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
-       if (cdbfr != NULL) {
-               Imap->msgids = (long*)cdbfr->ptr;
-               Imap->num_msgs = cdbfr->len / sizeof(long);
-               Imap->num_alloc = cdbfr->len / sizeof(long);
-               cdbfr->ptr = NULL;
-               cdbfr->len = 0;
-               cdb_free(cdbfr);
-       }
+       Imap->num_msgs = CtdlFetchMsgList(CC->room.QRnumber, &Imap->msgids);
+       Imap->num_alloc = Imap->num_msgs;
 
        if (Imap->num_msgs) {
                Imap->flags = malloc(Imap->num_alloc * sizeof(unsigned int));
                memset(Imap->flags, 0, (Imap->num_alloc * sizeof(unsigned int)) );
        }
-
        imap_set_seen_flags(0);
 }
 
@@ -313,13 +283,11 @@ void imap_load_msgids(void) {
  * Re-scan the selected room (folder) and see if it's been changed at all
  */
 void imap_rescan_msgids(void) {
-       struct CitContext *CCC = CC;
-       citimap *Imap = CCCIMAP;
+       citimap *Imap = IMAP;
        int original_num_msgs = 0;
        long original_highest = 0L;
        int i, j, jstart;
        int message_still_exists;
-       struct cdbdata *cdbfr;
        long *msglist = NULL;
        int num_msgs = 0;
        int num_recent = 0;
@@ -341,16 +309,7 @@ void imap_rescan_msgids(void) {
        /* 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 = (long*)cdbfr->ptr;
-               cdbfr->ptr = NULL;
-               num_msgs = cdbfr->len / sizeof(long);
-               cdbfr->len = 0;
-               cdb_free(cdbfr);
-       } else {
-               num_msgs = 0;
-       }
+       num_msgs = CtdlFetchMsgList(CC->room.QRnumber, &msglist);
 
        /*
         * Check to see if any of the messages we know about have been expunged
@@ -419,9 +378,7 @@ void imap_rescan_msgids(void) {
                IAPrintf("* %d RECENT\r\n", num_recent);
        }
 
-       if (msglist != NULL) {
-               free(msglist);
-       }
+       free(msglist);
        Imap->last_mtime = CC->room.QRmtime;
 }
 
@@ -431,8 +388,7 @@ void imap_rescan_msgids(void) {
  * the IMAP server.
  */
 void imap_cleanup_function(void) {
-       struct CitContext *CCC = CC;
-       citimap *Imap = CCCIMAP;
+       citimap *Imap = IMAP;
 
        /* Don't do this stuff if this is not a Imap session! */
        if (CC->h_command_function != imap_command_loop)
@@ -468,8 +424,7 @@ void imap_cleanup_function(void) {
 
 
 /*
- * Does the actual work of the CAPABILITY command (because we need to
- * output this stuff in other places as well)
+ * Does the actual work of the CAPABILITY command (because we need to output this stuff in other places as well)
  */
 void imap_output_capability_string(void) {
        IAPuts("CAPABILITY IMAP4REV1 NAMESPACE ID AUTH=PLAIN AUTH=LOGIN UIDPLUS");
@@ -532,20 +487,19 @@ void imap_id(int num_parms, ConstStr *Params) {
  */
 void imap_greeting(void) {
        citimap *Imap;
-       CitContext *CCC = CC;
 
-       strcpy(CCC->cs_clientname, "IMAP session");
-       CCC->session_specific_data = malloc(sizeof(citimap));
-       Imap = (citimap *)CCC->session_specific_data;
+       strcpy(CC->cs_clientname, "IMAP session");
+       CC->session_specific_data = malloc(sizeof(citimap));
+       Imap = (citimap *)CC->session_specific_data;
        memset(Imap, 0, sizeof(citimap));
        Imap->authstate = imap_as_normal;
        Imap->cached_rfc822_msgnum = (-1);
        Imap->cached_rfc822_withbody = 0;
        Imap->Reply = NewStrBufPlain(NULL, SIZ * 10); /* 40k */
 
-       if (CCC->nologin) {
+       if (CC->nologin) {
                IAPuts("* BYE; Server busy, try later\r\n");
-               CCC->kill_me = KILLME_NOLOGIN;
+               CC->kill_me = KILLME_NOLOGIN;
                IUnbuffer();
                return;
        }
@@ -576,7 +530,7 @@ void imap_login(int num_parms, ConstStr *Params) {
 
        switch (num_parms) {
        case 3:
-               if (Params[2].Key[0] == '{') {
+               if (Params[2].len && (Params[2].Key[0] == '{')) {
                        IAPuts("+ go ahead\r\n");
                        IMAP->authstate = imap_as_expecting_multilineusername;
                        strcpy(IMAP->authseq, Params[0].Key);
@@ -595,8 +549,7 @@ void imap_login(int num_parms, ConstStr *Params) {
                                IAPrintf("] Hello, %s\r\n", CC->user.fullname);
                                return;
                        }
-                       else
-                       {
+                       else {
                                IReplyPrintf("NO AUTHENTICATE %s failed", Params[3].Key);
                                return;
                        }
@@ -876,10 +829,7 @@ void imap_select(int num_parms, ConstStr *Params) {
         */
        IAPuts("* FLAGS (\\Deleted \\Seen \\Answered)\r\n");
        IAPuts("* OK [PERMANENTFLAGS (\\Deleted \\Seen \\Answered)] permanent flags\r\n");
-
-       IReplyPrintf("OK [%s] %s completed",
-               (Imap->readonly ? "READ-ONLY" : "READ-WRITE"), Params[1].Key
-       );
+       IReplyPrintf("OK [%s] %s completed", (Imap->readonly ? "READ-ONLY" : "READ-WRITE"), Params[1].Key);
 }
 
 
@@ -887,8 +837,7 @@ void imap_select(int num_parms, ConstStr *Params) {
  * Does the real work for expunge.
  */
 int imap_do_expunge(void) {
-       struct CitContext *CCC = CC;
-       citimap *Imap = CCCIMAP;
+       citimap *Imap = IMAP;
        int i;
        int num_expunged = 0;
        long *delmsgs = NULL;
@@ -1088,8 +1037,7 @@ int imap_grabroom(char *returned_roomname, const char *foldername, int zapped_ok
 
        /* Then try a mailbox name match */
        if (c != 0) {
-               CtdlMailboxName(augmented_roomname, sizeof augmented_roomname,
-                           &CC->user, roomname);
+               CtdlMailboxName(augmented_roomname, sizeof augmented_roomname, &CC->user, roomname);
                c = CtdlGetRoom(&QRscratch, augmented_roomname);
                if (c == 0)
                        safestrncpy(roomname, augmented_roomname, sizeof(roomname));
@@ -1113,7 +1061,8 @@ int imap_grabroom(char *returned_roomname, const char *foldername, int zapped_ok
        if (!ok) {
                strcpy(returned_roomname, "");
                return (2);
-       } else {
+       }
+       else {
                safestrncpy(returned_roomname, QRscratch.QRname, ROOMNAMELEN);
                return (0);
        }
@@ -1247,7 +1196,8 @@ void imap_unsubscribe(int num_parms, ConstStr *Params) {
         */
        if (CtdlForgetThisRoom() == 0) {
                IReply("OK UNSUBSCRIBE completed");
-       } else {
+       }
+       else {
                IReply("NO You may not unsubscribe from this folder.");
        }
 
@@ -1293,7 +1243,8 @@ void imap_delete(int num_parms, ConstStr *Params) {
        if (CtdlDoIHavePermissionToDeleteThisRoom(&CC->room)) {
                CtdlScheduleRoomForDeletion(&CC->room);
                IReply("OK DELETE completed");
-       } else {
+       }
+       else {
                IReply("NO Can't delete this folder.");
        }
 
@@ -1405,9 +1356,7 @@ void imap_rename(int num_parms, ConstStr *Params) {
 
                /* ... and now rename them. */
                while (irl != NULL) {
-                       r = CtdlRenameRoom(irl->irl_oldroom,
-                                          irl->irl_newroom,
-                                          irl->irl_newfloor);
+                       r = CtdlRenameRoom(irl->irl_oldroom, irl->irl_newroom, irl->irl_newfloor);
                        if (r != crr_ok) {
                                /* FIXME handle error returns better */
                                syslog(LOG_ERR, "CtdlRenameRoom() error %d", r);
@@ -1433,7 +1382,6 @@ void imap_rename(int num_parms, ConstStr *Params) {
  * Main command loop for IMAP sessions.
  */
 void imap_command_loop(void) {
-       struct CitContext *CCC = CC;
        struct timeval tv1, tv2;
        suseconds_t total_time = 0;
        citimap *Imap;
@@ -1441,8 +1389,8 @@ void imap_command_loop(void) {
        const imap_handler_hook *h;
 
        gettimeofday(&tv1, NULL);
-       CCC->lastcmd = time(NULL);
-       Imap = CCCIMAP;
+       CC->lastcmd = time(NULL);
+       Imap = IMAP;
 
        flush_output();
        if (Imap->Cmd.CmdBuf == NULL)
@@ -1462,8 +1410,7 @@ void imap_command_loop(void) {
        else if (Imap->authstate == imap_as_expecting_plainauth) {
                syslog(LOG_INFO, "<plain_auth>");
        }
-       else if ((Imap->authstate == imap_as_expecting_multilineusername) || 
-                cbmstrcasestr(ChrPtr(Imap->Cmd.CmdBuf), " LOGIN ")) {
+       else if ((Imap->authstate == imap_as_expecting_multilineusername) || cbmstrcasestr(ChrPtr(Imap->Cmd.CmdBuf), " LOGIN ")) {
                syslog(LOG_INFO, "LOGIN...");
        }
        else {